blob: c0b0670b96946bcc7387c9ec22b8066633a68613 [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"
Owen Anderson8f60c562006-05-12 05:49:47 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattnere7ea48c2005-03-13 19:04:41 +000026#include <algorithm>
Owen Anderson8f60c562006-05-12 05:49:47 +000027#include <cstdlib>
Owen Anderson2577c222006-05-12 07:01:44 +000028#include <sstream>
Chris Lattnerf0453282003-12-22 05:01:15 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Misha Brukman5560c9d2003-08-18 14:43:39 +000031// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000032namespace {
33 // Register the default SparcV9 implementation...
34 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
35}
36
Chris Lattnere7fb3602001-08-27 16:00:15 +000037static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Misha Brukmanc8e87642004-07-23 01:09:52 +000038 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000039
40//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000041// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000042//===----------------------------------------------------------------------===//
43
Chris Lattner0e7ac162004-02-26 08:02:17 +000044StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000045 StructAlignment = 0;
46 StructSize = 0;
47
48 // Loop over each of the elements, placing them in memory...
Misha Brukmanf976c852005-04-21 22:55:34 +000049 for (StructType::element_iterator TI = ST->element_begin(),
Misha Brukmanc8e87642004-07-23 01:09:52 +000050 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000051 const Type *Ty = *TI;
52 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000053 unsigned TyAlign;
54 uint64_t TySize;
55 getTypeInfo(Ty, &TD, TySize, A);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +000056 TyAlign = ST->isPacked() ? 1 : A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000057
Misha Brukman5560c9d2003-08-18 14:43:39 +000058 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000059 if (StructSize % TyAlign != 0)
60 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
61
62 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000063 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000064
65 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000066 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000067 }
68
Chris Lattner4e840d42003-05-21 18:08:44 +000069 // Empty structures have alignment of 1 byte.
70 if (StructAlignment == 0) StructAlignment = 1;
71
Chris Lattnere7fb3602001-08-27 16:00:15 +000072 // Add padding to the end of the struct so that it could be put in an array
73 // and all array elements would be aligned correctly.
74 if (StructSize % StructAlignment != 0)
75 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000076}
77
Chris Lattnere7ea48c2005-03-13 19:04:41 +000078
79/// getElementContainingOffset - Given a valid offset into the structure,
80/// return the structure index that contains it.
81unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
82 std::vector<uint64_t>::const_iterator SI =
83 std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(),
84 Offset);
85 assert(SI != MemberOffsets.begin() && "Offset not in structure type!");
86 --SI;
87 assert(*SI <= Offset && "upper_bound didn't work");
88 assert((SI == MemberOffsets.begin() || *(SI-1) < Offset) &&
89 (SI+1 == MemberOffsets.end() || *(SI+1) > Offset) &&
90 "Upper bound didn't work!");
91 return SI-MemberOffsets.begin();
92}
93
Chris Lattnere7fb3602001-08-27 16:00:15 +000094//===----------------------------------------------------------------------===//
95// TargetData Class Implementation
96//===----------------------------------------------------------------------===//
97
Chris Lattneracbc07a2006-06-16 18:11:26 +000098void TargetData::init(const std::string &TargetDescription) {
Owen Anderson8f60c562006-05-12 05:49:47 +000099 std::string temp = TargetDescription;
100
101 LittleEndian = false;
102 PointerSize = 8;
103 PointerAlignment = 8;
104 DoubleAlignment = 8;
105 FloatAlignment = 4;
106 LongAlignment = 8;
107 IntAlignment = 4;
108 ShortAlignment = 2;
109 ByteAlignment = 1;
110 BoolAlignment = 1;
111
Owen Andersond988b322006-05-20 00:24:56 +0000112 while (!temp.empty()) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000113 std::string token = getToken(temp, "-");
114
Owen Anderson84cc6db2006-05-17 21:56:02 +0000115 char signal = getToken(token, ":")[0];
116
117 switch(signal) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000118 case 'E':
Owen Anderson571a13f2006-05-12 06:06:55 +0000119 LittleEndian = false;
120 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000121 case 'e':
Owen Anderson571a13f2006-05-12 06:06:55 +0000122 LittleEndian = true;
123 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000124 case 'p':
Owen Anderson571a13f2006-05-12 06:06:55 +0000125 PointerSize = atoi(getToken(token,":").c_str()) / 8;
126 PointerAlignment = atoi(getToken(token,":").c_str()) / 8;
127 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000128 case 'd':
Owen Anderson571a13f2006-05-12 06:06:55 +0000129 DoubleAlignment = atoi(getToken(token,":").c_str()) / 8;
130 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000131 case 'f':
Owen Anderson571a13f2006-05-12 06:06:55 +0000132 FloatAlignment = atoi(getToken(token, ":").c_str()) / 8;
133 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000134 case 'l':
Owen Anderson571a13f2006-05-12 06:06:55 +0000135 LongAlignment = atoi(getToken(token, ":").c_str()) / 8;
136 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000137 case 'i':
Owen Anderson571a13f2006-05-12 06:06:55 +0000138 IntAlignment = atoi(getToken(token, ":").c_str()) / 8;
139 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000140 case 's':
Owen Anderson571a13f2006-05-12 06:06:55 +0000141 ShortAlignment = atoi(getToken(token, ":").c_str()) / 8;
142 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000143 case 'b':
Owen Anderson571a13f2006-05-12 06:06:55 +0000144 ByteAlignment = atoi(getToken(token, ":").c_str()) / 8;
145 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000146 case 'B':
Owen Anderson571a13f2006-05-12 06:06:55 +0000147 BoolAlignment = atoi(getToken(token, ":").c_str()) / 8;
148 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000149 default:
Owen Anderson571a13f2006-05-12 06:06:55 +0000150 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000151 }
152 }
153}
154
Chris Lattner1790d442006-06-16 18:22:52 +0000155TargetData::TargetData(const Module *M) {
Chris Lattner030574f2003-08-24 13:49:22 +0000156 LittleEndian = M->getEndianness() != Module::BigEndian;
157 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
Chris Lattner53a0c382003-04-24 19:09:05 +0000158 PointerAlignment = PointerSize;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000159 DoubleAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000160 FloatAlignment = 4;
Chris Lattnerda6122f2004-11-02 22:18:18 +0000161 LongAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000162 IntAlignment = 4;
163 ShortAlignment = 2;
164 ByteAlignment = 1;
Misha Brukmanc8e87642004-07-23 01:09:52 +0000165 BoolAlignment = 1;
Chris Lattner53a0c382003-04-24 19:09:05 +0000166}
167
Chris Lattner8dff24f2006-01-14 00:07:34 +0000168/// Layouts - The lazy cache of structure layout information maintained by
169/// TargetData.
170///
Chris Lattner0e7ac162004-02-26 08:02:17 +0000171static std::map<std::pair<const TargetData*,const StructType*>,
172 StructLayout> *Layouts = 0;
173
174
Chris Lattnere7fb3602001-08-27 16:00:15 +0000175TargetData::~TargetData() {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000176 if (Layouts) {
177 // Remove any layouts for this TD.
178 std::map<std::pair<const TargetData*,
179 const StructType*>, StructLayout>::iterator
180 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
181 while (I != Layouts->end() && I->first.first == this)
182 Layouts->erase(I++);
183 if (Layouts->empty()) {
184 delete Layouts;
185 Layouts = 0;
186 }
187 }
188}
189
Owen Anderson2577c222006-05-12 07:01:44 +0000190std::string TargetData::getStringRepresentation() const {
191 std::stringstream repr;
192
193 if (LittleEndian)
194 repr << "e";
195 else
196 repr << "E";
197
198 repr << "-p:" << (PointerSize * 8) << ":" << (PointerAlignment * 8);
199 repr << "-d:64:" << (DoubleAlignment * 8);
200 repr << "-f:32:" << (FloatAlignment * 8);
201 repr << "-l:64:" << (LongAlignment * 8);
202 repr << "-i:32:" << (IntAlignment * 8);
203 repr << "-s:16:" << (ShortAlignment * 8);
204 repr << "-b:8:" << (ByteAlignment * 8);
205 repr << "-B:8:" << (BoolAlignment * 8);
206
207 return repr.str();
208}
209
Chris Lattner0e7ac162004-02-26 08:02:17 +0000210const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
211 if (Layouts == 0)
212 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
213 StructLayout>();
214 std::map<std::pair<const TargetData*,const StructType*>,
215 StructLayout>::iterator
216 I = Layouts->lower_bound(std::make_pair(this, Ty));
217 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
218 return &I->second;
219 else {
220 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
221 StructLayout(Ty, *this)))->second;
222 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000223}
224
Chris Lattner8dff24f2006-01-14 00:07:34 +0000225/// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
226/// objects. If a TargetData object is alive when types are being refined and
227/// removed, this method must be called whenever a StructType is removed to
228/// avoid a dangling pointer in this cache.
229void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
230 if (!Layouts) return; // No cache.
231
232 std::map<std::pair<const TargetData*,const StructType*>,
233 StructLayout>::iterator I = Layouts->find(std::make_pair(this, Ty));
234 if (I != Layouts->end())
235 Layouts->erase(I);
236}
237
238
239
Chris Lattnere7fb3602001-08-27 16:00:15 +0000240static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000241 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000242 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000243 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000244 case Type::IntegerTyID: {
245 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
246 if (BitWidth <= 8) {
247 Size = 1; Alignment = TD->getByteAlignment();
248 } else if (BitWidth <= 16) {
249 Size = 2; Alignment = TD->getShortAlignment();
250 } else if (BitWidth <= 32) {
251 Size = 4; Alignment = TD->getIntAlignment();
252 } else if (BitWidth <= 64) {
253 Size = 8; Alignment = TD->getLongAlignment();
254 } else
255 assert(0 && "Integer types > 64 bits not supported.");
256 return;
257 }
258 case Type::VoidTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000259 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
260 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
261 case Type::LabelTyID:
262 case Type::PointerTyID:
263 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
264 return;
265 case Type::ArrayTyID: {
Chris Lattner59b00672004-07-01 17:32:59 +0000266 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000267 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000268 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000269 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000270 return;
271 }
Chris Lattner527efc62004-12-01 17:14:28 +0000272 case Type::PackedTyID: {
273 const PackedType *PTy = cast<PackedType>(Ty);
274 getTypeInfo(PTy->getElementType(), TD, Size, Alignment);
275 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
276 Size = AlignedSize*PTy->getNumElements();
Evan Chenge668bda2006-03-31 22:33:42 +0000277 // FIXME: The alignments of specific packed types are target dependent.
278 // For now, just set it to be equal to Size.
Chris Lattner0aab36f2006-04-03 23:14:49 +0000279 Alignment = Size;
Chris Lattner527efc62004-12-01 17:14:28 +0000280 return;
281 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000282 case Type::StructTyID: {
283 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000284 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnere7fb3602001-08-27 16:00:15 +0000285 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
286 return;
287 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000288
Chris Lattnere7fb3602001-08-27 16:00:15 +0000289 default:
290 assert(0 && "Bad type for getTypeInfo!!!");
291 return;
292 }
293}
294
Vikram S. Advef66723f2002-05-19 15:28:02 +0000295uint64_t TargetData::getTypeSize(const Type *Ty) const {
296 uint64_t Size;
297 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000298 getTypeInfo(Ty, this, Size, Align);
299 return Size;
300}
301
302unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000303 uint64_t Size;
304 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000305 getTypeInfo(Ty, this, Size, Align);
306 return Align;
307}
308
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000309unsigned char TargetData::getTypeAlignmentShift(const Type *Ty) const {
310 unsigned Align = getTypeAlignment(Ty);
311 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
Chris Lattner0561b3f2005-08-02 19:26:06 +0000312 return Log2_32(Align);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000313}
314
Chris Lattnerf0453282003-12-22 05:01:15 +0000315/// getIntPtrType - Return an unsigned integer type that is the same size or
316/// greater to the host pointer size.
317const Type *TargetData::getIntPtrType() const {
318 switch (getPointerSize()) {
319 default: assert(0 && "Unknown pointer size!");
Reid Spencer47857812006-12-31 05:55:36 +0000320 case 2: return Type::Int16Ty;
321 case 4: return Type::Int32Ty;
322 case 8: return Type::Int64Ty;
Chris Lattnerf0453282003-12-22 05:01:15 +0000323 }
324}
325
326
Vikram S. Advef66723f2002-05-19 15:28:02 +0000327uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000328 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000329 const Type *Ty = ptrTy;
330 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000331 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000332
Chris Lattner28977af2004-04-05 01:30:19 +0000333 generic_gep_type_iterator<std::vector<Value*>::const_iterator>
334 TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
335 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
336 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
Reid Spencer47857812006-12-31 05:55:36 +0000337 assert(Idx[CurIDX]->getType() == Type::Int32Ty && "Illegal struct idx");
Reid Spencerb83eb642006-10-20 07:07:24 +0000338 unsigned FieldNo = cast<ConstantInt>(Idx[CurIDX])->getZExtValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000339
340 // Get structure layout information...
341 const StructLayout *Layout = getStructLayout(STy);
342
343 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000344 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000345 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000346
Chris Lattnere7fb3602001-08-27 16:00:15 +0000347 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000348 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000349 } else {
350 // Update Ty to refer to current element
351 Ty = cast<SequentialType>(Ty)->getElementType();
352
353 // Get the array index and the size of each array element.
Reid Spencerb83eb642006-10-20 07:07:24 +0000354 int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getSExtValue();
Chris Lattner28977af2004-04-05 01:30:19 +0000355 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000356 }
357 }
358
359 return Result;
360}
Brian Gaeked0fde302003-11-11 22:41:34 +0000361
Devang Patelf9c197e2006-10-24 20:32:14 +0000362/// getPreferredAlignmentLog - Return the preferred alignment of the
363/// specified global, returned in log form. This includes an explicitly
364/// requested alignment (if the global has one).
365unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
366 const Type *ElemType = GV->getType()->getElementType();
367 unsigned Alignment = getTypeAlignmentShift(ElemType);
368 if (GV->getAlignment() > (1U << Alignment))
369 Alignment = Log2_32(GV->getAlignment());
370
371 if (GV->hasInitializer()) {
372 // Always round up alignment of global doubles to 8 bytes.
373 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
374 Alignment = 3;
375 if (Alignment < 4) {
376 // If the global is not external, see if it is large. If so, give it a
377 // larger alignment.
378 if (getTypeSize(ElemType) > 128)
379 Alignment = 4; // 16-byte alignment.
380 }
381 }
382 return Alignment;
383}
384