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