7b17cd271f3082674204f716aba8f2b17980aa28
[oota-llvm.git] / include / llvm / 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 // This file contains the simple types necessary to represent the
11 // attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
17
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include <cassert>
21 #include <string>
22
23 namespace llvm {
24 class Type;
25
26 namespace Attribute {
27 /// We use this proxy POD type to allow constructing Attributes constants
28 /// using initializer lists. Do not use this class directly.
29 struct AttrConst {
30   uint64_t v;
31   AttrConst operator | (const AttrConst Attrs) const {
32     AttrConst Res = {v | Attrs.v};
33     return Res;
34   }
35   AttrConst operator ~ () const {
36     AttrConst Res = {~v};
37     return Res;
38   }
39 };
40 }  // namespace Attribute
41
42
43 /// Attributes - A bitset of attributes.
44 class Attributes {
45  public:
46   Attributes() : Bits(0) { }
47   explicit Attributes(uint64_t Val) : Bits(Val) { }
48   /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) { }
49   Attributes(const Attributes &Attrs) : Bits(Attrs.Bits) { }
50   // This is a "safe bool() operator".
51   operator const void *() const { return Bits ? this : 0; }
52   bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; }
53   Attributes &operator = (const Attributes &Attrs) {
54     Bits = Attrs.Bits;
55     return *this;
56   }
57   bool operator == (const Attributes &Attrs) const {
58     return Bits == Attrs.Bits;
59   }
60   bool operator != (const Attributes &Attrs) const {
61     return Bits != Attrs.Bits;
62   }
63   Attributes operator | (const Attributes &Attrs) const {
64     return Attributes(Bits | Attrs.Bits);
65   }
66   Attributes operator & (const Attributes &Attrs) const {
67     return Attributes(Bits & Attrs.Bits);
68   }
69   Attributes operator ^ (const Attributes &Attrs) const {
70     return Attributes(Bits ^ Attrs.Bits);
71   }
72   Attributes &operator |= (const Attributes &Attrs) {
73     Bits |= Attrs.Bits;
74     return *this;
75   }
76   Attributes &operator &= (const Attributes &Attrs) {
77     Bits &= Attrs.Bits;
78     return *this;
79   }
80   Attributes operator ~ () const { return Attributes(~Bits); }
81   uint64_t Raw() const { return Bits; }
82  private:
83   // Currently, we need less than 64 bits.
84   uint64_t Bits;
85 };
86
87 namespace Attribute {
88
89 /// Function parameters and results can have attributes to indicate how they
90 /// should be treated by optimizations and code generation. This enumeration
91 /// lists the attributes that can be associated with parameters, function
92 /// results or the function itself.
93 /// @brief Function attributes.
94
95 // We declare AttrConst objects that will be used throughout the code
96 // and also raw uint64_t objects with _i suffix to be used below for other
97 // constant declarations. This is done to avoid static CTORs and at the same
98 // time to keep type-safety of Attributes.
99 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
100   const uint64_t name##_i = value; \
101   const AttrConst name = {value};
102
103 DECLARE_LLVM_ATTRIBUTE(None,0)    ///< No attributes have been set
104 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
105 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
106 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
107 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
108 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
109 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
110 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
111 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
112 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
113 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
114 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
115 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
116 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
117 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
118 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
119 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
120 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
121                                      // stored as log2 of alignment with +1 bias
122                                      // 0 means unaligned different from align 1
123 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
124 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
125 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
126                                            /// instructions.
127 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
128 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
129                                            ///desirable
130 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
131                                            ///function (3 bits) stored as log2
132                                            ///of alignment with +1 bias
133                                            ///0 means unaligned (different from
134                                            ///alignstack= {1))
135 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
136 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
137                                            ///table
138 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
139                                             /// often, so lazy binding isn't
140                                             /// worthwhile.
141 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
142
143 #undef DECLARE_LLVM_ATTRIBUTE
144
145 /// Note that uwtable is about the ABI or the user mandating an entry in the
146 /// unwind table. The nounwind attribute is about an exception passing by the
147 /// function.
148 /// In a theoretical system that uses tables for profiling and sjlj for
149 /// exceptions, they would be fully independent. In a normal system that
150 /// uses tables for both, the semantics are:
151 /// nil                = Needs an entry because an exception might pass by.
152 /// nounwind           = No need for an entry
153 /// uwtable            = Needs an entry because the ABI says so and because
154 ///                      an exception might pass by.
155 /// uwtable + nounwind = Needs an entry because the ABI says so.
156
157 /// @brief Attributes that only apply to function parameters.
158 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
159     StructRet_i | NoCapture_i};
160
161 /// @brief Attributes that may be applied to the function itself.  These cannot
162 /// be used on return values or function parameters.
163 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
164   ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
165   StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
166   Naked_i | InlineHint_i | StackAlignment_i |
167   UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
168
169 /// @brief Parameter attributes that do not apply to vararg call arguments.
170 const AttrConst VarArgsIncompatible = {StructRet_i};
171
172 /// @brief Attributes that are mutually incompatible.
173 const AttrConst MutuallyIncompatible[4] = {
174   {ByVal_i | InReg_i | Nest_i | StructRet_i},
175   {ZExt_i  | SExt_i},
176   {ReadNone_i | ReadOnly_i},
177   {NoInline_i | AlwaysInline_i}
178 };
179
180 /// @brief Which attributes cannot be applied to a type.
181 Attributes typeIncompatible(Type *Ty);
182
183 /// This turns an int alignment (a power of 2, normally) into the
184 /// form used internally in Attributes.
185 inline Attributes constructAlignmentFromInt(unsigned i) {
186   // Default alignment, allow the target to define how to align it.
187   if (i == 0)
188     return None;
189
190   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
191   assert(i <= 0x40000000 && "Alignment too large.");
192   return Attributes((Log2_32(i)+1) << 16);
193 }
194
195 /// This returns the alignment field of an attribute as a byte alignment value.
196 inline unsigned getAlignmentFromAttrs(Attributes A) {
197   Attributes Align = A & Attribute::Alignment;
198   if (!Align)
199     return 0;
200
201   return 1U << ((Align.Raw() >> 16) - 1);
202 }
203
204 /// This turns an int stack alignment (which must be a power of 2) into
205 /// the form used internally in Attributes.
206 inline Attributes constructStackAlignmentFromInt(unsigned i) {
207   // Default alignment, allow the target to define how to align it.
208   if (i == 0)
209     return None;
210
211   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
212   assert(i <= 0x100 && "Alignment too large.");
213   return Attributes((Log2_32(i)+1) << 26);
214 }
215
216 /// This returns the stack alignment field of an attribute as a byte alignment
217 /// value.
218 inline unsigned getStackAlignmentFromAttrs(Attributes A) {
219   Attributes StackAlign = A & Attribute::StackAlignment;
220   if (!StackAlign)
221     return 0;
222
223   return 1U << ((StackAlign.Raw() >> 26) - 1);
224 }
225
226
227 /// The set of Attributes set in Attributes is converted to a
228 /// string of equivalent mnemonics. This is, presumably, for writing out
229 /// the mnemonics for the assembly writer.
230 /// @brief Convert attribute bits to text
231 std::string getAsString(Attributes Attrs);
232 } // end namespace Attribute
233
234 /// This is just a pair of values to associate a set of attributes
235 /// with an index.
236 struct AttributeWithIndex {
237   Attributes Attrs; ///< The attributes that are set, or'd together.
238   unsigned Index; ///< Index of the parameter for which the attributes apply.
239                   ///< Index 0 is used for return value attributes.
240                   ///< Index ~0U is used for function attributes.
241
242   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
243     AttributeWithIndex P;
244     P.Index = Idx;
245     P.Attrs = Attrs;
246     return P;
247   }
248 };
249
250 //===----------------------------------------------------------------------===//
251 // AttrListPtr Smart Pointer
252 //===----------------------------------------------------------------------===//
253
254 class AttributeListImpl;
255
256 /// AttrListPtr - This class manages the ref count for the opaque
257 /// AttributeListImpl object and provides accessors for it.
258 class AttrListPtr {
259   /// AttrList - The attributes that we are managing.  This can be null
260   /// to represent the empty attributes list.
261   AttributeListImpl *AttrList;
262 public:
263   AttrListPtr() : AttrList(0) {}
264   AttrListPtr(const AttrListPtr &P);
265   const AttrListPtr &operator=(const AttrListPtr &RHS);
266   ~AttrListPtr();
267
268   //===--------------------------------------------------------------------===//
269   // Attribute List Construction and Mutation
270   //===--------------------------------------------------------------------===//
271
272   /// get - Return a Attributes list with the specified parameters in it.
273   static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
274
275   /// addAttr - Add the specified attribute at the specified index to this
276   /// attribute list.  Since attribute lists are immutable, this
277   /// returns the new list.
278   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
279
280   /// removeAttr - Remove the specified attribute at the specified index from
281   /// this attribute list.  Since attribute lists are immutable, this
282   /// returns the new list.
283   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
284
285   //===--------------------------------------------------------------------===//
286   // Attribute List Accessors
287   //===--------------------------------------------------------------------===//
288   /// getParamAttributes - The attributes for the specified index are
289   /// returned.
290   Attributes getParamAttributes(unsigned Idx) const {
291     assert (Idx && Idx != ~0U && "Invalid parameter index!");
292     return getAttributes(Idx);
293   }
294
295   /// getRetAttributes - The attributes for the ret value are
296   /// returned.
297   Attributes getRetAttributes() const {
298     return getAttributes(0);
299   }
300
301   /// getFnAttributes - The function attributes are returned.
302   Attributes getFnAttributes() const {
303     return getAttributes(~0U);
304   }
305
306   /// paramHasAttr - Return true if the specified parameter index has the
307   /// specified attribute set.
308   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
309     return getAttributes(Idx) & Attr;
310   }
311
312   /// getParamAlignment - Return the alignment for the specified function
313   /// parameter.
314   unsigned getParamAlignment(unsigned Idx) const {
315     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
316   }
317
318   /// hasAttrSomewhere - Return true if the specified attribute is set for at
319   /// least one parameter or for the return value.
320   bool hasAttrSomewhere(Attributes Attr) const;
321
322   /// operator==/!= - Provide equality predicates.
323   bool operator==(const AttrListPtr &RHS) const
324   { return AttrList == RHS.AttrList; }
325   bool operator!=(const AttrListPtr &RHS) const
326   { return AttrList != RHS.AttrList; }
327
328   void dump() const;
329
330   //===--------------------------------------------------------------------===//
331   // Attribute List Introspection
332   //===--------------------------------------------------------------------===//
333
334   /// getRawPointer - Return a raw pointer that uniquely identifies this
335   /// attribute list.
336   void *getRawPointer() const {
337     return AttrList;
338   }
339
340   // Attributes are stored as a dense set of slots, where there is one
341   // slot for each argument that has an attribute.  This allows walking over the
342   // dense set instead of walking the sparse list of attributes.
343
344   /// isEmpty - Return true if there are no attributes.
345   ///
346   bool isEmpty() const {
347     return AttrList == 0;
348   }
349
350   /// getNumSlots - Return the number of slots used in this attribute list.
351   /// This is the number of arguments that have an attribute set on them
352   /// (including the function itself).
353   unsigned getNumSlots() const;
354
355   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
356   /// holds a index number plus a set of attributes.
357   const AttributeWithIndex &getSlot(unsigned Slot) const;
358
359 private:
360   explicit AttrListPtr(AttributeListImpl *L);
361
362   /// getAttributes - The attributes for the specified index are
363   /// returned.  Attributes for the result are denoted with Idx = 0.
364   Attributes getAttributes(unsigned Idx) const;
365
366 };
367
368 } // End llvm namespace
369
370 #endif