Skip to content

Commit d8dcf61

Browse files
committed
Improve SimplifyCFG: remove conditional branches to the same target.
1 parent d82e0ff commit d8dcf61

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,17 +1571,29 @@ bool SimplifyCFG::simplifyCondBrBlock(CondBranchInst *BI) {
15711571

15721572
// Simplify cond_br where both sides jump to the same blocks with the same
15731573
// args.
1574-
if (TrueArgs == FalseArgs && (TrueSide == FalseTrampolineDest ||
1575-
FalseSide == TrueTrampolineDest)) {
1576-
LLVM_DEBUG(llvm::dbgs() << "replace cond_br with same dests with br: "
1577-
<< *BI);
1578-
SILBuilderWithScope(BI).createBranch(BI->getLoc(),
1579-
TrueTrampolineDest ? FalseSide : TrueSide, TrueArgs);
1574+
auto condBrToBr = [&](OperandValueArrayRef branchArgs,
1575+
SILBasicBlock *newDest) {
1576+
LLVM_DEBUG(llvm::dbgs()
1577+
<< "replace cond_br with same dests with br: " << *BI);
1578+
SILBuilderWithScope(BI).createBranch(BI->getLoc(), newDest, branchArgs);
15801579
BI->eraseFromParent();
15811580
addToWorklist(ThisBB);
1582-
addToWorklist(TrueSide);
15831581
++NumConstantFolded;
1584-
return true;
1582+
};
1583+
if (TrueArgs == FalseArgs) {
1584+
if (TrueTrampolineDest) {
1585+
if (TrueTrampolineDest == FalseSide
1586+
|| TrueTrampolineDest == FalseTrampolineDest) {
1587+
condBrToBr(TrueArgs, TrueTrampolineDest);
1588+
removeIfDead(TrueSide);
1589+
removeIfDead(FalseSide);
1590+
return true;
1591+
}
1592+
} else if (FalseTrampolineDest == TrueSide) {
1593+
condBrToBr(TrueArgs, FalseTrampolineDest);
1594+
removeIfDead(FalseSide);
1595+
return true;
1596+
}
15851597
}
15861598

15871599
auto *TrueTrampolineBr = getTrampolineWithoutBBArgsTerminator(TrueSide);

test/SILOptimizer/simplify_cfg_unique_values.sil

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ enum DupCaseEnum {
1313
// CHECK-LABEL: sil @performSwitch : $@convention(thin) (Int64, @thin DupCaseEnum.Type) -> DupCaseEnum {
1414
// CHECK: bb0(%0 : $Int64, %1 : $@thin DupCaseEnum.Type):
1515
// CHECK: select_value
16-
// CHECK: br bb1
17-
// CHECK: bb1:
18-
// CHECK: return
16+
// CHECK-NEXT: return
1917
sil @performSwitch : $@convention(thin) (Int64, @thin DupCaseEnum.Type) -> DupCaseEnum {
2018
// %0 // users: %9, %5, %3, %2
2119
bb0(%0 : $Int64, %1 : $@thin DupCaseEnum.Type):

0 commit comments

Comments
 (0)