45 assert(!ST->isOpaque() &&
"Cannot get layout of opaque structs");
48 NumElements = ST->getNumElements();
51 for (
unsigned i = 0, e = NumElements; i != e; ++i) {
52 Type *Ty = ST->getElementType(i);
53 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
56 if ((StructSize & (TyAlign-1)) != 0)
60 StructAlignment = std::max(TyAlign, StructAlignment);
62 MemberOffsets[i] = StructSize;
63 StructSize += DL.getTypeAllocSize(Ty);
67 if (StructAlignment == 0) StructAlignment = 1;
71 if ((StructSize & (StructAlignment-1)) != 0)
80 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
81 assert(SI != &MemberOffsets[0] &&
"Offset not in structure type!");
83 assert(*SI <= Offset &&
"upper_bound didn't work");
84 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
85 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
86 "Upper bound didn't work!");
93 return SI-&MemberOffsets[0];
102 unsigned pref_align, uint32_t bit_width) {
103 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
129 unsigned pref_align, uint32_t bit_width) {
130 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
158 LittleEndian =
false;
159 StackNaturalAlign = 0;
174 setPointerAlignment(0, 8, 8, 8);
176 parseSpecifier(Desc);
181 assert(!Str.
empty() &&
"parse error, string can't be empty here");
182 std::pair<StringRef, StringRef>
Split = Str.
split(Separator);
183 assert((!Split.second.empty() || Split.first == Str) &&
184 "a trailing separator is not allowed");
192 assert(!error &&
"not a number, or does not fit in an unsigned int");
198 assert(Bits % 8 == 0 &&
"number of bits must be a byte width multiple");
202 void DataLayout::parseSpecifier(
StringRef Desc) {
203 while (!Desc.
empty()) {
205 std::pair<StringRef, StringRef>
Split =
split(Desc,
'-');
209 Split =
split(Split.first,
':');
215 char Specifier = Tok.
front();
220 LittleEndian =
false;
227 unsigned AddrSpace = Tok.
empty() ? 0 :
getInt(Tok);
228 assert(AddrSpace < 1 << 24 &&
229 "Invalid address space, must be a 24bit integer");
232 Split =
split(Rest,
':');
236 Split =
split(Rest,
':');
240 unsigned PointerPrefAlign = PointerABIAlign;
242 Split =
split(Rest,
':');
246 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
269 Split =
split(Rest,
':');
273 unsigned PrefAlign = ABIAlign;
275 Split =
split(Rest,
':');
279 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
285 unsigned Width =
getInt(Tok);
286 assert(Width != 0 &&
"width must be non-zero");
290 Split =
split(Rest,
':');
310 "Tool did not specify a DataLayout to use?");
319 DataLayout::setAlignment(
AlignTypeEnum align_type,
unsigned abi_align,
320 unsigned pref_align, uint32_t bit_width) {
321 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
322 assert(pref_align < (1 << 16) &&
"Alignment doesn't fit in bitfield");
323 assert(bit_width < (1 << 24) &&
"Bit width doesn't fit in bitfield");
324 for (
unsigned i = 0, e = Alignments.size(); i != e; ++i) {
325 if (Alignments[i].AlignType == (
unsigned)align_type &&
326 Alignments[i].TypeBitWidth == bit_width) {
328 Alignments[i].ABIAlign = abi_align;
329 Alignments[i].PrefAlign = pref_align;
335 pref_align, bit_width));
339 DataLayout::setPointerAlignment(uint32_t addr_space,
unsigned abi_align,
340 unsigned pref_align, uint32_t bit_width) {
341 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
343 if (val == Pointers.end()) {
345 abi_align, pref_align, bit_width);
347 val->second.ABIAlign = abi_align;
348 val->second.PrefAlign = pref_align;
349 val->second.TypeBitWidth = bit_width;
355 unsigned DataLayout::getAlignmentInfo(
AlignTypeEnum AlignType,
356 uint32_t BitWidth,
bool ABIInfo,
359 int BestMatchIdx = -1;
361 for (
unsigned i = 0, e = Alignments.size(); i != e; ++i) {
362 if (Alignments[i].AlignType == (
unsigned)AlignType &&
363 Alignments[i].TypeBitWidth == BitWidth)
364 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
371 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
372 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
376 if (LargestInt == -1 ||
377 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
384 if (BestMatchIdx == -1) {
387 BestMatchIdx = LargestInt;
389 assert(AlignType ==
VECTOR_ALIGN &&
"Unknown alignment type!");
394 Align *= cast<VectorType>(Ty)->getNumElements();
397 if (Align & (Align-1))
404 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
405 : Alignments[BestMatchIdx].PrefAlign;
410 class StructLayoutMap {
412 LayoutInfoTy LayoutInfo;
415 virtual ~StructLayoutMap() {
417 for (LayoutInfoTy::iterator
I = LayoutInfo.begin(), E = LayoutInfo.end();
420 Value->~StructLayout();
426 return LayoutInfo[STy];
430 virtual void dump()
const {}
436 delete static_cast<StructLayoutMap*
>(LayoutMap);
440 delete static_cast<StructLayoutMap*
>(LayoutMap);
447 LayoutMap =
new StructLayoutMap();
449 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
472 OS << (LittleEndian ?
"e" :
"E");
478 pib = Pointers.begin(), pie = Pointers.end();
482 std::sort(addrSpaces.begin(), addrSpaces.end());
484 ase = addrSpaces.end(); asb != ase; ++asb) {
493 OS <<
"-S" << StackNaturalAlign*8;
495 for (
unsigned i = 0, e = Alignments.size(); i != e; ++i) {
501 if (!LegalIntWidths.
empty()) {
502 OS <<
"-n" << (
unsigned)LegalIntWidths[0];
504 for (
unsigned i = 1, e = LegalIntWidths.
size(); i != e; ++i)
505 OS <<
':' << (
unsigned)LegalIntWidths[i];
512 "This should only be called with a pointer or pointer vector type");
528 unsigned DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
531 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
545 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
549 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
554 unsigned Align = getAlignmentInfo(
AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
583 return getAlignment(Ty,
true);
593 for (
unsigned i = 0, e = Alignments.size(); i != e; ++i)
595 return Alignments[i].ABIAlign;
601 return getAlignment(Ty,
false);
606 assert(!(Align & (Align-1)) &&
"Alignment is not a power of two!");
617 "Expected a pointer or pointer vector type.");
620 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
626 for (
unsigned i = 0, e = (
unsigned)LegalIntWidths.
size(); i != e; ++i)
627 if (Width <= LegalIntWidths[i])
633 unsigned MaxWidth = 0;
634 for (
unsigned i = 0, e = (
unsigned)LegalIntWidths.
size(); i != e; ++i)
635 MaxWidth = std::max<unsigned>(MaxWidth, LegalIntWidths[i]);
642 assert(Ty->
isPointerTy() &&
"Illegal argument for getIndexedOffset()");
647 for (
unsigned CurIDX = 0, EndIDX = Indices.
size(); CurIDX != EndIDX;
649 if (
StructType *STy = dyn_cast<StructType>(*TI)) {
650 assert(Indices[CurIDX]->
getType() ==
652 "Illegal struct idx");
653 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
662 Ty = STy->getElementType(FieldNo);
665 Ty = cast<SequentialType>(Ty)->getElementType();
668 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
683 if (GVAlignment >= Alignment) {
684 Alignment = GVAlignment;
685 }
else if (GVAlignment != 0) {
690 if (Alignment < 16) {
unsigned PrefAlign
Pref. alignment for this type/bitw.
std::string getStringRepresentation() const
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
unsigned getPointerPrefAlignment(unsigned AS=0) const
AlignTypeEnum
Enum used to categorize the alignment types stored by LayoutAlignElem.
unsigned TypeBitWidth
Type bit width.
The main container class for the LLVM Intermediate Representation.
2: 32-bit floating point type
unsigned getAlignment() const
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getPointerTypeSizeInBits(Type *) const
unsigned getPrefTypeAlignment(Type *Ty) const
std::pair< StringRef, StringRef > split(char Separator) const
StringRef substr(size_t Start, size_t N=npos) const
bool isPtrOrPtrVectorTy() const
unsigned getAlignment() const
static std::pair< StringRef, StringRef > split(StringRef Str, char Separator)
Checked version of split, to ensure mandatory subparts.
4: 80-bit floating point type (X87)
1: 16-bit floating point type
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
unsigned getPointerABIAlignment(unsigned AS=0) const
const StructLayout * getStructLayout(StructType *Ty) const
unsigned getPreferredTypeAlignmentShift(Type *Ty) const
static unsigned getInt(StringRef R)
Get an unsinged integer, including error checks.
#define llvm_unreachable(msg)
unsigned ABIAlign
ABI alignment for this type/bitw.
uint64_t getIndexedOffset(Type *Ty, ArrayRef< Value * > Indices) const
ID
LLVM Calling Convention Representation.
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment)
unsigned PrefAlign
Pref. alignment for this type/bitw.
static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, unsigned pref_align, uint32_t bit_width)
Initializer.
bool doFinalization(Module &M)
Type * getElementType() const
size_t size() const
size - Get the array size.
uint64_t getElementOffset(unsigned Idx) const
10: Arbitrary bit width integers
unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
Floating point type alignment.
6: 128-bit floating point type (two 64-bits, PowerPC)
uint32_t AddressSpace
Address space for the pointer type.
Integer representation type.
enable_if_c< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
uint64_t NextPowerOf2(uint64_t A)
bool operator==(const LayoutAlignElem &rhs) const
Equality predicate.
unsigned getPreferredAlignment(const GlobalVariable *GV) const
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
unsigned getABITypeAlignment(Type *Ty) const
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
static void Split(std::vector< std::string > &V, const StringRef S)
15: SIMD 'packed' format, or other vector type
uint64_t getTypeAllocSize(Type *Ty) const
const std::string & getDataLayout() const
uint32_t TypeBitWidth
Type bit width.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getElementContainingOffset(uint64_t Offset) const
void initializeDataLayoutPass(PassRegistry &)
unsigned Log2_32(uint32_t Value)
bool hasInitializer() const
unsigned getLargestLegalIntTypeSize() const
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
bool operator==(const PointerAlignElem &rhs) const
Equality predicate.
void init(StringRef LayoutDescription)
unsigned getCallFrameTypeAlignment(Type *Ty) const
unsigned ABIAlign
ABI alignment for this type/bitw.
PointerType * getType() const
getType - Global values are always pointers.
void *malloc(size_t size);
static IntegerType * getInt32Ty(LLVMContext &C)
unsigned getPointerSizeInBits(unsigned AS=0) const
const Type * getScalarType() const
unsigned AlignType
Alignment type (AlignTypeEnum)
static unsigned inBytes(unsigned Bits)
Convert bits into bytes. Assert if not a byte width multiple.
3: 64-bit floating point type
char front() const
front - Get the first character in the string.
LLVM Value Representation.
static VectorType * get(Type *ElementType, unsigned NumElements)
uint64_t getTypeSizeInBits(Type *Ty) const
static PointerAlignElem get(uint32_t addr_space, unsigned abi_align, unsigned pref_align, uint32_t bit_width)
Initializer.
9: MMX vectors (64 bits, X86 specific)
unsigned getNumElements() const
Random access to the elements.
bool empty() const
empty - Check if the string is empty.
5: 128-bit floating point type (112-bit mantissa)
gep_type_iterator gep_type_begin(const User *GEP)