Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 1 | //===-- DataLayout.cpp - Data size & alignment routines --------------------==// |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 10 | // This file defines layout properties related to datatype size/offset/alignment |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 11 | // information. |
| 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 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 19 | #include "llvm/DataLayout.h" |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 24 | #include "llvm/Support/MathExtras.h" |
| 25 | #include "llvm/Support/ManagedStatic.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/Support/Mutex.h" |
| 29 | #include "llvm/ADT/DenseMap.h" |
| 30 | #include <algorithm> |
| 31 | #include <cstdlib> |
| 32 | using namespace llvm; |
| 33 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 34 | // Handle the Pass registration stuff necessary to use DataLayout's. |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 35 | |
| 36 | // Register the default SparcV9 implementation... |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 37 | INITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true) |
| 38 | char DataLayout::ID = 0; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Support for StructLayout |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 44 | StructLayout::StructLayout(StructType *ST, const DataLayout &TD) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 45 | assert(!ST->isOpaque() && "Cannot get layout of opaque structs"); |
| 46 | StructAlignment = 0; |
| 47 | StructSize = 0; |
| 48 | NumElements = ST->getNumElements(); |
| 49 | |
| 50 | // Loop over each of the elements, placing them in memory. |
| 51 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 52 | Type *Ty = ST->getElementType(i); |
| 53 | unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty); |
| 54 | |
| 55 | // Add padding if necessary to align the data element properly. |
| 56 | if ((StructSize & (TyAlign-1)) != 0) |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 57 | StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 58 | |
| 59 | // Keep track of maximum alignment constraint. |
| 60 | StructAlignment = std::max(TyAlign, StructAlignment); |
| 61 | |
| 62 | MemberOffsets[i] = StructSize; |
| 63 | StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item |
| 64 | } |
| 65 | |
| 66 | // Empty structures have alignment of 1 byte. |
| 67 | if (StructAlignment == 0) StructAlignment = 1; |
| 68 | |
| 69 | // Add padding to the end of the struct so that it could be put in an array |
| 70 | // and all array elements would be aligned correctly. |
| 71 | if ((StructSize & (StructAlignment-1)) != 0) |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 72 | StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
| 76 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 77 | /// return the structure index that contains it. |
| 78 | unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { |
| 79 | const uint64_t *SI = |
| 80 | std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); |
| 81 | assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); |
| 82 | --SI; |
| 83 | assert(*SI <= Offset && "upper_bound didn't work"); |
| 84 | assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) && |
| 85 | (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && |
| 86 | "Upper bound didn't work!"); |
| 87 | |
| 88 | // Multiple fields can have the same offset if any of them are zero sized. |
| 89 | // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop |
| 90 | // at the i32 element, because it is the last element at that offset. This is |
| 91 | // the right one to return, because anything after it will have a higher |
| 92 | // offset, implying that this element is non-empty. |
| 93 | return SI-&MemberOffsets[0]; |
| 94 | } |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 97 | // LayoutAlignElem, LayoutAlign support |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 100 | LayoutAlignElem |
| 101 | LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 102 | unsigned pref_align, uint32_t bit_width) { |
| 103 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 104 | LayoutAlignElem retval; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 105 | retval.AlignType = align_type; |
| 106 | retval.ABIAlign = abi_align; |
| 107 | retval.PrefAlign = pref_align; |
| 108 | retval.TypeBitWidth = bit_width; |
| 109 | return retval; |
| 110 | } |
| 111 | |
| 112 | bool |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 113 | LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 114 | return (AlignType == rhs.AlignType |
| 115 | && ABIAlign == rhs.ABIAlign |
| 116 | && PrefAlign == rhs.PrefAlign |
| 117 | && TypeBitWidth == rhs.TypeBitWidth); |
| 118 | } |
| 119 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 120 | const LayoutAlignElem |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 121 | DataLayout::InvalidAlignmentElem = |
| 122 | LayoutAlignElem::get((AlignTypeEnum) -1, 0, 0, 0); |
| 123 | |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | // PointerAlignElem, PointerAlign support |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | |
| 128 | PointerAlignElem |
| 129 | PointerAlignElem::get(uint32_t addr_space, unsigned abi_align, |
| 130 | unsigned pref_align, uint32_t bit_width) { |
| 131 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
| 132 | PointerAlignElem retval; |
| 133 | retval.AddressSpace = addr_space; |
| 134 | retval.ABIAlign = abi_align; |
| 135 | retval.PrefAlign = pref_align; |
| 136 | retval.TypeBitWidth = bit_width; |
| 137 | return retval; |
| 138 | } |
| 139 | |
| 140 | bool |
| 141 | PointerAlignElem::operator==(const PointerAlignElem &rhs) const { |
| 142 | return (ABIAlign == rhs.ABIAlign |
| 143 | && AddressSpace == rhs.AddressSpace |
| 144 | && PrefAlign == rhs.PrefAlign |
| 145 | && TypeBitWidth == rhs.TypeBitWidth); |
| 146 | } |
| 147 | |
| 148 | const PointerAlignElem |
| 149 | DataLayout::InvalidPointerElem = PointerAlignElem::get(~0U, 0U, 0U, 0U); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 150 | |
| 151 | //===----------------------------------------------------------------------===// |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 152 | // DataLayout Class Implementation |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | |
| 155 | /// getInt - Get an integer ignoring errors. |
| 156 | static int getInt(StringRef R) { |
| 157 | int Result = 0; |
| 158 | R.getAsInteger(10, Result); |
| 159 | return Result; |
| 160 | } |
| 161 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 162 | void DataLayout::init() { |
| 163 | initializeDataLayoutPass(*PassRegistry::getPassRegistry()); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 164 | |
| 165 | LayoutMap = 0; |
| 166 | LittleEndian = false; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 167 | StackNaturalAlign = 0; |
| 168 | |
| 169 | // Default alignments |
| 170 | setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1 |
| 171 | setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8 |
| 172 | setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16 |
| 173 | setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32 |
| 174 | setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64 |
| 175 | setAlignment(FLOAT_ALIGN, 2, 2, 16); // half |
| 176 | setAlignment(FLOAT_ALIGN, 4, 4, 32); // float |
| 177 | setAlignment(FLOAT_ALIGN, 8, 8, 64); // double |
| 178 | setAlignment(FLOAT_ALIGN, 16, 16, 128); // ppcf128, quad, ... |
| 179 | setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ... |
| 180 | setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ... |
| 181 | setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 182 | setPointerAlignment(0, 8, 8, 8); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 185 | std::string DataLayout::parseSpecifier(StringRef Desc, DataLayout *td) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 186 | |
| 187 | if (td) |
| 188 | td->init(); |
| 189 | |
| 190 | while (!Desc.empty()) { |
| 191 | std::pair<StringRef, StringRef> Split = Desc.split('-'); |
| 192 | StringRef Token = Split.first; |
| 193 | Desc = Split.second; |
| 194 | |
| 195 | if (Token.empty()) |
| 196 | continue; |
| 197 | |
| 198 | Split = Token.split(':'); |
| 199 | StringRef Specifier = Split.first; |
| 200 | Token = Split.second; |
| 201 | |
| 202 | assert(!Specifier.empty() && "Can't be empty here"); |
| 203 | |
| 204 | switch (Specifier[0]) { |
| 205 | case 'E': |
| 206 | if (td) |
| 207 | td->LittleEndian = false; |
| 208 | break; |
| 209 | case 'e': |
| 210 | if (td) |
| 211 | td->LittleEndian = true; |
| 212 | break; |
| 213 | case 'p': { |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 214 | int AddrSpace = 0; |
| 215 | if (Specifier.size() > 1) { |
| 216 | AddrSpace = getInt(Specifier.substr(1)); |
| 217 | if (AddrSpace < 0 || AddrSpace > (1 << 24)) |
| 218 | return "Invalid address space, must be a positive 24bit integer"; |
| 219 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 220 | Split = Token.split(':'); |
| 221 | int PointerMemSizeBits = getInt(Split.first); |
| 222 | if (PointerMemSizeBits < 0 || PointerMemSizeBits % 8 != 0) |
| 223 | return "invalid pointer size, must be a positive 8-bit multiple"; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 224 | |
| 225 | // Pointer ABI alignment. |
| 226 | Split = Split.second.split(':'); |
| 227 | int PointerABIAlignBits = getInt(Split.first); |
| 228 | if (PointerABIAlignBits < 0 || PointerABIAlignBits % 8 != 0) { |
| 229 | return "invalid pointer ABI alignment, " |
| 230 | "must be a positive 8-bit multiple"; |
| 231 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 232 | |
| 233 | // Pointer preferred alignment. |
| 234 | Split = Split.second.split(':'); |
| 235 | int PointerPrefAlignBits = getInt(Split.first); |
| 236 | if (PointerPrefAlignBits < 0 || PointerPrefAlignBits % 8 != 0) { |
| 237 | return "invalid pointer preferred alignment, " |
| 238 | "must be a positive 8-bit multiple"; |
| 239 | } |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 240 | |
| 241 | if (PointerPrefAlignBits == 0) |
| 242 | PointerPrefAlignBits = PointerABIAlignBits; |
| 243 | if (td) |
| 244 | td->setPointerAlignment(AddrSpace, PointerABIAlignBits/8, |
| 245 | PointerPrefAlignBits/8, PointerMemSizeBits/8); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | case 'i': |
| 249 | case 'v': |
| 250 | case 'f': |
| 251 | case 'a': |
| 252 | case 's': { |
| 253 | AlignTypeEnum AlignType; |
| 254 | char field = Specifier[0]; |
| 255 | switch (field) { |
| 256 | default: |
| 257 | case 'i': AlignType = INTEGER_ALIGN; break; |
| 258 | case 'v': AlignType = VECTOR_ALIGN; break; |
| 259 | case 'f': AlignType = FLOAT_ALIGN; break; |
| 260 | case 'a': AlignType = AGGREGATE_ALIGN; break; |
| 261 | case 's': AlignType = STACK_ALIGN; break; |
| 262 | } |
| 263 | int Size = getInt(Specifier.substr(1)); |
| 264 | if (Size < 0) { |
| 265 | return std::string("invalid ") + field + "-size field, " |
| 266 | "must be positive"; |
| 267 | } |
| 268 | |
| 269 | Split = Token.split(':'); |
| 270 | int ABIAlignBits = getInt(Split.first); |
| 271 | if (ABIAlignBits < 0 || ABIAlignBits % 8 != 0) { |
| 272 | return std::string("invalid ") + field +"-abi-alignment field, " |
| 273 | "must be a positive 8-bit multiple"; |
| 274 | } |
| 275 | unsigned ABIAlign = ABIAlignBits / 8; |
| 276 | |
| 277 | Split = Split.second.split(':'); |
| 278 | |
| 279 | int PrefAlignBits = getInt(Split.first); |
| 280 | if (PrefAlignBits < 0 || PrefAlignBits % 8 != 0) { |
| 281 | return std::string("invalid ") + field +"-preferred-alignment field, " |
| 282 | "must be a positive 8-bit multiple"; |
| 283 | } |
| 284 | unsigned PrefAlign = PrefAlignBits / 8; |
| 285 | if (PrefAlign == 0) |
| 286 | PrefAlign = ABIAlign; |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 287 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 288 | if (td) |
| 289 | td->setAlignment(AlignType, ABIAlign, PrefAlign, Size); |
| 290 | break; |
| 291 | } |
| 292 | case 'n': // Native integer types. |
| 293 | Specifier = Specifier.substr(1); |
| 294 | do { |
| 295 | int Width = getInt(Specifier); |
| 296 | if (Width <= 0) { |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 297 | return std::string("invalid native integer size \'") + |
| 298 | Specifier.str() + "\', must be a positive integer."; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 299 | } |
| 300 | if (td && Width != 0) |
| 301 | td->LegalIntWidths.push_back(Width); |
| 302 | Split = Token.split(':'); |
| 303 | Specifier = Split.first; |
| 304 | Token = Split.second; |
| 305 | } while (!Specifier.empty() || !Token.empty()); |
| 306 | break; |
| 307 | case 'S': { // Stack natural alignment. |
| 308 | int StackNaturalAlignBits = getInt(Specifier.substr(1)); |
| 309 | if (StackNaturalAlignBits < 0 || StackNaturalAlignBits % 8 != 0) { |
| 310 | return "invalid natural stack alignment (S-field), " |
| 311 | "must be a positive 8-bit multiple"; |
| 312 | } |
| 313 | if (td) |
| 314 | td->StackNaturalAlign = StackNaturalAlignBits / 8; |
| 315 | break; |
| 316 | } |
| 317 | default: |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return ""; |
| 323 | } |
| 324 | |
| 325 | /// Default ctor. |
| 326 | /// |
| 327 | /// @note This has to exist, because this is a pass, but it should never be |
| 328 | /// used. |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 329 | DataLayout::DataLayout() : ImmutablePass(ID) { |
| 330 | report_fatal_error("Bad DataLayout ctor used. " |
| 331 | "Tool did not specify a DataLayout to use?"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 334 | DataLayout::DataLayout(const Module *M) |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 335 | : ImmutablePass(ID) { |
| 336 | std::string errMsg = parseSpecifier(M->getDataLayout(), this); |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 337 | assert(errMsg == "" && "Module M has malformed data layout string."); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 338 | (void)errMsg; |
| 339 | } |
| 340 | |
| 341 | void |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 342 | DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 343 | unsigned pref_align, uint32_t bit_width) { |
| 344 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
| 345 | assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield"); |
| 346 | assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield"); |
| 347 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { |
Micah Villmow | 6d05e69 | 2012-10-05 17:02:14 +0000 | [diff] [blame] | 348 | if (Alignments[i].AlignType == (unsigned)align_type && |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 349 | Alignments[i].TypeBitWidth == bit_width) { |
| 350 | // Update the abi, preferred alignments. |
| 351 | Alignments[i].ABIAlign = abi_align; |
| 352 | Alignments[i].PrefAlign = pref_align; |
| 353 | return; |
| 354 | } |
| 355 | } |
| 356 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 357 | Alignments.push_back(LayoutAlignElem::get(align_type, abi_align, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 358 | pref_align, bit_width)); |
| 359 | } |
| 360 | |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 361 | void |
| 362 | DataLayout::setPointerAlignment(uint32_t addr_space, unsigned abi_align, |
| 363 | unsigned pref_align, uint32_t bit_width) { |
| 364 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
| 365 | DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(addr_space); |
| 366 | if (val == Pointers.end()) { |
| 367 | Pointers[addr_space] = PointerAlignElem::get(addr_space, |
| 368 | abi_align, pref_align, bit_width); |
| 369 | } else { |
| 370 | val->second.ABIAlign = abi_align; |
| 371 | val->second.PrefAlign = pref_align; |
| 372 | val->second.TypeBitWidth = bit_width; |
| 373 | } |
| 374 | } |
| 375 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 376 | /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 377 | /// preferred if ABIInfo = false) the layout wants for the specified datatype. |
| 378 | unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 379 | uint32_t BitWidth, bool ABIInfo, |
| 380 | Type *Ty) const { |
| 381 | // Check to see if we have an exact match and remember the best match we see. |
| 382 | int BestMatchIdx = -1; |
| 383 | int LargestInt = -1; |
| 384 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { |
Micah Villmow | 6d05e69 | 2012-10-05 17:02:14 +0000 | [diff] [blame] | 385 | if (Alignments[i].AlignType == (unsigned)AlignType && |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 386 | Alignments[i].TypeBitWidth == BitWidth) |
| 387 | return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign; |
| 388 | |
| 389 | // The best match so far depends on what we're looking for. |
| 390 | if (AlignType == INTEGER_ALIGN && |
| 391 | Alignments[i].AlignType == INTEGER_ALIGN) { |
| 392 | // The "best match" for integers is the smallest size that is larger than |
| 393 | // the BitWidth requested. |
| 394 | if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 || |
| 395 | Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth)) |
| 396 | BestMatchIdx = i; |
| 397 | // However, if there isn't one that's larger, then we must use the |
| 398 | // largest one we have (see below) |
| 399 | if (LargestInt == -1 || |
| 400 | Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth) |
| 401 | LargestInt = i; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Okay, we didn't find an exact solution. Fall back here depending on what |
| 406 | // is being looked for. |
| 407 | if (BestMatchIdx == -1) { |
| 408 | // If we didn't find an integer alignment, fall back on most conservative. |
| 409 | if (AlignType == INTEGER_ALIGN) { |
| 410 | BestMatchIdx = LargestInt; |
| 411 | } else { |
| 412 | assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!"); |
| 413 | |
| 414 | // By default, use natural alignment for vector types. This is consistent |
| 415 | // with what clang and llvm-gcc do. |
| 416 | unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); |
| 417 | Align *= cast<VectorType>(Ty)->getNumElements(); |
| 418 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 419 | // This happens for non-power-of-2 length vectors. |
| 420 | if (Align & (Align-1)) |
| 421 | Align = NextPowerOf2(Align); |
| 422 | return Align; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Since we got a "best match" index, just return it. |
| 427 | return ABIInfo ? Alignments[BestMatchIdx].ABIAlign |
| 428 | : Alignments[BestMatchIdx].PrefAlign; |
| 429 | } |
| 430 | |
| 431 | namespace { |
| 432 | |
| 433 | class StructLayoutMap { |
| 434 | typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy; |
| 435 | LayoutInfoTy LayoutInfo; |
| 436 | |
| 437 | public: |
| 438 | virtual ~StructLayoutMap() { |
| 439 | // Remove any layouts. |
| 440 | for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end(); |
| 441 | I != E; ++I) { |
| 442 | StructLayout *Value = I->second; |
| 443 | Value->~StructLayout(); |
| 444 | free(Value); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | StructLayout *&operator[](StructType *STy) { |
| 449 | return LayoutInfo[STy]; |
| 450 | } |
| 451 | |
| 452 | // for debugging... |
| 453 | virtual void dump() const {} |
| 454 | }; |
| 455 | |
| 456 | } // end anonymous namespace |
| 457 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 458 | DataLayout::~DataLayout() { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 459 | delete static_cast<StructLayoutMap*>(LayoutMap); |
| 460 | } |
| 461 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 462 | const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 463 | if (!LayoutMap) |
| 464 | LayoutMap = new StructLayoutMap(); |
| 465 | |
| 466 | StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); |
| 467 | StructLayout *&SL = (*STM)[Ty]; |
| 468 | if (SL) return SL; |
| 469 | |
| 470 | // Otherwise, create the struct layout. Because it is variable length, we |
| 471 | // malloc it, then use placement new. |
| 472 | int NumElts = Ty->getNumElements(); |
| 473 | StructLayout *L = |
| 474 | (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); |
| 475 | |
| 476 | // Set SL before calling StructLayout's ctor. The ctor could cause other |
| 477 | // entries to be added to TheMap, invalidating our reference. |
| 478 | SL = L; |
| 479 | |
| 480 | new (L) StructLayout(Ty, *this); |
| 481 | |
| 482 | return L; |
| 483 | } |
| 484 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 485 | std::string DataLayout::getStringRepresentation() const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 486 | std::string Result; |
| 487 | raw_string_ostream OS(Result); |
| 488 | |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 489 | OS << (LittleEndian ? "e" : "E"); |
| 490 | SmallVector<unsigned, 8> addrSpaces; |
| 491 | // Lets get all of the known address spaces and sort them |
| 492 | // into increasing order so that we can emit the string |
| 493 | // in a cleaner format. |
| 494 | for (DenseMap<unsigned, PointerAlignElem>::const_iterator |
| 495 | pib = Pointers.begin(), pie = Pointers.end(); |
| 496 | pib != pie; ++pib) { |
| 497 | addrSpaces.push_back(pib->first); |
| 498 | } |
| 499 | std::sort(addrSpaces.begin(), addrSpaces.end()); |
| 500 | for (SmallVector<unsigned, 8>::iterator asb = addrSpaces.begin(), |
| 501 | ase = addrSpaces.end(); asb != ase; ++asb) { |
| 502 | const PointerAlignElem &PI = Pointers.find(*asb)->second; |
| 503 | OS << "-p"; |
| 504 | if (PI.AddressSpace) { |
| 505 | OS << PI.AddressSpace; |
| 506 | } |
| 507 | OS << ":" << PI.TypeBitWidth*8 << ':' << PI.ABIAlign*8 |
| 508 | << ':' << PI.PrefAlign*8; |
| 509 | } |
| 510 | OS << "-S" << StackNaturalAlign*8; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 511 | |
| 512 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 513 | const LayoutAlignElem &AI = Alignments[i]; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 514 | OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':' |
| 515 | << AI.ABIAlign*8 << ':' << AI.PrefAlign*8; |
| 516 | } |
| 517 | |
| 518 | if (!LegalIntWidths.empty()) { |
| 519 | OS << "-n" << (unsigned)LegalIntWidths[0]; |
| 520 | |
| 521 | for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i) |
| 522 | OS << ':' << (unsigned)LegalIntWidths[i]; |
| 523 | } |
| 524 | return OS.str(); |
| 525 | } |
| 526 | |
| 527 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 528 | uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 529 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
| 530 | switch (Ty->getTypeID()) { |
| 531 | case Type::LabelTyID: |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 532 | return getPointerSizeInBits(0); |
| 533 | case Type::PointerTyID: { |
| 534 | unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace(); |
| 535 | return getPointerSizeInBits(AS); |
| 536 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 537 | case Type::ArrayTyID: { |
| 538 | ArrayType *ATy = cast<ArrayType>(Ty); |
| 539 | return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements(); |
| 540 | } |
| 541 | case Type::StructTyID: |
| 542 | // Get the layout annotation... which is lazily created on demand. |
| 543 | return getStructLayout(cast<StructType>(Ty))->getSizeInBits(); |
| 544 | case Type::IntegerTyID: |
| 545 | return cast<IntegerType>(Ty)->getBitWidth(); |
| 546 | case Type::VoidTyID: |
| 547 | return 8; |
| 548 | case Type::HalfTyID: |
| 549 | return 16; |
| 550 | case Type::FloatTyID: |
| 551 | return 32; |
| 552 | case Type::DoubleTyID: |
| 553 | case Type::X86_MMXTyID: |
| 554 | return 64; |
| 555 | case Type::PPC_FP128TyID: |
| 556 | case Type::FP128TyID: |
| 557 | return 128; |
| 558 | // In memory objects this is always aligned to a higher boundary, but |
| 559 | // only 80 bits contain information. |
| 560 | case Type::X86_FP80TyID: |
| 561 | return 80; |
Hal Finkel | 8884dc3 | 2012-10-21 20:38:03 +0000 | [diff] [blame^] | 562 | case Type::VectorTyID: { |
| 563 | VectorType *VTy = cast<VectorType>(Ty); |
| 564 | return VTy->getNumElements()*getTypeSizeInBits(VTy->getElementType()); |
| 565 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 566 | default: |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 567 | llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | /*! |
| 572 | \param abi_or_pref Flag that determines which alignment is returned. true |
| 573 | returns the ABI alignment, false returns the preferred alignment. |
| 574 | \param Ty The underlying type for which alignment is determined. |
| 575 | |
| 576 | Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref |
| 577 | == false) for the requested type \a Ty. |
| 578 | */ |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 579 | unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 580 | int AlignType = -1; |
| 581 | |
| 582 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
| 583 | switch (Ty->getTypeID()) { |
| 584 | // Early escape for the non-numeric types. |
| 585 | case Type::LabelTyID: |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 586 | return (abi_or_pref |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 587 | ? getPointerABIAlignment(0) |
| 588 | : getPointerPrefAlignment(0)); |
| 589 | case Type::PointerTyID: { |
| 590 | unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace(); |
| 591 | return (abi_or_pref |
| 592 | ? getPointerABIAlignment(AS) |
| 593 | : getPointerPrefAlignment(AS)); |
| 594 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 595 | case Type::ArrayTyID: |
| 596 | return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref); |
| 597 | |
| 598 | case Type::StructTyID: { |
| 599 | // Packed structure types always have an ABI alignment of one. |
| 600 | if (cast<StructType>(Ty)->isPacked() && abi_or_pref) |
| 601 | return 1; |
| 602 | |
| 603 | // Get the layout annotation... which is lazily created on demand. |
| 604 | const StructLayout *Layout = getStructLayout(cast<StructType>(Ty)); |
| 605 | unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty); |
| 606 | return std::max(Align, Layout->getAlignment()); |
| 607 | } |
| 608 | case Type::IntegerTyID: |
| 609 | case Type::VoidTyID: |
| 610 | AlignType = INTEGER_ALIGN; |
| 611 | break; |
| 612 | case Type::HalfTyID: |
| 613 | case Type::FloatTyID: |
| 614 | case Type::DoubleTyID: |
| 615 | // PPC_FP128TyID and FP128TyID have different data contents, but the |
| 616 | // same size and alignment, so they look the same here. |
| 617 | case Type::PPC_FP128TyID: |
| 618 | case Type::FP128TyID: |
| 619 | case Type::X86_FP80TyID: |
| 620 | AlignType = FLOAT_ALIGN; |
| 621 | break; |
| 622 | case Type::X86_MMXTyID: |
| 623 | case Type::VectorTyID: |
| 624 | AlignType = VECTOR_ALIGN; |
| 625 | break; |
| 626 | default: |
| 627 | llvm_unreachable("Bad type for getAlignment!!!"); |
| 628 | } |
| 629 | |
| 630 | return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty), |
| 631 | abi_or_pref, Ty); |
| 632 | } |
| 633 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 634 | unsigned DataLayout::getABITypeAlignment(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 635 | return getAlignment(Ty, true); |
| 636 | } |
| 637 | |
| 638 | /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for |
| 639 | /// an integer type of the specified bitwidth. |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 640 | unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 641 | return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0); |
| 642 | } |
| 643 | |
| 644 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 645 | unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 646 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) |
| 647 | if (Alignments[i].AlignType == STACK_ALIGN) |
| 648 | return Alignments[i].ABIAlign; |
| 649 | |
| 650 | return getABITypeAlignment(Ty); |
| 651 | } |
| 652 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 653 | unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 654 | return getAlignment(Ty, false); |
| 655 | } |
| 656 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 657 | unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 658 | unsigned Align = getPrefTypeAlignment(Ty); |
| 659 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
| 660 | return Log2_32(Align); |
| 661 | } |
| 662 | |
| 663 | /// getIntPtrType - Return an unsigned integer type that is the same size or |
| 664 | /// greater to the host pointer size. |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 665 | IntegerType *DataLayout::getIntPtrType(LLVMContext &C, |
| 666 | unsigned AddressSpace) const { |
| 667 | return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 671 | uint64_t DataLayout::getIndexedOffset(Type *ptrTy, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 672 | ArrayRef<Value *> Indices) const { |
| 673 | Type *Ty = ptrTy; |
| 674 | assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()"); |
| 675 | uint64_t Result = 0; |
| 676 | |
| 677 | generic_gep_type_iterator<Value* const*> |
| 678 | TI = gep_type_begin(ptrTy, Indices); |
| 679 | for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX; |
| 680 | ++CurIDX, ++TI) { |
| 681 | if (StructType *STy = dyn_cast<StructType>(*TI)) { |
| 682 | assert(Indices[CurIDX]->getType() == |
| 683 | Type::getInt32Ty(ptrTy->getContext()) && |
| 684 | "Illegal struct idx"); |
| 685 | unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue(); |
| 686 | |
| 687 | // Get structure layout information... |
| 688 | const StructLayout *Layout = getStructLayout(STy); |
| 689 | |
| 690 | // Add in the offset, as calculated by the structure layout info... |
| 691 | Result += Layout->getElementOffset(FieldNo); |
| 692 | |
| 693 | // Update Ty to refer to current element |
| 694 | Ty = STy->getElementType(FieldNo); |
| 695 | } else { |
| 696 | // Update Ty to refer to current element |
| 697 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 698 | |
| 699 | // Get the array index and the size of each array element. |
| 700 | if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue()) |
| 701 | Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return Result; |
| 706 | } |
| 707 | |
| 708 | /// getPreferredAlignment - Return the preferred alignment of the specified |
| 709 | /// global. This includes an explicitly requested alignment (if the global |
| 710 | /// has one). |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 711 | unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 712 | Type *ElemType = GV->getType()->getElementType(); |
| 713 | unsigned Alignment = getPrefTypeAlignment(ElemType); |
| 714 | unsigned GVAlignment = GV->getAlignment(); |
| 715 | if (GVAlignment >= Alignment) { |
| 716 | Alignment = GVAlignment; |
| 717 | } else if (GVAlignment != 0) { |
| 718 | Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType)); |
| 719 | } |
| 720 | |
| 721 | if (GV->hasInitializer() && GVAlignment == 0) { |
| 722 | if (Alignment < 16) { |
| 723 | // If the global is not external, see if it is large. If so, give it a |
| 724 | // larger alignment. |
| 725 | if (getTypeSizeInBits(ElemType) > 128) |
| 726 | Alignment = 16; // 16-byte alignment. |
| 727 | } |
| 728 | } |
| 729 | return Alignment; |
| 730 | } |
| 731 | |
| 732 | /// getPreferredAlignmentLog - Return the preferred alignment of the |
| 733 | /// specified global, returned in log form. This includes an explicitly |
| 734 | /// requested alignment (if the global has one). |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 735 | unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 736 | return Log2_32(getPreferredAlignment(GV)); |
| 737 | } |