aboutsummaryrefslogtreecommitdiff
path: root/lib/IR/MetadataTracking.cpp
blob: 47f0b9366d7d0db5f3b5a4d23f3826337fad3b1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===- MetadataTracking.cpp - Implement metadata tracking -----------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements Metadata tracking.
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/MetadataTracking.h"
#include "llvm/IR/Metadata.h"

using namespace llvm;

ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) {
  if (auto *N = dyn_cast<MDNode>(&MD))
    return N->Context.getReplaceableUses();
  return dyn_cast<ValueAsMetadata>(&MD);
}

bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
  assert(Ref && "Expected live reference");
  assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
         "Reference without owner must be direct");
  if (auto *R = ReplaceableMetadataImpl::get(MD)) {
    R->addRef(Ref, Owner);
    return true;
  }
  return false;
}

void MetadataTracking::untrack(void *Ref, Metadata &MD) {
  assert(Ref && "Expected live reference");
  if (auto *R = ReplaceableMetadataImpl::get(MD))
    R->dropRef(Ref);
}

bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
  assert(Ref && "Expected live reference");
  assert(New && "Expected live reference");
  assert(Ref != New && "Expected change");
  if (auto *R = ReplaceableMetadataImpl::get(MD)) {
    R->moveRef(Ref, New, MD);
    return true;
  }
  return false;
}

bool MetadataTracking::isReplaceable(const Metadata &MD) {
  return ReplaceableMetadataImpl::get(const_cast<Metadata &>(MD));
}