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