Skip to content

Commit f372d70

Browse files
committed
DeadStoreElimination: don't require values stored back to memory if the control flow ends in an unreachable
This makes dead store elimination less conservative, e.g. if error checks are in the control flow.
1 parent 1c0dcc1 commit f372d70

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/SILOptimizer/Transforms/DeadStoreElimination.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ void DSEContext::mergeSuccessorLiveIns(SILBasicBlock *BB) {
717717
// dead for block with no successor.
718718
BlockState *C = getBlockState(BB);
719719
if (BB->succ_empty()) {
720+
if (isa<UnreachableInst>(BB->getTerminator())) {
721+
C->BBWriteSetOut.set();
722+
return;
723+
}
720724
C->BBWriteSetOut |= C->BBDeallocateLocation;
721725
return;
722726
}

test/SILOptimizer/dead_store_elim.sil

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,29 @@ bb3:
291291
return %9999 : $()
292292
}
293293

294+
// CHECK-LABEL: sil @handle_unreachable : $@convention(thin) (@inout Builtin.Int32) -> () {
295+
// CHECK: bb0
296+
// CHECK-NEXT: integer_literal
297+
// CHECK-NEXT: cond_br
298+
// CHECK: return
299+
sil @handle_unreachable : $@convention(thin) (@inout Builtin.Int32) -> () {
300+
bb0(%0 : $*Builtin.Int32):
301+
%1 = integer_literal $Builtin.Int32, 0
302+
store %1 to %0 : $*Builtin.Int32
303+
cond_br undef, bb1, bb2
304+
305+
bb1:
306+
unreachable
307+
308+
bb2:
309+
br bb3
310+
311+
bb3:
312+
store %1 to %0 : $*Builtin.Int32
313+
%9999 = tuple()
314+
return %9999 : $()
315+
}
316+
294317
// CHECK-LABEL: sil @post_dominating_dead_store_partial : $@convention(thin) (@inout Builtin.Int32) -> () {
295318
// CHECK: bb0(
296319
// CHECK-NOT: {{ store}}

0 commit comments

Comments
 (0)