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