Skip to content

Commit 09dde09

Browse files
committed
[Mem2Reg] Replaced loop with getSingleSuccessor.
Thanks to the lack of critical edges in SIL, if a block B dominated by P has a successor S which is not dominated by P, then B must have only a single successor. Used this fact to replace a loop over successors to a call to getSinglePredecessor. Also added an assertion that, in the notation above, B is dominated by P.
1 parent 2ad0eb7 commit 09dde09

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,22 +1665,30 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
16651665
GraphNodeWorklist<SILBasicBlock *, 2> worklist;
16661666
worklist.initialize(parentBlock);
16671667
while (auto *block = worklist.pop()) {
1668+
assert(domInfo->dominates(parentBlock, block));
16681669
auto *terminator = block->getTerminator();
16691670
if (isa<UnreachableInst>(terminator)) {
16701671
endLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/terminator, ctx,
16711672
runningVals->value);
16721673
continue;
16731674
}
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) {
1675+
SILBasicBlock *successor = nullptr;
1676+
// If any successor is not dominated by the parentBlock, then we must end
1677+
// the lifetime before that successor.
1678+
//
1679+
// Suppose that a successor is not dominated by parentBlock. Recall that
1680+
// block _is_ dominated by parentBlock. Thus that successor must have
1681+
// more than one predecessor: block, and at least one other. (Otherwise
1682+
// it would be dominated by parentBlock contrary to our assumption.)
1683+
// Recall that SIL does not allow critical edges. Therefore block has
1684+
// only a single successor.
1685+
//
1686+
// Use the above fact to only look for lack of domination of a successor
1687+
// if that successor is the single successor of block.
1688+
if ((successor = block->getSingleSuccessorBlock()) &&
1689+
(!domInfo->dominates(parentBlock, successor))) {
1690+
endLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/terminator, ctx,
1691+
runningVals->value);
16841692
continue;
16851693
}
16861694
for (auto *successor : block->getSuccessorBlocks()) {

0 commit comments

Comments
 (0)