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"
29 class AttributeSetImpl;
30 class AttributeSetNode;
35 //===----------------------------------------------------------------------===//
37 /// \brief Functions, function parameters, and return types can have attributes
38 /// to indicate how they should be treated by optimizations and code
39 /// generation. This class represents one of those attributes. It's light-weight
40 /// and should be passed around by-value.
43 /// This enumeration lists the attributes that can be associated with
44 /// parameters, function results, or the function itself.
46 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
47 /// entry in the unwind table. The `nounwind' attribute is about an exception
48 /// passing by the function.
50 /// In a theoretical system that uses tables for profiling and SjLj for
51 /// exceptions, they would be fully independent. In a normal system that uses
52 /// tables for both, the semantics are:
54 /// nil = Needs an entry because an exception might pass by.
55 /// nounwind = No need for an entry
56 /// uwtable = Needs an entry because the ABI says so and because
57 /// an exception might pass by.
58 /// uwtable + nounwind = Needs an entry because the ABI says so.
61 // IR-Level Attributes
62 None, ///< No attributes have been set
63 AddressSafety, ///< Address safety checking is on.
64 Alignment, ///< Alignment of parameter (5 bits)
65 ///< stored as log2 of alignment with +1 bias
66 ///< 0 means unaligned (different from align(1))
67 AlwaysInline, ///< inline=always
68 ByVal, ///< Pass structure by value
69 InlineHint, ///< Source said inlining was desirable
70 InReg, ///< Force argument to be passed in register
71 MinSize, ///< Function must be optimized for size first
72 Naked, ///< Naked function
73 Nest, ///< Nested function static chain
74 NoAlias, ///< Considered to not alias after call
75 NoCapture, ///< Function creates no aliases of pointer
76 NoDuplicate, ///< Call cannot be duplicated
77 NoImplicitFloat, ///< Disable implicit floating point insts
78 NoInline, ///< inline=never
79 NonLazyBind, ///< Function is called early and/or
80 ///< often, so lazy binding isn't worthwhile
81 NoRedZone, ///< Disable redzone
82 NoReturn, ///< Mark the function as not returning
83 NoUnwind, ///< Function doesn't unwind stack
84 OptimizeForSize, ///< opt_size
85 ReadNone, ///< Function does not access memory
86 ReadOnly, ///< Function only reads from memory
87 ReturnsTwice, ///< Function can return twice
88 SExt, ///< Sign extended before/after call
89 StackAlignment, ///< Alignment of stack for function (3 bits)
90 ///< stored as log2 of alignment with +1 bias 0
91 ///< means unaligned (different from
93 StackProtect, ///< Stack protection.
94 StackProtectReq, ///< Stack protection required.
95 StackProtectStrong, ///< Strong Stack protection.
96 StructRet, ///< Hidden pointer to structure to return
97 UWTable, ///< Function must be in a unwind table
98 ZExt, ///< Zero extended before/after call
100 EndAttrKinds, ///< Sentinal value useful for loops
102 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
103 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
106 AttributeImpl *pImpl;
107 Attribute(AttributeImpl *A) : pImpl(A) {}
109 Attribute() : pImpl(0) {}
111 //===--------------------------------------------------------------------===//
112 // Attribute Construction
113 //===--------------------------------------------------------------------===//
115 /// \brief Return a uniquified Attribute object.
116 static Attribute get(LLVMContext &Context, AttrKind Kind, Constant *Val = 0);
118 /// \brief Return a uniquified Attribute object that has the specific
120 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
121 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
123 //===--------------------------------------------------------------------===//
124 // Attribute Accessors
125 //===--------------------------------------------------------------------===//
127 /// \brief Return true if the attribute is present.
128 bool hasAttribute(AttrKind Val) const;
130 /// \brief Return true if attributes exist
131 bool hasAttributes() const;
133 /// \brief Return the kind of this attribute.
134 Constant *getAttributeKind() const;
136 /// \brief Return the value (if present) of the non-target-specific attribute.
137 ArrayRef<Constant*> getAttributeValues() const;
139 /// \brief Returns the alignment field of an attribute as a byte alignment
141 unsigned getAlignment() const;
143 /// \brief Returns the stack alignment field of an attribute as a byte
145 unsigned getStackAlignment() const;
147 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
148 /// is, presumably, for writing out the mnemonics for the assembly writer.
149 std::string getAsString() const;
151 /// \brief Equality and non-equality query methods.
152 bool operator==(AttrKind K) const;
153 bool operator!=(AttrKind K) const;
155 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
156 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
158 /// \brief Less-than operator. Useful for sorting the attributes list.
159 bool operator<(Attribute A) const;
161 void Profile(FoldingSetNodeID &ID) const {
162 ID.AddPointer(pImpl);
165 // FIXME: Remove this.
166 uint64_t Raw() const;
169 //===----------------------------------------------------------------------===//
171 /// \brief This class manages the ref count for the opaque AttributeSetImpl
172 /// object and provides accessors for it.
180 friend class AttrBuilder;
181 friend class AttributeSetImpl;
183 /// \brief The attributes that we are managing. This can be null to represent
184 /// the empty attributes list.
185 AttributeSetImpl *pImpl;
187 /// \brief The attributes for the specified index are returned.
188 AttributeSetNode *getAttributes(unsigned Idx) const;
190 /// \brief Create an AttributeSet with the specified parameters in it.
191 static AttributeSet get(LLVMContext &C,
192 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
193 static AttributeSet get(LLVMContext &C,
194 ArrayRef<std::pair<unsigned,
195 AttributeSetNode*> > Attrs);
197 static AttributeSet getImpl(LLVMContext &C,
198 ArrayRef<std::pair<unsigned,
199 AttributeSetNode*> > Attrs);
202 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
204 AttributeSet() : pImpl(0) {}
205 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
206 const AttributeSet &operator=(const AttributeSet &RHS) {
211 //===--------------------------------------------------------------------===//
212 // AttributeSet Construction and Mutation
213 //===--------------------------------------------------------------------===//
215 /// \brief Return an AttributeSet with the specified parameters in it.
216 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
217 static AttributeSet get(LLVMContext &C, unsigned Idx,
218 ArrayRef<Attribute::AttrKind> Kind);
219 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
221 /// \brief Add an attribute to the attribute set at the given index. Since
222 /// attribute sets are immutable, this returns a new set.
223 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
224 Attribute::AttrKind Attr) const;
226 /// \brief Add attributes to the attribute set at the given index. Since
227 /// attribute sets are immutable, this returns a new set.
228 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
229 AttributeSet Attrs) const;
231 /// \brief Remove the specified attribute at the specified index from this
232 /// attribute list. Since attribute lists are immutable, this returns the new
234 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
235 Attribute::AttrKind Attr) const;
237 /// \brief Remove the specified attributes at the specified index from this
238 /// attribute list. Since attribute lists are immutable, this returns the new
240 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
241 AttributeSet Attrs) const;
243 //===--------------------------------------------------------------------===//
244 // AttributeSet Accessors
245 //===--------------------------------------------------------------------===//
247 /// \brief The attributes for the specified index are returned.
248 AttributeSet getParamAttributes(unsigned Idx) const;
250 /// \brief The attributes for the ret value are returned.
251 AttributeSet getRetAttributes() const;
253 /// \brief The function attributes are returned.
254 AttributeSet getFnAttributes() const;
256 /// \brief Return true if the attribute exists at the given index.
257 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
259 /// \brief Return true if attribute exists at the given index.
260 bool hasAttributes(unsigned Index) const;
262 /// \brief Return true if the specified attribute is set for at least one
263 /// parameter or for the return value.
264 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
266 /// \brief Return the alignment for the specified function parameter.
267 unsigned getParamAlignment(unsigned Idx) const;
269 /// \brief Get the stack alignment.
270 unsigned getStackAlignment(unsigned Index) const;
272 /// \brief Return the attributes at the index as a string.
273 std::string getAsString(unsigned Index) const;
275 /// operator==/!= - Provide equality predicates.
276 bool operator==(const AttributeSet &RHS) const {
277 return pImpl == RHS.pImpl;
279 bool operator!=(const AttributeSet &RHS) const {
280 return pImpl != RHS.pImpl;
283 //===--------------------------------------------------------------------===//
284 // AttributeSet Introspection
285 //===--------------------------------------------------------------------===//
287 // FIXME: Remove this.
288 uint64_t Raw(unsigned Index) const;
290 /// \brief Return a raw pointer that uniquely identifies this attribute list.
291 void *getRawPointer() const {
295 /// \brief Return true if there are no attributes.
296 bool isEmpty() const {
297 return getNumSlots() == 0;
300 /// \brief Return the number of slots used in this attribute list. This is
301 /// the number of arguments that have an attribute set on them (including the
302 /// function itself).
303 unsigned getNumSlots() const;
305 /// \brief Return the index for the given slot.
306 uint64_t getSlotIndex(unsigned Slot) const;
308 /// \brief Return the attributes at the given slot.
309 AttributeSet getSlotAttributes(unsigned Slot) const;
314 //===----------------------------------------------------------------------===//
316 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
318 template<> struct DenseMapInfo<Attribute::AttrKind> {
319 static inline Attribute::AttrKind getEmptyKey() {
320 return Attribute::AttrKindEmptyKey;
322 static inline Attribute::AttrKind getTombstoneKey() {
323 return Attribute::AttrKindTombstoneKey;
325 static unsigned getHashValue(const Attribute::AttrKind &Val) {
328 static bool isEqual(const Attribute::AttrKind &LHS,
329 const Attribute::AttrKind &RHS) {
334 //===----------------------------------------------------------------------===//
336 /// \brief This class is used in conjunction with the Attribute::get method to
337 /// create an Attribute object. The object itself is uniquified. The Builder's
338 /// value, however, is not. So this can be used as a quick way to test for
339 /// equality, presence of attributes, etc.
341 DenseSet<Attribute::AttrKind> Attrs;
343 uint64_t StackAlignment;
345 AttrBuilder() : Alignment(0), StackAlignment(0) {}
346 explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
349 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
352 AttrBuilder(AttributeSet AS, unsigned Idx);
356 /// \brief Add an attribute to the builder.
357 AttrBuilder &addAttribute(Attribute::AttrKind Val);
359 /// \brief Remove an attribute from the builder.
360 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
362 /// \brief Add the attributes to the builder.
363 AttrBuilder &addAttributes(Attribute A);
365 /// \brief Remove the attributes from the builder.
366 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
368 /// \brief Return true if the builder has the specified attribute.
369 bool contains(Attribute::AttrKind A) const;
371 /// \brief Return true if the builder has IR-level attributes.
372 bool hasAttributes() const;
374 /// \brief Return true if the builder has any attribute that's in the
375 /// specified attribute.
376 bool hasAttributes(AttributeSet A, uint64_t Index) const;
378 /// \brief Return true if the builder has an alignment attribute.
379 bool hasAlignmentAttr() const;
381 /// \brief Retrieve the alignment attribute, if it exists.
382 uint64_t getAlignment() const { return Alignment; }
384 /// \brief Retrieve the stack alignment attribute, if it exists.
385 uint64_t getStackAlignment() const { return StackAlignment; }
387 /// \brief This turns an int alignment (which must be a power of 2) into the
388 /// form used internally in Attribute.
389 AttrBuilder &addAlignmentAttr(unsigned Align);
391 /// \brief This turns an int stack alignment (which must be a power of 2) into
392 /// the form used internally in Attribute.
393 AttrBuilder &addStackAlignmentAttr(unsigned Align);
395 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
396 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
398 iterator begin() { return Attrs.begin(); }
399 iterator end() { return Attrs.end(); }
401 const_iterator begin() const { return Attrs.begin(); }
402 const_iterator end() const { return Attrs.end(); }
404 /// \brief Remove attributes that are used on functions only.
405 void removeFunctionOnlyAttrs() {
406 removeAttribute(Attribute::NoReturn)
407 .removeAttribute(Attribute::NoUnwind)
408 .removeAttribute(Attribute::ReadNone)
409 .removeAttribute(Attribute::ReadOnly)
410 .removeAttribute(Attribute::NoInline)
411 .removeAttribute(Attribute::AlwaysInline)
412 .removeAttribute(Attribute::OptimizeForSize)
413 .removeAttribute(Attribute::StackProtect)
414 .removeAttribute(Attribute::StackProtectReq)
415 .removeAttribute(Attribute::StackProtectStrong)
416 .removeAttribute(Attribute::NoRedZone)
417 .removeAttribute(Attribute::NoImplicitFloat)
418 .removeAttribute(Attribute::Naked)
419 .removeAttribute(Attribute::InlineHint)
420 .removeAttribute(Attribute::StackAlignment)
421 .removeAttribute(Attribute::UWTable)
422 .removeAttribute(Attribute::NonLazyBind)
423 .removeAttribute(Attribute::ReturnsTwice)
424 .removeAttribute(Attribute::AddressSafety)
425 .removeAttribute(Attribute::MinSize)
426 .removeAttribute(Attribute::NoDuplicate);
429 bool operator==(const AttrBuilder &B);
430 bool operator!=(const AttrBuilder &B) {
431 return !(*this == B);
434 // FIXME: Remove these.
436 /// \brief Add the raw value to the internal representation.
438 /// N.B. This should be used ONLY for decoding LLVM bitcode!
439 AttrBuilder &addRawValue(uint64_t Val);
441 uint64_t Raw() const;
444 namespace AttributeFuncs {
446 /// \brief Which attributes cannot be applied to a type.
447 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
449 /// \brief This returns an integer containing an encoding of all the LLVM
450 /// attributes found in the given attribute bitset. Any change to this encoding
451 /// is a breaking change to bitcode compatibility.
452 uint64_t encodeLLVMAttributesForBitcode(AttributeSet Attrs, unsigned Index);
454 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
455 /// been decoded from the given integer. This function must stay in sync with
456 /// 'encodeLLVMAttributesForBitcode'.
457 /// N.B. This should be used only by the bitcode reader!
458 void decodeLLVMAttributesForBitcode(LLVMContext &C, AttrBuilder &B,
459 uint64_t EncodedAttrs);
461 } // end AttributeFuncs namespace
463 } // end llvm namespace