Move the enum attributes defined in Attributes.h to a table-gen file.
[oota-llvm.git] / 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/Support/Compiler.h"
22 #include "llvm/Support/PointerLikeTypeTraits.h"
23 #include <bitset>
24 #include <cassert>
25 #include <map>
26 #include <string>
27
28 namespace llvm {
29
30 class AttrBuilder;
31 class AttributeImpl;
32 class AttributeSetImpl;
33 class AttributeSetNode;
34 class Constant;
35 template<typename T> struct DenseMapInfo;
36 class LLVMContext;
37 class Type;
38
39 //===----------------------------------------------------------------------===//
40 /// \class
41 /// \brief Functions, function parameters, and return types can have attributes
42 /// to indicate how they should be treated by optimizations and code
43 /// generation. This class represents one of those attributes. It's light-weight
44 /// and should be passed around by-value.
45 class Attribute {
46 public:
47   /// This enumeration lists the attributes that can be associated with
48   /// parameters, function results, or the function itself.
49   ///
50   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
51   /// entry in the unwind table. The `nounwind' attribute is about an exception
52   /// passing by the function.
53   ///
54   /// In a theoretical system that uses tables for profiling and SjLj for
55   /// exceptions, they would be fully independent. In a normal system that uses
56   /// tables for both, the semantics are:
57   ///
58   /// nil                = Needs an entry because an exception might pass by.
59   /// nounwind           = No need for an entry
60   /// uwtable            = Needs an entry because the ABI says so and because
61   ///                      an exception might pass by.
62   /// uwtable + nounwind = Needs an entry because the ABI says so.
63
64   enum AttrKind {
65     // IR-Level Attributes
66     None,                  ///< No attributes have been set
67     #define GET_ATTR_ENUM
68     #include "llvm/IR/Attributes.inc"
69     EndAttrKinds           ///< Sentinal value useful for loops
70   };
71
72 private:
73   AttributeImpl *pImpl;
74   Attribute(AttributeImpl *A) : pImpl(A) {}
75
76 public:
77   Attribute() : pImpl(nullptr) {}
78
79   //===--------------------------------------------------------------------===//
80   // Attribute Construction
81   //===--------------------------------------------------------------------===//
82
83   /// \brief Return a uniquified Attribute object.
84   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
85   static Attribute get(LLVMContext &Context, StringRef Kind,
86                        StringRef Val = StringRef());
87
88   /// \brief Return a uniquified Attribute object that has the specific
89   /// alignment set.
90   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
91   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
92   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
93                                               uint64_t Bytes);
94   static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
95                                                      uint64_t Bytes);
96
97   //===--------------------------------------------------------------------===//
98   // Attribute Accessors
99   //===--------------------------------------------------------------------===//
100
101   /// \brief Return true if the attribute is an Attribute::AttrKind type.
102   bool isEnumAttribute() const;
103
104   /// \brief Return true if the attribute is an integer attribute.
105   bool isIntAttribute() const;
106
107   /// \brief Return true if the attribute is a string (target-dependent)
108   /// attribute.
109   bool isStringAttribute() const;
110
111   /// \brief Return true if the attribute is present.
112   bool hasAttribute(AttrKind Val) const;
113
114   /// \brief Return true if the target-dependent attribute is present.
115   bool hasAttribute(StringRef Val) const;
116
117   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
118   /// requires the attribute to be an enum or alignment attribute.
119   Attribute::AttrKind getKindAsEnum() const;
120
121   /// \brief Return the attribute's value as an integer. This requires that the
122   /// attribute be an alignment attribute.
123   uint64_t getValueAsInt() const;
124
125   /// \brief Return the attribute's kind as a string. This requires the
126   /// attribute to be a string attribute.
127   StringRef getKindAsString() const;
128
129   /// \brief Return the attribute's value as a string. This requires the
130   /// attribute to be a string attribute.
131   StringRef getValueAsString() const;
132
133   /// \brief Returns the alignment field of an attribute as a byte alignment
134   /// value.
135   unsigned getAlignment() const;
136
137   /// \brief Returns the stack alignment field of an attribute as a byte
138   /// alignment value.
139   unsigned getStackAlignment() const;
140
141   /// \brief Returns the number of dereferenceable bytes from the
142   /// dereferenceable attribute (or zero if unknown).
143   uint64_t getDereferenceableBytes() const;
144
145   /// \brief Returns the number of dereferenceable_or_null bytes from the
146   /// dereferenceable_or_null attribute (or zero if unknown).
147   uint64_t getDereferenceableOrNullBytes() const;
148
149   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
150   /// is, presumably, for writing out the mnemonics for the assembly writer.
151   std::string getAsString(bool InAttrGrp = false) const;
152
153   /// \brief Equality and non-equality operators.
154   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
155   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
156
157   /// \brief Less-than operator. Useful for sorting the attributes list.
158   bool operator<(Attribute A) const;
159
160   void Profile(FoldingSetNodeID &ID) const {
161     ID.AddPointer(pImpl);
162   }
163 };
164
165 //===----------------------------------------------------------------------===//
166 /// \class
167 /// \brief This class holds the attributes for a function, its return value, and
168 /// its parameters. You access the attributes for each of them via an index into
169 /// the AttributeSet object. The function attributes are at index
170 /// `AttributeSet::FunctionIndex', the return value is at index
171 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
172 /// index `1'.
173 class AttributeSet {
174 public:
175   enum AttrIndex : unsigned {
176     ReturnIndex = 0U,
177     FunctionIndex = ~0U
178   };
179
180 private:
181   friend class AttrBuilder;
182   friend class AttributeSetImpl;
183   template <typename Ty> friend struct DenseMapInfo;
184
185   /// \brief The attributes that we are managing. This can be null to represent
186   /// the empty attributes list.
187   AttributeSetImpl *pImpl;
188
189   /// \brief The attributes for the specified index are returned.
190   AttributeSetNode *getAttributes(unsigned Index) const;
191
192   /// \brief Create an AttributeSet with the specified parameters in it.
193   static AttributeSet get(LLVMContext &C,
194                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
195   static AttributeSet get(LLVMContext &C,
196                           ArrayRef<std::pair<unsigned,
197                                              AttributeSetNode*> > Attrs);
198
199   static AttributeSet getImpl(LLVMContext &C,
200                               ArrayRef<std::pair<unsigned,
201                                                  AttributeSetNode*> > Attrs);
202
203   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
204
205 public:
206   AttributeSet() : pImpl(nullptr) {}
207
208   //===--------------------------------------------------------------------===//
209   // AttributeSet Construction and Mutation
210   //===--------------------------------------------------------------------===//
211
212   /// \brief Return an AttributeSet with the specified parameters in it.
213   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
214   static AttributeSet get(LLVMContext &C, unsigned Index,
215                           ArrayRef<Attribute::AttrKind> Kind);
216   static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
217
218   /// \brief Add an attribute to the attribute set at the given index. Because
219   /// attribute sets are immutable, this returns a new set.
220   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
221                             Attribute::AttrKind Attr) const;
222
223   /// \brief Add an attribute to the attribute set at the given index. Because
224   /// attribute sets are immutable, this returns a new set.
225   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
226                             StringRef Kind) const;
227   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
228                             StringRef Kind, StringRef Value) const;
229
230   /// \brief Add attributes to the attribute set at the given index. Because
231   /// attribute sets are immutable, this returns a new set.
232   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
233                              AttributeSet Attrs) const;
234
235   /// \brief Remove the specified attribute at the specified index from this
236   /// attribute list. Because attribute lists are immutable, this returns the
237   /// new list.
238   AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
239                                Attribute::AttrKind Attr) const;
240
241   /// \brief Remove the specified attributes at the specified index from this
242   /// attribute list. Because attribute lists are immutable, this returns the
243   /// new list.
244   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
245                                 AttributeSet Attrs) const;
246
247   /// \brief Remove the specified attributes at the specified index from this
248   /// attribute list. Because attribute lists are immutable, this returns the
249   /// new list.
250   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
251                                 const AttrBuilder &Attrs) const;
252
253   /// \brief Add the dereferenceable attribute to the attribute set at the given
254   /// index. Because attribute sets are immutable, this returns a new set.
255   AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
256                                       uint64_t Bytes) const;
257
258   /// \brief Add the dereferenceable_or_null attribute to the attribute set at
259   /// the given index. Because attribute sets are immutable, this returns a new
260   /// set.
261   AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
262                                             uint64_t Bytes) const;
263
264   //===--------------------------------------------------------------------===//
265   // AttributeSet Accessors
266   //===--------------------------------------------------------------------===//
267
268   /// \brief Retrieve the LLVM context.
269   LLVMContext &getContext() const;
270
271   /// \brief The attributes for the specified index are returned.
272   AttributeSet getParamAttributes(unsigned Index) const;
273
274   /// \brief The attributes for the ret value are returned.
275   AttributeSet getRetAttributes() const;
276
277   /// \brief The function attributes are returned.
278   AttributeSet getFnAttributes() const;
279
280   /// \brief Return true if the attribute exists at the given index.
281   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
282
283   /// \brief Return true if the attribute exists at the given index.
284   bool hasAttribute(unsigned Index, StringRef Kind) const;
285
286   /// \brief Return true if attribute exists at the given index.
287   bool hasAttributes(unsigned Index) const;
288
289   /// \brief Return true if the specified attribute is set for at least one
290   /// parameter or for the return value.
291   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
292
293   /// \brief Return the attribute object that exists at the given index.
294   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
295
296   /// \brief Return the attribute object that exists at the given index.
297   Attribute getAttribute(unsigned Index, StringRef Kind) const;
298
299   /// \brief Return the alignment for the specified function parameter.
300   unsigned getParamAlignment(unsigned Index) const;
301
302   /// \brief Get the stack alignment.
303   unsigned getStackAlignment(unsigned Index) const;
304
305   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
306   uint64_t getDereferenceableBytes(unsigned Index) const;
307
308   /// \brief Get the number of dereferenceable_or_null bytes (or zero if
309   /// unknown).
310   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
311
312   /// \brief Return the attributes at the index as a string.
313   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
314
315   typedef ArrayRef<Attribute>::iterator iterator;
316
317   iterator begin(unsigned Slot) const;
318   iterator end(unsigned Slot) const;
319
320   /// operator==/!= - Provide equality predicates.
321   bool operator==(const AttributeSet &RHS) const {
322     return pImpl == RHS.pImpl;
323   }
324   bool operator!=(const AttributeSet &RHS) const {
325     return pImpl != RHS.pImpl;
326   }
327
328   //===--------------------------------------------------------------------===//
329   // AttributeSet Introspection
330   //===--------------------------------------------------------------------===//
331
332   // FIXME: Remove this.
333   uint64_t Raw(unsigned Index) const;
334
335   /// \brief Return a raw pointer that uniquely identifies this attribute list.
336   void *getRawPointer() const {
337     return pImpl;
338   }
339
340   /// \brief Return true if there are no attributes.
341   bool isEmpty() const {
342     return getNumSlots() == 0;
343   }
344
345   /// \brief Return the number of slots used in this attribute list.  This is
346   /// the number of arguments that have an attribute set on them (including the
347   /// function itself).
348   unsigned getNumSlots() const;
349
350   /// \brief Return the index for the given slot.
351   unsigned getSlotIndex(unsigned Slot) const;
352
353   /// \brief Return the attributes at the given slot.
354   AttributeSet getSlotAttributes(unsigned Slot) const;
355
356   void dump() const;
357 };
358
359 //===----------------------------------------------------------------------===//
360 /// \class
361 /// \brief Provide DenseMapInfo for AttributeSet.
362 template<> struct DenseMapInfo<AttributeSet> {
363   static inline AttributeSet getEmptyKey() {
364     uintptr_t Val = static_cast<uintptr_t>(-1);
365     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
366     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
367   }
368   static inline AttributeSet getTombstoneKey() {
369     uintptr_t Val = static_cast<uintptr_t>(-2);
370     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
371     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
372   }
373   static unsigned getHashValue(AttributeSet AS) {
374     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
375            (unsigned((uintptr_t)AS.pImpl) >> 9);
376   }
377   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
378 };
379
380 //===----------------------------------------------------------------------===//
381 /// \class
382 /// \brief This class is used in conjunction with the Attribute::get method to
383 /// create an Attribute object. The object itself is uniquified. The Builder's
384 /// value, however, is not. So this can be used as a quick way to test for
385 /// equality, presence of attributes, etc.
386 class AttrBuilder {
387   std::bitset<Attribute::EndAttrKinds> Attrs;
388   std::map<std::string, std::string> TargetDepAttrs;
389   uint64_t Alignment;
390   uint64_t StackAlignment;
391   uint64_t DerefBytes;
392   uint64_t DerefOrNullBytes;
393
394 public:
395   AttrBuilder()
396       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
397         DerefOrNullBytes(0) {}
398   explicit AttrBuilder(uint64_t Val)
399       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
400         DerefOrNullBytes(0) {
401     addRawValue(Val);
402   }
403   AttrBuilder(const Attribute &A)
404       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
405         DerefOrNullBytes(0) {
406     addAttribute(A);
407   }
408   AttrBuilder(AttributeSet AS, unsigned Idx);
409
410   void clear();
411
412   /// \brief Add an attribute to the builder.
413   AttrBuilder &addAttribute(Attribute::AttrKind Val);
414
415   /// \brief Add the Attribute object to the builder.
416   AttrBuilder &addAttribute(Attribute A);
417
418   /// \brief Add the target-dependent attribute to the builder.
419   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
420
421   /// \brief Remove an attribute from the builder.
422   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
423
424   /// \brief Remove the attributes from the builder.
425   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
426
427   /// \brief Remove the target-dependent attribute to the builder.
428   AttrBuilder &removeAttribute(StringRef A);
429
430   /// \brief Add the attributes from the builder.
431   AttrBuilder &merge(const AttrBuilder &B);
432
433   /// \brief Remove the attributes from the builder.
434   AttrBuilder &remove(const AttrBuilder &B);
435
436   /// \brief Return true if the builder has any attribute that's in the
437   /// specified builder.
438   bool overlaps(const AttrBuilder &B) const;
439
440   /// \brief Return true if the builder has the specified attribute.
441   bool contains(Attribute::AttrKind A) const {
442     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
443     return Attrs[A];
444   }
445
446   /// \brief Return true if the builder has the specified target-dependent
447   /// attribute.
448   bool contains(StringRef A) const;
449
450   /// \brief Return true if the builder has IR-level attributes.
451   bool hasAttributes() const;
452
453   /// \brief Return true if the builder has any attribute that's in the
454   /// specified attribute.
455   bool hasAttributes(AttributeSet A, uint64_t Index) const;
456
457   /// \brief Return true if the builder has an alignment attribute.
458   bool hasAlignmentAttr() const;
459
460   /// \brief Retrieve the alignment attribute, if it exists.
461   uint64_t getAlignment() const { return Alignment; }
462
463   /// \brief Retrieve the stack alignment attribute, if it exists.
464   uint64_t getStackAlignment() const { return StackAlignment; }
465
466   /// \brief Retrieve the number of dereferenceable bytes, if the
467   /// dereferenceable attribute exists (zero is returned otherwise).
468   uint64_t getDereferenceableBytes() const { return DerefBytes; }
469
470   /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
471   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
472   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
473
474   /// \brief This turns an int alignment (which must be a power of 2) into the
475   /// form used internally in Attribute.
476   AttrBuilder &addAlignmentAttr(unsigned Align);
477
478   /// \brief This turns an int stack alignment (which must be a power of 2) into
479   /// the form used internally in Attribute.
480   AttrBuilder &addStackAlignmentAttr(unsigned Align);
481
482   /// \brief This turns the number of dereferenceable bytes into the form used
483   /// internally in Attribute.
484   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
485
486   /// \brief This turns the number of dereferenceable_or_null bytes into the
487   /// form used internally in Attribute.
488   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
489
490   /// \brief Return true if the builder contains no target-independent
491   /// attributes.
492   bool empty() const { return Attrs.none(); }
493
494   // Iterators for target-dependent attributes.
495   typedef std::pair<std::string, std::string>                td_type;
496   typedef std::map<std::string, std::string>::iterator       td_iterator;
497   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
498   typedef llvm::iterator_range<td_iterator>                  td_range;
499   typedef llvm::iterator_range<td_const_iterator>            td_const_range;
500
501   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
502   td_iterator td_end()               { return TargetDepAttrs.end(); }
503
504   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
505   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
506
507   td_range td_attrs() { return td_range(td_begin(), td_end()); }
508   td_const_range td_attrs() const {
509     return td_const_range(td_begin(), td_end());
510   }
511
512   bool td_empty() const              { return TargetDepAttrs.empty(); }
513
514   bool operator==(const AttrBuilder &B);
515   bool operator!=(const AttrBuilder &B) {
516     return !(*this == B);
517   }
518
519   // FIXME: Remove this in 4.0.
520
521   /// \brief Add the raw value to the internal representation.
522   AttrBuilder &addRawValue(uint64_t Val);
523 };
524
525 namespace AttributeFuncs {
526
527 /// \brief Which attributes cannot be applied to a type.
528 AttrBuilder typeIncompatible(Type *Ty);
529
530 } // end AttributeFuncs namespace
531
532 } // end llvm namespace
533
534 #endif