1 //===-- llvm/ParameterAttributes.h - Container for ParamAttrs ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the types necessary to represent the parameter attributes
11 // associated with functions and their calls.
13 // The implementations of these classes live in lib/VMCore/Function.cpp.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_PARAMETER_ATTRIBUTES_H
18 #define LLVM_PARAMETER_ATTRIBUTES_H
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/FoldingSet.h"
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
30 /// @brief Function parameter 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
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 |
53 /// @brief Attributes that only apply to function parameters.
54 const uint16_t ParameterOnly = ByVal | InReg | Nest | StructRet;
56 /// @brief Attributes that only apply to function return values.
57 const uint16_t ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
59 /// @brief Attributes that only apply to integers.
60 const uint16_t IntegerTypeOnly = SExt | ZExt;
62 /// @brief Attributes that only apply to pointers.
63 const uint16_t PointerTypeOnly = ByVal | Nest | NoAlias | StructRet;
65 /// @brief Attributes that do not apply to void type function return values.
66 const uint16_t VoidTypeIncompatible = IntegerTypeOnly | PointerTypeOnly |
69 /// @brief Attributes that are mutually incompatible.
70 const uint16_t MutuallyIncompatible[3] = {
71 ByVal | InReg | Nest | StructRet,
76 } // end namespace ParamAttr
78 /// This is just a pair of values to associate a set of parameter attributes
79 /// with a parameter index.
80 /// @brief ParameterAttributes with a parameter index.
81 struct ParamAttrsWithIndex {
82 uint16_t attrs; ///< The attributes that are set, or'd together
83 uint16_t index; ///< Index of the parameter for which the attributes apply
85 static ParamAttrsWithIndex get(uint16_t idx, uint16_t attrs) {
86 ParamAttrsWithIndex P;
93 /// @brief A vector of attribute/index pairs.
94 typedef SmallVector<ParamAttrsWithIndex,4> ParamAttrsVector;
96 /// @brief A more friendly way to reference the attributes.
97 typedef ParamAttr::Attributes ParameterAttributes;
99 /// This class represents a list of attribute/index pairs for parameter
100 /// attributes. Each entry in the list contains the index of a function
101 /// parameter and the associated ParameterAttributes. If a parameter's index is
102 /// not present in the list, then no attributes are set for that parameter. The
103 /// list may also be empty, but this does not occur in practice. An item in
104 /// the list with an index of 0 refers to the function as a whole or its result.
105 /// To construct a ParamAttrsList, you must first fill a ParamAttrsVector with
106 /// the attribute/index pairs you wish to set. The list of attributes can be
107 /// turned into a string of mnemonics suitable for LLVM Assembly output.
108 /// Various accessors are provided to obtain information about the attributes.
109 /// Note that objects of this class are "uniqued". The \p get method can return
110 /// the pointer of an existing and identical instance. Consequently, reference
111 /// counting is necessary in order to determine when the last reference to a
112 /// ParamAttrsList of a given shape is dropped. Users of this class should use
113 /// the addRef and dropRef methods to add/drop references. When the reference
114 /// count goes to zero, the ParamAttrsList object is deleted.
115 /// This class is used by Function, CallInst and InvokeInst to represent their
116 /// sets of parameter attributes.
117 /// @brief A List of ParameterAttributes.
118 class ParamAttrsList : public FoldingSetNode {
119 /// @name Construction
122 // ParamAttrsList is uniqued, these should not be publicly available
123 void operator=(const ParamAttrsList &); // Do not implement
124 ParamAttrsList(const ParamAttrsList &); // Do not implement
125 ParamAttrsList(); // Do not implement
126 ~ParamAttrsList(); // Private implementation
128 /// Only the \p get method can invoke this when it wants to create a
130 /// @brief Construct an ParamAttrsList from a ParamAttrsVector
131 explicit ParamAttrsList(const ParamAttrsVector &attrVec)
132 : attrs(attrVec), refCount(0) {}
135 /// This method ensures the uniqueness of ParamAttrsList instances. The
136 /// argument is a vector of attribute/index pairs as represented by the
137 /// ParamAttrsWithIndex structure. The index values must be in strictly
138 /// increasing order and ParamAttr::None is not allowed. The vector is
139 /// used to construct the ParamAttrsList instance. If an instance with
140 /// identical vector pairs exists, it will be returned instead of creating
142 /// @brief Get a ParamAttrsList instance.
143 static const ParamAttrsList *get(const ParamAttrsVector &attrVec);
145 /// Returns the ParamAttrsList obtained by modifying PAL using the supplied
146 /// list of attribute/index pairs. Any existing attributes for the given
147 /// index are replaced by the given attributes. If there were no attributes
148 /// then the new ones are inserted. Attributes can be deleted by replacing
149 /// them with ParamAttr::None. Index values must be strictly increasing.
150 /// @brief Get a new ParamAttrsList instance by modifying an existing one.
151 static const ParamAttrsList *getModified(const ParamAttrsList *PAL,
152 const ParamAttrsVector &modVec);
154 /// @brief Add the specified attributes to those in PAL at index idx.
155 static const ParamAttrsList *includeAttrs(const ParamAttrsList *PAL,
156 uint16_t idx, uint16_t attrs);
158 /// @brief Remove the specified attributes from those in PAL at index idx.
159 static const ParamAttrsList *excludeAttrs(const ParamAttrsList *PAL,
160 uint16_t idx, uint16_t attrs);
162 /// Returns whether each of the specified lists of attributes can be safely
163 /// replaced with the other in a function or a function call.
164 /// @brief Whether one attribute list can safely replace the other.
165 static bool areCompatible(const ParamAttrsList *A, const ParamAttrsList *B);
171 /// The parameter attributes for the \p indexth parameter are returned.
172 /// The 0th parameter refers to the return type of the function. Note that
173 /// the \p param_index is an index into the function's parameters, not an
174 /// index into this class's list of attributes. The result of getParamIndex
175 /// is always suitable input to this function.
176 /// @returns The all the ParameterAttributes for the \p indexth parameter
177 /// as a uint16_t of enumeration values OR'd together.
178 /// @brief Get the attributes for a parameter
179 uint16_t getParamAttrs(uint16_t param_index) const;
181 /// This checks to see if the \p ith function parameter has the parameter
182 /// attribute given by \p attr set.
183 /// @returns true if the parameter attribute is set
184 /// @brief Determine if a ParameterAttributes is set
185 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
186 return getParamAttrs(i) & attr;
189 /// The set of ParameterAttributes set in Attributes is converted to a
190 /// string of equivalent mnemonics. This is, presumably, for writing out
191 /// the mnemonics for the assembly writer.
192 /// @brief Convert parameter attribute bits to text
193 static std::string getParamAttrsText(uint16_t Attributes);
195 /// The \p Indexth parameter attribute is converted to string.
196 /// @brief Get the text for the parmeter attributes for one parameter.
197 std::string getParamAttrsTextByIndex(uint16_t Index) const {
198 return getParamAttrsText(getParamAttrs(Index));
201 /// @brief Comparison operator for ParamAttrsList
202 bool operator < (const ParamAttrsList& that) const {
203 if (this->attrs.size() < that.attrs.size())
205 if (this->attrs.size() > that.attrs.size())
207 for (unsigned i = 0; i < attrs.size(); ++i) {
208 if (attrs[i].index < that.attrs[i].index)
210 if (attrs[i].index > that.attrs[i].index)
212 if (attrs[i].attrs < that.attrs[i].attrs)
214 if (attrs[i].attrs > that.attrs[i].attrs)
220 /// Returns the parameter index of a particular parameter attribute in this
221 /// list of attributes. Note that the attr_index is an index into this
222 /// class's list of attributes, not the index of a parameter. The result
223 /// is the index of the parameter. Clients generally should not use this
224 /// method. It is used internally by LLVM.
225 /// @brief Get a parameter index
226 uint16_t getParamIndex(unsigned attr_index) const {
227 return attrs[attr_index].index;
230 uint16_t getParamAttrsAtIndex(unsigned attr_index) const {
231 return attrs[attr_index].attrs;
234 /// Determines how many parameter attributes are set in this ParamAttrsList.
235 /// This says nothing about how many parameters the function has. It also
236 /// says nothing about the highest parameter index that has attributes.
237 /// Clients generally should not use this method. It is used internally by
239 /// @returns the number of parameter attributes in this ParamAttrsList.
240 /// @brief Return the number of parameter attributes this type has.
241 unsigned size() const { return attrs.size(); }
243 /// @brief Return the number of references to this ParamAttrsList.
244 unsigned numRefs() const { return refCount; }
250 /// Classes retaining references to ParamAttrsList objects should call this
251 /// method to increment the reference count. This ensures that the
252 /// ParamAttrsList object will not disappear until the class drops it.
253 /// @brief Add a reference to this instance.
254 void addRef() const { refCount++; }
256 /// Classes retaining references to ParamAttrsList objects should call this
257 /// method to decrement the reference count and possibly delete the
258 /// ParamAttrsList object. This ensures that ParamAttrsList objects are
259 /// cleaned up only when the last reference to them is dropped.
260 /// @brief Drop a reference to this instance.
261 void dropRef() const {
262 assert(refCount != 0 && "dropRef without addRef");
268 /// @name Implementation Details
271 void Profile(FoldingSetNodeID &ID) const;
278 ParamAttrsVector attrs; ///< The list of attributes
279 mutable unsigned refCount; ///< The number of references to this object
283 } // End llvm namespace