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