eda274407af2019a362bbeb9a29e5d97a9807896
[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 // AttributeWithIndex
241 //===----------------------------------------------------------------------===//
242
243 /// AttributeWithIndex - This is just a pair of values to associate a set of
244 /// attributes with an index.
245 struct AttributeWithIndex {
246   Attribute Attrs;  ///< The attributes that are set, or'd together.
247   unsigned Index;    ///< Index of the parameter for which the attributes apply.
248                      ///< Index 0 is used for return value attributes.
249                      ///< Index ~0U is used for function attributes.
250
251   static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
252                                 ArrayRef<Attribute::AttrVal> Attrs) {
253     return get(Idx, Attribute::get(C, Attrs));
254   }
255   static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
256     AttributeWithIndex P;
257     P.Index = Idx;
258     P.Attrs = Attrs;
259     return P;
260   }
261 };
262
263 //===----------------------------------------------------------------------===//
264 // AttributeSet Smart Pointer
265 //===----------------------------------------------------------------------===//
266
267 class AttributeListImpl;
268
269 /// AttributeSet - This class manages the ref count for the opaque
270 /// AttributeListImpl 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   AttributeListImpl *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(AttributeListImpl *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   /// get - Return an AttributeSet with the specified parameters in it.
297   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
298
299   /// addAttr - Add the specified attribute at the specified index to this
300   /// attribute list.  Since attribute lists are immutable, this
301   /// returns the new list.
302   AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
303
304   /// removeAttr - Remove the specified attribute at the specified index from
305   /// this attribute list.  Since attribute lists are immutable, this
306   /// returns the new list.
307   AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
308
309   //===--------------------------------------------------------------------===//
310   // Attribute List Accessors
311   //===--------------------------------------------------------------------===//
312   /// getParamAttributes - The attributes for the specified index are
313   /// returned.
314   Attribute getParamAttributes(unsigned Idx) const {
315     return getAttributes(Idx);
316   }
317
318   /// getRetAttributes - The attributes for the ret value are
319   /// returned.
320   Attribute getRetAttributes() const {
321     return getAttributes(ReturnIndex);
322   }
323
324   /// getFnAttributes - The function attributes are returned.
325   Attribute getFnAttributes() const {
326     return getAttributes(FunctionIndex);
327   }
328
329   /// paramHasAttr - Return true if the specified parameter index has the
330   /// specified attribute set.
331   bool paramHasAttr(unsigned Idx, Attribute Attr) const {
332     return getAttributes(Idx).hasAttributes(Attr);
333   }
334
335   /// getParamAlignment - Return the alignment for the specified function
336   /// parameter.
337   unsigned getParamAlignment(unsigned Idx) const {
338     return getAttributes(Idx).getAlignment();
339   }
340
341   /// hasAttrSomewhere - Return true if the specified attribute is set for at
342   /// least one parameter or for the return value.
343   bool hasAttrSomewhere(Attribute::AttrVal Attr) const;
344
345   unsigned getNumAttrs() const;
346   Attribute &getAttributesAtIndex(unsigned i) const;
347
348   /// operator==/!= - Provide equality predicates.
349   bool operator==(const AttributeSet &RHS) const
350   { return AttrList == RHS.AttrList; }
351   bool operator!=(const AttributeSet &RHS) const
352   { return AttrList != RHS.AttrList; }
353
354   //===--------------------------------------------------------------------===//
355   // Attribute List Introspection
356   //===--------------------------------------------------------------------===//
357
358   /// getRawPointer - Return a raw pointer that uniquely identifies this
359   /// attribute list.
360   void *getRawPointer() const {
361     return AttrList;
362   }
363
364   // Attributes are stored as a dense set of slots, where there is one slot for
365   // each argument that has an attribute.  This allows walking over the dense
366   // set instead of walking the sparse list of attributes.
367
368   /// isEmpty - Return true if there are no attributes.
369   ///
370   bool isEmpty() const {
371     return AttrList == 0;
372   }
373
374   /// getNumSlots - Return the number of slots used in this attribute list.
375   /// This is the number of arguments that have an attribute set on them
376   /// (including the function itself).
377   unsigned getNumSlots() const;
378
379   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
380   /// holds a index number plus a set of attributes.
381   const AttributeWithIndex &getSlot(unsigned Slot) const;
382
383   void dump() const;
384 };
385
386 } // End llvm namespace
387
388 #endif