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 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the simple types necessary to represent the
12 /// attributes associated with functions and their calls.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ATTRIBUTES_H
17 #define LLVM_ATTRIBUTES_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/MathExtras.h"
31 //===----------------------------------------------------------------------===//
33 /// \brief Functions, function parameters, and return types can have attributes
34 /// to indicate how they should be treated by optimizations and code
35 /// generation. This class represents one of those attributes. It's light-weight
36 /// and should be passed around by-value.
39 /// This enumeration lists the attributes that can be associated with
40 /// parameters, function results or the function itself.
42 /// Note: uwtable is about the ABI or the user mandating an entry in the
43 /// unwind table. The nounwind attribute is about an exception passing by the
46 /// In a theoretical system that uses tables for profiling and sjlj for
47 /// exceptions, they would be fully independent. In a normal system that uses
48 /// tables for both, the semantics are:
50 /// nil = Needs an entry because an exception might pass by.
51 /// nounwind = No need for an entry
52 /// uwtable = Needs an entry because the ABI says so and because
53 /// an exception might pass by.
54 /// uwtable + nounwind = Needs an entry because the ABI says so.
57 // IR-Level Attributes
58 None, ///< No attributes have been set
59 AddressSafety, ///< Address safety checking is on.
60 Alignment, ///< Alignment of parameter (5 bits)
61 ///< stored as log2 of alignment with +1 bias
62 ///< 0 means unaligned (different from align(1))
63 AlwaysInline, ///< inline=always
64 ByVal, ///< Pass structure by value
65 InlineHint, ///< Source said inlining was desirable
66 InReg, ///< Force argument to be passed in register
67 MinSize, ///< Function must be optimized for size first
68 Naked, ///< Naked function
69 Nest, ///< Nested function static chain
70 NoAlias, ///< Considered to not alias after call
71 NoCapture, ///< Function creates no aliases of pointer
72 NoDuplicate, ///< Call cannot be duplicated
73 NoImplicitFloat, ///< Disable implicit floating point insts
74 NoInline, ///< inline=never
75 NonLazyBind, ///< Function is called early and/or
76 ///< often, so lazy binding isn't worthwhile
77 NoRedZone, ///< Disable redzone
78 NoReturn, ///< Mark the function as not returning
79 NoUnwind, ///< Function doesn't unwind stack
80 OptimizeForSize, ///< opt_size
81 ReadNone, ///< Function does not access memory
82 ReadOnly, ///< Function only reads from memory
83 ReturnsTwice, ///< Function can return twice
84 SExt, ///< Sign extended before/after call
85 StackAlignment, ///< Alignment of stack for function (3 bits)
86 ///< stored as log2 of alignment with +1 bias 0
87 ///< means unaligned (different from
89 StackProtect, ///< Stack protection.
90 StackProtectReq, ///< Stack protection required.
91 StructRet, ///< Hidden pointer to structure to return
92 UWTable, ///< Function must be in a unwind table
93 ZExt ///< Zero extended before/after call
97 Attribute(AttributeImpl *A) : pImpl(A) {}
99 Attribute() : pImpl(0) {}
101 /// \brief Return a uniquified Attribute object. This takes the uniquified
102 /// value from the Builder and wraps it in the Attribute class.
103 static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Vals);
104 static Attribute get(LLVMContext &Context, AttrBuilder &B);
106 /// \brief Return true if the attribute is present.
107 bool hasAttribute(AttrKind Val) const;
109 /// \brief Return true if attributes exist
110 bool hasAttributes() const;
112 /// \brief Return true if the attributes are a non-null intersection.
113 bool hasAttributes(const Attribute &A) const;
115 /// \brief Returns the alignment field of an attribute as a byte alignment
117 unsigned getAlignment() const;
119 /// \brief Returns the stack alignment field of an attribute as a byte
121 unsigned getStackAlignment() const;
123 bool operator==(const Attribute &A) const {
124 return pImpl == A.pImpl;
126 bool operator!=(const Attribute &A) const {
127 return pImpl != A.pImpl;
130 uint64_t getBitMask() const;
132 /// \brief Which attributes cannot be applied to a type.
133 static Attribute typeIncompatible(Type *Ty);
135 /// \brief This returns an integer containing an encoding of all the LLVM
136 /// attributes found in the given attribute bitset. Any change to this
137 /// encoding is a breaking change to bitcode compatibility.
138 static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs);
140 /// \brief This returns an attribute bitset containing the LLVM attributes
141 /// that have been decoded from the given integer. This function must stay in
142 /// sync with 'encodeLLVMAttributesForBitcode'.
143 static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
144 uint64_t EncodedAttrs);
146 /// \brief The set of attributes set in Attribute is converted to a string of
147 /// equivalent mnemonics. This is, presumably, for writing out the mnemonics
148 /// for the assembly writer.
149 std::string getAsString() const;
152 //===----------------------------------------------------------------------===//
154 /// \brief This class is used in conjunction with the Attribute::get method to
155 /// create an Attribute object. The object itself is uniquified. The Builder's
156 /// value, however, is not. So this can be used as a quick way to test for
157 /// equality, presence of attributes, etc.
161 AttrBuilder() : Bits(0) {}
162 explicit AttrBuilder(uint64_t B) : Bits(B) {}
163 AttrBuilder(const Attribute &A) : Bits(A.getBitMask()) {}
165 void clear() { Bits = 0; }
167 /// addAttribute - Add an attribute to the builder.
168 AttrBuilder &addAttribute(Attribute::AttrKind Val);
170 /// removeAttribute - Remove an attribute from the builder.
171 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
173 /// addAttribute - Add the attributes from A to the builder.
174 AttrBuilder &addAttributes(const Attribute &A);
176 /// removeAttribute - Remove the attributes from A from the builder.
177 AttrBuilder &removeAttributes(const Attribute &A);
179 /// \brief Return true if the builder has the specified attribute.
180 bool contains(Attribute::AttrKind A) const;
182 /// hasAttributes - Return true if the builder has IR-level attributes.
183 bool hasAttributes() const;
185 /// hasAttributes - Return true if the builder has any attribute that's in the
186 /// specified attribute.
187 bool hasAttributes(const Attribute &A) const;
189 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
190 bool hasAlignmentAttr() const;
192 /// getAlignment - Retrieve the alignment attribute, if it exists.
193 uint64_t getAlignment() const;
195 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
196 uint64_t getStackAlignment() const;
198 /// addAlignmentAttr - This turns an int alignment (which must be a power of
199 /// 2) into the form used internally in Attribute.
200 AttrBuilder &addAlignmentAttr(unsigned Align);
202 /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
203 /// power of 2) into the form used internally in Attribute.
204 AttrBuilder &addStackAlignmentAttr(unsigned Align);
206 /// addRawValue - Add the raw value to the internal representation.
207 /// N.B. This should be used ONLY for decoding LLVM bitcode!
208 AttrBuilder &addRawValue(uint64_t Val);
210 /// @brief Remove attributes that are used on functions only.
211 void removeFunctionOnlyAttrs() {
212 removeAttribute(Attribute::NoReturn)
213 .removeAttribute(Attribute::NoUnwind)
214 .removeAttribute(Attribute::ReadNone)
215 .removeAttribute(Attribute::ReadOnly)
216 .removeAttribute(Attribute::NoInline)
217 .removeAttribute(Attribute::AlwaysInline)
218 .removeAttribute(Attribute::OptimizeForSize)
219 .removeAttribute(Attribute::StackProtect)
220 .removeAttribute(Attribute::StackProtectReq)
221 .removeAttribute(Attribute::NoRedZone)
222 .removeAttribute(Attribute::NoImplicitFloat)
223 .removeAttribute(Attribute::Naked)
224 .removeAttribute(Attribute::InlineHint)
225 .removeAttribute(Attribute::StackAlignment)
226 .removeAttribute(Attribute::UWTable)
227 .removeAttribute(Attribute::NonLazyBind)
228 .removeAttribute(Attribute::ReturnsTwice)
229 .removeAttribute(Attribute::AddressSafety)
230 .removeAttribute(Attribute::MinSize)
231 .removeAttribute(Attribute::NoDuplicate);
234 uint64_t getBitMask() const { return Bits; }
236 bool operator==(const AttrBuilder &B) {
237 return Bits == B.Bits;
239 bool operator!=(const AttrBuilder &B) {
240 return Bits != B.Bits;
244 //===----------------------------------------------------------------------===//
246 /// \brief This is just a pair of values to associate a set of attributes with
248 struct AttributeWithIndex {
249 Attribute Attrs; ///< The attributes that are set, or'd together.
250 unsigned Index; ///< Index of the parameter for which the attributes apply.
251 ///< Index 0 is used for return value attributes.
252 ///< Index ~0U is used for function attributes.
254 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
255 ArrayRef<Attribute::AttrKind> Attrs) {
256 return get(Idx, Attribute::get(C, Attrs));
258 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
259 AttributeWithIndex P;
266 //===----------------------------------------------------------------------===//
267 // AttributeSet Smart Pointer
268 //===----------------------------------------------------------------------===//
270 class AttributeSetImpl;
272 //===----------------------------------------------------------------------===//
274 /// \brief This class manages the ref count for the opaque AttributeSetImpl
275 /// object and provides accessors for it.
283 /// \brief The attributes that we are managing. This can be null to represent
284 /// the empty attributes list.
285 AttributeSetImpl *AttrList;
287 /// \brief The attributes for the specified index are returned. Attributes
288 /// for the result are denoted with Idx = 0.
289 Attribute getAttributes(unsigned Idx) const;
291 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
293 AttributeSet() : AttrList(0) {}
294 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
295 const AttributeSet &operator=(const AttributeSet &RHS);
297 //===--------------------------------------------------------------------===//
298 // Attribute List Construction and Mutation
299 //===--------------------------------------------------------------------===//
301 /// \brief Return an AttributeSet with the specified parameters in it.
302 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
304 /// \brief Add the specified attribute at the specified index to this
305 /// attribute list. Since attribute lists are immutable, this returns the new
307 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
309 /// \brief Remove the specified attribute at the specified index from this
310 /// attribute list. Since attribute lists are immutable, this returns the new
312 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
314 //===--------------------------------------------------------------------===//
315 // Attribute List Accessors
316 //===--------------------------------------------------------------------===//
318 /// \brief The attributes for the specified index are returned.
319 Attribute getParamAttributes(unsigned Idx) const {
320 return getAttributes(Idx);
323 /// \brief The attributes for the ret value are returned.
324 Attribute getRetAttributes() const {
325 return getAttributes(ReturnIndex);
328 /// \brief The function attributes are returned.
329 Attribute getFnAttributes() const {
330 return getAttributes(FunctionIndex);
333 /// \brief Return true if the specified parameter index has the specified
335 bool paramHasAttr(unsigned Idx, Attribute Attr) const {
336 return getAttributes(Idx).hasAttributes(Attr);
339 /// \brief Return the alignment for the specified function parameter.
340 unsigned getParamAlignment(unsigned Idx) const {
341 return getAttributes(Idx).getAlignment();
344 /// \brief Return true if the attribute exists at the given index.
345 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
347 /// \brief Return true if attribute exists at the given index.
348 bool hasAttributes(unsigned Index) const;
350 /// \brief Get the stack alignment.
351 unsigned getStackAlignment(unsigned Index) const;
353 /// \brief Return the attributes at the index as a string.
354 std::string getAsString(unsigned Index) const;
356 uint64_t getBitMask(unsigned Index) const;
358 /// \brief Return true if the specified attribute is set for at least one
359 /// parameter or for the return value.
360 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
362 unsigned getNumAttrs() const;
363 Attribute &getAttributesAtIndex(unsigned i) const;
365 /// operator==/!= - Provide equality predicates.
366 bool operator==(const AttributeSet &RHS) const {
367 return AttrList == RHS.AttrList;
369 bool operator!=(const AttributeSet &RHS) const {
370 return AttrList != RHS.AttrList;
373 //===--------------------------------------------------------------------===//
374 // Attribute List Introspection
375 //===--------------------------------------------------------------------===//
377 /// \brief Return a raw pointer that uniquely identifies this attribute list.
378 void *getRawPointer() const {
382 // Attributes are stored as a dense set of slots, where there is one slot for
383 // each argument that has an attribute. This allows walking over the dense
384 // set instead of walking the sparse list of attributes.
386 /// \brief Return true if there are no attributes.
387 bool isEmpty() const {
388 return AttrList == 0;
391 /// \brief Return the number of slots used in this attribute list. This is
392 /// the number of arguments that have an attribute set on them (including the
393 /// function itself).
394 unsigned getNumSlots() const;
396 /// \brief Return the AttributeWithIndex at the specified slot. This holds a
397 /// index number plus a set of attributes.
398 const AttributeWithIndex &getSlot(unsigned Slot) const;
403 } // end llvm namespace