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