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