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