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/ADT/ArrayRef.h"
19 #include "llvm/Support/MathExtras.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) {}
96 /// get - Return a uniquified Attributes object. This takes the uniquified
97 /// value from the Builder and wraps it in the Attributes class.
98 static Attributes get(LLVMContext &Context, ArrayRef<AttrVal> Vals);
99 static Attributes get(LLVMContext &Context, AttrBuilder &B);
101 /// @brief Return true if the attribute is present.
102 bool hasAttribute(AttrVal Val) const;
104 /// @brief Return true if attributes exist
105 bool hasAttributes() const;
107 /// @brief Return true if the attributes are a non-null intersection.
108 bool hasAttributes(const Attributes &A) const;
110 /// @brief Returns the alignment field of an attribute as a byte alignment
112 unsigned getAlignment() const;
114 /// @brief Returns the stack alignment field of an attribute as a byte
116 unsigned getStackAlignment() const;
118 /// @brief Parameter attributes that do not apply to vararg call arguments.
119 bool hasIncompatibleWithVarArgsAttrs() const {
120 return hasAttribute(Attributes::StructRet);
123 /// @brief Attributes that only apply to function parameters.
124 bool hasParameterOnlyAttrs() const {
125 return hasAttribute(Attributes::ByVal) ||
126 hasAttribute(Attributes::Nest) ||
127 hasAttribute(Attributes::StructRet) ||
128 hasAttribute(Attributes::NoCapture);
131 /// @brief Attributes that may be applied to the function itself. These cannot
132 /// be used on return values or function parameters.
133 bool hasFunctionOnlyAttrs() const {
134 return hasAttribute(Attributes::NoReturn) ||
135 hasAttribute(Attributes::NoUnwind) ||
136 hasAttribute(Attributes::ReadNone) ||
137 hasAttribute(Attributes::ReadOnly) ||
138 hasAttribute(Attributes::NoInline) ||
139 hasAttribute(Attributes::AlwaysInline) ||
140 hasAttribute(Attributes::OptimizeForSize) ||
141 hasAttribute(Attributes::StackProtect) ||
142 hasAttribute(Attributes::StackProtectReq) ||
143 hasAttribute(Attributes::NoRedZone) ||
144 hasAttribute(Attributes::NoImplicitFloat) ||
145 hasAttribute(Attributes::Naked) ||
146 hasAttribute(Attributes::InlineHint) ||
147 hasAttribute(Attributes::StackAlignment) ||
148 hasAttribute(Attributes::UWTable) ||
149 hasAttribute(Attributes::NonLazyBind) ||
150 hasAttribute(Attributes::ReturnsTwice) ||
151 hasAttribute(Attributes::AddressSafety) ||
152 hasAttribute(Attributes::MinSize);
155 bool operator==(const Attributes &A) const {
156 return Attrs == A.Attrs;
158 bool operator!=(const Attributes &A) const {
159 return Attrs != A.Attrs;
162 uint64_t Raw() const;
164 /// @brief Which attributes cannot be applied to a type.
165 static Attributes typeIncompatible(Type *Ty);
167 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
168 /// encoding of all the LLVM attributes found in the given attribute bitset.
169 /// Any change to this encoding is a breaking change to bitcode compatibility.
170 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs);
172 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
173 /// containing the LLVM attributes that have been decoded from the given
174 /// integer. This function must stay in sync with
175 /// 'encodeLLVMAttributesForBitcode'.
176 static Attributes decodeLLVMAttributesForBitcode(LLVMContext &C,
177 uint64_t EncodedAttrs);
179 /// getAsString - The set of Attributes set in Attributes is converted to a
180 /// string of equivalent mnemonics. This is, presumably, for writing out the
181 /// mnemonics for the assembly writer.
182 /// @brief Convert attribute bits to text
183 std::string getAsString() const;
186 //===----------------------------------------------------------------------===//
187 /// AttrBuilder - This class is used in conjunction with the Attributes::get
188 /// method to create an Attributes object. The object itself is uniquified. The
189 /// Builder's value, however, is not. So this can be used as a quick way to test
190 /// for equality, presence of attributes, etc.
194 AttrBuilder() : Bits(0) {}
195 explicit AttrBuilder(uint64_t B) : Bits(B) {}
196 AttrBuilder(const Attributes &A) : Bits(A.Raw()) {}
198 void clear() { Bits = 0; }
200 /// addAttribute - Add an attribute to the builder.
201 AttrBuilder &addAttribute(Attributes::AttrVal Val);
203 /// removeAttribute - Remove an attribute from the builder.
204 AttrBuilder &removeAttribute(Attributes::AttrVal Val);
206 /// addAttribute - Add the attributes from A to the builder.
207 AttrBuilder &addAttributes(const Attributes &A);
209 /// removeAttribute - Remove the attributes from A from the builder.
210 AttrBuilder &removeAttributes(const Attributes &A);
212 /// hasAttribute - Return true if the builder has the specified attribute.
213 bool hasAttribute(Attributes::AttrVal A) const;
215 /// hasAttributes - Return true if the builder has IR-level attributes.
216 bool hasAttributes() const;
218 /// hasAttributes - Return true if the builder has any attribute that's in the
219 /// specified attribute.
220 bool hasAttributes(const Attributes &A) const;
222 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
223 bool hasAlignmentAttr() const;
225 /// getAlignment - Retrieve the alignment attribute, if it exists.
226 uint64_t getAlignment() const;
228 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
229 uint64_t getStackAlignment() const;
231 /// addAlignmentAttr - This turns an int alignment (which must be a power of
232 /// 2) into the form used internally in Attributes.
233 AttrBuilder &addAlignmentAttr(unsigned Align);
235 /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
236 /// power of 2) into the form used internally in Attributes.
237 AttrBuilder &addStackAlignmentAttr(unsigned Align);
239 /// addRawValue - Add the raw value to the internal representation.
240 /// N.B. This should be used ONLY for decoding LLVM bitcode!
241 AttrBuilder &addRawValue(uint64_t Val);
243 /// @brief Remove attributes that are used on functions only.
244 void removeFunctionOnlyAttrs() {
245 removeAttribute(Attributes::NoReturn)
246 .removeAttribute(Attributes::NoUnwind)
247 .removeAttribute(Attributes::ReadNone)
248 .removeAttribute(Attributes::ReadOnly)
249 .removeAttribute(Attributes::NoInline)
250 .removeAttribute(Attributes::AlwaysInline)
251 .removeAttribute(Attributes::OptimizeForSize)
252 .removeAttribute(Attributes::StackProtect)
253 .removeAttribute(Attributes::StackProtectReq)
254 .removeAttribute(Attributes::NoRedZone)
255 .removeAttribute(Attributes::NoImplicitFloat)
256 .removeAttribute(Attributes::Naked)
257 .removeAttribute(Attributes::InlineHint)
258 .removeAttribute(Attributes::StackAlignment)
259 .removeAttribute(Attributes::UWTable)
260 .removeAttribute(Attributes::NonLazyBind)
261 .removeAttribute(Attributes::ReturnsTwice)
262 .removeAttribute(Attributes::AddressSafety)
263 .removeAttribute(Attributes::MinSize);
266 uint64_t Raw() const { return Bits; }
268 bool operator==(const AttrBuilder &B) {
269 return Bits == B.Bits;
271 bool operator!=(const AttrBuilder &B) {
272 return Bits != B.Bits;
276 //===----------------------------------------------------------------------===//
277 // AttributeWithIndex
278 //===----------------------------------------------------------------------===//
280 /// AttributeWithIndex - This is just a pair of values to associate a set of
281 /// attributes with an index.
282 struct AttributeWithIndex {
283 Attributes Attrs; ///< The attributes that are set, or'd together.
284 unsigned Index; ///< Index of the parameter for which the attributes apply.
285 ///< Index 0 is used for return value attributes.
286 ///< Index ~0U is used for function attributes.
288 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
289 ArrayRef<Attributes::AttrVal> Attrs) {
290 return get(Idx, Attributes::get(C, Attrs));
292 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
293 AttributeWithIndex P;
300 //===----------------------------------------------------------------------===//
301 // AttributeSet Smart Pointer
302 //===----------------------------------------------------------------------===//
304 class AttributeListImpl;
306 /// AttributeSet - This class manages the ref count for the opaque
307 /// AttributeListImpl object and provides accessors for it.
315 /// @brief The attributes that we are managing. This can be null to represent
316 /// the empty attributes list.
317 AttributeListImpl *AttrList;
319 /// @brief The attributes for the specified index are returned. Attributes
320 /// for the result are denoted with Idx = 0.
321 Attributes getAttributes(unsigned Idx) const;
323 explicit AttributeSet(AttributeListImpl *LI) : AttrList(LI) {}
325 AttributeSet() : AttrList(0) {}
326 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
327 const AttributeSet &operator=(const AttributeSet &RHS);
329 //===--------------------------------------------------------------------===//
330 // Attribute List Construction and Mutation
331 //===--------------------------------------------------------------------===//
333 /// get - Return a Attributes list with the specified parameters in it.
334 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
336 /// addAttr - Add the specified attribute at the specified index to this
337 /// attribute list. Since attribute lists are immutable, this
338 /// returns the new list.
339 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
341 /// removeAttr - Remove the specified attribute at the specified index from
342 /// this attribute list. Since attribute lists are immutable, this
343 /// returns the new list.
344 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attributes Attrs) const;
346 //===--------------------------------------------------------------------===//
347 // Attribute List Accessors
348 //===--------------------------------------------------------------------===//
349 /// getParamAttributes - The attributes for the specified index are
351 Attributes getParamAttributes(unsigned Idx) const {
352 return getAttributes(Idx);
355 /// getRetAttributes - The attributes for the ret value are
357 Attributes getRetAttributes() const {
358 return getAttributes(ReturnIndex);
361 /// getFnAttributes - The function attributes are returned.
362 Attributes getFnAttributes() const {
363 return getAttributes(FunctionIndex);
366 /// paramHasAttr - Return true if the specified parameter index has the
367 /// specified attribute set.
368 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
369 return getAttributes(Idx).hasAttributes(Attr);
372 /// getParamAlignment - Return the alignment for the specified function
374 unsigned getParamAlignment(unsigned Idx) const {
375 return getAttributes(Idx).getAlignment();
378 /// hasAttrSomewhere - Return true if the specified attribute is set for at
379 /// least one parameter or for the return value.
380 bool hasAttrSomewhere(Attributes::AttrVal Attr) const;
382 unsigned getNumAttrs() const;
383 Attributes &getAttributesAtIndex(unsigned i) const;
385 /// operator==/!= - Provide equality predicates.
386 bool operator==(const AttributeSet &RHS) const
387 { return AttrList == RHS.AttrList; }
388 bool operator!=(const AttributeSet &RHS) const
389 { return AttrList != RHS.AttrList; }
391 //===--------------------------------------------------------------------===//
392 // Attribute List Introspection
393 //===--------------------------------------------------------------------===//
395 /// getRawPointer - Return a raw pointer that uniquely identifies this
397 void *getRawPointer() const {
401 // Attributes are stored as a dense set of slots, where there is one
402 // slot for each argument that has an attribute. This allows walking over the
403 // dense set instead of walking the sparse list of attributes.
405 /// isEmpty - Return true if there are no attributes.
407 bool isEmpty() const {
408 return AttrList == 0;
411 /// getNumSlots - Return the number of slots used in this attribute list.
412 /// This is the number of arguments that have an attribute set on them
413 /// (including the function itself).
414 unsigned getNumSlots() const;
416 /// getSlot - Return the AttributeWithIndex at the specified slot. This
417 /// holds a index number plus a set of attributes.
418 const AttributeWithIndex &getSlot(unsigned Slot) const;
423 } // End llvm namespace