1 //===-- TargetData.cpp - Data size & alignment routines --------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target properties related to datatype size/offset/alignment
13 // This structure should be created once, filled in if the defaults are not
14 // correct and then passed around by const&. None of the members functions
15 // require modification to the object.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Module.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
26 // Handle the Pass registration stuff necessary to use TargetData's.
28 // Register the default SparcV9 implementation...
29 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
32 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
33 uint64_t &Size, unsigned char &Alignment);
35 //===----------------------------------------------------------------------===//
36 // Support for StructLayout
37 //===----------------------------------------------------------------------===//
39 StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
43 // Loop over each of the elements, placing them in memory...
44 for (StructType::element_iterator TI = ST->element_begin(),
45 TE = ST->element_end(); TI != TE; ++TI) {
50 getTypeInfo(Ty, &TD, TySize, A);
53 // Add padding if necessary to make the data element aligned properly...
54 if (StructSize % TyAlign != 0)
55 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
57 // Keep track of maximum alignment constraint
58 StructAlignment = std::max(TyAlign, StructAlignment);
60 MemberOffsets.push_back(StructSize);
61 StructSize += TySize; // Consume space for this data item
64 // Empty structures have alignment of 1 byte.
65 if (StructAlignment == 0) StructAlignment = 1;
67 // Add padding to the end of the struct so that it could be put in an array
68 // and all array elements would be aligned correctly.
69 if (StructSize % StructAlignment != 0)
70 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
73 //===----------------------------------------------------------------------===//
74 // TargetData Class Implementation
75 //===----------------------------------------------------------------------===//
77 TargetData::TargetData(const std::string &TargetName,
78 bool isLittleEndian, unsigned char PtrSize,
79 unsigned char PtrAl, unsigned char DoubleAl,
80 unsigned char FloatAl, unsigned char LongAl,
81 unsigned char IntAl, unsigned char ShortAl,
82 unsigned char ByteAl) {
84 // If this assert triggers, a pass "required" TargetData information, but the
85 // top level tool did not provide one for it. We do not want to default
86 // construct, or else we might end up using a bad endianness or pointer size!
88 assert(!TargetName.empty() &&
89 "ERROR: Tool did not specify a target data to use!");
91 LittleEndian = isLittleEndian;
92 PointerSize = PtrSize;
93 PointerAlignment = PtrAl;
94 DoubleAlignment = DoubleAl;
95 FloatAlignment = FloatAl;
96 LongAlignment = LongAl;
98 ShortAlignment = ShortAl;
99 ByteAlignment = ByteAl;
102 TargetData::TargetData(const std::string &ToolName, const Module *M) {
103 LittleEndian = M->getEndianness() != Module::BigEndian;
104 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
105 PointerAlignment = PointerSize;
106 DoubleAlignment = PointerSize;
114 static std::map<std::pair<const TargetData*,const StructType*>,
115 StructLayout> *Layouts = 0;
118 TargetData::~TargetData() {
120 // Remove any layouts for this TD.
121 std::map<std::pair<const TargetData*,
122 const StructType*>, StructLayout>::iterator
123 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
124 while (I != Layouts->end() && I->first.first == this)
126 if (Layouts->empty()) {
133 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
135 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
137 std::map<std::pair<const TargetData*,const StructType*>,
138 StructLayout>::iterator
139 I = Layouts->lower_bound(std::make_pair(this, Ty));
140 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
143 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
144 StructLayout(Ty, *this)))->second;
148 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
149 uint64_t &Size, unsigned char &Alignment) {
150 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
151 switch (Ty->getTypeID()) {
154 case Type::UByteTyID:
155 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
156 case Type::UShortTyID:
157 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
159 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
160 case Type::ULongTyID:
161 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
162 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
163 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
164 case Type::LabelTyID:
165 case Type::PointerTyID:
166 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
168 case Type::ArrayTyID: {
169 const ArrayType *ATy = cast<ArrayType>(Ty);
170 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
171 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
172 Size = AlignedSize*ATy->getNumElements();
175 case Type::StructTyID: {
176 // Get the layout annotation... which is lazily created on demand.
177 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
178 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
183 assert(0 && "Bad type for getTypeInfo!!!");
188 uint64_t TargetData::getTypeSize(const Type *Ty) const {
191 getTypeInfo(Ty, this, Size, Align);
195 unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
198 getTypeInfo(Ty, this, Size, Align);
202 /// getIntPtrType - Return an unsigned integer type that is the same size or
203 /// greater to the host pointer size.
204 const Type *TargetData::getIntPtrType() const {
205 switch (getPointerSize()) {
206 default: assert(0 && "Unknown pointer size!");
207 case 2: return Type::UShortTy;
208 case 4: return Type::UIntTy;
209 case 8: return Type::ULongTy;
214 uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
215 const std::vector<Value*> &Idx) const {
216 const Type *Ty = ptrTy;
217 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
220 generic_gep_type_iterator<std::vector<Value*>::const_iterator>
221 TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
222 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
223 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
224 assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx");
225 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
227 // Get structure layout information...
228 const StructLayout *Layout = getStructLayout(STy);
230 // Add in the offset, as calculated by the structure layout info...
231 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
232 Result += Layout->MemberOffsets[FieldNo];
234 // Update Ty to refer to current element
235 Ty = STy->getElementType(FieldNo);
237 // Update Ty to refer to current element
238 Ty = cast<SequentialType>(Ty)->getElementType();
240 // Get the array index and the size of each array element.
241 int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue();
242 Result += arrayIdx * (int64_t)getTypeSize(Ty);