1 //===-- llvm/ParameterAttributes.h - Container for Param Attrs --*- 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"
24 /// Function parameters can have attributes to indicate how they should be
25 /// treated by optimizations and code generation. This enumeration lists the
26 /// attributes that can be associated with parameters or function results.
27 /// @brief Function parameter attributes.
28 enum ParameterAttributes {
29 NoAttributeSet = 0, ///< No attributes have been set
30 ZExtAttribute = 1 << 0, ///< zero extended before/after call
31 SExtAttribute = 1 << 1, ///< sign extended before/after call
32 NoReturnAttribute = 1 << 2, ///< mark the function as not returning
33 InRegAttribute = 1 << 3, ///< force argument to be passed in register
34 StructRetAttribute = 1 << 4, ///< hidden pointer to structure to return
35 NoUnwindAttribute = 1 << 5 ///< Function doesn't unwind stack
38 /// This class is used by Function and CallInst to represent the set of
39 /// parameter attributes used. It represents a list of pairs of uint16_t, one
40 /// for the parameter index, and one a set of ParameterAttributes bits.
41 /// Parameters that have no attributes are not present in the list. The list
42 /// may also be empty, but this doesn't occur in practice. The list constructs
43 /// as empty and is filled by the insert method. The list can be turned into
44 /// a string of mnemonics suitable for LLVM Assembly output. Various accessors
45 /// are provided to obtain information about the attributes.
46 /// @brief A List of ParameterAttributes.
47 class ParamAttrsList {
51 /// The parameter attributes for the \p indexth parameter are returned.
52 /// The 0th parameter refers to the return type of the function. Note that
53 /// the \p param_index is an index into the function's parameters, not an
54 /// index into this class's list of attributes. The result of getParamIndex
55 /// is always suitable input to this function.
56 /// @returns The all the ParameterAttributes for the \p indexth parameter
57 /// as a uint16_t of enumeration values OR'd together.
58 /// @brief Get the attributes for a parameter
59 uint16_t getParamAttrs(uint16_t param_index) const;
61 /// This checks to see if the \p ith function parameter has the parameter
62 /// attribute given by \p attr set.
63 /// @returns true if the parameter attribute is set
64 /// @brief Determine if a ParameterAttributes is set
65 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
66 return getParamAttrs(i) & attr;
69 /// The set of ParameterAttributes set in Attributes is converted to a
70 /// string of equivalent mnemonics. This is, presumably, for writing out
71 /// the mnemonics for the assembly writer.
72 /// @brief Convert parameter attribute bits to text
73 static std::string getParamAttrsText(uint16_t Attributes);
75 /// The \p Indexth parameter attribute is converted to string.
76 /// @brief Get the text for the parmeter attributes for one parameter.
77 std::string getParamAttrsTextByIndex(uint16_t Index) const {
78 return getParamAttrsText(getParamAttrs(Index));
81 /// @brief Comparison operator for ParamAttrsList
82 bool operator < (const ParamAttrsList& that) const {
83 if (this->attrs.size() < that.attrs.size())
85 if (this->attrs.size() > that.attrs.size())
87 for (unsigned i = 0; i < attrs.size(); ++i) {
88 if (attrs[i].index < that.attrs[i].index)
90 if (attrs[i].index > that.attrs[i].index)
92 if (attrs[i].attrs < that.attrs[i].attrs)
94 if (attrs[i].attrs > that.attrs[i].attrs)
100 /// Returns the parameter index of a particular parameter attribute in this
101 /// list of attributes. Note that the attr_index is an index into this
102 /// class's list of attributes, not the index of a parameter. The result
103 /// is the index of the parameter. Clients generally should not use this
104 /// method. It is used internally by LLVM.
105 /// @brief Get a parameter index
106 uint16_t getParamIndex(unsigned attr_index) const {
107 return attrs[attr_index].index;
110 /// Determines how many parameter attributes are set in this ParamAttrsList.
111 /// This says nothing about how many parameters the function has. It also
112 /// says nothing about the highest parameter index that has attributes.
113 /// Clients generally should not use this method. It is used internally by
115 /// @returns the number of parameter attributes in this ParamAttrsList.
116 /// @brief Return the number of parameter attributes this type has.
117 unsigned size() const { return attrs.size(); }
119 /// Clients generally should not use this method. It is used internally by
121 /// @returns true if this ParamAttrsList is empty.
122 /// @brief Determine emptiness of ParamAttrsList.
123 unsigned empty() const { return attrs.empty(); }
129 /// This method will add the \p attr to the parameter with index
130 /// \p param_index. If the parameter index does not exist it will be created
131 /// and the \p will be the only attribute set. Otherwise, any existing
132 /// attributes for the specified parameter remain set and the attribute
133 /// given by \p attr is also set.
134 /// @brief Add a single ParameterAttribute
135 void addAttribute(uint16_t param_index, ParameterAttributes attr);
137 /// This method will remove the \p attr to the parameter with index
138 /// \p param_index. If the parameter index does not exist in the list,
139 /// an assertion will occur. If the specified attribute is the last
140 /// attribute set for the specified parameter index, the attributes for
141 /// that index are removed completely from the list (size is decremented).
142 /// Otherwise, the specified attribute is removed from the set of attributes
143 /// for the given index.
144 /// @brief Remove a single ParameterAttribute
145 void removeAttribute(uint16_t param_index, ParameterAttributes attr);
147 /// This is identical to addAttribute but permits you to set multiple
148 /// attributes at the same time. The \p attrs value is expected to be a
149 /// bitwise OR of the attributes you would like to have added.
150 /// @brief Insert ParameterAttributes for an index
151 void addAttributes(uint16_t param_index, uint16_t attrs);
153 /// This is identical to removeAttribute but permits you to remove multiple
154 /// attributes at the same time. The\p attrs value is expected to be a
155 /// bitwise OR of the attribute syou would like to have removed.
156 /// @brief Remove ParameterAttributes for an index
157 void removeAttributes(uint16_t param_index, uint16_t attrs);
163 /// This is an internal structure used to associate the ParameterAttributes
164 /// with a parameter index.
165 /// @brief ParameterAttributes with a parameter index.
166 struct ParamAttrsWithIndex {
167 uint16_t attrs; ///< The attributes that are set, |'d together
168 uint16_t index; ///< Index of the parameter for which the attributes apply
171 SmallVector<ParamAttrsWithIndex,2> attrs; ///< The list of attributes
175 } // End llvm namespace