Reimplement the parameter attributes support, phase #1. hilights:
[oota-llvm.git] / include / llvm / ParameterAttributes.h
1 //===-- llvm/ParameterAttributes.h - Container for ParamAttrs ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the simple types necessary to represent the parameter
11 // attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_PARAMETER_ATTRIBUTES_H
16 #define LLVM_PARAMETER_ATTRIBUTES_H
17
18 #include <string>
19
20 namespace llvm {
21 class Type;
22
23 /// ParameterAttributes - A bitset of attributes for a parameter.
24 typedef unsigned ParameterAttributes;
25   
26 namespace ParamAttr {
27
28 /// Function parameters and results can have attributes to indicate how they 
29 /// should be treated by optimizations and code generation. This enumeration 
30 /// lists the attributes that can be associated with parameters or function 
31 /// results.
32 /// @brief Function parameter attributes.
33 typedef ParameterAttributes Attributes;
34
35 const Attributes None      = 0;     ///< No attributes have been set
36 const Attributes ZExt      = 1<<0;  ///< Zero extended before/after call
37 const Attributes SExt      = 1<<1;  ///< Sign extended before/after call
38 const Attributes NoReturn  = 1<<2;  ///< Mark the function as not returning
39 const Attributes InReg     = 1<<3;  ///< Force argument to be passed in register
40 const Attributes StructRet = 1<<4;  ///< Hidden pointer to structure to return
41 const Attributes NoUnwind  = 1<<5;  ///< Function doesn't unwind stack
42 const Attributes NoAlias   = 1<<6;  ///< Considered to not alias after call
43 const Attributes ByVal     = 1<<7;  ///< Pass structure by value
44 const Attributes Nest      = 1<<8;  ///< Nested function static chain
45 const Attributes ReadNone  = 1<<9;  ///< Function does not access memory
46 const Attributes ReadOnly  = 1<<10; ///< Function only reads from memory
47 const Attributes Alignment = 0xffff<<16; ///< Alignment of parameter (16 bits)
48                                     // 0 = unknown, else in clear (not log)
49
50 /// @brief Attributes that only apply to function parameters.
51 const Attributes ParameterOnly = ByVal | InReg | Nest | StructRet;
52
53 /// @brief Attributes that only apply to function return values.
54 const Attributes ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
55
56 /// @brief Parameter attributes that do not apply to vararg call arguments.
57 const Attributes VarArgsIncompatible = StructRet;
58
59 /// @brief Attributes that are mutually incompatible.
60 const Attributes MutuallyIncompatible[3] = {
61   ByVal | InReg | Nest  | StructRet,
62   ZExt  | SExt,
63   ReadNone | ReadOnly
64 };
65
66 /// @brief Which attributes cannot be applied to a type.
67 Attributes typeIncompatible(const Type *Ty);
68
69 /// This turns an int alignment (a power of 2, normally) into the
70 /// form used internally in ParameterAttributes.
71 ParamAttr::Attributes inline constructAlignmentFromInt(unsigned i) {
72   return (i << 16);
73 }
74
75 /// The set of ParameterAttributes set in Attributes is converted to a
76 /// string of equivalent mnemonics. This is, presumably, for writing out
77 /// the mnemonics for the assembly writer. 
78 /// @brief Convert parameter attribute bits to text
79 std::string getAsString(ParameterAttributes Attrs);
80 } // end namespace ParamAttr
81
82
83 /// This is just a pair of values to associate a set of parameter attributes
84 /// with a parameter index. 
85 struct ParamAttrsWithIndex {
86   ParameterAttributes Attrs; ///< The attributes that are set, or'd together.
87   unsigned Index; ///< Index of the parameter for which the attributes apply.
88   
89   static ParamAttrsWithIndex get(unsigned Idx, ParameterAttributes Attrs) {
90     ParamAttrsWithIndex P;
91     P.Index = Idx;
92     P.Attrs = Attrs;
93     return P;
94   }
95 };
96   
97 //===----------------------------------------------------------------------===//
98 // PAListPtr Smart Pointer
99 //===----------------------------------------------------------------------===//
100
101 class ParamAttributeListImpl;
102   
103 /// PAListPtr - This class manages the ref count for the opaque 
104 /// ParamAttributeListImpl object and provides accessors for it.
105 class PAListPtr {
106   /// PAList - The parameter attributes that we are managing.  This can be null
107   /// to represent the empty parameter attributes list.
108   ParamAttributeListImpl *PAList;
109 public:
110   PAListPtr() : PAList(0) {}
111   PAListPtr(const PAListPtr &P);
112   const PAListPtr &operator=(const PAListPtr &RHS);
113   ~PAListPtr();
114   
115   //===--------------------------------------------------------------------===//
116   // Parameter Attribute List Construction and Mutation
117   //===--------------------------------------------------------------------===//
118   
119   /// get - Return a ParamAttrs list with the specified parameter in it.
120   static PAListPtr get(const ParamAttrsWithIndex *Attr, unsigned NumAttrs);
121   
122   /// get - Return a ParamAttr list with the parameters specified by the
123   /// consequtive random access iterator range.
124   template <typename Iter>
125   static PAListPtr get(const Iter &I, const Iter &E) {
126     if (I == E) return PAListPtr();  // Empty list.
127     return get(&*I, E-I);
128   }
129
130   /// addAttr - Add the specified attribute at the specified index to this
131   /// attribute list.  Since parameter attribute lists are immutable, this
132   /// returns the new list.
133   PAListPtr addAttr(unsigned Idx, ParameterAttributes Attrs) const;
134   
135   /// removeAttr - Remove the specified attribute at the specified index from
136   /// this attribute list.  Since parameter attribute lists are immutable, this
137   /// returns the new list.
138   PAListPtr removeAttr(unsigned Idx, ParameterAttributes Attrs) const;
139   
140   //===--------------------------------------------------------------------===//
141   // Parameter Attribute List Accessors
142   //===--------------------------------------------------------------------===//
143   
144   /// getParamAttrs - The parameter attributes for the specified parameter are
145   /// returned.  Parameters for the result are denoted with Idx = 0.
146   ParameterAttributes getParamAttrs(unsigned Idx) const;
147   
148   /// paramHasAttr - Return true if the specified parameter index has the
149   /// specified attribute set.
150   bool paramHasAttr(unsigned Idx, ParameterAttributes Attr) const {
151     return getParamAttrs(Idx) & Attr;
152   }
153   
154   /// getParamAlignment - Return the alignment for the specified function
155   /// parameter.
156   unsigned getParamAlignment(unsigned Idx) const {
157     return (getParamAttrs(Idx) & ParamAttr::Alignment) >> 16;
158   }
159   
160   /// hasAttrSomewhere - Return true if the specified attribute is set for at
161   /// least one parameter or for the return value.
162   bool hasAttrSomewhere(ParameterAttributes Attr) const;
163
164   /// operator< - Provide an ordering for parameter attribute lists.
165   bool operator==(const PAListPtr &RHS) const { return PAList == RHS.PAList; }
166   bool operator!=(const PAListPtr &RHS) const { return PAList != RHS.PAList; }
167   
168   void dump() const;
169
170   //===--------------------------------------------------------------------===//
171   // Parameter Attribute List Introspection
172   //===--------------------------------------------------------------------===//
173   
174   /// getRawPointer - Return a raw pointer that uniquely identifies this
175   /// parameter attribute list. 
176   void *getRawPointer() const {
177     return PAList;
178   }
179   
180   // Parameter attributes are stored as a dense set of slots, where there is one
181   // slot for each argument that has an attribute.  This allows walking over the
182   // dense set instead of walking the sparse list of attributes.
183   
184   /// isEmpty - Return true if no parameters have an attribute.
185   ///
186   bool isEmpty() const {
187     return PAList == 0;
188   }
189   
190   /// getNumSlots - Return the number of slots used in this attribute list. 
191   /// This is the number of arguments that have an attribute set on them
192   /// (including the function itself).
193   unsigned getNumSlots() const;
194   
195   /// getSlot - Return the ParamAttrsWithIndex at the specified slot.  This
196   /// holds a parameter number plus a set of attributes.
197   const ParamAttrsWithIndex &getSlot(unsigned Slot) const;
198   
199 private:
200   explicit PAListPtr(ParamAttributeListImpl *L);
201 };
202
203 } // End llvm namespace
204
205 #endif