blob: 38f0854f8064d357fb88c4820f95e47d394360ba [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 Lattnerec6478b2007-02-10 19:43:18 +000025#include "llvm/Support/ManagedStatic.h"
Chris Lattnerf6ca09a2007-02-10 20:26:17 +000026#include "llvm/ADT/DenseMap.h"
Owen Anderson8f60c562006-05-12 05:49:47 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattnere7ea48c2005-03-13 19:04:41 +000028#include <algorithm>
Owen Anderson8f60c562006-05-12 05:49:47 +000029#include <cstdlib>
Owen Anderson2577c222006-05-12 07:01:44 +000030#include <sstream>
Chris Lattnerf0453282003-12-22 05:01:15 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Misha Brukman5560c9d2003-08-18 14:43:39 +000033// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000034namespace {
35 // Register the default SparcV9 implementation...
36 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
37}
38
Chris Lattner58092e32007-01-20 22:35:55 +000039static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD,
40 uint64_t &Size, unsigned char &Alignment);
41
42static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD,
43 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000044
45//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000046// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000047//===----------------------------------------------------------------------===//
48
Chris Lattner0e7ac162004-02-26 08:02:17 +000049StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000050 StructAlignment = 0;
51 StructSize = 0;
Chris Lattner9182e3f2007-02-10 20:15:41 +000052 NumElements = ST->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +000053
54 // Loop over each of the elements, placing them in memory...
Chris Lattner9182e3f2007-02-10 20:15:41 +000055 for (unsigned i = 0, e = NumElements; i != e; ++i) {
56 const Type *Ty = ST->getElementType(i);
Chris Lattnere7fb3602001-08-27 16:00:15 +000057 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000058 unsigned TyAlign;
59 uint64_t TySize;
Chris Lattner58092e32007-01-20 22:35:55 +000060 getTypeInfoABI(Ty, &TD, TySize, A);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +000061 TyAlign = ST->isPacked() ? 1 : A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000062
Misha Brukman5560c9d2003-08-18 14:43:39 +000063 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000064 if (StructSize % TyAlign != 0)
65 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
66
67 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000068 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000069
Chris Lattner9182e3f2007-02-10 20:15:41 +000070 MemberOffsets[i] = StructSize;
Vikram S. Advef66723f2002-05-19 15:28:02 +000071 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000072 }
73
Chris Lattner4e840d42003-05-21 18:08:44 +000074 // Empty structures have alignment of 1 byte.
75 if (StructAlignment == 0) StructAlignment = 1;
76
Chris Lattnere7fb3602001-08-27 16:00:15 +000077 // Add padding to the end of the struct so that it could be put in an array
78 // and all array elements would be aligned correctly.
79 if (StructSize % StructAlignment != 0)
80 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000081}
82
Chris Lattnere7ea48c2005-03-13 19:04:41 +000083
84/// getElementContainingOffset - Given a valid offset into the structure,
85/// return the structure index that contains it.
86unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
Chris Lattner9182e3f2007-02-10 20:15:41 +000087 const uint64_t *SI =
88 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
89 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
Chris Lattnere7ea48c2005-03-13 19:04:41 +000090 --SI;
91 assert(*SI <= Offset && "upper_bound didn't work");
Chris Lattner9182e3f2007-02-10 20:15:41 +000092 assert((SI == &MemberOffsets[0] || *(SI-1) < Offset) &&
93 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
Chris Lattnere7ea48c2005-03-13 19:04:41 +000094 "Upper bound didn't work!");
Chris Lattner9182e3f2007-02-10 20:15:41 +000095 return SI-&MemberOffsets[0];
Chris Lattnere7ea48c2005-03-13 19:04:41 +000096}
97
Chris Lattnere7fb3602001-08-27 16:00:15 +000098//===----------------------------------------------------------------------===//
99// TargetData Class Implementation
100//===----------------------------------------------------------------------===//
101
Chris Lattneracbc07a2006-06-16 18:11:26 +0000102void TargetData::init(const std::string &TargetDescription) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000103 std::string temp = TargetDescription;
104
105 LittleEndian = false;
Chris Lattner58092e32007-01-20 22:35:55 +0000106 PointerMemSize = 8;
107 PointerABIAlignment = 8;
Owen Anderson1027a532007-01-20 23:07:13 +0000108 DoubleABIAlignment = 0;
Chris Lattner58092e32007-01-20 22:35:55 +0000109 FloatABIAlignment = 4;
Owen Anderson1027a532007-01-20 23:07:13 +0000110 LongABIAlignment = 0;
Chris Lattner58092e32007-01-20 22:35:55 +0000111 IntABIAlignment = 4;
112 ShortABIAlignment = 2;
113 ByteABIAlignment = 1;
114 BoolABIAlignment = 1;
115 BoolPrefAlignment = BoolABIAlignment;
116 BytePrefAlignment = ByteABIAlignment;
117 ShortPrefAlignment = ShortABIAlignment;
118 IntPrefAlignment = IntABIAlignment;
Owen Anderson1027a532007-01-20 23:07:13 +0000119 LongPrefAlignment = 8;
Chris Lattner58092e32007-01-20 22:35:55 +0000120 FloatPrefAlignment = FloatABIAlignment;
Owen Anderson1027a532007-01-20 23:07:13 +0000121 DoublePrefAlignment = 8;
Chris Lattner58092e32007-01-20 22:35:55 +0000122 PointerPrefAlignment = PointerABIAlignment;
123 AggMinPrefAlignment = 0;
Owen Anderson8f60c562006-05-12 05:49:47 +0000124
Owen Andersond988b322006-05-20 00:24:56 +0000125 while (!temp.empty()) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000126 std::string token = getToken(temp, "-");
127
Owen Anderson84cc6db2006-05-17 21:56:02 +0000128 char signal = getToken(token, ":")[0];
129
130 switch(signal) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000131 case 'E':
Owen Anderson571a13f2006-05-12 06:06:55 +0000132 LittleEndian = false;
133 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000134 case 'e':
Owen Anderson571a13f2006-05-12 06:06:55 +0000135 LittleEndian = true;
136 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000137 case 'p':
Chris Lattner58092e32007-01-20 22:35:55 +0000138 PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
139 PointerABIAlignment = atoi(getToken(token,":").c_str()) / 8;
140 PointerPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
141 if (PointerPrefAlignment == 0)
142 PointerPrefAlignment = PointerABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000143 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000144 case 'd':
Chris Lattner58092e32007-01-20 22:35:55 +0000145 DoubleABIAlignment = atoi(getToken(token,":").c_str()) / 8;
146 DoublePrefAlignment = atoi(getToken(token,":").c_str()) / 8;
147 if (DoublePrefAlignment == 0)
148 DoublePrefAlignment = DoubleABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000149 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000150 case 'f':
Chris Lattner58092e32007-01-20 22:35:55 +0000151 FloatABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
152 FloatPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
153 if (FloatPrefAlignment == 0)
154 FloatPrefAlignment = FloatABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000155 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000156 case 'l':
Chris Lattner58092e32007-01-20 22:35:55 +0000157 LongABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
158 LongPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
159 if (LongPrefAlignment == 0)
160 LongPrefAlignment = LongABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000161 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000162 case 'i':
Chris Lattner58092e32007-01-20 22:35:55 +0000163 IntABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
164 IntPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
165 if (IntPrefAlignment == 0)
166 IntPrefAlignment = IntABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000167 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000168 case 's':
Chris Lattner58092e32007-01-20 22:35:55 +0000169 ShortABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
170 ShortPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
171 if (ShortPrefAlignment == 0)
172 ShortPrefAlignment = ShortABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000173 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000174 case 'b':
Chris Lattner58092e32007-01-20 22:35:55 +0000175 ByteABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
176 BytePrefAlignment = atoi(getToken(token,":").c_str()) / 8;
177 if (BytePrefAlignment == 0)
178 BytePrefAlignment = ByteABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000179 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000180 case 'B':
Chris Lattner58092e32007-01-20 22:35:55 +0000181 BoolABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
182 BoolPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
183 if (BoolPrefAlignment == 0)
184 BoolPrefAlignment = BoolABIAlignment;
185 break;
186 case 'A':
187 AggMinPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
Owen Anderson571a13f2006-05-12 06:06:55 +0000188 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000189 default:
Owen Anderson571a13f2006-05-12 06:06:55 +0000190 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000191 }
192 }
Owen Anderson1027a532007-01-20 23:07:13 +0000193
Reid Spencer26f23852007-01-26 08:11:39 +0000194 // Unless explicitly specified, the alignments for longs and doubles is
195 // capped by pointer size.
Owen Anderson1027a532007-01-20 23:07:13 +0000196 if (LongABIAlignment == 0)
197 LongABIAlignment = LongPrefAlignment = PointerMemSize;
198 if (DoubleABIAlignment == 0)
199 DoubleABIAlignment = DoublePrefAlignment = PointerMemSize;
Owen Anderson8f60c562006-05-12 05:49:47 +0000200}
201
Chris Lattner1790d442006-06-16 18:22:52 +0000202TargetData::TargetData(const Module *M) {
Reid Spencer26f23852007-01-26 08:11:39 +0000203 init(M->getDataLayout());
Chris Lattner53a0c382003-04-24 19:09:05 +0000204}
205
Chris Lattnerec6478b2007-02-10 19:43:18 +0000206/// LayoutInfo - The lazy cache of structure layout information maintained by
Chris Lattner9182e3f2007-02-10 20:15:41 +0000207/// TargetData. Note that the struct types must have been free'd before
208/// llvm_shutdown is called (and thus this is deallocated) because all the
209/// targets with cached elements should have been destroyed.
Chris Lattner8dff24f2006-01-14 00:07:34 +0000210///
Chris Lattnerec6478b2007-02-10 19:43:18 +0000211typedef std::pair<const TargetData*,const StructType*> LayoutKey;
Chris Lattnerf6ca09a2007-02-10 20:26:17 +0000212
213struct DenseMapLayoutKeyInfo {
214 static inline LayoutKey getEmptyKey() { return LayoutKey(0, 0); }
215 static inline LayoutKey getTombstoneKey() {
216 return LayoutKey((TargetData*)(intptr_t)-1, 0);
217 }
218 static unsigned getHashValue(const LayoutKey &Val) {
219 return DenseMapKeyInfo<void*>::getHashValue(Val.first) ^
220 DenseMapKeyInfo<void*>::getHashValue(Val.second);
221 }
222 static bool isPod() { return true; }
223};
224
225typedef DenseMap<LayoutKey, StructLayout*, DenseMapLayoutKeyInfo> LayoutInfoTy;
Chris Lattnera12bd032007-02-10 20:18:06 +0000226static ManagedStatic<LayoutInfoTy> LayoutInfo;
Chris Lattner0e7ac162004-02-26 08:02:17 +0000227
228
Chris Lattnere7fb3602001-08-27 16:00:15 +0000229TargetData::~TargetData() {
Chris Lattnerec6478b2007-02-10 19:43:18 +0000230 if (LayoutInfo.isConstructed()) {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000231 // Remove any layouts for this TD.
Chris Lattnera12bd032007-02-10 20:18:06 +0000232 LayoutInfoTy &TheMap = *LayoutInfo;
Chris Lattnerf6ca09a2007-02-10 20:26:17 +0000233 for (LayoutInfoTy::iterator I = TheMap.begin(), E = TheMap.end();
234 I != E; ) {
235 if (I->first.first == this) {
236 I->second->~StructLayout();
237 free(I->second);
238 TheMap.erase(I++);
239 } else {
240 ++I;
241 }
Chris Lattner9182e3f2007-02-10 20:15:41 +0000242 }
Chris Lattner0e7ac162004-02-26 08:02:17 +0000243 }
244}
245
Chris Lattner9182e3f2007-02-10 20:15:41 +0000246const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
Chris Lattnera12bd032007-02-10 20:18:06 +0000247 LayoutInfoTy &TheMap = *LayoutInfo;
Chris Lattner9182e3f2007-02-10 20:15:41 +0000248
Chris Lattnerf6ca09a2007-02-10 20:26:17 +0000249 StructLayout *&SL = TheMap[LayoutKey(this, Ty)];
250 if (SL) return SL;
Chris Lattner9182e3f2007-02-10 20:15:41 +0000251
252 // Otherwise, create the struct layout. Because it is variable length, we
253 // malloc it, then use placement new.
254 unsigned NumElts = Ty->getNumElements();
255 StructLayout *L =
256 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1)*sizeof(uint64_t));
Chris Lattnerf6ca09a2007-02-10 20:26:17 +0000257
258 // Set SL before calling StructLayout's ctor. The ctor could cause other
259 // entries to be added to TheMap, invalidating our reference.
260 SL = L;
261
Chris Lattner9182e3f2007-02-10 20:15:41 +0000262 new (L) StructLayout(Ty, *this);
263
Chris Lattner9182e3f2007-02-10 20:15:41 +0000264 return L;
265}
266
267/// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
268/// objects. If a TargetData object is alive when types are being refined and
269/// removed, this method must be called whenever a StructType is removed to
270/// avoid a dangling pointer in this cache.
271void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
272 if (!LayoutInfo.isConstructed()) return; // No cache.
273
Chris Lattnera12bd032007-02-10 20:18:06 +0000274 LayoutInfoTy::iterator I = LayoutInfo->find(LayoutKey(this, Ty));
Chris Lattner9182e3f2007-02-10 20:15:41 +0000275 if (I != LayoutInfo->end()) {
276 I->second->~StructLayout();
277 free(I->second);
278 LayoutInfo->erase(I);
279 }
280}
281
282
Owen Anderson2577c222006-05-12 07:01:44 +0000283std::string TargetData::getStringRepresentation() const {
284 std::stringstream repr;
285
286 if (LittleEndian)
287 repr << "e";
288 else
289 repr << "E";
290
Chris Lattner58092e32007-01-20 22:35:55 +0000291 repr << "-p:" << (PointerMemSize * 8) << ":" << (PointerABIAlignment * 8);
292 repr << "-d:" << (DoubleABIAlignment * 8) << ":"
293 << (DoublePrefAlignment * 8);
294 repr << "-f:" << (FloatABIAlignment * 8) << ":"
295 << (FloatPrefAlignment * 8);
296 repr << "-l:" << (LongABIAlignment * 8) << ":"
297 << (LongPrefAlignment * 8);
298 repr << "-i:" << (IntABIAlignment * 8) << ":"
299 << (IntPrefAlignment * 8);
300 repr << "-s:" << (ShortABIAlignment * 8) << ":"
301 << (ShortPrefAlignment * 8);
302 repr << "-b:" << (ByteABIAlignment * 8) << ":"
303 << (BytePrefAlignment * 8);
304 repr << "-B:" << (BoolABIAlignment * 8) << ":"
305 << (BoolPrefAlignment * 8);
306 repr << "-A:" << (AggMinPrefAlignment * 8);
Owen Anderson2577c222006-05-12 07:01:44 +0000307
308 return repr.str();
309}
310
Chris Lattner8dff24f2006-01-14 00:07:34 +0000311
Chris Lattner58092e32007-01-20 22:35:55 +0000312static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD,
313 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000314 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000315 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000316 case Type::IntegerTyID: {
317 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
318 if (BitWidth <= 8) {
Chris Lattner58092e32007-01-20 22:35:55 +0000319 Size = 1; Alignment = TD->getByteABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000320 } else if (BitWidth <= 16) {
Chris Lattner58092e32007-01-20 22:35:55 +0000321 Size = 2; Alignment = TD->getShortABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000322 } else if (BitWidth <= 32) {
Chris Lattner58092e32007-01-20 22:35:55 +0000323 Size = 4; Alignment = TD->getIntABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000324 } else if (BitWidth <= 64) {
Chris Lattner58092e32007-01-20 22:35:55 +0000325 Size = 8; Alignment = TD->getLongABIAlignment();
Reid Spencerd2a988c2007-02-05 23:51:43 +0000326 } else {
327 Size = ((BitWidth + 7) / 8) & ~1;
328 Alignment = TD->getLongABIAlignment();
329 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000330 return;
331 }
Chris Lattner58092e32007-01-20 22:35:55 +0000332 case Type::VoidTyID: Size = 1; Alignment = TD->getByteABIAlignment(); return;
333 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatABIAlignment(); return;
334 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleABIAlignment(); return;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000335 case Type::LabelTyID:
336 case Type::PointerTyID:
Chris Lattner58092e32007-01-20 22:35:55 +0000337 Size = TD->getPointerSize(); Alignment = TD->getPointerABIAlignment();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000338 return;
339 case Type::ArrayTyID: {
Chris Lattner59b00672004-07-01 17:32:59 +0000340 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +0000341 getTypeInfoABI(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000342 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000343 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000344 return;
345 }
Chris Lattner527efc62004-12-01 17:14:28 +0000346 case Type::PackedTyID: {
347 const PackedType *PTy = cast<PackedType>(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +0000348 getTypeInfoABI(PTy->getElementType(), TD, Size, Alignment);
Chris Lattner527efc62004-12-01 17:14:28 +0000349 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
350 Size = AlignedSize*PTy->getNumElements();
Evan Chenge668bda2006-03-31 22:33:42 +0000351 // FIXME: The alignments of specific packed types are target dependent.
352 // For now, just set it to be equal to Size.
Chris Lattner0aab36f2006-04-03 23:14:49 +0000353 Alignment = Size;
Chris Lattner527efc62004-12-01 17:14:28 +0000354 return;
355 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000356 case Type::StructTyID: {
357 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000358 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000359 Size = Layout->getSizeInBytes(); Alignment = Layout->getAlignment();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000360 return;
361 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000362
Chris Lattnere7fb3602001-08-27 16:00:15 +0000363 default:
364 assert(0 && "Bad type for getTypeInfo!!!");
365 return;
366 }
367}
368
Chris Lattner58092e32007-01-20 22:35:55 +0000369static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD,
370 uint64_t &Size, unsigned char &Alignment) {
371 assert(Ty->isSized() && "Cannot getTypeInfoPref() on a type that is unsized!");
372 switch (Ty->getTypeID()) {
373 case Type::IntegerTyID: {
374 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
375 if (BitWidth <= 8) {
376 Size = 1; Alignment = TD->getBytePrefAlignment();
377 } else if (BitWidth <= 16) {
378 Size = 2; Alignment = TD->getShortPrefAlignment();
379 } else if (BitWidth <= 32) {
380 Size = 4; Alignment = TD->getIntPrefAlignment();
381 } else if (BitWidth <= 64) {
382 Size = 8; Alignment = TD->getLongPrefAlignment();
383 } else
384 assert(0 && "Integer types > 64 bits not supported.");
385 return;
386 }
387 case Type::VoidTyID:
388 Size = 1; Alignment = TD->getBytePrefAlignment();
389 return;
390 case Type::FloatTyID:
391 Size = 4; Alignment = TD->getFloatPrefAlignment();
392 return;
393 case Type::DoubleTyID:
394 Size = 8; Alignment = TD->getDoublePrefAlignment();
395 return;
396 case Type::LabelTyID:
397 case Type::PointerTyID:
398 Size = TD->getPointerSize(); Alignment = TD->getPointerPrefAlignment();
399 return;
400 case Type::ArrayTyID: {
401 const ArrayType *ATy = cast<ArrayType>(Ty);
402 getTypeInfoPref(ATy->getElementType(), TD, Size, Alignment);
403 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
404 Size = AlignedSize*ATy->getNumElements();
405 return;
406 }
407 case Type::PackedTyID: {
408 const PackedType *PTy = cast<PackedType>(Ty);
409 getTypeInfoPref(PTy->getElementType(), TD, Size, Alignment);
410 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
411 Size = AlignedSize*PTy->getNumElements();
412 // FIXME: The alignments of specific packed types are target dependent.
413 // For now, just set it to be equal to Size.
414 Alignment = Size;
415 return;
416 }
417 case Type::StructTyID: {
418 // Get the layout annotation... which is lazily created on demand;
419 // enforce minimum aggregate alignment.
420 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000421 Size = Layout->getSizeInBytes();
422 Alignment = std::max(Layout->getAlignment(),
423 (const unsigned int)TD->getAggMinPrefAlignment());
Chris Lattner58092e32007-01-20 22:35:55 +0000424 return;
425 }
426
427 default:
428 assert(0 && "Bad type for getTypeInfoPref!!!");
429 return;
430 }
431}
432
433
Vikram S. Advef66723f2002-05-19 15:28:02 +0000434uint64_t TargetData::getTypeSize(const Type *Ty) const {
435 uint64_t Size;
436 unsigned char Align;
Chris Lattner58092e32007-01-20 22:35:55 +0000437 getTypeInfoABI(Ty, this, Size, Align);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000438 return Size;
439}
440
Reid Spencer7c292432007-01-20 23:32:04 +0000441uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
442 if (Ty->isInteger())
443 return cast<IntegerType>(Ty)->getBitWidth();
444
445 uint64_t Size;
446 unsigned char Align;
447 getTypeInfoABI(Ty, this, Size, Align);
448 return Size * 8;
449}
450
Chris Lattner58092e32007-01-20 22:35:55 +0000451unsigned char TargetData::getTypeAlignmentABI(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000452 uint64_t Size;
453 unsigned char Align;
Chris Lattner58092e32007-01-20 22:35:55 +0000454 getTypeInfoABI(Ty, this, Size, Align);
455 return Align;
456}
457
458unsigned char TargetData::getTypeAlignmentPref(const Type *Ty) const {
459 uint64_t Size;
460 unsigned char Align;
461 getTypeInfoPref(Ty, this, Size, Align);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000462 return Align;
463}
464
Evan Chengde268f72007-01-24 07:03:39 +0000465unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const {
Evan Cheng778900a2007-01-22 23:08:19 +0000466 unsigned Align = getTypeAlignmentPref(Ty);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000467 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
Chris Lattner0561b3f2005-08-02 19:26:06 +0000468 return Log2_32(Align);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000469}
470
Chris Lattnerf0453282003-12-22 05:01:15 +0000471/// getIntPtrType - Return an unsigned integer type that is the same size or
472/// greater to the host pointer size.
473const Type *TargetData::getIntPtrType() const {
474 switch (getPointerSize()) {
475 default: assert(0 && "Unknown pointer size!");
Reid Spencer47857812006-12-31 05:55:36 +0000476 case 2: return Type::Int16Ty;
477 case 4: return Type::Int32Ty;
478 case 8: return Type::Int64Ty;
Chris Lattnerf0453282003-12-22 05:01:15 +0000479 }
480}
481
482
Chris Lattnerddce8d22007-02-10 19:33:15 +0000483uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
484 unsigned NumIndices) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000485 const Type *Ty = ptrTy;
486 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000487 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000488
Chris Lattnerddce8d22007-02-10 19:33:15 +0000489 generic_gep_type_iterator<Value* const*>
490 TI = gep_type_begin(ptrTy, Indices, Indices+NumIndices);
491 for (unsigned CurIDX = 0; CurIDX != NumIndices; ++CurIDX, ++TI) {
Chris Lattner28977af2004-04-05 01:30:19 +0000492 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
Chris Lattnerddce8d22007-02-10 19:33:15 +0000493 assert(Indices[CurIDX]->getType() == Type::Int32Ty &&"Illegal struct idx");
494 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000495
496 // Get structure layout information...
497 const StructLayout *Layout = getStructLayout(STy);
498
499 // Add in the offset, as calculated by the structure layout info...
Chris Lattnerb1919e22007-02-10 19:55:17 +0000500 Result += Layout->getElementOffset(FieldNo);
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000501
Chris Lattnere7fb3602001-08-27 16:00:15 +0000502 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000503 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000504 } else {
505 // Update Ty to refer to current element
506 Ty = cast<SequentialType>(Ty)->getElementType();
507
508 // Get the array index and the size of each array element.
Chris Lattnerddce8d22007-02-10 19:33:15 +0000509 int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue();
Chris Lattner28977af2004-04-05 01:30:19 +0000510 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000511 }
512 }
513
514 return Result;
515}
Brian Gaeked0fde302003-11-11 22:41:34 +0000516
Devang Patelf9c197e2006-10-24 20:32:14 +0000517/// getPreferredAlignmentLog - Return the preferred alignment of the
518/// specified global, returned in log form. This includes an explicitly
519/// requested alignment (if the global has one).
520unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
521 const Type *ElemType = GV->getType()->getElementType();
Evan Chengde268f72007-01-24 07:03:39 +0000522 unsigned Alignment = getPreferredTypeAlignmentShift(ElemType);
Devang Patelf9c197e2006-10-24 20:32:14 +0000523 if (GV->getAlignment() > (1U << Alignment))
524 Alignment = Log2_32(GV->getAlignment());
525
526 if (GV->hasInitializer()) {
Devang Patelf9c197e2006-10-24 20:32:14 +0000527 if (Alignment < 4) {
528 // If the global is not external, see if it is large. If so, give it a
529 // larger alignment.
530 if (getTypeSize(ElemType) > 128)
531 Alignment = 4; // 16-byte alignment.
532 }
533 }
534 return Alignment;
535}
536