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"
30 /// Attributes - A bitset of attributes.
33 /// Function parameters and results can have attributes to indicate how they
34 /// should be treated by optimizations and code generation. This enumeration
35 /// lists the attributes that can be associated with parameters, function
36 /// results or the function itself.
38 /// Note that uwtable is about the ABI or the user mandating an entry in the
39 /// unwind table. The nounwind attribute is about an exception passing by the
42 /// In a theoretical system that uses tables for profiling and sjlj for
43 /// exceptions, they would be fully independent. In a normal system that uses
44 /// tables for both, the semantics are:
46 /// nil = Needs an entry because an exception might pass by.
47 /// nounwind = No need for an entry
48 /// uwtable = Needs an entry because the ABI says so and because
49 /// an exception might pass by.
50 /// uwtable + nounwind = Needs an entry because the ABI says so.
53 // IR-Level Attributes
54 None, ///< No attributes have been set
55 AddressSafety, ///< Address safety checking is on.
56 Alignment, ///< Alignment of parameter (5 bits)
57 ///< stored as log2 of alignment with +1 bias
58 ///< 0 means unaligned different from align 1
59 AlwaysInline, ///< inline=always
60 ByVal, ///< Pass structure by value
61 InlineHint, ///< Source said inlining was desirable
62 InReg, ///< Force argument to be passed in register
63 MinSize, ///< Function must be optimized for size first
64 Naked, ///< Naked function
65 Nest, ///< Nested function static chain
66 NoAlias, ///< Considered to not alias after call
67 NoCapture, ///< Function creates no aliases of pointer
68 NoImplicitFloat, ///< Disable implicit floating point insts
69 NoInline, ///< inline=never
70 NonLazyBind, ///< Function is called early and/or
71 ///< often, so lazy binding isn't worthwhile
72 NoRedZone, ///< Disable redzone
73 NoReturn, ///< Mark the function as not returning
74 NoUnwind, ///< Function doesn't unwind stack
75 OptimizeForSize, ///< opt_size
76 ReadNone, ///< Function does not access memory
77 ReadOnly, ///< Function only reads from memory
78 ReturnsTwice, ///< Function can return twice
79 SExt, ///< Sign extended before/after call
80 StackAlignment, ///< Alignment of stack for function (3 bits)
81 ///< stored as log2 of alignment with +1 bias 0
82 ///< means unaligned (different from
84 StackProtect, ///< Stack protection.
85 StackProtectReq, ///< Stack protection required.
86 StructRet, ///< Hidden pointer to structure to return
87 UWTable, ///< Function must be in a unwind table
88 ZExt ///< Zero extended before/after call
91 AttributesImpl *Attrs;
92 Attributes(AttributesImpl *A) : Attrs(A) {}
94 Attributes() : Attrs(0) {}
95 Attributes(const Attributes &A) : Attrs(A.Attrs) {}
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.
103 static Attributes get(LLVMContext &Context, ArrayRef<AttrVal> Vals);
104 static Attributes get(LLVMContext &Context, AttrBuilder &B);
106 /// @brief Return true if the attribute is present.
107 bool hasAttribute(AttrVal Val) const;
109 /// @brief Return true if attributes exist
110 bool hasAttributes() const;
112 /// @brief Return true if the attributes are a non-null intersection.
113 bool hasAttributes(const Attributes &A) const;
115 /// @brief Returns the alignment field of an attribute as a byte alignment
117 unsigned getAlignment() const;
119 /// @brief Returns the stack alignment field of an attribute as a byte
121 unsigned getStackAlignment() const;
123 /// @brief Parameter attributes that do not apply to vararg call arguments.
124 bool hasIncompatibleWithVarArgsAttrs() const {
125 return hasAttribute(Attributes::StructRet);
128 /// @brief Attributes that only apply to function parameters.
129 bool hasParameterOnlyAttrs() const {
130 return hasAttribute(Attributes::ByVal) ||
131 hasAttribute(Attributes::Nest) ||
132 hasAttribute(Attributes::StructRet) ||
133 hasAttribute(Attributes::NoCapture);
136 /// @brief Attributes that may be applied to the function itself. These cannot
137 /// be used on return values or function parameters.
138 bool hasFunctionOnlyAttrs() const {
139 return hasAttribute(Attributes::NoReturn) ||
140 hasAttribute(Attributes::NoUnwind) ||
141 hasAttribute(Attributes::ReadNone) ||
142 hasAttribute(Attributes::ReadOnly) ||
143 hasAttribute(Attributes::NoInline) ||
144 hasAttribute(Attributes::AlwaysInline) ||
145 hasAttribute(Attributes::OptimizeForSize) ||
146 hasAttribute(Attributes::StackProtect) ||
147 hasAttribute(Attributes::StackProtectReq) ||
148 hasAttribute(Attributes::NoRedZone) ||
149 hasAttribute(Attributes::NoImplicitFloat) ||
150 hasAttribute(Attributes::Naked) ||
151 hasAttribute(Attributes::InlineHint) ||
152 hasAttribute(Attributes::StackAlignment) ||
153 hasAttribute(Attributes::UWTable) ||
154 hasAttribute(Attributes::NonLazyBind) ||
155 hasAttribute(Attributes::ReturnsTwice) ||
156 hasAttribute(Attributes::AddressSafety) ||
157 hasAttribute(Attributes::MinSize);
160 bool operator==(const Attributes &A) const {
161 return Attrs == A.Attrs;
163 bool operator!=(const Attributes &A) const {
164 return Attrs != A.Attrs;
167 uint64_t Raw() const;
169 /// @brief Which attributes cannot be applied to a type.
170 static Attributes typeIncompatible(Type *Ty);
172 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
173 /// encoding of all the LLVM attributes found in the given attribute bitset.
174 /// Any change to this encoding is a breaking change to bitcode compatibility.
175 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs);
177 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
178 /// containing the LLVM attributes that have been decoded from the given
179 /// integer. This function must stay in sync with
180 /// 'encodeLLVMAttributesForBitcode'.
181 static Attributes decodeLLVMAttributesForBitcode(LLVMContext &C,
182 uint64_t EncodedAttrs);
184 /// getAsString - The set of Attributes set in Attributes is converted to a
185 /// string of equivalent mnemonics. This is, presumably, for writing out the
186 /// mnemonics for the assembly writer.
187 /// @brief Convert attribute bits to text
188 std::string getAsString() const;
191 //===----------------------------------------------------------------------===//
192 /// AttrBuilder - This class is used in conjunction with the Attributes::get
193 /// method to create an Attributes object. The object itself is uniquified. The
194 /// Builder's value, however, is not. So this can be used as a quick way to test
195 /// for equality, presence of attributes, etc.
199 AttrBuilder() : Bits(0) {}
200 explicit AttrBuilder(uint64_t B) : Bits(B) {}
201 AttrBuilder(const Attributes &A) : Bits(A.Raw()) {}
202 AttrBuilder(const AttrBuilder &B) : Bits(B.Bits) {}
204 void clear() { Bits = 0; }
206 /// addAttribute - Add an attribute to the builder.
207 AttrBuilder &addAttribute(Attributes::AttrVal Val);
209 /// removeAttribute - Remove an attribute from the builder.
210 AttrBuilder &removeAttribute(Attributes::AttrVal Val);
212 /// addAttribute - Add the attributes from A to the builder.
213 AttrBuilder &addAttributes(const Attributes &A);
215 /// removeAttribute - Remove the attributes from A from the builder.
216 AttrBuilder &removeAttributes(const Attributes &A);
218 /// hasAttribute - Return true if the builder has the specified attribute.
219 bool hasAttribute(Attributes::AttrVal A) const;
221 /// hasAttributes - Return true if the builder has IR-level attributes.
222 bool hasAttributes() const;
224 /// hasAttributes - Return true if the builder has any attribute that's in the
225 /// specified attribute.
226 bool hasAttributes(const Attributes &A) const;
228 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
229 bool hasAlignmentAttr() const;
231 /// getAlignment - Retrieve the alignment attribute, if it exists.
232 uint64_t getAlignment() const;
234 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
235 uint64_t getStackAlignment() const;
237 /// addAlignmentAttr - This turns an int alignment (which must be a power of
238 /// 2) into the form used internally in Attributes.
239 AttrBuilder &addAlignmentAttr(unsigned Align);
241 /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
242 /// power of 2) into the form used internally in Attributes.
243 AttrBuilder &addStackAlignmentAttr(unsigned Align);
245 /// addRawValue - Add the raw value to the internal representation.
246 /// N.B. This should be used ONLY for decoding LLVM bitcode!
247 AttrBuilder &addRawValue(uint64_t Val);
249 /// @brief Remove attributes that are used on functions only.
250 void removeFunctionOnlyAttrs() {
251 removeAttribute(Attributes::NoReturn)
252 .removeAttribute(Attributes::NoUnwind)
253 .removeAttribute(Attributes::ReadNone)
254 .removeAttribute(Attributes::ReadOnly)
255 .removeAttribute(Attributes::NoInline)
256 .removeAttribute(Attributes::AlwaysInline)
257 .removeAttribute(Attributes::OptimizeForSize)
258 .removeAttribute(Attributes::StackProtect)
259 .removeAttribute(Attributes::StackProtectReq)
260 .removeAttribute(Attributes::NoRedZone)
261 .removeAttribute(Attributes::NoImplicitFloat)
262 .removeAttribute(Attributes::Naked)
263 .removeAttribute(Attributes::InlineHint)
264 .removeAttribute(Attributes::StackAlignment)
265 .removeAttribute(Attributes::UWTable)
266 .removeAttribute(Attributes::NonLazyBind)
267 .removeAttribute(Attributes::ReturnsTwice)
268 .removeAttribute(Attributes::AddressSafety)
269 .removeAttribute(Attributes::MinSize);
272 uint64_t Raw() const { return Bits; }
274 bool operator==(const AttrBuilder &B) {
275 return Bits == B.Bits;
277 bool operator!=(const AttrBuilder &B) {
278 return Bits != B.Bits;
282 //===----------------------------------------------------------------------===//
283 // AttributeWithIndex
284 //===----------------------------------------------------------------------===//
286 /// AttributeWithIndex - This is just a pair of values to associate a set of
287 /// attributes with an index.
288 struct AttributeWithIndex {
289 Attributes Attrs; ///< The attributes that are set, or'd together.
290 unsigned Index; ///< Index of the parameter for which the attributes apply.
291 ///< Index 0 is used for return value attributes.
292 ///< Index ~0U is used for function attributes.
294 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
295 ArrayRef<Attributes::AttrVal> Attrs) {
296 return get(Idx, Attributes::get(C, Attrs));
298 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
299 AttributeWithIndex P;
306 //===----------------------------------------------------------------------===//
307 // AttrListPtr Smart Pointer
308 //===----------------------------------------------------------------------===//
310 class AttributeListImpl;
312 /// AttrListPtr - This class manages the ref count for the opaque
313 /// AttributeListImpl object and provides accessors for it.
321 /// AttrList - The attributes that we are managing. This can be null to
322 /// represent the empty attributes list.
323 AttributeListImpl *AttrList;
325 AttrListPtr() : AttrList(0) {}
326 AttrListPtr(const AttrListPtr &P);
327 const AttrListPtr &operator=(const AttrListPtr &RHS);
330 //===--------------------------------------------------------------------===//
331 // Attribute List Construction and Mutation
332 //===--------------------------------------------------------------------===//
334 /// get - Return a Attributes list with the specified parameters in it.
335 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
337 /// addAttr - Add the specified attribute at the specified index to this
338 /// attribute list. Since attribute lists are immutable, this
339 /// returns the new list.
340 AttrListPtr addAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
342 /// removeAttr - Remove the specified attribute at the specified index from
343 /// this attribute list. Since attribute lists are immutable, this
344 /// returns the new list.
345 AttrListPtr removeAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
347 //===--------------------------------------------------------------------===//
348 // Attribute List Accessors
349 //===--------------------------------------------------------------------===//
350 /// getParamAttributes - The attributes for the specified index are
352 Attributes getParamAttributes(unsigned Idx) const {
353 return getAttributes(Idx);
356 /// getRetAttributes - The attributes for the ret value are
358 Attributes getRetAttributes() const {
359 return getAttributes(ReturnIndex);
362 /// getFnAttributes - The function attributes are returned.
363 Attributes getFnAttributes() const {
364 return getAttributes(FunctionIndex);
367 /// paramHasAttr - Return true if the specified parameter index has the
368 /// specified attribute set.
369 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
370 return getAttributes(Idx).hasAttributes(Attr);
373 /// getParamAlignment - Return the alignment for the specified function
375 unsigned getParamAlignment(unsigned Idx) const {
376 return getAttributes(Idx).getAlignment();
379 /// hasAttrSomewhere - Return true if the specified attribute is set for at
380 /// least one parameter or for the return value.
381 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
383 unsigned getNumAttrs() const;
384 Attributes &getAttributesAtIndex(unsigned i) const;
386 /// operator==/!= - Provide equality predicates.
387 bool operator==(const AttrListPtr &RHS) const
388 { return AttrList == RHS.AttrList; }
389 bool operator!=(const AttrListPtr &RHS) const
390 { return AttrList != RHS.AttrList; }
392 //===--------------------------------------------------------------------===//
393 // Attribute List Introspection
394 //===--------------------------------------------------------------------===//
396 /// getRawPointer - Return a raw pointer that uniquely identifies this
398 void *getRawPointer() const {
402 // Attributes are stored as a dense set of slots, where there is one
403 // slot for each argument that has an attribute. This allows walking over the
404 // dense set instead of walking the sparse list of attributes.
406 /// isEmpty - Return true if there are no attributes.
408 bool isEmpty() const {
409 return AttrList == 0;
412 /// getNumSlots - Return the number of slots used in this attribute list.
413 /// This is the number of arguments that have an attribute set on them
414 /// (including the function itself).
415 unsigned getNumSlots() const;
417 /// getSlot - Return the AttributeWithIndex at the specified slot. This
418 /// holds a index number plus a set of attributes.
419 const AttributeWithIndex &getSlot(unsigned Slot) const;
424 explicit AttrListPtr(AttributeListImpl *L);
426 /// getAttributes - The attributes for the specified index are
427 /// returned. Attributes for the result are denoted with Idx = 0.
428 Attributes getAttributes(unsigned Idx) const;
431 } // End llvm namespace