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 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the simple types necessary to represent the
12 /// attributes associated with functions and their calls.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_ATTRIBUTES_H
17 #define LLVM_IR_ATTRIBUTES_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/Support/MathExtras.h"
33 //===----------------------------------------------------------------------===//
35 /// \brief Functions, function parameters, and return types can have attributes
36 /// to indicate how they should be treated by optimizations and code
37 /// generation. This class represents one of those attributes. It's light-weight
38 /// and should be passed around by-value.
41 /// This enumeration lists the attributes that can be associated with
42 /// parameters, function results or the function itself.
44 /// Note: uwtable is about the ABI or the user mandating an entry in the
45 /// unwind table. The nounwind attribute is about an exception passing by the
48 /// In a theoretical system that uses tables for profiling and sjlj for
49 /// exceptions, they would be fully independent. In a normal system that uses
50 /// tables for both, the semantics are:
52 /// nil = Needs an entry because an exception might pass by.
53 /// nounwind = No need for an entry
54 /// uwtable = Needs an entry because the ABI says so and because
55 /// an exception might pass by.
56 /// uwtable + nounwind = Needs an entry because the ABI says so.
59 // IR-Level Attributes
60 None, ///< No attributes have been set
61 AddressSafety, ///< Address safety checking is on.
62 Alignment, ///< Alignment of parameter (5 bits)
63 ///< stored as log2 of alignment with +1 bias
64 ///< 0 means unaligned (different from align(1))
65 AlwaysInline, ///< inline=always
66 ByVal, ///< Pass structure by value
67 InlineHint, ///< Source said inlining was desirable
68 InReg, ///< Force argument to be passed in register
69 MinSize, ///< Function must be optimized for size first
70 Naked, ///< Naked function
71 Nest, ///< Nested function static chain
72 NoAlias, ///< Considered to not alias after call
73 NoCapture, ///< Function creates no aliases of pointer
74 NoDuplicate, ///< Call cannot be duplicated
75 NoImplicitFloat, ///< Disable implicit floating point insts
76 NoInline, ///< inline=never
77 NonLazyBind, ///< Function is called early and/or
78 ///< often, so lazy binding isn't worthwhile
79 NoRedZone, ///< Disable redzone
80 NoReturn, ///< Mark the function as not returning
81 NoUnwind, ///< Function doesn't unwind stack
82 OptimizeForSize, ///< opt_size
83 ReadNone, ///< Function does not access memory
84 ReadOnly, ///< Function only reads from memory
85 ReturnsTwice, ///< Function can return twice
86 SExt, ///< Sign extended before/after call
87 StackAlignment, ///< Alignment of stack for function (3 bits)
88 ///< stored as log2 of alignment with +1 bias 0
89 ///< means unaligned (different from
91 StackProtect, ///< Stack protection.
92 StackProtectReq, ///< Stack protection required.
93 StackProtectStrong, ///< Strong Stack protection.
94 StructRet, ///< Hidden pointer to structure to return
95 UWTable, ///< Function must be in a unwind table
96 ZExt, ///< Zero extended before/after call
98 EndAttrKinds, ///< Sentinal value useful for loops
100 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
101 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
104 AttributeImpl *pImpl;
105 Attribute(AttributeImpl *A) : pImpl(A) {}
107 Attribute() : pImpl(0) {}
109 /// \brief Return a uniquified Attribute object. This takes the uniquified
110 /// value from the Builder and wraps it in the Attribute class.
111 static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Vals);
112 static Attribute get(LLVMContext &Context, AttrBuilder &B);
114 /// \brief Return true if the attribute is present.
115 bool hasAttribute(AttrKind Val) const;
117 /// \brief Return true if attributes exist
118 bool hasAttributes() const;
120 /// \brief Returns the alignment field of an attribute as a byte alignment
122 unsigned getAlignment() const;
124 /// \brief Set the alignment field of an attribute.
125 void setAlignment(unsigned Align);
127 /// \brief Returns the stack alignment field of an attribute as a byte
129 unsigned getStackAlignment() const;
131 /// \brief Set the stack alignment field of an attribute.
132 void setStackAlignment(unsigned Align);
134 /// \brief Equality and non-equality query methods.
135 bool operator==(AttrKind K) const;
136 bool operator!=(AttrKind K) const;
138 // FIXME: Remove these 'operator' methods.
139 bool operator==(const Attribute &A) const {
140 return pImpl == A.pImpl;
142 bool operator!=(const Attribute &A) const {
143 return pImpl != A.pImpl;
146 uint64_t Raw() const;
148 /// \brief Which attributes cannot be applied to a type.
149 static Attribute typeIncompatible(Type *Ty);
151 /// \brief This returns an integer containing an encoding of all the LLVM
152 /// attributes found in the given attribute bitset. Any change to this
153 /// encoding is a breaking change to bitcode compatibility.
154 static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs);
156 /// \brief This returns an attribute bitset containing the LLVM attributes
157 /// that have been decoded from the given integer. This function must stay in
158 /// sync with 'encodeLLVMAttributesForBitcode'.
159 static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
160 uint64_t EncodedAttrs);
162 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
163 /// is, presumably, for writing out the mnemonics for the assembly writer.
164 std::string getAsString() const;
167 //===----------------------------------------------------------------------===//
169 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
171 template<> struct DenseMapInfo<Attribute::AttrKind> {
172 static inline Attribute::AttrKind getEmptyKey() {
173 return Attribute::AttrKindEmptyKey;
175 static inline Attribute::AttrKind getTombstoneKey() {
176 return Attribute::AttrKindTombstoneKey;
178 static unsigned getHashValue(const Attribute::AttrKind &Val) {
181 static bool isEqual(const Attribute::AttrKind &LHS,
182 const Attribute::AttrKind &RHS) {
187 //===----------------------------------------------------------------------===//
188 // AttributeSet Smart Pointer
189 //===----------------------------------------------------------------------===//
192 class AttributeSetImpl;
193 struct AttributeWithIndex;
195 //===----------------------------------------------------------------------===//
197 /// \brief This class manages the ref count for the opaque AttributeSetImpl
198 /// object and provides accessors for it.
206 friend class AttrBuilder;
208 /// \brief The attributes that we are managing. This can be null to represent
209 /// the empty attributes list.
210 AttributeSetImpl *AttrList;
212 /// \brief The attributes for the specified index are returned. Attributes
213 /// for the result are denoted with Idx = 0.
214 Attribute getAttributes(unsigned Idx) const;
216 /// \brief Add the specified attribute at the specified index to this
217 /// attribute list. Since attribute lists are immutable, this returns the new
219 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
221 /// \brief Remove the specified attribute at the specified index from this
222 /// attribute list. Since attribute lists are immutable, this returns the new
224 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
226 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
228 AttributeSet() : AttrList(0) {}
229 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
230 const AttributeSet &operator=(const AttributeSet &RHS);
232 //===--------------------------------------------------------------------===//
233 // Attribute List Construction and Mutation
234 //===--------------------------------------------------------------------===//
236 /// \brief Return an AttributeSet with the specified parameters in it.
237 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
238 static AttributeSet get(LLVMContext &C, unsigned Idx,
239 Attribute::AttrKind Kind);
240 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
242 /// \brief Add an attribute to the attribute set at the given index. Since
243 /// attribute sets are immutable, this returns a new set.
244 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
245 Attribute::AttrKind Attr) const;
247 /// \brief Add attributes to the attribute set at the given index. Since
248 /// attribute sets are immutable, this returns a new set.
249 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
250 AttributeSet Attrs) const;
252 /// \brief Add return attributes to this attribute set. Since attribute sets
253 /// are immutable, this returns a new set.
254 AttributeSet addRetAttributes(LLVMContext &C, AttributeSet Attrs) const {
255 return addAttributes(C, ReturnIndex, Attrs);
258 /// \brief Add function attributes to this attribute set. Since attribute sets
259 /// are immutable, this returns a new set.
260 AttributeSet addFnAttributes(LLVMContext &C, AttributeSet Attrs) const {
261 return addAttributes(C, FunctionIndex, Attrs);
264 /// \brief Remove the specified attribute at the specified index from this
265 /// attribute list. Since attribute lists are immutable, this returns the new
267 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
268 Attribute::AttrKind Attr) const;
270 /// \brief Remove the specified attributes at the specified index from this
271 /// attribute list. Since attribute lists are immutable, this returns the new
273 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
274 AttributeSet Attrs) const;
276 //===--------------------------------------------------------------------===//
277 // Attribute List Accessors
278 //===--------------------------------------------------------------------===//
280 /// \brief The attributes for the specified index are returned.
281 AttributeSet getParamAttributes(unsigned Idx) const;
283 /// \brief The attributes for the ret value are returned.
284 AttributeSet getRetAttributes() const;
286 /// \brief The function attributes are returned.
287 AttributeSet getFnAttributes() const;
289 /// \brief Return the alignment for the specified function parameter.
290 unsigned getParamAlignment(unsigned Idx) const;
292 /// \brief Return true if the attribute exists at the given index.
293 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
295 /// \brief Return true if attribute exists at the given index.
296 bool hasAttributes(unsigned Index) const;
298 /// \brief Get the stack alignment.
299 unsigned getStackAlignment(unsigned Index) const;
301 /// \brief Return the attributes at the index as a string.
302 std::string getAsString(unsigned Index) const;
304 uint64_t Raw(unsigned Index) const;
306 /// \brief Return true if the specified attribute is set for at least one
307 /// parameter or for the return value.
308 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
310 /// operator==/!= - Provide equality predicates.
311 bool operator==(const AttributeSet &RHS) const {
312 return AttrList == RHS.AttrList;
314 bool operator!=(const AttributeSet &RHS) const {
315 return AttrList != RHS.AttrList;
318 //===--------------------------------------------------------------------===//
319 // Attribute List Introspection
320 //===--------------------------------------------------------------------===//
322 /// \brief Return a raw pointer that uniquely identifies this attribute list.
323 void *getRawPointer() const {
327 // Attributes are stored as a dense set of slots, where there is one slot for
328 // each argument that has an attribute. This allows walking over the dense
329 // set instead of walking the sparse list of attributes.
331 /// \brief Return true if there are no attributes.
332 bool isEmpty() const {
333 return AttrList == 0;
336 /// \brief Return the number of slots used in this attribute list. This is
337 /// the number of arguments that have an attribute set on them (including the
338 /// function itself).
339 unsigned getNumSlots() const;
341 /// \brief Return the AttributeWithIndex at the specified slot. This holds a
342 /// index number plus a set of attributes.
343 const AttributeWithIndex &getSlot(unsigned Slot) const;
348 //===----------------------------------------------------------------------===//
350 /// \brief This is just a pair of values to associate a set of attributes with
352 struct AttributeWithIndex {
353 Attribute Attrs; ///< The attributes that are set, or'd together.
354 unsigned Index; ///< Index of the parameter for which the attributes apply.
356 // FIXME: These methods all need to be revised. The first one is temporary.
357 static AttributeWithIndex get(LLVMContext &C, unsigned Idx, AttributeSet AS);
358 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
359 ArrayRef<Attribute::AttrKind> Attrs) {
360 return get(Idx, Attribute::get(C, Attrs));
362 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
363 AttributeWithIndex P;
370 //===----------------------------------------------------------------------===//
372 /// \brief This class is used in conjunction with the Attribute::get method to
373 /// create an Attribute object. The object itself is uniquified. The Builder's
374 /// value, however, is not. So this can be used as a quick way to test for
375 /// equality, presence of attributes, etc.
377 DenseSet<Attribute::AttrKind> Attrs;
379 uint64_t StackAlignment;
381 AttrBuilder() : Alignment(0), StackAlignment(0) {}
382 explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
385 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
388 AttrBuilder(AttributeSet AS, unsigned Idx);
392 /// \brief Add an attribute to the builder.
393 AttrBuilder &addAttribute(Attribute::AttrKind Val);
395 /// \brief Remove an attribute from the builder.
396 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
398 /// \brief Add the attributes from A to the builder.
399 AttrBuilder &addAttributes(const Attribute &A);
401 /// \brief Remove the attributes from A from the builder.
402 AttrBuilder &removeAttributes(const Attribute &A);
404 /// \brief Return true if the builder has the specified attribute.
405 bool contains(Attribute::AttrKind A) const;
407 /// \brief Return true if the builder has IR-level attributes.
408 bool hasAttributes() const;
410 /// \brief Return true if the builder has any attribute that's in the
411 /// specified attribute.
412 bool hasAttributes(const Attribute &A) const;
414 /// \brief Return true if the builder has an alignment attribute.
415 bool hasAlignmentAttr() const;
417 /// \brief Retrieve the alignment attribute, if it exists.
418 uint64_t getAlignment() const { return Alignment; }
420 /// \brief Retrieve the stack alignment attribute, if it exists.
421 uint64_t getStackAlignment() const { return StackAlignment; }
423 /// \brief This turns an int alignment (which must be a power of 2) into the
424 /// form used internally in Attribute.
425 AttrBuilder &addAlignmentAttr(unsigned Align);
427 /// \brief This turns an int stack alignment (which must be a power of 2) into
428 /// the form used internally in Attribute.
429 AttrBuilder &addStackAlignmentAttr(unsigned Align);
431 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
432 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
434 iterator begin() { return Attrs.begin(); }
435 iterator end() { return Attrs.end(); }
437 const_iterator begin() const { return Attrs.begin(); }
438 const_iterator end() const { return Attrs.end(); }
440 /// \brief Add the raw value to the internal representation.
442 /// N.B. This should be used ONLY for decoding LLVM bitcode!
443 AttrBuilder &addRawValue(uint64_t Val);
445 /// \brief Remove attributes that are used on functions only.
446 void removeFunctionOnlyAttrs() {
447 removeAttribute(Attribute::NoReturn)
448 .removeAttribute(Attribute::NoUnwind)
449 .removeAttribute(Attribute::ReadNone)
450 .removeAttribute(Attribute::ReadOnly)
451 .removeAttribute(Attribute::NoInline)
452 .removeAttribute(Attribute::AlwaysInline)
453 .removeAttribute(Attribute::OptimizeForSize)
454 .removeAttribute(Attribute::StackProtect)
455 .removeAttribute(Attribute::StackProtectReq)
456 .removeAttribute(Attribute::StackProtectStrong)
457 .removeAttribute(Attribute::NoRedZone)
458 .removeAttribute(Attribute::NoImplicitFloat)
459 .removeAttribute(Attribute::Naked)
460 .removeAttribute(Attribute::InlineHint)
461 .removeAttribute(Attribute::StackAlignment)
462 .removeAttribute(Attribute::UWTable)
463 .removeAttribute(Attribute::NonLazyBind)
464 .removeAttribute(Attribute::ReturnsTwice)
465 .removeAttribute(Attribute::AddressSafety)
466 .removeAttribute(Attribute::MinSize)
467 .removeAttribute(Attribute::NoDuplicate);
470 uint64_t Raw() const;
472 bool operator==(const AttrBuilder &B);
473 bool operator!=(const AttrBuilder &B) {
474 return !(*this == B);
478 } // end llvm namespace