@@ -187,9 +187,8 @@ class DCE {
187187 llvm::SmallPtrSetImpl<SILBasicBlock *> &);
188188 SILBasicBlock *nearestUsefulPostDominator (SILBasicBlock *Block);
189189 void replaceBranchWithJump (SILInstruction *Inst, SILBasicBlock *Block);
190- // / If \p value is live, insert a lifetime ending operation in ossa.
191- // / destroy_value for @owned value and end_borrow for a @guaranteed value.
192- void endLifetimeOfLiveValue (SILValue value, SILInstruction *insertPt);
190+ // / Insert lifetime ending instruction if defining value of \p op is live.
191+ void endLifetimeOfLiveValue (Operand *op, SILInstruction *insertPt);
193192
194193public:
195194 DCE (SILFunction *F, PostDominanceInfo *PDT)
@@ -585,23 +584,24 @@ void DCE::replaceBranchWithJump(SILInstruction *Inst, SILBasicBlock *Block) {
585584 (void )Branch;
586585}
587586
588- void DCE::endLifetimeOfLiveValue (SILValue value, SILInstruction *insertPt) {
587+ void DCE::endLifetimeOfLiveValue (Operand *op, SILInstruction *insertPt) {
588+ auto value = op->get ();
589589 if (SILInstruction *inst = value->getDefiningInstruction ()) {
590590 if (!LiveInstructions.contains (inst))
591591 return ;
592592 } else if (auto *arg = dyn_cast<SILArgument>(value)) {
593593 if (!LiveArguments.contains (arg))
594594 return ;
595595 }
596+
597+ assert (op->isLifetimeEnding ());
596598 SILBuilderWithScope builder (insertPt);
597599 if (value->getOwnershipKind () == OwnershipKind::Owned) {
598- builder.emitDestroyOperation (RegularLocation::getAutoGeneratedLocation (),
599- value);
600+ builder.createDestroyValue (RegularLocation::getAutoGeneratedLocation (),
601+ value);
600602 }
601- BorrowedValue borrow (lookThroughBorrowedFromDef (value));
602- if (borrow && borrow.isLocalScope ()) {
603- builder.emitEndBorrowOperation (RegularLocation::getAutoGeneratedLocation (),
604- value);
603+ if (value->getOwnershipKind () == OwnershipKind::Guaranteed) {
604+ builder.createEndBorrow (RegularLocation::getAutoGeneratedLocation (), value);
605605 }
606606}
607607
@@ -657,7 +657,7 @@ bool DCE::removeDead() {
657657 for (auto *pred : BB.getPredecessorBlocks ()) {
658658 auto *predTerm = pred->getTerminator ();
659659 SILInstruction *insertPt = predTerm;
660- if (phiArg->getOwnershipKind () == OwnershipKind::Guaranteed ) {
660+ if (phiArg->isReborrow () ) {
661661 // If the phiArg is dead and had reborrow dependencies, its baseValue
662662 // may also have been dead and a destroy_value of its baseValue may
663663 // have been inserted before the pred's terminator. Make sure to
@@ -677,8 +677,10 @@ bool DCE::removeDead() {
677677 insertPt = &predInst;
678678 }
679679 }
680-
681- endLifetimeOfLiveValue (phiArg->getIncomingPhiValue (pred), insertPt);
680+ auto *predOp = phiArg->getIncomingPhiOperand (pred);
681+ if (predOp->isLifetimeEnding ()) {
682+ endLifetimeOfLiveValue (predOp, insertPt);
683+ }
682684 }
683685 erasePhiArgument (&BB, i, /* cleanupDeadPhiOps=*/ true ,
684686 InstModCallbacks ().onCreateNewInst (
@@ -702,7 +704,7 @@ bool DCE::removeDead() {
702704
703705 for (auto &op : termInst->getAllOperands ()) {
704706 if (op.isLifetimeEnding ()) {
705- endLifetimeOfLiveValue (op. get () , termInst);
707+ endLifetimeOfLiveValue (&op , termInst);
706708 }
707709 }
708710 LLVM_DEBUG (llvm::dbgs () << " Replacing branch: " );
@@ -725,7 +727,7 @@ bool DCE::removeDead() {
725727 if (F->hasOwnership ()) {
726728 for (auto &Op : Inst->getAllOperands ()) {
727729 if (Op.isLifetimeEnding ()) {
728- endLifetimeOfLiveValue (Op. get () , Inst);
730+ endLifetimeOfLiveValue (&Op , Inst);
729731 }
730732 }
731733 }
0 commit comments