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