LLVM 19.0.0git
Metadata.cpp
Go to the documentation of this file.
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/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/IR/Argument.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/Constant.h"
32#include "llvm/IR/Constants.h"
34#include "llvm/IR/DebugLoc.h"
36#include "llvm/IR/Function.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"
45#include "llvm/IR/Type.h"
46#include "llvm/IR/Value.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
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.
82 Metadata *MD) {
83 if (!MD)
84 // !{}
85 return MDNode::get(Context, std::nullopt);
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, std::nullopt);
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
105 auto *&Entry = Context.pImpl->MetadataAsValues[MD];
106 if (!Entry)
108 return Entry;
109}
110
112 Metadata *MD) {
114 auto &Store = Context.pImpl->MetadataAsValues;
115 return Store.lookup(MD);
116}
117
118void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
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)
149}
150
151DPValue *DebugValueUser::getUser() { return static_cast<DPValue *>(this); }
153 return static_cast<const DPValue *>(this);
154}
155
157 // NOTE: We could inform the "owner" that a value has changed through
158 // getOwner, if needed.
159 auto OldMD = static_cast<Metadata **>(Old);
160 ptrdiff_t Idx = std::distance(&*DebugValues.begin(), OldMD);
161 // If replacing a ValueAsMetadata with a nullptr, replace it with a
162 // PoisonValue instead.
163 if (OldMD && isa<ValueAsMetadata>(*OldMD) && !New) {
164 auto *OldVAM = cast<ValueAsMetadata>(*OldMD);
165 New = ValueAsMetadata::get(PoisonValue::get(OldVAM->getValue()->getType()));
166 }
167 resetDebugValue(Idx, New);
168}
169
170void DebugValueUser::trackDebugValue(size_t Idx) {
171 assert(Idx < 3 && "Invalid debug value index.");
172 Metadata *&MD = DebugValues[Idx];
173 if (MD)
174 MetadataTracking::track(&MD, *MD, *this);
175}
176
177void DebugValueUser::trackDebugValues() {
178 for (Metadata *&MD : DebugValues)
179 if (MD)
180 MetadataTracking::track(&MD, *MD, *this);
181}
182
183void DebugValueUser::untrackDebugValue(size_t Idx) {
184 assert(Idx < 3 && "Invalid debug value index.");
185 Metadata *&MD = DebugValues[Idx];
186 if (MD)
188}
189
190void DebugValueUser::untrackDebugValues() {
191 for (Metadata *&MD : DebugValues)
192 if (MD)
194}
195
196void DebugValueUser::retrackDebugValues(DebugValueUser &X) {
197 assert(DebugValueUser::operator==(X) && "Expected values to match");
198 for (const auto &[MD, XMD] : zip(DebugValues, X.DebugValues))
199 if (XMD)
201 X.DebugValues.fill(nullptr);
202}
203
204bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
205 assert(Ref && "Expected live reference");
206 assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
207 "Reference without owner must be direct");
208 if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) {
209 R->addRef(Ref, Owner);
210 return true;
211 }
212 if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) {
213 assert(!PH->Use && "Placeholders can only be used once");
214 assert(!Owner && "Unexpected callback to owner");
215 PH->Use = static_cast<Metadata **>(Ref);
216 return true;
217 }
218 return false;
219}
220
222 assert(Ref && "Expected live reference");
223 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD))
224 R->dropRef(Ref);
225 else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD))
226 PH->Use = nullptr;
227}
228
229bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
230 assert(Ref && "Expected live reference");
231 assert(New && "Expected live reference");
232 assert(Ref != New && "Expected change");
233 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) {
234 R->moveRef(Ref, New, MD);
235 return true;
236 }
237 assert(!isa<DistinctMDOperandPlaceholder>(MD) &&
238 "Unexpected move of an MDOperand");
239 assert(!isReplaceable(MD) &&
240 "Expected un-replaceable metadata, since we didn't move a reference");
241 return false;
242}
243
245 return ReplaceableMetadataImpl::isReplaceable(MD);
246}
247
250 for (auto Pair : UseMap) {
251 OwnerTy Owner = Pair.second.first;
252 if (Owner.isNull())
253 continue;
254 if (!isa<Metadata *>(Owner))
255 continue;
256 Metadata *OwnerMD = cast<Metadata *>(Owner);
257 if (OwnerMD->getMetadataID() == Metadata::DIArgListKind)
258 MDUsersWithID.push_back(&UseMap[Pair.first]);
259 }
260 llvm::sort(MDUsersWithID, [](auto UserA, auto UserB) {
261 return UserA->second < UserB->second;
262 });
264 for (auto *UserWithID : MDUsersWithID)
265 MDUsers.push_back(cast<Metadata *>(UserWithID->first));
266 return MDUsers;
267}
268
271 for (auto Pair : UseMap) {
272 OwnerTy Owner = Pair.second.first;
273 if (Owner.isNull())
274 continue;
275 if (!Owner.is<DebugValueUser *>())
276 continue;
277 DPVUsersWithID.push_back(&UseMap[Pair.first]);
278 }
279 // Order DPValue users in reverse-creation order. Normal dbg.value users
280 // of MetadataAsValues are ordered by their UseList, i.e. reverse order of
281 // when they were added: we need to replicate that here. The structure of
282 // debug-info output depends on the ordering of intrinsics, thus we need
283 // to keep them consistent for comparisons sake.
284 llvm::sort(DPVUsersWithID, [](auto UserA, auto UserB) {
285 return UserA->second > UserB->second;
286 });
287 SmallVector<DPValue *> DPVUsers;
288 for (auto UserWithID : DPVUsersWithID)
289 DPVUsers.push_back(UserWithID->first.get<DebugValueUser *>()->getUser());
290 return DPVUsers;
291}
292
293void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
294 bool WasInserted =
295 UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
296 .second;
297 (void)WasInserted;
298 assert(WasInserted && "Expected to add a reference");
299
300 ++NextIndex;
301 assert(NextIndex != 0 && "Unexpected overflow");
302}
303
304void ReplaceableMetadataImpl::dropRef(void *Ref) {
305 bool WasErased = UseMap.erase(Ref);
306 (void)WasErased;
307 assert(WasErased && "Expected to drop a reference");
308}
309
310void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
311 const Metadata &MD) {
312 auto I = UseMap.find(Ref);
313 assert(I != UseMap.end() && "Expected to move a reference");
314 auto OwnerAndIndex = I->second;
315 UseMap.erase(I);
316 bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
317 (void)WasInserted;
318 assert(WasInserted && "Expected to add a reference");
319
320 // Check that the references are direct if there's no owner.
321 (void)MD;
322 assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
323 "Reference without owner must be direct");
324 assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
325 "Reference without owner must be direct");
326}
327
329 if (!C.isUsedByMetadata()) {
330 return;
331 }
332
333 LLVMContext &Context = C.getType()->getContext();
334 auto &Store = Context.pImpl->ValuesAsMetadata;
335 auto I = Store.find(&C);
336 ValueAsMetadata *MD = I->second;
337 using UseTy =
338 std::pair<void *, std::pair<MetadataTracking::OwnerTy, uint64_t>>;
339 // Copy out uses and update value of Constant used by debug info metadata with undef below
340 SmallVector<UseTy, 8> Uses(MD->UseMap.begin(), MD->UseMap.end());
341
342 for (const auto &Pair : Uses) {
343 MetadataTracking::OwnerTy Owner = Pair.second.first;
344 if (!Owner)
345 continue;
346 if (!isa<Metadata *>(Owner))
347 continue;
348 auto *OwnerMD = dyn_cast_if_present<MDNode>(cast<Metadata *>(Owner));
349 if (!OwnerMD)
350 continue;
351 if (isa<DINode>(OwnerMD)) {
352 OwnerMD->handleChangedOperand(
353 Pair.first, ValueAsMetadata::get(UndefValue::get(C.getType())));
354 }
355 }
356}
357
359 if (UseMap.empty())
360 return;
361
362 // Copy out uses since UseMap will get touched below.
363 using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
364 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
365 llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
366 return L.second.second < R.second.second;
367 });
368 for (const auto &Pair : Uses) {
369 // Check that this Ref hasn't disappeared after RAUW (when updating a
370 // previous Ref).
371 if (!UseMap.count(Pair.first))
372 continue;
373
374 OwnerTy Owner = Pair.second.first;
375 if (!Owner) {
376 // Update unowned tracking references directly.
377 Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
378 Ref = MD;
379 if (MD)
381 UseMap.erase(Pair.first);
382 continue;
383 }
384
385 // Check for MetadataAsValue.
386 if (isa<MetadataAsValue *>(Owner)) {
387 cast<MetadataAsValue *>(Owner)->handleChangedMetadata(MD);
388 continue;
389 }
390
391 if (Owner.is<DebugValueUser *>()) {
392 Owner.get<DebugValueUser *>()->handleChangedValue(Pair.first, MD);
393 continue;
394 }
395
396 // There's a Metadata owner -- dispatch.
397 Metadata *OwnerMD = cast<Metadata *>(Owner);
398 switch (OwnerMD->getMetadataID()) {
399#define HANDLE_METADATA_LEAF(CLASS) \
400 case Metadata::CLASS##Kind: \
401 cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
402 continue;
403#include "llvm/IR/Metadata.def"
404 default:
405 llvm_unreachable("Invalid metadata subclass");
406 }
407 }
408 assert(UseMap.empty() && "Expected all uses to be replaced");
409}
410
412 if (UseMap.empty())
413 return;
414
415 if (!ResolveUsers) {
416 UseMap.clear();
417 return;
418 }
419
420 // Copy out uses since UseMap could get touched below.
421 using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
422 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
423 llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
424 return L.second.second < R.second.second;
425 });
426 UseMap.clear();
427 for (const auto &Pair : Uses) {
428 auto Owner = Pair.second.first;
429 if (!Owner)
430 continue;
431 if (!Owner.is<Metadata *>())
432 continue;
433
434 // Resolve MDNodes that point at this.
435 auto *OwnerMD = dyn_cast_if_present<MDNode>(cast<Metadata *>(Owner));
436 if (!OwnerMD)
437 continue;
438 if (OwnerMD->isResolved())
439 continue;
440 OwnerMD->decrementUnresolvedOperandCount();
441 }
442}
443
444// Special handing of DIArgList is required in the RemoveDIs project, see
445// commentry in DIArgList::handleChangedOperand for details. Hidden behind
446// conditional compilation to avoid a compile time regression.
447ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
448 if (auto *N = dyn_cast<MDNode>(&MD)) {
449 return !N->isResolved() || N->isAlwaysReplaceable()
450 ? N->Context.getOrCreateReplaceableUses()
451 : nullptr;
452 }
453 if (auto ArgList = dyn_cast<DIArgList>(&MD))
454 return ArgList;
455 return dyn_cast<ValueAsMetadata>(&MD);
456}
457
458ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
459 if (auto *N = dyn_cast<MDNode>(&MD)) {
460 return !N->isResolved() || N->isAlwaysReplaceable()
461 ? N->Context.getReplaceableUses()
462 : nullptr;
463 }
464 if (auto ArgList = dyn_cast<DIArgList>(&MD))
465 return ArgList;
466 return dyn_cast<ValueAsMetadata>(&MD);
467}
468
469bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
470 if (auto *N = dyn_cast<MDNode>(&MD))
471 return !N->isResolved() || N->isAlwaysReplaceable();
472 return isa<ValueAsMetadata>(&MD) || isa<DIArgList>(&MD);
473}
474
476 assert(V && "Expected value");
477 if (auto *A = dyn_cast<Argument>(V)) {
478 if (auto *Fn = A->getParent())
479 return Fn->getSubprogram();
480 return nullptr;
481 }
482
483 if (BasicBlock *BB = cast<Instruction>(V)->getParent()) {
484 if (auto *Fn = BB->getParent())
485 return Fn->getSubprogram();
486 return nullptr;
487 }
488
489 return nullptr;
490}
491
493 assert(V && "Unexpected null Value");
494
495 auto &Context = V->getContext();
496 auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
497 if (!Entry) {
498 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
499 "Expected constant or function-local value");
500 assert(!V->IsUsedByMD && "Expected this to be the only metadata use");
501 V->IsUsedByMD = true;
502 if (auto *C = dyn_cast<Constant>(V))
503 Entry = new ConstantAsMetadata(C);
504 else
505 Entry = new LocalAsMetadata(V);
506 }
507
508 return Entry;
509}
510
512 assert(V && "Unexpected null Value");
513 return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
514}
515
517 assert(V && "Expected valid value");
518
519 auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
520 auto I = Store.find(V);
521 if (I == Store.end())
522 return;
523
524 // Remove old entry from the map.
525 ValueAsMetadata *MD = I->second;
526 assert(MD && "Expected valid metadata");
527 assert(MD->getValue() == V && "Expected valid mapping");
528 Store.erase(I);
529
530 // Delete the metadata.
531 MD->replaceAllUsesWith(nullptr);
532 delete MD;
533}
534
536 assert(From && "Expected valid value");
537 assert(To && "Expected valid value");
538 assert(From != To && "Expected changed value");
539 assert(&From->getContext() == &To->getContext() && "Expected same context");
540
541 LLVMContext &Context = From->getType()->getContext();
542 auto &Store = Context.pImpl->ValuesAsMetadata;
543 auto I = Store.find(From);
544 if (I == Store.end()) {
545 assert(!From->IsUsedByMD && "Expected From not to be used by metadata");
546 return;
547 }
548
549 // Remove old entry from the map.
550 assert(From->IsUsedByMD && "Expected From to be used by metadata");
551 From->IsUsedByMD = false;
552 ValueAsMetadata *MD = I->second;
553 assert(MD && "Expected valid metadata");
554 assert(MD->getValue() == From && "Expected valid mapping");
555 Store.erase(I);
556
557 if (isa<LocalAsMetadata>(MD)) {
558 if (auto *C = dyn_cast<Constant>(To)) {
559 // Local became a constant.
561 delete MD;
562 return;
563 }
566 // DISubprogram changed.
567 MD->replaceAllUsesWith(nullptr);
568 delete MD;
569 return;
571 } else if (!isa<Constant>(To)) {
572 // Changed to function-local value.
573 MD->replaceAllUsesWith(nullptr);
574 delete MD;
575 return;
576 }
578 auto *&Entry = Store[To];
579 if (Entry) {
580 // The target already exists.
581 MD->replaceAllUsesWith(Entry);
582 delete MD;
583 return;
584 }
585
586 // Update MD in place (and update the map entry).
587 assert(!To->IsUsedByMD && "Expected this to be the only metadata use");
588 To->IsUsedByMD = true;
589 MD->V = To;
590 Entry = MD;
591}
592
593//===----------------------------------------------------------------------===//
594// MDString implementation.
595//
596
598 auto &Store = Context.pImpl->MDStringCache;
599 auto I = Store.try_emplace(Str);
600 auto &MapEntry = I.first->getValue();
601 if (!I.second)
602 return &MapEntry;
603 MapEntry.Entry = &*I.first;
604 return &MapEntry;
605}
608 assert(Entry && "Expected to find string map entry");
609 return Entry->first();
610}
611
612//===----------------------------------------------------------------------===//
613// MDNode implementation.
614//
615
616// Assert that the MDNode types will not be unaligned by the objects
617// prepended to them.
618#define HANDLE_MDNODE_LEAF(CLASS) \
619 static_assert( \
620 alignof(uint64_t) >= alignof(CLASS), \
621 "Alignment is insufficient after objects prepended to " #CLASS);
622#include "llvm/IR/Metadata.def"
623
624void *MDNode::operator new(size_t Size, size_t NumOps, StorageType Storage) {
625 // uint64_t is the most aligned type we need support (ensured by static_assert
626 // above)
627 size_t AllocSize =
628 alignTo(Header::getAllocSize(Storage, NumOps), alignof(uint64_t));
629 char *Mem = reinterpret_cast<char *>(::operator new(AllocSize + Size));
630 Header *H = new (Mem + AllocSize - sizeof(Header)) Header(NumOps, Storage);
631 return reinterpret_cast<void *>(H + 1);
632}
633
634void MDNode::operator delete(void *N) {
635 Header *H = reinterpret_cast<Header *>(N) - 1;
636 void *Mem = H->getAllocation();
637 H->~Header();
638 ::operator delete(Mem);
639}
640
641MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
643 : Metadata(ID, Storage), Context(Context) {
644 unsigned Op = 0;
645 for (Metadata *MD : Ops1)
646 setOperand(Op++, MD);
647 for (Metadata *MD : Ops2)
648 setOperand(Op++, MD);
649
650 if (!isUniqued())
651 return;
652
653 // Count the unresolved operands. If there are any, RAUW support will be
654 // added lazily on first reference.
655 countUnresolvedOperands();
656}
657
658TempMDNode MDNode::clone() const {
659 switch (getMetadataID()) {
660 default:
661 llvm_unreachable("Invalid MDNode subclass");
662#define HANDLE_MDNODE_LEAF(CLASS) \
663 case CLASS##Kind: \
664 return cast<CLASS>(this)->cloneImpl();
665#include "llvm/IR/Metadata.def"
666 }
667}
668
669MDNode::Header::Header(size_t NumOps, StorageType Storage) {
670 IsLarge = isLarge(NumOps);
671 IsResizable = isResizable(Storage);
672 SmallSize = getSmallSize(NumOps, IsResizable, IsLarge);
673 if (IsLarge) {
674 SmallNumOps = 0;
675 new (getLargePtr()) LargeStorageVector();
676 getLarge().resize(NumOps);
677 return;
678 }
679 SmallNumOps = NumOps;
680 MDOperand *O = reinterpret_cast<MDOperand *>(this) - SmallSize;
681 for (MDOperand *E = O + SmallSize; O != E;)
682 (void)new (O++) MDOperand();
683}
684
685MDNode::Header::~Header() {
686 if (IsLarge) {
687 getLarge().~LargeStorageVector();
688 return;
689 }
690 MDOperand *O = reinterpret_cast<MDOperand *>(this);
691 for (MDOperand *E = O - SmallSize; O != E; --O)
692 (void)(O - 1)->~MDOperand();
693}
694
695void *MDNode::Header::getSmallPtr() {
696 static_assert(alignof(MDOperand) <= alignof(Header),
697 "MDOperand too strongly aligned");
698 return reinterpret_cast<char *>(const_cast<Header *>(this)) -
699 sizeof(MDOperand) * SmallSize;
700}
701
702void MDNode::Header::resize(size_t NumOps) {
703 assert(IsResizable && "Node is not resizable");
704 if (operands().size() == NumOps)
705 return;
706
707 if (IsLarge)
708 getLarge().resize(NumOps);
709 else if (NumOps <= SmallSize)
710 resizeSmall(NumOps);
711 else
712 resizeSmallToLarge(NumOps);
713}
714
715void MDNode::Header::resizeSmall(size_t NumOps) {
716 assert(!IsLarge && "Expected a small MDNode");
717 assert(NumOps <= SmallSize && "NumOps too large for small resize");
718
719 MutableArrayRef<MDOperand> ExistingOps = operands();
720 assert(NumOps != ExistingOps.size() && "Expected a different size");
721
722 int NumNew = (int)NumOps - (int)ExistingOps.size();
723 MDOperand *O = ExistingOps.end();
724 for (int I = 0, E = NumNew; I < E; ++I)
725 (O++)->reset();
726 for (int I = 0, E = NumNew; I > E; --I)
727 (--O)->reset();
728 SmallNumOps = NumOps;
729 assert(O == operands().end() && "Operands not (un)initialized until the end");
730}
731
732void MDNode::Header::resizeSmallToLarge(size_t NumOps) {
733 assert(!IsLarge && "Expected a small MDNode");
734 assert(NumOps > SmallSize && "Expected NumOps to be larger than allocation");
735 LargeStorageVector NewOps;
736 NewOps.resize(NumOps);
737 llvm::move(operands(), NewOps.begin());
738 resizeSmall(0);
739 new (getLargePtr()) LargeStorageVector(std::move(NewOps));
740 IsLarge = true;
741}
742
744 if (auto *N = dyn_cast_or_null<MDNode>(Op))
745 return !N->isResolved();
746 return false;
747}
748
749void MDNode::countUnresolvedOperands() {
750 assert(getNumUnresolved() == 0 && "Expected unresolved ops to be uncounted");
751 assert(isUniqued() && "Expected this to be uniqued");
753}
754
755void MDNode::makeUniqued() {
756 assert(isTemporary() && "Expected this to be temporary");
757 assert(!isResolved() && "Expected this to be unresolved");
758
759 // Enable uniquing callbacks.
760 for (auto &Op : mutable_operands())
761 Op.reset(Op.get(), this);
762
763 // Make this 'uniqued'.
765 countUnresolvedOperands();
766 if (!getNumUnresolved()) {
767 dropReplaceableUses();
768 assert(isResolved() && "Expected this to be resolved");
769 }
770
771 assert(isUniqued() && "Expected this to be uniqued");
772}
773
774void MDNode::makeDistinct() {
775 assert(isTemporary() && "Expected this to be temporary");
776 assert(!isResolved() && "Expected this to be unresolved");
777
778 // Drop RAUW support and store as a distinct node.
779 dropReplaceableUses();
781
782 assert(isDistinct() && "Expected this to be distinct");
783 assert(isResolved() && "Expected this to be resolved");
784}
785
787 assert(isUniqued() && "Expected this to be uniqued");
788 assert(!isResolved() && "Expected this to be unresolved");
789
791 dropReplaceableUses();
792
793 assert(isResolved() && "Expected this to be resolved");
794}
795
796void MDNode::dropReplaceableUses() {
797 assert(!getNumUnresolved() && "Unexpected unresolved operand");
798
799 // Drop any RAUW support.
800 if (Context.hasReplaceableUses())
801 Context.takeReplaceableUses()->resolveAllUses();
802}
803
804void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
805 assert(isUniqued() && "Expected this to be uniqued");
806 assert(getNumUnresolved() != 0 && "Expected unresolved operands");
807
808 // Check if an operand was resolved.
809 if (!isOperandUnresolved(Old)) {
810 if (isOperandUnresolved(New))
811 // An operand was un-resolved!
813 } else if (!isOperandUnresolved(New))
814 decrementUnresolvedOperandCount();
815}
816
817void MDNode::decrementUnresolvedOperandCount() {
818 assert(!isResolved() && "Expected this to be unresolved");
819 if (isTemporary())
820 return;
821
822 assert(isUniqued() && "Expected this to be uniqued");
824 if (getNumUnresolved())
825 return;
826
827 // Last unresolved operand has just been resolved.
828 dropReplaceableUses();
829 assert(isResolved() && "Expected this to become resolved");
830}
831
833 if (isResolved())
834 return;
835
836 // Resolve this node immediately.
837 resolve();
838
839 // Resolve all operands.
840 for (const auto &Op : operands()) {
841 auto *N = dyn_cast_or_null<MDNode>(Op);
842 if (!N)
843 continue;
844
845 assert(!N->isTemporary() &&
846 "Expected all forward declarations to be resolved");
847 if (!N->isResolved())
848 N->resolveCycles();
849 }
850}
851
852static bool hasSelfReference(MDNode *N) {
853 return llvm::is_contained(N->operands(), N);
854}
855
856MDNode *MDNode::replaceWithPermanentImpl() {
857 switch (getMetadataID()) {
858 default:
859 // If this type isn't uniquable, replace with a distinct node.
860 return replaceWithDistinctImpl();
861
862#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
863 case CLASS##Kind: \
864 break;
865#include "llvm/IR/Metadata.def"
866 }
867
868 // Even if this type is uniquable, self-references have to be distinct.
869 if (hasSelfReference(this))
870 return replaceWithDistinctImpl();
871 return replaceWithUniquedImpl();
872}
873
874MDNode *MDNode::replaceWithUniquedImpl() {
875 // Try to uniquify in place.
876 MDNode *UniquedNode = uniquify();
877
878 if (UniquedNode == this) {
879 makeUniqued();
880 return this;
881 }
882
883 // Collision, so RAUW instead.
884 replaceAllUsesWith(UniquedNode);
885 deleteAsSubclass();
886 return UniquedNode;
887}
888
889MDNode *MDNode::replaceWithDistinctImpl() {
890 makeDistinct();
891 return this;
892}
893
894void MDTuple::recalculateHash() {
895 setHash(MDTupleInfo::KeyTy::calculateHash(this));
896}
897
899 for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
900 setOperand(I, nullptr);
901 if (Context.hasReplaceableUses()) {
902 Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
903 (void)Context.takeReplaceableUses();
904 }
905}
906
907void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
908 unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
909 assert(Op < getNumOperands() && "Expected valid operand");
910
911 if (!isUniqued()) {
912 // This node is not uniqued. Just set the operand and be done with it.
913 setOperand(Op, New);
914 return;
915 }
916
917 // This node is uniqued.
918 eraseFromStore();
919
920 Metadata *Old = getOperand(Op);
921 setOperand(Op, New);
922
923 // Drop uniquing for self-reference cycles and deleted constants.
924 if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
925 if (!isResolved())
926 resolve();
928 return;
929 }
930
931 // Re-unique the node.
932 auto *Uniqued = uniquify();
933 if (Uniqued == this) {
934 if (!isResolved())
935 resolveAfterOperandChange(Old, New);
936 return;
937 }
938
939 // Collision.
940 if (!isResolved()) {
941 // Still unresolved, so RAUW.
942 //
943 // First, clear out all operands to prevent any recursion (similar to
944 // dropAllReferences(), but we still need the use-list).
945 for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
946 setOperand(O, nullptr);
947 if (Context.hasReplaceableUses())
948 Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
949 deleteAsSubclass();
950 return;
951 }
952
953 // Store in non-uniqued form if RAUW isn't possible.
955}
956
957void MDNode::deleteAsSubclass() {
958 switch (getMetadataID()) {
959 default:
960 llvm_unreachable("Invalid subclass of MDNode");
961#define HANDLE_MDNODE_LEAF(CLASS) \
962 case CLASS##Kind: \
963 delete cast<CLASS>(this); \
964 break;
965#include "llvm/IR/Metadata.def"
966 }
967}
968
969template <class T, class InfoT>
971 if (T *U = getUniqued(Store, N))
972 return U;
973
974 Store.insert(N);
975 return N;
976}
977
978template <class NodeTy> struct MDNode::HasCachedHash {
979 using Yes = char[1];
980 using No = char[2];
981 template <class U, U Val> struct SFINAE {};
982
983 template <class U>
984 static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
985 template <class U> static No &check(...);
986
987 static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
988};
989
990MDNode *MDNode::uniquify() {
991 assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
992
993 // Try to insert into uniquing store.
994 switch (getMetadataID()) {
995 default:
996 llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
997#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
998 case CLASS##Kind: { \
999 CLASS *SubclassThis = cast<CLASS>(this); \
1000 std::integral_constant<bool, HasCachedHash<CLASS>::value> \
1001 ShouldRecalculateHash; \
1002 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
1003 return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
1004 }
1005#include "llvm/IR/Metadata.def"
1006 }
1007}
1008
1009void MDNode::eraseFromStore() {
1010 switch (getMetadataID()) {
1011 default:
1012 llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
1013#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
1014 case CLASS##Kind: \
1015 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
1016 break;
1017#include "llvm/IR/Metadata.def"
1018 }
1019}
1020
1021MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
1022 StorageType Storage, bool ShouldCreate) {
1023 unsigned Hash = 0;
1024 if (Storage == Uniqued) {
1025 MDTupleInfo::KeyTy Key(MDs);
1026 if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
1027 return N;
1028 if (!ShouldCreate)
1029 return nullptr;
1030 Hash = Key.getHash();
1031 } else {
1032 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
1033 }
1034
1035 return storeImpl(new (MDs.size(), Storage)
1036 MDTuple(Context, Storage, Hash, MDs),
1037 Storage, Context.pImpl->MDTuples);
1038}
1039
1041 assert(N->isTemporary() && "Expected temporary node");
1042 N->replaceAllUsesWith(nullptr);
1043 N->deleteAsSubclass();
1044}
1045
1047 assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses");
1048 assert(!getNumUnresolved() && "Unexpected unresolved nodes");
1049 Storage = Distinct;
1050 assert(isResolved() && "Expected this to be resolved");
1051
1052 // Reset the hash.
1053 switch (getMetadataID()) {
1054 default:
1055 llvm_unreachable("Invalid subclass of MDNode");
1056#define HANDLE_MDNODE_LEAF(CLASS) \
1057 case CLASS##Kind: { \
1058 std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
1059 dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
1060 break; \
1061 }
1062#include "llvm/IR/Metadata.def"
1063 }
1064
1065 getContext().pImpl->DistinctMDNodes.push_back(this);
1066}
1067
1069 if (getOperand(I) == New)
1070 return;
1071
1072 if (!isUniqued()) {
1073 setOperand(I, New);
1074 return;
1075 }
1076
1077 handleChangedOperand(mutable_begin() + I, New);
1078}
1079
1080void MDNode::setOperand(unsigned I, Metadata *New) {
1081 assert(I < getNumOperands());
1082 mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
1083}
1084
1085/// Get a node or a self-reference that looks like it.
1086///
1087/// Special handling for finding self-references, for use by \a
1088/// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
1089/// when self-referencing nodes were still uniqued. If the first operand has
1090/// the same operands as \c Ops, return the first operand instead.
1093 if (!Ops.empty())
1094 if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
1095 if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
1096 for (unsigned I = 1, E = Ops.size(); I != E; ++I)
1097 if (Ops[I] != N->getOperand(I))
1098 return MDNode::get(Context, Ops);
1099 return N;
1100 }
1101
1102 return MDNode::get(Context, Ops);
1103}
1104
1106 if (!A)
1107 return B;
1108 if (!B)
1109 return A;
1110
1111 SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
1112 MDs.insert(B->op_begin(), B->op_end());
1113
1114 // FIXME: This preserves long-standing behaviour, but is it really the right
1115 // behaviour? Or was that an unintended side-effect of node uniquing?
1116 return getOrSelfReference(A->getContext(), MDs.getArrayRef());
1117}
1118
1120 if (!A || !B)
1121 return nullptr;
1122
1123 SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
1124 SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
1125 MDs.remove_if([&](Metadata *MD) { return !BSet.count(MD); });
1126
1127 // FIXME: This preserves long-standing behaviour, but is it really the right
1128 // behaviour? Or was that an unintended side-effect of node uniquing?
1129 return getOrSelfReference(A->getContext(), MDs.getArrayRef());
1130}
1131
1133 if (!A || !B)
1134 return nullptr;
1135
1136 // Take the intersection of domains then union the scopes
1137 // within those domains
1139 SmallPtrSet<const MDNode *, 16> IntersectDomains;
1141 for (const MDOperand &MDOp : A->operands())
1142 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
1143 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
1144 ADomains.insert(Domain);
1145
1146 for (const MDOperand &MDOp : B->operands())
1147 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
1148 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
1149 if (ADomains.contains(Domain)) {
1150 IntersectDomains.insert(Domain);
1151 MDs.insert(MDOp);
1152 }
1153
1154 for (const MDOperand &MDOp : A->operands())
1155 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
1156 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
1157 if (IntersectDomains.contains(Domain))
1158 MDs.insert(MDOp);
1159
1160 return MDs.empty() ? nullptr
1161 : getOrSelfReference(A->getContext(), MDs.getArrayRef());
1162}
1163
1165 if (!A || !B)
1166 return nullptr;
1167
1168 APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
1169 APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
1170 if (AVal < BVal)
1171 return A;
1172 return B;
1173}
1174
1175// Call instructions with branch weights are only used in SamplePGO as
1176// documented in
1177/// https://llvm.org/docs/BranchWeightMetadata.html#callinst).
1178MDNode *MDNode::mergeDirectCallProfMetadata(MDNode *A, MDNode *B,
1179 const Instruction *AInstr,
1180 const Instruction *BInstr) {
1181 assert(A && B && AInstr && BInstr && "Caller should guarantee");
1182 auto &Ctx = AInstr->getContext();
1183 MDBuilder MDHelper(Ctx);
1184
1185 // LLVM IR verifier verifies !prof metadata has at least 2 operands.
1186 assert(A->getNumOperands() >= 2 && B->getNumOperands() >= 2 &&
1187 "!prof annotations should have no less than 2 operands");
1188 MDString *AMDS = dyn_cast<MDString>(A->getOperand(0));
1189 MDString *BMDS = dyn_cast<MDString>(B->getOperand(0));
1190 // LLVM IR verfier verifies first operand is MDString.
1191 assert(AMDS != nullptr && BMDS != nullptr &&
1192 "first operand should be a non-null MDString");
1193 StringRef AProfName = AMDS->getString();
1194 StringRef BProfName = BMDS->getString();
1195 if (AProfName.equals("branch_weights") &&
1196 BProfName.equals("branch_weights")) {
1197 ConstantInt *AInstrWeight =
1198 mdconst::dyn_extract<ConstantInt>(A->getOperand(1));
1199 ConstantInt *BInstrWeight =
1200 mdconst::dyn_extract<ConstantInt>(B->getOperand(1));
1201 assert(AInstrWeight && BInstrWeight && "verified by LLVM verifier");
1202 return MDNode::get(Ctx,
1203 {MDHelper.createString("branch_weights"),
1204 MDHelper.createConstant(ConstantInt::get(
1205 Type::getInt64Ty(Ctx),
1206 SaturatingAdd(AInstrWeight->getZExtValue(),
1207 BInstrWeight->getZExtValue())))});
1208 }
1209 return nullptr;
1210}
1211
1212// Pass in both instructions and nodes. Instruction information (e.g.,
1213// instruction type) helps interpret profiles and make implementation clearer.
1215 const Instruction *AInstr,
1216 const Instruction *BInstr) {
1217 if (!(A && B)) {
1218 return A ? A : B;
1219 }
1220
1221 assert(AInstr->getMetadata(LLVMContext::MD_prof) == A &&
1222 "Caller should guarantee");
1223 assert(BInstr->getMetadata(LLVMContext::MD_prof) == B &&
1224 "Caller should guarantee");
1225
1226 const CallInst *ACall = dyn_cast<CallInst>(AInstr);
1227 const CallInst *BCall = dyn_cast<CallInst>(BInstr);
1228
1229 // Both ACall and BCall are direct callsites.
1230 if (ACall && BCall && ACall->getCalledFunction() &&
1231 BCall->getCalledFunction())
1232 return mergeDirectCallProfMetadata(A, B, AInstr, BInstr);
1233
1234 // The rest of the cases are not implemented but could be added
1235 // when there are use cases.
1236 return nullptr;
1237}
1238
1239static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1240 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1241}
1242
1243static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
1244 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
1245}
1246
1249 ConstantRange NewRange(Low->getValue(), High->getValue());
1250 unsigned Size = EndPoints.size();
1251 APInt LB = EndPoints[Size - 2]->getValue();
1252 APInt LE = EndPoints[Size - 1]->getValue();
1253 ConstantRange LastRange(LB, LE);
1254 if (canBeMerged(NewRange, LastRange)) {
1255 ConstantRange Union = LastRange.unionWith(NewRange);
1256 Type *Ty = High->getType();
1257 EndPoints[Size - 2] =
1258 cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
1259 EndPoints[Size - 1] =
1260 cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
1261 return true;
1262 }
1263 return false;
1264}
1265
1268 if (!EndPoints.empty())
1269 if (tryMergeRange(EndPoints, Low, High))
1270 return;
1271
1272 EndPoints.push_back(Low);
1273 EndPoints.push_back(High);
1274}
1275
1277 // Given two ranges, we want to compute the union of the ranges. This
1278 // is slightly complicated by having to combine the intervals and merge
1279 // the ones that overlap.
1280
1281 if (!A || !B)
1282 return nullptr;
1283
1284 if (A == B)
1285 return A;
1286
1287 // First, walk both lists in order of the lower boundary of each interval.
1288 // At each step, try to merge the new interval to the last one we adedd.
1290 int AI = 0;
1291 int BI = 0;
1292 int AN = A->getNumOperands() / 2;
1293 int BN = B->getNumOperands() / 2;
1294 while (AI < AN && BI < BN) {
1295 ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
1296 ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
1297
1298 if (ALow->getValue().slt(BLow->getValue())) {
1299 addRange(EndPoints, ALow,
1300 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1301 ++AI;
1302 } else {
1303 addRange(EndPoints, BLow,
1304 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1305 ++BI;
1306 }
1307 }
1308 while (AI < AN) {
1309 addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1310 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1311 ++AI;
1312 }
1313 while (BI < BN) {
1314 addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1315 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1316 ++BI;
1317 }
1318
1319 // If we have more than 2 ranges (4 endpoints) we have to try to merge
1320 // the last and first ones.
1321 unsigned Size = EndPoints.size();
1322 if (Size > 4) {
1323 ConstantInt *FB = EndPoints[0];
1324 ConstantInt *FE = EndPoints[1];
1325 if (tryMergeRange(EndPoints, FB, FE)) {
1326 for (unsigned i = 0; i < Size - 2; ++i) {
1327 EndPoints[i] = EndPoints[i + 2];
1328 }
1329 EndPoints.resize(Size - 2);
1330 }
1331 }
1332
1333 // If in the end we have a single range, it is possible that it is now the
1334 // full range. Just drop the metadata in that case.
1335 if (EndPoints.size() == 2) {
1336 ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1337 if (Range.isFullSet())
1338 return nullptr;
1339 }
1340
1342 MDs.reserve(EndPoints.size());
1343 for (auto *I : EndPoints)
1345 return MDNode::get(A->getContext(), MDs);
1346}
1347
1349 if (!A || !B)
1350 return nullptr;
1351
1352 ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1353 ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1354 if (AVal->getZExtValue() < BVal->getZExtValue())
1355 return A;
1356 return B;
1357}
1358
1359//===----------------------------------------------------------------------===//
1360// NamedMDNode implementation.
1361//
1362
1365}
1366
1367NamedMDNode::NamedMDNode(const Twine &N)
1368 : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1369
1372 delete &getNMDOps(Operands);
1373}
1374
1376 return (unsigned)getNMDOps(Operands).size();
1377}
1378
1380 assert(i < getNumOperands() && "Invalid Operand number!");
1381 auto *N = getNMDOps(Operands)[i].get();
1382 return cast_or_null<MDNode>(N);
1383}
1384
1385void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1386
1387void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1388 assert(I < getNumOperands() && "Invalid operand number");
1389 getNMDOps(Operands)[I].reset(New);
1390}
1391
1393
1394void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1395
1397
1398//===----------------------------------------------------------------------===//
1399// Instruction Metadata method implementations.
1400//
1401
1403 for (const auto &A : Attachments)
1404 if (A.MDKind == ID)
1405 return A.Node;
1406 return nullptr;
1407}
1408
1409void MDAttachments::get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const {
1410 for (const auto &A : Attachments)
1411 if (A.MDKind == ID)
1412 Result.push_back(A.Node);
1413}
1414
1416 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1417 for (const auto &A : Attachments)
1418 Result.emplace_back(A.MDKind, A.Node);
1419
1420 // Sort the resulting array so it is stable with respect to metadata IDs. We
1421 // need to preserve the original insertion order though.
1422 if (Result.size() > 1)
1423 llvm::stable_sort(Result, less_first());
1424}
1425
1426void MDAttachments::set(unsigned ID, MDNode *MD) {
1427 erase(ID);
1428 if (MD)
1429 insert(ID, *MD);
1430}
1431
1432void MDAttachments::insert(unsigned ID, MDNode &MD) {
1433 Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1434}
1435
1436bool MDAttachments::erase(unsigned ID) {
1437 if (empty())
1438 return false;
1439
1440 // Common case is one value.
1441 if (Attachments.size() == 1 && Attachments.back().MDKind == ID) {
1442 Attachments.pop_back();
1443 return true;
1444 }
1445
1446 auto OldSize = Attachments.size();
1447 llvm::erase_if(Attachments,
1448 [ID](const Attachment &A) { return A.MDKind == ID; });
1449 return OldSize != Attachments.size();
1450}
1451
1453 if (!hasMetadata())
1454 return nullptr;
1455 unsigned KindID = getContext().getMDKindID(Kind);
1456 return getMetadataImpl(KindID);
1457}
1458
1459MDNode *Value::getMetadataImpl(unsigned KindID) const {
1460 const LLVMContext &Ctx = getContext();
1461 const MDAttachments &Attachements = Ctx.pImpl->ValueMetadata.at(this);
1462 return Attachements.lookup(KindID);
1463}
1464
1465void Value::getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const {
1466 if (hasMetadata())
1467 getContext().pImpl->ValueMetadata.at(this).get(KindID, MDs);
1468}
1469
1471 if (hasMetadata())
1472 getMetadata(getContext().getMDKindID(Kind), MDs);
1473}
1474
1476 SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1477 if (hasMetadata()) {
1478 assert(getContext().pImpl->ValueMetadata.count(this) &&
1479 "bit out of sync with hash table");
1480 const MDAttachments &Info = getContext().pImpl->ValueMetadata.at(this);
1481 Info.getAll(MDs);
1482 }
1483}
1484
1485void Value::setMetadata(unsigned KindID, MDNode *Node) {
1486 assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1487
1488 // Handle the case when we're adding/updating metadata on a value.
1489 if (Node) {
1491 assert(!Info.empty() == HasMetadata && "bit out of sync with hash table");
1492 if (Info.empty())
1493 HasMetadata = true;
1494 Info.set(KindID, Node);
1495 return;
1496 }
1497
1498 // Otherwise, we're removing metadata from an instruction.
1499 assert((HasMetadata == (getContext().pImpl->ValueMetadata.count(this) > 0)) &&
1500 "bit out of sync with hash table");
1501 if (!HasMetadata)
1502 return; // Nothing to remove!
1503 MDAttachments &Info = getContext().pImpl->ValueMetadata.find(this)->second;
1504
1505 // Handle removal of an existing value.
1506 Info.erase(KindID);
1507 if (!Info.empty())
1508 return;
1509 getContext().pImpl->ValueMetadata.erase(this);
1510 HasMetadata = false;
1511}
1512
1514 if (!Node && !HasMetadata)
1515 return;
1516 setMetadata(getContext().getMDKindID(Kind), Node);
1517}
1518
1519void Value::addMetadata(unsigned KindID, MDNode &MD) {
1520 assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1521 if (!HasMetadata)
1522 HasMetadata = true;
1523 getContext().pImpl->ValueMetadata[this].insert(KindID, MD);
1524}
1525
1527 addMetadata(getContext().getMDKindID(Kind), MD);
1528}
1529
1530bool Value::eraseMetadata(unsigned KindID) {
1531 // Nothing to unset.
1532 if (!HasMetadata)
1533 return false;
1534
1535 MDAttachments &Store = getContext().pImpl->ValueMetadata.find(this)->second;
1536 bool Changed = Store.erase(KindID);
1537 if (Store.empty())
1538 clearMetadata();
1539 return Changed;
1540}
1541
1542void Value::eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred) {
1543 if (!HasMetadata)
1544 return;
1545
1546 auto &MetadataStore = getContext().pImpl->ValueMetadata;
1547 MDAttachments &Info = MetadataStore.find(this)->second;
1548 assert(!Info.empty() && "bit out of sync with hash table");
1549 Info.remove_if([Pred](const MDAttachments::Attachment &I) {
1550 return Pred(I.MDKind, I.Node);
1551 });
1552
1553 if (Info.empty())
1554 clearMetadata();
1555}
1556
1558 if (!HasMetadata)
1559 return;
1560 assert(getContext().pImpl->ValueMetadata.count(this) &&
1561 "bit out of sync with hash table");
1562 getContext().pImpl->ValueMetadata.erase(this);
1563 HasMetadata = false;
1564}
1565
1567 if (!Node && !hasMetadata())
1568 return;
1569 setMetadata(getContext().getMDKindID(Kind), Node);
1570}
1571
1572MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1573 const LLVMContext &Ctx = getContext();
1574 unsigned KindID = Ctx.getMDKindID(Kind);
1575 if (KindID == LLVMContext::MD_dbg)
1576 return DbgLoc.getAsMDNode();
1577 return Value::getMetadata(KindID);
1578}
1579
1580void Instruction::eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred) {
1581 if (DbgLoc && Pred(LLVMContext::MD_dbg, DbgLoc.getAsMDNode()))
1582 DbgLoc = {};
1583
1585}
1586
1588 if (!Value::hasMetadata())
1589 return; // Nothing to remove!
1590
1591 SmallSet<unsigned, 4> KnownSet;
1592 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1593
1594 // A DIAssignID attachment is debug metadata, don't drop it.
1595 KnownSet.insert(LLVMContext::MD_DIAssignID);
1596
1597 Value::eraseMetadataIf([&KnownSet](unsigned MDKind, MDNode *Node) {
1598 return !KnownSet.count(MDKind);
1599 });
1600}
1601
1602void Instruction::updateDIAssignIDMapping(DIAssignID *ID) {
1603 auto &IDToInstrs = getContext().pImpl->AssignmentIDToInstrs;
1604 if (const DIAssignID *CurrentID =
1605 cast_or_null<DIAssignID>(getMetadata(LLVMContext::MD_DIAssignID))) {
1606 // Nothing to do if the ID isn't changing.
1607 if (ID == CurrentID)
1608 return;
1609
1610 // Unmap this instruction from its current ID.
1611 auto InstrsIt = IDToInstrs.find(CurrentID);
1612 assert(InstrsIt != IDToInstrs.end() &&
1613 "Expect existing attachment to be mapped");
1614
1615 auto &InstVec = InstrsIt->second;
1616 auto *InstIt = llvm::find(InstVec, this);
1617 assert(InstIt != InstVec.end() &&
1618 "Expect instruction to be mapped to attachment");
1619 // The vector contains a ptr to this. If this is the only element in the
1620 // vector, remove the ID:vector entry, otherwise just remove the
1621 // instruction from the vector.
1622 if (InstVec.size() == 1)
1623 IDToInstrs.erase(InstrsIt);
1624 else
1625 InstVec.erase(InstIt);
1626 }
1627
1628 // Map this instruction to the new ID.
1629 if (ID)
1630 IDToInstrs[ID].push_back(this);
1631}
1632
1633void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1634 if (!Node && !hasMetadata())
1635 return;
1636
1637 // Handle 'dbg' as a special case since it is not stored in the hash table.
1638 if (KindID == LLVMContext::MD_dbg) {
1639 DbgLoc = DebugLoc(Node);
1640 return;
1641 }
1642
1643 // Update DIAssignID to Instruction(s) mapping.
1644 if (KindID == LLVMContext::MD_DIAssignID) {
1645 // The DIAssignID tracking infrastructure doesn't support RAUWing temporary
1646 // nodes with DIAssignIDs. The cast_or_null below would also catch this, but
1647 // having a dedicated assert helps make this obvious.
1648 assert((!Node || !Node->isTemporary()) &&
1649 "Temporary DIAssignIDs are invalid");
1650 updateDIAssignIDMapping(cast_or_null<DIAssignID>(Node));
1651 }
1652
1653 Value::setMetadata(KindID, Node);
1654}
1655
1658 if (auto *Existing = getMetadata(LLVMContext::MD_annotation)) {
1659 SmallSetVector<StringRef, 2> AnnotationsSet(Annotations.begin(),
1660 Annotations.end());
1661 auto *Tuple = cast<MDTuple>(Existing);
1662 for (auto &N : Tuple->operands()) {
1663 if (isa<MDString>(N.get())) {
1664 Names.push_back(N);
1665 continue;
1666 }
1667 auto *MDAnnotationTuple = cast<MDTuple>(N);
1668 if (any_of(MDAnnotationTuple->operands(), [&AnnotationsSet](auto &Op) {
1669 return AnnotationsSet.contains(cast<MDString>(Op)->getString());
1670 }))
1671 return;
1672 Names.push_back(N);
1673 }
1674 }
1675
1676 MDBuilder MDB(getContext());
1677 SmallVector<Metadata *> MDAnnotationStrings;
1678 for (StringRef Annotation : Annotations)
1679 MDAnnotationStrings.push_back(MDB.createString(Annotation));
1680 MDNode *InfoTuple = MDTuple::get(getContext(), MDAnnotationStrings);
1681 Names.push_back(InfoTuple);
1682 MDNode *MD = MDTuple::get(getContext(), Names);
1683 setMetadata(LLVMContext::MD_annotation, MD);
1684}
1685
1688 if (auto *Existing = getMetadata(LLVMContext::MD_annotation)) {
1689 auto *Tuple = cast<MDTuple>(Existing);
1690 for (auto &N : Tuple->operands()) {
1691 if (isa<MDString>(N.get()) &&
1692 cast<MDString>(N.get())->getString() == Name)
1693 return;
1694 Names.push_back(N.get());
1695 }
1696 }
1697
1698 MDBuilder MDB(getContext());
1699 Names.push_back(MDB.createString(Name));
1700 MDNode *MD = MDTuple::get(getContext(), Names);
1701 setMetadata(LLVMContext::MD_annotation, MD);
1702}
1703
1705 AAMDNodes Result;
1706 // Not using Instruction::hasMetadata() because we're not interested in
1707 // DebugInfoMetadata.
1708 if (Value::hasMetadata()) {
1709 const MDAttachments &Info = getContext().pImpl->ValueMetadata.at(this);
1710 Result.TBAA = Info.lookup(LLVMContext::MD_tbaa);
1711 Result.TBAAStruct = Info.lookup(LLVMContext::MD_tbaa_struct);
1712 Result.Scope = Info.lookup(LLVMContext::MD_alias_scope);
1713 Result.NoAlias = Info.lookup(LLVMContext::MD_noalias);
1714 }
1715 return Result;
1716}
1717
1719 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1720 setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
1721 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1722 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1723}
1724
1726 setMetadata(llvm::LLVMContext::MD_nosanitize,
1727 llvm::MDNode::get(getContext(), std::nullopt));
1728}
1729
1730void Instruction::getAllMetadataImpl(
1731 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1732 Result.clear();
1733
1734 // Handle 'dbg' as a special case since it is not stored in the hash table.
1735 if (DbgLoc) {
1736 Result.push_back(
1737 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1738 }
1739 Value::getAllMetadata(Result);
1740}
1741
1743 assert(
1744 (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select ||
1745 getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke ||
1746 getOpcode() == Instruction::IndirectBr ||
1747 getOpcode() == Instruction::Switch) &&
1748 "Looking for branch weights on something besides branch");
1749
1750 return ::extractProfTotalWeight(*this, TotalVal);
1751}
1752
1755 Other->getAllMetadata(MDs);
1756 for (auto &MD : MDs) {
1757 // We need to adjust the type metadata offset.
1758 if (Offset != 0 && MD.first == LLVMContext::MD_type) {
1759 auto *OffsetConst = cast<ConstantInt>(
1760 cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1761 Metadata *TypeId = MD.second->getOperand(1);
1762 auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1763 OffsetConst->getType(), OffsetConst->getValue() + Offset));
1764 addMetadata(LLVMContext::MD_type,
1765 *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1766 continue;
1767 }
1768 // If an offset adjustment was specified we need to modify the DIExpression
1769 // to prepend the adjustment:
1770 // !DIExpression(DW_OP_plus, Offset, [original expr])
1771 auto *Attachment = MD.second;
1772 if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
1773 DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1774 DIExpression *E = nullptr;
1775 if (!GV) {
1776 auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1777 GV = GVE->getVariable();
1778 E = GVE->getExpression();
1779 }
1780 ArrayRef<uint64_t> OrigElements;
1781 if (E)
1782 OrigElements = E->getElements();
1783 std::vector<uint64_t> Elements(OrigElements.size() + 2);
1784 Elements[0] = dwarf::DW_OP_plus_uconst;
1785 Elements[1] = Offset;
1786 llvm::copy(OrigElements, Elements.begin() + 2);
1787 E = DIExpression::get(getContext(), Elements);
1788 Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1789 }
1790 addMetadata(MD.first, *Attachment);
1791 }
1792}
1793
1796 LLVMContext::MD_type,
1798 {ConstantAsMetadata::get(ConstantInt::get(
1800 TypeID}));
1801}
1802
1804 // Remove any existing vcall visibility metadata first in case we are
1805 // updating.
1806 eraseMetadata(LLVMContext::MD_vcall_visibility);
1807 addMetadata(LLVMContext::MD_vcall_visibility,
1809 {ConstantAsMetadata::get(ConstantInt::get(
1811}
1812
1814 if (MDNode *MD = getMetadata(LLVMContext::MD_vcall_visibility)) {
1815 uint64_t Val = cast<ConstantInt>(
1816 cast<ConstantAsMetadata>(MD->getOperand(0))->getValue())
1817 ->getZExtValue();
1818 assert(Val <= 2 && "unknown vcall visibility!");
1819 return (VCallVisibility)Val;
1820 }
1822}
1823
1825 setMetadata(LLVMContext::MD_dbg, SP);
1826}
1827
1829 return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1830}
1831
1833 if (DISubprogram *SP = getSubprogram()) {
1834 if (DICompileUnit *CU = SP->getUnit()) {
1835 return CU->getDebugInfoForProfiling();
1836 }
1837 }
1838 return false;
1839}
1840
1842 addMetadata(LLVMContext::MD_dbg, *GV);
1843}
1844
1848 getMetadata(LLVMContext::MD_dbg, MDs);
1849 for (MDNode *MD : MDs)
1850 GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1851}
This file defines the StringMap class.
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
static const Function * getParent(const Value *V)
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Domain getDomain(const ConstantRange &CR)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live value
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Rewrite Partial Register Uses
#define I(x, y, z)
Definition: MD5.cpp:58
#define H(x, y, z)
Definition: MD5.cpp:57
mir Rename Register Operands
static DISubprogram * getLocalFunctionMetadata(Value *V)
Definition: Metadata.cpp:475
static Metadata * canonicalizeMetadataForValue(LLVMContext &Context, Metadata *MD)
Canonicalize metadata arguments to intrinsics.
Definition: Metadata.cpp:81
static bool isOperandUnresolved(Metadata *Op)
Definition: Metadata.cpp:743
static bool hasSelfReference(MDNode *N)
Definition: Metadata.cpp:852
static void addRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1266
static SmallVector< TrackingMDRef, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:1363
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1243
static T * uniquifyImpl(T *N, DenseSet< T *, InfoT > &Store)
Definition: Metadata.cpp:970
static bool isContiguous(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1239
static MDNode * getOrSelfReference(LLVMContext &Context, ArrayRef< Metadata * > Ops)
Get a node or a self-reference that looks like it.
Definition: Metadata.cpp:1091
static bool tryMergeRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1247
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
uint64_t High
LLVMContext & Context
This file contains the declarations for profiling metadata utility functions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
Class for arbitrary precision integers.
Definition: APInt.h:76
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1108
This is a simple wrapper around an MDNode which provides a higher-level interface by hiding the detai...
Definition: Metadata.h:1565
Annotations lets you mark points and ranges inside source code, for tests:
Definition: Annotations.h:53
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1703
This class represents a function call, abstracting a target machine's calling convention.
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:528
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:153
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:144
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
This is an important base class in LLVM.
Definition: Constant.h:41
Assignment ID.
DWARF expression.
A pair of DIGlobalVariable and DIExpression.
Subprogram description.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition: DebugLoc.h:106
Base class for tracking ValueAsMetadata/DIArgLists with user lookups and Owner callbacks outside of V...
Definition: Metadata.h:212
void handleChangedValue(void *Old, Metadata *NewDebugValue)
To be called by ReplaceableMetadataImpl::replaceAllUsesWith, where Old is a pointer to one of the poi...
Definition: Metadata.cpp:156
DPValue * getUser()
Definition: Metadata.cpp:151
std::array< Metadata *, 3 > DebugValues
Definition: Metadata.h:218
void resetDebugValue(size_t Idx, Metadata *DebugValue)
Definition: Metadata.h:273
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
Definition: Metadata.cpp:1824
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1828
bool shouldEmitDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Definition: Metadata.cpp:1832
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1794
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1485
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1753
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1813
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1530
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1519
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:565
void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1803
unsigned Visibility
Definition: GlobalValue.h:99
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1845
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1841
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1718
bool extractProfTotalWeight(uint64_t &TotalVal) const
Retrieve total raw weight values of a branch.
Definition: Metadata.cpp:1742
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:340
void addAnnotationMetadata(StringRef Annotation)
Adds an !annotation metadata node with Annotation to this instruction.
Definition: Metadata.cpp:1686
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:358
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1633
void setNoSanitizeMetadata()
Sets the nosanitize metadata on this instruction.
Definition: Metadata.cpp:1725
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1704
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:251
void dropUnknownNonDebugMetadata()
Definition: Instruction.h:414
void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata that matches the predicate.
Definition: Metadata.cpp:1580
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
StringMap< MDString, BumpPtrAllocator > MDStringCache
DenseMap< DIAssignID *, SmallVector< Instruction *, 1 > > AssignmentIDToInstrs
Map DIAssignID -> Instructions with that attachment.
std::vector< MDNode * > DistinctMDNodes
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:69
Multimap-like storage for metadata attachments.
void insert(unsigned ID, MDNode &MD)
Adds an attachment to a particular node.
Definition: Metadata.cpp:1432
void get(unsigned ID, SmallVectorImpl< MDNode * > &Result) const
Appends all attachments with the given ID to Result in insertion order.
Definition: Metadata.cpp:1409
void getAll(SmallVectorImpl< std::pair< unsigned, MDNode * > > &Result) const
Appends all attachments for the global to Result, sorting by attachment ID.
Definition: Metadata.cpp:1415
void set(unsigned ID, MDNode *MD)
Set an attachment to a particular node.
Definition: Metadata.cpp:1426
MDNode * lookup(unsigned ID) const
Returns the first attachment with the given ID or nullptr if no such attachment exists.
Definition: Metadata.cpp:1402
bool erase(unsigned ID)
Remove attachments with the given ID.
Definition: Metadata.cpp:1436
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
Metadata node.
Definition: Metadata.h:1067
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1132
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:1068
void resolveCycles()
Resolve cycles.
Definition: Metadata.cpp:832
mutable_op_range mutable_operands()
Definition: Metadata.h:1205
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition: Metadata.h:1264
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
Definition: Metadata.cpp:1105
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Definition: Metadata.cpp:1040
void resolve()
Resolve a unique, unresolved node.
Definition: Metadata.cpp:786
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
void storeDistinctInContext()
Definition: Metadata.cpp:1046
bool isTemporary() const
Definition: Metadata.h:1251
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1426
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
static MDNode * getMergedProfMetadata(MDNode *A, MDNode *B, const Instruction *AInstr, const Instruction *BInstr)
Merge !prof metadata from two instructions.
Definition: Metadata.cpp:1214
bool isUniqued() const
Definition: Metadata.h:1249
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1164
void setNumUnresolved(unsigned N)
Definition: Metadata.h:1351
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1434
MDOperand * mutable_begin()
Definition: Metadata.h:1200
TempMDNode clone() const
Create a (temporary) clone of this.
Definition: Metadata.cpp:658
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1276
bool isDistinct() const
Definition: Metadata.h:1250
void setOperand(unsigned I, Metadata *New)
Set an operand.
Definition: Metadata.cpp:1080
MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2=std::nullopt)
Definition: Metadata.cpp:641
bool isResolved() const
Check if node is fully resolved.
Definition: Metadata.h:1247
op_iterator op_begin() const
Definition: Metadata.h:1418
static MDNode * intersect(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1119
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
Definition: MetadataImpl.h:42
LLVMContext & getContext() const
Definition: Metadata.h:1231
void dropAllReferences()
Definition: Metadata.cpp:898
static MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1348
unsigned getNumUnresolved() const
Definition: Metadata.h:1349
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:889
void reset()
Definition: Metadata.h:923
A single uniqued string.
Definition: Metadata.h:720
StringRef getString() const
Definition: Metadata.cpp:607
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:597
Tuple of metadata.
Definition: Metadata.h:1470
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1498
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:176
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
static MetadataAsValue * getIfExists(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:111
static bool isReplaceable(const Metadata &MD)
Check whether metadata is replaceable.
Definition: Metadata.cpp:244
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Definition: Metadata.h:349
static bool retrack(Metadata *&MD, Metadata *&New)
Move tracking from one reference to another.
Definition: Metadata.h:360
static bool track(Metadata *&MD)
Track the reference to metadata.
Definition: Metadata.h:315
Root of the metadata hierarchy.
Definition: Metadata.h:62
StorageType
Active type of storage.
Definition: Metadata.h:70
unsigned char Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition: Metadata.h:73
unsigned getMetadataID() const
Definition: Metadata.h:102
void eraseNamedMetadata(NamedMDNode *NMD)
Remove the given NamedMDNode from this module and delete it.
Definition: Module.cpp:281
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
iterator end() const
Definition: ArrayRef.h:357
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:1387
StringRef getName() const
Definition: Metadata.cpp:1396
void dropAllReferences()
Remove all uses and clear node vector.
Definition: Metadata.h:1794
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1392
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1379
unsigned getNumOperands() const
Definition: Metadata.cpp:1375
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1394
Module * getParent()
Get the module that holds this named metadata collection.
Definition: Metadata.h:1799
void addOperand(MDNode *M)
Definition: Metadata.cpp:1385
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:142
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:155
bool is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:150
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1827
Shared implementation of use-lists for replaceable metadata.
Definition: Metadata.h:382
static void SalvageDebugInfo(const Constant &C)
Replace all uses of the constant with Undef in debug info metadata.
Definition: Metadata.cpp:328
void replaceAllUsesWith(Metadata *MD)
Replace all uses of this with MD.
Definition: Metadata.cpp:358
void resolveAllUses(bool ResolveUsers=true)
Resolve all uses of this.
Definition: Metadata.cpp:411
SmallVector< Metadata * > getAllArgListUsers()
Returns the list of all DIArgList users of this.
Definition: Metadata.cpp:248
SmallVector< DPValue * > getAllDPValueUsers()
Returns the list of all DPValue users of this.
Definition: Metadata.cpp:269
ArrayRef< value_type > getArrayRef() const
Definition: SetVector.h:84
bool remove_if(UnaryPredicate P)
Remove items from the set vector based on a predicate function.
Definition: SetVector.h:237
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:360
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:366
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:166
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:179
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void reserve(size_type N)
Definition: SmallVector.h:676
void resize(size_type N)
Definition: SmallVector.h:651
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:164
Tracking metadata reference.
Definition: TrackingMDRef.h:25
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getMetadataTy(LLVMContext &C)
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
static IntegerType * getInt64Ty(LLVMContext &C)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:450
void replaceAllUsesWith(Metadata *MD)
Handle collisions after Value::replaceAllUsesWith().
Definition: Metadata.h:510
static void handleDeletion(Value *V)
Definition: Metadata.cpp:516
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:492
static ValueAsMetadata * getIfExists(Value *V)
Definition: Metadata.cpp:511
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:535
Value * getValue() const
Definition: Metadata.h:490
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
unsigned IsUsedByMD
Definition: Value.h:111
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition: Value.h:589
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1485
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1475
MDNode * getMetadataImpl(unsigned KindID) const
Get metadata for the given kind, if any.
Definition: Metadata.cpp:1459
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1530
unsigned HasMetadata
Definition: Value.h:113
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1519
void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata attachments matching the given predicate.
Definition: Metadata.cpp:1542
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1557
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:565
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Key
PAL metadata keys.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:236
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
@ Offset
Definition: DWP.cpp:456
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition: STLExtras.h:862
void stable_sort(R &&Range)
Definition: STLExtras.h:2004
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1689
static T * getUniqued(DenseSet< T *, InfoT > &Store, const typename InfoT::KeyTy &Key)
Definition: MetadataImpl.h:22
TypedTrackingMDRef< MDNode > TrackingMDNodeRef
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1738
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1656
@ Ref
The access may reference the value stored in memory.
@ Other
Any other memory.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1833
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1858
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1930
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:2060
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:478
#define N
static Yes & check(SFINAE< void(U::*)(unsigned), &U::setHash > *)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
Function object to check whether the first component of a container supported by std::get (like std::...
Definition: STLExtras.h:1459