[InstCombine] Signed saturation patterns
[llvm-complete.git] / include / llvm / IR / Attributes.h
blobe6b280465f72e337c99ae89254ab9746eeb681cc
1 //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
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"
27 #include <bitset>
28 #include <cassert>
29 #include <cstdint>
30 #include <map>
31 #include <string>
32 #include <utility>
34 namespace llvm {
36 class AttrBuilder;
37 class AttributeImpl;
38 class AttributeListImpl;
39 class AttributeSetNode;
40 template<typename T> struct DenseMapInfo;
41 class Function;
42 class LLVMContext;
43 class Type;
45 //===----------------------------------------------------------------------===//
46 /// \class
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.
51 class Attribute {
52 public:
53 /// This enumeration lists the attributes that can be associated with
54 /// parameters, function results, or the function itself.
55 ///
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.
59 ///
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:
63 ///
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.
70 enum AttrKind {
71 // IR-Level Attributes
72 None, ///< No attributes have been set
73 #define GET_ATTR_ENUM
74 #include "llvm/IR/Attributes.inc"
75 EndAttrKinds ///< Sentinal value useful for loops
78 private:
79 AttributeImpl *pImpl = nullptr;
81 Attribute(AttributeImpl *A) : pImpl(A) {}
83 public:
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
97 /// alignment set.
98 static Attribute getWithAlignment(LLVMContext &Context, Align Alignment);
99 static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment);
100 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
101 uint64_t Bytes);
102 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
103 uint64_t Bytes);
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)
120 /// attribute.
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
153 /// value.
154 MaybeAlign getAlignment() const;
156 /// Returns the stack alignment field of an attribute as a byte
157 /// alignment value.
158 MaybeAlign 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)
169 /// if not known).
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 {
189 return pImpl;
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 //===----------------------------------------------------------------------===//
209 /// \class
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.
214 class AttributeSet {
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;
225 private:
226 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
228 public:
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
241 /// immutable.
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
246 /// are immutable.
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 MaybeAlign getAlignment() const;
289 MaybeAlign 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)
301 void dump() const;
302 #endif
305 //===----------------------------------------------------------------------===//
306 /// \class
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 //===----------------------------------------------------------------------===//
330 /// \class
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 {
338 public:
339 enum AttrIndex : unsigned {
340 ReturnIndex = 0U,
341 FunctionIndex = ~0U,
342 FirstArgIndex = 1,
345 private:
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;
356 public:
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);
369 private:
370 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
372 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
374 public:
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,
404 Attribute A) const;
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,
430 Attribute A) const;
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,
435 unsigned ArgNo,
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,
470 unsigned ArgNo,
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,
492 unsigned Index,
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
509 /// immutable.
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
540 /// returned.
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
574 /// may be faster.
575 bool hasFnAttribute(Attribute::AttrKind Kind) const;
577 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
578 /// may be faster.
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 MaybeAlign getRetAlignment() const;
609 /// Return the alignment for the specified function parameter.
610 MaybeAlign 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 MaybeAlign 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
622 /// arg.
623 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
624 return getDereferenceableBytes(ArgNo + FirstArgIndex);
627 /// Get the number of dereferenceable_or_null bytes (or zero if
628 /// unknown).
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 {
665 return pImpl;
668 /// Return true if there are no attributes.
669 bool isEmpty() const { return pImpl == nullptr; }
671 void dump() const;
674 //===----------------------------------------------------------------------===//
675 /// \class
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) {
696 return LHS == RHS;
700 //===----------------------------------------------------------------------===//
701 /// \class
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.
706 class AttrBuilder {
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;
716 public:
717 AttrBuilder() = default;
719 AttrBuilder(const Attribute &A) {
720 addAttribute(A);
723 AttrBuilder(AttributeList AS, unsigned Idx);
724 AttrBuilder(AttributeSet AS);
726 void clear();
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!");
759 return Attrs[A];
762 /// Return true if the builder has the specified target-dependent
763 /// attribute.
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 MaybeAlign getAlignment() const { return Alignment; }
779 /// Retrieve the stack alignment attribute, if it exists.
780 MaybeAlign getStackAlignment() const { return StackAlignment; }
782 /// Retrieve the number of dereferenceable bytes, if the
783 /// dereferenceable attribute exists (zero is returned otherwise).
784 uint64_t getDereferenceableBytes() const { return DerefBytes; }
786 /// Retrieve the number of dereferenceable_or_null bytes, if the
787 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
788 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
790 /// Retrieve the byval type.
791 Type *getByValType() const { return ByValType; }
793 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
794 /// doesn't exist, pair(0, 0) is returned.
795 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
797 /// This turns an alignment into the form used internally in Attribute.
798 /// This call has no effect if Align is not set.
799 AttrBuilder &addAlignmentAttr(MaybeAlign Align);
801 /// This turns an int alignment (which must be a power of 2) into the
802 /// form used internally in Attribute.
803 /// This call has no effect if Align is 0.
804 /// Deprecated, use the version using a MaybeAlign.
805 inline AttrBuilder &addAlignmentAttr(unsigned Align) {
806 return addAlignmentAttr(MaybeAlign(Align));
809 /// This turns a stack alignment into the form used internally in Attribute.
810 /// This call has no effect if Align is not set.
811 AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
813 /// This turns an int stack alignment (which must be a power of 2) into
814 /// the form used internally in Attribute.
815 /// This call has no effect if Align is 0.
816 /// Deprecated, use the version using a MaybeAlign.
817 inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
818 return addStackAlignmentAttr(MaybeAlign(Align));
821 /// This turns the number of dereferenceable bytes into the form used
822 /// internally in Attribute.
823 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
825 /// This turns the number of dereferenceable_or_null bytes into the
826 /// form used internally in Attribute.
827 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
829 /// This turns one (or two) ints into the form used internally in Attribute.
830 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
831 const Optional<unsigned> &NumElemsArg);
833 /// This turns a byval type into the form used internally in Attribute.
834 AttrBuilder &addByValAttr(Type *Ty);
836 /// Add an allocsize attribute, using the representation returned by
837 /// Attribute.getIntValue().
838 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
840 /// Return true if the builder contains no target-independent
841 /// attributes.
842 bool empty() const { return Attrs.none(); }
844 // Iterators for target-dependent attributes.
845 using td_type = std::pair<std::string, std::string>;
846 using td_iterator = std::map<std::string, std::string>::iterator;
847 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
848 using td_range = iterator_range<td_iterator>;
849 using td_const_range = iterator_range<td_const_iterator>;
851 td_iterator td_begin() { return TargetDepAttrs.begin(); }
852 td_iterator td_end() { return TargetDepAttrs.end(); }
854 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
855 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
857 td_range td_attrs() { return td_range(td_begin(), td_end()); }
859 td_const_range td_attrs() const {
860 return td_const_range(td_begin(), td_end());
863 bool td_empty() const { return TargetDepAttrs.empty(); }
865 bool operator==(const AttrBuilder &B);
866 bool operator!=(const AttrBuilder &B) {
867 return !(*this == B);
871 namespace AttributeFuncs {
873 /// Which attributes cannot be applied to a type.
874 AttrBuilder typeIncompatible(Type *Ty);
876 /// \returns Return true if the two functions have compatible target-independent
877 /// attributes for inlining purposes.
878 bool areInlineCompatible(const Function &Caller, const Function &Callee);
880 /// Merge caller's and callee's attributes.
881 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
883 } // end namespace AttributeFuncs
885 } // end namespace llvm
887 #endif // LLVM_IR_ATTRIBUTES_H