LCOV - code coverage report
Current view: top level - lib/IR - Attributes.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 793 859 92.3 %
Date: 2017-09-14 15:23:50 Functions: 142 155 91.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Attributes.cpp - Implement AttributesList --------------------------===//
       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             : // \file
      11             : // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
      12             : // AttributeListImpl, and AttributeList classes.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/IR/Attributes.h"
      17             : #include "AttributeImpl.h"
      18             : #include "LLVMContextImpl.h"
      19             : #include "llvm/ADT/ArrayRef.h"
      20             : #include "llvm/ADT/FoldingSet.h"
      21             : #include "llvm/ADT/Optional.h"
      22             : #include "llvm/ADT/STLExtras.h"
      23             : #include "llvm/ADT/SmallVector.h"
      24             : #include "llvm/ADT/StringExtras.h"
      25             : #include "llvm/ADT/StringRef.h"
      26             : #include "llvm/ADT/Twine.h"
      27             : #include "llvm/IR/Function.h"
      28             : #include "llvm/IR/LLVMContext.h"
      29             : #include "llvm/IR/Type.h"
      30             : #include "llvm/Support/Compiler.h"
      31             : #include "llvm/Support/Debug.h"
      32             : #include "llvm/Support/ErrorHandling.h"
      33             : #include "llvm/Support/MathExtras.h"
      34             : #include "llvm/Support/raw_ostream.h"
      35             : #include <algorithm>
      36             : #include <cassert>
      37             : #include <climits>
      38             : #include <cstddef>
      39             : #include <cstdint>
      40             : #include <limits>
      41             : #include <map>
      42             : #include <string>
      43             : #include <tuple>
      44             : #include <utility>
      45             : 
      46             : using namespace llvm;
      47             : 
      48             : //===----------------------------------------------------------------------===//
      49             : // Attribute Construction Methods
      50             : //===----------------------------------------------------------------------===//
      51             : 
      52             : // allocsize has two integer arguments, but because they're both 32 bits, we can
      53             : // pack them into one 64-bit value, at the cost of making said value
      54             : // nonsensical.
      55             : //
      56             : // In order to do this, we need to reserve one value of the second (optional)
      57             : // allocsize argument to signify "not present."
      58             : static const unsigned AllocSizeNumElemsNotPresent = -1;
      59             : 
      60             : static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
      61             :                                   const Optional<unsigned> &NumElemsArg) {
      62             :   assert((!NumElemsArg.hasValue() ||
      63             :           *NumElemsArg != AllocSizeNumElemsNotPresent) &&
      64             :          "Attempting to pack a reserved value");
      65             : 
      66         160 :   return uint64_t(ElemSizeArg) << 32 |
      67         160 :          NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
      68             : }
      69             : 
      70             : static std::pair<unsigned, Optional<unsigned>>
      71             : unpackAllocSizeArgs(uint64_t Num) {
      72         243 :   unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
      73         243 :   unsigned ElemSizeArg = Num >> 32;
      74             : 
      75         486 :   Optional<unsigned> NumElemsArg;
      76         243 :   if (NumElems != AllocSizeNumElemsNotPresent)
      77             :     NumElemsArg = NumElems;
      78         486 :   return std::make_pair(ElemSizeArg, NumElemsArg);
      79             : }
      80             : 
      81     1931477 : Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
      82             :                          uint64_t Val) {
      83     1931477 :   LLVMContextImpl *pImpl = Context.pImpl;
      84     3862954 :   FoldingSetNodeID ID;
      85     1931477 :   ID.AddInteger(Kind);
      86     1931477 :   if (Val) ID.AddInteger(Val);
      87             : 
      88             :   void *InsertPoint;
      89     3787164 :   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
      90             : 
      91     1855687 :   if (!PA) {
      92             :     // If we didn't find any existing attributes of the same shape then create a
      93             :     // new one and insert it.
      94       75790 :     if (!Val)
      95      145534 :       PA = new EnumAttributeImpl(Kind);
      96             :     else
      97        6046 :       PA = new IntAttributeImpl(Kind, Val);
      98       75790 :     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
      99             :   }
     100             : 
     101             :   // Return the Attribute that we found or created.
     102     3862954 :   return Attribute(PA);
     103             : }
     104             : 
     105     5701375 : Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
     106     5701375 :   LLVMContextImpl *pImpl = Context.pImpl;
     107    11402750 :   FoldingSetNodeID ID;
     108     5701375 :   ID.AddString(Kind);
     109     5701375 :   if (!Val.empty()) ID.AddString(Val);
     110             : 
     111             :   void *InsertPoint;
     112    11313561 :   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
     113             : 
     114     5612186 :   if (!PA) {
     115             :     // If we didn't find any existing attributes of the same shape then create a
     116             :     // new one and insert it.
     117       89189 :     PA = new StringAttributeImpl(Kind, Val);
     118       89189 :     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
     119             :   }
     120             : 
     121             :   // Return the Attribute that we found or created.
     122    11402750 :   return Attribute(PA);
     123             : }
     124             : 
     125        4487 : Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
     126             :   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
     127             :   assert(Align <= 0x40000000 && "Alignment too large.");
     128        4487 :   return get(Context, Alignment, Align);
     129             : }
     130             : 
     131         197 : Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
     132             :                                            uint64_t Align) {
     133             :   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
     134             :   assert(Align <= 0x100 && "Alignment too large.");
     135         197 :   return get(Context, StackAlignment, Align);
     136             : }
     137             : 
     138       87423 : Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
     139             :                                                 uint64_t Bytes) {
     140             :   assert(Bytes && "Bytes must be non-zero.");
     141       87423 :   return get(Context, Dereferenceable, Bytes);
     142             : }
     143             : 
     144          77 : Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
     145             :                                                        uint64_t Bytes) {
     146             :   assert(Bytes && "Bytes must be non-zero.");
     147          77 :   return get(Context, DereferenceableOrNull, Bytes);
     148             : }
     149             : 
     150             : Attribute
     151         106 : Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
     152             :                                 const Optional<unsigned> &NumElemsArg) {
     153             :   assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
     154             :          "Invalid allocsize arguments -- given allocsize(0, 0)");
     155         106 :   return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
     156             : }
     157             : 
     158             : //===----------------------------------------------------------------------===//
     159             : // Attribute Accessor Methods
     160             : //===----------------------------------------------------------------------===//
     161             : 
     162        6773 : bool Attribute::isEnumAttribute() const {
     163        6773 :   return pImpl && pImpl->isEnumAttribute();
     164             : }
     165             : 
     166        3399 : bool Attribute::isIntAttribute() const {
     167        3399 :   return pImpl && pImpl->isIntAttribute();
     168             : }
     169             : 
     170   124771654 : bool Attribute::isStringAttribute() const {
     171   124771654 :   return pImpl && pImpl->isStringAttribute();
     172             : }
     173             : 
     174     2381721 : Attribute::AttrKind Attribute::getKindAsEnum() const {
     175     2381721 :   if (!pImpl) return None;
     176             :   assert((isEnumAttribute() || isIntAttribute()) &&
     177             :          "Invalid attribute type to get the kind as an enum!");
     178     2381721 :   return pImpl->getKindAsEnum();
     179             : }
     180             : 
     181       11100 : uint64_t Attribute::getValueAsInt() const {
     182       11100 :   if (!pImpl) return 0;
     183             :   assert(isIntAttribute() &&
     184             :          "Expected the attribute to be an integer attribute!");
     185       11100 :   return pImpl->getValueAsInt();
     186             : }
     187             : 
     188     3975023 : StringRef Attribute::getKindAsString() const {
     189     3975023 :   if (!pImpl) return StringRef();
     190             :   assert(isStringAttribute() &&
     191             :          "Invalid attribute type to get the kind as a string!");
     192     3975023 :   return pImpl->getKindAsString();
     193             : }
     194             : 
     195    13753470 : StringRef Attribute::getValueAsString() const {
     196    13753470 :   if (!pImpl) return StringRef();
     197             :   assert(isStringAttribute() &&
     198             :          "Invalid attribute type to get the value as a string!");
     199    10243515 :   return pImpl->getValueAsString();
     200             : }
     201             : 
     202    23221136 : bool Attribute::hasAttribute(AttrKind Kind) const {
     203    23221136 :   return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
     204             : }
     205             : 
     206   114101560 : bool Attribute::hasAttribute(StringRef Kind) const {
     207   114101560 :   if (!isStringAttribute()) return false;
     208    95023720 :   return pImpl && pImpl->hasAttribute(Kind);
     209             : }
     210             : 
     211       21504 : unsigned Attribute::getAlignment() const {
     212             :   assert(hasAttribute(Attribute::Alignment) &&
     213             :          "Trying to get alignment from non-alignment attribute!");
     214       21504 :   return pImpl->getValueAsInt();
     215             : }
     216             : 
     217         175 : unsigned Attribute::getStackAlignment() const {
     218             :   assert(hasAttribute(Attribute::StackAlignment) &&
     219             :          "Trying to get alignment from non-alignment attribute!");
     220         175 :   return pImpl->getValueAsInt();
     221             : }
     222             : 
     223       77513 : uint64_t Attribute::getDereferenceableBytes() const {
     224             :   assert(hasAttribute(Attribute::Dereferenceable) &&
     225             :          "Trying to get dereferenceable bytes from "
     226             :          "non-dereferenceable attribute!");
     227       77513 :   return pImpl->getValueAsInt();
     228             : }
     229             : 
     230          87 : uint64_t Attribute::getDereferenceableOrNullBytes() const {
     231             :   assert(hasAttribute(Attribute::DereferenceableOrNull) &&
     232             :          "Trying to get dereferenceable bytes from "
     233             :          "non-dereferenceable attribute!");
     234          87 :   return pImpl->getValueAsInt();
     235             : }
     236             : 
     237         138 : std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
     238             :   assert(hasAttribute(Attribute::AllocSize) &&
     239             :          "Trying to get allocsize args from non-allocsize attribute");
     240         276 :   return unpackAllocSizeArgs(pImpl->getValueAsInt());
     241             : }
     242             : 
     243      418512 : std::string Attribute::getAsString(bool InAttrGrp) const {
     244      418512 :   if (!pImpl) return "";
     245             : 
     246      418512 :   if (hasAttribute(Attribute::SanitizeAddress))
     247        1792 :     return "sanitize_address";
     248      417616 :   if (hasAttribute(Attribute::AlwaysInline))
     249        1010 :     return "alwaysinline";
     250      417111 :   if (hasAttribute(Attribute::ArgMemOnly))
     251        9972 :     return "argmemonly";
     252      412125 :   if (hasAttribute(Attribute::Builtin))
     253         658 :     return "builtin";
     254      411796 :   if (hasAttribute(Attribute::ByVal))
     255        2126 :     return "byval";
     256      410733 :   if (hasAttribute(Attribute::Convergent))
     257        1028 :     return "convergent";
     258      410219 :   if (hasAttribute(Attribute::SwiftError))
     259         120 :     return "swifterror";
     260      410159 :   if (hasAttribute(Attribute::SwiftSelf))
     261          92 :     return "swiftself";
     262      410113 :   if (hasAttribute(Attribute::InaccessibleMemOnly))
     263          98 :     return "inaccessiblememonly";
     264      410064 :   if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
     265         164 :     return "inaccessiblemem_or_argmemonly";
     266      409982 :   if (hasAttribute(Attribute::InAlloca))
     267         392 :     return "inalloca";
     268      409786 :   if (hasAttribute(Attribute::InlineHint))
     269        2172 :     return "inlinehint";
     270      408700 :   if (hasAttribute(Attribute::InReg))
     271        1904 :     return "inreg";
     272      407748 :   if (hasAttribute(Attribute::JumpTable))
     273          48 :     return "jumptable";
     274      407724 :   if (hasAttribute(Attribute::MinSize))
     275         628 :     return "minsize";
     276      407410 :   if (hasAttribute(Attribute::Naked))
     277         180 :     return "naked";
     278      407320 :   if (hasAttribute(Attribute::Nest))
     279          74 :     return "nest";
     280      407283 :   if (hasAttribute(Attribute::NoAlias))
     281       14562 :     return "noalias";
     282      400002 :   if (hasAttribute(Attribute::NoBuiltin))
     283        1684 :     return "nobuiltin";
     284      399160 :   if (hasAttribute(Attribute::NoCapture))
     285       15514 :     return "nocapture";
     286      391403 :   if (hasAttribute(Attribute::NoDuplicate))
     287         104 :     return "noduplicate";
     288      391351 :   if (hasAttribute(Attribute::NoImplicitFloat))
     289         138 :     return "noimplicitfloat";
     290      391282 :   if (hasAttribute(Attribute::NoInline))
     291      123146 :     return "noinline";
     292      329709 :   if (hasAttribute(Attribute::NonLazyBind))
     293         976 :     return "nonlazybind";
     294      329221 :   if (hasAttribute(Attribute::NonNull))
     295        5022 :     return "nonnull";
     296      326710 :   if (hasAttribute(Attribute::NoRedZone))
     297         254 :     return "noredzone";
     298      326583 :   if (hasAttribute(Attribute::NoReturn))
     299        2266 :     return "noreturn";
     300      325450 :   if (hasAttribute(Attribute::NoRecurse))
     301        8238 :     return "norecurse";
     302      321331 :   if (hasAttribute(Attribute::NoUnwind))
     303      210812 :     return "nounwind";
     304      215925 :   if (hasAttribute(Attribute::OptimizeNone))
     305       89290 :     return "optnone";
     306      171280 :   if (hasAttribute(Attribute::OptimizeForSize))
     307        1516 :     return "optsize";
     308      170522 :   if (hasAttribute(Attribute::ReadNone))
     309       36214 :     return "readnone";
     310      152415 :   if (hasAttribute(Attribute::ReadOnly))
     311       10044 :     return "readonly";
     312      147393 :   if (hasAttribute(Attribute::WriteOnly))
     313        2566 :     return "writeonly";
     314      146110 :   if (hasAttribute(Attribute::Returned))
     315        3436 :     return "returned";
     316      144392 :   if (hasAttribute(Attribute::ReturnsTwice))
     317         232 :     return "returns_twice";
     318      144276 :   if (hasAttribute(Attribute::SExt))
     319        6002 :     return "signext";
     320      141275 :   if (hasAttribute(Attribute::Speculatable))
     321        7296 :     return "speculatable";
     322      137627 :   if (hasAttribute(Attribute::StackProtect))
     323        2496 :     return "ssp";
     324      136379 :   if (hasAttribute(Attribute::StackProtectReq))
     325         156 :     return "sspreq";
     326      136301 :   if (hasAttribute(Attribute::StackProtectStrong))
     327         184 :     return "sspstrong";
     328      136209 :   if (hasAttribute(Attribute::SafeStack))
     329         612 :     return "safestack";
     330      135903 :   if (hasAttribute(Attribute::StrictFP))
     331           8 :     return "strictfp";
     332      135899 :   if (hasAttribute(Attribute::StructRet))
     333        4642 :     return "sret";
     334      133578 :   if (hasAttribute(Attribute::SanitizeThread))
     335         294 :     return "sanitize_thread";
     336      133431 :   if (hasAttribute(Attribute::SanitizeMemory))
     337        1134 :     return "sanitize_memory";
     338      132864 :   if (hasAttribute(Attribute::UWTable))
     339        5940 :     return "uwtable";
     340      129894 :   if (hasAttribute(Attribute::ZExt))
     341       14284 :     return "zeroext";
     342      122752 :   if (hasAttribute(Attribute::Cold))
     343         122 :     return "cold";
     344             : 
     345             :   // FIXME: These should be output like this:
     346             :   //
     347             :   //   align=4
     348             :   //   alignstack=8
     349             :   //
     350      122691 :   if (hasAttribute(Attribute::Alignment)) {
     351        2414 :     std::string Result;
     352        1207 :     Result += "align";
     353        2414 :     Result += (InAttrGrp) ? "=" : " ";
     354        3621 :     Result += utostr(getValueAsInt());
     355        1207 :     return Result;
     356             :   }
     357             : 
     358        9575 :   auto AttrWithBytesToString = [&](const char *Name) {
     359        9575 :     std::string Result;
     360        9575 :     Result += Name;
     361        9575 :     if (InAttrGrp) {
     362          35 :       Result += "=";
     363        9645 :       Result += utostr(getValueAsInt());
     364             :     } else {
     365        9540 :       Result += "(";
     366       28620 :       Result += utostr(getValueAsInt());
     367             :       Result += ")";
     368             :     }
     369        9575 :     return Result;
     370      121484 :   };
     371             : 
     372      121484 :   if (hasAttribute(Attribute::StackAlignment))
     373          70 :     return AttrWithBytesToString("alignstack");
     374             : 
     375      121414 :   if (hasAttribute(Attribute::Dereferenceable))
     376        9460 :     return AttrWithBytesToString("dereferenceable");
     377             : 
     378      111954 :   if (hasAttribute(Attribute::DereferenceableOrNull))
     379          45 :     return AttrWithBytesToString("dereferenceable_or_null");
     380             : 
     381      111909 :   if (hasAttribute(Attribute::AllocSize)) {
     382             :     unsigned ElemSize;
     383          82 :     Optional<unsigned> NumElems;
     384         123 :     std::tie(ElemSize, NumElems) = getAllocSizeArgs();
     385             : 
     386         123 :     std::string Result = "allocsize(";
     387         123 :     Result += utostr(ElemSize);
     388          41 :     if (NumElems.hasValue()) {
     389          20 :       Result += ',';
     390          60 :       Result += utostr(*NumElems);
     391             :     }
     392          41 :     Result += ')';
     393          41 :     return Result;
     394             :   }
     395             : 
     396             :   // Convert target-dependent attributes to strings of the form:
     397             :   //
     398             :   //   "kind"
     399             :   //   "kind" = "value"
     400             :   //
     401      111868 :   if (isStringAttribute()) {
     402      223736 :     std::string Result;
     403      783076 :     Result += (Twine('"') + getKindAsString() + Twine('"')).str();
     404             : 
     405      335604 :     std::string AttrVal = pImpl->getValueAsString();
     406      111868 :     if (AttrVal.empty()) return Result;
     407             : 
     408             :     // Since some attribute strings contain special characters that cannot be
     409             :     // printable, those have to be escaped to make the attribute value printable
     410             :     // as is.  e.g. "\01__gnu_mcount_nc"
     411             :     {
     412      220954 :       raw_string_ostream OS(Result);
     413      110477 :       OS << "=\"";
     414      110477 :       PrintEscapedString(AttrVal, OS);
     415      110477 :       OS << "\"";
     416             :     }
     417             :     return Result;
     418             :   }
     419             : 
     420           0 :   llvm_unreachable("Unknown attribute");
     421             : }
     422             : 
     423    12131099 : bool Attribute::operator<(Attribute A) const {
     424    12131099 :   if (!pImpl && !A.pImpl) return false;
     425    12131099 :   if (!pImpl) return true;
     426    12131099 :   if (!A.pImpl) return false;
     427    12131099 :   return *pImpl < *A.pImpl;
     428             : }
     429             : 
     430             : //===----------------------------------------------------------------------===//
     431             : // AttributeImpl Definition
     432             : //===----------------------------------------------------------------------===//
     433             : 
     434             : // Pin the vtables to this file.
     435             : AttributeImpl::~AttributeImpl() = default;
     436             : 
     437           0 : void EnumAttributeImpl::anchor() {}
     438             : 
     439           0 : void IntAttributeImpl::anchor() {}
     440             : 
     441           0 : void StringAttributeImpl::anchor() {}
     442             : 
     443    19753134 : bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
     444    19753134 :   if (isStringAttribute()) return false;
     445     9675170 :   return getKindAsEnum() == A;
     446             : }
     447             : 
     448    95023720 : bool AttributeImpl::hasAttribute(StringRef Kind) const {
     449    95023720 :   if (!isStringAttribute()) return false;
     450    95023720 :   return getKindAsString() == Kind;
     451             : }
     452             : 
     453    17358528 : Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
     454             :   assert(isEnumAttribute() || isIntAttribute());
     455    17358528 :   return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
     456             : }
     457             : 
     458      756692 : uint64_t AttributeImpl::getValueAsInt() const {
     459             :   assert(isIntAttribute());
     460      756692 :   return static_cast<const IntAttributeImpl *>(this)->getValue();
     461             : }
     462             : 
     463   135041534 : StringRef AttributeImpl::getKindAsString() const {
     464             :   assert(isStringAttribute());
     465   135041534 :   return static_cast<const StringAttributeImpl *>(this)->getStringKind();
     466             : }
     467             : 
     468    16086974 : StringRef AttributeImpl::getValueAsString() const {
     469             :   assert(isStringAttribute());
     470    16086974 :   return static_cast<const StringAttributeImpl *>(this)->getStringValue();
     471             : }
     472             : 
     473    12131099 : bool AttributeImpl::operator<(const AttributeImpl &AI) const {
     474             :   // This sorts the attributes with Attribute::AttrKinds coming first (sorted
     475             :   // relative to their enum value) and then strings.
     476    12131099 :   if (isEnumAttribute()) {
     477     1131745 :     if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
     478      154115 :     if (AI.isIntAttribute()) return true;
     479      121738 :     if (AI.isStringAttribute()) return true;
     480             :   }
     481             : 
     482    10999354 :   if (isIntAttribute()) {
     483         258 :     if (AI.isEnumAttribute()) return false;
     484         145 :     if (AI.isIntAttribute()) {
     485         115 :       if (getKindAsEnum() == AI.getKindAsEnum())
     486           1 :         return getValueAsInt() < AI.getValueAsInt();
     487         114 :       return getKindAsEnum() < AI.getKindAsEnum();
     488             :     }
     489          30 :     if (AI.isStringAttribute()) return true;
     490             :   }
     491             : 
     492    10999096 :   if (AI.isEnumAttribute()) return false;
     493     7578157 :   if (AI.isIntAttribute()) return false;
     494     7577852 :   if (getKindAsString() == AI.getKindAsString())
     495          52 :     return getValueAsString() < AI.getValueAsString();
     496     7577800 :   return getKindAsString() < AI.getKindAsString();
     497             : }
     498             : 
     499             : //===----------------------------------------------------------------------===//
     500             : // AttributeSet Definition
     501             : //===----------------------------------------------------------------------===//
     502             : 
     503     3178642 : AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
     504     3178642 :   return AttributeSet(AttributeSetNode::get(C, B));
     505             : }
     506             : 
     507      118264 : AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
     508      118264 :   return AttributeSet(AttributeSetNode::get(C, Attrs));
     509             : }
     510             : 
     511           0 : AttributeSet AttributeSet::addAttribute(LLVMContext &C,
     512             :                                         Attribute::AttrKind Kind) const {
     513           0 :   if (hasAttribute(Kind)) return *this;
     514           0 :   AttrBuilder B;
     515           0 :   B.addAttribute(Kind);
     516           0 :   return addAttributes(C, AttributeSet::get(C, B));
     517             : }
     518             : 
     519          44 : AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
     520             :                                         StringRef Value) const {
     521          88 :   AttrBuilder B;
     522          44 :   B.addAttribute(Kind, Value);
     523          88 :   return addAttributes(C, AttributeSet::get(C, B));
     524             : }
     525             : 
     526          44 : AttributeSet AttributeSet::addAttributes(LLVMContext &C,
     527             :                                          const AttributeSet AS) const {
     528          44 :   if (!hasAttributes())
     529          19 :     return AS;
     530             : 
     531          25 :   if (!AS.hasAttributes())
     532           0 :     return *this;
     533             : 
     534          25 :   AttrBuilder B(AS);
     535          59 :   for (Attribute I : *this)
     536          34 :     B.addAttribute(I);
     537             : 
     538          25 :  return get(C, B);
     539             : }
     540             : 
     541           0 : AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
     542             :                                              Attribute::AttrKind Kind) const {
     543           0 :   if (!hasAttribute(Kind)) return *this;
     544           0 :   AttrBuilder B;
     545           0 :   B.addAttribute(Kind);
     546           0 :   return removeAttributes(C, B);
     547             : }
     548             : 
     549           0 : AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
     550             :                                              StringRef Kind) const {
     551           0 :   if (!hasAttribute(Kind)) return *this;
     552           0 :   AttrBuilder B;
     553           0 :   B.addAttribute(Kind);
     554           0 :   return removeAttributes(C, B);
     555             : }
     556             : 
     557           0 : AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
     558             :                                               const AttrBuilder &Attrs) const {
     559             : 
     560             :   // FIXME it is not obvious how this should work for alignment.
     561             :   // For now, say we can't pass in alignment, which no current use does.
     562             :   assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
     563             : 
     564           0 :   AttrBuilder B(*this);
     565           0 :   B.remove(Attrs);
     566           0 :   return get(C, B);
     567             : }
     568             : 
     569           4 : unsigned AttributeSet::getNumAttributes() const {
     570           4 :   return SetNode ? SetNode->getNumAttributes() : 0;
     571             : }
     572             : 
     573   314330638 : bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
     574   492700688 :   return SetNode ? SetNode->hasAttribute(Kind) : false;
     575             : }
     576             : 
     577     9800085 : bool AttributeSet::hasAttribute(StringRef Kind) const {
     578     9800085 :   return SetNode ? SetNode->hasAttribute(Kind) : false;
     579             : }
     580             : 
     581     2831347 : Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
     582     2831347 :   return SetNode ? SetNode->getAttribute(Kind) : Attribute();
     583             : }
     584             : 
     585    13906179 : Attribute AttributeSet::getAttribute(StringRef Kind) const {
     586    13906179 :   return SetNode ? SetNode->getAttribute(Kind) : Attribute();
     587             : }
     588             : 
     589     1531512 : unsigned AttributeSet::getAlignment() const {
     590     1531512 :   return SetNode ? SetNode->getAlignment() : 0;
     591             : }
     592             : 
     593          60 : unsigned AttributeSet::getStackAlignment() const {
     594          60 :   return SetNode ? SetNode->getStackAlignment() : 0;
     595             : }
     596             : 
     597     1291815 : uint64_t AttributeSet::getDereferenceableBytes() const {
     598     1291815 :   return SetNode ? SetNode->getDereferenceableBytes() : 0;
     599             : }
     600             : 
     601       47605 : uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
     602       47605 :   return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
     603             : }
     604             : 
     605          79 : std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
     606          79 :   return SetNode ? SetNode->getAllocSizeArgs()
     607         158 :                  : std::pair<unsigned, Optional<unsigned>>(0, 0);
     608             : }
     609             : 
     610       60752 : std::string AttributeSet::getAsString(bool InAttrGrp) const {
     611       60752 :   return SetNode ? SetNode->getAsString(InAttrGrp) : "";
     612             : }
     613             : 
     614     2618875 : AttributeSet::iterator AttributeSet::begin() const {
     615     4379783 :   return SetNode ? SetNode->begin() : nullptr;
     616             : }
     617             : 
     618     2618875 : AttributeSet::iterator AttributeSet::end() const {
     619     4379783 :   return SetNode ? SetNode->end() : nullptr;
     620             : }
     621             : 
     622             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     623             : LLVM_DUMP_METHOD void AttributeSet::dump() const {
     624             :   dbgs() << "AS =\n";
     625             :     dbgs() << "  { ";
     626             :     dbgs() << getAsString(true) << " }\n";
     627             : }
     628             : #endif
     629             : 
     630             : //===----------------------------------------------------------------------===//
     631             : // AttributeSetNode Definition
     632             : //===----------------------------------------------------------------------===//
     633             : 
     634      101650 : AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
     635      203300 :     : AvailableAttrs(0), NumAttrs(Attrs.size()) {
     636             :   // There's memory after the node where we can store the entries in.
     637      304950 :   std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
     638             : 
     639      823122 :   for (Attribute I : *this) {
     640      518172 :     if (!I.isStringAttribute()) {
     641      141484 :       AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
     642             :     }
     643             :   }
     644      101650 : }
     645             : 
     646     3296906 : AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
     647             :                                         ArrayRef<Attribute> Attrs) {
     648     3296906 :   if (Attrs.empty())
     649             :     return nullptr;
     650             : 
     651             :   // Otherwise, build a key to look up the existing attributes.
     652     1494078 :   LLVMContextImpl *pImpl = C.pImpl;
     653     1494078 :   FoldingSetNodeID ID;
     654             : 
     655     4482234 :   SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
     656     4482234 :   std::sort(SortedAttrs.begin(), SortedAttrs.end());
     657             : 
     658    11836337 :   for (Attribute Attr : SortedAttrs)
     659     7354103 :     Attr.Profile(ID);
     660             : 
     661             :   void *InsertPoint;
     662             :   AttributeSetNode *PA =
     663     2988156 :     pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
     664             : 
     665             :   // If we didn't find any existing attributes of the same shape then create a
     666             :   // new one and insert it.
     667     1494078 :   if (!PA) {
     668             :     // Coallocate entries after the AttributeSetNode itself.
     669      203300 :     void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
     670      101650 :     PA = new (Mem) AttributeSetNode(SortedAttrs);
     671      101650 :     pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
     672             :   }
     673             : 
     674             :   // Return the AttributeSetNode that we found or created.
     675     1494078 :   return PA;
     676             : }
     677             : 
     678     3178642 : AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
     679             :   // Add target-independent attributes.
     680     6357284 :   SmallVector<Attribute, 8> Attrs;
     681   178003952 :   for (Attribute::AttrKind Kind = Attribute::None;
     682   352829262 :        Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
     683   174825310 :     if (!B.contains(Kind))
     684   173297111 :       continue;
     685             : 
     686     1528199 :     Attribute Attr;
     687     1528199 :     switch (Kind) {
     688        4423 :     case Attribute::Alignment:
     689        4423 :       Attr = Attribute::getWithAlignment(C, B.getAlignment());
     690        4423 :       break;
     691         197 :     case Attribute::StackAlignment:
     692         197 :       Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
     693         197 :       break;
     694       87423 :     case Attribute::Dereferenceable:
     695       87423 :       Attr = Attribute::getWithDereferenceableBytes(
     696       87423 :           C, B.getDereferenceableBytes());
     697       87423 :       break;
     698          77 :     case Attribute::DereferenceableOrNull:
     699          77 :       Attr = Attribute::getWithDereferenceableOrNullBytes(
     700          77 :           C, B.getDereferenceableOrNullBytes());
     701          77 :       break;
     702         105 :     case Attribute::AllocSize: {
     703         210 :       auto A = B.getAllocSizeArgs();
     704         105 :       Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
     705             :       break;
     706             :     }
     707     1435974 :     default:
     708     1435974 :       Attr = Attribute::get(C, Kind);
     709             :     }
     710     1528199 :     Attrs.push_back(Attr);
     711             :   }
     712             : 
     713             :   // Add target-dependent (string) attributes.
     714     8774017 :   for (const auto &TDA : B.td_attrs())
     715    16786125 :     Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
     716             : 
     717     6357284 :   return get(C, Attrs);
     718             : }
     719             : 
     720     8532901 : bool AttributeSetNode::hasAttribute(StringRef Kind) const {
     721    64636820 :   for (Attribute I : *this)
     722    39087882 :     if (I.hasAttribute(Kind))
     723       49765 :       return true;
     724     8483136 :   return false;
     725             : }
     726             : 
     727     2820937 : Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
     728     5641874 :   if (hasAttribute(Kind)) {
     729         359 :     for (Attribute I : *this)
     730         149 :       if (I.hasAttribute(Kind))
     731         105 :         return I;
     732             :   }
     733     2820832 :   return Attribute();
     734             : }
     735             : 
     736    12981041 : Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
     737   107575531 :   for (Attribute I : *this)
     738    75013671 :     if (I.hasAttribute(Kind))
     739     6381266 :       return I;
     740     6599778 :   return Attribute();
     741             : }
     742             : 
     743      324450 : unsigned AttributeSetNode::getAlignment() const {
     744     1411873 :   for (Attribute I : *this)
     745      455578 :     if (I.hasAttribute(Attribute::Alignment))
     746       17055 :       return I.getAlignment();
     747      307395 :   return 0;
     748             : }
     749             : 
     750          60 : unsigned AttributeSetNode::getStackAlignment() const {
     751         198 :   for (Attribute I : *this)
     752          78 :     if (I.hasAttribute(Attribute::StackAlignment))
     753          60 :       return I.getStackAlignment();
     754           0 :   return 0;
     755             : }
     756             : 
     757      354363 : uint64_t AttributeSetNode::getDereferenceableBytes() const {
     758     1451379 :   for (Attribute I : *this)
     759      429451 :     if (I.hasAttribute(Attribute::Dereferenceable))
     760       41161 :       return I.getDereferenceableBytes();
     761      313202 :   return 0;
     762             : }
     763             : 
     764        6613 : uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
     765       30053 :   for (Attribute I : *this)
     766       10227 :     if (I.hasAttribute(Attribute::DereferenceableOrNull))
     767          13 :       return I.getDereferenceableOrNullBytes();
     768        6600 :   return 0;
     769             : }
     770             : 
     771             : std::pair<unsigned, Optional<unsigned>>
     772          79 : AttributeSetNode::getAllocSizeArgs() const {
     773         237 :   for (Attribute I : *this)
     774          79 :     if (I.hasAttribute(Attribute::AllocSize))
     775          79 :       return I.getAllocSizeArgs();
     776           0 :   return std::make_pair(0, 0);
     777             : }
     778             : 
     779       60752 : std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
     780       60752 :   std::string Str;
     781      318430 :   for (iterator I = begin(), E = end(); I != E; ++I) {
     782      196926 :     if (I != begin())
     783             :       Str += ' ';
     784      590778 :     Str += I->getAsString(InAttrGrp);
     785             :   }
     786       60752 :   return Str;
     787             : }
     788             : 
     789             : //===----------------------------------------------------------------------===//
     790             : // AttributeListImpl Definition
     791             : //===----------------------------------------------------------------------===//
     792             : 
     793             : /// Map from AttributeList index to the internal array index. Adding one works:
     794             : ///   FunctionIndex: ~0U -> 0
     795             : ///   ReturnIndex:    0  -> 1
     796             : ///   FirstArgIndex: 1.. -> 2..
     797             : static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
     798             :   // MSVC warns about '~0U + 1' wrapping around when this is called on
     799             :   // FunctionIndex, so cast to int first.
     800   333874088 :   return static_cast<int>(Index) + 1;
     801             : }
     802             : 
     803      153589 : AttributeListImpl::AttributeListImpl(LLVMContext &C,
     804      153589 :                                      ArrayRef<AttributeSet> Sets)
     805      307178 :     : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
     806             :   assert(!Sets.empty() && "pointless AttributeListImpl");
     807             : 
     808             :   // There's memory after the node where we can store the entries in.
     809      460767 :   std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
     810             : 
     811             :   // Initialize AvailableFunctionAttrs summary bitset.
     812             :   static_assert(Attribute::EndAttrKinds <=
     813             :                     sizeof(AvailableFunctionAttrs) * CHAR_BIT,
     814             :                 "Too many attributes");
     815             :   static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
     816             :                 "function should be stored in slot 0");
     817     1088395 :   for (Attribute I : Sets[0]) {
     818      934806 :     if (!I.isStringAttribute())
     819      165250 :       AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
     820             :   }
     821      153589 : }
     822             : 
     823     1565524 : void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
     824     4696572 :   Profile(ID, makeArrayRef(begin(), end()));
     825     1565524 : }
     826             : 
     827     2921353 : void AttributeListImpl::Profile(FoldingSetNodeID &ID,
     828             :                                 ArrayRef<AttributeSet> Sets) {
     829    11849144 :   for (const auto &Set : Sets)
     830     6006438 :     ID.AddPointer(Set.SetNode);
     831     2921353 : }
     832             : 
     833             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     834             : LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
     835             :   AttributeList(const_cast<AttributeListImpl *>(this)).dump();
     836             : }
     837             : #endif
     838             : 
     839             : //===----------------------------------------------------------------------===//
     840             : // AttributeList Construction and Mutation Methods
     841             : //===----------------------------------------------------------------------===//
     842             : 
     843     1355829 : AttributeList AttributeList::getImpl(LLVMContext &C,
     844             :                                      ArrayRef<AttributeSet> AttrSets) {
     845             :   assert(!AttrSets.empty() && "pointless AttributeListImpl");
     846             : 
     847     1355829 :   LLVMContextImpl *pImpl = C.pImpl;
     848     2711658 :   FoldingSetNodeID ID;
     849     1355829 :   AttributeListImpl::Profile(ID, AttrSets);
     850             : 
     851             :   void *InsertPoint;
     852             :   AttributeListImpl *PA =
     853     2711658 :       pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
     854             : 
     855             :   // If we didn't find any existing attributes of the same shape then
     856             :   // create a new one and insert it.
     857     1355829 :   if (!PA) {
     858             :     // Coallocate entries after the AttributeListImpl itself.
     859      307178 :     void *Mem = ::operator new(
     860      153589 :         AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
     861      153589 :     PA = new (Mem) AttributeListImpl(C, AttrSets);
     862      153589 :     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
     863             :   }
     864             : 
     865             :   // Return the AttributesList that we found or created.
     866     2711658 :   return AttributeList(PA);
     867             : }
     868             : 
     869             : AttributeList
     870      299093 : AttributeList::get(LLVMContext &C,
     871             :                    ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
     872             :   // If there are no attributes then return a null AttributesList pointer.
     873      299093 :   if (Attrs.empty())
     874      180829 :     return AttributeList();
     875             : 
     876             :   assert(std::is_sorted(Attrs.begin(), Attrs.end(),
     877             :                         [](const std::pair<unsigned, Attribute> &LHS,
     878             :                            const std::pair<unsigned, Attribute> &RHS) {
     879             :                           return LHS.first < RHS.first;
     880             :                         }) && "Misordered Attributes list!");
     881             :   assert(none_of(Attrs,
     882             :                  [](const std::pair<unsigned, Attribute> &Pair) {
     883             :                    return Pair.second.hasAttribute(Attribute::None);
     884             :                  }) &&
     885             :          "Pointless attribute!");
     886             : 
     887             :   // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
     888             :   // list.
     889      118264 :   SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
     890      236528 :   for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
     891      118264 :          E = Attrs.end(); I != E; ) {
     892      118264 :     unsigned Index = I->first;
     893      118264 :     SmallVector<Attribute, 4> AttrVec;
     894      579322 :     while (I != E && I->first == Index) {
     895      230529 :       AttrVec.push_back(I->second);
     896      230529 :       ++I;
     897             :     }
     898             : 
     899      118264 :     AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
     900             :   }
     901             : 
     902      118264 :   return get(C, AttrPairVec);
     903             : }
     904             : 
     905             : AttributeList
     906      336219 : AttributeList::get(LLVMContext &C,
     907             :                    ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
     908             :   // If there are no attributes then return a null AttributesList pointer.
     909      336219 :   if (Attrs.empty())
     910           0 :     return AttributeList();
     911             : 
     912             :   assert(std::is_sorted(Attrs.begin(), Attrs.end(),
     913             :                         [](const std::pair<unsigned, AttributeSet> &LHS,
     914             :                            const std::pair<unsigned, AttributeSet> &RHS) {
     915             :                           return LHS.first < RHS.first;
     916             :                         }) &&
     917             :          "Misordered Attributes list!");
     918             :   assert(none_of(Attrs,
     919             :                  [](const std::pair<unsigned, AttributeSet> &Pair) {
     920             :                    return !Pair.second.hasAttributes();
     921             :                  }) &&
     922             :          "Pointless attribute!");
     923             : 
     924      336219 :   unsigned MaxIndex = Attrs.back().first;
     925             : 
     926     1008657 :   SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
     927     1008657 :   for (auto Pair : Attrs)
     928     1008657 :     AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
     929             : 
     930      336219 :   return getImpl(C, AttrVec);
     931             : }
     932             : 
     933      628850 : AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
     934             :                                  AttributeSet RetAttrs,
     935             :                                  ArrayRef<AttributeSet> ArgAttrs) {
     936             :   // Scan from the end to find the last argument with attributes.  Most
     937             :   // arguments don't have attributes, so it's nice if we can have fewer unique
     938             :   // AttributeListImpls by dropping empty attribute sets at the end of the list.
     939      628850 :   unsigned NumSets = 0;
     940      628850 :   for (size_t I = ArgAttrs.size(); I != 0; --I) {
     941     2997726 :     if (ArgAttrs[I - 1].hasAttributes()) {
     942       83136 :       NumSets = I + 2;
     943       83136 :       break;
     944             :     }
     945             :   }
     946      628850 :   if (NumSets == 0) {
     947             :     // Check function and return attributes if we didn't have argument
     948             :     // attributes.
     949      545714 :     if (RetAttrs.hasAttributes())
     950             :       NumSets = 2;
     951      524572 :     else if (FnAttrs.hasAttributes())
     952             :       NumSets = 1;
     953             :   }
     954             : 
     955             :   // If all attribute sets were empty, we can use the empty attribute list.
     956      401244 :   if (NumSets == 0)
     957      318108 :     return AttributeList();
     958             : 
     959      310742 :   SmallVector<AttributeSet, 8> AttrSets;
     960      310742 :   AttrSets.reserve(NumSets);
     961             :   // If we have any attributes, we always have function attributes.
     962      310742 :   AttrSets.push_back(FnAttrs);
     963      310742 :   if (NumSets > 1)
     964      104278 :     AttrSets.push_back(RetAttrs);
     965      310742 :   if (NumSets > 2) {
     966             :     // Drop the empty argument attribute sets at the end.
     967      166272 :     ArgAttrs = ArgAttrs.take_front(NumSets - 2);
     968      166272 :     AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
     969             :   }
     970             : 
     971      310742 :   return getImpl(C, AttrSets);
     972             : }
     973             : 
     974        3215 : AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
     975             :                                  const AttrBuilder &B) {
     976        3215 :   if (!B.hasAttributes())
     977          17 :     return AttributeList();
     978        3198 :   Index = attrIdxToArrayIdx(Index);
     979        6396 :   SmallVector<AttributeSet, 8> AttrSets(Index + 1);
     980        6396 :   AttrSets[Index] = AttributeSet::get(C, B);
     981        3198 :   return getImpl(C, AttrSets);
     982             : }
     983             : 
     984      299093 : AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
     985             :                                  ArrayRef<Attribute::AttrKind> Kinds) {
     986      598186 :   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
     987      828715 :   for (Attribute::AttrKind K : Kinds)
     988      230529 :     Attrs.emplace_back(Index, Attribute::get(C, K));
     989      598186 :   return get(C, Attrs);
     990             : }
     991             : 
     992           0 : AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
     993             :                                  ArrayRef<StringRef> Kinds) {
     994           0 :   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
     995           0 :   for (StringRef K : Kinds)
     996           0 :     Attrs.emplace_back(Index, Attribute::get(C, K));
     997           0 :   return get(C, Attrs);
     998             : }
     999             : 
    1000      100371 : AttributeList AttributeList::get(LLVMContext &C,
    1001             :                                  ArrayRef<AttributeList> Attrs) {
    1002      100371 :   if (Attrs.empty())
    1003           0 :     return AttributeList();
    1004      100371 :   if (Attrs.size() == 1)
    1005       92920 :     return Attrs[0];
    1006             : 
    1007        7451 :   unsigned MaxSize = 0;
    1008       32416 :   for (AttributeList List : Attrs)
    1009       35028 :     MaxSize = std::max(MaxSize, List.getNumAttrSets());
    1010             : 
    1011             :   // If every list was empty, there is no point in merging the lists.
    1012        7451 :   if (MaxSize == 0)
    1013           1 :     return AttributeList();
    1014             : 
    1015       14900 :   SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
    1016       37586 :   for (unsigned I = 0; I < MaxSize; ++I) {
    1017       60272 :     AttrBuilder CurBuilder;
    1018      101975 :     for (AttributeList List : Attrs)
    1019      143678 :       CurBuilder.merge(List.getAttributes(I - 1));
    1020       60272 :     NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
    1021             :   }
    1022             : 
    1023        7450 :   return getImpl(C, NewAttrSets);
    1024             : }
    1025             : 
    1026      251903 : AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
    1027             :                                           Attribute::AttrKind Kind) const {
    1028      251903 :   if (hasAttribute(Index, Kind)) return *this;
    1029      210387 :   AttrBuilder B;
    1030      210387 :   B.addAttribute(Kind);
    1031      210387 :   return addAttributes(C, Index, B);
    1032             : }
    1033             : 
    1034          40 : AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
    1035             :                                           StringRef Kind,
    1036             :                                           StringRef Value) const {
    1037          80 :   AttrBuilder B;
    1038          40 :   B.addAttribute(Kind, Value);
    1039          80 :   return addAttributes(C, Index, B);
    1040             : }
    1041             : 
    1042      106016 : AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
    1043             :                                           Attribute A) const {
    1044      212032 :   AttrBuilder B;
    1045      106016 :   B.addAttribute(A);
    1046      212032 :   return addAttributes(C, Index, B);
    1047             : }
    1048             : 
    1049      987929 : AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
    1050             :                                            const AttrBuilder &B) const {
    1051      987929 :   if (!B.hasAttributes())
    1052      337268 :     return *this;
    1053             : 
    1054      650661 :   if (!pImpl)
    1055      653793 :     return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
    1056             : 
    1057             : #ifndef NDEBUG
    1058             :   // FIXME it is not obvious how this should work for alignment. For now, say
    1059             :   // we can't change a known alignment.
    1060             :   unsigned OldAlign = getAttributes(Index).getAlignment();
    1061             :   unsigned NewAlign = B.getAlignment();
    1062             :   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
    1063             :          "Attempt to change alignment!");
    1064             : #endif
    1065             : 
    1066      865460 :   Index = attrIdxToArrayIdx(Index);
    1067      865460 :   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
    1068      865460 :   if (Index >= AttrSets.size())
    1069        7977 :     AttrSets.resize(Index + 1);
    1070             : 
    1071     1298190 :   AttrBuilder Merged(AttrSets[Index]);
    1072      432730 :   Merged.merge(B);
    1073      865460 :   AttrSets[Index] = AttributeSet::get(C, Merged);
    1074             : 
    1075      432730 :   return getImpl(C, AttrSets);
    1076             : }
    1077             : 
    1078      172676 : AttributeList AttributeList::addParamAttribute(LLVMContext &C,
    1079             :                                                ArrayRef<unsigned> ArgNos,
    1080             :                                                Attribute A) const {
    1081             :   assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
    1082             : 
    1083      518028 :   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
    1084      345352 :   unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
    1085      345352 :   if (MaxIndex >= AttrSets.size())
    1086      144133 :     AttrSets.resize(MaxIndex + 1);
    1087             : 
    1088      537553 :   for (unsigned ArgNo : ArgNos) {
    1089      384402 :     unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
    1090      576603 :     AttrBuilder B(AttrSets[Index]);
    1091      192201 :     B.addAttribute(A);
    1092      384402 :     AttrSets[Index] = AttributeSet::get(C, B);
    1093             :   }
    1094             : 
    1095      345352 :   return getImpl(C, AttrSets);
    1096             : }
    1097             : 
    1098       95536 : AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
    1099             :                                              Attribute::AttrKind Kind) const {
    1100       95536 :   if (!hasAttribute(Index, Kind)) return *this;
    1101         260 :   AttrBuilder B;
    1102         260 :   B.addAttribute(Kind);
    1103         260 :   return removeAttributes(C, Index, B);
    1104             : }
    1105             : 
    1106          29 : AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
    1107             :                                              StringRef Kind) const {
    1108          29 :   if (!hasAttribute(Index, Kind)) return *this;
    1109          29 :   AttrBuilder B;
    1110          29 :   B.addAttribute(Kind);
    1111          29 :   return removeAttributes(C, Index, B);
    1112             : }
    1113             : 
    1114             : AttributeList
    1115        9317 : AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
    1116             :                                 const AttrBuilder &AttrsToRemove) const {
    1117        9317 :   if (!pImpl)
    1118         202 :     return AttributeList();
    1119             : 
    1120             :   // FIXME it is not obvious how this should work for alignment.
    1121             :   // For now, say we can't pass in alignment, which no current use does.
    1122             :   assert(!AttrsToRemove.hasAlignmentAttr() && "Attempt to change alignment!");
    1123             : 
    1124        9115 :   Index = attrIdxToArrayIdx(Index);
    1125       18230 :   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
    1126       18230 :   if (Index >= AttrSets.size())
    1127          44 :     AttrSets.resize(Index + 1);
    1128             : 
    1129       27345 :   AttrBuilder B(AttrSets[Index]);
    1130        9115 :   B.remove(AttrsToRemove);
    1131       18230 :   AttrSets[Index] = AttributeSet::get(C, B);
    1132             : 
    1133        9115 :   return getImpl(C, AttrSets);
    1134             : }
    1135             : 
    1136      342339 : AttributeList AttributeList::removeAttributes(LLVMContext &C,
    1137             :                                               unsigned WithoutIndex) const {
    1138      342339 :   if (!pImpl)
    1139      258640 :     return AttributeList();
    1140       83699 :   WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
    1141       83699 :   if (WithoutIndex >= getNumAttrSets())
    1142           0 :     return *this;
    1143      167398 :   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
    1144      167398 :   AttrSets[WithoutIndex] = AttributeSet();
    1145       83699 :   return getImpl(C, AttrSets);
    1146             : }
    1147             : 
    1148          41 : AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
    1149             :                                                     unsigned Index,
    1150             :                                                     uint64_t Bytes) const {
    1151          82 :   AttrBuilder B;
    1152          41 :   B.addDereferenceableAttr(Bytes);
    1153          82 :   return addAttributes(C, Index, B);
    1154             : }
    1155             : 
    1156             : AttributeList
    1157           0 : AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
    1158             :                                             uint64_t Bytes) const {
    1159           0 :   AttrBuilder B;
    1160           0 :   B.addDereferenceableOrNullAttr(Bytes);
    1161           0 :   return addAttributes(C, Index, B);
    1162             : }
    1163             : 
    1164             : AttributeList
    1165           0 : AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
    1166             :                                 unsigned ElemSizeArg,
    1167             :                                 const Optional<unsigned> &NumElemsArg) {
    1168           0 :   AttrBuilder B;
    1169           0 :   B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
    1170           0 :   return addAttributes(C, Index, B);
    1171             : }
    1172             : 
    1173             : //===----------------------------------------------------------------------===//
    1174             : // AttributeList Accessor Methods
    1175             : //===----------------------------------------------------------------------===//
    1176             : 
    1177           4 : LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
    1178             : 
    1179     1671288 : AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
    1180     1671288 :   return getAttributes(ArgNo + FirstArgIndex);
    1181             : }
    1182             : 
    1183      680098 : AttributeSet AttributeList::getRetAttributes() const {
    1184      680098 :   return getAttributes(ReturnIndex);
    1185             : }
    1186             : 
    1187     1592857 : AttributeSet AttributeList::getFnAttributes() const {
    1188     1592857 :   return getAttributes(FunctionIndex);
    1189             : }
    1190             : 
    1191   297583869 : bool AttributeList::hasAttribute(unsigned Index,
    1192             :                                  Attribute::AttrKind Kind) const {
    1193   297583869 :   return getAttributes(Index).hasAttribute(Kind);
    1194             : }
    1195             : 
    1196     9607358 : bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
    1197     9607358 :   return getAttributes(Index).hasAttribute(Kind);
    1198             : }
    1199             : 
    1200     1462871 : bool AttributeList::hasAttributes(unsigned Index) const {
    1201     2925742 :   return getAttributes(Index).hasAttributes();
    1202             : }
    1203             : 
    1204    85551396 : bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
    1205   168930496 :   return pImpl && pImpl->hasFnAttribute(Kind);
    1206             : }
    1207             : 
    1208     9315712 : bool AttributeList::hasFnAttribute(StringRef Kind) const {
    1209     9315712 :   return hasAttribute(AttributeList::FunctionIndex, Kind);
    1210             : }
    1211             : 
    1212    45489590 : bool AttributeList::hasParamAttribute(unsigned ArgNo,
    1213             :                                       Attribute::AttrKind Kind) const {
    1214    45489590 :   return hasAttribute(ArgNo + FirstArgIndex, Kind);
    1215             : }
    1216             : 
    1217     3120722 : bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
    1218             :                                      unsigned *Index) const {
    1219     3120722 :   if (!pImpl) return false;
    1220             : 
    1221    10284578 :   for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
    1222     4957070 :     if (hasAttribute(I, Attr)) {
    1223        3246 :       if (Index)
    1224         586 :         *Index = I;
    1225             :       return true;
    1226             :     }
    1227             :   }
    1228             : 
    1229             :   return false;
    1230             : }
    1231             : 
    1232     2831347 : Attribute AttributeList::getAttribute(unsigned Index,
    1233             :                                       Attribute::AttrKind Kind) const {
    1234     2831347 :   return getAttributes(Index).getAttribute(Kind);
    1235             : }
    1236             : 
    1237    13906166 : Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
    1238    13906166 :   return getAttributes(Index).getAttribute(Kind);
    1239             : }
    1240             : 
    1241      129036 : unsigned AttributeList::getRetAlignment() const {
    1242      129036 :   return getAttributes(ReturnIndex).getAlignment();
    1243             : }
    1244             : 
    1245     1402476 : unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
    1246     1402476 :   return getAttributes(ArgNo + FirstArgIndex).getAlignment();
    1247             : }
    1248             : 
    1249          60 : unsigned AttributeList::getStackAlignment(unsigned Index) const {
    1250          60 :   return getAttributes(Index).getStackAlignment();
    1251             : }
    1252             : 
    1253     1291815 : uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
    1254     1291815 :   return getAttributes(Index).getDereferenceableBytes();
    1255             : }
    1256             : 
    1257       47605 : uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
    1258       47605 :   return getAttributes(Index).getDereferenceableOrNullBytes();
    1259             : }
    1260             : 
    1261             : std::pair<unsigned, Optional<unsigned>>
    1262          79 : AttributeList::getAllocSizeArgs(unsigned Index) const {
    1263          79 :   return getAttributes(Index).getAllocSizeArgs();
    1264             : }
    1265             : 
    1266        4987 : std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
    1267        4987 :   return getAttributes(Index).getAsString(InAttrGrp);
    1268             : }
    1269             : 
    1270   332308031 : AttributeSet AttributeList::getAttributes(unsigned Index) const {
    1271   332308031 :   Index = attrIdxToArrayIdx(Index);
    1272   332308031 :   if (!pImpl || Index >= getNumAttrSets())
    1273    95552280 :     return AttributeSet();
    1274   473511504 :   return pImpl->begin()[Index];
    1275             : }
    1276             : 
    1277      698220 : AttributeList::iterator AttributeList::begin() const {
    1278     1340376 :   return pImpl ? pImpl->begin() : nullptr;
    1279             : }
    1280             : 
    1281      698220 : AttributeList::iterator AttributeList::end() const {
    1282     1340376 :   return pImpl ? pImpl->end() : nullptr;
    1283             : }
    1284             : 
    1285             : //===----------------------------------------------------------------------===//
    1286             : // AttributeList Introspection Methods
    1287             : //===----------------------------------------------------------------------===//
    1288             : 
    1289   261966050 : unsigned AttributeList::getNumAttrSets() const {
    1290   261966050 :   return pImpl ? pImpl->NumAttrSets : 0;
    1291             : }
    1292             : 
    1293             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    1294             : LLVM_DUMP_METHOD void AttributeList::dump() const {
    1295             :   dbgs() << "PAL[\n";
    1296             : 
    1297             :   for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
    1298             :     if (getAttributes(i).hasAttributes())
    1299             :       dbgs() << "  { " << i << " => " << getAsString(i) << " }\n";
    1300             :   }
    1301             : 
    1302             :   dbgs() << "]\n";
    1303             : }
    1304             : #endif
    1305             : 
    1306             : //===----------------------------------------------------------------------===//
    1307             : // AttrBuilder Method Implementations
    1308             : //===----------------------------------------------------------------------===//
    1309             : 
    1310             : // FIXME: Remove this ctor, use AttributeSet.
    1311       25824 : AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
    1312        8608 :   AttributeSet AS = AL.getAttributes(Index);
    1313        9541 :   for (const Attribute &A : AS)
    1314         933 :     addAttribute(A);
    1315        8608 : }
    1316             : 
    1317     4660836 : AttrBuilder::AttrBuilder(AttributeSet AS) {
    1318     6056782 :   for (const Attribute &A : AS)
    1319     4503170 :     addAttribute(A);
    1320     1553612 : }
    1321             : 
    1322     1329478 : void AttrBuilder::clear() {
    1323     2658956 :   Attrs.reset();
    1324     2658956 :   TargetDepAttrs.clear();
    1325     1329478 :   Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
    1326     1329478 :   AllocSizeArgs = 0;
    1327     1329478 : }
    1328             : 
    1329     1911430 : AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
    1330             :   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
    1331             :   assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
    1332             :          Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
    1333             :          "Adding integer attribute without adding a value!");
    1334     5734290 :   Attrs[Val] = true;
    1335     1911430 :   return *this;
    1336             : }
    1337             : 
    1338     4802410 : AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
    1339     4802410 :   if (Attr.isStringAttribute()) {
    1340     3860036 :     addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
    1341     3860036 :     return *this;
    1342             :   }
    1343             : 
    1344      942374 :   Attribute::AttrKind Kind = Attr.getKindAsEnum();
    1345     2827122 :   Attrs[Kind] = true;
    1346             : 
    1347      942374 :   if (Kind == Attribute::Alignment)
    1348        4449 :     Alignment = Attr.getAlignment();
    1349      937925 :   else if (Kind == Attribute::StackAlignment)
    1350         115 :     StackAlignment = Attr.getStackAlignment();
    1351      937810 :   else if (Kind == Attribute::Dereferenceable)
    1352       36352 :     DerefBytes = Attr.getDereferenceableBytes();
    1353      901458 :   else if (Kind == Attribute::DereferenceableOrNull)
    1354          74 :     DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
    1355      901384 :   else if (Kind == Attribute::AllocSize)
    1356          38 :     AllocSizeArgs = Attr.getValueAsInt();
    1357             :   return *this;
    1358             : }
    1359             : 
    1360     5499611 : AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
    1361    21998444 :   TargetDepAttrs[A] = V;
    1362     5499611 :   return *this;
    1363             : }
    1364             : 
    1365       39335 : AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
    1366             :   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
    1367      118005 :   Attrs[Val] = false;
    1368             : 
    1369       39335 :   if (Val == Attribute::Alignment)
    1370         592 :     Alignment = 0;
    1371       38743 :   else if (Val == Attribute::StackAlignment)
    1372           0 :     StackAlignment = 0;
    1373       38743 :   else if (Val == Attribute::Dereferenceable)
    1374           0 :     DerefBytes = 0;
    1375       38743 :   else if (Val == Attribute::DereferenceableOrNull)
    1376           0 :     DerefOrNullBytes = 0;
    1377       38743 :   else if (Val == Attribute::AllocSize)
    1378           0 :     AllocSizeArgs = 0;
    1379             : 
    1380       39335 :   return *this;
    1381             : }
    1382             : 
    1383           0 : AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
    1384           0 :   remove(A.getAttributes(Index));
    1385           0 :   return *this;
    1386             : }
    1387             : 
    1388           0 : AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
    1389           0 :   std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
    1390           0 :   if (I != TargetDepAttrs.end())
    1391           0 :     TargetDepAttrs.erase(I);
    1392           0 :   return *this;
    1393             : }
    1394             : 
    1395         105 : std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
    1396         210 :   return unpackAllocSizeArgs(AllocSizeArgs);
    1397             : }
    1398             : 
    1399        3737 : AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
    1400        3737 :   if (Align == 0) return *this;
    1401             : 
    1402             :   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
    1403             :   assert(Align <= 0x40000000 && "Alignment too large.");
    1404             : 
    1405       11211 :   Attrs[Attribute::Alignment] = true;
    1406        3737 :   Alignment = Align;
    1407        3737 :   return *this;
    1408             : }
    1409             : 
    1410          82 : AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
    1411             :   // Default alignment, allow the target to define how to align it.
    1412          82 :   if (Align == 0) return *this;
    1413             : 
    1414             :   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
    1415             :   assert(Align <= 0x100 && "Alignment too large.");
    1416             : 
    1417         246 :   Attrs[Attribute::StackAlignment] = true;
    1418          82 :   StackAlignment = Align;
    1419          82 :   return *this;
    1420             : }
    1421             : 
    1422      147190 : AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
    1423      147190 :   if (Bytes == 0) return *this;
    1424             : 
    1425      441351 :   Attrs[Attribute::Dereferenceable] = true;
    1426      147117 :   DerefBytes = Bytes;
    1427      147117 :   return *this;
    1428             : }
    1429             : 
    1430       86396 : AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
    1431       86396 :   if (Bytes == 0)
    1432             :     return *this;
    1433             : 
    1434      259188 :   Attrs[Attribute::DereferenceableOrNull] = true;
    1435       86396 :   DerefOrNullBytes = Bytes;
    1436       86396 :   return *this;
    1437             : }
    1438             : 
    1439          54 : AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
    1440             :                                            const Optional<unsigned> &NumElems) {
    1441          54 :   return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
    1442             : }
    1443             : 
    1444          64 : AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
    1445             :   // (0, 0) is our "not present" value, so we need to check for it here.
    1446             :   assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
    1447             : 
    1448         192 :   Attrs[Attribute::AllocSize] = true;
    1449             :   // Reuse existing machinery to store this as a single 64-bit integer so we can
    1450             :   // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
    1451          64 :   AllocSizeArgs = RawArgs;
    1452          64 :   return *this;
    1453             : }
    1454             : 
    1455      891901 : AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
    1456             :   // FIXME: What if both have alignments, but they don't match?!
    1457      891901 :   if (!Alignment)
    1458      891542 :     Alignment = B.Alignment;
    1459             : 
    1460      891901 :   if (!StackAlignment)
    1461      891832 :     StackAlignment = B.StackAlignment;
    1462             : 
    1463      891901 :   if (!DerefBytes)
    1464      891296 :     DerefBytes = B.DerefBytes;
    1465             : 
    1466      891901 :   if (!DerefOrNullBytes)
    1467      891896 :     DerefOrNullBytes = B.DerefOrNullBytes;
    1468             : 
    1469      891901 :   if (!AllocSizeArgs)
    1470      891889 :     AllocSizeArgs = B.AllocSizeArgs;
    1471             : 
    1472     1783802 :   Attrs |= B.Attrs;
    1473             : 
    1474     1727493 :   for (auto I : B.td_attrs())
    1475      835592 :     TargetDepAttrs[I.first] = I.second;
    1476             : 
    1477      891901 :   return *this;
    1478             : }
    1479             : 
    1480       12138 : AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
    1481             :   // FIXME: What if both have alignments, but they don't match?!
    1482       12138 :   if (B.Alignment)
    1483           0 :     Alignment = 0;
    1484             : 
    1485       12138 :   if (B.StackAlignment)
    1486           0 :     StackAlignment = 0;
    1487             : 
    1488       12138 :   if (B.DerefBytes)
    1489        3038 :     DerefBytes = 0;
    1490             : 
    1491       12138 :   if (B.DerefOrNullBytes)
    1492        3036 :     DerefOrNullBytes = 0;
    1493             : 
    1494       12138 :   if (B.AllocSizeArgs)
    1495           0 :     AllocSizeArgs = 0;
    1496             : 
    1497       36414 :   Attrs &= ~B.Attrs;
    1498             : 
    1499       12256 :   for (auto I : B.td_attrs())
    1500         118 :     TargetDepAttrs.erase(I.first);
    1501             : 
    1502       12138 :   return *this;
    1503             : }
    1504             : 
    1505      158497 : bool AttrBuilder::overlaps(const AttrBuilder &B) const {
    1506             :   // First check if any of the target independent attributes overlap.
    1507      475491 :   if ((Attrs & B.Attrs).any())
    1508             :     return true;
    1509             : 
    1510             :   // Then check if any target dependent ones do.
    1511      158546 :   for (const auto &I : td_attrs())
    1512         108 :     if (B.contains(I.first))
    1513             :       return true;
    1514             : 
    1515             :   return false;
    1516             : }
    1517             : 
    1518          54 : bool AttrBuilder::contains(StringRef A) const {
    1519         324 :   return TargetDepAttrs.find(A) != TargetDepAttrs.end();
    1520             : }
    1521             : 
    1522     1479914 : bool AttrBuilder::hasAttributes() const {
    1523     3952588 :   return !Attrs.none() || !TargetDepAttrs.empty();
    1524             : }
    1525             : 
    1526           0 : bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
    1527           0 :   AttributeSet AS = AL.getAttributes(Index);
    1528             : 
    1529           0 :   for (Attribute Attr : AS) {
    1530           0 :     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
    1531           0 :       if (contains(Attr.getKindAsEnum()))
    1532           0 :         return true;
    1533             :     } else {
    1534             :       assert(Attr.isStringAttribute() && "Invalid attribute kind!");
    1535           0 :       return contains(Attr.getKindAsString());
    1536             :     }
    1537             :   }
    1538             : 
    1539           0 :   return false;
    1540             : }
    1541             : 
    1542      575400 : bool AttrBuilder::hasAlignmentAttr() const {
    1543      575400 :   return Alignment != 0;
    1544             : }
    1545             : 
    1546        2841 : bool AttrBuilder::operator==(const AttrBuilder &B) {
    1547        5682 :   if (Attrs != B.Attrs)
    1548             :     return false;
    1549             : 
    1550        8484 :   for (td_const_iterator I = TargetDepAttrs.begin(),
    1551        8484 :          E = TargetDepAttrs.end(); I != E; ++I)
    1552           0 :     if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
    1553             :       return false;
    1554             : 
    1555        5655 :   return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
    1556        2827 :          DerefBytes == B.DerefBytes;
    1557             : }
    1558             : 
    1559             : //===----------------------------------------------------------------------===//
    1560             : // AttributeFuncs Function Defintions
    1561             : //===----------------------------------------------------------------------===//
    1562             : 
    1563             : /// \brief Which attributes cannot be applied to a type.
    1564      161627 : AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
    1565      161627 :   AttrBuilder Incompatible;
    1566             : 
    1567      161627 :   if (!Ty->isIntegerTy())
    1568             :     // Attribute that only apply to integers.
    1569       80308 :     Incompatible.addAttribute(Attribute::SExt)
    1570       80308 :       .addAttribute(Attribute::ZExt);
    1571             : 
    1572      161627 :   if (!Ty->isPointerTy())
    1573             :     // Attribute that only apply to pointers.
    1574       86330 :     Incompatible.addAttribute(Attribute::ByVal)
    1575       86330 :       .addAttribute(Attribute::Nest)
    1576       86330 :       .addAttribute(Attribute::NoAlias)
    1577       86330 :       .addAttribute(Attribute::NoCapture)
    1578       86330 :       .addAttribute(Attribute::NonNull)
    1579       86330 :       .addDereferenceableAttr(1) // the int here is ignored
    1580       86330 :       .addDereferenceableOrNullAttr(1) // the int here is ignored
    1581       86330 :       .addAttribute(Attribute::ReadNone)
    1582       86330 :       .addAttribute(Attribute::ReadOnly)
    1583       86330 :       .addAttribute(Attribute::StructRet)
    1584       86330 :       .addAttribute(Attribute::InAlloca);
    1585             : 
    1586      161627 :   return Incompatible;
    1587             : }
    1588             : 
    1589             : template<typename AttrClass>
    1590     1412888 : static bool isEqual(const Function &Caller, const Function &Callee) {
    1591     4238664 :   return Caller.getFnAttribute(AttrClass::getKind()) ==
    1592     1412888 :          Callee.getFnAttribute(AttrClass::getKind());
    1593             : }
    1594             : 
    1595             : /// \brief Compute the logical AND of the attributes of the caller and the
    1596             : /// callee.
    1597             : ///
    1598             : /// This function sets the caller's attribute to false if the callee's attribute
    1599             : /// is false.
    1600             : template<typename AttrClass>
    1601      351444 : static void setAND(Function &Caller, const Function &Callee) {
    1602      351448 :   if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
    1603           4 :       !AttrClass::isSet(Callee, AttrClass::getKind()))
    1604           2 :     AttrClass::set(Caller, AttrClass::getKind(), false);
    1605      351444 : }
    1606             : 
    1607             : /// \brief Compute the logical OR of the attributes of the caller and the
    1608             : /// callee.
    1609             : ///
    1610             : /// This function sets the caller's attribute to true if the callee's attribute
    1611             : /// is true.
    1612             : template<typename AttrClass>
    1613      263583 : static void setOR(Function &Caller, const Function &Callee) {
    1614      615018 :   if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
    1615      351430 :       AttrClass::isSet(Callee, AttrClass::getKind()))
    1616           4 :     AttrClass::set(Caller, AttrClass::getKind(), true);
    1617      263583 : }
    1618             : 
    1619             : /// \brief If the inlined function had a higher stack protection level than the
    1620             : /// calling function, then bump up the caller's stack protection level.
    1621       87861 : static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
    1622             :   // If upgrading the SSP attribute, clear out the old SSP Attributes first.
    1623             :   // Having multiple SSP attributes doesn't actually hurt, but it adds useless
    1624             :   // clutter to the IR.
    1625      175722 :   AttrBuilder OldSSPAttr;
    1626       87861 :   OldSSPAttr.addAttribute(Attribute::StackProtect)
    1627       87861 :       .addAttribute(Attribute::StackProtectStrong)
    1628       87861 :       .addAttribute(Attribute::StackProtectReq);
    1629             : 
    1630       87861 :   if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
    1631           8 :     Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
    1632             :     Caller.addFnAttr(Attribute::StackProtectReq);
    1633       96141 :   } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
    1634        8288 :              !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
    1635        8286 :     Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
    1636             :     Caller.addFnAttr(Attribute::StackProtectStrong);
    1637       79652 :   } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
    1638       79735 :              !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
    1639          83 :              !Caller.hasFnAttribute(Attribute::StackProtectStrong))
    1640             :     Caller.addFnAttr(Attribute::StackProtect);
    1641       87861 : }
    1642             : 
    1643             : /// \brief If the inlined function required stack probes, then ensure that
    1644             : /// the calling function has those too.
    1645       87861 : static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
    1646      263582 :   if (!Caller.hasFnAttribute("probe-stack") &&
    1647      175720 :       Callee.hasFnAttribute("probe-stack")) {
    1648           3 :     Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
    1649             :   }
    1650       87861 : }
    1651             : 
    1652             : /// \brief If the inlined function defines the size of guard region
    1653             : /// on the stack, then ensure that the calling function defines a guard region
    1654             : /// that is no larger.
    1655             : static void
    1656       87861 : adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
    1657      175722 :   if (Callee.hasFnAttribute("stack-probe-size")) {
    1658             :     uint64_t CalleeStackProbeSize;
    1659           9 :     Callee.getFnAttribute("stack-probe-size")
    1660           6 :           .getValueAsString()
    1661           3 :           .getAsInteger(0, CalleeStackProbeSize);
    1662           6 :     if (Caller.hasFnAttribute("stack-probe-size")) {
    1663             :       uint64_t CallerStackProbeSize;
    1664           6 :       Caller.getFnAttribute("stack-probe-size")
    1665           4 :             .getValueAsString()
    1666           2 :             .getAsInteger(0, CallerStackProbeSize);
    1667           2 :       if (CallerStackProbeSize > CalleeStackProbeSize) {
    1668           3 :         Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
    1669             :       }
    1670             :     } else {
    1671           3 :       Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
    1672             :     }
    1673             :   }
    1674       87861 : }
    1675             : 
    1676             : #define GET_ATTR_COMPAT_FUNC
    1677             : #include "AttributesCompatFunc.inc"
    1678             : 
    1679      353222 : bool AttributeFuncs::areInlineCompatible(const Function &Caller,
    1680             :                                          const Function &Callee) {
    1681      353222 :   return hasCompatibleFnAttrs(Caller, Callee);
    1682             : }
    1683             : 
    1684       87861 : void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
    1685             :                                                 const Function &Callee) {
    1686       87861 :   mergeFnAttrs(Caller, Callee);
    1687       87861 : }

Generated by: LCOV version 1.13