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/FoldingSet.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/PointerLikeTypeTraits.h"
32 class AttributeSetImpl;
33 class AttributeSetNode;
35 template<typename T> struct DenseMapInfo;
39 //===----------------------------------------------------------------------===//
41 /// \brief Functions, function parameters, and return types can have attributes
42 /// to indicate how they should be treated by optimizations and code
43 /// generation. This class represents one of those attributes. It's light-weight
44 /// and should be passed around by-value.
47 /// This enumeration lists the attributes that can be associated with
48 /// parameters, function results, or the function itself.
50 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
51 /// entry in the unwind table. The `nounwind' attribute is about an exception
52 /// passing by the function.
54 /// In a theoretical system that uses tables for profiling and SjLj for
55 /// exceptions, they would be fully independent. In a normal system that uses
56 /// tables for both, the semantics are:
58 /// nil = Needs an entry because an exception might pass by.
59 /// nounwind = No need for an entry
60 /// uwtable = Needs an entry because the ABI says so and because
61 /// an exception might pass by.
62 /// uwtable + nounwind = Needs an entry because the ABI says so.
65 // IR-Level Attributes
66 None, ///< No attributes have been set
67 Alignment, ///< Alignment of parameter (5 bits)
68 ///< stored as log2 of alignment with +1 bias
69 ///< 0 means unaligned (different from align(1))
70 AlwaysInline, ///< inline=always
71 Builtin, ///< Callee is recognized as a builtin, despite
72 ///< nobuiltin attribute on its declaration.
73 ByVal, ///< Pass structure by value
74 Cold, ///< Marks function as being in a cold path.
75 InlineHint, ///< Source said inlining was desirable
76 InReg, ///< Force argument to be passed in register
77 MinSize, ///< Function must be optimized for size first
78 Naked, ///< Naked function
79 Nest, ///< Nested function static chain
80 NoAlias, ///< Considered to not alias after call
81 NoBuiltin, ///< Callee isn't recognized as a builtin
82 NoCapture, ///< Function creates no aliases of pointer
83 NoDuplicate, ///< Call cannot be duplicated
84 NoImplicitFloat, ///< Disable implicit floating point insts
85 NoInline, ///< inline=never
86 NonLazyBind, ///< Function is called early and/or
87 ///< often, so lazy binding isn't worthwhile
88 NoRedZone, ///< Disable redzone
89 NoReturn, ///< Mark the function as not returning
90 NoUnwind, ///< Function doesn't unwind stack
91 OptimizeForSize, ///< opt_size
92 OptimizeNone, ///< Function must not be optimized.
93 ReadNone, ///< Function does not access memory
94 ReadOnly, ///< Function only reads from memory
95 Returned, ///< Return value is always equal to this argument
96 ReturnsTwice, ///< Function can return twice
97 SExt, ///< Sign extended before/after call
98 StackAlignment, ///< Alignment of stack for function (3 bits)
99 ///< stored as log2 of alignment with +1 bias 0
100 ///< means unaligned (different from
102 StackProtect, ///< Stack protection.
103 StackProtectReq, ///< Stack protection required.
104 StackProtectStrong, ///< Strong Stack protection.
105 StructRet, ///< Hidden pointer to structure to return
106 SanitizeAddress, ///< AddressSanitizer is on.
107 SanitizeThread, ///< ThreadSanitizer is on.
108 SanitizeMemory, ///< MemorySanitizer is on.
109 UWTable, ///< Function must be in a unwind table
110 ZExt, ///< Zero extended before/after call
112 EndAttrKinds ///< Sentinal value useful for loops
115 AttributeImpl *pImpl;
116 Attribute(AttributeImpl *A) : pImpl(A) {}
118 Attribute() : pImpl(0) {}
120 //===--------------------------------------------------------------------===//
121 // Attribute Construction
122 //===--------------------------------------------------------------------===//
124 /// \brief Return a uniquified Attribute object.
125 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
126 static Attribute get(LLVMContext &Context, StringRef Kind,
127 StringRef Val = StringRef());
129 /// \brief Return a uniquified Attribute object that has the specific
131 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
132 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
134 //===--------------------------------------------------------------------===//
135 // Attribute Accessors
136 //===--------------------------------------------------------------------===//
138 /// \brief Return true if the attribute is an Attribute::AttrKind type.
139 bool isEnumAttribute() const;
141 /// \brief Return true if the attribute is an alignment attribute.
142 bool isAlignAttribute() const;
144 /// \brief Return true if the attribute is a string (target-dependent)
146 bool isStringAttribute() const;
148 /// \brief Return true if the attribute is present.
149 bool hasAttribute(AttrKind Val) const;
151 /// \brief Return true if the target-dependent attribute is present.
152 bool hasAttribute(StringRef Val) const;
154 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
155 /// requires the attribute to be an enum or alignment attribute.
156 Attribute::AttrKind getKindAsEnum() const;
158 /// \brief Return the attribute's value as an integer. This requires that the
159 /// attribute be an alignment attribute.
160 uint64_t getValueAsInt() const;
162 /// \brief Return the attribute's kind as a string. This requires the
163 /// attribute to be a string attribute.
164 StringRef getKindAsString() const;
166 /// \brief Return the attribute's value as a string. This requires the
167 /// attribute to be a string attribute.
168 StringRef getValueAsString() const;
170 /// \brief Returns the alignment field of an attribute as a byte alignment
172 unsigned getAlignment() const;
174 /// \brief Returns the stack alignment field of an attribute as a byte
176 unsigned getStackAlignment() const;
178 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
179 /// is, presumably, for writing out the mnemonics for the assembly writer.
180 std::string getAsString(bool InAttrGrp = false) const;
182 /// \brief Equality and non-equality operators.
183 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
184 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
186 /// \brief Less-than operator. Useful for sorting the attributes list.
187 bool operator<(Attribute A) const;
189 void Profile(FoldingSetNodeID &ID) const {
190 ID.AddPointer(pImpl);
194 //===----------------------------------------------------------------------===//
196 /// \brief This class holds the attributes for a function, its return value, and
197 /// its parameters. You access the attributes for each of them via an index into
198 /// the AttributeSet object. The function attributes are at index
199 /// `AttributeSet::FunctionIndex', the return value is at index
200 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
204 enum AttrIndex LLVM_ENUM_INT_TYPE(unsigned) {
209 friend class AttrBuilder;
210 friend class AttributeSetImpl;
211 template <typename Ty> friend struct DenseMapInfo;
213 /// \brief The attributes that we are managing. This can be null to represent
214 /// the empty attributes list.
215 AttributeSetImpl *pImpl;
217 /// \brief The attributes for the specified index are returned.
218 AttributeSetNode *getAttributes(unsigned Index) const;
220 /// \brief Create an AttributeSet with the specified parameters in it.
221 static AttributeSet get(LLVMContext &C,
222 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
223 static AttributeSet get(LLVMContext &C,
224 ArrayRef<std::pair<unsigned,
225 AttributeSetNode*> > Attrs);
227 static AttributeSet getImpl(LLVMContext &C,
228 ArrayRef<std::pair<unsigned,
229 AttributeSetNode*> > Attrs);
232 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
234 AttributeSet() : pImpl(0) {}
236 //===--------------------------------------------------------------------===//
237 // AttributeSet Construction and Mutation
238 //===--------------------------------------------------------------------===//
240 /// \brief Return an AttributeSet with the specified parameters in it.
241 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
242 static AttributeSet get(LLVMContext &C, unsigned Index,
243 ArrayRef<Attribute::AttrKind> Kind);
244 static AttributeSet get(LLVMContext &C, unsigned Index, AttrBuilder &B);
246 /// \brief Add an attribute to the attribute set at the given index. Since
247 /// attribute sets are immutable, this returns a new set.
248 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
249 Attribute::AttrKind Attr) const;
251 /// \brief Add an attribute to the attribute set at the given index. Since
252 /// attribute sets are immutable, this returns a new set.
253 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
254 StringRef Kind) const;
255 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
256 StringRef Kind, StringRef Value) const;
258 /// \brief Add attributes to the attribute set at the given index. Since
259 /// attribute sets are immutable, this returns a new set.
260 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
261 AttributeSet Attrs) const;
263 /// \brief Remove the specified attribute at the specified index from this
264 /// attribute list. Since attribute lists are immutable, this returns the new
266 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
267 Attribute::AttrKind Attr) const;
269 /// \brief Remove the specified attributes at the specified index from this
270 /// attribute list. Since attribute lists are immutable, this returns the new
272 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
273 AttributeSet Attrs) const;
275 //===--------------------------------------------------------------------===//
276 // AttributeSet Accessors
277 //===--------------------------------------------------------------------===//
279 /// \brief Retrieve the LLVM context.
280 LLVMContext &getContext() const;
282 /// \brief The attributes for the specified index are returned.
283 AttributeSet getParamAttributes(unsigned Index) const;
285 /// \brief The attributes for the ret value are returned.
286 AttributeSet getRetAttributes() const;
288 /// \brief The function attributes are returned.
289 AttributeSet getFnAttributes() const;
291 /// \brief Return true if the attribute exists at the given index.
292 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
294 /// \brief Return true if the attribute exists at the given index.
295 bool hasAttribute(unsigned Index, StringRef Kind) const;
297 /// \brief Return true if attribute exists at the given index.
298 bool hasAttributes(unsigned Index) const;
300 /// \brief Return true if the specified attribute is set for at least one
301 /// parameter or for the return value.
302 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
304 /// \brief Return the attribute object that exists at the given index.
305 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
307 /// \brief Return the attribute object that exists at the given index.
308 Attribute getAttribute(unsigned Index, StringRef Kind) const;
310 /// \brief Return the alignment for the specified function parameter.
311 unsigned getParamAlignment(unsigned Index) const;
313 /// \brief Get the stack alignment.
314 unsigned getStackAlignment(unsigned Index) const;
316 /// \brief Return the attributes at the index as a string.
317 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
319 typedef ArrayRef<Attribute>::iterator iterator;
321 iterator begin(unsigned Slot) const;
322 iterator end(unsigned Slot) const;
324 /// operator==/!= - Provide equality predicates.
325 bool operator==(const AttributeSet &RHS) const {
326 return pImpl == RHS.pImpl;
328 bool operator!=(const AttributeSet &RHS) const {
329 return pImpl != RHS.pImpl;
332 //===--------------------------------------------------------------------===//
333 // AttributeSet Introspection
334 //===--------------------------------------------------------------------===//
336 // FIXME: Remove this.
337 uint64_t Raw(unsigned Index) const;
339 /// \brief Return a raw pointer that uniquely identifies this attribute list.
340 void *getRawPointer() const {
344 /// \brief Return true if there are no attributes.
345 bool isEmpty() const {
346 return getNumSlots() == 0;
349 /// \brief Return the number of slots used in this attribute list. This is
350 /// the number of arguments that have an attribute set on them (including the
351 /// function itself).
352 unsigned getNumSlots() const;
354 /// \brief Return the index for the given slot.
355 unsigned getSlotIndex(unsigned Slot) const;
357 /// \brief Return the attributes at the given slot.
358 AttributeSet getSlotAttributes(unsigned Slot) const;
363 //===----------------------------------------------------------------------===//
365 /// \brief Provide DenseMapInfo for AttributeSet.
366 template<> struct DenseMapInfo<AttributeSet> {
367 static inline AttributeSet getEmptyKey() {
368 uintptr_t Val = static_cast<uintptr_t>(-1);
369 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
370 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
372 static inline AttributeSet getTombstoneKey() {
373 uintptr_t Val = static_cast<uintptr_t>(-2);
374 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
375 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
377 static unsigned getHashValue(AttributeSet AS) {
378 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
379 (unsigned((uintptr_t)AS.pImpl) >> 9);
381 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
384 //===----------------------------------------------------------------------===//
386 /// \brief This class is used in conjunction with the Attribute::get method to
387 /// create an Attribute object. The object itself is uniquified. The Builder's
388 /// value, however, is not. So this can be used as a quick way to test for
389 /// equality, presence of attributes, etc.
391 std::bitset<Attribute::EndAttrKinds> Attrs;
392 std::map<std::string, std::string> TargetDepAttrs;
394 uint64_t StackAlignment;
396 AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
397 explicit AttrBuilder(uint64_t Val)
398 : Attrs(0), Alignment(0), StackAlignment(0) {
401 AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
404 AttrBuilder(AttributeSet AS, unsigned Idx);
405 AttrBuilder(const AttrBuilder &B)
407 TargetDepAttrs(B.TargetDepAttrs.begin(), B.TargetDepAttrs.end()),
408 Alignment(B.Alignment), StackAlignment(B.StackAlignment) {}
412 /// \brief Add an attribute to the builder.
413 AttrBuilder &addAttribute(Attribute::AttrKind Val);
415 /// \brief Add the Attribute object to the builder.
416 AttrBuilder &addAttribute(Attribute A);
418 /// \brief Add the target-dependent attribute to the builder.
419 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
421 /// \brief Remove an attribute from the builder.
422 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
424 /// \brief Remove the attributes from the builder.
425 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
427 /// \brief Remove the target-dependent attribute to the builder.
428 AttrBuilder &removeAttribute(StringRef A);
430 /// \brief Add the attributes from the builder.
431 AttrBuilder &merge(const AttrBuilder &B);
433 /// \brief Return true if the builder has the specified attribute.
434 bool contains(Attribute::AttrKind A) const {
435 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
439 /// \brief Return true if the builder has the specified target-dependent
441 bool contains(StringRef A) const;
443 /// \brief Return true if the builder has IR-level attributes.
444 bool hasAttributes() const;
446 /// \brief Return true if the builder has any attribute that's in the
447 /// specified attribute.
448 bool hasAttributes(AttributeSet A, uint64_t Index) const;
450 /// \brief Return true if the builder has an alignment attribute.
451 bool hasAlignmentAttr() const;
453 /// \brief Retrieve the alignment attribute, if it exists.
454 uint64_t getAlignment() const { return Alignment; }
456 /// \brief Retrieve the stack alignment attribute, if it exists.
457 uint64_t getStackAlignment() const { return StackAlignment; }
459 /// \brief This turns an int alignment (which must be a power of 2) into the
460 /// form used internally in Attribute.
461 AttrBuilder &addAlignmentAttr(unsigned Align);
463 /// \brief This turns an int stack alignment (which must be a power of 2) into
464 /// the form used internally in Attribute.
465 AttrBuilder &addStackAlignmentAttr(unsigned Align);
467 /// \brief Return true if the builder contains no target-independent
469 bool empty() const { return Attrs.none(); }
471 // Iterators for target-dependent attributes.
472 typedef std::pair<std::string, std::string> td_type;
473 typedef std::map<std::string, std::string>::iterator td_iterator;
474 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
476 td_iterator td_begin() { return TargetDepAttrs.begin(); }
477 td_iterator td_end() { return TargetDepAttrs.end(); }
479 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
480 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
482 bool td_empty() const { return TargetDepAttrs.empty(); }
484 bool operator==(const AttrBuilder &B);
485 bool operator!=(const AttrBuilder &B) {
486 return !(*this == B);
489 // FIXME: Remove this in 4.0.
491 /// \brief Add the raw value to the internal representation.
492 AttrBuilder &addRawValue(uint64_t Val);
495 namespace AttributeFuncs {
497 /// \brief Which attributes cannot be applied to a type.
498 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
500 } // end AttributeFuncs namespace
502 } // end llvm namespace