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());
93 static Attribute
get(LLVMContext
&Context
, AttrKind Kind
, Type
*Ty
);
95 /// Return a uniquified Attribute object that has the specific
97 static Attribute
getWithAlignment(LLVMContext
&Context
, uint64_t Align
);
98 static Attribute
getWithStackAlignment(LLVMContext
&Context
, uint64_t Align
);
99 static Attribute
getWithDereferenceableBytes(LLVMContext
&Context
,
101 static Attribute
getWithDereferenceableOrNullBytes(LLVMContext
&Context
,
103 static Attribute
getWithAllocSizeArgs(LLVMContext
&Context
,
104 unsigned ElemSizeArg
,
105 const Optional
<unsigned> &NumElemsArg
);
106 static Attribute
getWithByValType(LLVMContext
&Context
, Type
*Ty
);
108 //===--------------------------------------------------------------------===//
109 // Attribute Accessors
110 //===--------------------------------------------------------------------===//
112 /// Return true if the attribute is an Attribute::AttrKind type.
113 bool isEnumAttribute() const;
115 /// Return true if the attribute is an integer attribute.
116 bool isIntAttribute() const;
118 /// Return true if the attribute is a string (target-dependent)
120 bool isStringAttribute() const;
122 /// Return true if the attribute is a type attribute.
123 bool isTypeAttribute() const;
125 /// Return true if the attribute is present.
126 bool hasAttribute(AttrKind Val
) const;
128 /// Return true if the target-dependent attribute is present.
129 bool hasAttribute(StringRef Val
) const;
131 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
132 /// requires the attribute to be an enum or integer attribute.
133 Attribute::AttrKind
getKindAsEnum() const;
135 /// Return the attribute's value as an integer. This requires that the
136 /// attribute be an integer attribute.
137 uint64_t getValueAsInt() const;
139 /// Return the attribute's kind as a string. This requires the
140 /// attribute to be a string attribute.
141 StringRef
getKindAsString() const;
143 /// Return the attribute's value as a string. This requires the
144 /// attribute to be a string attribute.
145 StringRef
getValueAsString() const;
147 /// Return the attribute's value as a Type. This requires the attribute to be
148 /// a type attribute.
149 Type
*getValueAsType() const;
151 /// Returns the alignment field of an attribute as a byte alignment
153 unsigned getAlignment() const;
155 /// Returns the stack alignment field of an attribute as a byte
157 unsigned getStackAlignment() const;
159 /// Returns the number of dereferenceable bytes from the
160 /// dereferenceable attribute.
161 uint64_t getDereferenceableBytes() const;
163 /// Returns the number of dereferenceable_or_null bytes from the
164 /// dereferenceable_or_null attribute.
165 uint64_t getDereferenceableOrNullBytes() const;
167 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
169 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
171 /// The Attribute is converted to a string of equivalent mnemonic. This
172 /// is, presumably, for writing out the mnemonics for the assembly writer.
173 std::string
getAsString(bool InAttrGrp
= false) const;
175 /// Equality and non-equality operators.
176 bool operator==(Attribute A
) const { return pImpl
== A
.pImpl
; }
177 bool operator!=(Attribute A
) const { return pImpl
!= A
.pImpl
; }
179 /// Less-than operator. Useful for sorting the attributes list.
180 bool operator<(Attribute A
) const;
182 void Profile(FoldingSetNodeID
&ID
) const {
183 ID
.AddPointer(pImpl
);
186 /// Return a raw pointer that uniquely identifies this attribute.
187 void *getRawPointer() const {
191 /// Get an attribute from a raw pointer created by getRawPointer.
192 static Attribute
fromRawPointer(void *RawPtr
) {
193 return Attribute(reinterpret_cast<AttributeImpl
*>(RawPtr
));
197 // Specialized opaque value conversions.
198 inline LLVMAttributeRef
wrap(Attribute Attr
) {
199 return reinterpret_cast<LLVMAttributeRef
>(Attr
.getRawPointer());
202 // Specialized opaque value conversions.
203 inline Attribute
unwrap(LLVMAttributeRef Attr
) {
204 return Attribute::fromRawPointer(Attr
);
207 //===----------------------------------------------------------------------===//
209 /// This class holds the attributes for a particular argument, parameter,
210 /// function, or return value. It is an immutable value type that is cheap to
211 /// copy. Adding and removing enum attributes is intended to be fast, but adding
212 /// and removing string or integer attributes involves a FoldingSet lookup.
214 friend AttributeListImpl
;
215 template <typename Ty
> friend struct DenseMapInfo
;
217 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
218 // This will allow an efficient implementation of addAttribute and
219 // removeAttribute for enum attrs.
221 /// Private implementation pointer.
222 AttributeSetNode
*SetNode
= nullptr;
225 explicit AttributeSet(AttributeSetNode
*ASN
) : SetNode(ASN
) {}
228 /// AttributeSet is a trivially copyable value type.
229 AttributeSet() = default;
230 AttributeSet(const AttributeSet
&) = default;
231 ~AttributeSet() = default;
233 static AttributeSet
get(LLVMContext
&C
, const AttrBuilder
&B
);
234 static AttributeSet
get(LLVMContext
&C
, ArrayRef
<Attribute
> Attrs
);
236 bool operator==(const AttributeSet
&O
) const { return SetNode
== O
.SetNode
; }
237 bool operator!=(const AttributeSet
&O
) const { return !(*this == O
); }
239 /// Add an argument attribute. Returns a new set because attribute sets are
241 LLVM_NODISCARD AttributeSet
addAttribute(LLVMContext
&C
,
242 Attribute::AttrKind Kind
) const;
244 /// Add a target-dependent attribute. Returns a new set because attribute sets
246 LLVM_NODISCARD AttributeSet
addAttribute(LLVMContext
&C
, StringRef Kind
,
247 StringRef Value
= StringRef()) const;
249 /// Add attributes to the attribute set. Returns a new set because attribute
250 /// sets are immutable.
251 LLVM_NODISCARD AttributeSet
addAttributes(LLVMContext
&C
,
252 AttributeSet AS
) const;
254 /// Remove the specified attribute from this set. Returns a new set because
255 /// attribute sets are immutable.
256 LLVM_NODISCARD AttributeSet
removeAttribute(LLVMContext
&C
,
257 Attribute::AttrKind Kind
) const;
259 /// Remove the specified attribute from this set. Returns a new set because
260 /// attribute sets are immutable.
261 LLVM_NODISCARD AttributeSet
removeAttribute(LLVMContext
&C
,
262 StringRef Kind
) const;
264 /// Remove the specified attributes from this set. Returns a new set because
265 /// attribute sets are immutable.
266 LLVM_NODISCARD AttributeSet
267 removeAttributes(LLVMContext
&C
, const AttrBuilder
&AttrsToRemove
) const;
269 /// Return the number of attributes in this set.
270 unsigned getNumAttributes() const;
272 /// Return true if attributes exists in this set.
273 bool hasAttributes() const { return SetNode
!= nullptr; }
275 /// Return true if the attribute exists in this set.
276 bool hasAttribute(Attribute::AttrKind Kind
) const;
278 /// Return true if the attribute exists in this set.
279 bool hasAttribute(StringRef Kind
) const;
281 /// Return the attribute object.
282 Attribute
getAttribute(Attribute::AttrKind Kind
) const;
284 /// Return the target-dependent attribute object.
285 Attribute
getAttribute(StringRef Kind
) const;
287 unsigned getAlignment() const;
288 unsigned getStackAlignment() const;
289 uint64_t getDereferenceableBytes() const;
290 uint64_t getDereferenceableOrNullBytes() const;
291 Type
*getByValType() const;
292 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
293 std::string
getAsString(bool InAttrGrp
= false) const;
295 using iterator
= const Attribute
*;
297 iterator
begin() const;
298 iterator
end() const;
299 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
304 //===----------------------------------------------------------------------===//
306 /// Provide DenseMapInfo for AttributeSet.
307 template <> struct DenseMapInfo
<AttributeSet
> {
308 static AttributeSet
getEmptyKey() {
309 auto Val
= static_cast<uintptr_t>(-1);
310 Val
<<= PointerLikeTypeTraits
<void *>::NumLowBitsAvailable
;
311 return AttributeSet(reinterpret_cast<AttributeSetNode
*>(Val
));
314 static AttributeSet
getTombstoneKey() {
315 auto Val
= static_cast<uintptr_t>(-2);
316 Val
<<= PointerLikeTypeTraits
<void *>::NumLowBitsAvailable
;
317 return AttributeSet(reinterpret_cast<AttributeSetNode
*>(Val
));
320 static unsigned getHashValue(AttributeSet AS
) {
321 return (unsigned((uintptr_t)AS
.SetNode
) >> 4) ^
322 (unsigned((uintptr_t)AS
.SetNode
) >> 9);
325 static bool isEqual(AttributeSet LHS
, AttributeSet RHS
) { return LHS
== RHS
; }
328 //===----------------------------------------------------------------------===//
330 /// This class holds the attributes for a function, its return value, and
331 /// its parameters. You access the attributes for each of them via an index into
332 /// the AttributeList object. The function attributes are at index
333 /// `AttributeList::FunctionIndex', the return value is at index
334 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
335 /// index `AttributeList::FirstArgIndex'.
336 class AttributeList
{
338 enum AttrIndex
: unsigned {
345 friend class AttrBuilder
;
346 friend class AttributeListImpl
;
347 friend class AttributeSet
;
348 friend class AttributeSetNode
;
349 template <typename Ty
> friend struct DenseMapInfo
;
351 /// The attributes that we are managing. This can be null to represent
352 /// the empty attributes list.
353 AttributeListImpl
*pImpl
= nullptr;
356 /// Create an AttributeList with the specified parameters in it.
357 static AttributeList
get(LLVMContext
&C
,
358 ArrayRef
<std::pair
<unsigned, Attribute
>> Attrs
);
359 static AttributeList
get(LLVMContext
&C
,
360 ArrayRef
<std::pair
<unsigned, AttributeSet
>> Attrs
);
362 /// Create an AttributeList from attribute sets for a function, its
363 /// return value, and all of its arguments.
364 static AttributeList
get(LLVMContext
&C
, AttributeSet FnAttrs
,
365 AttributeSet RetAttrs
,
366 ArrayRef
<AttributeSet
> ArgAttrs
);
369 explicit AttributeList(AttributeListImpl
*LI
) : pImpl(LI
) {}
371 static AttributeList
getImpl(LLVMContext
&C
, ArrayRef
<AttributeSet
> AttrSets
);
374 AttributeList() = default;
376 //===--------------------------------------------------------------------===//
377 // AttributeList Construction and Mutation
378 //===--------------------------------------------------------------------===//
380 /// Return an AttributeList with the specified parameters in it.
381 static AttributeList
get(LLVMContext
&C
, ArrayRef
<AttributeList
> Attrs
);
382 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
383 ArrayRef
<Attribute::AttrKind
> Kinds
);
384 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
385 ArrayRef
<StringRef
> Kind
);
386 static AttributeList
get(LLVMContext
&C
, unsigned Index
,
387 const AttrBuilder
&B
);
389 /// Add an attribute to the attribute set at the given index.
390 /// Returns a new list because attribute lists are immutable.
391 LLVM_NODISCARD AttributeList
addAttribute(LLVMContext
&C
, unsigned Index
,
392 Attribute::AttrKind Kind
) const;
394 /// Add an attribute to the attribute set at the given index.
395 /// Returns a new list because attribute lists are immutable.
396 LLVM_NODISCARD AttributeList
397 addAttribute(LLVMContext
&C
, unsigned Index
, StringRef Kind
,
398 StringRef Value
= StringRef()) const;
400 /// Add an attribute to the attribute set at the given index.
401 /// Returns a new list because attribute lists are immutable.
402 LLVM_NODISCARD AttributeList
addAttribute(LLVMContext
&C
, unsigned Index
,
405 /// Add attributes to the attribute set at the given index.
406 /// Returns a new list because attribute lists are immutable.
407 LLVM_NODISCARD AttributeList
addAttributes(LLVMContext
&C
, unsigned Index
,
408 const AttrBuilder
&B
) const;
410 /// Add an argument attribute to the list. Returns a new list because
411 /// attribute lists are immutable.
412 LLVM_NODISCARD AttributeList
addParamAttribute(
413 LLVMContext
&C
, unsigned ArgNo
, Attribute::AttrKind Kind
) const {
414 return addAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
417 /// Add an argument attribute to the list. Returns a new list because
418 /// attribute lists are immutable.
419 LLVM_NODISCARD AttributeList
420 addParamAttribute(LLVMContext
&C
, unsigned ArgNo
, StringRef Kind
,
421 StringRef Value
= StringRef()) const {
422 return addAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
, Value
);
425 /// Add an attribute to the attribute list at the given arg indices. Returns a
426 /// new list because attribute lists are immutable.
427 LLVM_NODISCARD AttributeList
addParamAttribute(LLVMContext
&C
,
428 ArrayRef
<unsigned> ArgNos
,
431 /// Add an argument attribute to the list. Returns a new list because
432 /// attribute lists are immutable.
433 LLVM_NODISCARD AttributeList
addParamAttributes(LLVMContext
&C
,
435 const AttrBuilder
&B
) const {
436 return addAttributes(C
, ArgNo
+ FirstArgIndex
, B
);
439 /// Remove the specified attribute at the specified index from this
440 /// attribute list. Returns a new list because attribute lists are immutable.
441 LLVM_NODISCARD AttributeList
removeAttribute(LLVMContext
&C
, unsigned Index
,
442 Attribute::AttrKind Kind
) const;
444 /// Remove the specified attribute at the specified index from this
445 /// attribute list. Returns a new list because attribute lists are immutable.
446 LLVM_NODISCARD AttributeList
removeAttribute(LLVMContext
&C
, unsigned Index
,
447 StringRef Kind
) const;
449 /// Remove the specified attributes at the specified index from this
450 /// attribute list. Returns a new list because attribute lists are immutable.
451 LLVM_NODISCARD AttributeList
removeAttributes(
452 LLVMContext
&C
, unsigned Index
, const AttrBuilder
&AttrsToRemove
) const;
454 /// Remove all attributes at the specified index from this
455 /// attribute list. Returns a new list because attribute lists are immutable.
456 LLVM_NODISCARD AttributeList
removeAttributes(LLVMContext
&C
,
457 unsigned Index
) const;
459 /// Remove the specified attribute at the specified arg index from this
460 /// attribute list. Returns a new list because attribute lists are immutable.
461 LLVM_NODISCARD AttributeList
removeParamAttribute(
462 LLVMContext
&C
, unsigned ArgNo
, Attribute::AttrKind Kind
) const {
463 return removeAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
466 /// Remove the specified attribute at the specified arg index from this
467 /// attribute list. Returns a new list because attribute lists are immutable.
468 LLVM_NODISCARD AttributeList
removeParamAttribute(LLVMContext
&C
,
470 StringRef Kind
) const {
471 return removeAttribute(C
, ArgNo
+ FirstArgIndex
, Kind
);
474 /// Remove the specified attribute at the specified arg index from this
475 /// attribute list. Returns a new list because attribute lists are immutable.
476 LLVM_NODISCARD AttributeList
removeParamAttributes(
477 LLVMContext
&C
, unsigned ArgNo
, const AttrBuilder
&AttrsToRemove
) const {
478 return removeAttributes(C
, ArgNo
+ FirstArgIndex
, AttrsToRemove
);
481 /// Remove all attributes at the specified arg index from this
482 /// attribute list. Returns a new list because attribute lists are immutable.
483 LLVM_NODISCARD AttributeList
removeParamAttributes(LLVMContext
&C
,
484 unsigned ArgNo
) const {
485 return removeAttributes(C
, ArgNo
+ FirstArgIndex
);
488 /// \brief Add the dereferenceable attribute to the attribute set at the given
489 /// index. Returns a new list because attribute lists are immutable.
490 LLVM_NODISCARD AttributeList
addDereferenceableAttr(LLVMContext
&C
,
492 uint64_t Bytes
) const;
494 /// \brief Add the dereferenceable attribute to the attribute set at the given
495 /// arg index. Returns a new list because attribute lists are immutable.
496 LLVM_NODISCARD AttributeList
addDereferenceableParamAttr(
497 LLVMContext
&C
, unsigned ArgNo
, uint64_t Bytes
) const {
498 return addDereferenceableAttr(C
, ArgNo
+ FirstArgIndex
, Bytes
);
501 /// Add the dereferenceable_or_null attribute to the attribute set at
502 /// the given index. Returns a new list because attribute lists are immutable.
503 LLVM_NODISCARD AttributeList
addDereferenceableOrNullAttr(
504 LLVMContext
&C
, unsigned Index
, uint64_t Bytes
) const;
506 /// Add the dereferenceable_or_null attribute to the attribute set at
507 /// the given arg index. Returns a new list because attribute lists are
509 LLVM_NODISCARD AttributeList
addDereferenceableOrNullParamAttr(
510 LLVMContext
&C
, unsigned ArgNo
, uint64_t Bytes
) const {
511 return addDereferenceableOrNullAttr(C
, ArgNo
+ FirstArgIndex
, Bytes
);
514 /// Add the allocsize attribute to the attribute set at the given index.
515 /// Returns a new list because attribute lists are immutable.
516 LLVM_NODISCARD AttributeList
517 addAllocSizeAttr(LLVMContext
&C
, unsigned Index
, unsigned ElemSizeArg
,
518 const Optional
<unsigned> &NumElemsArg
);
520 /// Add the allocsize attribute to the attribute set at the given arg index.
521 /// Returns a new list because attribute lists are immutable.
522 LLVM_NODISCARD AttributeList
523 addAllocSizeParamAttr(LLVMContext
&C
, unsigned ArgNo
, unsigned ElemSizeArg
,
524 const Optional
<unsigned> &NumElemsArg
) {
525 return addAllocSizeAttr(C
, ArgNo
+ FirstArgIndex
, ElemSizeArg
, NumElemsArg
);
528 //===--------------------------------------------------------------------===//
529 // AttributeList Accessors
530 //===--------------------------------------------------------------------===//
532 /// Retrieve the LLVM context.
533 LLVMContext
&getContext() const;
535 /// The attributes for the specified index are returned.
536 AttributeSet
getAttributes(unsigned Index
) const;
538 /// The attributes for the argument or parameter at the given index are
540 AttributeSet
getParamAttributes(unsigned ArgNo
) const;
542 /// The attributes for the ret value are returned.
543 AttributeSet
getRetAttributes() const;
545 /// The function attributes are returned.
546 AttributeSet
getFnAttributes() const;
548 /// Return true if the attribute exists at the given index.
549 bool hasAttribute(unsigned Index
, Attribute::AttrKind Kind
) const;
551 /// Return true if the attribute exists at the given index.
552 bool hasAttribute(unsigned Index
, StringRef Kind
) const;
554 /// Return true if attribute exists at the given index.
555 bool hasAttributes(unsigned Index
) const;
557 /// Return true if the attribute exists for the given argument
558 bool hasParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
559 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
562 /// Return true if the attribute exists for the given argument
563 bool hasParamAttr(unsigned ArgNo
, StringRef Kind
) const {
564 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
567 /// Return true if attributes exists for the given argument
568 bool hasParamAttrs(unsigned ArgNo
) const {
569 return hasAttributes(ArgNo
+ FirstArgIndex
);
572 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
574 bool hasFnAttribute(Attribute::AttrKind Kind
) const;
576 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
578 bool hasFnAttribute(StringRef Kind
) const;
580 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
581 bool hasParamAttribute(unsigned ArgNo
, Attribute::AttrKind Kind
) const;
583 /// Return true if the specified attribute is set for at least one
584 /// parameter or for the return value. If Index is not nullptr, the index
585 /// of a parameter with the specified attribute is provided.
586 bool hasAttrSomewhere(Attribute::AttrKind Kind
,
587 unsigned *Index
= nullptr) const;
589 /// Return the attribute object that exists at the given index.
590 Attribute
getAttribute(unsigned Index
, Attribute::AttrKind Kind
) const;
592 /// Return the attribute object that exists at the given index.
593 Attribute
getAttribute(unsigned Index
, StringRef Kind
) const;
595 /// Return the attribute object that exists at the arg index.
596 Attribute
getParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
597 return getAttribute(ArgNo
+ FirstArgIndex
, Kind
);
600 /// Return the attribute object that exists at the given index.
601 Attribute
getParamAttr(unsigned ArgNo
, StringRef Kind
) const {
602 return getAttribute(ArgNo
+ FirstArgIndex
, Kind
);
605 /// Return the alignment of the return value.
606 unsigned getRetAlignment() const;
608 /// Return the alignment for the specified function parameter.
609 unsigned getParamAlignment(unsigned ArgNo
) const;
611 /// Return the byval type for the specified function parameter.
612 Type
*getParamByValType(unsigned ArgNo
) const;
614 /// Get the stack alignment.
615 unsigned getStackAlignment(unsigned Index
) const;
617 /// Get the number of dereferenceable bytes (or zero if unknown).
618 uint64_t getDereferenceableBytes(unsigned Index
) const;
620 /// Get the number of dereferenceable bytes (or zero if unknown) of an
622 uint64_t getParamDereferenceableBytes(unsigned ArgNo
) const {
623 return getDereferenceableBytes(ArgNo
+ FirstArgIndex
);
626 /// Get the number of dereferenceable_or_null bytes (or zero if
628 uint64_t getDereferenceableOrNullBytes(unsigned Index
) const;
630 /// Get the number of dereferenceable_or_null bytes (or zero if
631 /// unknown) of an arg.
632 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo
) const {
633 return getDereferenceableOrNullBytes(ArgNo
+ FirstArgIndex
);
636 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
637 std::pair
<unsigned, Optional
<unsigned>>
638 getAllocSizeArgs(unsigned Index
) const;
640 /// Return the attributes at the index as a string.
641 std::string
getAsString(unsigned Index
, bool InAttrGrp
= false) const;
643 //===--------------------------------------------------------------------===//
644 // AttributeList Introspection
645 //===--------------------------------------------------------------------===//
647 using iterator
= const AttributeSet
*;
649 iterator
begin() const;
650 iterator
end() const;
652 unsigned getNumAttrSets() const;
654 /// Use these to iterate over the valid attribute indices.
655 unsigned index_begin() const { return AttributeList::FunctionIndex
; }
656 unsigned index_end() const { return getNumAttrSets() - 1; }
658 /// operator==/!= - Provide equality predicates.
659 bool operator==(const AttributeList
&RHS
) const { return pImpl
== RHS
.pImpl
; }
660 bool operator!=(const AttributeList
&RHS
) const { return pImpl
!= RHS
.pImpl
; }
662 /// Return a raw pointer that uniquely identifies this attribute list.
663 void *getRawPointer() const {
667 /// Return true if there are no attributes.
668 bool isEmpty() const { return pImpl
== nullptr; }
673 //===----------------------------------------------------------------------===//
675 /// Provide DenseMapInfo for AttributeList.
676 template <> struct DenseMapInfo
<AttributeList
> {
677 static AttributeList
getEmptyKey() {
678 auto Val
= static_cast<uintptr_t>(-1);
679 Val
<<= PointerLikeTypeTraits
<void*>::NumLowBitsAvailable
;
680 return AttributeList(reinterpret_cast<AttributeListImpl
*>(Val
));
683 static AttributeList
getTombstoneKey() {
684 auto Val
= static_cast<uintptr_t>(-2);
685 Val
<<= PointerLikeTypeTraits
<void*>::NumLowBitsAvailable
;
686 return AttributeList(reinterpret_cast<AttributeListImpl
*>(Val
));
689 static unsigned getHashValue(AttributeList AS
) {
690 return (unsigned((uintptr_t)AS
.pImpl
) >> 4) ^
691 (unsigned((uintptr_t)AS
.pImpl
) >> 9);
694 static bool isEqual(AttributeList LHS
, AttributeList RHS
) {
699 //===----------------------------------------------------------------------===//
701 /// This class is used in conjunction with the Attribute::get method to
702 /// create an Attribute object. The object itself is uniquified. The Builder's
703 /// value, however, is not. So this can be used as a quick way to test for
704 /// equality, presence of attributes, etc.
706 std::bitset
<Attribute::EndAttrKinds
> Attrs
;
707 std::map
<std::string
, std::string
> TargetDepAttrs
;
708 uint64_t Alignment
= 0;
709 uint64_t StackAlignment
= 0;
710 uint64_t DerefBytes
= 0;
711 uint64_t DerefOrNullBytes
= 0;
712 uint64_t AllocSizeArgs
= 0;
713 Type
*ByValType
= nullptr;
716 AttrBuilder() = default;
718 AttrBuilder(const Attribute
&A
) {
722 AttrBuilder(AttributeList AS
, unsigned Idx
);
723 AttrBuilder(AttributeSet AS
);
727 /// Add an attribute to the builder.
728 AttrBuilder
&addAttribute(Attribute::AttrKind Val
);
730 /// Add the Attribute object to the builder.
731 AttrBuilder
&addAttribute(Attribute A
);
733 /// Add the target-dependent attribute to the builder.
734 AttrBuilder
&addAttribute(StringRef A
, StringRef V
= StringRef());
736 /// Remove an attribute from the builder.
737 AttrBuilder
&removeAttribute(Attribute::AttrKind Val
);
739 /// Remove the attributes from the builder.
740 AttrBuilder
&removeAttributes(AttributeList A
, uint64_t WithoutIndex
);
742 /// Remove the target-dependent attribute to the builder.
743 AttrBuilder
&removeAttribute(StringRef A
);
745 /// Add the attributes from the builder.
746 AttrBuilder
&merge(const AttrBuilder
&B
);
748 /// Remove the attributes from the builder.
749 AttrBuilder
&remove(const AttrBuilder
&B
);
751 /// Return true if the builder has any attribute that's in the
752 /// specified builder.
753 bool overlaps(const AttrBuilder
&B
) const;
755 /// Return true if the builder has the specified attribute.
756 bool contains(Attribute::AttrKind A
) const {
757 assert((unsigned)A
< Attribute::EndAttrKinds
&& "Attribute out of range!");
761 /// Return true if the builder has the specified target-dependent
763 bool contains(StringRef A
) const;
765 /// Return true if the builder has IR-level attributes.
766 bool hasAttributes() const;
768 /// Return true if the builder has any attribute that's in the
769 /// specified attribute.
770 bool hasAttributes(AttributeList A
, uint64_t Index
) const;
772 /// Return true if the builder has an alignment attribute.
773 bool hasAlignmentAttr() const;
775 /// Retrieve the alignment attribute, if it exists.
776 uint64_t getAlignment() const { return Alignment
; }
778 /// Retrieve the stack alignment attribute, if it exists.
779 uint64_t getStackAlignment() const { return StackAlignment
; }
781 /// Retrieve the number of dereferenceable bytes, if the
782 /// dereferenceable attribute exists (zero is returned otherwise).
783 uint64_t getDereferenceableBytes() const { return DerefBytes
; }
785 /// Retrieve the number of dereferenceable_or_null bytes, if the
786 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
787 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes
; }
789 /// Retrieve the byval type.
790 Type
*getByValType() const { return ByValType
; }
792 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
793 /// doesn't exist, pair(0, 0) is returned.
794 std::pair
<unsigned, Optional
<unsigned>> getAllocSizeArgs() const;
796 /// This turns an int alignment (which must be a power of 2) into the
797 /// form used internally in Attribute.
798 AttrBuilder
&addAlignmentAttr(unsigned Align
);
800 /// This turns an int stack alignment (which must be a power of 2) into
801 /// the form used internally in Attribute.
802 AttrBuilder
&addStackAlignmentAttr(unsigned Align
);
804 /// This turns the number of dereferenceable bytes into the form used
805 /// internally in Attribute.
806 AttrBuilder
&addDereferenceableAttr(uint64_t Bytes
);
808 /// This turns the number of dereferenceable_or_null bytes into the
809 /// form used internally in Attribute.
810 AttrBuilder
&addDereferenceableOrNullAttr(uint64_t Bytes
);
812 /// This turns one (or two) ints into the form used internally in Attribute.
813 AttrBuilder
&addAllocSizeAttr(unsigned ElemSizeArg
,
814 const Optional
<unsigned> &NumElemsArg
);
816 /// This turns a byval type into the form used internally in Attribute.
817 AttrBuilder
&addByValAttr(Type
*Ty
);
819 /// Add an allocsize attribute, using the representation returned by
820 /// Attribute.getIntValue().
821 AttrBuilder
&addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr
);
823 /// Return true if the builder contains no target-independent
825 bool empty() const { return Attrs
.none(); }
827 // Iterators for target-dependent attributes.
828 using td_type
= std::pair
<std::string
, std::string
>;
829 using td_iterator
= std::map
<std::string
, std::string
>::iterator
;
830 using td_const_iterator
= std::map
<std::string
, std::string
>::const_iterator
;
831 using td_range
= iterator_range
<td_iterator
>;
832 using td_const_range
= iterator_range
<td_const_iterator
>;
834 td_iterator
td_begin() { return TargetDepAttrs
.begin(); }
835 td_iterator
td_end() { return TargetDepAttrs
.end(); }
837 td_const_iterator
td_begin() const { return TargetDepAttrs
.begin(); }
838 td_const_iterator
td_end() const { return TargetDepAttrs
.end(); }
840 td_range
td_attrs() { return td_range(td_begin(), td_end()); }
842 td_const_range
td_attrs() const {
843 return td_const_range(td_begin(), td_end());
846 bool td_empty() const { return TargetDepAttrs
.empty(); }
848 bool operator==(const AttrBuilder
&B
);
849 bool operator!=(const AttrBuilder
&B
) {
850 return !(*this == B
);
854 namespace AttributeFuncs
{
856 /// Which attributes cannot be applied to a type.
857 AttrBuilder
typeIncompatible(Type
*Ty
);
859 /// \returns Return true if the two functions have compatible target-independent
860 /// attributes for inlining purposes.
861 bool areInlineCompatible(const Function
&Caller
, const Function
&Callee
);
863 /// Merge caller's and callee's attributes.
864 void mergeAttributesForInlining(Function
&Caller
, const Function
&Callee
);
866 } // end namespace AttributeFuncs
868 } // end namespace llvm
870 #endif // LLVM_IR_ATTRIBUTES_H