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 /// \brief Attribute that only apply to function parameters.
121 bool hasParameterOnlyAttrs() const {
122 return hasAttribute(Attribute::ByVal) ||
123 hasAttribute(Attribute::Nest) ||
124 hasAttribute(Attribute::StructRet) ||
125 hasAttribute(Attribute::NoCapture);
128 /// \brief Attribute that may be applied to the function itself. These cannot
129 /// be used on return values or function parameters.
130 bool hasFunctionOnlyAttrs() const {
131 return hasAttribute(Attribute::NoReturn) ||
132 hasAttribute(Attribute::NoUnwind) ||
133 hasAttribute(Attribute::ReadNone) ||
134 hasAttribute(Attribute::ReadOnly) ||
135 hasAttribute(Attribute::NoInline) ||
136 hasAttribute(Attribute::AlwaysInline) ||
137 hasAttribute(Attribute::OptimizeForSize) ||
138 hasAttribute(Attribute::StackProtect) ||
139 hasAttribute(Attribute::StackProtectReq) ||
140 hasAttribute(Attribute::NoRedZone) ||
141 hasAttribute(Attribute::NoImplicitFloat) ||
142 hasAttribute(Attribute::Naked) ||
143 hasAttribute(Attribute::InlineHint) ||
144 hasAttribute(Attribute::StackAlignment) ||
145 hasAttribute(Attribute::UWTable) ||
146 hasAttribute(Attribute::NonLazyBind) ||
147 hasAttribute(Attribute::ReturnsTwice) ||
148 hasAttribute(Attribute::AddressSafety) ||
149 hasAttribute(Attribute::MinSize);
152 bool operator==(const Attribute &A) const {
153 return Attrs == A.Attrs;
155 bool operator!=(const Attribute &A) const {
156 return Attrs != A.Attrs;
159 uint64_t Raw() const;
161 /// \brief Which attributes cannot be applied to a type.
162 static Attribute typeIncompatible(Type *Ty);
164 /// \brief This returns an integer containing an encoding of all the LLVM
165 /// attributes found in the given attribute bitset. Any change to this
166 /// encoding is a breaking change to bitcode compatibility.
167 static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs);
169 /// \brief This returns an attribute bitset containing the LLVM attributes
170 /// that have been decoded from the given integer. This function must stay in
171 /// sync with 'encodeLLVMAttributesForBitcode'.
172 static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
173 uint64_t EncodedAttrs);
175 /// \brief The set of attributes set in Attribute is converted to a string of
176 /// equivalent mnemonics. This is, presumably, for writing out the mnemonics
177 /// for the assembly writer. @brief Convert attribute bits to text
178 std::string getAsString() const;
181 //===----------------------------------------------------------------------===//
182 /// AttrBuilder - This class is used in conjunction with the Attribute::get
183 /// method to create an Attribute object. The object itself is uniquified. The
184 /// Builder's value, however, is not. So this can be used as a quick way to test
185 /// for equality, presence of attributes, etc.
189 AttrBuilder() : Bits(0) {}
190 explicit AttrBuilder(uint64_t B) : Bits(B) {}
191 AttrBuilder(const Attribute &A) : Bits(A.Raw()) {}
193 void clear() { Bits = 0; }
195 /// addAttribute - Add an attribute to the builder.
196 AttrBuilder &addAttribute(Attribute::AttrVal Val);
198 /// removeAttribute - Remove an attribute from the builder.
199 AttrBuilder &removeAttribute(Attribute::AttrVal Val);
201 /// addAttribute - Add the attributes from A to the builder.
202 AttrBuilder &addAttributes(const Attribute &A);
204 /// removeAttribute - Remove the attributes from A from the builder.
205 AttrBuilder &removeAttributes(const Attribute &A);
207 /// hasAttribute - Return true if the builder has the specified attribute.
208 bool hasAttribute(Attribute::AttrVal A) const;
210 /// hasAttributes - Return true if the builder has IR-level attributes.
211 bool hasAttributes() const;
213 /// hasAttributes - Return true if the builder has any attribute that's in the
214 /// specified attribute.
215 bool hasAttributes(const Attribute &A) const;
217 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
218 bool hasAlignmentAttr() const;
220 /// getAlignment - Retrieve the alignment attribute, if it exists.
221 uint64_t getAlignment() const;
223 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
224 uint64_t getStackAlignment() const;
226 /// addAlignmentAttr - This turns an int alignment (which must be a power of
227 /// 2) into the form used internally in Attribute.
228 AttrBuilder &addAlignmentAttr(unsigned Align);
230 /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
231 /// power of 2) into the form used internally in Attribute.
232 AttrBuilder &addStackAlignmentAttr(unsigned Align);
234 /// addRawValue - Add the raw value to the internal representation.
235 /// N.B. This should be used ONLY for decoding LLVM bitcode!
236 AttrBuilder &addRawValue(uint64_t Val);
238 /// @brief Remove attributes that are used on functions only.
239 void removeFunctionOnlyAttrs() {
240 removeAttribute(Attribute::NoReturn)
241 .removeAttribute(Attribute::NoUnwind)
242 .removeAttribute(Attribute::ReadNone)
243 .removeAttribute(Attribute::ReadOnly)
244 .removeAttribute(Attribute::NoInline)
245 .removeAttribute(Attribute::AlwaysInline)
246 .removeAttribute(Attribute::OptimizeForSize)
247 .removeAttribute(Attribute::StackProtect)
248 .removeAttribute(Attribute::StackProtectReq)
249 .removeAttribute(Attribute::NoRedZone)
250 .removeAttribute(Attribute::NoImplicitFloat)
251 .removeAttribute(Attribute::Naked)
252 .removeAttribute(Attribute::InlineHint)
253 .removeAttribute(Attribute::StackAlignment)
254 .removeAttribute(Attribute::UWTable)
255 .removeAttribute(Attribute::NonLazyBind)
256 .removeAttribute(Attribute::ReturnsTwice)
257 .removeAttribute(Attribute::AddressSafety)
258 .removeAttribute(Attribute::MinSize);
261 uint64_t Raw() const { return Bits; }
263 bool operator==(const AttrBuilder &B) {
264 return Bits == B.Bits;
266 bool operator!=(const AttrBuilder &B) {
267 return Bits != B.Bits;
271 //===----------------------------------------------------------------------===//
272 // AttributeWithIndex
273 //===----------------------------------------------------------------------===//
275 /// AttributeWithIndex - This is just a pair of values to associate a set of
276 /// attributes with an index.
277 struct AttributeWithIndex {
278 Attribute Attrs; ///< The attributes that are set, or'd together.
279 unsigned Index; ///< Index of the parameter for which the attributes apply.
280 ///< Index 0 is used for return value attributes.
281 ///< Index ~0U is used for function attributes.
283 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
284 ArrayRef<Attribute::AttrVal> Attrs) {
285 return get(Idx, Attribute::get(C, Attrs));
287 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
288 AttributeWithIndex P;
295 //===----------------------------------------------------------------------===//
296 // AttributeSet Smart Pointer
297 //===----------------------------------------------------------------------===//
299 class AttributeListImpl;
301 /// AttributeSet - This class manages the ref count for the opaque
302 /// AttributeListImpl object and provides accessors for it.
310 /// @brief The attributes that we are managing. This can be null to represent
311 /// the empty attributes list.
312 AttributeListImpl *AttrList;
314 /// @brief The attributes for the specified index are returned. Attributes
315 /// for the result are denoted with Idx = 0.
316 Attribute getAttributes(unsigned Idx) const;
318 explicit AttributeSet(AttributeListImpl *LI) : AttrList(LI) {}
320 AttributeSet() : AttrList(0) {}
321 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
322 const AttributeSet &operator=(const AttributeSet &RHS);
324 //===--------------------------------------------------------------------===//
325 // Attribute List Construction and Mutation
326 //===--------------------------------------------------------------------===//
328 /// get - Return an AttributeSet with the specified parameters in it.
329 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
331 /// addAttr - Add the specified attribute at the specified index to this
332 /// attribute list. Since attribute lists are immutable, this
333 /// returns the new list.
334 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
336 /// removeAttr - Remove the specified attribute at the specified index from
337 /// this attribute list. Since attribute lists are immutable, this
338 /// returns the new list.
339 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
341 //===--------------------------------------------------------------------===//
342 // Attribute List Accessors
343 //===--------------------------------------------------------------------===//
344 /// getParamAttributes - The attributes for the specified index are
346 Attribute getParamAttributes(unsigned Idx) const {
347 return getAttributes(Idx);
350 /// getRetAttributes - The attributes for the ret value are
352 Attribute getRetAttributes() const {
353 return getAttributes(ReturnIndex);
356 /// getFnAttributes - The function attributes are returned.
357 Attribute getFnAttributes() const {
358 return getAttributes(FunctionIndex);
361 /// paramHasAttr - Return true if the specified parameter index has the
362 /// specified attribute set.
363 bool paramHasAttr(unsigned Idx, Attribute Attr) const {
364 return getAttributes(Idx).hasAttributes(Attr);
367 /// getParamAlignment - Return the alignment for the specified function
369 unsigned getParamAlignment(unsigned Idx) const {
370 return getAttributes(Idx).getAlignment();
373 /// hasAttrSomewhere - Return true if the specified attribute is set for at
374 /// least one parameter or for the return value.
375 bool hasAttrSomewhere(Attribute::AttrVal Attr) const;
377 unsigned getNumAttrs() const;
378 Attribute &getAttributesAtIndex(unsigned i) const;
380 /// operator==/!= - Provide equality predicates.
381 bool operator==(const AttributeSet &RHS) const
382 { return AttrList == RHS.AttrList; }
383 bool operator!=(const AttributeSet &RHS) const
384 { return AttrList != RHS.AttrList; }
386 //===--------------------------------------------------------------------===//
387 // Attribute List Introspection
388 //===--------------------------------------------------------------------===//
390 /// getRawPointer - Return a raw pointer that uniquely identifies this
392 void *getRawPointer() const {
396 // Attributes are stored as a dense set of slots, where there is one slot for
397 // each argument that has an attribute. This allows walking over the dense
398 // set instead of walking the sparse list of attributes.
400 /// isEmpty - Return true if there are no attributes.
402 bool isEmpty() const {
403 return AttrList == 0;
406 /// getNumSlots - Return the number of slots used in this attribute list.
407 /// This is the number of arguments that have an attribute set on them
408 /// (including the function itself).
409 unsigned getNumSlots() const;
411 /// getSlot - Return the AttributeWithIndex at the specified slot. This
412 /// holds a index number plus a set of attributes.
413 const AttributeWithIndex &getSlot(unsigned Slot) const;
418 } // End llvm namespace