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