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/ADT/iterator_range.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/PointerLikeTypeTraits.h"
25 #include "llvm-c/Types.h"
37 class AttributeListImpl;
39 class AttributeSetNode;
40 template<typename T> struct DenseMapInfo;
45 //===----------------------------------------------------------------------===//
47 /// \brief 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.gen"
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 /// \brief 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 /// \brief 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 /// \brief Return true if the attribute is an Attribute::AttrKind type.
112 bool isEnumAttribute() const;
114 /// \brief Return true if the attribute is an integer attribute.
115 bool isIntAttribute() const;
117 /// \brief Return true if the attribute is a string (target-dependent)
119 bool isStringAttribute() const;
121 /// \brief Return true if the attribute is present.
122 bool hasAttribute(AttrKind Val) const;
124 /// \brief Return true if the target-dependent attribute is present.
125 bool hasAttribute(StringRef Val) const;
127 /// \brief 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 /// \brief Return the attribute's value as an integer. This requires that the
132 /// attribute be an integer attribute.
133 uint64_t getValueAsInt() const;
135 /// \brief Return the attribute's kind as a string. This requires the
136 /// attribute to be a string attribute.
137 StringRef getKindAsString() const;
139 /// \brief Return the attribute's value as a string. This requires the
140 /// attribute to be a string attribute.
141 StringRef getValueAsString() const;
143 /// \brief Returns the alignment field of an attribute as a byte alignment
145 unsigned getAlignment() const;
147 /// \brief Returns the stack alignment field of an attribute as a byte
149 unsigned getStackAlignment() const;
151 /// \brief Returns the number of dereferenceable bytes from the
152 /// dereferenceable attribute.
153 uint64_t getDereferenceableBytes() const;
155 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief Return a raw pointer that uniquely identifies this attribute.
179 void *getRawPointer() const {
183 /// \brief 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 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
207 // This will allow an efficient implementation of addAttribute and
208 // removeAttribute for enum attrs.
210 /// Private implementation pointer.
211 AttributeSetNode *SetNode = nullptr;
213 friend AttributeListImpl;
214 template <typename Ty> friend struct DenseMapInfo;
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) { return SetNode == O.SetNode; }
229 bool operator!=(const AttributeSet &O) { return !(*this == O); }
231 /// Add an argument attribute. Because
232 /// attribute sets are immutable, this returns a new set.
233 AttributeSet addAttribute(LLVMContext &C,
234 Attribute::AttrKind Kind) const;
236 /// Add a target-dependent attribute. Because
237 /// attribute sets are immutable, this returns a new set.
238 AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
239 StringRef Value = StringRef()) const;
241 /// Add attributes to the attribute set. Because
242 /// attribute sets are immutable, this returns a new set.
243 AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const;
245 /// Remove the specified attribute from this set. Because
246 /// attribute sets are immutable, this returns a new set.
247 AttributeSet removeAttribute(LLVMContext &C,
248 Attribute::AttrKind Kind) const;
250 /// Remove the specified attribute from this set. Because
251 /// attribute sets are immutable, this returns a new set.
252 AttributeSet removeAttribute(LLVMContext &C,
253 StringRef Kind) const;
255 /// Remove the specified attributes from this set. Because
256 /// attribute sets are immutable, this returns a new set.
257 AttributeSet removeAttributes(LLVMContext &C,
258 const AttrBuilder &AttrsToRemove) const;
260 /// Return the number of attributes in this set.
261 unsigned getNumAttributes() const;
263 /// Return true if attributes exists in this set.
264 bool hasAttributes() const { return SetNode != nullptr; }
266 /// Return true if the attribute exists in this set.
267 bool hasAttribute(Attribute::AttrKind Kind) const;
269 /// Return true if the attribute exists in this set.
270 bool hasAttribute(StringRef Kind) const;
272 /// Return the attribute object.
273 Attribute getAttribute(Attribute::AttrKind Kind) const;
275 /// Return the target-dependent attribute object.
276 Attribute getAttribute(StringRef Kind) const;
278 unsigned getAlignment() const;
279 unsigned getStackAlignment() const;
280 uint64_t getDereferenceableBytes() const;
281 uint64_t getDereferenceableOrNullBytes() const;
282 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
283 std::string getAsString(bool InAttrGrp = false) const;
285 using iterator = const Attribute *;
287 iterator begin() const;
288 iterator end() const;
289 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
294 //===----------------------------------------------------------------------===//
296 /// \brief Provide DenseMapInfo for AttributeSet.
297 template <> struct DenseMapInfo<AttributeSet> {
298 static inline AttributeSet getEmptyKey() {
299 uintptr_t Val = static_cast<uintptr_t>(-1);
300 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
301 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
304 static inline AttributeSet getTombstoneKey() {
305 uintptr_t Val = static_cast<uintptr_t>(-2);
306 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
307 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
310 static unsigned getHashValue(AttributeSet AS) {
311 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
312 (unsigned((uintptr_t)AS.SetNode) >> 9);
315 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
318 //===----------------------------------------------------------------------===//
320 /// \brief This class holds the attributes for a function, its return value, and
321 /// its parameters. You access the attributes for each of them via an index into
322 /// the AttributeList object. The function attributes are at index
323 /// `AttributeList::FunctionIndex', the return value is at index
324 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
326 class AttributeList {
328 enum AttrIndex : unsigned {
335 friend class AttrBuilder;
336 friend class AttributeListImpl;
337 friend class AttributeSet;
338 friend class AttributeSetNode;
340 template <typename Ty> friend struct DenseMapInfo;
342 /// \brief The attributes that we are managing. This can be null to represent
343 /// the empty attributes list.
344 AttributeListImpl *pImpl = nullptr;
347 /// \brief Create an AttributeList with the specified parameters in it.
348 static AttributeList get(LLVMContext &C,
349 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
351 get(LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
353 /// \brief 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 getImpl(LLVMContext &C,
361 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
364 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
367 AttributeList() = default;
369 //===--------------------------------------------------------------------===//
370 // AttributeList Construction and Mutation
371 //===--------------------------------------------------------------------===//
373 /// \brief Return an AttributeList with the specified parameters in it.
374 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
375 static AttributeList get(LLVMContext &C, unsigned Index,
376 ArrayRef<Attribute::AttrKind> Kinds);
377 static AttributeList get(LLVMContext &C, unsigned Index,
378 ArrayRef<StringRef> Kind);
379 static AttributeList get(LLVMContext &C, unsigned Index,
380 const AttrBuilder &B);
382 /// Add an argument attribute to the list. Returns a new list because
383 /// attribute lists are immutable.
384 AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
385 Attribute::AttrKind Kind) const {
386 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
389 /// \brief Add an attribute to the attribute set at the given index. Because
390 /// attribute sets are immutable, this returns a new set.
391 AttributeList addAttribute(LLVMContext &C, unsigned Index,
392 Attribute::AttrKind Kind) const;
394 /// \brief Add an attribute to the attribute set at the given index. Because
395 /// attribute sets are immutable, this returns a new set.
396 AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
397 StringRef Value = StringRef()) const;
399 /// Add an attribute to the attribute set at the given indices. Because
400 /// attribute sets are immutable, this returns a new set.
401 AttributeList addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices,
404 /// \brief Add attributes to the attribute set at the given index. Because
405 /// attribute sets are immutable, this returns a new set.
406 AttributeList addAttributes(LLVMContext &C, unsigned Index,
407 const AttrBuilder &B) const;
409 /// \brief Remove the specified attribute at the specified index from this
410 /// attribute list. Because attribute lists are immutable, this returns the
412 AttributeList removeAttribute(LLVMContext &C, unsigned Index,
413 Attribute::AttrKind Kind) const;
415 /// \brief Remove the specified attribute at the specified index from this
416 /// attribute list. Because attribute lists are immutable, this returns the
418 AttributeList removeAttribute(LLVMContext &C, unsigned Index,
419 StringRef Kind) const;
421 /// \brief Remove the specified attributes at the specified index from this
422 /// attribute list. Because attribute lists are immutable, this returns the
424 AttributeList removeAttributes(LLVMContext &C, unsigned Index,
425 const AttrBuilder &AttrsToRemove) const;
427 /// \brief Remove all attributes at the specified index from this
428 /// attribute list. Because attribute lists are immutable, this returns the
430 AttributeList removeAttributes(LLVMContext &C, unsigned Index) const;
432 /// \brief Add the dereferenceable attribute to the attribute set at the given
433 /// index. Because attribute sets are immutable, this returns a new set.
434 AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index,
435 uint64_t Bytes) const;
437 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
438 /// the given index. Because attribute sets are immutable, this returns a new
440 AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
441 uint64_t Bytes) const;
443 /// Add the allocsize attribute to the attribute set at the given index.
444 /// Because attribute sets are immutable, this returns a new set.
445 AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index,
446 unsigned ElemSizeArg,
447 const Optional<unsigned> &NumElemsArg);
449 //===--------------------------------------------------------------------===//
450 // AttributeList Accessors
451 //===--------------------------------------------------------------------===//
453 /// \brief Retrieve the LLVM context.
454 LLVMContext &getContext() const;
456 /// \brief The attributes for the specified index are returned.
457 AttributeSet getAttributes(unsigned Index) const;
459 /// \brief The attributes for the argument or parameter at the given index are
461 AttributeSet getParamAttributes(unsigned ArgNo) const;
463 /// \brief The attributes for the ret value are returned.
464 AttributeSet getRetAttributes() const;
466 /// \brief The function attributes are returned.
467 AttributeSet getFnAttributes() const;
469 /// \brief Return true if the attribute exists at the given index.
470 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
472 /// \brief Return true if the attribute exists at the given index.
473 bool hasAttribute(unsigned Index, StringRef Kind) const;
475 /// \brief Return true if attribute exists at the given index.
476 bool hasAttributes(unsigned Index) const;
478 /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
480 bool hasFnAttribute(Attribute::AttrKind Kind) const;
482 /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
484 bool hasFnAttribute(StringRef Kind) const;
486 /// \brief Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
487 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
489 /// \brief Return true if the specified attribute is set for at least one
490 /// parameter or for the return value. If Index is not nullptr, the index
491 /// of a parameter with the specified attribute is provided.
492 bool hasAttrSomewhere(Attribute::AttrKind Kind,
493 unsigned *Index = nullptr) const;
495 /// \brief Return the attribute object that exists at the given index.
496 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
498 /// \brief Return the attribute object that exists at the given index.
499 Attribute getAttribute(unsigned Index, StringRef Kind) const;
501 /// \brief Return the alignment of the return value.
502 unsigned getRetAlignment() const;
504 /// \brief Return the alignment for the specified function parameter.
505 unsigned getParamAlignment(unsigned ArgNo) const;
507 /// \brief Get the stack alignment.
508 unsigned getStackAlignment(unsigned Index) const;
510 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
511 uint64_t getDereferenceableBytes(unsigned Index) const;
513 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
515 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
517 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
518 std::pair<unsigned, Optional<unsigned>>
519 getAllocSizeArgs(unsigned Index) const;
521 /// \brief Return the attributes at the index as a string.
522 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
524 using iterator = ArrayRef<Attribute>::iterator;
526 iterator begin(unsigned Slot) const;
527 iterator end(unsigned Slot) const;
529 /// operator==/!= - Provide equality predicates.
530 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
531 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
533 //===--------------------------------------------------------------------===//
534 // AttributeList Introspection
535 //===--------------------------------------------------------------------===//
537 /// \brief Return a raw pointer that uniquely identifies this attribute list.
538 void *getRawPointer() const {
542 /// \brief Return true if there are no attributes.
543 bool isEmpty() const {
544 return getNumSlots() == 0;
547 /// \brief Return the number of slots used in this attribute list. This is
548 /// the number of arguments that have an attribute set on them (including the
549 /// function itself).
550 unsigned getNumSlots() const;
552 /// \brief Return the index for the given slot.
553 unsigned getSlotIndex(unsigned Slot) const;
555 /// \brief Return the attributes at the given slot.
556 AttributeSet getSlotAttributes(unsigned Slot) const;
561 //===----------------------------------------------------------------------===//
563 /// \brief Provide DenseMapInfo for AttributeList.
564 template <> struct DenseMapInfo<AttributeList> {
565 static inline AttributeList getEmptyKey() {
566 uintptr_t Val = static_cast<uintptr_t>(-1);
567 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
568 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
571 static inline AttributeList getTombstoneKey() {
572 uintptr_t Val = static_cast<uintptr_t>(-2);
573 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
574 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
577 static unsigned getHashValue(AttributeList AS) {
578 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
579 (unsigned((uintptr_t)AS.pImpl) >> 9);
582 static bool isEqual(AttributeList LHS, AttributeList RHS) {
587 //===----------------------------------------------------------------------===//
589 /// \brief This class is used in conjunction with the Attribute::get method to
590 /// create an Attribute object. The object itself is uniquified. The Builder's
591 /// value, however, is not. So this can be used as a quick way to test for
592 /// equality, presence of attributes, etc.
594 std::bitset<Attribute::EndAttrKinds> Attrs;
595 std::map<std::string, std::string> TargetDepAttrs;
596 uint64_t Alignment = 0;
597 uint64_t StackAlignment = 0;
598 uint64_t DerefBytes = 0;
599 uint64_t DerefOrNullBytes = 0;
600 uint64_t AllocSizeArgs = 0;
603 AttrBuilder() = default;
604 AttrBuilder(const Attribute &A) {
607 AttrBuilder(AttributeList AS, unsigned Idx);
608 AttrBuilder(AttributeSet AS);
612 /// \brief Add an attribute to the builder.
613 AttrBuilder &addAttribute(Attribute::AttrKind Val);
615 /// \brief Add the Attribute object to the builder.
616 AttrBuilder &addAttribute(Attribute A);
618 /// \brief Add the target-dependent attribute to the builder.
619 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
621 /// \brief Remove an attribute from the builder.
622 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
624 /// \brief Remove the attributes from the builder.
625 AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
627 /// \brief Remove the target-dependent attribute to the builder.
628 AttrBuilder &removeAttribute(StringRef A);
630 /// \brief Add the attributes from the builder.
631 AttrBuilder &merge(const AttrBuilder &B);
633 /// \brief Remove the attributes from the builder.
634 AttrBuilder &remove(const AttrBuilder &B);
636 /// \brief Return true if the builder has any attribute that's in the
637 /// specified builder.
638 bool overlaps(const AttrBuilder &B) const;
640 /// \brief Return true if the builder has the specified attribute.
641 bool contains(Attribute::AttrKind A) const {
642 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
646 /// \brief Return true if the builder has the specified target-dependent
648 bool contains(StringRef A) const;
650 /// \brief Return true if the builder has IR-level attributes.
651 bool hasAttributes() const;
653 /// \brief Return true if the builder has any attribute that's in the
654 /// specified attribute.
655 bool hasAttributes(AttributeList A, uint64_t Index) const;
657 /// \brief Return true if the builder has an alignment attribute.
658 bool hasAlignmentAttr() const;
660 /// \brief Retrieve the alignment attribute, if it exists.
661 uint64_t getAlignment() const { return Alignment; }
663 /// \brief Retrieve the stack alignment attribute, if it exists.
664 uint64_t getStackAlignment() const { return StackAlignment; }
666 /// \brief Retrieve the number of dereferenceable bytes, if the
667 /// dereferenceable attribute exists (zero is returned otherwise).
668 uint64_t getDereferenceableBytes() const { return DerefBytes; }
670 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
671 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
672 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
674 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
675 /// doesn't exist, pair(0, 0) is returned.
676 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
678 /// \brief This turns an int alignment (which must be a power of 2) into the
679 /// form used internally in Attribute.
680 AttrBuilder &addAlignmentAttr(unsigned Align);
682 /// \brief This turns an int stack alignment (which must be a power of 2) into
683 /// the form used internally in Attribute.
684 AttrBuilder &addStackAlignmentAttr(unsigned Align);
686 /// \brief This turns the number of dereferenceable bytes into the form used
687 /// internally in Attribute.
688 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
690 /// \brief This turns the number of dereferenceable_or_null bytes into the
691 /// form used internally in Attribute.
692 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
694 /// This turns one (or two) ints into the form used internally in Attribute.
695 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
696 const Optional<unsigned> &NumElemsArg);
698 /// Add an allocsize attribute, using the representation returned by
699 /// Attribute.getIntValue().
700 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
702 /// \brief Return true if the builder contains no target-independent
704 bool empty() const { return Attrs.none(); }
706 // Iterators for target-dependent attributes.
707 using td_type = std::pair<std::string, std::string>;
708 using td_iterator = std::map<std::string, std::string>::iterator;
709 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
710 using td_range = iterator_range<td_iterator>;
711 using td_const_range = iterator_range<td_const_iterator>;
713 td_iterator td_begin() { return TargetDepAttrs.begin(); }
714 td_iterator td_end() { return TargetDepAttrs.end(); }
716 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
717 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
719 td_range td_attrs() { return td_range(td_begin(), td_end()); }
720 td_const_range td_attrs() const {
721 return td_const_range(td_begin(), td_end());
724 bool td_empty() const { return TargetDepAttrs.empty(); }
726 bool operator==(const AttrBuilder &B);
727 bool operator!=(const AttrBuilder &B) {
728 return !(*this == B);
732 namespace AttributeFuncs {
734 /// \brief Which attributes cannot be applied to a type.
735 AttrBuilder typeIncompatible(Type *Ty);
737 /// \returns Return true if the two functions have compatible target-independent
738 /// attributes for inlining purposes.
739 bool areInlineCompatible(const Function &Caller, const Function &Callee);
741 /// \brief Merge caller's and callee's attributes.
742 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
744 } // end AttributeFuncs namespace
746 } // end llvm namespace
748 #endif // LLVM_IR_ATTRIBUTES_H