Move FN_NOTE_AlwaysInline and other out of ParamAttrs namespace.
[oota-llvm.git] / include / llvm / Attributes.h
1 //===-- llvm/Attributes.h - Container for ParamAttrs ---*---------- 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 parameter
11 // attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_PARAMETER_ATTRIBUTES_H
16 #define LLVM_PARAMETER_ATTRIBUTES_H
17
18 #include <string>
19
20 namespace llvm {
21 class Type;
22
23 /// Attributes - A bitset of attributes for a parameter.
24 typedef unsigned Attributes;
25   
26 namespace ParamAttr {
27
28 /// Function parameters and results can have attributes to indicate how they 
29 /// should be treated by optimizations and code generation. This enumeration 
30 /// lists the attributes that can be associated with parameters or function 
31 /// results.
32 /// @brief Function parameter attributes.
33
34 const Attributes None      = 0;     ///< No attributes have been set
35 const Attributes ZExt      = 1<<0;  ///< Zero extended before/after call
36 const Attributes SExt      = 1<<1;  ///< Sign extended before/after call
37 const Attributes NoReturn  = 1<<2;  ///< Mark the function as not returning
38 const Attributes InReg     = 1<<3;  ///< Force argument to be passed in register
39 const Attributes StructRet = 1<<4;  ///< Hidden pointer to structure to return
40 const Attributes NoUnwind  = 1<<5;  ///< Function doesn't unwind stack
41 const Attributes NoAlias   = 1<<6;  ///< Considered to not alias after call
42 const Attributes ByVal     = 1<<7;  ///< Pass structure by value
43 const Attributes Nest      = 1<<8;  ///< Nested function static chain
44 const Attributes ReadNone  = 1<<9;  ///< Function does not access memory
45 const Attributes ReadOnly  = 1<<10; ///< Function only reads from memory
46 const Attributes Alignment = 0xffff<<16; ///< Alignment of parameter (16 bits)
47                                     // 0 = unknown, else in clear (not log)
48                                     
49 /// @brief Attributes that only apply to function parameters.
50 const Attributes ParameterOnly = ByVal | Nest | StructRet;
51
52 /// @brief Attributes that only apply to function return values.
53 const Attributes ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
54
55 /// @brief Parameter attributes that do not apply to vararg call arguments.
56 const Attributes VarArgsIncompatible = StructRet;
57
58 /// @brief Attributes that are mutually incompatible.
59 const Attributes MutuallyIncompatible[3] = {
60   ByVal | InReg | Nest  | StructRet,
61   ZExt  | SExt,
62   ReadNone | ReadOnly
63 };
64
65 /// @brief Which attributes cannot be applied to a type.
66 Attributes typeIncompatible(const Type *Ty);
67
68 /// This turns an int alignment (a power of 2, normally) into the
69 /// form used internally in Attributes.
70 inline Attributes constructAlignmentFromInt(unsigned i) {
71   return (i << 16);
72 }
73
74 /// The set of Attributes set in Attributes is converted to a
75 /// string of equivalent mnemonics. This is, presumably, for writing out
76 /// the mnemonics for the assembly writer. 
77 /// @brief Convert parameter attribute bits to text
78 std::string getAsString(Attributes Attrs);
79 } // end namespace ParamAttr
80
81 /// Function notes are implemented as attributes stored at index ~0 in 
82 /// parameter attribute list.
83 const Attributes FN_NOTE_None            = 0;    
84 const Attributes FN_NOTE_NoInline        = 1<<0; // inline=never 
85 const Attributes FN_NOTE_AlwaysInline    = 1<<1; // inline=always
86 const Attributes FN_NOTE_OptimizeForSize = 1<<2; // opt_size
87
88 /// This is just a pair of values to associate a set of parameter attributes
89 /// with a parameter index. 
90 struct ParamAttrsWithIndex {
91   Attributes Attrs; ///< The attributes that are set, or'd together.
92   unsigned Index; ///< Index of the parameter for which the attributes apply.
93   
94   static ParamAttrsWithIndex get(unsigned Idx, Attributes Attrs) {
95     ParamAttrsWithIndex P;
96     P.Index = Idx;
97     P.Attrs = Attrs;
98     return P;
99   }
100 };
101   
102 //===----------------------------------------------------------------------===//
103 // PAListPtr Smart Pointer
104 //===----------------------------------------------------------------------===//
105
106 class ParamAttributeListImpl;
107   
108 /// PAListPtr - This class manages the ref count for the opaque 
109 /// ParamAttributeListImpl object and provides accessors for it.
110 class PAListPtr {
111   /// PAList - The parameter attributes that we are managing.  This can be null
112   /// to represent the empty parameter attributes list.
113   ParamAttributeListImpl *PAList;
114 public:
115   PAListPtr() : PAList(0) {}
116   PAListPtr(const PAListPtr &P);
117   const PAListPtr &operator=(const PAListPtr &RHS);
118   ~PAListPtr();
119   
120   //===--------------------------------------------------------------------===//
121   // Parameter Attribute List Construction and Mutation
122   //===--------------------------------------------------------------------===//
123   
124   /// get - Return a ParamAttrs list with the specified parameter in it.
125   static PAListPtr get(const ParamAttrsWithIndex *Attr, unsigned NumAttrs);
126   
127   /// get - Return a ParamAttr list with the parameters specified by the
128   /// consecutive random access iterator range.
129   template <typename Iter>
130   static PAListPtr get(const Iter &I, const Iter &E) {
131     if (I == E) return PAListPtr();  // Empty list.
132     return get(&*I, static_cast<unsigned>(E-I));
133   }
134
135   /// addAttr - Add the specified attribute at the specified index to this
136   /// attribute list.  Since parameter attribute lists are immutable, this
137   /// returns the new list.
138   PAListPtr addAttr(unsigned Idx, Attributes Attrs) const;
139   
140   /// removeAttr - Remove the specified attribute at the specified index from
141   /// this attribute list.  Since parameter attribute lists are immutable, this
142   /// returns the new list.
143   PAListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
144   
145   //===--------------------------------------------------------------------===//
146   // Parameter Attribute List Accessors
147   //===--------------------------------------------------------------------===//
148   
149   /// getParamAttrs - The parameter attributes for the specified parameter are
150   /// returned.  Parameters for the result are denoted with Idx = 0.
151   Attributes getParamAttrs(unsigned Idx) const;
152   
153   /// paramHasAttr - Return true if the specified parameter index has the
154   /// specified attribute set.
155   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
156     return getParamAttrs(Idx) & Attr;
157   }
158   
159   /// getParamAlignment - Return the alignment for the specified function
160   /// parameter.
161   unsigned getParamAlignment(unsigned Idx) const {
162     return (getParamAttrs(Idx) & ParamAttr::Alignment) >> 16;
163   }
164   
165   /// hasAttrSomewhere - Return true if the specified attribute is set for at
166   /// least one parameter or for the return value.
167   bool hasAttrSomewhere(Attributes Attr) const;
168
169   /// operator==/!= - Provide equality predicates.
170   bool operator==(const PAListPtr &RHS) const { return PAList == RHS.PAList; }
171   bool operator!=(const PAListPtr &RHS) const { return PAList != RHS.PAList; }
172   
173   void dump() const;
174
175   //===--------------------------------------------------------------------===//
176   // Parameter Attribute List Introspection
177   //===--------------------------------------------------------------------===//
178   
179   /// getRawPointer - Return a raw pointer that uniquely identifies this
180   /// parameter attribute list. 
181   void *getRawPointer() const {
182     return PAList;
183   }
184   
185   // Parameter attributes are stored as a dense set of slots, where there is one
186   // slot for each argument that has an attribute.  This allows walking over the
187   // dense set instead of walking the sparse list of attributes.
188   
189   /// isEmpty - Return true if no parameters have an attribute.
190   ///
191   bool isEmpty() const {
192     return PAList == 0;
193   }
194   
195   /// getNumSlots - Return the number of slots used in this attribute list. 
196   /// This is the number of arguments that have an attribute set on them
197   /// (including the function itself).
198   unsigned getNumSlots() const;
199   
200   /// getSlot - Return the ParamAttrsWithIndex at the specified slot.  This
201   /// holds a parameter number plus a set of attributes.
202   const ParamAttrsWithIndex &getSlot(unsigned Slot) const;
203   
204 private:
205   explicit PAListPtr(ParamAttributeListImpl *L);
206 };
207
208 } // End llvm namespace
209
210 #endif