Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 1 | //===-- TargetData.cpp - Data size & alignment routines --------------------==// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines target properties related to datatype size/offset/alignment |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 11 | // information. |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 12 | // |
| 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. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 27 | #include <cstdlib> |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 28 | #include <sstream> |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 31 | // Handle the Pass registration stuff necessary to use TargetData's. |
Chris Lattner | aa31ad0 | 2002-09-25 23:46:55 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | // Register the default SparcV9 implementation... |
| 34 | RegisterPass<TargetData> X("targetdata", "Target Data Layout"); |
| 35 | } |
| 36 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 37 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 38 | uint64_t &Size, unsigned char &Alignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 41 | // Support for StructLayout |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 44 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 45 | StructAlignment = 0; |
| 46 | StructSize = 0; |
| 47 | |
| 48 | // Loop over each of the elements, placing them in memory... |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 49 | for (StructType::element_iterator TI = ST->element_begin(), |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 50 | TE = ST->element_end(); TI != TE; ++TI) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 51 | const Type *Ty = *TI; |
| 52 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 53 | unsigned TyAlign; |
| 54 | uint64_t TySize; |
| 55 | getTypeInfo(Ty, &TD, TySize, A); |
| 56 | TyAlign = A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 57 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 58 | // Add padding if necessary to make the data element aligned properly... |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 59 | if (StructSize % TyAlign != 0) |
| 60 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 61 | |
| 62 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 63 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 64 | |
| 65 | MemberOffsets.push_back(StructSize); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 66 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 4e840d4 | 2003-05-21 18:08:44 +0000 | [diff] [blame] | 69 | // Empty structures have alignment of 1 byte. |
| 70 | if (StructAlignment == 0) StructAlignment = 1; |
| 71 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 72 | // 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 Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 78 | |
| 79 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 80 | /// return the structure index that contains it. |
| 81 | unsigned 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 Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
| 95 | // TargetData Class Implementation |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 98 | TargetData::TargetData(const std::string &TargetName, |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 99 | bool isLittleEndian, unsigned char PtrSize, |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 100 | unsigned char PtrAl, unsigned char DoubleAl, |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 101 | unsigned char FloatAl, unsigned char LongAl, |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 102 | unsigned char IntAl, unsigned char ShortAl, |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 103 | unsigned char ByteAl, unsigned char BoolAl) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 46326d9 | 2003-04-25 02:50:45 +0000 | [diff] [blame] | 105 | // If this assert triggers, a pass "required" TargetData information, but the |
Brian Gaeke | 8121fcd | 2004-04-14 21:21:56 +0000 | [diff] [blame] | 106 | // top level tool did not provide one for it. We do not want to default |
Chris Lattner | 46326d9 | 2003-04-25 02:50:45 +0000 | [diff] [blame] | 107 | // construct, or else we might end up using a bad endianness or pointer size! |
| 108 | // |
| 109 | assert(!TargetName.empty() && |
| 110 | "ERROR: Tool did not specify a target data to use!"); |
| 111 | |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 112 | LittleEndian = isLittleEndian; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 113 | PointerSize = PtrSize; |
| 114 | PointerAlignment = PtrAl; |
| 115 | DoubleAlignment = DoubleAl; |
| 116 | FloatAlignment = FloatAl; |
| 117 | LongAlignment = LongAl; |
| 118 | IntAlignment = IntAl; |
| 119 | ShortAlignment = ShortAl; |
| 120 | ByteAlignment = ByteAl; |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 121 | BoolAlignment = BoolAl; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 124 | TargetData::TargetData(const std::string &TargetName, |
| 125 | const std::string &TargetDescription) { |
| 126 | std::string temp = TargetDescription; |
| 127 | |
| 128 | LittleEndian = false; |
| 129 | PointerSize = 8; |
| 130 | PointerAlignment = 8; |
| 131 | DoubleAlignment = 8; |
| 132 | FloatAlignment = 4; |
| 133 | LongAlignment = 8; |
| 134 | IntAlignment = 4; |
| 135 | ShortAlignment = 2; |
| 136 | ByteAlignment = 1; |
| 137 | BoolAlignment = 1; |
| 138 | |
| 139 | while (temp.length() > 0) { |
| 140 | std::string token = getToken(temp, "-"); |
| 141 | |
Owen Anderson | 84cc6db | 2006-05-17 21:56:02 +0000 | [diff] [blame^] | 142 | char signal = getToken(token, ":")[0]; |
| 143 | |
| 144 | switch(signal) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 145 | case 'E': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 146 | LittleEndian = false; |
| 147 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 148 | case 'e': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 149 | LittleEndian = true; |
| 150 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 151 | case 'p': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 152 | PointerSize = atoi(getToken(token,":").c_str()) / 8; |
| 153 | PointerAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 154 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 155 | case 'd': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 156 | token = getToken(token,":"); //Ignore the size |
| 157 | DoubleAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 158 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 159 | case 'f': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 160 | token = getToken(token, ":"); //Ignore the size |
| 161 | FloatAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 162 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 163 | case 'l': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 164 | token = getToken(token, ":"); //Ignore the size |
| 165 | LongAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 166 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 167 | case 'i': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 168 | token = getToken(token, ":"); //Ignore the size |
| 169 | IntAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 170 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 171 | case 's': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 172 | token = getToken(token, ":"); //Ignore the size |
| 173 | ShortAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 174 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 175 | case 'b': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 176 | token = getToken(token, ":"); //Ignore the size |
| 177 | ByteAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 178 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 179 | case 'B': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 180 | token = getToken(token, ":"); //Ignore the size |
| 181 | BoolAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 182 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 183 | default: |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 184 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 189 | TargetData::TargetData(const std::string &ToolName, const Module *M) { |
Chris Lattner | 030574f | 2003-08-24 13:49:22 +0000 | [diff] [blame] | 190 | LittleEndian = M->getEndianness() != Module::BigEndian; |
| 191 | PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 192 | PointerAlignment = PointerSize; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 193 | DoubleAlignment = PointerSize; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 194 | FloatAlignment = 4; |
Chris Lattner | da6122f | 2004-11-02 22:18:18 +0000 | [diff] [blame] | 195 | LongAlignment = PointerSize; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 196 | IntAlignment = 4; |
| 197 | ShortAlignment = 2; |
| 198 | ByteAlignment = 1; |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 199 | BoolAlignment = 1; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 8dff24f | 2006-01-14 00:07:34 +0000 | [diff] [blame] | 202 | /// Layouts - The lazy cache of structure layout information maintained by |
| 203 | /// TargetData. |
| 204 | /// |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 205 | static std::map<std::pair<const TargetData*,const StructType*>, |
| 206 | StructLayout> *Layouts = 0; |
| 207 | |
| 208 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 209 | TargetData::~TargetData() { |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 210 | if (Layouts) { |
| 211 | // Remove any layouts for this TD. |
| 212 | std::map<std::pair<const TargetData*, |
| 213 | const StructType*>, StructLayout>::iterator |
| 214 | I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0)); |
| 215 | while (I != Layouts->end() && I->first.first == this) |
| 216 | Layouts->erase(I++); |
| 217 | if (Layouts->empty()) { |
| 218 | delete Layouts; |
| 219 | Layouts = 0; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 224 | std::string TargetData::getStringRepresentation() const { |
| 225 | std::stringstream repr; |
| 226 | |
| 227 | if (LittleEndian) |
| 228 | repr << "e"; |
| 229 | else |
| 230 | repr << "E"; |
| 231 | |
| 232 | repr << "-p:" << (PointerSize * 8) << ":" << (PointerAlignment * 8); |
| 233 | repr << "-d:64:" << (DoubleAlignment * 8); |
| 234 | repr << "-f:32:" << (FloatAlignment * 8); |
| 235 | repr << "-l:64:" << (LongAlignment * 8); |
| 236 | repr << "-i:32:" << (IntAlignment * 8); |
| 237 | repr << "-s:16:" << (ShortAlignment * 8); |
| 238 | repr << "-b:8:" << (ByteAlignment * 8); |
| 239 | repr << "-B:8:" << (BoolAlignment * 8); |
| 240 | |
| 241 | return repr.str(); |
| 242 | } |
| 243 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 244 | const StructLayout *TargetData::getStructLayout(const StructType *Ty) const { |
| 245 | if (Layouts == 0) |
| 246 | Layouts = new std::map<std::pair<const TargetData*,const StructType*>, |
| 247 | StructLayout>(); |
| 248 | std::map<std::pair<const TargetData*,const StructType*>, |
| 249 | StructLayout>::iterator |
| 250 | I = Layouts->lower_bound(std::make_pair(this, Ty)); |
| 251 | if (I != Layouts->end() && I->first.first == this && I->first.second == Ty) |
| 252 | return &I->second; |
| 253 | else { |
| 254 | return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty), |
| 255 | StructLayout(Ty, *this)))->second; |
| 256 | } |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | 8dff24f | 2006-01-14 00:07:34 +0000 | [diff] [blame] | 259 | /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout |
| 260 | /// objects. If a TargetData object is alive when types are being refined and |
| 261 | /// removed, this method must be called whenever a StructType is removed to |
| 262 | /// avoid a dangling pointer in this cache. |
| 263 | void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const { |
| 264 | if (!Layouts) return; // No cache. |
| 265 | |
| 266 | std::map<std::pair<const TargetData*,const StructType*>, |
| 267 | StructLayout>::iterator I = Layouts->find(std::make_pair(this, Ty)); |
| 268 | if (I != Layouts->end()) |
| 269 | Layouts->erase(I); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 274 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 275 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 276 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 277 | switch (Ty->getTypeID()) { |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 278 | case Type::BoolTyID: Size = 1; Alignment = TD->getBoolAlignment(); return; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 279 | case Type::VoidTyID: |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 280 | case Type::UByteTyID: |
| 281 | case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return; |
| 282 | case Type::UShortTyID: |
| 283 | case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return; |
| 284 | case Type::UIntTyID: |
| 285 | case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return; |
| 286 | case Type::ULongTyID: |
| 287 | case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return; |
| 288 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return; |
| 289 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return; |
| 290 | case Type::LabelTyID: |
| 291 | case Type::PointerTyID: |
| 292 | Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment(); |
| 293 | return; |
| 294 | case Type::ArrayTyID: { |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 295 | const ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 296 | getTypeInfo(ATy->getElementType(), TD, Size, Alignment); |
Brian Gaeke | e0e3589 | 2004-07-02 07:01:31 +0000 | [diff] [blame] | 297 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 298 | Size = AlignedSize*ATy->getNumElements(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 299 | return; |
| 300 | } |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 301 | case Type::PackedTyID: { |
| 302 | const PackedType *PTy = cast<PackedType>(Ty); |
| 303 | getTypeInfo(PTy->getElementType(), TD, Size, Alignment); |
| 304 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 305 | Size = AlignedSize*PTy->getNumElements(); |
Evan Cheng | e668bda | 2006-03-31 22:33:42 +0000 | [diff] [blame] | 306 | // FIXME: The alignments of specific packed types are target dependent. |
| 307 | // For now, just set it to be equal to Size. |
Chris Lattner | 0aab36f | 2006-04-03 23:14:49 +0000 | [diff] [blame] | 308 | Alignment = Size; |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 309 | return; |
| 310 | } |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 311 | case Type::StructTyID: { |
| 312 | // Get the layout annotation... which is lazily created on demand. |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 313 | const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty)); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 314 | Size = Layout->StructSize; Alignment = Layout->StructAlignment; |
| 315 | return; |
| 316 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 317 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 318 | default: |
| 319 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 324 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 325 | uint64_t Size; |
| 326 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 327 | getTypeInfo(Ty, this, Size, Align); |
| 328 | return Size; |
| 329 | } |
| 330 | |
| 331 | unsigned char TargetData::getTypeAlignment(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 332 | uint64_t Size; |
| 333 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 334 | getTypeInfo(Ty, this, Size, Align); |
| 335 | return Align; |
| 336 | } |
| 337 | |
Chris Lattner | d2b0bb4 | 2004-08-17 19:13:00 +0000 | [diff] [blame] | 338 | unsigned char TargetData::getTypeAlignmentShift(const Type *Ty) const { |
| 339 | unsigned Align = getTypeAlignment(Ty); |
| 340 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 341 | return Log2_32(Align); |
Chris Lattner | d2b0bb4 | 2004-08-17 19:13:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 344 | /// getIntPtrType - Return an unsigned integer type that is the same size or |
| 345 | /// greater to the host pointer size. |
| 346 | const Type *TargetData::getIntPtrType() const { |
| 347 | switch (getPointerSize()) { |
| 348 | default: assert(0 && "Unknown pointer size!"); |
| 349 | case 2: return Type::UShortTy; |
| 350 | case 4: return Type::UIntTy; |
| 351 | case 8: return Type::ULongTy; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 356 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 357 | const std::vector<Value*> &Idx) const { |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 358 | const Type *Ty = ptrTy; |
| 359 | assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 360 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 362 | generic_gep_type_iterator<std::vector<Value*>::const_iterator> |
| 363 | TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end()); |
| 364 | for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) { |
| 365 | if (const StructType *STy = dyn_cast<StructType>(*TI)) { |
| 366 | assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 367 | unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 368 | |
| 369 | // Get structure layout information... |
| 370 | const StructLayout *Layout = getStructLayout(STy); |
| 371 | |
| 372 | // Add in the offset, as calculated by the structure layout info... |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 373 | assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 374 | Result += Layout->MemberOffsets[FieldNo]; |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 375 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 376 | // Update Ty to refer to current element |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 377 | Ty = STy->getElementType(FieldNo); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 378 | } else { |
| 379 | // Update Ty to refer to current element |
| 380 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 381 | |
| 382 | // Get the array index and the size of each array element. |
| 383 | int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue(); |
| 384 | Result += arrayIdx * (int64_t)getTypeSize(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | return Result; |
| 389 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 390 | |