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 | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 37 | static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD, |
| 38 | uint64_t &Size, unsigned char &Alignment); |
| 39 | |
| 40 | static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD, |
| 41 | uint64_t &Size, unsigned char &Alignment); |
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 | // Support for StructLayout |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 47 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 48 | StructAlignment = 0; |
| 49 | StructSize = 0; |
| 50 | |
| 51 | // Loop over each of the elements, placing them in memory... |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 52 | for (StructType::element_iterator TI = ST->element_begin(), |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 53 | TE = ST->element_end(); TI != TE; ++TI) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 54 | const Type *Ty = *TI; |
| 55 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 56 | unsigned TyAlign; |
| 57 | uint64_t TySize; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 58 | getTypeInfoABI(Ty, &TD, TySize, A); |
Andrew Lenharth | 38ecbf1 | 2006-12-08 18:06:16 +0000 | [diff] [blame] | 59 | TyAlign = ST->isPacked() ? 1 : A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 60 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 61 | // Add padding if necessary to make the data element aligned properly... |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 62 | if (StructSize % TyAlign != 0) |
| 63 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 64 | |
| 65 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 66 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 67 | |
| 68 | MemberOffsets.push_back(StructSize); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 69 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 4e840d4 | 2003-05-21 18:08:44 +0000 | [diff] [blame] | 72 | // Empty structures have alignment of 1 byte. |
| 73 | if (StructAlignment == 0) StructAlignment = 1; |
| 74 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 75 | // 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 Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 81 | |
| 82 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 83 | /// return the structure index that contains it. |
| 84 | unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { |
| 85 | std::vector<uint64_t>::const_iterator SI = |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 86 | std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(), Offset); |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 87 | 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 Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | // TargetData Class Implementation |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Chris Lattner | acbc07a | 2006-06-16 18:11:26 +0000 | [diff] [blame] | 100 | void TargetData::init(const std::string &TargetDescription) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 101 | std::string temp = TargetDescription; |
| 102 | |
| 103 | LittleEndian = false; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 104 | PointerMemSize = 8; |
| 105 | PointerABIAlignment = 8; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 106 | DoubleABIAlignment = 0; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 107 | FloatABIAlignment = 4; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 108 | LongABIAlignment = 0; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 109 | IntABIAlignment = 4; |
| 110 | ShortABIAlignment = 2; |
| 111 | ByteABIAlignment = 1; |
| 112 | BoolABIAlignment = 1; |
| 113 | BoolPrefAlignment = BoolABIAlignment; |
| 114 | BytePrefAlignment = ByteABIAlignment; |
| 115 | ShortPrefAlignment = ShortABIAlignment; |
| 116 | IntPrefAlignment = IntABIAlignment; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 117 | LongPrefAlignment = 8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 118 | FloatPrefAlignment = FloatABIAlignment; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 119 | DoublePrefAlignment = 8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 120 | PointerPrefAlignment = PointerABIAlignment; |
| 121 | AggMinPrefAlignment = 0; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 122 | |
Owen Anderson | d988b32 | 2006-05-20 00:24:56 +0000 | [diff] [blame] | 123 | while (!temp.empty()) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 124 | std::string token = getToken(temp, "-"); |
| 125 | |
Owen Anderson | 84cc6db | 2006-05-17 21:56:02 +0000 | [diff] [blame] | 126 | char signal = getToken(token, ":")[0]; |
| 127 | |
| 128 | switch(signal) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 129 | case 'E': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 130 | LittleEndian = false; |
| 131 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 132 | case 'e': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 133 | LittleEndian = true; |
| 134 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 135 | case 'p': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 136 | 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 Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 141 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 142 | case 'd': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 143 | DoubleABIAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 144 | DoublePrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 145 | if (DoublePrefAlignment == 0) |
| 146 | DoublePrefAlignment = DoubleABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 147 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 148 | case 'f': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 149 | FloatABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 150 | FloatPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 151 | if (FloatPrefAlignment == 0) |
| 152 | FloatPrefAlignment = FloatABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 153 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 154 | case 'l': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 155 | LongABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 156 | LongPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 157 | if (LongPrefAlignment == 0) |
| 158 | LongPrefAlignment = LongABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 159 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 160 | case 'i': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 161 | IntABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 162 | IntPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 163 | if (IntPrefAlignment == 0) |
| 164 | IntPrefAlignment = IntABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 165 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 166 | case 's': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 167 | ShortABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 168 | ShortPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 169 | if (ShortPrefAlignment == 0) |
| 170 | ShortPrefAlignment = ShortABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 171 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 172 | case 'b': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 173 | ByteABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 174 | BytePrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 175 | if (BytePrefAlignment == 0) |
| 176 | BytePrefAlignment = ByteABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 177 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 178 | case 'B': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 179 | 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 Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 186 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 187 | default: |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 188 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 191 | |
| 192 | // Unless explicitly specified, the alignments for longs and doubles is capped by |
| 193 | // pointer size. |
| 194 | if (LongABIAlignment == 0) |
| 195 | LongABIAlignment = LongPrefAlignment = PointerMemSize; |
| 196 | if (DoubleABIAlignment == 0) |
| 197 | DoubleABIAlignment = DoublePrefAlignment = PointerMemSize; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | 1790d44 | 2006-06-16 18:22:52 +0000 | [diff] [blame] | 200 | TargetData::TargetData(const Module *M) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 201 | LittleEndian = M->getEndianness() != Module::BigEndian; |
| 202 | PointerMemSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8; |
| 203 | PointerABIAlignment = PointerMemSize; |
| 204 | DoubleABIAlignment = PointerMemSize; |
| 205 | FloatABIAlignment = 4; |
| 206 | LongABIAlignment = PointerMemSize; |
| 207 | IntABIAlignment = 4; |
| 208 | ShortABIAlignment = 2; |
| 209 | ByteABIAlignment = 1; |
| 210 | BoolABIAlignment = 1; |
| 211 | BoolPrefAlignment = BoolABIAlignment; |
| 212 | BytePrefAlignment = ByteABIAlignment; |
| 213 | ShortPrefAlignment = ShortABIAlignment; |
| 214 | IntPrefAlignment = IntABIAlignment; |
| 215 | LongPrefAlignment = LongABIAlignment; |
| 216 | FloatPrefAlignment = FloatABIAlignment; |
| 217 | DoublePrefAlignment = DoubleABIAlignment; |
| 218 | PointerPrefAlignment = PointerABIAlignment; |
| 219 | AggMinPrefAlignment = 0; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | 8dff24f | 2006-01-14 00:07:34 +0000 | [diff] [blame] | 222 | /// Layouts - The lazy cache of structure layout information maintained by |
| 223 | /// TargetData. |
| 224 | /// |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 225 | static std::map<std::pair<const TargetData*,const StructType*>, |
| 226 | StructLayout> *Layouts = 0; |
| 227 | |
| 228 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 229 | TargetData::~TargetData() { |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 230 | if (Layouts) { |
| 231 | // Remove any layouts for this TD. |
| 232 | std::map<std::pair<const TargetData*, |
| 233 | const StructType*>, StructLayout>::iterator |
| 234 | I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0)); |
| 235 | while (I != Layouts->end() && I->first.first == this) |
| 236 | Layouts->erase(I++); |
| 237 | if (Layouts->empty()) { |
| 238 | delete Layouts; |
| 239 | Layouts = 0; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 244 | std::string TargetData::getStringRepresentation() const { |
| 245 | std::stringstream repr; |
| 246 | |
| 247 | if (LittleEndian) |
| 248 | repr << "e"; |
| 249 | else |
| 250 | repr << "E"; |
| 251 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 252 | repr << "-p:" << (PointerMemSize * 8) << ":" << (PointerABIAlignment * 8); |
| 253 | repr << "-d:" << (DoubleABIAlignment * 8) << ":" |
| 254 | << (DoublePrefAlignment * 8); |
| 255 | repr << "-f:" << (FloatABIAlignment * 8) << ":" |
| 256 | << (FloatPrefAlignment * 8); |
| 257 | repr << "-l:" << (LongABIAlignment * 8) << ":" |
| 258 | << (LongPrefAlignment * 8); |
| 259 | repr << "-i:" << (IntABIAlignment * 8) << ":" |
| 260 | << (IntPrefAlignment * 8); |
| 261 | repr << "-s:" << (ShortABIAlignment * 8) << ":" |
| 262 | << (ShortPrefAlignment * 8); |
| 263 | repr << "-b:" << (ByteABIAlignment * 8) << ":" |
| 264 | << (BytePrefAlignment * 8); |
| 265 | repr << "-B:" << (BoolABIAlignment * 8) << ":" |
| 266 | << (BoolPrefAlignment * 8); |
| 267 | repr << "-A:" << (AggMinPrefAlignment * 8); |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 268 | |
| 269 | return repr.str(); |
| 270 | } |
| 271 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 272 | const StructLayout *TargetData::getStructLayout(const StructType *Ty) const { |
| 273 | if (Layouts == 0) |
| 274 | Layouts = new std::map<std::pair<const TargetData*,const StructType*>, |
| 275 | StructLayout>(); |
| 276 | std::map<std::pair<const TargetData*,const StructType*>, |
| 277 | StructLayout>::iterator |
| 278 | I = Layouts->lower_bound(std::make_pair(this, Ty)); |
| 279 | if (I != Layouts->end() && I->first.first == this && I->first.second == Ty) |
| 280 | return &I->second; |
| 281 | else { |
| 282 | return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty), |
| 283 | StructLayout(Ty, *this)))->second; |
| 284 | } |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | 8dff24f | 2006-01-14 00:07:34 +0000 | [diff] [blame] | 287 | /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout |
| 288 | /// objects. If a TargetData object is alive when types are being refined and |
| 289 | /// removed, this method must be called whenever a StructType is removed to |
| 290 | /// avoid a dangling pointer in this cache. |
| 291 | void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const { |
| 292 | if (!Layouts) return; // No cache. |
| 293 | |
| 294 | std::map<std::pair<const TargetData*,const StructType*>, |
| 295 | StructLayout>::iterator I = Layouts->find(std::make_pair(this, Ty)); |
| 296 | if (I != Layouts->end()) |
| 297 | Layouts->erase(I); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 302 | static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD, |
| 303 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 304 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 305 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 306 | case Type::IntegerTyID: { |
| 307 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 308 | if (BitWidth <= 8) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 309 | Size = 1; Alignment = TD->getByteABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 310 | } else if (BitWidth <= 16) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 311 | Size = 2; Alignment = TD->getShortABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 312 | } else if (BitWidth <= 32) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 313 | Size = 4; Alignment = TD->getIntABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 314 | } else if (BitWidth <= 64) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 315 | Size = 8; Alignment = TD->getLongABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 316 | } else |
| 317 | assert(0 && "Integer types > 64 bits not supported."); |
| 318 | return; |
| 319 | } |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 320 | case Type::VoidTyID: Size = 1; Alignment = TD->getByteABIAlignment(); return; |
| 321 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatABIAlignment(); return; |
| 322 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleABIAlignment(); return; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 323 | case Type::LabelTyID: |
| 324 | case Type::PointerTyID: |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 325 | Size = TD->getPointerSize(); Alignment = TD->getPointerABIAlignment(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 326 | return; |
| 327 | case Type::ArrayTyID: { |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 328 | const ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 329 | getTypeInfoABI(ATy->getElementType(), TD, Size, Alignment); |
Brian Gaeke | e0e3589 | 2004-07-02 07:01:31 +0000 | [diff] [blame] | 330 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 331 | Size = AlignedSize*ATy->getNumElements(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 332 | return; |
| 333 | } |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 334 | case Type::PackedTyID: { |
| 335 | const PackedType *PTy = cast<PackedType>(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 336 | getTypeInfoABI(PTy->getElementType(), TD, Size, Alignment); |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 337 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 338 | Size = AlignedSize*PTy->getNumElements(); |
Evan Cheng | e668bda | 2006-03-31 22:33:42 +0000 | [diff] [blame] | 339 | // FIXME: The alignments of specific packed types are target dependent. |
| 340 | // For now, just set it to be equal to Size. |
Chris Lattner | 0aab36f | 2006-04-03 23:14:49 +0000 | [diff] [blame] | 341 | Alignment = Size; |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 344 | case Type::StructTyID: { |
| 345 | // Get the layout annotation... which is lazily created on demand. |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 346 | const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty)); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 347 | Size = Layout->StructSize; Alignment = Layout->StructAlignment; |
| 348 | return; |
| 349 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 350 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 351 | default: |
| 352 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 353 | return; |
| 354 | } |
| 355 | } |
| 356 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 357 | static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD, |
| 358 | uint64_t &Size, unsigned char &Alignment) { |
| 359 | assert(Ty->isSized() && "Cannot getTypeInfoPref() on a type that is unsized!"); |
| 360 | switch (Ty->getTypeID()) { |
| 361 | case Type::IntegerTyID: { |
| 362 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 363 | if (BitWidth <= 8) { |
| 364 | Size = 1; Alignment = TD->getBytePrefAlignment(); |
| 365 | } else if (BitWidth <= 16) { |
| 366 | Size = 2; Alignment = TD->getShortPrefAlignment(); |
| 367 | } else if (BitWidth <= 32) { |
| 368 | Size = 4; Alignment = TD->getIntPrefAlignment(); |
| 369 | } else if (BitWidth <= 64) { |
| 370 | Size = 8; Alignment = TD->getLongPrefAlignment(); |
| 371 | } else |
| 372 | assert(0 && "Integer types > 64 bits not supported."); |
| 373 | return; |
| 374 | } |
| 375 | case Type::VoidTyID: |
| 376 | Size = 1; Alignment = TD->getBytePrefAlignment(); |
| 377 | return; |
| 378 | case Type::FloatTyID: |
| 379 | Size = 4; Alignment = TD->getFloatPrefAlignment(); |
| 380 | return; |
| 381 | case Type::DoubleTyID: |
| 382 | Size = 8; Alignment = TD->getDoublePrefAlignment(); |
| 383 | return; |
| 384 | case Type::LabelTyID: |
| 385 | case Type::PointerTyID: |
| 386 | Size = TD->getPointerSize(); Alignment = TD->getPointerPrefAlignment(); |
| 387 | return; |
| 388 | case Type::ArrayTyID: { |
| 389 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 390 | getTypeInfoPref(ATy->getElementType(), TD, Size, Alignment); |
| 391 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 392 | Size = AlignedSize*ATy->getNumElements(); |
| 393 | return; |
| 394 | } |
| 395 | case Type::PackedTyID: { |
| 396 | const PackedType *PTy = cast<PackedType>(Ty); |
| 397 | getTypeInfoPref(PTy->getElementType(), TD, Size, Alignment); |
| 398 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 399 | Size = AlignedSize*PTy->getNumElements(); |
| 400 | // FIXME: The alignments of specific packed types are target dependent. |
| 401 | // For now, just set it to be equal to Size. |
| 402 | Alignment = Size; |
| 403 | return; |
| 404 | } |
| 405 | case Type::StructTyID: { |
| 406 | // Get the layout annotation... which is lazily created on demand; |
| 407 | // enforce minimum aggregate alignment. |
| 408 | const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty)); |
| 409 | Size = Layout->StructSize; |
| 410 | Alignment = std::max(Layout->StructAlignment, |
| 411 | (const unsigned int) TD->getAggMinPrefAlignment()); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | default: |
| 416 | assert(0 && "Bad type for getTypeInfoPref!!!"); |
| 417 | return; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 422 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 423 | uint64_t Size; |
| 424 | unsigned char Align; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 425 | getTypeInfoABI(Ty, this, Size, Align); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 426 | return Size; |
| 427 | } |
| 428 | |
Reid Spencer | 7c29243 | 2007-01-20 23:32:04 +0000 | [diff] [blame] | 429 | uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const { |
| 430 | if (Ty->isInteger()) |
| 431 | return cast<IntegerType>(Ty)->getBitWidth(); |
| 432 | |
| 433 | uint64_t Size; |
| 434 | unsigned char Align; |
| 435 | getTypeInfoABI(Ty, this, Size, Align); |
| 436 | return Size * 8; |
| 437 | } |
| 438 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 439 | unsigned char TargetData::getTypeAlignmentABI(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 440 | uint64_t Size; |
| 441 | unsigned char Align; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 442 | getTypeInfoABI(Ty, this, Size, Align); |
| 443 | return Align; |
| 444 | } |
| 445 | |
| 446 | unsigned char TargetData::getTypeAlignmentPref(const Type *Ty) const { |
| 447 | uint64_t Size; |
| 448 | unsigned char Align; |
| 449 | getTypeInfoPref(Ty, this, Size, Align); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 450 | return Align; |
| 451 | } |
| 452 | |
Evan Cheng | de268f7 | 2007-01-24 07:03:39 +0000 | [diff] [blame] | 453 | unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const { |
Evan Cheng | 778900a | 2007-01-22 23:08:19 +0000 | [diff] [blame] | 454 | unsigned Align = getTypeAlignmentPref(Ty); |
Chris Lattner | d2b0bb4 | 2004-08-17 19:13:00 +0000 | [diff] [blame] | 455 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 456 | return Log2_32(Align); |
Chris Lattner | d2b0bb4 | 2004-08-17 19:13:00 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 459 | /// getIntPtrType - Return an unsigned integer type that is the same size or |
| 460 | /// greater to the host pointer size. |
| 461 | const Type *TargetData::getIntPtrType() const { |
| 462 | switch (getPointerSize()) { |
| 463 | default: assert(0 && "Unknown pointer size!"); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 464 | case 2: return Type::Int16Ty; |
| 465 | case 4: return Type::Int32Ty; |
| 466 | case 8: return Type::Int64Ty; |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 471 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, |
Misha Brukman | c8e8764 | 2004-07-23 01:09:52 +0000 | [diff] [blame] | 472 | const std::vector<Value*> &Idx) const { |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 473 | const Type *Ty = ptrTy; |
| 474 | assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 475 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 477 | generic_gep_type_iterator<std::vector<Value*>::const_iterator> |
| 478 | TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end()); |
| 479 | for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) { |
| 480 | if (const StructType *STy = dyn_cast<StructType>(*TI)) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 481 | assert(Idx[CurIDX]->getType() == Type::Int32Ty && "Illegal struct idx"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 482 | unsigned FieldNo = cast<ConstantInt>(Idx[CurIDX])->getZExtValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 483 | |
| 484 | // Get structure layout information... |
| 485 | const StructLayout *Layout = getStructLayout(STy); |
| 486 | |
| 487 | // Add in the offset, as calculated by the structure layout info... |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 488 | assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 489 | Result += Layout->MemberOffsets[FieldNo]; |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 490 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 491 | // Update Ty to refer to current element |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 492 | Ty = STy->getElementType(FieldNo); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 493 | } else { |
| 494 | // Update Ty to refer to current element |
| 495 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 496 | |
| 497 | // Get the array index and the size of each array element. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 498 | int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getSExtValue(); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 499 | Result += arrayIdx * (int64_t)getTypeSize(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | return Result; |
| 504 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 505 | |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 506 | /// getPreferredAlignmentLog - Return the preferred alignment of the |
| 507 | /// specified global, returned in log form. This includes an explicitly |
| 508 | /// requested alignment (if the global has one). |
| 509 | unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const { |
| 510 | const Type *ElemType = GV->getType()->getElementType(); |
Evan Cheng | de268f7 | 2007-01-24 07:03:39 +0000 | [diff] [blame] | 511 | unsigned Alignment = getPreferredTypeAlignmentShift(ElemType); |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 512 | if (GV->getAlignment() > (1U << Alignment)) |
| 513 | Alignment = Log2_32(GV->getAlignment()); |
| 514 | |
| 515 | if (GV->hasInitializer()) { |
| 516 | // Always round up alignment of global doubles to 8 bytes. |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 517 | if (Alignment < 4) { |
| 518 | // If the global is not external, see if it is large. If so, give it a |
| 519 | // larger alignment. |
| 520 | if (getTypeSize(ElemType) > 128) |
| 521 | Alignment = 4; // 16-byte alignment. |
| 522 | } |
| 523 | } |
| 524 | return Alignment; |
| 525 | } |
| 526 | |