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