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