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