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