LLVM 17.0.0git
Metadata.cpp
Go to the documentation of this file.
1//===- Metadata.cpp - Implement Metadata classes --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Metadata classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Metadata.h"
14#include "LLVMContextImpl.h"
15#include "MetadataImpl.h"
16#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/IR/Argument.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/Constant.h"
32#include "llvm/IR/Constants.h"
34#include "llvm/IR/DebugLoc.h"
35#include "llvm/IR/Function.h"
38#include "llvm/IR/Instruction.h"
39#include "llvm/IR/LLVMContext.h"
40#include "llvm/IR/MDBuilder.h"
41#include "llvm/IR/Module.h"
44#include "llvm/IR/Type.h"
45#include "llvm/IR/Value.h"
49#include <algorithm>
50#include <cassert>
51#include <cstddef>
52#include <cstdint>
53#include <type_traits>
54#include <utility>
55#include <vector>
56
57using namespace llvm;
58
59MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
60 : Value(Ty, MetadataAsValueVal), MD(MD) {
61 track();
62}
63
66 untrack();
67}
68
69/// Canonicalize metadata arguments to intrinsics.
70///
71/// To support bitcode upgrades (and assembly semantic sugar) for \a
72/// MetadataAsValue, we need to canonicalize certain metadata.
73///
74/// - nullptr is replaced by an empty MDNode.
75/// - An MDNode with a single null operand is replaced by an empty MDNode.
76/// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped.
77///
78/// This maintains readability of bitcode from when metadata was a type of
79/// value, and these bridges were unnecessary.
81 Metadata *MD) {
82 if (!MD)
83 // !{}
84 return MDNode::get(Context, std::nullopt);
85
86 // Return early if this isn't a single-operand MDNode.
87 auto *N = dyn_cast<MDNode>(MD);
88 if (!N || N->getNumOperands() != 1)
89 return MD;
90
91 if (!N->getOperand(0))
92 // !{}
93 return MDNode::get(Context, std::nullopt);
94
95 if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
96 // Look through the MDNode.
97 return C;
98
99 return MD;
100}
101
104 auto *&Entry = Context.pImpl->MetadataAsValues[MD];
105 if (!Entry)
107 return Entry;
108}
109
111 Metadata *MD) {
113 auto &Store = Context.pImpl->MetadataAsValues;
114 return Store.lookup(MD);
115}
116
117void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
119 MD = canonicalizeMetadataForValue(Context, MD);
120 auto &Store = Context.pImpl->MetadataAsValues;
121
122 // Stop tracking the old metadata.
123 Store.erase(this->MD);
124 untrack();
125 this->MD = nullptr;
126
127 // Start tracking MD, or RAUW if necessary.
128 auto *&Entry = Store[MD];
129 if (Entry) {
130 replaceAllUsesWith(Entry);
131 delete this;
132 return;
133 }
134
135 this->MD = MD;
136 track();
137 Entry = this;
138}
139
140void MetadataAsValue::track() {
141 if (MD)
142 MetadataTracking::track(&MD, *MD, *this);
143}
144
145void MetadataAsValue::untrack() {
146 if (MD)
148}
149
150bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
151 assert(Ref && "Expected live reference");
152 assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
153 "Reference without owner must be direct");
154 if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) {
155 R->addRef(Ref, Owner);
156 return true;
157 }
158 if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) {
159 assert(!PH->Use && "Placeholders can only be used once");
160 assert(!Owner && "Unexpected callback to owner");
161 PH->Use = static_cast<Metadata **>(Ref);
162 return true;
163 }
164 return false;
165}
166
168 assert(Ref && "Expected live reference");
169 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD))
170 R->dropRef(Ref);
171 else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD))
172 PH->Use = nullptr;
173}
174
175bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
176 assert(Ref && "Expected live reference");
177 assert(New && "Expected live reference");
178 assert(Ref != New && "Expected change");
179 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) {
180 R->moveRef(Ref, New, MD);
181 return true;
182 }
183 assert(!isa<DistinctMDOperandPlaceholder>(MD) &&
184 "Unexpected move of an MDOperand");
185 assert(!isReplaceable(MD) &&
186 "Expected un-replaceable metadata, since we didn't move a reference");
187 return false;
188}
189
191 return ReplaceableMetadataImpl::isReplaceable(MD);
192}
193
196 for (auto Pair : UseMap) {
197 OwnerTy Owner = Pair.second.first;
198 if (!Owner.is<Metadata *>())
199 continue;
200 Metadata *OwnerMD = Owner.get<Metadata *>();
201 if (OwnerMD->getMetadataID() == Metadata::DIArgListKind)
202 MDUsersWithID.push_back(&UseMap[Pair.first]);
203 }
204 llvm::sort(MDUsersWithID, [](auto UserA, auto UserB) {
205 return UserA->second < UserB->second;
206 });
208 for (auto *UserWithID : MDUsersWithID)
209 MDUsers.push_back(UserWithID->first.get<Metadata *>());
210 return MDUsers;
211}
212
213void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
214 bool WasInserted =
215 UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
216 .second;
217 (void)WasInserted;
218 assert(WasInserted && "Expected to add a reference");
219
220 ++NextIndex;
221 assert(NextIndex != 0 && "Unexpected overflow");
222}
223
224void ReplaceableMetadataImpl::dropRef(void *Ref) {
225 bool WasErased = UseMap.erase(Ref);
226 (void)WasErased;
227 assert(WasErased && "Expected to drop a reference");
228}
229
230void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
231 const Metadata &MD) {
232 auto I = UseMap.find(Ref);
233 assert(I != UseMap.end() && "Expected to move a reference");
234 auto OwnerAndIndex = I->second;
235 UseMap.erase(I);
236 bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
237 (void)WasInserted;
238 assert(WasInserted && "Expected to add a reference");
239
240 // Check that the references are direct if there's no owner.
241 (void)MD;
242 assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
243 "Reference without owner must be direct");
244 assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
245 "Reference without owner must be direct");
246}
247
249 if (!C.isUsedByMetadata()) {
250 return;
251 }
252
253 LLVMContext &Context = C.getType()->getContext();
254 auto &Store = Context.pImpl->ValuesAsMetadata;
255 auto I = Store.find(&C);
256 ValueAsMetadata *MD = I->second;
257 using UseTy =
258 std::pair<void *, std::pair<MetadataTracking::OwnerTy, uint64_t>>;
259 // Copy out uses and update value of Constant used by debug info metadata with undef below
260 SmallVector<UseTy, 8> Uses(MD->UseMap.begin(), MD->UseMap.end());
261
262 for (const auto &Pair : Uses) {
263 MetadataTracking::OwnerTy Owner = Pair.second.first;
264 if (!Owner)
265 continue;
266 if (!Owner.is<Metadata *>())
267 continue;
268 auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
269 if (!OwnerMD)
270 continue;
271 if (isa<DINode>(OwnerMD)) {
272 OwnerMD->handleChangedOperand(
273 Pair.first, ValueAsMetadata::get(UndefValue::get(C.getType())));
274 }
275 }
276}
277
279 if (UseMap.empty())
280 return;
281
282 // Copy out uses since UseMap will get touched below.
283 using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
284 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
286 for (const auto &Pair : Uses) {
287 // Check that this Ref hasn't disappeared after RAUW (when updating a
288 // previous Ref).
289 if (!UseMap.count(Pair.first))
290 continue;
291
292 OwnerTy Owner = Pair.second.first;
293 if (!Owner) {
294 // Update unowned tracking references directly.
295 Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
296 Ref = MD;
297 if (MD)
299 UseMap.erase(Pair.first);
300 continue;
301 }
302
303 // Check for MetadataAsValue.
304 if (Owner.is<MetadataAsValue *>()) {
305 Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
306 continue;
307 }
308
309 // There's a Metadata owner -- dispatch.
310 Metadata *OwnerMD = Owner.get<Metadata *>();
311 switch (OwnerMD->getMetadataID()) {
312#define HANDLE_METADATA_LEAF(CLASS) \
313 case Metadata::CLASS##Kind: \
314 cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
315 continue;
316#include "llvm/IR/Metadata.def"
317 default:
318 llvm_unreachable("Invalid metadata subclass");
319 }
320 }
321 assert(UseMap.empty() && "Expected all uses to be replaced");
322}
323
325 if (UseMap.empty())
326 return;
327
328 if (!ResolveUsers) {
329 UseMap.clear();
330 return;
331 }
332
333 // Copy out uses since UseMap could get touched below.
334 using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
335 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
336 llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
337 return L.second.second < R.second.second;
338 });
339 UseMap.clear();
340 for (const auto &Pair : Uses) {
341 auto Owner = Pair.second.first;
342 if (!Owner)
343 continue;
344 if (Owner.is<MetadataAsValue *>())
345 continue;
346
347 // Resolve MDNodes that point at this.
348 auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
349 if (!OwnerMD)
350 continue;
351 if (OwnerMD->isResolved())
352 continue;
353 OwnerMD->decrementUnresolvedOperandCount();
354 }
355}
356
357ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
358 if (auto *N = dyn_cast<MDNode>(&MD))
359 return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses();
360 return dyn_cast<ValueAsMetadata>(&MD);
361}
362
363ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
364 if (auto *N = dyn_cast<MDNode>(&MD))
365 return N->isResolved() ? nullptr : N->Context.getReplaceableUses();
366 return dyn_cast<ValueAsMetadata>(&MD);
367}
368
369bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
370 if (auto *N = dyn_cast<MDNode>(&MD))
371 return !N->isResolved();
372 return isa<ValueAsMetadata>(&MD);
373}
374
376 assert(V && "Expected value");
377 if (auto *A = dyn_cast<Argument>(V)) {
378 if (auto *Fn = A->getParent())
379 return Fn->getSubprogram();
380 return nullptr;
381 }
382
383 if (BasicBlock *BB = cast<Instruction>(V)->getParent()) {
384 if (auto *Fn = BB->getParent())
385 return Fn->getSubprogram();
386 return nullptr;
387 }
388
389 return nullptr;
390}
391
393 assert(V && "Unexpected null Value");
394
395 auto &Context = V->getContext();
396 auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
397 if (!Entry) {
398 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
399 "Expected constant or function-local value");
400 assert(!V->IsUsedByMD && "Expected this to be the only metadata use");
401 V->IsUsedByMD = true;
402 if (auto *C = dyn_cast<Constant>(V))
403 Entry = new ConstantAsMetadata(C);
404 else
405 Entry = new LocalAsMetadata(V);
406 }
407
408 return Entry;
409}
410
412 assert(V && "Unexpected null Value");
413 return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
414}
415
417 assert(V && "Expected valid value");
418
419 auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
420 auto I = Store.find(V);
421 if (I == Store.end())
422 return;
423
424 // Remove old entry from the map.
425 ValueAsMetadata *MD = I->second;
426 assert(MD && "Expected valid metadata");
427 assert(MD->getValue() == V && "Expected valid mapping");
428 Store.erase(I);
429
430 // Delete the metadata.
431 MD->replaceAllUsesWith(nullptr);
432 delete MD;
433}
434
436 assert(From && "Expected valid value");
437 assert(To && "Expected valid value");
438 assert(From != To && "Expected changed value");
439 assert(From->getType() == To->getType() && "Unexpected type change");
440
441 LLVMContext &Context = From->getType()->getContext();
442 auto &Store = Context.pImpl->ValuesAsMetadata;
443 auto I = Store.find(From);
444 if (I == Store.end()) {
445 assert(!From->IsUsedByMD && "Expected From not to be used by metadata");
446 return;
447 }
448
449 // Remove old entry from the map.
450 assert(From->IsUsedByMD && "Expected From to be used by metadata");
451 From->IsUsedByMD = false;
452 ValueAsMetadata *MD = I->second;
453 assert(MD && "Expected valid metadata");
454 assert(MD->getValue() == From && "Expected valid mapping");
455 Store.erase(I);
456
457 if (isa<LocalAsMetadata>(MD)) {
458 if (auto *C = dyn_cast<Constant>(To)) {
459 // Local became a constant.
461 delete MD;
462 return;
463 }
466 // DISubprogram changed.
467 MD->replaceAllUsesWith(nullptr);
468 delete MD;
469 return;
470 }
471 } else if (!isa<Constant>(To)) {
472 // Changed to function-local value.
473 MD->replaceAllUsesWith(nullptr);
474 delete MD;
475 return;
476 }
477
478 auto *&Entry = Store[To];
479 if (Entry) {
480 // The target already exists.
481 MD->replaceAllUsesWith(Entry);
482 delete MD;
483 return;
484 }
485
486 // Update MD in place (and update the map entry).
487 assert(!To->IsUsedByMD && "Expected this to be the only metadata use");
488 To->IsUsedByMD = true;
489 MD->V = To;
490 Entry = MD;
491}
492
493//===----------------------------------------------------------------------===//
494// MDString implementation.
495//
496
498 auto &Store = Context.pImpl->MDStringCache;
499 auto I = Store.try_emplace(Str);
500 auto &MapEntry = I.first->getValue();
501 if (!I.second)
502 return &MapEntry;
503 MapEntry.Entry = &*I.first;
504 return &MapEntry;
505}
506
508 assert(Entry && "Expected to find string map entry");
509 return Entry->first();
510}
511
512//===----------------------------------------------------------------------===//
513// MDNode implementation.
514//
515
516// Assert that the MDNode types will not be unaligned by the objects
517// prepended to them.
518#define HANDLE_MDNODE_LEAF(CLASS) \
519 static_assert( \
520 alignof(uint64_t) >= alignof(CLASS), \
521 "Alignment is insufficient after objects prepended to " #CLASS);
522#include "llvm/IR/Metadata.def"
523
524void *MDNode::operator new(size_t Size, size_t NumOps, StorageType Storage) {
525 // uint64_t is the most aligned type we need support (ensured by static_assert
526 // above)
527 size_t AllocSize =
528 alignTo(Header::getAllocSize(Storage, NumOps), alignof(uint64_t));
529 char *Mem = reinterpret_cast<char *>(::operator new(AllocSize + Size));
530 Header *H = new (Mem + AllocSize - sizeof(Header)) Header(NumOps, Storage);
531 return reinterpret_cast<void *>(H + 1);
532}
533
534void MDNode::operator delete(void *N) {
535 Header *H = reinterpret_cast<Header *>(N) - 1;
536 void *Mem = H->getAllocation();
537 H->~Header();
538 ::operator delete(Mem);
539}
540
541MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
543 : Metadata(ID, Storage), Context(Context) {
544 unsigned Op = 0;
545 for (Metadata *MD : Ops1)
546 setOperand(Op++, MD);
547 for (Metadata *MD : Ops2)
548 setOperand(Op++, MD);
549
550 if (!isUniqued())
551 return;
552
553 // Count the unresolved operands. If there are any, RAUW support will be
554 // added lazily on first reference.
555 countUnresolvedOperands();
556}
557
558TempMDNode MDNode::clone() const {
559 switch (getMetadataID()) {
560 default:
561 llvm_unreachable("Invalid MDNode subclass");
562#define HANDLE_MDNODE_LEAF(CLASS) \
563 case CLASS##Kind: \
564 return cast<CLASS>(this)->cloneImpl();
565#include "llvm/IR/Metadata.def"
567}
568
569MDNode::Header::Header(size_t NumOps, StorageType Storage) {
570 IsLarge = isLarge(NumOps);
571 IsResizable = isResizable(Storage);
572 SmallSize = getSmallSize(NumOps, IsResizable, IsLarge);
573 if (IsLarge) {
574 SmallNumOps = 0;
575 new (getLargePtr()) LargeStorageVector();
576 getLarge().resize(NumOps);
577 return;
578 }
579 SmallNumOps = NumOps;
580 MDOperand *O = reinterpret_cast<MDOperand *>(this) - SmallSize;
581 for (MDOperand *E = O + SmallSize; O != E;)
582 (void)new (O++) MDOperand();
583}
584
585MDNode::Header::~Header() {
586 if (IsLarge) {
587 getLarge().~LargeStorageVector();
588 return;
589 }
590 MDOperand *O = reinterpret_cast<MDOperand *>(this);
591 for (MDOperand *E = O - SmallSize; O != E; --O)
592 (void)(O - 1)->~MDOperand();
593}
594
595void *MDNode::Header::getSmallPtr() {
596 static_assert(alignof(MDOperand) <= alignof(Header),
597 "MDOperand too strongly aligned");
598 return reinterpret_cast<char *>(const_cast<Header *>(this)) -
599 sizeof(MDOperand) * SmallSize;
600}
601
602void MDNode::Header::resize(size_t NumOps) {
603 assert(IsResizable && "Node is not resizable");
604 if (operands().size() == NumOps)
605 return;
606
607 if (IsLarge)
608 getLarge().resize(NumOps);
609 else if (NumOps <= SmallSize)
610 resizeSmall(NumOps);
611 else
612 resizeSmallToLarge(NumOps);
613}
614
615void MDNode::Header::resizeSmall(size_t NumOps) {
616 assert(!IsLarge && "Expected a small MDNode");
617 assert(NumOps <= SmallSize && "NumOps too large for small resize");
619 MutableArrayRef<MDOperand> ExistingOps = operands();
620 assert(NumOps != ExistingOps.size() && "Expected a different size");
621
622 int NumNew = (int)NumOps - (int)ExistingOps.size();
623 MDOperand *O = ExistingOps.end();
624 for (int I = 0, E = NumNew; I < E; ++I)
625 (O++)->reset();
626 for (int I = 0, E = NumNew; I > E; --I)
627 (--O)->reset();
628 SmallNumOps = NumOps;
629 assert(O == operands().end() && "Operands not (un)initialized until the end");
630}
631
632void MDNode::Header::resizeSmallToLarge(size_t NumOps) {
633 assert(!IsLarge && "Expected a small MDNode");
634 assert(NumOps > SmallSize && "Expected NumOps to be larger than allocation");
635 LargeStorageVector NewOps;
636 NewOps.resize(NumOps);
637 llvm::move(operands(), NewOps.begin());
638 resizeSmall(0);
639 new (getLargePtr()) LargeStorageVector(std::move(NewOps));
640 IsLarge = true;
641}
642
644 if (auto *N = dyn_cast_or_null<MDNode>(Op))
645 return !N->isResolved();
646 return false;
647}
648
649void MDNode::countUnresolvedOperands() {
650 assert(getNumUnresolved() == 0 && "Expected unresolved ops to be uncounted");
651 assert(isUniqued() && "Expected this to be uniqued");
653}
654
655void MDNode::makeUniqued() {
656 assert(isTemporary() && "Expected this to be temporary");
657 assert(!isResolved() && "Expected this to be unresolved");
658
659 // Enable uniquing callbacks.
660 for (auto &Op : mutable_operands())
661 Op.reset(Op.get(), this);
662
663 // Make this 'uniqued'.
665 countUnresolvedOperands();
666 if (!getNumUnresolved()) {
667 dropReplaceableUses();
668 assert(isResolved() && "Expected this to be resolved");
669 }
670
671 assert(isUniqued() && "Expected this to be uniqued");
672}
673
674void MDNode::makeDistinct() {
675 assert(isTemporary() && "Expected this to be temporary");
676 assert(!isResolved() && "Expected this to be unresolved");
677
678 // Drop RAUW support and store as a distinct node.
679 dropReplaceableUses();
681
682 assert(isDistinct() && "Expected this to be distinct");
683 assert(isResolved() && "Expected this to be resolved");
684}
685
687 assert(isUniqued() && "Expected this to be uniqued");
688 assert(!isResolved() && "Expected this to be unresolved");
689
691 dropReplaceableUses();
692
693 assert(isResolved() && "Expected this to be resolved");
694}
695
696void MDNode::dropReplaceableUses() {
697 assert(!getNumUnresolved() && "Unexpected unresolved operand");
698
699 // Drop any RAUW support.
700 if (Context.hasReplaceableUses())
701 Context.takeReplaceableUses()->resolveAllUses();
702}
703
704void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
705 assert(isUniqued() && "Expected this to be uniqued");
706 assert(getNumUnresolved() != 0 && "Expected unresolved operands");
707
708 // Check if an operand was resolved.
709 if (!isOperandUnresolved(Old)) {
710 if (isOperandUnresolved(New))
711 // An operand was un-resolved!
713 } else if (!isOperandUnresolved(New))
714 decrementUnresolvedOperandCount();
715}
716
717void MDNode::decrementUnresolvedOperandCount() {
718 assert(!isResolved() && "Expected this to be unresolved");
719 if (isTemporary())
720 return;
721
722 assert(isUniqued() && "Expected this to be uniqued");
724 if (getNumUnresolved())
725 return;
726
727 // Last unresolved operand has just been resolved.
728 dropReplaceableUses();
729 assert(isResolved() && "Expected this to become resolved");
730}
731
733 if (isResolved())
734 return;
735
736 // Resolve this node immediately.
737 resolve();
738
739 // Resolve all operands.
740 for (const auto &Op : operands()) {
741 auto *N = dyn_cast_or_null<MDNode>(Op);
742 if (!N)
743 continue;
744
745 assert(!N->isTemporary() &&
746 "Expected all forward declarations to be resolved");
747 if (!N->isResolved())
748 N->resolveCycles();
749 }
750}
751
752static bool hasSelfReference(MDNode *N) {
753 return llvm::is_contained(N->operands(), N);
754}
755
756MDNode *MDNode::replaceWithPermanentImpl() {
757 switch (getMetadataID()) {
758 default:
759 // If this type isn't uniquable, replace with a distinct node.
760 return replaceWithDistinctImpl();
761
762#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
763 case CLASS##Kind: \
764 break;
765#include "llvm/IR/Metadata.def"
766 }
767
768 // Even if this type is uniquable, self-references have to be distinct.
769 if (hasSelfReference(this))
770 return replaceWithDistinctImpl();
771 return replaceWithUniquedImpl();
772}
773
774MDNode *MDNode::replaceWithUniquedImpl() {
775 // Try to uniquify in place.
776 MDNode *UniquedNode = uniquify();
777
778 if (UniquedNode == this) {
779 makeUniqued();
780 return this;
781 }
782
783 // Collision, so RAUW instead.
784 replaceAllUsesWith(UniquedNode);
785 deleteAsSubclass();
786 return UniquedNode;
787}
788
789MDNode *MDNode::replaceWithDistinctImpl() {
790 makeDistinct();
791 return this;
792}
793
794void MDTuple::recalculateHash() {
795 setHash(MDTupleInfo::KeyTy::calculateHash(this));
796}
797
799 for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
800 setOperand(I, nullptr);
801 if (Context.hasReplaceableUses()) {
802 Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
803 (void)Context.takeReplaceableUses();
804 }
805}
806
807void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
808 unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
809 assert(Op < getNumOperands() && "Expected valid operand");
810
811 if (!isUniqued()) {
812 // This node is not uniqued. Just set the operand and be done with it.
813 setOperand(Op, New);
814 return;
815 }
816
817 // This node is uniqued.
818 eraseFromStore();
819
820 Metadata *Old = getOperand(Op);
821 setOperand(Op, New);
822
823 // Drop uniquing for self-reference cycles and deleted constants.
824 if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
825 if (!isResolved())
826 resolve();
828 return;
829 }
830
831 // Re-unique the node.
832 auto *Uniqued = uniquify();
833 if (Uniqued == this) {
834 if (!isResolved())
835 resolveAfterOperandChange(Old, New);
836 return;
837 }
838
839 // Collision.
840 if (!isResolved()) {
841 // Still unresolved, so RAUW.
842 //
843 // First, clear out all operands to prevent any recursion (similar to
844 // dropAllReferences(), but we still need the use-list).
845 for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
846 setOperand(O, nullptr);
847 if (Context.hasReplaceableUses())
848 Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
849 deleteAsSubclass();
850 return;
851 }
852
853 // Store in non-uniqued form if RAUW isn't possible.
855}
856
857void MDNode::deleteAsSubclass() {
858 switch (getMetadataID()) {
859 default:
860 llvm_unreachable("Invalid subclass of MDNode");
861#define HANDLE_MDNODE_LEAF(CLASS) \
862 case CLASS##Kind: \
863 delete cast<CLASS>(this); \
864 break;
865#include "llvm/IR/Metadata.def"
866 }
867}
868
869template <class T, class InfoT>
871 if (T *U = getUniqued(Store, N))
872 return U;
873
874 Store.insert(N);
875 return N;
876}
877
878template <class NodeTy> struct MDNode::HasCachedHash {
879 using Yes = char[1];
880 using No = char[2];
881 template <class U, U Val> struct SFINAE {};
882
883 template <class U>
884 static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
885 template <class U> static No &check(...);
886
887 static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
888};
889
890MDNode *MDNode::uniquify() {
891 assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
892
893 // Try to insert into uniquing store.
894 switch (getMetadataID()) {
895 default:
896 llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
897#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
898 case CLASS##Kind: { \
899 CLASS *SubclassThis = cast<CLASS>(this); \
900 std::integral_constant<bool, HasCachedHash<CLASS>::value> \
901 ShouldRecalculateHash; \
902 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
903 return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
904 }
905#include "llvm/IR/Metadata.def"
906 }
907}
908
909void MDNode::eraseFromStore() {
910 switch (getMetadataID()) {
911 default:
912 llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
913#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
914 case CLASS##Kind: \
915 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
916 break;
917#include "llvm/IR/Metadata.def"
918 }
919}
920
921MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
922 StorageType Storage, bool ShouldCreate) {
923 unsigned Hash = 0;
924 if (Storage == Uniqued) {
925 MDTupleInfo::KeyTy Key(MDs);
926 if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
927 return N;
928 if (!ShouldCreate)
929 return nullptr;
930 Hash = Key.getHash();
931 } else {
932 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
933 }
934
935 return storeImpl(new (MDs.size(), Storage)
936 MDTuple(Context, Storage, Hash, MDs),
937 Storage, Context.pImpl->MDTuples);
938}
939
941 assert(N->isTemporary() && "Expected temporary node");
942 N->replaceAllUsesWith(nullptr);
943 N->deleteAsSubclass();
944}
945
947 assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses");
948 assert(!getNumUnresolved() && "Unexpected unresolved nodes");
950 assert(isResolved() && "Expected this to be resolved");
951
952 // Reset the hash.
953 switch (getMetadataID()) {
954 default:
955 llvm_unreachable("Invalid subclass of MDNode");
956#define HANDLE_MDNODE_LEAF(CLASS) \
957 case CLASS##Kind: { \
958 std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
959 dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
960 break; \
961 }
962#include "llvm/IR/Metadata.def"
963 }
964
965 getContext().pImpl->DistinctMDNodes.push_back(this);
966}
967
969 if (getOperand(I) == New)
970 return;
971
972 if (!isUniqued()) {
973 setOperand(I, New);
974 return;
975 }
976
977 handleChangedOperand(mutable_begin() + I, New);
978}
979
980void MDNode::setOperand(unsigned I, Metadata *New) {
982 mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
983}
984
985/// Get a node or a self-reference that looks like it.
986///
987/// Special handling for finding self-references, for use by \a
988/// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
989/// when self-referencing nodes were still uniqued. If the first operand has
990/// the same operands as \c Ops, return the first operand instead.
993 if (!Ops.empty())
994 if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
995 if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
996 for (unsigned I = 1, E = Ops.size(); I != E; ++I)
997 if (Ops[I] != N->getOperand(I))
998 return MDNode::get(Context, Ops);
999 return N;
1000 }
1001
1002 return MDNode::get(Context, Ops);
1003}
1004
1006 if (!A)
1007 return B;
1008 if (!B)
1009 return A;
1010
1011 SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
1012 MDs.insert(B->op_begin(), B->op_end());
1013
1014 // FIXME: This preserves long-standing behaviour, but is it really the right
1015 // behaviour? Or was that an unintended side-effect of node uniquing?
1016 return getOrSelfReference(A->getContext(), MDs.getArrayRef());
1017}
1018
1020 if (!A || !B)
1021 return nullptr;
1022
1023 SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
1024 SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
1025 MDs.remove_if([&](Metadata *MD) { return !BSet.count(MD); });
1026
1027 // FIXME: This preserves long-standing behaviour, but is it really the right
1028 // behaviour? Or was that an unintended side-effect of node uniquing?
1029 return getOrSelfReference(A->getContext(), MDs.getArrayRef());
1030}
1031
1033 if (!A || !B)
1034 return nullptr;
1035
1036 // Take the intersection of domains then union the scopes
1037 // within those domains
1039 SmallPtrSet<const MDNode *, 16> IntersectDomains;
1041 for (const MDOperand &MDOp : A->operands())
1042 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
1043 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
1044 ADomains.insert(Domain);
1045
1046 for (const MDOperand &MDOp : B->operands())
1047 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
1048 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
1049 if (ADomains.contains(Domain)) {
1050 IntersectDomains.insert(Domain);
1051 MDs.insert(MDOp);
1052 }
1053
1054 for (const MDOperand &MDOp : A->operands())
1055 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
1056 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
1057 if (IntersectDomains.contains(Domain))
1058 MDs.insert(MDOp);
1059
1060 return MDs.empty() ? nullptr
1061 : getOrSelfReference(A->getContext(), MDs.getArrayRef());
1062}
1063
1065 if (!A || !B)
1066 return nullptr;
1067
1068 APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
1069 APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
1070 if (AVal < BVal)
1071 return A;
1072 return B;
1073}
1074
1075static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1076 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1077}
1078
1079static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
1080 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
1081}
1082
1085 ConstantRange NewRange(Low->getValue(), High->getValue());
1086 unsigned Size = EndPoints.size();
1087 APInt LB = EndPoints[Size - 2]->getValue();
1088 APInt LE = EndPoints[Size - 1]->getValue();
1089 ConstantRange LastRange(LB, LE);
1090 if (canBeMerged(NewRange, LastRange)) {
1091 ConstantRange Union = LastRange.unionWith(NewRange);
1092 Type *Ty = High->getType();
1093 EndPoints[Size - 2] =
1094 cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
1095 EndPoints[Size - 1] =
1096 cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
1097 return true;
1098 }
1099 return false;
1100}
1101
1104 if (!EndPoints.empty())
1105 if (tryMergeRange(EndPoints, Low, High))
1106 return;
1107
1108 EndPoints.push_back(Low);
1109 EndPoints.push_back(High);
1110}
1111
1113 // Given two ranges, we want to compute the union of the ranges. This
1114 // is slightly complicated by having to combine the intervals and merge
1115 // the ones that overlap.
1116
1117 if (!A || !B)
1118 return nullptr;
1119
1120 if (A == B)
1121 return A;
1122
1123 // First, walk both lists in order of the lower boundary of each interval.
1124 // At each step, try to merge the new interval to the last one we adedd.
1126 int AI = 0;
1127 int BI = 0;
1128 int AN = A->getNumOperands() / 2;
1129 int BN = B->getNumOperands() / 2;
1130 while (AI < AN && BI < BN) {
1131 ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
1132 ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
1133
1134 if (ALow->getValue().slt(BLow->getValue())) {
1135 addRange(EndPoints, ALow,
1136 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1137 ++AI;
1138 } else {
1139 addRange(EndPoints, BLow,
1140 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1141 ++BI;
1142 }
1143 }
1144 while (AI < AN) {
1145 addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1146 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1147 ++AI;
1148 }
1149 while (BI < BN) {
1150 addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1151 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1152 ++BI;
1153 }
1154
1155 // If we have more than 2 ranges (4 endpoints) we have to try to merge
1156 // the last and first ones.
1157 unsigned Size = EndPoints.size();
1158 if (Size > 4) {
1159 ConstantInt *FB = EndPoints[0];
1160 ConstantInt *FE = EndPoints[1];
1161 if (tryMergeRange(EndPoints, FB, FE)) {
1162 for (unsigned i = 0; i < Size - 2; ++i) {
1163 EndPoints[i] = EndPoints[i + 2];
1164 }
1165 EndPoints.resize(Size - 2);
1166 }
1167 }
1168
1169 // If in the end we have a single range, it is possible that it is now the
1170 // full range. Just drop the metadata in that case.
1171 if (EndPoints.size() == 2) {
1172 ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1173 if (Range.isFullSet())
1174 return nullptr;
1175 }
1176
1178 MDs.reserve(EndPoints.size());
1179 for (auto *I : EndPoints)
1181 return MDNode::get(A->getContext(), MDs);
1182}
1183
1185 if (!A || !B)
1186 return nullptr;
1187
1188 ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1189 ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1190 if (AVal->getZExtValue() < BVal->getZExtValue())
1191 return A;
1192 return B;
1193}
1194
1195//===----------------------------------------------------------------------===//
1196// NamedMDNode implementation.
1197//
1198
1201}
1202
1203NamedMDNode::NamedMDNode(const Twine &N)
1204 : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1205
1208 delete &getNMDOps(Operands);
1209}
1210
1212 return (unsigned)getNMDOps(Operands).size();
1213}
1214
1216 assert(i < getNumOperands() && "Invalid Operand number!");
1217 auto *N = getNMDOps(Operands)[i].get();
1218 return cast_or_null<MDNode>(N);
1219}
1220
1221void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1222
1223void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1224 assert(I < getNumOperands() && "Invalid operand number");
1225 getNMDOps(Operands)[I].reset(New);
1226}
1227
1229
1230void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1231
1233
1234//===----------------------------------------------------------------------===//
1235// Instruction Metadata method implementations.
1236//
1237
1239 for (const auto &A : Attachments)
1240 if (A.MDKind == ID)
1241 return A.Node;
1242 return nullptr;
1243}
1244
1245void MDAttachments::get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const {
1246 for (const auto &A : Attachments)
1247 if (A.MDKind == ID)
1248 Result.push_back(A.Node);
1249}
1250
1252 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1253 for (const auto &A : Attachments)
1254 Result.emplace_back(A.MDKind, A.Node);
1255
1256 // Sort the resulting array so it is stable with respect to metadata IDs. We
1257 // need to preserve the original insertion order though.
1258 if (Result.size() > 1)
1259 llvm::stable_sort(Result, less_first());
1260}
1261
1262void MDAttachments::set(unsigned ID, MDNode *MD) {
1263 erase(ID);
1264 if (MD)
1265 insert(ID, *MD);
1266}
1267
1268void MDAttachments::insert(unsigned ID, MDNode &MD) {
1269 Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1270}
1271
1272bool MDAttachments::erase(unsigned ID) {
1273 if (empty())
1274 return false;
1275
1276 // Common case is one value.
1277 if (Attachments.size() == 1 && Attachments.back().MDKind == ID) {
1278 Attachments.pop_back();
1279 return true;
1280 }
1281
1282 auto OldSize = Attachments.size();
1283 llvm::erase_if(Attachments,
1284 [ID](const Attachment &A) { return A.MDKind == ID; });
1285 return OldSize != Attachments.size();
1286}
1287
1288MDNode *Value::getMetadata(unsigned KindID) const {
1289 if (!hasMetadata())
1290 return nullptr;
1291 const auto &Info = getContext().pImpl->ValueMetadata[this];
1292 assert(!Info.empty() && "bit out of sync with hash table");
1293 return Info.lookup(KindID);
1294}
1295
1297 if (!hasMetadata())
1298 return nullptr;
1299 const auto &Info = getContext().pImpl->ValueMetadata[this];
1300 assert(!Info.empty() && "bit out of sync with hash table");
1301 return Info.lookup(getContext().getMDKindID(Kind));
1302}
1303
1304void Value::getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const {
1305 if (hasMetadata())
1306 getContext().pImpl->ValueMetadata[this].get(KindID, MDs);
1307}
1308
1310 if (hasMetadata())
1311 getMetadata(getContext().getMDKindID(Kind), MDs);
1312}
1313
1315 SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1316 if (hasMetadata()) {
1317 assert(getContext().pImpl->ValueMetadata.count(this) &&
1318 "bit out of sync with hash table");
1319 const auto &Info = getContext().pImpl->ValueMetadata.find(this)->second;
1320 assert(!Info.empty() && "Shouldn't have called this");
1321 Info.getAll(MDs);
1322 }
1323}
1324
1325void Value::setMetadata(unsigned KindID, MDNode *Node) {
1326 assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1327
1328 // Handle the case when we're adding/updating metadata on a value.
1329 if (Node) {
1330 auto &Info = getContext().pImpl->ValueMetadata[this];
1331 assert(!Info.empty() == HasMetadata && "bit out of sync with hash table");
1332 if (Info.empty())
1333 HasMetadata = true;
1334 Info.set(KindID, Node);
1335 return;
1336 }
1337
1338 // Otherwise, we're removing metadata from an instruction.
1339 assert((HasMetadata == (getContext().pImpl->ValueMetadata.count(this) > 0)) &&
1340 "bit out of sync with hash table");
1341 if (!HasMetadata)
1342 return; // Nothing to remove!
1343 auto &Info = getContext().pImpl->ValueMetadata[this];
1344
1345 // Handle removal of an existing value.
1346 Info.erase(KindID);
1347 if (!Info.empty())
1348 return;
1349 getContext().pImpl->ValueMetadata.erase(this);
1350 HasMetadata = false;
1351}
1352
1354 if (!Node && !HasMetadata)
1355 return;
1356 setMetadata(getContext().getMDKindID(Kind), Node);
1357}
1358
1359void Value::addMetadata(unsigned KindID, MDNode &MD) {
1360 assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1361 if (!HasMetadata)
1362 HasMetadata = true;
1363 getContext().pImpl->ValueMetadata[this].insert(KindID, MD);
1364}
1365
1367 addMetadata(getContext().getMDKindID(Kind), MD);
1368}
1369
1370bool Value::eraseMetadata(unsigned KindID) {
1371 // Nothing to unset.
1372 if (!HasMetadata)
1373 return false;
1374
1375 auto &Store = getContext().pImpl->ValueMetadata[this];
1376 bool Changed = Store.erase(KindID);
1377 if (Store.empty())
1378 clearMetadata();
1379 return Changed;
1380}
1381
1383 if (!HasMetadata)
1384 return;
1385 assert(getContext().pImpl->ValueMetadata.count(this) &&
1386 "bit out of sync with hash table");
1387 getContext().pImpl->ValueMetadata.erase(this);
1388 HasMetadata = false;
1389}
1390
1392 if (!Node && !hasMetadata())
1393 return;
1394 setMetadata(getContext().getMDKindID(Kind), Node);
1395}
1396
1397MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1398 return getMetadataImpl(getContext().getMDKindID(Kind));
1399}
1400
1402 if (!Value::hasMetadata())
1403 return; // Nothing to remove!
1404
1405 SmallSet<unsigned, 4> KnownSet;
1406 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1407
1408 // A DIAssignID attachment is debug metadata, don't drop it.
1409 KnownSet.insert(LLVMContext::MD_DIAssignID);
1410
1411 auto &MetadataStore = getContext().pImpl->ValueMetadata;
1412 auto &Info = MetadataStore[this];
1413 assert(!Info.empty() && "bit out of sync with hash table");
1414 Info.remove_if([&KnownSet](const MDAttachments::Attachment &I) {
1415 return !KnownSet.count(I.MDKind);
1416 });
1417
1418 if (Info.empty()) {
1419 // Drop our entry at the store.
1420 clearMetadata();
1421 }
1422}
1423
1424void Instruction::updateDIAssignIDMapping(DIAssignID *ID) {
1425 auto &IDToInstrs = getContext().pImpl->AssignmentIDToInstrs;
1426 if (const DIAssignID *CurrentID =
1427 cast_or_null<DIAssignID>(getMetadata(LLVMContext::MD_DIAssignID))) {
1428 // Nothing to do if the ID isn't changing.
1429 if (ID == CurrentID)
1430 return;
1431
1432 // Unmap this instruction from its current ID.
1433 auto InstrsIt = IDToInstrs.find(CurrentID);
1434 assert(InstrsIt != IDToInstrs.end() &&
1435 "Expect existing attachment to be mapped");
1436
1437 auto &InstVec = InstrsIt->second;
1438 auto *InstIt = std::find(InstVec.begin(), InstVec.end(), this);
1439 assert(InstIt != InstVec.end() &&
1440 "Expect instruction to be mapped to attachment");
1441 // The vector contains a ptr to this. If this is the only element in the
1442 // vector, remove the ID:vector entry, otherwise just remove the
1443 // instruction from the vector.
1444 if (InstVec.size() == 1)
1445 IDToInstrs.erase(InstrsIt);
1446 else
1447 InstVec.erase(InstIt);
1448 }
1449
1450 // Map this instruction to the new ID.
1451 if (ID)
1452 IDToInstrs[ID].push_back(this);
1453}
1454
1455void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1456 if (!Node && !hasMetadata())
1457 return;
1458
1459 // Handle 'dbg' as a special case since it is not stored in the hash table.
1460 if (KindID == LLVMContext::MD_dbg) {
1461 DbgLoc = DebugLoc(Node);
1462 return;
1463 }
1464
1465 // Update DIAssignID to Instruction(s) mapping.
1466 if (KindID == LLVMContext::MD_DIAssignID) {
1467 // The DIAssignID tracking infrastructure doesn't support RAUWing temporary
1468 // nodes with DIAssignIDs. The cast_or_null below would also catch this, but
1469 // having a dedicated assert helps make this obvious.
1470 assert((!Node || !Node->isTemporary()) &&
1471 "Temporary DIAssignIDs are invalid");
1472 updateDIAssignIDMapping(cast_or_null<DIAssignID>(Node));
1473 }
1474
1475 Value::setMetadata(KindID, Node);
1476}
1477
1479 MDBuilder MDB(getContext());
1480
1481 auto *Existing = getMetadata(LLVMContext::MD_annotation);
1483 bool AppendName = true;
1484 if (Existing) {
1485 auto *Tuple = cast<MDTuple>(Existing);
1486 for (auto &N : Tuple->operands()) {
1487 if (cast<MDString>(N.get())->getString() == Name)
1488 AppendName = false;
1489 Names.push_back(N.get());
1490 }
1491 }
1492 if (AppendName)
1493 Names.push_back(MDB.createString(Name));
1494
1495 MDNode *MD = MDTuple::get(getContext(), Names);
1496 setMetadata(LLVMContext::MD_annotation, MD);
1497}
1498
1500 AAMDNodes Result;
1501 // Not using Instruction::hasMetadata() because we're not interested in
1502 // DebugInfoMetadata.
1503 if (Value::hasMetadata()) {
1504 const auto &Info = getContext().pImpl->ValueMetadata[this];
1505 Result.TBAA = Info.lookup(LLVMContext::MD_tbaa);
1506 Result.TBAAStruct = Info.lookup(LLVMContext::MD_tbaa_struct);
1507 Result.Scope = Info.lookup(LLVMContext::MD_alias_scope);
1508 Result.NoAlias = Info.lookup(LLVMContext::MD_noalias);
1509 }
1510 return Result;
1511}
1512
1514 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1515 setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
1516 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1517 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1518}
1519
1520MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
1521 // Handle 'dbg' as a special case since it is not stored in the hash table.
1522 if (KindID == LLVMContext::MD_dbg)
1523 return DbgLoc.getAsMDNode();
1524 return Value::getMetadata(KindID);
1525}
1526
1527void Instruction::getAllMetadataImpl(
1528 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1529 Result.clear();
1530
1531 // Handle 'dbg' as a special case since it is not stored in the hash table.
1532 if (DbgLoc) {
1533 Result.push_back(
1534 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1535 }
1536 Value::getAllMetadata(Result);
1537}
1538
1540 assert(
1541 (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select ||
1542 getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke ||
1543 getOpcode() == Instruction::IndirectBr ||
1544 getOpcode() == Instruction::Switch) &&
1545 "Looking for branch weights on something besides branch");
1546
1547 return ::extractProfTotalWeight(*this, TotalVal);
1548}
1549
1552 Other->getAllMetadata(MDs);
1553 for (auto &MD : MDs) {
1554 // We need to adjust the type metadata offset.
1555 if (Offset != 0 && MD.first == LLVMContext::MD_type) {
1556 auto *OffsetConst = cast<ConstantInt>(
1557 cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1558 Metadata *TypeId = MD.second->getOperand(1);
1559 auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1560 OffsetConst->getType(), OffsetConst->getValue() + Offset));
1561 addMetadata(LLVMContext::MD_type,
1562 *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1563 continue;
1564 }
1565 // If an offset adjustment was specified we need to modify the DIExpression
1566 // to prepend the adjustment:
1567 // !DIExpression(DW_OP_plus, Offset, [original expr])
1568 auto *Attachment = MD.second;
1569 if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
1570 DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1571 DIExpression *E = nullptr;
1572 if (!GV) {
1573 auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1574 GV = GVE->getVariable();
1575 E = GVE->getExpression();
1576 }
1577 ArrayRef<uint64_t> OrigElements;
1578 if (E)
1579 OrigElements = E->getElements();
1580 std::vector<uint64_t> Elements(OrigElements.size() + 2);
1581 Elements[0] = dwarf::DW_OP_plus_uconst;
1582 Elements[1] = Offset;
1583 llvm::copy(OrigElements, Elements.begin() + 2);
1584 E = DIExpression::get(getContext(), Elements);
1585 Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1586 }
1587 addMetadata(MD.first, *Attachment);
1588 }
1589}
1590
1593 LLVMContext::MD_type,
1597 TypeID}));
1598}
1599
1601 // Remove any existing vcall visibility metadata first in case we are
1602 // updating.
1603 eraseMetadata(LLVMContext::MD_vcall_visibility);
1604 addMetadata(LLVMContext::MD_vcall_visibility,
1608}
1609
1611 if (MDNode *MD = getMetadata(LLVMContext::MD_vcall_visibility)) {
1612 uint64_t Val = cast<ConstantInt>(
1613 cast<ConstantAsMetadata>(MD->getOperand(0))->getValue())
1614 ->getZExtValue();
1615 assert(Val <= 2 && "unknown vcall visibility!");
1616 return (VCallVisibility)Val;
1617 }
1619}
1620
1622 setMetadata(LLVMContext::MD_dbg, SP);
1623}
1624
1626 return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1627}
1628
1630 if (DISubprogram *SP = getSubprogram()) {
1631 if (DICompileUnit *CU = SP->getUnit()) {
1632 return CU->getDebugInfoForProfiling();
1633 }
1634 }
1635 return false;
1636}
1637
1639 addMetadata(LLVMContext::MD_dbg, *GV);
1640}
1641
1645 getMetadata(LLVMContext::MD_dbg, MDs);
1646 for (MDNode *MD : MDs)
1647 GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1648}
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...
SmallPtrSet< MachineInstr *, 2 > Uses
static const Function * getParent(const Value *V)
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Domain getDomain(const ConstantRange &CR)
Given that RA is a live value
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
#define H(x, y, z)
Definition: MD5.cpp:57
mir Rename Register Operands
static DISubprogram * getLocalFunctionMetadata(Value *V)
Definition: Metadata.cpp:375
static Metadata * canonicalizeMetadataForValue(LLVMContext &Context, Metadata *MD)
Canonicalize metadata arguments to intrinsics.
Definition: Metadata.cpp:80
static bool isOperandUnresolved(Metadata *Op)
Definition: Metadata.cpp:643
static bool hasSelfReference(MDNode *N)
Definition: Metadata.cpp:752
static void addRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1102
static SmallVector< TrackingMDRef, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:1199
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1079
static T * uniquifyImpl(T *N, DenseSet< T *, InfoT > &Store)
Definition: Metadata.cpp:870
static bool isContiguous(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1075
static MDNode * getOrSelfReference(LLVMContext &Context, ArrayRef< Metadata * > Ops)
Get a node or a self-reference that looks like it.
Definition: Metadata.cpp:991
static bool tryMergeRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1083
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
uint64_t High
LLVMContext & Context
return ToRemove size() > 0
This file contains the declarations for profiling metadata utility functions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
Class for arbitrary precision integers.
Definition: APInt.h:75
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1108
This is a simple wrapper around an MDNode which provides a higher-level interface by hiding the detai...
Definition: Metadata.h:1423
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:152
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
iterator begin() const
Definition: ArrayRef.h:151
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:158
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:419
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:887
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:141
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:132
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
This is an important base class in LLVM.
Definition: Constant.h:41
Assignment ID.
DWARF expression.
A pair of DIGlobalVariable and DIExpression.
Subprogram description.
A debug info location.
Definition: DebugLoc.h:33
MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition: DebugLoc.h:106
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
Definition: Metadata.cpp:1621
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1625
bool shouldEmitDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Definition: Metadata.cpp:1629
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1591
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1325
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1288
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1550
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1610
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1370
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1359
void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1600
unsigned Visibility
Definition: GlobalValue.h:95
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1642
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1638
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1513
bool extractProfTotalWeight(uint64_t &TotalVal) const
Retrieve total raw weight values of a branch.
Definition: Metadata.cpp:1539
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:257
void addAnnotationMetadata(StringRef Annotation)
Adds an !annotation metadata node with Annotation to this instruction.
Definition: Metadata.cpp:1478
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:275
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1455
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1499
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:168
void dropUnknownNonDebugMetadata()
Definition: Instruction.h:326
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
StringMap< MDString, BumpPtrAllocator > MDStringCache
DenseMap< DIAssignID *, SmallVector< Instruction *, 1 > > AssignmentIDToInstrs
Map DIAssignID -> Instructions with that attachment.
std::vector< MDNode * > DistinctMDNodes
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:69
void insert(unsigned ID, MDNode &MD)
Adds an attachment to a particular node.
Definition: Metadata.cpp:1268
void get(unsigned ID, SmallVectorImpl< MDNode * > &Result) const
Appends all attachments with the given ID to Result in insertion order.
Definition: Metadata.cpp:1245
void getAll(SmallVectorImpl< std::pair< unsigned, MDNode * > > &Result) const
Appends all attachments for the global to Result, sorting by attachment ID.
Definition: Metadata.cpp:1251
void set(unsigned ID, MDNode *MD)
Set an attachment to a particular node.
Definition: Metadata.cpp:1262
MDNode * lookup(unsigned ID) const
Returns the first attachment with the given ID or nullptr if no such attachment exists.
Definition: Metadata.cpp:1238
bool erase(unsigned ID)
Remove attachments with the given ID.
Definition: Metadata.cpp:1272
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
Metadata node.
Definition: Metadata.h:943
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1032
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:968
void resolveCycles()
Resolve cycles.
Definition: Metadata.cpp:732
mutable_op_range mutable_operands()
Definition: Metadata.h:1081
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition: Metadata.h:1132
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
Definition: Metadata.cpp:1005
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Definition: Metadata.cpp:940
void resolve()
Resolve a unique, unresolved node.
Definition: Metadata.cpp:686
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1291
void storeDistinctInContext()
Definition: Metadata.cpp:946
bool isTemporary() const
Definition: Metadata.h:1127
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1289
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1399
bool isUniqued() const
Definition: Metadata.h:1125
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1064
void setNumUnresolved(unsigned N)
Definition: Metadata.h:1219
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1297
MDOperand * mutable_begin()
Definition: Metadata.h:1076
TempMDNode clone() const
Create a (temporary) clone of this.
Definition: Metadata.cpp:558
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1112
bool isDistinct() const
Definition: Metadata.h:1126
void setOperand(unsigned I, Metadata *New)
Set an operand.
Definition: Metadata.cpp:980
MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2=std::nullopt)
Definition: Metadata.cpp:541
bool isResolved() const
Check if node is fully resolved.
Definition: Metadata.h:1123
op_iterator op_begin() const
Definition: Metadata.h:1281
static MDNode * intersect(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1019
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
Definition: MetadataImpl.h:42
LLVMContext & getContext() const
Definition: Metadata.h:1107
void dropAllReferences()
Definition: Metadata.cpp:798
static MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1184
unsigned getNumUnresolved() const
Definition: Metadata.h:1217
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:772
void reset()
Definition: Metadata.h:799
A single uniqued string.
Definition: Metadata.h:611
StringRef getString() const
Definition: Metadata.cpp:507
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:497
Tuple of metadata.
Definition: Metadata.h:1328
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1356
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:175
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:102
static MetadataAsValue * getIfExists(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:110
static bool isReplaceable(const Metadata &MD)
Check whether metadata is replaceable.
Definition: Metadata.cpp:190
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Definition: Metadata.h:247
static bool retrack(Metadata *&MD, Metadata *&New)
Move tracking from one reference to another.
Definition: Metadata.h:258
static bool track(Metadata *&MD)
Track the reference to metadata.
Definition: Metadata.h:222
Root of the metadata hierarchy.
Definition: Metadata.h:61
StorageType
Active type of storage.
Definition: Metadata.h:69
unsigned char Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition: Metadata.h:72
unsigned getMetadataID() const
Definition: Metadata.h:101
void eraseNamedMetadata(NamedMDNode *NMD)
Remove the given NamedMDNode from this module and delete it.
Definition: Module.cpp:272
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:305
iterator end() const
Definition: ArrayRef.h:355
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:1223
StringRef getName() const
Definition: Metadata.cpp:1232
void dropAllReferences()
Remove all uses and clear node vector.
Definition: Metadata.h:1652
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1228
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1215
unsigned getNumOperands() const
Definition: Metadata.cpp:1211
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1230
Module * getParent()
Get the module that holds this named metadata collection.
Definition: Metadata.h:1657
void addOperand(MDNode *M)
Definition: Metadata.cpp:1221
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:155
bool is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:150
Shared implementation of use-lists for replaceable metadata.
Definition: Metadata.h:280
static void SalvageDebugInfo(const Constant &C)
Replace all uses of the constant with Undef in debug info metadata.
Definition: Metadata.cpp:248
void replaceAllUsesWith(Metadata *MD)
Replace all uses of this with MD.
Definition: Metadata.cpp:278
void resolveAllUses(bool ResolveUsers=true)
Resolve all uses of this.
Definition: Metadata.cpp:324
SmallVector< Metadata * > getAllArgListUsers()
Returns the list of all DIArgList users of this.
Definition: Metadata.cpp:194
bool remove_if(UnaryPredicate P)
Remove items from the set vector based on a predicate function.
Definition: SetVector.h:192
ArrayRef< T > getArrayRef() const
Definition: SetVector.h:63
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:72
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:389
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:301
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:164
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:177
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void reserve(size_type N)
Definition: SmallVector.h:667
void resize(size_type N)
Definition: SmallVector.h:642
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Tracking metadata reference.
Definition: TrackingMDRef.h:25
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getMetadataTy(LLVMContext &C)
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
static IntegerType * getInt64Ty(LLVMContext &C)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1740
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:344
void replaceAllUsesWith(Metadata *MD)
Handle collisions after Value::replaceAllUsesWith().
Definition: Metadata.h:401
static void handleDeletion(Value *V)
Definition: Metadata.cpp:416
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:392
static ValueAsMetadata * getIfExists(Value *V)
Definition: Metadata.cpp:411
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:435
Value * getValue() const
Definition: Metadata.h:384
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
unsigned IsUsedByMD
Definition: Value.h:117
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition: Value.h:585
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1325
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1314
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1288
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1370
unsigned HasMetadata
Definition: Value.h:119
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1359
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:994
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1382
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Key
PAL metadata keys.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
@ Offset
Definition: DWP.cpp:406
void stable_sort(R &&Range)
Definition: STLExtras.h:1948
static T * getUniqued(DenseSet< T *, InfoT > &Store, const typename InfoT::KeyTy &Key)
Definition: MetadataImpl.h:22
TypedTrackingMDRef< MDNode > TrackingMDNodeRef
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1683
@ Ref
The access may reference the value stored in memory.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1837
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:1862
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:1903
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:1998
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1869
#define N
static Yes & check(SFINAE< void(U::*)(unsigned), &U::setHash > *)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:651
Function object to check whether the first component of a std::pair compares less than the first comp...
Definition: STLExtras.h:1477
Function object to check whether the second component of a std::pair compares less than the second co...
Definition: STLExtras.h:1485