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 assert(DoubleAlignment == PtrAl &&
96 "Double alignment and pointer alignment agree for now!");
97 FloatAlignment = FloatAl;
98 LongAlignment = LongAl;
100 ShortAlignment = ShortAl;
101 ByteAlignment = ByteAl;
104 TargetData::TargetData(const std::string &ToolName, const Module *M) {
105 LittleEndian = M->getEndianness() != Module::BigEndian;
106 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
107 PointerAlignment = PointerSize;
108 DoubleAlignment = PointerSize;
116 static std::map<std::pair<const TargetData*,const StructType*>,
117 StructLayout> *Layouts = 0;
120 TargetData::~TargetData() {
122 // Remove any layouts for this TD.
123 std::map<std::pair<const TargetData*,
124 const StructType*>, StructLayout>::iterator
125 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
126 while (I != Layouts->end() && I->first.first == this)
128 if (Layouts->empty()) {
135 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
137 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
139 std::map<std::pair<const TargetData*,const StructType*>,
140 StructLayout>::iterator
141 I = Layouts->lower_bound(std::make_pair(this, Ty));
142 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
145 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
146 StructLayout(Ty, *this)))->second;
150 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
151 uint64_t &Size, unsigned char &Alignment) {
152 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
153 switch (Ty->getTypeID()) {
156 case Type::UByteTyID:
157 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
158 case Type::UShortTyID:
159 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
161 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
162 case Type::ULongTyID:
163 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
164 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
165 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
166 case Type::LabelTyID:
167 case Type::PointerTyID:
168 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
170 case Type::ArrayTyID: {
171 const ArrayType *ATy = cast<ArrayType>(Ty);
172 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
173 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
174 Size = AlignedSize*ATy->getNumElements();
177 case Type::StructTyID: {
178 // Get the layout annotation... which is lazily created on demand.
179 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
180 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
185 assert(0 && "Bad type for getTypeInfo!!!");
190 uint64_t TargetData::getTypeSize(const Type *Ty) const {
193 getTypeInfo(Ty, this, Size, Align);
197 unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
200 getTypeInfo(Ty, this, Size, Align);
204 /// getIntPtrType - Return an unsigned integer type that is the same size or
205 /// greater to the host pointer size.
206 const Type *TargetData::getIntPtrType() const {
207 switch (getPointerSize()) {
208 default: assert(0 && "Unknown pointer size!");
209 case 2: return Type::UShortTy;
210 case 4: return Type::UIntTy;
211 case 8: return Type::ULongTy;
216 uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
217 const std::vector<Value*> &Idx) const {
218 const Type *Ty = ptrTy;
219 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
222 generic_gep_type_iterator<std::vector<Value*>::const_iterator>
223 TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
224 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
225 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
226 assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx");
227 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
229 // Get structure layout information...
230 const StructLayout *Layout = getStructLayout(STy);
232 // Add in the offset, as calculated by the structure layout info...
233 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
234 Result += Layout->MemberOffsets[FieldNo];
236 // Update Ty to refer to current element
237 Ty = STy->getElementType(FieldNo);
239 // Update Ty to refer to current element
240 Ty = cast<SequentialType>(Ty)->getElementType();
242 // Get the array index and the size of each array element.
243 int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue();
244 Result += arrayIdx * (int64_t)getTypeSize(Ty);