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
104 MD = canonicalizeMetadataForValue(Context, MD);
105 auto *&Entry = Context.pImpl->MetadataAsValues[MD];
106 if (!Entry)
107 Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
108 return Entry;
109}
110
112 Metadata *MD) {
113 MD = canonicalizeMetadataForValue(Context, MD);
114 auto &Store = Context.pImpl->MetadataAsValues;
115 return Store.lookup(MD);
116}
117
118void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
119 LLVMContext &Context = getContext();
120 MD = canonicalizeMetadataForValue(Context, MD);
121 auto &Store = Context.pImpl->MetadataAsValues;
122
123 // Stop tracking the old metadata.
124 Store.erase(this->MD);
125 untrack();
126 this->MD = nullptr;
127
128 // Start tracking MD, or RAUW if necessary.
129 auto *&Entry = Store[MD];
130 if (Entry) {
131 replaceAllUsesWith(Entry);
132 delete this;
133 return;
134 }
135
136 this->MD = MD;
137 track();
138 Entry = this;
139}
140
141void MetadataAsValue::track() {
142 if (MD)
143 MetadataTracking::track(&MD, *MD, *this);
144}
145
146void MetadataAsValue::untrack() {
147 if (MD)
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 == "branch_weights" && BProfName == "branch_weights") {
1199 ConstantInt *AInstrWeight = mdconst::dyn_extract<ConstantInt>(
1200 A->getOperand(getBranchWeightOffset(A)));
1201 ConstantInt *BInstrWeight = mdconst::dyn_extract<ConstantInt>(
1202 B->getOperand(getBranchWeightOffset(B)));
1203 assert(AInstrWeight && BInstrWeight && "verified by LLVM verifier");
1204 return MDNode::get(Ctx,
1205 {MDHelper.createString("branch_weights"),
1206 MDHelper.createConstant(ConstantInt::get(
1207 Type::getInt64Ty(Ctx),
1208 SaturatingAdd(AInstrWeight->getZExtValue(),
1209 BInstrWeight->getZExtValue())))});
1210 }
1211 return nullptr;
1212}
1213
1214// Pass in both instructions and nodes. Instruction information (e.g.,
1215// instruction type) helps interpret profiles and make implementation clearer.
1217 const Instruction *AInstr,
1218 const Instruction *BInstr) {
1219 if (!(A && B)) {
1220 return A ? A : B;
1221 }
1222
1223 assert(AInstr->getMetadata(LLVMContext::MD_prof) == A &&
1224 "Caller should guarantee");
1225 assert(BInstr->getMetadata(LLVMContext::MD_prof) == B &&
1226 "Caller should guarantee");
1227
1228 const CallInst *ACall = dyn_cast<CallInst>(AInstr);
1229 const CallInst *BCall = dyn_cast<CallInst>(BInstr);
1230
1231 // Both ACall and BCall are direct callsites.
1232 if (ACall && BCall && ACall->getCalledFunction() &&
1233 BCall->getCalledFunction())
1234 return mergeDirectCallProfMetadata(A, B, AInstr, BInstr);
1235
1236 // The rest of the cases are not implemented but could be added
1237 // when there are use cases.
1238 return nullptr;
1239}
1240
1241static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1242 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1243}
1244
1245static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
1246 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
1247}
1248
1251 ConstantRange NewRange(Low->getValue(), High->getValue());
1252 unsigned Size = EndPoints.size();
1253 APInt LB = EndPoints[Size - 2]->getValue();
1254 APInt LE = EndPoints[Size - 1]->getValue();
1255 ConstantRange LastRange(LB, LE);
1256 if (canBeMerged(NewRange, LastRange)) {
1257 ConstantRange Union = LastRange.unionWith(NewRange);
1258 Type *Ty = High->getType();
1259 EndPoints[Size - 2] =
1260 cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
1261 EndPoints[Size - 1] =
1262 cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
1263 return true;
1264 }
1265 return false;
1266}
1267
1270 if (!EndPoints.empty())
1271 if (tryMergeRange(EndPoints, Low, High))
1272 return;
1273
1274 EndPoints.push_back(Low);
1275 EndPoints.push_back(High);
1276}
1277
1279 // Given two ranges, we want to compute the union of the ranges. This
1280 // is slightly complicated by having to combine the intervals and merge
1281 // the ones that overlap.
1282
1283 if (!A || !B)
1284 return nullptr;
1285
1286 if (A == B)
1287 return A;
1288
1289 // First, walk both lists in order of the lower boundary of each interval.
1290 // At each step, try to merge the new interval to the last one we added.
1292 unsigned AI = 0;
1293 unsigned BI = 0;
1294 unsigned AN = A->getNumOperands() / 2;
1295 unsigned BN = B->getNumOperands() / 2;
1296 while (AI < AN && BI < BN) {
1297 ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
1298 ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
1299
1300 if (ALow->getValue().slt(BLow->getValue())) {
1301 addRange(EndPoints, ALow,
1302 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1303 ++AI;
1304 } else {
1305 addRange(EndPoints, BLow,
1306 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1307 ++BI;
1308 }
1309 }
1310 while (AI < AN) {
1311 addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1312 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1313 ++AI;
1314 }
1315 while (BI < BN) {
1316 addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1317 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1318 ++BI;
1319 }
1320
1321 // If we have more than 2 ranges (4 endpoints) we have to try to merge
1322 // the last and first ones.
1323 unsigned Size = EndPoints.size();
1324 if (Size > 4) {
1325 ConstantInt *FB = EndPoints[0];
1326 ConstantInt *FE = EndPoints[1];
1327 if (tryMergeRange(EndPoints, FB, FE)) {
1328 for (unsigned i = 0; i < Size - 2; ++i) {
1329 EndPoints[i] = EndPoints[i + 2];
1330 }
1331 EndPoints.resize(Size - 2);
1332 }
1333 }
1334
1335 // If in the end we have a single range, it is possible that it is now the
1336 // full range. Just drop the metadata in that case.
1337 if (EndPoints.size() == 2) {
1338 ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1339 if (Range.isFullSet())
1340 return nullptr;
1341 }
1342
1344 MDs.reserve(EndPoints.size());
1345 for (auto *I : EndPoints)
1347 return MDNode::get(A->getContext(), MDs);
1348}
1349
1351 if (!A || !B)
1352 return nullptr;
1353
1354 ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1355 ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1356 if (AVal->getZExtValue() < BVal->getZExtValue())
1357 return A;
1358 return B;
1359}
1360
1361//===----------------------------------------------------------------------===//
1362// NamedMDNode implementation.
1363//
1364
1367}
1368
1369NamedMDNode::NamedMDNode(const Twine &N)
1370 : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1371
1374 delete &getNMDOps(Operands);
1375}
1376
1378 return (unsigned)getNMDOps(Operands).size();
1379}
1380
1382 assert(i < getNumOperands() && "Invalid Operand number!");
1383 auto *N = getNMDOps(Operands)[i].get();
1384 return cast_or_null<MDNode>(N);
1385}
1386
1387void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1388
1389void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1390 assert(I < getNumOperands() && "Invalid operand number");
1391 getNMDOps(Operands)[I].reset(New);
1392}
1393
1395
1396void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1397
1399
1400//===----------------------------------------------------------------------===//
1401// Instruction Metadata method implementations.
1402//
1403
1405 for (const auto &A : Attachments)
1406 if (A.MDKind == ID)
1407 return A.Node;
1408 return nullptr;
1409}
1410
1411void MDAttachments::get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const {
1412 for (const auto &A : Attachments)
1413 if (A.MDKind == ID)
1414 Result.push_back(A.Node);
1415}
1416
1418 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1419 for (const auto &A : Attachments)
1420 Result.emplace_back(A.MDKind, A.Node);
1421
1422 // Sort the resulting array so it is stable with respect to metadata IDs. We
1423 // need to preserve the original insertion order though.
1424 if (Result.size() > 1)
1425 llvm::stable_sort(Result, less_first());
1426}
1427
1428void MDAttachments::set(unsigned ID, MDNode *MD) {
1429 erase(ID);
1430 if (MD)
1431 insert(ID, *MD);
1432}
1433
1434void MDAttachments::insert(unsigned ID, MDNode &MD) {
1435 Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1436}
1437
1438bool MDAttachments::erase(unsigned ID) {
1439 if (empty())
1440 return false;
1441
1442 // Common case is one value.
1443 if (Attachments.size() == 1 && Attachments.back().MDKind == ID) {
1444 Attachments.pop_back();
1445 return true;
1446 }
1447
1448 auto OldSize = Attachments.size();
1449 llvm::erase_if(Attachments,
1450 [ID](const Attachment &A) { return A.MDKind == ID; });
1451 return OldSize != Attachments.size();
1452}
1453
1455 if (!hasMetadata())
1456 return nullptr;
1457 unsigned KindID = getContext().getMDKindID(Kind);
1458 return getMetadataImpl(KindID);
1459}
1460
1461MDNode *Value::getMetadataImpl(unsigned KindID) const {
1462 const LLVMContext &Ctx = getContext();
1463 const MDAttachments &Attachements = Ctx.pImpl->ValueMetadata.at(this);
1464 return Attachements.lookup(KindID);
1465}
1466
1467void Value::getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const {
1468 if (hasMetadata())
1469 getContext().pImpl->ValueMetadata.at(this).get(KindID, MDs);
1470}
1471
1473 if (hasMetadata())
1474 getMetadata(getContext().getMDKindID(Kind), MDs);
1475}
1476
1478 SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1479 if (hasMetadata()) {
1480 assert(getContext().pImpl->ValueMetadata.count(this) &&
1481 "bit out of sync with hash table");
1482 const MDAttachments &Info = getContext().pImpl->ValueMetadata.at(this);
1483 Info.getAll(MDs);
1484 }
1485}
1486
1487void Value::setMetadata(unsigned KindID, MDNode *Node) {
1488 assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1489
1490 // Handle the case when we're adding/updating metadata on a value.
1491 if (Node) {
1493 assert(!Info.empty() == HasMetadata && "bit out of sync with hash table");
1494 if (Info.empty())
1495 HasMetadata = true;
1496 Info.set(KindID, Node);
1497 return;
1498 }
1499
1500 // Otherwise, we're removing metadata from an instruction.
1501 assert((HasMetadata == (getContext().pImpl->ValueMetadata.count(this) > 0)) &&
1502 "bit out of sync with hash table");
1503 if (!HasMetadata)
1504 return; // Nothing to remove!
1505 MDAttachments &Info = getContext().pImpl->ValueMetadata.find(this)->second;
1506
1507 // Handle removal of an existing value.
1508 Info.erase(KindID);
1509 if (!Info.empty())
1510 return;
1511 getContext().pImpl->ValueMetadata.erase(this);
1512 HasMetadata = false;
1513}
1514
1516 if (!Node && !HasMetadata)
1517 return;
1518 setMetadata(getContext().getMDKindID(Kind), Node);
1519}
1520
1521void Value::addMetadata(unsigned KindID, MDNode &MD) {
1522 assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1523 if (!HasMetadata)
1524 HasMetadata = true;
1525 getContext().pImpl->ValueMetadata[this].insert(KindID, MD);
1526}
1527
1529 addMetadata(getContext().getMDKindID(Kind), MD);
1530}
1531
1532bool Value::eraseMetadata(unsigned KindID) {
1533 // Nothing to unset.
1534 if (!HasMetadata)
1535 return false;
1536
1537 MDAttachments &Store = getContext().pImpl->ValueMetadata.find(this)->second;
1538 bool Changed = Store.erase(KindID);
1539 if (Store.empty())
1540 clearMetadata();
1541 return Changed;
1542}
1543
1544void Value::eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred) {
1545 if (!HasMetadata)
1546 return;
1547
1548 auto &MetadataStore = getContext().pImpl->ValueMetadata;
1549 MDAttachments &Info = MetadataStore.find(this)->second;
1550 assert(!Info.empty() && "bit out of sync with hash table");
1551 Info.remove_if([Pred](const MDAttachments::Attachment &I) {
1552 return Pred(I.MDKind, I.Node);
1553 });
1554
1555 if (Info.empty())
1556 clearMetadata();
1557}
1558
1560 if (!HasMetadata)
1561 return;
1562 assert(getContext().pImpl->ValueMetadata.count(this) &&
1563 "bit out of sync with hash table");
1564 getContext().pImpl->ValueMetadata.erase(this);
1565 HasMetadata = false;
1566}
1567
1569 if (!Node && !hasMetadata())
1570 return;
1571 setMetadata(getContext().getMDKindID(Kind), Node);
1572}
1573
1574MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1575 const LLVMContext &Ctx = getContext();
1576 unsigned KindID = Ctx.getMDKindID(Kind);
1577 if (KindID == LLVMContext::MD_dbg)
1578 return DbgLoc.getAsMDNode();
1579 return Value::getMetadata(KindID);
1580}
1581
1582void Instruction::eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred) {
1583 if (DbgLoc && Pred(LLVMContext::MD_dbg, DbgLoc.getAsMDNode()))
1584 DbgLoc = {};
1585
1587}
1588
1590 if (!Value::hasMetadata())
1591 return; // Nothing to remove!
1592
1593 SmallSet<unsigned, 32> KnownSet;
1594 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1595
1596 // A DIAssignID attachment is debug metadata, don't drop it.
1597 KnownSet.insert(LLVMContext::MD_DIAssignID);
1598
1599 Value::eraseMetadataIf([&KnownSet](unsigned MDKind, MDNode *Node) {
1600 return !KnownSet.count(MDKind);
1601 });
1602}
1603
1604void Instruction::updateDIAssignIDMapping(DIAssignID *ID) {
1605 auto &IDToInstrs = getContext().pImpl->AssignmentIDToInstrs;
1606 if (const DIAssignID *CurrentID =
1607 cast_or_null<DIAssignID>(getMetadata(LLVMContext::MD_DIAssignID))) {
1608 // Nothing to do if the ID isn't changing.
1609 if (ID == CurrentID)
1610 return;
1611
1612 // Unmap this instruction from its current ID.
1613 auto InstrsIt = IDToInstrs.find(CurrentID);
1614 assert(InstrsIt != IDToInstrs.end() &&
1615 "Expect existing attachment to be mapped");
1616
1617 auto &InstVec = InstrsIt->second;
1618 auto *InstIt = llvm::find(InstVec, this);
1619 assert(InstIt != InstVec.end() &&
1620 "Expect instruction to be mapped to attachment");
1621 // The vector contains a ptr to this. If this is the only element in the
1622 // vector, remove the ID:vector entry, otherwise just remove the
1623 // instruction from the vector.
1624 if (InstVec.size() == 1)
1625 IDToInstrs.erase(InstrsIt);
1626 else
1627 InstVec.erase(InstIt);
1628 }
1629
1630 // Map this instruction to the new ID.
1631 if (ID)
1632 IDToInstrs[ID].push_back(this);
1633}
1634
1635void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1636 if (!Node && !hasMetadata())
1637 return;
1638
1639 // Handle 'dbg' as a special case since it is not stored in the hash table.
1640 if (KindID == LLVMContext::MD_dbg) {
1641 DbgLoc = DebugLoc(Node);
1642 return;
1643 }
1644
1645 // Update DIAssignID to Instruction(s) mapping.
1646 if (KindID == LLVMContext::MD_DIAssignID) {
1647 // The DIAssignID tracking infrastructure doesn't support RAUWing temporary
1648 // nodes with DIAssignIDs. The cast_or_null below would also catch this, but
1649 // having a dedicated assert helps make this obvious.
1650 assert((!Node || !Node->isTemporary()) &&
1651 "Temporary DIAssignIDs are invalid");
1652 updateDIAssignIDMapping(cast_or_null<DIAssignID>(Node));
1653 }
1654
1655 Value::setMetadata(KindID, Node);
1656}
1657
1660 if (auto *Existing = getMetadata(LLVMContext::MD_annotation)) {
1661 SmallSetVector<StringRef, 2> AnnotationsSet(Annotations.begin(),
1662 Annotations.end());
1663 auto *Tuple = cast<MDTuple>(Existing);
1664 for (auto &N : Tuple->operands()) {
1665 if (isa<MDString>(N.get())) {
1666 Names.push_back(N);
1667 continue;
1668 }
1669 auto *MDAnnotationTuple = cast<MDTuple>(N);
1670 if (any_of(MDAnnotationTuple->operands(), [&AnnotationsSet](auto &Op) {
1671 return AnnotationsSet.contains(cast<MDString>(Op)->getString());
1672 }))
1673 return;
1674 Names.push_back(N);
1675 }
1676 }
1677
1678 MDBuilder MDB(getContext());
1679 SmallVector<Metadata *> MDAnnotationStrings;
1680 for (StringRef Annotation : Annotations)
1681 MDAnnotationStrings.push_back(MDB.createString(Annotation));
1682 MDNode *InfoTuple = MDTuple::get(getContext(), MDAnnotationStrings);
1683 Names.push_back(InfoTuple);
1684 MDNode *MD = MDTuple::get(getContext(), Names);
1685 setMetadata(LLVMContext::MD_annotation, MD);
1686}
1687
1690 if (auto *Existing = getMetadata(LLVMContext::MD_annotation)) {
1691 auto *Tuple = cast<MDTuple>(Existing);
1692 for (auto &N : Tuple->operands()) {
1693 if (isa<MDString>(N.get()) &&
1694 cast<MDString>(N.get())->getString() == Name)
1695 return;
1696 Names.push_back(N.get());
1697 }
1698 }
1699
1700 MDBuilder MDB(getContext());
1701 Names.push_back(MDB.createString(Name));
1702 MDNode *MD = MDTuple::get(getContext(), Names);
1703 setMetadata(LLVMContext::MD_annotation, MD);
1704}
1705
1707 AAMDNodes Result;
1708 // Not using Instruction::hasMetadata() because we're not interested in
1709 // DebugInfoMetadata.
1710 if (Value::hasMetadata()) {
1711 const MDAttachments &Info = getContext().pImpl->ValueMetadata.at(this);
1712 Result.TBAA = Info.lookup(LLVMContext::MD_tbaa);
1713 Result.TBAAStruct = Info.lookup(LLVMContext::MD_tbaa_struct);
1714 Result.Scope = Info.lookup(LLVMContext::MD_alias_scope);
1715 Result.NoAlias = Info.lookup(LLVMContext::MD_noalias);
1716 }
1717 return Result;
1718}
1719
1721 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1722 setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
1723 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1724 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1725}
1726
1728 setMetadata(llvm::LLVMContext::MD_nosanitize,
1729 llvm::MDNode::get(getContext(), std::nullopt));
1730}
1731
1732void Instruction::getAllMetadataImpl(
1733 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1734 Result.clear();
1735
1736 // Handle 'dbg' as a special case since it is not stored in the hash table.
1737 if (DbgLoc) {
1738 Result.push_back(
1739 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1740 }
1741 Value::getAllMetadata(Result);
1742}
1743
1745 assert(
1746 (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select ||
1747 getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke ||
1748 getOpcode() == Instruction::IndirectBr ||
1749 getOpcode() == Instruction::Switch) &&
1750 "Looking for branch weights on something besides branch");
1751
1752 return ::extractProfTotalWeight(*this, TotalVal);
1753}
1754
1757 Other->getAllMetadata(MDs);
1758 for (auto &MD : MDs) {
1759 // We need to adjust the type metadata offset.
1760 if (Offset != 0 && MD.first == LLVMContext::MD_type) {
1761 auto *OffsetConst = cast<ConstantInt>(
1762 cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1763 Metadata *TypeId = MD.second->getOperand(1);
1764 auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1765 OffsetConst->getType(), OffsetConst->getValue() + Offset));
1766 addMetadata(LLVMContext::MD_type,
1767 *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1768 continue;
1769 }
1770 // If an offset adjustment was specified we need to modify the DIExpression
1771 // to prepend the adjustment:
1772 // !DIExpression(DW_OP_plus, Offset, [original expr])
1773 auto *Attachment = MD.second;
1774 if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
1775 DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1776 DIExpression *E = nullptr;
1777 if (!GV) {
1778 auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1779 GV = GVE->getVariable();
1780 E = GVE->getExpression();
1781 }
1782 ArrayRef<uint64_t> OrigElements;
1783 if (E)
1784 OrigElements = E->getElements();
1785 std::vector<uint64_t> Elements(OrigElements.size() + 2);
1786 Elements[0] = dwarf::DW_OP_plus_uconst;
1787 Elements[1] = Offset;
1788 llvm::copy(OrigElements, Elements.begin() + 2);
1789 E = DIExpression::get(getContext(), Elements);
1790 Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1791 }
1792 addMetadata(MD.first, *Attachment);
1793 }
1794}
1795
1798 LLVMContext::MD_type,
1800 {ConstantAsMetadata::get(ConstantInt::get(
1802 TypeID}));
1803}
1804
1806 // Remove any existing vcall visibility metadata first in case we are
1807 // updating.
1808 eraseMetadata(LLVMContext::MD_vcall_visibility);
1809 addMetadata(LLVMContext::MD_vcall_visibility,
1811 {ConstantAsMetadata::get(ConstantInt::get(
1813}
1814
1816 if (MDNode *MD = getMetadata(LLVMContext::MD_vcall_visibility)) {
1817 uint64_t Val = cast<ConstantInt>(
1818 cast<ConstantAsMetadata>(MD->getOperand(0))->getValue())
1819 ->getZExtValue();
1820 assert(Val <= 2 && "unknown vcall visibility!");
1821 return (VCallVisibility)Val;
1822 }
1824}
1825
1827 setMetadata(LLVMContext::MD_dbg, SP);
1828}
1829
1831 return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1832}
1833
1835 if (DISubprogram *SP = getSubprogram()) {
1836 if (DICompileUnit *CU = SP->getUnit()) {
1837 return CU->getDebugInfoForProfiling();
1838 }
1839 }
1840 return false;
1841}
1842
1844 addMetadata(LLVMContext::MD_dbg, *GV);
1845}
1846
1850 getMetadata(LLVMContext::MD_dbg, MDs);
1851 for (MDNode *MD : MDs)
1852 GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1853}
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:1268
static SmallVector< TrackingMDRef, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:1365
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1245
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:1241
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:1249
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
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:78
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1110
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:61
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1465
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:81
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:155
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:146
This class represents a range of values.
Definition: ConstantRange.h:47
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
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:42
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:1826
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1830
bool shouldEmitDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Definition: Metadata.cpp:1834
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1796
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1487
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1755
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1815
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1532
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1521
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:1805
unsigned Visibility
Definition: GlobalValue.h:99
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1847
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1843
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1720
bool extractProfTotalWeight(uint64_t &TotalVal) const
Retrieve total raw weight values of a branch.
Definition: Metadata.cpp:1744
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:363
void addAnnotationMetadata(StringRef Annotation)
Adds an !annotation metadata node with Annotation to this instruction.
Definition: Metadata.cpp:1688
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:381
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs=std::nullopt)
Drop all unknown metadata except for debug locations.
Definition: Metadata.cpp:1589
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1635
void setNoSanitizeMetadata()
Sets the nosanitize metadata on this instruction.
Definition: Metadata.cpp:1727
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1706
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata that matches the predicate.
Definition: Metadata.cpp:1582
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:1434
void get(unsigned ID, SmallVectorImpl< MDNode * > &Result) const
Appends all attachments with the given ID to Result in insertion order.
Definition: Metadata.cpp:1411
void getAll(SmallVectorImpl< std::pair< unsigned, MDNode * > > &Result) const
Appends all attachments for the global to Result, sorting by attachment ID.
Definition: Metadata.cpp:1417
void set(unsigned ID, MDNode *MD)
Set an attachment to a particular node.
Definition: Metadata.cpp:1428
MDNode * lookup(unsigned ID) const
Returns the first attachment with the given ID or nullptr if no such attachment exists.
Definition: Metadata.cpp:1404
bool erase(unsigned ID)
Remove attachments with the given ID.
Definition: Metadata.cpp:1438
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:1216
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:1278
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:1350
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:283
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:1389
StringRef getName() const
Definition: Metadata.cpp:1398
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:1394
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1381
unsigned getNumOperands() const
Definition: Metadata.cpp:1377
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1396
Module * getParent()
Get the module that holds this named metadata collection.
Definition: Metadata.h:1799
void addOperand(MDNode *M)
Definition: Metadata.cpp:1387
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:1852
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:412
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:344
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:418
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
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
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:1833
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:1487
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:1477
MDNode * getMetadataImpl(unsigned KindID) const
Get metadata for the given kind, if any.
Definition: Metadata.cpp:1461
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1532
unsigned HasMetadata
Definition: Value.h:113
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1521
void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata attachments matching the given predicate.
Definition: Metadata.cpp:1544
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1559
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.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
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:480
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:853
void stable_sort(R &&Range)
Definition: STLExtras.h:1995
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:1742
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:1680
unsigned getBranchWeightOffset(const MDNode *ProfileData)
Return the offset to the first branch weight data.
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:1729
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
@ 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:1824
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:1849
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:1921
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:2051
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
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:601
#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:1450