]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/Attributes.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, 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/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/PointerLikeTypeTraits.h"
25 #include "llvm-c/Types.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. Because
232   /// attribute sets are immutable, this returns a new set.
233   AttributeSet addAttribute(LLVMContext &C,
234                             Attribute::AttrKind Kind) const;
235
236   /// Add a target-dependent attribute. Because
237   /// attribute sets are immutable, this returns a new set.
238   AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
239                             StringRef Value = StringRef()) const;
240
241   /// Add attributes to the attribute set. Because
242   /// attribute sets are immutable, this returns a new set.
243   AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const;
244
245   /// Remove the specified attribute from this set. Because
246   /// attribute sets are immutable, this returns a new set.
247   AttributeSet removeAttribute(LLVMContext &C,
248                                 Attribute::AttrKind Kind) const;
249
250   /// Remove the specified attribute from this set. Because
251   /// attribute sets are immutable, this returns a new set.
252   AttributeSet removeAttribute(LLVMContext &C,
253                                 StringRef Kind) const;
254
255   /// Remove the specified attributes from this set. Because
256   /// attribute sets are immutable, this returns a new set.
257   AttributeSet removeAttributes(LLVMContext &C,
258                                  const AttrBuilder &AttrsToRemove) const;
259
260   /// Return the number of attributes in this set.
261   unsigned getNumAttributes() const;
262
263   /// Return true if attributes exists in this set.
264   bool hasAttributes() const { return SetNode != nullptr; }
265
266   /// Return true if the attribute exists in this set.
267   bool hasAttribute(Attribute::AttrKind Kind) const;
268
269   /// Return true if the attribute exists in this set.
270   bool hasAttribute(StringRef Kind) const;
271
272   /// Return the attribute object.
273   Attribute getAttribute(Attribute::AttrKind Kind) const;
274
275   /// Return the target-dependent attribute object.
276   Attribute getAttribute(StringRef Kind) const;
277
278   unsigned getAlignment() const;
279   unsigned getStackAlignment() const;
280   uint64_t getDereferenceableBytes() const;
281   uint64_t getDereferenceableOrNullBytes() const;
282   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
283   std::string getAsString(bool InAttrGrp = false) const;
284
285   using iterator = const Attribute *;
286
287   iterator begin() const;
288   iterator end() const;
289 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
290   void dump() const;
291 #endif
292 };
293
294 //===----------------------------------------------------------------------===//
295 /// \class
296 /// \brief Provide DenseMapInfo for AttributeSet.
297 template <> struct DenseMapInfo<AttributeSet> {
298   static inline AttributeSet getEmptyKey() {
299     uintptr_t Val = static_cast<uintptr_t>(-1);
300     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
301     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
302   }
303
304   static inline AttributeSet getTombstoneKey() {
305     uintptr_t Val = static_cast<uintptr_t>(-2);
306     Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
307     return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
308   }
309
310   static unsigned getHashValue(AttributeSet AS) {
311     return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
312            (unsigned((uintptr_t)AS.SetNode) >> 9);
313   }
314
315   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
316 };
317
318 //===----------------------------------------------------------------------===//
319 /// \class
320 /// \brief This class holds the attributes for a function, its return value, and
321 /// its parameters. You access the attributes for each of them via an index into
322 /// the AttributeList object. The function attributes are at index
323 /// `AttributeList::FunctionIndex', the return value is at index
324 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
325 /// index `AttributeList::FirstArgIndex'.
326 class AttributeList {
327 public:
328   enum AttrIndex : unsigned {
329     ReturnIndex = 0U,
330     FunctionIndex = ~0U,
331     FirstArgIndex = 1,
332   };
333
334 private:
335   friend class AttrBuilder;
336   friend class AttributeListImpl;
337   friend class AttributeSet;
338   friend class AttributeSetNode;
339
340   template <typename Ty> friend struct DenseMapInfo;
341
342   /// \brief The attributes that we are managing. This can be null to represent
343   /// the empty attributes list.
344   AttributeListImpl *pImpl = nullptr;
345
346 public:
347   /// \brief Create an AttributeList with the specified parameters in it.
348   static AttributeList get(LLVMContext &C,
349                            ArrayRef<std::pair<unsigned, Attribute>> Attrs);
350   static AttributeList get(LLVMContext &C,
351                            ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
352
353   /// \brief Create an AttributeList from attribute sets for a function, its
354   /// return value, and all of its arguments.
355   static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
356                            AttributeSet RetAttrs,
357                            ArrayRef<AttributeSet> ArgAttrs);
358
359 private:
360   explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
361
362   static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
363
364 public:
365   AttributeList() = default;
366
367   //===--------------------------------------------------------------------===//
368   // AttributeList Construction and Mutation
369   //===--------------------------------------------------------------------===//
370
371   /// \brief Return an AttributeList with the specified parameters in it.
372   static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
373   static AttributeList get(LLVMContext &C, unsigned Index,
374                            ArrayRef<Attribute::AttrKind> Kinds);
375   static AttributeList get(LLVMContext &C, unsigned Index,
376                            ArrayRef<StringRef> Kind);
377   static AttributeList get(LLVMContext &C, unsigned Index,
378                            const AttrBuilder &B);
379
380   /// Add an argument attribute to the list. Returns a new list because
381   /// attribute lists are immutable.
382   AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
383                                   Attribute::AttrKind Kind) const {
384     return addAttribute(C, ArgNo + FirstArgIndex, Kind);
385   }
386
387   /// \brief Add an attribute to the attribute set at the given index. Because
388   /// attribute sets are immutable, this returns a new set.
389   AttributeList addAttribute(LLVMContext &C, unsigned Index,
390                              Attribute::AttrKind Kind) const;
391
392   /// \brief Add an attribute to the attribute set at the given index. Because
393   /// attribute sets are immutable, this returns a new set.
394   AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
395                              StringRef Value = StringRef()) const;
396
397   /// Add an attribute to the attribute set at the given indices. Because
398   /// attribute sets are immutable, this returns a new set.
399   AttributeList addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices,
400                              Attribute A) const;
401
402   /// \brief Add attributes to the attribute set at the given index. Because
403   /// attribute sets are immutable, this returns a new set.
404   AttributeList addAttributes(LLVMContext &C, unsigned Index,
405                               const AttrBuilder &B) const;
406
407   /// \brief Remove the specified attribute at the specified index from this
408   /// attribute list. Because attribute lists are immutable, this returns the
409   /// new list.
410   AttributeList removeAttribute(LLVMContext &C, unsigned Index,
411                                 Attribute::AttrKind Kind) const;
412
413   /// \brief Remove the specified attribute at the specified index from this
414   /// attribute list. Because attribute lists are immutable, this returns the
415   /// new list.
416   AttributeList removeAttribute(LLVMContext &C, unsigned Index,
417                                 StringRef Kind) const;
418
419   /// \brief Remove the specified attributes at the specified index from this
420   /// attribute list. Because attribute lists are immutable, this returns the
421   /// new list.
422   AttributeList removeAttributes(LLVMContext &C, unsigned Index,
423                                  const AttrBuilder &AttrsToRemove) const;
424
425   /// \brief Remove all attributes at the specified index from this
426   /// attribute list. Because attribute lists are immutable, this returns the
427   /// new list.
428   AttributeList removeAttributes(LLVMContext &C, unsigned Index) const;
429
430   /// \brief Add the dereferenceable attribute to the attribute set at the given
431   /// index. Because attribute sets are immutable, this returns a new set.
432   AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index,
433                                        uint64_t Bytes) const;
434
435   /// \brief Add the dereferenceable_or_null attribute to the attribute set at
436   /// the given index. Because attribute sets are immutable, this returns a new
437   /// set.
438   AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
439                                              uint64_t Bytes) const;
440
441   /// Add the allocsize attribute to the attribute set at the given index.
442   /// Because attribute sets are immutable, this returns a new set.
443   AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index,
444                                  unsigned ElemSizeArg,
445                                  const Optional<unsigned> &NumElemsArg);
446
447   //===--------------------------------------------------------------------===//
448   // AttributeList Accessors
449   //===--------------------------------------------------------------------===//
450
451   /// \brief Retrieve the LLVM context.
452   LLVMContext &getContext() const;
453
454   /// \brief The attributes for the specified index are returned.
455   AttributeSet getAttributes(unsigned Index) const;
456
457   /// \brief The attributes for the argument or parameter at the given index are
458   /// returned.
459   AttributeSet getParamAttributes(unsigned ArgNo) const;
460
461   /// \brief The attributes for the ret value are returned.
462   AttributeSet getRetAttributes() const;
463
464   /// \brief The function attributes are returned.
465   AttributeSet getFnAttributes() const;
466
467   /// \brief Return true if the attribute exists at the given index.
468   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
469
470   /// \brief Return true if the attribute exists at the given index.
471   bool hasAttribute(unsigned Index, StringRef Kind) const;
472
473   /// \brief Return true if attribute exists at the given index.
474   bool hasAttributes(unsigned Index) const;
475
476   /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
477   /// may be faster.
478   bool hasFnAttribute(Attribute::AttrKind Kind) const;
479
480   /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
481   /// may be faster.
482   bool hasFnAttribute(StringRef Kind) const;
483
484   /// \brief Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
485   bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
486
487   /// \brief Return true if the specified attribute is set for at least one
488   /// parameter or for the return value. If Index is not nullptr, the index
489   /// of a parameter with the specified attribute is provided.
490   bool hasAttrSomewhere(Attribute::AttrKind Kind,
491                         unsigned *Index = nullptr) const;
492
493   /// \brief Return the attribute object that exists at the given index.
494   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
495
496   /// \brief Return the attribute object that exists at the given index.
497   Attribute getAttribute(unsigned Index, StringRef Kind) const;
498
499   /// \brief Return the alignment of the return value.
500   unsigned getRetAlignment() const;
501
502   /// \brief Return the alignment for the specified function parameter.
503   unsigned getParamAlignment(unsigned ArgNo) const;
504
505   /// \brief Get the stack alignment.
506   unsigned getStackAlignment(unsigned Index) const;
507
508   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
509   uint64_t getDereferenceableBytes(unsigned Index) const;
510
511   /// \brief Get the number of dereferenceable_or_null bytes (or zero if
512   /// unknown).
513   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
514
515   /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
516   std::pair<unsigned, Optional<unsigned>>
517   getAllocSizeArgs(unsigned Index) const;
518
519   /// \brief Return the attributes at the index as a string.
520   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
521
522   //===--------------------------------------------------------------------===//
523   // AttributeList Introspection
524   //===--------------------------------------------------------------------===//
525
526   typedef const AttributeSet *iterator;
527   iterator begin() const;
528   iterator end() const;
529
530   unsigned getNumAttrSets() const;
531
532   /// Use these to iterate over the valid attribute indices.
533   unsigned index_begin() const { return AttributeList::FunctionIndex; }
534   unsigned index_end() const { return getNumAttrSets() - 1; }
535
536   /// operator==/!= - Provide equality predicates.
537   bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
538   bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
539
540   /// \brief Return a raw pointer that uniquely identifies this attribute list.
541   void *getRawPointer() const {
542     return pImpl;
543   }
544
545   /// \brief Return true if there are no attributes.
546   bool isEmpty() const { return pImpl == nullptr; }
547
548   void dump() const;
549 };
550
551 //===----------------------------------------------------------------------===//
552 /// \class
553 /// \brief Provide DenseMapInfo for AttributeList.
554 template <> struct DenseMapInfo<AttributeList> {
555   static inline AttributeList getEmptyKey() {
556     uintptr_t Val = static_cast<uintptr_t>(-1);
557     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
558     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
559   }
560
561   static inline AttributeList getTombstoneKey() {
562     uintptr_t Val = static_cast<uintptr_t>(-2);
563     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
564     return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
565   }
566
567   static unsigned getHashValue(AttributeList AS) {
568     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
569            (unsigned((uintptr_t)AS.pImpl) >> 9);
570   }
571
572   static bool isEqual(AttributeList LHS, AttributeList RHS) {
573     return LHS == RHS;
574   }
575 };
576
577 //===----------------------------------------------------------------------===//
578 /// \class
579 /// \brief This class is used in conjunction with the Attribute::get method to
580 /// create an Attribute object. The object itself is uniquified. The Builder's
581 /// value, however, is not. So this can be used as a quick way to test for
582 /// equality, presence of attributes, etc.
583 class AttrBuilder {
584   std::bitset<Attribute::EndAttrKinds> Attrs;
585   std::map<std::string, std::string> TargetDepAttrs;
586   uint64_t Alignment = 0;
587   uint64_t StackAlignment = 0;
588   uint64_t DerefBytes = 0;
589   uint64_t DerefOrNullBytes = 0;
590   uint64_t AllocSizeArgs = 0;
591
592 public:
593   AttrBuilder() = default;
594   AttrBuilder(const Attribute &A) {
595     addAttribute(A);
596   }
597   AttrBuilder(AttributeList AS, unsigned Idx);
598   AttrBuilder(AttributeSet AS);
599
600   void clear();
601
602   /// \brief Add an attribute to the builder.
603   AttrBuilder &addAttribute(Attribute::AttrKind Val);
604
605   /// \brief Add the Attribute object to the builder.
606   AttrBuilder &addAttribute(Attribute A);
607
608   /// \brief Add the target-dependent attribute to the builder.
609   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
610
611   /// \brief Remove an attribute from the builder.
612   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
613
614   /// \brief Remove the attributes from the builder.
615   AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
616
617   /// \brief Remove the target-dependent attribute to the builder.
618   AttrBuilder &removeAttribute(StringRef A);
619
620   /// \brief Add the attributes from the builder.
621   AttrBuilder &merge(const AttrBuilder &B);
622
623   /// \brief Remove the attributes from the builder.
624   AttrBuilder &remove(const AttrBuilder &B);
625
626   /// \brief Return true if the builder has any attribute that's in the
627   /// specified builder.
628   bool overlaps(const AttrBuilder &B) const;
629
630   /// \brief Return true if the builder has the specified attribute.
631   bool contains(Attribute::AttrKind A) const {
632     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
633     return Attrs[A];
634   }
635
636   /// \brief Return true if the builder has the specified target-dependent
637   /// attribute.
638   bool contains(StringRef A) const;
639
640   /// \brief Return true if the builder has IR-level attributes.
641   bool hasAttributes() const;
642
643   /// \brief Return true if the builder has any attribute that's in the
644   /// specified attribute.
645   bool hasAttributes(AttributeList A, uint64_t Index) const;
646
647   /// \brief Return true if the builder has an alignment attribute.
648   bool hasAlignmentAttr() const;
649
650   /// \brief Retrieve the alignment attribute, if it exists.
651   uint64_t getAlignment() const { return Alignment; }
652
653   /// \brief Retrieve the stack alignment attribute, if it exists.
654   uint64_t getStackAlignment() const { return StackAlignment; }
655
656   /// \brief Retrieve the number of dereferenceable bytes, if the
657   /// dereferenceable attribute exists (zero is returned otherwise).
658   uint64_t getDereferenceableBytes() const { return DerefBytes; }
659
660   /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
661   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
662   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
663
664   /// Retrieve the allocsize args, if the allocsize attribute exists.  If it
665   /// doesn't exist, pair(0, 0) is returned.
666   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
667
668   /// \brief This turns an int alignment (which must be a power of 2) into the
669   /// form used internally in Attribute.
670   AttrBuilder &addAlignmentAttr(unsigned Align);
671
672   /// \brief This turns an int stack alignment (which must be a power of 2) into
673   /// the form used internally in Attribute.
674   AttrBuilder &addStackAlignmentAttr(unsigned Align);
675
676   /// \brief This turns the number of dereferenceable bytes into the form used
677   /// internally in Attribute.
678   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
679
680   /// \brief This turns the number of dereferenceable_or_null bytes into the
681   /// form used internally in Attribute.
682   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
683
684   /// This turns one (or two) ints into the form used internally in Attribute.
685   AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
686                                 const Optional<unsigned> &NumElemsArg);
687
688   /// Add an allocsize attribute, using the representation returned by
689   /// Attribute.getIntValue().
690   AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
691
692   /// \brief Return true if the builder contains no target-independent
693   /// attributes.
694   bool empty() const { return Attrs.none(); }
695
696   // Iterators for target-dependent attributes.
697   using td_type = std::pair<std::string, std::string>;
698   using td_iterator = std::map<std::string, std::string>::iterator;
699   using td_const_iterator = std::map<std::string, std::string>::const_iterator;
700   using td_range = iterator_range<td_iterator>;
701   using td_const_range = iterator_range<td_const_iterator>;
702
703   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
704   td_iterator td_end()               { return TargetDepAttrs.end(); }
705
706   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
707   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
708
709   td_range td_attrs() { return td_range(td_begin(), td_end()); }
710   td_const_range td_attrs() const {
711     return td_const_range(td_begin(), td_end());
712   }
713
714   bool td_empty() const              { return TargetDepAttrs.empty(); }
715
716   bool operator==(const AttrBuilder &B);
717   bool operator!=(const AttrBuilder &B) {
718     return !(*this == B);
719   }
720 };
721
722 namespace AttributeFuncs {
723
724 /// \brief Which attributes cannot be applied to a type.
725 AttrBuilder typeIncompatible(Type *Ty);
726
727 /// \returns Return true if the two functions have compatible target-independent
728 /// attributes for inlining purposes.
729 bool areInlineCompatible(const Function &Caller, const Function &Callee);
730
731 /// \brief Merge caller's and callee's attributes.
732 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
733
734 } // end AttributeFuncs namespace
735
736 } // end llvm namespace
737
738 #endif // LLVM_IR_ATTRIBUTES_H