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