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/Alignment.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());
94 static Attribute
get(LLVMContext
&Context
, AttrKind Kind
, Type
*Ty
);
96 /// Return a uniquified Attribute object that has the specific
98 static Attribute
getWithAlignment(LLVMContext
&Context
, uint64_t Align
);
99 static Attribute
getWithStackAlignment(LLVMContext
&Context
, uint64_t Align
);
100 static Attribute
getWithDereferenceableBytes(LLVMContext
&Context
,
102 static Attribute
getWithDereferenceableOrNullBytes(LLVMContext
&Context
,
104 static Attribute
getWithAllocSizeArgs(LLVMContext
&Context
,
105 unsigned ElemSizeArg
,
106 const Optional
<unsigned> &NumElemsArg
);
107 static Attribute
getWithByValType(LLVMContext
&Context
, Type
*Ty
);
109 //===--------------------------------------------------------------------===//
110 // Attribute Accessors
111 //===--------------------------------------------------------------------===//
113 /// Return true if the attribute is an Attribute::AttrKind type.
114 bool isEnumAttribute() const;
116 /// Return true if the attribute is an integer attribute.
117 bool isIntAttribute() const;
119 /// Return true if the attribute is a string (target-dependent)
121 bool isStringAttribute() const;
123 /// Return true if the attribute is a type attribute.
124 bool isTypeAttribute() const;
126 /// Return true if the attribute is present.
127 bool hasAttribute(AttrKind Val
) const;
129 /// Return true if the target-dependent attribute is present.
130 bool hasAttribute(StringRef Val
) const;
132 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
133 /// requires the attribute to be an enum or integer attribute.
134 Attribute::AttrKind
getKindAsEnum() const;
136 /// Return the attribute's value as an integer. This requires that the
137 /// attribute be an integer attribute.
138 uint64_t getValueAsInt() const;
140 /// Return the attribute's kind as a string. This requires the
141 /// attribute to be a string attribute.
142 StringRef
getKindAsString() const;
144 /// Return the attribute's value as a string. This requires the
145 /// attribute to be a string attribute.
146 StringRef
getValueAsString() const;
148 /// Return the attribute's value as a Type. This requires the attribute to be
149 /// a type attribute.
150 Type
*getValueAsType() const;
152 /// Returns the alignment field of an attribute as a byte alignment
154 unsigned getAlignment() const;
156 /// Returns the stack alignment field of an attribute as a byte
158 unsigned getStackAlignment() const;
160 /// Returns the number of dereferenceable bytes from the
161 /// dereferenceable attribute.
162 uint64_t getDereferenceableBytes() const;
164 /// Returns the number of dereferenceable_or_null bytes from the
165 /// dereferenceable_or_null attribute.
166 uint64_t getDereferenceableOrNullBytes() const;
168 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
170 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
172 /// The Attribute is converted to a string of equivalent mnemonic. This
173 /// is, presumably, for writing out the mnemonics for the assembly writer.
174 std::string
getAsString(bool InAttrGrp
= false) const;
176 /// Equality and non-equality operators.
177 bool operator==(Attribute A
) const { return pImpl
== A
.pImpl
; }
178 bool operator!=(Attribute A
) const { return pImpl
!= A
.pImpl
; }
180 /// Less-than operator. Useful for sorting the attributes list.
181 bool operator<(Attribute A
) const;
183 void Profile(FoldingSetNodeID
&ID
) const {
184 ID
.AddPointer(pImpl
);
187 /// Return a raw pointer that uniquely identifies this attribute.
188 void *getRawPointer() const {
192 /// Get an attribute from a raw pointer created by getRawPointer.
193 static Attribute
fromRawPointer(void *RawPtr
) {
194 return Attribute(reinterpret_cast<AttributeImpl
*>(RawPtr
));
198 // Specialized opaque value conversions.
199 inline LLVMAttributeRef
wrap(Attribute Attr
) {
200 return reinterpret_cast<LLVMAttributeRef
>(Attr
.getRawPointer());
203 // Specialized opaque value conversions.
204 inline Attribute
unwrap(LLVMAttributeRef Attr
) {
205 return Attribute::fromRawPointer(Attr
);
208 //===----------------------------------------------------------------------===//
210 /// This class holds the attributes for a particular argument, parameter,
211 /// function, or return value. It is an immutable value type that is cheap to
212 /// copy. Adding and removing enum attributes is intended to be fast, but adding
213 /// and removing string or integer attributes involves a FoldingSet lookup.
215 friend AttributeListImpl
;
216 template <typename Ty
> friend struct DenseMapInfo
;
218 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
219 // This will allow an efficient implementation of addAttribute and
220 // removeAttribute for enum attrs.
222 /// Private implementation pointer.
223 AttributeSetNode
*SetNode
= nullptr;
226 explicit AttributeSet(AttributeSetNode
*ASN
) : SetNode(ASN
) {}
229 /// AttributeSet is a trivially copyable value type.
230 AttributeSet() = default;
231 AttributeSet(const AttributeSet
&) = default;
232 ~AttributeSet() = default;
234 static AttributeSet
get(LLVMContext
&C
, const AttrBuilder
&B
);
235 static AttributeSet
get(LLVMContext
&C
, ArrayRef
<Attribute
> Attrs
);
237 bool operator==(const AttributeSet
&O
) const { return SetNode
== O
.SetNode
; }
238 bool operator!=(const AttributeSet
&O
) const { return !(*this == O
); }
240 /// Add an argument attribute. Returns a new set because attribute sets are
242 LLVM_NODISCARD AttributeSet
addAttribute(LLVMContext
&C
,
243 Attribute::AttrKind Kind
) const;
245 /// Add a target-dependent attribute. Returns a new set because attribute sets
247 LLVM_NODISCARD AttributeSet
addAttribute(LLVMContext
&C
, StringRef Kind
,
248 StringRef Value
= StringRef()) const;
250 /// Add attributes to the attribute set. Returns a new set because attribute
251 /// sets are immutable.
252 LLVM_NODISCARD AttributeSet
addAttributes(LLVMContext
&C
,
253 AttributeSet AS
) const;
255 /// Remove the specified attribute from this set. Returns a new set because
256 /// attribute sets are immutable.
257 LLVM_NODISCARD AttributeSet
removeAttribute(LLVMContext
&C
,
258 Attribute::AttrKind Kind
) const;
260 /// Remove the specified attribute from this set. Returns a new set because
261 /// attribute sets are immutable.
262 LLVM_NODISCARD AttributeSet
removeAttribute(LLVMContext
&C
,
263 StringRef Kind
) const;
265 /// Remove the specified attributes from this set. Returns a new set because
266 /// attribute sets are immutable.
267 LLVM_NODISCARD AttributeSet
268 removeAttributes(LLVMContext
&C
, const AttrBuilder
&AttrsToRemove
) const;
270 /// Return the number of attributes in this set.
271 unsigned getNumAttributes() const;
273 /// Return true if attributes exists in this set.
274 bool hasAttributes() const { return SetNode
!= nullptr; }
276 /// Return true if the attribute exists in this set.
277 bool hasAttribute(Attribute::AttrKind Kind
) const;
279 /// Return true if the attribute exists in this set.
280 bool hasAttribute(StringRef Kind
) const;
282 /// Return the attribute object.
283 Attribute
getAttribute(Attribute::AttrKind Kind
) const;
285 /// Return the target-dependent attribute object.
286 Attribute
getAttribute(StringRef Kind
) const;
288 unsigned getAlignment() const;
289 unsigned getStackAlignment() const;
290 uint64_t getDereferenceableBytes() const;
291 uint64_t getDereferenceableOrNullBytes() const;
292 Type
*getByValType() const;
293 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
294 std::string
getAsString(bool InAttrGrp
= false) const;
296 using iterator
= const Attribute
*;
298 iterator
begin() const;
299 iterator
end() const;
300 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
305 //===----------------------------------------------------------------------===//
307 /// Provide DenseMapInfo for AttributeSet.
308 template <> struct DenseMapInfo
<AttributeSet
> {
309 static AttributeSet
getEmptyKey() {
310 auto Val
= static_cast<uintptr_t>(-1);
311 Val
<<= PointerLikeTypeTraits
<void *>::NumLowBitsAvailable
;
312 return AttributeSet(reinterpret_cast<AttributeSetNode
*>(Val
));
315 static AttributeSet
getTombstoneKey() {
316 auto Val
= static_cast<uintptr_t>(-2);
317 Val
<<= PointerLikeTypeTraits
<void *>::NumLowBitsAvailable
;
318 return AttributeSet(reinterpret_cast<AttributeSetNode
*>(Val
));
321 static unsigned getHashValue(AttributeSet AS
) {
322 return (unsigned((uintptr_t)AS
.SetNode
) >> 4) ^
323 (unsigned((uintptr_t)AS
.SetNode
) >> 9);
326 static bool isEqual(AttributeSet LHS
, AttributeSet RHS
) { return LHS
== RHS
; }
329 //===----------------------------------------------------------------------===//
331 /// This class holds the attributes for a function, its return value, and
332 /// its parameters. You access the attributes for each of them via an index into
333 /// the AttributeList object. The function attributes are at index
334 /// `AttributeList::FunctionIndex', the return value is at index
335 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
336 /// index `AttributeList::FirstArgIndex'.
337 class AttributeList
{
339 enum AttrIndex
: unsigned {
346 friend class AttrBuilder
;
347 friend class AttributeListImpl
;
348 friend class AttributeSet
;
349 friend class AttributeSetNode
;
350 template <typename Ty
> friend struct DenseMapInfo
;
352 /// The attributes that we are managing. This can be null to represent
353 /// the empty attributes list.
354 AttributeListImpl
*pImpl
= nullptr;
357 /// Create an AttributeList with the specified parameters in it.
358 static AttributeList
get(LLVMContext
&C
,
359 ArrayRef
<std::pair
<unsigned, Attribute
>> Attrs
);
360 static AttributeList
get(LLVMContext
&C
,
361 ArrayRef
<std::pair
<unsigned, AttributeSet
>> Attrs
);
363 /// Create an AttributeList from attribute sets for a function, its
364 /// return value, and all of its arguments.
365 static AttributeList
get(LLVMContext
&C
, AttributeSet FnAttrs
,
366 AttributeSet RetAttrs
,
367 ArrayRef
<AttributeSet
> ArgAttrs
);
370 explicit AttributeList(AttributeListImpl
*LI
) : pImpl(LI
) {}
372 static AttributeList
getImpl(LLVMContext
&C
, ArrayRef
<AttributeSet
> AttrSets
);
375 AttributeList() = default;
377 //===--------------------------------------------------------------------===//
378 // AttributeList Construction and Mutation
379 //===--------------------------------------------------------------------===//
381 /// Return an AttributeList with the specified parameters in it.
382 static AttributeList
get(LLVMContext
&C
, ArrayRef
<AttributeList
> Attrs
);
383 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
384 ArrayRef
<Attribute::AttrKind
> Kinds
);
385 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
386 ArrayRef
<StringRef
> Kind
);
387 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
388 const AttrBuilder
&B
);
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
,
393 Attribute::AttrKind Kind
) const;
395 /// Add an attribute to the attribute set at the given index.
396 /// Returns a new list because attribute lists are immutable.
397 LLVM_NODISCARD AttributeList
398 addAttribute(LLVMContext
&C
, unsigned Index
, StringRef Kind
,
399 StringRef Value
= StringRef()) const;
401 /// Add an attribute to the attribute set at the given index.
402 /// Returns a new list because attribute lists are immutable.
403 LLVM_NODISCARD AttributeList
addAttribute(LLVMContext
&C
, unsigned Index
,
406 /// Add attributes to the attribute set at the given index.
407 /// Returns a new list because attribute lists are immutable.
408 LLVM_NODISCARD AttributeList
addAttributes(LLVMContext
&C
, unsigned Index
,
409 const AttrBuilder
&B
) const;
411 /// Add an argument attribute to the list. Returns a new list because
412 /// attribute lists are immutable.
413 LLVM_NODISCARD AttributeList
addParamAttribute(
414 LLVMContext
&C
, unsigned ArgNo
, Attribute::AttrKind Kind
) const {
415 return addAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
418 /// Add an argument attribute to the list. Returns a new list because
419 /// attribute lists are immutable.
420 LLVM_NODISCARD AttributeList
421 addParamAttribute(LLVMContext
&C
, unsigned ArgNo
, StringRef Kind
,
422 StringRef Value
= StringRef()) const {
423 return addAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
, Value
);
426 /// Add an attribute to the attribute list at the given arg indices. Returns a
427 /// new list because attribute lists are immutable.
428 LLVM_NODISCARD AttributeList
addParamAttribute(LLVMContext
&C
,
429 ArrayRef
<unsigned> ArgNos
,
432 /// Add an argument attribute to the list. Returns a new list because
433 /// attribute lists are immutable.
434 LLVM_NODISCARD AttributeList
addParamAttributes(LLVMContext
&C
,
436 const AttrBuilder
&B
) const {
437 return addAttributes(C
, ArgNo
+ FirstArgIndex
, B
);
440 /// Remove the specified attribute at the specified index from this
441 /// attribute list. Returns a new list because attribute lists are immutable.
442 LLVM_NODISCARD AttributeList
removeAttribute(LLVMContext
&C
, unsigned Index
,
443 Attribute::AttrKind Kind
) const;
445 /// Remove the specified attribute at the specified index from this
446 /// attribute list. Returns a new list because attribute lists are immutable.
447 LLVM_NODISCARD AttributeList
removeAttribute(LLVMContext
&C
, unsigned Index
,
448 StringRef Kind
) const;
450 /// Remove the specified attributes at the specified index from this
451 /// attribute list. Returns a new list because attribute lists are immutable.
452 LLVM_NODISCARD AttributeList
removeAttributes(
453 LLVMContext
&C
, unsigned Index
, const AttrBuilder
&AttrsToRemove
) const;
455 /// Remove all attributes at the specified index from this
456 /// attribute list. Returns a new list because attribute lists are immutable.
457 LLVM_NODISCARD AttributeList
removeAttributes(LLVMContext
&C
,
458 unsigned Index
) const;
460 /// Remove the specified attribute at the specified arg index from this
461 /// attribute list. Returns a new list because attribute lists are immutable.
462 LLVM_NODISCARD AttributeList
removeParamAttribute(
463 LLVMContext
&C
, unsigned ArgNo
, Attribute::AttrKind Kind
) const {
464 return removeAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
467 /// Remove the specified attribute at the specified arg index from this
468 /// attribute list. Returns a new list because attribute lists are immutable.
469 LLVM_NODISCARD AttributeList
removeParamAttribute(LLVMContext
&C
,
471 StringRef Kind
) const {
472 return removeAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
475 /// Remove the specified attribute at the specified arg index from this
476 /// attribute list. Returns a new list because attribute lists are immutable.
477 LLVM_NODISCARD AttributeList
removeParamAttributes(
478 LLVMContext
&C
, unsigned ArgNo
, const AttrBuilder
&AttrsToRemove
) const {
479 return removeAttributes(C
, ArgNo
+ FirstArgIndex
, AttrsToRemove
);
482 /// Remove all attributes at the specified arg index from this
483 /// attribute list. Returns a new list because attribute lists are immutable.
484 LLVM_NODISCARD AttributeList
removeParamAttributes(LLVMContext
&C
,
485 unsigned ArgNo
) const {
486 return removeAttributes(C
, ArgNo
+ FirstArgIndex
);
489 /// \brief Add the dereferenceable attribute to the attribute set at the given
490 /// index. Returns a new list because attribute lists are immutable.
491 LLVM_NODISCARD AttributeList
addDereferenceableAttr(LLVMContext
&C
,
493 uint64_t Bytes
) const;
495 /// \brief Add the dereferenceable attribute to the attribute set at the given
496 /// arg index. Returns a new list because attribute lists are immutable.
497 LLVM_NODISCARD AttributeList
addDereferenceableParamAttr(
498 LLVMContext
&C
, unsigned ArgNo
, uint64_t Bytes
) const {
499 return addDereferenceableAttr(C
, ArgNo
+ FirstArgIndex
, Bytes
);
502 /// Add the dereferenceable_or_null attribute to the attribute set at
503 /// the given index. Returns a new list because attribute lists are immutable.
504 LLVM_NODISCARD AttributeList
addDereferenceableOrNullAttr(
505 LLVMContext
&C
, unsigned Index
, uint64_t Bytes
) const;
507 /// Add the dereferenceable_or_null attribute to the attribute set at
508 /// the given arg index. Returns a new list because attribute lists are
510 LLVM_NODISCARD AttributeList
addDereferenceableOrNullParamAttr(
511 LLVMContext
&C
, unsigned ArgNo
, uint64_t Bytes
) const {
512 return addDereferenceableOrNullAttr(C
, ArgNo
+ FirstArgIndex
, Bytes
);
515 /// Add the allocsize attribute to the attribute set at the given index.
516 /// Returns a new list because attribute lists are immutable.
517 LLVM_NODISCARD AttributeList
518 addAllocSizeAttr(LLVMContext
&C
, unsigned Index
, unsigned ElemSizeArg
,
519 const Optional
<unsigned> &NumElemsArg
);
521 /// Add the allocsize attribute to the attribute set at the given arg index.
522 /// Returns a new list because attribute lists are immutable.
523 LLVM_NODISCARD AttributeList
524 addAllocSizeParamAttr(LLVMContext
&C
, unsigned ArgNo
, unsigned ElemSizeArg
,
525 const Optional
<unsigned> &NumElemsArg
) {
526 return addAllocSizeAttr(C
, ArgNo
+ FirstArgIndex
, ElemSizeArg
, NumElemsArg
);
529 //===--------------------------------------------------------------------===//
530 // AttributeList Accessors
531 //===--------------------------------------------------------------------===//
533 /// Retrieve the LLVM context.
534 LLVMContext
&getContext() const;
536 /// The attributes for the specified index are returned.
537 AttributeSet
getAttributes(unsigned Index
) const;
539 /// The attributes for the argument or parameter at the given index are
541 AttributeSet
getParamAttributes(unsigned ArgNo
) const;
543 /// The attributes for the ret value are returned.
544 AttributeSet
getRetAttributes() const;
546 /// The function attributes are returned.
547 AttributeSet
getFnAttributes() const;
549 /// Return true if the attribute exists at the given index.
550 bool hasAttribute(unsigned Index
, Attribute::AttrKind Kind
) const;
552 /// Return true if the attribute exists at the given index.
553 bool hasAttribute(unsigned Index
, StringRef Kind
) const;
555 /// Return true if attribute exists at the given index.
556 bool hasAttributes(unsigned Index
) const;
558 /// Return true if the attribute exists for the given argument
559 bool hasParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
560 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
563 /// Return true if the attribute exists for the given argument
564 bool hasParamAttr(unsigned ArgNo
, StringRef Kind
) const {
565 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
568 /// Return true if attributes exists for the given argument
569 bool hasParamAttrs(unsigned ArgNo
) const {
570 return hasAttributes(ArgNo
+ FirstArgIndex
);
573 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
575 bool hasFnAttribute(Attribute::AttrKind Kind
) const;
577 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
579 bool hasFnAttribute(StringRef Kind
) const;
581 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
582 bool hasParamAttribute(unsigned ArgNo
, Attribute::AttrKind Kind
) const;
584 /// Return true if the specified attribute is set for at least one
585 /// parameter or for the return value. If Index is not nullptr, the index
586 /// of a parameter with the specified attribute is provided.
587 bool hasAttrSomewhere(Attribute::AttrKind Kind
,
588 unsigned *Index
= nullptr) const;
590 /// Return the attribute object that exists at the given index.
591 Attribute
getAttribute(unsigned Index
, Attribute::AttrKind Kind
) const;
593 /// Return the attribute object that exists at the given index.
594 Attribute
getAttribute(unsigned Index
, StringRef Kind
) const;
596 /// Return the attribute object that exists at the arg index.
597 Attribute
getParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
598 return getAttribute(ArgNo
+ FirstArgIndex
, Kind
);
601 /// Return the attribute object that exists at the given index.
602 Attribute
getParamAttr(unsigned ArgNo
, StringRef Kind
) const {
603 return getAttribute(ArgNo
+ FirstArgIndex
, Kind
);
606 /// Return the alignment of the return value.
607 unsigned getRetAlignment() const;
609 /// Return the alignment for the specified function parameter.
610 unsigned getParamAlignment(unsigned ArgNo
) const;
612 /// Return the byval type for the specified function parameter.
613 Type
*getParamByValType(unsigned ArgNo
) const;
615 /// Get the stack alignment.
616 unsigned getStackAlignment(unsigned Index
) const;
618 /// Get the number of dereferenceable bytes (or zero if unknown).
619 uint64_t getDereferenceableBytes(unsigned Index
) const;
621 /// Get the number of dereferenceable bytes (or zero if unknown) of an
623 uint64_t getParamDereferenceableBytes(unsigned ArgNo
) const {
624 return getDereferenceableBytes(ArgNo
+ FirstArgIndex
);
627 /// Get the number of dereferenceable_or_null bytes (or zero if
629 uint64_t getDereferenceableOrNullBytes(unsigned Index
) const;
631 /// Get the number of dereferenceable_or_null bytes (or zero if
632 /// unknown) of an arg.
633 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo
) const {
634 return getDereferenceableOrNullBytes(ArgNo
+ FirstArgIndex
);
637 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
638 std::pair
<unsigned, Optional
<unsigned>>
639 getAllocSizeArgs(unsigned Index
) const;
641 /// Return the attributes at the index as a string.
642 std::string
getAsString(unsigned Index
, bool InAttrGrp
= false) const;
644 //===--------------------------------------------------------------------===//
645 // AttributeList Introspection
646 //===--------------------------------------------------------------------===//
648 using iterator
= const AttributeSet
*;
650 iterator
begin() const;
651 iterator
end() const;
653 unsigned getNumAttrSets() const;
655 /// Use these to iterate over the valid attribute indices.
656 unsigned index_begin() const { return AttributeList::FunctionIndex
; }
657 unsigned index_end() const { return getNumAttrSets() - 1; }
659 /// operator==/!= - Provide equality predicates.
660 bool operator==(const AttributeList
&RHS
) const { return pImpl
== RHS
.pImpl
; }
661 bool operator!=(const AttributeList
&RHS
) const { return pImpl
!= RHS
.pImpl
; }
663 /// Return a raw pointer that uniquely identifies this attribute list.
664 void *getRawPointer() const {
668 /// Return true if there are no attributes.
669 bool isEmpty() const { return pImpl
== nullptr; }
674 //===----------------------------------------------------------------------===//
676 /// Provide DenseMapInfo for AttributeList.
677 template <> struct DenseMapInfo
<AttributeList
> {
678 static AttributeList
getEmptyKey() {
679 auto Val
= static_cast<uintptr_t>(-1);
680 Val
<<= PointerLikeTypeTraits
<void*>::NumLowBitsAvailable
;
681 return AttributeList(reinterpret_cast<AttributeListImpl
*>(Val
));
684 static AttributeList
getTombstoneKey() {
685 auto Val
= static_cast<uintptr_t>(-2);
686 Val
<<= PointerLikeTypeTraits
<void*>::NumLowBitsAvailable
;
687 return AttributeList(reinterpret_cast<AttributeListImpl
*>(Val
));
690 static unsigned getHashValue(AttributeList AS
) {
691 return (unsigned((uintptr_t)AS
.pImpl
) >> 4) ^
692 (unsigned((uintptr_t)AS
.pImpl
) >> 9);
695 static bool isEqual(AttributeList LHS
, AttributeList RHS
) {
700 //===----------------------------------------------------------------------===//
702 /// This class is used in conjunction with the Attribute::get method to
703 /// create an Attribute object. The object itself is uniquified. The Builder's
704 /// value, however, is not. So this can be used as a quick way to test for
705 /// equality, presence of attributes, etc.
707 std::bitset
<Attribute::EndAttrKinds
> Attrs
;
708 std::map
<std::string
, std::string
, std::less
<>> TargetDepAttrs
;
709 MaybeAlign Alignment
;
710 MaybeAlign StackAlignment
;
711 uint64_t DerefBytes
= 0;
712 uint64_t DerefOrNullBytes
= 0;
713 uint64_t AllocSizeArgs
= 0;
714 Type
*ByValType
= nullptr;
717 AttrBuilder() = default;
719 AttrBuilder(const Attribute
&A
) {
723 AttrBuilder(AttributeList AS
, unsigned Idx
);
724 AttrBuilder(AttributeSet AS
);
728 /// Add an attribute to the builder.
729 AttrBuilder
&addAttribute(Attribute::AttrKind Val
);
731 /// Add the Attribute object to the builder.
732 AttrBuilder
&addAttribute(Attribute A
);
734 /// Add the target-dependent attribute to the builder.
735 AttrBuilder
&addAttribute(StringRef A
, StringRef V
= StringRef());
737 /// Remove an attribute from the builder.
738 AttrBuilder
&removeAttribute(Attribute::AttrKind Val
);
740 /// Remove the attributes from the builder.
741 AttrBuilder
&removeAttributes(AttributeList A
, uint64_t WithoutIndex
);
743 /// Remove the target-dependent attribute to the builder.
744 AttrBuilder
&removeAttribute(StringRef A
);
746 /// Add the attributes from the builder.
747 AttrBuilder
&merge(const AttrBuilder
&B
);
749 /// Remove the attributes from the builder.
750 AttrBuilder
&remove(const AttrBuilder
&B
);
752 /// Return true if the builder has any attribute that's in the
753 /// specified builder.
754 bool overlaps(const AttrBuilder
&B
) const;
756 /// Return true if the builder has the specified attribute.
757 bool contains(Attribute::AttrKind A
) const {
758 assert((unsigned)A
< Attribute::EndAttrKinds
&& "Attribute out of range!");
762 /// Return true if the builder has the specified target-dependent
764 bool contains(StringRef A
) const;
766 /// Return true if the builder has IR-level attributes.
767 bool hasAttributes() const;
769 /// Return true if the builder has any attribute that's in the
770 /// specified attribute.
771 bool hasAttributes(AttributeList A
, uint64_t Index
) const;
773 /// Return true if the builder has an alignment attribute.
774 bool hasAlignmentAttr() const;
776 /// Retrieve the alignment attribute, if it exists.
777 uint64_t getAlignment() const { return Alignment
? Alignment
->value() : 0; }
779 /// Retrieve the stack alignment attribute, if it exists.
780 uint64_t getStackAlignment() const {
781 return StackAlignment
? StackAlignment
->value() : 0;
784 /// Retrieve the number of dereferenceable bytes, if the
785 /// dereferenceable attribute exists (zero is returned otherwise).
786 uint64_t getDereferenceableBytes() const { return DerefBytes
; }
788 /// Retrieve the number of dereferenceable_or_null bytes, if the
789 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
790 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes
; }
792 /// Retrieve the byval type.
793 Type
*getByValType() const { return ByValType
; }
795 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
796 /// doesn't exist, pair(0, 0) is returned.
797 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
799 /// This turns an int alignment (which must be a power of 2) into the
800 /// form used internally in Attribute.
801 AttrBuilder
&addAlignmentAttr(unsigned Align
);
803 /// This turns an int stack alignment (which must be a power of 2) into
804 /// the form used internally in Attribute.
805 AttrBuilder
&addStackAlignmentAttr(unsigned Align
);
807 /// This turns the number of dereferenceable bytes into the form used
808 /// internally in Attribute.
809 AttrBuilder
&addDereferenceableAttr(uint64_t Bytes
);
811 /// This turns the number of dereferenceable_or_null bytes into the
812 /// form used internally in Attribute.
813 AttrBuilder
&addDereferenceableOrNullAttr(uint64_t Bytes
);
815 /// This turns one (or two) ints into the form used internally in Attribute.
816 AttrBuilder
&addAllocSizeAttr(unsigned ElemSizeArg
,
817 const Optional
<unsigned> &NumElemsArg
);
819 /// This turns a byval type into the form used internally in Attribute.
820 AttrBuilder
&addByValAttr(Type
*Ty
);
822 /// Add an allocsize attribute, using the representation returned by
823 /// Attribute.getIntValue().
824 AttrBuilder
&addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr
);
826 /// Return true if the builder contains no target-independent
828 bool empty() const { return Attrs
.none(); }
830 // Iterators for target-dependent attributes.
831 using td_type
= std::pair
<std::string
, std::string
>;
832 using td_iterator
= std::map
<std::string
, std::string
>::iterator
;
833 using td_const_iterator
= std::map
<std::string
, std::string
>::const_iterator
;
834 using td_range
= iterator_range
<td_iterator
>;
835 using td_const_range
= iterator_range
<td_const_iterator
>;
837 td_iterator
td_begin() { return TargetDepAttrs
.begin(); }
838 td_iterator
td_end() { return TargetDepAttrs
.end(); }
840 td_const_iterator
td_begin() const { return TargetDepAttrs
.begin(); }
841 td_const_iterator
td_end() const { return TargetDepAttrs
.end(); }
843 td_range
td_attrs() { return td_range(td_begin(), td_end()); }
845 td_const_range
td_attrs() const {
846 return td_const_range(td_begin(), td_end());
849 bool td_empty() const { return TargetDepAttrs
.empty(); }
851 bool operator==(const AttrBuilder
&B
);
852 bool operator!=(const AttrBuilder
&B
) {
853 return !(*this == B
);
857 namespace AttributeFuncs
{
859 /// Which attributes cannot be applied to a type.
860 AttrBuilder
typeIncompatible(Type
*Ty
);
862 /// \returns Return true if the two functions have compatible target-independent
863 /// attributes for inlining purposes.
864 bool areInlineCompatible(const Function
&Caller
, const Function
&Callee
);
866 /// Merge caller's and callee's attributes.
867 void mergeAttributesForInlining(Function
&Caller
, const Function
&Callee
);
869 } // end namespace AttributeFuncs
871 } // end namespace llvm
873 #endif // LLVM_IR_ATTRIBUTES_H