50a4cef9ea91d752bf2fba9f8d159bd70fa918f5
[oota-llvm.git] / include / llvm / Attributes.h
1 //===-- llvm/Attributes.h - Container for Attributes ---*---------- 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
11 // attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
17
18 #include <cassert>
19 #include <string>
20
21 namespace llvm {
22 class Type;
23
24 /// Attributes - A bitset of attributes.
25 typedef unsigned Attributes;
26   
27 namespace Attribute {
28
29 /// Function parameters and results can have attributes to indicate how they 
30 /// should be treated by optimizations and code generation. This enumeration 
31 /// lists the attributes that can be associated with parameters, function 
32 /// results or the function itself.
33 /// @brief Function 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 NoInline        = 1<<11; // inline=never 
48 const Attributes AlwaysInline    = 1<<12; // inline=always
49 const Attributes OptimizeForSize = 1<<13; // opt_size
50 const Attributes Alignment = 0xffff<<16; ///< Alignment of parameter (16 bits)
51                                     // 0 = unknown, else in clear (not log)
52                                     
53 /// @brief Attributes that only apply to function parameters.
54 const Attributes ParameterOnly = ByVal | Nest | StructRet;
55
56 /// @brief Attributes that only apply to function.
57 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
58
59 /// @brief Parameter attributes that do not apply to vararg call arguments.
60 const Attributes VarArgsIncompatible = StructRet;
61
62 /// @brief Attributes that are mutually incompatible.
63 const Attributes MutuallyIncompatible[4] = {
64   ByVal | InReg | Nest  | StructRet,
65   ZExt  | SExt,
66   ReadNone | ReadOnly,
67   NoInline | AlwaysInline
68 };
69
70 /// @brief Which attributes cannot be applied to a type.
71 Attributes typeIncompatible(const Type *Ty);
72
73 /// This turns an int alignment (a power of 2, normally) into the
74 /// form used internally in Attributes.
75 inline Attributes constructAlignmentFromInt(unsigned i) {
76   return (i << 16);
77 }
78
79 /// The set of Attributes set in Attributes is converted to a
80 /// string of equivalent mnemonics. This is, presumably, for writing out
81 /// the mnemonics for the assembly writer. 
82 /// @brief Convert attribute bits to text
83 std::string getAsString(Attributes Attrs);
84 } // end namespace Attribute
85
86 namespace Attribute {
87 } // end namespace Attribute
88
89 /// This is just a pair of values to associate a set of attributes
90 /// with an index. 
91 struct AttributeWithIndex {
92   Attributes Attrs; ///< The attributes that are set, or'd together.
93   unsigned Index; ///< Index of the parameter for which the attributes apply.
94                   ///< Index 0 is used for return value attributes.
95                   ///< Index ~0U is used for function attributes.
96   
97   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
98     AttributeWithIndex P;
99     P.Index = Idx;
100     P.Attrs = Attrs;
101     return P;
102   }
103 };
104   
105 //===----------------------------------------------------------------------===//
106 // AttrListPtr Smart Pointer
107 //===----------------------------------------------------------------------===//
108
109 class AttributeListImpl;
110   
111 /// AttrListPtr - This class manages the ref count for the opaque 
112 /// AttributeListImpl object and provides accessors for it.
113 class AttrListPtr {
114   /// AttrList - The attributes that we are managing.  This can be null
115   /// to represent the empty attributes list.
116   AttributeListImpl *AttrList;
117 public:
118   AttrListPtr() : AttrList(0) {}
119   AttrListPtr(const AttrListPtr &P);
120   const AttrListPtr &operator=(const AttrListPtr &RHS);
121   ~AttrListPtr();
122   
123   //===--------------------------------------------------------------------===//
124   // Attribute List Construction and Mutation
125   //===--------------------------------------------------------------------===//
126   
127   /// get - Return a Attributes list with the specified parameter in it.
128   static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
129   
130   /// get - Return a Attribute list with the parameters specified by the
131   /// consecutive random access iterator range.
132   template <typename Iter>
133   static AttrListPtr get(const Iter &I, const Iter &E) {
134     if (I == E) return AttrListPtr();  // Empty list.
135     return get(&*I, static_cast<unsigned>(E-I));
136   }
137
138   /// addAttr - Add the specified attribute at the specified index to this
139   /// attribute list.  Since attribute lists are immutable, this
140   /// returns the new list.
141   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
142   
143   /// removeAttr - Remove the specified attribute at the specified index from
144   /// this attribute list.  Since attribute lists are immutable, this
145   /// returns the new list.
146   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
147   
148   //===--------------------------------------------------------------------===//
149   // Attribute List Accessors
150   //===--------------------------------------------------------------------===//
151   /// getParamAttributes - The attributes for the specified index are
152   /// returned. 
153   Attributes getParamAttributes(unsigned Idx) const {
154     assert (Idx && Idx != ~0U && "Invalid parameter index!");
155     return getAttributes(Idx);
156   }
157
158   /// getRetAttributes - The attributes for the ret value are
159   /// returned. 
160   Attributes getRetAttributes() const {
161     return getAttributes(0);
162   }
163
164   /// getFnAttributes - The function attributes are  returned. 
165   Attributes getFnAttributes() const {
166     return getAttributes(~0);
167   }
168   
169   /// paramHasAttr - Return true if the specified parameter index has the
170   /// specified attribute set.
171   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
172     return getAttributes(Idx) & Attr;
173   }
174   
175   /// getParamAlignment - Return the alignment for the specified function
176   /// parameter.
177   unsigned getParamAlignment(unsigned Idx) const {
178     return (getAttributes(Idx) & Attribute::Alignment) >> 16;
179   }
180   
181   /// hasAttrSomewhere - Return true if the specified attribute is set for at
182   /// least one parameter or for the return value.
183   bool hasAttrSomewhere(Attributes Attr) const;
184
185   /// operator==/!= - Provide equality predicates.
186   bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
187   bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
188   
189   void dump() const;
190
191   //===--------------------------------------------------------------------===//
192   // Attribute List Introspection
193   //===--------------------------------------------------------------------===//
194   
195   /// getRawPointer - Return a raw pointer that uniquely identifies this
196   /// attribute list. 
197   void *getRawPointer() const {
198     return AttrList;
199   }
200   
201   // Attributes are stored as a dense set of slots, where there is one
202   // slot for each argument that has an attribute.  This allows walking over the
203   // dense set instead of walking the sparse list of attributes.
204   
205   /// isEmpty - Return true if there are no attributes.
206   ///
207   bool isEmpty() const {
208     return AttrList == 0;
209   }
210   
211   /// getNumSlots - Return the number of slots used in this attribute list. 
212   /// This is the number of arguments that have an attribute set on them
213   /// (including the function itself).
214   unsigned getNumSlots() const;
215   
216   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
217   /// holds a index number plus a set of attributes.
218   const AttributeWithIndex &getSlot(unsigned Slot) const;
219   
220 private:
221   explicit AttrListPtr(AttributeListImpl *L);
222
223   /// getAttributes - The attributes for the specified index are
224   /// returned.  Attributes for the result are denoted with Idx = 0.
225   Attributes getAttributes(unsigned Idx) const;
226
227 };
228
229 } // End llvm namespace
230
231 #endif