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