dff48190235612b34b1460f9578258e8f25604b6
[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   /// \brief Attribute that may be applied to the function itself.  These cannot
121   /// be used on return values or function parameters.
122   bool hasFunctionOnlyAttrs() const {
123     return hasAttribute(Attribute::NoReturn) ||
124       hasAttribute(Attribute::NoUnwind) ||
125       hasAttribute(Attribute::ReadNone) ||
126       hasAttribute(Attribute::ReadOnly) ||
127       hasAttribute(Attribute::NoInline) ||
128       hasAttribute(Attribute::AlwaysInline) ||
129       hasAttribute(Attribute::OptimizeForSize) ||
130       hasAttribute(Attribute::StackProtect) ||
131       hasAttribute(Attribute::StackProtectReq) ||
132       hasAttribute(Attribute::NoRedZone) ||
133       hasAttribute(Attribute::NoImplicitFloat) ||
134       hasAttribute(Attribute::Naked) ||
135       hasAttribute(Attribute::InlineHint) ||
136       hasAttribute(Attribute::StackAlignment) ||
137       hasAttribute(Attribute::UWTable) ||
138       hasAttribute(Attribute::NonLazyBind) ||
139       hasAttribute(Attribute::ReturnsTwice) ||
140       hasAttribute(Attribute::AddressSafety) ||
141       hasAttribute(Attribute::MinSize);
142   }
143
144   bool operator==(const Attribute &A) const {
145     return Attrs == A.Attrs;
146   }
147   bool operator!=(const Attribute &A) const {
148     return Attrs != A.Attrs;
149   }
150
151   uint64_t Raw() const;
152
153   /// \brief Which attributes cannot be applied to a type.
154   static Attribute typeIncompatible(Type *Ty);
155
156   /// \brief This returns an integer containing an encoding of all the LLVM
157   /// attributes found in the given attribute bitset.  Any change to this
158   /// encoding is a breaking change to bitcode compatibility.
159   static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs);
160
161   /// \brief This returns an attribute bitset containing the LLVM attributes
162   /// that have been decoded from the given integer.  This function must stay in
163   /// sync with 'encodeLLVMAttributesForBitcode'.
164   static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
165                                                    uint64_t EncodedAttrs);
166
167   /// \brief The set of attributes set in Attribute is converted to a string of
168   /// equivalent mnemonics. This is, presumably, for writing out the mnemonics
169   /// for the assembly writer.  @brief Convert attribute bits to text
170   std::string getAsString() const;
171 };
172
173 //===----------------------------------------------------------------------===//
174 /// AttrBuilder - This class is used in conjunction with the Attribute::get
175 /// method to create an Attribute object. The object itself is uniquified. The
176 /// Builder's value, however, is not. So this can be used as a quick way to test
177 /// for equality, presence of attributes, etc.
178 class AttrBuilder {
179   uint64_t Bits;
180 public:
181   AttrBuilder() : Bits(0) {}
182   explicit AttrBuilder(uint64_t B) : Bits(B) {}
183   AttrBuilder(const Attribute &A) : Bits(A.Raw()) {}
184
185   void clear() { Bits = 0; }
186
187   /// addAttribute - Add an attribute to the builder.
188   AttrBuilder &addAttribute(Attribute::AttrVal Val);
189
190   /// removeAttribute - Remove an attribute from the builder.
191   AttrBuilder &removeAttribute(Attribute::AttrVal Val);
192
193   /// addAttribute - Add the attributes from A to the builder.
194   AttrBuilder &addAttributes(const Attribute &A);
195
196   /// removeAttribute - Remove the attributes from A from the builder.
197   AttrBuilder &removeAttributes(const Attribute &A);
198
199   /// hasAttribute - Return true if the builder has the specified attribute.
200   bool hasAttribute(Attribute::AttrVal A) const;
201
202   /// hasAttributes - Return true if the builder has IR-level attributes.
203   bool hasAttributes() const;
204
205   /// hasAttributes - Return true if the builder has any attribute that's in the
206   /// specified attribute.
207   bool hasAttributes(const Attribute &A) const;
208
209   /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
210   bool hasAlignmentAttr() const;
211
212   /// getAlignment - Retrieve the alignment attribute, if it exists.
213   uint64_t getAlignment() const;
214
215   /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
216   uint64_t getStackAlignment() const;
217
218   /// addAlignmentAttr - This turns an int alignment (which must be a power of
219   /// 2) into the form used internally in Attribute.
220   AttrBuilder &addAlignmentAttr(unsigned Align);
221
222   /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
223   /// power of 2) into the form used internally in Attribute.
224   AttrBuilder &addStackAlignmentAttr(unsigned Align);
225
226   /// addRawValue - Add the raw value to the internal representation.
227   /// N.B. This should be used ONLY for decoding LLVM bitcode!
228   AttrBuilder &addRawValue(uint64_t Val);
229
230   /// @brief Remove attributes that are used on functions only.
231   void removeFunctionOnlyAttrs() {
232     removeAttribute(Attribute::NoReturn)
233       .removeAttribute(Attribute::NoUnwind)
234       .removeAttribute(Attribute::ReadNone)
235       .removeAttribute(Attribute::ReadOnly)
236       .removeAttribute(Attribute::NoInline)
237       .removeAttribute(Attribute::AlwaysInline)
238       .removeAttribute(Attribute::OptimizeForSize)
239       .removeAttribute(Attribute::StackProtect)
240       .removeAttribute(Attribute::StackProtectReq)
241       .removeAttribute(Attribute::NoRedZone)
242       .removeAttribute(Attribute::NoImplicitFloat)
243       .removeAttribute(Attribute::Naked)
244       .removeAttribute(Attribute::InlineHint)
245       .removeAttribute(Attribute::StackAlignment)
246       .removeAttribute(Attribute::UWTable)
247       .removeAttribute(Attribute::NonLazyBind)
248       .removeAttribute(Attribute::ReturnsTwice)
249       .removeAttribute(Attribute::AddressSafety)
250       .removeAttribute(Attribute::MinSize);
251   }
252
253   uint64_t Raw() const { return Bits; }
254
255   bool operator==(const AttrBuilder &B) {
256     return Bits == B.Bits;
257   }
258   bool operator!=(const AttrBuilder &B) {
259     return Bits != B.Bits;
260   }
261 };
262
263 //===----------------------------------------------------------------------===//
264 // AttributeWithIndex
265 //===----------------------------------------------------------------------===//
266
267 /// AttributeWithIndex - This is just a pair of values to associate a set of
268 /// attributes with an index.
269 struct AttributeWithIndex {
270   Attribute Attrs;  ///< The attributes that are set, or'd together.
271   unsigned Index;    ///< Index of the parameter for which the attributes apply.
272                      ///< Index 0 is used for return value attributes.
273                      ///< Index ~0U is used for function attributes.
274
275   static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
276                                 ArrayRef<Attribute::AttrVal> Attrs) {
277     return get(Idx, Attribute::get(C, Attrs));
278   }
279   static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
280     AttributeWithIndex P;
281     P.Index = Idx;
282     P.Attrs = Attrs;
283     return P;
284   }
285 };
286
287 //===----------------------------------------------------------------------===//
288 // AttributeSet Smart Pointer
289 //===----------------------------------------------------------------------===//
290
291 class AttributeListImpl;
292
293 /// AttributeSet - This class manages the ref count for the opaque
294 /// AttributeListImpl object and provides accessors for it.
295 class AttributeSet {
296 public:
297   enum AttrIndex {
298     ReturnIndex = 0U,
299     FunctionIndex = ~0U
300   };
301 private:
302   /// @brief The attributes that we are managing.  This can be null to represent
303   /// the empty attributes list.
304   AttributeListImpl *AttrList;
305
306   /// @brief The attributes for the specified index are returned.  Attributes
307   /// for the result are denoted with Idx = 0.
308   Attribute getAttributes(unsigned Idx) const;
309
310   explicit AttributeSet(AttributeListImpl *LI) : AttrList(LI) {}
311 public:
312   AttributeSet() : AttrList(0) {}
313   AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
314   const AttributeSet &operator=(const AttributeSet &RHS);
315
316   //===--------------------------------------------------------------------===//
317   // Attribute List Construction and Mutation
318   //===--------------------------------------------------------------------===//
319
320   /// get - Return an AttributeSet with the specified parameters in it.
321   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
322
323   /// addAttr - Add the specified attribute at the specified index to this
324   /// attribute list.  Since attribute lists are immutable, this
325   /// returns the new list.
326   AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
327
328   /// removeAttr - Remove the specified attribute at the specified index from
329   /// this attribute list.  Since attribute lists are immutable, this
330   /// returns the new list.
331   AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
332
333   //===--------------------------------------------------------------------===//
334   // Attribute List Accessors
335   //===--------------------------------------------------------------------===//
336   /// getParamAttributes - The attributes for the specified index are
337   /// returned.
338   Attribute getParamAttributes(unsigned Idx) const {
339     return getAttributes(Idx);
340   }
341
342   /// getRetAttributes - The attributes for the ret value are
343   /// returned.
344   Attribute getRetAttributes() const {
345     return getAttributes(ReturnIndex);
346   }
347
348   /// getFnAttributes - The function attributes are returned.
349   Attribute getFnAttributes() const {
350     return getAttributes(FunctionIndex);
351   }
352
353   /// paramHasAttr - Return true if the specified parameter index has the
354   /// specified attribute set.
355   bool paramHasAttr(unsigned Idx, Attribute Attr) const {
356     return getAttributes(Idx).hasAttributes(Attr);
357   }
358
359   /// getParamAlignment - Return the alignment for the specified function
360   /// parameter.
361   unsigned getParamAlignment(unsigned Idx) const {
362     return getAttributes(Idx).getAlignment();
363   }
364
365   /// hasAttrSomewhere - Return true if the specified attribute is set for at
366   /// least one parameter or for the return value.
367   bool hasAttrSomewhere(Attribute::AttrVal Attr) const;
368
369   unsigned getNumAttrs() const;
370   Attribute &getAttributesAtIndex(unsigned i) const;
371
372   /// operator==/!= - Provide equality predicates.
373   bool operator==(const AttributeSet &RHS) const
374   { return AttrList == RHS.AttrList; }
375   bool operator!=(const AttributeSet &RHS) const
376   { return AttrList != RHS.AttrList; }
377
378   //===--------------------------------------------------------------------===//
379   // Attribute List Introspection
380   //===--------------------------------------------------------------------===//
381
382   /// getRawPointer - Return a raw pointer that uniquely identifies this
383   /// attribute list.
384   void *getRawPointer() const {
385     return AttrList;
386   }
387
388   // Attributes are stored as a dense set of slots, where there is one slot for
389   // each argument that has an attribute.  This allows walking over the dense
390   // set instead of walking the sparse list of attributes.
391
392   /// isEmpty - Return true if there are no attributes.
393   ///
394   bool isEmpty() const {
395     return AttrList == 0;
396   }
397
398   /// getNumSlots - Return the number of slots used in this attribute list.
399   /// This is the number of arguments that have an attribute set on them
400   /// (including the function itself).
401   unsigned getNumSlots() const;
402
403   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
404   /// holds a index number plus a set of attributes.
405   const AttributeWithIndex &getSlot(unsigned Slot) const;
406
407   void dump() const;
408 };
409
410 } // End llvm namespace
411
412 #endif