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 getWithPreallocatedType(LLVMContext &Context, Type *Ty);
112 
113   static Attribute::AttrKind getAttrKindFromName(StringRef AttrName);
114 
115   static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
116 
117   /// Return true if and only if the attribute has an Argument.
118   static bool doesAttrKindHaveArgument(Attribute::AttrKind AttrKind);
119 
120   /// Return true if the provided string matches the IR name of an attribute.
121   /// example: "noalias" return true but not "NoAlias"
122   static bool isExistingAttribute(StringRef Name);
123 
124   //===--------------------------------------------------------------------===//
125   // Attribute Accessors
126   //===--------------------------------------------------------------------===//
127 
128   /// Return true if the attribute is an Attribute::AttrKind type.
129   bool isEnumAttribute() const;
130 
131   /// Return true if the attribute is an integer attribute.
132   bool isIntAttribute() const;
133 
134   /// Return true if the attribute is a string (target-dependent)
135   /// attribute.
136   bool isStringAttribute() const;
137 
138   /// Return true if the attribute is a type attribute.
139   bool isTypeAttribute() const;
140 
141   /// Return true if the attribute is present.
142   bool hasAttribute(AttrKind Val) const;
143 
144   /// Return true if the target-dependent attribute is present.
145   bool hasAttribute(StringRef Val) const;
146 
147   /// Return the attribute's kind as an enum (Attribute::AttrKind). This
148   /// requires the attribute to be an enum or integer attribute.
149   Attribute::AttrKind getKindAsEnum() const;
150 
151   /// Return the attribute's value as an integer. This requires that the
152   /// attribute be an integer attribute.
153   uint64_t getValueAsInt() const;
154 
155   /// Return the attribute's kind as a string. This requires the
156   /// attribute to be a string attribute.
157   StringRef getKindAsString() const;
158 
159   /// Return the attribute's value as a string. This requires the
160   /// attribute to be a string attribute.
161   StringRef getValueAsString() const;
162 
163   /// Return the attribute's value as a Type. This requires the attribute to be
164   /// a type attribute.
165   Type *getValueAsType() const;
166 
167   /// Returns the alignment field of an attribute as a byte alignment
168   /// value.
169   MaybeAlign getAlignment() const;
170 
171   /// Returns the stack alignment field of an attribute as a byte
172   /// alignment value.
173   MaybeAlign getStackAlignment() const;
174 
175   /// Returns the number of dereferenceable bytes from the
176   /// dereferenceable attribute.
177   uint64_t getDereferenceableBytes() const;
178 
179   /// Returns the number of dereferenceable_or_null bytes from the
180   /// dereferenceable_or_null attribute.
181   uint64_t getDereferenceableOrNullBytes() const;
182 
183   /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
184   /// if not known).
185   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
186 
187   /// The Attribute is converted to a string of equivalent mnemonic. This
188   /// is, presumably, for writing out the mnemonics for the assembly writer.
189   std::string getAsString(bool InAttrGrp = false) const;
190 
191   /// Equality and non-equality operators.
192   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
193   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
194 
195   /// Less-than operator. Useful for sorting the attributes list.
196   bool operator<(Attribute A) const;
197 
198   void Profile(FoldingSetNodeID &ID) const;
199 
200   /// Return a raw pointer that uniquely identifies this attribute.
201   void *getRawPointer() const {
202     return pImpl;
203   }
204 
205   /// Get an attribute from a raw pointer created by getRawPointer.
206   static Attribute fromRawPointer(void *RawPtr) {
207     return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
208   }
209 };
210 
211 // Specialized opaque value conversions.
212 inline LLVMAttributeRef wrap(Attribute Attr) {
213   return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
214 }
215 
216 // Specialized opaque value conversions.
217 inline Attribute unwrap(LLVMAttributeRef Attr) {
218   return Attribute::fromRawPointer(Attr);
219 }
220 
221 //===----------------------------------------------------------------------===//
222 /// \class
223 /// This class holds the attributes for a particular argument, parameter,
224 /// function, or return value. It is an immutable value type that is cheap to
225 /// copy. Adding and removing enum attributes is intended to be fast, but adding
226 /// and removing string or integer attributes involves a FoldingSet lookup.
227 class AttributeSet {
228   friend AttributeListImpl;
229   template <typename Ty> friend struct DenseMapInfo;
230 
231   // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
232   // This will allow an efficient implementation of addAttribute and
233   // removeAttribute for enum attrs.
234 
235   /// Private implementation pointer.
236   AttributeSetNode *SetNode = nullptr;
237 
238 private:
239   explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
240 
241 public:
242   /// AttributeSet is a trivially copyable value type.
243   AttributeSet() = default;
244   AttributeSet(const AttributeSet &) = default;
245   ~AttributeSet() = default;
246 
247   static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
248   static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
249 
250   bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
251   bool operator!=(const AttributeSet &O) const { return !(*this == O); }
252 
253   /// Add an argument attribute. Returns a new set because attribute sets are
254   /// immutable.
255   LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
256                                            Attribute::AttrKind Kind) const;
257 
258   /// Add a target-dependent attribute. Returns a new set because attribute sets
259   /// are immutable.
260   LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
261                                            StringRef Value = StringRef()) const;
262 
263   /// Add attributes to the attribute set. Returns a new set because attribute
264   /// sets are immutable.
265   LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
266                                             AttributeSet AS) const;
267 
268   /// Remove the specified attribute from this set. Returns a new set because
269   /// attribute sets are immutable.
270   LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
271                                               Attribute::AttrKind Kind) const;
272 
273   /// Remove the specified attribute from this set. Returns a new set because
274   /// attribute sets are immutable.
275   LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
276                                               StringRef Kind) const;
277 
278   /// Remove the specified attributes from this set. Returns a new set because
279   /// attribute sets are immutable.
280   LLVM_NODISCARD AttributeSet
281   removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
282 
283   /// Return the number of attributes in this set.
284   unsigned getNumAttributes() const;
285 
286   /// Return true if attributes exists in this set.
287   bool hasAttributes() const { return SetNode != nullptr; }
288 
289   /// Return true if the attribute exists in this set.
290   bool hasAttribute(Attribute::AttrKind Kind) const;
291 
292   /// Return true if the attribute exists in this set.
293   bool hasAttribute(StringRef Kind) const;
294 
295   /// Return the attribute object.
296   Attribute getAttribute(Attribute::AttrKind Kind) const;
297 
298   /// Return the target-dependent attribute object.
299   Attribute getAttribute(StringRef Kind) const;
300 
301   MaybeAlign getAlignment() const;
302   MaybeAlign getStackAlignment() const;
303   uint64_t getDereferenceableBytes() const;
304   uint64_t getDereferenceableOrNullBytes() const;
305   Type *getByValType() const;
306   Type *getPreallocatedType() const;
307   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
308   std::string getAsString(bool InAttrGrp = false) const;
309 
310   using iterator = const Attribute *;
311 
312   iterator begin() const;
313   iterator end() const;
314 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
315   void dump() const;
316 #endif
317 };
318 
319 //===----------------------------------------------------------------------===//
320 /// \class
321 /// Provide DenseMapInfo for AttributeSet.
322 template <> struct DenseMapInfo<AttributeSet> {
323   static AttributeSet getEmptyKey() {
324     auto Val = static_cast<uintptr_t>(-1);
325     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
326     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
327   }
328 
329   static AttributeSet getTombstoneKey() {
330     auto Val = static_cast<uintptr_t>(-2);
331     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
332     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
333   }
334 
335   static unsigned getHashValue(AttributeSet AS) {
336     return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
337            (unsigned((uintptr_t)AS.SetNode) >> 9);
338   }
339 
340   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
341 };
342 
343 //===----------------------------------------------------------------------===//
344 /// \class
345 /// This class holds the attributes for a function, its return value, and
346 /// its parameters. You access the attributes for each of them via an index into
347 /// the AttributeList object. The function attributes are at index
348 /// `AttributeList::FunctionIndex', the return value is at index
349 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
350 /// index `AttributeList::FirstArgIndex'.
351 class AttributeList {
352 public:
353   enum AttrIndex : unsigned {
354     ReturnIndex = 0U,
355     FunctionIndex = ~0U,
356     FirstArgIndex = 1,
357   };
358 
359 private:
360   friend class AttrBuilder;
361   friend class AttributeListImpl;
362   friend class AttributeSet;
363   friend class AttributeSetNode;
364   template <typename Ty> friend struct DenseMapInfo;
365 
366   /// The attributes that we are managing. This can be null to represent
367   /// the empty attributes list.
368   AttributeListImpl *pImpl = nullptr;
369 
370 public:
371   /// Create an AttributeList with the specified parameters in it.
372   static AttributeList get(LLVMContext &C,
373                            ArrayRef<std::pair<unsigned, Attribute>> Attrs);
374   static AttributeList get(LLVMContext &C,
375                            ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
376 
377   /// Create an AttributeList from attribute sets for a function, its
378   /// return value, and all of its arguments.
379   static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
380                            AttributeSet RetAttrs,
381                            ArrayRef<AttributeSet> ArgAttrs);
382 
383 private:
384   explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
385 
386   static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
387 
388 public:
389   AttributeList() = default;
390 
391   //===--------------------------------------------------------------------===//
392   // AttributeList Construction and Mutation
393   //===--------------------------------------------------------------------===//
394 
395   /// Return an AttributeList with the specified parameters in it.
396   static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
397   static AttributeList get(LLVMContext &C, unsigned Index,
398                            ArrayRef<Attribute::AttrKind> Kinds);
399   static AttributeList get(LLVMContext &C, unsigned Index,
400                            ArrayRef<Attribute::AttrKind> Kinds,
401                            ArrayRef<uint64_t> Values);
402   static AttributeList get(LLVMContext &C, unsigned Index,
403                            ArrayRef<StringRef> Kind);
404   static AttributeList get(LLVMContext &C, unsigned Index,
405                            const AttrBuilder &B);
406 
407   /// Add an attribute to the attribute set at the given index.
408   /// Returns a new list because attribute lists are immutable.
409   LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
410                                             Attribute::AttrKind Kind) const;
411 
412   /// Add an attribute to the attribute set at the given index.
413   /// Returns a new list because attribute lists are immutable.
414   LLVM_NODISCARD AttributeList
415   addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
416                StringRef Value = StringRef()) const;
417 
418   /// Add an attribute to the attribute set at the given index.
419   /// Returns a new list because attribute lists are immutable.
420   LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
421                                             Attribute A) const;
422 
423   /// Add attributes to the attribute set at the given index.
424   /// Returns a new list because attribute lists are immutable.
425   LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
426                                              const AttrBuilder &B) const;
427 
428   /// Add an argument attribute to the list. Returns a new list because
429   /// attribute lists are immutable.
430   LLVM_NODISCARD AttributeList addParamAttribute(
431       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
432     return addAttribute(C, ArgNo + FirstArgIndex, Kind);
433   }
434 
435   /// Add an argument attribute to the list. Returns a new list because
436   /// attribute lists are immutable.
437   LLVM_NODISCARD AttributeList
438   addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
439                     StringRef Value = StringRef()) const {
440     return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
441   }
442 
443   /// Add an attribute to the attribute list at the given arg indices. Returns a
444   /// new list because attribute lists are immutable.
445   LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
446                                                  ArrayRef<unsigned> ArgNos,
447                                                  Attribute A) const;
448 
449   /// Add an argument attribute to the list. Returns a new list because
450   /// attribute lists are immutable.
451   LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
452                                                   unsigned ArgNo,
453                                                   const AttrBuilder &B) const {
454     return addAttributes(C, ArgNo + FirstArgIndex, B);
455   }
456 
457   /// Remove the specified attribute at the specified index from this
458   /// attribute list. Returns a new list because attribute lists are immutable.
459   LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
460                                                Attribute::AttrKind Kind) const;
461 
462   /// Remove the specified attribute at the specified index from this
463   /// attribute list. Returns a new list because attribute lists are immutable.
464   LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
465                                                StringRef Kind) const;
466 
467   /// Remove the specified attributes at the specified index from this
468   /// attribute list. Returns a new list because attribute lists are immutable.
469   LLVM_NODISCARD AttributeList removeAttributes(
470       LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
471 
472   /// Remove all attributes at the specified index from this
473   /// attribute list. Returns a new list because attribute lists are immutable.
474   LLVM_NODISCARD AttributeList removeAttributes(LLVMContext &C,
475                                                 unsigned Index) const;
476 
477   /// Remove the specified attribute at the specified arg index from this
478   /// attribute list. Returns a new list because attribute lists are immutable.
479   LLVM_NODISCARD AttributeList removeParamAttribute(
480       LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
481     return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
482   }
483 
484   /// Remove the specified attribute at the specified arg index from this
485   /// attribute list. Returns a new list because attribute lists are immutable.
486   LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
487                                                     unsigned ArgNo,
488                                                     StringRef Kind) const {
489     return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
490   }
491 
492   /// Remove the specified attribute at the specified arg index from this
493   /// attribute list. Returns a new list because attribute lists are immutable.
494   LLVM_NODISCARD AttributeList removeParamAttributes(
495       LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
496     return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
497   }
498 
499   /// Remove all attributes at the specified arg index from this
500   /// attribute list. Returns a new list because attribute lists are immutable.
501   LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
502                                                      unsigned ArgNo) const {
503     return removeAttributes(C, ArgNo + FirstArgIndex);
504   }
505 
506   /// \brief Add the dereferenceable attribute to the attribute set at the given
507   /// index. Returns a new list because attribute lists are immutable.
508   LLVM_NODISCARD AttributeList addDereferenceableAttr(LLVMContext &C,
509                                                       unsigned Index,
510                                                       uint64_t Bytes) const;
511 
512   /// \brief Add the dereferenceable attribute to the attribute set at the given
513   /// arg index. Returns a new list because attribute lists are immutable.
514   LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
515       LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
516     return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
517   }
518 
519   /// Add the dereferenceable_or_null attribute to the attribute set at
520   /// the given index. Returns a new list because attribute lists are immutable.
521   LLVM_NODISCARD AttributeList addDereferenceableOrNullAttr(
522       LLVMContext &C, unsigned Index, uint64_t Bytes) const;
523 
524   /// Add the dereferenceable_or_null attribute to the attribute set at
525   /// the given arg index. Returns a new list because attribute lists are
526   /// immutable.
527   LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
528       LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
529     return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
530   }
531 
532   /// Add the allocsize attribute to the attribute set at the given index.
533   /// Returns a new list because attribute lists are immutable.
534   LLVM_NODISCARD AttributeList
535   addAllocSizeAttr(LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
536                    const Optional<unsigned> &NumElemsArg);
537 
538   /// Add the allocsize attribute to the attribute set at the given arg index.
539   /// Returns a new list because attribute lists are immutable.
540   LLVM_NODISCARD AttributeList
541   addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
542                         const Optional<unsigned> &NumElemsArg) {
543     return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
544   }
545 
546   //===--------------------------------------------------------------------===//
547   // AttributeList Accessors
548   //===--------------------------------------------------------------------===//
549 
550   /// The attributes for the specified index are returned.
551   AttributeSet getAttributes(unsigned Index) const;
552 
553   /// The attributes for the argument or parameter at the given index are
554   /// returned.
555   AttributeSet getParamAttributes(unsigned ArgNo) const;
556 
557   /// The attributes for the ret value are returned.
558   AttributeSet getRetAttributes() const;
559 
560   /// The function attributes are returned.
561   AttributeSet getFnAttributes() const;
562 
563   /// Return true if the attribute exists at the given index.
564   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
565 
566   /// Return true if the attribute exists at the given index.
567   bool hasAttribute(unsigned Index, StringRef Kind) const;
568 
569   /// Return true if attribute exists at the given index.
570   bool hasAttributes(unsigned Index) const;
571 
572   /// Return true if the attribute exists for the given argument
573   bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
574     return hasAttribute(ArgNo + FirstArgIndex, Kind);
575   }
576 
577   /// Return true if the attribute exists for the given argument
578   bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
579     return hasAttribute(ArgNo + FirstArgIndex, Kind);
580   }
581 
582   /// Return true if attributes exists for the given argument
583   bool hasParamAttrs(unsigned ArgNo) const {
584     return hasAttributes(ArgNo + FirstArgIndex);
585   }
586 
587   /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
588   /// may be faster.
589   bool hasFnAttribute(Attribute::AttrKind Kind) const;
590 
591   /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
592   /// may be faster.
593   bool hasFnAttribute(StringRef Kind) const;
594 
595   /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
596   bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
597 
598   /// Return true if the specified attribute is set for at least one
599   /// parameter or for the return value. If Index is not nullptr, the index
600   /// of a parameter with the specified attribute is provided.
601   bool hasAttrSomewhere(Attribute::AttrKind Kind,
602                         unsigned *Index = nullptr) const;
603 
604   /// Return the attribute object that exists at the given index.
605   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
606 
607   /// Return the attribute object that exists at the given index.
608   Attribute getAttribute(unsigned Index, StringRef Kind) const;
609 
610   /// Return the attribute object that exists at the arg index.
611   Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
612     return getAttribute(ArgNo + FirstArgIndex, Kind);
613   }
614 
615   /// Return the attribute object that exists at the given index.
616   Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
617     return getAttribute(ArgNo + FirstArgIndex, Kind);
618   }
619 
620   /// Return the alignment of the return value.
621   MaybeAlign getRetAlignment() const;
622 
623   /// Return the alignment for the specified function parameter.
624   MaybeAlign getParamAlignment(unsigned ArgNo) const;
625 
626   /// Return the byval type for the specified function parameter.
627   Type *getParamByValType(unsigned ArgNo) const;
628 
629   /// Return the preallocated type for the specified function parameter.
630   Type *getParamPreallocatedType(unsigned ArgNo) const;
631 
632   /// Get the stack alignment.
633   MaybeAlign getStackAlignment(unsigned Index) const;
634 
635   /// Get the number of dereferenceable bytes (or zero if unknown).
636   uint64_t getDereferenceableBytes(unsigned Index) const;
637 
638   /// Get the number of dereferenceable bytes (or zero if unknown) of an
639   /// arg.
640   uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
641     return getDereferenceableBytes(ArgNo + FirstArgIndex);
642   }
643 
644   /// Get the number of dereferenceable_or_null bytes (or zero if
645   /// unknown).
646   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
647 
648   /// Get the number of dereferenceable_or_null bytes (or zero if
649   /// unknown) of an arg.
650   uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
651     return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
652   }
653 
654   /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
655   std::pair<unsigned, Optional<unsigned>>
656   getAllocSizeArgs(unsigned Index) const;
657 
658   /// Return the attributes at the index as a string.
659   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
660 
661   //===--------------------------------------------------------------------===//
662   // AttributeList Introspection
663   //===--------------------------------------------------------------------===//
664 
665   using iterator = const AttributeSet *;
666 
667   iterator begin() const;
668   iterator end() const;
669 
670   unsigned getNumAttrSets() const;
671 
672   /// Use these to iterate over the valid attribute indices.
673   unsigned index_begin() const { return AttributeList::FunctionIndex; }
674   unsigned index_end() const { return getNumAttrSets() - 1; }
675 
676   /// operator==/!= - Provide equality predicates.
677   bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
678   bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
679 
680   /// Return a raw pointer that uniquely identifies this attribute list.
681   void *getRawPointer() const {
682     return pImpl;
683   }
684 
685   /// Return true if there are no attributes.
686   bool isEmpty() const { return pImpl == nullptr; }
687 
688   void dump() const;
689 };
690 
691 //===----------------------------------------------------------------------===//
692 /// \class
693 /// Provide DenseMapInfo for AttributeList.
694 template <> struct DenseMapInfo<AttributeList> {
695   static AttributeList getEmptyKey() {
696     auto Val = static_cast<uintptr_t>(-1);
697     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
698     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
699   }
700 
701   static AttributeList getTombstoneKey() {
702     auto Val = static_cast<uintptr_t>(-2);
703     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
704     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
705   }
706 
707   static unsigned getHashValue(AttributeList AS) {
708     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
709            (unsigned((uintptr_t)AS.pImpl) >> 9);
710   }
711 
712   static bool isEqual(AttributeList LHS, AttributeList RHS) {
713     return LHS == RHS;
714   }
715 };
716 
717 //===----------------------------------------------------------------------===//
718 /// \class
719 /// This class is used in conjunction with the Attribute::get method to
720 /// create an Attribute object. The object itself is uniquified. The Builder's
721 /// value, however, is not. So this can be used as a quick way to test for
722 /// equality, presence of attributes, etc.
723 class AttrBuilder {
724   std::bitset<Attribute::EndAttrKinds> Attrs;
725   std::map<std::string, std::string, std::less<>> TargetDepAttrs;
726   MaybeAlign Alignment;
727   MaybeAlign StackAlignment;
728   uint64_t DerefBytes = 0;
729   uint64_t DerefOrNullBytes = 0;
730   uint64_t AllocSizeArgs = 0;
731   Type *ByValType = nullptr;
732   Type *PreallocatedType = nullptr;
733 
734 public:
735   AttrBuilder() = default;
736 
737   AttrBuilder(const Attribute &A) {
738     addAttribute(A);
739   }
740 
741   AttrBuilder(AttributeList AS, unsigned Idx);
742   AttrBuilder(AttributeSet AS);
743 
744   void clear();
745 
746   /// Add an attribute to the builder.
747   AttrBuilder &addAttribute(Attribute::AttrKind Val);
748 
749   /// Add the Attribute object to the builder.
750   AttrBuilder &addAttribute(Attribute A);
751 
752   /// Add the target-dependent attribute to the builder.
753   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
754 
755   /// Remove an attribute from the builder.
756   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
757 
758   /// Remove the attributes from the builder.
759   AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
760 
761   /// Remove the target-dependent attribute to the builder.
762   AttrBuilder &removeAttribute(StringRef A);
763 
764   /// Add the attributes from the builder.
765   AttrBuilder &merge(const AttrBuilder &B);
766 
767   /// Remove the attributes from the builder.
768   AttrBuilder &remove(const AttrBuilder &B);
769 
770   /// Return true if the builder has any attribute that's in the
771   /// specified builder.
772   bool overlaps(const AttrBuilder &B) const;
773 
774   /// Return true if the builder has the specified attribute.
775   bool contains(Attribute::AttrKind A) const {
776     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
777     return Attrs[A];
778   }
779 
780   /// Return true if the builder has the specified target-dependent
781   /// attribute.
782   bool contains(StringRef A) const;
783 
784   /// Return true if the builder has IR-level attributes.
785   bool hasAttributes() const;
786 
787   /// Return true if the builder has any attribute that's in the
788   /// specified attribute.
789   bool hasAttributes(AttributeList A, uint64_t Index) const;
790 
791   /// Return true if the builder has an alignment attribute.
792   bool hasAlignmentAttr() const;
793 
794   /// Retrieve the alignment attribute, if it exists.
795   MaybeAlign getAlignment() const { return Alignment; }
796 
797   /// Retrieve the stack alignment attribute, if it exists.
798   MaybeAlign getStackAlignment() const { return StackAlignment; }
799 
800   /// Retrieve the number of dereferenceable bytes, if the
801   /// dereferenceable attribute exists (zero is returned otherwise).
802   uint64_t getDereferenceableBytes() const { return DerefBytes; }
803 
804   /// Retrieve the number of dereferenceable_or_null bytes, if the
805   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
806   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
807 
808   /// Retrieve the byval type.
809   Type *getByValType() const { return ByValType; }
810 
811   /// Retrieve the preallocated type.
812   Type *getPreallocatedType() const { return PreallocatedType; }
813 
814   /// Retrieve the allocsize args, if the allocsize attribute exists.  If it
815   /// doesn't exist, pair(0, 0) is returned.
816   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
817 
818   /// This turns an alignment into the form used internally in Attribute.
819   /// This call has no effect if Align is not set.
820   AttrBuilder &addAlignmentAttr(MaybeAlign Align);
821 
822   /// This turns an int alignment (which must be a power of 2) into the
823   /// form used internally in Attribute.
824   /// This call has no effect if Align is 0.
825   /// Deprecated, use the version using a MaybeAlign.
826   inline AttrBuilder &addAlignmentAttr(unsigned Align) {
827     return addAlignmentAttr(MaybeAlign(Align));
828   }
829 
830   /// This turns a stack alignment into the form used internally in Attribute.
831   /// This call has no effect if Align is not set.
832   AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
833 
834   /// This turns an int stack alignment (which must be a power of 2) into
835   /// the form used internally in Attribute.
836   /// This call has no effect if Align is 0.
837   /// Deprecated, use the version using a MaybeAlign.
838   inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
839     return addStackAlignmentAttr(MaybeAlign(Align));
840   }
841 
842   /// This turns the number of dereferenceable bytes into the form used
843   /// internally in Attribute.
844   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
845 
846   /// This turns the number of dereferenceable_or_null bytes into the
847   /// form used internally in Attribute.
848   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
849 
850   /// This turns one (or two) ints into the form used internally in Attribute.
851   AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
852                                 const Optional<unsigned> &NumElemsArg);
853 
854   /// This turns a byval type into the form used internally in Attribute.
855   AttrBuilder &addByValAttr(Type *Ty);
856 
857   /// This turns a preallocated type into the form used internally in Attribute.
858   AttrBuilder &addPreallocatedAttr(Type *Ty);
859 
860   /// Add an allocsize attribute, using the representation returned by
861   /// Attribute.getIntValue().
862   AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
863 
864   /// Return true if the builder contains no target-independent
865   /// attributes.
866   bool empty() const { return Attrs.none(); }
867 
868   // Iterators for target-dependent attributes.
869   using td_type = std::pair<std::string, std::string>;
870   using td_iterator = decltype(TargetDepAttrs)::iterator;
871   using td_const_iterator = decltype(TargetDepAttrs)::const_iterator;
872   using td_range = iterator_range<td_iterator>;
873   using td_const_range = iterator_range<td_const_iterator>;
874 
875   td_iterator td_begin() { return TargetDepAttrs.begin(); }
876   td_iterator td_end() { return TargetDepAttrs.end(); }
877 
878   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
879   td_const_iterator td_end() const { return TargetDepAttrs.end(); }
880 
881   td_range td_attrs() { return td_range(td_begin(), td_end()); }
882 
883   td_const_range td_attrs() const {
884     return td_const_range(td_begin(), td_end());
885   }
886 
887   bool td_empty() const { return TargetDepAttrs.empty(); }
888 
889   bool operator==(const AttrBuilder &B);
890   bool operator!=(const AttrBuilder &B) {
891     return !(*this == B);
892   }
893 };
894 
895 namespace AttributeFuncs {
896 
897 /// Which attributes cannot be applied to a type.
898 AttrBuilder typeIncompatible(Type *Ty);
899 
900 /// \returns Return true if the two functions have compatible target-independent
901 /// attributes for inlining purposes.
902 bool areInlineCompatible(const Function &Caller, const Function &Callee);
903 
904 /// Merge caller's and callee's attributes.
905 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
906 
907 } // end namespace AttributeFuncs
908 
909 } // end namespace llvm
910 
911 #endif // LLVM_IR_ATTRIBUTES_H
912