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 InAlloca, ///< Pass structure in an alloca
75 Cold, ///< Marks function as being in a cold path.
76 Convergent, ///< Can only be moved to control-equivalent blocks
77 InlineHint, ///< Source said inlining was desirable
78 InReg, ///< Force argument to be passed in register
79 JumpTable, ///< Build jump-instruction tables and replace refs.
80 MinSize, ///< Function must be optimized for size first
81 Naked, ///< Naked function
82 Nest, ///< Nested function static chain
83 NoAlias, ///< Considered to not alias after call
84 NoBuiltin, ///< Callee isn't recognized as a builtin
85 NoCapture, ///< Function creates no aliases of pointer
86 NoDuplicate, ///< Call cannot be duplicated
87 NoImplicitFloat, ///< Disable implicit floating point insts
88 NoInline, ///< inline=never
89 NonLazyBind, ///< Function is called early and/or
90 ///< often, so lazy binding isn't worthwhile
91 NonNull, ///< Pointer is known to be not null
92 Dereferenceable, ///< Pointer is known to be dereferenceable
93 DereferenceableOrNull, ///< Pointer is either null or dereferenceable
94 NoRedZone, ///< Disable redzone
95 NoReturn, ///< Mark the function as not returning
96 NoUnwind, ///< Function doesn't unwind stack
97 OptimizeForSize, ///< opt_size
98 OptimizeNone, ///< Function must not be optimized.
99 ReadNone, ///< Function does not access memory
100 ReadOnly, ///< Function only reads from memory
101 ArgMemOnly, ///< Funciton can access memory only using pointers
102 ///< based on its arguments.
103 Returned, ///< Return value is always equal to this argument
104 ReturnsTwice, ///< Function can return twice
105 SExt, ///< Sign extended before/after call
106 StackAlignment, ///< Alignment of stack for function (3 bits)
107 ///< stored as log2 of alignment with +1 bias 0
108 ///< means unaligned (different from
110 StackProtect, ///< Stack protection.
111 StackProtectReq, ///< Stack protection required.
112 StackProtectStrong, ///< Strong Stack protection.
113 SafeStack, ///< Safe Stack protection.
114 StructRet, ///< Hidden pointer to structure to return
115 SanitizeAddress, ///< AddressSanitizer is on.
116 SanitizeThread, ///< ThreadSanitizer is on.
117 SanitizeMemory, ///< MemorySanitizer is on.
118 UWTable, ///< Function must be in a unwind table
119 ZExt, ///< Zero extended before/after call
121 EndAttrKinds ///< Sentinal value useful for loops
124 AttributeImpl *pImpl;
125 Attribute(AttributeImpl *A) : pImpl(A) {}
127 Attribute() : pImpl(nullptr) {}
129 //===--------------------------------------------------------------------===//
130 // Attribute Construction
131 //===--------------------------------------------------------------------===//
133 /// \brief Return a uniquified Attribute object.
134 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
135 static Attribute get(LLVMContext &Context, StringRef Kind,
136 StringRef Val = StringRef());
138 /// \brief Return a uniquified Attribute object that has the specific
140 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
141 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
142 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
144 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
147 //===--------------------------------------------------------------------===//
148 // Attribute Accessors
149 //===--------------------------------------------------------------------===//
151 /// \brief Return true if the attribute is an Attribute::AttrKind type.
152 bool isEnumAttribute() const;
154 /// \brief Return true if the attribute is an integer attribute.
155 bool isIntAttribute() const;
157 /// \brief Return true if the attribute is a string (target-dependent)
159 bool isStringAttribute() const;
161 /// \brief Return true if the attribute is present.
162 bool hasAttribute(AttrKind Val) const;
164 /// \brief Return true if the target-dependent attribute is present.
165 bool hasAttribute(StringRef Val) const;
167 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
168 /// requires the attribute to be an enum or alignment attribute.
169 Attribute::AttrKind getKindAsEnum() const;
171 /// \brief Return the attribute's value as an integer. This requires that the
172 /// attribute be an alignment attribute.
173 uint64_t getValueAsInt() const;
175 /// \brief Return the attribute's kind as a string. This requires the
176 /// attribute to be a string attribute.
177 StringRef getKindAsString() const;
179 /// \brief Return the attribute's value as a string. This requires the
180 /// attribute to be a string attribute.
181 StringRef getValueAsString() const;
183 /// \brief Returns the alignment field of an attribute as a byte alignment
185 unsigned getAlignment() const;
187 /// \brief Returns the stack alignment field of an attribute as a byte
189 unsigned getStackAlignment() const;
191 /// \brief Returns the number of dereferenceable bytes from the
192 /// dereferenceable attribute (or zero if unknown).
193 uint64_t getDereferenceableBytes() const;
195 /// \brief Returns the number of dereferenceable_or_null bytes from the
196 /// dereferenceable_or_null attribute (or zero if unknown).
197 uint64_t getDereferenceableOrNullBytes() const;
199 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
200 /// is, presumably, for writing out the mnemonics for the assembly writer.
201 std::string getAsString(bool InAttrGrp = false) const;
203 /// \brief Equality and non-equality operators.
204 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
205 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
207 /// \brief Less-than operator. Useful for sorting the attributes list.
208 bool operator<(Attribute A) const;
210 void Profile(FoldingSetNodeID &ID) const {
211 ID.AddPointer(pImpl);
215 //===----------------------------------------------------------------------===//
217 /// \brief This class holds the attributes for a function, its return value, and
218 /// its parameters. You access the attributes for each of them via an index into
219 /// the AttributeSet object. The function attributes are at index
220 /// `AttributeSet::FunctionIndex', the return value is at index
221 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
225 enum AttrIndex : unsigned {
230 friend class AttrBuilder;
231 friend class AttributeSetImpl;
232 template <typename Ty> friend struct DenseMapInfo;
234 /// \brief The attributes that we are managing. This can be null to represent
235 /// the empty attributes list.
236 AttributeSetImpl *pImpl;
238 /// \brief The attributes for the specified index are returned.
239 AttributeSetNode *getAttributes(unsigned Index) const;
241 /// \brief Create an AttributeSet with the specified parameters in it.
242 static AttributeSet get(LLVMContext &C,
243 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
244 static AttributeSet get(LLVMContext &C,
245 ArrayRef<std::pair<unsigned,
246 AttributeSetNode*> > Attrs);
248 static AttributeSet getImpl(LLVMContext &C,
249 ArrayRef<std::pair<unsigned,
250 AttributeSetNode*> > Attrs);
253 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
255 AttributeSet() : pImpl(nullptr) {}
257 //===--------------------------------------------------------------------===//
258 // AttributeSet Construction and Mutation
259 //===--------------------------------------------------------------------===//
261 /// \brief Return an AttributeSet with the specified parameters in it.
262 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
263 static AttributeSet get(LLVMContext &C, unsigned Index,
264 ArrayRef<Attribute::AttrKind> Kind);
265 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
267 /// \brief Add an attribute to the attribute set at the given index. Because
268 /// attribute sets are immutable, this returns a new set.
269 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
270 Attribute::AttrKind Attr) const;
272 /// \brief Add an attribute to the attribute set at the given index. Because
273 /// attribute sets are immutable, this returns a new set.
274 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
275 StringRef Kind) const;
276 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
277 StringRef Kind, StringRef Value) const;
279 /// \brief Add attributes to the attribute set at the given index. Because
280 /// attribute sets are immutable, this returns a new set.
281 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
282 AttributeSet Attrs) const;
284 /// \brief Remove the specified attribute at the specified index from this
285 /// attribute list. Because attribute lists are immutable, this returns the
287 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
288 Attribute::AttrKind Attr) const;
290 /// \brief Remove the specified attributes at the specified index from this
291 /// attribute list. Because attribute lists are immutable, this returns the
293 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
294 AttributeSet Attrs) const;
296 /// \brief Remove the specified attributes at the specified index from this
297 /// attribute list. Because attribute lists are immutable, this returns the
299 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
300 const AttrBuilder &Attrs) const;
302 /// \brief Add the dereferenceable attribute to the attribute set at the given
303 /// index. Because attribute sets are immutable, this returns a new set.
304 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
305 uint64_t Bytes) const;
307 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
308 /// the given index. Because attribute sets are immutable, this returns a new
310 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
311 uint64_t Bytes) const;
313 //===--------------------------------------------------------------------===//
314 // AttributeSet Accessors
315 //===--------------------------------------------------------------------===//
317 /// \brief Retrieve the LLVM context.
318 LLVMContext &getContext() const;
320 /// \brief The attributes for the specified index are returned.
321 AttributeSet getParamAttributes(unsigned Index) const;
323 /// \brief The attributes for the ret value are returned.
324 AttributeSet getRetAttributes() const;
326 /// \brief The function attributes are returned.
327 AttributeSet getFnAttributes() const;
329 /// \brief Return true if the attribute exists at the given index.
330 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
332 /// \brief Return true if the attribute exists at the given index.
333 bool hasAttribute(unsigned Index, StringRef Kind) const;
335 /// \brief Return true if attribute exists at the given index.
336 bool hasAttributes(unsigned Index) const;
338 /// \brief Return true if the specified attribute is set for at least one
339 /// parameter or for the return value.
340 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
342 /// \brief Return the attribute object that exists at the given index.
343 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
345 /// \brief Return the attribute object that exists at the given index.
346 Attribute getAttribute(unsigned Index, StringRef Kind) const;
348 /// \brief Return the alignment for the specified function parameter.
349 unsigned getParamAlignment(unsigned Index) const;
351 /// \brief Get the stack alignment.
352 unsigned getStackAlignment(unsigned Index) const;
354 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
355 uint64_t getDereferenceableBytes(unsigned Index) const;
357 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
359 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
361 /// \brief Return the attributes at the index as a string.
362 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
364 typedef ArrayRef<Attribute>::iterator iterator;
366 iterator begin(unsigned Slot) const;
367 iterator end(unsigned Slot) const;
369 /// operator==/!= - Provide equality predicates.
370 bool operator==(const AttributeSet &RHS) const {
371 return pImpl == RHS.pImpl;
373 bool operator!=(const AttributeSet &RHS) const {
374 return pImpl != RHS.pImpl;
377 //===--------------------------------------------------------------------===//
378 // AttributeSet Introspection
379 //===--------------------------------------------------------------------===//
381 // FIXME: Remove this.
382 uint64_t Raw(unsigned Index) const;
384 /// \brief Return a raw pointer that uniquely identifies this attribute list.
385 void *getRawPointer() const {
389 /// \brief Return true if there are no attributes.
390 bool isEmpty() const {
391 return getNumSlots() == 0;
394 /// \brief Return the number of slots used in this attribute list. This is
395 /// the number of arguments that have an attribute set on them (including the
396 /// function itself).
397 unsigned getNumSlots() const;
399 /// \brief Return the index for the given slot.
400 unsigned getSlotIndex(unsigned Slot) const;
402 /// \brief Return the attributes at the given slot.
403 AttributeSet getSlotAttributes(unsigned Slot) const;
408 //===----------------------------------------------------------------------===//
410 /// \brief Provide DenseMapInfo for AttributeSet.
411 template<> struct DenseMapInfo<AttributeSet> {
412 static inline AttributeSet getEmptyKey() {
413 uintptr_t Val = static_cast<uintptr_t>(-1);
414 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
415 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
417 static inline AttributeSet getTombstoneKey() {
418 uintptr_t Val = static_cast<uintptr_t>(-2);
419 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
420 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
422 static unsigned getHashValue(AttributeSet AS) {
423 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
424 (unsigned((uintptr_t)AS.pImpl) >> 9);
426 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
429 //===----------------------------------------------------------------------===//
431 /// \brief This class is used in conjunction with the Attribute::get method to
432 /// create an Attribute object. The object itself is uniquified. The Builder's
433 /// value, however, is not. So this can be used as a quick way to test for
434 /// equality, presence of attributes, etc.
436 std::bitset<Attribute::EndAttrKinds> Attrs;
437 std::map<std::string, std::string> TargetDepAttrs;
439 uint64_t StackAlignment;
441 uint64_t DerefOrNullBytes;
444 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
445 DerefOrNullBytes(0) {}
446 explicit AttrBuilder(uint64_t Val)
447 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
448 DerefOrNullBytes(0) {
451 AttrBuilder(const Attribute &A)
452 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
453 DerefOrNullBytes(0) {
456 AttrBuilder(AttributeSet AS, unsigned Idx);
460 /// \brief Add an attribute to the builder.
461 AttrBuilder &addAttribute(Attribute::AttrKind Val);
463 /// \brief Add the Attribute object to the builder.
464 AttrBuilder &addAttribute(Attribute A);
466 /// \brief Add the target-dependent attribute to the builder.
467 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
469 /// \brief Remove an attribute from the builder.
470 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
472 /// \brief Remove the attributes from the builder.
473 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
475 /// \brief Remove the target-dependent attribute to the builder.
476 AttrBuilder &removeAttribute(StringRef A);
478 /// \brief Add the attributes from the builder.
479 AttrBuilder &merge(const AttrBuilder &B);
481 /// \brief Remove the attributes from the builder.
482 AttrBuilder &remove(const AttrBuilder &B);
484 /// \brief Return true if the builder has any attribute that's in the
485 /// specified builder.
486 bool overlaps(const AttrBuilder &B) const;
488 /// \brief Return true if the builder has the specified attribute.
489 bool contains(Attribute::AttrKind A) const {
490 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
494 /// \brief Return true if the builder has the specified target-dependent
496 bool contains(StringRef A) const;
498 /// \brief Return true if the builder has IR-level attributes.
499 bool hasAttributes() const;
501 /// \brief Return true if the builder has any attribute that's in the
502 /// specified attribute.
503 bool hasAttributes(AttributeSet A, uint64_t Index) const;
505 /// \brief Return true if the builder has an alignment attribute.
506 bool hasAlignmentAttr() const;
508 /// \brief Retrieve the alignment attribute, if it exists.
509 uint64_t getAlignment() const { return Alignment; }
511 /// \brief Retrieve the stack alignment attribute, if it exists.
512 uint64_t getStackAlignment() const { return StackAlignment; }
514 /// \brief Retrieve the number of dereferenceable bytes, if the dereferenceable
515 /// attribute exists (zero is returned otherwise).
516 uint64_t getDereferenceableBytes() const { return DerefBytes; }
518 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
519 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
520 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
522 /// \brief This turns an int alignment (which must be a power of 2) into the
523 /// form used internally in Attribute.
524 AttrBuilder &addAlignmentAttr(unsigned Align);
526 /// \brief This turns an int stack alignment (which must be a power of 2) into
527 /// the form used internally in Attribute.
528 AttrBuilder &addStackAlignmentAttr(unsigned Align);
530 /// \brief This turns the number of dereferenceable bytes into the form used
531 /// internally in Attribute.
532 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
534 /// \brief This turns the number of dereferenceable_or_null bytes into the
535 /// form used internally in Attribute.
536 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
538 /// \brief Return true if the builder contains no target-independent
540 bool empty() const { return Attrs.none(); }
542 // Iterators for target-dependent attributes.
543 typedef std::pair<std::string, std::string> td_type;
544 typedef std::map<std::string, std::string>::iterator td_iterator;
545 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
546 typedef llvm::iterator_range<td_iterator> td_range;
547 typedef llvm::iterator_range<td_const_iterator> td_const_range;
549 td_iterator td_begin() { return TargetDepAttrs.begin(); }
550 td_iterator td_end() { return TargetDepAttrs.end(); }
552 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
553 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
555 td_range td_attrs() { return td_range(td_begin(), td_end()); }
556 td_const_range td_attrs() const {
557 return td_const_range(td_begin(), td_end());
560 bool td_empty() const { return TargetDepAttrs.empty(); }
562 bool operator==(const AttrBuilder &B);
563 bool operator!=(const AttrBuilder &B) {
564 return !(*this == B);
567 // FIXME: Remove this in 4.0.
569 /// \brief Add the raw value to the internal representation.
570 AttrBuilder &addRawValue(uint64_t Val);
573 namespace AttributeFuncs {
575 /// \brief Which attributes cannot be applied to a type.
576 AttrBuilder typeIncompatible(Type *Ty);
578 } // end AttributeFuncs namespace
580 } // end llvm namespace