LCOV - code coverage report
Current view: top level - lib/IR - DataLayout.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 354 370 95.7 %
Date: 2018-10-20 13:21:21 Functions: 42 43 97.7 %
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      278869 : StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
      47             :   assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
      48      278869 :   StructAlignment = 0;
      49      278869 :   StructSize = 0;
      50      278869 :   IsPadded = false;
      51      278869 :   NumElements = ST->getNumElements();
      52             : 
      53             :   // Loop over each of the elements, placing them in memory.
      54      771277 :   for (unsigned i = 0, e = NumElements; i != e; ++i) {
      55      492408 :     Type *Ty = ST->getElementType(i);
      56      492408 :     unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
      57             : 
      58             :     // Add padding if necessary to align the data element properly.
      59      492408 :     if ((StructSize & (TyAlign-1)) != 0) {
      60        8166 :       IsPadded = true;
      61       16332 :       StructSize = alignTo(StructSize, TyAlign);
      62             :     }
      63             : 
      64             :     // Keep track of maximum alignment constraint.
      65      492408 :     StructAlignment = std::max(TyAlign, StructAlignment);
      66             : 
      67      492408 :     MemberOffsets[i] = StructSize;
      68      492408 :     StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
      69             :   }
      70             : 
      71             :   // Empty structures have alignment of 1 byte.
      72      278869 :   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      278869 :   if ((StructSize & (StructAlignment-1)) != 0) {
      77        7050 :     IsPadded = true;
      78       14100 :     StructSize = alignTo(StructSize, StructAlignment);
      79             :   }
      80      278869 : }
      81             : 
      82             : /// getElementContainingOffset - Given a valid offset into the structure,
      83             : /// return the structure index that contains it.
      84      271306 : unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
      85             :   const uint64_t *SI =
      86      271306 :     std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
      87             :   assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
      88      271306 :   --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      271306 :   return SI-&MemberOffsets[0];
     100             : }
     101             : 
     102             : //===----------------------------------------------------------------------===//
     103             : // LayoutAlignElem, LayoutAlign support
     104             : //===----------------------------------------------------------------------===//
     105             : 
     106             : LayoutAlignElem
     107     1884911 : 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     1884911 :   retval.AlignType = align_type;
     112     1884911 :   retval.ABIAlign = abi_align;
     113     1884911 :   retval.PrefAlign = pref_align;
     114     1884911 :   retval.TypeBitWidth = bit_width;
     115     1884911 :   return retval;
     116             : }
     117             : 
     118             : bool
     119       13969 : LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
     120       13969 :   return (AlignType == rhs.AlignType
     121             :           && ABIAlign == rhs.ABIAlign
     122       13969 :           && PrefAlign == rhs.PrefAlign
     123       27938 :           && TypeBitWidth == rhs.TypeBitWidth);
     124             : }
     125             : 
     126             : //===----------------------------------------------------------------------===//
     127             : // PointerAlignElem, PointerAlign support
     128             : //===----------------------------------------------------------------------===//
     129             : 
     130             : PointerAlignElem
     131      164565 : PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
     132             :                       unsigned PrefAlign, uint32_t TypeByteWidth,
     133             :                       uint32_t IndexWidth) {
     134             :   assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
     135             :   PointerAlignElem retval;
     136      164565 :   retval.AddressSpace = AddressSpace;
     137      164565 :   retval.ABIAlign = ABIAlign;
     138      164565 :   retval.PrefAlign = PrefAlign;
     139      164565 :   retval.TypeByteWidth = TypeByteWidth;
     140      164565 :   retval.IndexWidth = IndexWidth;
     141      164565 :   return retval;
     142             : }
     143             : 
     144             : bool
     145        1118 : PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
     146        1118 :   return (ABIAlign == rhs.ABIAlign
     147        1117 :           && AddressSpace == rhs.AddressSpace
     148        1117 :           && PrefAlign == rhs.PrefAlign
     149        1117 :           && TypeByteWidth == rhs.TypeByteWidth
     150        2235 :           && IndexWidth == rhs.IndexWidth);
     151             : }
     152             : 
     153             : //===----------------------------------------------------------------------===//
     154             : //                       DataLayout Class Implementation
     155             : //===----------------------------------------------------------------------===//
     156             : 
     157       31831 : const char *DataLayout::getManglingComponent(const Triple &T) {
     158       31831 :   if (T.isOSBinFormatMachO())
     159             :     return "-m:o";
     160       27273 :   if (T.isOSWindows() && T.isOSBinFormatCOFF())
     161        1521 :     return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
     162             :   return "-m:e";
     163             : }
     164             : 
     165             : static const LayoutAlignElem DefaultAlignments[] = {
     166             :   { INTEGER_ALIGN, 1, 1, 1 },    // i1
     167             :   { INTEGER_ALIGN, 8, 1, 1 },    // i8
     168             :   { INTEGER_ALIGN, 16, 2, 2 },   // i16
     169             :   { INTEGER_ALIGN, 32, 4, 4 },   // i32
     170             :   { INTEGER_ALIGN, 64, 4, 8 },   // i64
     171             :   { FLOAT_ALIGN, 16, 2, 2 },     // half
     172             :   { FLOAT_ALIGN, 32, 4, 4 },     // float
     173             :   { FLOAT_ALIGN, 64, 8, 8 },     // double
     174             :   { FLOAT_ALIGN, 128, 16, 16 },  // ppcf128, quad, ...
     175             :   { VECTOR_ALIGN, 64, 8, 8 },    // v2i32, v1i64, ...
     176             :   { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
     177             :   { AGGREGATE_ALIGN, 0, 0, 8 }   // struct
     178             : };
     179             : 
     180      147944 : void DataLayout::reset(StringRef Desc) {
     181      147944 :   clear();
     182             : 
     183      147943 :   LayoutMap = nullptr;
     184      147943 :   BigEndian = false;
     185      147943 :   AllocaAddrSpace = 0;
     186      147943 :   StackNaturalAlign = 0;
     187      147943 :   ProgramAddrSpace = 0;
     188      147943 :   ManglingMode = MM_None;
     189             :   NonIntegralAddressSpaces.clear();
     190             : 
     191             :   // Default alignments
     192     1923271 :   for (const LayoutAlignElem &E : DefaultAlignments) {
     193     1775327 :     setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
     194     1775327 :                  E.TypeBitWidth);
     195             :   }
     196      147944 :   setPointerAlignment(0, 8, 8, 8, 8);
     197             : 
     198      147944 :   parseSpecifier(Desc);
     199      147916 : }
     200             : 
     201             : /// Checked version of split, to ensure mandatory subparts.
     202     1955207 : static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
     203             :   assert(!Str.empty() && "parse error, string can't be empty here");
     204     1955207 :   std::pair<StringRef, StringRef> Split = Str.split(Separator);
     205     1955207 :   if (Split.second.empty() && Split.first != Str)
     206           1 :     report_fatal_error("Trailing separator in datalayout string");
     207     1955206 :   if (!Split.second.empty() && Split.first.empty())
     208           1 :     report_fatal_error("Expected token before separator in datalayout string");
     209     1955205 :   return Split;
     210             : }
     211             : 
     212             : /// Get an unsigned integer, including error checks.
     213     1079548 : static unsigned getInt(StringRef R) {
     214             :   unsigned Result;
     215           1 :   bool error = R.getAsInteger(10, Result); (void)error;
     216             :   if (error)
     217           1 :     report_fatal_error("not a number, or does not fit in an unsigned int");
     218     1079547 :   return Result;
     219             : }
     220             : 
     221             : /// Convert bits into bytes. Assert if not a byte width multiple.
     222             : static unsigned inBytes(unsigned Bits) {
     223      507471 :   if (Bits % 8)
     224           1 :     report_fatal_error("number of bits must be a byte width multiple");
     225       74681 :   return Bits / 8;
     226             : }
     227             : 
     228        3316 : static unsigned getAddrSpace(StringRef R) {
     229        3316 :   unsigned AddrSpace = getInt(R);
     230        3316 :   if (!isUInt<24>(AddrSpace))
     231           3 :     report_fatal_error("Invalid address space, must be a 24-bit integer");
     232        3313 :   return AddrSpace;
     233             : }
     234             : 
     235      147944 : void DataLayout::parseSpecifier(StringRef Desc) {
     236      295888 :   StringRepresentation = Desc;
     237      807862 :   while (!Desc.empty()) {
     238             :     // Split at '-'.
     239      659946 :     std::pair<StringRef, StringRef> Split = split(Desc, '-');
     240      659946 :     Desc = Split.second;
     241             : 
     242             :     // Split at ':'.
     243      659946 :     Split = split(Split.first, ':');
     244             : 
     245             :     // Aliases used below.
     246             :     StringRef &Tok  = Split.first;  // Current token.
     247             :     StringRef &Rest = Split.second; // The rest of the string.
     248             : 
     249             :     if (Tok == "ni") {
     250             :       do {
     251          45 :         Split = split(Rest, ':');
     252          45 :         Rest = Split.second;
     253          45 :         unsigned AS = getInt(Split.first);
     254          45 :         if (AS == 0)
     255           0 :           report_fatal_error("Address space 0 can never be non-integral");
     256          45 :         NonIntegralAddressSpaces.push_back(AS);
     257          45 :       } while (!Rest.empty());
     258             : 
     259          39 :       continue;
     260             :     }
     261             : 
     262             :     char Specifier = Tok.front();
     263      659905 :     Tok = Tok.substr(1);
     264             : 
     265      659905 :     switch (Specifier) {
     266             :     case 's':
     267             :       // Ignored for backward compatibility.
     268             :       // FIXME: remove this on LLVM 4.0.
     269             :       break;
     270        4902 :     case 'E':
     271        4902 :       BigEndian = true;
     272        4902 :       break;
     273       87920 :     case 'e':
     274       87920 :       BigEndian = false;
     275       87920 :       break;
     276             :     case 'p': {
     277             :       // Address space.
     278       43888 :       unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
     279       16622 :       if (!isUInt<24>(AddrSpace))
     280           1 :         report_fatal_error("Invalid address space, must be a 24bit integer");
     281             : 
     282             :       // Size.
     283       43887 :       if (Rest.empty())
     284           1 :         report_fatal_error(
     285             :             "Missing size specification for pointer in datalayout string");
     286       43886 :       Split = split(Rest, ':');
     287       43886 :       unsigned PointerMemSize = inBytes(getInt(Tok));
     288       43884 :       if (!PointerMemSize)
     289           1 :         report_fatal_error("Invalid pointer size of 0 bytes");
     290             : 
     291             :       // ABI alignment.
     292       43883 :       if (Rest.empty())
     293           1 :         report_fatal_error(
     294             :             "Missing alignment specification for pointer in datalayout string");
     295       43882 :       Split = split(Rest, ':');
     296       43882 :       unsigned PointerABIAlign = inBytes(getInt(Tok));
     297       43882 :       if (!isPowerOf2_64(PointerABIAlign))
     298           1 :         report_fatal_error(
     299             :             "Pointer ABI alignment must be a power of 2");
     300             : 
     301             :       // Size of index used in GEP for address calculation.
     302             :       // The parameter is optional. By default it is equal to size of pointer.
     303             :       unsigned IndexSize = PointerMemSize;
     304             : 
     305             :       // Preferred alignment.
     306             :       unsigned PointerPrefAlign = PointerABIAlign;
     307       43881 :       if (!Rest.empty()) {
     308        3960 :         Split = split(Rest, ':');
     309        3960 :         PointerPrefAlign = inBytes(getInt(Tok));
     310        3960 :         if (!isPowerOf2_64(PointerPrefAlign))
     311           1 :           report_fatal_error(
     312             :             "Pointer preferred alignment must be a power of 2");
     313             : 
     314             :         // Now read the index. It is the second optional parameter here.
     315        3959 :         if (!Rest.empty()) {
     316           6 :           Split = split(Rest, ':');
     317           6 :           IndexSize = inBytes(getInt(Tok));
     318           6 :           if (!IndexSize)
     319           0 :             report_fatal_error("Invalid index size of 0 bytes");
     320             :         }
     321             :       }
     322       43880 :       setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
     323             :                           PointerMemSize, IndexSize);
     324       43879 :       break;
     325             :     }
     326      259824 :     case 'i':
     327             :     case 'v':
     328             :     case 'f':
     329             :     case 'a': {
     330             :       AlignTypeEnum AlignType;
     331             :       switch (Specifier) {
     332           0 :       default: llvm_unreachable("Unexpected specifier!");
     333             :       case 'i': AlignType = INTEGER_ALIGN; break;
     334       50175 :       case 'v': AlignType = VECTOR_ALIGN; break;
     335       88706 :       case 'f': AlignType = FLOAT_ALIGN; break;
     336       10788 :       case 'a': AlignType = AGGREGATE_ALIGN; break;
     337             :       }
     338             : 
     339             :       // Bit size.
     340      259824 :       unsigned Size = Tok.empty() ? 0 : getInt(Tok);
     341             : 
     342      259824 :       if (AlignType == AGGREGATE_ALIGN && Size != 0)
     343           1 :         report_fatal_error(
     344             :             "Sized aggregate specification in datalayout string");
     345             : 
     346             :       // ABI alignment.
     347      259823 :       if (Rest.empty())
     348           1 :         report_fatal_error(
     349             :             "Missing alignment specification in datalayout string");
     350      259822 :       Split = split(Rest, ':');
     351      259822 :       unsigned ABIAlign = inBytes(getInt(Tok));
     352      259822 :       if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
     353           1 :         report_fatal_error(
     354             :             "ABI alignment specification must be >0 for non-aggregate types");
     355             : 
     356             :       // Preferred alignment.
     357             :       unsigned PrefAlign = ABIAlign;
     358      259821 :       if (!Rest.empty()) {
     359       74681 :         Split = split(Rest, ':');
     360       74681 :         PrefAlign = inBytes(getInt(Tok));
     361             :       }
     362             : 
     363      259821 :       setAlignment(AlignType, ABIAlign, PrefAlign, Size);
     364             : 
     365      259815 :       break;
     366             :     }
     367      300704 :     case 'n':  // Native integer types.
     368             :       while (true) {
     369      300704 :         unsigned Width = getInt(Tok);
     370      300704 :         if (Width == 0)
     371           1 :           report_fatal_error(
     372             :               "Zero width native integer type in datalayout string");
     373      300703 :         LegalIntWidths.push_back(Width);
     374      300703 :         if (Rest.empty())
     375             :           break;
     376      209033 :         Split = split(Rest, ':');
     377      209033 :       }
     378             :       break;
     379       81235 :     case 'S': { // Stack natural alignment.
     380       81235 :       StackNaturalAlign = inBytes(getInt(Tok));
     381       81235 :       break;
     382             :     }
     383         149 :     case 'P': { // Function address space.
     384         149 :       ProgramAddrSpace = getAddrSpace(Tok);
     385         147 :       break;
     386             :     }
     387        3167 :     case 'A': { // Default stack/alloca address space.
     388        3167 :       AllocaAddrSpace = getAddrSpace(Tok);
     389        3166 :       break;
     390             :     }
     391             :     case 'm':
     392       85738 :       if (!Tok.empty())
     393           1 :         report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
     394       85737 :       if (Rest.empty())
     395           1 :         report_fatal_error("Expected mangling specifier in datalayout string");
     396       85736 :       if (Rest.size() > 1)
     397           0 :         report_fatal_error("Unknown mangling specifier in datalayout string");
     398       85736 :       switch(Rest[0]) {
     399           1 :       default:
     400           1 :         report_fatal_error("Unknown mangling in datalayout string");
     401       72422 :       case 'e':
     402       72422 :         ManglingMode = MM_ELF;
     403       72422 :         break;
     404        8729 :       case 'o':
     405        8729 :         ManglingMode = MM_MachO;
     406        8729 :         break;
     407        1508 :       case 'm':
     408        1508 :         ManglingMode = MM_Mips;
     409        1508 :         break;
     410        1688 :       case 'w':
     411        1688 :         ManglingMode = MM_WinCOFF;
     412        1688 :         break;
     413        1388 :       case 'x':
     414        1388 :         ManglingMode = MM_WinCOFFX86;
     415        1388 :         break;
     416             :       }
     417             :       break;
     418           1 :     default:
     419           1 :       report_fatal_error("Unknown specifier in datalayout string");
     420             :       break;
     421             :     }
     422             :   }
     423      147916 : }
     424             : 
     425        9750 : DataLayout::DataLayout(const Module *M) {
     426        4875 :   init(M);
     427        4875 : }
     428             : 
     429        4875 : void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
     430             : 
     431        1113 : bool DataLayout::operator==(const DataLayout &Other) const {
     432        1113 :   bool Ret = BigEndian == Other.BigEndian &&
     433             :              AllocaAddrSpace == Other.AllocaAddrSpace &&
     434        1113 :              StackNaturalAlign == Other.StackNaturalAlign &&
     435        1112 :              ProgramAddrSpace == Other.ProgramAddrSpace &&
     436        1112 :              ManglingMode == Other.ManglingMode &&
     437        2224 :              LegalIntWidths == Other.LegalIntWidths &&
     438        3337 :              Alignments == Other.Alignments && Pointers == Other.Pointers;
     439             :   // Note: getStringRepresentation() might differs, it is not canonicalized
     440        1113 :   return Ret;
     441             : }
     442             : 
     443             : DataLayout::AlignmentsTy::iterator
     444   247954319 : DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
     445             :                                     uint32_t BitWidth) {
     446   247954319 :   auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
     447             :   return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
     448             :                           [](const LayoutAlignElem &LHS,
     449             :                              const std::pair<unsigned, uint32_t> &RHS) {
     450           0 :                             return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
     451             :                                    std::tie(RHS.first, RHS.second);
     452   247954318 :                           });
     453             : }
     454             : 
     455             : void
     456     2035147 : DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
     457             :                          unsigned pref_align, uint32_t bit_width) {
     458     2035147 :   if (!isUInt<24>(bit_width))
     459           1 :     report_fatal_error("Invalid bit width, must be a 24bit integer");
     460     2035146 :   if (!isUInt<16>(abi_align))
     461           1 :     report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
     462     2035145 :   if (!isUInt<16>(pref_align))
     463           1 :     report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
     464     2035144 :   if (abi_align != 0 && !isPowerOf2_64(abi_align))
     465           1 :     report_fatal_error("Invalid ABI alignment, must be a power of 2");
     466     2035143 :   if (pref_align != 0 && !isPowerOf2_64(pref_align))
     467           1 :     report_fatal_error("Invalid preferred alignment, must be a power of 2");
     468             : 
     469     2035142 :   if (pref_align < abi_align)
     470           1 :     report_fatal_error(
     471             :         "Preferred alignment cannot be less than the ABI alignment");
     472             : 
     473     2035141 :   AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
     474      980236 :   if (I != Alignments.end() &&
     475     2035138 :       I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
     476             :     // Update the abi, preferred alignments.
     477      150228 :     I->ABIAlign = abi_align;
     478      150228 :     I->PrefAlign = pref_align;
     479             :   } else {
     480             :     // Insert before I to keep the vector sorted.
     481     1884913 :     Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
     482     1884910 :                                               pref_align, bit_width));
     483             :   }
     484     2035142 : }
     485             : 
     486             : DataLayout::PointersTy::iterator
     487   299295862 : DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
     488             :   return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
     489             :                           [](const PointerAlignElem &A, uint32_t AddressSpace) {
     490           0 :     return A.AddressSpace < AddressSpace;
     491   299295862 :   });
     492             : }
     493             : 
     494      191824 : void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
     495             :                                      unsigned PrefAlign, uint32_t TypeByteWidth,
     496             :                                      uint32_t IndexWidth) {
     497      191824 :   if (PrefAlign < ABIAlign)
     498           1 :     report_fatal_error(
     499             :         "Preferred alignment cannot be less than the ABI alignment");
     500             : 
     501      191823 :   PointersTy::iterator I = findPointerLowerBound(AddrSpace);
     502      191823 :   if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
     503      164565 :     Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
     504      164565 :                                              TypeByteWidth, IndexWidth));
     505             :   } else {
     506       27258 :     I->ABIAlign = ABIAlign;
     507       27258 :     I->PrefAlign = PrefAlign;
     508       27258 :     I->TypeByteWidth = TypeByteWidth;
     509       27258 :     I->IndexWidth = IndexWidth;
     510             :   }
     511      191823 : }
     512             : 
     513             : /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
     514             : /// preferred if ABIInfo = false) the layout wants for the specified datatype.
     515   245919177 : unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
     516             :                                       uint32_t BitWidth, bool ABIInfo,
     517             :                                       Type *Ty) const {
     518             :   AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
     519             :   // See if we found an exact match. Of if we are looking for an integer type,
     520             :   // but don't have an exact match take the next largest integer. This is where
     521             :   // the lower_bound will point to when it fails an exact match.
     522   245919178 :   if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
     523   245735757 :       (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
     524   245721627 :     return ABIInfo ? I->ABIAlign : I->PrefAlign;
     525             : 
     526      197551 :   if (AlignType == INTEGER_ALIGN) {
     527             :     // If we didn't have a larger value try the largest value we have.
     528       34753 :     if (I != Alignments.begin()) {
     529             :       --I; // Go to the previous entry and see if its an integer.
     530       34753 :       if (I->AlignType == INTEGER_ALIGN)
     531       34753 :         return ABIInfo ? I->ABIAlign : I->PrefAlign;
     532             :     }
     533      162798 :   } else if (AlignType == VECTOR_ALIGN) {
     534             :     // By default, use natural alignment for vector types. This is consistent
     535             :     // with what clang and llvm-gcc do.
     536      162779 :     unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
     537      162779 :     Align *= cast<VectorType>(Ty)->getNumElements();
     538      162779 :     Align = PowerOf2Ceil(Align);
     539      162779 :     return Align;
     540             :    }
     541             : 
     542             :   // If we still couldn't find a reasonable default alignment, fall back
     543             :   // to a simple heuristic that the alignment is the first power of two
     544             :   // greater-or-equal to the store size of the type.  This is a reasonable
     545             :   // approximation of reality, and if the user wanted something less
     546             :   // less conservative, they should have specified it explicitly in the data
     547             :   // layout.
     548             :   unsigned Align = getTypeStoreSize(Ty);
     549          19 :   Align = PowerOf2Ceil(Align);
     550          19 :   return Align;
     551             : }
     552             : 
     553             : namespace {
     554             : 
     555       15583 : class StructLayoutMap {
     556             :   using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
     557             :   LayoutInfoTy LayoutInfo;
     558             : 
     559             : public:
     560       22158 :   ~StructLayoutMap() {
     561             :     // Remove any layouts.
     562      138544 :     for (const auto &I : LayoutInfo) {
     563      127465 :       StructLayout *Value = I.second;
     564             :       Value->~StructLayout();
     565      127465 :       free(Value);
     566             :     }
     567       11079 :   }
     568             : 
     569             :   StructLayout *&operator[](StructType *STy) {
     570    38587598 :     return LayoutInfo[STy];
     571             :   }
     572             : };
     573             : 
     574             : } // end anonymous namespace
     575             : 
     576     5574992 : void DataLayout::clear() {
     577             :   LegalIntWidths.clear();
     578             :   Alignments.clear();
     579             :   Pointers.clear();
     580     5574992 :   delete static_cast<StructLayoutMap *>(LayoutMap);
     581     5574992 :   LayoutMap = nullptr;
     582     5574992 : }
     583             : 
     584     5491329 : DataLayout::~DataLayout() {
     585     2745664 :   clear();
     586     2745665 : }
     587             : 
     588    38587598 : const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
     589    38587598 :   if (!LayoutMap)
     590       31166 :     LayoutMap = new StructLayoutMap();
     591             : 
     592    38587598 :   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
     593    38587598 :   StructLayout *&SL = (*STM)[Ty];
     594    38587598 :   if (SL) return SL;
     595             : 
     596             :   // Otherwise, create the struct layout.  Because it is variable length, we
     597             :   // malloc it, then use placement new.
     598      278869 :   int NumElts = Ty->getNumElements();
     599             :   StructLayout *L = (StructLayout *)
     600      278869 :       safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
     601             : 
     602             :   // Set SL before calling StructLayout's ctor.  The ctor could cause other
     603             :   // entries to be added to TheMap, invalidating our reference.
     604      278869 :   SL = L;
     605             : 
     606      278869 :   new (L) StructLayout(Ty, *this);
     607             : 
     608      278869 :   return L;
     609             : }
     610             : 
     611    22926214 : unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
     612             :   PointersTy::const_iterator I = findPointerLowerBound(AS);
     613    22926214 :   if (I == Pointers.end() || I->AddressSpace != AS) {
     614             :     I = findPointerLowerBound(0);
     615             :     assert(I->AddressSpace == 0);
     616             :   }
     617    22926214 :   return I->ABIAlign;
     618             : }
     619             : 
     620     5296986 : unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
     621             :   PointersTy::const_iterator I = findPointerLowerBound(AS);
     622     5296986 :   if (I == Pointers.end() || I->AddressSpace != AS) {
     623             :     I = findPointerLowerBound(0);
     624             :     assert(I->AddressSpace == 0);
     625             :   }
     626     5296986 :   return I->PrefAlign;
     627             : }
     628             : 
     629   146585488 : unsigned DataLayout::getPointerSize(unsigned AS) const {
     630             :   PointersTy::const_iterator I = findPointerLowerBound(AS);
     631   146585488 :   if (I == Pointers.end() || I->AddressSpace != AS) {
     632             :     I = findPointerLowerBound(0);
     633             :     assert(I->AddressSpace == 0);
     634             :   }
     635   146585488 :   return I->TypeByteWidth;
     636             : }
     637             : 
     638    26761094 : unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
     639             :   assert(Ty->isPtrOrPtrVectorTy() &&
     640             :          "This should only be called with a pointer or pointer vector type");
     641             :   Ty = Ty->getScalarType();
     642    26761094 :   return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
     643             : }
     644             : 
     645   124177785 : unsigned DataLayout::getIndexSize(unsigned AS) const {
     646             :   PointersTy::const_iterator I = findPointerLowerBound(AS);
     647   124177785 :   if (I == Pointers.end() || I->AddressSpace != AS) {
     648             :     I = findPointerLowerBound(0);
     649             :     assert(I->AddressSpace == 0);
     650             :   }
     651   124177785 :   return I->IndexWidth;
     652             : }
     653             : 
     654   118383713 : unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
     655             :   assert(Ty->isPtrOrPtrVectorTy() &&
     656             :          "This should only be called with a pointer or pointer vector type");
     657             :   Ty = Ty->getScalarType();
     658   118383713 :   return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
     659             : }
     660             : 
     661             : /*!
     662             :   \param abi_or_pref Flag that determines which alignment is returned. true
     663             :   returns the ABI alignment, false returns the preferred alignment.
     664             :   \param Ty The underlying type for which alignment is determined.
     665             : 
     666             :   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
     667             :   == false) for the requested type \a Ty.
     668             :  */
     669   336351143 : unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
     670             :   AlignTypeEnum AlignType;
     671             : 
     672             :   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
     673   336351143 :   switch (Ty->getTypeID()) {
     674             :   // Early escape for the non-numeric types.
     675           0 :   case Type::LabelTyID:
     676             :     return (abi_or_pref
     677           0 :             ? getPointerABIAlignment(0)
     678           0 :             : getPointerPrefAlignment(0));
     679             :   case Type::PointerTyID: {
     680             :     unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
     681             :     return (abi_or_pref
     682    28220653 :             ? getPointerABIAlignment(AS)
     683     5296559 :             : getPointerPrefAlignment(AS));
     684             :     }
     685    61519912 :   case Type::ArrayTyID:
     686    61519912 :     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
     687             : 
     688             :   case Type::StructTyID: {
     689             :     // Packed structure types always have an ABI alignment of one.
     690    16935964 :     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
     691             :       return 1;
     692             : 
     693             :     // Get the layout annotation... which is lazily created on demand.
     694    16244448 :     const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
     695    16244448 :     unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
     696    31187065 :     return std::max(Align, Layout->getAlignment());
     697             :   }
     698             :   case Type::IntegerTyID:
     699             :     AlignType = INTEGER_ALIGN;
     700             :     break;
     701     1328007 :   case Type::HalfTyID:
     702             :   case Type::FloatTyID:
     703             :   case Type::DoubleTyID:
     704             :   // PPC_FP128TyID and FP128TyID have different data contents, but the
     705             :   // same size and alignment, so they look the same here.
     706             :   case Type::PPC_FP128TyID:
     707             :   case Type::FP128TyID:
     708             :   case Type::X86_FP80TyID:
     709             :     AlignType = FLOAT_ALIGN;
     710     1328007 :     break;
     711     3378570 :   case Type::X86_MMXTyID:
     712             :   case Type::VectorTyID:
     713             :     AlignType = VECTOR_ALIGN;
     714     3378570 :     break;
     715           0 :   default:
     716           0 :     llvm_unreachable("Bad type for getAlignment!!!");
     717             :   }
     718             : 
     719   229674614 :   return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
     720             : }
     721             : 
     722   244216510 : unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
     723   244216510 :   return getAlignment(Ty, true);
     724             : }
     725             : 
     726             : /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
     727             : /// an integer type of the specified bitwidth.
     728         116 : unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
     729         116 :   return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
     730             : }
     731             : 
     732    30614720 : unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
     733    30614720 :   return getAlignment(Ty, false);
     734             : }
     735             : 
     736          51 : unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
     737          51 :   unsigned Align = getPrefTypeAlignment(Ty);
     738             :   assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
     739          51 :   return Log2_32(Align);
     740             : }
     741             : 
     742      869784 : IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
     743             :                                        unsigned AddressSpace) const {
     744      869784 :   return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
     745             : }
     746             : 
     747    37072185 : Type *DataLayout::getIntPtrType(Type *Ty) const {
     748             :   assert(Ty->isPtrOrPtrVectorTy() &&
     749             :          "Expected a pointer or pointer vector type.");
     750    37072185 :   unsigned NumBits = getIndexTypeSizeInBits(Ty);
     751    37072185 :   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
     752             :   if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
     753          85 :     return VectorType::get(IntTy, VecTy->getNumElements());
     754             :   return IntTy;
     755             : }
     756             : 
     757           0 : Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
     758           0 :   for (unsigned LegalIntWidth : LegalIntWidths)
     759           0 :     if (Width <= LegalIntWidth)
     760           0 :       return Type::getIntNTy(C, LegalIntWidth);
     761             :   return nullptr;
     762             : }
     763             : 
     764        1201 : unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
     765             :   auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
     766        1201 :   return Max != LegalIntWidths.end() ? *Max : 0;
     767             : }
     768             : 
     769     1273375 : Type *DataLayout::getIndexType(Type *Ty) const {
     770             :   assert(Ty->isPtrOrPtrVectorTy() &&
     771             :          "Expected a pointer or pointer vector type.");
     772     1273375 :   unsigned NumBits = getIndexTypeSizeInBits(Ty);
     773     1273375 :   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
     774             :   if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
     775           0 :     return VectorType::get(IntTy, VecTy->getNumElements());
     776             :   return IntTy;
     777             : }
     778             : 
     779    16389348 : int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
     780             :                                            ArrayRef<Value *> Indices) const {
     781             :   int64_t Result = 0;
     782             : 
     783             :   generic_gep_type_iterator<Value* const*>
     784             :     GTI = gep_type_begin(ElemTy, Indices),
     785             :     GTE = gep_type_end(ElemTy, Indices);
     786    49211054 :   for (; GTI != GTE; ++GTI) {
     787             :     Value *Idx = GTI.getOperand();
     788       79786 :     if (StructType *STy = GTI.getStructTypeOrNull()) {
     789             :       assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
     790       79786 :       unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
     791             : 
     792             :       // Get structure layout information...
     793       79786 :       const StructLayout *Layout = getStructLayout(STy);
     794             : 
     795             :       // Add in the offset, as calculated by the structure layout info...
     796       79786 :       Result += Layout->getElementOffset(FieldNo);
     797             :     } else {
     798             :       // Get the array index and the size of each array element.
     799    32741920 :       if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
     800    12132031 :         Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
     801             :     }
     802             :   }
     803             : 
     804    16389348 :   return Result;
     805             : }
     806             : 
     807             : /// getPreferredAlignment - Return the preferred alignment of the specified
     808             : /// global.  This includes an explicitly requested alignment (if the global
     809             : /// has one).
     810    12053956 : unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
     811    12053956 :   unsigned GVAlignment = GV->getAlignment();
     812             :   // If a section is specified, always precisely honor explicit alignment,
     813             :   // so we don't insert padding into a section we don't control.
     814    12053956 :   if (GVAlignment && GV->hasSection())
     815             :     return GVAlignment;
     816             : 
     817             :   // If no explicit alignment is specified, compute the alignment based on
     818             :   // the IR type. If an alignment is specified, increase it to match the ABI
     819             :   // alignment of the IR type.
     820             :   //
     821             :   // FIXME: Not sure it makes sense to use the alignment of the type if
     822             :   // there's already an explicit alignment specification.
     823    12053204 :   Type *ElemType = GV->getValueType();
     824    12053204 :   unsigned Alignment = getPrefTypeAlignment(ElemType);
     825    12053204 :   if (GVAlignment >= Alignment) {
     826             :     Alignment = GVAlignment;
     827    11360571 :   } else if (GVAlignment != 0) {
     828        7081 :     Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
     829             :   }
     830             : 
     831             :   // If no explicit alignment is specified, and the global is large, increase
     832             :   // the alignment to 16.
     833             :   // FIXME: Why 16, specifically?
     834    12053204 :   if (GV->hasInitializer() && GVAlignment == 0) {
     835    11354000 :     if (Alignment < 16) {
     836             :       // If the global is not external, see if it is large.  If so, give it a
     837             :       // larger alignment.
     838    11350970 :       if (getTypeSizeInBits(ElemType) > 128)
     839             :         Alignment = 16;    // 16-byte alignment.
     840             :     }
     841             :   }
     842             :   return Alignment;
     843             : }
     844             : 
     845             : /// getPreferredAlignmentLog - Return the preferred alignment of the
     846             : /// specified global, returned in log form.  This includes an explicitly
     847             : /// requested alignment (if the global has one).
     848      540412 : unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
     849      540412 :   return Log2_32(getPreferredAlignment(GV));
     850             : }

Generated by: LCOV version 1.13