llvm.org GIT mirror llvm / 3cb3650
Change uses of getTypeSize to getABITypeSize, getTypeStoreSize or getTypeSizeInBits as appropriate in ScalarReplAggregates. The right change to make was not always obvious, so it would be good to have an sroa guru review this. While there I noticed some bugs, and fixed them: (1) arrays of x86 long double have holes due to alignment padding, but this wasn't being spotted by HasStructPadding (renamed to HasPadding). The same goes for arrays of oddly sized ints. Vectors also suffer from this, in fact the problem for vectors is much worse because basic vector assumptions seem to be broken by vectors of type with alignment padding. I didn't try to fix any of these vector problems. (2) The code for extracting smaller integers from larger ones (in the "int union" case) was wrong on big-endian machines for integers with size not a multiple of 8, like i1. Probably this is impossible to hit via llvm-gcc, but I fixed it anyway while there and added a testcase. I also got rid of some trailing whitespace and changed a function name which had an obvious typo in it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43672 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 12 years ago
3 changed file(s) with 92 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
276276 assert(Idx < NumElements && "Invalid element idx!");
277277 return MemberOffsets[Idx];
278278 }
279
279
280 uint64_t getElementOffsetInBits(unsigned Idx) const {
281 return getElementOffset(Idx)*8;
282 }
283
280284 private:
281285 friend class TargetData; // Only TargetData can create this class
282286 StructLayout(const StructType *ST, const TargetData &TD);
219219 (isa(AI->getAllocatedType()) ||
220220 isa(AI->getAllocatedType())) &&
221221 AI->getAllocatedType()->isSized() &&
222 TD.getTypeSize(AI->getAllocatedType()) < SRThreshold) {
222 TD.getABITypeSize(AI->getAllocatedType()) < SRThreshold) {
223223 // Check that all of the users of the allocation are capable of being
224224 // transformed.
225225 switch (isSafeAllocaToScalarRepl(AI)) {
518518
519519 // If not the whole aggregate, give up.
520520 const TargetData &TD = getAnalysis();
521 if (Length->getZExtValue() != TD.getTypeSize(AI->getType()->getElementType()))
521 if (Length->getZExtValue() !=
522 TD.getABITypeSize(AI->getType()->getElementType()))
522523 return MarkUnsafe(Info);
523524
524525 // We only know about memcpy/memset/memmove.
657658 const Type *ValTy = EltTy;
658659 if (const VectorType *VTy = dyn_cast(ValTy))
659660 ValTy = VTy->getElementType();
660
661
661662 // Construct an integer with the right value.
662 unsigned EltSize = TD.getTypeSize(ValTy);
663 APInt OneVal(EltSize*8, CI->getZExtValue());
663 unsigned EltSize = TD.getTypeSizeInBits(ValTy);
664 APInt OneVal(EltSize, CI->getZExtValue());
664665 APInt TotalVal(OneVal);
665666 // Set each byte.
666 for (unsigned i = 0; i != EltSize-1; ++i) {
667 for (unsigned i = 0; 8*i < EltSize; ++i) {
667668 TotalVal = TotalVal.shl(8);
668669 TotalVal |= OneVal;
669670 }
670
671
671672 // Convert the integer value to the appropriate type.
672673 StoreVal = ConstantInt::get(TotalVal);
673674 if (isa(ValTy))
700701 OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(),
701702 MI);
702703
703 unsigned EltSize = TD.getTypeSize(EltTy);
704 unsigned EltSize = TD.getABITypeSize(EltTy);
704705
705706 // Finally, insert the meminst for this element.
706707 if (isa(MI) || isa(MI)) {
729730 }
730731 }
731732
732 /// HasStructPadding - Return true if the specified type has any structure
733 /// padding, false otherwise.
734 static bool HasStructPadding(const Type *Ty, const TargetData &TD) {
733 /// HasPadding - Return true if the specified type has any structure or
734 /// alignment padding, false otherwise.
735 static bool HasPadding(const Type *Ty, const TargetData &TD) {
735736 if (const StructType *STy = dyn_cast(Ty)) {
736737 const StructLayout *SL = TD.getStructLayout(STy);
737738 unsigned PrevFieldBitOffset = 0;
738739 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
739 unsigned FieldBitOffset = SL->getElementOffset(i)*8;
740
740 unsigned FieldBitOffset = SL->getElementOffsetInBits(i);
741
741742 // Padding in sub-elements?
742 if (HasStructPadding(STy->getElementType(i), TD))
743 if (HasPadding(STy->getElementType(i), TD))
743744 return true;
744
745
745746 // Check to see if there is any padding between this element and the
746747 // previous one.
747748 if (i) {
748 unsigned PrevFieldEnd =
749 unsigned PrevFieldEnd =
749750 PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1));
750751 if (PrevFieldEnd < FieldBitOffset)
751752 return true;
752753 }
753
754
754755 PrevFieldBitOffset = FieldBitOffset;
755756 }
756
757
757758 // Check for tail padding.
758759 if (unsigned EltCount = STy->getNumElements()) {
759760 unsigned PrevFieldEnd = PrevFieldBitOffset +
760761 TD.getTypeSizeInBits(STy->getElementType(EltCount-1));
761 if (PrevFieldEnd < SL->getSizeInBytes()*8)
762 if (PrevFieldEnd < SL->getSizeInBits())
762763 return true;
763764 }
764765
765766 } else if (const ArrayType *ATy = dyn_cast(Ty)) {
766 return HasStructPadding(ATy->getElementType(), TD);
767 }
768 return false;
767 return HasPadding(ATy->getElementType(), TD);
768 } else if (const VectorType *VTy = dyn_cast(Ty)) {
769 return HasPadding(VTy->getElementType(), TD);
770 }
771 return TD.getTypeSizeInBits(Ty) != TD.getABITypeSizeInBits(Ty);
769772 }
770773
771774 /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of
792795 // types, but may actually be used. In these cases, we refuse to promote the
793796 // struct.
794797 if (Info.isMemCpySrc && Info.isMemCpyDst &&
795 HasStructPadding(AI->getType()->getElementType(),
796 getAnalysis()))
798 HasPadding(AI->getType()->getElementType(), getAnalysis()))
797799 return 0;
798
800
799801 // If we require cleanup, return 1, otherwise return 3.
800802 return Info.needsCanon ? 1 : 3;
801803 }
928930 return false;
929931 }
930932
931 /// getUIntAtLeastAsBitAs - Return an unsigned integer type that is at least
933 /// getUIntAtLeastAsBigAs - Return an unsigned integer type that is at least
932934 /// as big as the specified type. If there is no suitable type, this returns
933935 /// null.
934 const Type *getUIntAtLeastAsBitAs(unsigned NumBits) {
936 const Type *getUIntAtLeastAsBigAs(unsigned NumBits) {
935937 if (NumBits > 64) return 0;
936938 if (NumBits > 32) return Type::Int64Ty;
937939 if (NumBits > 16) return Type::Int32Ty;
973975 // Check to see if this is stepping over an element: GEP Ptr, int C
974976 if (GEP->getNumOperands() == 2 && isa(GEP->getOperand(1))) {
975977 unsigned Idx = cast(GEP->getOperand(1))->getZExtValue();
976 unsigned ElSize = TD.getTypeSize(PTy->getElementType());
978 unsigned ElSize = TD.getABITypeSize(PTy->getElementType());
977979 unsigned BitOffset = Idx*ElSize*8;
978980 if (BitOffset > 64 || !isPowerOf2_32(ElSize)) return 0;
979981
982984 if (SubElt == 0) return 0;
983985 if (SubElt != Type::VoidTy && SubElt->isInteger()) {
984986 const Type *NewTy =
985 getUIntAtLeastAsBitAs(TD.getTypeSize(SubElt)*8+BitOffset);
987 getUIntAtLeastAsBigAs(TD.getABITypeSizeInBits(SubElt)+BitOffset);
986988 if (NewTy == 0 || MergeInType(NewTy, UsedType, TD)) return 0;
987989 continue;
988990 }
10191021 } else {
10201022 return 0;
10211023 }
1022 const Type *NTy = getUIntAtLeastAsBitAs(TD.getTypeSize(AggTy)*8);
1024 const Type *NTy = getUIntAtLeastAsBigAs(TD.getABITypeSizeInBits(AggTy));
10231025 if (NTy == 0 || MergeInType(NTy, UsedType, TD)) return 0;
10241026 const Type *SubTy = CanConvertToScalar(GEP, IsNotTrivial);
10251027 if (SubTy == 0) return 0;
10821084 NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI);
10831085 } else {
10841086 // Must be an element access.
1085 unsigned Elt = Offset/(TD.getTypeSize(PTy->getElementType())*8);
1087 unsigned Elt = Offset/TD.getABITypeSizeInBits(PTy->getElementType());
10861088 NV = new ExtractElementInst(
10871089 NV, ConstantInt::get(Type::Int32Ty, Elt), "tmp", LI);
10881090 }
10931095 NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI);
10941096 } else {
10951097 const IntegerType *NTy = cast(NV->getType());
1096 unsigned LIBitWidth = TD.getTypeSizeInBits(LI->getType());
1097
1098
10981099 // If this is a big-endian system and the load is narrower than the
10991100 // full alloca type, we need to do a shift to get the right bits.
11001101 int ShAmt = 0;
11011102 if (TD.isBigEndian()) {
1102 ShAmt = NTy->getBitWidth()-LIBitWidth-Offset;
1103 // On big-endian machines, the lowest bit is stored at the bit offset
1104 // from the pointer given by getTypeStoreSizeInBits. This matters for
1105 // integers with a bitwidth that is not a multiple of 8.
1106 ShAmt = TD.getTypeStoreSizeInBits(NTy) -
1107 TD.getTypeStoreSizeInBits(LI->getType()) - Offset;
11031108 } else {
11041109 ShAmt = Offset;
11051110 }
1106
1111
11071112 // Note: we support negative bitwidths (with shl) which are not defined.
11081113 // We do this to support (f.e.) loads off the end of a structure where
11091114 // only some bits are used.
11171122 LI->getName(), LI);
11181123
11191124 // Finally, unconditionally truncate the integer to the right width.
1125 unsigned LIBitWidth = TD.getTypeSizeInBits(LI->getType());
11201126 if (LIBitWidth < NTy->getBitWidth())
11211127 NV = new TruncInst(NV, IntegerType::get(LIBitWidth),
11221128 LI->getName(), LI);
11501156 // access or a bitcast to another vector type.
11511157 if (isa(SV->getType())) {
11521158 SV = new BitCastInst(SV, AllocaType, SV->getName(), SI);
1153 } else {
1159 } else {
11541160 // Must be an element insertion.
1155 unsigned Elt = Offset/(TD.getTypeSize(PTy->getElementType())*8);
1161 unsigned Elt = Offset/TD.getABITypeSizeInBits(PTy->getElementType());
11561162 SV = new InsertElementInst(Old, SV,
11571163 ConstantInt::get(Type::Int32Ty, Elt),
11581164 "tmp", SI);
11691175 // If it is a pointer, do the same, and also handle ptr->ptr casts
11701176 // here.
11711177 unsigned SrcWidth = TD.getTypeSizeInBits(SV->getType());
1172 unsigned DestWidth = AllocaType->getPrimitiveSizeInBits();
1178 unsigned DestWidth = TD.getTypeSizeInBits(AllocaType);
11731179 if (SV->getType()->isFloatingPoint())
11741180 SV = new BitCastInst(SV, IntegerType::get(SrcWidth),
11751181 SV->getName(), SI);
11791185 // Always zero extend the value if needed.
11801186 if (SV->getType() != AllocaType)
11811187 SV = new ZExtInst(SV, AllocaType, SV->getName(), SI);
1182
1188
11831189 // If this is a big-endian system and the store is narrower than the
11841190 // full alloca type, we need to do a shift to get the right bits.
11851191 int ShAmt = 0;
11861192 if (TD.isBigEndian()) {
1187 ShAmt = DestWidth-SrcWidth-Offset;
1193 // On big-endian machines, the lowest bit is stored at the bit offset
1194 // from the pointer given by getTypeStoreSizeInBits. This matters for
1195 // integers with a bitwidth that is not a multiple of 8.
1196 ShAmt = TD.getTypeStoreSizeInBits(AllocaType) -
1197 TD.getTypeStoreSizeInBits(SV->getType()) - Offset;
11881198 } else {
11891199 ShAmt = Offset;
11901200 }
1191
1201
11921202 // Note: we support negative bitwidths (with shr) which are not defined.
11931203 // We do this to support (f.e.) stores off the end of a structure where
11941204 // only some bits in the structure are set.
12241234 const PointerType *AggPtrTy =
12251235 cast(GEP->getOperand(0)->getType());
12261236 const TargetData &TD = getAnalysis();
1227 unsigned AggSizeInBits = TD.getTypeSize(AggPtrTy->getElementType())*8;
1228
1237 unsigned AggSizeInBits =
1238 TD.getABITypeSizeInBits(AggPtrTy->getElementType());
1239
12291240 // Check to see if this is stepping over an element: GEP Ptr, int C
12301241 unsigned NewOffset = Offset;
12311242 if (GEP->getNumOperands() == 2) {
12381249 unsigned Idx = cast(GEP->getOperand(2))->getZExtValue();
12391250 const Type *AggTy = AggPtrTy->getElementType();
12401251 if (const SequentialType *SeqTy = dyn_cast(AggTy)) {
1241 unsigned ElSizeBits = TD.getTypeSize(SeqTy->getElementType())*8;
1252 unsigned ElSizeBits =
1253 TD.getABITypeSizeInBits(SeqTy->getElementType());
12421254
12431255 NewOffset += ElSizeBits*Idx;
12441256 } else if (const StructType *STy = dyn_cast(AggTy)) {
12451257 unsigned EltBitOffset =
1246 TD.getStructLayout(STy)->getElementOffset(Idx)*8;
1258 TD.getStructLayout(STy)->getElementOffsetInBits(Idx);
12471259
12481260 NewOffset += EltBitOffset;
12491261 } else {
0 ; RUN: llvm-as < %s | opt -scalarrepl | llvm-dis | not grep shr
1
2 %struct.S = type { i16 }
3
4 define i1 @f(i16 signext %b) zeroext {
5 entry:
6 %b_addr = alloca i16 ; [#uses=2]
7 %retval = alloca i32 ; [#uses=2]
8 %s = alloca %struct.S ; <%struct.S*> [#uses=2]
9 %tmp = alloca i32 ; [#uses=2]
10 %"alloca point" = bitcast i32 0 to i32 ; [#uses=0]
11 store i16 %b, i16* %b_addr
12 %tmp1 = getelementptr %struct.S* %s, i32 0, i32 0 ; [#uses=1]
13 %tmp2 = load i16* %b_addr, align 2 ; [#uses=1]
14 store i16 %tmp2, i16* %tmp1, align 2
15 %tmp3 = getelementptr %struct.S* %s, i32 0, i32 0 ; [#uses=1]
16 %tmp34 = bitcast i16* %tmp3 to [2 x i1]* ; <[2 x i1]*> [#uses=1]
17 %tmp5 = getelementptr [2 x i1]* %tmp34, i32 0, i32 1 ; [#uses=1]
18 %tmp6 = load i1* %tmp5, align 1 ; [#uses=1]
19 %tmp67 = zext i1 %tmp6 to i32 ; [#uses=1]
20 store i32 %tmp67, i32* %tmp, align 4
21 %tmp8 = load i32* %tmp, align 4 ; [#uses=1]
22 store i32 %tmp8, i32* %retval, align 4
23 br label %return
24
25 return: ; preds = %entry
26 %retval9 = load i32* %retval ; [#uses=1]
27 %retval910 = trunc i32 %retval9 to i1 ; [#uses=1]
28 ret i1 %retval910
29 }