blob: aa32530d4b5340d5d9cd8afa95cf5fdbd0b36d20 [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 Lattner58092e32007-01-20 22:35:55 +000037static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD,
38 uint64_t &Size, unsigned char &Alignment);
39
40static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD,
41 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000042
43//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000044// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000045//===----------------------------------------------------------------------===//
46
Chris Lattner0e7ac162004-02-26 08:02:17 +000047StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000048 StructAlignment = 0;
49 StructSize = 0;
50
51 // Loop over each of the elements, placing them in memory...
Misha Brukmanf976c852005-04-21 22:55:34 +000052 for (StructType::element_iterator TI = ST->element_begin(),
Misha Brukmanc8e87642004-07-23 01:09:52 +000053 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000054 const Type *Ty = *TI;
55 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000056 unsigned TyAlign;
57 uint64_t TySize;
Chris Lattner58092e32007-01-20 22:35:55 +000058 getTypeInfoABI(Ty, &TD, TySize, A);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +000059 TyAlign = ST->isPacked() ? 1 : A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000060
Misha Brukman5560c9d2003-08-18 14:43:39 +000061 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000062 if (StructSize % TyAlign != 0)
63 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
64
65 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000066 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000067
68 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000069 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000070 }
71
Chris Lattner4e840d42003-05-21 18:08:44 +000072 // Empty structures have alignment of 1 byte.
73 if (StructAlignment == 0) StructAlignment = 1;
74
Chris Lattnere7fb3602001-08-27 16:00:15 +000075 // Add padding to the end of the struct so that it could be put in an array
76 // and all array elements would be aligned correctly.
77 if (StructSize % StructAlignment != 0)
78 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000079}
80
Chris Lattnere7ea48c2005-03-13 19:04:41 +000081
82/// getElementContainingOffset - Given a valid offset into the structure,
83/// return the structure index that contains it.
84unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
85 std::vector<uint64_t>::const_iterator SI =
Chris Lattner58092e32007-01-20 22:35:55 +000086 std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(), Offset);
Chris Lattnere7ea48c2005-03-13 19:04:41 +000087 assert(SI != MemberOffsets.begin() && "Offset not in structure type!");
88 --SI;
89 assert(*SI <= Offset && "upper_bound didn't work");
90 assert((SI == MemberOffsets.begin() || *(SI-1) < Offset) &&
91 (SI+1 == MemberOffsets.end() || *(SI+1) > Offset) &&
92 "Upper bound didn't work!");
93 return SI-MemberOffsets.begin();
94}
95
Chris Lattnere7fb3602001-08-27 16:00:15 +000096//===----------------------------------------------------------------------===//
97// TargetData Class Implementation
98//===----------------------------------------------------------------------===//
99
Chris Lattneracbc07a2006-06-16 18:11:26 +0000100void TargetData::init(const std::string &TargetDescription) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000101 std::string temp = TargetDescription;
102
103 LittleEndian = false;
Chris Lattner58092e32007-01-20 22:35:55 +0000104 PointerMemSize = 8;
105 PointerABIAlignment = 8;
Owen Anderson1027a532007-01-20 23:07:13 +0000106 DoubleABIAlignment = 0;
Chris Lattner58092e32007-01-20 22:35:55 +0000107 FloatABIAlignment = 4;
Owen Anderson1027a532007-01-20 23:07:13 +0000108 LongABIAlignment = 0;
Chris Lattner58092e32007-01-20 22:35:55 +0000109 IntABIAlignment = 4;
110 ShortABIAlignment = 2;
111 ByteABIAlignment = 1;
112 BoolABIAlignment = 1;
113 BoolPrefAlignment = BoolABIAlignment;
114 BytePrefAlignment = ByteABIAlignment;
115 ShortPrefAlignment = ShortABIAlignment;
116 IntPrefAlignment = IntABIAlignment;
Owen Anderson1027a532007-01-20 23:07:13 +0000117 LongPrefAlignment = 8;
Chris Lattner58092e32007-01-20 22:35:55 +0000118 FloatPrefAlignment = FloatABIAlignment;
Owen Anderson1027a532007-01-20 23:07:13 +0000119 DoublePrefAlignment = 8;
Chris Lattner58092e32007-01-20 22:35:55 +0000120 PointerPrefAlignment = PointerABIAlignment;
121 AggMinPrefAlignment = 0;
Owen Anderson8f60c562006-05-12 05:49:47 +0000122
Owen Andersond988b322006-05-20 00:24:56 +0000123 while (!temp.empty()) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000124 std::string token = getToken(temp, "-");
125
Owen Anderson84cc6db2006-05-17 21:56:02 +0000126 char signal = getToken(token, ":")[0];
127
128 switch(signal) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000129 case 'E':
Owen Anderson571a13f2006-05-12 06:06:55 +0000130 LittleEndian = false;
131 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000132 case 'e':
Owen Anderson571a13f2006-05-12 06:06:55 +0000133 LittleEndian = true;
134 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000135 case 'p':
Chris Lattner58092e32007-01-20 22:35:55 +0000136 PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
137 PointerABIAlignment = atoi(getToken(token,":").c_str()) / 8;
138 PointerPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
139 if (PointerPrefAlignment == 0)
140 PointerPrefAlignment = PointerABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000141 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000142 case 'd':
Chris Lattner58092e32007-01-20 22:35:55 +0000143 DoubleABIAlignment = atoi(getToken(token,":").c_str()) / 8;
144 DoublePrefAlignment = atoi(getToken(token,":").c_str()) / 8;
145 if (DoublePrefAlignment == 0)
146 DoublePrefAlignment = DoubleABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000147 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000148 case 'f':
Chris Lattner58092e32007-01-20 22:35:55 +0000149 FloatABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
150 FloatPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
151 if (FloatPrefAlignment == 0)
152 FloatPrefAlignment = FloatABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000153 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000154 case 'l':
Chris Lattner58092e32007-01-20 22:35:55 +0000155 LongABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
156 LongPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
157 if (LongPrefAlignment == 0)
158 LongPrefAlignment = LongABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000159 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000160 case 'i':
Chris Lattner58092e32007-01-20 22:35:55 +0000161 IntABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
162 IntPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
163 if (IntPrefAlignment == 0)
164 IntPrefAlignment = IntABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000165 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000166 case 's':
Chris Lattner58092e32007-01-20 22:35:55 +0000167 ShortABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
168 ShortPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
169 if (ShortPrefAlignment == 0)
170 ShortPrefAlignment = ShortABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000171 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000172 case 'b':
Chris Lattner58092e32007-01-20 22:35:55 +0000173 ByteABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
174 BytePrefAlignment = atoi(getToken(token,":").c_str()) / 8;
175 if (BytePrefAlignment == 0)
176 BytePrefAlignment = ByteABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000177 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000178 case 'B':
Chris Lattner58092e32007-01-20 22:35:55 +0000179 BoolABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
180 BoolPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
181 if (BoolPrefAlignment == 0)
182 BoolPrefAlignment = BoolABIAlignment;
183 break;
184 case 'A':
185 AggMinPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
Owen Anderson571a13f2006-05-12 06:06:55 +0000186 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000187 default:
Owen Anderson571a13f2006-05-12 06:06:55 +0000188 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000189 }
190 }
Owen Anderson1027a532007-01-20 23:07:13 +0000191
Reid Spencer26f23852007-01-26 08:11:39 +0000192 // Unless explicitly specified, the alignments for longs and doubles is
193 // capped by pointer size.
Owen Anderson1027a532007-01-20 23:07:13 +0000194 if (LongABIAlignment == 0)
195 LongABIAlignment = LongPrefAlignment = PointerMemSize;
196 if (DoubleABIAlignment == 0)
197 DoubleABIAlignment = DoublePrefAlignment = PointerMemSize;
Owen Anderson8f60c562006-05-12 05:49:47 +0000198}
199
Chris Lattner1790d442006-06-16 18:22:52 +0000200TargetData::TargetData(const Module *M) {
Reid Spencer26f23852007-01-26 08:11:39 +0000201 init(M->getDataLayout());
Chris Lattner53a0c382003-04-24 19:09:05 +0000202}
203
Chris Lattner8dff24f2006-01-14 00:07:34 +0000204/// Layouts - The lazy cache of structure layout information maintained by
205/// TargetData.
206///
Chris Lattner0e7ac162004-02-26 08:02:17 +0000207static std::map<std::pair<const TargetData*,const StructType*>,
208 StructLayout> *Layouts = 0;
209
210
Chris Lattnere7fb3602001-08-27 16:00:15 +0000211TargetData::~TargetData() {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000212 if (Layouts) {
213 // Remove any layouts for this TD.
214 std::map<std::pair<const TargetData*,
215 const StructType*>, StructLayout>::iterator
216 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
217 while (I != Layouts->end() && I->first.first == this)
218 Layouts->erase(I++);
219 if (Layouts->empty()) {
220 delete Layouts;
221 Layouts = 0;
222 }
223 }
224}
225
Owen Anderson2577c222006-05-12 07:01:44 +0000226std::string TargetData::getStringRepresentation() const {
227 std::stringstream repr;
228
229 if (LittleEndian)
230 repr << "e";
231 else
232 repr << "E";
233
Chris Lattner58092e32007-01-20 22:35:55 +0000234 repr << "-p:" << (PointerMemSize * 8) << ":" << (PointerABIAlignment * 8);
235 repr << "-d:" << (DoubleABIAlignment * 8) << ":"
236 << (DoublePrefAlignment * 8);
237 repr << "-f:" << (FloatABIAlignment * 8) << ":"
238 << (FloatPrefAlignment * 8);
239 repr << "-l:" << (LongABIAlignment * 8) << ":"
240 << (LongPrefAlignment * 8);
241 repr << "-i:" << (IntABIAlignment * 8) << ":"
242 << (IntPrefAlignment * 8);
243 repr << "-s:" << (ShortABIAlignment * 8) << ":"
244 << (ShortPrefAlignment * 8);
245 repr << "-b:" << (ByteABIAlignment * 8) << ":"
246 << (BytePrefAlignment * 8);
247 repr << "-B:" << (BoolABIAlignment * 8) << ":"
248 << (BoolPrefAlignment * 8);
249 repr << "-A:" << (AggMinPrefAlignment * 8);
Owen Anderson2577c222006-05-12 07:01:44 +0000250
251 return repr.str();
252}
253
Chris Lattner0e7ac162004-02-26 08:02:17 +0000254const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
255 if (Layouts == 0)
256 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
257 StructLayout>();
258 std::map<std::pair<const TargetData*,const StructType*>,
259 StructLayout>::iterator
260 I = Layouts->lower_bound(std::make_pair(this, Ty));
261 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
262 return &I->second;
263 else {
264 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
265 StructLayout(Ty, *this)))->second;
266 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000267}
268
Chris Lattner8dff24f2006-01-14 00:07:34 +0000269/// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
270/// objects. If a TargetData object is alive when types are being refined and
271/// removed, this method must be called whenever a StructType is removed to
272/// avoid a dangling pointer in this cache.
273void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
274 if (!Layouts) return; // No cache.
275
276 std::map<std::pair<const TargetData*,const StructType*>,
277 StructLayout>::iterator I = Layouts->find(std::make_pair(this, Ty));
278 if (I != Layouts->end())
279 Layouts->erase(I);
280}
281
282
283
Chris Lattner58092e32007-01-20 22:35:55 +0000284static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD,
285 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000286 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000287 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000288 case Type::IntegerTyID: {
289 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
290 if (BitWidth <= 8) {
Chris Lattner58092e32007-01-20 22:35:55 +0000291 Size = 1; Alignment = TD->getByteABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000292 } else if (BitWidth <= 16) {
Chris Lattner58092e32007-01-20 22:35:55 +0000293 Size = 2; Alignment = TD->getShortABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000294 } else if (BitWidth <= 32) {
Chris Lattner58092e32007-01-20 22:35:55 +0000295 Size = 4; Alignment = TD->getIntABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000296 } else if (BitWidth <= 64) {
Chris Lattner58092e32007-01-20 22:35:55 +0000297 Size = 8; Alignment = TD->getLongABIAlignment();
Reid Spencerd2a988c2007-02-05 23:51:43 +0000298 } else {
299 Size = ((BitWidth + 7) / 8) & ~1;
300 Alignment = TD->getLongABIAlignment();
301 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000302 return;
303 }
Chris Lattner58092e32007-01-20 22:35:55 +0000304 case Type::VoidTyID: Size = 1; Alignment = TD->getByteABIAlignment(); return;
305 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatABIAlignment(); return;
306 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleABIAlignment(); return;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000307 case Type::LabelTyID:
308 case Type::PointerTyID:
Chris Lattner58092e32007-01-20 22:35:55 +0000309 Size = TD->getPointerSize(); Alignment = TD->getPointerABIAlignment();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000310 return;
311 case Type::ArrayTyID: {
Chris Lattner59b00672004-07-01 17:32:59 +0000312 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +0000313 getTypeInfoABI(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000314 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000315 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000316 return;
317 }
Chris Lattner527efc62004-12-01 17:14:28 +0000318 case Type::PackedTyID: {
319 const PackedType *PTy = cast<PackedType>(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +0000320 getTypeInfoABI(PTy->getElementType(), TD, Size, Alignment);
Chris Lattner527efc62004-12-01 17:14:28 +0000321 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
322 Size = AlignedSize*PTy->getNumElements();
Evan Chenge668bda2006-03-31 22:33:42 +0000323 // FIXME: The alignments of specific packed types are target dependent.
324 // For now, just set it to be equal to Size.
Chris Lattner0aab36f2006-04-03 23:14:49 +0000325 Alignment = Size;
Chris Lattner527efc62004-12-01 17:14:28 +0000326 return;
327 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000328 case Type::StructTyID: {
329 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000330 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnere7fb3602001-08-27 16:00:15 +0000331 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
332 return;
333 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000334
Chris Lattnere7fb3602001-08-27 16:00:15 +0000335 default:
336 assert(0 && "Bad type for getTypeInfo!!!");
337 return;
338 }
339}
340
Chris Lattner58092e32007-01-20 22:35:55 +0000341static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD,
342 uint64_t &Size, unsigned char &Alignment) {
343 assert(Ty->isSized() && "Cannot getTypeInfoPref() on a type that is unsized!");
344 switch (Ty->getTypeID()) {
345 case Type::IntegerTyID: {
346 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
347 if (BitWidth <= 8) {
348 Size = 1; Alignment = TD->getBytePrefAlignment();
349 } else if (BitWidth <= 16) {
350 Size = 2; Alignment = TD->getShortPrefAlignment();
351 } else if (BitWidth <= 32) {
352 Size = 4; Alignment = TD->getIntPrefAlignment();
353 } else if (BitWidth <= 64) {
354 Size = 8; Alignment = TD->getLongPrefAlignment();
355 } else
356 assert(0 && "Integer types > 64 bits not supported.");
357 return;
358 }
359 case Type::VoidTyID:
360 Size = 1; Alignment = TD->getBytePrefAlignment();
361 return;
362 case Type::FloatTyID:
363 Size = 4; Alignment = TD->getFloatPrefAlignment();
364 return;
365 case Type::DoubleTyID:
366 Size = 8; Alignment = TD->getDoublePrefAlignment();
367 return;
368 case Type::LabelTyID:
369 case Type::PointerTyID:
370 Size = TD->getPointerSize(); Alignment = TD->getPointerPrefAlignment();
371 return;
372 case Type::ArrayTyID: {
373 const ArrayType *ATy = cast<ArrayType>(Ty);
374 getTypeInfoPref(ATy->getElementType(), TD, Size, Alignment);
375 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
376 Size = AlignedSize*ATy->getNumElements();
377 return;
378 }
379 case Type::PackedTyID: {
380 const PackedType *PTy = cast<PackedType>(Ty);
381 getTypeInfoPref(PTy->getElementType(), TD, Size, Alignment);
382 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
383 Size = AlignedSize*PTy->getNumElements();
384 // FIXME: The alignments of specific packed types are target dependent.
385 // For now, just set it to be equal to Size.
386 Alignment = Size;
387 return;
388 }
389 case Type::StructTyID: {
390 // Get the layout annotation... which is lazily created on demand;
391 // enforce minimum aggregate alignment.
392 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
393 Size = Layout->StructSize;
394 Alignment = std::max(Layout->StructAlignment,
395 (const unsigned int) TD->getAggMinPrefAlignment());
396 return;
397 }
398
399 default:
400 assert(0 && "Bad type for getTypeInfoPref!!!");
401 return;
402 }
403}
404
405
Vikram S. Advef66723f2002-05-19 15:28:02 +0000406uint64_t TargetData::getTypeSize(const Type *Ty) const {
407 uint64_t Size;
408 unsigned char Align;
Chris Lattner58092e32007-01-20 22:35:55 +0000409 getTypeInfoABI(Ty, this, Size, Align);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000410 return Size;
411}
412
Reid Spencer7c292432007-01-20 23:32:04 +0000413uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
414 if (Ty->isInteger())
415 return cast<IntegerType>(Ty)->getBitWidth();
416
417 uint64_t Size;
418 unsigned char Align;
419 getTypeInfoABI(Ty, this, Size, Align);
420 return Size * 8;
421}
422
Chris Lattner58092e32007-01-20 22:35:55 +0000423unsigned char TargetData::getTypeAlignmentABI(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000424 uint64_t Size;
425 unsigned char Align;
Chris Lattner58092e32007-01-20 22:35:55 +0000426 getTypeInfoABI(Ty, this, Size, Align);
427 return Align;
428}
429
430unsigned char TargetData::getTypeAlignmentPref(const Type *Ty) const {
431 uint64_t Size;
432 unsigned char Align;
433 getTypeInfoPref(Ty, this, Size, Align);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000434 return Align;
435}
436
Evan Chengde268f72007-01-24 07:03:39 +0000437unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const {
Evan Cheng778900a2007-01-22 23:08:19 +0000438 unsigned Align = getTypeAlignmentPref(Ty);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000439 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
Chris Lattner0561b3f2005-08-02 19:26:06 +0000440 return Log2_32(Align);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000441}
442
Chris Lattnerf0453282003-12-22 05:01:15 +0000443/// getIntPtrType - Return an unsigned integer type that is the same size or
444/// greater to the host pointer size.
445const Type *TargetData::getIntPtrType() const {
446 switch (getPointerSize()) {
447 default: assert(0 && "Unknown pointer size!");
Reid Spencer47857812006-12-31 05:55:36 +0000448 case 2: return Type::Int16Ty;
449 case 4: return Type::Int32Ty;
450 case 8: return Type::Int64Ty;
Chris Lattnerf0453282003-12-22 05:01:15 +0000451 }
452}
453
454
Vikram S. Advef66723f2002-05-19 15:28:02 +0000455uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000456 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000457 const Type *Ty = ptrTy;
458 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000459 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000460
Chris Lattner28977af2004-04-05 01:30:19 +0000461 generic_gep_type_iterator<std::vector<Value*>::const_iterator>
462 TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
463 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
464 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
Reid Spencer47857812006-12-31 05:55:36 +0000465 assert(Idx[CurIDX]->getType() == Type::Int32Ty && "Illegal struct idx");
Reid Spencerb83eb642006-10-20 07:07:24 +0000466 unsigned FieldNo = cast<ConstantInt>(Idx[CurIDX])->getZExtValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000467
468 // Get structure layout information...
469 const StructLayout *Layout = getStructLayout(STy);
470
471 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000472 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000473 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000474
Chris Lattnere7fb3602001-08-27 16:00:15 +0000475 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000476 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000477 } else {
478 // Update Ty to refer to current element
479 Ty = cast<SequentialType>(Ty)->getElementType();
480
481 // Get the array index and the size of each array element.
Reid Spencerb83eb642006-10-20 07:07:24 +0000482 int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getSExtValue();
Chris Lattner28977af2004-04-05 01:30:19 +0000483 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000484 }
485 }
486
487 return Result;
488}
Brian Gaeked0fde302003-11-11 22:41:34 +0000489
Devang Patelf9c197e2006-10-24 20:32:14 +0000490/// getPreferredAlignmentLog - Return the preferred alignment of the
491/// specified global, returned in log form. This includes an explicitly
492/// requested alignment (if the global has one).
493unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
494 const Type *ElemType = GV->getType()->getElementType();
Evan Chengde268f72007-01-24 07:03:39 +0000495 unsigned Alignment = getPreferredTypeAlignmentShift(ElemType);
Devang Patelf9c197e2006-10-24 20:32:14 +0000496 if (GV->getAlignment() > (1U << Alignment))
497 Alignment = Log2_32(GV->getAlignment());
498
499 if (GV->hasInitializer()) {
Devang Patelf9c197e2006-10-24 20:32:14 +0000500 if (Alignment < 4) {
501 // If the global is not external, see if it is large. If so, give it a
502 // larger alignment.
503 if (getTypeSize(ElemType) > 128)
504 Alignment = 4; // 16-byte alignment.
505 }
506 }
507 return Alignment;
508}
509