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