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