Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/IR/Metadata.cpp
Warning:line 531, column 3
Potential leak of memory pointed to by 'Mem'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name Metadata.cpp -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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/IR -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/IR -I include -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-10-03-140002-15933-1 -x c++ /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/IR/Metadata.cpp

/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/IR/Metadata.cpp

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

/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/include/llvm/IR/Metadata.h

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