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/ADT/FoldingSet.h"
22 #include "llvm/Support/MathExtras.h"
34 //===----------------------------------------------------------------------===//
36 /// \brief Functions, function parameters, and return types can have attributes
37 /// to indicate how they should be treated by optimizations and code
38 /// generation. This class represents one of those attributes. It's light-weight
39 /// and should be passed around by-value.
42 /// This enumeration lists the attributes that can be associated with
43 /// parameters, function results or the function itself.
45 /// Note: uwtable is about the ABI or the user mandating an entry in the
46 /// unwind table. The nounwind attribute is about an exception passing by the
49 /// In a theoretical system that uses tables for profiling and sjlj for
50 /// exceptions, they would be fully independent. In a normal system that uses
51 /// tables for both, the semantics are:
53 /// nil = Needs an entry because an exception might pass by.
54 /// nounwind = No need for an entry
55 /// uwtable = Needs an entry because the ABI says so and because
56 /// an exception might pass by.
57 /// uwtable + nounwind = Needs an entry because the ABI says so.
60 // IR-Level Attributes
61 None, ///< No attributes have been set
62 AddressSafety, ///< Address safety checking is on.
63 Alignment, ///< Alignment of parameter (5 bits)
64 ///< stored as log2 of alignment with +1 bias
65 ///< 0 means unaligned (different from align(1))
66 AlwaysInline, ///< inline=always
67 ByVal, ///< Pass structure by value
68 InlineHint, ///< Source said inlining was desirable
69 InReg, ///< Force argument to be passed in register
70 MinSize, ///< Function must be optimized for size first
71 Naked, ///< Naked function
72 Nest, ///< Nested function static chain
73 NoAlias, ///< Considered to not alias after call
74 NoCapture, ///< Function creates no aliases of pointer
75 NoDuplicate, ///< Call cannot be duplicated
76 NoImplicitFloat, ///< Disable implicit floating point insts
77 NoInline, ///< inline=never
78 NonLazyBind, ///< Function is called early and/or
79 ///< often, so lazy binding isn't worthwhile
80 NoRedZone, ///< Disable redzone
81 NoReturn, ///< Mark the function as not returning
82 NoUnwind, ///< Function doesn't unwind stack
83 OptimizeForSize, ///< opt_size
84 ReadNone, ///< Function does not access memory
85 ReadOnly, ///< Function only reads from memory
86 ReturnsTwice, ///< Function can return twice
87 SExt, ///< Sign extended before/after call
88 StackAlignment, ///< Alignment of stack for function (3 bits)
89 ///< stored as log2 of alignment with +1 bias 0
90 ///< means unaligned (different from
92 StackProtect, ///< Stack protection.
93 StackProtectReq, ///< Stack protection required.
94 StackProtectStrong, ///< Strong Stack protection.
95 StructRet, ///< Hidden pointer to structure to return
96 UWTable, ///< Function must be in a unwind table
97 ZExt, ///< Zero extended before/after call
99 EndAttrKinds, ///< Sentinal value useful for loops
101 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
102 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
105 AttributeImpl *pImpl;
106 Attribute(AttributeImpl *A) : pImpl(A) {}
108 Attribute() : pImpl(0) {}
110 /// \brief Return a uniquified Attribute object.
111 static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Kinds);
112 static Attribute get(LLVMContext &Context, AttrBuilder &B);
114 /// \brief Return a uniquified Attribute object that has the specific
116 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
117 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
119 /// \brief Return true if the attribute is present.
120 bool hasAttribute(AttrKind Val) const;
122 /// \brief Return true if attributes exist
123 bool hasAttributes() const;
125 /// \brief Returns the alignment field of an attribute as a byte alignment
127 unsigned getAlignment() const;
129 /// \brief Returns the stack alignment field of an attribute as a byte
131 unsigned getStackAlignment() const;
133 /// \brief Equality and non-equality query methods.
134 bool operator==(AttrKind K) const;
135 bool operator!=(AttrKind K) const;
137 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
138 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
140 /// \brief Less-than operator. Useful for sorting the attributes list.
141 bool operator<(Attribute A) const;
143 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
144 /// is, presumably, for writing out the mnemonics for the assembly writer.
145 std::string getAsString() const;
147 void Profile(FoldingSetNodeID &ID) const {
148 ID.AddPointer(pImpl);
151 uint64_t Raw() const;
154 //===----------------------------------------------------------------------===//
156 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
158 template<> struct DenseMapInfo<Attribute::AttrKind> {
159 static inline Attribute::AttrKind getEmptyKey() {
160 return Attribute::AttrKindEmptyKey;
162 static inline Attribute::AttrKind getTombstoneKey() {
163 return Attribute::AttrKindTombstoneKey;
165 static unsigned getHashValue(const Attribute::AttrKind &Val) {
168 static bool isEqual(const Attribute::AttrKind &LHS,
169 const Attribute::AttrKind &RHS) {
174 //===----------------------------------------------------------------------===//
175 // AttributeSet Smart Pointer
176 //===----------------------------------------------------------------------===//
179 class AttributeSetImpl;
181 //===----------------------------------------------------------------------===//
183 /// \brief This is just a pair of values to associate a set of attributes with
185 struct AttributeWithIndex {
186 Attribute Attrs; ///< The attributes that are set, or'd together.
187 unsigned Index; ///< Index of the parameter for which the attributes apply.
189 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
190 AttributeWithIndex P;
197 //===----------------------------------------------------------------------===//
199 /// \brief This class manages the ref count for the opaque AttributeSetImpl
200 /// object and provides accessors for it.
208 friend class AttrBuilder;
209 friend class AttributeSetImpl;
211 /// \brief The attributes that we are managing. This can be null to represent
212 /// the empty attributes list.
213 AttributeSetImpl *pImpl;
215 /// \brief The attributes for the specified index are returned. Attributes
216 /// for the result are denoted with Idx = 0.
217 Attribute getAttributes(unsigned Idx) const;
219 /// \brief Add the specified attribute at the specified index to this
220 /// attribute list. Since attribute lists are immutable, this returns the new
222 AttributeSet addAttr(LLVMContext &C, unsigned Idx, AttributeSet Attrs) const;
224 /// \brief Remove the specified attribute at the specified index from this
225 /// attribute list. Since attribute lists are immutable, this returns the new
227 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
229 /// \brief Create an AttributeSet from the AttributeWithIndex structures.
230 /// N.B. this is only temporary. It will be disappearing in the future.
231 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
233 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
235 AttributeSet() : pImpl(0) {}
236 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
237 const AttributeSet &operator=(const AttributeSet &RHS) {
242 //===--------------------------------------------------------------------===//
243 // Attribute List Construction and Mutation
244 //===--------------------------------------------------------------------===//
246 /// \brief Return an AttributeSet with the specified parameters in it.
247 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
248 static AttributeSet get(LLVMContext &C, unsigned Idx,
249 ArrayRef<Attribute::AttrKind> Kind);
250 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
252 /// \brief Add an attribute to the attribute set at the given index. Since
253 /// attribute sets are immutable, this returns a new set.
254 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
255 Attribute::AttrKind Attr) const;
257 /// \brief Add attributes to the attribute set at the given index. Since
258 /// attribute sets are immutable, this returns a new set.
259 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
260 AttributeSet Attrs) const;
262 /// \brief Add return attributes to this attribute set. Since attribute sets
263 /// are immutable, this returns a new set.
264 AttributeSet addRetAttributes(LLVMContext &C, AttributeSet Attrs) const {
265 return addAttributes(C, ReturnIndex, Attrs);
268 /// \brief Add function attributes to this attribute set. Since attribute sets
269 /// are immutable, this returns a new set.
270 AttributeSet addFnAttributes(LLVMContext &C, AttributeSet Attrs) const {
271 return addAttributes(C, FunctionIndex, Attrs);
274 /// \brief Remove the specified attribute at the specified index from this
275 /// attribute list. Since attribute lists are immutable, this returns the new
277 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
278 Attribute::AttrKind Attr) const;
280 /// \brief Remove the specified attributes at the specified index from this
281 /// attribute list. Since attribute lists are immutable, this returns the new
283 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
284 AttributeSet Attrs) const;
286 //===--------------------------------------------------------------------===//
287 // Attribute Set Accessors
288 //===--------------------------------------------------------------------===//
290 /// \brief The attributes for the specified index are returned.
291 AttributeSet getParamAttributes(unsigned Idx) const;
293 /// \brief The attributes for the ret value are returned.
294 AttributeSet getRetAttributes() const;
296 /// \brief The function attributes are returned.
297 AttributeSet getFnAttributes() const;
299 /// \brief Return true if the attribute exists at the given index.
300 bool hasAttribute(uint64_t Index, Attribute::AttrKind Kind) const;
302 /// \brief Return true if attribute exists at the given index.
303 bool hasAttributes(uint64_t Index) const;
305 /// \brief Return the alignment for the specified function parameter.
306 unsigned getParamAlignment(uint64_t Idx) const;
308 /// \brief Get the stack alignment.
309 unsigned getStackAlignment(uint64_t Index) const;
311 /// \brief Return the attributes at the index as a string.
312 std::string getAsString(uint64_t Index) const;
314 uint64_t Raw(unsigned Index) const;
316 /// \brief Return true if the specified attribute is set for at least one
317 /// parameter or for the return value.
318 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
320 /// operator==/!= - Provide equality predicates.
321 bool operator==(const AttributeSet &RHS) const {
322 return pImpl == RHS.pImpl;
324 bool operator!=(const AttributeSet &RHS) const {
325 return pImpl != RHS.pImpl;
328 //===--------------------------------------------------------------------===//
329 // Attribute List Introspection
330 //===--------------------------------------------------------------------===//
332 /// \brief Return a raw pointer that uniquely identifies this attribute list.
333 void *getRawPointer() const {
337 // Attributes are stored as a dense set of slots, where there is one slot for
338 // each argument that has an attribute. This allows walking over the dense
339 // set instead of walking the sparse list of attributes.
341 /// \brief Return true if there are no attributes.
342 bool isEmpty() const {
346 /// \brief Return the number of slots used in this attribute list. This is
347 /// the number of arguments that have an attribute set on them (including the
348 /// function itself).
349 unsigned getNumSlots() const;
351 /// \brief Return the index for the given slot.
352 uint64_t getSlotIndex(unsigned Slot) const;
354 /// \brief Return the attributes at the given slot.
355 AttributeSet getSlotAttributes(unsigned Slot) const;
360 //===----------------------------------------------------------------------===//
362 /// \brief This class is used in conjunction with the Attribute::get method to
363 /// create an Attribute object. The object itself is uniquified. The Builder's
364 /// value, however, is not. So this can be used as a quick way to test for
365 /// equality, presence of attributes, etc.
367 DenseSet<Attribute::AttrKind> Attrs;
369 uint64_t StackAlignment;
371 AttrBuilder() : Alignment(0), StackAlignment(0) {}
372 explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
375 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
378 AttrBuilder(AttributeSet AS, unsigned Idx);
382 /// \brief Add an attribute to the builder.
383 AttrBuilder &addAttribute(Attribute::AttrKind Val);
385 /// \brief Remove an attribute from the builder.
386 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
388 /// \brief Add the attributes to the builder.
389 AttrBuilder &addAttributes(Attribute A);
391 /// \brief Remove the attributes from the builder.
392 AttrBuilder &removeAttributes(Attribute A);
394 /// \brief Add the attributes to the builder.
395 AttrBuilder &addAttributes(AttributeSet A);
397 /// \brief Return true if the builder has the specified attribute.
398 bool contains(Attribute::AttrKind A) const;
400 /// \brief Return true if the builder has IR-level attributes.
401 bool hasAttributes() const;
403 /// \brief Return true if the builder has any attribute that's in the
404 /// specified attribute.
405 bool hasAttributes(const Attribute &A) const;
407 /// \brief Return true if the builder has an alignment attribute.
408 bool hasAlignmentAttr() const;
410 /// \brief Retrieve the alignment attribute, if it exists.
411 uint64_t getAlignment() const { return Alignment; }
413 /// \brief Retrieve the stack alignment attribute, if it exists.
414 uint64_t getStackAlignment() const { return StackAlignment; }
416 /// \brief This turns an int alignment (which must be a power of 2) into the
417 /// form used internally in Attribute.
418 AttrBuilder &addAlignmentAttr(unsigned Align);
420 /// \brief This turns an int stack alignment (which must be a power of 2) into
421 /// the form used internally in Attribute.
422 AttrBuilder &addStackAlignmentAttr(unsigned Align);
424 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
425 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
427 iterator begin() { return Attrs.begin(); }
428 iterator end() { return Attrs.end(); }
430 const_iterator begin() const { return Attrs.begin(); }
431 const_iterator end() const { return Attrs.end(); }
433 /// \brief Add the raw value to the internal representation.
435 /// N.B. This should be used ONLY for decoding LLVM bitcode!
436 AttrBuilder &addRawValue(uint64_t Val);
438 /// \brief Remove attributes that are used on functions only.
439 void removeFunctionOnlyAttrs() {
440 removeAttribute(Attribute::NoReturn)
441 .removeAttribute(Attribute::NoUnwind)
442 .removeAttribute(Attribute::ReadNone)
443 .removeAttribute(Attribute::ReadOnly)
444 .removeAttribute(Attribute::NoInline)
445 .removeAttribute(Attribute::AlwaysInline)
446 .removeAttribute(Attribute::OptimizeForSize)
447 .removeAttribute(Attribute::StackProtect)
448 .removeAttribute(Attribute::StackProtectReq)
449 .removeAttribute(Attribute::StackProtectStrong)
450 .removeAttribute(Attribute::NoRedZone)
451 .removeAttribute(Attribute::NoImplicitFloat)
452 .removeAttribute(Attribute::Naked)
453 .removeAttribute(Attribute::InlineHint)
454 .removeAttribute(Attribute::StackAlignment)
455 .removeAttribute(Attribute::UWTable)
456 .removeAttribute(Attribute::NonLazyBind)
457 .removeAttribute(Attribute::ReturnsTwice)
458 .removeAttribute(Attribute::AddressSafety)
459 .removeAttribute(Attribute::MinSize)
460 .removeAttribute(Attribute::NoDuplicate);
463 uint64_t Raw() const;
465 bool operator==(const AttrBuilder &B);
466 bool operator!=(const AttrBuilder &B) {
467 return !(*this == B);
471 namespace AttributeFuncs {
473 /// \brief Which attributes cannot be applied to a type.
474 Attribute typeIncompatible(Type *Ty);
476 /// \brief This returns an integer containing an encoding of all the LLVM
477 /// attributes found in the given attribute bitset. Any change to this encoding
478 /// is a breaking change to bitcode compatibility.
479 uint64_t encodeLLVMAttributesForBitcode(AttributeSet Attrs, unsigned Index);
481 /// \brief This returns an attribute bitset containing the LLVM attributes that
482 /// have been decoded from the given integer. This function must stay in sync
483 /// with 'encodeLLVMAttributesForBitcode'.
484 Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
485 uint64_t EncodedAttrs);
487 } // end AttributeFuncs namespace
489 } // end llvm namespace