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"
25 // Handle the Pass registration stuff necessary to use TargetData's.
27 // Register the default SparcV9 implementation...
28 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
31 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
32 uint64_t &Size, unsigned char &Alignment);
34 //===----------------------------------------------------------------------===//
35 // Support for StructLayout
36 //===----------------------------------------------------------------------===//
38 StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
42 // Loop over each of the elements, placing them in memory...
43 for (StructType::element_iterator TI = ST->element_begin(),
44 TE = ST->element_end(); TI != TE; ++TI) {
49 getTypeInfo(Ty, &TD, TySize, A);
52 // Add padding if necessary to make the data element aligned properly...
53 if (StructSize % TyAlign != 0)
54 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
56 // Keep track of maximum alignment constraint
57 StructAlignment = std::max(TyAlign, StructAlignment);
59 MemberOffsets.push_back(StructSize);
60 StructSize += TySize; // Consume space for this data item
63 // Empty structures have alignment of 1 byte.
64 if (StructAlignment == 0) StructAlignment = 1;
66 // Add padding to the end of the struct so that it could be put in an array
67 // and all array elements would be aligned correctly.
68 if (StructSize % StructAlignment != 0)
69 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
72 //===----------------------------------------------------------------------===//
73 // TargetData Class Implementation
74 //===----------------------------------------------------------------------===//
76 TargetData::TargetData(const std::string &TargetName,
77 bool isLittleEndian, unsigned char PtrSize,
78 unsigned char PtrAl, unsigned char DoubleAl,
79 unsigned char FloatAl, unsigned char LongAl,
80 unsigned char IntAl, unsigned char ShortAl,
81 unsigned char ByteAl) {
83 // If this assert triggers, a pass "required" TargetData information, but the
84 // top level tool did not provide once for it. We do not want to default
85 // construct, or else we might end up using a bad endianness or pointer size!
87 assert(!TargetName.empty() &&
88 "ERROR: Tool did not specify a target data to use!");
90 LittleEndian = isLittleEndian;
91 PointerSize = PtrSize;
92 PointerAlignment = PtrAl;
93 DoubleAlignment = DoubleAl;
94 assert(DoubleAlignment == PtrAl &&
95 "Double alignment and pointer alignment agree for now!");
96 FloatAlignment = FloatAl;
97 LongAlignment = LongAl;
99 ShortAlignment = ShortAl;
100 ByteAlignment = ByteAl;
103 TargetData::TargetData(const std::string &ToolName, const Module *M) {
104 LittleEndian = M->getEndianness() != Module::BigEndian;
105 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
106 PointerAlignment = PointerSize;
107 DoubleAlignment = PointerSize;
115 static std::map<std::pair<const TargetData*,const StructType*>,
116 StructLayout> *Layouts = 0;
119 TargetData::~TargetData() {
121 // Remove any layouts for this TD.
122 std::map<std::pair<const TargetData*,
123 const StructType*>, StructLayout>::iterator
124 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
125 while (I != Layouts->end() && I->first.first == this)
127 if (Layouts->empty()) {
134 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
136 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
138 std::map<std::pair<const TargetData*,const StructType*>,
139 StructLayout>::iterator
140 I = Layouts->lower_bound(std::make_pair(this, Ty));
141 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
144 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
145 StructLayout(Ty, *this)))->second;
149 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
150 uint64_t &Size, unsigned char &Alignment) {
151 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
152 switch (Ty->getPrimitiveID()) {
155 case Type::UByteTyID:
156 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
157 case Type::UShortTyID:
158 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
160 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
161 case Type::ULongTyID:
162 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
163 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
164 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
165 case Type::LabelTyID:
166 case Type::PointerTyID:
167 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
169 case Type::ArrayTyID: {
170 const ArrayType *ATy = (const ArrayType *)Ty;
171 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
172 Size *= ATy->getNumElements();
175 case Type::StructTyID: {
176 // Get the layout annotation... which is lazily created on demand.
177 const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty);
178 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
184 assert(0 && "Bad type for getTypeInfo!!!");
189 uint64_t TargetData::getTypeSize(const Type *Ty) const {
192 getTypeInfo(Ty, this, Size, Align);
196 unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
199 getTypeInfo(Ty, this, Size, Align);
203 /// getIntPtrType - Return an unsigned integer type that is the same size or
204 /// greater to the host pointer size.
205 const Type *TargetData::getIntPtrType() const {
206 switch (getPointerSize()) {
207 default: assert(0 && "Unknown pointer size!");
208 case 2: return Type::UShortTy;
209 case 4: return Type::UIntTy;
210 case 8: return Type::ULongTy;
215 uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
216 const std::vector<Value*> &Idx) const {
217 const Type *Ty = ptrTy;
218 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
221 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) {
222 if (Idx[CurIDX]->getType() == Type::LongTy) {
223 // Update Ty to refer to current element
224 Ty = cast<SequentialType>(Ty)->getElementType();
226 // Get the array index and the size of each array element.
227 int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
228 Result += arrayIdx * (int64_t)getTypeSize(Ty);
230 const StructType *STy = cast<StructType>(Ty);
231 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
232 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
234 // Get structure layout information...
235 const StructLayout *Layout = getStructLayout(STy);
237 // Add in the offset, as calculated by the structure layout info...
238 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
239 Result += Layout->MemberOffsets[FieldNo];
241 // Update Ty to refer to current element
242 Ty = STy->getElementType(FieldNo);