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"
29 /// AttributeImpl - The internal representation of the Attributes class. This is
33 /// Attributes - A bitset of attributes.
36 /// Function parameters and results can have attributes to indicate how they
37 /// should be treated by optimizations and code generation. This enumeration
38 /// lists the attributes that can be associated with parameters, function
39 /// results or the function itself.
41 /// Note that uwtable is about the ABI or the user mandating an entry in the
42 /// unwind table. The nounwind attribute is about an exception passing by the
45 /// In a theoretical system that uses tables for profiling and sjlj for
46 /// exceptions, they would be fully independent. In a normal system that uses
47 /// tables for both, the semantics are:
49 /// nil = Needs an entry because an exception might pass by.
50 /// nounwind = No need for an entry
51 /// uwtable = Needs an entry because the ABI says so and because
52 /// an exception might pass by.
53 /// uwtable + nounwind = Needs an entry because the ABI says so.
56 None = 0, ///< No attributes have been set
57 AddressSafety = 1, ///< Address safety checking is on.
58 Alignment = 2, ///< Alignment of parameter (5 bits)
59 ///< stored as log2 of alignment with +1 bias
60 ///< 0 means unaligned different from align 1
61 AlwaysInline = 3, ///< inline=always
62 ByVal = 4, ///< Pass structure by value
63 InlineHint = 5, ///< Source said inlining was desirable
64 InReg = 6, ///< Force argument to be passed in register
65 Naked = 7, ///< Naked function
66 Nest = 8, ///< Nested function static chain
67 NoAlias = 9, ///< Considered to not alias after call
68 NoCapture = 10, ///< Function creates no aliases of pointer
69 NoImplicitFloat = 11, ///< Disable implicit floating point insts
70 NoInline = 12, ///< inline=never
71 NonLazyBind = 13, ///< Function is called early and/or
72 ///< often, so lazy binding isn't worthwhile
73 NoRedZone = 14, ///< Disable redzone
74 NoReturn = 15, ///< Mark the function as not returning
75 NoUnwind = 16, ///< Function doesn't unwind stack
76 OptimizeForSize = 17, ///< opt_size
77 ReadNone = 18, ///< Function does not access memory
78 ReadOnly = 19, ///< Function only reads from memory
79 ReturnsTwice = 20, ///< Function can return twice
80 SExt = 21, ///< Sign extended before/after call
81 StackAlignment = 22, ///< Alignment of stack for function (3 bits)
82 ///< stored as log2 of alignment with +1 bias 0
83 ///< means unaligned (different from
85 StackProtect = 23, ///< Stack protection.
86 StackProtectReq = 24, ///< Stack protection required.
87 StructRet = 25, ///< Hidden pointer to structure to return
88 UWTable = 26, ///< Function must be in a unwind table
89 ZExt = 27 ///< Zero extended before/after call
94 explicit Attributes(AttributesImpl *A);
96 Attributes() : Attrs(0) {}
97 explicit Attributes(uint64_t Val);
98 explicit Attributes(LLVMContext &C, AttrVal Val);
99 Attributes(const Attributes &A);
102 friend class Attributes;
105 Builder() : Bits(0) {}
106 Builder(const Attributes &A) : Bits(A.Raw()) {}
108 void clear() { Bits = 0; }
110 bool hasAttribute(Attributes::AttrVal A) const;
111 bool hasAttributes() const;
112 bool hasAttributes(const Attributes &A) const;
113 bool hasAlignmentAttr() const;
115 uint64_t getAlignment() const;
116 uint64_t getStackAlignment() const;
118 Builder &addAttribute(Attributes::AttrVal Val);
119 Builder &removeAttribute(Attributes::AttrVal Val);
121 void addAlignmentAttr(unsigned Align);
123 /// addStackAlignmentAttr - This turns an int stack alignment (which must be
124 /// a power of 2) into the form used internally in Attributes.
125 void addStackAlignmentAttr(unsigned Align);
127 void removeAttributes(const Attributes &A);
129 /// @brief Remove attributes that are used on functions only.
130 void removeFunctionOnlyAttrs() {
131 removeAttribute(Attributes::NoReturn)
132 .removeAttribute(Attributes::NoUnwind)
133 .removeAttribute(Attributes::ReadNone)
134 .removeAttribute(Attributes::ReadOnly)
135 .removeAttribute(Attributes::NoInline)
136 .removeAttribute(Attributes::AlwaysInline)
137 .removeAttribute(Attributes::OptimizeForSize)
138 .removeAttribute(Attributes::StackProtect)
139 .removeAttribute(Attributes::StackProtectReq)
140 .removeAttribute(Attributes::NoRedZone)
141 .removeAttribute(Attributes::NoImplicitFloat)
142 .removeAttribute(Attributes::Naked)
143 .removeAttribute(Attributes::InlineHint)
144 .removeAttribute(Attributes::StackAlignment)
145 .removeAttribute(Attributes::UWTable)
146 .removeAttribute(Attributes::NonLazyBind)
147 .removeAttribute(Attributes::ReturnsTwice)
148 .removeAttribute(Attributes::AddressSafety);
152 /// get - Return a uniquified Attributes object. This takes the uniquified
153 /// value from the Builder and wraps it in the Attributes class.
154 static Attributes get(Builder &B);
155 static Attributes get(LLVMContext &Context, Builder &B);
157 /// @brief Return true if the attribute is present.
158 bool hasAttribute(AttrVal Val) const;
160 /// @brief Return true if attributes exist
161 bool hasAttributes() const {
162 return Attrs.hasAttributes();
165 /// @brief Return true if the attributes are a non-null intersection.
166 bool hasAttributes(const Attributes &A) const;
168 /// @brief Returns the alignment field of an attribute as a byte alignment
170 unsigned getAlignment() const;
172 /// @brief Returns the stack alignment field of an attribute as a byte
174 unsigned getStackAlignment() const;
176 /// @brief Parameter attributes that do not apply to vararg call arguments.
177 bool hasIncompatibleWithVarArgsAttrs() const {
178 return hasAttribute(Attributes::StructRet);
181 /// @brief Attributes that only apply to function parameters.
182 bool hasParameterOnlyAttrs() const {
183 return hasAttribute(Attributes::ByVal) ||
184 hasAttribute(Attributes::Nest) ||
185 hasAttribute(Attributes::StructRet) ||
186 hasAttribute(Attributes::NoCapture);
189 /// @brief Attributes that may be applied to the function itself. These cannot
190 /// be used on return values or function parameters.
191 bool hasFunctionOnlyAttrs() const {
192 return hasAttribute(Attributes::NoReturn) ||
193 hasAttribute(Attributes::NoUnwind) ||
194 hasAttribute(Attributes::ReadNone) ||
195 hasAttribute(Attributes::ReadOnly) ||
196 hasAttribute(Attributes::NoInline) ||
197 hasAttribute(Attributes::AlwaysInline) ||
198 hasAttribute(Attributes::OptimizeForSize) ||
199 hasAttribute(Attributes::StackProtect) ||
200 hasAttribute(Attributes::StackProtectReq) ||
201 hasAttribute(Attributes::NoRedZone) ||
202 hasAttribute(Attributes::NoImplicitFloat) ||
203 hasAttribute(Attributes::Naked) ||
204 hasAttribute(Attributes::InlineHint) ||
205 hasAttribute(Attributes::StackAlignment) ||
206 hasAttribute(Attributes::UWTable) ||
207 hasAttribute(Attributes::NonLazyBind) ||
208 hasAttribute(Attributes::ReturnsTwice) ||
209 hasAttribute(Attributes::AddressSafety);
212 bool isEmptyOrSingleton() const;
214 // This is a "safe bool() operator".
215 operator const void *() const { return Attrs.Bits ? this : 0; }
216 bool operator == (const Attributes &A) const {
217 return Attrs.Bits == A.Attrs.Bits;
219 bool operator != (const Attributes &A) const {
220 return Attrs.Bits != A.Attrs.Bits;
223 Attributes operator | (const Attributes &A) const;
224 Attributes operator & (const Attributes &A) const;
225 Attributes operator ^ (const Attributes &A) const;
226 Attributes &operator |= (const Attributes &A);
227 Attributes &operator &= (const Attributes &A);
228 Attributes operator ~ () const;
230 uint64_t Raw() const;
232 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
233 /// normally) into the form used internally in Attributes.
234 static Attributes constructAlignmentFromInt(unsigned i) {
235 // Default alignment, allow the target to define how to align it.
239 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
240 assert(i <= 0x40000000 && "Alignment too large.");
241 return Attributes((Log2_32(i)+1) << 16);
244 /// @brief Which attributes cannot be applied to a type.
245 static Attributes typeIncompatible(Type *Ty);
247 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
248 /// encoding of all the LLVM attributes found in the given attribute bitset.
249 /// Any change to this encoding is a breaking change to bitcode compatibility.
250 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
251 // FIXME: It doesn't make sense to store the alignment information as an
252 // expanded out value, we should store it as a log2 value. However, we
253 // can't just change that here without breaking bitcode compatibility. If
254 // this ever becomes a problem in practice, we should introduce new tag
255 // numbers in the bitcode file and have those tags use a more efficiently
256 // encoded alignment field.
258 // Store the alignment in the bitcode as a 16-bit raw value instead of a
259 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
261 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
262 if (Attrs.hasAttribute(Attributes::Alignment))
263 EncodedAttrs |= Attrs.getAlignment() << 16;
264 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
268 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
269 /// containing the LLVM attributes that have been decoded from the given
270 /// integer. This function must stay in sync with
271 /// 'encodeLLVMAttributesForBitcode'.
272 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
273 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
274 // the bits above 31 down by 11 bits.
275 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
276 assert((!Alignment || isPowerOf2_32(Alignment)) &&
277 "Alignment must be a power of two.");
279 Attributes Attrs(EncodedAttrs & 0xffff);
281 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
282 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
286 /// getAsString - The set of Attributes set in Attributes is converted to a
287 /// string of equivalent mnemonics. This is, presumably, for writing out the
288 /// mnemonics for the assembly writer.
289 /// @brief Convert attribute bits to text
290 std::string getAsString() const;
293 //===----------------------------------------------------------------------===//
294 // AttributeWithIndex
295 //===----------------------------------------------------------------------===//
297 /// AttributeWithIndex - This is just a pair of values to associate a set of
298 /// attributes with an index.
299 struct AttributeWithIndex {
300 Attributes Attrs; ///< The attributes that are set, or'd together.
301 unsigned Index; ///< Index of the parameter for which the attributes apply.
302 ///< Index 0 is used for return value attributes.
303 ///< Index ~0U is used for function attributes.
305 static AttributeWithIndex get(unsigned Idx,
306 ArrayRef<Attributes::AttrVal> Attrs) {
307 Attributes::Builder B;
309 for (ArrayRef<Attributes::AttrVal>::iterator I = Attrs.begin(),
310 E = Attrs.end(); I != E; ++I)
313 AttributeWithIndex P;
315 P.Attrs = Attributes::get(B);
318 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
319 AttributeWithIndex P;
326 //===----------------------------------------------------------------------===//
327 // AttrListPtr Smart Pointer
328 //===----------------------------------------------------------------------===//
330 class AttributeListImpl;
332 /// AttrListPtr - This class manages the ref count for the opaque
333 /// AttributeListImpl object and provides accessors for it.
335 /// AttrList - The attributes that we are managing. This can be null
336 /// to represent the empty attributes list.
337 AttributeListImpl *AttrList;
339 AttrListPtr() : AttrList(0) {}
340 AttrListPtr(const AttrListPtr &P);
341 const AttrListPtr &operator=(const AttrListPtr &RHS);
344 //===--------------------------------------------------------------------===//
345 // Attribute List Construction and Mutation
346 //===--------------------------------------------------------------------===//
348 /// get - Return a Attributes list with the specified parameters in it.
349 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
351 /// addAttr - Add the specified attribute at the specified index to this
352 /// attribute list. Since attribute lists are immutable, this
353 /// returns the new list.
354 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
356 /// removeAttr - Remove the specified attribute at the specified index from
357 /// this attribute list. Since attribute lists are immutable, this
358 /// returns the new list.
359 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
361 //===--------------------------------------------------------------------===//
362 // Attribute List Accessors
363 //===--------------------------------------------------------------------===//
364 /// getParamAttributes - The attributes for the specified index are
366 Attributes getParamAttributes(unsigned Idx) const {
367 return getAttributes(Idx);
370 /// getRetAttributes - The attributes for the ret value are
372 Attributes getRetAttributes() const {
373 return getAttributes(0);
376 /// getFnAttributes - The function attributes are returned.
377 Attributes getFnAttributes() const {
378 return getAttributes(~0U);
381 /// paramHasAttr - Return true if the specified parameter index has the
382 /// specified attribute set.
383 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
384 return getAttributes(Idx).hasAttributes(Attr);
387 /// getParamAlignment - Return the alignment for the specified function
389 unsigned getParamAlignment(unsigned Idx) const {
390 return getAttributes(Idx).getAlignment();
393 /// hasAttrSomewhere - Return true if the specified attribute is set for at
394 /// least one parameter or for the return value.
395 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
397 unsigned getNumAttrs() const;
398 Attributes &getAttributesAtIndex(unsigned i) const;
400 /// operator==/!= - Provide equality predicates.
401 bool operator==(const AttrListPtr &RHS) const
402 { return AttrList == RHS.AttrList; }
403 bool operator!=(const AttrListPtr &RHS) const
404 { return AttrList != RHS.AttrList; }
408 //===--------------------------------------------------------------------===//
409 // Attribute List Introspection
410 //===--------------------------------------------------------------------===//
412 /// getRawPointer - Return a raw pointer that uniquely identifies this
414 void *getRawPointer() const {
418 // Attributes are stored as a dense set of slots, where there is one
419 // slot for each argument that has an attribute. This allows walking over the
420 // dense set instead of walking the sparse list of attributes.
422 /// isEmpty - Return true if there are no attributes.
424 bool isEmpty() const {
425 return AttrList == 0;
428 /// getNumSlots - Return the number of slots used in this attribute list.
429 /// This is the number of arguments that have an attribute set on them
430 /// (including the function itself).
431 unsigned getNumSlots() const;
433 /// getSlot - Return the AttributeWithIndex at the specified slot. This
434 /// holds a index number plus a set of attributes.
435 const AttributeWithIndex &getSlot(unsigned Slot) const;
438 explicit AttrListPtr(AttributeListImpl *L);
440 /// getAttributes - The attributes for the specified index are
441 /// returned. Attributes for the result are denoted with Idx = 0.
442 Attributes getAttributes(unsigned Idx) const;
445 } // End llvm namespace