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