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