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