1 //===- MetadataTracking.cpp - Implement metadata tracking -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Metadata tracking.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/MetadataTracking.h"
15 #include "llvm/IR/Metadata.h"
19 ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) {
20 if (auto *N = dyn_cast<MDNode>(&MD))
21 return N->Context.getReplaceableUses();
22 return dyn_cast<ValueAsMetadata>(&MD);
25 bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
26 assert(Ref && "Expected live reference");
27 assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
28 "Reference without owner must be direct");
29 if (auto *R = ReplaceableMetadataImpl::get(MD)) {
30 R->addRef(Ref, Owner);
36 void MetadataTracking::untrack(void *Ref, Metadata &MD) {
37 assert(Ref && "Expected live reference");
38 if (auto *R = ReplaceableMetadataImpl::get(MD))
42 bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
43 assert(Ref && "Expected live reference");
44 assert(New && "Expected live reference");
45 assert(Ref != New && "Expected change");
46 if (auto *R = ReplaceableMetadataImpl::get(MD)) {
47 R->moveRef(Ref, New, MD);
53 bool MetadataTracking::isReplaceable(const Metadata &MD) {
54 return ReplaceableMetadataImpl::get(const_cast<Metadata &>(MD));