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 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
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
93 AttributesImpl *Attrs;
94 Attribute(AttributesImpl *A) : Attrs(A) {}
96 Attribute() : Attrs(0) {}
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);
103 /// \brief Return true if the attribute is present.
104 bool hasAttribute(AttrVal Val) const;
106 /// \brief Return true if attributes exist
107 bool hasAttributes() const;
109 /// \brief Return true if the attributes are a non-null intersection.
110 bool hasAttributes(const Attribute &A) const;
112 /// \brief Returns the alignment field of an attribute as a byte alignment
114 unsigned getAlignment() const;
116 /// \brief Returns the stack alignment field of an attribute as a byte
118 unsigned getStackAlignment() const;
120 bool operator==(const Attribute &A) const {
121 return Attrs == A.Attrs;
123 bool operator!=(const Attribute &A) const {
124 return Attrs != A.Attrs;
127 uint64_t Raw() const;
129 /// \brief Which attributes cannot be applied to a type.
130 static Attribute typeIncompatible(Type *Ty);
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);
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);
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;
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.
157 AttrBuilder() : Bits(0) {}
158 explicit AttrBuilder(uint64_t B) : Bits(B) {}
159 AttrBuilder(const Attribute &A) : Bits(A.Raw()) {}
161 void clear() { Bits = 0; }
163 /// addAttribute - Add an attribute to the builder.
164 AttrBuilder &addAttribute(Attribute::AttrVal Val);
166 /// removeAttribute - Remove an attribute from the builder.
167 AttrBuilder &removeAttribute(Attribute::AttrVal Val);
169 /// addAttribute - Add the attributes from A to the builder.
170 AttrBuilder &addAttributes(const Attribute &A);
172 /// removeAttribute - Remove the attributes from A from the builder.
173 AttrBuilder &removeAttributes(const Attribute &A);
175 /// hasAttribute - Return true if the builder has the specified attribute.
176 bool hasAttribute(Attribute::AttrVal A) const;
178 /// hasAttributes - Return true if the builder has IR-level attributes.
179 bool hasAttributes() const;
181 /// hasAttributes - Return true if the builder has any attribute that's in the
182 /// specified attribute.
183 bool hasAttributes(const Attribute &A) const;
185 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
186 bool hasAlignmentAttr() const;
188 /// getAlignment - Retrieve the alignment attribute, if it exists.
189 uint64_t getAlignment() const;
191 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
192 uint64_t getStackAlignment() const;
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);
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);
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);
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);
229 uint64_t Raw() const { return Bits; }
231 bool operator==(const AttrBuilder &B) {
232 return Bits == B.Bits;
234 bool operator!=(const AttrBuilder &B) {
235 return Bits != B.Bits;
239 //===----------------------------------------------------------------------===//
240 /// \class This is just a pair of values to associate a set of attributes with
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.
248 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
249 ArrayRef<Attribute::AttrVal> Attrs) {
250 return get(Idx, Attribute::get(C, Attrs));
252 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
253 AttributeWithIndex P;
260 //===----------------------------------------------------------------------===//
261 // AttributeSet Smart Pointer
262 //===----------------------------------------------------------------------===//
264 class AttributeSetImpl;
266 //===----------------------------------------------------------------------===//
267 /// \class This class manages the ref count for the opaque AttributeSetImpl
268 /// object and provides accessors for it.
276 /// \brief The attributes that we are managing. This can be null to represent
277 /// the empty attributes list.
278 AttributeSetImpl *AttrList;
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;
284 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
286 AttributeSet() : AttrList(0) {}
287 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
288 const AttributeSet &operator=(const AttributeSet &RHS);
290 //===--------------------------------------------------------------------===//
291 // Attribute List Construction and Mutation
292 //===--------------------------------------------------------------------===//
294 /// \brief Return an AttributeSet with the specified parameters in it.
295 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
297 /// \brief Add the specified attribute at the specified index to this
298 /// attribute list. Since attribute lists are immutable, this returns the new
300 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
302 /// \brief Remove the specified attribute at the specified index from this
303 /// attribute list. Since attribute lists are immutable, this returns the new
305 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
307 //===--------------------------------------------------------------------===//
308 // Attribute List Accessors
309 //===--------------------------------------------------------------------===//
311 /// \brief The attributes for the specified index are returned.
312 Attribute getParamAttributes(unsigned Idx) const {
313 return getAttributes(Idx);
316 /// \brief The attributes for the ret value are returned.
317 Attribute getRetAttributes() const {
318 return getAttributes(ReturnIndex);
321 /// \brief The function attributes are returned.
322 Attribute getFnAttributes() const {
323 return getAttributes(FunctionIndex);
326 /// \brief Return true if the specified parameter index has the specified
328 bool paramHasAttr(unsigned Idx, Attribute Attr) const {
329 return getAttributes(Idx).hasAttributes(Attr);
332 /// \brief Return the alignment for the specified function parameter.
333 unsigned getParamAlignment(unsigned Idx) const {
334 return getAttributes(Idx).getAlignment();
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;
341 unsigned getNumAttrs() const;
342 Attribute &getAttributesAtIndex(unsigned i) const;
344 /// operator==/!= - Provide equality predicates.
345 bool operator==(const AttributeSet &RHS) const {
346 return AttrList == RHS.AttrList;
348 bool operator!=(const AttributeSet &RHS) const {
349 return AttrList != RHS.AttrList;
352 //===--------------------------------------------------------------------===//
353 // Attribute List Introspection
354 //===--------------------------------------------------------------------===//
356 /// \brief Return a raw pointer that uniquely identifies this attribute list.
357 void *getRawPointer() const {
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.
365 /// \brief Return true if there are no attributes.
366 bool isEmpty() const {
367 return AttrList == 0;
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;
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;
382 } // End llvm namespace