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/Support/MathExtras.h"
19 #include "llvm/ADT/ArrayRef.h"
29 /// We use this proxy POD type to allow constructing Attributes constants using
30 /// initializer lists. Do not use this class directly.
33 AttrConst operator | (const AttrConst Attrs) const {
34 AttrConst Res = {v | Attrs.v};
37 AttrConst operator ~ () const {
43 /// Function parameters and results can have attributes to indicate how they
44 /// should be treated by optimizations and code generation. This enumeration
45 /// lists the attributes that can be associated with parameters, function
46 /// results or the function itself.
47 /// @brief Function attributes.
49 /// We declare AttrConst objects that will be used throughout the code and also
50 /// raw uint64_t objects with _i suffix to be used below for other constant
51 /// declarations. This is done to avoid static CTORs and at the same time to
52 /// keep type-safety of Attributes.
53 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
54 const uint64_t name##_i = value; \
55 const AttrConst name = {value};
57 DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set
58 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
59 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
60 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
61 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
62 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
63 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
64 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
65 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
66 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
67 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
68 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
69 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
70 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
71 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
72 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
73 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
74 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
75 // stored as log2 of alignment with +1 bias
76 // 0 means unaligned different from align 1
77 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
78 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
79 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
81 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
82 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
84 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
85 ///function (3 bits) stored as log2
86 ///of alignment with +1 bias
87 ///0 means unaligned (different from
89 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
90 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
92 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
93 /// often, so lazy binding isn't
95 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
97 #undef DECLARE_LLVM_ATTRIBUTE
99 } // namespace Attribute
101 /// Attributes - A bitset of attributes.
103 // Currently, we need less than 64 bits.
106 Attributes() : Bits(0) { }
107 explicit Attributes(uint64_t Val) : Bits(Val) { }
108 /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) { }
110 // Attribute query methods.
111 // FIXME: StackAlignment & Alignment attributes have no predicate methods.
112 bool hasAttributes() const { return Bits != 0; }
114 bool hasZExtAttr() const {
115 return Bits & Attribute::ZExt_i;
117 bool hasSExtAttr() const {
118 return Bits & Attribute::SExt_i;
120 bool hasNoReturnAttr() const {
121 return Bits & Attribute::NoReturn_i;
123 bool hasInRegAttr() const {
124 return Bits & Attribute::InReg_i;
126 bool hasStructRetAttr() const {
127 return Bits & Attribute::StructRet_i;
129 bool hasNoUnwindAttr() const {
130 return Bits & Attribute::NoUnwind_i;
132 bool hasNoAliasAttr() const {
133 return Bits & Attribute::NoAlias_i;
135 bool hasByValAttr() const {
136 return Bits & Attribute::ByVal_i;
138 bool hasNestAttr() const {
139 return Bits & Attribute::Nest_i;
141 bool hasReadNoneAttr() const {
142 return Bits & Attribute::ReadNone_i;
144 bool hasReadOnlyAttr() const {
145 return Bits & Attribute::ReadOnly_i;
147 bool hasNoInlineAttr() const {
148 return Bits & Attribute::NoInline_i;
150 bool hasAlwaysInlineAttr() const {
151 return Bits & Attribute::AlwaysInline_i;
153 bool hasOptimizeForSizeAttr() const {
154 return Bits & Attribute::OptimizeForSize_i;
156 bool hasStackProtectAttr() const {
157 return Bits & Attribute::StackProtect_i;
159 bool hasStackProtectReqAttr() const {
160 return Bits & Attribute::StackProtectReq_i;
162 bool hasNoCaptureAttr() const {
163 return Bits & Attribute::NoCapture_i;
165 bool hasNoRedZoneAttr() const {
166 return Bits & Attribute::NoRedZone_i;
168 bool hasNoImplicitFloatAttr() const {
169 return Bits & Attribute::NoImplicitFloat_i;
171 bool hasNakedAttr() const {
172 return Bits & Attribute::Naked_i;
174 bool hasInlineHintAttr() const {
175 return Bits & Attribute::InlineHint_i;
177 bool hasReturnsTwiceAttr() const {
178 return Bits & Attribute::ReturnsTwice_i;
180 bool hasUWTableAttr() const {
181 return Bits & Attribute::UWTable_i;
183 bool hasNonLazyBindAttr() const {
184 return Bits & Attribute::NonLazyBind_i;
186 bool hasAddressSafetyAttr() const {
187 return Bits & Attribute::AddressSafety_i;
190 // This is a "safe bool() operator".
191 operator const void *() const { return Bits ? this : 0; }
192 bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; }
193 bool operator == (const Attributes &Attrs) const {
194 return Bits == Attrs.Bits;
196 bool operator != (const Attributes &Attrs) const {
197 return Bits != Attrs.Bits;
199 Attributes operator | (const Attributes &Attrs) const {
200 return Attributes(Bits | Attrs.Bits);
202 Attributes operator & (const Attributes &Attrs) const {
203 return Attributes(Bits & Attrs.Bits);
205 Attributes operator ^ (const Attributes &Attrs) const {
206 return Attributes(Bits ^ Attrs.Bits);
208 Attributes &operator |= (const Attributes &Attrs) {
212 Attributes &operator &= (const Attributes &Attrs) {
216 Attributes operator ~ () const { return Attributes(~Bits); }
217 uint64_t Raw() const { return Bits; }
219 /// The set of Attributes set in Attributes is converted to a string of
220 /// equivalent mnemonics. This is, presumably, for writing out the mnemonics
221 /// for the assembly writer.
222 /// @brief Convert attribute bits to text
223 std::string getAsString() const;
226 namespace Attribute {
228 /// Note that uwtable is about the ABI or the user mandating an entry in the
229 /// unwind table. The nounwind attribute is about an exception passing by the
231 /// In a theoretical system that uses tables for profiling and sjlj for
232 /// exceptions, they would be fully independent. In a normal system that
233 /// uses tables for both, the semantics are:
234 /// nil = Needs an entry because an exception might pass by.
235 /// nounwind = No need for an entry
236 /// uwtable = Needs an entry because the ABI says so and because
237 /// an exception might pass by.
238 /// uwtable + nounwind = Needs an entry because the ABI says so.
240 /// @brief Attributes that only apply to function parameters.
241 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
242 StructRet_i | NoCapture_i};
244 /// @brief Attributes that may be applied to the function itself. These cannot
245 /// be used on return values or function parameters.
246 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
247 ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
248 StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
249 Naked_i | InlineHint_i | StackAlignment_i |
250 UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
252 /// @brief Parameter attributes that do not apply to vararg call arguments.
253 const AttrConst VarArgsIncompatible = {StructRet_i};
255 /// @brief Attributes that are mutually incompatible.
256 const AttrConst MutuallyIncompatible[5] = {
257 {ByVal_i | Nest_i | StructRet_i},
258 {ByVal_i | Nest_i | InReg_i },
260 {ReadNone_i | ReadOnly_i},
261 {NoInline_i | AlwaysInline_i}
264 /// @brief Which attributes cannot be applied to a type.
265 Attributes typeIncompatible(Type *Ty);
267 /// This turns an int alignment (a power of 2, normally) into the
268 /// form used internally in Attributes.
269 inline Attributes constructAlignmentFromInt(unsigned i) {
270 // Default alignment, allow the target to define how to align it.
274 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
275 assert(i <= 0x40000000 && "Alignment too large.");
276 return Attributes((Log2_32(i)+1) << 16);
279 /// This returns the alignment field of an attribute as a byte alignment value.
280 inline unsigned getAlignmentFromAttrs(Attributes A) {
281 Attributes Align = A & Attribute::Alignment;
285 return 1U << ((Align.Raw() >> 16) - 1);
288 /// This turns an int stack alignment (which must be a power of 2) into
289 /// the form used internally in Attributes.
290 inline Attributes constructStackAlignmentFromInt(unsigned i) {
291 // Default alignment, allow the target to define how to align it.
295 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
296 assert(i <= 0x100 && "Alignment too large.");
297 return Attributes((Log2_32(i)+1) << 26);
300 /// This returns the stack alignment field of an attribute as a byte alignment
302 inline unsigned getStackAlignmentFromAttrs(Attributes A) {
303 Attributes StackAlign = A & Attribute::StackAlignment;
307 return 1U << ((StackAlign.Raw() >> 26) - 1);
310 /// This returns an integer containing an encoding of all the
311 /// LLVM attributes found in the given attribute bitset. Any
312 /// change to this encoding is a breaking change to bitcode
314 inline uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
315 // FIXME: It doesn't make sense to store the alignment information as an
316 // expanded out value, we should store it as a log2 value. However, we can't
317 // just change that here without breaking bitcode compatibility. If this ever
318 // becomes a problem in practice, we should introduce new tag numbers in the
319 // bitcode file and have those tags use a more efficiently encoded alignment
322 // Store the alignment in the bitcode as a 16-bit raw value instead of a
323 // 5-bit log2 encoded value. Shift the bits above the alignment up by
326 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
327 if (Attrs & Attribute::Alignment)
328 EncodedAttrs |= (1ull << 16) <<
329 (((Attrs & Attribute::Alignment).Raw()-1) >> 16);
330 EncodedAttrs |= (Attrs.Raw() & (0xfffull << 21)) << 11;
335 /// This returns an attribute bitset containing the LLVM attributes
336 /// that have been decoded from the given integer. This function
337 /// must stay in sync with 'encodeLLVMAttributesForBitcode'.
338 inline Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
339 // The alignment is stored as a 16-bit raw value from bits 31--16.
340 // We shift the bits above 31 down by 11 bits.
342 unsigned Alignment = (EncodedAttrs & (0xffffull << 16)) >> 16;
343 assert((!Alignment || isPowerOf2_32(Alignment)) &&
344 "Alignment must be a power of two.");
346 Attributes Attrs(EncodedAttrs & 0xffff);
348 Attrs |= Attribute::constructAlignmentFromInt(Alignment);
349 Attrs |= Attributes((EncodedAttrs & (0xfffull << 32)) >> 11);
354 } // end namespace Attribute
356 /// This is just a pair of values to associate a set of attributes
358 struct AttributeWithIndex {
359 Attributes Attrs; ///< The attributes that are set, or'd together.
360 unsigned Index; ///< Index of the parameter for which the attributes apply.
361 ///< Index 0 is used for return value attributes.
362 ///< Index ~0U is used for function attributes.
364 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
365 AttributeWithIndex P;
372 //===----------------------------------------------------------------------===//
373 // AttrListPtr Smart Pointer
374 //===----------------------------------------------------------------------===//
376 class AttributeListImpl;
378 /// AttrListPtr - This class manages the ref count for the opaque
379 /// AttributeListImpl object and provides accessors for it.
381 /// AttrList - The attributes that we are managing. This can be null
382 /// to represent the empty attributes list.
383 AttributeListImpl *AttrList;
385 AttrListPtr() : AttrList(0) {}
386 AttrListPtr(const AttrListPtr &P);
387 const AttrListPtr &operator=(const AttrListPtr &RHS);
390 //===--------------------------------------------------------------------===//
391 // Attribute List Construction and Mutation
392 //===--------------------------------------------------------------------===//
394 /// get - Return a Attributes list with the specified parameters in it.
395 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
397 /// addAttr - Add the specified attribute at the specified index to this
398 /// attribute list. Since attribute lists are immutable, this
399 /// returns the new list.
400 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
402 /// removeAttr - Remove the specified attribute at the specified index from
403 /// this attribute list. Since attribute lists are immutable, this
404 /// returns the new list.
405 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
407 //===--------------------------------------------------------------------===//
408 // Attribute List Accessors
409 //===--------------------------------------------------------------------===//
410 /// getParamAttributes - The attributes for the specified index are
412 Attributes getParamAttributes(unsigned Idx) const {
413 assert (Idx && Idx != ~0U && "Invalid parameter index!");
414 return getAttributes(Idx);
417 /// getRetAttributes - The attributes for the ret value are
419 Attributes getRetAttributes() const {
420 return getAttributes(0);
423 /// getFnAttributes - The function attributes are returned.
424 Attributes getFnAttributes() const {
425 return getAttributes(~0U);
428 /// paramHasAttr - Return true if the specified parameter index has the
429 /// specified attribute set.
430 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
431 return getAttributes(Idx) & Attr;
434 /// getParamAlignment - Return the alignment for the specified function
436 unsigned getParamAlignment(unsigned Idx) const {
437 return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
440 /// hasAttrSomewhere - Return true if the specified attribute is set for at
441 /// least one parameter or for the return value.
442 bool hasAttrSomewhere(Attributes Attr) const;
444 /// operator==/!= - Provide equality predicates.
445 bool operator==(const AttrListPtr &RHS) const
446 { return AttrList == RHS.AttrList; }
447 bool operator!=(const AttrListPtr &RHS) const
448 { return AttrList != RHS.AttrList; }
452 //===--------------------------------------------------------------------===//
453 // Attribute List Introspection
454 //===--------------------------------------------------------------------===//
456 /// getRawPointer - Return a raw pointer that uniquely identifies this
458 void *getRawPointer() const {
462 // Attributes are stored as a dense set of slots, where there is one
463 // slot for each argument that has an attribute. This allows walking over the
464 // dense set instead of walking the sparse list of attributes.
466 /// isEmpty - Return true if there are no attributes.
468 bool isEmpty() const {
469 return AttrList == 0;
472 /// getNumSlots - Return the number of slots used in this attribute list.
473 /// This is the number of arguments that have an attribute set on them
474 /// (including the function itself).
475 unsigned getNumSlots() const;
477 /// getSlot - Return the AttributeWithIndex at the specified slot. This
478 /// holds a index number plus a set of attributes.
479 const AttributeWithIndex &getSlot(unsigned Slot) const;
482 explicit AttrListPtr(AttributeListImpl *L);
484 /// getAttributes - The attributes for the specified index are
485 /// returned. Attributes for the result are denoted with Idx = 0.
486 Attributes getAttributes(unsigned Idx) const;
490 } // End llvm namespace