@@ -666,6 +666,11 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
666666 runningVals = beginLexicalLifetimeAfterStore (asi, si);
667667 // Create a use of the newly created copy in order to keep phi pruning
668668 // from deleting our lifetime beginning instructions.
669+ //
670+ // TODO: Remove this hack, it is only necessary because erasePhiArgument
671+ // calls deleteEdgeValue which calls
672+ // deleteTriviallyDeadOperandsOfDeadArgument and deletes the copy
673+ // and borrow that we added and want not to have deleted.
669674 SILBuilderWithScope::insertAfter (
670675 runningVals->value .copy ->getDefiningInstruction (),
671676 [&](auto builder) {
@@ -1665,22 +1670,30 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
16651670 GraphNodeWorklist<SILBasicBlock *, 2 > worklist;
16661671 worklist.initialize (parentBlock);
16671672 while (auto *block = worklist.pop ()) {
1673+ assert (domInfo->dominates (parentBlock, block));
16681674 auto *terminator = block->getTerminator ();
16691675 if (isa<UnreachableInst>(terminator)) {
16701676 endLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ terminator, ctx,
16711677 runningVals->value );
16721678 continue ;
16731679 }
1674- bool endedLifetime = false ;
1675- for (auto *successor : block->getSuccessorBlocks ()) {
1676- if (!domInfo->dominates (parentBlock, successor)) {
1677- endLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ terminator,
1678- ctx, runningVals->value );
1679- endedLifetime = true ;
1680- break ;
1681- }
1682- }
1683- if (endedLifetime) {
1680+ SILBasicBlock *successor = nullptr ;
1681+ // If any successor is not dominated by the parentBlock, then we must end
1682+ // the lifetime before that successor.
1683+ //
1684+ // Suppose that a successor is not dominated by parentBlock. Recall that
1685+ // block _is_ dominated by parentBlock. Thus that successor must have
1686+ // more than one predecessor: block, and at least one other. (Otherwise
1687+ // it would be dominated by parentBlock contrary to our assumption.)
1688+ // Recall that SIL does not allow critical edges. Therefore block has
1689+ // only a single successor.
1690+ //
1691+ // Use the above fact to only look for lack of domination of a successor
1692+ // if that successor is the single successor of block.
1693+ if ((successor = block->getSingleSuccessorBlock ()) &&
1694+ (!domInfo->dominates (parentBlock, successor))) {
1695+ endLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ terminator, ctx,
1696+ runningVals->value );
16841697 continue ;
16851698 }
16861699 for (auto *successor : block->getSuccessorBlocks ()) {
0 commit comments