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