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