Support for trampolines, except for X86 codegen which is
[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 };
43
44 }
45
46 /// This is just a pair of values to associate a set of parameter attributes
47 /// with a parameter index. 
48 /// @brief ParameterAttributes with a parameter index.
49 struct ParamAttrsWithIndex {
50   uint16_t attrs; ///< The attributes that are set, |'d together
51   uint16_t index; ///< Index of the parameter for which the attributes apply
52   
53   static ParamAttrsWithIndex get(uint16_t idx, uint16_t attrs) {
54     ParamAttrsWithIndex P;
55     P.index = idx;
56     P.attrs = attrs;
57     return P;
58   }
59 };
60
61 /// @brief A vector of attribute/index pairs.
62 typedef SmallVector<ParamAttrsWithIndex,4> ParamAttrsVector;
63
64 /// @brief A more friendly way to reference the attributes.
65 typedef ParamAttr::Attributes ParameterAttributes;
66
67 /// This class represents a list of attribute/index pairs for parameter 
68 /// attributes. Each entry in the list contains the index of a function 
69 /// parameter and the associated ParameterAttributes. If a parameter's index is
70 /// not present in the list, then no attributes are set for that parameter. The
71 /// list may also be empty, but this does not occur in practice. An item in
72 /// the list with an index of 0 refers to the function as a whole or its result.
73 /// To construct a ParamAttrsList, you must first fill a ParamAttrsVector with 
74 /// the attribute/index pairs you wish to set.  The list of attributes can be 
75 /// turned into a string of mnemonics suitable for LLVM Assembly output. 
76 /// Various accessors are provided to obtain information about the attributes.
77 /// Note that objects of this class are "uniqued". The \p get method can return
78 /// the pointer of an existing and identical instance. Consequently, reference
79 /// counting is necessary in order to determine when the last reference to a 
80 /// ParamAttrsList of a given shape is dropped. Users of this class should use
81 /// the addRef and dropRef methods to add/drop references. When the reference
82 /// count goes to zero, the ParamAttrsList object is deleted.
83 /// This class is used by Function, CallInst and InvokeInst to represent their
84 /// sets of parameter attributes. 
85 /// @brief A List of ParameterAttributes.
86 class ParamAttrsList : public FoldingSetNode {
87   /// @name Construction
88   /// @{
89   private:
90     // ParamAttrsList is uniqued, these should not be publicly available
91     void operator=(const ParamAttrsList &); // Do not implement
92     ParamAttrsList(const ParamAttrsList &); // Do not implement
93     ParamAttrsList();                       // Do not implement
94     ~ParamAttrsList();                      // Private implementation
95
96     /// Only the \p get method can invoke this when it wants to create a
97     /// new instance.
98     /// @brief Construct an ParamAttrsList from a ParamAttrsVector
99     explicit ParamAttrsList(const ParamAttrsVector &attrVec) 
100       : attrs(attrVec), refCount(0) {}
101
102   public:
103     /// This method ensures the uniqueness of ParamAttrsList instances. The
104     /// argument is a vector of attribute/index pairs as represented by the
105     /// ParamAttrsWithIndex structure. The vector is used in the construction of
106     /// the ParamAttrsList instance. If an instance with identical vector pairs
107     /// exists, it will be returned instead of creating a new instance.
108     /// @brief Get a ParamAttrsList instance.
109     static ParamAttrsList *get(const ParamAttrsVector &attrVec);
110
111   /// @}
112   /// @name Accessors
113   /// @{
114   public:
115     /// The parameter attributes for the \p indexth parameter are returned. 
116     /// The 0th parameter refers to the return type of the function. Note that
117     /// the \p param_index is an index into the function's parameters, not an
118     /// index into this class's list of attributes. The result of getParamIndex
119     /// is always suitable input to this function.
120     /// @returns The all the ParameterAttributes for the \p indexth parameter
121     /// as a uint16_t of enumeration values OR'd together.
122     /// @brief Get the attributes for a parameter
123     uint16_t getParamAttrs(uint16_t param_index) const;
124
125     /// This checks to see if the \p ith function parameter has the parameter
126     /// attribute given by \p attr set.
127     /// @returns true if the parameter attribute is set
128     /// @brief Determine if a ParameterAttributes is set
129     bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
130       return getParamAttrs(i) & attr;
131     }
132
133     /// The set of ParameterAttributes set in Attributes is converted to a
134     /// string of equivalent mnemonics. This is, presumably, for writing out
135     /// the mnemonics for the assembly writer. 
136     /// @brief Convert parameter attribute bits to text
137     static std::string getParamAttrsText(uint16_t Attributes);
138
139     /// The \p Indexth parameter attribute is converted to string.
140     /// @brief Get the text for the parmeter attributes for one parameter.
141     std::string getParamAttrsTextByIndex(uint16_t Index) const {
142       return getParamAttrsText(getParamAttrs(Index));
143     }
144
145     /// @brief Comparison operator for ParamAttrsList
146     bool operator < (const ParamAttrsList& that) const {
147       if (this->attrs.size() < that.attrs.size())
148         return true;
149       if (this->attrs.size() > that.attrs.size())
150         return false;
151       for (unsigned i = 0; i < attrs.size(); ++i) {
152         if (attrs[i].index < that.attrs[i].index)
153           return true;
154         if (attrs[i].index > that.attrs[i].index)
155           return false;
156         if (attrs[i].attrs < that.attrs[i].attrs)
157           return true;
158         if (attrs[i].attrs > that.attrs[i].attrs)
159           return false;
160       }
161       return false;
162     }
163
164     /// Returns the parameter index of a particular parameter attribute in this
165     /// list of attributes. Note that the attr_index is an index into this 
166     /// class's list of attributes, not the index of a parameter. The result
167     /// is the index of the parameter. Clients generally should not use this
168     /// method. It is used internally by LLVM.
169     /// @brief Get a parameter index
170     uint16_t getParamIndex(unsigned attr_index) const {
171       return attrs[attr_index].index;
172     }
173
174     uint16_t getParamAttrsAtIndex(unsigned attr_index) const {
175       return attrs[attr_index].attrs;
176     }
177     
178     /// Determines how many parameter attributes are set in this ParamAttrsList.
179     /// This says nothing about how many parameters the function has. It also
180     /// says nothing about the highest parameter index that has attributes. 
181     /// Clients generally should not use this method. It is used internally by
182     /// LLVM.
183     /// @returns the number of parameter attributes in this ParamAttrsList.
184     /// @brief Return the number of parameter attributes this type has.
185     unsigned size() const { return attrs.size(); }
186
187     /// Classes retaining references to ParamAttrsList objects should call this
188     /// method to increment the reference count. This ensures that the
189     /// ParamAttrsList object will not disappear until the class drops it.
190     /// @brief Add a reference to this instance.
191     void addRef() const { refCount++; }
192
193     /// Classes retaining references to ParamAttrsList objects should call this
194     /// method to decrement the reference count and possibly delete the 
195     /// ParamAttrsList object. This ensures that ParamAttrsList objects are 
196     /// cleaned up only when the last reference to them is dropped.
197     /// @brief Drop a reference to this instance.
198     void dropRef() const { 
199       assert(refCount != 0 && "dropRef without addRef");
200       if (--refCount == 0) 
201         delete this; 
202     }
203
204   /// @}
205   /// @name Implementation Details
206   /// @{
207   public:
208     void Profile(FoldingSetNodeID &ID) const;
209     void dump() const;
210
211   /// @}
212   /// @name Data
213   /// @{
214   private:
215     ParamAttrsVector attrs;     ///< The list of attributes
216     mutable unsigned refCount;  ///< The number of references to this object
217   /// @}
218 };
219
220 } // End llvm namespace
221
222 #endif