1 //===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the simple types necessary to represent the
11 // attributes associated with functions and their calls.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/MathExtras.h"
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.
37 /// This enumeration lists the attributes that can be associated with
38 /// parameters, function results or the function itself.
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
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:
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.
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
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
95 Attribute(AttributeImpl *A) : Attrs(A) {}
97 Attribute() : Attrs(0) {}
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);
104 /// \brief Return true if the attribute is present.
105 bool hasAttribute(AttrVal Val) const;
107 /// \brief Return true if attributes exist
108 bool hasAttributes() const;
110 /// \brief Return true if the attributes are a non-null intersection.
111 bool hasAttributes(const Attribute &A) const;
113 /// \brief Returns the alignment field of an attribute as a byte alignment
115 unsigned getAlignment() const;
117 /// \brief Returns the stack alignment field of an attribute as a byte
119 unsigned getStackAlignment() const;
121 bool operator==(const Attribute &A) const {
122 return Attrs == A.Attrs;
124 bool operator!=(const Attribute &A) const {
125 return Attrs != A.Attrs;
128 uint64_t Raw() const;
130 /// \brief Which attributes cannot be applied to a type.
131 static Attribute typeIncompatible(Type *Ty);
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);
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);
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;
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.
158 AttrBuilder() : Bits(0) {}
159 explicit AttrBuilder(uint64_t B) : Bits(B) {}
160 AttrBuilder(const Attribute &A) : Bits(A.Raw()) {}
162 void clear() { Bits = 0; }
164 /// addAttribute - Add an attribute to the builder.
165 AttrBuilder &addAttribute(Attribute::AttrVal Val);
167 /// removeAttribute - Remove an attribute from the builder.
168 AttrBuilder &removeAttribute(Attribute::AttrVal Val);
170 /// addAttribute - Add the attributes from A to the builder.
171 AttrBuilder &addAttributes(const Attribute &A);
173 /// removeAttribute - Remove the attributes from A from the builder.
174 AttrBuilder &removeAttributes(const Attribute &A);
176 /// hasAttribute - Return true if the builder has the specified attribute.
177 bool hasAttribute(Attribute::AttrVal A) const;
179 /// hasAttributes - Return true if the builder has IR-level attributes.
180 bool hasAttributes() const;
182 /// hasAttributes - Return true if the builder has any attribute that's in the
183 /// specified attribute.
184 bool hasAttributes(const Attribute &A) const;
186 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
187 bool hasAlignmentAttr() const;
189 /// getAlignment - Retrieve the alignment attribute, if it exists.
190 uint64_t getAlignment() const;
192 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
193 uint64_t getStackAlignment() const;
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);
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);
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);
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);
231 uint64_t Raw() const { return Bits; }
233 bool operator==(const AttrBuilder &B) {
234 return Bits == B.Bits;
236 bool operator!=(const AttrBuilder &B) {
237 return Bits != B.Bits;
241 //===----------------------------------------------------------------------===//
242 /// \class This is just a pair of values to associate a set of attributes with
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.
250 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
251 ArrayRef<Attribute::AttrVal> Attrs) {
252 return get(Idx, Attribute::get(C, Attrs));
254 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
255 AttributeWithIndex P;
262 //===----------------------------------------------------------------------===//
263 // AttributeSet Smart Pointer
264 //===----------------------------------------------------------------------===//
266 class AttributeSetImpl;
268 //===----------------------------------------------------------------------===//
269 /// \class This class manages the ref count for the opaque AttributeSetImpl
270 /// object and provides accessors for it.
278 /// \brief The attributes that we are managing. This can be null to represent
279 /// the empty attributes list.
280 AttributeSetImpl *AttrList;
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;
286 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
288 AttributeSet() : AttrList(0) {}
289 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
290 const AttributeSet &operator=(const AttributeSet &RHS);
292 //===--------------------------------------------------------------------===//
293 // Attribute List Construction and Mutation
294 //===--------------------------------------------------------------------===//
296 /// \brief Return an AttributeSet with the specified parameters in it.
297 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
299 /// \brief Add the specified attribute at the specified index to this
300 /// attribute list. Since attribute lists are immutable, this returns the new
302 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
304 /// \brief Remove the specified attribute at the specified index from this
305 /// attribute list. Since attribute lists are immutable, this returns the new
307 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
309 //===--------------------------------------------------------------------===//
310 // Attribute List Accessors
311 //===--------------------------------------------------------------------===//
313 /// \brief The attributes for the specified index are returned.
314 Attribute getParamAttributes(unsigned Idx) const {
315 return getAttributes(Idx);
318 /// \brief The attributes for the ret value are returned.
319 Attribute getRetAttributes() const {
320 return getAttributes(ReturnIndex);
323 /// \brief The function attributes are returned.
324 Attribute getFnAttributes() const {
325 return getAttributes(FunctionIndex);
328 /// \brief Return true if the specified parameter index has the specified
330 bool paramHasAttr(unsigned Idx, Attribute Attr) const {
331 return getAttributes(Idx).hasAttributes(Attr);
334 /// \brief Return the alignment for the specified function parameter.
335 unsigned getParamAlignment(unsigned Idx) const {
336 return getAttributes(Idx).getAlignment();
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;
343 unsigned getNumAttrs() const;
344 Attribute &getAttributesAtIndex(unsigned i) const;
346 /// operator==/!= - Provide equality predicates.
347 bool operator==(const AttributeSet &RHS) const {
348 return AttrList == RHS.AttrList;
350 bool operator!=(const AttributeSet &RHS) const {
351 return AttrList != RHS.AttrList;
354 //===--------------------------------------------------------------------===//
355 // Attribute List Introspection
356 //===--------------------------------------------------------------------===//
358 /// \brief Return a raw pointer that uniquely identifies this attribute list.
359 void *getRawPointer() const {
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.
367 /// \brief Return true if there are no attributes.
368 bool isEmpty() const {
369 return AttrList == 0;
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;
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;
384 } // End llvm namespace