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