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