1 //===-- DataLayout.cpp - Data size & alignment routines --------------------==// 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 // 10 // This file defines layout properties related to datatype size/offset/alignment 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 19 #include "llvm/IR/DataLayout.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/DerivedTypes.h" 25 #include "llvm/IR/GetElementPtrTypeIterator.h" 26 #include "llvm/IR/Module.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/ManagedStatic.h" 29 #include "llvm/Support/MathExtras.h" 30 #include "llvm/Support/Mutex.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <algorithm> 33 #include <cstdlib> 34 using namespace llvm; 35 36 //===----------------------------------------------------------------------===// 37 // Support for StructLayout 38 //===----------------------------------------------------------------------===// 39 40 StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { 41 assert(!ST->isOpaque() && "Cannot get layout of opaque structs"); 42 StructAlignment = 0; 43 StructSize = 0; 44 IsPadded = false; 45 NumElements = ST->getNumElements(); 46 47 // Loop over each of the elements, placing them in memory. 48 for (unsigned i = 0, e = NumElements; i != e; ++i) { 49 Type *Ty = ST->getElementType(i); 50 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty); 51 52 // Add padding if necessary to align the data element properly. 53 if ((StructSize & (TyAlign-1)) != 0) { 54 IsPadded = true; 55 StructSize = alignTo(StructSize, TyAlign); 56 } 57 58 // Keep track of maximum alignment constraint. 59 StructAlignment = std::max(TyAlign, StructAlignment); 60 61 MemberOffsets[i] = StructSize; 62 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item 63 } 64 65 // Empty structures have alignment of 1 byte. 66 if (StructAlignment == 0) StructAlignment = 1; 67 68 // Add padding to the end of the struct so that it could be put in an array 69 // and all array elements would be aligned correctly. 70 if ((StructSize & (StructAlignment-1)) != 0) { 71 IsPadded = true; 72 StructSize = alignTo(StructSize, StructAlignment); 73 } 74 } 75 76 77 /// getElementContainingOffset - Given a valid offset into the structure, 78 /// return the structure index that contains it. 79 unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { 80 const uint64_t *SI = 81 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); 82 assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); 83 --SI; 84 assert(*SI <= Offset && "upper_bound didn't work"); 85 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) && 86 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && 87 "Upper bound didn't work!"); 88 89 // Multiple fields can have the same offset if any of them are zero sized. 90 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop 91 // at the i32 element, because it is the last element at that offset. This is 92 // the right one to return, because anything after it will have a higher 93 // offset, implying that this element is non-empty. 94 return SI-&MemberOffsets[0]; 95 } 96 97 //===----------------------------------------------------------------------===// 98 // LayoutAlignElem, LayoutAlign support 99 //===----------------------------------------------------------------------===// 100 101 LayoutAlignElem 102 LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align, 103 unsigned pref_align, uint32_t bit_width) { 104 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); 105 LayoutAlignElem retval; 106 retval.AlignType = align_type; 107 retval.ABIAlign = abi_align; 108 retval.PrefAlign = pref_align; 109 retval.TypeBitWidth = bit_width; 110 return retval; 111 } 112 113 bool 114 LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const { 115 return (AlignType == rhs.AlignType 116 && ABIAlign == rhs.ABIAlign 117 && PrefAlign == rhs.PrefAlign 118 && TypeBitWidth == rhs.TypeBitWidth); 119 } 120 121 //===----------------------------------------------------------------------===// 122 // PointerAlignElem, PointerAlign support 123 //===----------------------------------------------------------------------===// 124 125 PointerAlignElem 126 PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign, 127 unsigned PrefAlign, uint32_t TypeByteWidth) { 128 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!"); 129 PointerAlignElem retval; 130 retval.AddressSpace = AddressSpace; 131 retval.ABIAlign = ABIAlign; 132 retval.PrefAlign = PrefAlign; 133 retval.TypeByteWidth = TypeByteWidth; 134 return retval; 135 } 136 137 bool 138 PointerAlignElem::operator==(const PointerAlignElem &rhs) const { 139 return (ABIAlign == rhs.ABIAlign 140 && AddressSpace == rhs.AddressSpace 141 && PrefAlign == rhs.PrefAlign 142 && TypeByteWidth == rhs.TypeByteWidth); 143 } 144 145 //===----------------------------------------------------------------------===// 146 // DataLayout Class Implementation 147 //===----------------------------------------------------------------------===// 148 149 const char *DataLayout::getManglingComponent(const Triple &T) { 150 if (T.isOSBinFormatMachO()) 151 return "-m:o"; 152 if (T.isOSWindows() && T.isOSBinFormatCOFF()) 153 return T.getArch() == Triple::x86 ? "-m:x" : "-m:w"; 154 return "-m:e"; 155 } 156 157 static const LayoutAlignElem DefaultAlignments[] = { 158 { INTEGER_ALIGN, 1, 1, 1 }, // i1 159 { INTEGER_ALIGN, 8, 1, 1 }, // i8 160 { INTEGER_ALIGN, 16, 2, 2 }, // i16 161 { INTEGER_ALIGN, 32, 4, 4 }, // i32 162 { INTEGER_ALIGN, 64, 4, 8 }, // i64 163 { FLOAT_ALIGN, 16, 2, 2 }, // half 164 { FLOAT_ALIGN, 32, 4, 4 }, // float 165 { FLOAT_ALIGN, 64, 8, 8 }, // double 166 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ... 167 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ... 168 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ... 169 { AGGREGATE_ALIGN, 0, 0, 8 } // struct 170 }; 171 172 void DataLayout::reset(StringRef Desc) { 173 clear(); 174 175 LayoutMap = nullptr; 176 BigEndian = false; 177 StackNaturalAlign = 0; 178 ManglingMode = MM_None; 179 NonIntegralAddressSpaces.clear(); 180 181 // Default alignments 182 for (const LayoutAlignElem &E : DefaultAlignments) { 183 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign, 184 E.TypeBitWidth); 185 } 186 setPointerAlignment(0, 8, 8, 8); 187 188 parseSpecifier(Desc); 189 } 190 191 /// Checked version of split, to ensure mandatory subparts. 192 static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) { 193 assert(!Str.empty() && "parse error, string can't be empty here"); 194 std::pair<StringRef, StringRef> Split = Str.split(Separator); 195 if (Split.second.empty() && Split.first != Str) 196 report_fatal_error("Trailing separator in datalayout string"); 197 if (!Split.second.empty() && Split.first.empty()) 198 report_fatal_error("Expected token before separator in datalayout string"); 199 return Split; 200 } 201 202 /// Get an unsigned integer, including error checks. 203 static unsigned getInt(StringRef R) { 204 unsigned Result; 205 bool error = R.getAsInteger(10, Result); (void)error; 206 if (error) 207 report_fatal_error("not a number, or does not fit in an unsigned int"); 208 return Result; 209 } 210 211 /// Convert bits into bytes. Assert if not a byte width multiple. 212 static unsigned inBytes(unsigned Bits) { 213 if (Bits % 8) 214 report_fatal_error("number of bits must be a byte width multiple"); 215 return Bits / 8; 216 } 217 218 void DataLayout::parseSpecifier(StringRef Desc) { 219 StringRepresentation = Desc; 220 while (!Desc.empty()) { 221 // Split at '-'. 222 std::pair<StringRef, StringRef> Split = split(Desc, '-'); 223 Desc = Split.second; 224 225 // Split at ':'. 226 Split = split(Split.first, ':'); 227 228 // Aliases used below. 229 StringRef &Tok = Split.first; // Current token. 230 StringRef &Rest = Split.second; // The rest of the string. 231 232 if (Tok == "ni") { 233 do { 234 Split = split(Rest, ':'); 235 Rest = Split.second; 236 unsigned AS = getInt(Split.first); 237 if (AS == 0) 238 report_fatal_error("Address space 0 can never be non-integral"); 239 NonIntegralAddressSpaces.push_back(AS); 240 } while (!Rest.empty()); 241 242 continue; 243 } 244 245 char Specifier = Tok.front(); 246 Tok = Tok.substr(1); 247 248 switch (Specifier) { 249 case 's': 250 // Ignored for backward compatibility. 251 // FIXME: remove this on LLVM 4.0. 252 break; 253 case 'E': 254 BigEndian = true; 255 break; 256 case 'e': 257 BigEndian = false; 258 break; 259 case 'p': { 260 // Address space. 261 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); 262 if (!isUInt<24>(AddrSpace)) 263 report_fatal_error("Invalid address space, must be a 24bit integer"); 264 265 // Size. 266 if (Rest.empty()) 267 report_fatal_error( 268 "Missing size specification for pointer in datalayout string"); 269 Split = split(Rest, ':'); 270 unsigned PointerMemSize = inBytes(getInt(Tok)); 271 if (!PointerMemSize) 272 report_fatal_error("Invalid pointer size of 0 bytes"); 273 274 // ABI alignment. 275 if (Rest.empty()) 276 report_fatal_error( 277 "Missing alignment specification for pointer in datalayout string"); 278 Split = split(Rest, ':'); 279 unsigned PointerABIAlign = inBytes(getInt(Tok)); 280 if (!isPowerOf2_64(PointerABIAlign)) 281 report_fatal_error( 282 "Pointer ABI alignment must be a power of 2"); 283 284 // Preferred alignment. 285 unsigned PointerPrefAlign = PointerABIAlign; 286 if (!Rest.empty()) { 287 Split = split(Rest, ':'); 288 PointerPrefAlign = inBytes(getInt(Tok)); 289 if (!isPowerOf2_64(PointerPrefAlign)) 290 report_fatal_error( 291 "Pointer preferred alignment must be a power of 2"); 292 } 293 294 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign, 295 PointerMemSize); 296 break; 297 } 298 case 'i': 299 case 'v': 300 case 'f': 301 case 'a': { 302 AlignTypeEnum AlignType; 303 switch (Specifier) { 304 default: 305 case 'i': AlignType = INTEGER_ALIGN; break; 306 case 'v': AlignType = VECTOR_ALIGN; break; 307 case 'f': AlignType = FLOAT_ALIGN; break; 308 case 'a': AlignType = AGGREGATE_ALIGN; break; 309 } 310 311 // Bit size. 312 unsigned Size = Tok.empty() ? 0 : getInt(Tok); 313 314 if (AlignType == AGGREGATE_ALIGN && Size != 0) 315 report_fatal_error( 316 "Sized aggregate specification in datalayout string"); 317 318 // ABI alignment. 319 if (Rest.empty()) 320 report_fatal_error( 321 "Missing alignment specification in datalayout string"); 322 Split = split(Rest, ':'); 323 unsigned ABIAlign = inBytes(getInt(Tok)); 324 if (AlignType != AGGREGATE_ALIGN && !ABIAlign) 325 report_fatal_error( 326 "ABI alignment specification must be >0 for non-aggregate types"); 327 328 // Preferred alignment. 329 unsigned PrefAlign = ABIAlign; 330 if (!Rest.empty()) { 331 Split = split(Rest, ':'); 332 PrefAlign = inBytes(getInt(Tok)); 333 } 334 335 setAlignment(AlignType, ABIAlign, PrefAlign, Size); 336 337 break; 338 } 339 case 'n': // Native integer types. 340 for (;;) { 341 unsigned Width = getInt(Tok); 342 if (Width == 0) 343 report_fatal_error( 344 "Zero width native integer type in datalayout string"); 345 LegalIntWidths.push_back(Width); 346 if (Rest.empty()) 347 break; 348 Split = split(Rest, ':'); 349 } 350 break; 351 case 'S': { // Stack natural alignment. 352 StackNaturalAlign = inBytes(getInt(Tok)); 353 break; 354 } 355 case 'm': 356 if (!Tok.empty()) 357 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string"); 358 if (Rest.empty()) 359 report_fatal_error("Expected mangling specifier in datalayout string"); 360 if (Rest.size() > 1) 361 report_fatal_error("Unknown mangling specifier in datalayout string"); 362 switch(Rest[0]) { 363 default: 364 report_fatal_error("Unknown mangling in datalayout string"); 365 case 'e': 366 ManglingMode = MM_ELF; 367 break; 368 case 'o': 369 ManglingMode = MM_MachO; 370 break; 371 case 'm': 372 ManglingMode = MM_Mips; 373 break; 374 case 'w': 375 ManglingMode = MM_WinCOFF; 376 break; 377 case 'x': 378 ManglingMode = MM_WinCOFFX86; 379 break; 380 } 381 break; 382 default: 383 report_fatal_error("Unknown specifier in datalayout string"); 384 break; 385 } 386 } 387 } 388 389 DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) { 390 init(M); 391 } 392 393 void DataLayout::init(const Module *M) { *this = M->getDataLayout(); } 394 395 bool DataLayout::operator==(const DataLayout &Other) const { 396 bool Ret = BigEndian == Other.BigEndian && 397 StackNaturalAlign == Other.StackNaturalAlign && 398 ManglingMode == Other.ManglingMode && 399 LegalIntWidths == Other.LegalIntWidths && 400 Alignments == Other.Alignments && Pointers == Other.Pointers; 401 // Note: getStringRepresentation() might differs, it is not canonicalized 402 return Ret; 403 } 404 405 DataLayout::AlignmentsTy::iterator 406 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType, 407 uint32_t BitWidth) { 408 auto Pair = std::make_pair((unsigned)AlignType, BitWidth); 409 return std::lower_bound(Alignments.begin(), Alignments.end(), Pair, 410 [](const LayoutAlignElem &LHS, 411 const std::pair<unsigned, uint32_t> &RHS) { 412 return std::tie(LHS.AlignType, LHS.TypeBitWidth) < 413 std::tie(RHS.first, RHS.second); 414 }); 415 } 416 417 void 418 DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, 419 unsigned pref_align, uint32_t bit_width) { 420 if (!isUInt<24>(bit_width)) 421 report_fatal_error("Invalid bit width, must be a 24bit integer"); 422 if (!isUInt<16>(abi_align)) 423 report_fatal_error("Invalid ABI alignment, must be a 16bit integer"); 424 if (!isUInt<16>(pref_align)) 425 report_fatal_error("Invalid preferred alignment, must be a 16bit integer"); 426 if (abi_align != 0 && !isPowerOf2_64(abi_align)) 427 report_fatal_error("Invalid ABI alignment, must be a power of 2"); 428 if (pref_align != 0 && !isPowerOf2_64(pref_align)) 429 report_fatal_error("Invalid preferred alignment, must be a power of 2"); 430 431 if (pref_align < abi_align) 432 report_fatal_error( 433 "Preferred alignment cannot be less than the ABI alignment"); 434 435 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width); 436 if (I != Alignments.end() && 437 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) { 438 // Update the abi, preferred alignments. 439 I->ABIAlign = abi_align; 440 I->PrefAlign = pref_align; 441 } else { 442 // Insert before I to keep the vector sorted. 443 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align, 444 pref_align, bit_width)); 445 } 446 } 447 448 DataLayout::PointersTy::iterator 449 DataLayout::findPointerLowerBound(uint32_t AddressSpace) { 450 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace, 451 [](const PointerAlignElem &A, uint32_t AddressSpace) { 452 return A.AddressSpace < AddressSpace; 453 }); 454 } 455 456 void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, 457 unsigned PrefAlign, 458 uint32_t TypeByteWidth) { 459 if (PrefAlign < ABIAlign) 460 report_fatal_error( 461 "Preferred alignment cannot be less than the ABI alignment"); 462 463 PointersTy::iterator I = findPointerLowerBound(AddrSpace); 464 if (I == Pointers.end() || I->AddressSpace != AddrSpace) { 465 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, 466 TypeByteWidth)); 467 } else { 468 I->ABIAlign = ABIAlign; 469 I->PrefAlign = PrefAlign; 470 I->TypeByteWidth = TypeByteWidth; 471 } 472 } 473 474 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or 475 /// preferred if ABIInfo = false) the layout wants for the specified datatype. 476 unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, 477 uint32_t BitWidth, bool ABIInfo, 478 Type *Ty) const { 479 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth); 480 // See if we found an exact match. Of if we are looking for an integer type, 481 // but don't have an exact match take the next largest integer. This is where 482 // the lower_bound will point to when it fails an exact match. 483 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType && 484 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN)) 485 return ABIInfo ? I->ABIAlign : I->PrefAlign; 486 487 if (AlignType == INTEGER_ALIGN) { 488 // If we didn't have a larger value try the largest value we have. 489 if (I != Alignments.begin()) { 490 --I; // Go to the previous entry and see if its an integer. 491 if (I->AlignType == INTEGER_ALIGN) 492 return ABIInfo ? I->ABIAlign : I->PrefAlign; 493 } 494 } else if (AlignType == VECTOR_ALIGN) { 495 // By default, use natural alignment for vector types. This is consistent 496 // with what clang and llvm-gcc do. 497 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); 498 Align *= cast<VectorType>(Ty)->getNumElements(); 499 Align = PowerOf2Ceil(Align); 500 return Align; 501 } 502 503 // If we still couldn't find a reasonable default alignment, fall back 504 // to a simple heuristic that the alignment is the first power of two 505 // greater-or-equal to the store size of the type. This is a reasonable 506 // approximation of reality, and if the user wanted something less 507 // less conservative, they should have specified it explicitly in the data 508 // layout. 509 unsigned Align = getTypeStoreSize(Ty); 510 Align = PowerOf2Ceil(Align); 511 return Align; 512 } 513 514 namespace { 515 516 class StructLayoutMap { 517 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy; 518 LayoutInfoTy LayoutInfo; 519 520 public: 521 ~StructLayoutMap() { 522 // Remove any layouts. 523 for (const auto &I : LayoutInfo) { 524 StructLayout *Value = I.second; 525 Value->~StructLayout(); 526 free(Value); 527 } 528 } 529 530 StructLayout *&operator[](StructType *STy) { 531 return LayoutInfo[STy]; 532 } 533 }; 534 535 } // end anonymous namespace 536 537 void DataLayout::clear() { 538 LegalIntWidths.clear(); 539 Alignments.clear(); 540 Pointers.clear(); 541 delete static_cast<StructLayoutMap *>(LayoutMap); 542 LayoutMap = nullptr; 543 } 544 545 DataLayout::~DataLayout() { 546 clear(); 547 } 548 549 const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { 550 if (!LayoutMap) 551 LayoutMap = new StructLayoutMap(); 552 553 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); 554 StructLayout *&SL = (*STM)[Ty]; 555 if (SL) return SL; 556 557 // Otherwise, create the struct layout. Because it is variable length, we 558 // malloc it, then use placement new. 559 int NumElts = Ty->getNumElements(); 560 StructLayout *L = 561 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); 562 563 // Set SL before calling StructLayout's ctor. The ctor could cause other 564 // entries to be added to TheMap, invalidating our reference. 565 SL = L; 566 567 new (L) StructLayout(Ty, *this); 568 569 return L; 570 } 571 572 573 unsigned DataLayout::getPointerABIAlignment(unsigned AS) const { 574 PointersTy::const_iterator I = findPointerLowerBound(AS); 575 if (I == Pointers.end() || I->AddressSpace != AS) { 576 I = findPointerLowerBound(0); 577 assert(I->AddressSpace == 0); 578 } 579 return I->ABIAlign; 580 } 581 582 unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const { 583 PointersTy::const_iterator I = findPointerLowerBound(AS); 584 if (I == Pointers.end() || I->AddressSpace != AS) { 585 I = findPointerLowerBound(0); 586 assert(I->AddressSpace == 0); 587 } 588 return I->PrefAlign; 589 } 590 591 unsigned DataLayout::getPointerSize(unsigned AS) const { 592 PointersTy::const_iterator I = findPointerLowerBound(AS); 593 if (I == Pointers.end() || I->AddressSpace != AS) { 594 I = findPointerLowerBound(0); 595 assert(I->AddressSpace == 0); 596 } 597 return I->TypeByteWidth; 598 } 599 600 unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const { 601 assert(Ty->isPtrOrPtrVectorTy() && 602 "This should only be called with a pointer or pointer vector type"); 603 604 if (Ty->isPointerTy()) 605 return getTypeSizeInBits(Ty); 606 607 return getTypeSizeInBits(Ty->getScalarType()); 608 } 609 610 /*! 611 \param abi_or_pref Flag that determines which alignment is returned. true 612 returns the ABI alignment, false returns the preferred alignment. 613 \param Ty The underlying type for which alignment is determined. 614 615 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref 616 == false) for the requested type \a Ty. 617 */ 618 unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { 619 int AlignType = -1; 620 621 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); 622 switch (Ty->getTypeID()) { 623 // Early escape for the non-numeric types. 624 case Type::LabelTyID: 625 return (abi_or_pref 626 ? getPointerABIAlignment(0) 627 : getPointerPrefAlignment(0)); 628 case Type::PointerTyID: { 629 unsigned AS = cast<PointerType>(Ty)->getAddressSpace(); 630 return (abi_or_pref 631 ? getPointerABIAlignment(AS) 632 : getPointerPrefAlignment(AS)); 633 } 634 case Type::ArrayTyID: 635 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref); 636 637 case Type::StructTyID: { 638 // Packed structure types always have an ABI alignment of one. 639 if (cast<StructType>(Ty)->isPacked() && abi_or_pref) 640 return 1; 641 642 // Get the layout annotation... which is lazily created on demand. 643 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty)); 644 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty); 645 return std::max(Align, Layout->getAlignment()); 646 } 647 case Type::IntegerTyID: 648 AlignType = INTEGER_ALIGN; 649 break; 650 case Type::HalfTyID: 651 case Type::FloatTyID: 652 case Type::DoubleTyID: 653 // PPC_FP128TyID and FP128TyID have different data contents, but the 654 // same size and alignment, so they look the same here. 655 case Type::PPC_FP128TyID: 656 case Type::FP128TyID: 657 case Type::X86_FP80TyID: 658 AlignType = FLOAT_ALIGN; 659 break; 660 case Type::X86_MMXTyID: 661 case Type::VectorTyID: 662 AlignType = VECTOR_ALIGN; 663 break; 664 default: 665 llvm_unreachable("Bad type for getAlignment!!!"); 666 } 667 668 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty), 669 abi_or_pref, Ty); 670 } 671 672 unsigned DataLayout::getABITypeAlignment(Type *Ty) const { 673 return getAlignment(Ty, true); 674 } 675 676 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for 677 /// an integer type of the specified bitwidth. 678 unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { 679 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr); 680 } 681 682 unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { 683 return getAlignment(Ty, false); 684 } 685 686 unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const { 687 unsigned Align = getPrefTypeAlignment(Ty); 688 assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); 689 return Log2_32(Align); 690 } 691 692 IntegerType *DataLayout::getIntPtrType(LLVMContext &C, 693 unsigned AddressSpace) const { 694 return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); 695 } 696 697 Type *DataLayout::getIntPtrType(Type *Ty) const { 698 assert(Ty->isPtrOrPtrVectorTy() && 699 "Expected a pointer or pointer vector type."); 700 unsigned NumBits = getPointerTypeSizeInBits(Ty); 701 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); 702 if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) 703 return VectorType::get(IntTy, VecTy->getNumElements()); 704 return IntTy; 705 } 706 707 Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { 708 for (unsigned LegalIntWidth : LegalIntWidths) 709 if (Width <= LegalIntWidth) 710 return Type::getIntNTy(C, LegalIntWidth); 711 return nullptr; 712 } 713 714 unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const { 715 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end()); 716 return Max != LegalIntWidths.end() ? *Max : 0; 717 } 718 719 int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, 720 ArrayRef<Value *> Indices) const { 721 int64_t Result = 0; 722 723 generic_gep_type_iterator<Value* const*> 724 GTI = gep_type_begin(ElemTy, Indices), 725 GTE = gep_type_end(ElemTy, Indices); 726 for (; GTI != GTE; ++GTI) { 727 Value *Idx = GTI.getOperand(); 728 if (StructType *STy = GTI.getStructTypeOrNull()) { 729 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx"); 730 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue(); 731 732 // Get structure layout information... 733 const StructLayout *Layout = getStructLayout(STy); 734 735 // Add in the offset, as calculated by the structure layout info... 736 Result += Layout->getElementOffset(FieldNo); 737 } else { 738 // Get the array index and the size of each array element. 739 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue()) 740 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType()); 741 } 742 } 743 744 return Result; 745 } 746 747 /// getPreferredAlignment - Return the preferred alignment of the specified 748 /// global. This includes an explicitly requested alignment (if the global 749 /// has one). 750 unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const { 751 Type *ElemType = GV->getValueType(); 752 unsigned Alignment = getPrefTypeAlignment(ElemType); 753 unsigned GVAlignment = GV->getAlignment(); 754 if (GVAlignment >= Alignment) { 755 Alignment = GVAlignment; 756 } else if (GVAlignment != 0) { 757 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType)); 758 } 759 760 if (GV->hasInitializer() && GVAlignment == 0) { 761 if (Alignment < 16) { 762 // If the global is not external, see if it is large. If so, give it a 763 // larger alignment. 764 if (getTypeSizeInBits(ElemType) > 128) 765 Alignment = 16; // 16-byte alignment. 766 } 767 } 768 return Alignment; 769 } 770 771 /// getPreferredAlignmentLog - Return the preferred alignment of the 772 /// specified global, returned in log form. This includes an explicitly 773 /// requested alignment (if the global has one). 774 unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { 775 return Log2_32(getPreferredAlignment(GV)); 776 } 777 778