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