File indexing completed on 2026-05-10 08:44:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef LLVM_IR_METADATA_H
0016 #define LLVM_IR_METADATA_H
0017
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/DenseMap.h"
0020 #include "llvm/ADT/DenseMapInfo.h"
0021 #include "llvm/ADT/PointerUnion.h"
0022 #include "llvm/ADT/SmallVector.h"
0023 #include "llvm/ADT/StringRef.h"
0024 #include "llvm/ADT/ilist_node.h"
0025 #include "llvm/ADT/iterator_range.h"
0026 #include "llvm/IR/Constant.h"
0027 #include "llvm/IR/LLVMContext.h"
0028 #include "llvm/IR/Value.h"
0029 #include "llvm/Support/CBindingWrapping.h"
0030 #include "llvm/Support/Casting.h"
0031 #include "llvm/Support/ErrorHandling.h"
0032 #include <cassert>
0033 #include <cstddef>
0034 #include <cstdint>
0035 #include <iterator>
0036 #include <memory>
0037 #include <string>
0038 #include <type_traits>
0039 #include <utility>
0040
0041 namespace llvm {
0042
0043 class Module;
0044 class ModuleSlotTracker;
0045 class raw_ostream;
0046 class DbgVariableRecord;
0047 template <typename T> class StringMapEntry;
0048 template <typename ValueTy> class StringMapEntryStorage;
0049 class Type;
0050
0051 enum LLVMConstants : uint32_t {
0052 DEBUG_METADATA_VERSION = 3
0053 };
0054
0055
0056
0057 const uint64_t NOMORE_ICP_MAGICNUM = -1;
0058
0059
0060
0061
0062 class Metadata {
0063 friend class ReplaceableMetadataImpl;
0064
0065
0066 const unsigned char SubclassID;
0067
0068 protected:
0069
0070 enum StorageType { Uniqued, Distinct, Temporary };
0071
0072
0073 unsigned char Storage : 7;
0074
0075 unsigned char SubclassData1 : 1;
0076 unsigned short SubclassData16 = 0;
0077 unsigned SubclassData32 = 0;
0078
0079 public:
0080 enum MetadataKind {
0081 #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
0082 #include "llvm/IR/Metadata.def"
0083 };
0084
0085 protected:
0086 Metadata(unsigned ID, StorageType Storage)
0087 : SubclassID(ID), Storage(Storage), SubclassData1(false) {
0088 static_assert(sizeof(*this) == 8, "Metadata fields poorly packed");
0089 }
0090
0091 ~Metadata() = default;
0092
0093
0094
0095
0096
0097 void handleChangedOperand(void *, Metadata *) {
0098 llvm_unreachable("Unimplemented in Metadata subclass");
0099 }
0100
0101 public:
0102 unsigned getMetadataID() const { return SubclassID; }
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113 void dump() const;
0114 void dump(const Module *M) const;
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 void print(raw_ostream &OS, const Module *M = nullptr,
0125 bool IsForDebug = false) const;
0126 void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr,
0127 bool IsForDebug = false) const;
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const;
0138 void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
0139 const Module *M = nullptr) const;
0140
0141
0142
0143 constexpr static const unsigned PoisonGeneratingIDs[] = {
0144 LLVMContext::MD_range, LLVMContext::MD_nonnull, LLVMContext::MD_align};
0145 };
0146
0147
0148 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
0149
0150
0151 inline Metadata **unwrap(LLVMMetadataRef *MDs) {
0152 return reinterpret_cast<Metadata**>(MDs);
0153 }
0154
0155 #define HANDLE_METADATA(CLASS) class CLASS;
0156 #include "llvm/IR/Metadata.def"
0157
0158
0159
0160 #define HANDLE_METADATA_LEAF(CLASS) \
0161 template <> struct isa_impl<CLASS, Metadata> { \
0162 static inline bool doit(const Metadata &MD) { \
0163 return MD.getMetadataID() == Metadata::CLASS##Kind; \
0164 } \
0165 };
0166 #include "llvm/IR/Metadata.def"
0167
0168 inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
0169 MD.print(OS);
0170 return OS;
0171 }
0172
0173
0174
0175
0176
0177
0178
0179
0180 class MetadataAsValue : public Value {
0181 friend class ReplaceableMetadataImpl;
0182 friend class LLVMContextImpl;
0183
0184 Metadata *MD;
0185
0186 MetadataAsValue(Type *Ty, Metadata *MD);
0187
0188
0189 void dropUse() { MD = nullptr; }
0190
0191 public:
0192 ~MetadataAsValue();
0193
0194 static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
0195 static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
0196
0197 Metadata *getMetadata() const { return MD; }
0198
0199 static bool classof(const Value *V) {
0200 return V->getValueID() == MetadataAsValueVal;
0201 }
0202
0203 private:
0204 void handleChangedMetadata(Metadata *MD);
0205 void track();
0206 void untrack();
0207 };
0208
0209
0210
0211
0212
0213
0214
0215
0216 class DebugValueUser {
0217 protected:
0218
0219
0220
0221
0222 std::array<Metadata *, 3> DebugValues;
0223
0224 ArrayRef<Metadata *> getDebugValues() const { return DebugValues; }
0225
0226 public:
0227 DbgVariableRecord *getUser();
0228 const DbgVariableRecord *getUser() const;
0229
0230
0231
0232
0233
0234
0235 void handleChangedValue(void *Old, Metadata *NewDebugValue);
0236 DebugValueUser() = default;
0237 explicit DebugValueUser(std::array<Metadata *, 3> DebugValues)
0238 : DebugValues(DebugValues) {
0239 trackDebugValues();
0240 }
0241 DebugValueUser(DebugValueUser &&X) {
0242 DebugValues = X.DebugValues;
0243 retrackDebugValues(X);
0244 }
0245 DebugValueUser(const DebugValueUser &X) {
0246 DebugValues = X.DebugValues;
0247 trackDebugValues();
0248 }
0249
0250 DebugValueUser &operator=(DebugValueUser &&X) {
0251 if (&X == this)
0252 return *this;
0253
0254 untrackDebugValues();
0255 DebugValues = X.DebugValues;
0256 retrackDebugValues(X);
0257 return *this;
0258 }
0259
0260 DebugValueUser &operator=(const DebugValueUser &X) {
0261 if (&X == this)
0262 return *this;
0263
0264 untrackDebugValues();
0265 DebugValues = X.DebugValues;
0266 trackDebugValues();
0267 return *this;
0268 }
0269
0270 ~DebugValueUser() { untrackDebugValues(); }
0271
0272 void resetDebugValues() {
0273 untrackDebugValues();
0274 DebugValues.fill(nullptr);
0275 }
0276
0277 void resetDebugValue(size_t Idx, Metadata *DebugValue) {
0278 assert(Idx < 3 && "Invalid debug value index.");
0279 untrackDebugValue(Idx);
0280 DebugValues[Idx] = DebugValue;
0281 trackDebugValue(Idx);
0282 }
0283
0284 bool operator==(const DebugValueUser &X) const {
0285 return DebugValues == X.DebugValues;
0286 }
0287 bool operator!=(const DebugValueUser &X) const {
0288 return DebugValues != X.DebugValues;
0289 }
0290
0291 private:
0292 void trackDebugValue(size_t Idx);
0293 void trackDebugValues();
0294
0295 void untrackDebugValue(size_t Idx);
0296 void untrackDebugValues();
0297
0298 void retrackDebugValues(DebugValueUser &X);
0299 };
0300
0301
0302
0303
0304
0305
0306
0307
0308 class MetadataTracking {
0309 public:
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 static bool track(Metadata *&MD) {
0320 return track(&MD, *MD, static_cast<Metadata *>(nullptr));
0321 }
0322
0323
0324
0325
0326
0327
0328 static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
0329 return track(Ref, MD, &Owner);
0330 }
0331
0332
0333
0334
0335
0336
0337 static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
0338 return track(Ref, MD, &Owner);
0339 }
0340
0341
0342
0343
0344
0345
0346 static bool track(void *Ref, Metadata &MD, DebugValueUser &Owner) {
0347 return track(Ref, MD, &Owner);
0348 }
0349
0350
0351
0352
0353 static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
0354 static void untrack(void *Ref, Metadata &MD);
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 static bool retrack(Metadata *&MD, Metadata *&New) {
0365 return retrack(&MD, *MD, &New);
0366 }
0367 static bool retrack(void *Ref, Metadata &MD, void *New);
0368
0369
0370 static bool isReplaceable(const Metadata &MD);
0371
0372 using OwnerTy = PointerUnion<MetadataAsValue *, Metadata *, DebugValueUser *>;
0373
0374 private:
0375
0376
0377
0378 static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
0379 };
0380
0381
0382
0383
0384
0385
0386 class ReplaceableMetadataImpl {
0387 friend class MetadataTracking;
0388
0389 public:
0390 using OwnerTy = MetadataTracking::OwnerTy;
0391
0392 private:
0393 LLVMContext &Context;
0394 uint64_t NextIndex = 0;
0395 SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
0396
0397 public:
0398 ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {}
0399
0400 ~ReplaceableMetadataImpl() {
0401 assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
0402 }
0403
0404 LLVMContext &getContext() const { return Context; }
0405
0406
0407
0408
0409 void replaceAllUsesWith(Metadata *MD);
0410
0411 static void SalvageDebugInfo(const Constant &C);
0412
0413 SmallVector<Metadata *> getAllArgListUsers();
0414
0415 SmallVector<DbgVariableRecord *> getAllDbgVariableRecordUsers();
0416
0417
0418
0419
0420
0421
0422 void resolveAllUses(bool ResolveUsers = true);
0423
0424 unsigned getNumUses() const { return UseMap.size(); }
0425
0426 private:
0427 void addRef(void *Ref, OwnerTy Owner);
0428 void dropRef(void *Ref);
0429 void moveRef(void *Ref, void *New, const Metadata &MD);
0430
0431
0432
0433
0434
0435 static ReplaceableMetadataImpl *getOrCreate(Metadata &MD);
0436
0437
0438 static ReplaceableMetadataImpl *getIfExists(Metadata &MD);
0439
0440
0441
0442
0443 static bool isReplaceable(const Metadata &MD);
0444 };
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454 class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
0455 friend class ReplaceableMetadataImpl;
0456 friend class LLVMContextImpl;
0457
0458 Value *V;
0459
0460
0461 void dropUsers() {
0462 ReplaceableMetadataImpl::resolveAllUses( false);
0463 }
0464
0465 protected:
0466 ValueAsMetadata(unsigned ID, Value *V)
0467 : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
0468 assert(V && "Expected valid value");
0469 }
0470
0471 ~ValueAsMetadata() = default;
0472
0473 public:
0474 static ValueAsMetadata *get(Value *V);
0475
0476 static ConstantAsMetadata *getConstant(Value *C) {
0477 return cast<ConstantAsMetadata>(get(C));
0478 }
0479
0480 static LocalAsMetadata *getLocal(Value *Local) {
0481 return cast<LocalAsMetadata>(get(Local));
0482 }
0483
0484 static ValueAsMetadata *getIfExists(Value *V);
0485
0486 static ConstantAsMetadata *getConstantIfExists(Value *C) {
0487 return cast_or_null<ConstantAsMetadata>(getIfExists(C));
0488 }
0489
0490 static LocalAsMetadata *getLocalIfExists(Value *Local) {
0491 return cast_or_null<LocalAsMetadata>(getIfExists(Local));
0492 }
0493
0494 Value *getValue() const { return V; }
0495 Type *getType() const { return V->getType(); }
0496 LLVMContext &getContext() const { return V->getContext(); }
0497
0498 SmallVector<Metadata *> getAllArgListUsers() {
0499 return ReplaceableMetadataImpl::getAllArgListUsers();
0500 }
0501 SmallVector<DbgVariableRecord *> getAllDbgVariableRecordUsers() {
0502 return ReplaceableMetadataImpl::getAllDbgVariableRecordUsers();
0503 }
0504
0505 static void handleDeletion(Value *V);
0506 static void handleRAUW(Value *From, Value *To);
0507
0508 protected:
0509
0510
0511
0512
0513
0514 void replaceAllUsesWith(Metadata *MD) {
0515 ReplaceableMetadataImpl::replaceAllUsesWith(MD);
0516 }
0517
0518 public:
0519 static bool classof(const Metadata *MD) {
0520 return MD->getMetadataID() == LocalAsMetadataKind ||
0521 MD->getMetadataID() == ConstantAsMetadataKind;
0522 }
0523 };
0524
0525 class ConstantAsMetadata : public ValueAsMetadata {
0526 friend class ValueAsMetadata;
0527
0528 ConstantAsMetadata(Constant *C)
0529 : ValueAsMetadata(ConstantAsMetadataKind, C) {}
0530
0531 public:
0532 static ConstantAsMetadata *get(Constant *C) {
0533 return ValueAsMetadata::getConstant(C);
0534 }
0535
0536 static ConstantAsMetadata *getIfExists(Constant *C) {
0537 return ValueAsMetadata::getConstantIfExists(C);
0538 }
0539
0540 Constant *getValue() const {
0541 return cast<Constant>(ValueAsMetadata::getValue());
0542 }
0543
0544 static bool classof(const Metadata *MD) {
0545 return MD->getMetadataID() == ConstantAsMetadataKind;
0546 }
0547 };
0548
0549 class LocalAsMetadata : public ValueAsMetadata {
0550 friend class ValueAsMetadata;
0551
0552 LocalAsMetadata(Value *Local)
0553 : ValueAsMetadata(LocalAsMetadataKind, Local) {
0554 assert(!isa<Constant>(Local) && "Expected local value");
0555 }
0556
0557 public:
0558 static LocalAsMetadata *get(Value *Local) {
0559 return ValueAsMetadata::getLocal(Local);
0560 }
0561
0562 static LocalAsMetadata *getIfExists(Value *Local) {
0563 return ValueAsMetadata::getLocalIfExists(Local);
0564 }
0565
0566 static bool classof(const Metadata *MD) {
0567 return MD->getMetadataID() == LocalAsMetadataKind;
0568 }
0569 };
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619 namespace mdconst {
0620
0621 namespace detail {
0622
0623 template <class T> T &make();
0624 template <class T, class Result> struct HasDereference {
0625 using Yes = char[1];
0626 using No = char[2];
0627 template <size_t N> struct SFINAE {};
0628
0629 template <class U, class V>
0630 static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
0631 template <class U, class V> static No &hasDereference(...);
0632
0633 static const bool value =
0634 sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
0635 };
0636 template <class V, class M> struct IsValidPointer {
0637 static const bool value = std::is_base_of<Constant, V>::value &&
0638 HasDereference<M, const Metadata &>::value;
0639 };
0640 template <class V, class M> struct IsValidReference {
0641 static const bool value = std::is_base_of<Constant, V>::value &&
0642 std::is_convertible<M, const Metadata &>::value;
0643 };
0644
0645 }
0646
0647
0648
0649
0650
0651 template <class X, class Y>
0652 inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, bool>
0653 hasa(Y &&MD) {
0654 assert(MD && "Null pointer sent into hasa");
0655 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
0656 return isa<X>(V->getValue());
0657 return false;
0658 }
0659 template <class X, class Y>
0660 inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, bool>
0661 hasa(Y &MD) {
0662 return hasa(&MD);
0663 }
0664
0665
0666
0667
0668 template <class X, class Y>
0669 inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
0670 extract(Y &&MD) {
0671 return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
0672 }
0673 template <class X, class Y>
0674 inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, X *>
0675 extract(Y &MD) {
0676 return extract(&MD);
0677 }
0678
0679
0680
0681
0682
0683 template <class X, class Y>
0684 inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
0685 extract_or_null(Y &&MD) {
0686 if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
0687 return cast<X>(V->getValue());
0688 return nullptr;
0689 }
0690
0691
0692
0693
0694
0695
0696 template <class X, class Y>
0697 inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
0698 dyn_extract(Y &&MD) {
0699 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
0700 return dyn_cast<X>(V->getValue());
0701 return nullptr;
0702 }
0703
0704
0705
0706
0707
0708
0709 template <class X, class Y>
0710 inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
0711 dyn_extract_or_null(Y &&MD) {
0712 if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
0713 return dyn_cast<X>(V->getValue());
0714 return nullptr;
0715 }
0716
0717 }
0718
0719
0720
0721
0722
0723
0724 class MDString : public Metadata {
0725 friend class StringMapEntryStorage<MDString>;
0726
0727 StringMapEntry<MDString> *Entry = nullptr;
0728
0729 MDString() : Metadata(MDStringKind, Uniqued) {}
0730
0731 public:
0732 MDString(const MDString &) = delete;
0733 MDString &operator=(MDString &&) = delete;
0734 MDString &operator=(const MDString &) = delete;
0735
0736 static MDString *get(LLVMContext &Context, StringRef Str);
0737 static MDString *get(LLVMContext &Context, const char *Str) {
0738 return get(Context, Str ? StringRef(Str) : StringRef());
0739 }
0740
0741 StringRef getString() const;
0742
0743 unsigned getLength() const { return (unsigned)getString().size(); }
0744
0745 using iterator = StringRef::iterator;
0746
0747
0748 iterator begin() const { return getString().begin(); }
0749
0750
0751 iterator end() const { return getString().end(); }
0752
0753 const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
0754 const unsigned char *bytes_end() const { return getString().bytes_end(); }
0755
0756
0757 static bool classof(const Metadata *MD) {
0758 return MD->getMetadataID() == MDStringKind;
0759 }
0760 };
0761
0762
0763
0764 struct AAMDNodes {
0765 explicit AAMDNodes() = default;
0766 explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N)
0767 : TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N) {}
0768
0769 bool operator==(const AAMDNodes &A) const {
0770 return TBAA == A.TBAA && TBAAStruct == A.TBAAStruct && Scope == A.Scope &&
0771 NoAlias == A.NoAlias;
0772 }
0773
0774 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
0775
0776 explicit operator bool() const {
0777 return TBAA || TBAAStruct || Scope || NoAlias;
0778 }
0779
0780
0781 MDNode *TBAA = nullptr;
0782
0783
0784 MDNode *TBAAStruct = nullptr;
0785
0786
0787 MDNode *Scope = nullptr;
0788
0789
0790 MDNode *NoAlias = nullptr;
0791
0792
0793 static MDNode *shiftTBAA(MDNode *M, size_t off);
0794
0795
0796 static MDNode *shiftTBAAStruct(MDNode *M, size_t off);
0797
0798
0799
0800 static MDNode *extendToTBAA(MDNode *TBAA, ssize_t len);
0801
0802
0803
0804
0805
0806
0807 AAMDNodes intersect(const AAMDNodes &Other) const {
0808 AAMDNodes Result;
0809 Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
0810 Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr;
0811 Result.Scope = Other.Scope == Scope ? Scope : nullptr;
0812 Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
0813 return Result;
0814 }
0815
0816
0817
0818 AAMDNodes shift(size_t Offset) const {
0819 AAMDNodes Result;
0820 Result.TBAA = TBAA ? shiftTBAA(TBAA, Offset) : nullptr;
0821 Result.TBAAStruct =
0822 TBAAStruct ? shiftTBAAStruct(TBAAStruct, Offset) : nullptr;
0823 Result.Scope = Scope;
0824 Result.NoAlias = NoAlias;
0825 return Result;
0826 }
0827
0828
0829
0830
0831 AAMDNodes extendTo(ssize_t Len) const {
0832 AAMDNodes Result;
0833 Result.TBAA = TBAA ? extendToTBAA(TBAA, Len) : nullptr;
0834
0835
0836
0837 Result.TBAAStruct = TBAAStruct;
0838 Result.Scope = Scope;
0839 Result.NoAlias = NoAlias;
0840 return Result;
0841 }
0842
0843
0844
0845 AAMDNodes merge(const AAMDNodes &Other) const;
0846
0847
0848
0849
0850 AAMDNodes concat(const AAMDNodes &Other) const;
0851
0852
0853
0854
0855
0856
0857 AAMDNodes adjustForAccess(unsigned AccessSize);
0858 AAMDNodes adjustForAccess(size_t Offset, Type *AccessTy,
0859 const DataLayout &DL);
0860 AAMDNodes adjustForAccess(size_t Offset, unsigned AccessSize);
0861 };
0862
0863
0864 template<>
0865 struct DenseMapInfo<AAMDNodes> {
0866 static inline AAMDNodes getEmptyKey() {
0867 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(),
0868 nullptr, nullptr, nullptr);
0869 }
0870
0871 static inline AAMDNodes getTombstoneKey() {
0872 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(),
0873 nullptr, nullptr, nullptr);
0874 }
0875
0876 static unsigned getHashValue(const AAMDNodes &Val) {
0877 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
0878 DenseMapInfo<MDNode *>::getHashValue(Val.TBAAStruct) ^
0879 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
0880 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
0881 }
0882
0883 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
0884 return LHS == RHS;
0885 }
0886 };
0887
0888
0889
0890
0891
0892
0893
0894
0895 class MDOperand {
0896 Metadata *MD = nullptr;
0897
0898 public:
0899 MDOperand() = default;
0900 MDOperand(const MDOperand &) = delete;
0901 MDOperand(MDOperand &&Op) {
0902 MD = Op.MD;
0903 if (MD)
0904 (void)MetadataTracking::retrack(Op.MD, MD);
0905 Op.MD = nullptr;
0906 }
0907 MDOperand &operator=(const MDOperand &) = delete;
0908 MDOperand &operator=(MDOperand &&Op) {
0909 MD = Op.MD;
0910 if (MD)
0911 (void)MetadataTracking::retrack(Op.MD, MD);
0912 Op.MD = nullptr;
0913 return *this;
0914 }
0915
0916
0917 bool equalsStr(StringRef Str) const {
0918 return isa<MDString>(this->get()) &&
0919 cast<MDString>(this->get())->getString() == Str;
0920 }
0921
0922 ~MDOperand() { untrack(); }
0923
0924 Metadata *get() const { return MD; }
0925 operator Metadata *() const { return get(); }
0926 Metadata *operator->() const { return get(); }
0927 Metadata &operator*() const { return *get(); }
0928
0929 void reset() {
0930 untrack();
0931 MD = nullptr;
0932 }
0933 void reset(Metadata *MD, Metadata *Owner) {
0934 untrack();
0935 this->MD = MD;
0936 track(Owner);
0937 }
0938
0939 private:
0940 void track(Metadata *Owner) {
0941 if (MD) {
0942 if (Owner)
0943 MetadataTracking::track(this, *MD, *Owner);
0944 else
0945 MetadataTracking::track(MD);
0946 }
0947 }
0948
0949 void untrack() {
0950 assert(static_cast<void *>(this) == &MD && "Expected same address");
0951 if (MD)
0952 MetadataTracking::untrack(MD);
0953 }
0954 };
0955
0956 template <> struct simplify_type<MDOperand> {
0957 using SimpleType = Metadata *;
0958
0959 static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
0960 };
0961
0962 template <> struct simplify_type<const MDOperand> {
0963 using SimpleType = Metadata *;
0964
0965 static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
0966 };
0967
0968
0969
0970
0971
0972 class ContextAndReplaceableUses {
0973 PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr;
0974
0975 public:
0976 ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
0977 ContextAndReplaceableUses(
0978 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
0979 : Ptr(ReplaceableUses.release()) {
0980 assert(getReplaceableUses() && "Expected non-null replaceable uses");
0981 }
0982 ContextAndReplaceableUses() = delete;
0983 ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete;
0984 ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete;
0985 ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete;
0986 ContextAndReplaceableUses &
0987 operator=(const ContextAndReplaceableUses &) = delete;
0988 ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
0989
0990 operator LLVMContext &() { return getContext(); }
0991
0992
0993 bool hasReplaceableUses() const {
0994 return isa<ReplaceableMetadataImpl *>(Ptr);
0995 }
0996
0997 LLVMContext &getContext() const {
0998 if (hasReplaceableUses())
0999 return getReplaceableUses()->getContext();
1000 return *cast<LLVMContext *>(Ptr);
1001 }
1002
1003 ReplaceableMetadataImpl *getReplaceableUses() const {
1004 if (hasReplaceableUses())
1005 return cast<ReplaceableMetadataImpl *>(Ptr);
1006 return nullptr;
1007 }
1008
1009
1010 ReplaceableMetadataImpl *getOrCreateReplaceableUses() {
1011 if (!hasReplaceableUses())
1012 makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(getContext()));
1013 return getReplaceableUses();
1014 }
1015
1016
1017
1018
1019
1020 void
1021 makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
1022 assert(ReplaceableUses && "Expected non-null replaceable uses");
1023 assert(&ReplaceableUses->getContext() == &getContext() &&
1024 "Expected same context");
1025 delete getReplaceableUses();
1026 Ptr = ReplaceableUses.release();
1027 }
1028
1029
1030
1031
1032 std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
1033 assert(hasReplaceableUses() && "Expected to own replaceable uses");
1034 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
1035 getReplaceableUses());
1036 Ptr = &ReplaceableUses->getContext();
1037 return ReplaceableUses;
1038 }
1039 };
1040
1041 struct TempMDNodeDeleter {
1042 inline void operator()(MDNode *Node) const;
1043 };
1044
1045 #define HANDLE_MDNODE_LEAF(CLASS) \
1046 using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>;
1047 #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
1048 #include "llvm/IR/Metadata.def"
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 class MDNode : public Metadata {
1074 friend class ReplaceableMetadataImpl;
1075 friend class LLVMContextImpl;
1076 friend class DIAssignID;
1077
1078
1079
1080
1081
1082
1083
1084
1085 struct alignas(alignof(size_t)) Header {
1086 bool IsResizable : 1;
1087 bool IsLarge : 1;
1088 size_t SmallSize : 4;
1089 size_t SmallNumOps : 4;
1090 size_t : sizeof(size_t) * CHAR_BIT - 10;
1091
1092 unsigned NumUnresolved = 0;
1093 using LargeStorageVector = SmallVector<MDOperand, 0>;
1094
1095 static constexpr size_t NumOpsFitInVector =
1096 sizeof(LargeStorageVector) / sizeof(MDOperand);
1097 static_assert(
1098 NumOpsFitInVector * sizeof(MDOperand) == sizeof(LargeStorageVector),
1099 "sizeof(LargeStorageVector) must be a multiple of sizeof(MDOperand)");
1100
1101 static constexpr size_t MaxSmallSize = 15;
1102
1103 static constexpr size_t getOpSize(unsigned NumOps) {
1104 return sizeof(MDOperand) * NumOps;
1105 }
1106
1107
1108 static size_t getSmallSize(size_t NumOps, bool IsResizable, bool IsLarge) {
1109 return IsLarge ? NumOpsFitInVector
1110 : std::max(NumOps, NumOpsFitInVector * IsResizable);
1111 }
1112
1113 static size_t getAllocSize(StorageType Storage, size_t NumOps) {
1114 return getOpSize(
1115 getSmallSize(NumOps, isResizable(Storage), isLarge(NumOps))) +
1116 sizeof(Header);
1117 }
1118
1119
1120 static bool isResizable(StorageType Storage) { return Storage != Uniqued; }
1121 static bool isLarge(size_t NumOps) { return NumOps > MaxSmallSize; }
1122
1123 size_t getAllocSize() const {
1124 return getOpSize(SmallSize) + sizeof(Header);
1125 }
1126 void *getAllocation() {
1127 return reinterpret_cast<char *>(this + 1) -
1128 alignTo(getAllocSize(), alignof(uint64_t));
1129 }
1130
1131 void *getLargePtr() const {
1132 static_assert(alignof(LargeStorageVector) <= alignof(Header),
1133 "LargeStorageVector too strongly aligned");
1134 return reinterpret_cast<char *>(const_cast<Header *>(this)) -
1135 sizeof(LargeStorageVector);
1136 }
1137
1138 void *getSmallPtr();
1139
1140 LargeStorageVector &getLarge() {
1141 assert(IsLarge);
1142 return *reinterpret_cast<LargeStorageVector *>(getLargePtr());
1143 }
1144
1145 const LargeStorageVector &getLarge() const {
1146 assert(IsLarge);
1147 return *reinterpret_cast<const LargeStorageVector *>(getLargePtr());
1148 }
1149
1150 void resizeSmall(size_t NumOps);
1151 void resizeSmallToLarge(size_t NumOps);
1152 void resize(size_t NumOps);
1153
1154 explicit Header(size_t NumOps, StorageType Storage);
1155 ~Header();
1156
1157 MutableArrayRef<MDOperand> operands() {
1158 if (IsLarge)
1159 return getLarge();
1160 return MutableArrayRef(
1161 reinterpret_cast<MDOperand *>(this) - SmallSize, SmallNumOps);
1162 }
1163
1164 ArrayRef<MDOperand> operands() const {
1165 if (IsLarge)
1166 return getLarge();
1167 return ArrayRef(reinterpret_cast<const MDOperand *>(this) - SmallSize,
1168 SmallNumOps);
1169 }
1170
1171 unsigned getNumOperands() const {
1172 if (!IsLarge)
1173 return SmallNumOps;
1174 return getLarge().size();
1175 }
1176 };
1177
1178 Header &getHeader() { return *(reinterpret_cast<Header *>(this) - 1); }
1179
1180 const Header &getHeader() const {
1181 return *(reinterpret_cast<const Header *>(this) - 1);
1182 }
1183
1184 ContextAndReplaceableUses Context;
1185
1186 protected:
1187 MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
1188 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = {});
1189 ~MDNode() = default;
1190
1191 void *operator new(size_t Size, size_t NumOps, StorageType Storage);
1192 void operator delete(void *Mem);
1193
1194
1195 void operator delete(void *, unsigned) {
1196 llvm_unreachable("Constructor throws?");
1197 }
1198
1199
1200 void operator delete(void *, unsigned, bool) {
1201 llvm_unreachable("Constructor throws?");
1202 }
1203
1204 void dropAllReferences();
1205
1206 MDOperand *mutable_begin() { return getHeader().operands().begin(); }
1207 MDOperand *mutable_end() { return getHeader().operands().end(); }
1208
1209 using mutable_op_range = iterator_range<MDOperand *>;
1210
1211 mutable_op_range mutable_operands() {
1212 return mutable_op_range(mutable_begin(), mutable_end());
1213 }
1214
1215 public:
1216 MDNode(const MDNode &) = delete;
1217 void operator=(const MDNode &) = delete;
1218 void *operator new(size_t) = delete;
1219
1220 static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
1221 static inline MDTuple *getIfExists(LLVMContext &Context,
1222 ArrayRef<Metadata *> MDs);
1223 static inline MDTuple *getDistinct(LLVMContext &Context,
1224 ArrayRef<Metadata *> MDs);
1225 static inline TempMDTuple getTemporary(LLVMContext &Context,
1226 ArrayRef<Metadata *> MDs);
1227
1228
1229 TempMDNode clone() const;
1230
1231
1232
1233
1234
1235 static void deleteTemporary(MDNode *N);
1236
1237 LLVMContext &getContext() const { return Context.getContext(); }
1238
1239
1240 void replaceOperandWith(unsigned I, Metadata *New);
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253 bool isResolved() const { return !isTemporary() && !getNumUnresolved(); }
1254
1255 bool isUniqued() const { return Storage == Uniqued; }
1256 bool isDistinct() const { return Storage == Distinct; }
1257 bool isTemporary() const { return Storage == Temporary; }
1258
1259 bool isReplaceable() const { return isTemporary() || isAlwaysReplaceable(); }
1260 bool isAlwaysReplaceable() const { return getMetadataID() == DIAssignIDKind; }
1261
1262 unsigned getNumTemporaryUses() const {
1263 assert(isTemporary() && "Only for temporaries");
1264 return Context.getReplaceableUses()->getNumUses();
1265 }
1266
1267
1268
1269
1270 void replaceAllUsesWith(Metadata *MD) {
1271 assert(isReplaceable() && "Expected temporary/replaceable node");
1272 if (Context.hasReplaceableUses())
1273 Context.getReplaceableUses()->replaceAllUsesWith(MD);
1274 }
1275
1276
1277
1278
1279
1280
1281
1282 void resolveCycles();
1283
1284
1285 void resolve();
1286
1287
1288
1289
1290
1291 template <class T>
1292 static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
1293 replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
1294 return cast<T>(N.release()->replaceWithPermanentImpl());
1295 }
1296
1297
1298
1299
1300
1301
1302
1303 template <class T>
1304 static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
1305 replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
1306 return cast<T>(N.release()->replaceWithUniquedImpl());
1307 }
1308
1309
1310
1311
1312
1313 template <class T>
1314 static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
1315 replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
1316 return cast<T>(N.release()->replaceWithDistinctImpl());
1317 }
1318
1319
1320
1321
1322
1323
1324
1325
1326 void printTree(raw_ostream &OS, const Module *M = nullptr) const;
1327 void printTree(raw_ostream &OS, ModuleSlotTracker &MST,
1328 const Module *M = nullptr) const;
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 void dumpTree() const;
1341 void dumpTree(const Module *M) const;
1342
1343
1344 private:
1345 MDNode *replaceWithPermanentImpl();
1346 MDNode *replaceWithUniquedImpl();
1347 MDNode *replaceWithDistinctImpl();
1348
1349 protected:
1350
1351
1352
1353 void setOperand(unsigned I, Metadata *New);
1354
1355 unsigned getNumUnresolved() const { return getHeader().NumUnresolved; }
1356
1357 void setNumUnresolved(unsigned N) { getHeader().NumUnresolved = N; }
1358 void storeDistinctInContext();
1359 template <class T, class StoreT>
1360 static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
1361 template <class T> static T *storeImpl(T *N, StorageType Storage);
1362
1363
1364
1365
1366
1367 void resize(size_t NumOps) {
1368 assert(!isUniqued() && "Resizing is not supported for uniqued nodes");
1369 assert(getMetadataID() == MDTupleKind &&
1370 "Resizing is not supported for this node kind");
1371 getHeader().resize(NumOps);
1372 }
1373
1374 private:
1375 void handleChangedOperand(void *Ref, Metadata *New);
1376
1377
1378 void dropReplaceableUses();
1379
1380 void resolveAfterOperandChange(Metadata *Old, Metadata *New);
1381 void decrementUnresolvedOperandCount();
1382 void countUnresolvedOperands();
1383
1384
1385
1386
1387
1388
1389 void makeUniqued();
1390
1391
1392
1393
1394
1395 void makeDistinct();
1396
1397 void deleteAsSubclass();
1398 MDNode *uniquify();
1399 void eraseFromStore();
1400
1401 template <class NodeTy> struct HasCachedHash;
1402 template <class NodeTy>
1403 static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
1404 N->recalculateHash();
1405 }
1406 template <class NodeTy>
1407 static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
1408 template <class NodeTy>
1409 static void dispatchResetHash(NodeTy *N, std::true_type) {
1410 N->setHash(0);
1411 }
1412 template <class NodeTy>
1413 static void dispatchResetHash(NodeTy *, std::false_type) {}
1414
1415
1416 static MDNode *mergeDirectCallProfMetadata(MDNode *A, MDNode *B,
1417 const Instruction *AInstr,
1418 const Instruction *BInstr);
1419
1420 public:
1421 using op_iterator = const MDOperand *;
1422 using op_range = iterator_range<op_iterator>;
1423
1424 op_iterator op_begin() const {
1425 return const_cast<MDNode *>(this)->mutable_begin();
1426 }
1427
1428 op_iterator op_end() const {
1429 return const_cast<MDNode *>(this)->mutable_end();
1430 }
1431
1432 ArrayRef<MDOperand> operands() const { return getHeader().operands(); }
1433
1434 const MDOperand &getOperand(unsigned I) const {
1435 assert(I < getNumOperands() && "Out of range");
1436 return getHeader().operands()[I];
1437 }
1438
1439
1440 unsigned getNumOperands() const { return getHeader().getNumOperands(); }
1441
1442
1443 static bool classof(const Metadata *MD) {
1444 switch (MD->getMetadataID()) {
1445 default:
1446 return false;
1447 #define HANDLE_MDNODE_LEAF(CLASS) \
1448 case CLASS##Kind: \
1449 return true;
1450 #include "llvm/IR/Metadata.def"
1451 }
1452 }
1453
1454
1455 bool isTBAAVtableAccess() const;
1456
1457
1458 static MDNode *concatenate(MDNode *A, MDNode *B);
1459 static MDNode *intersect(MDNode *A, MDNode *B);
1460 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
1461 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
1462 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
1463 static MDNode *getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B);
1464 static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
1465 static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B);
1466
1467
1468 static MDNode *getMergedProfMetadata(MDNode *A, MDNode *B,
1469 const Instruction *AInstr,
1470 const Instruction *BInstr);
1471 static MDNode *getMergedMemProfMetadata(MDNode *A, MDNode *B);
1472 static MDNode *getMergedCallsiteMetadata(MDNode *A, MDNode *B);
1473 };
1474
1475
1476
1477
1478
1479 class MDTuple : public MDNode {
1480 friend class LLVMContextImpl;
1481 friend class MDNode;
1482
1483 MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
1484 ArrayRef<Metadata *> Vals)
1485 : MDNode(C, MDTupleKind, Storage, Vals) {
1486 setHash(Hash);
1487 }
1488
1489 ~MDTuple() { dropAllReferences(); }
1490
1491 void setHash(unsigned Hash) { SubclassData32 = Hash; }
1492 void recalculateHash();
1493
1494 static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
1495 StorageType Storage, bool ShouldCreate = true);
1496
1497 TempMDTuple cloneImpl() const {
1498 ArrayRef<MDOperand> Operands = operands();
1499 return getTemporary(getContext(), SmallVector<Metadata *, 4>(Operands));
1500 }
1501
1502 public:
1503
1504 unsigned getHash() const { return SubclassData32; }
1505
1506 static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1507 return getImpl(Context, MDs, Uniqued);
1508 }
1509
1510 static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1511 return getImpl(Context, MDs, Uniqued, false);
1512 }
1513
1514
1515
1516
1517 static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1518 return getImpl(Context, MDs, Distinct);
1519 }
1520
1521
1522
1523
1524
1525
1526 static TempMDTuple getTemporary(LLVMContext &Context,
1527 ArrayRef<Metadata *> MDs) {
1528 return TempMDTuple(getImpl(Context, MDs, Temporary));
1529 }
1530
1531
1532 TempMDTuple clone() const { return cloneImpl(); }
1533
1534
1535 void push_back(Metadata *MD) {
1536 size_t NumOps = getNumOperands();
1537 resize(NumOps + 1);
1538 setOperand(NumOps, MD);
1539 }
1540
1541
1542 void pop_back() { resize(getNumOperands() - 1); }
1543
1544 static bool classof(const Metadata *MD) {
1545 return MD->getMetadataID() == MDTupleKind;
1546 }
1547 };
1548
1549 MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1550 return MDTuple::get(Context, MDs);
1551 }
1552
1553 MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1554 return MDTuple::getIfExists(Context, MDs);
1555 }
1556
1557 MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1558 return MDTuple::getDistinct(Context, MDs);
1559 }
1560
1561 TempMDTuple MDNode::getTemporary(LLVMContext &Context,
1562 ArrayRef<Metadata *> MDs) {
1563 return MDTuple::getTemporary(Context, MDs);
1564 }
1565
1566 void TempMDNodeDeleter::operator()(MDNode *Node) const {
1567 MDNode::deleteTemporary(Node);
1568 }
1569
1570
1571
1572
1573 class AliasScopeNode {
1574 const MDNode *Node = nullptr;
1575
1576 public:
1577 AliasScopeNode() = default;
1578 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
1579
1580
1581 const MDNode *getNode() const { return Node; }
1582
1583
1584 const MDNode *getDomain() const {
1585 if (Node->getNumOperands() < 2)
1586 return nullptr;
1587 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
1588 }
1589 StringRef getName() const {
1590 if (Node->getNumOperands() > 2)
1591 if (MDString *N = dyn_cast_or_null<MDString>(Node->getOperand(2)))
1592 return N->getString();
1593 return StringRef();
1594 }
1595 };
1596
1597
1598
1599
1600
1601 template <class T> class TypedMDOperandIterator {
1602 MDNode::op_iterator I = nullptr;
1603
1604 public:
1605 using iterator_category = std::input_iterator_tag;
1606 using value_type = T *;
1607 using difference_type = std::ptrdiff_t;
1608 using pointer = void;
1609 using reference = T *;
1610
1611 TypedMDOperandIterator() = default;
1612 explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {}
1613
1614 T *operator*() const { return cast_or_null<T>(*I); }
1615
1616 TypedMDOperandIterator &operator++() {
1617 ++I;
1618 return *this;
1619 }
1620
1621 TypedMDOperandIterator operator++(int) {
1622 TypedMDOperandIterator Temp(*this);
1623 ++I;
1624 return Temp;
1625 }
1626
1627 bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
1628 bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
1629 };
1630
1631
1632
1633
1634
1635 template <class T> class MDTupleTypedArrayWrapper {
1636 const MDTuple *N = nullptr;
1637
1638 public:
1639 MDTupleTypedArrayWrapper() = default;
1640 MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
1641
1642 template <class U>
1643 MDTupleTypedArrayWrapper(
1644 const MDTupleTypedArrayWrapper<U> &Other,
1645 std::enable_if_t<std::is_convertible<U *, T *>::value> * = nullptr)
1646 : N(Other.get()) {}
1647
1648 template <class U>
1649 explicit MDTupleTypedArrayWrapper(
1650 const MDTupleTypedArrayWrapper<U> &Other,
1651 std::enable_if_t<!std::is_convertible<U *, T *>::value> * = nullptr)
1652 : N(Other.get()) {}
1653
1654 explicit operator bool() const { return get(); }
1655 explicit operator MDTuple *() const { return get(); }
1656
1657 MDTuple *get() const { return const_cast<MDTuple *>(N); }
1658 MDTuple *operator->() const { return get(); }
1659 MDTuple &operator*() const { return *get(); }
1660
1661
1662 unsigned size() const { return N ? N->getNumOperands() : 0u; }
1663 bool empty() const { return N ? N->getNumOperands() == 0 : true; }
1664 T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
1665
1666
1667 using iterator = TypedMDOperandIterator<T>;
1668
1669 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
1670 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
1671 };
1672
1673 #define HANDLE_METADATA(CLASS) \
1674 using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
1675 #include "llvm/IR/Metadata.def"
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692 class DistinctMDOperandPlaceholder : public Metadata {
1693 friend class MetadataTracking;
1694
1695 Metadata **Use = nullptr;
1696
1697 public:
1698 explicit DistinctMDOperandPlaceholder(unsigned ID)
1699 : Metadata(DistinctMDOperandPlaceholderKind, Distinct) {
1700 SubclassData32 = ID;
1701 }
1702
1703 DistinctMDOperandPlaceholder() = delete;
1704 DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&) = delete;
1705 DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &) = delete;
1706
1707 ~DistinctMDOperandPlaceholder() {
1708 if (Use)
1709 *Use = nullptr;
1710 }
1711
1712 unsigned getID() const { return SubclassData32; }
1713
1714
1715 void replaceUseWith(Metadata *MD) {
1716 if (!Use)
1717 return;
1718 *Use = MD;
1719
1720 if (*Use)
1721 MetadataTracking::track(*Use);
1722
1723 Metadata *T = cast<Metadata>(this);
1724 MetadataTracking::untrack(T);
1725 assert(!Use && "Use is still being tracked despite being untracked!");
1726 }
1727 };
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737 class NamedMDNode : public ilist_node<NamedMDNode> {
1738 friend class LLVMContextImpl;
1739 friend class Module;
1740
1741 std::string Name;
1742 Module *Parent = nullptr;
1743 void *Operands;
1744
1745 void setParent(Module *M) { Parent = M; }
1746
1747 explicit NamedMDNode(const Twine &N);
1748
1749 template <class T1> class op_iterator_impl {
1750 friend class NamedMDNode;
1751
1752 const NamedMDNode *Node = nullptr;
1753 unsigned Idx = 0;
1754
1755 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {}
1756
1757 public:
1758 using iterator_category = std::bidirectional_iterator_tag;
1759 using value_type = T1;
1760 using difference_type = std::ptrdiff_t;
1761 using pointer = value_type *;
1762 using reference = value_type;
1763
1764 op_iterator_impl() = default;
1765
1766 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
1767 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
1768
1769 op_iterator_impl &operator++() {
1770 ++Idx;
1771 return *this;
1772 }
1773
1774 op_iterator_impl operator++(int) {
1775 op_iterator_impl tmp(*this);
1776 operator++();
1777 return tmp;
1778 }
1779
1780 op_iterator_impl &operator--() {
1781 --Idx;
1782 return *this;
1783 }
1784
1785 op_iterator_impl operator--(int) {
1786 op_iterator_impl tmp(*this);
1787 operator--();
1788 return tmp;
1789 }
1790
1791 T1 operator*() const { return Node->getOperand(Idx); }
1792 };
1793
1794 public:
1795 NamedMDNode(const NamedMDNode &) = delete;
1796 ~NamedMDNode();
1797
1798
1799 void eraseFromParent();
1800
1801
1802 void dropAllReferences() { clearOperands(); }
1803
1804 void clearOperands();
1805
1806
1807 inline Module *getParent() { return Parent; }
1808 inline const Module *getParent() const { return Parent; }
1809
1810 MDNode *getOperand(unsigned i) const;
1811 unsigned getNumOperands() const;
1812 void addOperand(MDNode *M);
1813 void setOperand(unsigned I, MDNode *New);
1814 StringRef getName() const;
1815 void print(raw_ostream &ROS, bool IsForDebug = false) const;
1816 void print(raw_ostream &ROS, ModuleSlotTracker &MST,
1817 bool IsForDebug = false) const;
1818 void dump() const;
1819
1820
1821
1822
1823 using op_iterator = op_iterator_impl<MDNode *>;
1824
1825 op_iterator op_begin() { return op_iterator(this, 0); }
1826 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
1827
1828 using const_op_iterator = op_iterator_impl<const MDNode *>;
1829
1830 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
1831 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
1832
1833 inline iterator_range<op_iterator> operands() {
1834 return make_range(op_begin(), op_end());
1835 }
1836 inline iterator_range<const_op_iterator> operands() const {
1837 return make_range(op_begin(), op_end());
1838 }
1839 };
1840
1841
1842 DEFINE_ISA_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDNodeRef)
1843
1844 }
1845
1846 #endif