For PR1146:
[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     /// Returns the parameter index of a particular parameter attribute in this
52     /// list of attributes. Note that the attr_index is an index into this 
53     /// class's list of attributes, not the index of the parameter. The result
54     /// is the index of the parameter. 
55     /// @brief Get a parameter index
56     uint16_t getParamIndex(unsigned attr_index) const {
57       return attrs[attr_index].index;
58     }
59
60     /// The parameter attributes for the \p indexth parameter are returned. 
61     /// The 0th parameter refers to the return type of the function. Note that
62     /// the \p param_index is an index into the function's parameters, not an
63     /// index into this class's list of attributes. The result of getParamIndex
64     /// is always suitable input to this function.
65     /// @returns The all the ParameterAttributes for the \p indexth parameter
66     /// as a uint16_t of enumeration values OR'd together.
67     /// @brief Get the attributes for a parameter
68     uint16_t getParamAttrs(uint16_t param_index) const;
69
70     /// This checks to see if the \p ith function parameter has the parameter
71     /// attribute given by \p attr set.
72     /// @returns true if the parameter attribute is set
73     /// @brief Determine if a ParameterAttributes is set
74     bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
75       return getParamAttrs(i) & attr;
76     }
77
78     /// Determines how many parameter attributes are set in this ParamAttrsList.
79     /// This says nothing about how many parameters the function has. It also
80     /// says nothing about the highest parameter index that has attributes.
81     /// @returns the number of parameter attributes in this ParamAttrsList.
82     /// @brief Return the number of parameter attributes this type has.
83     unsigned size() const { return attrs.size(); }
84
85     /// @returns true if this ParamAttrsList is empty.
86     /// @brief Determine emptiness of ParamAttrsList.
87     unsigned empty() const { return attrs.empty(); }
88
89     /// The set of ParameterAttributes set in Attributes is converted to a
90     /// string of equivalent mnemonics. This is, presumably, for writing out
91     /// the mnemonics for the assembly writer. 
92     /// @brief Convert parameter attribute bits to text
93     static std::string getParamAttrsText(uint16_t Attributes);
94
95     /// The \p Indexth parameter attribute is converted to string.
96     /// @brief Get the text for the parmeter attributes for one parameter.
97     std::string getParamAttrsTextByIndex(uint16_t Index) const {
98       return getParamAttrsText(getParamAttrs(Index));
99     }
100
101     /// @brief Comparison operator for ParamAttrsList
102     bool operator < (const ParamAttrsList& that) const {
103       if (this->attrs.size() < that.attrs.size())
104         return true;
105       if (this->attrs.size() > that.attrs.size())
106         return false;
107       for (unsigned i = 0; i < attrs.size(); ++i) {
108         if (attrs[i].index < that.attrs[i].index)
109           return true;
110         if (attrs[i].index > that.attrs[i].index)
111           return false;
112         if (attrs[i].attrs < that.attrs[i].attrs)
113           return true;
114         if (attrs[i].attrs > that.attrs[i].attrs)
115           return false;
116       }
117       return false;
118     }
119   /// @}
120   /// @name Mutators
121   /// @{
122   public:
123     /// This adds a pair to the list of parameter index and attribute pairs 
124     /// represented by this class. No check is made to determine whether 
125     /// param_index exists already. This pair is just added to the end. It is
126     /// the user's responsibility to insert the pairs wisely.
127     /// @brief Insert ParameterAttributes for an index
128     void insert(uint16_t param_index, uint16_t attrs);
129
130   /// @}
131   /// @name Data
132   /// @{
133   private:
134     /// This is an internal structure used to associate the ParameterAttributes
135     /// with a parameter index. 
136     /// @brief ParameterAttributes with a parameter index.
137     struct ParamAttrsWithIndex {
138       uint16_t attrs; ///< The attributes that are set, |'d together
139       uint16_t index; ///< Index of the parameter for which the attributes apply
140     };
141
142     SmallVector<ParamAttrsWithIndex,2> attrs; ///< The list of attributes
143   /// @}
144 };
145
146 } // End llvm namespace
147
148 #endif