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/Support/MathExtras.h"
19 #include "llvm/ADT/ArrayRef.h"
28 /// AttributeImpl - The internal representation of the Attributes class. This is
32 /// Attributes - A bitset of attributes.
35 /// Function parameters and results can have attributes to indicate how they
36 /// should be treated by optimizations and code generation. This enumeration
37 /// lists the attributes that can be associated with parameters, function
38 /// results or the function itself.
40 /// Note that uwtable is about the ABI or the user mandating an entry in the
41 /// unwind table. The nounwind attribute is about an exception passing by the
44 /// In a theoretical system that uses tables for profiling and sjlj for
45 /// exceptions, they would be fully independent. In a normal system that uses
46 /// tables for both, the semantics are:
48 /// nil = Needs an entry because an exception might pass by.
49 /// nounwind = No need for an entry
50 /// uwtable = Needs an entry because the ABI says so and because
51 /// an exception might pass by.
52 /// uwtable + nounwind = Needs an entry because the ABI says so.
55 None = 0, ///< No attributes have been set
56 AddressSafety = 1, ///< Address safety checking is on.
57 Alignment = 2, ///< Alignment of parameter (5 bits)
58 ///< stored as log2 of alignment with +1 bias
59 ///< 0 means unaligned different from align 1
60 AlwaysInline = 3, ///< inline=always
61 ByVal = 4, ///< Pass structure by value
62 InlineHint = 5, ///< Source said inlining was desirable
63 InReg = 6, ///< Force argument to be passed in register
64 Naked = 7, ///< Naked function
65 Nest = 8, ///< Nested function static chain
66 NoAlias = 9, ///< Considered to not alias after call
67 NoCapture = 10, ///< Function creates no aliases of pointer
68 NoImplicitFloat = 11, ///< Disable implicit floating point insts
69 NoInline = 12, ///< inline=never
70 NonLazyBind = 13, ///< Function is called early and/or
71 ///< often, so lazy binding isn't worthwhile
72 NoRedZone = 14, ///< Disable redzone
73 NoReturn = 15, ///< Mark the function as not returning
74 NoUnwind = 16, ///< Function doesn't unwind stack
75 OptimizeForSize = 17, ///< opt_size
76 ReadNone = 18, ///< Function does not access memory
77 ReadOnly = 19, ///< Function only reads from memory
78 ReturnsTwice = 20, ///< Function can return twice
79 SExt = 21, ///< Sign extended before/after call
80 StackAlignment = 22, ///< Alignment of stack for function (3 bits)
81 ///< stored as log2 of alignment with +1 bias 0
82 ///< means unaligned (different from
84 StackProtect = 23, ///< Stack protection.
85 StackProtectReq = 24, ///< Stack protection required.
86 StructRet = 25, ///< Hidden pointer to structure to return
87 UWTable = 26, ///< Function must be in a unwind table
88 ZExt = 27 ///< Zero extended before/after call
91 AttributesImpl *Attrs;
92 Attributes(AttributesImpl *A);
94 Attributes() : Attrs(0) {}
95 Attributes(const Attributes &A);
96 Attributes &operator=(const Attributes &A) {
101 /// get - Return a uniquified Attributes object. This takes the uniquified
102 /// value from the Builder and wraps it in the Attributes class.
104 static Attributes get(LLVMContext &Context, ArrayRef<AttrVal> Vals);
105 static Attributes get(LLVMContext &Context, Builder &B);
107 //===--------------------------------------------------------------------===//
108 /// Attributes::Builder - This class is used in conjunction with the
109 /// Attributes::get method to create an Attributes object. The object itself
110 /// is uniquified. The Builder's value, however, is not. So this can be used
111 /// as a quick way to test for equality, presence of attributes, etc.
113 friend class Attributes;
116 Builder() : Bits(0) {}
117 explicit Builder(uint64_t B) : Bits(B) {}
118 Builder(const Attributes &A) : Bits(A.Raw()) {}
119 Builder(const Builder &B) : Bits(B.Bits) {}
121 void clear() { Bits = 0; }
123 bool hasAttribute(Attributes::AttrVal A) const;
124 bool hasAttributes() const;
125 bool hasAttributes(const Attributes &A) const;
126 bool hasAlignmentAttr() const;
128 uint64_t getAlignment() const;
129 uint64_t getStackAlignment() const;
131 Builder &addAttribute(Attributes::AttrVal Val);
132 Builder &removeAttribute(Attributes::AttrVal Val);
134 Builder &addAttributes(const Attributes &A);
135 Builder &removeAttributes(const Attributes &A);
137 /// addRawValue - Add the raw value to the internal representation. This
138 /// should be used ONLY for decoding bitcode!
139 Builder &addRawValue(uint64_t Val);
141 /// addAlignmentAttr - This turns an int alignment (which must be a power of
142 /// 2) into the form used internally in Attributes.
143 Builder &addAlignmentAttr(unsigned Align);
145 /// addStackAlignmentAttr - This turns an int stack alignment (which must be
146 /// a power of 2) into the form used internally in Attributes.
147 Builder &addStackAlignmentAttr(unsigned Align);
149 /// @brief Remove attributes that are used on functions only.
150 void removeFunctionOnlyAttrs() {
151 removeAttribute(Attributes::NoReturn)
152 .removeAttribute(Attributes::NoUnwind)
153 .removeAttribute(Attributes::ReadNone)
154 .removeAttribute(Attributes::ReadOnly)
155 .removeAttribute(Attributes::NoInline)
156 .removeAttribute(Attributes::AlwaysInline)
157 .removeAttribute(Attributes::OptimizeForSize)
158 .removeAttribute(Attributes::StackProtect)
159 .removeAttribute(Attributes::StackProtectReq)
160 .removeAttribute(Attributes::NoRedZone)
161 .removeAttribute(Attributes::NoImplicitFloat)
162 .removeAttribute(Attributes::Naked)
163 .removeAttribute(Attributes::InlineHint)
164 .removeAttribute(Attributes::StackAlignment)
165 .removeAttribute(Attributes::UWTable)
166 .removeAttribute(Attributes::NonLazyBind)
167 .removeAttribute(Attributes::ReturnsTwice)
168 .removeAttribute(Attributes::AddressSafety);
171 bool operator==(const Builder &B) {
172 return Bits == B.Bits;
174 bool operator!=(const Builder &B) {
175 return Bits != B.Bits;
179 /// @brief Return true if the attribute is present.
180 bool hasAttribute(AttrVal Val) const;
182 /// @brief Return true if attributes exist
183 bool hasAttributes() const;
185 /// @brief Return true if the attributes are a non-null intersection.
186 bool hasAttributes(const Attributes &A) const;
188 /// @brief Returns the alignment field of an attribute as a byte alignment
190 unsigned getAlignment() const;
192 /// @brief Returns the stack alignment field of an attribute as a byte
194 unsigned getStackAlignment() const;
196 /// @brief Parameter attributes that do not apply to vararg call arguments.
197 bool hasIncompatibleWithVarArgsAttrs() const {
198 return hasAttribute(Attributes::StructRet);
201 /// @brief Attributes that only apply to function parameters.
202 bool hasParameterOnlyAttrs() const {
203 return hasAttribute(Attributes::ByVal) ||
204 hasAttribute(Attributes::Nest) ||
205 hasAttribute(Attributes::StructRet) ||
206 hasAttribute(Attributes::NoCapture);
209 /// @brief Attributes that may be applied to the function itself. These cannot
210 /// be used on return values or function parameters.
211 bool hasFunctionOnlyAttrs() const {
212 return hasAttribute(Attributes::NoReturn) ||
213 hasAttribute(Attributes::NoUnwind) ||
214 hasAttribute(Attributes::ReadNone) ||
215 hasAttribute(Attributes::ReadOnly) ||
216 hasAttribute(Attributes::NoInline) ||
217 hasAttribute(Attributes::AlwaysInline) ||
218 hasAttribute(Attributes::OptimizeForSize) ||
219 hasAttribute(Attributes::StackProtect) ||
220 hasAttribute(Attributes::StackProtectReq) ||
221 hasAttribute(Attributes::NoRedZone) ||
222 hasAttribute(Attributes::NoImplicitFloat) ||
223 hasAttribute(Attributes::Naked) ||
224 hasAttribute(Attributes::InlineHint) ||
225 hasAttribute(Attributes::StackAlignment) ||
226 hasAttribute(Attributes::UWTable) ||
227 hasAttribute(Attributes::NonLazyBind) ||
228 hasAttribute(Attributes::ReturnsTwice) ||
229 hasAttribute(Attributes::AddressSafety);
232 bool operator==(const Attributes &A) const {
233 return Attrs == A.Attrs;
235 bool operator!=(const Attributes &A) const {
236 return Attrs != A.Attrs;
239 uint64_t Raw() const;
241 /// @brief Which attributes cannot be applied to a type.
242 static Attributes typeIncompatible(Type *Ty);
244 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
245 /// encoding of all the LLVM attributes found in the given attribute bitset.
246 /// Any change to this encoding is a breaking change to bitcode compatibility.
247 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
248 // FIXME: It doesn't make sense to store the alignment information as an
249 // expanded out value, we should store it as a log2 value. However, we
250 // can't just change that here without breaking bitcode compatibility. If
251 // this ever becomes a problem in practice, we should introduce new tag
252 // numbers in the bitcode file and have those tags use a more efficiently
253 // encoded alignment field.
255 // Store the alignment in the bitcode as a 16-bit raw value instead of a
256 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
258 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
259 if (Attrs.hasAttribute(Attributes::Alignment))
260 EncodedAttrs |= Attrs.getAlignment() << 16;
261 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
265 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
266 /// containing the LLVM attributes that have been decoded from the given
267 /// integer. This function must stay in sync with
268 /// 'encodeLLVMAttributesForBitcode'.
269 static Attributes decodeLLVMAttributesForBitcode(LLVMContext &C,
270 uint64_t EncodedAttrs) {
271 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
272 // the bits above 31 down by 11 bits.
273 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
274 assert((!Alignment || isPowerOf2_32(Alignment)) &&
275 "Alignment must be a power of two.");
277 Attributes::Builder B(EncodedAttrs & 0xffff);
279 B.addAlignmentAttr(Alignment);
280 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
281 return Attributes::get(C, B);
284 /// getAsString - The set of Attributes set in Attributes is converted to a
285 /// string of equivalent mnemonics. This is, presumably, for writing out the
286 /// mnemonics for the assembly writer.
287 /// @brief Convert attribute bits to text
288 std::string getAsString() const;
291 //===----------------------------------------------------------------------===//
292 // AttributeWithIndex
293 //===----------------------------------------------------------------------===//
295 /// AttributeWithIndex - This is just a pair of values to associate a set of
296 /// attributes with an index.
297 struct AttributeWithIndex {
298 Attributes Attrs; ///< The attributes that are set, or'd together.
299 unsigned Index; ///< Index of the parameter for which the attributes apply.
300 ///< Index 0 is used for return value attributes.
301 ///< Index ~0U is used for function attributes.
303 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
304 ArrayRef<Attributes::AttrVal> Attrs) {
305 Attributes::Builder B;
307 for (ArrayRef<Attributes::AttrVal>::iterator I = Attrs.begin(),
308 E = Attrs.end(); I != E; ++I)
311 AttributeWithIndex P;
313 P.Attrs = Attributes::get(C, B);
316 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
317 AttributeWithIndex P;
324 //===----------------------------------------------------------------------===//
325 // AttrListPtr Smart Pointer
326 //===----------------------------------------------------------------------===//
328 class AttributeListImpl;
330 /// AttrListPtr - This class manages the ref count for the opaque
331 /// AttributeListImpl object and provides accessors for it.
339 /// AttrList - The attributes that we are managing. This can be null
340 /// to represent the empty attributes list.
341 AttributeListImpl *AttrList;
343 AttrListPtr() : AttrList(0) {}
344 AttrListPtr(const AttrListPtr &P);
345 const AttrListPtr &operator=(const AttrListPtr &RHS);
348 //===--------------------------------------------------------------------===//
349 // Attribute List Construction and Mutation
350 //===--------------------------------------------------------------------===//
352 /// get - Return a Attributes list with the specified parameters in it.
353 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
355 /// addAttr - Add the specified attribute at the specified index to this
356 /// attribute list. Since attribute lists are immutable, this
357 /// returns the new list.
358 AttrListPtr addAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
360 /// removeAttr - Remove the specified attribute at the specified index from
361 /// this attribute list. Since attribute lists are immutable, this
362 /// returns the new list.
363 AttrListPtr removeAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
365 //===--------------------------------------------------------------------===//
366 // Attribute List Accessors
367 //===--------------------------------------------------------------------===//
368 /// getParamAttributes - The attributes for the specified index are
370 Attributes getParamAttributes(unsigned Idx) const {
371 return getAttributes(Idx);
374 /// getRetAttributes - The attributes for the ret value are
376 Attributes getRetAttributes() const {
377 return getAttributes(ReturnIndex);
380 /// getFnAttributes - The function attributes are returned.
381 Attributes getFnAttributes() const {
382 return getAttributes(FunctionIndex);
385 /// paramHasAttr - Return true if the specified parameter index has the
386 /// specified attribute set.
387 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
388 return getAttributes(Idx).hasAttributes(Attr);
391 /// getParamAlignment - Return the alignment for the specified function
393 unsigned getParamAlignment(unsigned Idx) const {
394 return getAttributes(Idx).getAlignment();
397 /// hasAttrSomewhere - Return true if the specified attribute is set for at
398 /// least one parameter or for the return value.
399 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
401 unsigned getNumAttrs() const;
402 Attributes &getAttributesAtIndex(unsigned i) const;
404 /// operator==/!= - Provide equality predicates.
405 bool operator==(const AttrListPtr &RHS) const
406 { return AttrList == RHS.AttrList; }
407 bool operator!=(const AttrListPtr &RHS) const
408 { return AttrList != RHS.AttrList; }
412 //===--------------------------------------------------------------------===//
413 // Attribute List Introspection
414 //===--------------------------------------------------------------------===//
416 /// getRawPointer - Return a raw pointer that uniquely identifies this
418 void *getRawPointer() const {
422 // Attributes are stored as a dense set of slots, where there is one
423 // slot for each argument that has an attribute. This allows walking over the
424 // dense set instead of walking the sparse list of attributes.
426 /// isEmpty - Return true if there are no attributes.
428 bool isEmpty() const {
429 return AttrList == 0;
432 /// getNumSlots - Return the number of slots used in this attribute list.
433 /// This is the number of arguments that have an attribute set on them
434 /// (including the function itself).
435 unsigned getNumSlots() const;
437 /// getSlot - Return the AttributeWithIndex at the specified slot. This
438 /// holds a index number plus a set of attributes.
439 const AttributeWithIndex &getSlot(unsigned Slot) const;
442 explicit AttrListPtr(AttributeListImpl *L);
444 /// getAttributes - The attributes for the specified index are
445 /// returned. Attributes for the result are denoted with Idx = 0.
446 Attributes getAttributes(unsigned Idx) const;
449 } // End llvm namespace