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 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
234 Attribute::AttrKind Kind) const;
236 /// Add a target-dependent attribute. Returns a new set because attribute sets
238 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
239 StringRef Value = StringRef()) const;
241 /// Add attributes to the attribute set. Returns a new set because attribute
242 /// sets are immutable.
243 LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
244 AttributeSet AS) const;
246 /// Remove the specified attribute from this set. Returns a new set because
247 /// attribute sets are immutable.
248 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
249 Attribute::AttrKind Kind) const;
251 /// Remove the specified attribute from this set. Returns a new set because
252 /// attribute sets are immutable.
253 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
254 StringRef Kind) const;
256 /// Remove the specified attributes from this set. Returns a new set because
257 /// attribute sets are immutable.
258 LLVM_NODISCARD AttributeSet
259 removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
261 /// Return the number of attributes in this set.
262 unsigned getNumAttributes() const;
264 /// Return true if attributes exists in this set.
265 bool hasAttributes() const { return SetNode != nullptr; }
267 /// Return true if the attribute exists in this set.
268 bool hasAttribute(Attribute::AttrKind Kind) const;
270 /// Return true if the attribute exists in this set.
271 bool hasAttribute(StringRef Kind) const;
273 /// Return the attribute object.
274 Attribute getAttribute(Attribute::AttrKind Kind) const;
276 /// Return the target-dependent attribute object.
277 Attribute getAttribute(StringRef Kind) const;
279 unsigned getAlignment() const;
280 unsigned getStackAlignment() const;
281 uint64_t getDereferenceableBytes() const;
282 uint64_t getDereferenceableOrNullBytes() const;
283 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
284 std::string getAsString(bool InAttrGrp = false) const;
286 using iterator = const Attribute *;
288 iterator begin() const;
289 iterator end() const;
290 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
295 //===----------------------------------------------------------------------===//
297 /// Provide DenseMapInfo for AttributeSet.
298 template <> struct DenseMapInfo<AttributeSet> {
299 static AttributeSet getEmptyKey() {
300 auto Val = static_cast<uintptr_t>(-1);
301 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
302 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
305 static AttributeSet getTombstoneKey() {
306 auto Val = static_cast<uintptr_t>(-2);
307 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
308 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
311 static unsigned getHashValue(AttributeSet AS) {
312 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
313 (unsigned((uintptr_t)AS.SetNode) >> 9);
316 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
319 //===----------------------------------------------------------------------===//
321 /// This class holds the attributes for a function, its return value, and
322 /// its parameters. You access the attributes for each of them via an index into
323 /// the AttributeList object. The function attributes are at index
324 /// `AttributeList::FunctionIndex', the return value is at index
325 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
326 /// index `AttributeList::FirstArgIndex'.
327 class AttributeList {
329 enum AttrIndex : unsigned {
336 friend class AttrBuilder;
337 friend class AttributeListImpl;
338 friend class AttributeSet;
339 friend class AttributeSetNode;
340 template <typename Ty> friend struct DenseMapInfo;
342 /// The attributes that we are managing. This can be null to represent
343 /// the empty attributes list.
344 AttributeListImpl *pImpl = nullptr;
347 /// Create an AttributeList with the specified parameters in it.
348 static AttributeList get(LLVMContext &C,
349 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
350 static AttributeList get(LLVMContext &C,
351 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
353 /// Create an AttributeList from attribute sets for a function, its
354 /// return value, and all of its arguments.
355 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
356 AttributeSet RetAttrs,
357 ArrayRef<AttributeSet> ArgAttrs);
360 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
362 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
365 AttributeList() = default;
367 //===--------------------------------------------------------------------===//
368 // AttributeList Construction and Mutation
369 //===--------------------------------------------------------------------===//
371 /// Return an AttributeList with the specified parameters in it.
372 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
373 static AttributeList get(LLVMContext &C, unsigned Index,
374 ArrayRef<Attribute::AttrKind> Kinds);
375 static AttributeList get(LLVMContext &C, unsigned Index,
376 ArrayRef<StringRef> Kind);
377 static AttributeList get(LLVMContext &C, unsigned Index,
378 const AttrBuilder &B);
380 /// Add an attribute to the attribute set at the given index.
381 /// Returns a new list because attribute lists are immutable.
382 LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
383 Attribute::AttrKind Kind) const;
385 /// Add an attribute to the attribute set at the given index.
386 /// Returns a new list because attribute lists are immutable.
387 LLVM_NODISCARD AttributeList
388 addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
389 StringRef Value = StringRef()) const;
391 /// Add an attribute to the attribute set at the given index.
392 /// Returns a new list because attribute lists are immutable.
393 LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
396 /// Add attributes to the attribute set at the given index.
397 /// Returns a new list because attribute lists are immutable.
398 LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
399 const AttrBuilder &B) const;
401 /// Add an argument attribute to the list. Returns a new list because
402 /// attribute lists are immutable.
403 LLVM_NODISCARD AttributeList addParamAttribute(
404 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
405 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
408 /// Add an argument attribute to the list. Returns a new list because
409 /// attribute lists are immutable.
410 LLVM_NODISCARD AttributeList
411 addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
412 StringRef Value = StringRef()) const {
413 return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
416 /// Add an attribute to the attribute list at the given arg indices. Returns a
417 /// new list because attribute lists are immutable.
418 LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
419 ArrayRef<unsigned> ArgNos,
422 /// Add an argument attribute to the list. Returns a new list because
423 /// attribute lists are immutable.
424 LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
426 const AttrBuilder &B) const {
427 return addAttributes(C, ArgNo + FirstArgIndex, B);
430 /// Remove the specified attribute at the specified index from this
431 /// attribute list. Returns a new list because attribute lists are immutable.
432 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
433 Attribute::AttrKind Kind) const;
435 /// Remove the specified attribute at the specified index from this
436 /// attribute list. Returns a new list because attribute lists are immutable.
437 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
438 StringRef Kind) const;
440 /// Remove the specified attributes at the specified index from this
441 /// attribute list. Returns a new list because attribute lists are immutable.
442 LLVM_NODISCARD AttributeList removeAttributes(
443 LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
445 /// Remove all attributes at the specified index from this
446 /// attribute list. Returns a new list because attribute lists are immutable.
447 LLVM_NODISCARD AttributeList removeAttributes(LLVMContext &C,
448 unsigned Index) const;
450 /// Remove the specified attribute at the specified arg index from this
451 /// attribute list. Returns a new list because attribute lists are immutable.
452 LLVM_NODISCARD AttributeList removeParamAttribute(
453 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
454 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
457 /// Remove the specified attribute at the specified arg index from this
458 /// attribute list. Returns a new list because attribute lists are immutable.
459 LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
461 StringRef Kind) const {
462 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
465 /// Remove the specified attribute at the specified arg index from this
466 /// attribute list. Returns a new list because attribute lists are immutable.
467 LLVM_NODISCARD AttributeList removeParamAttributes(
468 LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
469 return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
472 /// Remove all attributes at the specified arg index from this
473 /// attribute list. Returns a new list because attribute lists are immutable.
474 LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
475 unsigned ArgNo) const {
476 return removeAttributes(C, ArgNo + FirstArgIndex);
479 /// \brief Add the dereferenceable attribute to the attribute set at the given
480 /// index. Returns a new list because attribute lists are immutable.
481 LLVM_NODISCARD AttributeList addDereferenceableAttr(LLVMContext &C,
483 uint64_t Bytes) const;
485 /// \brief Add the dereferenceable attribute to the attribute set at the given
486 /// arg index. Returns a new list because attribute lists are immutable.
487 LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
488 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
489 return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
492 /// Add the dereferenceable_or_null attribute to the attribute set at
493 /// the given index. Returns a new list because attribute lists are immutable.
494 LLVM_NODISCARD AttributeList addDereferenceableOrNullAttr(
495 LLVMContext &C, unsigned Index, uint64_t Bytes) const;
497 /// Add the dereferenceable_or_null attribute to the attribute set at
498 /// the given arg index. Returns a new list because attribute lists are
500 LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
501 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
502 return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
505 /// Add the allocsize attribute to the attribute set at the given index.
506 /// Returns a new list because attribute lists are immutable.
507 LLVM_NODISCARD AttributeList
508 addAllocSizeAttr(LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
509 const Optional<unsigned> &NumElemsArg);
511 /// Add the allocsize attribute to the attribute set at the given arg index.
512 /// Returns a new list because attribute lists are immutable.
513 LLVM_NODISCARD AttributeList
514 addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
515 const Optional<unsigned> &NumElemsArg) {
516 return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
519 //===--------------------------------------------------------------------===//
520 // AttributeList Accessors
521 //===--------------------------------------------------------------------===//
523 /// Retrieve the LLVM context.
524 LLVMContext &getContext() const;
526 /// The attributes for the specified index are returned.
527 AttributeSet getAttributes(unsigned Index) const;
529 /// The attributes for the argument or parameter at the given index are
531 AttributeSet getParamAttributes(unsigned ArgNo) const;
533 /// The attributes for the ret value are returned.
534 AttributeSet getRetAttributes() const;
536 /// The function attributes are returned.
537 AttributeSet getFnAttributes() const;
539 /// Return true if the attribute exists at the given index.
540 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
542 /// Return true if the attribute exists at the given index.
543 bool hasAttribute(unsigned Index, StringRef Kind) const;
545 /// Return true if attribute exists at the given index.
546 bool hasAttributes(unsigned Index) const;
548 /// Return true if the attribute exists for the given argument
549 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
550 return hasAttribute(ArgNo + FirstArgIndex, Kind);
553 /// Return true if the attribute exists for the given argument
554 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
555 return hasAttribute(ArgNo + FirstArgIndex, Kind);
558 /// Return true if attributes exists for the given argument
559 bool hasParamAttrs(unsigned ArgNo) const {
560 return hasAttributes(ArgNo + FirstArgIndex);
563 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
565 bool hasFnAttribute(Attribute::AttrKind Kind) const;
567 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
569 bool hasFnAttribute(StringRef Kind) const;
571 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
572 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
574 /// Return true if the specified attribute is set for at least one
575 /// parameter or for the return value. If Index is not nullptr, the index
576 /// of a parameter with the specified attribute is provided.
577 bool hasAttrSomewhere(Attribute::AttrKind Kind,
578 unsigned *Index = nullptr) const;
580 /// Return the attribute object that exists at the given index.
581 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
583 /// Return the attribute object that exists at the given index.
584 Attribute getAttribute(unsigned Index, StringRef Kind) const;
586 /// Return the attribute object that exists at the arg index.
587 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
588 return getAttribute(ArgNo + FirstArgIndex, Kind);
591 /// Return the attribute object that exists at the given index.
592 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
593 return getAttribute(ArgNo + FirstArgIndex, Kind);
596 /// Return the alignment of the return value.
597 unsigned getRetAlignment() const;
599 /// Return the alignment for the specified function parameter.
600 unsigned getParamAlignment(unsigned ArgNo) const;
602 /// Get the stack alignment.
603 unsigned getStackAlignment(unsigned Index) const;
605 /// Get the number of dereferenceable bytes (or zero if unknown).
606 uint64_t getDereferenceableBytes(unsigned Index) const;
608 /// Get the number of dereferenceable bytes (or zero if unknown) of an
610 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
611 return getDereferenceableBytes(ArgNo + FirstArgIndex);
614 /// Get the number of dereferenceable_or_null bytes (or zero if
616 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
618 /// Get the number of dereferenceable_or_null bytes (or zero if
619 /// unknown) of an arg.
620 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
621 return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
624 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
625 std::pair<unsigned, Optional<unsigned>>
626 getAllocSizeArgs(unsigned Index) const;
628 /// Return the attributes at the index as a string.
629 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
631 //===--------------------------------------------------------------------===//
632 // AttributeList Introspection
633 //===--------------------------------------------------------------------===//
635 using iterator = const AttributeSet *;
637 iterator begin() const;
638 iterator end() const;
640 unsigned getNumAttrSets() const;
642 /// Use these to iterate over the valid attribute indices.
643 unsigned index_begin() const { return AttributeList::FunctionIndex; }
644 unsigned index_end() const { return getNumAttrSets() - 1; }
646 /// operator==/!= - Provide equality predicates.
647 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
648 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
650 /// Return a raw pointer that uniquely identifies this attribute list.
651 void *getRawPointer() const {
655 /// Return true if there are no attributes.
656 bool isEmpty() const { return pImpl == nullptr; }
661 //===----------------------------------------------------------------------===//
663 /// Provide DenseMapInfo for AttributeList.
664 template <> struct DenseMapInfo<AttributeList> {
665 static AttributeList getEmptyKey() {
666 auto Val = static_cast<uintptr_t>(-1);
667 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
668 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
671 static AttributeList getTombstoneKey() {
672 auto Val = static_cast<uintptr_t>(-2);
673 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
674 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
677 static unsigned getHashValue(AttributeList AS) {
678 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
679 (unsigned((uintptr_t)AS.pImpl) >> 9);
682 static bool isEqual(AttributeList LHS, AttributeList RHS) {
687 //===----------------------------------------------------------------------===//
689 /// This class is used in conjunction with the Attribute::get method to
690 /// create an Attribute object. The object itself is uniquified. The Builder's
691 /// value, however, is not. So this can be used as a quick way to test for
692 /// equality, presence of attributes, etc.
694 std::bitset<Attribute::EndAttrKinds> Attrs;
695 std::map<std::string, std::string> TargetDepAttrs;
696 uint64_t Alignment = 0;
697 uint64_t StackAlignment = 0;
698 uint64_t DerefBytes = 0;
699 uint64_t DerefOrNullBytes = 0;
700 uint64_t AllocSizeArgs = 0;
703 AttrBuilder() = default;
705 AttrBuilder(const Attribute &A) {
709 AttrBuilder(AttributeList AS, unsigned Idx);
710 AttrBuilder(AttributeSet AS);
714 /// Add an attribute to the builder.
715 AttrBuilder &addAttribute(Attribute::AttrKind Val);
717 /// Add the Attribute object to the builder.
718 AttrBuilder &addAttribute(Attribute A);
720 /// Add the target-dependent attribute to the builder.
721 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
723 /// Remove an attribute from the builder.
724 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
726 /// Remove the attributes from the builder.
727 AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
729 /// Remove the target-dependent attribute to the builder.
730 AttrBuilder &removeAttribute(StringRef A);
732 /// Add the attributes from the builder.
733 AttrBuilder &merge(const AttrBuilder &B);
735 /// Remove the attributes from the builder.
736 AttrBuilder &remove(const AttrBuilder &B);
738 /// Return true if the builder has any attribute that's in the
739 /// specified builder.
740 bool overlaps(const AttrBuilder &B) const;
742 /// Return true if the builder has the specified attribute.
743 bool contains(Attribute::AttrKind A) const {
744 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
748 /// Return true if the builder has the specified target-dependent
750 bool contains(StringRef A) const;
752 /// Return true if the builder has IR-level attributes.
753 bool hasAttributes() const;
755 /// Return true if the builder has any attribute that's in the
756 /// specified attribute.
757 bool hasAttributes(AttributeList A, uint64_t Index) const;
759 /// Return true if the builder has an alignment attribute.
760 bool hasAlignmentAttr() const;
762 /// Retrieve the alignment attribute, if it exists.
763 uint64_t getAlignment() const { return Alignment; }
765 /// Retrieve the stack alignment attribute, if it exists.
766 uint64_t getStackAlignment() const { return StackAlignment; }
768 /// Retrieve the number of dereferenceable bytes, if the
769 /// dereferenceable attribute exists (zero is returned otherwise).
770 uint64_t getDereferenceableBytes() const { return DerefBytes; }
772 /// Retrieve the number of dereferenceable_or_null bytes, if the
773 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
774 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
776 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
777 /// doesn't exist, pair(0, 0) is returned.
778 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
780 /// This turns an int alignment (which must be a power of 2) into the
781 /// form used internally in Attribute.
782 AttrBuilder &addAlignmentAttr(unsigned Align);
784 /// This turns an int stack alignment (which must be a power of 2) into
785 /// the form used internally in Attribute.
786 AttrBuilder &addStackAlignmentAttr(unsigned Align);
788 /// This turns the number of dereferenceable bytes into the form used
789 /// internally in Attribute.
790 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
792 /// This turns the number of dereferenceable_or_null bytes into the
793 /// form used internally in Attribute.
794 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
796 /// This turns one (or two) ints into the form used internally in Attribute.
797 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
798 const Optional<unsigned> &NumElemsArg);
800 /// Add an allocsize attribute, using the representation returned by
801 /// Attribute.getIntValue().
802 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
804 /// Return true if the builder contains no target-independent
806 bool empty() const { return Attrs.none(); }
808 // Iterators for target-dependent attributes.
809 using td_type = std::pair<std::string, std::string>;
810 using td_iterator = std::map<std::string, std::string>::iterator;
811 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
812 using td_range = iterator_range<td_iterator>;
813 using td_const_range = iterator_range<td_const_iterator>;
815 td_iterator td_begin() { return TargetDepAttrs.begin(); }
816 td_iterator td_end() { return TargetDepAttrs.end(); }
818 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
819 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
821 td_range td_attrs() { return td_range(td_begin(), td_end()); }
823 td_const_range td_attrs() const {
824 return td_const_range(td_begin(), td_end());
827 bool td_empty() const { return TargetDepAttrs.empty(); }
829 bool operator==(const AttrBuilder &B);
830 bool operator!=(const AttrBuilder &B) {
831 return !(*this == B);
835 namespace AttributeFuncs {
837 /// Which attributes cannot be applied to a type.
838 AttrBuilder typeIncompatible(Type *Ty);
840 /// \returns Return true if the two functions have compatible target-independent
841 /// attributes for inlining purposes.
842 bool areInlineCompatible(const Function &Caller, const Function &Callee);
844 /// Merge caller's and callee's attributes.
845 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
847 } // end namespace AttributeFuncs
849 } // end namespace llvm
851 #endif // LLVM_IR_ATTRIBUTES_H