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 #undef DECLARE_LLVM_ATTRIBUTE
60 } // namespace Attribute
62 /// AttributeImpl - The internal representation of the Attributes class. This is
66 /// Attributes - A bitset of attributes.
69 /// Note that uwtable is about the ABI or the user mandating an entry in the
70 /// unwind table. The nounwind attribute is about an exception passing by the
73 /// In a theoretical system that uses tables for profiling and sjlj for
74 /// exceptions, they would be fully independent. In a normal system that uses
75 /// tables for both, the semantics are:
77 /// nil = Needs an entry because an exception might pass by.
78 /// nounwind = No need for an entry
79 /// uwtable = Needs an entry because the ABI says so and because
80 /// an exception might pass by.
81 /// uwtable + nounwind = Needs an entry because the ABI says so.
84 None = 0, ///< No attributes have been set
85 AddressSafety = 1, ///< Address safety checking is on.
86 Alignment = 2, ///< Alignment of parameter (5 bits)
87 ///< stored as log2 of alignment with +1 bias
88 ///< 0 means unaligned different from align 1
89 AlwaysInline = 3, ///< inline=always
90 ByVal = 4, ///< Pass structure by value
91 InlineHint = 5, ///< Source said inlining was desirable
92 InReg = 6, ///< Force argument to be passed in register
93 Naked = 7, ///< Naked function
94 Nest = 8, ///< Nested function static chain
95 NoAlias = 9, ///< Considered to not alias after call
96 NoCapture = 10, ///< Function creates no aliases of pointer
97 NoImplicitFloat = 11, ///< Disable implicit floating point insts
98 NoInline = 12, ///< inline=never
99 NonLazyBind = 13, ///< Function is called early and/or
100 ///< often, so lazy binding isn't worthwhile
101 NoRedZone = 14, ///< Disable redzone
102 NoReturn = 15, ///< Mark the function as not returning
103 NoUnwind = 16, ///< Function doesn't unwind stack
104 OptimizeForSize = 17, ///< opt_size
105 ReadNone = 18, ///< Function does not access memory
106 ReadOnly = 19, ///< Function only reads from memory
107 ReturnsTwice = 20, ///< Function can return twice
108 SExt = 21, ///< Sign extended before/after call
109 StackAlignment = 22, ///< Alignment of stack for function (3 bits)
110 ///< stored as log2 of alignment with +1 bias 0
111 ///< means unaligned (different from
113 StackProtect = 23, ///< Stack protection.
114 StackProtectReq = 24, ///< Stack protection required.
115 StructRet = 25, ///< Hidden pointer to structure to return
116 UWTable = 26, ///< Function must be in a unwind table
117 ZExt = 27 ///< Zero extended before/after call
120 AttributesImpl Attrs;
122 explicit Attributes(AttributesImpl *A);
124 Attributes() : Attrs(0) {}
125 explicit Attributes(uint64_t Val);
126 /*implicit*/ Attributes(Attribute::AttrConst Val);
127 Attributes(const Attributes &A);
130 friend class Attributes;
133 Builder() : Bits(0) {}
134 Builder(const Attributes &A) : Bits(A.Raw()) {}
136 void clear() { Bits = 0; }
138 bool hasAttribute(Attributes::AttrVal A) const;
139 bool hasAttributes() const;
140 bool hasAttributes(const Attributes &A) const;
141 bool hasAlignmentAttr() const;
143 uint64_t getAlignment() const;
144 uint64_t getStackAlignment() const;
146 Builder &addAttribute(Attributes::AttrVal Val);
147 Builder &removeAttribute(Attributes::AttrVal Val);
149 void addAlignmentAttr(unsigned Align);
150 void addStackAlignmentAttr(unsigned Align);
152 void removeAttributes(const Attributes &A);
154 /// @brief Remove attributes that are used on functions only.
155 void removeFunctionOnlyAttrs() {
156 removeAttribute(Attributes::NoReturn)
157 .removeAttribute(Attributes::NoUnwind)
158 .removeAttribute(Attributes::ReadNone)
159 .removeAttribute(Attributes::ReadOnly)
160 .removeAttribute(Attributes::NoInline)
161 .removeAttribute(Attributes::AlwaysInline)
162 .removeAttribute(Attributes::OptimizeForSize)
163 .removeAttribute(Attributes::StackProtect)
164 .removeAttribute(Attributes::StackProtectReq)
165 .removeAttribute(Attributes::NoRedZone)
166 .removeAttribute(Attributes::NoImplicitFloat)
167 .removeAttribute(Attributes::Naked)
168 .removeAttribute(Attributes::InlineHint)
169 .removeAttribute(Attributes::StackAlignment)
170 .removeAttribute(Attributes::UWTable)
171 .removeAttribute(Attributes::NonLazyBind)
172 .removeAttribute(Attributes::ReturnsTwice)
173 .removeAttribute(Attributes::AddressSafety);
177 /// get - Return a uniquified Attributes object. This takes the uniquified
178 /// value from the Builder and wraps it in the Attributes class.
179 static Attributes get(Builder &B);
180 static Attributes get(LLVMContext &Context, Builder &B);
182 /// @brief Return true if the attribute is present.
183 bool hasAttribute(AttrVal Val) const;
185 /// @brief Return true if attributes exist
186 bool hasAttributes() const {
187 return Attrs.hasAttributes();
190 /// @brief Return true if the attributes are a non-null intersection.
191 bool hasAttributes(const Attributes &A) const;
193 /// @brief Returns the alignment field of an attribute as a byte alignment
195 unsigned getAlignment() const;
197 /// @brief Returns the stack alignment field of an attribute as a byte
199 unsigned getStackAlignment() const;
201 /// @brief Parameter attributes that do not apply to vararg call arguments.
202 bool hasIncompatibleWithVarArgsAttrs() const {
203 return hasAttribute(Attributes::StructRet);
206 /// @brief Attributes that only apply to function parameters.
207 bool hasParameterOnlyAttrs() const {
208 return hasAttribute(Attributes::ByVal) ||
209 hasAttribute(Attributes::Nest) ||
210 hasAttribute(Attributes::StructRet) ||
211 hasAttribute(Attributes::NoCapture);
214 /// @brief Attributes that may be applied to the function itself. These cannot
215 /// be used on return values or function parameters.
216 bool hasFunctionOnlyAttrs() const {
217 return hasAttribute(Attributes::NoReturn) ||
218 hasAttribute(Attributes::NoUnwind) ||
219 hasAttribute(Attributes::ReadNone) ||
220 hasAttribute(Attributes::ReadOnly) ||
221 hasAttribute(Attributes::NoInline) ||
222 hasAttribute(Attributes::AlwaysInline) ||
223 hasAttribute(Attributes::OptimizeForSize) ||
224 hasAttribute(Attributes::StackProtect) ||
225 hasAttribute(Attributes::StackProtectReq) ||
226 hasAttribute(Attributes::NoRedZone) ||
227 hasAttribute(Attributes::NoImplicitFloat) ||
228 hasAttribute(Attributes::Naked) ||
229 hasAttribute(Attributes::InlineHint) ||
230 hasAttribute(Attributes::StackAlignment) ||
231 hasAttribute(Attributes::UWTable) ||
232 hasAttribute(Attributes::NonLazyBind) ||
233 hasAttribute(Attributes::ReturnsTwice) ||
234 hasAttribute(Attributes::AddressSafety);
237 bool isEmptyOrSingleton() const;
239 // This is a "safe bool() operator".
240 operator const void *() const { return Attrs.Bits ? this : 0; }
241 bool operator == (const Attributes &A) const {
242 return Attrs.Bits == A.Attrs.Bits;
244 bool operator != (const Attributes &A) const {
245 return Attrs.Bits != A.Attrs.Bits;
248 Attributes operator | (const Attributes &A) const;
249 Attributes operator & (const Attributes &A) const;
250 Attributes operator ^ (const Attributes &A) const;
251 Attributes &operator |= (const Attributes &A);
252 Attributes &operator &= (const Attributes &A);
253 Attributes operator ~ () const;
255 uint64_t Raw() const;
257 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
258 /// normally) into the form used internally in Attributes.
259 static Attributes constructAlignmentFromInt(unsigned i) {
260 // Default alignment, allow the target to define how to align it.
264 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
265 assert(i <= 0x40000000 && "Alignment too large.");
266 return Attributes((Log2_32(i)+1) << 16);
269 /// constructStackAlignmentFromInt - This turns an int stack alignment (which
270 /// must be a power of 2) into the form used internally in Attributes.
271 static Attributes constructStackAlignmentFromInt(unsigned i) {
272 // Default alignment, allow the target to define how to align it.
276 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
277 assert(i <= 0x100 && "Alignment too large.");
278 return Attributes((Log2_32(i)+1) << 26);
281 /// @brief Which attributes cannot be applied to a type.
282 static Attributes typeIncompatible(Type *Ty);
284 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
285 /// encoding of all the LLVM attributes found in the given attribute bitset.
286 /// Any change to this encoding is a breaking change to bitcode compatibility.
287 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
288 // FIXME: It doesn't make sense to store the alignment information as an
289 // expanded out value, we should store it as a log2 value. However, we
290 // can't just change that here without breaking bitcode compatibility. If
291 // this ever becomes a problem in practice, we should introduce new tag
292 // numbers in the bitcode file and have those tags use a more efficiently
293 // encoded alignment field.
295 // Store the alignment in the bitcode as a 16-bit raw value instead of a
296 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
298 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
299 if (Attrs.hasAttribute(Attributes::Alignment))
300 EncodedAttrs |= Attrs.getAlignment() << 16;
301 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
305 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
306 /// containing the LLVM attributes that have been decoded from the given
307 /// integer. This function must stay in sync with
308 /// 'encodeLLVMAttributesForBitcode'.
309 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
310 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
311 // the bits above 31 down by 11 bits.
312 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
313 assert((!Alignment || isPowerOf2_32(Alignment)) &&
314 "Alignment must be a power of two.");
316 Attributes Attrs(EncodedAttrs & 0xffff);
318 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
319 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
323 /// getAsString - The set of Attributes set in Attributes is converted to a
324 /// string of equivalent mnemonics. This is, presumably, for writing out the
325 /// mnemonics for the assembly writer.
326 /// @brief Convert attribute bits to text
327 std::string getAsString() const;
330 //===----------------------------------------------------------------------===//
331 // AttributeWithIndex
332 //===----------------------------------------------------------------------===//
334 /// AttributeWithIndex - This is just a pair of values to associate a set of
335 /// attributes with an index.
336 struct AttributeWithIndex {
337 Attributes Attrs; ///< The attributes that are set, or'd together.
338 unsigned Index; ///< Index of the parameter for which the attributes apply.
339 ///< Index 0 is used for return value attributes.
340 ///< Index ~0U is used for function attributes.
342 static AttributeWithIndex get(unsigned Idx,
343 ArrayRef<Attributes::AttrVal> Attrs) {
344 Attributes::Builder B;
346 for (ArrayRef<Attributes::AttrVal>::iterator I = Attrs.begin(),
347 E = Attrs.end(); I != E; ++I)
350 AttributeWithIndex P;
352 P.Attrs = Attributes::get(B);
355 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
356 AttributeWithIndex P;
363 //===----------------------------------------------------------------------===//
364 // AttrListPtr Smart Pointer
365 //===----------------------------------------------------------------------===//
367 class AttributeListImpl;
369 /// AttrListPtr - This class manages the ref count for the opaque
370 /// AttributeListImpl object and provides accessors for it.
372 /// AttrList - The attributes that we are managing. This can be null
373 /// to represent the empty attributes list.
374 AttributeListImpl *AttrList;
376 AttrListPtr() : AttrList(0) {}
377 AttrListPtr(const AttrListPtr &P);
378 const AttrListPtr &operator=(const AttrListPtr &RHS);
381 //===--------------------------------------------------------------------===//
382 // Attribute List Construction and Mutation
383 //===--------------------------------------------------------------------===//
385 /// get - Return a Attributes list with the specified parameters in it.
386 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
388 /// addAttr - Add the specified attribute at the specified index to this
389 /// attribute list. Since attribute lists are immutable, this
390 /// returns the new list.
391 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
393 /// removeAttr - Remove the specified attribute at the specified index from
394 /// this attribute list. Since attribute lists are immutable, this
395 /// returns the new list.
396 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
398 //===--------------------------------------------------------------------===//
399 // Attribute List Accessors
400 //===--------------------------------------------------------------------===//
401 /// getParamAttributes - The attributes for the specified index are
403 Attributes getParamAttributes(unsigned Idx) const {
404 return getAttributes(Idx);
407 /// getRetAttributes - The attributes for the ret value are
409 Attributes getRetAttributes() const {
410 return getAttributes(0);
413 /// getFnAttributes - The function attributes are returned.
414 Attributes getFnAttributes() const {
415 return getAttributes(~0U);
418 /// paramHasAttr - Return true if the specified parameter index has the
419 /// specified attribute set.
420 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
421 return getAttributes(Idx).hasAttributes(Attr);
424 /// getParamAlignment - Return the alignment for the specified function
426 unsigned getParamAlignment(unsigned Idx) const {
427 return getAttributes(Idx).getAlignment();
430 /// hasAttrSomewhere - Return true if the specified attribute is set for at
431 /// least one parameter or for the return value.
432 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
434 unsigned getNumAttrs() const;
435 Attributes &getAttributesAtIndex(unsigned i) const;
437 /// operator==/!= - Provide equality predicates.
438 bool operator==(const AttrListPtr &RHS) const
439 { return AttrList == RHS.AttrList; }
440 bool operator!=(const AttrListPtr &RHS) const
441 { return AttrList != RHS.AttrList; }
445 //===--------------------------------------------------------------------===//
446 // Attribute List Introspection
447 //===--------------------------------------------------------------------===//
449 /// getRawPointer - Return a raw pointer that uniquely identifies this
451 void *getRawPointer() const {
455 // Attributes are stored as a dense set of slots, where there is one
456 // slot for each argument that has an attribute. This allows walking over the
457 // dense set instead of walking the sparse list of attributes.
459 /// isEmpty - Return true if there are no attributes.
461 bool isEmpty() const {
462 return AttrList == 0;
465 /// getNumSlots - Return the number of slots used in this attribute list.
466 /// This is the number of arguments that have an attribute set on them
467 /// (including the function itself).
468 unsigned getNumSlots() const;
470 /// getSlot - Return the AttributeWithIndex at the specified slot. This
471 /// holds a index number plus a set of attributes.
472 const AttributeWithIndex &getSlot(unsigned Slot) const;
475 explicit AttrListPtr(AttributeListImpl *L);
477 /// getAttributes - The attributes for the specified index are
478 /// returned. Attributes for the result are denoted with Idx = 0.
479 Attributes getAttributes(unsigned Idx) const;
482 } // End llvm namespace