diff options
author | Owen Anderson <resistor@mac.com> | 2015-03-08 21:53:59 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2015-03-08 21:53:59 +0000 |
commit | c03496d4d02ee1d560a80c323e5bbef7b3bd7320 (patch) | |
tree | a3db4dd61c103b6e964a3e3d7dd63d2e0f767505 | |
parent | 692f7382b5d50099a6112ac2d11f769d9589422a (diff) |
Teach DataLayout to infer a plausible alignment for things even when nothing is specified by the user.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231613 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/IR/DataLayout.cpp | 17 | ||||
-rw-r--r-- | test/Transforms/InstCombine/default-alignment.ll | 10 |
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index c70d7c68a91..5dcb5fbf490 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -479,9 +479,7 @@ unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, // If we didn't find an integer alignment, fall back on most conservative. if (AlignType == INTEGER_ALIGN) { BestMatchIdx = LargestInt; - } else { - assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!"); - + } else if (AlignType == VECTOR_ALIGN) { // By default, use natural alignment for vector types. This is consistent // with what clang and llvm-gcc do. unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); @@ -494,6 +492,19 @@ unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, } } + // If we still couldn't find a reasonable default alignment, fall back + // to a simple heuristic that the alignment is the first power of two + // greater-or-equal to the store size of the type. This is a reasonable + // approximation of reality, and if the user wanted something less + // less conservative, they should have specified it explicitly in the data + // layout. + if (BestMatchIdx == -1) { + unsigned Align = getTypeStoreSize(Ty); + if (Align & (Align-1)) + Align = NextPowerOf2(Align); + return Align; + } + // Since we got a "best match" index, just return it. return ABIInfo ? Alignments[BestMatchIdx].ABIAlign : Alignments[BestMatchIdx].PrefAlign; diff --git a/test/Transforms/InstCombine/default-alignment.ll b/test/Transforms/InstCombine/default-alignment.ll new file mode 100644 index 00000000000..718da213671 --- /dev/null +++ b/test/Transforms/InstCombine/default-alignment.ll @@ -0,0 +1,10 @@ +; RUN: opt -verify -instcombine < %s +%Foo = type <{ i8, x86_fp80 }> + +define i8 @t(%Foo* %arg) { +entry: + %0 = getelementptr %Foo, %Foo* %arg, i32 0, i32 0 + %1 = load i8, i8* %0, align 1 + ret i8 %1 +} + |