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