6f774e0819d1e8225386a174986bc7a071bd64a0
[oota-llvm.git] / include / llvm / ParameterAttributes.h
1 //===-- llvm/ParameterAttributes.h - Container for Param Attrs --*- 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
22 namespace llvm {
23
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
36 };
37
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 {
48   /// @name Accessors
49   /// @{
50   public:
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;
60
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;
67     }
68
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);
74
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));
79     }
80
81     /// @brief Comparison operator for ParamAttrsList
82     bool operator < (const ParamAttrsList& that) const {
83       if (this->attrs.size() < that.attrs.size())
84         return true;
85       if (this->attrs.size() > that.attrs.size())
86         return false;
87       for (unsigned i = 0; i < attrs.size(); ++i) {
88         if (attrs[i].index < that.attrs[i].index)
89           return true;
90         if (attrs[i].index > that.attrs[i].index)
91           return false;
92         if (attrs[i].attrs < that.attrs[i].attrs)
93           return true;
94         if (attrs[i].attrs > that.attrs[i].attrs)
95           return false;
96       }
97       return false;
98     }
99
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;
108     }
109
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
114     /// LLVM.
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(); }
118
119     /// Clients generally should not use this method. It is used internally by
120     /// LLVM.
121     /// @returns true if this ParamAttrsList is empty.
122     /// @brief Determine emptiness of ParamAttrsList.
123     unsigned empty() const { return attrs.empty(); }
124
125   /// @}
126   /// @name Mutators
127   /// @{
128   public:
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);
136
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);
146
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);
152
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);
158
159   /// @}
160   /// @name Data
161   /// @{
162   private:
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
169     };
170
171     SmallVector<ParamAttrsWithIndex,2> attrs; ///< The list of attributes
172   /// @}
173 };
174
175 } // End llvm namespace
176
177 #endif