llvm.org GIT mirror llvm / 343bb3e
IR: Prepare for a new UniquableMDNode subclass, NFC Add generic dispatch for the parts of `UniquableMDNode` that cast to `MDTuple`. This makes adding other subclasses (like PR21433's `MDLocation`) easier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225697 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
4 changed file(s) with 81 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
1111 //===----------------------------------------------------------------------===//
1212
1313 #if !(defined HANDLE_METADATA || defined HANDLE_METADATA_LEAF || \
14 defined HANDLE_METADATA_BRANCH)
14 defined HANDLE_METADATA_BRANCH || defined HANDLE_UNIQUABLE_LEAF || \
15 defined HANDLE_UNIQUABLE_BRANCH)
1516 #error "Missing macro definition of HANDLE_METADATA*"
1617 #endif
1718
3031 #define HANDLE_METADATA_BRANCH(CLASS) HANDLE_METADATA(CLASS)
3132 #endif
3233
34 // Handler for leaf nodes under UniquableMDNode.
35 #ifndef HANDLE_UNIQUABLE_LEAF
36 #define HANDLE_UNIQUABLE_LEAF(CLASS) HANDLE_METADATA_LEAF(CLASS)
37 #endif
38
39 // Handler for non-leaf nodes under UniquableMDNode.
40 #ifndef HANDLE_UNIQUABLE_BRANCH
41 #define HANDLE_UNIQUABLE_BRANCH(CLASS) HANDLE_METADATA_BRANCH(CLASS)
42 #endif
43
3344 HANDLE_METADATA_LEAF(MDString)
3445 HANDLE_METADATA_BRANCH(ValueAsMetadata)
3546 HANDLE_METADATA_LEAF(ConstantAsMetadata)
3647 HANDLE_METADATA_LEAF(LocalAsMetadata)
3748 HANDLE_METADATA_BRANCH(MDNode)
3849 HANDLE_METADATA_LEAF(MDNodeFwdDecl)
39 HANDLE_METADATA_BRANCH(UniquableMDNode)
40 HANDLE_METADATA_LEAF(MDTuple)
50 HANDLE_UNIQUABLE_BRANCH(UniquableMDNode)
51 HANDLE_UNIQUABLE_LEAF(MDTuple)
4152
4253 #undef HANDLE_METADATA
4354 #undef HANDLE_METADATA_LEAF
4455 #undef HANDLE_METADATA_BRANCH
56 #undef HANDLE_UNIQUABLE_LEAF
57 #undef HANDLE_UNIQUABLE_BRANCH
754754 void resolve();
755755 void resolveAfterOperandChange(Metadata *Old, Metadata *New);
756756 void decrementUnresolvedOperandCount();
757
758 void deleteAsSubclass();
759 UniquableMDNode *uniquify();
760 void eraseFromStore();
757761 };
758762
759763 /// \brief Tuple of metadata.
793797 static bool classof(const Metadata *MD) {
794798 return MD->getMetadataID() == MDTupleKind;
795799 }
800
801 private:
802 MDTuple *uniquifyImpl();
803 void eraseFromStoreImpl();
796804 };
797805
798806 MDNode *MDNode::get(LLVMContext &Context, ArrayRef MDs) {
141141 I->dropAllReferences();
142142
143143 for (UniquableMDNode *I : DistinctMDNodes)
144 delete cast(I);
144 I->deleteAsSubclass();
145145 for (MDTuple *I : MDTuples)
146146 delete I;
147147
524524 return;
525525 }
526526
527 auto &Store = getContext().pImpl->MDTuples;
528 Store.erase(cast(this));
527 // This node is uniqued.
528 eraseFromStore();
529529
530530 Metadata *Old = getOperand(Op);
531531 setOperand(Op, New);
539539 }
540540
541541 // Re-unique the node.
542 cast(this)->recalculateHash();
543 MDTupleInfo::KeyTy Key(cast(this));
544 auto I = Store.find_as(Key);
545 if (I == Store.end()) {
546 Store.insert(cast(this));
547
542 auto *Uniqued = uniquify();
543 if (Uniqued == this) {
548544 if (!isResolved())
549545 resolveAfterOperandChange(Old, New);
550
551546 return;
552547 }
553548
559554 // dropAllReferences(), but we still need the use-list).
560555 for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
561556 setOperand(O, nullptr);
562 ReplaceableUses->replaceAllUsesWith(*I);
563 delete cast(this);
557 ReplaceableUses->replaceAllUsesWith(Uniqued);
558 deleteAsSubclass();
564559 return;
565560 }
566561
567562 // Store in non-uniqued form if RAUW isn't possible.
568563 storeDistinctInContext();
564 }
565
566 void UniquableMDNode::deleteAsSubclass() {
567 switch (getMetadataID()) {
568 default:
569 llvm_unreachable("Invalid subclass of UniquableMDNode");
570 #define HANDLE_UNIQUABLE_LEAF(CLASS) \
571 case CLASS##Kind: \
572 delete cast(this); \
573 break;
574 #include "llvm/IR/Metadata.def"
575 }
576 }
577
578 UniquableMDNode *UniquableMDNode::uniquify() {
579 switch (getMetadataID()) {
580 default:
581 llvm_unreachable("Invalid subclass of UniquableMDNode");
582 #define HANDLE_UNIQUABLE_LEAF(CLASS) \
583 case CLASS##Kind: \
584 return cast(this)->uniquifyImpl();
585 #include "llvm/IR/Metadata.def"
586 }
587 }
588
589 void UniquableMDNode::eraseFromStore() {
590 switch (getMetadataID()) {
591 default:
592 llvm_unreachable("Invalid subclass of UniquableMDNode");
593 #define HANDLE_UNIQUABLE_LEAF(CLASS) \
594 case CLASS##Kind: \
595 cast(this)->eraseFromStoreImpl(); \
596 break;
597 #include "llvm/IR/Metadata.def"
598 }
569599 }
570600
571601 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef MDs,
591621 N->storeDistinctInContext();
592622 return N;
593623 }
624
625 MDTuple *MDTuple::uniquifyImpl() {
626 recalculateHash();
627 MDTupleInfo::KeyTy Key(this);
628
629 auto &Store = getContext().pImpl->MDTuples;
630 auto I = Store.find_as(Key);
631 if (I == Store.end()) {
632 Store.insert(this);
633 return this;
634 }
635 return *I;
636 }
637
638 void MDTuple::eraseFromStoreImpl() { getContext().pImpl->MDTuples.erase(this); }
594639
595640 MDNodeFwdDecl *MDNode::getTemporary(LLVMContext &Context,
596641 ArrayRef MDs) {