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 explicit Builder(uint64_t B) : Bits(B) {}
107 Builder(const Attributes &A) : Bits(A.Raw()) {}
109 void clear() { Bits = 0; }
111 bool hasAttribute(Attributes::AttrVal A) const;
112 bool hasAttributes() const;
113 bool hasAttributes(const Attributes &A) const;
114 bool hasAlignmentAttr() const;
116 uint64_t getAlignment() const;
117 uint64_t getStackAlignment() const;
119 Builder &addAttribute(Attributes::AttrVal Val);
120 Builder &removeAttribute(Attributes::AttrVal Val);
122 /// addRawValue - Add the raw value to the internal representation. This
123 /// should be used ONLY for decoding bitcode!
124 Builder &addRawValue(uint64_t Val);
126 /// addAlignmentAttr - This turns an int alignment (which must be a power of
127 /// 2) into the form used internally in Attributes.
128 Builder &addAlignmentAttr(unsigned Align);
130 /// addStackAlignmentAttr - This turns an int stack alignment (which must be
131 /// a power of 2) into the form used internally in Attributes.
132 Builder &addStackAlignmentAttr(unsigned Align);
134 Builder &removeAttributes(const Attributes &A);
136 /// @brief Remove attributes that are used on functions only.
137 void removeFunctionOnlyAttrs() {
138 removeAttribute(Attributes::NoReturn)
139 .removeAttribute(Attributes::NoUnwind)
140 .removeAttribute(Attributes::ReadNone)
141 .removeAttribute(Attributes::ReadOnly)
142 .removeAttribute(Attributes::NoInline)
143 .removeAttribute(Attributes::AlwaysInline)
144 .removeAttribute(Attributes::OptimizeForSize)
145 .removeAttribute(Attributes::StackProtect)
146 .removeAttribute(Attributes::StackProtectReq)
147 .removeAttribute(Attributes::NoRedZone)
148 .removeAttribute(Attributes::NoImplicitFloat)
149 .removeAttribute(Attributes::Naked)
150 .removeAttribute(Attributes::InlineHint)
151 .removeAttribute(Attributes::StackAlignment)
152 .removeAttribute(Attributes::UWTable)
153 .removeAttribute(Attributes::NonLazyBind)
154 .removeAttribute(Attributes::ReturnsTwice)
155 .removeAttribute(Attributes::AddressSafety);
158 bool operator==(const Builder &B) {
159 return Bits == B.Bits;
161 bool operator!=(const Builder &B) {
162 return Bits != B.Bits;
166 /// get - Return a uniquified Attributes object. This takes the uniquified
167 /// value from the Builder and wraps it in the Attributes class.
168 static Attributes get(Builder &B);
169 static Attributes get(LLVMContext &Context, Builder &B);
171 /// @brief Return true if the attribute is present.
172 bool hasAttribute(AttrVal Val) const;
174 /// @brief Return true if attributes exist
175 bool hasAttributes() const {
176 return Attrs.hasAttributes();
179 /// @brief Return true if the attributes are a non-null intersection.
180 bool hasAttributes(const Attributes &A) const;
182 /// @brief Returns the alignment field of an attribute as a byte alignment
184 unsigned getAlignment() const;
186 /// @brief Returns the stack alignment field of an attribute as a byte
188 unsigned getStackAlignment() const;
190 /// @brief Parameter attributes that do not apply to vararg call arguments.
191 bool hasIncompatibleWithVarArgsAttrs() const {
192 return hasAttribute(Attributes::StructRet);
195 /// @brief Attributes that only apply to function parameters.
196 bool hasParameterOnlyAttrs() const {
197 return hasAttribute(Attributes::ByVal) ||
198 hasAttribute(Attributes::Nest) ||
199 hasAttribute(Attributes::StructRet) ||
200 hasAttribute(Attributes::NoCapture);
203 /// @brief Attributes that may be applied to the function itself. These cannot
204 /// be used on return values or function parameters.
205 bool hasFunctionOnlyAttrs() const {
206 return hasAttribute(Attributes::NoReturn) ||
207 hasAttribute(Attributes::NoUnwind) ||
208 hasAttribute(Attributes::ReadNone) ||
209 hasAttribute(Attributes::ReadOnly) ||
210 hasAttribute(Attributes::NoInline) ||
211 hasAttribute(Attributes::AlwaysInline) ||
212 hasAttribute(Attributes::OptimizeForSize) ||
213 hasAttribute(Attributes::StackProtect) ||
214 hasAttribute(Attributes::StackProtectReq) ||
215 hasAttribute(Attributes::NoRedZone) ||
216 hasAttribute(Attributes::NoImplicitFloat) ||
217 hasAttribute(Attributes::Naked) ||
218 hasAttribute(Attributes::InlineHint) ||
219 hasAttribute(Attributes::StackAlignment) ||
220 hasAttribute(Attributes::UWTable) ||
221 hasAttribute(Attributes::NonLazyBind) ||
222 hasAttribute(Attributes::ReturnsTwice) ||
223 hasAttribute(Attributes::AddressSafety);
226 bool isEmptyOrSingleton() const;
228 // This is a "safe bool() operator".
229 operator const void *() const { return Attrs.Bits ? this : 0; }
230 bool operator == (const Attributes &A) const {
231 return Attrs.Bits == A.Attrs.Bits;
233 bool operator != (const Attributes &A) const {
234 return Attrs.Bits != A.Attrs.Bits;
237 Attributes operator | (const Attributes &A) const;
238 Attributes operator & (const Attributes &A) const;
239 Attributes operator ^ (const Attributes &A) const;
240 Attributes &operator |= (const Attributes &A);
241 Attributes &operator &= (const Attributes &A);
243 uint64_t Raw() const;
245 /// @brief Which attributes cannot be applied to a type.
246 static Attributes typeIncompatible(Type *Ty);
248 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
249 /// encoding of all the LLVM attributes found in the given attribute bitset.
250 /// Any change to this encoding is a breaking change to bitcode compatibility.
251 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
252 // FIXME: It doesn't make sense to store the alignment information as an
253 // expanded out value, we should store it as a log2 value. However, we
254 // can't just change that here without breaking bitcode compatibility. If
255 // this ever becomes a problem in practice, we should introduce new tag
256 // numbers in the bitcode file and have those tags use a more efficiently
257 // encoded alignment field.
259 // Store the alignment in the bitcode as a 16-bit raw value instead of a
260 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
262 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
263 if (Attrs.hasAttribute(Attributes::Alignment))
264 EncodedAttrs |= Attrs.getAlignment() << 16;
265 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
269 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
270 /// containing the LLVM attributes that have been decoded from the given
271 /// integer. This function must stay in sync with
272 /// 'encodeLLVMAttributesForBitcode'.
273 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
274 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
275 // the bits above 31 down by 11 bits.
276 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
277 assert((!Alignment || isPowerOf2_32(Alignment)) &&
278 "Alignment must be a power of two.");
280 Attributes::Builder B(EncodedAttrs & 0xffff);
282 B.addAlignmentAttr(Alignment);
283 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
284 return Attributes::get(B);
287 /// getAsString - The set of Attributes set in Attributes is converted to a
288 /// string of equivalent mnemonics. This is, presumably, for writing out the
289 /// mnemonics for the assembly writer.
290 /// @brief Convert attribute bits to text
291 std::string getAsString() const;
294 //===----------------------------------------------------------------------===//
295 // AttributeWithIndex
296 //===----------------------------------------------------------------------===//
298 /// AttributeWithIndex - This is just a pair of values to associate a set of
299 /// attributes with an index.
300 struct AttributeWithIndex {
301 Attributes Attrs; ///< The attributes that are set, or'd together.
302 unsigned Index; ///< Index of the parameter for which the attributes apply.
303 ///< Index 0 is used for return value attributes.
304 ///< Index ~0U is used for function attributes.
306 static AttributeWithIndex get(unsigned Idx,
307 ArrayRef<Attributes::AttrVal> Attrs) {
308 Attributes::Builder B;
310 for (ArrayRef<Attributes::AttrVal>::iterator I = Attrs.begin(),
311 E = Attrs.end(); I != E; ++I)
314 AttributeWithIndex P;
316 P.Attrs = Attributes::get(B);
319 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
320 AttributeWithIndex P;
327 //===----------------------------------------------------------------------===//
328 // AttrListPtr Smart Pointer
329 //===----------------------------------------------------------------------===//
331 class AttributeListImpl;
333 /// AttrListPtr - This class manages the ref count for the opaque
334 /// AttributeListImpl object and provides accessors for it.
336 /// AttrList - The attributes that we are managing. This can be null
337 /// to represent the empty attributes list.
338 AttributeListImpl *AttrList;
340 AttrListPtr() : AttrList(0) {}
341 AttrListPtr(const AttrListPtr &P);
342 const AttrListPtr &operator=(const AttrListPtr &RHS);
345 //===--------------------------------------------------------------------===//
346 // Attribute List Construction and Mutation
347 //===--------------------------------------------------------------------===//
349 /// get - Return a Attributes list with the specified parameters in it.
350 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
352 /// addAttr - Add the specified attribute at the specified index to this
353 /// attribute list. Since attribute lists are immutable, this
354 /// returns the new list.
355 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
357 /// removeAttr - Remove the specified attribute at the specified index from
358 /// this attribute list. Since attribute lists are immutable, this
359 /// returns the new list.
360 AttrListPtr removeAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
362 //===--------------------------------------------------------------------===//
363 // Attribute List Accessors
364 //===--------------------------------------------------------------------===//
365 /// getParamAttributes - The attributes for the specified index are
367 Attributes getParamAttributes(unsigned Idx) const {
368 return getAttributes(Idx);
371 /// getRetAttributes - The attributes for the ret value are
373 Attributes getRetAttributes() const {
374 return getAttributes(0);
377 /// getFnAttributes - The function attributes are returned.
378 Attributes getFnAttributes() const {
379 return getAttributes(~0U);
382 /// paramHasAttr - Return true if the specified parameter index has the
383 /// specified attribute set.
384 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
385 return getAttributes(Idx).hasAttributes(Attr);
388 /// getParamAlignment - Return the alignment for the specified function
390 unsigned getParamAlignment(unsigned Idx) const {
391 return getAttributes(Idx).getAlignment();
394 /// hasAttrSomewhere - Return true if the specified attribute is set for at
395 /// least one parameter or for the return value.
396 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
398 unsigned getNumAttrs() const;
399 Attributes &getAttributesAtIndex(unsigned i) const;
401 /// operator==/!= - Provide equality predicates.
402 bool operator==(const AttrListPtr &RHS) const
403 { return AttrList == RHS.AttrList; }
404 bool operator!=(const AttrListPtr &RHS) const
405 { return AttrList != RHS.AttrList; }
409 //===--------------------------------------------------------------------===//
410 // Attribute List Introspection
411 //===--------------------------------------------------------------------===//
413 /// getRawPointer - Return a raw pointer that uniquely identifies this
415 void *getRawPointer() const {
419 // Attributes are stored as a dense set of slots, where there is one
420 // slot for each argument that has an attribute. This allows walking over the
421 // dense set instead of walking the sparse list of attributes.
423 /// isEmpty - Return true if there are no attributes.
425 bool isEmpty() const {
426 return AttrList == 0;
429 /// getNumSlots - Return the number of slots used in this attribute list.
430 /// This is the number of arguments that have an attribute set on them
431 /// (including the function itself).
432 unsigned getNumSlots() const;
434 /// getSlot - Return the AttributeWithIndex at the specified slot. This
435 /// holds a index number plus a set of attributes.
436 const AttributeWithIndex &getSlot(unsigned Slot) const;
439 explicit AttrListPtr(AttributeListImpl *L);
441 /// getAttributes - The attributes for the specified index are
442 /// returned. Attributes for the result are denoted with Idx = 0.
443 Attributes getAttributes(unsigned Idx) const;
446 } // End llvm namespace