Make IntInits and ListInits typed. This helps deduce types of !if and
[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
61 /// @brief Attributes that only apply to function parameters.
62 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
63
64 /// @brief Attributes that only apply to function.
65 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly | 
66   NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
67   NoRedZone | NoImplicitFloat;
68
69 /// @brief Parameter attributes that do not apply to vararg call arguments.
70 const Attributes VarArgsIncompatible = StructRet;
71
72 /// @brief Attributes that are mutually incompatible.
73 const Attributes MutuallyIncompatible[4] = {
74   ByVal | InReg | Nest | StructRet,
75   ZExt  | SExt,
76   ReadNone | ReadOnly,
77   NoInline | AlwaysInline
78 };
79
80 /// @brief Which attributes cannot be applied to a type.
81 Attributes typeIncompatible(const Type *Ty);
82
83 /// This turns an int alignment (a power of 2, normally) into the
84 /// form used internally in Attributes.
85 inline Attributes constructAlignmentFromInt(unsigned i) {
86   // Default alignment, allow the target to define how to align it.
87   if (i == 0)
88     return 0;
89
90   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
91   assert(i <= 0x40000000 && "Alignment too large.");
92   return (Log2_32(i)+1) << 16;
93 }
94
95 /// This returns the alignment field of an attribute as a byte alignment value.
96 inline unsigned getAlignmentFromAttrs(Attributes A) {
97   Attributes Align = A & Attribute::Alignment;
98   if (Align == 0)
99     return 0;
100   
101   return 1U << ((Align >> 16) - 1);
102 }
103   
104   
105 /// The set of Attributes set in Attributes is converted to a
106 /// string of equivalent mnemonics. This is, presumably, for writing out
107 /// the mnemonics for the assembly writer. 
108 /// @brief Convert attribute bits to text
109 std::string getAsString(Attributes Attrs);
110 } // end namespace Attribute
111
112 /// This is just a pair of values to associate a set of attributes
113 /// with an index. 
114 struct AttributeWithIndex {
115   Attributes Attrs; ///< The attributes that are set, or'd together.
116   unsigned Index; ///< Index of the parameter for which the attributes apply.
117                   ///< Index 0 is used for return value attributes.
118                   ///< Index ~0U is used for function attributes.
119   
120   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
121     AttributeWithIndex P;
122     P.Index = Idx;
123     P.Attrs = Attrs;
124     return P;
125   }
126 };
127   
128 //===----------------------------------------------------------------------===//
129 // AttrListPtr Smart Pointer
130 //===----------------------------------------------------------------------===//
131
132 class AttributeListImpl;
133   
134 /// AttrListPtr - This class manages the ref count for the opaque 
135 /// AttributeListImpl object and provides accessors for it.
136 class AttrListPtr {
137   /// AttrList - The attributes that we are managing.  This can be null
138   /// to represent the empty attributes list.
139   AttributeListImpl *AttrList;
140 public:
141   AttrListPtr() : AttrList(0) {}
142   AttrListPtr(const AttrListPtr &P);
143   const AttrListPtr &operator=(const AttrListPtr &RHS);
144   ~AttrListPtr();
145   
146   //===--------------------------------------------------------------------===//
147   // Attribute List Construction and Mutation
148   //===--------------------------------------------------------------------===//
149   
150   /// get - Return a Attributes list with the specified parameter in it.
151   static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
152   
153   /// get - Return a Attribute list with the parameters specified by the
154   /// consecutive random access iterator range.
155   template <typename Iter>
156   static AttrListPtr get(const Iter &I, const Iter &E) {
157     if (I == E) return AttrListPtr();  // Empty list.
158     return get(&*I, static_cast<unsigned>(E-I));
159   }
160
161   /// addAttr - Add the specified attribute at the specified index to this
162   /// attribute list.  Since attribute lists are immutable, this
163   /// returns the new list.
164   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
165   
166   /// removeAttr - Remove the specified attribute at the specified index from
167   /// this attribute list.  Since attribute lists are immutable, this
168   /// returns the new list.
169   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
170   
171   //===--------------------------------------------------------------------===//
172   // Attribute List Accessors
173   //===--------------------------------------------------------------------===//
174   /// getParamAttributes - The attributes for the specified index are
175   /// returned. 
176   Attributes getParamAttributes(unsigned Idx) const {
177     assert (Idx && Idx != ~0U && "Invalid parameter index!");
178     return getAttributes(Idx);
179   }
180
181   /// getRetAttributes - The attributes for the ret value are
182   /// returned. 
183   Attributes getRetAttributes() const {
184     return getAttributes(0);
185   }
186
187   /// getFnAttributes - The function attributes are returned.
188   Attributes getFnAttributes() const {
189     return getAttributes(~0);
190   }
191   
192   /// paramHasAttr - Return true if the specified parameter index has the
193   /// specified attribute set.
194   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
195     return getAttributes(Idx) & Attr;
196   }
197   
198   /// getParamAlignment - Return the alignment for the specified function
199   /// parameter.
200   unsigned getParamAlignment(unsigned Idx) const {
201     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
202   }
203   
204   /// hasAttrSomewhere - Return true if the specified attribute is set for at
205   /// least one parameter or for the return value.
206   bool hasAttrSomewhere(Attributes Attr) const;
207
208   /// operator==/!= - Provide equality predicates.
209   bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
210   bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
211   
212   void dump() const;
213
214   //===--------------------------------------------------------------------===//
215   // Attribute List Introspection
216   //===--------------------------------------------------------------------===//
217   
218   /// getRawPointer - Return a raw pointer that uniquely identifies this
219   /// attribute list. 
220   void *getRawPointer() const {
221     return AttrList;
222   }
223   
224   // Attributes are stored as a dense set of slots, where there is one
225   // slot for each argument that has an attribute.  This allows walking over the
226   // dense set instead of walking the sparse list of attributes.
227   
228   /// isEmpty - Return true if there are no attributes.
229   ///
230   bool isEmpty() const {
231     return AttrList == 0;
232   }
233   
234   /// getNumSlots - Return the number of slots used in this attribute list. 
235   /// This is the number of arguments that have an attribute set on them
236   /// (including the function itself).
237   unsigned getNumSlots() const;
238   
239   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
240   /// holds a index number plus a set of attributes.
241   const AttributeWithIndex &getSlot(unsigned Slot) const;
242   
243 private:
244   explicit AttrListPtr(AttributeListImpl *L);
245
246   /// getAttributes - The attributes for the specified index are
247   /// returned.  Attributes for the result are denoted with Idx = 0.
248   Attributes getAttributes(unsigned Idx) const;
249
250 };
251
252 } // End llvm namespace
253
254 #endif