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 /// 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-c/Types.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/Support/PointerLikeTypeTraits.h"
38 class AttributeListImpl;
39 class AttributeSetNode;
40 template<typename T> struct DenseMapInfo;
45 //===----------------------------------------------------------------------===//
47 /// Functions, function parameters, and return types can have attributes
48 /// to indicate how they should be treated by optimizations and code
49 /// generation. This class represents one of those attributes. It's light-weight
50 /// and should be passed around by-value.
53 /// This enumeration lists the attributes that can be associated with
54 /// parameters, function results, or the function itself.
56 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
57 /// entry in the unwind table. The `nounwind' attribute is about an exception
58 /// passing by the function.
60 /// In a theoretical system that uses tables for profiling and SjLj for
61 /// exceptions, they would be fully independent. In a normal system that uses
62 /// tables for both, the semantics are:
64 /// nil = Needs an entry because an exception might pass by.
65 /// nounwind = No need for an entry
66 /// uwtable = Needs an entry because the ABI says so and because
67 /// an exception might pass by.
68 /// uwtable + nounwind = Needs an entry because the ABI says so.
71 // IR-Level Attributes
72 None, ///< No attributes have been set
74 #include "llvm/IR/Attributes.inc"
75 EndAttrKinds ///< Sentinal value useful for loops
79 AttributeImpl *pImpl = nullptr;
81 Attribute(AttributeImpl *A) : pImpl(A) {}
84 Attribute() = default;
86 //===--------------------------------------------------------------------===//
87 // Attribute Construction
88 //===--------------------------------------------------------------------===//
90 /// Return a uniquified Attribute object.
91 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
92 static Attribute get(LLVMContext &Context, StringRef Kind,
93 StringRef Val = StringRef());
95 /// Return a uniquified Attribute object that has the specific
97 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
98 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
99 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
101 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
103 static Attribute getWithAllocSizeArgs(LLVMContext &Context,
104 unsigned ElemSizeArg,
105 const Optional<unsigned> &NumElemsArg);
107 //===--------------------------------------------------------------------===//
108 // Attribute Accessors
109 //===--------------------------------------------------------------------===//
111 /// Return true if the attribute is an Attribute::AttrKind type.
112 bool isEnumAttribute() const;
114 /// Return true if the attribute is an integer attribute.
115 bool isIntAttribute() const;
117 /// Return true if the attribute is a string (target-dependent)
119 bool isStringAttribute() const;
121 /// Return true if the attribute is present.
122 bool hasAttribute(AttrKind Val) const;
124 /// Return true if the target-dependent attribute is present.
125 bool hasAttribute(StringRef Val) const;
127 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
128 /// requires the attribute to be an enum or integer attribute.
129 Attribute::AttrKind getKindAsEnum() const;
131 /// Return the attribute's value as an integer. This requires that the
132 /// attribute be an integer attribute.
133 uint64_t getValueAsInt() const;
135 /// Return the attribute's kind as a string. This requires the
136 /// attribute to be a string attribute.
137 StringRef getKindAsString() const;
139 /// Return the attribute's value as a string. This requires the
140 /// attribute to be a string attribute.
141 StringRef getValueAsString() const;
143 /// Returns the alignment field of an attribute as a byte alignment
145 unsigned getAlignment() const;
147 /// Returns the stack alignment field of an attribute as a byte
149 unsigned getStackAlignment() const;
151 /// Returns the number of dereferenceable bytes from the
152 /// dereferenceable attribute.
153 uint64_t getDereferenceableBytes() const;
155 /// Returns the number of dereferenceable_or_null bytes from the
156 /// dereferenceable_or_null attribute.
157 uint64_t getDereferenceableOrNullBytes() const;
159 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
161 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
163 /// The Attribute is converted to a string of equivalent mnemonic. This
164 /// is, presumably, for writing out the mnemonics for the assembly writer.
165 std::string getAsString(bool InAttrGrp = false) const;
167 /// Equality and non-equality operators.
168 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
169 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
171 /// Less-than operator. Useful for sorting the attributes list.
172 bool operator<(Attribute A) const;
174 void Profile(FoldingSetNodeID &ID) const {
175 ID.AddPointer(pImpl);
178 /// Return a raw pointer that uniquely identifies this attribute.
179 void *getRawPointer() const {
183 /// Get an attribute from a raw pointer created by getRawPointer.
184 static Attribute fromRawPointer(void *RawPtr) {
185 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
189 // Specialized opaque value conversions.
190 inline LLVMAttributeRef wrap(Attribute Attr) {
191 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
194 // Specialized opaque value conversions.
195 inline Attribute unwrap(LLVMAttributeRef Attr) {
196 return Attribute::fromRawPointer(Attr);
199 //===----------------------------------------------------------------------===//
201 /// This class holds the attributes for a particular argument, parameter,
202 /// function, or return value. It is an immutable value type that is cheap to
203 /// copy. Adding and removing enum attributes is intended to be fast, but adding
204 /// and removing string or integer attributes involves a FoldingSet lookup.
206 friend AttributeListImpl;
207 template <typename Ty> friend struct DenseMapInfo;
209 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
210 // This will allow an efficient implementation of addAttribute and
211 // removeAttribute for enum attrs.
213 /// Private implementation pointer.
214 AttributeSetNode *SetNode = nullptr;
217 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
220 /// AttributeSet is a trivially copyable value type.
221 AttributeSet() = default;
222 AttributeSet(const AttributeSet &) = default;
223 ~AttributeSet() = default;
225 static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
226 static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
228 bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
229 bool operator!=(const AttributeSet &O) const { return !(*this == O); }
231 /// Add an argument attribute. Returns a new set because attribute sets are
233 AttributeSet addAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
235 /// Add a target-dependent attribute. Returns a new set because attribute sets
237 AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
238 StringRef Value = StringRef()) const;
240 /// Add attributes to the attribute set. Returns a new set because attribute
241 /// sets are immutable.
242 AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const;
244 /// Remove the specified attribute from this set. Returns a new set because
245 /// attribute sets are immutable.
246 AttributeSet removeAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
248 /// Remove the specified attribute from this set. Returns a new set because
249 /// attribute sets are immutable.
250 AttributeSet removeAttribute(LLVMContext &C, StringRef Kind) const;
252 /// Remove the specified attributes from this set. Returns a new set because
253 /// attribute sets are immutable.
254 AttributeSet removeAttributes(LLVMContext &C,
255 const AttrBuilder &AttrsToRemove) const;
257 /// Return the number of attributes in this set.
258 unsigned getNumAttributes() const;
260 /// Return true if attributes exists in this set.
261 bool hasAttributes() const { return SetNode != nullptr; }
263 /// Return true if the attribute exists in this set.
264 bool hasAttribute(Attribute::AttrKind Kind) const;
266 /// Return true if the attribute exists in this set.
267 bool hasAttribute(StringRef Kind) const;
269 /// Return the attribute object.
270 Attribute getAttribute(Attribute::AttrKind Kind) const;
272 /// Return the target-dependent attribute object.
273 Attribute getAttribute(StringRef Kind) const;
275 unsigned getAlignment() const;
276 unsigned getStackAlignment() const;
277 uint64_t getDereferenceableBytes() const;
278 uint64_t getDereferenceableOrNullBytes() const;
279 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
280 std::string getAsString(bool InAttrGrp = false) const;
282 using iterator = const Attribute *;
284 iterator begin() const;
285 iterator end() const;
286 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
291 //===----------------------------------------------------------------------===//
293 /// Provide DenseMapInfo for AttributeSet.
294 template <> struct DenseMapInfo<AttributeSet> {
295 static AttributeSet getEmptyKey() {
296 auto Val = static_cast<uintptr_t>(-1);
297 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
298 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
301 static AttributeSet getTombstoneKey() {
302 auto Val = static_cast<uintptr_t>(-2);
303 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
304 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
307 static unsigned getHashValue(AttributeSet AS) {
308 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
309 (unsigned((uintptr_t)AS.SetNode) >> 9);
312 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
315 //===----------------------------------------------------------------------===//
317 /// This class holds the attributes for a function, its return value, and
318 /// its parameters. You access the attributes for each of them via an index into
319 /// the AttributeList object. The function attributes are at index
320 /// `AttributeList::FunctionIndex', the return value is at index
321 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
322 /// index `AttributeList::FirstArgIndex'.
323 class AttributeList {
325 enum AttrIndex : unsigned {
332 friend class AttrBuilder;
333 friend class AttributeListImpl;
334 friend class AttributeSet;
335 friend class AttributeSetNode;
336 template <typename Ty> friend struct DenseMapInfo;
338 /// The attributes that we are managing. This can be null to represent
339 /// the empty attributes list.
340 AttributeListImpl *pImpl = nullptr;
343 /// Create an AttributeList with the specified parameters in it.
344 static AttributeList get(LLVMContext &C,
345 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
346 static AttributeList get(LLVMContext &C,
347 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
349 /// Create an AttributeList from attribute sets for a function, its
350 /// return value, and all of its arguments.
351 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
352 AttributeSet RetAttrs,
353 ArrayRef<AttributeSet> ArgAttrs);
356 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
358 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
361 AttributeList() = default;
363 //===--------------------------------------------------------------------===//
364 // AttributeList Construction and Mutation
365 //===--------------------------------------------------------------------===//
367 /// Return an AttributeList with the specified parameters in it.
368 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
369 static AttributeList get(LLVMContext &C, unsigned Index,
370 ArrayRef<Attribute::AttrKind> Kinds);
371 static AttributeList get(LLVMContext &C, unsigned Index,
372 ArrayRef<StringRef> Kind);
373 static AttributeList get(LLVMContext &C, unsigned Index,
374 const AttrBuilder &B);
376 /// Add an attribute to the attribute set at the given index.
377 /// Returns a new list because attribute lists are immutable.
378 AttributeList addAttribute(LLVMContext &C, unsigned Index,
379 Attribute::AttrKind Kind) const;
381 /// Add an attribute to the attribute set at the given index.
382 /// Returns a new list because attribute lists are immutable.
383 AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
384 StringRef Value = StringRef()) const;
386 /// Add an attribute to the attribute set at the given index.
387 /// Returns a new list because attribute lists are immutable.
388 AttributeList addAttribute(LLVMContext &C, unsigned Index, Attribute A) const;
390 /// Add attributes to the attribute set at the given index.
391 /// Returns a new list because attribute lists are immutable.
392 AttributeList addAttributes(LLVMContext &C, unsigned Index,
393 const AttrBuilder &B) const;
395 /// Add an argument attribute to the list. Returns a new list because
396 /// attribute lists are immutable.
397 AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
398 Attribute::AttrKind Kind) const {
399 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
402 /// Add an argument attribute to the list. Returns a new list because
403 /// attribute lists are immutable.
404 AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
406 StringRef Value = StringRef()) const {
407 return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
410 /// Add an attribute to the attribute list at the given arg indices. Returns a
411 /// new list because attribute lists are immutable.
412 AttributeList addParamAttribute(LLVMContext &C, ArrayRef<unsigned> ArgNos,
415 /// Add an argument attribute to the list. Returns a new list because
416 /// attribute lists are immutable.
417 AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo,
418 const AttrBuilder &B) const {
419 return addAttributes(C, ArgNo + FirstArgIndex, B);
422 /// Remove the specified attribute at the specified index from this
423 /// attribute list. Returns a new list because attribute lists are immutable.
424 AttributeList removeAttribute(LLVMContext &C, unsigned Index,
425 Attribute::AttrKind Kind) const;
427 /// Remove the specified attribute at the specified index from this
428 /// attribute list. Returns a new list because attribute lists are immutable.
429 AttributeList removeAttribute(LLVMContext &C, unsigned Index,
430 StringRef Kind) const;
432 /// Remove the specified attributes at the specified index from this
433 /// attribute list. Returns a new list because attribute lists are immutable.
434 AttributeList removeAttributes(LLVMContext &C, unsigned Index,
435 const AttrBuilder &AttrsToRemove) const;
437 /// Remove all attributes at the specified index from this
438 /// attribute list. Returns a new list because attribute lists are immutable.
439 AttributeList removeAttributes(LLVMContext &C, unsigned Index) const;
441 /// Remove the specified attribute at the specified arg index from this
442 /// attribute list. Returns a new list because attribute lists are immutable.
443 AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
444 Attribute::AttrKind Kind) const {
445 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
448 /// Remove the specified attribute at the specified arg index from this
449 /// attribute list. Returns a new list because attribute lists are immutable.
450 AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
451 StringRef Kind) const {
452 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
455 /// Remove the specified attribute at the specified arg index from this
456 /// attribute list. Returns a new list because attribute lists are immutable.
457 AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo,
458 const AttrBuilder &AttrsToRemove) const {
459 return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
462 /// Remove all attributes at the specified arg index from this
463 /// attribute list. Returns a new list because attribute lists are immutable.
464 AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo) const {
465 return removeAttributes(C, ArgNo + FirstArgIndex);
468 /// \brief Add the dereferenceable attribute to the attribute set at the given
469 /// index. Returns a new list because attribute lists are immutable.
470 AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index,
471 uint64_t Bytes) const;
473 /// \brief Add the dereferenceable attribute to the attribute set at the given
474 /// arg index. Returns a new list because attribute lists are immutable.
475 AttributeList addDereferenceableParamAttr(LLVMContext &C, unsigned ArgNo,
476 uint64_t Bytes) const {
477 return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
480 /// Add the dereferenceable_or_null attribute to the attribute set at
481 /// the given index. Returns a new list because attribute lists are immutable.
482 AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
483 uint64_t Bytes) const;
485 /// Add the dereferenceable_or_null attribute to the attribute set at
486 /// the given arg index. Returns a new list because attribute lists are
488 AttributeList addDereferenceableOrNullParamAttr(LLVMContext &C,
490 uint64_t Bytes) const {
491 return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
494 /// Add the allocsize attribute to the attribute set at the given index.
495 /// Returns a new list because attribute lists are immutable.
496 AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index,
497 unsigned ElemSizeArg,
498 const Optional<unsigned> &NumElemsArg);
500 /// Add the allocsize attribute to the attribute set at the given arg index.
501 /// Returns a new list because attribute lists are immutable.
502 AttributeList addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo,
503 unsigned ElemSizeArg,
504 const Optional<unsigned> &NumElemsArg) {
505 return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
508 //===--------------------------------------------------------------------===//
509 // AttributeList Accessors
510 //===--------------------------------------------------------------------===//
512 /// Retrieve the LLVM context.
513 LLVMContext &getContext() const;
515 /// The attributes for the specified index are returned.
516 AttributeSet getAttributes(unsigned Index) const;
518 /// The attributes for the argument or parameter at the given index are
520 AttributeSet getParamAttributes(unsigned ArgNo) const;
522 /// The attributes for the ret value are returned.
523 AttributeSet getRetAttributes() const;
525 /// The function attributes are returned.
526 AttributeSet getFnAttributes() const;
528 /// Return true if the attribute exists at the given index.
529 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
531 /// Return true if the attribute exists at the given index.
532 bool hasAttribute(unsigned Index, StringRef Kind) const;
534 /// Return true if attribute exists at the given index.
535 bool hasAttributes(unsigned Index) const;
537 /// Return true if the attribute exists for the given argument
538 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
539 return hasAttribute(ArgNo + FirstArgIndex, Kind);
542 /// Return true if the attribute exists for the given argument
543 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
544 return hasAttribute(ArgNo + FirstArgIndex, Kind);
547 /// Return true if attributes exists for the given argument
548 bool hasParamAttrs(unsigned ArgNo) const {
549 return hasAttributes(ArgNo + FirstArgIndex);
552 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
554 bool hasFnAttribute(Attribute::AttrKind Kind) const;
556 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
558 bool hasFnAttribute(StringRef Kind) const;
560 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
561 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
563 /// Return true if the specified attribute is set for at least one
564 /// parameter or for the return value. If Index is not nullptr, the index
565 /// of a parameter with the specified attribute is provided.
566 bool hasAttrSomewhere(Attribute::AttrKind Kind,
567 unsigned *Index = nullptr) const;
569 /// Return the attribute object that exists at the given index.
570 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
572 /// Return the attribute object that exists at the given index.
573 Attribute getAttribute(unsigned Index, StringRef Kind) const;
575 /// Return the attribute object that exists at the arg index.
576 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
577 return getAttribute(ArgNo + FirstArgIndex, Kind);
580 /// Return the attribute object that exists at the given index.
581 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
582 return getAttribute(ArgNo + FirstArgIndex, Kind);
585 /// Return the alignment of the return value.
586 unsigned getRetAlignment() const;
588 /// Return the alignment for the specified function parameter.
589 unsigned getParamAlignment(unsigned ArgNo) const;
591 /// Get the stack alignment.
592 unsigned getStackAlignment(unsigned Index) const;
594 /// Get the number of dereferenceable bytes (or zero if unknown).
595 uint64_t getDereferenceableBytes(unsigned Index) const;
597 /// Get the number of dereferenceable bytes (or zero if unknown) of an
599 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
600 return getDereferenceableBytes(ArgNo + FirstArgIndex);
603 /// Get the number of dereferenceable_or_null bytes (or zero if
605 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
607 /// Get the number of dereferenceable_or_null bytes (or zero if
608 /// unknown) of an arg.
609 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
610 return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
613 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
614 std::pair<unsigned, Optional<unsigned>>
615 getAllocSizeArgs(unsigned Index) const;
617 /// Return the attributes at the index as a string.
618 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
620 //===--------------------------------------------------------------------===//
621 // AttributeList Introspection
622 //===--------------------------------------------------------------------===//
624 using iterator = const AttributeSet *;
626 iterator begin() const;
627 iterator end() const;
629 unsigned getNumAttrSets() const;
631 /// Use these to iterate over the valid attribute indices.
632 unsigned index_begin() const { return AttributeList::FunctionIndex; }
633 unsigned index_end() const { return getNumAttrSets() - 1; }
635 /// operator==/!= - Provide equality predicates.
636 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
637 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
639 /// Return a raw pointer that uniquely identifies this attribute list.
640 void *getRawPointer() const {
644 /// Return true if there are no attributes.
645 bool isEmpty() const { return pImpl == nullptr; }
650 //===----------------------------------------------------------------------===//
652 /// Provide DenseMapInfo for AttributeList.
653 template <> struct DenseMapInfo<AttributeList> {
654 static AttributeList getEmptyKey() {
655 auto Val = static_cast<uintptr_t>(-1);
656 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
657 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
660 static AttributeList getTombstoneKey() {
661 auto Val = static_cast<uintptr_t>(-2);
662 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
663 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
666 static unsigned getHashValue(AttributeList AS) {
667 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
668 (unsigned((uintptr_t)AS.pImpl) >> 9);
671 static bool isEqual(AttributeList LHS, AttributeList RHS) {
676 //===----------------------------------------------------------------------===//
678 /// This class is used in conjunction with the Attribute::get method to
679 /// create an Attribute object. The object itself is uniquified. The Builder's
680 /// value, however, is not. So this can be used as a quick way to test for
681 /// equality, presence of attributes, etc.
683 std::bitset<Attribute::EndAttrKinds> Attrs;
684 std::map<std::string, std::string> TargetDepAttrs;
685 uint64_t Alignment = 0;
686 uint64_t StackAlignment = 0;
687 uint64_t DerefBytes = 0;
688 uint64_t DerefOrNullBytes = 0;
689 uint64_t AllocSizeArgs = 0;
692 AttrBuilder() = default;
694 AttrBuilder(const Attribute &A) {
698 AttrBuilder(AttributeList AS, unsigned Idx);
699 AttrBuilder(AttributeSet AS);
703 /// Add an attribute to the builder.
704 AttrBuilder &addAttribute(Attribute::AttrKind Val);
706 /// Add the Attribute object to the builder.
707 AttrBuilder &addAttribute(Attribute A);
709 /// Add the target-dependent attribute to the builder.
710 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
712 /// Remove an attribute from the builder.
713 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
715 /// Remove the attributes from the builder.
716 AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
718 /// Remove the target-dependent attribute to the builder.
719 AttrBuilder &removeAttribute(StringRef A);
721 /// Add the attributes from the builder.
722 AttrBuilder &merge(const AttrBuilder &B);
724 /// Remove the attributes from the builder.
725 AttrBuilder &remove(const AttrBuilder &B);
727 /// Return true if the builder has any attribute that's in the
728 /// specified builder.
729 bool overlaps(const AttrBuilder &B) const;
731 /// Return true if the builder has the specified attribute.
732 bool contains(Attribute::AttrKind A) const {
733 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
737 /// Return true if the builder has the specified target-dependent
739 bool contains(StringRef A) const;
741 /// Return true if the builder has IR-level attributes.
742 bool hasAttributes() const;
744 /// Return true if the builder has any attribute that's in the
745 /// specified attribute.
746 bool hasAttributes(AttributeList A, uint64_t Index) const;
748 /// Return true if the builder has an alignment attribute.
749 bool hasAlignmentAttr() const;
751 /// Retrieve the alignment attribute, if it exists.
752 uint64_t getAlignment() const { return Alignment; }
754 /// Retrieve the stack alignment attribute, if it exists.
755 uint64_t getStackAlignment() const { return StackAlignment; }
757 /// Retrieve the number of dereferenceable bytes, if the
758 /// dereferenceable attribute exists (zero is returned otherwise).
759 uint64_t getDereferenceableBytes() const { return DerefBytes; }
761 /// Retrieve the number of dereferenceable_or_null bytes, if the
762 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
763 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
765 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
766 /// doesn't exist, pair(0, 0) is returned.
767 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
769 /// This turns an int alignment (which must be a power of 2) into the
770 /// form used internally in Attribute.
771 AttrBuilder &addAlignmentAttr(unsigned Align);
773 /// This turns an int stack alignment (which must be a power of 2) into
774 /// the form used internally in Attribute.
775 AttrBuilder &addStackAlignmentAttr(unsigned Align);
777 /// This turns the number of dereferenceable bytes into the form used
778 /// internally in Attribute.
779 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
781 /// This turns the number of dereferenceable_or_null bytes into the
782 /// form used internally in Attribute.
783 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
785 /// This turns one (or two) ints into the form used internally in Attribute.
786 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
787 const Optional<unsigned> &NumElemsArg);
789 /// Add an allocsize attribute, using the representation returned by
790 /// Attribute.getIntValue().
791 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
793 /// Return true if the builder contains no target-independent
795 bool empty() const { return Attrs.none(); }
797 // Iterators for target-dependent attributes.
798 using td_type = std::pair<std::string, std::string>;
799 using td_iterator = std::map<std::string, std::string>::iterator;
800 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
801 using td_range = iterator_range<td_iterator>;
802 using td_const_range = iterator_range<td_const_iterator>;
804 td_iterator td_begin() { return TargetDepAttrs.begin(); }
805 td_iterator td_end() { return TargetDepAttrs.end(); }
807 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
808 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
810 td_range td_attrs() { return td_range(td_begin(), td_end()); }
812 td_const_range td_attrs() const {
813 return td_const_range(td_begin(), td_end());
816 bool td_empty() const { return TargetDepAttrs.empty(); }
818 bool operator==(const AttrBuilder &B);
819 bool operator!=(const AttrBuilder &B) {
820 return !(*this == B);
824 namespace AttributeFuncs {
826 /// Which attributes cannot be applied to a type.
827 AttrBuilder typeIncompatible(Type *Ty);
829 /// \returns Return true if the two functions have compatible target-independent
830 /// attributes for inlining purposes.
831 bool areInlineCompatible(const Function &Caller, const Function &Callee);
833 /// Merge caller's and callee's attributes.
834 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
836 } // end namespace AttributeFuncs
838 } // end namespace llvm
840 #endif // LLVM_IR_ATTRIBUTES_H