@@ -666,6 +666,11 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
666
666
runningVals = beginLexicalLifetimeAfterStore (asi, si);
667
667
// Create a use of the newly created copy in order to keep phi pruning
668
668
// 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.
669
674
SILBuilderWithScope::insertAfter (
670
675
runningVals->value .copy ->getDefiningInstruction (),
671
676
[&](auto builder) {
@@ -1665,22 +1670,30 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1665
1670
GraphNodeWorklist<SILBasicBlock *, 2 > worklist;
1666
1671
worklist.initialize (parentBlock);
1667
1672
while (auto *block = worklist.pop ()) {
1673
+ assert (domInfo->dominates (parentBlock, block));
1668
1674
auto *terminator = block->getTerminator ();
1669
1675
if (isa<UnreachableInst>(terminator)) {
1670
1676
endLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ terminator, ctx,
1671
1677
runningVals->value );
1672
1678
continue ;
1673
1679
}
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 );
1684
1697
continue ;
1685
1698
}
1686
1699
for (auto *successor : block->getSuccessorBlocks ()) {
0 commit comments