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