blob: adbd22a5269a27ea2f752956711a8f597475ef37 [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"
Owen Anderson8f60c562006-05-12 05:49:47 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattnere7ea48c2005-03-13 19:04:41 +000027#include <algorithm>
Owen Anderson8f60c562006-05-12 05:49:47 +000028#include <cstdlib>
Owen Anderson2577c222006-05-12 07:01:44 +000029#include <sstream>
Chris Lattnerf0453282003-12-22 05:01:15 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Misha Brukman5560c9d2003-08-18 14:43:39 +000032// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000033namespace {
34 // Register the default SparcV9 implementation...
35 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
36}
37
Chris Lattner58092e32007-01-20 22:35:55 +000038static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD,
39 uint64_t &Size, unsigned char &Alignment);
40
41static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD,
42 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000043
44//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000045// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000046//===----------------------------------------------------------------------===//
47
Chris Lattner0e7ac162004-02-26 08:02:17 +000048StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000049 StructAlignment = 0;
50 StructSize = 0;
51
52 // Loop over each of the elements, placing them in memory...
Misha Brukmanf976c852005-04-21 22:55:34 +000053 for (StructType::element_iterator TI = ST->element_begin(),
Misha Brukmanc8e87642004-07-23 01:09:52 +000054 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000055 const Type *Ty = *TI;
56 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000057 unsigned TyAlign;
58 uint64_t TySize;
Chris Lattner58092e32007-01-20 22:35:55 +000059 getTypeInfoABI(Ty, &TD, TySize, A);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +000060 TyAlign = ST->isPacked() ? 1 : A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000061
Misha Brukman5560c9d2003-08-18 14:43:39 +000062 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000063 if (StructSize % TyAlign != 0)
64 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
65
66 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000067 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000068
69 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000070 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000071 }
72
Chris Lattner4e840d42003-05-21 18:08:44 +000073 // Empty structures have alignment of 1 byte.
74 if (StructAlignment == 0) StructAlignment = 1;
75
Chris Lattnere7fb3602001-08-27 16:00:15 +000076 // Add padding to the end of the struct so that it could be put in an array
77 // and all array elements would be aligned correctly.
78 if (StructSize % StructAlignment != 0)
79 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000080}
81
Chris Lattnere7ea48c2005-03-13 19:04:41 +000082
83/// getElementContainingOffset - Given a valid offset into the structure,
84/// return the structure index that contains it.
85unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
86 std::vector<uint64_t>::const_iterator SI =
Chris Lattner58092e32007-01-20 22:35:55 +000087 std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(), Offset);
Chris Lattnere7ea48c2005-03-13 19:04:41 +000088 assert(SI != MemberOffsets.begin() && "Offset not in structure type!");
89 --SI;
90 assert(*SI <= Offset && "upper_bound didn't work");
91 assert((SI == MemberOffsets.begin() || *(SI-1) < Offset) &&
92 (SI+1 == MemberOffsets.end() || *(SI+1) > Offset) &&
93 "Upper bound didn't work!");
94 return SI-MemberOffsets.begin();
95}
96
Chris Lattnere7fb3602001-08-27 16:00:15 +000097//===----------------------------------------------------------------------===//
98// TargetData Class Implementation
99//===----------------------------------------------------------------------===//
100
Chris Lattneracbc07a2006-06-16 18:11:26 +0000101void TargetData::init(const std::string &TargetDescription) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000102 std::string temp = TargetDescription;
103
104 LittleEndian = false;
Chris Lattner58092e32007-01-20 22:35:55 +0000105 PointerMemSize = 8;
106 PointerABIAlignment = 8;
Owen Anderson1027a532007-01-20 23:07:13 +0000107 DoubleABIAlignment = 0;
Chris Lattner58092e32007-01-20 22:35:55 +0000108 FloatABIAlignment = 4;
Owen Anderson1027a532007-01-20 23:07:13 +0000109 LongABIAlignment = 0;
Chris Lattner58092e32007-01-20 22:35:55 +0000110 IntABIAlignment = 4;
111 ShortABIAlignment = 2;
112 ByteABIAlignment = 1;
113 BoolABIAlignment = 1;
114 BoolPrefAlignment = BoolABIAlignment;
115 BytePrefAlignment = ByteABIAlignment;
116 ShortPrefAlignment = ShortABIAlignment;
117 IntPrefAlignment = IntABIAlignment;
Owen Anderson1027a532007-01-20 23:07:13 +0000118 LongPrefAlignment = 8;
Chris Lattner58092e32007-01-20 22:35:55 +0000119 FloatPrefAlignment = FloatABIAlignment;
Owen Anderson1027a532007-01-20 23:07:13 +0000120 DoublePrefAlignment = 8;
Chris Lattner58092e32007-01-20 22:35:55 +0000121 PointerPrefAlignment = PointerABIAlignment;
122 AggMinPrefAlignment = 0;
Owen Anderson8f60c562006-05-12 05:49:47 +0000123
Owen Andersond988b322006-05-20 00:24:56 +0000124 while (!temp.empty()) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000125 std::string token = getToken(temp, "-");
126
Owen Anderson84cc6db2006-05-17 21:56:02 +0000127 char signal = getToken(token, ":")[0];
128
129 switch(signal) {
Owen Anderson8f60c562006-05-12 05:49:47 +0000130 case 'E':
Owen Anderson571a13f2006-05-12 06:06:55 +0000131 LittleEndian = false;
132 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000133 case 'e':
Owen Anderson571a13f2006-05-12 06:06:55 +0000134 LittleEndian = true;
135 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000136 case 'p':
Chris Lattner58092e32007-01-20 22:35:55 +0000137 PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
138 PointerABIAlignment = atoi(getToken(token,":").c_str()) / 8;
139 PointerPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
140 if (PointerPrefAlignment == 0)
141 PointerPrefAlignment = PointerABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000142 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000143 case 'd':
Chris Lattner58092e32007-01-20 22:35:55 +0000144 DoubleABIAlignment = atoi(getToken(token,":").c_str()) / 8;
145 DoublePrefAlignment = atoi(getToken(token,":").c_str()) / 8;
146 if (DoublePrefAlignment == 0)
147 DoublePrefAlignment = DoubleABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000148 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000149 case 'f':
Chris Lattner58092e32007-01-20 22:35:55 +0000150 FloatABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
151 FloatPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
152 if (FloatPrefAlignment == 0)
153 FloatPrefAlignment = FloatABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000154 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000155 case 'l':
Chris Lattner58092e32007-01-20 22:35:55 +0000156 LongABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
157 LongPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
158 if (LongPrefAlignment == 0)
159 LongPrefAlignment = LongABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000160 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000161 case 'i':
Chris Lattner58092e32007-01-20 22:35:55 +0000162 IntABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
163 IntPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
164 if (IntPrefAlignment == 0)
165 IntPrefAlignment = IntABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000166 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000167 case 's':
Chris Lattner58092e32007-01-20 22:35:55 +0000168 ShortABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
169 ShortPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
170 if (ShortPrefAlignment == 0)
171 ShortPrefAlignment = ShortABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000172 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000173 case 'b':
Chris Lattner58092e32007-01-20 22:35:55 +0000174 ByteABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
175 BytePrefAlignment = atoi(getToken(token,":").c_str()) / 8;
176 if (BytePrefAlignment == 0)
177 BytePrefAlignment = ByteABIAlignment;
Owen Anderson571a13f2006-05-12 06:06:55 +0000178 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000179 case 'B':
Chris Lattner58092e32007-01-20 22:35:55 +0000180 BoolABIAlignment = atoi(getToken(token, ":").c_str()) / 8;
181 BoolPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
182 if (BoolPrefAlignment == 0)
183 BoolPrefAlignment = BoolABIAlignment;
184 break;
185 case 'A':
186 AggMinPrefAlignment = atoi(getToken(token,":").c_str()) / 8;
Owen Anderson571a13f2006-05-12 06:06:55 +0000187 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000188 default:
Owen Anderson571a13f2006-05-12 06:06:55 +0000189 break;
Owen Anderson8f60c562006-05-12 05:49:47 +0000190 }
191 }
Owen Anderson1027a532007-01-20 23:07:13 +0000192
Reid Spencer26f23852007-01-26 08:11:39 +0000193 // Unless explicitly specified, the alignments for longs and doubles is
194 // capped by pointer size.
Owen Anderson1027a532007-01-20 23:07:13 +0000195 if (LongABIAlignment == 0)
196 LongABIAlignment = LongPrefAlignment = PointerMemSize;
197 if (DoubleABIAlignment == 0)
198 DoubleABIAlignment = DoublePrefAlignment = PointerMemSize;
Owen Anderson8f60c562006-05-12 05:49:47 +0000199}
200
Chris Lattner1790d442006-06-16 18:22:52 +0000201TargetData::TargetData(const Module *M) {
Reid Spencer26f23852007-01-26 08:11:39 +0000202 init(M->getDataLayout());
Chris Lattner53a0c382003-04-24 19:09:05 +0000203}
204
Chris Lattnerec6478b2007-02-10 19:43:18 +0000205/// LayoutInfo - The lazy cache of structure layout information maintained by
Chris Lattner8dff24f2006-01-14 00:07:34 +0000206/// TargetData.
207///
Chris Lattnerec6478b2007-02-10 19:43:18 +0000208typedef std::pair<const TargetData*,const StructType*> LayoutKey;
209static ManagedStatic<std::map<LayoutKey, StructLayout> > LayoutInfo;
Chris Lattner0e7ac162004-02-26 08:02:17 +0000210
211
Chris Lattnere7fb3602001-08-27 16:00:15 +0000212TargetData::~TargetData() {
Chris Lattnerec6478b2007-02-10 19:43:18 +0000213 if (LayoutInfo.isConstructed()) {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000214 // Remove any layouts for this TD.
Chris Lattnerec6478b2007-02-10 19:43:18 +0000215 std::map<LayoutKey, StructLayout> &TheMap = *LayoutInfo;
216 std::map<LayoutKey, StructLayout>::iterator
217 I = TheMap.lower_bound(LayoutKey(this, (const StructType*)0));
218
219 for (std::map<LayoutKey, StructLayout>::iterator E = TheMap.end();
220 I != E && I->first.first == this; )
221 TheMap.erase(I++);
Chris Lattner0e7ac162004-02-26 08:02:17 +0000222 }
223}
224
Owen Anderson2577c222006-05-12 07:01:44 +0000225std::string TargetData::getStringRepresentation() const {
226 std::stringstream repr;
227
228 if (LittleEndian)
229 repr << "e";
230 else
231 repr << "E";
232
Chris Lattner58092e32007-01-20 22:35:55 +0000233 repr << "-p:" << (PointerMemSize * 8) << ":" << (PointerABIAlignment * 8);
234 repr << "-d:" << (DoubleABIAlignment * 8) << ":"
235 << (DoublePrefAlignment * 8);
236 repr << "-f:" << (FloatABIAlignment * 8) << ":"
237 << (FloatPrefAlignment * 8);
238 repr << "-l:" << (LongABIAlignment * 8) << ":"
239 << (LongPrefAlignment * 8);
240 repr << "-i:" << (IntABIAlignment * 8) << ":"
241 << (IntPrefAlignment * 8);
242 repr << "-s:" << (ShortABIAlignment * 8) << ":"
243 << (ShortPrefAlignment * 8);
244 repr << "-b:" << (ByteABIAlignment * 8) << ":"
245 << (BytePrefAlignment * 8);
246 repr << "-B:" << (BoolABIAlignment * 8) << ":"
247 << (BoolPrefAlignment * 8);
248 repr << "-A:" << (AggMinPrefAlignment * 8);
Owen Anderson2577c222006-05-12 07:01:44 +0000249
250 return repr.str();
251}
252
Chris Lattner0e7ac162004-02-26 08:02:17 +0000253const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
Chris Lattnerec6478b2007-02-10 19:43:18 +0000254 std::map<LayoutKey, StructLayout> &TheMap = *LayoutInfo;
255
256 std::map<LayoutKey, StructLayout>::iterator
257 I = TheMap.lower_bound(LayoutKey(this, Ty));
258 if (I != TheMap.end() && I->first.first == this && I->first.second == Ty)
Chris Lattner0e7ac162004-02-26 08:02:17 +0000259 return &I->second;
260 else {
Chris Lattnerec6478b2007-02-10 19:43:18 +0000261 return &TheMap.insert(I, std::make_pair(LayoutKey(this, Ty),
262 StructLayout(Ty, *this)))->second;
Chris Lattner0e7ac162004-02-26 08:02:17 +0000263 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000264}
265
Chris Lattner8dff24f2006-01-14 00:07:34 +0000266/// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
267/// objects. If a TargetData object is alive when types are being refined and
268/// removed, this method must be called whenever a StructType is removed to
269/// avoid a dangling pointer in this cache.
270void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
Chris Lattnerec6478b2007-02-10 19:43:18 +0000271 if (!LayoutInfo.isConstructed()) return; // No cache.
Chris Lattner8dff24f2006-01-14 00:07:34 +0000272
Chris Lattnerec6478b2007-02-10 19:43:18 +0000273 std::map<LayoutKey, StructLayout>::iterator I =
274 LayoutInfo->find(std::make_pair(this, Ty));
275 if (I != LayoutInfo->end())
276 LayoutInfo->erase(I);
Chris Lattner8dff24f2006-01-14 00:07:34 +0000277}
278
279
280
Chris Lattner58092e32007-01-20 22:35:55 +0000281static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD,
282 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000283 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000284 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000285 case Type::IntegerTyID: {
286 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
287 if (BitWidth <= 8) {
Chris Lattner58092e32007-01-20 22:35:55 +0000288 Size = 1; Alignment = TD->getByteABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000289 } else if (BitWidth <= 16) {
Chris Lattner58092e32007-01-20 22:35:55 +0000290 Size = 2; Alignment = TD->getShortABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000291 } else if (BitWidth <= 32) {
Chris Lattner58092e32007-01-20 22:35:55 +0000292 Size = 4; Alignment = TD->getIntABIAlignment();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000293 } else if (BitWidth <= 64) {
Chris Lattner58092e32007-01-20 22:35:55 +0000294 Size = 8; Alignment = TD->getLongABIAlignment();
Reid Spencerd2a988c2007-02-05 23:51:43 +0000295 } else {
296 Size = ((BitWidth + 7) / 8) & ~1;
297 Alignment = TD->getLongABIAlignment();
298 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000299 return;
300 }
Chris Lattner58092e32007-01-20 22:35:55 +0000301 case Type::VoidTyID: Size = 1; Alignment = TD->getByteABIAlignment(); return;
302 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatABIAlignment(); return;
303 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleABIAlignment(); return;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000304 case Type::LabelTyID:
305 case Type::PointerTyID:
Chris Lattner58092e32007-01-20 22:35:55 +0000306 Size = TD->getPointerSize(); Alignment = TD->getPointerABIAlignment();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000307 return;
308 case Type::ArrayTyID: {
Chris Lattner59b00672004-07-01 17:32:59 +0000309 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +0000310 getTypeInfoABI(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000311 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000312 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000313 return;
314 }
Chris Lattner527efc62004-12-01 17:14:28 +0000315 case Type::PackedTyID: {
316 const PackedType *PTy = cast<PackedType>(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +0000317 getTypeInfoABI(PTy->getElementType(), TD, Size, Alignment);
Chris Lattner527efc62004-12-01 17:14:28 +0000318 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
319 Size = AlignedSize*PTy->getNumElements();
Evan Chenge668bda2006-03-31 22:33:42 +0000320 // FIXME: The alignments of specific packed types are target dependent.
321 // For now, just set it to be equal to Size.
Chris Lattner0aab36f2006-04-03 23:14:49 +0000322 Alignment = Size;
Chris Lattner527efc62004-12-01 17:14:28 +0000323 return;
324 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000325 case Type::StructTyID: {
326 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000327 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000328 Size = Layout->getSizeInBytes(); Alignment = Layout->getAlignment();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000329 return;
330 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000331
Chris Lattnere7fb3602001-08-27 16:00:15 +0000332 default:
333 assert(0 && "Bad type for getTypeInfo!!!");
334 return;
335 }
336}
337
Chris Lattner58092e32007-01-20 22:35:55 +0000338static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD,
339 uint64_t &Size, unsigned char &Alignment) {
340 assert(Ty->isSized() && "Cannot getTypeInfoPref() on a type that is unsized!");
341 switch (Ty->getTypeID()) {
342 case Type::IntegerTyID: {
343 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
344 if (BitWidth <= 8) {
345 Size = 1; Alignment = TD->getBytePrefAlignment();
346 } else if (BitWidth <= 16) {
347 Size = 2; Alignment = TD->getShortPrefAlignment();
348 } else if (BitWidth <= 32) {
349 Size = 4; Alignment = TD->getIntPrefAlignment();
350 } else if (BitWidth <= 64) {
351 Size = 8; Alignment = TD->getLongPrefAlignment();
352 } else
353 assert(0 && "Integer types > 64 bits not supported.");
354 return;
355 }
356 case Type::VoidTyID:
357 Size = 1; Alignment = TD->getBytePrefAlignment();
358 return;
359 case Type::FloatTyID:
360 Size = 4; Alignment = TD->getFloatPrefAlignment();
361 return;
362 case Type::DoubleTyID:
363 Size = 8; Alignment = TD->getDoublePrefAlignment();
364 return;
365 case Type::LabelTyID:
366 case Type::PointerTyID:
367 Size = TD->getPointerSize(); Alignment = TD->getPointerPrefAlignment();
368 return;
369 case Type::ArrayTyID: {
370 const ArrayType *ATy = cast<ArrayType>(Ty);
371 getTypeInfoPref(ATy->getElementType(), TD, Size, Alignment);
372 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
373 Size = AlignedSize*ATy->getNumElements();
374 return;
375 }
376 case Type::PackedTyID: {
377 const PackedType *PTy = cast<PackedType>(Ty);
378 getTypeInfoPref(PTy->getElementType(), TD, Size, Alignment);
379 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
380 Size = AlignedSize*PTy->getNumElements();
381 // FIXME: The alignments of specific packed types are target dependent.
382 // For now, just set it to be equal to Size.
383 Alignment = Size;
384 return;
385 }
386 case Type::StructTyID: {
387 // Get the layout annotation... which is lazily created on demand;
388 // enforce minimum aggregate alignment.
389 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000390 Size = Layout->getSizeInBytes();
391 Alignment = std::max(Layout->getAlignment(),
392 (const unsigned int)TD->getAggMinPrefAlignment());
Chris Lattner58092e32007-01-20 22:35:55 +0000393 return;
394 }
395
396 default:
397 assert(0 && "Bad type for getTypeInfoPref!!!");
398 return;
399 }
400}
401
402
Vikram S. Advef66723f2002-05-19 15:28:02 +0000403uint64_t TargetData::getTypeSize(const Type *Ty) const {
404 uint64_t Size;
405 unsigned char Align;
Chris Lattner58092e32007-01-20 22:35:55 +0000406 getTypeInfoABI(Ty, this, Size, Align);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000407 return Size;
408}
409
Reid Spencer7c292432007-01-20 23:32:04 +0000410uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
411 if (Ty->isInteger())
412 return cast<IntegerType>(Ty)->getBitWidth();
413
414 uint64_t Size;
415 unsigned char Align;
416 getTypeInfoABI(Ty, this, Size, Align);
417 return Size * 8;
418}
419
Chris Lattner58092e32007-01-20 22:35:55 +0000420unsigned char TargetData::getTypeAlignmentABI(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000421 uint64_t Size;
422 unsigned char Align;
Chris Lattner58092e32007-01-20 22:35:55 +0000423 getTypeInfoABI(Ty, this, Size, Align);
424 return Align;
425}
426
427unsigned char TargetData::getTypeAlignmentPref(const Type *Ty) const {
428 uint64_t Size;
429 unsigned char Align;
430 getTypeInfoPref(Ty, this, Size, Align);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000431 return Align;
432}
433
Evan Chengde268f72007-01-24 07:03:39 +0000434unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const {
Evan Cheng778900a2007-01-22 23:08:19 +0000435 unsigned Align = getTypeAlignmentPref(Ty);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000436 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
Chris Lattner0561b3f2005-08-02 19:26:06 +0000437 return Log2_32(Align);
Chris Lattnerd2b0bb42004-08-17 19:13:00 +0000438}
439
Chris Lattnerf0453282003-12-22 05:01:15 +0000440/// getIntPtrType - Return an unsigned integer type that is the same size or
441/// greater to the host pointer size.
442const Type *TargetData::getIntPtrType() const {
443 switch (getPointerSize()) {
444 default: assert(0 && "Unknown pointer size!");
Reid Spencer47857812006-12-31 05:55:36 +0000445 case 2: return Type::Int16Ty;
446 case 4: return Type::Int32Ty;
447 case 8: return Type::Int64Ty;
Chris Lattnerf0453282003-12-22 05:01:15 +0000448 }
449}
450
451
Chris Lattnerddce8d22007-02-10 19:33:15 +0000452uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
453 unsigned NumIndices) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000454 const Type *Ty = ptrTy;
455 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000456 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000457
Chris Lattnerddce8d22007-02-10 19:33:15 +0000458 generic_gep_type_iterator<Value* const*>
459 TI = gep_type_begin(ptrTy, Indices, Indices+NumIndices);
460 for (unsigned CurIDX = 0; CurIDX != NumIndices; ++CurIDX, ++TI) {
Chris Lattner28977af2004-04-05 01:30:19 +0000461 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
Chris Lattnerddce8d22007-02-10 19:33:15 +0000462 assert(Indices[CurIDX]->getType() == Type::Int32Ty &&"Illegal struct idx");
463 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000464
465 // Get structure layout information...
466 const StructLayout *Layout = getStructLayout(STy);
467
468 // Add in the offset, as calculated by the structure layout info...
Chris Lattnerb1919e22007-02-10 19:55:17 +0000469 Result += Layout->getElementOffset(FieldNo);
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000470
Chris Lattnere7fb3602001-08-27 16:00:15 +0000471 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000472 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000473 } else {
474 // Update Ty to refer to current element
475 Ty = cast<SequentialType>(Ty)->getElementType();
476
477 // Get the array index and the size of each array element.
Chris Lattnerddce8d22007-02-10 19:33:15 +0000478 int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue();
Chris Lattner28977af2004-04-05 01:30:19 +0000479 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000480 }
481 }
482
483 return Result;
484}
Brian Gaeked0fde302003-11-11 22:41:34 +0000485
Devang Patelf9c197e2006-10-24 20:32:14 +0000486/// getPreferredAlignmentLog - Return the preferred alignment of the
487/// specified global, returned in log form. This includes an explicitly
488/// requested alignment (if the global has one).
489unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
490 const Type *ElemType = GV->getType()->getElementType();
Evan Chengde268f72007-01-24 07:03:39 +0000491 unsigned Alignment = getPreferredTypeAlignmentShift(ElemType);
Devang Patelf9c197e2006-10-24 20:32:14 +0000492 if (GV->getAlignment() > (1U << Alignment))
493 Alignment = Log2_32(GV->getAlignment());
494
495 if (GV->hasInitializer()) {
Devang Patelf9c197e2006-10-24 20:32:14 +0000496 if (Alignment < 4) {
497 // If the global is not external, see if it is large. If so, give it a
498 // larger alignment.
499 if (getTypeSize(ElemType) > 128)
500 Alignment = 4; // 16-byte alignment.
501 }
502 }
503 return Alignment;
504}
505