xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/Attributes.h (revision c7a063741720ef81d4caa4613242579d12f1d605)
1 //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
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 contains the simple types necessary to represent the
11 /// attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_ATTRIBUTES_H
16 #define LLVM_IR_ATTRIBUTES_H
17 
18 #include "llvm-c/Types.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/Support/Alignment.h"
25 #include "llvm/Support/PointerLikeTypeTraits.h"
26 #include <bitset>
27 #include <cassert>
28 #include <cstdint>
29 #include <set>
30 #include <string>
31 #include <utility>
32 
33 namespace llvm {
34 
35 class AttrBuilder;
36 class AttributeMask;
37 class AttributeImpl;
38 class AttributeListImpl;
39 class AttributeSetNode;
40 class FoldingSetNodeID;
41 class Function;
42 class LLVMContext;
43 class Type;
44 
45 //===----------------------------------------------------------------------===//
46 /// \class
47 /// Functions, function parameters, and return types can have attributes
48 /// to indicate how they should be treated by optimizations and code
49 /// generation. This class represents one of those attributes. It's light-weight
50 /// and should be passed around by-value.
51 class Attribute {
52 public:
53   /// This enumeration lists the attributes that can be associated with
54   /// parameters, function results, or the function itself.
55   ///
56   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
57   /// entry in the unwind table. The `nounwind' attribute is about an exception
58   /// passing by the function.
59   ///
60   /// In a theoretical system that uses tables for profiling and SjLj for
61   /// exceptions, they would be fully independent. In a normal system that uses
62   /// tables for both, the semantics are:
63   ///
64   /// nil                = Needs an entry because an exception might pass by.
65   /// nounwind           = No need for an entry
66   /// uwtable            = Needs an entry because the ABI says so and because
67   ///                      an exception might pass by.
68   /// uwtable + nounwind = Needs an entry because the ABI says so.
69 
70   enum AttrKind {
71     // IR-Level Attributes
72     None,                  ///< No attributes have been set
73     #define GET_ATTR_ENUM
74     #include "llvm/IR/Attributes.inc"
75     EndAttrKinds,          ///< Sentinal value useful for loops
76     EmptyKey,              ///< Use as Empty key for DenseMap of AttrKind
77     TombstoneKey,          ///< Use as Tombstone key for DenseMap of AttrKind
78   };
79 
80   static const unsigned NumIntAttrKinds = LastIntAttr - FirstIntAttr + 1;
81   static const unsigned NumTypeAttrKinds = LastTypeAttr - FirstTypeAttr + 1;
82 
83   static bool isEnumAttrKind(AttrKind Kind) {
84     return Kind >= FirstEnumAttr && Kind <= LastEnumAttr;
85   }
86   static bool isIntAttrKind(AttrKind Kind) {
87     return Kind >= FirstIntAttr && Kind <= LastIntAttr;
88   }
89   static bool isTypeAttrKind(AttrKind Kind) {
90     return Kind >= FirstTypeAttr && Kind <= LastTypeAttr;
91   }
92 
93   static bool canUseAsFnAttr(AttrKind Kind);
94   static bool canUseAsParamAttr(AttrKind Kind);
95   static bool canUseAsRetAttr(AttrKind Kind);
96 
97 private:
98   AttributeImpl *pImpl = nullptr;
99 
100   Attribute(AttributeImpl *A) : pImpl(A) {}
101 
102 public:
103   Attribute() = default;
104 
105   //===--------------------------------------------------------------------===//
106   // Attribute Construction
107   //===--------------------------------------------------------------------===//
108 
109   /// Return a uniquified Attribute object.
110   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
111   static Attribute get(LLVMContext &Context, StringRef Kind,
112                        StringRef Val = StringRef());
113   static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);
114 
115   /// Return a uniquified Attribute object that has the specific
116   /// alignment set.
117   static Attribute getWithAlignment(LLVMContext &Context, Align Alignment);
118   static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment);
119   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
120                                               uint64_t Bytes);
121   static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
122                                                      uint64_t Bytes);
123   static Attribute getWithAllocSizeArgs(LLVMContext &Context,
124                                         unsigned ElemSizeArg,
125                                         const Optional<unsigned> &NumElemsArg);
126   static Attribute getWithVScaleRangeArgs(LLVMContext &Context,
127                                           unsigned MinValue, unsigned MaxValue);
128   static Attribute getWithByValType(LLVMContext &Context, Type *Ty);
129   static Attribute getWithStructRetType(LLVMContext &Context, Type *Ty);
130   static Attribute getWithByRefType(LLVMContext &Context, Type *Ty);
131   static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty);
132   static Attribute getWithInAllocaType(LLVMContext &Context, Type *Ty);
133 
134   /// For a typed attribute, return the equivalent attribute with the type
135   /// changed to \p ReplacementTy.
136   Attribute getWithNewType(LLVMContext &Context, Type *ReplacementTy) {
137     assert(isTypeAttribute() && "this requires a typed attribute");
138     return get(Context, getKindAsEnum(), ReplacementTy);
139   }
140 
141   static Attribute::AttrKind getAttrKindFromName(StringRef AttrName);
142 
143   static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
144 
145   /// Return true if the provided string matches the IR name of an attribute.
146   /// example: "noalias" return true but not "NoAlias"
147   static bool isExistingAttribute(StringRef Name);
148 
149   //===--------------------------------------------------------------------===//
150   // Attribute Accessors
151   //===--------------------------------------------------------------------===//
152 
153   /// Return true if the attribute is an Attribute::AttrKind type.
154   bool isEnumAttribute() const;
155 
156   /// Return true if the attribute is an integer attribute.
157   bool isIntAttribute() const;
158 
159   /// Return true if the attribute is a string (target-dependent)
160   /// attribute.
161   bool isStringAttribute() const;
162 
163   /// Return true if the attribute is a type attribute.
164   bool isTypeAttribute() const;
165 
166   /// Return true if the attribute is any kind of attribute.
167   bool isValid() const { return pImpl; }
168 
169   /// Return true if the attribute is present.
170   bool hasAttribute(AttrKind Val) const;
171 
172   /// Return true if the target-dependent attribute is present.
173   bool hasAttribute(StringRef Val) const;
174 
175   /// Return the attribute's kind as an enum (Attribute::AttrKind). This
176   /// requires the attribute to be an enum, integer, or type attribute.
177   Attribute::AttrKind getKindAsEnum() const;
178 
179   /// Return the attribute's value as an integer. This requires that the
180   /// attribute be an integer attribute.
181   uint64_t getValueAsInt() const;
182 
183   /// Return the attribute's value as a boolean. This requires that the
184   /// attribute be a string attribute.
185   bool getValueAsBool() const;
186 
187   /// Return the attribute's kind as a string. This requires the
188   /// attribute to be a string attribute.
189   StringRef getKindAsString() const;
190 
191   /// Return the attribute's value as a string. This requires the
192   /// attribute to be a string attribute.
193   StringRef getValueAsString() const;
194 
195   /// Return the attribute's value as a Type. This requires the attribute to be
196   /// a type attribute.
197   Type *getValueAsType() const;
198 
199   /// Returns the alignment field of an attribute as a byte alignment
200   /// value.
201   MaybeAlign getAlignment() const;
202 
203   /// Returns the stack alignment field of an attribute as a byte
204   /// alignment value.
205   MaybeAlign getStackAlignment() const;
206 
207   /// Returns the number of dereferenceable bytes from the
208   /// dereferenceable attribute.
209   uint64_t getDereferenceableBytes() const;
210 
211   /// Returns the number of dereferenceable_or_null bytes from the
212   /// dereferenceable_or_null attribute.
213   uint64_t getDereferenceableOrNullBytes() const;
214 
215   /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
216   /// if not known).
217   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
218 
219   /// Returns the minimum value for the vscale_range attribute.
220   unsigned getVScaleRangeMin() const;
221 
222   /// Returns the maximum value for the vscale_range attribute or None when
223   /// unknown.
224   Optional<unsigned> getVScaleRangeMax() const;
225 
226   /// The Attribute is converted to a string of equivalent mnemonic. This
227   /// is, presumably, for writing out the mnemonics for the assembly writer.
228   std::string getAsString(bool InAttrGrp = false) const;
229 
230   /// Return true if this attribute belongs to the LLVMContext.
231   bool hasParentContext(LLVMContext &C) const;
232 
233   /// Equality and non-equality operators.
234   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
235   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
236 
237   /// Less-than operator. Useful for sorting the attributes list.
238   bool operator<(Attribute A) const;
239 
240   void Profile(FoldingSetNodeID &ID) const;
241 
242   /// Return a raw pointer that uniquely identifies this attribute.
243   void *getRawPointer() const {
244     return pImpl;
245   }
246 
247   /// Get an attribute from a raw pointer created by getRawPointer.
248   static Attribute fromRawPointer(void *RawPtr) {
249     return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
250   }
251 };
252 
253 // Specialized opaque value conversions.
254 inline LLVMAttributeRef wrap(Attribute Attr) {
255   return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
256 }
257 
258 // Specialized opaque value conversions.
259 inline Attribute unwrap(LLVMAttributeRef Attr) {
260   return Attribute::fromRawPointer(Attr);
261 }
262 
263 //===----------------------------------------------------------------------===//
264 /// \class
265 /// This class holds the attributes for a particular argument, parameter,
266 /// function, or return value. It is an immutable value type that is cheap to
267 /// copy. Adding and removing enum attributes is intended to be fast, but adding
268 /// and removing string or integer attributes involves a FoldingSet lookup.
269 class AttributeSet {
270   friend AttributeListImpl;
271   template <typename Ty, typename Enable> friend struct DenseMapInfo;
272 
273   // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
274   // This will allow an efficient implementation of addAttribute and
275   // removeAttribute for enum attrs.
276 
277   /// Private implementation pointer.
278   AttributeSetNode *SetNode = nullptr;
279 
280 private:
281   explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
282 
283 public:
284   /// AttributeSet is a trivially copyable value type.
285   AttributeSet() = default;
286   AttributeSet(const AttributeSet &) = default;
287   ~AttributeSet() = default;
288 
289   static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
290   static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
291 
292   bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
293   bool operator!=(const AttributeSet &O) const { return !(*this == O); }
294 
295   /// Add an argument attribute. Returns a new set because attribute sets are
296   /// immutable.
297   LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
298                                            Attribute::AttrKind Kind) const;
299 
300   /// Add a target-dependent attribute. Returns a new set because attribute sets
301   /// are immutable.
302   LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
303                                            StringRef Value = StringRef()) const;
304 
305   /// Add attributes to the attribute set. Returns a new set because attribute
306   /// sets are immutable.
307   LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
308                                             AttributeSet AS) const;
309 
310   /// Remove the specified attribute from this set. Returns a new set because
311   /// attribute sets are immutable.
312   LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
313                                               Attribute::AttrKind Kind) const;
314 
315   /// Remove the specified attribute from this set. Returns a new set because
316   /// attribute sets are immutable.
317   LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
318                                               StringRef Kind) const;
319 
320   /// Remove the specified attributes from this set. Returns a new set because
321   /// attribute sets are immutable.
322   LLVM_NODISCARD AttributeSet
323   removeAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const;
324 
325   /// Return the number of attributes in this set.
326   unsigned getNumAttributes() const;
327 
328   /// Return true if attributes exists in this set.
329   bool hasAttributes() const { return SetNode != nullptr; }
330 
331   /// Return true if the attribute exists in this set.
332   bool hasAttribute(Attribute::AttrKind Kind) const;
333 
334   /// Return true if the attribute exists in this set.
335   bool hasAttribute(StringRef Kind) const;
336 
337   /// Return the attribute object.
338   Attribute getAttribute(Attribute::AttrKind Kind) const;
339 
340   /// Return the target-dependent attribute object.
341   Attribute getAttribute(StringRef Kind) const;
342 
343   MaybeAlign getAlignment() const;
344   MaybeAlign getStackAlignment() const;
345   uint64_t getDereferenceableBytes() const;
346   uint64_t getDereferenceableOrNullBytes() const;
347   Type *getByValType() const;
348   Type *getStructRetType() const;
349   Type *getByRefType() const;
350   Type *getPreallocatedType() const;
351   Type *getInAllocaType() const;
352   Type *getElementType() const;
353   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
354   unsigned getVScaleRangeMin() const;
355   Optional<unsigned> getVScaleRangeMax() const;
356   std::string getAsString(bool InAttrGrp = false) const;
357 
358   /// Return true if this attribute set belongs to the LLVMContext.
359   bool hasParentContext(LLVMContext &C) const;
360 
361   using iterator = const Attribute *;
362 
363   iterator begin() const;
364   iterator end() const;
365 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
366   void dump() const;
367 #endif
368 };
369 
370 //===----------------------------------------------------------------------===//
371 /// \class
372 /// Provide DenseMapInfo for AttributeSet.
373 template <> struct DenseMapInfo<AttributeSet, void> {
374   static AttributeSet getEmptyKey() {
375     auto Val = static_cast<uintptr_t>(-1);
376     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
377     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
378   }
379 
380   static AttributeSet getTombstoneKey() {
381     auto Val = static_cast<uintptr_t>(-2);
382     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
383     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
384   }
385 
386   static unsigned getHashValue(AttributeSet AS) {
387     return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
388            (unsigned((uintptr_t)AS.SetNode) >> 9);
389   }
390 
391   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
392 };
393 
394 //===----------------------------------------------------------------------===//
395 /// \class
396 /// This class holds the attributes for a function, its return value, and
397 /// its parameters. You access the attributes for each of them via an index into
398 /// the AttributeList object. The function attributes are at index
399 /// `AttributeList::FunctionIndex', the return value is at index
400 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
401 /// index `AttributeList::FirstArgIndex'.
402 class AttributeList {
403 public:
404   enum AttrIndex : unsigned {
405     ReturnIndex = 0U,
406     FunctionIndex = ~0U,
407     FirstArgIndex = 1,
408   };
409 
410 private:
411   friend class AttrBuilder;
412   friend class AttributeListImpl;
413   friend class AttributeSet;
414   friend class AttributeSetNode;
415   template <typename Ty, typename Enable> friend struct DenseMapInfo;
416 
417   /// The attributes that we are managing. This can be null to represent
418   /// the empty attributes list.
419   AttributeListImpl *pImpl = nullptr;
420 
421 public:
422   /// Create an AttributeList with the specified parameters in it.
423   static AttributeList get(LLVMContext &C,
424                            ArrayRef<std::pair<unsigned, Attribute>> Attrs);
425   static AttributeList get(LLVMContext &C,
426                            ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
427 
428   /// Create an AttributeList from attribute sets for a function, its
429   /// return value, and all of its arguments.
430   static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
431                            AttributeSet RetAttrs,
432                            ArrayRef<AttributeSet> ArgAttrs);
433 
434 private:
435   explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
436 
437   static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
438 
439   AttributeList setAttributesAtIndex(LLVMContext &C, unsigned Index,
440                                      AttributeSet Attrs) const;
441 
442 public:
443   AttributeList() = default;
444 
445   //===--------------------------------------------------------------------===//
446   // AttributeList Construction and Mutation
447   //===--------------------------------------------------------------------===//
448 
449   /// Return an AttributeList with the specified parameters in it.
450   static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
451   static AttributeList get(LLVMContext &C, unsigned Index,
452                            ArrayRef<Attribute::AttrKind> Kinds);
453   static AttributeList get(LLVMContext &C, unsigned Index,
454                            ArrayRef<Attribute::AttrKind> Kinds,
455                            ArrayRef<uint64_t> Values);
456   static AttributeList get(LLVMContext &C, unsigned Index,
457                            ArrayRef<StringRef> Kind);
458   static AttributeList get(LLVMContext &C, unsigned Index,
459                            AttributeSet Attrs);
460   static AttributeList get(LLVMContext &C, unsigned Index,
461                            const AttrBuilder &B);
462 
463   // TODO: remove non-AtIndex versions of these methods.
464   /// Add an attribute to the attribute set at the given index.
465   /// Returns a new list because attribute lists are immutable.
466   LLVM_NODISCARD AttributeList addAttributeAtIndex(
467       LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const;
468 
469   /// Add an attribute to the attribute set at the given index.
470   /// Returns a new list because attribute lists are immutable.
471   LLVM_NODISCARD AttributeList
472   addAttributeAtIndex(LLVMContext &C, unsigned Index, StringRef Kind,
473                       StringRef Value = StringRef()) const;
474 
475   /// Add an attribute to the attribute set at the given index.
476   /// Returns a new list because attribute lists are immutable.
477   LLVM_NODISCARD AttributeList addAttributeAtIndex(LLVMContext &C,
478                                                    unsigned Index,
479                                                    Attribute A) const;
480 
481   /// Add attributes to the attribute set at the given index.
482   /// Returns a new list because attribute lists are immutable.
483   LLVM_NODISCARD AttributeList addAttributesAtIndex(LLVMContext &C,
484                                                     unsigned Index,
485                                                     const AttrBuilder &B) const;
486 
487   /// Add a function attribute to the list. Returns a new list because
488   /// attribute lists are immutable.
489   LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
490                                               Attribute::AttrKind Kind) const {
491     return addAttributeAtIndex(C, FunctionIndex, Kind);
492   }
493 
494   /// Add a function attribute to the list. Returns a new list because
495   /// attribute lists are immutable.
496   LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
497                                               Attribute Attr) const {
498     return addAttributeAtIndex(C, FunctionIndex, Attr);
499   }
500 
501   /// Add a function attribute to the list. Returns a new list because
502   /// attribute lists are immutable.
503   LLVM_NODISCARD AttributeList addFnAttribute(
504       LLVMContext &C, StringRef Kind, StringRef Value = StringRef()) const {
505     return addAttributeAtIndex(C, FunctionIndex, Kind, Value);
506   }
507 
508   /// Add function attribute to the list. Returns a new list because
509   /// attribute lists are immutable.
510   LLVM_NODISCARD AttributeList addFnAttributes(LLVMContext &C,
511                                                const AttrBuilder &B) const {
512     return addAttributesAtIndex(C, FunctionIndex, B);
513   }
514 
515   /// Add a return value attribute to the list. Returns a new list because
516   /// attribute lists are immutable.
517   LLVM_NODISCARD AttributeList addRetAttribute(LLVMContext &C,
518                                                Attribute::AttrKind Kind) const {
519     return addAttributeAtIndex(C, ReturnIndex, Kind);
520   }
521 
522   /// Add a return value attribute to the list. Returns a new list because
523   /// attribute lists are immutable.
524   LLVM_NODISCARD AttributeList addRetAttribute(LLVMContext &C,
525                                                Attribute Attr) const {
526     return addAttributeAtIndex(C, ReturnIndex, Attr);
527   }
528 
529   /// Add a return value attribute to the list. Returns a new list because
530   /// attribute lists are immutable.
531   LLVM_NODISCARD AttributeList addRetAttributes(LLVMContext &C,
532                                                 const AttrBuilder &B) const {
533     return addAttributesAtIndex(C, ReturnIndex, B);
534   }
535 
536   /// Add an argument attribute to the list. Returns a new list because
537   /// attribute lists are immutable.
538   LLVM_NODISCARD AttributeList addParamAttribute(
539       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
540     return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
541   }
542 
543   /// Add an argument attribute to the list. Returns a new list because
544   /// attribute lists are immutable.
545   LLVM_NODISCARD AttributeList
546   addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
547                     StringRef Value = StringRef()) const {
548     return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind, Value);
549   }
550 
551   /// Add an attribute to the attribute list at the given arg indices. Returns a
552   /// new list because attribute lists are immutable.
553   LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
554                                                  ArrayRef<unsigned> ArgNos,
555                                                  Attribute A) const;
556 
557   /// Add an argument attribute to the list. Returns a new list because
558   /// attribute lists are immutable.
559   LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
560                                                   unsigned ArgNo,
561                                                   const AttrBuilder &B) const {
562     return addAttributesAtIndex(C, ArgNo + FirstArgIndex, B);
563   }
564 
565   /// Remove the specified attribute at the specified index from this
566   /// attribute list. Returns a new list because attribute lists are immutable.
567   LLVM_NODISCARD AttributeList removeAttributeAtIndex(
568       LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const;
569 
570   /// Remove the specified attribute at the specified index from this
571   /// attribute list. Returns a new list because attribute lists are immutable.
572   LLVM_NODISCARD AttributeList removeAttributeAtIndex(LLVMContext &C,
573                                                       unsigned Index,
574                                                       StringRef Kind) const;
575   LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
576                                                StringRef Kind) const {
577     return removeAttributeAtIndex(C, Index, Kind);
578   }
579 
580   /// Remove the specified attributes at the specified index from this
581   /// attribute list. Returns a new list because attribute lists are immutable.
582   LLVM_NODISCARD AttributeList removeAttributesAtIndex(
583       LLVMContext &C, unsigned Index, const AttributeMask &AttrsToRemove) const;
584 
585   /// Remove all attributes at the specified index from this
586   /// attribute list. Returns a new list because attribute lists are immutable.
587   LLVM_NODISCARD AttributeList removeAttributesAtIndex(LLVMContext &C,
588                                                        unsigned Index) const;
589 
590   /// Remove the specified attribute at the function index from this
591   /// attribute list. Returns a new list because attribute lists are immutable.
592   LLVM_NODISCARD AttributeList
593   removeFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
594     return removeAttributeAtIndex(C, FunctionIndex, Kind);
595   }
596 
597   /// Remove the specified attribute at the function index from this
598   /// attribute list. Returns a new list because attribute lists are immutable.
599   LLVM_NODISCARD AttributeList removeFnAttribute(LLVMContext &C,
600                                                  StringRef Kind) const {
601     return removeAttributeAtIndex(C, FunctionIndex, Kind);
602   }
603 
604   /// Remove the specified attribute at the function index from this
605   /// attribute list. Returns a new list because attribute lists are immutable.
606   LLVM_NODISCARD AttributeList
607   removeFnAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const {
608     return removeAttributesAtIndex(C, FunctionIndex, AttrsToRemove);
609   }
610 
611   /// Remove the attributes at the function index from this
612   /// attribute list. Returns a new list because attribute lists are immutable.
613   LLVM_NODISCARD AttributeList removeFnAttributes(LLVMContext &C) const {
614     return removeAttributesAtIndex(C, FunctionIndex);
615   }
616 
617   /// Remove the specified attribute at the return value index from this
618   /// attribute list. Returns a new list because attribute lists are immutable.
619   LLVM_NODISCARD AttributeList
620   removeRetAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
621     return removeAttributeAtIndex(C, ReturnIndex, Kind);
622   }
623 
624   /// Remove the specified attribute at the return value index from this
625   /// attribute list. Returns a new list because attribute lists are immutable.
626   LLVM_NODISCARD AttributeList removeRetAttribute(LLVMContext &C,
627                                                   StringRef Kind) const {
628     return removeAttributeAtIndex(C, ReturnIndex, Kind);
629   }
630 
631   /// Remove the specified attribute at the return value index from this
632   /// attribute list. Returns a new list because attribute lists are immutable.
633   LLVM_NODISCARD AttributeList removeRetAttributes(
634       LLVMContext &C, const AttributeMask &AttrsToRemove) const {
635     return removeAttributesAtIndex(C, ReturnIndex, AttrsToRemove);
636   }
637 
638   /// Remove the specified attribute at the specified arg index from this
639   /// attribute list. Returns a new list because attribute lists are immutable.
640   LLVM_NODISCARD AttributeList removeParamAttribute(
641       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
642     return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
643   }
644 
645   /// Remove the specified attribute at the specified arg index from this
646   /// attribute list. Returns a new list because attribute lists are immutable.
647   LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
648                                                     unsigned ArgNo,
649                                                     StringRef Kind) const {
650     return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
651   }
652 
653   /// Remove the specified attribute at the specified arg index from this
654   /// attribute list. Returns a new list because attribute lists are immutable.
655   LLVM_NODISCARD AttributeList
656   removeParamAttributes(LLVMContext &C, unsigned ArgNo,
657                         const AttributeMask &AttrsToRemove) const {
658     return removeAttributesAtIndex(C, ArgNo + FirstArgIndex, AttrsToRemove);
659   }
660 
661   /// Remove all attributes at the specified arg index from this
662   /// attribute list. Returns a new list because attribute lists are immutable.
663   LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
664                                                      unsigned ArgNo) const {
665     return removeAttributesAtIndex(C, ArgNo + FirstArgIndex);
666   }
667 
668   /// Replace the type contained by attribute \p AttrKind at index \p ArgNo wih
669   /// \p ReplacementTy, preserving all other attributes.
670   LLVM_NODISCARD AttributeList replaceAttributeTypeAtIndex(
671       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind,
672       Type *ReplacementTy) const {
673     Attribute Attr = getAttributeAtIndex(ArgNo, Kind);
674     auto Attrs = removeAttributeAtIndex(C, ArgNo, Kind);
675     return Attrs.addAttributeAtIndex(C, ArgNo,
676                                      Attr.getWithNewType(C, ReplacementTy));
677   }
678 
679   /// \brief Add the dereferenceable attribute to the attribute set at the given
680   /// index. Returns a new list because attribute lists are immutable.
681   LLVM_NODISCARD AttributeList addDereferenceableRetAttr(LLVMContext &C,
682                                                          uint64_t Bytes) const;
683 
684   /// \brief Add the dereferenceable attribute to the attribute set at the given
685   /// arg index. Returns a new list because attribute lists are immutable.
686   LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
687       LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const;
688 
689   /// Add the dereferenceable_or_null attribute to the attribute set at
690   /// the given arg index. Returns a new list because attribute lists are
691   /// immutable.
692   LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
693       LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const;
694 
695   /// Add the allocsize attribute to the attribute set at the given arg index.
696   /// Returns a new list because attribute lists are immutable.
697   LLVM_NODISCARD AttributeList
698   addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
699                         const Optional<unsigned> &NumElemsArg);
700 
701   //===--------------------------------------------------------------------===//
702   // AttributeList Accessors
703   //===--------------------------------------------------------------------===//
704 
705   /// The attributes for the specified index are returned.
706   AttributeSet getAttributes(unsigned Index) const;
707 
708   /// The attributes for the argument or parameter at the given index are
709   /// returned.
710   AttributeSet getParamAttrs(unsigned ArgNo) const;
711 
712   /// The attributes for the ret value are returned.
713   AttributeSet getRetAttrs() const;
714 
715   /// The function attributes are returned.
716   AttributeSet getFnAttrs() const;
717 
718   /// Return true if the attribute exists at the given index.
719   bool hasAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const;
720 
721   /// Return true if the attribute exists at the given index.
722   bool hasAttributeAtIndex(unsigned Index, StringRef Kind) const;
723 
724   /// Return true if attribute exists at the given index.
725   bool hasAttributesAtIndex(unsigned Index) const;
726 
727   /// Return true if the attribute exists for the given argument
728   bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
729     return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
730   }
731 
732   /// Return true if the attribute exists for the given argument
733   bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
734     return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
735   }
736 
737   /// Return true if attributes exists for the given argument
738   bool hasParamAttrs(unsigned ArgNo) const {
739     return hasAttributesAtIndex(ArgNo + FirstArgIndex);
740   }
741 
742   /// Return true if the attribute exists for the return value.
743   bool hasRetAttr(Attribute::AttrKind Kind) const {
744     return hasAttributeAtIndex(ReturnIndex, Kind);
745   }
746 
747   /// Return true if the attribute exists for the return value.
748   bool hasRetAttr(StringRef Kind) const {
749     return hasAttributeAtIndex(ReturnIndex, Kind);
750   }
751 
752   /// Return true if attributes exist for the return value.
753   bool hasRetAttrs() const { return hasAttributesAtIndex(ReturnIndex); }
754 
755   /// Return true if the attribute exists for the function.
756   bool hasFnAttr(Attribute::AttrKind Kind) const;
757 
758   /// Return true if the attribute exists for the function.
759   bool hasFnAttr(StringRef Kind) const;
760 
761   /// Return true the attributes exist for the function.
762   bool hasFnAttrs() const { return hasAttributesAtIndex(FunctionIndex); }
763 
764   /// Return true if the specified attribute is set for at least one
765   /// parameter or for the return value. If Index is not nullptr, the index
766   /// of a parameter with the specified attribute is provided.
767   bool hasAttrSomewhere(Attribute::AttrKind Kind,
768                         unsigned *Index = nullptr) const;
769 
770   /// Return the attribute object that exists at the given index.
771   Attribute getAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const;
772 
773   /// Return the attribute object that exists at the given index.
774   Attribute getAttributeAtIndex(unsigned Index, StringRef Kind) const;
775 
776   /// Return the attribute object that exists at the arg index.
777   Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
778     return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
779   }
780 
781   /// Return the attribute object that exists at the given index.
782   Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
783     return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
784   }
785 
786   /// Return the attribute object that exists for the function.
787   Attribute getFnAttr(Attribute::AttrKind Kind) const {
788     return getAttributeAtIndex(FunctionIndex, Kind);
789   }
790 
791   /// Return the attribute object that exists for the function.
792   Attribute getFnAttr(StringRef Kind) const {
793     return getAttributeAtIndex(FunctionIndex, Kind);
794   }
795 
796   /// Return the alignment of the return value.
797   MaybeAlign getRetAlignment() const;
798 
799   /// Return the alignment for the specified function parameter.
800   MaybeAlign getParamAlignment(unsigned ArgNo) const;
801 
802   /// Return the stack alignment for the specified function parameter.
803   MaybeAlign getParamStackAlignment(unsigned ArgNo) const;
804 
805   /// Return the byval type for the specified function parameter.
806   Type *getParamByValType(unsigned ArgNo) const;
807 
808   /// Return the sret type for the specified function parameter.
809   Type *getParamStructRetType(unsigned ArgNo) const;
810 
811   /// Return the byref type for the specified function parameter.
812   Type *getParamByRefType(unsigned ArgNo) const;
813 
814   /// Return the preallocated type for the specified function parameter.
815   Type *getParamPreallocatedType(unsigned ArgNo) const;
816 
817   /// Return the inalloca type for the specified function parameter.
818   Type *getParamInAllocaType(unsigned ArgNo) const;
819 
820   /// Return the elementtype type for the specified function parameter.
821   Type *getParamElementType(unsigned ArgNo) const;
822 
823   /// Get the stack alignment of the function.
824   MaybeAlign getFnStackAlignment() const;
825 
826   /// Get the stack alignment of the return value.
827   MaybeAlign getRetStackAlignment() const;
828 
829   /// Get the number of dereferenceable bytes (or zero if unknown) of the return
830   /// value.
831   uint64_t getRetDereferenceableBytes() const;
832 
833   /// Get the number of dereferenceable bytes (or zero if unknown) of an arg.
834   uint64_t getParamDereferenceableBytes(unsigned Index) const;
835 
836   /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of
837   /// the return value.
838   uint64_t getRetDereferenceableOrNullBytes() const;
839 
840   /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of an
841   /// arg.
842   uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const;
843 
844   /// Return the attributes at the index as a string.
845   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
846 
847   /// Return true if this attribute list belongs to the LLVMContext.
848   bool hasParentContext(LLVMContext &C) const;
849 
850   //===--------------------------------------------------------------------===//
851   // AttributeList Introspection
852   //===--------------------------------------------------------------------===//
853 
854   using iterator = const AttributeSet *;
855 
856   iterator begin() const;
857   iterator end() const;
858 
859   unsigned getNumAttrSets() const;
860 
861   // Implementation of indexes(). Produces iterators that wrap an index. Mostly
862   // to hide the awkwardness of unsigned wrapping when iterating over valid
863   // indexes.
864   struct index_iterator {
865     unsigned NumAttrSets;
866     index_iterator(int NumAttrSets) : NumAttrSets(NumAttrSets) {}
867     struct int_wrapper {
868       int_wrapper(unsigned i) : i(i) {}
869       unsigned i;
870       unsigned operator*() { return i; }
871       bool operator!=(const int_wrapper &Other) { return i != Other.i; }
872       int_wrapper &operator++() {
873         // This is expected to undergo unsigned wrapping since FunctionIndex is
874         // ~0 and that's where we start.
875         ++i;
876         return *this;
877       }
878     };
879 
880     int_wrapper begin() { return int_wrapper(AttributeList::FunctionIndex); }
881 
882     int_wrapper end() { return int_wrapper(NumAttrSets - 1); }
883   };
884 
885   /// Use this to iterate over the valid attribute indexes.
886   index_iterator indexes() const { return index_iterator(getNumAttrSets()); }
887 
888   /// operator==/!= - Provide equality predicates.
889   bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
890   bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
891 
892   /// Return a raw pointer that uniquely identifies this attribute list.
893   void *getRawPointer() const {
894     return pImpl;
895   }
896 
897   /// Return true if there are no attributes.
898   bool isEmpty() const { return pImpl == nullptr; }
899 
900   void print(raw_ostream &O) const;
901 
902   void dump() const;
903 };
904 
905 //===----------------------------------------------------------------------===//
906 /// \class
907 /// Provide DenseMapInfo for AttributeList.
908 template <> struct DenseMapInfo<AttributeList, void> {
909   static AttributeList getEmptyKey() {
910     auto Val = static_cast<uintptr_t>(-1);
911     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
912     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
913   }
914 
915   static AttributeList getTombstoneKey() {
916     auto Val = static_cast<uintptr_t>(-2);
917     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
918     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
919   }
920 
921   static unsigned getHashValue(AttributeList AS) {
922     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
923            (unsigned((uintptr_t)AS.pImpl) >> 9);
924   }
925 
926   static bool isEqual(AttributeList LHS, AttributeList RHS) {
927     return LHS == RHS;
928   }
929 };
930 
931 //===----------------------------------------------------------------------===//
932 /// \class
933 /// This class stores enough information to efficiently remove some attributes
934 /// from an existing AttrBuilder, AttributeSet or AttributeList.
935 class AttributeMask {
936   std::bitset<Attribute::EndAttrKinds> Attrs;
937   std::set<SmallString<32>, std::less<>> TargetDepAttrs;
938 
939 public:
940   AttributeMask() = default;
941   AttributeMask(const AttributeMask &) = delete;
942   AttributeMask(AttributeMask &&) = default;
943 
944   AttributeMask(AttributeSet AS) {
945     for (Attribute A : AS)
946       addAttribute(A);
947   }
948 
949   /// Add an attribute to the mask.
950   AttributeMask &addAttribute(Attribute::AttrKind Val) {
951     assert((unsigned)Val < Attribute::EndAttrKinds &&
952            "Attribute out of range!");
953     Attrs[Val] = true;
954     return *this;
955   }
956 
957   /// Add the Attribute object to the builder.
958   AttributeMask &addAttribute(Attribute A) {
959     if (A.isStringAttribute())
960       addAttribute(A.getKindAsString());
961     else
962       addAttribute(A.getKindAsEnum());
963     return *this;
964   }
965 
966   /// Add the target-dependent attribute to the builder.
967   AttributeMask &addAttribute(StringRef A) {
968     TargetDepAttrs.insert(A);
969     return *this;
970   }
971 
972   /// Return true if the builder has the specified attribute.
973   bool contains(Attribute::AttrKind A) const {
974     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
975     return Attrs[A];
976   }
977 
978   /// Return true if the builder has the specified target-dependent
979   /// attribute.
980   bool contains(StringRef A) const { return TargetDepAttrs.count(A); }
981 
982   /// Return true if the mask contains the specified attribute.
983   bool contains(Attribute A) const {
984     if (A.isStringAttribute())
985       return contains(A.getKindAsString());
986     return contains(A.getKindAsEnum());
987   }
988 };
989 
990 //===----------------------------------------------------------------------===//
991 /// \class
992 /// This class is used in conjunction with the Attribute::get method to
993 /// create an Attribute object. The object itself is uniquified. The Builder's
994 /// value, however, is not. So this can be used as a quick way to test for
995 /// equality, presence of attributes, etc.
996 class AttrBuilder {
997   LLVMContext &Ctx;
998   SmallVector<Attribute, 8> Attrs;
999 
1000 public:
1001   AttrBuilder(LLVMContext &Ctx) : Ctx(Ctx) {}
1002   AttrBuilder(const AttrBuilder &) = delete;
1003   AttrBuilder(AttrBuilder &&) = default;
1004 
1005   AttrBuilder(LLVMContext &Ctx, const Attribute &A) : Ctx(Ctx) {
1006     addAttribute(A);
1007   }
1008 
1009   AttrBuilder(LLVMContext &Ctx, AttributeSet AS);
1010 
1011   void clear();
1012 
1013   /// Add an attribute to the builder.
1014   AttrBuilder &addAttribute(Attribute::AttrKind Val);
1015 
1016   /// Add the Attribute object to the builder.
1017   AttrBuilder &addAttribute(Attribute A);
1018 
1019   /// Add the target-dependent attribute to the builder.
1020   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
1021 
1022   /// Remove an attribute from the builder.
1023   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
1024 
1025   /// Remove the target-dependent attribute from the builder.
1026   AttrBuilder &removeAttribute(StringRef A);
1027 
1028   /// Remove the target-dependent attribute from the builder.
1029   AttrBuilder &removeAttribute(Attribute A) {
1030     if (A.isStringAttribute())
1031       return removeAttribute(A.getKindAsString());
1032     else
1033       return removeAttribute(A.getKindAsEnum());
1034   }
1035 
1036   /// Add the attributes from the builder. Attributes in the passed builder
1037   /// overwrite attributes in this builder if they have the same key.
1038   AttrBuilder &merge(const AttrBuilder &B);
1039 
1040   /// Remove the attributes from the builder.
1041   AttrBuilder &remove(const AttributeMask &AM);
1042 
1043   /// Return true if the builder has any attribute that's in the
1044   /// specified builder.
1045   bool overlaps(const AttributeMask &AM) const;
1046 
1047   /// Return true if the builder has the specified attribute.
1048   bool contains(Attribute::AttrKind A) const;
1049 
1050   /// Return true if the builder has the specified target-dependent
1051   /// attribute.
1052   bool contains(StringRef A) const;
1053 
1054   /// Return true if the builder has IR-level attributes.
1055   bool hasAttributes() const { return !Attrs.empty(); }
1056 
1057   /// Return true if the builder has an alignment attribute.
1058   bool hasAlignmentAttr() const;
1059 
1060   /// Return Attribute with the given Kind. The returned attribute will be
1061   /// invalid if the Kind is not present in the builder.
1062   Attribute getAttribute(Attribute::AttrKind Kind) const;
1063 
1064   /// Return Attribute with the given Kind. The returned attribute will be
1065   /// invalid if the Kind is not present in the builder.
1066   Attribute getAttribute(StringRef Kind) const;
1067 
1068   /// Return raw (possibly packed/encoded) value of integer attribute or 0 if
1069   /// not set.
1070   uint64_t getRawIntAttr(Attribute::AttrKind Kind) const;
1071 
1072   /// Retrieve the alignment attribute, if it exists.
1073   MaybeAlign getAlignment() const {
1074     return MaybeAlign(getRawIntAttr(Attribute::Alignment));
1075   }
1076 
1077   /// Retrieve the stack alignment attribute, if it exists.
1078   MaybeAlign getStackAlignment() const {
1079     return MaybeAlign(getRawIntAttr(Attribute::StackAlignment));
1080   }
1081 
1082   /// Retrieve the number of dereferenceable bytes, if the
1083   /// dereferenceable attribute exists (zero is returned otherwise).
1084   uint64_t getDereferenceableBytes() const {
1085     return getRawIntAttr(Attribute::Dereferenceable);
1086   }
1087 
1088   /// Retrieve the number of dereferenceable_or_null bytes, if the
1089   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
1090   uint64_t getDereferenceableOrNullBytes() const {
1091     return getRawIntAttr(Attribute::DereferenceableOrNull);
1092   }
1093 
1094   /// Retrieve type for the given type attribute.
1095   Type *getTypeAttr(Attribute::AttrKind Kind) const;
1096 
1097   /// Retrieve the byval type.
1098   Type *getByValType() const { return getTypeAttr(Attribute::ByVal); }
1099 
1100   /// Retrieve the sret type.
1101   Type *getStructRetType() const { return getTypeAttr(Attribute::StructRet); }
1102 
1103   /// Retrieve the byref type.
1104   Type *getByRefType() const { return getTypeAttr(Attribute::ByRef); }
1105 
1106   /// Retrieve the preallocated type.
1107   Type *getPreallocatedType() const {
1108     return getTypeAttr(Attribute::Preallocated);
1109   }
1110 
1111   /// Retrieve the inalloca type.
1112   Type *getInAllocaType() const { return getTypeAttr(Attribute::InAlloca); }
1113 
1114   /// Retrieve the allocsize args, if the allocsize attribute exists.  If it
1115   /// doesn't exist, pair(0, 0) is returned.
1116   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
1117 
1118   /// Retrieve the minimum value of 'vscale_range'.
1119   unsigned getVScaleRangeMin() const;
1120 
1121   /// Retrieve the maximum value of 'vscale_range' or None when unknown.
1122   Optional<unsigned> getVScaleRangeMax() const;
1123 
1124   /// Add integer attribute with raw value (packed/encoded if necessary).
1125   AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value);
1126 
1127   /// This turns an alignment into the form used internally in Attribute.
1128   /// This call has no effect if Align is not set.
1129   AttrBuilder &addAlignmentAttr(MaybeAlign Align);
1130 
1131   /// This turns an int alignment (which must be a power of 2) into the
1132   /// form used internally in Attribute.
1133   /// This call has no effect if Align is 0.
1134   /// Deprecated, use the version using a MaybeAlign.
1135   inline AttrBuilder &addAlignmentAttr(unsigned Align) {
1136     return addAlignmentAttr(MaybeAlign(Align));
1137   }
1138 
1139   /// This turns a stack alignment into the form used internally in Attribute.
1140   /// This call has no effect if Align is not set.
1141   AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
1142 
1143   /// This turns an int stack alignment (which must be a power of 2) into
1144   /// the form used internally in Attribute.
1145   /// This call has no effect if Align is 0.
1146   /// Deprecated, use the version using a MaybeAlign.
1147   inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
1148     return addStackAlignmentAttr(MaybeAlign(Align));
1149   }
1150 
1151   /// This turns the number of dereferenceable bytes into the form used
1152   /// internally in Attribute.
1153   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
1154 
1155   /// This turns the number of dereferenceable_or_null bytes into the
1156   /// form used internally in Attribute.
1157   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
1158 
1159   /// This turns one (or two) ints into the form used internally in Attribute.
1160   AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
1161                                 const Optional<unsigned> &NumElemsArg);
1162 
1163   /// This turns two ints into the form used internally in Attribute.
1164   AttrBuilder &addVScaleRangeAttr(unsigned MinValue,
1165                                   Optional<unsigned> MaxValue);
1166 
1167   /// Add a type attribute with the given type.
1168   AttrBuilder &addTypeAttr(Attribute::AttrKind Kind, Type *Ty);
1169 
1170   /// This turns a byval type into the form used internally in Attribute.
1171   AttrBuilder &addByValAttr(Type *Ty);
1172 
1173   /// This turns a sret type into the form used internally in Attribute.
1174   AttrBuilder &addStructRetAttr(Type *Ty);
1175 
1176   /// This turns a byref type into the form used internally in Attribute.
1177   AttrBuilder &addByRefAttr(Type *Ty);
1178 
1179   /// This turns a preallocated type into the form used internally in Attribute.
1180   AttrBuilder &addPreallocatedAttr(Type *Ty);
1181 
1182   /// This turns an inalloca type into the form used internally in Attribute.
1183   AttrBuilder &addInAllocaAttr(Type *Ty);
1184 
1185   /// Add an allocsize attribute, using the representation returned by
1186   /// Attribute.getIntValue().
1187   AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
1188 
1189   /// Add a vscale_range attribute, using the representation returned by
1190   /// Attribute.getIntValue().
1191   AttrBuilder &addVScaleRangeAttrFromRawRepr(uint64_t RawVScaleRangeRepr);
1192 
1193   ArrayRef<Attribute> attrs() const { return Attrs; }
1194 
1195   bool operator==(const AttrBuilder &B) const;
1196   bool operator!=(const AttrBuilder &B) const { return !(*this == B); }
1197 };
1198 
1199 namespace AttributeFuncs {
1200 
1201 /// Which attributes cannot be applied to a type.
1202 AttributeMask typeIncompatible(Type *Ty);
1203 
1204 /// Get param/return attributes which imply immediate undefined behavior if an
1205 /// invalid value is passed. For example, this includes noundef (where undef
1206 /// implies UB), but not nonnull (where null implies poison). It also does not
1207 /// include attributes like nocapture, which constrain the function
1208 /// implementation rather than the passed value.
1209 AttributeMask getUBImplyingAttributes();
1210 
1211 /// \returns Return true if the two functions have compatible target-independent
1212 /// attributes for inlining purposes.
1213 bool areInlineCompatible(const Function &Caller, const Function &Callee);
1214 
1215 
1216 /// Checks  if there are any incompatible function attributes between
1217 /// \p A and \p B.
1218 ///
1219 /// \param [in] A - The first function to be compared with.
1220 /// \param [in] B - The second function to be compared with.
1221 /// \returns true if the functions have compatible attributes.
1222 bool areOutlineCompatible(const Function &A, const Function &B);
1223 
1224 /// Merge caller's and callee's attributes.
1225 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
1226 
1227 /// Merges the functions attributes from \p ToMerge into function \p Base.
1228 ///
1229 /// \param [in,out] Base - The function being merged into.
1230 /// \param [in] ToMerge - The function to merge attributes from.
1231 void mergeAttributesForOutlining(Function &Base, const Function &ToMerge);
1232 
1233 } // end namespace AttributeFuncs
1234 
1235 } // end namespace llvm
1236 
1237 #endif // LLVM_IR_ATTRIBUTES_H
1238