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 uint64_t name##_i = value; \
57 const AttrConst name = {value};
59 DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set
60 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
61 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
62 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
63 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
64 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
65 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
66 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
67 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
68 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
69 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
70 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
71 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
72 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
73 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
74 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
75 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
76 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
77 // stored as log2 of alignment with +1 bias
78 // 0 means unaligned different from align 1
79 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
80 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
81 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
83 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
84 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
86 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
87 ///function (3 bits) stored as log2
88 ///of alignment with +1 bias
89 ///0 means unaligned (different from
91 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
92 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
94 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
95 /// often, so lazy binding isn't
97 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
99 #undef DECLARE_LLVM_ATTRIBUTE
101 /// Note that uwtable is about the ABI or the user mandating an entry in the
102 /// unwind table. The nounwind attribute is about an exception passing by the
104 /// In a theoretical system that uses tables for profiling and sjlj for
105 /// exceptions, they would be fully independent. In a normal system that
106 /// uses tables for both, the semantics are:
107 /// nil = Needs an entry because an exception might pass by.
108 /// nounwind = No need for an entry
109 /// uwtable = Needs an entry because the ABI says so and because
110 /// an exception might pass by.
111 /// uwtable + nounwind = Needs an entry because the ABI says so.
113 /// @brief Attributes that are mutually incompatible.
114 const AttrConst MutuallyIncompatible[5] = {
115 {ByVal_i | Nest_i | StructRet_i},
116 {ByVal_i | Nest_i | InReg_i },
118 {ReadNone_i | ReadOnly_i},
119 {NoInline_i | AlwaysInline_i}
122 } // namespace Attribute
124 /// AttributeImpl - The internal representation of the Attributes class. This is
126 class AttributesImpl;
128 /// Attributes - A bitset of attributes.
132 None = 0, ///< No attributes have been set
133 AddressSafety = 1, ///< Address safety checking is on.
134 Alignment = 2, ///< Alignment of parameter (5 bits)
135 ///< stored as log2 of alignment with +1 bias
136 ///< 0 means unaligned different from align 1
137 AlwaysInline = 3, ///< inline=always
138 ByVal = 4, ///< Pass structure by value
139 InlineHint = 5, ///< Source said inlining was desirable
140 InReg = 6, ///< Force argument to be passed in register
141 Naked = 7, ///< Naked function
142 Nest = 8, ///< Nested function static chain
143 NoAlias = 9, ///< Considered to not alias after call
144 NoCapture = 10, ///< Function creates no aliases of pointer
145 NoImplicitFloat = 11, ///< Disable implicit floating point insts
146 NoInline = 12, ///< inline=never
147 NonLazyBind = 13, ///< Function is called early and/or
148 ///< often, so lazy binding isn't worthwhile
149 NoRedZone = 14, ///< Disable redzone
150 NoReturn = 15, ///< Mark the function as not returning
151 NoUnwind = 16, ///< Function doesn't unwind stack
152 OptimizeForSize = 17, ///< opt_size
153 ReadNone = 18, ///< Function does not access memory
154 ReadOnly = 19, ///< Function only reads from memory
155 ReturnsTwice = 20, ///< Function can return twice
156 SExt = 21, ///< Sign extended before/after call
157 StackAlignment = 22, ///< Alignment of stack for function (3 bits)
158 ///< stored as log2 of alignment with +1 bias 0
159 ///< means unaligned (different from
161 StackProtect = 23, ///< Stack protection.
162 StackProtectReq = 24, ///< Stack protection required.
163 StructRet = 25, ///< Hidden pointer to structure to return
164 UWTable = 26, ///< Function must be in a unwind table
165 ZExt = 27 ///< Zero extended before/after call
168 AttributesImpl Attrs;
170 explicit Attributes(AttributesImpl *A);
172 Attributes() : Attrs(0) {}
173 explicit Attributes(uint64_t Val);
174 /*implicit*/ Attributes(Attribute::AttrConst Val);
175 Attributes(const Attributes &A);
178 friend class Attributes;
181 Builder() : Bits(0) {}
182 Builder(const Attributes &A) : Bits(A.Raw()) {}
184 void clear() { Bits = 0; }
186 bool hasAttributes() const;
187 bool hasAttributes(const Attributes &A) const;
188 bool hasAlignmentAttr() const;
190 uint64_t getAlignment() const;
192 Builder &addAttribute(Attributes::AttrVal Val);
193 Builder &removeAttribute(Attributes::AttrVal Val);
195 void addAlignmentAttr(unsigned Align);
196 void addStackAlignmentAttr(unsigned Align);
198 void removeAttributes(const Attributes &A);
200 /// @brief Remove attributes that are used on functions only.
201 void removeFunctionOnlyAttrs() {
202 removeAttribute(Attributes::NoReturn)
203 .removeAttribute(Attributes::NoUnwind)
204 .removeAttribute(Attributes::ReadNone)
205 .removeAttribute(Attributes::ReadOnly)
206 .removeAttribute(Attributes::NoInline)
207 .removeAttribute(Attributes::AlwaysInline)
208 .removeAttribute(Attributes::OptimizeForSize)
209 .removeAttribute(Attributes::StackProtect)
210 .removeAttribute(Attributes::StackProtectReq)
211 .removeAttribute(Attributes::NoRedZone)
212 .removeAttribute(Attributes::NoImplicitFloat)
213 .removeAttribute(Attributes::Naked)
214 .removeAttribute(Attributes::InlineHint)
215 .removeAttribute(Attributes::StackAlignment)
216 .removeAttribute(Attributes::UWTable)
217 .removeAttribute(Attributes::NonLazyBind)
218 .removeAttribute(Attributes::ReturnsTwice)
219 .removeAttribute(Attributes::AddressSafety);
223 /// get - Return a uniquified Attributes object. This takes the uniquified
224 /// value from the Builder and wraps it in the Attributes class.
225 static Attributes get(Builder &B);
226 static Attributes get(LLVMContext &Context, Builder &B);
228 /// @brief Return true if the attribute is present.
229 bool hasAttribute(AttrVal Val) const;
231 /// @brief Return true if attributes exist
232 bool hasAttributes() const {
233 return Attrs.hasAttributes();
236 /// @brief Return true if the attributes are a non-null intersection.
237 bool hasAttributes(const Attributes &A) const;
239 /// @brief Returns the alignment field of an attribute as a byte alignment
241 unsigned getAlignment() const;
243 /// @brief Returns the stack alignment field of an attribute as a byte
245 unsigned getStackAlignment() const;
247 /// @brief Parameter attributes that do not apply to vararg call arguments.
248 bool hasIncompatibleWithVarArgsAttrs() const {
249 return hasAttribute(Attributes::StructRet);
252 /// @brief Attributes that only apply to function parameters.
253 bool hasParameterOnlyAttrs() const {
254 return hasAttribute(Attributes::ByVal) ||
255 hasAttribute(Attributes::Nest) ||
256 hasAttribute(Attributes::StructRet) ||
257 hasAttribute(Attributes::NoCapture);
260 /// @brief Attributes that may be applied to the function itself. These cannot
261 /// be used on return values or function parameters.
262 bool hasFunctionOnlyAttrs() const {
263 return hasAttribute(Attributes::NoReturn) ||
264 hasAttribute(Attributes::NoUnwind) ||
265 hasAttribute(Attributes::ReadNone) ||
266 hasAttribute(Attributes::ReadOnly) ||
267 hasAttribute(Attributes::NoInline) ||
268 hasAttribute(Attributes::AlwaysInline) ||
269 hasAttribute(Attributes::OptimizeForSize) ||
270 hasAttribute(Attributes::StackProtect) ||
271 hasAttribute(Attributes::StackProtectReq) ||
272 hasAttribute(Attributes::NoRedZone) ||
273 hasAttribute(Attributes::NoImplicitFloat) ||
274 hasAttribute(Attributes::Naked) ||
275 hasAttribute(Attributes::InlineHint) ||
276 hasAttribute(Attributes::StackAlignment) ||
277 hasAttribute(Attributes::UWTable) ||
278 hasAttribute(Attributes::NonLazyBind) ||
279 hasAttribute(Attributes::ReturnsTwice) ||
280 hasAttribute(Attributes::AddressSafety);
283 bool isEmptyOrSingleton() const;
285 // This is a "safe bool() operator".
286 operator const void *() const { return Attrs.Bits ? this : 0; }
287 bool operator == (const Attributes &A) const {
288 return Attrs.Bits == A.Attrs.Bits;
290 bool operator != (const Attributes &A) const {
291 return Attrs.Bits != A.Attrs.Bits;
294 Attributes operator | (const Attributes &A) const;
295 Attributes operator & (const Attributes &A) const;
296 Attributes operator ^ (const Attributes &A) const;
297 Attributes &operator |= (const Attributes &A);
298 Attributes &operator &= (const Attributes &A);
299 Attributes operator ~ () const;
301 uint64_t Raw() const;
303 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
304 /// normally) into the form used internally in Attributes.
305 static Attributes constructAlignmentFromInt(unsigned i) {
306 // Default alignment, allow the target to define how to align it.
308 return Attribute::None;
310 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
311 assert(i <= 0x40000000 && "Alignment too large.");
312 return Attributes((Log2_32(i)+1) << 16);
315 /// constructStackAlignmentFromInt - This turns an int stack alignment (which
316 /// must be a power of 2) into the form used internally in Attributes.
317 static Attributes constructStackAlignmentFromInt(unsigned i) {
318 // Default alignment, allow the target to define how to align it.
320 return Attribute::None;
322 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
323 assert(i <= 0x100 && "Alignment too large.");
324 return Attributes((Log2_32(i)+1) << 26);
327 /// @brief Which attributes cannot be applied to a type.
328 static Attributes typeIncompatible(Type *Ty);
330 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
331 /// encoding of all the LLVM attributes found in the given attribute bitset.
332 /// Any change to this encoding is a breaking change to bitcode compatibility.
333 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
334 // FIXME: It doesn't make sense to store the alignment information as an
335 // expanded out value, we should store it as a log2 value. However, we
336 // can't just change that here without breaking bitcode compatibility. If
337 // this ever becomes a problem in practice, we should introduce new tag
338 // numbers in the bitcode file and have those tags use a more efficiently
339 // encoded alignment field.
341 // Store the alignment in the bitcode as a 16-bit raw value instead of a
342 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
344 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
345 if (Attrs.hasAttribute(Attributes::Alignment))
346 EncodedAttrs |= (1ULL << 16) <<
347 (((Attrs.Raw() & Attribute::Alignment_i) - 1) >> 16);
348 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
352 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
353 /// containing the LLVM attributes that have been decoded from the given
354 /// integer. This function must stay in sync with
355 /// 'encodeLLVMAttributesForBitcode'.
356 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
357 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
358 // the bits above 31 down by 11 bits.
359 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
360 assert((!Alignment || isPowerOf2_32(Alignment)) &&
361 "Alignment must be a power of two.");
363 Attributes Attrs(EncodedAttrs & 0xffff);
365 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
366 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
370 /// getAsString - The set of Attributes set in Attributes is converted to a
371 /// string of equivalent mnemonics. This is, presumably, for writing out the
372 /// mnemonics for the assembly writer.
373 /// @brief Convert attribute bits to text
374 std::string getAsString() const;
377 //===----------------------------------------------------------------------===//
378 // AttributeWithIndex
379 //===----------------------------------------------------------------------===//
381 /// AttributeWithIndex - This is just a pair of values to associate a set of
382 /// attributes with an index.
383 struct AttributeWithIndex {
384 Attributes Attrs; ///< The attributes that are set, or'd together.
385 unsigned Index; ///< Index of the parameter for which the attributes apply.
386 ///< Index 0 is used for return value attributes.
387 ///< Index ~0U is used for function attributes.
389 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
390 AttributeWithIndex P;
397 //===----------------------------------------------------------------------===//
398 // AttrListPtr Smart Pointer
399 //===----------------------------------------------------------------------===//
401 class AttributeListImpl;
403 /// AttrListPtr - This class manages the ref count for the opaque
404 /// AttributeListImpl object and provides accessors for it.
406 /// AttrList - The attributes that we are managing. This can be null
407 /// to represent the empty attributes list.
408 AttributeListImpl *AttrList;
410 AttrListPtr() : AttrList(0) {}
411 AttrListPtr(const AttrListPtr &P);
412 const AttrListPtr &operator=(const AttrListPtr &RHS);
415 //===--------------------------------------------------------------------===//
416 // Attribute List Construction and Mutation
417 //===--------------------------------------------------------------------===//
419 /// get - Return a Attributes list with the specified parameters in it.
420 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
422 /// addAttr - Add the specified attribute at the specified index to this
423 /// attribute list. Since attribute lists are immutable, this
424 /// returns the new list.
425 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
427 /// removeAttr - Remove the specified attribute at the specified index from
428 /// this attribute list. Since attribute lists are immutable, this
429 /// returns the new list.
430 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
432 //===--------------------------------------------------------------------===//
433 // Attribute List Accessors
434 //===--------------------------------------------------------------------===//
435 /// getParamAttributes - The attributes for the specified index are
437 Attributes getParamAttributes(unsigned Idx) const {
438 return getAttributes(Idx);
441 /// getRetAttributes - The attributes for the ret value are
443 Attributes getRetAttributes() const {
444 return getAttributes(0);
447 /// getFnAttributes - The function attributes are returned.
448 Attributes getFnAttributes() const {
449 return getAttributes(~0U);
452 /// paramHasAttr - Return true if the specified parameter index has the
453 /// specified attribute set.
454 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
455 return getAttributes(Idx).hasAttributes(Attr);
458 /// getParamAlignment - Return the alignment for the specified function
460 unsigned getParamAlignment(unsigned Idx) const {
461 return getAttributes(Idx).getAlignment();
464 /// hasAttrSomewhere - Return true if the specified attribute is set for at
465 /// least one parameter or for the return value.
466 bool hasAttrSomewhere(Attributes Attr) const;
468 unsigned getNumAttrs() const;
469 Attributes &getAttributesAtIndex(unsigned i) const;
471 /// operator==/!= - Provide equality predicates.
472 bool operator==(const AttrListPtr &RHS) const
473 { return AttrList == RHS.AttrList; }
474 bool operator!=(const AttrListPtr &RHS) const
475 { return AttrList != RHS.AttrList; }
479 //===--------------------------------------------------------------------===//
480 // Attribute List Introspection
481 //===--------------------------------------------------------------------===//
483 /// getRawPointer - Return a raw pointer that uniquely identifies this
485 void *getRawPointer() const {
489 // Attributes are stored as a dense set of slots, where there is one
490 // slot for each argument that has an attribute. This allows walking over the
491 // dense set instead of walking the sparse list of attributes.
493 /// isEmpty - Return true if there are no attributes.
495 bool isEmpty() const {
496 return AttrList == 0;
499 /// getNumSlots - Return the number of slots used in this attribute list.
500 /// This is the number of arguments that have an attribute set on them
501 /// (including the function itself).
502 unsigned getNumSlots() const;
504 /// getSlot - Return the AttributeWithIndex at the specified slot. This
505 /// holds a index number plus a set of attributes.
506 const AttributeWithIndex &getSlot(unsigned Slot) const;
509 explicit AttrListPtr(AttributeListImpl *L);
511 /// getAttributes - The attributes for the specified index are
512 /// returned. Attributes for the result are denoted with Idx = 0.
513 Attributes getAttributes(unsigned Idx) const;
516 } // End llvm namespace