Sort the enums in Attributes.h in case insensitive alphabetical order.
[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     Alignment,             ///< Alignment of parameter (5 bits)
68                            ///< stored as log2 of alignment with +1 bias
69                            ///< 0 means unaligned (different from align(1))
70     AlwaysInline,          ///< inline=always
71     ArgMemOnly,            ///< Funciton can access memory only using pointers
72                            ///< based on its arguments.
73     Builtin,               ///< Callee is recognized as a builtin, despite
74                            ///< nobuiltin attribute on its declaration.
75     ByVal,                 ///< Pass structure by value
76     Cold,                  ///< Marks function as being in a cold path.
77     Convergent,            ///< Can only be moved to control-equivalent blocks
78     Dereferenceable,       ///< Pointer is known to be dereferenceable
79     DereferenceableOrNull, ///< Pointer is either null or dereferenceable
80     InAlloca,              ///< Pass structure in an alloca
81     InlineHint,            ///< Source said inlining was desirable
82     InReg,                 ///< Force argument to be passed in register
83     JumpTable,             ///< Build jump-instruction tables and replace refs.
84     MinSize,               ///< Function must be optimized for size first
85     Naked,                 ///< Naked function
86     Nest,                  ///< Nested function static chain
87     NoAlias,               ///< Considered to not alias after call
88     NoBuiltin,             ///< Callee isn't recognized as a builtin
89     NoCapture,             ///< Function creates no aliases of pointer
90     NoDuplicate,           ///< Call cannot be duplicated
91     NoImplicitFloat,       ///< Disable implicit floating point insts
92     NoInline,              ///< inline=never
93     NonLazyBind,           ///< Function is called early and/or
94                            ///< often, so lazy binding isn't worthwhile
95     NonNull,               ///< Pointer is known to be not null
96     NoRecurse,             ///< The function does not recurse
97     NoRedZone,             ///< Disable redzone
98     NoReturn,              ///< Mark the function as not returning
99     NoUnwind,              ///< Function doesn't unwind stack
100     OptimizeForSize,       ///< opt_size
101     OptimizeNone,          ///< Function must not be optimized.
102     ReadNone,              ///< Function does not access memory
103     ReadOnly,              ///< Function only reads from memory
104     Returned,              ///< Return value is always equal to this argument
105     ReturnsTwice,          ///< Function can return twice
106     SafeStack,             ///< Safe Stack protection.
107     SanitizeAddress,       ///< AddressSanitizer is on.
108     SanitizeMemory,        ///< MemorySanitizer is on.
109     SanitizeThread,        ///< ThreadSanitizer is on.
110     SExt,                  ///< Sign extended before/after call
111     StackAlignment,        ///< Alignment of stack for function (3 bits)
112                            ///< stored as log2 of alignment with +1 bias 0
113                            ///< means unaligned (different from
114                            ///< alignstack=(1))
115     StackProtect,          ///< Stack protection.
116     StackProtectReq,       ///< Stack protection required.
117     StackProtectStrong,    ///< Strong Stack protection.
118     StructRet,             ///< Hidden pointer to structure to return
119     UWTable,               ///< Function must be in a unwind table
120     ZExt,                  ///< Zero extended before/after call
121
122     EndAttrKinds           ///< Sentinal value useful for loops
123   };
124
125 private:
126   AttributeImpl *pImpl;
127   Attribute(AttributeImpl *A) : pImpl(A) {}
128
129 public:
130   Attribute() : pImpl(nullptr) {}
131
132   //===--------------------------------------------------------------------===//
133   // Attribute Construction
134   //===--------------------------------------------------------------------===//
135
136   /// \brief Return a uniquified Attribute object.
137   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
138   static Attribute get(LLVMContext &Context, StringRef Kind,
139                        StringRef Val = StringRef());
140
141   /// \brief Return a uniquified Attribute object that has the specific
142   /// alignment set.
143   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
144   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
145   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
146                                               uint64_t Bytes);
147   static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
148                                                      uint64_t Bytes);
149
150   //===--------------------------------------------------------------------===//
151   // Attribute Accessors
152   //===--------------------------------------------------------------------===//
153
154   /// \brief Return true if the attribute is an Attribute::AttrKind type.
155   bool isEnumAttribute() const;
156
157   /// \brief Return true if the attribute is an integer attribute.
158   bool isIntAttribute() const;
159
160   /// \brief Return true if the attribute is a string (target-dependent)
161   /// attribute.
162   bool isStringAttribute() const;
163
164   /// \brief Return true if the attribute is present.
165   bool hasAttribute(AttrKind Val) const;
166
167   /// \brief Return true if the target-dependent attribute is present.
168   bool hasAttribute(StringRef Val) const;
169
170   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
171   /// requires the attribute to be an enum or alignment attribute.
172   Attribute::AttrKind getKindAsEnum() const;
173
174   /// \brief Return the attribute's value as an integer. This requires that the
175   /// attribute be an alignment attribute.
176   uint64_t getValueAsInt() const;
177
178   /// \brief Return the attribute's kind as a string. This requires the
179   /// attribute to be a string attribute.
180   StringRef getKindAsString() const;
181
182   /// \brief Return the attribute's value as a string. This requires the
183   /// attribute to be a string attribute.
184   StringRef getValueAsString() const;
185
186   /// \brief Returns the alignment field of an attribute as a byte alignment
187   /// value.
188   unsigned getAlignment() const;
189
190   /// \brief Returns the stack alignment field of an attribute as a byte
191   /// alignment value.
192   unsigned getStackAlignment() const;
193
194   /// \brief Returns the number of dereferenceable bytes from the
195   /// dereferenceable attribute (or zero if unknown).
196   uint64_t getDereferenceableBytes() const;
197
198   /// \brief Returns the number of dereferenceable_or_null bytes from the
199   /// dereferenceable_or_null attribute (or zero if unknown).
200   uint64_t getDereferenceableOrNullBytes() const;
201
202   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
203   /// is, presumably, for writing out the mnemonics for the assembly writer.
204   std::string getAsString(bool InAttrGrp = false) const;
205
206   /// \brief Equality and non-equality operators.
207   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
208   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
209
210   /// \brief Less-than operator. Useful for sorting the attributes list.
211   bool operator<(Attribute A) const;
212
213   void Profile(FoldingSetNodeID &ID) const {
214     ID.AddPointer(pImpl);
215   }
216 };
217
218 //===----------------------------------------------------------------------===//
219 /// \class
220 /// \brief This class holds the attributes for a function, its return value, and
221 /// its parameters. You access the attributes for each of them via an index into
222 /// the AttributeSet object. The function attributes are at index
223 /// `AttributeSet::FunctionIndex', the return value is at index
224 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
225 /// index `1'.
226 class AttributeSet {
227 public:
228   enum AttrIndex : unsigned {
229     ReturnIndex = 0U,
230     FunctionIndex = ~0U
231   };
232
233 private:
234   friend class AttrBuilder;
235   friend class AttributeSetImpl;
236   template <typename Ty> friend struct DenseMapInfo;
237
238   /// \brief The attributes that we are managing. This can be null to represent
239   /// the empty attributes list.
240   AttributeSetImpl *pImpl;
241
242   /// \brief The attributes for the specified index are returned.
243   AttributeSetNode *getAttributes(unsigned Index) const;
244
245   /// \brief Create an AttributeSet with the specified parameters in it.
246   static AttributeSet get(LLVMContext &C,
247                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
248   static AttributeSet get(LLVMContext &C,
249                           ArrayRef<std::pair<unsigned,
250                                              AttributeSetNode*> > Attrs);
251
252   static AttributeSet getImpl(LLVMContext &C,
253                               ArrayRef<std::pair<unsigned,
254                                                  AttributeSetNode*> > Attrs);
255
256   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
257
258 public:
259   AttributeSet() : pImpl(nullptr) {}
260
261   //===--------------------------------------------------------------------===//
262   // AttributeSet Construction and Mutation
263   //===--------------------------------------------------------------------===//
264
265   /// \brief Return an AttributeSet with the specified parameters in it.
266   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
267   static AttributeSet get(LLVMContext &C, unsigned Index,
268                           ArrayRef<Attribute::AttrKind> Kind);
269   static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
270
271   /// \brief Add an attribute to the attribute set at the given index. Because
272   /// attribute sets are immutable, this returns a new set.
273   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
274                             Attribute::AttrKind Attr) const;
275
276   /// \brief Add an attribute to the attribute set at the given index. Because
277   /// attribute sets are immutable, this returns a new set.
278   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
279                             StringRef Kind) const;
280   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
281                             StringRef Kind, StringRef Value) const;
282
283   /// \brief Add attributes to the attribute set at the given index. Because
284   /// attribute sets are immutable, this returns a new set.
285   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
286                              AttributeSet Attrs) const;
287
288   /// \brief Remove the specified attribute at the specified index from this
289   /// attribute list. Because attribute lists are immutable, this returns the
290   /// new list.
291   AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
292                                Attribute::AttrKind Attr) const;
293
294   /// \brief Remove the specified attributes at the specified index from this
295   /// attribute list. Because attribute lists are immutable, this returns the
296   /// new list.
297   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
298                                 AttributeSet Attrs) const;
299
300   /// \brief Remove the specified attributes at the specified index from this
301   /// attribute list. Because attribute lists are immutable, this returns the
302   /// new list.
303   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
304                                 const AttrBuilder &Attrs) const;
305
306   /// \brief Add the dereferenceable attribute to the attribute set at the given
307   /// index. Because attribute sets are immutable, this returns a new set.
308   AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
309                                       uint64_t Bytes) const;
310
311   /// \brief Add the dereferenceable_or_null attribute to the attribute set at
312   /// the given index. Because attribute sets are immutable, this returns a new
313   /// set.
314   AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
315                                             uint64_t Bytes) const;
316
317   //===--------------------------------------------------------------------===//
318   // AttributeSet Accessors
319   //===--------------------------------------------------------------------===//
320
321   /// \brief Retrieve the LLVM context.
322   LLVMContext &getContext() const;
323
324   /// \brief The attributes for the specified index are returned.
325   AttributeSet getParamAttributes(unsigned Index) const;
326
327   /// \brief The attributes for the ret value are returned.
328   AttributeSet getRetAttributes() const;
329
330   /// \brief The function attributes are returned.
331   AttributeSet getFnAttributes() const;
332
333   /// \brief Return true if the attribute exists at the given index.
334   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
335
336   /// \brief Return true if the attribute exists at the given index.
337   bool hasAttribute(unsigned Index, StringRef Kind) const;
338
339   /// \brief Return true if attribute exists at the given index.
340   bool hasAttributes(unsigned Index) const;
341
342   /// \brief Return true if the specified attribute is set for at least one
343   /// parameter or for the return value.
344   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
345
346   /// \brief Return the attribute object that exists at the given index.
347   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
348
349   /// \brief Return the attribute object that exists at the given index.
350   Attribute getAttribute(unsigned Index, StringRef Kind) const;
351
352   /// \brief Return the alignment for the specified function parameter.
353   unsigned getParamAlignment(unsigned Index) const;
354
355   /// \brief Get the stack alignment.
356   unsigned getStackAlignment(unsigned Index) const;
357
358   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
359   uint64_t getDereferenceableBytes(unsigned Index) const;
360
361   /// \brief Get the number of dereferenceable_or_null bytes (or zero if
362   /// unknown).
363   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
364
365   /// \brief Return the attributes at the index as a string.
366   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
367
368   typedef ArrayRef<Attribute>::iterator iterator;
369
370   iterator begin(unsigned Slot) const;
371   iterator end(unsigned Slot) const;
372
373   /// operator==/!= - Provide equality predicates.
374   bool operator==(const AttributeSet &RHS) const {
375     return pImpl == RHS.pImpl;
376   }
377   bool operator!=(const AttributeSet &RHS) const {
378     return pImpl != RHS.pImpl;
379   }
380
381   //===--------------------------------------------------------------------===//
382   // AttributeSet Introspection
383   //===--------------------------------------------------------------------===//
384
385   // FIXME: Remove this.
386   uint64_t Raw(unsigned Index) const;
387
388   /// \brief Return a raw pointer that uniquely identifies this attribute list.
389   void *getRawPointer() const {
390     return pImpl;
391   }
392
393   /// \brief Return true if there are no attributes.
394   bool isEmpty() const {
395     return getNumSlots() == 0;
396   }
397
398   /// \brief Return the number of slots used in this attribute list.  This is
399   /// the number of arguments that have an attribute set on them (including the
400   /// function itself).
401   unsigned getNumSlots() const;
402
403   /// \brief Return the index for the given slot.
404   unsigned getSlotIndex(unsigned Slot) const;
405
406   /// \brief Return the attributes at the given slot.
407   AttributeSet getSlotAttributes(unsigned Slot) const;
408
409   void dump() const;
410 };
411
412 //===----------------------------------------------------------------------===//
413 /// \class
414 /// \brief Provide DenseMapInfo for AttributeSet.
415 template<> struct DenseMapInfo<AttributeSet> {
416   static inline AttributeSet getEmptyKey() {
417     uintptr_t Val = static_cast<uintptr_t>(-1);
418     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
419     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
420   }
421   static inline AttributeSet getTombstoneKey() {
422     uintptr_t Val = static_cast<uintptr_t>(-2);
423     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
424     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
425   }
426   static unsigned getHashValue(AttributeSet AS) {
427     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
428            (unsigned((uintptr_t)AS.pImpl) >> 9);
429   }
430   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
431 };
432
433 //===----------------------------------------------------------------------===//
434 /// \class
435 /// \brief This class is used in conjunction with the Attribute::get method to
436 /// create an Attribute object. The object itself is uniquified. The Builder's
437 /// value, however, is not. So this can be used as a quick way to test for
438 /// equality, presence of attributes, etc.
439 class AttrBuilder {
440   std::bitset<Attribute::EndAttrKinds> Attrs;
441   std::map<std::string, std::string> TargetDepAttrs;
442   uint64_t Alignment;
443   uint64_t StackAlignment;
444   uint64_t DerefBytes;
445   uint64_t DerefOrNullBytes;
446
447 public:
448   AttrBuilder()
449       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
450         DerefOrNullBytes(0) {}
451   explicit AttrBuilder(uint64_t Val)
452       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
453         DerefOrNullBytes(0) {
454     addRawValue(Val);
455   }
456   AttrBuilder(const Attribute &A)
457       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
458         DerefOrNullBytes(0) {
459     addAttribute(A);
460   }
461   AttrBuilder(AttributeSet AS, unsigned Idx);
462
463   void clear();
464
465   /// \brief Add an attribute to the builder.
466   AttrBuilder &addAttribute(Attribute::AttrKind Val);
467
468   /// \brief Add the Attribute object to the builder.
469   AttrBuilder &addAttribute(Attribute A);
470
471   /// \brief Add the target-dependent attribute to the builder.
472   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
473
474   /// \brief Remove an attribute from the builder.
475   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
476
477   /// \brief Remove the attributes from the builder.
478   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
479
480   /// \brief Remove the target-dependent attribute to the builder.
481   AttrBuilder &removeAttribute(StringRef A);
482
483   /// \brief Add the attributes from the builder.
484   AttrBuilder &merge(const AttrBuilder &B);
485
486   /// \brief Remove the attributes from the builder.
487   AttrBuilder &remove(const AttrBuilder &B);
488
489   /// \brief Return true if the builder has any attribute that's in the
490   /// specified builder.
491   bool overlaps(const AttrBuilder &B) const;
492
493   /// \brief Return true if the builder has the specified attribute.
494   bool contains(Attribute::AttrKind A) const {
495     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
496     return Attrs[A];
497   }
498
499   /// \brief Return true if the builder has the specified target-dependent
500   /// attribute.
501   bool contains(StringRef A) const;
502
503   /// \brief Return true if the builder has IR-level attributes.
504   bool hasAttributes() const;
505
506   /// \brief Return true if the builder has any attribute that's in the
507   /// specified attribute.
508   bool hasAttributes(AttributeSet A, uint64_t Index) const;
509
510   /// \brief Return true if the builder has an alignment attribute.
511   bool hasAlignmentAttr() const;
512
513   /// \brief Retrieve the alignment attribute, if it exists.
514   uint64_t getAlignment() const { return Alignment; }
515
516   /// \brief Retrieve the stack alignment attribute, if it exists.
517   uint64_t getStackAlignment() const { return StackAlignment; }
518
519   /// \brief Retrieve the number of dereferenceable bytes, if the
520   /// dereferenceable attribute exists (zero is returned otherwise).
521   uint64_t getDereferenceableBytes() const { return DerefBytes; }
522
523   /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
524   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
525   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
526
527   /// \brief This turns an int alignment (which must be a power of 2) into the
528   /// form used internally in Attribute.
529   AttrBuilder &addAlignmentAttr(unsigned Align);
530
531   /// \brief This turns an int stack alignment (which must be a power of 2) into
532   /// the form used internally in Attribute.
533   AttrBuilder &addStackAlignmentAttr(unsigned Align);
534
535   /// \brief This turns the number of dereferenceable bytes into the form used
536   /// internally in Attribute.
537   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
538
539   /// \brief This turns the number of dereferenceable_or_null bytes into the
540   /// form used internally in Attribute.
541   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
542
543   /// \brief Return true if the builder contains no target-independent
544   /// attributes.
545   bool empty() const { return Attrs.none(); }
546
547   // Iterators for target-dependent attributes.
548   typedef std::pair<std::string, std::string>                td_type;
549   typedef std::map<std::string, std::string>::iterator       td_iterator;
550   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
551   typedef llvm::iterator_range<td_iterator>                  td_range;
552   typedef llvm::iterator_range<td_const_iterator>            td_const_range;
553
554   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
555   td_iterator td_end()               { return TargetDepAttrs.end(); }
556
557   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
558   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
559
560   td_range td_attrs() { return td_range(td_begin(), td_end()); }
561   td_const_range td_attrs() const {
562     return td_const_range(td_begin(), td_end());
563   }
564
565   bool td_empty() const              { return TargetDepAttrs.empty(); }
566
567   bool operator==(const AttrBuilder &B);
568   bool operator!=(const AttrBuilder &B) {
569     return !(*this == B);
570   }
571
572   // FIXME: Remove this in 4.0.
573
574   /// \brief Add the raw value to the internal representation.
575   AttrBuilder &addRawValue(uint64_t Val);
576 };
577
578 namespace AttributeFuncs {
579
580 /// \brief Which attributes cannot be applied to a type.
581 AttrBuilder typeIncompatible(Type *Ty);
582
583 } // end AttributeFuncs namespace
584
585 } // end llvm namespace
586
587 #endif