Expand ParameterAttributes to 32 bits (in preparation
[oota-llvm.git] / include / llvm / ParameterAttributes.h
1 //===-- llvm/ParameterAttributes.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 types necessary to represent the parameter attributes
11 // associated with functions and their calls.
12 //
13 // The implementations of these classes live in lib/VMCore/Function.cpp.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_PARAMETER_ATTRIBUTES_H
18 #define LLVM_PARAMETER_ATTRIBUTES_H
19
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include <cassert>
23
24 namespace llvm {
25 class Type;
26
27 namespace ParamAttr {
28
29 /// Function parameters and results can have attributes to indicate how they 
30 /// should be treated by optimizations and code generation. This enumeration 
31 /// lists the attributes that can be associated with parameters or function 
32 /// results.
33 /// @brief Function parameter attributes.
34
35 /// @brief A more friendly way to reference the attributes.
36 typedef uint32_t Attributes;
37
38 const Attributes None      = 0;     ///< No attributes have been set
39 const Attributes ZExt      = 1<<0;  ///< Zero extended before/after call
40 const Attributes SExt      = 1<<1;  ///< Sign extended before/after call
41 const Attributes NoReturn  = 1<<2;  ///< Mark the function as not returning
42 const Attributes InReg     = 1<<3;  ///< Force argument to be passed in register
43 const Attributes StructRet = 1<<4;  ///< Hidden pointer to structure to return
44 const Attributes NoUnwind  = 1<<5;  ///< Function doesn't unwind stack
45 const Attributes NoAlias   = 1<<6;  ///< Considered to not alias after call
46 const Attributes ByVal     = 1<<7;  ///< Pass structure by value
47 const Attributes Nest      = 1<<8;  ///< Nested function static chain
48 const Attributes ReadNone  = 1<<9;  ///< Function does not access memory
49 const Attributes ReadOnly  = 1<<10; ///< Function only reads from memory
50
51 /// @brief Attributes that only apply to function parameters.
52 const Attributes ParameterOnly = ByVal | InReg | Nest | StructRet;
53
54 /// @brief Attributes that only apply to function return values.
55 const Attributes ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
56
57 /// @brief Parameter attributes that do not apply to vararg call arguments.
58 const Attributes VarArgsIncompatible = StructRet;
59
60 /// @brief Attributes that are mutually incompatible.
61 const Attributes MutuallyIncompatible[3] = {
62   ByVal | InReg | Nest  | StructRet,
63   ZExt  | SExt,
64   ReadNone | ReadOnly
65 };
66
67 /// @brief Which attributes cannot be applied to a type.
68 Attributes typeIncompatible (const Type *Ty);
69
70 } // end namespace ParamAttr
71
72 /// @brief A more friendly way to reference the attributes.
73 typedef ParamAttr::Attributes ParameterAttributes;
74
75 /// This is just a pair of values to associate a set of parameter attributes
76 /// with a parameter index. 
77 /// @brief ParameterAttributes with a parameter index.
78 struct ParamAttrsWithIndex {
79   ParameterAttributes attrs; ///< The attributes that are set, or'd together
80   uint16_t index; ///< Index of the parameter for which the attributes apply
81   
82   static ParamAttrsWithIndex get(uint16_t idx, ParameterAttributes attrs) {
83     ParamAttrsWithIndex P;
84     P.index = idx;
85     P.attrs = attrs;
86     return P;
87   }
88 };
89
90 /// @brief A vector of attribute/index pairs.
91 typedef SmallVector<ParamAttrsWithIndex,4> ParamAttrsVector;
92
93 /// This class represents a list of attribute/index pairs for parameter 
94 /// attributes. Each entry in the list contains the index of a function 
95 /// parameter and the associated ParameterAttributes. If a parameter's index is
96 /// not present in the list, then no attributes are set for that parameter. The
97 /// list may also be empty, but this does not occur in practice. An item in
98 /// the list with an index of 0 refers to the function as a whole or its result.
99 /// To construct a ParamAttrsList, you must first fill a ParamAttrsVector with 
100 /// the attribute/index pairs you wish to set.  The list of attributes can be 
101 /// turned into a string of mnemonics suitable for LLVM Assembly output. 
102 /// Various accessors are provided to obtain information about the attributes.
103 /// Note that objects of this class are "uniqued". The \p get method can return
104 /// the pointer of an existing and identical instance. Consequently, reference
105 /// counting is necessary in order to determine when the last reference to a 
106 /// ParamAttrsList of a given shape is dropped. Users of this class should use
107 /// the addRef and dropRef methods to add/drop references. When the reference
108 /// count goes to zero, the ParamAttrsList object is deleted.
109 /// This class is used by Function, CallInst and InvokeInst to represent their
110 /// sets of parameter attributes. 
111 /// @brief A List of ParameterAttributes.
112 class ParamAttrsList : public FoldingSetNode {
113   /// @name Construction
114   /// @{
115   private:
116     // ParamAttrsList is uniqued, these should not be publicly available
117     void operator=(const ParamAttrsList &); // Do not implement
118     ParamAttrsList(const ParamAttrsList &); // Do not implement
119     ~ParamAttrsList();                      // Private implementation
120
121     /// Only the \p get method can invoke this when it wants to create a
122     /// new instance.
123     /// @brief Construct an ParamAttrsList from a ParamAttrsVector
124     explicit ParamAttrsList(const ParamAttrsVector &attrVec);
125
126   public:
127     /// This method ensures the uniqueness of ParamAttrsList instances.  The
128     /// argument is a vector of attribute/index pairs as represented by the
129     /// ParamAttrsWithIndex structure.  The index values must be in strictly
130     /// increasing order and ParamAttr::None is not allowed.  The vector is
131     /// used to construct the ParamAttrsList instance.  If an instance with
132     /// identical vector pairs exists, it will be returned instead of creating
133     /// a new instance.
134     /// @brief Get a ParamAttrsList instance.
135     static const ParamAttrsList *get(const ParamAttrsVector &attrVec);
136
137     /// Returns the ParamAttrsList obtained by modifying PAL using the supplied
138     /// list of attribute/index pairs.  Any existing attributes for the given
139     /// index are replaced by the given attributes.  If there were no attributes
140     /// then the new ones are inserted.  Attributes can be deleted by replacing
141     /// them with ParamAttr::None.  Index values must be strictly increasing.
142     /// @brief Get a new ParamAttrsList instance by modifying an existing one.
143     static const ParamAttrsList *getModified(const ParamAttrsList *PAL,
144                                              const ParamAttrsVector &modVec);
145
146     /// @brief Add the specified attributes to those in PAL at index idx.
147     static const ParamAttrsList *includeAttrs(const ParamAttrsList *PAL,
148                                               uint16_t idx, 
149                                               ParameterAttributes attrs);
150
151     /// @brief Remove the specified attributes from those in PAL at index idx.
152     static const ParamAttrsList *excludeAttrs(const ParamAttrsList *PAL,
153                                               uint16_t idx, 
154                                               ParameterAttributes attrs);
155
156   /// @}
157   /// @name Accessors
158   /// @{
159   public:
160     /// The parameter attributes for the \p indexth parameter are returned. 
161     /// The 0th parameter refers to the return type of the function. Note that
162     /// the \p param_index is an index into the function's parameters, not an
163     /// index into this class's list of attributes. The result of getParamIndex
164     /// is always suitable input to this function.
165     /// @returns The all the ParameterAttributes for the \p indexth parameter
166     /// as a uint16_t of enumeration values OR'd together.
167     /// @brief Get the attributes for a parameter
168     ParameterAttributes getParamAttrs(uint16_t param_index) const;
169
170     /// This checks to see if the \p ith function parameter has the parameter
171     /// attribute given by \p attr set.
172     /// @returns true if the parameter attribute is set
173     /// @brief Determine if a ParameterAttributes is set
174     bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
175       return getParamAttrs(i) & attr;
176     }
177
178     /// This returns whether the given attribute is set for at least one
179     /// parameter or for the return value.
180     /// @returns true if the parameter attribute is set somewhere
181     /// @brief Determine if a ParameterAttributes is set somewhere
182     bool hasAttrSomewhere(ParameterAttributes attr) const;
183
184     /// The set of ParameterAttributes set in Attributes is converted to a
185     /// string of equivalent mnemonics. This is, presumably, for writing out
186     /// the mnemonics for the assembly writer. 
187     /// @brief Convert parameter attribute bits to text
188     static std::string getParamAttrsText(ParameterAttributes Attributes);
189
190     /// The \p Indexth parameter attribute is converted to string.
191     /// @brief Get the text for the parmeter attributes for one parameter.
192     std::string getParamAttrsTextByIndex(uint16_t Index) const {
193       return getParamAttrsText(getParamAttrs(Index));
194     }
195
196     /// @brief Comparison operator for ParamAttrsList
197     bool operator < (const ParamAttrsList& that) const {
198       if (this->attrs.size() < that.attrs.size())
199         return true;
200       if (this->attrs.size() > that.attrs.size())
201         return false;
202       for (unsigned i = 0; i < attrs.size(); ++i) {
203         if (attrs[i].index < that.attrs[i].index)
204           return true;
205         if (attrs[i].index > that.attrs[i].index)
206           return false;
207         if (attrs[i].attrs < that.attrs[i].attrs)
208           return true;
209         if (attrs[i].attrs > that.attrs[i].attrs)
210           return false;
211       }
212       return false;
213     }
214
215     /// Returns the parameter index of a particular parameter attribute in this
216     /// list of attributes. Note that the attr_index is an index into this 
217     /// class's list of attributes, not the index of a parameter. The result
218     /// is the index of the parameter. Clients generally should not use this
219     /// method. It is used internally by LLVM.
220     /// @brief Get a parameter index
221     uint16_t getParamIndex(unsigned attr_index) const {
222       return attrs[attr_index].index;
223     }
224
225     ParameterAttributes getParamAttrsAtIndex(unsigned attr_index) const {
226       return attrs[attr_index].attrs;
227     }
228     
229     /// Determines how many parameter attributes are set in this ParamAttrsList.
230     /// This says nothing about how many parameters the function has. It also
231     /// says nothing about the highest parameter index that has attributes. 
232     /// Clients generally should not use this method. It is used internally by
233     /// LLVM.
234     /// @returns the number of parameter attributes in this ParamAttrsList.
235     /// @brief Return the number of parameter attributes this type has.
236     unsigned size() const { return attrs.size(); }
237
238     /// @brief Return the number of references to this ParamAttrsList.
239     unsigned numRefs() const { return refCount; }
240
241   /// @}
242   /// @name Mutators
243   /// @{
244   public:
245     /// Classes retaining references to ParamAttrsList objects should call this
246     /// method to increment the reference count. This ensures that the
247     /// ParamAttrsList object will not disappear until the class drops it.
248     /// @brief Add a reference to this instance.
249     void addRef() const { refCount++; }
250
251     /// Classes retaining references to ParamAttrsList objects should call this
252     /// method to decrement the reference count and possibly delete the 
253     /// ParamAttrsList object. This ensures that ParamAttrsList objects are 
254     /// cleaned up only when the last reference to them is dropped.
255     /// @brief Drop a reference to this instance.
256     void dropRef() const { 
257       assert(refCount != 0 && "dropRef without addRef");
258       if (--refCount == 0) 
259         delete this; 
260     }
261
262   /// @}
263   /// @name Implementation Details
264   /// @{
265   public:
266     void Profile(FoldingSetNodeID &ID) const {
267       Profile(ID, attrs);
268     }
269     static void Profile(FoldingSetNodeID &ID, const ParamAttrsVector &Attrs);
270     void dump() const;
271
272   /// @}
273   /// @name Data
274   /// @{
275   private:
276     ParamAttrsVector attrs;     ///< The list of attributes
277     mutable unsigned refCount;  ///< The number of references to this object
278   /// @}
279 };
280
281 } // End llvm namespace
282
283 #endif