1 //===- Attributes.cpp - Implement AttributesList --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // \file
10 // This file implements the Attribute, AttributeImpl, AttrBuilder,
11 // AttributeListImpl, and AttributeList classes.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/Attributes.h"
16 #include "AttributeImpl.h"
17 #include "LLVMContextImpl.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <climits>
39 #include <cstddef>
40 #include <cstdint>
41 #include <limits>
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   return uint64_t(ElemSizeArg) << 32 |
67          NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
68 }
69 
70 static std::pair<unsigned, Optional<unsigned>>
71 unpackAllocSizeArgs(uint64_t Num) {
72   unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
73   unsigned ElemSizeArg = Num >> 32;
74 
75   Optional<unsigned> NumElemsArg;
76   if (NumElems != AllocSizeNumElemsNotPresent)
77     NumElemsArg = NumElems;
78   return std::make_pair(ElemSizeArg, NumElemsArg);
79 }
80 
81 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
82                          uint64_t Val) {
83   LLVMContextImpl *pImpl = Context.pImpl;
84   FoldingSetNodeID ID;
85   ID.AddInteger(Kind);
86   if (Val) ID.AddInteger(Val);
87 
88   void *InsertPoint;
89   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
90 
91   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     if (!Val)
95       PA = new (pImpl->Alloc) EnumAttributeImpl(Kind);
96     else
97       PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val);
98     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
99   }
100 
101   // Return the Attribute that we found or created.
102   return Attribute(PA);
103 }
104 
105 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
106   LLVMContextImpl *pImpl = Context.pImpl;
107   FoldingSetNodeID ID;
108   ID.AddString(Kind);
109   if (!Val.empty()) ID.AddString(Val);
110 
111   void *InsertPoint;
112   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
113 
114   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     void *Mem =
118         pImpl->Alloc.Allocate(StringAttributeImpl::totalSizeToAlloc(Kind, Val),
119                               alignof(StringAttributeImpl));
120     PA = new (Mem) StringAttributeImpl(Kind, Val);
121     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
122   }
123 
124   // Return the Attribute that we found or created.
125   return Attribute(PA);
126 }
127 
128 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
129                          Type *Ty) {
130   LLVMContextImpl *pImpl = Context.pImpl;
131   FoldingSetNodeID ID;
132   ID.AddInteger(Kind);
133   ID.AddPointer(Ty);
134 
135   void *InsertPoint;
136   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
137 
138   if (!PA) {
139     // If we didn't find any existing attributes of the same shape then create a
140     // new one and insert it.
141     PA = new (pImpl->Alloc) TypeAttributeImpl(Kind, Ty);
142     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
143   }
144 
145   // Return the Attribute that we found or created.
146   return Attribute(PA);
147 }
148 
149 Attribute Attribute::getWithAlignment(LLVMContext &Context, Align A) {
150   assert(A <= llvm::Value::MaximumAlignment && "Alignment too large.");
151   return get(Context, Alignment, A.value());
152 }
153 
154 Attribute Attribute::getWithStackAlignment(LLVMContext &Context, Align A) {
155   assert(A <= 0x100 && "Alignment too large.");
156   return get(Context, StackAlignment, A.value());
157 }
158 
159 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
160                                                 uint64_t Bytes) {
161   assert(Bytes && "Bytes must be non-zero.");
162   return get(Context, Dereferenceable, Bytes);
163 }
164 
165 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
166                                                        uint64_t Bytes) {
167   assert(Bytes && "Bytes must be non-zero.");
168   return get(Context, DereferenceableOrNull, Bytes);
169 }
170 
171 Attribute Attribute::getWithByValType(LLVMContext &Context, Type *Ty) {
172   return get(Context, ByVal, Ty);
173 }
174 
175 Attribute Attribute::getWithStructRetType(LLVMContext &Context, Type *Ty) {
176   return get(Context, StructRet, Ty);
177 }
178 
179 Attribute Attribute::getWithByRefType(LLVMContext &Context, Type *Ty) {
180   return get(Context, ByRef, Ty);
181 }
182 
183 Attribute Attribute::getWithPreallocatedType(LLVMContext &Context, Type *Ty) {
184   return get(Context, Preallocated, Ty);
185 }
186 
187 Attribute
188 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
189                                 const Optional<unsigned> &NumElemsArg) {
190   assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
191          "Invalid allocsize arguments -- given allocsize(0, 0)");
192   return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
193 }
194 
195 Attribute::AttrKind Attribute::getAttrKindFromName(StringRef AttrName) {
196   return StringSwitch<Attribute::AttrKind>(AttrName)
197 #define GET_ATTR_NAMES
198 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)                                \
199   .Case(#DISPLAY_NAME, Attribute::ENUM_NAME)
200 #include "llvm/IR/Attributes.inc"
201       .Default(Attribute::None);
202 }
203 
204 StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) {
205   switch (AttrKind) {
206 #define GET_ATTR_NAMES
207 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)                                \
208   case Attribute::ENUM_NAME:                                                   \
209     return #DISPLAY_NAME;
210 #include "llvm/IR/Attributes.inc"
211   case Attribute::None:
212     return "none";
213   default:
214     llvm_unreachable("invalid Kind");
215   }
216 }
217 
218 bool Attribute::doesAttrKindHaveArgument(Attribute::AttrKind AttrKind) {
219   return AttrKind == Attribute::Alignment ||
220          AttrKind == Attribute::StackAlignment ||
221          AttrKind == Attribute::Dereferenceable ||
222          AttrKind == Attribute::AllocSize ||
223          AttrKind == Attribute::DereferenceableOrNull;
224 }
225 
226 bool Attribute::isExistingAttribute(StringRef Name) {
227   return StringSwitch<bool>(Name)
228 #define GET_ATTR_NAMES
229 #define ATTRIBUTE_ALL(ENUM_NAME, DISPLAY_NAME) .Case(#DISPLAY_NAME, true)
230 #include "llvm/IR/Attributes.inc"
231       .Default(false);
232 }
233 
234 //===----------------------------------------------------------------------===//
235 // Attribute Accessor Methods
236 //===----------------------------------------------------------------------===//
237 
238 bool Attribute::isEnumAttribute() const {
239   return pImpl && pImpl->isEnumAttribute();
240 }
241 
242 bool Attribute::isIntAttribute() const {
243   return pImpl && pImpl->isIntAttribute();
244 }
245 
246 bool Attribute::isStringAttribute() const {
247   return pImpl && pImpl->isStringAttribute();
248 }
249 
250 bool Attribute::isTypeAttribute() const {
251   return pImpl && pImpl->isTypeAttribute();
252 }
253 
254 Attribute::AttrKind Attribute::getKindAsEnum() const {
255   if (!pImpl) return None;
256   assert((isEnumAttribute() || isIntAttribute() || isTypeAttribute()) &&
257          "Invalid attribute type to get the kind as an enum!");
258   return pImpl->getKindAsEnum();
259 }
260 
261 uint64_t Attribute::getValueAsInt() const {
262   if (!pImpl) return 0;
263   assert(isIntAttribute() &&
264          "Expected the attribute to be an integer attribute!");
265   return pImpl->getValueAsInt();
266 }
267 
268 StringRef Attribute::getKindAsString() const {
269   if (!pImpl) return {};
270   assert(isStringAttribute() &&
271          "Invalid attribute type to get the kind as a string!");
272   return pImpl->getKindAsString();
273 }
274 
275 StringRef Attribute::getValueAsString() const {
276   if (!pImpl) return {};
277   assert(isStringAttribute() &&
278          "Invalid attribute type to get the value as a string!");
279   return pImpl->getValueAsString();
280 }
281 
282 Type *Attribute::getValueAsType() const {
283   if (!pImpl) return {};
284   assert(isTypeAttribute() &&
285          "Invalid attribute type to get the value as a type!");
286   return pImpl->getValueAsType();
287 }
288 
289 
290 bool Attribute::hasAttribute(AttrKind Kind) const {
291   return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
292 }
293 
294 bool Attribute::hasAttribute(StringRef Kind) const {
295   if (!isStringAttribute()) return false;
296   return pImpl && pImpl->hasAttribute(Kind);
297 }
298 
299 MaybeAlign Attribute::getAlignment() const {
300   assert(hasAttribute(Attribute::Alignment) &&
301          "Trying to get alignment from non-alignment attribute!");
302   return MaybeAlign(pImpl->getValueAsInt());
303 }
304 
305 MaybeAlign Attribute::getStackAlignment() const {
306   assert(hasAttribute(Attribute::StackAlignment) &&
307          "Trying to get alignment from non-alignment attribute!");
308   return MaybeAlign(pImpl->getValueAsInt());
309 }
310 
311 uint64_t Attribute::getDereferenceableBytes() const {
312   assert(hasAttribute(Attribute::Dereferenceable) &&
313          "Trying to get dereferenceable bytes from "
314          "non-dereferenceable attribute!");
315   return pImpl->getValueAsInt();
316 }
317 
318 uint64_t Attribute::getDereferenceableOrNullBytes() const {
319   assert(hasAttribute(Attribute::DereferenceableOrNull) &&
320          "Trying to get dereferenceable bytes from "
321          "non-dereferenceable attribute!");
322   return pImpl->getValueAsInt();
323 }
324 
325 std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
326   assert(hasAttribute(Attribute::AllocSize) &&
327          "Trying to get allocsize args from non-allocsize attribute");
328   return unpackAllocSizeArgs(pImpl->getValueAsInt());
329 }
330 
331 std::string Attribute::getAsString(bool InAttrGrp) const {
332   if (!pImpl) return {};
333 
334   if (hasAttribute(Attribute::SanitizeAddress))
335     return "sanitize_address";
336   if (hasAttribute(Attribute::SanitizeHWAddress))
337     return "sanitize_hwaddress";
338   if (hasAttribute(Attribute::SanitizeMemTag))
339     return "sanitize_memtag";
340   if (hasAttribute(Attribute::AlwaysInline))
341     return "alwaysinline";
342   if (hasAttribute(Attribute::ArgMemOnly))
343     return "argmemonly";
344   if (hasAttribute(Attribute::Builtin))
345     return "builtin";
346   if (hasAttribute(Attribute::Convergent))
347     return "convergent";
348   if (hasAttribute(Attribute::SwiftError))
349     return "swifterror";
350   if (hasAttribute(Attribute::SwiftSelf))
351     return "swiftself";
352   if (hasAttribute(Attribute::InaccessibleMemOnly))
353     return "inaccessiblememonly";
354   if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
355     return "inaccessiblemem_or_argmemonly";
356   if (hasAttribute(Attribute::InAlloca))
357     return "inalloca";
358   if (hasAttribute(Attribute::InlineHint))
359     return "inlinehint";
360   if (hasAttribute(Attribute::InReg))
361     return "inreg";
362   if (hasAttribute(Attribute::JumpTable))
363     return "jumptable";
364   if (hasAttribute(Attribute::MinSize))
365     return "minsize";
366   if (hasAttribute(Attribute::Naked))
367     return "naked";
368   if (hasAttribute(Attribute::Nest))
369     return "nest";
370   if (hasAttribute(Attribute::NoAlias))
371     return "noalias";
372   if (hasAttribute(Attribute::NoBuiltin))
373     return "nobuiltin";
374   if (hasAttribute(Attribute::NoCallback))
375     return "nocallback";
376   if (hasAttribute(Attribute::NoCapture))
377     return "nocapture";
378   if (hasAttribute(Attribute::NoDuplicate))
379     return "noduplicate";
380   if (hasAttribute(Attribute::NoFree))
381     return "nofree";
382   if (hasAttribute(Attribute::NoImplicitFloat))
383     return "noimplicitfloat";
384   if (hasAttribute(Attribute::NoInline))
385     return "noinline";
386   if (hasAttribute(Attribute::NonLazyBind))
387     return "nonlazybind";
388   if (hasAttribute(Attribute::NoMerge))
389     return "nomerge";
390   if (hasAttribute(Attribute::NonNull))
391     return "nonnull";
392   if (hasAttribute(Attribute::NoRedZone))
393     return "noredzone";
394   if (hasAttribute(Attribute::NoReturn))
395     return "noreturn";
396   if (hasAttribute(Attribute::NoSync))
397     return "nosync";
398   if (hasAttribute(Attribute::NullPointerIsValid))
399     return "null_pointer_is_valid";
400   if (hasAttribute(Attribute::WillReturn))
401     return "willreturn";
402   if (hasAttribute(Attribute::NoCfCheck))
403     return "nocf_check";
404   if (hasAttribute(Attribute::NoRecurse))
405     return "norecurse";
406   if (hasAttribute(Attribute::NoProfile))
407     return "noprofile";
408   if (hasAttribute(Attribute::NoUnwind))
409     return "nounwind";
410   if (hasAttribute(Attribute::OptForFuzzing))
411     return "optforfuzzing";
412   if (hasAttribute(Attribute::OptimizeNone))
413     return "optnone";
414   if (hasAttribute(Attribute::OptimizeForSize))
415     return "optsize";
416   if (hasAttribute(Attribute::ReadNone))
417     return "readnone";
418   if (hasAttribute(Attribute::ReadOnly))
419     return "readonly";
420   if (hasAttribute(Attribute::WriteOnly))
421     return "writeonly";
422   if (hasAttribute(Attribute::Returned))
423     return "returned";
424   if (hasAttribute(Attribute::ReturnsTwice))
425     return "returns_twice";
426   if (hasAttribute(Attribute::SExt))
427     return "signext";
428   if (hasAttribute(Attribute::SpeculativeLoadHardening))
429     return "speculative_load_hardening";
430   if (hasAttribute(Attribute::Speculatable))
431     return "speculatable";
432   if (hasAttribute(Attribute::StackProtect))
433     return "ssp";
434   if (hasAttribute(Attribute::StackProtectReq))
435     return "sspreq";
436   if (hasAttribute(Attribute::StackProtectStrong))
437     return "sspstrong";
438   if (hasAttribute(Attribute::SafeStack))
439     return "safestack";
440   if (hasAttribute(Attribute::ShadowCallStack))
441     return "shadowcallstack";
442   if (hasAttribute(Attribute::StrictFP))
443     return "strictfp";
444   if (hasAttribute(Attribute::SanitizeThread))
445     return "sanitize_thread";
446   if (hasAttribute(Attribute::SanitizeMemory))
447     return "sanitize_memory";
448   if (hasAttribute(Attribute::UWTable))
449     return "uwtable";
450   if (hasAttribute(Attribute::ZExt))
451     return "zeroext";
452   if (hasAttribute(Attribute::Cold))
453     return "cold";
454   if (hasAttribute(Attribute::Hot))
455     return "hot";
456   if (hasAttribute(Attribute::ImmArg))
457     return "immarg";
458   if (hasAttribute(Attribute::NoUndef))
459     return "noundef";
460   if (hasAttribute(Attribute::MustProgress))
461     return "mustprogress";
462 
463   const bool IsByVal = hasAttribute(Attribute::ByVal);
464   if (IsByVal || hasAttribute(Attribute::StructRet)) {
465     std::string Result;
466     Result += IsByVal ? "byval" : "sret";
467     if (Type *Ty = getValueAsType()) {
468       raw_string_ostream OS(Result);
469       Result += '(';
470       Ty->print(OS, false, true);
471       OS.flush();
472       Result += ')';
473     }
474     return Result;
475   }
476 
477   const bool IsByRef = hasAttribute(Attribute::ByRef);
478   if (IsByRef || hasAttribute(Attribute::Preallocated)) {
479     std::string Result = IsByRef ? "byref" : "preallocated";
480     raw_string_ostream OS(Result);
481     Result += '(';
482     getValueAsType()->print(OS, false, true);
483     OS.flush();
484     Result += ')';
485     return Result;
486   }
487 
488   // FIXME: These should be output like this:
489   //
490   //   align=4
491   //   alignstack=8
492   //
493   if (hasAttribute(Attribute::Alignment)) {
494     std::string Result;
495     Result += "align";
496     Result += (InAttrGrp) ? "=" : " ";
497     Result += utostr(getValueAsInt());
498     return Result;
499   }
500 
501   auto AttrWithBytesToString = [&](const char *Name) {
502     std::string Result;
503     Result += Name;
504     if (InAttrGrp) {
505       Result += "=";
506       Result += utostr(getValueAsInt());
507     } else {
508       Result += "(";
509       Result += utostr(getValueAsInt());
510       Result += ")";
511     }
512     return Result;
513   };
514 
515   if (hasAttribute(Attribute::StackAlignment))
516     return AttrWithBytesToString("alignstack");
517 
518   if (hasAttribute(Attribute::Dereferenceable))
519     return AttrWithBytesToString("dereferenceable");
520 
521   if (hasAttribute(Attribute::DereferenceableOrNull))
522     return AttrWithBytesToString("dereferenceable_or_null");
523 
524   if (hasAttribute(Attribute::AllocSize)) {
525     unsigned ElemSize;
526     Optional<unsigned> NumElems;
527     std::tie(ElemSize, NumElems) = getAllocSizeArgs();
528 
529     std::string Result = "allocsize(";
530     Result += utostr(ElemSize);
531     if (NumElems.hasValue()) {
532       Result += ',';
533       Result += utostr(*NumElems);
534     }
535     Result += ')';
536     return Result;
537   }
538 
539   // Convert target-dependent attributes to strings of the form:
540   //
541   //   "kind"
542   //   "kind" = "value"
543   //
544   if (isStringAttribute()) {
545     std::string Result;
546     {
547       raw_string_ostream OS(Result);
548       OS << '"' << getKindAsString() << '"';
549 
550       // Since some attribute strings contain special characters that cannot be
551       // printable, those have to be escaped to make the attribute value
552       // printable as is.  e.g. "\01__gnu_mcount_nc"
553       const auto &AttrVal = pImpl->getValueAsString();
554       if (!AttrVal.empty()) {
555         OS << "=\"";
556         printEscapedString(AttrVal, OS);
557         OS << "\"";
558       }
559     }
560     return Result;
561   }
562 
563   llvm_unreachable("Unknown attribute");
564 }
565 
566 bool Attribute::operator<(Attribute A) const {
567   if (!pImpl && !A.pImpl) return false;
568   if (!pImpl) return true;
569   if (!A.pImpl) return false;
570   return *pImpl < *A.pImpl;
571 }
572 
573 void Attribute::Profile(FoldingSetNodeID &ID) const {
574   ID.AddPointer(pImpl);
575 }
576 
577 //===----------------------------------------------------------------------===//
578 // AttributeImpl Definition
579 //===----------------------------------------------------------------------===//
580 
581 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
582   if (isStringAttribute()) return false;
583   return getKindAsEnum() == A;
584 }
585 
586 bool AttributeImpl::hasAttribute(StringRef Kind) const {
587   if (!isStringAttribute()) return false;
588   return getKindAsString() == Kind;
589 }
590 
591 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
592   assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute());
593   return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
594 }
595 
596 uint64_t AttributeImpl::getValueAsInt() const {
597   assert(isIntAttribute());
598   return static_cast<const IntAttributeImpl *>(this)->getValue();
599 }
600 
601 StringRef AttributeImpl::getKindAsString() const {
602   assert(isStringAttribute());
603   return static_cast<const StringAttributeImpl *>(this)->getStringKind();
604 }
605 
606 StringRef AttributeImpl::getValueAsString() const {
607   assert(isStringAttribute());
608   return static_cast<const StringAttributeImpl *>(this)->getStringValue();
609 }
610 
611 Type *AttributeImpl::getValueAsType() const {
612   assert(isTypeAttribute());
613   return static_cast<const TypeAttributeImpl *>(this)->getTypeValue();
614 }
615 
616 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
617   if (this == &AI)
618     return false;
619   // This sorts the attributes with Attribute::AttrKinds coming first (sorted
620   // relative to their enum value) and then strings.
621   if (isEnumAttribute()) {
622     if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
623     if (AI.isIntAttribute()) return true;
624     if (AI.isStringAttribute()) return true;
625     if (AI.isTypeAttribute()) return true;
626   }
627 
628   if (isTypeAttribute()) {
629     if (AI.isEnumAttribute()) return false;
630     if (AI.isTypeAttribute()) {
631       assert(getKindAsEnum() != AI.getKindAsEnum() &&
632              "Comparison of types would be unstable");
633       return getKindAsEnum() < AI.getKindAsEnum();
634     }
635     if (AI.isIntAttribute()) return true;
636     if (AI.isStringAttribute()) return true;
637   }
638 
639   if (isIntAttribute()) {
640     if (AI.isEnumAttribute()) return false;
641     if (AI.isTypeAttribute()) return false;
642     if (AI.isIntAttribute()) {
643       if (getKindAsEnum() == AI.getKindAsEnum())
644         return getValueAsInt() < AI.getValueAsInt();
645       return getKindAsEnum() < AI.getKindAsEnum();
646     }
647     if (AI.isStringAttribute()) return true;
648   }
649 
650   assert(isStringAttribute());
651   if (AI.isEnumAttribute()) return false;
652   if (AI.isTypeAttribute()) return false;
653   if (AI.isIntAttribute()) return false;
654   if (getKindAsString() == AI.getKindAsString())
655     return getValueAsString() < AI.getValueAsString();
656   return getKindAsString() < AI.getKindAsString();
657 }
658 
659 //===----------------------------------------------------------------------===//
660 // AttributeSet Definition
661 //===----------------------------------------------------------------------===//
662 
663 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
664   return AttributeSet(AttributeSetNode::get(C, B));
665 }
666 
667 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
668   return AttributeSet(AttributeSetNode::get(C, Attrs));
669 }
670 
671 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
672                                         Attribute::AttrKind Kind) const {
673   if (hasAttribute(Kind)) return *this;
674   AttrBuilder B;
675   B.addAttribute(Kind);
676   return addAttributes(C, AttributeSet::get(C, B));
677 }
678 
679 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
680                                         StringRef Value) const {
681   AttrBuilder B;
682   B.addAttribute(Kind, Value);
683   return addAttributes(C, AttributeSet::get(C, B));
684 }
685 
686 AttributeSet AttributeSet::addAttributes(LLVMContext &C,
687                                          const AttributeSet AS) const {
688   if (!hasAttributes())
689     return AS;
690 
691   if (!AS.hasAttributes())
692     return *this;
693 
694   AttrBuilder B(AS);
695   for (const auto &I : *this)
696     B.addAttribute(I);
697 
698  return get(C, B);
699 }
700 
701 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
702                                              Attribute::AttrKind Kind) const {
703   if (!hasAttribute(Kind)) return *this;
704   AttrBuilder B(*this);
705   B.removeAttribute(Kind);
706   return get(C, B);
707 }
708 
709 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
710                                              StringRef Kind) const {
711   if (!hasAttribute(Kind)) return *this;
712   AttrBuilder B(*this);
713   B.removeAttribute(Kind);
714   return get(C, B);
715 }
716 
717 AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
718                                               const AttrBuilder &Attrs) const {
719   AttrBuilder B(*this);
720   B.remove(Attrs);
721   return get(C, B);
722 }
723 
724 unsigned AttributeSet::getNumAttributes() const {
725   return SetNode ? SetNode->getNumAttributes() : 0;
726 }
727 
728 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
729   return SetNode ? SetNode->hasAttribute(Kind) : false;
730 }
731 
732 bool AttributeSet::hasAttribute(StringRef Kind) const {
733   return SetNode ? SetNode->hasAttribute(Kind) : false;
734 }
735 
736 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
737   return SetNode ? SetNode->getAttribute(Kind) : Attribute();
738 }
739 
740 Attribute AttributeSet::getAttribute(StringRef Kind) const {
741   return SetNode ? SetNode->getAttribute(Kind) : Attribute();
742 }
743 
744 MaybeAlign AttributeSet::getAlignment() const {
745   return SetNode ? SetNode->getAlignment() : None;
746 }
747 
748 MaybeAlign AttributeSet::getStackAlignment() const {
749   return SetNode ? SetNode->getStackAlignment() : None;
750 }
751 
752 uint64_t AttributeSet::getDereferenceableBytes() const {
753   return SetNode ? SetNode->getDereferenceableBytes() : 0;
754 }
755 
756 uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
757   return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
758 }
759 
760 Type *AttributeSet::getByRefType() const {
761   return SetNode ? SetNode->getByRefType() : nullptr;
762 }
763 
764 Type *AttributeSet::getByValType() const {
765   return SetNode ? SetNode->getByValType() : nullptr;
766 }
767 
768 Type *AttributeSet::getStructRetType() const {
769   return SetNode ? SetNode->getStructRetType() : nullptr;
770 }
771 
772 Type *AttributeSet::getPreallocatedType() const {
773   return SetNode ? SetNode->getPreallocatedType() : nullptr;
774 }
775 
776 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
777   return SetNode ? SetNode->getAllocSizeArgs()
778                  : std::pair<unsigned, Optional<unsigned>>(0, 0);
779 }
780 
781 std::string AttributeSet::getAsString(bool InAttrGrp) const {
782   return SetNode ? SetNode->getAsString(InAttrGrp) : "";
783 }
784 
785 AttributeSet::iterator AttributeSet::begin() const {
786   return SetNode ? SetNode->begin() : nullptr;
787 }
788 
789 AttributeSet::iterator AttributeSet::end() const {
790   return SetNode ? SetNode->end() : nullptr;
791 }
792 
793 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
794 LLVM_DUMP_METHOD void AttributeSet::dump() const {
795   dbgs() << "AS =\n";
796     dbgs() << "  { ";
797     dbgs() << getAsString(true) << " }\n";
798 }
799 #endif
800 
801 //===----------------------------------------------------------------------===//
802 // AttributeSetNode Definition
803 //===----------------------------------------------------------------------===//
804 
805 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
806     : NumAttrs(Attrs.size()) {
807   // There's memory after the node where we can store the entries in.
808   llvm::copy(Attrs, getTrailingObjects<Attribute>());
809 
810   for (const auto &I : *this) {
811     if (I.isStringAttribute())
812       StringAttrs.insert({ I.getKindAsString(), I });
813     else
814       AvailableAttrs.addAttribute(I.getKindAsEnum());
815   }
816 }
817 
818 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
819                                         ArrayRef<Attribute> Attrs) {
820   SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
821   llvm::sort(SortedAttrs);
822   return getSorted(C, SortedAttrs);
823 }
824 
825 AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C,
826                                               ArrayRef<Attribute> SortedAttrs) {
827   if (SortedAttrs.empty())
828     return nullptr;
829 
830   // Build a key to look up the existing attributes.
831   LLVMContextImpl *pImpl = C.pImpl;
832   FoldingSetNodeID ID;
833 
834   assert(llvm::is_sorted(SortedAttrs) && "Expected sorted attributes!");
835   for (const auto &Attr : SortedAttrs)
836     Attr.Profile(ID);
837 
838   void *InsertPoint;
839   AttributeSetNode *PA =
840     pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
841 
842   // If we didn't find any existing attributes of the same shape then create a
843   // new one and insert it.
844   if (!PA) {
845     // Coallocate entries after the AttributeSetNode itself.
846     void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
847     PA = new (Mem) AttributeSetNode(SortedAttrs);
848     pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
849   }
850 
851   // Return the AttributeSetNode that we found or created.
852   return PA;
853 }
854 
855 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
856   // Add target-independent attributes.
857   SmallVector<Attribute, 8> Attrs;
858   for (Attribute::AttrKind Kind = Attribute::None;
859        Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
860     if (!B.contains(Kind))
861       continue;
862 
863     Attribute Attr;
864     switch (Kind) {
865     case Attribute::ByVal:
866       Attr = Attribute::getWithByValType(C, B.getByValType());
867       break;
868     case Attribute::StructRet:
869       Attr = Attribute::getWithStructRetType(C, B.getStructRetType());
870       break;
871     case Attribute::ByRef:
872       Attr = Attribute::getWithByRefType(C, B.getByRefType());
873       break;
874     case Attribute::Preallocated:
875       Attr = Attribute::getWithPreallocatedType(C, B.getPreallocatedType());
876       break;
877     case Attribute::Alignment:
878       assert(B.getAlignment() && "Alignment must be set");
879       Attr = Attribute::getWithAlignment(C, *B.getAlignment());
880       break;
881     case Attribute::StackAlignment:
882       assert(B.getStackAlignment() && "StackAlignment must be set");
883       Attr = Attribute::getWithStackAlignment(C, *B.getStackAlignment());
884       break;
885     case Attribute::Dereferenceable:
886       Attr = Attribute::getWithDereferenceableBytes(
887           C, B.getDereferenceableBytes());
888       break;
889     case Attribute::DereferenceableOrNull:
890       Attr = Attribute::getWithDereferenceableOrNullBytes(
891           C, B.getDereferenceableOrNullBytes());
892       break;
893     case Attribute::AllocSize: {
894       auto A = B.getAllocSizeArgs();
895       Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
896       break;
897     }
898     default:
899       Attr = Attribute::get(C, Kind);
900     }
901     Attrs.push_back(Attr);
902   }
903 
904   // Add target-dependent (string) attributes.
905   for (const auto &TDA : B.td_attrs())
906     Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
907 
908   return getSorted(C, Attrs);
909 }
910 
911 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
912   return StringAttrs.count(Kind);
913 }
914 
915 Optional<Attribute>
916 AttributeSetNode::findEnumAttribute(Attribute::AttrKind Kind) const {
917   // Do a quick presence check.
918   if (!hasAttribute(Kind))
919     return None;
920 
921   // Attributes in a set are sorted by enum value, followed by string
922   // attributes. Binary search the one we want.
923   const Attribute *I =
924       std::lower_bound(begin(), end() - StringAttrs.size(), Kind,
925                        [](Attribute A, Attribute::AttrKind Kind) {
926                          return A.getKindAsEnum() < Kind;
927                        });
928   assert(I != end() && I->hasAttribute(Kind) && "Presence check failed?");
929   return *I;
930 }
931 
932 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
933   if (auto A = findEnumAttribute(Kind))
934     return *A;
935   return {};
936 }
937 
938 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
939   return StringAttrs.lookup(Kind);
940 }
941 
942 MaybeAlign AttributeSetNode::getAlignment() const {
943   if (auto A = findEnumAttribute(Attribute::Alignment))
944     return A->getAlignment();
945   return None;
946 }
947 
948 MaybeAlign AttributeSetNode::getStackAlignment() const {
949   if (auto A = findEnumAttribute(Attribute::StackAlignment))
950     return A->getStackAlignment();
951   return None;
952 }
953 
954 Type *AttributeSetNode::getByValType() const {
955   if (auto A = findEnumAttribute(Attribute::ByVal))
956     return A->getValueAsType();
957   return nullptr;
958 }
959 
960 Type *AttributeSetNode::getStructRetType() const {
961   if (auto A = findEnumAttribute(Attribute::StructRet))
962     return A->getValueAsType();
963   return nullptr;
964 }
965 
966 Type *AttributeSetNode::getByRefType() const {
967   if (auto A = findEnumAttribute(Attribute::ByRef))
968     return A->getValueAsType();
969   return nullptr;
970 }
971 
972 Type *AttributeSetNode::getPreallocatedType() const {
973   if (auto A = findEnumAttribute(Attribute::Preallocated))
974     return A->getValueAsType();
975   return nullptr;
976 }
977 
978 uint64_t AttributeSetNode::getDereferenceableBytes() const {
979   if (auto A = findEnumAttribute(Attribute::Dereferenceable))
980     return A->getDereferenceableBytes();
981   return 0;
982 }
983 
984 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
985   if (auto A = findEnumAttribute(Attribute::DereferenceableOrNull))
986     return A->getDereferenceableOrNullBytes();
987   return 0;
988 }
989 
990 std::pair<unsigned, Optional<unsigned>>
991 AttributeSetNode::getAllocSizeArgs() const {
992   if (auto A = findEnumAttribute(Attribute::AllocSize))
993     return A->getAllocSizeArgs();
994   return std::make_pair(0, 0);
995 }
996 
997 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
998   std::string Str;
999   for (iterator I = begin(), E = end(); I != E; ++I) {
1000     if (I != begin())
1001       Str += ' ';
1002     Str += I->getAsString(InAttrGrp);
1003   }
1004   return Str;
1005 }
1006 
1007 //===----------------------------------------------------------------------===//
1008 // AttributeListImpl Definition
1009 //===----------------------------------------------------------------------===//
1010 
1011 /// Map from AttributeList index to the internal array index. Adding one happens
1012 /// to work, because -1 wraps around to 0.
1013 static unsigned attrIdxToArrayIdx(unsigned Index) {
1014   return Index + 1;
1015 }
1016 
1017 AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets)
1018     : NumAttrSets(Sets.size()) {
1019   assert(!Sets.empty() && "pointless AttributeListImpl");
1020 
1021   // There's memory after the node where we can store the entries in.
1022   llvm::copy(Sets, getTrailingObjects<AttributeSet>());
1023 
1024   // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs
1025   // summary bitsets.
1026   for (const auto &I : Sets[attrIdxToArrayIdx(AttributeList::FunctionIndex)])
1027     if (!I.isStringAttribute())
1028       AvailableFunctionAttrs.addAttribute(I.getKindAsEnum());
1029 
1030   for (const auto &Set : Sets)
1031     for (const auto &I : Set)
1032       if (!I.isStringAttribute())
1033         AvailableSomewhereAttrs.addAttribute(I.getKindAsEnum());
1034 }
1035 
1036 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
1037   Profile(ID, makeArrayRef(begin(), end()));
1038 }
1039 
1040 void AttributeListImpl::Profile(FoldingSetNodeID &ID,
1041                                 ArrayRef<AttributeSet> Sets) {
1042   for (const auto &Set : Sets)
1043     ID.AddPointer(Set.SetNode);
1044 }
1045 
1046 bool AttributeListImpl::hasAttrSomewhere(Attribute::AttrKind Kind,
1047                                         unsigned *Index) const {
1048   if (!AvailableSomewhereAttrs.hasAttribute(Kind))
1049     return false;
1050 
1051   if (Index) {
1052     for (unsigned I = 0, E = NumAttrSets; I != E; ++I) {
1053       if (begin()[I].hasAttribute(Kind)) {
1054         *Index = I - 1;
1055         break;
1056       }
1057     }
1058   }
1059 
1060   return true;
1061 }
1062 
1063 
1064 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1065 LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
1066   AttributeList(const_cast<AttributeListImpl *>(this)).dump();
1067 }
1068 #endif
1069 
1070 //===----------------------------------------------------------------------===//
1071 // AttributeList Construction and Mutation Methods
1072 //===----------------------------------------------------------------------===//
1073 
1074 AttributeList AttributeList::getImpl(LLVMContext &C,
1075                                      ArrayRef<AttributeSet> AttrSets) {
1076   assert(!AttrSets.empty() && "pointless AttributeListImpl");
1077 
1078   LLVMContextImpl *pImpl = C.pImpl;
1079   FoldingSetNodeID ID;
1080   AttributeListImpl::Profile(ID, AttrSets);
1081 
1082   void *InsertPoint;
1083   AttributeListImpl *PA =
1084       pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
1085 
1086   // If we didn't find any existing attributes of the same shape then
1087   // create a new one and insert it.
1088   if (!PA) {
1089     // Coallocate entries after the AttributeListImpl itself.
1090     void *Mem = pImpl->Alloc.Allocate(
1091         AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()),
1092         alignof(AttributeListImpl));
1093     PA = new (Mem) AttributeListImpl(AttrSets);
1094     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
1095   }
1096 
1097   // Return the AttributesList that we found or created.
1098   return AttributeList(PA);
1099 }
1100 
1101 AttributeList
1102 AttributeList::get(LLVMContext &C,
1103                    ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
1104   // If there are no attributes then return a null AttributesList pointer.
1105   if (Attrs.empty())
1106     return {};
1107 
1108   assert(llvm::is_sorted(Attrs,
1109                          [](const std::pair<unsigned, Attribute> &LHS,
1110                             const std::pair<unsigned, Attribute> &RHS) {
1111                            return LHS.first < RHS.first;
1112                          }) &&
1113          "Misordered Attributes list!");
1114   assert(llvm::all_of(Attrs,
1115                       [](const std::pair<unsigned, Attribute> &Pair) {
1116                         return Pair.second.isValid();
1117                       }) &&
1118          "Pointless attribute!");
1119 
1120   // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
1121   // list.
1122   SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
1123   for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
1124          E = Attrs.end(); I != E; ) {
1125     unsigned Index = I->first;
1126     SmallVector<Attribute, 4> AttrVec;
1127     while (I != E && I->first == Index) {
1128       AttrVec.push_back(I->second);
1129       ++I;
1130     }
1131 
1132     AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
1133   }
1134 
1135   return get(C, AttrPairVec);
1136 }
1137 
1138 AttributeList
1139 AttributeList::get(LLVMContext &C,
1140                    ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
1141   // If there are no attributes then return a null AttributesList pointer.
1142   if (Attrs.empty())
1143     return {};
1144 
1145   assert(llvm::is_sorted(Attrs,
1146                          [](const std::pair<unsigned, AttributeSet> &LHS,
1147                             const std::pair<unsigned, AttributeSet> &RHS) {
1148                            return LHS.first < RHS.first;
1149                          }) &&
1150          "Misordered Attributes list!");
1151   assert(llvm::none_of(Attrs,
1152                        [](const std::pair<unsigned, AttributeSet> &Pair) {
1153                          return !Pair.second.hasAttributes();
1154                        }) &&
1155          "Pointless attribute!");
1156 
1157   unsigned MaxIndex = Attrs.back().first;
1158   // If the MaxIndex is FunctionIndex and there are other indices in front
1159   // of it, we need to use the largest of those to get the right size.
1160   if (MaxIndex == FunctionIndex && Attrs.size() > 1)
1161     MaxIndex = Attrs[Attrs.size() - 2].first;
1162 
1163   SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
1164   for (const auto &Pair : Attrs)
1165     AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
1166 
1167   return getImpl(C, AttrVec);
1168 }
1169 
1170 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
1171                                  AttributeSet RetAttrs,
1172                                  ArrayRef<AttributeSet> ArgAttrs) {
1173   // Scan from the end to find the last argument with attributes.  Most
1174   // arguments don't have attributes, so it's nice if we can have fewer unique
1175   // AttributeListImpls by dropping empty attribute sets at the end of the list.
1176   unsigned NumSets = 0;
1177   for (size_t I = ArgAttrs.size(); I != 0; --I) {
1178     if (ArgAttrs[I - 1].hasAttributes()) {
1179       NumSets = I + 2;
1180       break;
1181     }
1182   }
1183   if (NumSets == 0) {
1184     // Check function and return attributes if we didn't have argument
1185     // attributes.
1186     if (RetAttrs.hasAttributes())
1187       NumSets = 2;
1188     else if (FnAttrs.hasAttributes())
1189       NumSets = 1;
1190   }
1191 
1192   // If all attribute sets were empty, we can use the empty attribute list.
1193   if (NumSets == 0)
1194     return {};
1195 
1196   SmallVector<AttributeSet, 8> AttrSets;
1197   AttrSets.reserve(NumSets);
1198   // If we have any attributes, we always have function attributes.
1199   AttrSets.push_back(FnAttrs);
1200   if (NumSets > 1)
1201     AttrSets.push_back(RetAttrs);
1202   if (NumSets > 2) {
1203     // Drop the empty argument attribute sets at the end.
1204     ArgAttrs = ArgAttrs.take_front(NumSets - 2);
1205     llvm::append_range(AttrSets, ArgAttrs);
1206   }
1207 
1208   return getImpl(C, AttrSets);
1209 }
1210 
1211 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1212                                  const AttrBuilder &B) {
1213   if (!B.hasAttributes())
1214     return {};
1215   Index = attrIdxToArrayIdx(Index);
1216   SmallVector<AttributeSet, 8> AttrSets(Index + 1);
1217   AttrSets[Index] = AttributeSet::get(C, B);
1218   return getImpl(C, AttrSets);
1219 }
1220 
1221 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1222                                  ArrayRef<Attribute::AttrKind> Kinds) {
1223   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1224   for (const auto K : Kinds)
1225     Attrs.emplace_back(Index, Attribute::get(C, K));
1226   return get(C, Attrs);
1227 }
1228 
1229 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1230                                  ArrayRef<Attribute::AttrKind> Kinds,
1231                                  ArrayRef<uint64_t> Values) {
1232   assert(Kinds.size() == Values.size() && "Mismatched attribute values.");
1233   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1234   auto VI = Values.begin();
1235   for (const auto K : Kinds)
1236     Attrs.emplace_back(Index, Attribute::get(C, K, *VI++));
1237   return get(C, Attrs);
1238 }
1239 
1240 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1241                                  ArrayRef<StringRef> Kinds) {
1242   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1243   for (const auto &K : Kinds)
1244     Attrs.emplace_back(Index, Attribute::get(C, K));
1245   return get(C, Attrs);
1246 }
1247 
1248 AttributeList AttributeList::get(LLVMContext &C,
1249                                  ArrayRef<AttributeList> Attrs) {
1250   if (Attrs.empty())
1251     return {};
1252   if (Attrs.size() == 1)
1253     return Attrs[0];
1254 
1255   unsigned MaxSize = 0;
1256   for (const auto &List : Attrs)
1257     MaxSize = std::max(MaxSize, List.getNumAttrSets());
1258 
1259   // If every list was empty, there is no point in merging the lists.
1260   if (MaxSize == 0)
1261     return {};
1262 
1263   SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1264   for (unsigned I = 0; I < MaxSize; ++I) {
1265     AttrBuilder CurBuilder;
1266     for (const auto &List : Attrs)
1267       CurBuilder.merge(List.getAttributes(I - 1));
1268     NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
1269   }
1270 
1271   return getImpl(C, NewAttrSets);
1272 }
1273 
1274 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1275                                           Attribute::AttrKind Kind) const {
1276   if (hasAttribute(Index, Kind)) return *this;
1277   AttributeSet Attrs = getAttributes(Index);
1278   // TODO: Insert at correct position and avoid sort.
1279   SmallVector<Attribute, 8> NewAttrs(Attrs.begin(), Attrs.end());
1280   NewAttrs.push_back(Attribute::get(C, Kind));
1281   return setAttributes(C, Index, AttributeSet::get(C, NewAttrs));
1282 }
1283 
1284 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1285                                           StringRef Kind,
1286                                           StringRef Value) const {
1287   AttrBuilder B;
1288   B.addAttribute(Kind, Value);
1289   return addAttributes(C, Index, B);
1290 }
1291 
1292 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1293                                           Attribute A) const {
1294   AttrBuilder B;
1295   B.addAttribute(A);
1296   return addAttributes(C, Index, B);
1297 }
1298 
1299 AttributeList AttributeList::setAttributes(LLVMContext &C, unsigned Index,
1300                                            AttributeSet Attrs) const {
1301   Index = attrIdxToArrayIdx(Index);
1302   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1303   if (Index >= AttrSets.size())
1304     AttrSets.resize(Index + 1);
1305   AttrSets[Index] = Attrs;
1306   return AttributeList::getImpl(C, AttrSets);
1307 }
1308 
1309 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
1310                                            const AttrBuilder &B) const {
1311   if (!B.hasAttributes())
1312     return *this;
1313 
1314   if (!pImpl)
1315     return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
1316 
1317 #ifndef NDEBUG
1318   // FIXME it is not obvious how this should work for alignment. For now, say
1319   // we can't change a known alignment.
1320   const MaybeAlign OldAlign = getAttributes(Index).getAlignment();
1321   const MaybeAlign NewAlign = B.getAlignment();
1322   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1323          "Attempt to change alignment!");
1324 #endif
1325 
1326   AttrBuilder Merged(getAttributes(Index));
1327   Merged.merge(B);
1328   return setAttributes(C, Index, AttributeSet::get(C, Merged));
1329 }
1330 
1331 AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1332                                                ArrayRef<unsigned> ArgNos,
1333                                                Attribute A) const {
1334   assert(llvm::is_sorted(ArgNos));
1335 
1336   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1337   unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1338   if (MaxIndex >= AttrSets.size())
1339     AttrSets.resize(MaxIndex + 1);
1340 
1341   for (unsigned ArgNo : ArgNos) {
1342     unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1343     AttrBuilder B(AttrSets[Index]);
1344     B.addAttribute(A);
1345     AttrSets[Index] = AttributeSet::get(C, B);
1346   }
1347 
1348   return getImpl(C, AttrSets);
1349 }
1350 
1351 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1352                                              Attribute::AttrKind Kind) const {
1353   if (!hasAttribute(Index, Kind)) return *this;
1354 
1355   Index = attrIdxToArrayIdx(Index);
1356   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1357   assert(Index < AttrSets.size());
1358 
1359   AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1360 
1361   return getImpl(C, AttrSets);
1362 }
1363 
1364 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1365                                              StringRef Kind) const {
1366   if (!hasAttribute(Index, Kind)) return *this;
1367 
1368   Index = attrIdxToArrayIdx(Index);
1369   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1370   assert(Index < AttrSets.size());
1371 
1372   AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1373 
1374   return getImpl(C, AttrSets);
1375 }
1376 
1377 AttributeList
1378 AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1379                                 const AttrBuilder &AttrsToRemove) const {
1380   if (!pImpl)
1381     return {};
1382 
1383   Index = attrIdxToArrayIdx(Index);
1384   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1385   if (Index >= AttrSets.size())
1386     AttrSets.resize(Index + 1);
1387 
1388   AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
1389 
1390   return getImpl(C, AttrSets);
1391 }
1392 
1393 AttributeList AttributeList::removeAttributes(LLVMContext &C,
1394                                               unsigned WithoutIndex) const {
1395   if (!pImpl)
1396     return {};
1397   WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1398   if (WithoutIndex >= getNumAttrSets())
1399     return *this;
1400   SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1401   AttrSets[WithoutIndex] = AttributeSet();
1402   return getImpl(C, AttrSets);
1403 }
1404 
1405 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1406                                                     unsigned Index,
1407                                                     uint64_t Bytes) const {
1408   AttrBuilder B;
1409   B.addDereferenceableAttr(Bytes);
1410   return addAttributes(C, Index, B);
1411 }
1412 
1413 AttributeList
1414 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1415                                             uint64_t Bytes) const {
1416   AttrBuilder B;
1417   B.addDereferenceableOrNullAttr(Bytes);
1418   return addAttributes(C, Index, B);
1419 }
1420 
1421 AttributeList
1422 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1423                                 unsigned ElemSizeArg,
1424                                 const Optional<unsigned> &NumElemsArg) {
1425   AttrBuilder B;
1426   B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1427   return addAttributes(C, Index, B);
1428 }
1429 
1430 //===----------------------------------------------------------------------===//
1431 // AttributeList Accessor Methods
1432 //===----------------------------------------------------------------------===//
1433 
1434 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1435   return getAttributes(ArgNo + FirstArgIndex);
1436 }
1437 
1438 AttributeSet AttributeList::getRetAttributes() const {
1439   return getAttributes(ReturnIndex);
1440 }
1441 
1442 AttributeSet AttributeList::getFnAttributes() const {
1443   return getAttributes(FunctionIndex);
1444 }
1445 
1446 bool AttributeList::hasAttribute(unsigned Index,
1447                                  Attribute::AttrKind Kind) const {
1448   return getAttributes(Index).hasAttribute(Kind);
1449 }
1450 
1451 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
1452   return getAttributes(Index).hasAttribute(Kind);
1453 }
1454 
1455 bool AttributeList::hasAttributes(unsigned Index) const {
1456   return getAttributes(Index).hasAttributes();
1457 }
1458 
1459 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
1460   return pImpl && pImpl->hasFnAttribute(Kind);
1461 }
1462 
1463 bool AttributeList::hasFnAttribute(StringRef Kind) const {
1464   return hasAttribute(AttributeList::FunctionIndex, Kind);
1465 }
1466 
1467 bool AttributeList::hasParamAttribute(unsigned ArgNo,
1468                                       Attribute::AttrKind Kind) const {
1469   return hasAttribute(ArgNo + FirstArgIndex, Kind);
1470 }
1471 
1472 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1473                                      unsigned *Index) const {
1474   return pImpl && pImpl->hasAttrSomewhere(Attr, Index);
1475 }
1476 
1477 Attribute AttributeList::getAttribute(unsigned Index,
1478                                       Attribute::AttrKind Kind) const {
1479   return getAttributes(Index).getAttribute(Kind);
1480 }
1481 
1482 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
1483   return getAttributes(Index).getAttribute(Kind);
1484 }
1485 
1486 MaybeAlign AttributeList::getRetAlignment() const {
1487   return getAttributes(ReturnIndex).getAlignment();
1488 }
1489 
1490 MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const {
1491   return getAttributes(ArgNo + FirstArgIndex).getAlignment();
1492 }
1493 
1494 Type *AttributeList::getParamByValType(unsigned Index) const {
1495   return getAttributes(Index+FirstArgIndex).getByValType();
1496 }
1497 
1498 Type *AttributeList::getParamStructRetType(unsigned Index) const {
1499   return getAttributes(Index + FirstArgIndex).getStructRetType();
1500 }
1501 
1502 Type *AttributeList::getParamByRefType(unsigned Index) const {
1503   return getAttributes(Index + FirstArgIndex).getByRefType();
1504 }
1505 
1506 Type *AttributeList::getParamPreallocatedType(unsigned Index) const {
1507   return getAttributes(Index + FirstArgIndex).getPreallocatedType();
1508 }
1509 
1510 MaybeAlign AttributeList::getStackAlignment(unsigned Index) const {
1511   return getAttributes(Index).getStackAlignment();
1512 }
1513 
1514 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
1515   return getAttributes(Index).getDereferenceableBytes();
1516 }
1517 
1518 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
1519   return getAttributes(Index).getDereferenceableOrNullBytes();
1520 }
1521 
1522 std::pair<unsigned, Optional<unsigned>>
1523 AttributeList::getAllocSizeArgs(unsigned Index) const {
1524   return getAttributes(Index).getAllocSizeArgs();
1525 }
1526 
1527 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
1528   return getAttributes(Index).getAsString(InAttrGrp);
1529 }
1530 
1531 AttributeSet AttributeList::getAttributes(unsigned Index) const {
1532   Index = attrIdxToArrayIdx(Index);
1533   if (!pImpl || Index >= getNumAttrSets())
1534     return {};
1535   return pImpl->begin()[Index];
1536 }
1537 
1538 AttributeList::iterator AttributeList::begin() const {
1539   return pImpl ? pImpl->begin() : nullptr;
1540 }
1541 
1542 AttributeList::iterator AttributeList::end() const {
1543   return pImpl ? pImpl->end() : nullptr;
1544 }
1545 
1546 //===----------------------------------------------------------------------===//
1547 // AttributeList Introspection Methods
1548 //===----------------------------------------------------------------------===//
1549 
1550 unsigned AttributeList::getNumAttrSets() const {
1551   return pImpl ? pImpl->NumAttrSets : 0;
1552 }
1553 
1554 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1555 LLVM_DUMP_METHOD void AttributeList::dump() const {
1556   dbgs() << "PAL[\n";
1557 
1558   for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1559     if (getAttributes(i).hasAttributes())
1560       dbgs() << "  { " << i << " => " << getAsString(i) << " }\n";
1561   }
1562 
1563   dbgs() << "]\n";
1564 }
1565 #endif
1566 
1567 //===----------------------------------------------------------------------===//
1568 // AttrBuilder Method Implementations
1569 //===----------------------------------------------------------------------===//
1570 
1571 // FIXME: Remove this ctor, use AttributeSet.
1572 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1573   AttributeSet AS = AL.getAttributes(Index);
1574   for (const auto &A : AS)
1575     addAttribute(A);
1576 }
1577 
1578 AttrBuilder::AttrBuilder(AttributeSet AS) {
1579   for (const auto &A : AS)
1580     addAttribute(A);
1581 }
1582 
1583 void AttrBuilder::clear() {
1584   Attrs.reset();
1585   TargetDepAttrs.clear();
1586   Alignment.reset();
1587   StackAlignment.reset();
1588   DerefBytes = DerefOrNullBytes = 0;
1589   AllocSizeArgs = 0;
1590   ByValType = nullptr;
1591   StructRetType = nullptr;
1592   ByRefType = nullptr;
1593   PreallocatedType = nullptr;
1594 }
1595 
1596 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1597   if (Attr.isStringAttribute()) {
1598     addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1599     return *this;
1600   }
1601 
1602   Attribute::AttrKind Kind = Attr.getKindAsEnum();
1603   Attrs[Kind] = true;
1604 
1605   if (Kind == Attribute::Alignment)
1606     Alignment = Attr.getAlignment();
1607   else if (Kind == Attribute::StackAlignment)
1608     StackAlignment = Attr.getStackAlignment();
1609   else if (Kind == Attribute::ByVal)
1610     ByValType = Attr.getValueAsType();
1611   else if (Kind == Attribute::StructRet)
1612     StructRetType = Attr.getValueAsType();
1613   else if (Kind == Attribute::ByRef)
1614     ByRefType = Attr.getValueAsType();
1615   else if (Kind == Attribute::Preallocated)
1616     PreallocatedType = Attr.getValueAsType();
1617   else if (Kind == Attribute::Dereferenceable)
1618     DerefBytes = Attr.getDereferenceableBytes();
1619   else if (Kind == Attribute::DereferenceableOrNull)
1620     DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1621   else if (Kind == Attribute::AllocSize)
1622     AllocSizeArgs = Attr.getValueAsInt();
1623   return *this;
1624 }
1625 
1626 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1627   TargetDepAttrs[std::string(A)] = std::string(V);
1628   return *this;
1629 }
1630 
1631 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1632   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1633   Attrs[Val] = false;
1634 
1635   if (Val == Attribute::Alignment)
1636     Alignment.reset();
1637   else if (Val == Attribute::StackAlignment)
1638     StackAlignment.reset();
1639   else if (Val == Attribute::ByVal)
1640     ByValType = nullptr;
1641   else if (Val == Attribute::StructRet)
1642     StructRetType = nullptr;
1643   else if (Val == Attribute::ByRef)
1644     ByRefType = nullptr;
1645   else if (Val == Attribute::Preallocated)
1646     PreallocatedType = nullptr;
1647   else if (Val == Attribute::Dereferenceable)
1648     DerefBytes = 0;
1649   else if (Val == Attribute::DereferenceableOrNull)
1650     DerefOrNullBytes = 0;
1651   else if (Val == Attribute::AllocSize)
1652     AllocSizeArgs = 0;
1653 
1654   return *this;
1655 }
1656 
1657 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
1658   remove(A.getAttributes(Index));
1659   return *this;
1660 }
1661 
1662 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1663   auto I = TargetDepAttrs.find(A);
1664   if (I != TargetDepAttrs.end())
1665     TargetDepAttrs.erase(I);
1666   return *this;
1667 }
1668 
1669 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1670   return unpackAllocSizeArgs(AllocSizeArgs);
1671 }
1672 
1673 AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {
1674   if (!Align)
1675     return *this;
1676 
1677   assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large.");
1678 
1679   Attrs[Attribute::Alignment] = true;
1680   Alignment = Align;
1681   return *this;
1682 }
1683 
1684 AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) {
1685   // Default alignment, allow the target to define how to align it.
1686   if (!Align)
1687     return *this;
1688 
1689   assert(*Align <= 0x100 && "Alignment too large.");
1690 
1691   Attrs[Attribute::StackAlignment] = true;
1692   StackAlignment = Align;
1693   return *this;
1694 }
1695 
1696 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1697   if (Bytes == 0) return *this;
1698 
1699   Attrs[Attribute::Dereferenceable] = true;
1700   DerefBytes = Bytes;
1701   return *this;
1702 }
1703 
1704 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1705   if (Bytes == 0)
1706     return *this;
1707 
1708   Attrs[Attribute::DereferenceableOrNull] = true;
1709   DerefOrNullBytes = Bytes;
1710   return *this;
1711 }
1712 
1713 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1714                                            const Optional<unsigned> &NumElems) {
1715   return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1716 }
1717 
1718 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1719   // (0, 0) is our "not present" value, so we need to check for it here.
1720   assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1721 
1722   Attrs[Attribute::AllocSize] = true;
1723   // Reuse existing machinery to store this as a single 64-bit integer so we can
1724   // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1725   AllocSizeArgs = RawArgs;
1726   return *this;
1727 }
1728 
1729 AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) {
1730   Attrs[Attribute::ByVal] = true;
1731   ByValType = Ty;
1732   return *this;
1733 }
1734 
1735 AttrBuilder &AttrBuilder::addStructRetAttr(Type *Ty) {
1736   Attrs[Attribute::StructRet] = true;
1737   StructRetType = Ty;
1738   return *this;
1739 }
1740 
1741 AttrBuilder &AttrBuilder::addByRefAttr(Type *Ty) {
1742   Attrs[Attribute::ByRef] = true;
1743   ByRefType = Ty;
1744   return *this;
1745 }
1746 
1747 AttrBuilder &AttrBuilder::addPreallocatedAttr(Type *Ty) {
1748   Attrs[Attribute::Preallocated] = true;
1749   PreallocatedType = Ty;
1750   return *this;
1751 }
1752 
1753 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1754   // FIXME: What if both have alignments, but they don't match?!
1755   if (!Alignment)
1756     Alignment = B.Alignment;
1757 
1758   if (!StackAlignment)
1759     StackAlignment = B.StackAlignment;
1760 
1761   if (!DerefBytes)
1762     DerefBytes = B.DerefBytes;
1763 
1764   if (!DerefOrNullBytes)
1765     DerefOrNullBytes = B.DerefOrNullBytes;
1766 
1767   if (!AllocSizeArgs)
1768     AllocSizeArgs = B.AllocSizeArgs;
1769 
1770   if (!ByValType)
1771     ByValType = B.ByValType;
1772 
1773   if (!StructRetType)
1774     StructRetType = B.StructRetType;
1775 
1776   if (!ByRefType)
1777     ByRefType = B.ByRefType;
1778 
1779   if (!PreallocatedType)
1780     PreallocatedType = B.PreallocatedType;
1781 
1782   Attrs |= B.Attrs;
1783 
1784   for (const auto &I : B.td_attrs())
1785     TargetDepAttrs[I.first] = I.second;
1786 
1787   return *this;
1788 }
1789 
1790 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1791   // FIXME: What if both have alignments, but they don't match?!
1792   if (B.Alignment)
1793     Alignment.reset();
1794 
1795   if (B.StackAlignment)
1796     StackAlignment.reset();
1797 
1798   if (B.DerefBytes)
1799     DerefBytes = 0;
1800 
1801   if (B.DerefOrNullBytes)
1802     DerefOrNullBytes = 0;
1803 
1804   if (B.AllocSizeArgs)
1805     AllocSizeArgs = 0;
1806 
1807   if (B.ByValType)
1808     ByValType = nullptr;
1809 
1810   if (B.StructRetType)
1811     StructRetType = nullptr;
1812 
1813   if (B.ByRefType)
1814     ByRefType = nullptr;
1815 
1816   if (B.PreallocatedType)
1817     PreallocatedType = nullptr;
1818 
1819   Attrs &= ~B.Attrs;
1820 
1821   for (const auto &I : B.td_attrs())
1822     TargetDepAttrs.erase(I.first);
1823 
1824   return *this;
1825 }
1826 
1827 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1828   // First check if any of the target independent attributes overlap.
1829   if ((Attrs & B.Attrs).any())
1830     return true;
1831 
1832   // Then check if any target dependent ones do.
1833   for (const auto &I : td_attrs())
1834     if (B.contains(I.first))
1835       return true;
1836 
1837   return false;
1838 }
1839 
1840 bool AttrBuilder::contains(StringRef A) const {
1841   return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1842 }
1843 
1844 bool AttrBuilder::hasAttributes() const {
1845   return !Attrs.none() || !TargetDepAttrs.empty();
1846 }
1847 
1848 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1849   AttributeSet AS = AL.getAttributes(Index);
1850 
1851   for (const auto &Attr : AS) {
1852     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1853       if (contains(Attr.getKindAsEnum()))
1854         return true;
1855     } else {
1856       assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1857       return contains(Attr.getKindAsString());
1858     }
1859   }
1860 
1861   return false;
1862 }
1863 
1864 bool AttrBuilder::hasAlignmentAttr() const {
1865   return Alignment != 0;
1866 }
1867 
1868 bool AttrBuilder::operator==(const AttrBuilder &B) const {
1869   if (Attrs != B.Attrs)
1870     return false;
1871 
1872   for (td_const_iterator I = TargetDepAttrs.begin(),
1873          E = TargetDepAttrs.end(); I != E; ++I)
1874     if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1875       return false;
1876 
1877   return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1878          DerefBytes == B.DerefBytes && ByValType == B.ByValType &&
1879          StructRetType == B.StructRetType && ByRefType == B.ByRefType &&
1880          PreallocatedType == B.PreallocatedType;
1881 }
1882 
1883 //===----------------------------------------------------------------------===//
1884 // AttributeFuncs Function Defintions
1885 //===----------------------------------------------------------------------===//
1886 
1887 /// Which attributes cannot be applied to a type.
1888 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1889   AttrBuilder Incompatible;
1890 
1891   if (!Ty->isIntegerTy())
1892     // Attribute that only apply to integers.
1893     Incompatible.addAttribute(Attribute::SExt)
1894       .addAttribute(Attribute::ZExt);
1895 
1896   if (!Ty->isPointerTy())
1897     // Attribute that only apply to pointers.
1898     Incompatible.addAttribute(Attribute::Nest)
1899         .addAttribute(Attribute::NoAlias)
1900         .addAttribute(Attribute::NoCapture)
1901         .addAttribute(Attribute::NonNull)
1902         .addAlignmentAttr(1)             // the int here is ignored
1903         .addDereferenceableAttr(1)       // the int here is ignored
1904         .addDereferenceableOrNullAttr(1) // the int here is ignored
1905         .addAttribute(Attribute::ReadNone)
1906         .addAttribute(Attribute::ReadOnly)
1907         .addAttribute(Attribute::InAlloca)
1908         .addPreallocatedAttr(Ty)
1909         .addByValAttr(Ty)
1910         .addStructRetAttr(Ty)
1911         .addByRefAttr(Ty);
1912 
1913   // Some attributes can apply to all "values" but there are no `void` values.
1914   if (Ty->isVoidTy())
1915     Incompatible.addAttribute(Attribute::NoUndef);
1916 
1917   return Incompatible;
1918 }
1919 
1920 template<typename AttrClass>
1921 static bool isEqual(const Function &Caller, const Function &Callee) {
1922   return Caller.getFnAttribute(AttrClass::getKind()) ==
1923          Callee.getFnAttribute(AttrClass::getKind());
1924 }
1925 
1926 /// Compute the logical AND of the attributes of the caller and the
1927 /// callee.
1928 ///
1929 /// This function sets the caller's attribute to false if the callee's attribute
1930 /// is false.
1931 template<typename AttrClass>
1932 static void setAND(Function &Caller, const Function &Callee) {
1933   if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1934       !AttrClass::isSet(Callee, AttrClass::getKind()))
1935     AttrClass::set(Caller, AttrClass::getKind(), false);
1936 }
1937 
1938 /// Compute the logical OR of the attributes of the caller and the
1939 /// callee.
1940 ///
1941 /// This function sets the caller's attribute to true if the callee's attribute
1942 /// is true.
1943 template<typename AttrClass>
1944 static void setOR(Function &Caller, const Function &Callee) {
1945   if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1946       AttrClass::isSet(Callee, AttrClass::getKind()))
1947     AttrClass::set(Caller, AttrClass::getKind(), true);
1948 }
1949 
1950 /// If the inlined function had a higher stack protection level than the
1951 /// calling function, then bump up the caller's stack protection level.
1952 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1953 #ifndef NDEBUG
1954   if (!Callee.hasFnAttribute(Attribute::AlwaysInline)) {
1955     assert(!(!Callee.hasStackProtectorFnAttr() &&
1956              Caller.hasStackProtectorFnAttr()) &&
1957            "stack protected caller but callee requested no stack protector");
1958     assert(!(!Caller.hasStackProtectorFnAttr() &&
1959              Callee.hasStackProtectorFnAttr()) &&
1960            "stack protected callee but caller requested no stack protector");
1961   }
1962 #endif
1963   // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1964   // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1965   // clutter to the IR.
1966   AttrBuilder OldSSPAttr;
1967   OldSSPAttr.addAttribute(Attribute::StackProtect)
1968       .addAttribute(Attribute::StackProtectStrong)
1969       .addAttribute(Attribute::StackProtectReq);
1970 
1971   if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1972     Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1973     Caller.addFnAttr(Attribute::StackProtectReq);
1974   } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1975              !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1976     Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1977     Caller.addFnAttr(Attribute::StackProtectStrong);
1978   } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1979              !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1980              !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1981     Caller.addFnAttr(Attribute::StackProtect);
1982 }
1983 
1984 /// If the inlined function required stack probes, then ensure that
1985 /// the calling function has those too.
1986 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
1987   if (!Caller.hasFnAttribute("probe-stack") &&
1988       Callee.hasFnAttribute("probe-stack")) {
1989     Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1990   }
1991 }
1992 
1993 /// If the inlined function defines the size of guard region
1994 /// on the stack, then ensure that the calling function defines a guard region
1995 /// that is no larger.
1996 static void
1997 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1998   Attribute CalleeAttr = Callee.getFnAttribute("stack-probe-size");
1999   if (CalleeAttr.isValid()) {
2000     Attribute CallerAttr = Caller.getFnAttribute("stack-probe-size");
2001     if (CallerAttr.isValid()) {
2002       uint64_t CallerStackProbeSize, CalleeStackProbeSize;
2003       CallerAttr.getValueAsString().getAsInteger(0, CallerStackProbeSize);
2004       CalleeAttr.getValueAsString().getAsInteger(0, CalleeStackProbeSize);
2005 
2006       if (CallerStackProbeSize > CalleeStackProbeSize) {
2007         Caller.addFnAttr(CalleeAttr);
2008       }
2009     } else {
2010       Caller.addFnAttr(CalleeAttr);
2011     }
2012   }
2013 }
2014 
2015 /// If the inlined function defines a min legal vector width, then ensure
2016 /// the calling function has the same or larger min legal vector width. If the
2017 /// caller has the attribute, but the callee doesn't, we need to remove the
2018 /// attribute from the caller since we can't make any guarantees about the
2019 /// caller's requirements.
2020 /// This function is called after the inlining decision has been made so we have
2021 /// to merge the attribute this way. Heuristics that would use
2022 /// min-legal-vector-width to determine inline compatibility would need to be
2023 /// handled as part of inline cost analysis.
2024 static void
2025 adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
2026   Attribute CallerAttr = Caller.getFnAttribute("min-legal-vector-width");
2027   if (CallerAttr.isValid()) {
2028     Attribute CalleeAttr = Callee.getFnAttribute("min-legal-vector-width");
2029     if (CalleeAttr.isValid()) {
2030       uint64_t CallerVectorWidth, CalleeVectorWidth;
2031       CallerAttr.getValueAsString().getAsInteger(0, CallerVectorWidth);
2032       CalleeAttr.getValueAsString().getAsInteger(0, CalleeVectorWidth);
2033       if (CallerVectorWidth < CalleeVectorWidth)
2034         Caller.addFnAttr(CalleeAttr);
2035     } else {
2036       // If the callee doesn't have the attribute then we don't know anything
2037       // and must drop the attribute from the caller.
2038       Caller.removeFnAttr("min-legal-vector-width");
2039     }
2040   }
2041 }
2042 
2043 /// If the inlined function has null_pointer_is_valid attribute,
2044 /// set this attribute in the caller post inlining.
2045 static void
2046 adjustNullPointerValidAttr(Function &Caller, const Function &Callee) {
2047   if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) {
2048     Caller.addFnAttr(Attribute::NullPointerIsValid);
2049   }
2050 }
2051 
2052 struct EnumAttr {
2053   static bool isSet(const Function &Fn,
2054                     Attribute::AttrKind Kind) {
2055     return Fn.hasFnAttribute(Kind);
2056   }
2057 
2058   static void set(Function &Fn,
2059                   Attribute::AttrKind Kind, bool Val) {
2060     if (Val)
2061       Fn.addFnAttr(Kind);
2062     else
2063       Fn.removeFnAttr(Kind);
2064   }
2065 };
2066 
2067 struct StrBoolAttr {
2068   static bool isSet(const Function &Fn,
2069                     StringRef Kind) {
2070     auto A = Fn.getFnAttribute(Kind);
2071     return A.getValueAsString().equals("true");
2072   }
2073 
2074   static void set(Function &Fn,
2075                   StringRef Kind, bool Val) {
2076     Fn.addFnAttr(Kind, Val ? "true" : "false");
2077   }
2078 };
2079 
2080 #define GET_ATTR_NAMES
2081 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)                                \
2082   struct ENUM_NAME##Attr : EnumAttr {                                          \
2083     static enum Attribute::AttrKind getKind() {                                \
2084       return llvm::Attribute::ENUM_NAME;                                       \
2085     }                                                                          \
2086   };
2087 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME)                             \
2088   struct ENUM_NAME##Attr : StrBoolAttr {                                       \
2089     static StringRef getKind() { return #DISPLAY_NAME; }                       \
2090   };
2091 #include "llvm/IR/Attributes.inc"
2092 
2093 #define GET_ATTR_COMPAT_FUNC
2094 #include "llvm/IR/Attributes.inc"
2095 
2096 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
2097                                          const Function &Callee) {
2098   return hasCompatibleFnAttrs(Caller, Callee);
2099 }
2100 
2101 bool AttributeFuncs::areOutlineCompatible(const Function &A,
2102                                           const Function &B) {
2103   return hasCompatibleFnAttrs(A, B);
2104 }
2105 
2106 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
2107                                                 const Function &Callee) {
2108   mergeFnAttrs(Caller, Callee);
2109 }
2110 
2111 void AttributeFuncs::mergeAttributesForOutlining(Function &Base,
2112                                                 const Function &ToMerge) {
2113 
2114   // We merge functions so that they meet the most general case.
2115   // For example, if the NoNansFPMathAttr is set in one function, but not in
2116   // the other, in the merged function we can say that the NoNansFPMathAttr
2117   // is not set.
2118   // However if we have the SpeculativeLoadHardeningAttr set true in one
2119   // function, but not the other, we make sure that the function retains
2120   // that aspect in the merged function.
2121   mergeFnAttrs(Base, ToMerge);
2122 }
2123