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