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