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 // AttributeWithIndex
241 //===----------------------------------------------------------------------===//
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.
251 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
252 ArrayRef<Attribute::AttrVal> Attrs) {
253 return get(Idx, Attribute::get(C, Attrs));
255 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
256 AttributeWithIndex P;
263 //===----------------------------------------------------------------------===//
264 // AttributeSet Smart Pointer
265 //===----------------------------------------------------------------------===//
267 class AttributeListImpl;
269 /// AttributeSet - This class manages the ref count for the opaque
270 /// AttributeListImpl 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 AttributeListImpl *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(AttributeListImpl *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 /// get - Return an AttributeSet with the specified parameters in it.
297 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
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;
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;
309 //===--------------------------------------------------------------------===//
310 // Attribute List Accessors
311 //===--------------------------------------------------------------------===//
312 /// getParamAttributes - The attributes for the specified index are
314 Attribute getParamAttributes(unsigned Idx) const {
315 return getAttributes(Idx);
318 /// getRetAttributes - The attributes for the ret value are
320 Attribute getRetAttributes() const {
321 return getAttributes(ReturnIndex);
324 /// getFnAttributes - The function attributes are returned.
325 Attribute getFnAttributes() const {
326 return getAttributes(FunctionIndex);
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);
335 /// getParamAlignment - Return the alignment for the specified function
337 unsigned getParamAlignment(unsigned Idx) const {
338 return getAttributes(Idx).getAlignment();
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;
345 unsigned getNumAttrs() const;
346 Attribute &getAttributesAtIndex(unsigned i) const;
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; }
354 //===--------------------------------------------------------------------===//
355 // Attribute List Introspection
356 //===--------------------------------------------------------------------===//
358 /// getRawPointer - Return a raw pointer that uniquely identifies this
360 void *getRawPointer() const {
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.
368 /// isEmpty - Return true if there are no attributes.
370 bool isEmpty() const {
371 return AttrList == 0;
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;
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;
386 } // End llvm namespace