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