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