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 ArgMemOnly, ///< Funciton can access memory only using pointers
72 ///< based on its arguments.
73 Builtin, ///< Callee is recognized as a builtin, despite
74 ///< nobuiltin attribute on its declaration.
75 ByVal, ///< Pass structure by value
76 Cold, ///< Marks function as being in a cold path.
77 Convergent, ///< Can only be moved to control-equivalent blocks
78 Dereferenceable, ///< Pointer is known to be dereferenceable
79 DereferenceableOrNull, ///< Pointer is either null or dereferenceable
80 InAlloca, ///< Pass structure in an alloca
81 InlineHint, ///< Source said inlining was desirable
82 InReg, ///< Force argument to be passed in register
83 JumpTable, ///< Build jump-instruction tables and replace refs.
84 MinSize, ///< Function must be optimized for size first
85 Naked, ///< Naked function
86 Nest, ///< Nested function static chain
87 NoAlias, ///< Considered to not alias after call
88 NoBuiltin, ///< Callee isn't recognized as a builtin
89 NoCapture, ///< Function creates no aliases of pointer
90 NoDuplicate, ///< Call cannot be duplicated
91 NoImplicitFloat, ///< Disable implicit floating point insts
92 NoInline, ///< inline=never
93 NonLazyBind, ///< Function is called early and/or
94 ///< often, so lazy binding isn't worthwhile
95 NonNull, ///< Pointer is known to be not null
96 NoRecurse, ///< The function does not recurse
97 NoRedZone, ///< Disable redzone
98 NoReturn, ///< Mark the function as not returning
99 NoUnwind, ///< Function doesn't unwind stack
100 OptimizeForSize, ///< opt_size
101 OptimizeNone, ///< Function must not be optimized.
102 ReadNone, ///< Function does not access memory
103 ReadOnly, ///< Function only reads from memory
104 Returned, ///< Return value is always equal to this argument
105 ReturnsTwice, ///< Function can return twice
106 SafeStack, ///< Safe Stack protection.
107 SanitizeAddress, ///< AddressSanitizer is on.
108 SanitizeMemory, ///< MemorySanitizer is on.
109 SanitizeThread, ///< ThreadSanitizer is on.
110 SExt, ///< Sign extended before/after call
111 StackAlignment, ///< Alignment of stack for function (3 bits)
112 ///< stored as log2 of alignment with +1 bias 0
113 ///< means unaligned (different from
115 StackProtect, ///< Stack protection.
116 StackProtectReq, ///< Stack protection required.
117 StackProtectStrong, ///< Strong Stack protection.
118 StructRet, ///< Hidden pointer to structure to return
119 UWTable, ///< Function must be in a unwind table
120 ZExt, ///< Zero extended before/after call
122 EndAttrKinds ///< Sentinal value useful for loops
126 AttributeImpl *pImpl;
127 Attribute(AttributeImpl *A) : pImpl(A) {}
130 Attribute() : pImpl(nullptr) {}
132 //===--------------------------------------------------------------------===//
133 // Attribute Construction
134 //===--------------------------------------------------------------------===//
136 /// \brief Return a uniquified Attribute object.
137 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
138 static Attribute get(LLVMContext &Context, StringRef Kind,
139 StringRef Val = StringRef());
141 /// \brief Return a uniquified Attribute object that has the specific
143 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
144 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
145 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
147 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
150 //===--------------------------------------------------------------------===//
151 // Attribute Accessors
152 //===--------------------------------------------------------------------===//
154 /// \brief Return true if the attribute is an Attribute::AttrKind type.
155 bool isEnumAttribute() const;
157 /// \brief Return true if the attribute is an integer attribute.
158 bool isIntAttribute() const;
160 /// \brief Return true if the attribute is a string (target-dependent)
162 bool isStringAttribute() const;
164 /// \brief Return true if the attribute is present.
165 bool hasAttribute(AttrKind Val) const;
167 /// \brief Return true if the target-dependent attribute is present.
168 bool hasAttribute(StringRef Val) const;
170 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
171 /// requires the attribute to be an enum or alignment attribute.
172 Attribute::AttrKind getKindAsEnum() const;
174 /// \brief Return the attribute's value as an integer. This requires that the
175 /// attribute be an alignment attribute.
176 uint64_t getValueAsInt() const;
178 /// \brief Return the attribute's kind as a string. This requires the
179 /// attribute to be a string attribute.
180 StringRef getKindAsString() const;
182 /// \brief Return the attribute's value as a string. This requires the
183 /// attribute to be a string attribute.
184 StringRef getValueAsString() const;
186 /// \brief Returns the alignment field of an attribute as a byte alignment
188 unsigned getAlignment() const;
190 /// \brief Returns the stack alignment field of an attribute as a byte
192 unsigned getStackAlignment() const;
194 /// \brief Returns the number of dereferenceable bytes from the
195 /// dereferenceable attribute (or zero if unknown).
196 uint64_t getDereferenceableBytes() const;
198 /// \brief Returns the number of dereferenceable_or_null bytes from the
199 /// dereferenceable_or_null attribute (or zero if unknown).
200 uint64_t getDereferenceableOrNullBytes() const;
202 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
203 /// is, presumably, for writing out the mnemonics for the assembly writer.
204 std::string getAsString(bool InAttrGrp = false) const;
206 /// \brief Equality and non-equality operators.
207 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
208 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
210 /// \brief Less-than operator. Useful for sorting the attributes list.
211 bool operator<(Attribute A) const;
213 void Profile(FoldingSetNodeID &ID) const {
214 ID.AddPointer(pImpl);
218 //===----------------------------------------------------------------------===//
220 /// \brief This class holds the attributes for a function, its return value, and
221 /// its parameters. You access the attributes for each of them via an index into
222 /// the AttributeSet object. The function attributes are at index
223 /// `AttributeSet::FunctionIndex', the return value is at index
224 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
228 enum AttrIndex : unsigned {
234 friend class AttrBuilder;
235 friend class AttributeSetImpl;
236 template <typename Ty> friend struct DenseMapInfo;
238 /// \brief The attributes that we are managing. This can be null to represent
239 /// the empty attributes list.
240 AttributeSetImpl *pImpl;
242 /// \brief The attributes for the specified index are returned.
243 AttributeSetNode *getAttributes(unsigned Index) const;
245 /// \brief Create an AttributeSet with the specified parameters in it.
246 static AttributeSet get(LLVMContext &C,
247 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
248 static AttributeSet get(LLVMContext &C,
249 ArrayRef<std::pair<unsigned,
250 AttributeSetNode*> > Attrs);
252 static AttributeSet getImpl(LLVMContext &C,
253 ArrayRef<std::pair<unsigned,
254 AttributeSetNode*> > Attrs);
256 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
259 AttributeSet() : pImpl(nullptr) {}
261 //===--------------------------------------------------------------------===//
262 // AttributeSet Construction and Mutation
263 //===--------------------------------------------------------------------===//
265 /// \brief Return an AttributeSet with the specified parameters in it.
266 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
267 static AttributeSet get(LLVMContext &C, unsigned Index,
268 ArrayRef<Attribute::AttrKind> Kind);
269 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
271 /// \brief Add an attribute to the attribute set at the given index. Because
272 /// attribute sets are immutable, this returns a new set.
273 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
274 Attribute::AttrKind Attr) const;
276 /// \brief Add an attribute to the attribute set at the given index. Because
277 /// attribute sets are immutable, this returns a new set.
278 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
279 StringRef Kind) const;
280 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
281 StringRef Kind, StringRef Value) const;
283 /// \brief Add attributes to the attribute set at the given index. Because
284 /// attribute sets are immutable, this returns a new set.
285 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
286 AttributeSet Attrs) const;
288 /// \brief Remove the specified attribute at the specified index from this
289 /// attribute list. Because attribute lists are immutable, this returns the
291 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
292 Attribute::AttrKind Attr) const;
294 /// \brief Remove the specified attributes at the specified index from this
295 /// attribute list. Because attribute lists are immutable, this returns the
297 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
298 AttributeSet Attrs) const;
300 /// \brief Remove the specified attributes at the specified index from this
301 /// attribute list. Because attribute lists are immutable, this returns the
303 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
304 const AttrBuilder &Attrs) const;
306 /// \brief Add the dereferenceable attribute to the attribute set at the given
307 /// index. Because attribute sets are immutable, this returns a new set.
308 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
309 uint64_t Bytes) const;
311 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
312 /// the given index. Because attribute sets are immutable, this returns a new
314 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
315 uint64_t Bytes) const;
317 //===--------------------------------------------------------------------===//
318 // AttributeSet Accessors
319 //===--------------------------------------------------------------------===//
321 /// \brief Retrieve the LLVM context.
322 LLVMContext &getContext() const;
324 /// \brief The attributes for the specified index are returned.
325 AttributeSet getParamAttributes(unsigned Index) const;
327 /// \brief The attributes for the ret value are returned.
328 AttributeSet getRetAttributes() const;
330 /// \brief The function attributes are returned.
331 AttributeSet getFnAttributes() const;
333 /// \brief Return true if the attribute exists at the given index.
334 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
336 /// \brief Return true if the attribute exists at the given index.
337 bool hasAttribute(unsigned Index, StringRef Kind) const;
339 /// \brief Return true if attribute exists at the given index.
340 bool hasAttributes(unsigned Index) const;
342 /// \brief Return true if the specified attribute is set for at least one
343 /// parameter or for the return value.
344 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
346 /// \brief Return the attribute object that exists at the given index.
347 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
349 /// \brief Return the attribute object that exists at the given index.
350 Attribute getAttribute(unsigned Index, StringRef Kind) const;
352 /// \brief Return the alignment for the specified function parameter.
353 unsigned getParamAlignment(unsigned Index) const;
355 /// \brief Get the stack alignment.
356 unsigned getStackAlignment(unsigned Index) const;
358 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
359 uint64_t getDereferenceableBytes(unsigned Index) const;
361 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
363 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
365 /// \brief Return the attributes at the index as a string.
366 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
368 typedef ArrayRef<Attribute>::iterator iterator;
370 iterator begin(unsigned Slot) const;
371 iterator end(unsigned Slot) const;
373 /// operator==/!= - Provide equality predicates.
374 bool operator==(const AttributeSet &RHS) const {
375 return pImpl == RHS.pImpl;
377 bool operator!=(const AttributeSet &RHS) const {
378 return pImpl != RHS.pImpl;
381 //===--------------------------------------------------------------------===//
382 // AttributeSet Introspection
383 //===--------------------------------------------------------------------===//
385 // FIXME: Remove this.
386 uint64_t Raw(unsigned Index) const;
388 /// \brief Return a raw pointer that uniquely identifies this attribute list.
389 void *getRawPointer() const {
393 /// \brief Return true if there are no attributes.
394 bool isEmpty() const {
395 return getNumSlots() == 0;
398 /// \brief Return the number of slots used in this attribute list. This is
399 /// the number of arguments that have an attribute set on them (including the
400 /// function itself).
401 unsigned getNumSlots() const;
403 /// \brief Return the index for the given slot.
404 unsigned getSlotIndex(unsigned Slot) const;
406 /// \brief Return the attributes at the given slot.
407 AttributeSet getSlotAttributes(unsigned Slot) const;
412 //===----------------------------------------------------------------------===//
414 /// \brief Provide DenseMapInfo for AttributeSet.
415 template<> struct DenseMapInfo<AttributeSet> {
416 static inline AttributeSet getEmptyKey() {
417 uintptr_t Val = static_cast<uintptr_t>(-1);
418 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
419 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
421 static inline AttributeSet getTombstoneKey() {
422 uintptr_t Val = static_cast<uintptr_t>(-2);
423 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
424 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
426 static unsigned getHashValue(AttributeSet AS) {
427 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
428 (unsigned((uintptr_t)AS.pImpl) >> 9);
430 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
433 //===----------------------------------------------------------------------===//
435 /// \brief This class is used in conjunction with the Attribute::get method to
436 /// create an Attribute object. The object itself is uniquified. The Builder's
437 /// value, however, is not. So this can be used as a quick way to test for
438 /// equality, presence of attributes, etc.
440 std::bitset<Attribute::EndAttrKinds> Attrs;
441 std::map<std::string, std::string> TargetDepAttrs;
443 uint64_t StackAlignment;
445 uint64_t DerefOrNullBytes;
449 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
450 DerefOrNullBytes(0) {}
451 explicit AttrBuilder(uint64_t Val)
452 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
453 DerefOrNullBytes(0) {
456 AttrBuilder(const Attribute &A)
457 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
458 DerefOrNullBytes(0) {
461 AttrBuilder(AttributeSet AS, unsigned Idx);
465 /// \brief Add an attribute to the builder.
466 AttrBuilder &addAttribute(Attribute::AttrKind Val);
468 /// \brief Add the Attribute object to the builder.
469 AttrBuilder &addAttribute(Attribute A);
471 /// \brief Add the target-dependent attribute to the builder.
472 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
474 /// \brief Remove an attribute from the builder.
475 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
477 /// \brief Remove the attributes from the builder.
478 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
480 /// \brief Remove the target-dependent attribute to the builder.
481 AttrBuilder &removeAttribute(StringRef A);
483 /// \brief Add the attributes from the builder.
484 AttrBuilder &merge(const AttrBuilder &B);
486 /// \brief Remove the attributes from the builder.
487 AttrBuilder &remove(const AttrBuilder &B);
489 /// \brief Return true if the builder has any attribute that's in the
490 /// specified builder.
491 bool overlaps(const AttrBuilder &B) const;
493 /// \brief Return true if the builder has the specified attribute.
494 bool contains(Attribute::AttrKind A) const {
495 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
499 /// \brief Return true if the builder has the specified target-dependent
501 bool contains(StringRef A) const;
503 /// \brief Return true if the builder has IR-level attributes.
504 bool hasAttributes() const;
506 /// \brief Return true if the builder has any attribute that's in the
507 /// specified attribute.
508 bool hasAttributes(AttributeSet A, uint64_t Index) const;
510 /// \brief Return true if the builder has an alignment attribute.
511 bool hasAlignmentAttr() const;
513 /// \brief Retrieve the alignment attribute, if it exists.
514 uint64_t getAlignment() const { return Alignment; }
516 /// \brief Retrieve the stack alignment attribute, if it exists.
517 uint64_t getStackAlignment() const { return StackAlignment; }
519 /// \brief Retrieve the number of dereferenceable bytes, if the
520 /// dereferenceable attribute exists (zero is returned otherwise).
521 uint64_t getDereferenceableBytes() const { return DerefBytes; }
523 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
524 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
525 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
527 /// \brief This turns an int alignment (which must be a power of 2) into the
528 /// form used internally in Attribute.
529 AttrBuilder &addAlignmentAttr(unsigned Align);
531 /// \brief This turns an int stack alignment (which must be a power of 2) into
532 /// the form used internally in Attribute.
533 AttrBuilder &addStackAlignmentAttr(unsigned Align);
535 /// \brief This turns the number of dereferenceable bytes into the form used
536 /// internally in Attribute.
537 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
539 /// \brief This turns the number of dereferenceable_or_null bytes into the
540 /// form used internally in Attribute.
541 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
543 /// \brief Return true if the builder contains no target-independent
545 bool empty() const { return Attrs.none(); }
547 // Iterators for target-dependent attributes.
548 typedef std::pair<std::string, std::string> td_type;
549 typedef std::map<std::string, std::string>::iterator td_iterator;
550 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
551 typedef llvm::iterator_range<td_iterator> td_range;
552 typedef llvm::iterator_range<td_const_iterator> td_const_range;
554 td_iterator td_begin() { return TargetDepAttrs.begin(); }
555 td_iterator td_end() { return TargetDepAttrs.end(); }
557 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
558 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
560 td_range td_attrs() { return td_range(td_begin(), td_end()); }
561 td_const_range td_attrs() const {
562 return td_const_range(td_begin(), td_end());
565 bool td_empty() const { return TargetDepAttrs.empty(); }
567 bool operator==(const AttrBuilder &B);
568 bool operator!=(const AttrBuilder &B) {
569 return !(*this == B);
572 // FIXME: Remove this in 4.0.
574 /// \brief Add the raw value to the internal representation.
575 AttrBuilder &addRawValue(uint64_t Val);
578 namespace AttributeFuncs {
580 /// \brief Which attributes cannot be applied to a type.
581 AttrBuilder typeIncompatible(Type *Ty);
583 } // end AttributeFuncs namespace
585 } // end llvm namespace