Bug Summary

File:lib/IR/Metadata.cpp
Warning:line 827, column 69
Potential memory leak

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Metadata.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/lib/IR -I /build/llvm-toolchain-snapshot-7~svn325118/lib/IR -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn325118/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/lib/IR -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-02-14-150435-17243-1 -x c++ /build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp

/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp

1//===- Metadata.cpp - Implement Metadata classes --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLVMContextImpl.h"
15#include "MetadataImpl.h"
16#include "SymbolTableListTraitsImpl.h"
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APInt.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/None.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/Twine.h"
30#include "llvm/IR/Argument.h"
31#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/Constant.h"
33#include "llvm/IR/ConstantRange.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DebugInfoMetadata.h"
36#include "llvm/IR/DebugLoc.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalObject.h"
39#include "llvm/IR/GlobalVariable.h"
40#include "llvm/IR/Instruction.h"
41#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/Metadata.h"
43#include "llvm/IR/Module.h"
44#include "llvm/IR/TrackingMDRef.h"
45#include "llvm/IR/Type.h"
46#include "llvm/IR/Value.h"
47#include "llvm/IR/ValueHandle.h"
48#include "llvm/Support/Casting.h"
49#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
51#include <algorithm>
52#include <cassert>
53#include <cstddef>
54#include <cstdint>
55#include <iterator>
56#include <tuple>
57#include <type_traits>
58#include <utility>
59#include <vector>
60
61using namespace llvm;
62
63MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
64 : Value(Ty, MetadataAsValueVal), MD(MD) {
65 track();
66}
67
68MetadataAsValue::~MetadataAsValue() {
69 getType()->getContext().pImpl->MetadataAsValues.erase(MD);
70 untrack();
71}
72
73/// Canonicalize metadata arguments to intrinsics.
74///
75/// To support bitcode upgrades (and assembly semantic sugar) for \a
76/// MetadataAsValue, we need to canonicalize certain metadata.
77///
78/// - nullptr is replaced by an empty MDNode.
79/// - An MDNode with a single null operand is replaced by an empty MDNode.
80/// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped.
81///
82/// This maintains readability of bitcode from when metadata was a type of
83/// value, and these bridges were unnecessary.
84static Metadata *canonicalizeMetadataForValue(LLVMContext &Context,
85 Metadata *MD) {
86 if (!MD)
87 // !{}
88 return MDNode::get(Context, None);
89
90 // Return early if this isn't a single-operand MDNode.
91 auto *N = dyn_cast<MDNode>(MD);
92 if (!N || N->getNumOperands() != 1)
93 return MD;
94
95 if (!N->getOperand(0))
96 // !{}
97 return MDNode::get(Context, None);
98
99 if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
100 // Look through the MDNode.
101 return C;
102
103 return MD;
104}
105
106MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) {
107 MD = canonicalizeMetadataForValue(Context, MD);
108 auto *&Entry = Context.pImpl->MetadataAsValues[MD];
109 if (!Entry)
110 Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
111 return Entry;
112}
113
114MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context,
115 Metadata *MD) {
116 MD = canonicalizeMetadataForValue(Context, MD);
117 auto &Store = Context.pImpl->MetadataAsValues;
118 return Store.lookup(MD);
119}
120
121void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
122 LLVMContext &Context = getContext();
123 MD = canonicalizeMetadataForValue(Context, MD);
124 auto &Store = Context.pImpl->MetadataAsValues;
125
126 // Stop tracking the old metadata.
127 Store.erase(this->MD);
128 untrack();
129 this->MD = nullptr;
130
131 // Start tracking MD, or RAUW if necessary.
132 auto *&Entry = Store[MD];
133 if (Entry) {
134 replaceAllUsesWith(Entry);
135 delete this;
136 return;
137 }
138
139 this->MD = MD;
140 track();
141 Entry = this;
142}
143
144void MetadataAsValue::track() {
145 if (MD)
146 MetadataTracking::track(&MD, *MD, *this);
147}
148
149void MetadataAsValue::untrack() {
150 if (MD)
151 MetadataTracking::untrack(MD);
152}
153
154bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
155 assert(Ref && "Expected live reference")(static_cast <bool> (Ref && "Expected live reference"
) ? void (0) : __assert_fail ("Ref && \"Expected live reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 155, __extension__ __PRETTY_FUNCTION__))
;
156 assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&(static_cast <bool> ((Owner || *static_cast<Metadata
**>(Ref) == &MD) && "Reference without owner must be direct"
) ? void (0) : __assert_fail ("(Owner || *static_cast<Metadata **>(Ref) == &MD) && \"Reference without owner must be direct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 157, __extension__ __PRETTY_FUNCTION__))
157 "Reference without owner must be direct")(static_cast <bool> ((Owner || *static_cast<Metadata
**>(Ref) == &MD) && "Reference without owner must be direct"
) ? void (0) : __assert_fail ("(Owner || *static_cast<Metadata **>(Ref) == &MD) && \"Reference without owner must be direct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 157, __extension__ __PRETTY_FUNCTION__))
;
158 if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) {
159 R->addRef(Ref, Owner);
160 return true;
161 }
162 if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) {
163 assert(!PH->Use && "Placeholders can only be used once")(static_cast <bool> (!PH->Use && "Placeholders can only be used once"
) ? void (0) : __assert_fail ("!PH->Use && \"Placeholders can only be used once\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 163, __extension__ __PRETTY_FUNCTION__))
;
164 assert(!Owner && "Unexpected callback to owner")(static_cast <bool> (!Owner && "Unexpected callback to owner"
) ? void (0) : __assert_fail ("!Owner && \"Unexpected callback to owner\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 164, __extension__ __PRETTY_FUNCTION__))
;
165 PH->Use = static_cast<Metadata **>(Ref);
166 return true;
167 }
168 return false;
169}
170
171void MetadataTracking::untrack(void *Ref, Metadata &MD) {
172 assert(Ref && "Expected live reference")(static_cast <bool> (Ref && "Expected live reference"
) ? void (0) : __assert_fail ("Ref && \"Expected live reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 172, __extension__ __PRETTY_FUNCTION__))
;
173 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD))
174 R->dropRef(Ref);
175 else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD))
176 PH->Use = nullptr;
177}
178
179bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
180 assert(Ref && "Expected live reference")(static_cast <bool> (Ref && "Expected live reference"
) ? void (0) : __assert_fail ("Ref && \"Expected live reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 180, __extension__ __PRETTY_FUNCTION__))
;
181 assert(New && "Expected live reference")(static_cast <bool> (New && "Expected live reference"
) ? void (0) : __assert_fail ("New && \"Expected live reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 181, __extension__ __PRETTY_FUNCTION__))
;
182 assert(Ref != New && "Expected change")(static_cast <bool> (Ref != New && "Expected change"
) ? void (0) : __assert_fail ("Ref != New && \"Expected change\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 182, __extension__ __PRETTY_FUNCTION__))
;
183 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) {
184 R->moveRef(Ref, New, MD);
185 return true;
186 }
187 assert(!isa<DistinctMDOperandPlaceholder>(MD) &&(static_cast <bool> (!isa<DistinctMDOperandPlaceholder
>(MD) && "Unexpected move of an MDOperand") ? void
(0) : __assert_fail ("!isa<DistinctMDOperandPlaceholder>(MD) && \"Unexpected move of an MDOperand\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 188, __extension__ __PRETTY_FUNCTION__))
188 "Unexpected move of an MDOperand")(static_cast <bool> (!isa<DistinctMDOperandPlaceholder
>(MD) && "Unexpected move of an MDOperand") ? void
(0) : __assert_fail ("!isa<DistinctMDOperandPlaceholder>(MD) && \"Unexpected move of an MDOperand\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 188, __extension__ __PRETTY_FUNCTION__))
;
189 assert(!isReplaceable(MD) &&(static_cast <bool> (!isReplaceable(MD) && "Expected un-replaceable metadata, since we didn't move a reference"
) ? void (0) : __assert_fail ("!isReplaceable(MD) && \"Expected un-replaceable metadata, since we didn't move a reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 190, __extension__ __PRETTY_FUNCTION__))
190 "Expected un-replaceable metadata, since we didn't move a reference")(static_cast <bool> (!isReplaceable(MD) && "Expected un-replaceable metadata, since we didn't move a reference"
) ? void (0) : __assert_fail ("!isReplaceable(MD) && \"Expected un-replaceable metadata, since we didn't move a reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 190, __extension__ __PRETTY_FUNCTION__))
;
191 return false;
192}
193
194bool MetadataTracking::isReplaceable(const Metadata &MD) {
195 return ReplaceableMetadataImpl::isReplaceable(MD);
196}
197
198void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
199 bool WasInserted =
200 UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
201 .second;
202 (void)WasInserted;
203 assert(WasInserted && "Expected to add a reference")(static_cast <bool> (WasInserted && "Expected to add a reference"
) ? void (0) : __assert_fail ("WasInserted && \"Expected to add a reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 203, __extension__ __PRETTY_FUNCTION__))
;
204
205 ++NextIndex;
206 assert(NextIndex != 0 && "Unexpected overflow")(static_cast <bool> (NextIndex != 0 && "Unexpected overflow"
) ? void (0) : __assert_fail ("NextIndex != 0 && \"Unexpected overflow\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 206, __extension__ __PRETTY_FUNCTION__))
;
207}
208
209void ReplaceableMetadataImpl::dropRef(void *Ref) {
210 bool WasErased = UseMap.erase(Ref);
211 (void)WasErased;
212 assert(WasErased && "Expected to drop a reference")(static_cast <bool> (WasErased && "Expected to drop a reference"
) ? void (0) : __assert_fail ("WasErased && \"Expected to drop a reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 212, __extension__ __PRETTY_FUNCTION__))
;
213}
214
215void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
216 const Metadata &MD) {
217 auto I = UseMap.find(Ref);
218 assert(I != UseMap.end() && "Expected to move a reference")(static_cast <bool> (I != UseMap.end() && "Expected to move a reference"
) ? void (0) : __assert_fail ("I != UseMap.end() && \"Expected to move a reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 218, __extension__ __PRETTY_FUNCTION__))
;
219 auto OwnerAndIndex = I->second;
220 UseMap.erase(I);
221 bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
222 (void)WasInserted;
223 assert(WasInserted && "Expected to add a reference")(static_cast <bool> (WasInserted && "Expected to add a reference"
) ? void (0) : __assert_fail ("WasInserted && \"Expected to add a reference\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 223, __extension__ __PRETTY_FUNCTION__))
;
224
225 // Check that the references are direct if there's no owner.
226 (void)MD;
227 assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&(static_cast <bool> ((OwnerAndIndex.first || *static_cast
<Metadata **>(Ref) == &MD) && "Reference without owner must be direct"
) ? void (0) : __assert_fail ("(OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) && \"Reference without owner must be direct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 228, __extension__ __PRETTY_FUNCTION__))
228 "Reference without owner must be direct")(static_cast <bool> ((OwnerAndIndex.first || *static_cast
<Metadata **>(Ref) == &MD) && "Reference without owner must be direct"
) ? void (0) : __assert_fail ("(OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) && \"Reference without owner must be direct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 228, __extension__ __PRETTY_FUNCTION__))
;
229 assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&(static_cast <bool> ((OwnerAndIndex.first || *static_cast
<Metadata **>(New) == &MD) && "Reference without owner must be direct"
) ? void (0) : __assert_fail ("(OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) && \"Reference without owner must be direct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 230, __extension__ __PRETTY_FUNCTION__))
230 "Reference without owner must be direct")(static_cast <bool> ((OwnerAndIndex.first || *static_cast
<Metadata **>(New) == &MD) && "Reference without owner must be direct"
) ? void (0) : __assert_fail ("(OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) && \"Reference without owner must be direct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 230, __extension__ __PRETTY_FUNCTION__))
;
231}
232
233void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) {
234 if (UseMap.empty())
235 return;
236
237 // Copy out uses since UseMap will get touched below.
238 using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
239 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
240 std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
241 return L.second.second < R.second.second;
242 });
243 for (const auto &Pair : Uses) {
244 // Check that this Ref hasn't disappeared after RAUW (when updating a
245 // previous Ref).
246 if (!UseMap.count(Pair.first))
247 continue;
248
249 OwnerTy Owner = Pair.second.first;
250 if (!Owner) {
251 // Update unowned tracking references directly.
252 Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
253 Ref = MD;
254 if (MD)
255 MetadataTracking::track(Ref);
256 UseMap.erase(Pair.first);
257 continue;
258 }
259
260 // Check for MetadataAsValue.
261 if (Owner.is<MetadataAsValue *>()) {
262 Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
263 continue;
264 }
265
266 // There's a Metadata owner -- dispatch.
267 Metadata *OwnerMD = Owner.get<Metadata *>();
268 switch (OwnerMD->getMetadataID()) {
269#define HANDLE_METADATA_LEAF(CLASS) \
270 case Metadata::CLASS##Kind: \
271 cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
272 continue;
273#include "llvm/IR/Metadata.def"
274 default:
275 llvm_unreachable("Invalid metadata subclass")::llvm::llvm_unreachable_internal("Invalid metadata subclass"
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 275)
;
276 }
277 }
278 assert(UseMap.empty() && "Expected all uses to be replaced")(static_cast <bool> (UseMap.empty() && "Expected all uses to be replaced"
) ? void (0) : __assert_fail ("UseMap.empty() && \"Expected all uses to be replaced\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 278, __extension__ __PRETTY_FUNCTION__))
;
279}
280
281void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) {
282 if (UseMap.empty())
283 return;
284
285 if (!ResolveUsers) {
286 UseMap.clear();
287 return;
288 }
289
290 // Copy out uses since UseMap could get touched below.
291 using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
292 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
293 std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
294 return L.second.second < R.second.second;
295 });
296 UseMap.clear();
297 for (const auto &Pair : Uses) {
298 auto Owner = Pair.second.first;
299 if (!Owner)
300 continue;
301 if (Owner.is<MetadataAsValue *>())
302 continue;
303
304 // Resolve MDNodes that point at this.
305 auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
306 if (!OwnerMD)
307 continue;
308 if (OwnerMD->isResolved())
309 continue;
310 OwnerMD->decrementUnresolvedOperandCount();
311 }
312}
313
314ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
315 if (auto *N = dyn_cast<MDNode>(&MD))
316 return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses();
317 return dyn_cast<ValueAsMetadata>(&MD);
318}
319
320ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
321 if (auto *N = dyn_cast<MDNode>(&MD))
322 return N->isResolved() ? nullptr : N->Context.getReplaceableUses();
323 return dyn_cast<ValueAsMetadata>(&MD);
324}
325
326bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
327 if (auto *N = dyn_cast<MDNode>(&MD))
328 return !N->isResolved();
329 return dyn_cast<ValueAsMetadata>(&MD);
330}
331
332static DISubprogram *getLocalFunctionMetadata(Value *V) {
333 assert(V && "Expected value")(static_cast <bool> (V && "Expected value") ? void
(0) : __assert_fail ("V && \"Expected value\"", "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 333, __extension__ __PRETTY_FUNCTION__))
;
334 if (auto *A = dyn_cast<Argument>(V)) {
335 if (auto *Fn = A->getParent())
336 return Fn->getSubprogram();
337 return nullptr;
338 }
339
340 if (BasicBlock *BB = cast<Instruction>(V)->getParent()) {
341 if (auto *Fn = BB->getParent())
342 return Fn->getSubprogram();
343 return nullptr;
344 }
345
346 return nullptr;
347}
348
349ValueAsMetadata *ValueAsMetadata::get(Value *V) {
350 assert(V && "Unexpected null Value")(static_cast <bool> (V && "Unexpected null Value"
) ? void (0) : __assert_fail ("V && \"Unexpected null Value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 350, __extension__ __PRETTY_FUNCTION__))
;
351
352 auto &Context = V->getContext();
353 auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
354 if (!Entry) {
355 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&(static_cast <bool> ((isa<Constant>(V) || isa<
Argument>(V) || isa<Instruction>(V)) && "Expected constant or function-local value"
) ? void (0) : __assert_fail ("(isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && \"Expected constant or function-local value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 356, __extension__ __PRETTY_FUNCTION__))
356 "Expected constant or function-local value")(static_cast <bool> ((isa<Constant>(V) || isa<
Argument>(V) || isa<Instruction>(V)) && "Expected constant or function-local value"
) ? void (0) : __assert_fail ("(isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && \"Expected constant or function-local value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 356, __extension__ __PRETTY_FUNCTION__))
;
357 assert(!V->IsUsedByMD && "Expected this to be the only metadata use")(static_cast <bool> (!V->IsUsedByMD && "Expected this to be the only metadata use"
) ? void (0) : __assert_fail ("!V->IsUsedByMD && \"Expected this to be the only metadata use\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 357, __extension__ __PRETTY_FUNCTION__))
;
358 V->IsUsedByMD = true;
359 if (auto *C = dyn_cast<Constant>(V))
360 Entry = new ConstantAsMetadata(C);
361 else
362 Entry = new LocalAsMetadata(V);
363 }
364
365 return Entry;
366}
367
368ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) {
369 assert(V && "Unexpected null Value")(static_cast <bool> (V && "Unexpected null Value"
) ? void (0) : __assert_fail ("V && \"Unexpected null Value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 369, __extension__ __PRETTY_FUNCTION__))
;
370 return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
371}
372
373void ValueAsMetadata::handleDeletion(Value *V) {
374 assert(V && "Expected valid value")(static_cast <bool> (V && "Expected valid value"
) ? void (0) : __assert_fail ("V && \"Expected valid value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 374, __extension__ __PRETTY_FUNCTION__))
;
375
376 auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
377 auto I = Store.find(V);
378 if (I == Store.end())
379 return;
380
381 // Remove old entry from the map.
382 ValueAsMetadata *MD = I->second;
383 assert(MD && "Expected valid metadata")(static_cast <bool> (MD && "Expected valid metadata"
) ? void (0) : __assert_fail ("MD && \"Expected valid metadata\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 383, __extension__ __PRETTY_FUNCTION__))
;
384 assert(MD->getValue() == V && "Expected valid mapping")(static_cast <bool> (MD->getValue() == V && "Expected valid mapping"
) ? void (0) : __assert_fail ("MD->getValue() == V && \"Expected valid mapping\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 384, __extension__ __PRETTY_FUNCTION__))
;
385 Store.erase(I);
386
387 // Delete the metadata.
388 MD->replaceAllUsesWith(nullptr);
389 delete MD;
390}
391
392void ValueAsMetadata::handleRAUW(Value *From, Value *To) {
393 assert(From && "Expected valid value")(static_cast <bool> (From && "Expected valid value"
) ? void (0) : __assert_fail ("From && \"Expected valid value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 393, __extension__ __PRETTY_FUNCTION__))
;
394 assert(To && "Expected valid value")(static_cast <bool> (To && "Expected valid value"
) ? void (0) : __assert_fail ("To && \"Expected valid value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 394, __extension__ __PRETTY_FUNCTION__))
;
395 assert(From != To && "Expected changed value")(static_cast <bool> (From != To && "Expected changed value"
) ? void (0) : __assert_fail ("From != To && \"Expected changed value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 395, __extension__ __PRETTY_FUNCTION__))
;
396 assert(From->getType() == To->getType() && "Unexpected type change")(static_cast <bool> (From->getType() == To->getType
() && "Unexpected type change") ? void (0) : __assert_fail
("From->getType() == To->getType() && \"Unexpected type change\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 396, __extension__ __PRETTY_FUNCTION__))
;
397
398 LLVMContext &Context = From->getType()->getContext();
399 auto &Store = Context.pImpl->ValuesAsMetadata;
400 auto I = Store.find(From);
401 if (I == Store.end()) {
402 assert(!From->IsUsedByMD && "Expected From not to be used by metadata")(static_cast <bool> (!From->IsUsedByMD && "Expected From not to be used by metadata"
) ? void (0) : __assert_fail ("!From->IsUsedByMD && \"Expected From not to be used by metadata\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 402, __extension__ __PRETTY_FUNCTION__))
;
403 return;
404 }
405
406 // Remove old entry from the map.
407 assert(From->IsUsedByMD && "Expected From to be used by metadata")(static_cast <bool> (From->IsUsedByMD && "Expected From to be used by metadata"
) ? void (0) : __assert_fail ("From->IsUsedByMD && \"Expected From to be used by metadata\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 407, __extension__ __PRETTY_FUNCTION__))
;
408 From->IsUsedByMD = false;
409 ValueAsMetadata *MD = I->second;
410 assert(MD && "Expected valid metadata")(static_cast <bool> (MD && "Expected valid metadata"
) ? void (0) : __assert_fail ("MD && \"Expected valid metadata\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 410, __extension__ __PRETTY_FUNCTION__))
;
411 assert(MD->getValue() == From && "Expected valid mapping")(static_cast <bool> (MD->getValue() == From &&
"Expected valid mapping") ? void (0) : __assert_fail ("MD->getValue() == From && \"Expected valid mapping\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 411, __extension__ __PRETTY_FUNCTION__))
;
412 Store.erase(I);
413
414 if (isa<LocalAsMetadata>(MD)) {
415 if (auto *C = dyn_cast<Constant>(To)) {
416 // Local became a constant.
417 MD->replaceAllUsesWith(ConstantAsMetadata::get(C));
418 delete MD;
419 return;
420 }
421 if (getLocalFunctionMetadata(From) && getLocalFunctionMetadata(To) &&
422 getLocalFunctionMetadata(From) != getLocalFunctionMetadata(To)) {
423 // DISubprogram changed.
424 MD->replaceAllUsesWith(nullptr);
425 delete MD;
426 return;
427 }
428 } else if (!isa<Constant>(To)) {
429 // Changed to function-local value.
430 MD->replaceAllUsesWith(nullptr);
431 delete MD;
432 return;
433 }
434
435 auto *&Entry = Store[To];
436 if (Entry) {
437 // The target already exists.
438 MD->replaceAllUsesWith(Entry);
439 delete MD;
440 return;
441 }
442
443 // Update MD in place (and update the map entry).
444 assert(!To->IsUsedByMD && "Expected this to be the only metadata use")(static_cast <bool> (!To->IsUsedByMD && "Expected this to be the only metadata use"
) ? void (0) : __assert_fail ("!To->IsUsedByMD && \"Expected this to be the only metadata use\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 444, __extension__ __PRETTY_FUNCTION__))
;
445 To->IsUsedByMD = true;
446 MD->V = To;
447 Entry = MD;
448}
449
450//===----------------------------------------------------------------------===//
451// MDString implementation.
452//
453
454MDString *MDString::get(LLVMContext &Context, StringRef Str) {
455 auto &Store = Context.pImpl->MDStringCache;
456 auto I = Store.try_emplace(Str);
457 auto &MapEntry = I.first->getValue();
458 if (!I.second)
459 return &MapEntry;
460 MapEntry.Entry = &*I.first;
461 return &MapEntry;
462}
463
464StringRef MDString::getString() const {
465 assert(Entry && "Expected to find string map entry")(static_cast <bool> (Entry && "Expected to find string map entry"
) ? void (0) : __assert_fail ("Entry && \"Expected to find string map entry\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 465, __extension__ __PRETTY_FUNCTION__))
;
466 return Entry->first();
467}
468
469//===----------------------------------------------------------------------===//
470// MDNode implementation.
471//
472
473// Assert that the MDNode types will not be unaligned by the objects
474// prepended to them.
475#define HANDLE_MDNODE_LEAF(CLASS) \
476 static_assert( \
477 alignof(uint64_t) >= alignof(CLASS), \
478 "Alignment is insufficient after objects prepended to " #CLASS);
479#include "llvm/IR/Metadata.def"
480
481void *MDNode::operator new(size_t Size, unsigned NumOps) {
482 size_t OpSize = NumOps * sizeof(MDOperand);
483 // uint64_t is the most aligned type we need support (ensured by static_assert
484 // above)
485 OpSize = alignTo(OpSize, alignof(uint64_t));
486 void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize;
8
Memory is allocated
487 MDOperand *O = static_cast<MDOperand *>(Ptr);
488 for (MDOperand *E = O - NumOps; O != E; --O)
9
Assuming 'O' is equal to 'E'
10
Loop condition is false. Execution continues on line 490
489 (void)new (O - 1) MDOperand;
490 return Ptr;
491}
492
493void MDNode::operator delete(void *Mem) {
494 MDNode *N = static_cast<MDNode *>(Mem);
495 size_t OpSize = N->NumOperands * sizeof(MDOperand);
496 OpSize = alignTo(OpSize, alignof(uint64_t));
497
498 MDOperand *O = static_cast<MDOperand *>(Mem);
499 for (MDOperand *E = O - N->NumOperands; O != E; --O)
500 (O - 1)->~MDOperand();
501 ::operator delete(reinterpret_cast<char *>(Mem) - OpSize);
502}
503
504MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
505 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2)
506 : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()),
507 NumUnresolved(0), Context(Context) {
508 unsigned Op = 0;
509 for (Metadata *MD : Ops1)
510 setOperand(Op++, MD);
511 for (Metadata *MD : Ops2)
512 setOperand(Op++, MD);
513
514 if (!isUniqued())
515 return;
516
517 // Count the unresolved operands. If there are any, RAUW support will be
518 // added lazily on first reference.
519 countUnresolvedOperands();
520}
521
522TempMDNode MDNode::clone() const {
523 switch (getMetadataID()) {
524 default:
525 llvm_unreachable("Invalid MDNode subclass")::llvm::llvm_unreachable_internal("Invalid MDNode subclass", "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 525)
;
526#define HANDLE_MDNODE_LEAF(CLASS) \
527 case CLASS##Kind: \
528 return cast<CLASS>(this)->cloneImpl();
529#include "llvm/IR/Metadata.def"
530 }
531}
532
533static bool isOperandUnresolved(Metadata *Op) {
534 if (auto *N = dyn_cast_or_null<MDNode>(Op))
535 return !N->isResolved();
536 return false;
537}
538
539void MDNode::countUnresolvedOperands() {
540 assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted")(static_cast <bool> (NumUnresolved == 0 && "Expected unresolved ops to be uncounted"
) ? void (0) : __assert_fail ("NumUnresolved == 0 && \"Expected unresolved ops to be uncounted\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 540, __extension__ __PRETTY_FUNCTION__))
;
541 assert(isUniqued() && "Expected this to be uniqued")(static_cast <bool> (isUniqued() && "Expected this to be uniqued"
) ? void (0) : __assert_fail ("isUniqued() && \"Expected this to be uniqued\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 541, __extension__ __PRETTY_FUNCTION__))
;
542 NumUnresolved = count_if(operands(), isOperandUnresolved);
543}
544
545void MDNode::makeUniqued() {
546 assert(isTemporary() && "Expected this to be temporary")(static_cast <bool> (isTemporary() && "Expected this to be temporary"
) ? void (0) : __assert_fail ("isTemporary() && \"Expected this to be temporary\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 546, __extension__ __PRETTY_FUNCTION__))
;
547 assert(!isResolved() && "Expected this to be unresolved")(static_cast <bool> (!isResolved() && "Expected this to be unresolved"
) ? void (0) : __assert_fail ("!isResolved() && \"Expected this to be unresolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 547, __extension__ __PRETTY_FUNCTION__))
;
548
549 // Enable uniquing callbacks.
550 for (auto &Op : mutable_operands())
551 Op.reset(Op.get(), this);
552
553 // Make this 'uniqued'.
554 Storage = Uniqued;
555 countUnresolvedOperands();
556 if (!NumUnresolved) {
557 dropReplaceableUses();
558 assert(isResolved() && "Expected this to be resolved")(static_cast <bool> (isResolved() && "Expected this to be resolved"
) ? void (0) : __assert_fail ("isResolved() && \"Expected this to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 558, __extension__ __PRETTY_FUNCTION__))
;
559 }
560
561 assert(isUniqued() && "Expected this to be uniqued")(static_cast <bool> (isUniqued() && "Expected this to be uniqued"
) ? void (0) : __assert_fail ("isUniqued() && \"Expected this to be uniqued\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 561, __extension__ __PRETTY_FUNCTION__))
;
562}
563
564void MDNode::makeDistinct() {
565 assert(isTemporary() && "Expected this to be temporary")(static_cast <bool> (isTemporary() && "Expected this to be temporary"
) ? void (0) : __assert_fail ("isTemporary() && \"Expected this to be temporary\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 565, __extension__ __PRETTY_FUNCTION__))
;
566 assert(!isResolved() && "Expected this to be unresolved")(static_cast <bool> (!isResolved() && "Expected this to be unresolved"
) ? void (0) : __assert_fail ("!isResolved() && \"Expected this to be unresolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 566, __extension__ __PRETTY_FUNCTION__))
;
567
568 // Drop RAUW support and store as a distinct node.
569 dropReplaceableUses();
570 storeDistinctInContext();
571
572 assert(isDistinct() && "Expected this to be distinct")(static_cast <bool> (isDistinct() && "Expected this to be distinct"
) ? void (0) : __assert_fail ("isDistinct() && \"Expected this to be distinct\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 572, __extension__ __PRETTY_FUNCTION__))
;
573 assert(isResolved() && "Expected this to be resolved")(static_cast <bool> (isResolved() && "Expected this to be resolved"
) ? void (0) : __assert_fail ("isResolved() && \"Expected this to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 573, __extension__ __PRETTY_FUNCTION__))
;
574}
575
576void MDNode::resolve() {
577 assert(isUniqued() && "Expected this to be uniqued")(static_cast <bool> (isUniqued() && "Expected this to be uniqued"
) ? void (0) : __assert_fail ("isUniqued() && \"Expected this to be uniqued\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 577, __extension__ __PRETTY_FUNCTION__))
;
578 assert(!isResolved() && "Expected this to be unresolved")(static_cast <bool> (!isResolved() && "Expected this to be unresolved"
) ? void (0) : __assert_fail ("!isResolved() && \"Expected this to be unresolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 578, __extension__ __PRETTY_FUNCTION__))
;
579
580 NumUnresolved = 0;
581 dropReplaceableUses();
582
583 assert(isResolved() && "Expected this to be resolved")(static_cast <bool> (isResolved() && "Expected this to be resolved"
) ? void (0) : __assert_fail ("isResolved() && \"Expected this to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 583, __extension__ __PRETTY_FUNCTION__))
;
584}
585
586void MDNode::dropReplaceableUses() {
587 assert(!NumUnresolved && "Unexpected unresolved operand")(static_cast <bool> (!NumUnresolved && "Unexpected unresolved operand"
) ? void (0) : __assert_fail ("!NumUnresolved && \"Unexpected unresolved operand\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 587, __extension__ __PRETTY_FUNCTION__))
;
588
589 // Drop any RAUW support.
590 if (Context.hasReplaceableUses())
591 Context.takeReplaceableUses()->resolveAllUses();
592}
593
594void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
595 assert(isUniqued() && "Expected this to be uniqued")(static_cast <bool> (isUniqued() && "Expected this to be uniqued"
) ? void (0) : __assert_fail ("isUniqued() && \"Expected this to be uniqued\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 595, __extension__ __PRETTY_FUNCTION__))
;
596 assert(NumUnresolved != 0 && "Expected unresolved operands")(static_cast <bool> (NumUnresolved != 0 && "Expected unresolved operands"
) ? void (0) : __assert_fail ("NumUnresolved != 0 && \"Expected unresolved operands\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 596, __extension__ __PRETTY_FUNCTION__))
;
597
598 // Check if an operand was resolved.
599 if (!isOperandUnresolved(Old)) {
600 if (isOperandUnresolved(New))
601 // An operand was un-resolved!
602 ++NumUnresolved;
603 } else if (!isOperandUnresolved(New))
604 decrementUnresolvedOperandCount();
605}
606
607void MDNode::decrementUnresolvedOperandCount() {
608 assert(!isResolved() && "Expected this to be unresolved")(static_cast <bool> (!isResolved() && "Expected this to be unresolved"
) ? void (0) : __assert_fail ("!isResolved() && \"Expected this to be unresolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 608, __extension__ __PRETTY_FUNCTION__))
;
609 if (isTemporary())
610 return;
611
612 assert(isUniqued() && "Expected this to be uniqued")(static_cast <bool> (isUniqued() && "Expected this to be uniqued"
) ? void (0) : __assert_fail ("isUniqued() && \"Expected this to be uniqued\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 612, __extension__ __PRETTY_FUNCTION__))
;
613 if (--NumUnresolved)
614 return;
615
616 // Last unresolved operand has just been resolved.
617 dropReplaceableUses();
618 assert(isResolved() && "Expected this to become resolved")(static_cast <bool> (isResolved() && "Expected this to become resolved"
) ? void (0) : __assert_fail ("isResolved() && \"Expected this to become resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 618, __extension__ __PRETTY_FUNCTION__))
;
619}
620
621void MDNode::resolveCycles() {
622 if (isResolved())
623 return;
624
625 // Resolve this node immediately.
626 resolve();
627
628 // Resolve all operands.
629 for (const auto &Op : operands()) {
630 auto *N = dyn_cast_or_null<MDNode>(Op);
631 if (!N)
632 continue;
633
634 assert(!N->isTemporary() &&(static_cast <bool> (!N->isTemporary() && "Expected all forward declarations to be resolved"
) ? void (0) : __assert_fail ("!N->isTemporary() && \"Expected all forward declarations to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 635, __extension__ __PRETTY_FUNCTION__))
635 "Expected all forward declarations to be resolved")(static_cast <bool> (!N->isTemporary() && "Expected all forward declarations to be resolved"
) ? void (0) : __assert_fail ("!N->isTemporary() && \"Expected all forward declarations to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 635, __extension__ __PRETTY_FUNCTION__))
;
636 if (!N->isResolved())
637 N->resolveCycles();
638 }
639}
640
641static bool hasSelfReference(MDNode *N) {
642 for (Metadata *MD : N->operands())
643 if (MD == N)
644 return true;
645 return false;
646}
647
648MDNode *MDNode::replaceWithPermanentImpl() {
649 switch (getMetadataID()) {
650 default:
651 // If this type isn't uniquable, replace with a distinct node.
652 return replaceWithDistinctImpl();
653
654#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
655 case CLASS##Kind: \
656 break;
657#include "llvm/IR/Metadata.def"
658 }
659
660 // Even if this type is uniquable, self-references have to be distinct.
661 if (hasSelfReference(this))
662 return replaceWithDistinctImpl();
663 return replaceWithUniquedImpl();
664}
665
666MDNode *MDNode::replaceWithUniquedImpl() {
667 // Try to uniquify in place.
668 MDNode *UniquedNode = uniquify();
669
670 if (UniquedNode == this) {
671 makeUniqued();
672 return this;
673 }
674
675 // Collision, so RAUW instead.
676 replaceAllUsesWith(UniquedNode);
677 deleteAsSubclass();
678 return UniquedNode;
679}
680
681MDNode *MDNode::replaceWithDistinctImpl() {
682 makeDistinct();
683 return this;
684}
685
686void MDTuple::recalculateHash() {
687 setHash(MDTupleInfo::KeyTy::calculateHash(this));
688}
689
690void MDNode::dropAllReferences() {
691 for (unsigned I = 0, E = NumOperands; I != E; ++I)
692 setOperand(I, nullptr);
693 if (Context.hasReplaceableUses()) {
694 Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
695 (void)Context.takeReplaceableUses();
696 }
697}
698
699void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
700 unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
701 assert(Op < getNumOperands() && "Expected valid operand")(static_cast <bool> (Op < getNumOperands() &&
"Expected valid operand") ? void (0) : __assert_fail ("Op < getNumOperands() && \"Expected valid operand\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 701, __extension__ __PRETTY_FUNCTION__))
;
702
703 if (!isUniqued()) {
704 // This node is not uniqued. Just set the operand and be done with it.
705 setOperand(Op, New);
706 return;
707 }
708
709 // This node is uniqued.
710 eraseFromStore();
711
712 Metadata *Old = getOperand(Op);
713 setOperand(Op, New);
714
715 // Drop uniquing for self-reference cycles and deleted constants.
716 if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
717 if (!isResolved())
718 resolve();
719 storeDistinctInContext();
720 return;
721 }
722
723 // Re-unique the node.
724 auto *Uniqued = uniquify();
725 if (Uniqued == this) {
726 if (!isResolved())
727 resolveAfterOperandChange(Old, New);
728 return;
729 }
730
731 // Collision.
732 if (!isResolved()) {
733 // Still unresolved, so RAUW.
734 //
735 // First, clear out all operands to prevent any recursion (similar to
736 // dropAllReferences(), but we still need the use-list).
737 for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
738 setOperand(O, nullptr);
739 if (Context.hasReplaceableUses())
740 Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
741 deleteAsSubclass();
742 return;
743 }
744
745 // Store in non-uniqued form if RAUW isn't possible.
746 storeDistinctInContext();
747}
748
749void MDNode::deleteAsSubclass() {
750 switch (getMetadataID()) {
751 default:
752 llvm_unreachable("Invalid subclass of MDNode")::llvm::llvm_unreachable_internal("Invalid subclass of MDNode"
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 752)
;
753#define HANDLE_MDNODE_LEAF(CLASS) \
754 case CLASS##Kind: \
755 delete cast<CLASS>(this); \
756 break;
757#include "llvm/IR/Metadata.def"
758 }
759}
760
761template <class T, class InfoT>
762static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
763 if (T *U = getUniqued(Store, N))
764 return U;
765
766 Store.insert(N);
767 return N;
768}
769
770template <class NodeTy> struct MDNode::HasCachedHash {
771 using Yes = char[1];
772 using No = char[2];
773 template <class U, U Val> struct SFINAE {};
774
775 template <class U>
776 static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
777 template <class U> static No &check(...);
778
779 static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
780};
781
782MDNode *MDNode::uniquify() {
783 assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node")(static_cast <bool> (!hasSelfReference(this) &&
"Cannot uniquify a self-referencing node") ? void (0) : __assert_fail
("!hasSelfReference(this) && \"Cannot uniquify a self-referencing node\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 783, __extension__ __PRETTY_FUNCTION__))
;
784
785 // Try to insert into uniquing store.
786 switch (getMetadataID()) {
787 default:
788 llvm_unreachable("Invalid or non-uniquable subclass of MDNode")::llvm::llvm_unreachable_internal("Invalid or non-uniquable subclass of MDNode"
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 788)
;
789#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
790 case CLASS##Kind: { \
791 CLASS *SubclassThis = cast<CLASS>(this); \
792 std::integral_constant<bool, HasCachedHash<CLASS>::value> \
793 ShouldRecalculateHash; \
794 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
795 return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
796 }
797#include "llvm/IR/Metadata.def"
798 }
799}
800
801void MDNode::eraseFromStore() {
802 switch (getMetadataID()) {
803 default:
804 llvm_unreachable("Invalid or non-uniquable subclass of MDNode")::llvm::llvm_unreachable_internal("Invalid or non-uniquable subclass of MDNode"
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 804)
;
805#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
806 case CLASS##Kind: \
807 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
808 break;
809#include "llvm/IR/Metadata.def"
810 }
811}
812
813MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
814 StorageType Storage, bool ShouldCreate) {
815 unsigned Hash = 0;
816 if (Storage == Uniqued) {
3
Taking true branch
817 MDTupleInfo::KeyTy Key(MDs);
818 if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
4
Assuming 'N' is null
5
Taking false branch
819 return N;
820 if (!ShouldCreate)
6
Taking false branch
821 return nullptr;
822 Hash = Key.getHash();
823 } else {
824 assert(ShouldCreate && "Expected non-uniqued nodes to always be created")(static_cast <bool> (ShouldCreate && "Expected non-uniqued nodes to always be created"
) ? void (0) : __assert_fail ("ShouldCreate && \"Expected non-uniqued nodes to always be created\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 824, __extension__ __PRETTY_FUNCTION__))
;
825 }
826
827 return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs),
7
Calling 'MDNode::operator new'
11
Returning from 'MDNode::operator new'
12
Potential memory leak
828 Storage, Context.pImpl->MDTuples);
829}
830
831void MDNode::deleteTemporary(MDNode *N) {
832 assert(N->isTemporary() && "Expected temporary node")(static_cast <bool> (N->isTemporary() && "Expected temporary node"
) ? void (0) : __assert_fail ("N->isTemporary() && \"Expected temporary node\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 832, __extension__ __PRETTY_FUNCTION__))
;
833 N->replaceAllUsesWith(nullptr);
834 N->deleteAsSubclass();
835}
836
837void MDNode::storeDistinctInContext() {
838 assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses")(static_cast <bool> (!Context.hasReplaceableUses() &&
"Unexpected replaceable uses") ? void (0) : __assert_fail ("!Context.hasReplaceableUses() && \"Unexpected replaceable uses\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 838, __extension__ __PRETTY_FUNCTION__))
;
839 assert(!NumUnresolved && "Unexpected unresolved nodes")(static_cast <bool> (!NumUnresolved && "Unexpected unresolved nodes"
) ? void (0) : __assert_fail ("!NumUnresolved && \"Unexpected unresolved nodes\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 839, __extension__ __PRETTY_FUNCTION__))
;
840 Storage = Distinct;
841 assert(isResolved() && "Expected this to be resolved")(static_cast <bool> (isResolved() && "Expected this to be resolved"
) ? void (0) : __assert_fail ("isResolved() && \"Expected this to be resolved\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 841, __extension__ __PRETTY_FUNCTION__))
;
842
843 // Reset the hash.
844 switch (getMetadataID()) {
845 default:
846 llvm_unreachable("Invalid subclass of MDNode")::llvm::llvm_unreachable_internal("Invalid subclass of MDNode"
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 846)
;
847#define HANDLE_MDNODE_LEAF(CLASS) \
848 case CLASS##Kind: { \
849 std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
850 dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
851 break; \
852 }
853#include "llvm/IR/Metadata.def"
854 }
855
856 getContext().pImpl->DistinctMDNodes.push_back(this);
857}
858
859void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
860 if (getOperand(I) == New)
861 return;
862
863 if (!isUniqued()) {
864 setOperand(I, New);
865 return;
866 }
867
868 handleChangedOperand(mutable_begin() + I, New);
869}
870
871void MDNode::setOperand(unsigned I, Metadata *New) {
872 assert(I < NumOperands)(static_cast <bool> (I < NumOperands) ? void (0) : __assert_fail
("I < NumOperands", "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 872, __extension__ __PRETTY_FUNCTION__))
;
873 mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
874}
875
876/// Get a node or a self-reference that looks like it.
877///
878/// Special handling for finding self-references, for use by \a
879/// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
880/// when self-referencing nodes were still uniqued. If the first operand has
881/// the same operands as \c Ops, return the first operand instead.
882static MDNode *getOrSelfReference(LLVMContext &Context,
883 ArrayRef<Metadata *> Ops) {
884 if (!Ops.empty())
885 if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
886 if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
887 for (unsigned I = 1, E = Ops.size(); I != E; ++I)
888 if (Ops[I] != N->getOperand(I))
889 return MDNode::get(Context, Ops);
890 return N;
891 }
892
893 return MDNode::get(Context, Ops);
894}
895
896MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
897 if (!A)
898 return B;
899 if (!B)
900 return A;
901
902 SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
903 MDs.insert(B->op_begin(), B->op_end());
904
905 // FIXME: This preserves long-standing behaviour, but is it really the right
906 // behaviour? Or was that an unintended side-effect of node uniquing?
907 return getOrSelfReference(A->getContext(), MDs.getArrayRef());
908}
909
910MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
911 if (!A || !B)
912 return nullptr;
913
914 SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
915 SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
916 MDs.remove_if([&](Metadata *MD) { return !is_contained(BSet, MD); });
917
918 // FIXME: This preserves long-standing behaviour, but is it really the right
919 // behaviour? Or was that an unintended side-effect of node uniquing?
920 return getOrSelfReference(A->getContext(), MDs.getArrayRef());
921}
922
923MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) {
924 if (!A || !B)
925 return nullptr;
926
927 return concatenate(A, B);
928}
929
930MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
931 if (!A || !B)
932 return nullptr;
933
934 APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
935 APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
936 if (AVal.compare(BVal) == APFloat::cmpLessThan)
937 return A;
938 return B;
939}
940
941static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
942 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
943}
944
945static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
946 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
947}
948
949static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints,
950 ConstantInt *Low, ConstantInt *High) {
951 ConstantRange NewRange(Low->getValue(), High->getValue());
952 unsigned Size = EndPoints.size();
953 APInt LB = EndPoints[Size - 2]->getValue();
954 APInt LE = EndPoints[Size - 1]->getValue();
955 ConstantRange LastRange(LB, LE);
956 if (canBeMerged(NewRange, LastRange)) {
957 ConstantRange Union = LastRange.unionWith(NewRange);
958 Type *Ty = High->getType();
959 EndPoints[Size - 2] =
960 cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
961 EndPoints[Size - 1] =
962 cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
963 return true;
964 }
965 return false;
966}
967
968static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints,
969 ConstantInt *Low, ConstantInt *High) {
970 if (!EndPoints.empty())
971 if (tryMergeRange(EndPoints, Low, High))
972 return;
973
974 EndPoints.push_back(Low);
975 EndPoints.push_back(High);
976}
977
978MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
979 // Given two ranges, we want to compute the union of the ranges. This
980 // is slightly complicated by having to combine the intervals and merge
981 // the ones that overlap.
982
983 if (!A || !B)
984 return nullptr;
985
986 if (A == B)
987 return A;
988
989 // First, walk both lists in order of the lower boundary of each interval.
990 // At each step, try to merge the new interval to the last one we adedd.
991 SmallVector<ConstantInt *, 4> EndPoints;
992 int AI = 0;
993 int BI = 0;
994 int AN = A->getNumOperands() / 2;
995 int BN = B->getNumOperands() / 2;
996 while (AI < AN && BI < BN) {
997 ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
998 ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
999
1000 if (ALow->getValue().slt(BLow->getValue())) {
1001 addRange(EndPoints, ALow,
1002 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1003 ++AI;
1004 } else {
1005 addRange(EndPoints, BLow,
1006 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1007 ++BI;
1008 }
1009 }
1010 while (AI < AN) {
1011 addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1012 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1013 ++AI;
1014 }
1015 while (BI < BN) {
1016 addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1017 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1018 ++BI;
1019 }
1020
1021 // If we have more than 2 ranges (4 endpoints) we have to try to merge
1022 // the last and first ones.
1023 unsigned Size = EndPoints.size();
1024 if (Size > 4) {
1025 ConstantInt *FB = EndPoints[0];
1026 ConstantInt *FE = EndPoints[1];
1027 if (tryMergeRange(EndPoints, FB, FE)) {
1028 for (unsigned i = 0; i < Size - 2; ++i) {
1029 EndPoints[i] = EndPoints[i + 2];
1030 }
1031 EndPoints.resize(Size - 2);
1032 }
1033 }
1034
1035 // If in the end we have a single range, it is possible that it is now the
1036 // full range. Just drop the metadata in that case.
1037 if (EndPoints.size() == 2) {
1038 ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1039 if (Range.isFullSet())
1040 return nullptr;
1041 }
1042
1043 SmallVector<Metadata *, 4> MDs;
1044 MDs.reserve(EndPoints.size());
1045 for (auto *I : EndPoints)
1046 MDs.push_back(ConstantAsMetadata::get(I));
1047 return MDNode::get(A->getContext(), MDs);
1048}
1049
1050MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) {
1051 if (!A || !B)
1052 return nullptr;
1053
1054 ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1055 ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1056 if (AVal->getZExtValue() < BVal->getZExtValue())
1057 return A;
1058 return B;
1059}
1060
1061//===----------------------------------------------------------------------===//
1062// NamedMDNode implementation.
1063//
1064
1065static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) {
1066 return *(SmallVector<TrackingMDRef, 4> *)Operands;
1067}
1068
1069NamedMDNode::NamedMDNode(const Twine &N)
1070 : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1071
1072NamedMDNode::~NamedMDNode() {
1073 dropAllReferences();
1074 delete &getNMDOps(Operands);
1075}
1076
1077unsigned NamedMDNode::getNumOperands() const {
1078 return (unsigned)getNMDOps(Operands).size();
1079}
1080
1081MDNode *NamedMDNode::getOperand(unsigned i) const {
1082 assert(i < getNumOperands() && "Invalid Operand number!")(static_cast <bool> (i < getNumOperands() &&
"Invalid Operand number!") ? void (0) : __assert_fail ("i < getNumOperands() && \"Invalid Operand number!\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1082, __extension__ __PRETTY_FUNCTION__))
;
1083 auto *N = getNMDOps(Operands)[i].get();
1084 return cast_or_null<MDNode>(N);
1085}
1086
1087void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1088
1089void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1090 assert(I < getNumOperands() && "Invalid operand number")(static_cast <bool> (I < getNumOperands() &&
"Invalid operand number") ? void (0) : __assert_fail ("I < getNumOperands() && \"Invalid operand number\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1090, __extension__ __PRETTY_FUNCTION__))
;
1091 getNMDOps(Operands)[I].reset(New);
1092}
1093
1094void NamedMDNode::eraseFromParent() { getParent()->eraseNamedMetadata(this); }
1095
1096void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1097
1098StringRef NamedMDNode::getName() const { return StringRef(Name); }
1099
1100//===----------------------------------------------------------------------===//
1101// Instruction Metadata method implementations.
1102//
1103void MDAttachmentMap::set(unsigned ID, MDNode &MD) {
1104 for (auto &I : Attachments)
1105 if (I.first == ID) {
1106 I.second.reset(&MD);
1107 return;
1108 }
1109 Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID),
1110 std::make_tuple(&MD));
1111}
1112
1113void MDAttachmentMap::erase(unsigned ID) {
1114 if (empty())
1115 return;
1116
1117 // Common case is one/last value.
1118 if (Attachments.back().first == ID) {
1119 Attachments.pop_back();
1120 return;
1121 }
1122
1123 for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E;
1124 ++I)
1125 if (I->first == ID) {
1126 *I = std::move(Attachments.back());
1127 Attachments.pop_back();
1128 return;
1129 }
1130}
1131
1132MDNode *MDAttachmentMap::lookup(unsigned ID) const {
1133 for (const auto &I : Attachments)
1134 if (I.first == ID)
1135 return I.second;
1136 return nullptr;
1137}
1138
1139void MDAttachmentMap::getAll(
1140 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1141 Result.append(Attachments.begin(), Attachments.end());
1142
1143 // Sort the resulting array so it is stable.
1144 if (Result.size() > 1)
1145 array_pod_sort(Result.begin(), Result.end());
1146}
1147
1148void MDGlobalAttachmentMap::insert(unsigned ID, MDNode &MD) {
1149 Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1150}
1151
1152void MDGlobalAttachmentMap::get(unsigned ID,
1153 SmallVectorImpl<MDNode *> &Result) {
1154 for (auto A : Attachments)
1155 if (A.MDKind == ID)
1156 Result.push_back(A.Node);
1157}
1158
1159void MDGlobalAttachmentMap::erase(unsigned ID) {
1160 auto Follower = Attachments.begin();
1161 for (auto Leader = Attachments.begin(), E = Attachments.end(); Leader != E;
1162 ++Leader) {
1163 if (Leader->MDKind != ID) {
1164 if (Follower != Leader)
1165 *Follower = std::move(*Leader);
1166 ++Follower;
1167 }
1168 }
1169 Attachments.resize(Follower - Attachments.begin());
1170}
1171
1172void MDGlobalAttachmentMap::getAll(
1173 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1174 for (auto &A : Attachments)
1175 Result.emplace_back(A.MDKind, A.Node);
1176
1177 // Sort the resulting array so it is stable with respect to metadata IDs. We
1178 // need to preserve the original insertion order though.
1179 std::stable_sort(
1180 Result.begin(), Result.end(),
1181 [](const std::pair<unsigned, MDNode *> &A,
1182 const std::pair<unsigned, MDNode *> &B) { return A.first < B.first; });
1183}
1184
1185void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
1186 if (!Node && !hasMetadata())
1187 return;
1188 setMetadata(getContext().getMDKindID(Kind), Node);
1189}
1190
1191MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1192 return getMetadataImpl(getContext().getMDKindID(Kind));
1193}
1194
1195void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
1196 if (!hasMetadataHashEntry())
1197 return; // Nothing to remove!
1198
1199 auto &InstructionMetadata = getContext().pImpl->InstructionMetadata;
1200
1201 SmallSet<unsigned, 4> KnownSet;
1202 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1203 if (KnownSet.empty()) {
1204 // Just drop our entry at the store.
1205 InstructionMetadata.erase(this);
1206 setHasMetadataHashEntry(false);
1207 return;
1208 }
1209
1210 auto &Info = InstructionMetadata[this];
1211 Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
1212 return !KnownSet.count(I.first);
1213 });
1214
1215 if (Info.empty()) {
1216 // Drop our entry at the store.
1217 InstructionMetadata.erase(this);
1218 setHasMetadataHashEntry(false);
1219 }
1220}
1221
1222void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1223 if (!Node && !hasMetadata())
1224 return;
1225
1226 // Handle 'dbg' as a special case since it is not stored in the hash table.
1227 if (KindID == LLVMContext::MD_dbg) {
1228 DbgLoc = DebugLoc(Node);
1229 return;
1230 }
1231
1232 // Handle the case when we're adding/updating metadata on an instruction.
1233 if (Node) {
1234 auto &Info = getContext().pImpl->InstructionMetadata[this];
1235 assert(!Info.empty() == hasMetadataHashEntry() &&(static_cast <bool> (!Info.empty() == hasMetadataHashEntry
() && "HasMetadata bit is wonked") ? void (0) : __assert_fail
("!Info.empty() == hasMetadataHashEntry() && \"HasMetadata bit is wonked\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1236, __extension__ __PRETTY_FUNCTION__))
1236 "HasMetadata bit is wonked")(static_cast <bool> (!Info.empty() == hasMetadataHashEntry
() && "HasMetadata bit is wonked") ? void (0) : __assert_fail
("!Info.empty() == hasMetadataHashEntry() && \"HasMetadata bit is wonked\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1236, __extension__ __PRETTY_FUNCTION__))
;
1237 if (Info.empty())
1238 setHasMetadataHashEntry(true);
1239 Info.set(KindID, *Node);
1240 return;
1241 }
1242
1243 // Otherwise, we're removing metadata from an instruction.
1244 assert((hasMetadataHashEntry() ==(static_cast <bool> ((hasMetadataHashEntry() == (getContext
().pImpl->InstructionMetadata.count(this) > 0)) &&
"HasMetadata bit out of date!") ? void (0) : __assert_fail (
"(hasMetadataHashEntry() == (getContext().pImpl->InstructionMetadata.count(this) > 0)) && \"HasMetadata bit out of date!\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1246, __extension__ __PRETTY_FUNCTION__))
1245 (getContext().pImpl->InstructionMetadata.count(this) > 0)) &&(static_cast <bool> ((hasMetadataHashEntry() == (getContext
().pImpl->InstructionMetadata.count(this) > 0)) &&
"HasMetadata bit out of date!") ? void (0) : __assert_fail (
"(hasMetadataHashEntry() == (getContext().pImpl->InstructionMetadata.count(this) > 0)) && \"HasMetadata bit out of date!\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1246, __extension__ __PRETTY_FUNCTION__))
1246 "HasMetadata bit out of date!")(static_cast <bool> ((hasMetadataHashEntry() == (getContext
().pImpl->InstructionMetadata.count(this) > 0)) &&
"HasMetadata bit out of date!") ? void (0) : __assert_fail (
"(hasMetadataHashEntry() == (getContext().pImpl->InstructionMetadata.count(this) > 0)) && \"HasMetadata bit out of date!\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1246, __extension__ __PRETTY_FUNCTION__))
;
1247 if (!hasMetadataHashEntry())
1248 return; // Nothing to remove!
1249 auto &Info = getContext().pImpl->InstructionMetadata[this];
1250
1251 // Handle removal of an existing value.
1252 Info.erase(KindID);
1253
1254 if (!Info.empty())
1255 return;
1256
1257 getContext().pImpl->InstructionMetadata.erase(this);
1258 setHasMetadataHashEntry(false);
1259}
1260
1261void Instruction::setAAMetadata(const AAMDNodes &N) {
1262 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1263 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1264 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1265}
1266
1267MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
1268 // Handle 'dbg' as a special case since it is not stored in the hash table.
1269 if (KindID == LLVMContext::MD_dbg)
1270 return DbgLoc.getAsMDNode();
1271
1272 if (!hasMetadataHashEntry())
1273 return nullptr;
1274 auto &Info = getContext().pImpl->InstructionMetadata[this];
1275 assert(!Info.empty() && "bit out of sync with hash table")(static_cast <bool> (!Info.empty() && "bit out of sync with hash table"
) ? void (0) : __assert_fail ("!Info.empty() && \"bit out of sync with hash table\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1275, __extension__ __PRETTY_FUNCTION__))
;
1276
1277 return Info.lookup(KindID);
1278}
1279
1280void Instruction::getAllMetadataImpl(
1281 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1282 Result.clear();
1283
1284 // Handle 'dbg' as a special case since it is not stored in the hash table.
1285 if (DbgLoc) {
1286 Result.push_back(
1287 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1288 if (!hasMetadataHashEntry())
1289 return;
1290 }
1291
1292 assert(hasMetadataHashEntry() &&(static_cast <bool> (hasMetadataHashEntry() && getContext
().pImpl->InstructionMetadata.count(this) && "Shouldn't have called this"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && getContext().pImpl->InstructionMetadata.count(this) && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1294, __extension__ __PRETTY_FUNCTION__))
1293 getContext().pImpl->InstructionMetadata.count(this) &&(static_cast <bool> (hasMetadataHashEntry() && getContext
().pImpl->InstructionMetadata.count(this) && "Shouldn't have called this"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && getContext().pImpl->InstructionMetadata.count(this) && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1294, __extension__ __PRETTY_FUNCTION__))
1294 "Shouldn't have called this")(static_cast <bool> (hasMetadataHashEntry() && getContext
().pImpl->InstructionMetadata.count(this) && "Shouldn't have called this"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && getContext().pImpl->InstructionMetadata.count(this) && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1294, __extension__ __PRETTY_FUNCTION__))
;
1295 const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
1296 assert(!Info.empty() && "Shouldn't have called this")(static_cast <bool> (!Info.empty() && "Shouldn't have called this"
) ? void (0) : __assert_fail ("!Info.empty() && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1296, __extension__ __PRETTY_FUNCTION__))
;
1297 Info.getAll(Result);
1298}
1299
1300void Instruction::getAllMetadataOtherThanDebugLocImpl(
1301 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1302 Result.clear();
1303 assert(hasMetadataHashEntry() &&(static_cast <bool> (hasMetadataHashEntry() && getContext
().pImpl->InstructionMetadata.count(this) && "Shouldn't have called this"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && getContext().pImpl->InstructionMetadata.count(this) && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1305, __extension__ __PRETTY_FUNCTION__))
1304 getContext().pImpl->InstructionMetadata.count(this) &&(static_cast <bool> (hasMetadataHashEntry() && getContext
().pImpl->InstructionMetadata.count(this) && "Shouldn't have called this"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && getContext().pImpl->InstructionMetadata.count(this) && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1305, __extension__ __PRETTY_FUNCTION__))
1305 "Shouldn't have called this")(static_cast <bool> (hasMetadataHashEntry() && getContext
().pImpl->InstructionMetadata.count(this) && "Shouldn't have called this"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && getContext().pImpl->InstructionMetadata.count(this) && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1305, __extension__ __PRETTY_FUNCTION__))
;
1306 const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
1307 assert(!Info.empty() && "Shouldn't have called this")(static_cast <bool> (!Info.empty() && "Shouldn't have called this"
) ? void (0) : __assert_fail ("!Info.empty() && \"Shouldn't have called this\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1307, __extension__ __PRETTY_FUNCTION__))
;
1308 Info.getAll(Result);
1309}
1310
1311bool Instruction::extractProfMetadata(uint64_t &TrueVal,
1312 uint64_t &FalseVal) const {
1313 assert((static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select) && "Looking for branch weights on something besides branch or select"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) && \"Looking for branch weights on something besides branch or select\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1315, __extension__ __PRETTY_FUNCTION__))
1314 (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) &&(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select) && "Looking for branch weights on something besides branch or select"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) && \"Looking for branch weights on something besides branch or select\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1315, __extension__ __PRETTY_FUNCTION__))
1315 "Looking for branch weights on something besides branch or select")(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select) && "Looking for branch weights on something besides branch or select"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) && \"Looking for branch weights on something besides branch or select\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1315, __extension__ __PRETTY_FUNCTION__))
;
1316
1317 auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1318 if (!ProfileData || ProfileData->getNumOperands() != 3)
1319 return false;
1320
1321 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1322 if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
1323 return false;
1324
1325 auto *CITrue = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
1326 auto *CIFalse = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
1327 if (!CITrue || !CIFalse)
1328 return false;
1329
1330 TrueVal = CITrue->getValue().getZExtValue();
1331 FalseVal = CIFalse->getValue().getZExtValue();
1332
1333 return true;
1334}
1335
1336bool Instruction::extractProfTotalWeight(uint64_t &TotalVal) const {
1337 assert((getOpcode() == Instruction::Br ||(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select || getOpcode() == Instruction
::Call || getOpcode() == Instruction::Invoke || getOpcode() ==
Instruction::Switch) && "Looking for branch weights on something besides branch"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select || getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke || getOpcode() == Instruction::Switch) && \"Looking for branch weights on something besides branch\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1342, __extension__ __PRETTY_FUNCTION__))
1338 getOpcode() == Instruction::Select ||(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select || getOpcode() == Instruction
::Call || getOpcode() == Instruction::Invoke || getOpcode() ==
Instruction::Switch) && "Looking for branch weights on something besides branch"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select || getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke || getOpcode() == Instruction::Switch) && \"Looking for branch weights on something besides branch\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1342, __extension__ __PRETTY_FUNCTION__))
1339 getOpcode() == Instruction::Call ||(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select || getOpcode() == Instruction
::Call || getOpcode() == Instruction::Invoke || getOpcode() ==
Instruction::Switch) && "Looking for branch weights on something besides branch"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select || getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke || getOpcode() == Instruction::Switch) && \"Looking for branch weights on something besides branch\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1342, __extension__ __PRETTY_FUNCTION__))
1340 getOpcode() == Instruction::Invoke ||(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select || getOpcode() == Instruction
::Call || getOpcode() == Instruction::Invoke || getOpcode() ==
Instruction::Switch) && "Looking for branch weights on something besides branch"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select || getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke || getOpcode() == Instruction::Switch) && \"Looking for branch weights on something besides branch\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1342, __extension__ __PRETTY_FUNCTION__))
1341 getOpcode() == Instruction::Switch) &&(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select || getOpcode() == Instruction
::Call || getOpcode() == Instruction::Invoke || getOpcode() ==
Instruction::Switch) && "Looking for branch weights on something besides branch"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select || getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke || getOpcode() == Instruction::Switch) && \"Looking for branch weights on something besides branch\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1342, __extension__ __PRETTY_FUNCTION__))
1342 "Looking for branch weights on something besides branch")(static_cast <bool> ((getOpcode() == Instruction::Br ||
getOpcode() == Instruction::Select || getOpcode() == Instruction
::Call || getOpcode() == Instruction::Invoke || getOpcode() ==
Instruction::Switch) && "Looking for branch weights on something besides branch"
) ? void (0) : __assert_fail ("(getOpcode() == Instruction::Br || getOpcode() == Instruction::Select || getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke || getOpcode() == Instruction::Switch) && \"Looking for branch weights on something besides branch\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1342, __extension__ __PRETTY_FUNCTION__))
;
1343
1344 TotalVal = 0;
1345 auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1346 if (!ProfileData)
1347 return false;
1348
1349 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1350 if (!ProfDataName)
1351 return false;
1352
1353 if (ProfDataName->getString().equals("branch_weights")) {
1354 TotalVal = 0;
1355 for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
1356 auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i));
1357 if (!V)
1358 return false;
1359 TotalVal += V->getValue().getZExtValue();
1360 }
1361 return true;
1362 } else if (ProfDataName->getString().equals("VP") &&
1363 ProfileData->getNumOperands() > 3) {
1364 TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
1365 ->getValue()
1366 .getZExtValue();
1367 return true;
1368 }
1369 return false;
1370}
1371
1372void Instruction::clearMetadataHashEntries() {
1373 assert(hasMetadataHashEntry() && "Caller should check")(static_cast <bool> (hasMetadataHashEntry() && "Caller should check"
) ? void (0) : __assert_fail ("hasMetadataHashEntry() && \"Caller should check\""
, "/build/llvm-toolchain-snapshot-7~svn325118/lib/IR/Metadata.cpp"
, 1373, __extension__ __PRETTY_FUNCTION__))
;
1374 getContext().pImpl->InstructionMetadata.erase(this);
1375 setHasMetadataHashEntry(false);
1376}
1377
1378void GlobalObject::getMetadata(unsigned KindID,
1379 SmallVectorImpl<MDNode *> &MDs) const {
1380 if (hasMetadata())
1381 getContext().pImpl->GlobalObjectMetadata[this].get(KindID, MDs);
1382}
1383
1384void GlobalObject::getMetadata(StringRef Kind,
1385 SmallVectorImpl<MDNode *> &MDs) const {
1386 if (hasMetadata())
1387 getMetadata(getContext().getMDKindID(Kind), MDs);
1388}
1389
1390void GlobalObject::addMetadata(unsigned KindID, MDNode &MD) {
1391 if (!hasMetadata())
1392 setHasMetadataHashEntry(true);
1393
1394 getContext().pImpl->GlobalObjectMetadata[this].insert(KindID, MD);
1395}
1396
1397void GlobalObject::addMetadata(StringRef Kind, MDNode &MD) {
1398 addMetadata(getContext().getMDKindID(Kind), MD);
1399}
1400
1401void GlobalObject::eraseMetadata(unsigned KindID) {
1402 // Nothing to unset.
1403 if (!hasMetadata())
1404 return;
1405
1406 auto &Store = getContext().pImpl->GlobalObjectMetadata[this];
1407 Store.erase(KindID);
1408 if (Store.empty())
1409 clearMetadata();
1410}
1411
1412void GlobalObject::getAllMetadata(
1413 SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1414 MDs.clear();
1415
1416 if (!hasMetadata())
1417 return;
1418
1419 getContext().pImpl->GlobalObjectMetadata[this].getAll(MDs);
1420}
1421
1422void GlobalObject::clearMetadata() {
1423 if (!hasMetadata())
1424 return;
1425 getContext().pImpl->GlobalObjectMetadata.erase(this);
1426 setHasMetadataHashEntry(false);
1427}
1428
1429void GlobalObject::setMetadata(unsigned KindID, MDNode *N) {
1430 eraseMetadata(KindID);
1431 if (N)
1432 addMetadata(KindID, *N);
1433}
1434
1435void GlobalObject::setMetadata(StringRef Kind, MDNode *N) {
1436 setMetadata(getContext().getMDKindID(Kind), N);
1437}
1438
1439MDNode *GlobalObject::getMetadata(unsigned KindID) const {
1440 SmallVector<MDNode *, 1> MDs;
1441 getMetadata(KindID, MDs);
1442 if (MDs.empty())
1443 return nullptr;
1444 return MDs[0];
1445}
1446
1447MDNode *GlobalObject::getMetadata(StringRef Kind) const {
1448 return getMetadata(getContext().getMDKindID(Kind));
1449}
1450
1451void GlobalObject::copyMetadata(const GlobalObject *Other, unsigned Offset) {
1452 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1453 Other->getAllMetadata(MDs);
1454 for (auto &MD : MDs) {
1455 // We need to adjust the type metadata offset.
1456 if (Offset != 0 && MD.first == LLVMContext::MD_type) {
1457 auto *OffsetConst = cast<ConstantInt>(
1458 cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1459 Metadata *TypeId = MD.second->getOperand(1);
1460 auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1461 OffsetConst->getType(), OffsetConst->getValue() + Offset));
1462 addMetadata(LLVMContext::MD_type,
1463 *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1464 continue;
1465 }
1466 // If an offset adjustment was specified we need to modify the DIExpression
1467 // to prepend the adjustment:
1468 // !DIExpression(DW_OP_plus, Offset, [original expr])
1469 auto *Attachment = MD.second;
1470 if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
1471 DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1472 DIExpression *E = nullptr;
1473 if (!GV) {
1474 auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1475 GV = GVE->getVariable();
1476 E = GVE->getExpression();
1477 }
1478 ArrayRef<uint64_t> OrigElements;
1479 if (E)
1480 OrigElements = E->getElements();
1481 std::vector<uint64_t> Elements(OrigElements.size() + 2);
1482 Elements[0] = dwarf::DW_OP_plus_uconst;
1483 Elements[1] = Offset;
1484 std::copy(OrigElements.begin(), OrigElements.end(), Elements.begin() + 2);
1485 E = DIExpression::get(getContext(), Elements);
1486 Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1487 }
1488 addMetadata(MD.first, *Attachment);
1489 }
1490}
1491
1492void GlobalObject::addTypeMetadata(unsigned Offset, Metadata *TypeID) {
1493 addMetadata(
1494 LLVMContext::MD_type,
1495 *MDTuple::get(getContext(),
1
Calling 'MDTuple::get'
1496 {ConstantAsMetadata::get(ConstantInt::get(
1497 Type::getInt64Ty(getContext()), Offset)),
1498 TypeID}));
1499}
1500
1501void Function::setSubprogram(DISubprogram *SP) {
1502 setMetadata(LLVMContext::MD_dbg, SP);
1503}
1504
1505DISubprogram *Function::getSubprogram() const {
1506 return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1507}
1508
1509bool Function::isDebugInfoForProfiling() const {
1510 if (DISubprogram *SP = getSubprogram()) {
1511 if (DICompileUnit *CU = SP->getUnit()) {
1512 return CU->getDebugInfoForProfiling();
1513 }
1514 }
1515 return false;
1516}
1517
1518void GlobalVariable::addDebugInfo(DIGlobalVariableExpression *GV) {
1519 addMetadata(LLVMContext::MD_dbg, *GV);
1520}
1521
1522void GlobalVariable::getDebugInfo(
1523 SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const {
1524 SmallVector<MDNode *, 1> MDs;
1525 getMetadata(LLVMContext::MD_dbg, MDs);
1526 for (MDNode *MD : MDs)
1527 GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1528}

/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h

1//===- llvm/IR/Metadata.h - Metadata definitions ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// @file
11/// This file contains the declarations for metadata subclasses.
12/// They represent the different flavors of metadata that live in LLVM.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_METADATA_H
17#define LLVM_IR_METADATA_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/DenseMapInfo.h"
22#include "llvm/ADT/None.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/ilist_node.h"
28#include "llvm/ADT/iterator_range.h"
29#include "llvm/IR/Constant.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Value.h"
32#include "llvm/Support/CBindingWrapping.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/ErrorHandling.h"
35#include <cassert>
36#include <cstddef>
37#include <cstdint>
38#include <iterator>
39#include <memory>
40#include <string>
41#include <type_traits>
42#include <utility>
43
44namespace llvm {
45
46class Module;
47class ModuleSlotTracker;
48class raw_ostream;
49class Type;
50
51enum LLVMConstants : uint32_t {
52 DEBUG_METADATA_VERSION = 3 // Current debug info version number.
53};
54
55/// \brief Root of the metadata hierarchy.
56///
57/// This is a root class for typeless data in the IR.
58class Metadata {
59 friend class ReplaceableMetadataImpl;
60
61 /// \brief RTTI.
62 const unsigned char SubclassID;
63
64protected:
65 /// \brief Active type of storage.
66 enum StorageType { Uniqued, Distinct, Temporary };
67
68 /// \brief Storage flag for non-uniqued, otherwise unowned, metadata.
69 unsigned char Storage;
70 // TODO: expose remaining bits to subclasses.
71
72 unsigned short SubclassData16 = 0;
73 unsigned SubclassData32 = 0;
74
75public:
76 enum MetadataKind {
77#define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
78#include "llvm/IR/Metadata.def"
79 };
80
81protected:
82 Metadata(unsigned ID, StorageType Storage)
83 : SubclassID(ID), Storage(Storage) {
84 static_assert(sizeof(*this) == 8, "Metadata fields poorly packed");
85 }
86
87 ~Metadata() = default;
88
89 /// \brief Default handling of a changed operand, which asserts.
90 ///
91 /// If subclasses pass themselves in as owners to a tracking node reference,
92 /// they must provide an implementation of this method.
93 void handleChangedOperand(void *, Metadata *) {
94 llvm_unreachable("Unimplemented in Metadata subclass")::llvm::llvm_unreachable_internal("Unimplemented in Metadata subclass"
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 94)
;
95 }
96
97public:
98 unsigned getMetadataID() const { return SubclassID; }
99
100 /// \brief User-friendly dump.
101 ///
102 /// If \c M is provided, metadata nodes will be numbered canonically;
103 /// otherwise, pointer addresses are substituted.
104 ///
105 /// Note: this uses an explicit overload instead of default arguments so that
106 /// the nullptr version is easy to call from a debugger.
107 ///
108 /// @{
109 void dump() const;
110 void dump(const Module *M) const;
111 /// @}
112
113 /// \brief Print.
114 ///
115 /// Prints definition of \c this.
116 ///
117 /// If \c M is provided, metadata nodes will be numbered canonically;
118 /// otherwise, pointer addresses are substituted.
119 /// @{
120 void print(raw_ostream &OS, const Module *M = nullptr,
121 bool IsForDebug = false) const;
122 void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr,
123 bool IsForDebug = false) const;
124 /// @}
125
126 /// \brief Print as operand.
127 ///
128 /// Prints reference of \c this.
129 ///
130 /// If \c M is provided, metadata nodes will be numbered canonically;
131 /// otherwise, pointer addresses are substituted.
132 /// @{
133 void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const;
134 void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
135 const Module *M = nullptr) const;
136 /// @}
137};
138
139// Create wrappers for C Binding types (see CBindingWrapping.h).
140DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)inline Metadata *unwrap(LLVMMetadataRef P) { return reinterpret_cast
<Metadata*>(P); } inline LLVMMetadataRef wrap(const Metadata
*P) { return reinterpret_cast<LLVMMetadataRef>(const_cast
<Metadata*>(P)); } template<typename T> inline T *
unwrap(LLVMMetadataRef P) { return cast<T>(unwrap(P)); }
141
142// Specialized opaque metadata conversions.
143inline Metadata **unwrap(LLVMMetadataRef *MDs) {
144 return reinterpret_cast<Metadata**>(MDs);
145}
146
147#define HANDLE_METADATA(CLASS) class CLASS;
148#include "llvm/IR/Metadata.def"
149
150// Provide specializations of isa so that we don't need definitions of
151// subclasses to see if the metadata is a subclass.
152#define HANDLE_METADATA_LEAF(CLASS) \
153 template <> struct isa_impl<CLASS, Metadata> { \
154 static inline bool doit(const Metadata &MD) { \
155 return MD.getMetadataID() == Metadata::CLASS##Kind; \
156 } \
157 };
158#include "llvm/IR/Metadata.def"
159
160inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
161 MD.print(OS);
162 return OS;
163}
164
165/// \brief Metadata wrapper in the Value hierarchy.
166///
167/// A member of the \a Value hierarchy to represent a reference to metadata.
168/// This allows, e.g., instrinsics to have metadata as operands.
169///
170/// Notably, this is the only thing in either hierarchy that is allowed to
171/// reference \a LocalAsMetadata.
172class MetadataAsValue : public Value {
173 friend class ReplaceableMetadataImpl;
174 friend class LLVMContextImpl;
175
176 Metadata *MD;
177
178 MetadataAsValue(Type *Ty, Metadata *MD);
179
180 /// \brief Drop use of metadata (during teardown).
181 void dropUse() { MD = nullptr; }
182
183public:
184 ~MetadataAsValue();
185
186 static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
187 static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
188
189 Metadata *getMetadata() const { return MD; }
190
191 static bool classof(const Value *V) {
192 return V->getValueID() == MetadataAsValueVal;
193 }
194
195private:
196 void handleChangedMetadata(Metadata *MD);
197 void track();
198 void untrack();
199};
200
201/// \brief API for tracking metadata references through RAUW and deletion.
202///
203/// Shared API for updating \a Metadata pointers in subclasses that support
204/// RAUW.
205///
206/// This API is not meant to be used directly. See \a TrackingMDRef for a
207/// user-friendly tracking reference.
208class MetadataTracking {
209public:
210 /// \brief Track the reference to metadata.
211 ///
212 /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
213 /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
214 /// deleted, \c MD will be set to \c nullptr.
215 ///
216 /// If tracking isn't supported, \c *MD will not change.
217 ///
218 /// \return true iff tracking is supported by \c MD.
219 static bool track(Metadata *&MD) {
220 return track(&MD, *MD, static_cast<Metadata *>(nullptr));
221 }
222
223 /// \brief Track the reference to metadata for \a Metadata.
224 ///
225 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
226 /// tell it that its operand changed. This could trigger \c Owner being
227 /// re-uniqued.
228 static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
229 return track(Ref, MD, &Owner);
230 }
231
232 /// \brief Track the reference to metadata for \a MetadataAsValue.
233 ///
234 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
235 /// tell it that its operand changed. This could trigger \c Owner being
236 /// re-uniqued.
237 static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
238 return track(Ref, MD, &Owner);
239 }
240
241 /// \brief Stop tracking a reference to metadata.
242 ///
243 /// Stops \c *MD from tracking \c MD.
244 static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
245 static void untrack(void *Ref, Metadata &MD);
246
247 /// \brief Move tracking from one reference to another.
248 ///
249 /// Semantically equivalent to \c untrack(MD) followed by \c track(New),
250 /// except that ownership callbacks are maintained.
251 ///
252 /// Note: it is an error if \c *MD does not equal \c New.
253 ///
254 /// \return true iff tracking is supported by \c MD.
255 static bool retrack(Metadata *&MD, Metadata *&New) {
256 return retrack(&MD, *MD, &New);
257 }
258 static bool retrack(void *Ref, Metadata &MD, void *New);
259
260 /// \brief Check whether metadata is replaceable.
261 static bool isReplaceable(const Metadata &MD);
262
263 using OwnerTy = PointerUnion<MetadataAsValue *, Metadata *>;
264
265private:
266 /// \brief Track a reference to metadata for an owner.
267 ///
268 /// Generalized version of tracking.
269 static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
270};
271
272/// \brief Shared implementation of use-lists for replaceable metadata.
273///
274/// Most metadata cannot be RAUW'ed. This is a shared implementation of
275/// use-lists and associated API for the two that support it (\a ValueAsMetadata
276/// and \a TempMDNode).
277class ReplaceableMetadataImpl {
278 friend class MetadataTracking;
279
280public:
281 using OwnerTy = MetadataTracking::OwnerTy;
282
283private:
284 LLVMContext &Context;
285 uint64_t NextIndex = 0;
286 SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
287
288public:
289 ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {}
290
291 ~ReplaceableMetadataImpl() {
292 assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata")(static_cast <bool> (UseMap.empty() && "Cannot destroy in-use replaceable metadata"
) ? void (0) : __assert_fail ("UseMap.empty() && \"Cannot destroy in-use replaceable metadata\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 292, __extension__ __PRETTY_FUNCTION__))
;
293 }
294
295 LLVMContext &getContext() const { return Context; }
296
297 /// \brief Replace all uses of this with MD.
298 ///
299 /// Replace all uses of this with \c MD, which is allowed to be null.
300 void replaceAllUsesWith(Metadata *MD);
301
302 /// \brief Resolve all uses of this.
303 ///
304 /// Resolve all uses of this, turning off RAUW permanently. If \c
305 /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
306 /// is resolved.
307 void resolveAllUses(bool ResolveUsers = true);
308
309private:
310 void addRef(void *Ref, OwnerTy Owner);
311 void dropRef(void *Ref);
312 void moveRef(void *Ref, void *New, const Metadata &MD);
313
314 /// Lazily construct RAUW support on MD.
315 ///
316 /// If this is an unresolved MDNode, RAUW support will be created on-demand.
317 /// ValueAsMetadata always has RAUW support.
318 static ReplaceableMetadataImpl *getOrCreate(Metadata &MD);
319
320 /// Get RAUW support on MD, if it exists.
321 static ReplaceableMetadataImpl *getIfExists(Metadata &MD);
322
323 /// Check whether this node will support RAUW.
324 ///
325 /// Returns \c true unless getOrCreate() would return null.
326 static bool isReplaceable(const Metadata &MD);
327};
328
329/// \brief Value wrapper in the Metadata hierarchy.
330///
331/// This is a custom value handle that allows other metadata to refer to
332/// classes in the Value hierarchy.
333///
334/// Because of full uniquing support, each value is only wrapped by a single \a
335/// ValueAsMetadata object, so the lookup maps are far more efficient than
336/// those using ValueHandleBase.
337class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
338 friend class ReplaceableMetadataImpl;
339 friend class LLVMContextImpl;
340
341 Value *V;
342
343 /// \brief Drop users without RAUW (during teardown).
344 void dropUsers() {
345 ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
346 }
347
348protected:
349 ValueAsMetadata(unsigned ID, Value *V)
350 : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
351 assert(V && "Expected valid value")(static_cast <bool> (V && "Expected valid value"
) ? void (0) : __assert_fail ("V && \"Expected valid value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 351, __extension__ __PRETTY_FUNCTION__))
;
352 }
353
354 ~ValueAsMetadata() = default;
355
356public:
357 static ValueAsMetadata *get(Value *V);
358
359 static ConstantAsMetadata *getConstant(Value *C) {
360 return cast<ConstantAsMetadata>(get(C));
361 }
362
363 static LocalAsMetadata *getLocal(Value *Local) {
364 return cast<LocalAsMetadata>(get(Local));
365 }
366
367 static ValueAsMetadata *getIfExists(Value *V);
368
369 static ConstantAsMetadata *getConstantIfExists(Value *C) {
370 return cast_or_null<ConstantAsMetadata>(getIfExists(C));
371 }
372
373 static LocalAsMetadata *getLocalIfExists(Value *Local) {
374 return cast_or_null<LocalAsMetadata>(getIfExists(Local));
375 }
376
377 Value *getValue() const { return V; }
378 Type *getType() const { return V->getType(); }
379 LLVMContext &getContext() const { return V->getContext(); }
380
381 static void handleDeletion(Value *V);
382 static void handleRAUW(Value *From, Value *To);
383
384protected:
385 /// \brief Handle collisions after \a Value::replaceAllUsesWith().
386 ///
387 /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
388 /// \a Value gets RAUW'ed and the target already exists, this is used to
389 /// merge the two metadata nodes.
390 void replaceAllUsesWith(Metadata *MD) {
391 ReplaceableMetadataImpl::replaceAllUsesWith(MD);
392 }
393
394public:
395 static bool classof(const Metadata *MD) {
396 return MD->getMetadataID() == LocalAsMetadataKind ||
397 MD->getMetadataID() == ConstantAsMetadataKind;
398 }
399};
400
401class ConstantAsMetadata : public ValueAsMetadata {
402 friend class ValueAsMetadata;
403
404 ConstantAsMetadata(Constant *C)
405 : ValueAsMetadata(ConstantAsMetadataKind, C) {}
406
407public:
408 static ConstantAsMetadata *get(Constant *C) {
409 return ValueAsMetadata::getConstant(C);
410 }
411
412 static ConstantAsMetadata *getIfExists(Constant *C) {
413 return ValueAsMetadata::getConstantIfExists(C);
414 }
415
416 Constant *getValue() const {
417 return cast<Constant>(ValueAsMetadata::getValue());
418 }
419
420 static bool classof(const Metadata *MD) {
421 return MD->getMetadataID() == ConstantAsMetadataKind;
422 }
423};
424
425class LocalAsMetadata : public ValueAsMetadata {
426 friend class ValueAsMetadata;
427
428 LocalAsMetadata(Value *Local)
429 : ValueAsMetadata(LocalAsMetadataKind, Local) {
430 assert(!isa<Constant>(Local) && "Expected local value")(static_cast <bool> (!isa<Constant>(Local) &&
"Expected local value") ? void (0) : __assert_fail ("!isa<Constant>(Local) && \"Expected local value\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 430, __extension__ __PRETTY_FUNCTION__))
;
431 }
432
433public:
434 static LocalAsMetadata *get(Value *Local) {
435 return ValueAsMetadata::getLocal(Local);
436 }
437
438 static LocalAsMetadata *getIfExists(Value *Local) {
439 return ValueAsMetadata::getLocalIfExists(Local);
440 }
441
442 static bool classof(const Metadata *MD) {
443 return MD->getMetadataID() == LocalAsMetadataKind;
444 }
445};
446
447/// \brief Transitional API for extracting constants from Metadata.
448///
449/// This namespace contains transitional functions for metadata that points to
450/// \a Constants.
451///
452/// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
453/// operands could refer to any \a Value. There's was a lot of code like this:
454///
455/// \code
456/// MDNode *N = ...;
457/// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
458/// \endcode
459///
460/// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
461/// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
462/// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
463/// cast in the \a Value hierarchy. Besides creating boiler-plate, this
464/// requires subtle control flow changes.
465///
466/// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
467/// so that metadata can refer to numbers without traversing a bridge to the \a
468/// Value hierarchy. In this final state, the code above would look like this:
469///
470/// \code
471/// MDNode *N = ...;
472/// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
473/// \endcode
474///
475/// The API in this namespace supports the transition. \a MDInt doesn't exist
476/// yet, and even once it does, changing each metadata schema to use it is its
477/// own mini-project. In the meantime this API prevents us from introducing
478/// complex and bug-prone control flow that will disappear in the end. In
479/// particular, the above code looks like this:
480///
481/// \code
482/// MDNode *N = ...;
483/// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
484/// \endcode
485///
486/// The full set of provided functions includes:
487///
488/// mdconst::hasa <=> isa
489/// mdconst::extract <=> cast
490/// mdconst::extract_or_null <=> cast_or_null
491/// mdconst::dyn_extract <=> dyn_cast
492/// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
493///
494/// The target of the cast must be a subclass of \a Constant.
495namespace mdconst {
496
497namespace detail {
498
499template <class T> T &make();
500template <class T, class Result> struct HasDereference {
501 using Yes = char[1];
502 using No = char[2];
503 template <size_t N> struct SFINAE {};
504
505 template <class U, class V>
506 static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
507 template <class U, class V> static No &hasDereference(...);
508
509 static const bool value =
510 sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
511};
512template <class V, class M> struct IsValidPointer {
513 static const bool value = std::is_base_of<Constant, V>::value &&
514 HasDereference<M, const Metadata &>::value;
515};
516template <class V, class M> struct IsValidReference {
517 static const bool value = std::is_base_of<Constant, V>::value &&
518 std::is_convertible<M, const Metadata &>::value;
519};
520
521} // end namespace detail
522
523/// \brief Check whether Metadata has a Value.
524///
525/// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
526/// type \c X.
527template <class X, class Y>
528inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, bool>::type
529hasa(Y &&MD) {
530 assert(MD && "Null pointer sent into hasa")(static_cast <bool> (MD && "Null pointer sent into hasa"
) ? void (0) : __assert_fail ("MD && \"Null pointer sent into hasa\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 530, __extension__ __PRETTY_FUNCTION__))
;
531 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
532 return isa<X>(V->getValue());
533 return false;
534}
535template <class X, class Y>
536inline
537 typename std::enable_if<detail::IsValidReference<X, Y &>::value, bool>::type
538 hasa(Y &MD) {
539 return hasa(&MD);
540}
541
542/// \brief Extract a Value from Metadata.
543///
544/// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
545template <class X, class Y>
546inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
547extract(Y &&MD) {
548 return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
549}
550template <class X, class Y>
551inline
552 typename std::enable_if<detail::IsValidReference<X, Y &>::value, X *>::type
553 extract(Y &MD) {
554 return extract(&MD);
555}
556
557/// \brief Extract a Value from Metadata, allowing null.
558///
559/// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
560/// from \c MD, allowing \c MD to be null.
561template <class X, class Y>
562inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
563extract_or_null(Y &&MD) {
564 if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
565 return cast<X>(V->getValue());
566 return nullptr;
567}
568
569/// \brief Extract a Value from Metadata, if any.
570///
571/// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
572/// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
573/// Value it does contain is of the wrong subclass.
574template <class X, class Y>
575inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
576dyn_extract(Y &&MD) {
577 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
578 return dyn_cast<X>(V->getValue());
579 return nullptr;
580}
581
582/// \brief Extract a Value from Metadata, if any, allowing null.
583///
584/// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
585/// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
586/// Value it does contain is of the wrong subclass, allowing \c MD to be null.
587template <class X, class Y>
588inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
589dyn_extract_or_null(Y &&MD) {
590 if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
591 return dyn_cast<X>(V->getValue());
592 return nullptr;
593}
594
595} // end namespace mdconst
596
597//===----------------------------------------------------------------------===//
598/// \brief A single uniqued string.
599///
600/// These are used to efficiently contain a byte sequence for metadata.
601/// MDString is always unnamed.
602class MDString : public Metadata {
603 friend class StringMapEntry<MDString>;
604
605 StringMapEntry<MDString> *Entry = nullptr;
606
607 MDString() : Metadata(MDStringKind, Uniqued) {}
608
609public:
610 MDString(const MDString &) = delete;
611 MDString &operator=(MDString &&) = delete;
612 MDString &operator=(const MDString &) = delete;
613
614 static MDString *get(LLVMContext &Context, StringRef Str);
615 static MDString *get(LLVMContext &Context, const char *Str) {
616 return get(Context, Str ? StringRef(Str) : StringRef());
617 }
618
619 StringRef getString() const;
620
621 unsigned getLength() const { return (unsigned)getString().size(); }
622
623 using iterator = StringRef::iterator;
624
625 /// \brief Pointer to the first byte of the string.
626 iterator begin() const { return getString().begin(); }
627
628 /// \brief Pointer to one byte past the end of the string.
629 iterator end() const { return getString().end(); }
630
631 const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
632 const unsigned char *bytes_end() const { return getString().bytes_end(); }
633
634 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
635 static bool classof(const Metadata *MD) {
636 return MD->getMetadataID() == MDStringKind;
637 }
638};
639
640/// \brief A collection of metadata nodes that might be associated with a
641/// memory access used by the alias-analysis infrastructure.
642struct AAMDNodes {
643 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
644 MDNode *N = nullptr)
645 : TBAA(T), Scope(S), NoAlias(N) {}
646
647 bool operator==(const AAMDNodes &A) const {
648 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
649 }
650
651 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
652
653 explicit operator bool() const { return TBAA || Scope || NoAlias; }
654
655 /// \brief The tag for type-based alias analysis.
656 MDNode *TBAA;
657
658 /// \brief The tag for alias scope specification (used with noalias).
659 MDNode *Scope;
660
661 /// \brief The tag specifying the noalias scope.
662 MDNode *NoAlias;
663
664 /// \brief Given two sets of AAMDNodes that apply to the same pointer,
665 /// give the best AAMDNodes that are compatible with both (i.e. a set of
666 /// nodes whose allowable aliasing conclusions are a subset of those
667 /// allowable by both of the inputs). However, for efficiency
668 /// reasons, do not create any new MDNodes.
669 AAMDNodes intersect(const AAMDNodes &Other) {
670 AAMDNodes Result;
671 Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
672 Result.Scope = Other.Scope == Scope ? Scope : nullptr;
673 Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
674 return Result;
675 }
676};
677
678// Specialize DenseMapInfo for AAMDNodes.
679template<>
680struct DenseMapInfo<AAMDNodes> {
681 static inline AAMDNodes getEmptyKey() {
682 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(),
683 nullptr, nullptr);
684 }
685
686 static inline AAMDNodes getTombstoneKey() {
687 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(),
688 nullptr, nullptr);
689 }
690
691 static unsigned getHashValue(const AAMDNodes &Val) {
692 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
693 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
694 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
695 }
696
697 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
698 return LHS == RHS;
699 }
700};
701
702/// \brief Tracking metadata reference owned by Metadata.
703///
704/// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
705/// of \a Metadata, which has the option of registering itself for callbacks to
706/// re-unique itself.
707///
708/// In particular, this is used by \a MDNode.
709class MDOperand {
710 Metadata *MD = nullptr;
711
712public:
713 MDOperand() = default;
714 MDOperand(MDOperand &&) = delete;
715 MDOperand(const MDOperand &) = delete;
716 MDOperand &operator=(MDOperand &&) = delete;
717 MDOperand &operator=(const MDOperand &) = delete;
718 ~MDOperand() { untrack(); }
719
720 Metadata *get() const { return MD; }
721 operator Metadata *() const { return get(); }
722 Metadata *operator->() const { return get(); }
723 Metadata &operator*() const { return *get(); }
724
725 void reset() {
726 untrack();
727 MD = nullptr;
728 }
729 void reset(Metadata *MD, Metadata *Owner) {
730 untrack();
731 this->MD = MD;
732 track(Owner);
733 }
734
735private:
736 void track(Metadata *Owner) {
737 if (MD) {
738 if (Owner)
739 MetadataTracking::track(this, *MD, *Owner);
740 else
741 MetadataTracking::track(MD);
742 }
743 }
744
745 void untrack() {
746 assert(static_cast<void *>(this) == &MD && "Expected same address")(static_cast <bool> (static_cast<void *>(this) ==
&MD && "Expected same address") ? void (0) : __assert_fail
("static_cast<void *>(this) == &MD && \"Expected same address\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 746, __extension__ __PRETTY_FUNCTION__))
;
747 if (MD)
748 MetadataTracking::untrack(MD);
749 }
750};
751
752template <> struct simplify_type<MDOperand> {
753 using SimpleType = Metadata *;
754
755 static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
756};
757
758template <> struct simplify_type<const MDOperand> {
759 using SimpleType = Metadata *;
760
761 static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
762};
763
764/// \brief Pointer to the context, with optional RAUW support.
765///
766/// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
767/// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
768class ContextAndReplaceableUses {
769 PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr;
770
771public:
772 ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
773 ContextAndReplaceableUses(
774 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
775 : Ptr(ReplaceableUses.release()) {
776 assert(getReplaceableUses() && "Expected non-null replaceable uses")(static_cast <bool> (getReplaceableUses() && "Expected non-null replaceable uses"
) ? void (0) : __assert_fail ("getReplaceableUses() && \"Expected non-null replaceable uses\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 776, __extension__ __PRETTY_FUNCTION__))
;
777 }
778 ContextAndReplaceableUses() = delete;
779 ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete;
780 ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete;
781 ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete;
782 ContextAndReplaceableUses &
783 operator=(const ContextAndReplaceableUses &) = delete;
784 ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
785
786 operator LLVMContext &() { return getContext(); }
787
788 /// \brief Whether this contains RAUW support.
789 bool hasReplaceableUses() const {
790 return Ptr.is<ReplaceableMetadataImpl *>();
791 }
792
793 LLVMContext &getContext() const {
794 if (hasReplaceableUses())
795 return getReplaceableUses()->getContext();
796 return *Ptr.get<LLVMContext *>();
797 }
798
799 ReplaceableMetadataImpl *getReplaceableUses() const {
800 if (hasReplaceableUses())
801 return Ptr.get<ReplaceableMetadataImpl *>();
802 return nullptr;
803 }
804
805 /// Ensure that this has RAUW support, and then return it.
806 ReplaceableMetadataImpl *getOrCreateReplaceableUses() {
807 if (!hasReplaceableUses())
808 makeReplaceable(llvm::make_unique<ReplaceableMetadataImpl>(getContext()));
809 return getReplaceableUses();
810 }
811
812 /// \brief Assign RAUW support to this.
813 ///
814 /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
815 /// not be null).
816 void
817 makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
818 assert(ReplaceableUses && "Expected non-null replaceable uses")(static_cast <bool> (ReplaceableUses && "Expected non-null replaceable uses"
) ? void (0) : __assert_fail ("ReplaceableUses && \"Expected non-null replaceable uses\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 818, __extension__ __PRETTY_FUNCTION__))
;
819 assert(&ReplaceableUses->getContext() == &getContext() &&(static_cast <bool> (&ReplaceableUses->getContext
() == &getContext() && "Expected same context") ?
void (0) : __assert_fail ("&ReplaceableUses->getContext() == &getContext() && \"Expected same context\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 820, __extension__ __PRETTY_FUNCTION__))
820 "Expected same context")(static_cast <bool> (&ReplaceableUses->getContext
() == &getContext() && "Expected same context") ?
void (0) : __assert_fail ("&ReplaceableUses->getContext() == &getContext() && \"Expected same context\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 820, __extension__ __PRETTY_FUNCTION__))
;
821 delete getReplaceableUses();
822 Ptr = ReplaceableUses.release();
823 }
824
825 /// \brief Drop RAUW support.
826 ///
827 /// Cede ownership of RAUW support, returning it.
828 std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
829 assert(hasReplaceableUses() && "Expected to own replaceable uses")(static_cast <bool> (hasReplaceableUses() && "Expected to own replaceable uses"
) ? void (0) : __assert_fail ("hasReplaceableUses() && \"Expected to own replaceable uses\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 829, __extension__ __PRETTY_FUNCTION__))
;
830 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
831 getReplaceableUses());
832 Ptr = &ReplaceableUses->getContext();
833 return ReplaceableUses;
834 }
835};
836
837struct TempMDNodeDeleter {
838 inline void operator()(MDNode *Node) const;
839};
840
841#define HANDLE_MDNODE_LEAF(CLASS) \
842 using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>;
843#define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
844#include "llvm/IR/Metadata.def"
845
846/// \brief Metadata node.
847///
848/// Metadata nodes can be uniqued, like constants, or distinct. Temporary
849/// metadata nodes (with full support for RAUW) can be used to delay uniquing
850/// until forward references are known. The basic metadata node is an \a
851/// MDTuple.
852///
853/// There is limited support for RAUW at construction time. At construction
854/// time, if any operand is a temporary node (or an unresolved uniqued node,
855/// which indicates a transitive temporary operand), the node itself will be
856/// unresolved. As soon as all operands become resolved, it will drop RAUW
857/// support permanently.
858///
859/// If an unresolved node is part of a cycle, \a resolveCycles() needs
860/// to be called on some member of the cycle once all temporary nodes have been
861/// replaced.
862class MDNode : public Metadata {
863 friend class ReplaceableMetadataImpl;
864 friend class LLVMContextImpl;
865
866 unsigned NumOperands;
867 unsigned NumUnresolved;
868
869 ContextAndReplaceableUses Context;
870
871protected:
872 MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
873 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None);
874 ~MDNode() = default;
875
876 void *operator new(size_t Size, unsigned NumOps);
877 void operator delete(void *Mem);
878
879 /// \brief Required by std, but never called.
880 void operator delete(void *, unsigned) {
881 llvm_unreachable("Constructor throws?")::llvm::llvm_unreachable_internal("Constructor throws?", "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 881)
;
882 }
883
884 /// \brief Required by std, but never called.
885 void operator delete(void *, unsigned, bool) {
886 llvm_unreachable("Constructor throws?")::llvm::llvm_unreachable_internal("Constructor throws?", "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 886)
;
887 }
888
889 void dropAllReferences();
890
891 MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
892 MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
893
894 using mutable_op_range = iterator_range<MDOperand *>;
895
896 mutable_op_range mutable_operands() {
897 return mutable_op_range(mutable_begin(), mutable_end());
898 }
899
900public:
901 MDNode(const MDNode &) = delete;
902 void operator=(const MDNode &) = delete;
903 void *operator new(size_t) = delete;
904
905 static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
906 static inline MDTuple *getIfExists(LLVMContext &Context,
907 ArrayRef<Metadata *> MDs);
908 static inline MDTuple *getDistinct(LLVMContext &Context,
909 ArrayRef<Metadata *> MDs);
910 static inline TempMDTuple getTemporary(LLVMContext &Context,
911 ArrayRef<Metadata *> MDs);
912
913 /// \brief Create a (temporary) clone of this.
914 TempMDNode clone() const;
915
916 /// \brief Deallocate a node created by getTemporary.
917 ///
918 /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
919 /// references will be reset.
920 static void deleteTemporary(MDNode *N);
921
922 LLVMContext &getContext() const { return Context.getContext(); }
923
924 /// \brief Replace a specific operand.
925 void replaceOperandWith(unsigned I, Metadata *New);
926
927 /// \brief Check if node is fully resolved.
928 ///
929 /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
930 /// this always returns \c true.
931 ///
932 /// If \a isUniqued(), returns \c true if this has already dropped RAUW
933 /// support (because all operands are resolved).
934 ///
935 /// As forward declarations are resolved, their containers should get
936 /// resolved automatically. However, if this (or one of its operands) is
937 /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
938 bool isResolved() const { return !isTemporary() && !NumUnresolved; }
939
940 bool isUniqued() const { return Storage == Uniqued; }
941 bool isDistinct() const { return Storage == Distinct; }
942 bool isTemporary() const { return Storage == Temporary; }
943
944 /// \brief RAUW a temporary.
945 ///
946 /// \pre \a isTemporary() must be \c true.
947 void replaceAllUsesWith(Metadata *MD) {
948 assert(isTemporary() && "Expected temporary node")(static_cast <bool> (isTemporary() && "Expected temporary node"
) ? void (0) : __assert_fail ("isTemporary() && \"Expected temporary node\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 948, __extension__ __PRETTY_FUNCTION__))
;
949 if (Context.hasReplaceableUses())
950 Context.getReplaceableUses()->replaceAllUsesWith(MD);
951 }
952
953 /// \brief Resolve cycles.
954 ///
955 /// Once all forward declarations have been resolved, force cycles to be
956 /// resolved.
957 ///
958 /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
959 void resolveCycles();
960
961 /// Resolve a unique, unresolved node.
962 void resolve();
963
964 /// \brief Replace a temporary node with a permanent one.
965 ///
966 /// Try to create a uniqued version of \c N -- in place, if possible -- and
967 /// return it. If \c N cannot be uniqued, return a distinct node instead.
968 template <class T>
969 static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
970 replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
971 return cast<T>(N.release()->replaceWithPermanentImpl());
972 }
973
974 /// \brief Replace a temporary node with a uniqued one.
975 ///
976 /// Create a uniqued version of \c N -- in place, if possible -- and return
977 /// it. Takes ownership of the temporary node.
978 ///
979 /// \pre N does not self-reference.
980 template <class T>
981 static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
982 replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
983 return cast<T>(N.release()->replaceWithUniquedImpl());
984 }
985
986 /// \brief Replace a temporary node with a distinct one.
987 ///
988 /// Create a distinct version of \c N -- in place, if possible -- and return
989 /// it. Takes ownership of the temporary node.
990 template <class T>
991 static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
992 replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
993 return cast<T>(N.release()->replaceWithDistinctImpl());
994 }
995
996private:
997 MDNode *replaceWithPermanentImpl();
998 MDNode *replaceWithUniquedImpl();
999 MDNode *replaceWithDistinctImpl();
1000
1001protected:
1002 /// \brief Set an operand.
1003 ///
1004 /// Sets the operand directly, without worrying about uniquing.
1005 void setOperand(unsigned I, Metadata *New);
1006
1007 void storeDistinctInContext();
1008 template <class T, class StoreT>
1009 static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
1010 template <class T> static T *storeImpl(T *N, StorageType Storage);
1011
1012private:
1013 void handleChangedOperand(void *Ref, Metadata *New);
1014
1015 /// Drop RAUW support, if any.
1016 void dropReplaceableUses();
1017
1018 void resolveAfterOperandChange(Metadata *Old, Metadata *New);
1019 void decrementUnresolvedOperandCount();
1020 void countUnresolvedOperands();
1021
1022 /// \brief Mutate this to be "uniqued".
1023 ///
1024 /// Mutate this so that \a isUniqued().
1025 /// \pre \a isTemporary().
1026 /// \pre already added to uniquing set.
1027 void makeUniqued();
1028
1029 /// \brief Mutate this to be "distinct".
1030 ///
1031 /// Mutate this so that \a isDistinct().
1032 /// \pre \a isTemporary().
1033 void makeDistinct();
1034
1035 void deleteAsSubclass();
1036 MDNode *uniquify();
1037 void eraseFromStore();
1038
1039 template <class NodeTy> struct HasCachedHash;
1040 template <class NodeTy>
1041 static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
1042 N->recalculateHash();
1043 }
1044 template <class NodeTy>
1045 static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
1046 template <class NodeTy>
1047 static void dispatchResetHash(NodeTy *N, std::true_type) {
1048 N->setHash(0);
1049 }
1050 template <class NodeTy>
1051 static void dispatchResetHash(NodeTy *, std::false_type) {}
1052
1053public:
1054 using op_iterator = const MDOperand *;
1055 using op_range = iterator_range<op_iterator>;
1056
1057 op_iterator op_begin() const {
1058 return const_cast<MDNode *>(this)->mutable_begin();
1059 }
1060
1061 op_iterator op_end() const {
1062 return const_cast<MDNode *>(this)->mutable_end();
1063 }
1064
1065 op_range operands() const { return op_range(op_begin(), op_end()); }
1066
1067 const MDOperand &getOperand(unsigned I) const {
1068 assert(I < NumOperands && "Out of range")(static_cast <bool> (I < NumOperands && "Out of range"
) ? void (0) : __assert_fail ("I < NumOperands && \"Out of range\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 1068, __extension__ __PRETTY_FUNCTION__))
;
1069 return op_begin()[I];
1070 }
1071
1072 /// \brief Return number of MDNode operands.
1073 unsigned getNumOperands() const { return NumOperands; }
1074
1075 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1076 static bool classof(const Metadata *MD) {
1077 switch (MD->getMetadataID()) {
1078 default:
1079 return false;
1080#define HANDLE_MDNODE_LEAF(CLASS) \
1081 case CLASS##Kind: \
1082 return true;
1083#include "llvm/IR/Metadata.def"
1084 }
1085 }
1086
1087 /// \brief Check whether MDNode is a vtable access.
1088 bool isTBAAVtableAccess() const;
1089
1090 /// \brief Methods for metadata merging.
1091 static MDNode *concatenate(MDNode *A, MDNode *B);
1092 static MDNode *intersect(MDNode *A, MDNode *B);
1093 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
1094 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
1095 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
1096 static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
1097 static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B);
1098};
1099
1100/// \brief Tuple of metadata.
1101///
1102/// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by
1103/// default based on their operands.
1104class MDTuple : public MDNode {
1105 friend class LLVMContextImpl;
1106 friend class MDNode;
1107
1108 MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
1109 ArrayRef<Metadata *> Vals)
1110 : MDNode(C, MDTupleKind, Storage, Vals) {
1111 setHash(Hash);
1112 }
1113
1114 ~MDTuple() { dropAllReferences(); }
1115
1116 void setHash(unsigned Hash) { SubclassData32 = Hash; }
1117 void recalculateHash();
1118
1119 static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
1120 StorageType Storage, bool ShouldCreate = true);
1121
1122 TempMDTuple cloneImpl() const {
1123 return getTemporary(getContext(),
1124 SmallVector<Metadata *, 4>(op_begin(), op_end()));
1125 }
1126
1127public:
1128 /// \brief Get the hash, if any.
1129 unsigned getHash() const { return SubclassData32; }
1130
1131 static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1132 return getImpl(Context, MDs, Uniqued);
2
Calling 'MDTuple::getImpl'
1133 }
1134
1135 static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1136 return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
1137 }
1138
1139 /// \brief Return a distinct node.
1140 ///
1141 /// Return a distinct node -- i.e., a node that is not uniqued.
1142 static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1143 return getImpl(Context, MDs, Distinct);
1144 }
1145
1146 /// \brief Return a temporary node.
1147 ///
1148 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
1149 /// not uniqued, may be RAUW'd, and must be manually deleted with
1150 /// deleteTemporary.
1151 static TempMDTuple getTemporary(LLVMContext &Context,
1152 ArrayRef<Metadata *> MDs) {
1153 return TempMDTuple(getImpl(Context, MDs, Temporary));
1154 }
1155
1156 /// \brief Return a (temporary) clone of this.
1157 TempMDTuple clone() const { return cloneImpl(); }
1158
1159 static bool classof(const Metadata *MD) {
1160 return MD->getMetadataID() == MDTupleKind;
1161 }
1162};
1163
1164MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1165 return MDTuple::get(Context, MDs);
1166}
1167
1168MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1169 return MDTuple::getIfExists(Context, MDs);
1170}
1171
1172MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1173 return MDTuple::getDistinct(Context, MDs);
1174}
1175
1176TempMDTuple MDNode::getTemporary(LLVMContext &Context,
1177 ArrayRef<Metadata *> MDs) {
1178 return MDTuple::getTemporary(Context, MDs);
1179}
1180
1181void TempMDNodeDeleter::operator()(MDNode *Node) const {
1182 MDNode::deleteTemporary(Node);
1183}
1184
1185/// \brief Typed iterator through MDNode operands.
1186///
1187/// An iterator that transforms an \a MDNode::iterator into an iterator over a
1188/// particular Metadata subclass.
1189template <class T>
1190class TypedMDOperandIterator
1191 : public std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void,
1192 T *> {
1193 MDNode::op_iterator I = nullptr;
1194
1195public:
1196 TypedMDOperandIterator() = default;
1197 explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {}
1198
1199 T *operator*() const { return cast_or_null<T>(*I); }
1200
1201 TypedMDOperandIterator &operator++() {
1202 ++I;
1203 return *this;
1204 }
1205
1206 TypedMDOperandIterator operator++(int) {
1207 TypedMDOperandIterator Temp(*this);
1208 ++I;
1209 return Temp;
1210 }
1211
1212 bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
1213 bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
1214};
1215
1216/// \brief Typed, array-like tuple of metadata.
1217///
1218/// This is a wrapper for \a MDTuple that makes it act like an array holding a
1219/// particular type of metadata.
1220template <class T> class MDTupleTypedArrayWrapper {
1221 const MDTuple *N = nullptr;
1222
1223public:
1224 MDTupleTypedArrayWrapper() = default;
1225 MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
1226
1227 template <class U>
1228 MDTupleTypedArrayWrapper(
1229 const MDTupleTypedArrayWrapper<U> &Other,
1230 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
1231 nullptr)
1232 : N(Other.get()) {}
1233
1234 template <class U>
1235 explicit MDTupleTypedArrayWrapper(
1236 const MDTupleTypedArrayWrapper<U> &Other,
1237 typename std::enable_if<!std::is_convertible<U *, T *>::value>::type * =
1238 nullptr)
1239 : N(Other.get()) {}
1240
1241 explicit operator bool() const { return get(); }
1242 explicit operator MDTuple *() const { return get(); }
1243
1244 MDTuple *get() const { return const_cast<MDTuple *>(N); }
1245 MDTuple *operator->() const { return get(); }
1246 MDTuple &operator*() const { return *get(); }
1247
1248 // FIXME: Fix callers and remove condition on N.
1249 unsigned size() const { return N ? N->getNumOperands() : 0u; }
1250 bool empty() const { return N ? N->getNumOperands() == 0 : true; }
1251 T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
1252
1253 // FIXME: Fix callers and remove condition on N.
1254 using iterator = TypedMDOperandIterator<T>;
1255
1256 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
1257 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
1258};
1259
1260#define HANDLE_METADATA(CLASS) \
1261 using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
1262#include "llvm/IR/Metadata.def"
1263
1264/// Placeholder metadata for operands of distinct MDNodes.
1265///
1266/// This is a lightweight placeholder for an operand of a distinct node. It's
1267/// purpose is to help track forward references when creating a distinct node.
1268/// This allows distinct nodes involved in a cycle to be constructed before
1269/// their operands without requiring a heavyweight temporary node with
1270/// full-blown RAUW support.
1271///
1272/// Each placeholder supports only a single MDNode user. Clients should pass
1273/// an ID, retrieved via \a getID(), to indicate the "real" operand that this
1274/// should be replaced with.
1275///
1276/// While it would be possible to implement move operators, they would be
1277/// fairly expensive. Leave them unimplemented to discourage their use
1278/// (clients can use std::deque, std::list, BumpPtrAllocator, etc.).
1279class DistinctMDOperandPlaceholder : public Metadata {
1280 friend class MetadataTracking;
1281
1282 Metadata **Use = nullptr;
1283
1284public:
1285 explicit DistinctMDOperandPlaceholder(unsigned ID)
1286 : Metadata(DistinctMDOperandPlaceholderKind, Distinct) {
1287 SubclassData32 = ID;
1288 }
1289
1290 DistinctMDOperandPlaceholder() = delete;
1291 DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&) = delete;
1292 DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &) = delete;
1293
1294 ~DistinctMDOperandPlaceholder() {
1295 if (Use)
1296 *Use = nullptr;
1297 }
1298
1299 unsigned getID() const { return SubclassData32; }
1300
1301 /// Replace the use of this with MD.
1302 void replaceUseWith(Metadata *MD) {
1303 if (!Use)
1304 return;
1305 *Use = MD;
1306
1307 if (*Use)
1308 MetadataTracking::track(*Use);
1309
1310 Metadata *T = cast<Metadata>(this);
1311 MetadataTracking::untrack(T);
1312 assert(!Use && "Use is still being tracked despite being untracked!")(static_cast <bool> (!Use && "Use is still being tracked despite being untracked!"
) ? void (0) : __assert_fail ("!Use && \"Use is still being tracked despite being untracked!\""
, "/build/llvm-toolchain-snapshot-7~svn325118/include/llvm/IR/Metadata.h"
, 1312, __extension__ __PRETTY_FUNCTION__))
;
1313 }
1314};
1315
1316//===----------------------------------------------------------------------===//
1317/// \brief A tuple of MDNodes.
1318///
1319/// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
1320/// to modules, have names, and contain lists of MDNodes.
1321///
1322/// TODO: Inherit from Metadata.
1323class NamedMDNode : public ilist_node<NamedMDNode> {
1324 friend class LLVMContextImpl;
1325 friend class Module;
1326
1327 std::string Name;
1328 Module *Parent = nullptr;
1329 void *Operands; // SmallVector<TrackingMDRef, 4>
1330
1331 void setParent(Module *M) { Parent = M; }
1332
1333 explicit NamedMDNode(const Twine &N);
1334
1335 template<class T1, class T2>
1336 class op_iterator_impl :
1337 public std::iterator<std::bidirectional_iterator_tag, T2> {
1338 friend class NamedMDNode;
1339
1340 const NamedMDNode *Node = nullptr;
1341 unsigned Idx = 0;
1342
1343 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {}
1344
1345 public:
1346 op_iterator_impl() = default;
1347
1348 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
1349 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
1350
1351 op_iterator_impl &operator++() {
1352 ++Idx;
1353 return *this;
1354 }
1355
1356 op_iterator_impl operator++(int) {
1357 op_iterator_impl tmp(*this);
1358 operator++();
1359 return tmp;
1360 }
1361
1362 op_iterator_impl &operator--() {
1363 --Idx;
1364 return *this;
1365 }
1366
1367 op_iterator_impl operator--(int) {
1368 op_iterator_impl tmp(*this);
1369 operator--();
1370 return tmp;
1371 }
1372
1373 T1 operator*() const { return Node->getOperand(Idx); }
1374 };
1375
1376public:
1377 NamedMDNode(const NamedMDNode &) = delete;
1378 ~NamedMDNode();
1379
1380 /// \brief Drop all references and remove the node from parent module.
1381 void eraseFromParent();
1382
1383 /// Remove all uses and clear node vector.
1384 void dropAllReferences() { clearOperands(); }
1385 /// Drop all references to this node's operands.
1386 void clearOperands();
1387
1388 /// \brief Get the module that holds this named metadata collection.
1389 inline Module *getParent() { return Parent; }
1390 inline const Module *getParent() const { return Parent; }
1391
1392 MDNode *getOperand(unsigned i) const;
1393 unsigned getNumOperands() const;
1394 void addOperand(MDNode *M);
1395 void setOperand(unsigned I, MDNode *New);
1396 StringRef getName() const;
1397 void print(raw_ostream &ROS, bool IsForDebug = false) const;
1398 void print(raw_ostream &ROS, ModuleSlotTracker &MST,
1399 bool IsForDebug = false) const;
1400 void dump() const;
1401
1402 // ---------------------------------------------------------------------------
1403 // Operand Iterator interface...
1404 //
1405 using op_iterator = op_iterator_impl<MDNode *, MDNode>;
1406
1407 op_iterator op_begin() { return op_iterator(this, 0); }
1408 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
1409
1410 using const_op_iterator = op_iterator_impl<const MDNode *, MDNode>;
1411
1412 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
1413 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
1414
1415 inline iterator_range<op_iterator> operands() {
1416 return make_range(op_begin(), op_end());
1417 }
1418 inline iterator_range<const_op_iterator> operands() const {
1419 return make_range(op_begin(), op_end());
1420 }
1421};
1422
1423} // end namespace llvm
1424
1425#endif // LLVM_IR_METADATA_H