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"
30 /// We use this proxy POD type to allow constructing Attributes constants using
31 /// initializer lists. Do not use this class directly.
34 AttrConst operator | (const AttrConst Attrs) const {
35 AttrConst Res = {v | Attrs.v};
38 AttrConst operator ~ () const {
44 /// Function parameters and results can have attributes to indicate how they
45 /// should be treated by optimizations and code generation. This enumeration
46 /// lists the attributes that can be associated with parameters, function
47 /// results or the function itself.
48 /// @brief Function attributes.
50 /// We declare AttrConst objects that will be used throughout the code and also
51 /// raw uint64_t objects with _i suffix to be used below for other constant
52 /// declarations. This is done to avoid static CTORs and at the same time to
53 /// keep type-safety of Attributes.
54 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
55 const uint64_t name##_i = value; \
56 const AttrConst name = {value};
58 DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set
59 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
60 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
61 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
62 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
63 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
64 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
65 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
66 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
67 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
68 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
69 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
70 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
71 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
72 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
73 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
74 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
75 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
76 // stored as log2 of alignment with +1 bias
77 // 0 means unaligned different from align 1
78 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
79 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
80 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
82 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
83 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
85 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
86 ///function (3 bits) stored as log2
87 ///of alignment with +1 bias
88 ///0 means unaligned (different from
90 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
91 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
93 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
94 /// often, so lazy binding isn't
96 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
98 #undef DECLARE_LLVM_ATTRIBUTE
100 /// Note that uwtable is about the ABI or the user mandating an entry in the
101 /// unwind table. The nounwind attribute is about an exception passing by the
103 /// In a theoretical system that uses tables for profiling and sjlj for
104 /// exceptions, they would be fully independent. In a normal system that
105 /// uses tables for both, the semantics are:
106 /// nil = Needs an entry because an exception might pass by.
107 /// nounwind = No need for an entry
108 /// uwtable = Needs an entry because the ABI says so and because
109 /// an exception might pass by.
110 /// uwtable + nounwind = Needs an entry because the ABI says so.
112 /// @brief Attributes that only apply to function parameters.
113 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
114 StructRet_i | NoCapture_i};
116 /// @brief Attributes that may be applied to the function itself. These cannot
117 /// be used on return values or function parameters.
118 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
119 ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
120 StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
121 Naked_i | InlineHint_i | StackAlignment_i |
122 UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
124 /// @brief Parameter attributes that do not apply to vararg call arguments.
125 const AttrConst VarArgsIncompatible = {StructRet_i};
127 /// @brief Attributes that are mutually incompatible.
128 const AttrConst MutuallyIncompatible[5] = {
129 {ByVal_i | Nest_i | StructRet_i},
130 {ByVal_i | Nest_i | InReg_i },
132 {ReadNone_i | ReadOnly_i},
133 {NoInline_i | AlwaysInline_i}
136 } // namespace Attribute
138 /// AttributeImpl - The internal representation of the Attributes class. This is
140 class AttributesImpl;
142 /// Attributes - A bitset of attributes.
144 // Currently, we need less than 64 bits.
147 explicit Attributes(AttributesImpl *A);
149 Attributes() : Bits(0) {}
150 explicit Attributes(uint64_t Val) : Bits(Val) {}
151 /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) {}
154 friend class Attributes;
157 Builder() : Bits(0) {}
158 Builder(const Attributes &A) : Bits(A.Bits) {}
161 Bits |= Attribute::ZExt_i;
164 Bits |= Attribute::SExt_i;
166 void addNoReturnAttr() {
167 Bits |= Attribute::NoReturn_i;
169 void addInRegAttr() {
170 Bits |= Attribute::InReg_i;
172 void addStructRetAttr() {
173 Bits |= Attribute::StructRet_i;
175 void addNoUnwindAttr() {
176 Bits |= Attribute::NoUnwind_i;
178 void addNoAliasAttr() {
179 Bits |= Attribute::NoAlias_i;
181 void addByValAttr() {
182 Bits |= Attribute::ByVal_i;
185 Bits |= Attribute::Nest_i;
187 void addReadNoneAttr() {
188 Bits |= Attribute::ReadNone_i;
190 void addReadOnlyAttr() {
191 Bits |= Attribute::ReadOnly_i;
193 void addNoInlineAttr() {
194 Bits |= Attribute::NoInline_i;
196 void addAlwaysInlineAttr() {
197 Bits |= Attribute::AlwaysInline_i;
199 void addOptimizeForSizeAttr() {
200 Bits |= Attribute::OptimizeForSize_i;
202 void addStackProtectAttr() {
203 Bits |= Attribute::StackProtect_i;
205 void addStackProtectReqAttr() {
206 Bits |= Attribute::StackProtectReq_i;
208 void addNoCaptureAttr() {
209 Bits |= Attribute::NoCapture_i;
211 void addNoRedZoneAttr() {
212 Bits |= Attribute::NoRedZone_i;
214 void addNoImplicitFloatAttr() {
215 Bits |= Attribute::NoImplicitFloat_i;
217 void addNakedAttr() {
218 Bits |= Attribute::Naked_i;
220 void addInlineHintAttr() {
221 Bits |= Attribute::InlineHint_i;
223 void addReturnsTwiceAttr() {
224 Bits |= Attribute::ReturnsTwice_i;
226 void addUWTableAttr() {
227 Bits |= Attribute::UWTable_i;
229 void addNonLazyBindAttr() {
230 Bits |= Attribute::NonLazyBind_i;
232 void addAddressSafetyAttr() {
233 Bits |= Attribute::AddressSafety_i;
235 void addAlignmentAttr(unsigned Align) {
236 if (Align == 0) return;
237 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
238 assert(Align <= 0x40000000 && "Alignment too large.");
239 Bits |= (Log2_32(Align) + 1) << 16;
241 void addStackAlignmentAttr(unsigned Align) {
242 // Default alignment, allow the target to define how to align it.
243 if (Align == 0) return;
245 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
246 assert(Align <= 0x100 && "Alignment too large.");
247 Bits |= (Log2_32(Align) + 1) << 26;
251 /// get - Return a uniquified Attributes object. This takes the uniquified
252 /// value from the Builder and wraps it in the Attributes class.
253 static Attributes get(LLVMContext &Context, Builder &B);
255 // Attribute query methods.
256 // FIXME: StackAlignment & Alignment attributes have no predicate methods.
257 bool hasAttributes() const {
260 bool hasAttributes(const Attributes &A) const {
261 return Bits & A.Bits;
264 bool hasZExtAttr() const {
265 return Bits & Attribute::ZExt_i;
267 bool hasSExtAttr() const {
268 return Bits & Attribute::SExt_i;
270 bool hasNoReturnAttr() const {
271 return Bits & Attribute::NoReturn_i;
273 bool hasInRegAttr() const {
274 return Bits & Attribute::InReg_i;
276 bool hasStructRetAttr() const {
277 return Bits & Attribute::StructRet_i;
279 bool hasNoUnwindAttr() const {
280 return Bits & Attribute::NoUnwind_i;
282 bool hasNoAliasAttr() const {
283 return Bits & Attribute::NoAlias_i;
285 bool hasByValAttr() const {
286 return Bits & Attribute::ByVal_i;
288 bool hasNestAttr() const {
289 return Bits & Attribute::Nest_i;
291 bool hasReadNoneAttr() const {
292 return Bits & Attribute::ReadNone_i;
294 bool hasReadOnlyAttr() const {
295 return Bits & Attribute::ReadOnly_i;
297 bool hasNoInlineAttr() const {
298 return Bits & Attribute::NoInline_i;
300 bool hasAlwaysInlineAttr() const {
301 return Bits & Attribute::AlwaysInline_i;
303 bool hasOptimizeForSizeAttr() const {
304 return Bits & Attribute::OptimizeForSize_i;
306 bool hasStackProtectAttr() const {
307 return Bits & Attribute::StackProtect_i;
309 bool hasStackProtectReqAttr() const {
310 return Bits & Attribute::StackProtectReq_i;
312 bool hasAlignmentAttr() const {
313 return Bits & Attribute::Alignment_i;
315 bool hasNoCaptureAttr() const {
316 return Bits & Attribute::NoCapture_i;
318 bool hasNoRedZoneAttr() const {
319 return Bits & Attribute::NoRedZone_i;
321 bool hasNoImplicitFloatAttr() const {
322 return Bits & Attribute::NoImplicitFloat_i;
324 bool hasNakedAttr() const {
325 return Bits & Attribute::Naked_i;
327 bool hasInlineHintAttr() const {
328 return Bits & Attribute::InlineHint_i;
330 bool hasReturnsTwiceAttr() const {
331 return Bits & Attribute::ReturnsTwice_i;
333 bool hasStackAlignmentAttr() const {
334 return Bits & Attribute::StackAlignment_i;
336 bool hasUWTableAttr() const {
337 return Bits & Attribute::UWTable_i;
339 bool hasNonLazyBindAttr() const {
340 return Bits & Attribute::NonLazyBind_i;
342 bool hasAddressSafetyAttr() const {
343 return Bits & Attribute::AddressSafety_i;
346 /// This returns the alignment field of an attribute as a byte alignment
348 unsigned getAlignment() const {
349 if (!hasAlignmentAttr())
351 return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
354 /// This returns the stack alignment field of an attribute as a byte alignment
356 unsigned getStackAlignment() const {
357 if (!hasStackAlignmentAttr())
359 return 1U << (((Bits & Attribute::StackAlignment_i) >> 26) - 1);
362 // This is a "safe bool() operator".
363 operator const void *() const { return Bits ? this : 0; }
364 bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; }
365 bool operator == (const Attributes &Attrs) const {
366 return Bits == Attrs.Bits;
368 bool operator != (const Attributes &Attrs) const {
369 return Bits != Attrs.Bits;
371 Attributes operator | (const Attributes &Attrs) const {
372 return Attributes(Bits | Attrs.Bits);
374 Attributes operator & (const Attributes &Attrs) const {
375 return Attributes(Bits & Attrs.Bits);
377 Attributes operator ^ (const Attributes &Attrs) const {
378 return Attributes(Bits ^ Attrs.Bits);
380 Attributes &operator |= (const Attributes &Attrs) {
384 Attributes &operator &= (const Attributes &Attrs) {
388 Attributes operator ~ () const { return Attributes(~Bits); }
389 uint64_t Raw() const { return Bits; }
391 /// This turns an int alignment (a power of 2, normally) into the form used
392 /// internally in Attributes.
393 static Attributes constructAlignmentFromInt(unsigned i) {
394 // Default alignment, allow the target to define how to align it.
396 return Attribute::None;
398 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
399 assert(i <= 0x40000000 && "Alignment too large.");
400 return Attributes((Log2_32(i)+1) << 16);
403 /// This turns an int stack alignment (which must be a power of 2) into the
404 /// form used internally in Attributes.
405 static Attributes constructStackAlignmentFromInt(unsigned i) {
406 // Default alignment, allow the target to define how to align it.
408 return Attribute::None;
410 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
411 assert(i <= 0x100 && "Alignment too large.");
412 return Attributes((Log2_32(i)+1) << 26);
415 /// @brief Which attributes cannot be applied to a type.
416 static Attributes typeIncompatible(Type *Ty);
418 /// This returns an integer containing an encoding of all the LLVM attributes
419 /// found in the given attribute bitset. Any change to this encoding is a
420 /// breaking change to bitcode compatibility.
421 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
422 // FIXME: It doesn't make sense to store the alignment information as an
423 // expanded out value, we should store it as a log2 value. However, we
424 // can't just change that here without breaking bitcode compatibility. If
425 // this ever becomes a problem in practice, we should introduce new tag
426 // numbers in the bitcode file and have those tags use a more efficiently
427 // encoded alignment field.
429 // Store the alignment in the bitcode as a 16-bit raw value instead of a
430 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
432 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
433 if (Attrs.hasAlignmentAttr())
434 EncodedAttrs |= (1ULL << 16) <<
435 (((Attrs.Bits & Attribute::Alignment_i) - 1) >> 16);
436 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
440 /// This returns an attribute bitset containing the LLVM attributes that have
441 /// been decoded from the given integer. This function must stay in sync with
442 /// 'encodeLLVMAttributesForBitcode'.
443 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
444 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
445 // the bits above 31 down by 11 bits.
446 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
447 assert((!Alignment || isPowerOf2_32(Alignment)) &&
448 "Alignment must be a power of two.");
450 Attributes Attrs(EncodedAttrs & 0xffff);
452 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
453 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
457 /// The set of Attributes set in Attributes is converted to a string of
458 /// equivalent mnemonics. This is, presumably, for writing out the mnemonics
459 /// for the assembly writer.
460 /// @brief Convert attribute bits to text
461 std::string getAsString() const;
464 /// This is just a pair of values to associate a set of attributes
466 struct AttributeWithIndex {
467 Attributes Attrs; ///< The attributes that are set, or'd together.
468 unsigned Index; ///< Index of the parameter for which the attributes apply.
469 ///< Index 0 is used for return value attributes.
470 ///< Index ~0U is used for function attributes.
472 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
473 AttributeWithIndex P;
480 //===----------------------------------------------------------------------===//
481 // AttrListPtr Smart Pointer
482 //===----------------------------------------------------------------------===//
484 class AttributeListImpl;
486 /// AttrListPtr - This class manages the ref count for the opaque
487 /// AttributeListImpl object and provides accessors for it.
489 /// AttrList - The attributes that we are managing. This can be null
490 /// to represent the empty attributes list.
491 AttributeListImpl *AttrList;
493 AttrListPtr() : AttrList(0) {}
494 AttrListPtr(const AttrListPtr &P);
495 const AttrListPtr &operator=(const AttrListPtr &RHS);
498 //===--------------------------------------------------------------------===//
499 // Attribute List Construction and Mutation
500 //===--------------------------------------------------------------------===//
502 /// get - Return a Attributes list with the specified parameters in it.
503 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
505 /// addAttr - Add the specified attribute at the specified index to this
506 /// attribute list. Since attribute lists are immutable, this
507 /// returns the new list.
508 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
510 /// removeAttr - Remove the specified attribute at the specified index from
511 /// this attribute list. Since attribute lists are immutable, this
512 /// returns the new list.
513 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
515 //===--------------------------------------------------------------------===//
516 // Attribute List Accessors
517 //===--------------------------------------------------------------------===//
518 /// getParamAttributes - The attributes for the specified index are
520 Attributes getParamAttributes(unsigned Idx) const {
521 return getAttributes(Idx);
524 /// getRetAttributes - The attributes for the ret value are
526 Attributes getRetAttributes() const {
527 return getAttributes(0);
530 /// getFnAttributes - The function attributes are returned.
531 Attributes getFnAttributes() const {
532 return getAttributes(~0U);
535 /// paramHasAttr - Return true if the specified parameter index has the
536 /// specified attribute set.
537 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
538 return getAttributes(Idx).hasAttributes(Attr);
541 /// getParamAlignment - Return the alignment for the specified function
543 unsigned getParamAlignment(unsigned Idx) const {
544 return getAttributes(Idx).getAlignment();
547 /// hasAttrSomewhere - Return true if the specified attribute is set for at
548 /// least one parameter or for the return value.
549 bool hasAttrSomewhere(Attributes Attr) const;
551 /// operator==/!= - Provide equality predicates.
552 bool operator==(const AttrListPtr &RHS) const
553 { return AttrList == RHS.AttrList; }
554 bool operator!=(const AttrListPtr &RHS) const
555 { return AttrList != RHS.AttrList; }
559 //===--------------------------------------------------------------------===//
560 // Attribute List Introspection
561 //===--------------------------------------------------------------------===//
563 /// getRawPointer - Return a raw pointer that uniquely identifies this
565 void *getRawPointer() const {
569 // Attributes are stored as a dense set of slots, where there is one
570 // slot for each argument that has an attribute. This allows walking over the
571 // dense set instead of walking the sparse list of attributes.
573 /// isEmpty - Return true if there are no attributes.
575 bool isEmpty() const {
576 return AttrList == 0;
579 /// getNumSlots - Return the number of slots used in this attribute list.
580 /// This is the number of arguments that have an attribute set on them
581 /// (including the function itself).
582 unsigned getNumSlots() const;
584 /// getSlot - Return the AttributeWithIndex at the specified slot. This
585 /// holds a index number plus a set of attributes.
586 const AttributeWithIndex &getSlot(unsigned Slot) const;
589 explicit AttrListPtr(AttributeListImpl *L);
591 /// getAttributes - The attributes for the specified index are
592 /// returned. Attributes for the result are denoted with Idx = 0.
593 Attributes getAttributes(unsigned Idx) const;
597 } // End llvm namespace