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"
31 /// AttrConst - We use this proxy POD type to allow constructing Attributes
32 /// constants using initializer lists. Do not use this class directly.
35 AttrConst operator | (const AttrConst Attrs) const {
36 AttrConst Res = {v | Attrs.v};
39 AttrConst operator ~ () const {
45 /// Function parameters and results can have attributes to indicate how they
46 /// should be treated by optimizations and code generation. This enumeration
47 /// lists the attributes that can be associated with parameters, function
48 /// results or the function itself.
49 /// @brief Function attributes.
51 /// We declare AttrConst objects that will be used throughout the code and also
52 /// raw uint64_t objects with _i suffix to be used below for other constant
53 /// declarations. This is done to avoid static CTORs and at the same time to
54 /// keep type-safety of Attributes.
55 #define DECLARE_LLVM_ATTRIBUTE(name, 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 } // namespace Attribute
114 /// AttributeImpl - The internal representation of the Attributes class. This is
116 class AttributesImpl;
118 /// Attributes - A bitset of attributes.
122 None = 0, ///< No attributes have been set
123 AddressSafety = 1, ///< Address safety checking is on.
124 Alignment = 2, ///< Alignment of parameter (5 bits)
125 ///< stored as log2 of alignment with +1 bias
126 ///< 0 means unaligned different from align 1
127 AlwaysInline = 3, ///< inline=always
128 ByVal = 4, ///< Pass structure by value
129 InlineHint = 5, ///< Source said inlining was desirable
130 InReg = 6, ///< Force argument to be passed in register
131 Naked = 7, ///< Naked function
132 Nest = 8, ///< Nested function static chain
133 NoAlias = 9, ///< Considered to not alias after call
134 NoCapture = 10, ///< Function creates no aliases of pointer
135 NoImplicitFloat = 11, ///< Disable implicit floating point insts
136 NoInline = 12, ///< inline=never
137 NonLazyBind = 13, ///< Function is called early and/or
138 ///< often, so lazy binding isn't worthwhile
139 NoRedZone = 14, ///< Disable redzone
140 NoReturn = 15, ///< Mark the function as not returning
141 NoUnwind = 16, ///< Function doesn't unwind stack
142 OptimizeForSize = 17, ///< opt_size
143 ReadNone = 18, ///< Function does not access memory
144 ReadOnly = 19, ///< Function only reads from memory
145 ReturnsTwice = 20, ///< Function can return twice
146 SExt = 21, ///< Sign extended before/after call
147 StackAlignment = 22, ///< Alignment of stack for function (3 bits)
148 ///< stored as log2 of alignment with +1 bias 0
149 ///< means unaligned (different from
151 StackProtect = 23, ///< Stack protection.
152 StackProtectReq = 24, ///< Stack protection required.
153 StructRet = 25, ///< Hidden pointer to structure to return
154 UWTable = 26, ///< Function must be in a unwind table
155 ZExt = 27 ///< Zero extended before/after call
158 AttributesImpl Attrs;
160 explicit Attributes(AttributesImpl *A);
162 Attributes() : Attrs(0) {}
163 explicit Attributes(uint64_t Val);
164 /*implicit*/ Attributes(Attribute::AttrConst Val);
165 Attributes(const Attributes &A);
168 friend class Attributes;
171 Builder() : Bits(0) {}
172 Builder(const Attributes &A) : Bits(A.Raw()) {}
174 void clear() { Bits = 0; }
176 bool hasAttributes() const;
177 bool hasAttributes(const Attributes &A) const;
178 bool hasAlignmentAttr() const;
180 uint64_t getAlignment() const;
182 Builder &addAttribute(Attributes::AttrVal Val);
183 Builder &removeAttribute(Attributes::AttrVal Val);
185 void addAlignmentAttr(unsigned Align);
186 void addStackAlignmentAttr(unsigned Align);
188 void removeAttributes(const Attributes &A);
190 /// @brief Remove attributes that are used on functions only.
191 void removeFunctionOnlyAttrs() {
192 removeAttribute(Attributes::NoReturn)
193 .removeAttribute(Attributes::NoUnwind)
194 .removeAttribute(Attributes::ReadNone)
195 .removeAttribute(Attributes::ReadOnly)
196 .removeAttribute(Attributes::NoInline)
197 .removeAttribute(Attributes::AlwaysInline)
198 .removeAttribute(Attributes::OptimizeForSize)
199 .removeAttribute(Attributes::StackProtect)
200 .removeAttribute(Attributes::StackProtectReq)
201 .removeAttribute(Attributes::NoRedZone)
202 .removeAttribute(Attributes::NoImplicitFloat)
203 .removeAttribute(Attributes::Naked)
204 .removeAttribute(Attributes::InlineHint)
205 .removeAttribute(Attributes::StackAlignment)
206 .removeAttribute(Attributes::UWTable)
207 .removeAttribute(Attributes::NonLazyBind)
208 .removeAttribute(Attributes::ReturnsTwice)
209 .removeAttribute(Attributes::AddressSafety);
213 /// get - Return a uniquified Attributes object. This takes the uniquified
214 /// value from the Builder and wraps it in the Attributes class.
215 static Attributes get(Builder &B);
216 static Attributes get(LLVMContext &Context, Builder &B);
218 /// @brief Return true if the attribute is present.
219 bool hasAttribute(AttrVal Val) const;
221 /// @brief Return true if attributes exist
222 bool hasAttributes() const {
223 return Attrs.hasAttributes();
226 /// @brief Return true if the attributes are a non-null intersection.
227 bool hasAttributes(const Attributes &A) const;
229 /// @brief Returns the alignment field of an attribute as a byte alignment
231 unsigned getAlignment() const;
233 /// @brief Returns the stack alignment field of an attribute as a byte
235 unsigned getStackAlignment() const;
237 /// @brief Parameter attributes that do not apply to vararg call arguments.
238 bool hasIncompatibleWithVarArgsAttrs() const {
239 return hasAttribute(Attributes::StructRet);
242 /// @brief Attributes that only apply to function parameters.
243 bool hasParameterOnlyAttrs() const {
244 return hasAttribute(Attributes::ByVal) ||
245 hasAttribute(Attributes::Nest) ||
246 hasAttribute(Attributes::StructRet) ||
247 hasAttribute(Attributes::NoCapture);
250 /// @brief Attributes that may be applied to the function itself. These cannot
251 /// be used on return values or function parameters.
252 bool hasFunctionOnlyAttrs() const {
253 return hasAttribute(Attributes::NoReturn) ||
254 hasAttribute(Attributes::NoUnwind) ||
255 hasAttribute(Attributes::ReadNone) ||
256 hasAttribute(Attributes::ReadOnly) ||
257 hasAttribute(Attributes::NoInline) ||
258 hasAttribute(Attributes::AlwaysInline) ||
259 hasAttribute(Attributes::OptimizeForSize) ||
260 hasAttribute(Attributes::StackProtect) ||
261 hasAttribute(Attributes::StackProtectReq) ||
262 hasAttribute(Attributes::NoRedZone) ||
263 hasAttribute(Attributes::NoImplicitFloat) ||
264 hasAttribute(Attributes::Naked) ||
265 hasAttribute(Attributes::InlineHint) ||
266 hasAttribute(Attributes::StackAlignment) ||
267 hasAttribute(Attributes::UWTable) ||
268 hasAttribute(Attributes::NonLazyBind) ||
269 hasAttribute(Attributes::ReturnsTwice) ||
270 hasAttribute(Attributes::AddressSafety);
273 bool isEmptyOrSingleton() const;
275 // This is a "safe bool() operator".
276 operator const void *() const { return Attrs.Bits ? this : 0; }
277 bool operator == (const Attributes &A) const {
278 return Attrs.Bits == A.Attrs.Bits;
280 bool operator != (const Attributes &A) const {
281 return Attrs.Bits != A.Attrs.Bits;
284 Attributes operator | (const Attributes &A) const;
285 Attributes operator & (const Attributes &A) const;
286 Attributes operator ^ (const Attributes &A) const;
287 Attributes &operator |= (const Attributes &A);
288 Attributes &operator &= (const Attributes &A);
289 Attributes operator ~ () const;
291 uint64_t Raw() const;
293 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
294 /// normally) into the form used internally in Attributes.
295 static Attributes constructAlignmentFromInt(unsigned i) {
296 // Default alignment, allow the target to define how to align it.
300 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
301 assert(i <= 0x40000000 && "Alignment too large.");
302 return Attributes((Log2_32(i)+1) << 16);
305 /// constructStackAlignmentFromInt - This turns an int stack alignment (which
306 /// must be a power of 2) into the form used internally in Attributes.
307 static Attributes constructStackAlignmentFromInt(unsigned i) {
308 // Default alignment, allow the target to define how to align it.
312 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
313 assert(i <= 0x100 && "Alignment too large.");
314 return Attributes((Log2_32(i)+1) << 26);
317 /// @brief Which attributes cannot be applied to a type.
318 static Attributes typeIncompatible(Type *Ty);
320 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
321 /// encoding of all the LLVM attributes found in the given attribute bitset.
322 /// Any change to this encoding is a breaking change to bitcode compatibility.
323 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
324 // FIXME: It doesn't make sense to store the alignment information as an
325 // expanded out value, we should store it as a log2 value. However, we
326 // can't just change that here without breaking bitcode compatibility. If
327 // this ever becomes a problem in practice, we should introduce new tag
328 // numbers in the bitcode file and have those tags use a more efficiently
329 // encoded alignment field.
331 // Store the alignment in the bitcode as a 16-bit raw value instead of a
332 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
334 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
335 if (Attrs.hasAttribute(Attributes::Alignment))
336 EncodedAttrs |= Attrs.getAlignment() << 16;
337 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
341 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
342 /// containing the LLVM attributes that have been decoded from the given
343 /// integer. This function must stay in sync with
344 /// 'encodeLLVMAttributesForBitcode'.
345 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
346 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
347 // the bits above 31 down by 11 bits.
348 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
349 assert((!Alignment || isPowerOf2_32(Alignment)) &&
350 "Alignment must be a power of two.");
352 Attributes Attrs(EncodedAttrs & 0xffff);
354 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
355 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
359 /// getAsString - The set of Attributes set in Attributes is converted to a
360 /// string of equivalent mnemonics. This is, presumably, for writing out the
361 /// mnemonics for the assembly writer.
362 /// @brief Convert attribute bits to text
363 std::string getAsString() const;
366 //===----------------------------------------------------------------------===//
367 // AttributeWithIndex
368 //===----------------------------------------------------------------------===//
370 /// AttributeWithIndex - This is just a pair of values to associate a set of
371 /// attributes with an index.
372 struct AttributeWithIndex {
373 Attributes Attrs; ///< The attributes that are set, or'd together.
374 unsigned Index; ///< Index of the parameter for which the attributes apply.
375 ///< Index 0 is used for return value attributes.
376 ///< Index ~0U is used for function attributes.
378 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
379 AttributeWithIndex P;
386 //===----------------------------------------------------------------------===//
387 // AttrListPtr Smart Pointer
388 //===----------------------------------------------------------------------===//
390 class AttributeListImpl;
392 /// AttrListPtr - This class manages the ref count for the opaque
393 /// AttributeListImpl object and provides accessors for it.
395 /// AttrList - The attributes that we are managing. This can be null
396 /// to represent the empty attributes list.
397 AttributeListImpl *AttrList;
399 AttrListPtr() : AttrList(0) {}
400 AttrListPtr(const AttrListPtr &P);
401 const AttrListPtr &operator=(const AttrListPtr &RHS);
404 //===--------------------------------------------------------------------===//
405 // Attribute List Construction and Mutation
406 //===--------------------------------------------------------------------===//
408 /// get - Return a Attributes list with the specified parameters in it.
409 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
411 /// addAttr - Add the specified attribute at the specified index to this
412 /// attribute list. Since attribute lists are immutable, this
413 /// returns the new list.
414 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
416 /// removeAttr - Remove the specified attribute at the specified index from
417 /// this attribute list. Since attribute lists are immutable, this
418 /// returns the new list.
419 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
421 //===--------------------------------------------------------------------===//
422 // Attribute List Accessors
423 //===--------------------------------------------------------------------===//
424 /// getParamAttributes - The attributes for the specified index are
426 Attributes getParamAttributes(unsigned Idx) const {
427 return getAttributes(Idx);
430 /// getRetAttributes - The attributes for the ret value are
432 Attributes getRetAttributes() const {
433 return getAttributes(0);
436 /// getFnAttributes - The function attributes are returned.
437 Attributes getFnAttributes() const {
438 return getAttributes(~0U);
441 /// paramHasAttr - Return true if the specified parameter index has the
442 /// specified attribute set.
443 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
444 return getAttributes(Idx).hasAttributes(Attr);
447 /// getParamAlignment - Return the alignment for the specified function
449 unsigned getParamAlignment(unsigned Idx) const {
450 return getAttributes(Idx).getAlignment();
453 /// hasAttrSomewhere - Return true if the specified attribute is set for at
454 /// least one parameter or for the return value.
455 bool hasAttrSomewhere(Attributes Attr) const;
457 unsigned getNumAttrs() const;
458 Attributes &getAttributesAtIndex(unsigned i) const;
460 /// operator==/!= - Provide equality predicates.
461 bool operator==(const AttrListPtr &RHS) const
462 { return AttrList == RHS.AttrList; }
463 bool operator!=(const AttrListPtr &RHS) const
464 { return AttrList != RHS.AttrList; }
468 //===--------------------------------------------------------------------===//
469 // Attribute List Introspection
470 //===--------------------------------------------------------------------===//
472 /// getRawPointer - Return a raw pointer that uniquely identifies this
474 void *getRawPointer() const {
478 // Attributes are stored as a dense set of slots, where there is one
479 // slot for each argument that has an attribute. This allows walking over the
480 // dense set instead of walking the sparse list of attributes.
482 /// isEmpty - Return true if there are no attributes.
484 bool isEmpty() const {
485 return AttrList == 0;
488 /// getNumSlots - Return the number of slots used in this attribute list.
489 /// This is the number of arguments that have an attribute set on them
490 /// (including the function itself).
491 unsigned getNumSlots() const;
493 /// getSlot - Return the AttributeWithIndex at the specified slot. This
494 /// holds a index number plus a set of attributes.
495 const AttributeWithIndex &getSlot(unsigned Slot) const;
498 explicit AttrListPtr(AttributeListImpl *L);
500 /// getAttributes - The attributes for the specified index are
501 /// returned. Attributes for the result are denoted with Idx = 0.
502 Attributes getAttributes(unsigned Idx) const;
505 } // End llvm namespace