1 //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file contains the simple types necessary to represent the
11 /// attributes associated with functions and their calls.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_ATTRIBUTES_H
16 #define LLVM_IR_ATTRIBUTES_H
18 #include "llvm-c/Types.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/PointerLikeTypeTraits.h"
37 class AttributeListImpl
;
38 class AttributeSetNode
;
39 template<typename T
> struct DenseMapInfo
;
44 //===----------------------------------------------------------------------===//
46 /// Functions, function parameters, and return types can have attributes
47 /// to indicate how they should be treated by optimizations and code
48 /// generation. This class represents one of those attributes. It's light-weight
49 /// and should be passed around by-value.
52 /// This enumeration lists the attributes that can be associated with
53 /// parameters, function results, or the function itself.
55 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
56 /// entry in the unwind table. The `nounwind' attribute is about an exception
57 /// passing by the function.
59 /// In a theoretical system that uses tables for profiling and SjLj for
60 /// exceptions, they would be fully independent. In a normal system that uses
61 /// tables for both, the semantics are:
63 /// nil = Needs an entry because an exception might pass by.
64 /// nounwind = No need for an entry
65 /// uwtable = Needs an entry because the ABI says so and because
66 /// an exception might pass by.
67 /// uwtable + nounwind = Needs an entry because the ABI says so.
70 // IR-Level Attributes
71 None
, ///< No attributes have been set
73 #include "llvm/IR/Attributes.inc"
74 EndAttrKinds
///< Sentinal value useful for loops
78 AttributeImpl
*pImpl
= nullptr;
80 Attribute(AttributeImpl
*A
) : pImpl(A
) {}
83 Attribute() = default;
85 //===--------------------------------------------------------------------===//
86 // Attribute Construction
87 //===--------------------------------------------------------------------===//
89 /// Return a uniquified Attribute object.
90 static Attribute
get(LLVMContext
&Context
, AttrKind Kind
, uint64_t Val
= 0);
91 static Attribute
get(LLVMContext
&Context
, StringRef Kind
,
92 StringRef Val
= StringRef());
94 /// Return a uniquified Attribute object that has the specific
96 static Attribute
getWithAlignment(LLVMContext
&Context
, uint64_t Align
);
97 static Attribute
getWithStackAlignment(LLVMContext
&Context
, uint64_t Align
);
98 static Attribute
getWithDereferenceableBytes(LLVMContext
&Context
,
100 static Attribute
getWithDereferenceableOrNullBytes(LLVMContext
&Context
,
102 static Attribute
getWithAllocSizeArgs(LLVMContext
&Context
,
103 unsigned ElemSizeArg
,
104 const Optional
<unsigned> &NumElemsArg
);
106 //===--------------------------------------------------------------------===//
107 // Attribute Accessors
108 //===--------------------------------------------------------------------===//
110 /// Return true if the attribute is an Attribute::AttrKind type.
111 bool isEnumAttribute() const;
113 /// Return true if the attribute is an integer attribute.
114 bool isIntAttribute() const;
116 /// Return true if the attribute is a string (target-dependent)
118 bool isStringAttribute() const;
120 /// Return true if the attribute is present.
121 bool hasAttribute(AttrKind Val
) const;
123 /// Return true if the target-dependent attribute is present.
124 bool hasAttribute(StringRef Val
) const;
126 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
127 /// requires the attribute to be an enum or integer attribute.
128 Attribute::AttrKind
getKindAsEnum() const;
130 /// Return the attribute's value as an integer. This requires that the
131 /// attribute be an integer attribute.
132 uint64_t getValueAsInt() const;
134 /// Return the attribute's kind as a string. This requires the
135 /// attribute to be a string attribute.
136 StringRef
getKindAsString() const;
138 /// Return the attribute's value as a string. This requires the
139 /// attribute to be a string attribute.
140 StringRef
getValueAsString() const;
142 /// Returns the alignment field of an attribute as a byte alignment
144 unsigned getAlignment() const;
146 /// Returns the stack alignment field of an attribute as a byte
148 unsigned getStackAlignment() const;
150 /// Returns the number of dereferenceable bytes from the
151 /// dereferenceable attribute.
152 uint64_t getDereferenceableBytes() const;
154 /// Returns the number of dereferenceable_or_null bytes from the
155 /// dereferenceable_or_null attribute.
156 uint64_t getDereferenceableOrNullBytes() const;
158 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
160 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
162 /// The Attribute is converted to a string of equivalent mnemonic. This
163 /// is, presumably, for writing out the mnemonics for the assembly writer.
164 std::string
getAsString(bool InAttrGrp
= false) const;
166 /// Equality and non-equality operators.
167 bool operator==(Attribute A
) const { return pImpl
== A
.pImpl
; }
168 bool operator!=(Attribute A
) const { return pImpl
!= A
.pImpl
; }
170 /// Less-than operator. Useful for sorting the attributes list.
171 bool operator<(Attribute A
) const;
173 void Profile(FoldingSetNodeID
&ID
) const {
174 ID
.AddPointer(pImpl
);
177 /// Return a raw pointer that uniquely identifies this attribute.
178 void *getRawPointer() const {
182 /// Get an attribute from a raw pointer created by getRawPointer.
183 static Attribute
fromRawPointer(void *RawPtr
) {
184 return Attribute(reinterpret_cast<AttributeImpl
*>(RawPtr
));
188 // Specialized opaque value conversions.
189 inline LLVMAttributeRef
wrap(Attribute Attr
) {
190 return reinterpret_cast<LLVMAttributeRef
>(Attr
.getRawPointer());
193 // Specialized opaque value conversions.
194 inline Attribute
unwrap(LLVMAttributeRef Attr
) {
195 return Attribute::fromRawPointer(Attr
);
198 //===----------------------------------------------------------------------===//
200 /// This class holds the attributes for a particular argument, parameter,
201 /// function, or return value. It is an immutable value type that is cheap to
202 /// copy. Adding and removing enum attributes is intended to be fast, but adding
203 /// and removing string or integer attributes involves a FoldingSet lookup.
205 friend AttributeListImpl
;
206 template <typename Ty
> friend struct DenseMapInfo
;
208 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
209 // This will allow an efficient implementation of addAttribute and
210 // removeAttribute for enum attrs.
212 /// Private implementation pointer.
213 AttributeSetNode
*SetNode
= nullptr;
216 explicit AttributeSet(AttributeSetNode
*ASN
) : SetNode(ASN
) {}
219 /// AttributeSet is a trivially copyable value type.
220 AttributeSet() = default;
221 AttributeSet(const AttributeSet
&) = default;
222 ~AttributeSet() = default;
224 static AttributeSet
get(LLVMContext
&C
, const AttrBuilder
&B
);
225 static AttributeSet
get(LLVMContext
&C
, ArrayRef
<Attribute
> Attrs
);
227 bool operator==(const AttributeSet
&O
) const { return SetNode
== O
.SetNode
; }
228 bool operator!=(const AttributeSet
&O
) const { return !(*this == O
); }
230 /// Add an argument attribute. Returns a new set because attribute sets are
232 LLVM_NODISCARD AttributeSet
addAttribute(LLVMContext
&C
,
233 Attribute::AttrKind Kind
) const;
235 /// Add a target-dependent attribute. Returns a new set because attribute sets
237 LLVM_NODISCARD 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 LLVM_NODISCARD AttributeSet
addAttributes(LLVMContext
&C
,
243 AttributeSet AS
) const;
245 /// Remove the specified attribute from this set. Returns a new set because
246 /// attribute sets are immutable.
247 LLVM_NODISCARD AttributeSet
removeAttribute(LLVMContext
&C
,
248 Attribute::AttrKind Kind
) const;
250 /// Remove the specified attribute from this set. Returns a new set because
251 /// attribute sets are immutable.
252 LLVM_NODISCARD AttributeSet
removeAttribute(LLVMContext
&C
,
253 StringRef Kind
) const;
255 /// Remove the specified attributes from this set. Returns a new set because
256 /// attribute sets are immutable.
257 LLVM_NODISCARD AttributeSet
258 removeAttributes(LLVMContext
&C
, 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 /// Provide DenseMapInfo for AttributeSet.
297 template <> struct DenseMapInfo
<AttributeSet
> {
298 static AttributeSet
getEmptyKey() {
299 auto Val
= static_cast<uintptr_t>(-1);
300 Val
<<= PointerLikeTypeTraits
<void *>::NumLowBitsAvailable
;
301 return AttributeSet(reinterpret_cast<AttributeSetNode
*>(Val
));
304 static AttributeSet
getTombstoneKey() {
305 auto 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 /// 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
325 /// index `AttributeList::FirstArgIndex'.
326 class AttributeList
{
328 enum AttrIndex
: unsigned {
335 friend class AttrBuilder
;
336 friend class AttributeListImpl
;
337 friend class AttributeSet
;
338 friend class AttributeSetNode
;
339 template <typename Ty
> friend struct DenseMapInfo
;
341 /// The attributes that we are managing. This can be null to represent
342 /// the empty attributes list.
343 AttributeListImpl
*pImpl
= nullptr;
346 /// Create an AttributeList with the specified parameters in it.
347 static AttributeList
get(LLVMContext
&C
,
348 ArrayRef
<std::pair
<unsigned, Attribute
>> Attrs
);
349 static AttributeList
get(LLVMContext
&C
,
350 ArrayRef
<std::pair
<unsigned, AttributeSet
>> Attrs
);
352 /// Create an AttributeList from attribute sets for a function, its
353 /// return value, and all of its arguments.
354 static AttributeList
get(LLVMContext
&C
, AttributeSet FnAttrs
,
355 AttributeSet RetAttrs
,
356 ArrayRef
<AttributeSet
> ArgAttrs
);
359 explicit AttributeList(AttributeListImpl
*LI
) : pImpl(LI
) {}
361 static AttributeList
getImpl(LLVMContext
&C
, ArrayRef
<AttributeSet
> AttrSets
);
364 AttributeList() = default;
366 //===--------------------------------------------------------------------===//
367 // AttributeList Construction and Mutation
368 //===--------------------------------------------------------------------===//
370 /// Return an AttributeList with the specified parameters in it.
371 static AttributeList
get(LLVMContext
&C
, ArrayRef
<AttributeList
> Attrs
);
372 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
373 ArrayRef
<Attribute::AttrKind
> Kinds
);
374 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
375 ArrayRef
<StringRef
> Kind
);
376 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
377 const AttrBuilder
&B
);
379 /// Add an attribute to the attribute set at the given index.
380 /// Returns a new list because attribute lists are immutable.
381 LLVM_NODISCARD AttributeList
addAttribute(LLVMContext
&C
, unsigned Index
,
382 Attribute::AttrKind Kind
) const;
384 /// Add an attribute to the attribute set at the given index.
385 /// Returns a new list because attribute lists are immutable.
386 LLVM_NODISCARD AttributeList
387 addAttribute(LLVMContext
&C
, unsigned Index
, StringRef Kind
,
388 StringRef Value
= StringRef()) const;
390 /// Add an attribute to the attribute set at the given index.
391 /// Returns a new list because attribute lists are immutable.
392 LLVM_NODISCARD AttributeList
addAttribute(LLVMContext
&C
, unsigned Index
,
395 /// Add attributes to the attribute set at the given index.
396 /// Returns a new list because attribute lists are immutable.
397 LLVM_NODISCARD AttributeList
addAttributes(LLVMContext
&C
, unsigned Index
,
398 const AttrBuilder
&B
) const;
400 /// Add an argument attribute to the list. Returns a new list because
401 /// attribute lists are immutable.
402 LLVM_NODISCARD AttributeList
addParamAttribute(
403 LLVMContext
&C
, unsigned ArgNo
, Attribute::AttrKind Kind
) const {
404 return addAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
407 /// Add an argument attribute to the list. Returns a new list because
408 /// attribute lists are immutable.
409 LLVM_NODISCARD AttributeList
410 addParamAttribute(LLVMContext
&C
, unsigned ArgNo
, StringRef Kind
,
411 StringRef Value
= StringRef()) const {
412 return addAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
, Value
);
415 /// Add an attribute to the attribute list at the given arg indices. Returns a
416 /// new list because attribute lists are immutable.
417 LLVM_NODISCARD AttributeList
addParamAttribute(LLVMContext
&C
,
418 ArrayRef
<unsigned> ArgNos
,
421 /// Add an argument attribute to the list. Returns a new list because
422 /// attribute lists are immutable.
423 LLVM_NODISCARD AttributeList
addParamAttributes(LLVMContext
&C
,
425 const AttrBuilder
&B
) const {
426 return addAttributes(C
, ArgNo
+ FirstArgIndex
, B
);
429 /// Remove the specified attribute at the specified index from this
430 /// attribute list. Returns a new list because attribute lists are immutable.
431 LLVM_NODISCARD AttributeList
removeAttribute(LLVMContext
&C
, unsigned Index
,
432 Attribute::AttrKind Kind
) const;
434 /// Remove the specified attribute at the specified index from this
435 /// attribute list. Returns a new list because attribute lists are immutable.
436 LLVM_NODISCARD AttributeList
removeAttribute(LLVMContext
&C
, unsigned Index
,
437 StringRef Kind
) const;
439 /// Remove the specified attributes at the specified index from this
440 /// attribute list. Returns a new list because attribute lists are immutable.
441 LLVM_NODISCARD AttributeList
removeAttributes(
442 LLVMContext
&C
, unsigned Index
, const AttrBuilder
&AttrsToRemove
) const;
444 /// Remove all attributes at the specified index from this
445 /// attribute list. Returns a new list because attribute lists are immutable.
446 LLVM_NODISCARD AttributeList
removeAttributes(LLVMContext
&C
,
447 unsigned Index
) const;
449 /// Remove the specified attribute at the specified arg index from this
450 /// attribute list. Returns a new list because attribute lists are immutable.
451 LLVM_NODISCARD AttributeList
removeParamAttribute(
452 LLVMContext
&C
, unsigned ArgNo
, Attribute::AttrKind Kind
) const {
453 return removeAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
456 /// Remove the specified attribute at the specified arg index from this
457 /// attribute list. Returns a new list because attribute lists are immutable.
458 LLVM_NODISCARD AttributeList
removeParamAttribute(LLVMContext
&C
,
460 StringRef Kind
) const {
461 return removeAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
464 /// Remove the specified attribute at the specified arg index from this
465 /// attribute list. Returns a new list because attribute lists are immutable.
466 LLVM_NODISCARD AttributeList
removeParamAttributes(
467 LLVMContext
&C
, unsigned ArgNo
, const AttrBuilder
&AttrsToRemove
) const {
468 return removeAttributes(C
, ArgNo
+ FirstArgIndex
, AttrsToRemove
);
471 /// Remove all attributes at the specified arg index from this
472 /// attribute list. Returns a new list because attribute lists are immutable.
473 LLVM_NODISCARD AttributeList
removeParamAttributes(LLVMContext
&C
,
474 unsigned ArgNo
) const {
475 return removeAttributes(C
, ArgNo
+ FirstArgIndex
);
478 /// \brief Add the dereferenceable attribute to the attribute set at the given
479 /// index. Returns a new list because attribute lists are immutable.
480 LLVM_NODISCARD AttributeList
addDereferenceableAttr(LLVMContext
&C
,
482 uint64_t Bytes
) const;
484 /// \brief Add the dereferenceable attribute to the attribute set at the given
485 /// arg index. Returns a new list because attribute lists are immutable.
486 LLVM_NODISCARD AttributeList
addDereferenceableParamAttr(
487 LLVMContext
&C
, unsigned ArgNo
, uint64_t Bytes
) const {
488 return addDereferenceableAttr(C
, ArgNo
+ FirstArgIndex
, Bytes
);
491 /// Add the dereferenceable_or_null attribute to the attribute set at
492 /// the given index. Returns a new list because attribute lists are immutable.
493 LLVM_NODISCARD AttributeList
addDereferenceableOrNullAttr(
494 LLVMContext
&C
, unsigned Index
, uint64_t Bytes
) const;
496 /// Add the dereferenceable_or_null attribute to the attribute set at
497 /// the given arg index. Returns a new list because attribute lists are
499 LLVM_NODISCARD AttributeList
addDereferenceableOrNullParamAttr(
500 LLVMContext
&C
, unsigned ArgNo
, uint64_t Bytes
) const {
501 return addDereferenceableOrNullAttr(C
, ArgNo
+ FirstArgIndex
, Bytes
);
504 /// Add the allocsize attribute to the attribute set at the given index.
505 /// Returns a new list because attribute lists are immutable.
506 LLVM_NODISCARD AttributeList
507 addAllocSizeAttr(LLVMContext
&C
, unsigned Index
, unsigned ElemSizeArg
,
508 const Optional
<unsigned> &NumElemsArg
);
510 /// Add the allocsize attribute to the attribute set at the given arg index.
511 /// Returns a new list because attribute lists are immutable.
512 LLVM_NODISCARD AttributeList
513 addAllocSizeParamAttr(LLVMContext
&C
, unsigned ArgNo
, unsigned ElemSizeArg
,
514 const Optional
<unsigned> &NumElemsArg
) {
515 return addAllocSizeAttr(C
, ArgNo
+ FirstArgIndex
, ElemSizeArg
, NumElemsArg
);
518 //===--------------------------------------------------------------------===//
519 // AttributeList Accessors
520 //===--------------------------------------------------------------------===//
522 /// Retrieve the LLVM context.
523 LLVMContext
&getContext() const;
525 /// The attributes for the specified index are returned.
526 AttributeSet
getAttributes(unsigned Index
) const;
528 /// The attributes for the argument or parameter at the given index are
530 AttributeSet
getParamAttributes(unsigned ArgNo
) const;
532 /// The attributes for the ret value are returned.
533 AttributeSet
getRetAttributes() const;
535 /// The function attributes are returned.
536 AttributeSet
getFnAttributes() const;
538 /// Return true if the attribute exists at the given index.
539 bool hasAttribute(unsigned Index
, Attribute::AttrKind Kind
) const;
541 /// Return true if the attribute exists at the given index.
542 bool hasAttribute(unsigned Index
, StringRef Kind
) const;
544 /// Return true if attribute exists at the given index.
545 bool hasAttributes(unsigned Index
) const;
547 /// Return true if the attribute exists for the given argument
548 bool hasParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
549 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
552 /// Return true if the attribute exists for the given argument
553 bool hasParamAttr(unsigned ArgNo
, StringRef Kind
) const {
554 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
557 /// Return true if attributes exists for the given argument
558 bool hasParamAttrs(unsigned ArgNo
) const {
559 return hasAttributes(ArgNo
+ FirstArgIndex
);
562 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
564 bool hasFnAttribute(Attribute::AttrKind Kind
) const;
566 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
568 bool hasFnAttribute(StringRef Kind
) const;
570 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
571 bool hasParamAttribute(unsigned ArgNo
, Attribute::AttrKind Kind
) const;
573 /// Return true if the specified attribute is set for at least one
574 /// parameter or for the return value. If Index is not nullptr, the index
575 /// of a parameter with the specified attribute is provided.
576 bool hasAttrSomewhere(Attribute::AttrKind Kind
,
577 unsigned *Index
= nullptr) const;
579 /// Return the attribute object that exists at the given index.
580 Attribute
getAttribute(unsigned Index
, Attribute::AttrKind Kind
) const;
582 /// Return the attribute object that exists at the given index.
583 Attribute
getAttribute(unsigned Index
, StringRef Kind
) const;
585 /// Return the attribute object that exists at the arg index.
586 Attribute
getParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
587 return getAttribute(ArgNo
+ FirstArgIndex
, Kind
);
590 /// Return the attribute object that exists at the given index.
591 Attribute
getParamAttr(unsigned ArgNo
, StringRef Kind
) const {
592 return getAttribute(ArgNo
+ FirstArgIndex
, Kind
);
595 /// Return the alignment of the return value.
596 unsigned getRetAlignment() const;
598 /// Return the alignment for the specified function parameter.
599 unsigned getParamAlignment(unsigned ArgNo
) const;
601 /// Get the stack alignment.
602 unsigned getStackAlignment(unsigned Index
) const;
604 /// Get the number of dereferenceable bytes (or zero if unknown).
605 uint64_t getDereferenceableBytes(unsigned Index
) const;
607 /// Get the number of dereferenceable bytes (or zero if unknown) of an
609 uint64_t getParamDereferenceableBytes(unsigned ArgNo
) const {
610 return getDereferenceableBytes(ArgNo
+ FirstArgIndex
);
613 /// Get the number of dereferenceable_or_null bytes (or zero if
615 uint64_t getDereferenceableOrNullBytes(unsigned Index
) const;
617 /// Get the number of dereferenceable_or_null bytes (or zero if
618 /// unknown) of an arg.
619 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo
) const {
620 return getDereferenceableOrNullBytes(ArgNo
+ FirstArgIndex
);
623 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
624 std::pair
<unsigned, Optional
<unsigned>>
625 getAllocSizeArgs(unsigned Index
) const;
627 /// Return the attributes at the index as a string.
628 std::string
getAsString(unsigned Index
, bool InAttrGrp
= false) const;
630 //===--------------------------------------------------------------------===//
631 // AttributeList Introspection
632 //===--------------------------------------------------------------------===//
634 using iterator
= const AttributeSet
*;
636 iterator
begin() const;
637 iterator
end() const;
639 unsigned getNumAttrSets() const;
641 /// Use these to iterate over the valid attribute indices.
642 unsigned index_begin() const { return AttributeList::FunctionIndex
; }
643 unsigned index_end() const { return getNumAttrSets() - 1; }
645 /// operator==/!= - Provide equality predicates.
646 bool operator==(const AttributeList
&RHS
) const { return pImpl
== RHS
.pImpl
; }
647 bool operator!=(const AttributeList
&RHS
) const { return pImpl
!= RHS
.pImpl
; }
649 /// Return a raw pointer that uniquely identifies this attribute list.
650 void *getRawPointer() const {
654 /// Return true if there are no attributes.
655 bool isEmpty() const { return pImpl
== nullptr; }
660 //===----------------------------------------------------------------------===//
662 /// Provide DenseMapInfo for AttributeList.
663 template <> struct DenseMapInfo
<AttributeList
> {
664 static AttributeList
getEmptyKey() {
665 auto Val
= static_cast<uintptr_t>(-1);
666 Val
<<= PointerLikeTypeTraits
<void*>::NumLowBitsAvailable
;
667 return AttributeList(reinterpret_cast<AttributeListImpl
*>(Val
));
670 static AttributeList
getTombstoneKey() {
671 auto Val
= static_cast<uintptr_t>(-2);
672 Val
<<= PointerLikeTypeTraits
<void*>::NumLowBitsAvailable
;
673 return AttributeList(reinterpret_cast<AttributeListImpl
*>(Val
));
676 static unsigned getHashValue(AttributeList AS
) {
677 return (unsigned((uintptr_t)AS
.pImpl
) >> 4) ^
678 (unsigned((uintptr_t)AS
.pImpl
) >> 9);
681 static bool isEqual(AttributeList LHS
, AttributeList RHS
) {
686 //===----------------------------------------------------------------------===//
688 /// This class is used in conjunction with the Attribute::get method to
689 /// create an Attribute object. The object itself is uniquified. The Builder's
690 /// value, however, is not. So this can be used as a quick way to test for
691 /// equality, presence of attributes, etc.
693 std::bitset
<Attribute::EndAttrKinds
> Attrs
;
694 std::map
<std::string
, std::string
> TargetDepAttrs
;
695 uint64_t Alignment
= 0;
696 uint64_t StackAlignment
= 0;
697 uint64_t DerefBytes
= 0;
698 uint64_t DerefOrNullBytes
= 0;
699 uint64_t AllocSizeArgs
= 0;
702 AttrBuilder() = default;
704 AttrBuilder(const Attribute
&A
) {
708 AttrBuilder(AttributeList AS
, unsigned Idx
);
709 AttrBuilder(AttributeSet AS
);
713 /// Add an attribute to the builder.
714 AttrBuilder
&addAttribute(Attribute::AttrKind Val
);
716 /// Add the Attribute object to the builder.
717 AttrBuilder
&addAttribute(Attribute A
);
719 /// Add the target-dependent attribute to the builder.
720 AttrBuilder
&addAttribute(StringRef A
, StringRef V
= StringRef());
722 /// Remove an attribute from the builder.
723 AttrBuilder
&removeAttribute(Attribute::AttrKind Val
);
725 /// Remove the attributes from the builder.
726 AttrBuilder
&removeAttributes(AttributeList A
, uint64_t WithoutIndex
);
728 /// Remove the target-dependent attribute to the builder.
729 AttrBuilder
&removeAttribute(StringRef A
);
731 /// Add the attributes from the builder.
732 AttrBuilder
&merge(const AttrBuilder
&B
);
734 /// Remove the attributes from the builder.
735 AttrBuilder
&remove(const AttrBuilder
&B
);
737 /// Return true if the builder has any attribute that's in the
738 /// specified builder.
739 bool overlaps(const AttrBuilder
&B
) const;
741 /// Return true if the builder has the specified attribute.
742 bool contains(Attribute::AttrKind A
) const {
743 assert((unsigned)A
< Attribute::EndAttrKinds
&& "Attribute out of range!");
747 /// Return true if the builder has the specified target-dependent
749 bool contains(StringRef A
) const;
751 /// Return true if the builder has IR-level attributes.
752 bool hasAttributes() const;
754 /// Return true if the builder has any attribute that's in the
755 /// specified attribute.
756 bool hasAttributes(AttributeList A
, uint64_t Index
) const;
758 /// Return true if the builder has an alignment attribute.
759 bool hasAlignmentAttr() const;
761 /// Retrieve the alignment attribute, if it exists.
762 uint64_t getAlignment() const { return Alignment
; }
764 /// Retrieve the stack alignment attribute, if it exists.
765 uint64_t getStackAlignment() const { return StackAlignment
; }
767 /// Retrieve the number of dereferenceable bytes, if the
768 /// dereferenceable attribute exists (zero is returned otherwise).
769 uint64_t getDereferenceableBytes() const { return DerefBytes
; }
771 /// Retrieve the number of dereferenceable_or_null bytes, if the
772 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
773 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes
; }
775 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
776 /// doesn't exist, pair(0, 0) is returned.
777 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
779 /// This turns an int alignment (which must be a power of 2) into the
780 /// form used internally in Attribute.
781 AttrBuilder
&addAlignmentAttr(unsigned Align
);
783 /// This turns an int stack alignment (which must be a power of 2) into
784 /// the form used internally in Attribute.
785 AttrBuilder
&addStackAlignmentAttr(unsigned Align
);
787 /// This turns the number of dereferenceable bytes into the form used
788 /// internally in Attribute.
789 AttrBuilder
&addDereferenceableAttr(uint64_t Bytes
);
791 /// This turns the number of dereferenceable_or_null bytes into the
792 /// form used internally in Attribute.
793 AttrBuilder
&addDereferenceableOrNullAttr(uint64_t Bytes
);
795 /// This turns one (or two) ints into the form used internally in Attribute.
796 AttrBuilder
&addAllocSizeAttr(unsigned ElemSizeArg
,
797 const Optional
<unsigned> &NumElemsArg
);
799 /// Add an allocsize attribute, using the representation returned by
800 /// Attribute.getIntValue().
801 AttrBuilder
&addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr
);
803 /// Return true if the builder contains no target-independent
805 bool empty() const { return Attrs
.none(); }
807 // Iterators for target-dependent attributes.
808 using td_type
= std::pair
<std::string
, std::string
>;
809 using td_iterator
= std::map
<std::string
, std::string
>::iterator
;
810 using td_const_iterator
= std::map
<std::string
, std::string
>::const_iterator
;
811 using td_range
= iterator_range
<td_iterator
>;
812 using td_const_range
= iterator_range
<td_const_iterator
>;
814 td_iterator
td_begin() { return TargetDepAttrs
.begin(); }
815 td_iterator
td_end() { return TargetDepAttrs
.end(); }
817 td_const_iterator
td_begin() const { return TargetDepAttrs
.begin(); }
818 td_const_iterator
td_end() const { return TargetDepAttrs
.end(); }
820 td_range
td_attrs() { return td_range(td_begin(), td_end()); }
822 td_const_range
td_attrs() const {
823 return td_const_range(td_begin(), td_end());
826 bool td_empty() const { return TargetDepAttrs
.empty(); }
828 bool operator==(const AttrBuilder
&B
);
829 bool operator!=(const AttrBuilder
&B
) {
830 return !(*this == B
);
834 namespace AttributeFuncs
{
836 /// Which attributes cannot be applied to a type.
837 AttrBuilder
typeIncompatible(Type
*Ty
);
839 /// \returns Return true if the two functions have compatible target-independent
840 /// attributes for inlining purposes.
841 bool areInlineCompatible(const Function
&Caller
, const Function
&Callee
);
843 /// Merge caller's and callee's attributes.
844 void mergeAttributesForInlining(Function
&Caller
, const Function
&Callee
);
846 } // end namespace AttributeFuncs
848 } // end namespace llvm
850 #endif // LLVM_IR_ATTRIBUTES_H