LLVM  14.0.0git
TrackingMDRef.h
Go to the documentation of this file.
1 //===- llvm/IR/TrackingMDRef.h - Tracking Metadata references ---*- 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 // References to metadata that track RAUW.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_TRACKINGMDREF_H
14 #define LLVM_IR_TRACKINGMDREF_H
15 
16 #include "llvm/IR/Metadata.h"
17 #include <algorithm>
18 #include <cassert>
19 
20 namespace llvm {
21 
22 /// Tracking metadata reference.
23 ///
24 /// This class behaves like \a TrackingVH, but for metadata.
26  Metadata *MD = nullptr;
27 
28 public:
29  TrackingMDRef() = default;
30  explicit TrackingMDRef(Metadata *MD) : MD(MD) { track(); }
31 
32  TrackingMDRef(TrackingMDRef &&X) : MD(X.MD) { retrack(X); }
33  TrackingMDRef(const TrackingMDRef &X) : MD(X.MD) { track(); }
34 
36  if (&X == this)
37  return *this;
38 
39  untrack();
40  MD = X.MD;
41  retrack(X);
42  return *this;
43  }
44 
46  if (&X == this)
47  return *this;
48 
49  untrack();
50  MD = X.MD;
51  track();
52  return *this;
53  }
54 
55  ~TrackingMDRef() { untrack(); }
56 
57  Metadata *get() const { return MD; }
58  operator Metadata *() const { return get(); }
59  Metadata *operator->() const { return get(); }
60  Metadata &operator*() const { return *get(); }
61 
62  void reset() {
63  untrack();
64  MD = nullptr;
65  }
66  void reset(Metadata *MD) {
67  untrack();
68  this->MD = MD;
69  track();
70  }
71 
72  /// Check whether this has a trivial destructor.
73  ///
74  /// If \c MD isn't replaceable, the destructor will be a no-op.
75  bool hasTrivialDestructor() const {
76  return !MD || !MetadataTracking::isReplaceable(*MD);
77  }
78 
79  bool operator==(const TrackingMDRef &X) const { return MD == X.MD; }
80  bool operator!=(const TrackingMDRef &X) const { return MD != X.MD; }
81 
82 private:
83  void track() {
84  if (MD)
86  }
87 
88  void untrack() {
89  if (MD)
91  }
92 
93  void retrack(TrackingMDRef &X) {
94  assert(MD == X.MD && "Expected values to match");
95  if (X.MD) {
97  X.MD = nullptr;
98  }
99  }
100 };
101 
102 /// Typed tracking ref.
103 ///
104 /// Track refererences of a particular type. It's useful to use this for \a
105 /// MDNode and \a ValueAsMetadata.
106 template <class T> class TypedTrackingMDRef {
107  TrackingMDRef Ref;
108 
109 public:
110  TypedTrackingMDRef() = default;
111  explicit TypedTrackingMDRef(T *MD) : Ref(static_cast<Metadata *>(MD)) {}
112 
115 
117  Ref = std::move(X.Ref);
118  return *this;
119  }
120 
122  Ref = X.Ref;
123  return *this;
124  }
125 
126  T *get() const { return (T *)Ref.get(); }
127  operator T *() const { return get(); }
128  T *operator->() const { return get(); }
129  T &operator*() const { return *get(); }
130 
131  bool operator==(const TypedTrackingMDRef &X) const { return Ref == X.Ref; }
132  bool operator!=(const TypedTrackingMDRef &X) const { return Ref != X.Ref; }
133 
134  void reset() { Ref.reset(); }
135  void reset(T *MD) { Ref.reset(static_cast<Metadata *>(MD)); }
136 
137  /// Check whether this has a trivial destructor.
138  bool hasTrivialDestructor() const { return Ref.hasTrivialDestructor(); }
139 };
140 
143 
144 // Expose the underlying metadata to casting.
145 template <> struct simplify_type<TrackingMDRef> {
146  using SimpleType = Metadata *;
147 
148  static SimpleType getSimplifiedValue(TrackingMDRef &MD) { return MD.get(); }
149 };
150 
151 template <> struct simplify_type<const TrackingMDRef> {
152  using SimpleType = Metadata *;
153 
155  return MD.get();
156  }
157 };
158 
159 template <class T> struct simplify_type<TypedTrackingMDRef<T>> {
160  using SimpleType = T *;
161 
163  return MD.get();
164  }
165 };
166 
167 template <class T> struct simplify_type<const TypedTrackingMDRef<T>> {
168  using SimpleType = T *;
169 
171  return MD.get();
172  }
173 };
174 
175 } // end namespace llvm
176 
177 #endif // LLVM_IR_TRACKINGMDREF_H
llvm::TypedTrackingMDRef::TypedTrackingMDRef
TypedTrackingMDRef(TypedTrackingMDRef &&X)
Definition: TrackingMDRef.h:113
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::MetadataTracking::untrack
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Definition: Metadata.h:248
llvm::MetadataTracking::isReplaceable
static bool isReplaceable(const Metadata &MD)
Check whether metadata is replaceable.
Definition: Metadata.cpp:194
Metadata.h
llvm::TypedTrackingMDRef::TypedTrackingMDRef
TypedTrackingMDRef(T *MD)
Definition: TrackingMDRef.h:111
llvm::simplify_type< TypedTrackingMDRef< T > >::SimpleType
T * SimpleType
Definition: TrackingMDRef.h:160
llvm::MetadataTracking::track
static bool track(Metadata *&MD)
Track the reference to metadata.
Definition: Metadata.h:223
llvm::TrackingMDRef::~TrackingMDRef
~TrackingMDRef()
Definition: TrackingMDRef.h:55
llvm::TrackingMDRef::operator=
TrackingMDRef & operator=(const TrackingMDRef &X)
Definition: TrackingMDRef.h:45
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::MetadataTracking::retrack
static bool retrack(Metadata *&MD, Metadata *&New)
Move tracking from one reference to another.
Definition: Metadata.h:259
llvm::TrackingMDRef::operator==
bool operator==(const TrackingMDRef &X) const
Definition: TrackingMDRef.h:79
llvm::TrackingMDRef::operator->
Metadata * operator->() const
Definition: TrackingMDRef.h:59
llvm::TrackingMDRef::TrackingMDRef
TrackingMDRef(const TrackingMDRef &X)
Definition: TrackingMDRef.h:33
llvm::simplify_type< const TypedTrackingMDRef< T > >::SimpleType
T * SimpleType
Definition: TrackingMDRef.h:168
llvm::TrackingMDRef::reset
void reset()
Definition: TrackingMDRef.h:62
llvm::TypedTrackingMDRef::get
T * get() const
Definition: TrackingMDRef.h:126
llvm::TypedTrackingMDRef::operator=
TypedTrackingMDRef & operator=(TypedTrackingMDRef &&X)
Definition: TrackingMDRef.h:116
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::simplify_type< TrackingMDRef >::getSimplifiedValue
static SimpleType getSimplifiedValue(TrackingMDRef &MD)
Definition: TrackingMDRef.h:148
llvm::simplify_type
Definition: ilist_iterator.h:178
llvm::TypedTrackingMDRef::TypedTrackingMDRef
TypedTrackingMDRef(const TypedTrackingMDRef &X)
Definition: TrackingMDRef.h:114
llvm::TypedTrackingMDRef::TypedTrackingMDRef
TypedTrackingMDRef()=default
llvm::TypedTrackingMDRef::operator*
T & operator*() const
Definition: TrackingMDRef.h:129
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::simplify_type< const TypedTrackingMDRef< T > >::getSimplifiedValue
static SimpleType getSimplifiedValue(const TypedTrackingMDRef< T > &MD)
Definition: TrackingMDRef.h:170
llvm::TrackingMDRef::hasTrivialDestructor
bool hasTrivialDestructor() const
Check whether this has a trivial destructor.
Definition: TrackingMDRef.h:75
llvm::TrackingMDRef::operator!=
bool operator!=(const TrackingMDRef &X) const
Definition: TrackingMDRef.h:80
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::TrackingMDRef::TrackingMDRef
TrackingMDRef(Metadata *MD)
Definition: TrackingMDRef.h:30
llvm::TrackingMDRef::get
Metadata * get() const
Definition: TrackingMDRef.h:57
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::simplify_type< TypedTrackingMDRef< T > >::getSimplifiedValue
static SimpleType getSimplifiedValue(TypedTrackingMDRef< T > &MD)
Definition: TrackingMDRef.h:162
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1658
llvm::TypedTrackingMDRef::operator!=
bool operator!=(const TypedTrackingMDRef &X) const
Definition: TrackingMDRef.h:132
llvm::TypedTrackingMDRef::reset
void reset()
Definition: TrackingMDRef.h:134
llvm::TrackingMDRef::reset
void reset(Metadata *MD)
Definition: TrackingMDRef.h:66
llvm::TypedTrackingMDRef::hasTrivialDestructor
bool hasTrivialDestructor() const
Check whether this has a trivial destructor.
Definition: TrackingMDRef.h:138
llvm::TypedTrackingMDRef::operator==
bool operator==(const TypedTrackingMDRef &X) const
Definition: TrackingMDRef.h:131
llvm::simplify_type< const TrackingMDRef >::getSimplifiedValue
static SimpleType getSimplifiedValue(const TrackingMDRef &MD)
Definition: TrackingMDRef.h:154
llvm::TypedTrackingMDRef::operator=
TypedTrackingMDRef & operator=(const TypedTrackingMDRef &X)
Definition: TrackingMDRef.h:121
std
Definition: BitVector.h:838
llvm::TrackingMDRef::operator=
TrackingMDRef & operator=(TrackingMDRef &&X)
Definition: TrackingMDRef.h:35
llvm::TrackingMDRef::operator*
Metadata & operator*() const
Definition: TrackingMDRef.h:60
llvm::TrackingMDRef::TrackingMDRef
TrackingMDRef()=default
llvm::TrackingMDRef
Tracking metadata reference.
Definition: TrackingMDRef.h:25
llvm::TypedTrackingMDRef
Typed tracking ref.
Definition: TrackingMDRef.h:106
llvm::TrackingMDRef::TrackingMDRef
TrackingMDRef(TrackingMDRef &&X)
Definition: TrackingMDRef.h:32
llvm::TypedTrackingMDRef::operator->
T * operator->() const
Definition: TrackingMDRef.h:128
llvm::TypedTrackingMDRef::reset
void reset(T *MD)
Definition: TrackingMDRef.h:135