Move llvm/Support/MDBuilder.h to llvm/MDBuilder.h, to live with
[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 "llvm/ADT/ArrayRef.h"
20 #include <cassert>
21 #include <string>
22
23 namespace llvm {
24 class Type;
25
26 namespace Attribute {
27 /// We use this proxy POD type to allow constructing Attributes constants
28 /// using initializer lists. Do not use this class directly.
29 struct AttrConst {
30   uint64_t v;
31   AttrConst operator | (const AttrConst Attrs) const {
32     AttrConst Res = {v | Attrs.v};
33     return Res;
34   }
35   AttrConst operator ~ () const {
36     AttrConst Res = {~v};
37     return Res;
38   }
39 };
40 }  // namespace Attribute
41
42
43 /// Attributes - A bitset of attributes.
44 class Attributes {
45  public:
46   Attributes() : Bits(0) { }
47   explicit Attributes(uint64_t Val) : Bits(Val) { }
48   /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) { }
49   // This is a "safe bool() operator".
50   operator const void *() const { return Bits ? this : 0; }
51   bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; }
52   bool operator == (const Attributes &Attrs) const {
53     return Bits == Attrs.Bits;
54   }
55   bool operator != (const Attributes &Attrs) const {
56     return Bits != Attrs.Bits;
57   }
58   Attributes operator | (const Attributes &Attrs) const {
59     return Attributes(Bits | Attrs.Bits);
60   }
61   Attributes operator & (const Attributes &Attrs) const {
62     return Attributes(Bits & Attrs.Bits);
63   }
64   Attributes operator ^ (const Attributes &Attrs) const {
65     return Attributes(Bits ^ Attrs.Bits);
66   }
67   Attributes &operator |= (const Attributes &Attrs) {
68     Bits |= Attrs.Bits;
69     return *this;
70   }
71   Attributes &operator &= (const Attributes &Attrs) {
72     Bits &= Attrs.Bits;
73     return *this;
74   }
75   Attributes operator ~ () const { return Attributes(~Bits); }
76   uint64_t Raw() const { return Bits; }
77  private:
78   // Currently, we need less than 64 bits.
79   uint64_t Bits;
80 };
81
82 namespace Attribute {
83
84 /// Function parameters and results can have attributes to indicate how they
85 /// should be treated by optimizations and code generation. This enumeration
86 /// lists the attributes that can be associated with parameters, function
87 /// results or the function itself.
88 /// @brief Function attributes.
89
90 // We declare AttrConst objects that will be used throughout the code
91 // and also raw uint64_t objects with _i suffix to be used below for other
92 // constant declarations. This is done to avoid static CTORs and at the same
93 // time to keep type-safety of Attributes.
94 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
95   const uint64_t name##_i = value; \
96   const AttrConst name = {value};
97
98 DECLARE_LLVM_ATTRIBUTE(None,0)    ///< No attributes have been set
99 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
100 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
101 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
102 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
103 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
104 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
105 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
106 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
107 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
108 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
109 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
110 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
111 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
112 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
113 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
114 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
115 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
116                                      // stored as log2 of alignment with +1 bias
117                                      // 0 means unaligned different from align 1
118 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
119 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
120 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
121                                            /// instructions.
122 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
123 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
124                                            ///desirable
125 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
126                                            ///function (3 bits) stored as log2
127                                            ///of alignment with +1 bias
128                                            ///0 means unaligned (different from
129                                            ///alignstack= {1))
130 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
131 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
132                                            ///table
133 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
134                                             /// often, so lazy binding isn't
135                                             /// worthwhile.
136 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
137
138 #undef DECLARE_LLVM_ATTRIBUTE
139
140 /// Note that uwtable is about the ABI or the user mandating an entry in the
141 /// unwind table. The nounwind attribute is about an exception passing by the
142 /// function.
143 /// In a theoretical system that uses tables for profiling and sjlj for
144 /// exceptions, they would be fully independent. In a normal system that
145 /// uses tables for both, the semantics are:
146 /// nil                = Needs an entry because an exception might pass by.
147 /// nounwind           = No need for an entry
148 /// uwtable            = Needs an entry because the ABI says so and because
149 ///                      an exception might pass by.
150 /// uwtable + nounwind = Needs an entry because the ABI says so.
151
152 /// @brief Attributes that only apply to function parameters.
153 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
154     StructRet_i | NoCapture_i};
155
156 /// @brief Attributes that may be applied to the function itself.  These cannot
157 /// be used on return values or function parameters.
158 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
159   ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
160   StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
161   Naked_i | InlineHint_i | StackAlignment_i |
162   UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
163
164 /// @brief Parameter attributes that do not apply to vararg call arguments.
165 const AttrConst VarArgsIncompatible = {StructRet_i};
166
167 /// @brief Attributes that are mutually incompatible.
168 const AttrConst MutuallyIncompatible[4] = {
169   {ByVal_i | InReg_i | Nest_i | StructRet_i},
170   {ZExt_i  | SExt_i},
171   {ReadNone_i | ReadOnly_i},
172   {NoInline_i | AlwaysInline_i}
173 };
174
175 /// @brief Which attributes cannot be applied to a type.
176 Attributes typeIncompatible(Type *Ty);
177
178 /// This turns an int alignment (a power of 2, normally) into the
179 /// form used internally in Attributes.
180 inline Attributes constructAlignmentFromInt(unsigned i) {
181   // Default alignment, allow the target to define how to align it.
182   if (i == 0)
183     return None;
184
185   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
186   assert(i <= 0x40000000 && "Alignment too large.");
187   return Attributes((Log2_32(i)+1) << 16);
188 }
189
190 /// This returns the alignment field of an attribute as a byte alignment value.
191 inline unsigned getAlignmentFromAttrs(Attributes A) {
192   Attributes Align = A & Attribute::Alignment;
193   if (!Align)
194     return 0;
195
196   return 1U << ((Align.Raw() >> 16) - 1);
197 }
198
199 /// This turns an int stack alignment (which must be a power of 2) into
200 /// the form used internally in Attributes.
201 inline Attributes constructStackAlignmentFromInt(unsigned i) {
202   // Default alignment, allow the target to define how to align it.
203   if (i == 0)
204     return None;
205
206   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
207   assert(i <= 0x100 && "Alignment too large.");
208   return Attributes((Log2_32(i)+1) << 26);
209 }
210
211 /// This returns the stack alignment field of an attribute as a byte alignment
212 /// value.
213 inline unsigned getStackAlignmentFromAttrs(Attributes A) {
214   Attributes StackAlign = A & Attribute::StackAlignment;
215   if (!StackAlign)
216     return 0;
217
218   return 1U << ((StackAlign.Raw() >> 26) - 1);
219 }
220
221 /// This returns an integer containing an encoding of all the
222 /// LLVM attributes found in the given attribute bitset.  Any
223 /// change to this encoding is a breaking change to bitcode
224 /// compatibility.
225 inline uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
226   // FIXME: It doesn't make sense to store the alignment information as an
227   // expanded out value, we should store it as a log2 value.  However, we can't
228   // just change that here without breaking bitcode compatibility.  If this ever
229   // becomes a problem in practice, we should introduce new tag numbers in the
230   // bitcode file and have those tags use a more efficiently encoded alignment
231   // field.
232
233   // Store the alignment in the bitcode as a 16-bit raw value instead of a
234   // 5-bit log2 encoded value. Shift the bits above the alignment up by
235   // 11 bits.
236
237   uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
238   if (Attrs & Attribute::Alignment)
239     EncodedAttrs |= (1ull << 16) <<
240       (((Attrs & Attribute::Alignment).Raw()-1) >> 16);
241   EncodedAttrs |= (Attrs.Raw() & (0xfffull << 21)) << 11;
242
243   return EncodedAttrs;
244 }
245
246 /// This returns an attribute bitset containing the LLVM attributes
247 /// that have been decoded from the given integer.  This function
248 /// must stay in sync with 'encodeLLVMAttributesForBitcode'.
249 inline Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
250   // The alignment is stored as a 16-bit raw value from bits 31--16.
251   // We shift the bits above 31 down by 11 bits.
252
253   unsigned Alignment = (EncodedAttrs & (0xffffull << 16)) >> 16;
254   assert((!Alignment || isPowerOf2_32(Alignment)) &&
255          "Alignment must be a power of two.");
256
257   Attributes Attrs(EncodedAttrs & 0xffff);
258   if (Alignment)
259     Attrs |= Attribute::constructAlignmentFromInt(Alignment);
260   Attrs |= Attributes((EncodedAttrs & (0xfffull << 32)) >> 11);
261
262   return Attrs;
263 }
264
265
266 /// The set of Attributes set in Attributes is converted to a
267 /// string of equivalent mnemonics. This is, presumably, for writing out
268 /// the mnemonics for the assembly writer.
269 /// @brief Convert attribute bits to text
270 std::string getAsString(Attributes Attrs);
271 } // end namespace Attribute
272
273 /// This is just a pair of values to associate a set of attributes
274 /// with an index.
275 struct AttributeWithIndex {
276   Attributes Attrs; ///< The attributes that are set, or'd together.
277   unsigned Index; ///< Index of the parameter for which the attributes apply.
278                   ///< Index 0 is used for return value attributes.
279                   ///< Index ~0U is used for function attributes.
280
281   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
282     AttributeWithIndex P;
283     P.Index = Idx;
284     P.Attrs = Attrs;
285     return P;
286   }
287 };
288
289 //===----------------------------------------------------------------------===//
290 // AttrListPtr Smart Pointer
291 //===----------------------------------------------------------------------===//
292
293 class AttributeListImpl;
294
295 /// AttrListPtr - This class manages the ref count for the opaque
296 /// AttributeListImpl object and provides accessors for it.
297 class AttrListPtr {
298   /// AttrList - The attributes that we are managing.  This can be null
299   /// to represent the empty attributes list.
300   AttributeListImpl *AttrList;
301 public:
302   AttrListPtr() : AttrList(0) {}
303   AttrListPtr(const AttrListPtr &P);
304   const AttrListPtr &operator=(const AttrListPtr &RHS);
305   ~AttrListPtr();
306
307   //===--------------------------------------------------------------------===//
308   // Attribute List Construction and Mutation
309   //===--------------------------------------------------------------------===//
310
311   /// get - Return a Attributes list with the specified parameters in it.
312   static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
313
314   /// addAttr - Add the specified attribute at the specified index to this
315   /// attribute list.  Since attribute lists are immutable, this
316   /// returns the new list.
317   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
318
319   /// removeAttr - Remove the specified attribute at the specified index from
320   /// this attribute list.  Since attribute lists are immutable, this
321   /// returns the new list.
322   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
323
324   //===--------------------------------------------------------------------===//
325   // Attribute List Accessors
326   //===--------------------------------------------------------------------===//
327   /// getParamAttributes - The attributes for the specified index are
328   /// returned.
329   Attributes getParamAttributes(unsigned Idx) const {
330     assert (Idx && Idx != ~0U && "Invalid parameter index!");
331     return getAttributes(Idx);
332   }
333
334   /// getRetAttributes - The attributes for the ret value are
335   /// returned.
336   Attributes getRetAttributes() const {
337     return getAttributes(0);
338   }
339
340   /// getFnAttributes - The function attributes are returned.
341   Attributes getFnAttributes() const {
342     return getAttributes(~0U);
343   }
344
345   /// paramHasAttr - Return true if the specified parameter index has the
346   /// specified attribute set.
347   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
348     return getAttributes(Idx) & Attr;
349   }
350
351   /// getParamAlignment - Return the alignment for the specified function
352   /// parameter.
353   unsigned getParamAlignment(unsigned Idx) const {
354     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
355   }
356
357   /// hasAttrSomewhere - Return true if the specified attribute is set for at
358   /// least one parameter or for the return value.
359   bool hasAttrSomewhere(Attributes Attr) const;
360
361   /// operator==/!= - Provide equality predicates.
362   bool operator==(const AttrListPtr &RHS) const
363   { return AttrList == RHS.AttrList; }
364   bool operator!=(const AttrListPtr &RHS) const
365   { return AttrList != RHS.AttrList; }
366
367   void dump() const;
368
369   //===--------------------------------------------------------------------===//
370   // Attribute List Introspection
371   //===--------------------------------------------------------------------===//
372
373   /// getRawPointer - Return a raw pointer that uniquely identifies this
374   /// attribute list.
375   void *getRawPointer() const {
376     return AttrList;
377   }
378
379   // Attributes are stored as a dense set of slots, where there is one
380   // slot for each argument that has an attribute.  This allows walking over the
381   // dense set instead of walking the sparse list of attributes.
382
383   /// isEmpty - Return true if there are no attributes.
384   ///
385   bool isEmpty() const {
386     return AttrList == 0;
387   }
388
389   /// getNumSlots - Return the number of slots used in this attribute list.
390   /// This is the number of arguments that have an attribute set on them
391   /// (including the function itself).
392   unsigned getNumSlots() const;
393
394   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
395   /// holds a index number plus a set of attributes.
396   const AttributeWithIndex &getSlot(unsigned Slot) const;
397
398 private:
399   explicit AttrListPtr(AttributeListImpl *L);
400
401   /// getAttributes - The attributes for the specified index are
402   /// returned.  Attributes for the result are denoted with Idx = 0.
403   Attributes getAttributes(unsigned Idx) const;
404
405 };
406
407 } // End llvm namespace
408
409 #endif