xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/Attributes.h (revision 0d8fe2373503aeac48492f28073049a8bfa4feb5)
1 //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file contains the simple types necessary to represent the
11 /// attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_ATTRIBUTES_H
16 #define LLVM_IR_ATTRIBUTES_H
17 
18 #include "llvm-c/Types.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/Support/Alignment.h"
25 #include "llvm/Support/PointerLikeTypeTraits.h"
26 #include <bitset>
27 #include <cassert>
28 #include <cstdint>
29 #include <map>
30 #include <string>
31 #include <utility>
32 
33 namespace llvm {
34 
35 class AttrBuilder;
36 class AttributeImpl;
37 class AttributeListImpl;
38 class AttributeSetNode;
39 template<typename T> struct DenseMapInfo;
40 class FoldingSetNodeID;
41 class Function;
42 class LLVMContext;
43 class Type;
44 
45 //===----------------------------------------------------------------------===//
46 /// \class
47 /// Functions, function parameters, and return types can have attributes
48 /// to indicate how they should be treated by optimizations and code
49 /// generation. This class represents one of those attributes. It's light-weight
50 /// and should be passed around by-value.
51 class Attribute {
52 public:
53   /// This enumeration lists the attributes that can be associated with
54   /// parameters, function results, or the function itself.
55   ///
56   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
57   /// entry in the unwind table. The `nounwind' attribute is about an exception
58   /// passing by the function.
59   ///
60   /// In a theoretical system that uses tables for profiling and SjLj for
61   /// exceptions, they would be fully independent. In a normal system that uses
62   /// tables for both, the semantics are:
63   ///
64   /// nil                = Needs an entry because an exception might pass by.
65   /// nounwind           = No need for an entry
66   /// uwtable            = Needs an entry because the ABI says so and because
67   ///                      an exception might pass by.
68   /// uwtable + nounwind = Needs an entry because the ABI says so.
69 
70   enum AttrKind {
71     // IR-Level Attributes
72     None,                  ///< No attributes have been set
73     #define GET_ATTR_NAMES
74     #define ATTRIBUTE_ENUM(ENUM_NAME, OTHER) ENUM_NAME,
75     #include "llvm/IR/Attributes.inc"
76     EndAttrKinds,          ///< Sentinal value useful for loops
77     EmptyKey,              ///< Use as Empty key for DenseMap of AttrKind
78     TombstoneKey,          ///< Use as Tombstone key for DenseMap of AttrKind
79   };
80 
81 private:
82   AttributeImpl *pImpl = nullptr;
83 
84   Attribute(AttributeImpl *A) : pImpl(A) {}
85 
86 public:
87   Attribute() = default;
88 
89   //===--------------------------------------------------------------------===//
90   // Attribute Construction
91   //===--------------------------------------------------------------------===//
92 
93   /// Return a uniquified Attribute object.
94   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
95   static Attribute get(LLVMContext &Context, StringRef Kind,
96                        StringRef Val = StringRef());
97   static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);
98 
99   /// Return a uniquified Attribute object that has the specific
100   /// alignment set.
101   static Attribute getWithAlignment(LLVMContext &Context, Align Alignment);
102   static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment);
103   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
104                                               uint64_t Bytes);
105   static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
106                                                      uint64_t Bytes);
107   static Attribute getWithAllocSizeArgs(LLVMContext &Context,
108                                         unsigned ElemSizeArg,
109                                         const Optional<unsigned> &NumElemsArg);
110   static Attribute getWithByValType(LLVMContext &Context, Type *Ty);
111   static Attribute getWithStructRetType(LLVMContext &Context, Type *Ty);
112   static Attribute getWithByRefType(LLVMContext &Context, Type *Ty);
113   static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty);
114 
115   /// For a typed attribute, return the equivalent attribute with the type
116   /// changed to \p ReplacementTy.
117   Attribute getWithNewType(LLVMContext &Context, Type *ReplacementTy) {
118     assert(isTypeAttribute() && "this requires a typed attribute");
119     return get(Context, getKindAsEnum(), ReplacementTy);
120   }
121 
122   static Attribute::AttrKind getAttrKindFromName(StringRef AttrName);
123 
124   static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
125 
126   /// Return true if and only if the attribute has an Argument.
127   static bool doesAttrKindHaveArgument(Attribute::AttrKind AttrKind);
128 
129   /// Return true if the provided string matches the IR name of an attribute.
130   /// example: "noalias" return true but not "NoAlias"
131   static bool isExistingAttribute(StringRef Name);
132 
133   //===--------------------------------------------------------------------===//
134   // Attribute Accessors
135   //===--------------------------------------------------------------------===//
136 
137   /// Return true if the attribute is an Attribute::AttrKind type.
138   bool isEnumAttribute() const;
139 
140   /// Return true if the attribute is an integer attribute.
141   bool isIntAttribute() const;
142 
143   /// Return true if the attribute is a string (target-dependent)
144   /// attribute.
145   bool isStringAttribute() const;
146 
147   /// Return true if the attribute is a type attribute.
148   bool isTypeAttribute() const;
149 
150   /// Return true if the attribute is any kind of attribute.
151   bool isValid() const { return pImpl; }
152 
153   /// Return true if the attribute is present.
154   bool hasAttribute(AttrKind Val) const;
155 
156   /// Return true if the target-dependent attribute is present.
157   bool hasAttribute(StringRef Val) const;
158 
159   /// Return the attribute's kind as an enum (Attribute::AttrKind). This
160   /// requires the attribute to be an enum or integer attribute.
161   Attribute::AttrKind getKindAsEnum() const;
162 
163   /// Return the attribute's value as an integer. This requires that the
164   /// attribute be an integer attribute.
165   uint64_t getValueAsInt() const;
166 
167   /// Return the attribute's kind as a string. This requires the
168   /// attribute to be a string attribute.
169   StringRef getKindAsString() const;
170 
171   /// Return the attribute's value as a string. This requires the
172   /// attribute to be a string attribute.
173   StringRef getValueAsString() const;
174 
175   /// Return the attribute's value as a Type. This requires the attribute to be
176   /// a type attribute.
177   Type *getValueAsType() const;
178 
179   /// Returns the alignment field of an attribute as a byte alignment
180   /// value.
181   MaybeAlign getAlignment() const;
182 
183   /// Returns the stack alignment field of an attribute as a byte
184   /// alignment value.
185   MaybeAlign getStackAlignment() const;
186 
187   /// Returns the number of dereferenceable bytes from the
188   /// dereferenceable attribute.
189   uint64_t getDereferenceableBytes() const;
190 
191   /// Returns the number of dereferenceable_or_null bytes from the
192   /// dereferenceable_or_null attribute.
193   uint64_t getDereferenceableOrNullBytes() const;
194 
195   /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
196   /// if not known).
197   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
198 
199   /// The Attribute is converted to a string of equivalent mnemonic. This
200   /// is, presumably, for writing out the mnemonics for the assembly writer.
201   std::string getAsString(bool InAttrGrp = false) const;
202 
203   /// Equality and non-equality operators.
204   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
205   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
206 
207   /// Less-than operator. Useful for sorting the attributes list.
208   bool operator<(Attribute A) const;
209 
210   void Profile(FoldingSetNodeID &ID) const;
211 
212   /// Return a raw pointer that uniquely identifies this attribute.
213   void *getRawPointer() const {
214     return pImpl;
215   }
216 
217   /// Get an attribute from a raw pointer created by getRawPointer.
218   static Attribute fromRawPointer(void *RawPtr) {
219     return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
220   }
221 };
222 
223 // Specialized opaque value conversions.
224 inline LLVMAttributeRef wrap(Attribute Attr) {
225   return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
226 }
227 
228 // Specialized opaque value conversions.
229 inline Attribute unwrap(LLVMAttributeRef Attr) {
230   return Attribute::fromRawPointer(Attr);
231 }
232 
233 //===----------------------------------------------------------------------===//
234 /// \class
235 /// This class holds the attributes for a particular argument, parameter,
236 /// function, or return value. It is an immutable value type that is cheap to
237 /// copy. Adding and removing enum attributes is intended to be fast, but adding
238 /// and removing string or integer attributes involves a FoldingSet lookup.
239 class AttributeSet {
240   friend AttributeListImpl;
241   template <typename Ty> friend struct DenseMapInfo;
242 
243   // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
244   // This will allow an efficient implementation of addAttribute and
245   // removeAttribute for enum attrs.
246 
247   /// Private implementation pointer.
248   AttributeSetNode *SetNode = nullptr;
249 
250 private:
251   explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
252 
253 public:
254   /// AttributeSet is a trivially copyable value type.
255   AttributeSet() = default;
256   AttributeSet(const AttributeSet &) = default;
257   ~AttributeSet() = default;
258 
259   static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
260   static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
261 
262   bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
263   bool operator!=(const AttributeSet &O) const { return !(*this == O); }
264 
265   /// Add an argument attribute. Returns a new set because attribute sets are
266   /// immutable.
267   LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
268                                            Attribute::AttrKind Kind) const;
269 
270   /// Add a target-dependent attribute. Returns a new set because attribute sets
271   /// are immutable.
272   LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
273                                            StringRef Value = StringRef()) const;
274 
275   /// Add attributes to the attribute set. Returns a new set because attribute
276   /// sets are immutable.
277   LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
278                                             AttributeSet AS) const;
279 
280   /// Remove the specified attribute from this set. Returns a new set because
281   /// attribute sets are immutable.
282   LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
283                                               Attribute::AttrKind Kind) const;
284 
285   /// Remove the specified attribute from this set. Returns a new set because
286   /// attribute sets are immutable.
287   LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
288                                               StringRef Kind) const;
289 
290   /// Remove the specified attributes from this set. Returns a new set because
291   /// attribute sets are immutable.
292   LLVM_NODISCARD AttributeSet
293   removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
294 
295   /// Return the number of attributes in this set.
296   unsigned getNumAttributes() const;
297 
298   /// Return true if attributes exists in this set.
299   bool hasAttributes() const { return SetNode != nullptr; }
300 
301   /// Return true if the attribute exists in this set.
302   bool hasAttribute(Attribute::AttrKind Kind) const;
303 
304   /// Return true if the attribute exists in this set.
305   bool hasAttribute(StringRef Kind) const;
306 
307   /// Return the attribute object.
308   Attribute getAttribute(Attribute::AttrKind Kind) const;
309 
310   /// Return the target-dependent attribute object.
311   Attribute getAttribute(StringRef Kind) const;
312 
313   MaybeAlign getAlignment() const;
314   MaybeAlign getStackAlignment() const;
315   uint64_t getDereferenceableBytes() const;
316   uint64_t getDereferenceableOrNullBytes() const;
317   Type *getByValType() const;
318   Type *getStructRetType() const;
319   Type *getByRefType() const;
320   Type *getPreallocatedType() const;
321   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
322   std::string getAsString(bool InAttrGrp = false) const;
323 
324   using iterator = const Attribute *;
325 
326   iterator begin() const;
327   iterator end() const;
328 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
329   void dump() const;
330 #endif
331 };
332 
333 //===----------------------------------------------------------------------===//
334 /// \class
335 /// Provide DenseMapInfo for AttributeSet.
336 template <> struct DenseMapInfo<AttributeSet> {
337   static AttributeSet getEmptyKey() {
338     auto Val = static_cast<uintptr_t>(-1);
339     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
340     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
341   }
342 
343   static AttributeSet getTombstoneKey() {
344     auto Val = static_cast<uintptr_t>(-2);
345     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
346     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
347   }
348 
349   static unsigned getHashValue(AttributeSet AS) {
350     return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
351            (unsigned((uintptr_t)AS.SetNode) >> 9);
352   }
353 
354   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
355 };
356 
357 //===----------------------------------------------------------------------===//
358 /// \class
359 /// This class holds the attributes for a function, its return value, and
360 /// its parameters. You access the attributes for each of them via an index into
361 /// the AttributeList object. The function attributes are at index
362 /// `AttributeList::FunctionIndex', the return value is at index
363 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
364 /// index `AttributeList::FirstArgIndex'.
365 class AttributeList {
366 public:
367   enum AttrIndex : unsigned {
368     ReturnIndex = 0U,
369     FunctionIndex = ~0U,
370     FirstArgIndex = 1,
371   };
372 
373 private:
374   friend class AttrBuilder;
375   friend class AttributeListImpl;
376   friend class AttributeSet;
377   friend class AttributeSetNode;
378   template <typename Ty> friend struct DenseMapInfo;
379 
380   /// The attributes that we are managing. This can be null to represent
381   /// the empty attributes list.
382   AttributeListImpl *pImpl = nullptr;
383 
384 public:
385   /// Create an AttributeList with the specified parameters in it.
386   static AttributeList get(LLVMContext &C,
387                            ArrayRef<std::pair<unsigned, Attribute>> Attrs);
388   static AttributeList get(LLVMContext &C,
389                            ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
390 
391   /// Create an AttributeList from attribute sets for a function, its
392   /// return value, and all of its arguments.
393   static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
394                            AttributeSet RetAttrs,
395                            ArrayRef<AttributeSet> ArgAttrs);
396 
397 private:
398   explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
399 
400   static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
401 
402   AttributeList setAttributes(LLVMContext &C, unsigned Index,
403                               AttributeSet Attrs) const;
404 
405 public:
406   AttributeList() = default;
407 
408   //===--------------------------------------------------------------------===//
409   // AttributeList Construction and Mutation
410   //===--------------------------------------------------------------------===//
411 
412   /// Return an AttributeList with the specified parameters in it.
413   static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
414   static AttributeList get(LLVMContext &C, unsigned Index,
415                            ArrayRef<Attribute::AttrKind> Kinds);
416   static AttributeList get(LLVMContext &C, unsigned Index,
417                            ArrayRef<Attribute::AttrKind> Kinds,
418                            ArrayRef<uint64_t> Values);
419   static AttributeList get(LLVMContext &C, unsigned Index,
420                            ArrayRef<StringRef> Kind);
421   static AttributeList get(LLVMContext &C, unsigned Index,
422                            const AttrBuilder &B);
423 
424   /// Add an attribute to the attribute set at the given index.
425   /// Returns a new list because attribute lists are immutable.
426   LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
427                                             Attribute::AttrKind Kind) const;
428 
429   /// Add an attribute to the attribute set at the given index.
430   /// Returns a new list because attribute lists are immutable.
431   LLVM_NODISCARD AttributeList
432   addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
433                StringRef Value = StringRef()) const;
434 
435   /// Add an attribute to the attribute set at the given index.
436   /// Returns a new list because attribute lists are immutable.
437   LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
438                                             Attribute A) const;
439 
440   /// Add attributes to the attribute set at the given index.
441   /// Returns a new list because attribute lists are immutable.
442   LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
443                                              const AttrBuilder &B) const;
444 
445   /// Add an argument attribute to the list. Returns a new list because
446   /// attribute lists are immutable.
447   LLVM_NODISCARD AttributeList addParamAttribute(
448       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
449     return addAttribute(C, ArgNo + FirstArgIndex, Kind);
450   }
451 
452   /// Add an argument attribute to the list. Returns a new list because
453   /// attribute lists are immutable.
454   LLVM_NODISCARD AttributeList
455   addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
456                     StringRef Value = StringRef()) const {
457     return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
458   }
459 
460   /// Add an attribute to the attribute list at the given arg indices. Returns a
461   /// new list because attribute lists are immutable.
462   LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
463                                                  ArrayRef<unsigned> ArgNos,
464                                                  Attribute A) const;
465 
466   /// Add an argument attribute to the list. Returns a new list because
467   /// attribute lists are immutable.
468   LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
469                                                   unsigned ArgNo,
470                                                   const AttrBuilder &B) const {
471     return addAttributes(C, ArgNo + FirstArgIndex, B);
472   }
473 
474   /// Remove the specified attribute at the specified index from this
475   /// attribute list. Returns a new list because attribute lists are immutable.
476   LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
477                                                Attribute::AttrKind Kind) const;
478 
479   /// Remove the specified attribute at the specified index from this
480   /// attribute list. Returns a new list because attribute lists are immutable.
481   LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
482                                                StringRef Kind) const;
483 
484   /// Remove the specified attributes at the specified index from this
485   /// attribute list. Returns a new list because attribute lists are immutable.
486   LLVM_NODISCARD AttributeList removeAttributes(
487       LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
488 
489   /// Remove all attributes at the specified index from this
490   /// attribute list. Returns a new list because attribute lists are immutable.
491   LLVM_NODISCARD AttributeList removeAttributes(LLVMContext &C,
492                                                 unsigned Index) const;
493 
494   /// Remove the specified attribute at the specified arg index from this
495   /// attribute list. Returns a new list because attribute lists are immutable.
496   LLVM_NODISCARD AttributeList removeParamAttribute(
497       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
498     return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
499   }
500 
501   /// Remove the specified attribute at the specified arg index from this
502   /// attribute list. Returns a new list because attribute lists are immutable.
503   LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
504                                                     unsigned ArgNo,
505                                                     StringRef Kind) const {
506     return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
507   }
508 
509   /// Remove the specified attribute at the specified arg index from this
510   /// attribute list. Returns a new list because attribute lists are immutable.
511   LLVM_NODISCARD AttributeList removeParamAttributes(
512       LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
513     return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
514   }
515 
516   /// Remove all attributes at the specified arg index from this
517   /// attribute list. Returns a new list because attribute lists are immutable.
518   LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
519                                                      unsigned ArgNo) const {
520     return removeAttributes(C, ArgNo + FirstArgIndex);
521   }
522 
523   /// Replace the type contained by attribute \p AttrKind at index \p ArgNo wih
524   /// \p ReplacementTy, preserving all other attributes.
525   LLVM_NODISCARD AttributeList replaceAttributeType(LLVMContext &C,
526                                                     unsigned ArgNo,
527                                                     Attribute::AttrKind Kind,
528                                                     Type *ReplacementTy) const {
529     Attribute Attr = getAttribute(ArgNo, Kind);
530     auto Attrs = removeAttribute(C, ArgNo, Kind);
531     return Attrs.addAttribute(C, ArgNo, Attr.getWithNewType(C, ReplacementTy));
532   }
533 
534   /// \brief Add the dereferenceable attribute to the attribute set at the given
535   /// index. Returns a new list because attribute lists are immutable.
536   LLVM_NODISCARD AttributeList addDereferenceableAttr(LLVMContext &C,
537                                                       unsigned Index,
538                                                       uint64_t Bytes) const;
539 
540   /// \brief Add the dereferenceable attribute to the attribute set at the given
541   /// arg index. Returns a new list because attribute lists are immutable.
542   LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
543       LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
544     return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
545   }
546 
547   /// Add the dereferenceable_or_null attribute to the attribute set at
548   /// the given index. Returns a new list because attribute lists are immutable.
549   LLVM_NODISCARD AttributeList addDereferenceableOrNullAttr(
550       LLVMContext &C, unsigned Index, uint64_t Bytes) const;
551 
552   /// Add the dereferenceable_or_null attribute to the attribute set at
553   /// the given arg index. Returns a new list because attribute lists are
554   /// immutable.
555   LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
556       LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
557     return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
558   }
559 
560   /// Add the allocsize attribute to the attribute set at the given index.
561   /// Returns a new list because attribute lists are immutable.
562   LLVM_NODISCARD AttributeList
563   addAllocSizeAttr(LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
564                    const Optional<unsigned> &NumElemsArg);
565 
566   /// Add the allocsize attribute to the attribute set at the given arg index.
567   /// Returns a new list because attribute lists are immutable.
568   LLVM_NODISCARD AttributeList
569   addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
570                         const Optional<unsigned> &NumElemsArg) {
571     return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
572   }
573 
574   //===--------------------------------------------------------------------===//
575   // AttributeList Accessors
576   //===--------------------------------------------------------------------===//
577 
578   /// The attributes for the specified index are returned.
579   AttributeSet getAttributes(unsigned Index) const;
580 
581   /// The attributes for the argument or parameter at the given index are
582   /// returned.
583   AttributeSet getParamAttributes(unsigned ArgNo) const;
584 
585   /// The attributes for the ret value are returned.
586   AttributeSet getRetAttributes() const;
587 
588   /// The function attributes are returned.
589   AttributeSet getFnAttributes() const;
590 
591   /// Return true if the attribute exists at the given index.
592   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
593 
594   /// Return true if the attribute exists at the given index.
595   bool hasAttribute(unsigned Index, StringRef Kind) const;
596 
597   /// Return true if attribute exists at the given index.
598   bool hasAttributes(unsigned Index) const;
599 
600   /// Return true if the attribute exists for the given argument
601   bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
602     return hasAttribute(ArgNo + FirstArgIndex, Kind);
603   }
604 
605   /// Return true if the attribute exists for the given argument
606   bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
607     return hasAttribute(ArgNo + FirstArgIndex, Kind);
608   }
609 
610   /// Return true if attributes exists for the given argument
611   bool hasParamAttrs(unsigned ArgNo) const {
612     return hasAttributes(ArgNo + FirstArgIndex);
613   }
614 
615   /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
616   /// may be faster.
617   bool hasFnAttribute(Attribute::AttrKind Kind) const;
618 
619   /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
620   /// may be faster.
621   bool hasFnAttribute(StringRef Kind) const;
622 
623   /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
624   bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
625 
626   /// Return true if the specified attribute is set for at least one
627   /// parameter or for the return value. If Index is not nullptr, the index
628   /// of a parameter with the specified attribute is provided.
629   bool hasAttrSomewhere(Attribute::AttrKind Kind,
630                         unsigned *Index = nullptr) const;
631 
632   /// Return the attribute object that exists at the given index.
633   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
634 
635   /// Return the attribute object that exists at the given index.
636   Attribute getAttribute(unsigned Index, StringRef Kind) const;
637 
638   /// Return the attribute object that exists at the arg index.
639   Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
640     return getAttribute(ArgNo + FirstArgIndex, Kind);
641   }
642 
643   /// Return the attribute object that exists at the given index.
644   Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
645     return getAttribute(ArgNo + FirstArgIndex, Kind);
646   }
647 
648   /// Return the alignment of the return value.
649   MaybeAlign getRetAlignment() const;
650 
651   /// Return the alignment for the specified function parameter.
652   MaybeAlign getParamAlignment(unsigned ArgNo) const;
653 
654   /// Return the byval type for the specified function parameter.
655   Type *getParamByValType(unsigned ArgNo) const;
656 
657   /// Return the sret type for the specified function parameter.
658   Type *getParamStructRetType(unsigned ArgNo) const;
659 
660   /// Return the byref type for the specified function parameter.
661   Type *getParamByRefType(unsigned ArgNo) const;
662 
663   /// Return the preallocated type for the specified function parameter.
664   Type *getParamPreallocatedType(unsigned ArgNo) const;
665 
666   /// Get the stack alignment.
667   MaybeAlign getStackAlignment(unsigned Index) const;
668 
669   /// Get the number of dereferenceable bytes (or zero if unknown).
670   uint64_t getDereferenceableBytes(unsigned Index) const;
671 
672   /// Get the number of dereferenceable bytes (or zero if unknown) of an
673   /// arg.
674   uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
675     return getDereferenceableBytes(ArgNo + FirstArgIndex);
676   }
677 
678   /// Get the number of dereferenceable_or_null bytes (or zero if
679   /// unknown).
680   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
681 
682   /// Get the number of dereferenceable_or_null bytes (or zero if
683   /// unknown) of an arg.
684   uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
685     return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
686   }
687 
688   /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
689   std::pair<unsigned, Optional<unsigned>>
690   getAllocSizeArgs(unsigned Index) const;
691 
692   /// Return the attributes at the index as a string.
693   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
694 
695   //===--------------------------------------------------------------------===//
696   // AttributeList Introspection
697   //===--------------------------------------------------------------------===//
698 
699   using iterator = const AttributeSet *;
700 
701   iterator begin() const;
702   iterator end() const;
703 
704   unsigned getNumAttrSets() const;
705 
706   /// Use these to iterate over the valid attribute indices.
707   unsigned index_begin() const { return AttributeList::FunctionIndex; }
708   unsigned index_end() const { return getNumAttrSets() - 1; }
709 
710   /// operator==/!= - Provide equality predicates.
711   bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
712   bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
713 
714   /// Return a raw pointer that uniquely identifies this attribute list.
715   void *getRawPointer() const {
716     return pImpl;
717   }
718 
719   /// Return true if there are no attributes.
720   bool isEmpty() const { return pImpl == nullptr; }
721 
722   void dump() const;
723 };
724 
725 //===----------------------------------------------------------------------===//
726 /// \class
727 /// Provide DenseMapInfo for AttributeList.
728 template <> struct DenseMapInfo<AttributeList> {
729   static AttributeList getEmptyKey() {
730     auto Val = static_cast<uintptr_t>(-1);
731     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
732     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
733   }
734 
735   static AttributeList getTombstoneKey() {
736     auto Val = static_cast<uintptr_t>(-2);
737     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
738     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
739   }
740 
741   static unsigned getHashValue(AttributeList AS) {
742     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
743            (unsigned((uintptr_t)AS.pImpl) >> 9);
744   }
745 
746   static bool isEqual(AttributeList LHS, AttributeList RHS) {
747     return LHS == RHS;
748   }
749 };
750 
751 //===----------------------------------------------------------------------===//
752 /// \class
753 /// This class is used in conjunction with the Attribute::get method to
754 /// create an Attribute object. The object itself is uniquified. The Builder's
755 /// value, however, is not. So this can be used as a quick way to test for
756 /// equality, presence of attributes, etc.
757 class AttrBuilder {
758   std::bitset<Attribute::EndAttrKinds> Attrs;
759   std::map<std::string, std::string, std::less<>> TargetDepAttrs;
760   MaybeAlign Alignment;
761   MaybeAlign StackAlignment;
762   uint64_t DerefBytes = 0;
763   uint64_t DerefOrNullBytes = 0;
764   uint64_t AllocSizeArgs = 0;
765   Type *ByValType = nullptr;
766   Type *StructRetType = nullptr;
767   Type *ByRefType = nullptr;
768   Type *PreallocatedType = nullptr;
769 
770 public:
771   AttrBuilder() = default;
772 
773   AttrBuilder(const Attribute &A) {
774     addAttribute(A);
775   }
776 
777   AttrBuilder(AttributeList AS, unsigned Idx);
778   AttrBuilder(AttributeSet AS);
779 
780   void clear();
781 
782   /// Add an attribute to the builder.
783   AttrBuilder &addAttribute(Attribute::AttrKind Val) {
784     assert((unsigned)Val < Attribute::EndAttrKinds &&
785            "Attribute out of range!");
786     assert(!Attribute::doesAttrKindHaveArgument(Val) &&
787            "Adding integer attribute without adding a value!");
788     Attrs[Val] = true;
789     return *this;
790   }
791 
792   /// Add the Attribute object to the builder.
793   AttrBuilder &addAttribute(Attribute A);
794 
795   /// Add the target-dependent attribute to the builder.
796   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
797 
798   /// Remove an attribute from the builder.
799   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
800 
801   /// Remove the attributes from the builder.
802   AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
803 
804   /// Remove the target-dependent attribute to the builder.
805   AttrBuilder &removeAttribute(StringRef A);
806 
807   /// Add the attributes from the builder.
808   AttrBuilder &merge(const AttrBuilder &B);
809 
810   /// Remove the attributes from the builder.
811   AttrBuilder &remove(const AttrBuilder &B);
812 
813   /// Return true if the builder has any attribute that's in the
814   /// specified builder.
815   bool overlaps(const AttrBuilder &B) const;
816 
817   /// Return true if the builder has the specified attribute.
818   bool contains(Attribute::AttrKind A) const {
819     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
820     return Attrs[A];
821   }
822 
823   /// Return true if the builder has the specified target-dependent
824   /// attribute.
825   bool contains(StringRef A) const;
826 
827   /// Return true if the builder has IR-level attributes.
828   bool hasAttributes() const;
829 
830   /// Return true if the builder has any attribute that's in the
831   /// specified attribute.
832   bool hasAttributes(AttributeList A, uint64_t Index) const;
833 
834   /// Return true if the builder has an alignment attribute.
835   bool hasAlignmentAttr() const;
836 
837   /// Retrieve the alignment attribute, if it exists.
838   MaybeAlign getAlignment() const { return Alignment; }
839 
840   /// Retrieve the stack alignment attribute, if it exists.
841   MaybeAlign getStackAlignment() const { return StackAlignment; }
842 
843   /// Retrieve the number of dereferenceable bytes, if the
844   /// dereferenceable attribute exists (zero is returned otherwise).
845   uint64_t getDereferenceableBytes() const { return DerefBytes; }
846 
847   /// Retrieve the number of dereferenceable_or_null bytes, if the
848   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
849   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
850 
851   /// Retrieve the byval type.
852   Type *getByValType() const { return ByValType; }
853 
854   /// Retrieve the sret type.
855   Type *getStructRetType() const { return StructRetType; }
856 
857   /// Retrieve the byref type.
858   Type *getByRefType() const { return ByRefType; }
859 
860   /// Retrieve the preallocated type.
861   Type *getPreallocatedType() const { return PreallocatedType; }
862 
863   /// Retrieve the allocsize args, if the allocsize attribute exists.  If it
864   /// doesn't exist, pair(0, 0) is returned.
865   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
866 
867   /// This turns an alignment into the form used internally in Attribute.
868   /// This call has no effect if Align is not set.
869   AttrBuilder &addAlignmentAttr(MaybeAlign Align);
870 
871   /// This turns an int alignment (which must be a power of 2) into the
872   /// form used internally in Attribute.
873   /// This call has no effect if Align is 0.
874   /// Deprecated, use the version using a MaybeAlign.
875   inline AttrBuilder &addAlignmentAttr(unsigned Align) {
876     return addAlignmentAttr(MaybeAlign(Align));
877   }
878 
879   /// This turns a stack alignment into the form used internally in Attribute.
880   /// This call has no effect if Align is not set.
881   AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
882 
883   /// This turns an int stack alignment (which must be a power of 2) into
884   /// the form used internally in Attribute.
885   /// This call has no effect if Align is 0.
886   /// Deprecated, use the version using a MaybeAlign.
887   inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
888     return addStackAlignmentAttr(MaybeAlign(Align));
889   }
890 
891   /// This turns the number of dereferenceable bytes into the form used
892   /// internally in Attribute.
893   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
894 
895   /// This turns the number of dereferenceable_or_null bytes into the
896   /// form used internally in Attribute.
897   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
898 
899   /// This turns one (or two) ints into the form used internally in Attribute.
900   AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
901                                 const Optional<unsigned> &NumElemsArg);
902 
903   /// This turns a byval type into the form used internally in Attribute.
904   AttrBuilder &addByValAttr(Type *Ty);
905 
906   /// This turns a sret type into the form used internally in Attribute.
907   AttrBuilder &addStructRetAttr(Type *Ty);
908 
909   /// This turns a byref type into the form used internally in Attribute.
910   AttrBuilder &addByRefAttr(Type *Ty);
911 
912   /// This turns a preallocated type into the form used internally in Attribute.
913   AttrBuilder &addPreallocatedAttr(Type *Ty);
914 
915   /// Add an allocsize attribute, using the representation returned by
916   /// Attribute.getIntValue().
917   AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
918 
919   /// Return true if the builder contains no target-independent
920   /// attributes.
921   bool empty() const { return Attrs.none(); }
922 
923   // Iterators for target-dependent attributes.
924   using td_type = std::pair<std::string, std::string>;
925   using td_iterator = decltype(TargetDepAttrs)::iterator;
926   using td_const_iterator = decltype(TargetDepAttrs)::const_iterator;
927   using td_range = iterator_range<td_iterator>;
928   using td_const_range = iterator_range<td_const_iterator>;
929 
930   td_iterator td_begin() { return TargetDepAttrs.begin(); }
931   td_iterator td_end() { return TargetDepAttrs.end(); }
932 
933   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
934   td_const_iterator td_end() const { return TargetDepAttrs.end(); }
935 
936   td_range td_attrs() { return td_range(td_begin(), td_end()); }
937 
938   td_const_range td_attrs() const {
939     return td_const_range(td_begin(), td_end());
940   }
941 
942   bool td_empty() const { return TargetDepAttrs.empty(); }
943 
944   bool operator==(const AttrBuilder &B) const;
945   bool operator!=(const AttrBuilder &B) const { return !(*this == B); }
946 };
947 
948 namespace AttributeFuncs {
949 
950 /// Which attributes cannot be applied to a type.
951 AttrBuilder typeIncompatible(Type *Ty);
952 
953 /// \returns Return true if the two functions have compatible target-independent
954 /// attributes for inlining purposes.
955 bool areInlineCompatible(const Function &Caller, const Function &Callee);
956 
957 
958 /// Checks  if there are any incompatible function attributes between
959 /// \p A and \p B.
960 ///
961 /// \param [in] A - The first function to be compared with.
962 /// \param [in] B - The second function to be compared with.
963 /// \returns true if the functions have compatible attributes.
964 bool areOutlineCompatible(const Function &A, const Function &B);
965 
966 /// Merge caller's and callee's attributes.
967 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
968 
969 /// Merges the functions attributes from \p ToMerge into function \p Base.
970 ///
971 /// \param [in,out] Base - The function being merged into.
972 /// \param [in] ToMerge - The function to merge attributes from.
973 void mergeAttributesForOutlining(Function &Base, const Function &ToMerge);
974 
975 } // end namespace AttributeFuncs
976 
977 } // end namespace llvm
978 
979 #endif // LLVM_IR_ATTRIBUTES_H
980