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"
29 /// Function parameters and results can have attributes to indicate how they
30 /// should be treated by optimizations and code generation. This enumeration
31 /// lists the attributes that can be associated with parameters or function
33 /// @brief Function parameter attributes.
35 /// @brief A more friendly way to reference the attributes.
36 typedef uint32_t Attributes;
38 const Attributes None = 0; ///< No attributes have been set
39 const Attributes ZExt = 1<<0; ///< Zero extended before/after call
40 const Attributes SExt = 1<<1; ///< Sign extended before/after call
41 const Attributes NoReturn = 1<<2; ///< Mark the function as not returning
42 const Attributes InReg = 1<<3; ///< Force argument to be passed in register
43 const Attributes StructRet = 1<<4; ///< Hidden pointer to structure to return
44 const Attributes NoUnwind = 1<<5; ///< Function doesn't unwind stack
45 const Attributes NoAlias = 1<<6; ///< Considered to not alias after call
46 const Attributes ByVal = 1<<7; ///< Pass structure by value
47 const Attributes Nest = 1<<8; ///< Nested function static chain
48 const Attributes ReadNone = 1<<9; ///< Function does not access memory
49 const Attributes ReadOnly = 1<<10; ///< Function only reads from memory
50 const Attributes Alignment = 0xffff<<16; ///< Alignment of parameter (16 bits)
51 // 0 = unknown, else in clear (not log)
53 /// @brief Attributes that only apply to function parameters.
54 const Attributes ParameterOnly = ByVal | InReg | Nest | StructRet;
56 /// @brief Attributes that only apply to function return values.
57 const Attributes ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
59 /// @brief Parameter attributes that do not apply to vararg call arguments.
60 const Attributes VarArgsIncompatible = StructRet;
62 /// @brief Attributes that are mutually incompatible.
63 const Attributes MutuallyIncompatible[3] = {
64 ByVal | InReg | Nest | StructRet,
69 /// @brief Which attributes cannot be applied to a type.
70 Attributes typeIncompatible (const Type *Ty);
72 /// This turns an int alignment (a power of 2, normally) into the
73 /// form used internally in ParameterAttributes.
74 ParamAttr::Attributes inline constructAlignmentFromInt(uint32_t i) {
78 } // end namespace ParamAttr
80 /// @brief A more friendly way to reference the attributes.
81 typedef ParamAttr::Attributes ParameterAttributes;
83 /// This is just a pair of values to associate a set of parameter attributes
84 /// with a parameter index.
85 /// @brief ParameterAttributes with a parameter index.
86 struct ParamAttrsWithIndex {
87 ParameterAttributes attrs; ///< The attributes that are set, or'd together
88 uint16_t index; ///< Index of the parameter for which the attributes apply
90 static ParamAttrsWithIndex get(uint16_t idx, ParameterAttributes attrs) {
91 ParamAttrsWithIndex P;
98 /// @brief A vector of attribute/index pairs.
99 typedef SmallVector<ParamAttrsWithIndex,4> ParamAttrsVector;
101 /// This class represents a list of attribute/index pairs for parameter
102 /// attributes. Each entry in the list contains the index of a function
103 /// parameter and the associated ParameterAttributes. If a parameter's index is
104 /// not present in the list, then no attributes are set for that parameter. The
105 /// list may also be empty, but this does not occur in practice. An item in
106 /// the list with an index of 0 refers to the function as a whole or its result.
107 /// To construct a ParamAttrsList, you must first fill a ParamAttrsVector with
108 /// the attribute/index pairs you wish to set. The list of attributes can be
109 /// turned into a string of mnemonics suitable for LLVM Assembly output.
110 /// Various accessors are provided to obtain information about the attributes.
111 /// Note that objects of this class are "uniqued". The \p get method can return
112 /// the pointer of an existing and identical instance. Consequently, reference
113 /// counting is necessary in order to determine when the last reference to a
114 /// ParamAttrsList of a given shape is dropped. Users of this class should use
115 /// the addRef and dropRef methods to add/drop references. When the reference
116 /// count goes to zero, the ParamAttrsList object is deleted.
117 /// This class is used by Function, CallInst and InvokeInst to represent their
118 /// sets of parameter attributes.
119 /// @brief A List of ParameterAttributes.
120 class ParamAttrsList : public FoldingSetNode {
121 /// @name Construction
124 // ParamAttrsList is uniqued, these should not be publicly available
125 void operator=(const ParamAttrsList &); // Do not implement
126 ParamAttrsList(const ParamAttrsList &); // Do not implement
127 ~ParamAttrsList(); // Private implementation
129 /// Only the \p get method can invoke this when it wants to create a
131 /// @brief Construct an ParamAttrsList from a ParamAttrsVector
132 explicit ParamAttrsList(const ParamAttrsVector &attrVec);
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,
157 ParameterAttributes attrs);
159 /// @brief Remove the specified attributes from those in PAL at index idx.
160 static const ParamAttrsList *excludeAttrs(const ParamAttrsList *PAL,
162 ParameterAttributes attrs);
168 /// The parameter attributes for the \p indexth parameter are returned.
169 /// The 0th parameter refers to the return type of the function. Note that
170 /// the \p param_index is an index into the function's parameters, not an
171 /// index into this class's list of attributes. The result of getParamIndex
172 /// is always suitable input to this function.
173 /// @returns The all the ParameterAttributes for the \p indexth parameter
174 /// as a uint16_t of enumeration values OR'd together.
175 /// @brief Get the attributes for a parameter
176 ParameterAttributes getParamAttrs(uint16_t param_index) const;
178 /// This checks to see if the \p ith function parameter has the parameter
179 /// attribute given by \p attr set.
180 /// @returns true if the parameter attribute is set
181 /// @brief Determine if a ParameterAttributes is set
182 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
183 return getParamAttrs(i) & attr;
186 /// This extracts the alignment for the \p ith function parameter.
187 /// @returns 0 if unknown, else the alignment in bytes
188 /// @brief Extract the Alignment
189 uint16_t getParamAlignment(uint16_t i) const {
190 return (getParamAttrs(i) & ParamAttr::Alignment) >> 16;
193 /// This returns whether the given attribute is set for at least one
194 /// parameter or for the return value.
195 /// @returns true if the parameter attribute is set somewhere
196 /// @brief Determine if a ParameterAttributes is set somewhere
197 bool hasAttrSomewhere(ParameterAttributes attr) const;
199 /// The set of ParameterAttributes set in Attributes is converted to a
200 /// string of equivalent mnemonics. This is, presumably, for writing out
201 /// the mnemonics for the assembly writer.
202 /// @brief Convert parameter attribute bits to text
203 static std::string getParamAttrsText(ParameterAttributes Attributes);
205 /// The \p Indexth parameter attribute is converted to string.
206 /// @brief Get the text for the parmeter attributes for one parameter.
207 std::string getParamAttrsTextByIndex(uint16_t Index) const {
208 return getParamAttrsText(getParamAttrs(Index));
211 /// @brief Comparison operator for ParamAttrsList
212 bool operator < (const ParamAttrsList& that) const {
213 if (this->attrs.size() < that.attrs.size())
215 if (this->attrs.size() > that.attrs.size())
217 for (unsigned i = 0; i < attrs.size(); ++i) {
218 if (attrs[i].index < that.attrs[i].index)
220 if (attrs[i].index > that.attrs[i].index)
222 if (attrs[i].attrs < that.attrs[i].attrs)
224 if (attrs[i].attrs > that.attrs[i].attrs)
230 /// Returns the parameter index of a particular parameter attribute in this
231 /// list of attributes. Note that the attr_index is an index into this
232 /// class's list of attributes, not the index of a parameter. The result
233 /// is the index of the parameter. Clients generally should not use this
234 /// method. It is used internally by LLVM.
235 /// @brief Get a parameter index
236 uint16_t getParamIndex(unsigned attr_index) const {
237 return attrs[attr_index].index;
240 ParameterAttributes getParamAttrsAtIndex(unsigned attr_index) const {
241 return attrs[attr_index].attrs;
244 /// Determines how many parameter attributes are set in this ParamAttrsList.
245 /// This says nothing about how many parameters the function has. It also
246 /// says nothing about the highest parameter index that has attributes.
247 /// Clients generally should not use this method. It is used internally by
249 /// @returns the number of parameter attributes in this ParamAttrsList.
250 /// @brief Return the number of parameter attributes this type has.
251 unsigned size() const { return attrs.size(); }
253 /// @brief Return the number of references to this ParamAttrsList.
254 unsigned numRefs() const { return refCount; }
260 /// Classes retaining references to ParamAttrsList objects should call this
261 /// method to increment the reference count. This ensures that the
262 /// ParamAttrsList object will not disappear until the class drops it.
263 /// @brief Add a reference to this instance.
264 void addRef() const { refCount++; }
266 /// Classes retaining references to ParamAttrsList objects should call this
267 /// method to decrement the reference count and possibly delete the
268 /// ParamAttrsList object. This ensures that ParamAttrsList objects are
269 /// cleaned up only when the last reference to them is dropped.
270 /// @brief Drop a reference to this instance.
271 void dropRef() const {
272 assert(refCount != 0 && "dropRef without addRef");
278 /// @name Implementation Details
281 void Profile(FoldingSetNodeID &ID) const {
284 static void Profile(FoldingSetNodeID &ID, const ParamAttrsVector &Attrs);
291 ParamAttrsVector attrs; ///< The list of attributes
292 mutable unsigned refCount; ///< The number of references to this object
296 } // End llvm namespace