LCOV - code coverage report
Current view: top level - lib/IR - DataLayout.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 364 378 96.3 %
Date: 2017-09-14 15:23:50 Functions: 38 39 97.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13