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/AttributesImpl.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/ADT/ArrayRef.h"
29 /// AttributeImpl - The internal representation of the Attributes class. This is
33 /// Attributes - A bitset of attributes.
36 /// Function parameters and results can have attributes to indicate how they
37 /// should be treated by optimizations and code generation. This enumeration
38 /// lists the attributes that can be associated with parameters, function
39 /// results or the function itself.
41 /// Note that uwtable is about the ABI or the user mandating an entry in the
42 /// unwind table. The nounwind attribute is about an exception passing by the
45 /// In a theoretical system that uses tables for profiling and sjlj for
46 /// exceptions, they would be fully independent. In a normal system that uses
47 /// tables for both, the semantics are:
49 /// nil = Needs an entry because an exception might pass by.
50 /// nounwind = No need for an entry
51 /// uwtable = Needs an entry because the ABI says so and because
52 /// an exception might pass by.
53 /// uwtable + nounwind = Needs an entry because the ABI says so.
56 None = 0, ///< No attributes have been set
57 AddressSafety = 1, ///< Address safety checking is on.
58 Alignment = 2, ///< Alignment of parameter (5 bits)
59 ///< stored as log2 of alignment with +1 bias
60 ///< 0 means unaligned different from align 1
61 AlwaysInline = 3, ///< inline=always
62 ByVal = 4, ///< Pass structure by value
63 InlineHint = 5, ///< Source said inlining was desirable
64 InReg = 6, ///< Force argument to be passed in register
65 Naked = 7, ///< Naked function
66 Nest = 8, ///< Nested function static chain
67 NoAlias = 9, ///< Considered to not alias after call
68 NoCapture = 10, ///< Function creates no aliases of pointer
69 NoImplicitFloat = 11, ///< Disable implicit floating point insts
70 NoInline = 12, ///< inline=never
71 NonLazyBind = 13, ///< Function is called early and/or
72 ///< often, so lazy binding isn't worthwhile
73 NoRedZone = 14, ///< Disable redzone
74 NoReturn = 15, ///< Mark the function as not returning
75 NoUnwind = 16, ///< Function doesn't unwind stack
76 OptimizeForSize = 17, ///< opt_size
77 ReadNone = 18, ///< Function does not access memory
78 ReadOnly = 19, ///< Function only reads from memory
79 ReturnsTwice = 20, ///< Function can return twice
80 SExt = 21, ///< Sign extended before/after call
81 StackAlignment = 22, ///< Alignment of stack for function (3 bits)
82 ///< stored as log2 of alignment with +1 bias 0
83 ///< means unaligned (different from
85 StackProtect = 23, ///< Stack protection.
86 StackProtectReq = 24, ///< Stack protection required.
87 StructRet = 25, ///< Hidden pointer to structure to return
88 UWTable = 26, ///< Function must be in a unwind table
89 ZExt = 27 ///< Zero extended before/after call
94 explicit Attributes(AttributesImpl *A);
96 Attributes() : Attrs(0) {}
97 explicit Attributes(uint64_t Val);
98 explicit Attributes(LLVMContext &C, AttrVal Val);
99 Attributes(const Attributes &A);
102 friend class Attributes;
105 Builder() : Bits(0) {}
106 Builder(const Attributes &A) : Bits(A.Raw()) {}
108 void clear() { Bits = 0; }
110 bool hasAttribute(Attributes::AttrVal A) const;
111 bool hasAttributes() const;
112 bool hasAttributes(const Attributes &A) const;
113 bool hasAlignmentAttr() const;
115 uint64_t getAlignment() const;
116 uint64_t getStackAlignment() const;
118 Builder &addAttribute(Attributes::AttrVal Val);
119 Builder &removeAttribute(Attributes::AttrVal Val);
121 void addAlignmentAttr(unsigned Align);
122 void addStackAlignmentAttr(unsigned Align);
124 void removeAttributes(const Attributes &A);
126 /// @brief Remove attributes that are used on functions only.
127 void removeFunctionOnlyAttrs() {
128 removeAttribute(Attributes::NoReturn)
129 .removeAttribute(Attributes::NoUnwind)
130 .removeAttribute(Attributes::ReadNone)
131 .removeAttribute(Attributes::ReadOnly)
132 .removeAttribute(Attributes::NoInline)
133 .removeAttribute(Attributes::AlwaysInline)
134 .removeAttribute(Attributes::OptimizeForSize)
135 .removeAttribute(Attributes::StackProtect)
136 .removeAttribute(Attributes::StackProtectReq)
137 .removeAttribute(Attributes::NoRedZone)
138 .removeAttribute(Attributes::NoImplicitFloat)
139 .removeAttribute(Attributes::Naked)
140 .removeAttribute(Attributes::InlineHint)
141 .removeAttribute(Attributes::StackAlignment)
142 .removeAttribute(Attributes::UWTable)
143 .removeAttribute(Attributes::NonLazyBind)
144 .removeAttribute(Attributes::ReturnsTwice)
145 .removeAttribute(Attributes::AddressSafety);
149 /// get - Return a uniquified Attributes object. This takes the uniquified
150 /// value from the Builder and wraps it in the Attributes class.
151 static Attributes get(Builder &B);
152 static Attributes get(LLVMContext &Context, Builder &B);
154 /// @brief Return true if the attribute is present.
155 bool hasAttribute(AttrVal Val) const;
157 /// @brief Return true if attributes exist
158 bool hasAttributes() const {
159 return Attrs.hasAttributes();
162 /// @brief Return true if the attributes are a non-null intersection.
163 bool hasAttributes(const Attributes &A) const;
165 /// @brief Returns the alignment field of an attribute as a byte alignment
167 unsigned getAlignment() const;
169 /// @brief Returns the stack alignment field of an attribute as a byte
171 unsigned getStackAlignment() const;
173 /// @brief Parameter attributes that do not apply to vararg call arguments.
174 bool hasIncompatibleWithVarArgsAttrs() const {
175 return hasAttribute(Attributes::StructRet);
178 /// @brief Attributes that only apply to function parameters.
179 bool hasParameterOnlyAttrs() const {
180 return hasAttribute(Attributes::ByVal) ||
181 hasAttribute(Attributes::Nest) ||
182 hasAttribute(Attributes::StructRet) ||
183 hasAttribute(Attributes::NoCapture);
186 /// @brief Attributes that may be applied to the function itself. These cannot
187 /// be used on return values or function parameters.
188 bool hasFunctionOnlyAttrs() const {
189 return hasAttribute(Attributes::NoReturn) ||
190 hasAttribute(Attributes::NoUnwind) ||
191 hasAttribute(Attributes::ReadNone) ||
192 hasAttribute(Attributes::ReadOnly) ||
193 hasAttribute(Attributes::NoInline) ||
194 hasAttribute(Attributes::AlwaysInline) ||
195 hasAttribute(Attributes::OptimizeForSize) ||
196 hasAttribute(Attributes::StackProtect) ||
197 hasAttribute(Attributes::StackProtectReq) ||
198 hasAttribute(Attributes::NoRedZone) ||
199 hasAttribute(Attributes::NoImplicitFloat) ||
200 hasAttribute(Attributes::Naked) ||
201 hasAttribute(Attributes::InlineHint) ||
202 hasAttribute(Attributes::StackAlignment) ||
203 hasAttribute(Attributes::UWTable) ||
204 hasAttribute(Attributes::NonLazyBind) ||
205 hasAttribute(Attributes::ReturnsTwice) ||
206 hasAttribute(Attributes::AddressSafety);
209 bool isEmptyOrSingleton() const;
211 // This is a "safe bool() operator".
212 operator const void *() const { return Attrs.Bits ? this : 0; }
213 bool operator == (const Attributes &A) const {
214 return Attrs.Bits == A.Attrs.Bits;
216 bool operator != (const Attributes &A) const {
217 return Attrs.Bits != A.Attrs.Bits;
220 Attributes operator | (const Attributes &A) const;
221 Attributes operator & (const Attributes &A) const;
222 Attributes operator ^ (const Attributes &A) const;
223 Attributes &operator |= (const Attributes &A);
224 Attributes &operator &= (const Attributes &A);
225 Attributes operator ~ () const;
227 uint64_t Raw() const;
229 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
230 /// normally) into the form used internally in Attributes.
231 static Attributes constructAlignmentFromInt(unsigned i) {
232 // Default alignment, allow the target to define how to align it.
236 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
237 assert(i <= 0x40000000 && "Alignment too large.");
238 return Attributes((Log2_32(i)+1) << 16);
241 /// constructStackAlignmentFromInt - This turns an int stack alignment (which
242 /// must be a power of 2) into the form used internally in Attributes.
243 static Attributes constructStackAlignmentFromInt(unsigned i) {
244 // Default alignment, allow the target to define how to align it.
248 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
249 assert(i <= 0x100 && "Alignment too large.");
250 return Attributes((Log2_32(i)+1) << 26);
253 /// @brief Which attributes cannot be applied to a type.
254 static Attributes typeIncompatible(Type *Ty);
256 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
257 /// encoding of all the LLVM attributes found in the given attribute bitset.
258 /// Any change to this encoding is a breaking change to bitcode compatibility.
259 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
260 // FIXME: It doesn't make sense to store the alignment information as an
261 // expanded out value, we should store it as a log2 value. However, we
262 // can't just change that here without breaking bitcode compatibility. If
263 // this ever becomes a problem in practice, we should introduce new tag
264 // numbers in the bitcode file and have those tags use a more efficiently
265 // encoded alignment field.
267 // Store the alignment in the bitcode as a 16-bit raw value instead of a
268 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
270 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
271 if (Attrs.hasAttribute(Attributes::Alignment))
272 EncodedAttrs |= Attrs.getAlignment() << 16;
273 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
277 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
278 /// containing the LLVM attributes that have been decoded from the given
279 /// integer. This function must stay in sync with
280 /// 'encodeLLVMAttributesForBitcode'.
281 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
282 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
283 // the bits above 31 down by 11 bits.
284 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
285 assert((!Alignment || isPowerOf2_32(Alignment)) &&
286 "Alignment must be a power of two.");
288 Attributes Attrs(EncodedAttrs & 0xffff);
290 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
291 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
295 /// getAsString - The set of Attributes set in Attributes is converted to a
296 /// string of equivalent mnemonics. This is, presumably, for writing out the
297 /// mnemonics for the assembly writer.
298 /// @brief Convert attribute bits to text
299 std::string getAsString() const;
302 //===----------------------------------------------------------------------===//
303 // AttributeWithIndex
304 //===----------------------------------------------------------------------===//
306 /// AttributeWithIndex - This is just a pair of values to associate a set of
307 /// attributes with an index.
308 struct AttributeWithIndex {
309 Attributes Attrs; ///< The attributes that are set, or'd together.
310 unsigned Index; ///< Index of the parameter for which the attributes apply.
311 ///< Index 0 is used for return value attributes.
312 ///< Index ~0U is used for function attributes.
314 static AttributeWithIndex get(unsigned Idx,
315 ArrayRef<Attributes::AttrVal> Attrs) {
316 Attributes::Builder B;
318 for (ArrayRef<Attributes::AttrVal>::iterator I = Attrs.begin(),
319 E = Attrs.end(); I != E; ++I)
322 AttributeWithIndex P;
324 P.Attrs = Attributes::get(B);
327 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
328 AttributeWithIndex P;
335 //===----------------------------------------------------------------------===//
336 // AttrListPtr Smart Pointer
337 //===----------------------------------------------------------------------===//
339 class AttributeListImpl;
341 /// AttrListPtr - This class manages the ref count for the opaque
342 /// AttributeListImpl object and provides accessors for it.
344 /// AttrList - The attributes that we are managing. This can be null
345 /// to represent the empty attributes list.
346 AttributeListImpl *AttrList;
348 AttrListPtr() : AttrList(0) {}
349 AttrListPtr(const AttrListPtr &P);
350 const AttrListPtr &operator=(const AttrListPtr &RHS);
353 //===--------------------------------------------------------------------===//
354 // Attribute List Construction and Mutation
355 //===--------------------------------------------------------------------===//
357 /// get - Return a Attributes list with the specified parameters in it.
358 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
360 /// addAttr - Add the specified attribute at the specified index to this
361 /// attribute list. Since attribute lists are immutable, this
362 /// returns the new list.
363 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
365 /// removeAttr - Remove the specified attribute at the specified index from
366 /// this attribute list. Since attribute lists are immutable, this
367 /// returns the new list.
368 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
370 //===--------------------------------------------------------------------===//
371 // Attribute List Accessors
372 //===--------------------------------------------------------------------===//
373 /// getParamAttributes - The attributes for the specified index are
375 Attributes getParamAttributes(unsigned Idx) const {
376 return getAttributes(Idx);
379 /// getRetAttributes - The attributes for the ret value are
381 Attributes getRetAttributes() const {
382 return getAttributes(0);
385 /// getFnAttributes - The function attributes are returned.
386 Attributes getFnAttributes() const {
387 return getAttributes(~0U);
390 /// paramHasAttr - Return true if the specified parameter index has the
391 /// specified attribute set.
392 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
393 return getAttributes(Idx).hasAttributes(Attr);
396 /// getParamAlignment - Return the alignment for the specified function
398 unsigned getParamAlignment(unsigned Idx) const {
399 return getAttributes(Idx).getAlignment();
402 /// hasAttrSomewhere - Return true if the specified attribute is set for at
403 /// least one parameter or for the return value.
404 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
406 unsigned getNumAttrs() const;
407 Attributes &getAttributesAtIndex(unsigned i) const;
409 /// operator==/!= - Provide equality predicates.
410 bool operator==(const AttrListPtr &RHS) const
411 { return AttrList == RHS.AttrList; }
412 bool operator!=(const AttrListPtr &RHS) const
413 { return AttrList != RHS.AttrList; }
417 //===--------------------------------------------------------------------===//
418 // Attribute List Introspection
419 //===--------------------------------------------------------------------===//
421 /// getRawPointer - Return a raw pointer that uniquely identifies this
423 void *getRawPointer() const {
427 // Attributes are stored as a dense set of slots, where there is one
428 // slot for each argument that has an attribute. This allows walking over the
429 // dense set instead of walking the sparse list of attributes.
431 /// isEmpty - Return true if there are no attributes.
433 bool isEmpty() const {
434 return AttrList == 0;
437 /// getNumSlots - Return the number of slots used in this attribute list.
438 /// This is the number of arguments that have an attribute set on them
439 /// (including the function itself).
440 unsigned getNumSlots() const;
442 /// getSlot - Return the AttributeWithIndex at the specified slot. This
443 /// holds a index number plus a set of attributes.
444 const AttributeWithIndex &getSlot(unsigned Slot) const;
447 explicit AttrListPtr(AttributeListImpl *L);
449 /// getAttributes - The attributes for the specified index are
450 /// returned. Attributes for the result are denoted with Idx = 0.
451 Attributes getAttributes(unsigned Idx) const;
454 } // End llvm namespace