blob: 75e76fe18605989cebcc9e4bd40a3c4ba6b4a918 [file] [log] [blame]
Chris Lattnere7fb3602001-08-27 16:00:15 +00001//===-- TargetData.cpp - Data size & alignment routines --------------------==//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere7fb3602001-08-27 16:00:15 +00009//
10// This file defines target properties related to datatype size/offset/alignment
Chris Lattner0e7ac162004-02-26 08:02:17 +000011// information.
Chris Lattnere7fb3602001-08-27 16:00:15 +000012//
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.
16//
17//===----------------------------------------------------------------------===//
18
Vikram S. Adve0799fc42001-09-18 12:58:33 +000019#include "llvm/Target/TargetData.h"
Chris Lattner53a0c382003-04-24 19:09:05 +000020#include "llvm/Module.h"
Chris Lattnere7fb3602001-08-27 16:00:15 +000021#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattner28977af2004-04-05 01:30:19 +000023#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnere7ea48c2005-03-13 19:04:41 +000025#include <algorithm>
Chris Lattnerf0453282003-12-22 05:01:15 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Misha Brukman5560c9d2003-08-18 14:43:39 +000028// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000029namespace {
30 // Register the default SparcV9 implementation...
31 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
32}
33
Chris Lattnere7fb3602001-08-27 16:00:15 +000034static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Misha Brukmanc8e87642004-07-23 01:09:52 +000035 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000036
37//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000038// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000039//===----------------------------------------------------------------------===//
40
Chris Lattner0e7ac162004-02-26 08:02:17 +000041StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000042 StructAlignment = 0;
43 StructSize = 0;
44
45 // Loop over each of the elements, placing them in memory...
Misha Brukmanf976c852005-04-21 22:55:34 +000046 for (StructType::element_iterator TI = ST->element_begin(),
Misha Brukmanc8e87642004-07-23 01:09:52 +000047 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000048 const Type *Ty = *TI;
49 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000050 unsigned TyAlign;
51 uint64_t TySize;
52 getTypeInfo(Ty, &TD, TySize, A);
53 TyAlign = A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000054
Misha Brukman5560c9d2003-08-18 14:43:39 +000055 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000056 if (StructSize % TyAlign != 0)
57 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
58
59 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000060 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000061
62 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000063 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000064 }
65
Chris Lattner4e840d42003-05-21 18:08:44 +000066 // Empty structures have alignment of 1 byte.
67 if (StructAlignment == 0) StructAlignment = 1;
68
Chris Lattnere7fb3602001-08-27 16:00:15 +000069 // Add padding to the end of the struct so that it could be put in an array
70 // and all array elements would be aligned correctly.
71 if (StructSize % StructAlignment != 0)
72 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000073}
74
Chris Lattnere7ea48c2005-03-13 19:04:41 +000075
76/// getElementContainingOffset - Given a valid offset into the structure,
77/// return the structure index that contains it.
78unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
79 std::vector<uint64_t>::const_iterator SI =
80 std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(),
81 Offset);
82 assert(SI != MemberOffsets.begin() && "Offset not in structure type!");
83 --SI;
84 assert(*SI <= Offset && "upper_bound didn't work");
85 assert((SI == MemberOffsets.begin() || *(SI-1) < Offset) &&
86 (SI+1 == MemberOffsets.end() || *(SI+1) > Offset) &&
87 "Upper bound didn't work!");
88 return SI-MemberOffsets.begin();
89}
90
Chris Lattnere7fb3602001-08-27 16:00:15 +000091//===----------------------------------------------------------------------===//
92// TargetData Class Implementation
93//===----------------------------------------------------------------------===//
94
Vikram S. Advef66723f2002-05-19 15:28:02 +000095TargetData::TargetData(const std::string &TargetName,
Chris Lattner10daaa12003-04-26 20:11:09 +000096 bool isLittleEndian, unsigned char PtrSize,
Chris Lattner85131c82002-10-14 22:41:13 +000097 unsigned char PtrAl, unsigned char DoubleAl,
Misha Brukmanf976c852005-04-21 22:55:34 +000098 unsigned char FloatAl, unsigned char LongAl,
Chris Lattner85131c82002-10-14 22:41:13 +000099 unsigned char IntAl, unsigned char ShortAl,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000100 unsigned char ByteAl, unsigned char BoolAl) {
Chris Lattnere7fb3602001-08-27 16:00:15 +0000101
Chris Lattner46326d92003-04-25 02:50:45 +0000102 // If this assert triggers, a pass "required" TargetData information, but the
Brian Gaeke8121fcd2004-04-14 21:21:56 +0000103 // top level tool did not provide one for it. We do not want to default
Chris Lattner46326d92003-04-25 02:50:45 +0000104 // construct, or else we might end up using a bad endianness or pointer size!
105 //
106 assert(!TargetName.empty() &&
107 "ERROR: Tool did not specify a target data to use!");
108
Chris Lattner85131c82002-10-14 22:41:13 +0000109 LittleEndian = isLittleEndian;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000110 PointerSize = PtrSize;
111 PointerAlignment = PtrAl;
112 DoubleAlignment = DoubleAl;
113 FloatAlignment = FloatAl;
114 LongAlignment = LongAl;
115 IntAlignment = IntAl;
116 ShortAlignment = ShortAl;
117 ByteAlignment = ByteAl;
Misha Brukmanc8e87642004-07-23 01:09:52 +0000118 BoolAlignment = BoolAl;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000119}
120
Chris Lattner0e7ac162004-02-26 08:02:17 +0000121TargetData::TargetData(const std::string &ToolName, const Module *M) {
Chris Lattner030574f2003-08-24 13:49:22 +0000122 LittleEndian = M->getEndianness() != Module::BigEndian;
123 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
Chris Lattner53a0c382003-04-24 19:09:05 +0000124 PointerAlignment = PointerSize;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000125 DoubleAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000126 FloatAlignment = 4;
Chris Lattnerda6122f2004-11-02 22:18:18 +0000127 LongAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000128 IntAlignment = 4;
129 ShortAlignment = 2;
130 ByteAlignment = 1;
Misha Brukmanc8e87642004-07-23 01:09:52 +0000131 BoolAlignment = 1;
Chris Lattner53a0c382003-04-24 19:09:05 +0000132}
133
Chris Lattner0e7ac162004-02-26 08:02:17 +0000134static std::map<std::pair<const TargetData*,const StructType*>,
135 StructLayout> *Layouts = 0;
136
137
Chris Lattnere7fb3602001-08-27 16:00:15 +0000138TargetData::~TargetData() {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000139 if (Layouts) {
140 // Remove any layouts for this TD.
141 std::map<std::pair<const TargetData*,
142 const StructType*>, StructLayout>::iterator
143 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
144 while (I != Layouts->end() && I->first.first == this)
145 Layouts->erase(I++);
146 if (Layouts->empty()) {
147 delete Layouts;
148 Layouts = 0;
149 }
150 }
151}
152
153const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
154 if (Layouts == 0)
155 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
156 StructLayout>();
157 std::map<std::pair<const TargetData*,const StructType*>,
158 StructLayout>::iterator
159 I = Layouts->lower_bound(std::make_pair(this, Ty));
160 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
161 return &I->second;
162 else {
163 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
164 StructLayout(Ty, *this)))->second;
165 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000166}
167
168static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000169 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000170 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000171 switch (Ty->getTypeID()) {
Misha Brukmanc8e87642004-07-23 01:09:52 +0000172 case Type::BoolTyID: Size = 1; Alignment = TD->getBoolAlignment(); return;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000173 case Type::VoidTyID:
Chris Lattnere7fb3602001-08-27 16:00:15 +0000174 case Type::UByteTyID:
175 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
176 case Type::UShortTyID:
177 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
178 case Type::UIntTyID:
179 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
180 case Type::ULongTyID:
181 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
182 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
183 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
184 case Type::LabelTyID:
185 case Type::PointerTyID:
186 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
187 return;
188 case Type::ArrayTyID: {
Chris Lattner59b00672004-07-01 17:32:59 +0000189 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000190 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000191 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000192 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000193 return;
194 }
Chris Lattner527efc62004-12-01 17:14:28 +0000195 case Type::PackedTyID: {
196 const PackedType *PTy = cast<PackedType>(Ty);
197 getTypeInfo(PTy->getElementType(), TD, Size, Alignment);
198 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
199 Size = AlignedSize*PTy->getNumElements();
200 return;
201 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000202 case Type::StructTyID: {
203 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000204 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnere7fb3602001-08-27 16:00:15 +0000205 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
206 return;
207 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000208
Chris Lattnere7fb3602001-08-27 16:00:15 +0000209 default:
210 assert(0 && "Bad type for getTypeInfo!!!");
211 return;
212 }
213}
214
Vikram S. Advef66723f2002-05-19 15:28:02 +0000215uint64_t TargetData::getTypeSize(const Type *Ty) const {
216 uint64_t Size;
217 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000218 getTypeInfo(Ty, this, Size, Align);
219 return Size;
220}
221
222unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000223 uint64_t Size;
224 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000225 getTypeInfo(Ty, this, Size, Align);
226 return Align;
227}
228
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000229unsigned char TargetData::getTypeAlignmentShift(const Type *Ty) const {
230 unsigned Align = getTypeAlignment(Ty);
231 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
Chris Lattner0561b3f2005-08-02 19:26:06 +0000232 return Log2_32(Align);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000233}
234
Chris Lattnerf0453282003-12-22 05:01:15 +0000235/// getIntPtrType - Return an unsigned integer type that is the same size or
236/// greater to the host pointer size.
237const Type *TargetData::getIntPtrType() const {
238 switch (getPointerSize()) {
239 default: assert(0 && "Unknown pointer size!");
240 case 2: return Type::UShortTy;
241 case 4: return Type::UIntTy;
242 case 8: return Type::ULongTy;
243 }
244}
245
246
Vikram S. Advef66723f2002-05-19 15:28:02 +0000247uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000248 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000249 const Type *Ty = ptrTy;
250 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000251 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000252
Chris Lattner28977af2004-04-05 01:30:19 +0000253 generic_gep_type_iterator<std::vector<Value*>::const_iterator>
254 TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
255 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
256 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
257 assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000258 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000259
260 // Get structure layout information...
261 const StructLayout *Layout = getStructLayout(STy);
262
263 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000264 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000265 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000266
Chris Lattnere7fb3602001-08-27 16:00:15 +0000267 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000268 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000269 } else {
270 // Update Ty to refer to current element
271 Ty = cast<SequentialType>(Ty)->getElementType();
272
273 // Get the array index and the size of each array element.
274 int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue();
275 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000276 }
277 }
278
279 return Result;
280}
Brian Gaeked0fde302003-11-11 22:41:34 +0000281