Skip to content

Commit a592155

Browse files
Merge pull request #10885 from aschwaighofer/jumpthread_for_arc
SimplifyCFG: JumpThread to facilitate ARC removal
2 parents d87cb5b + ae974b0 commit a592155

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,31 @@ void SimplifyCFG::findLoopHeaders() {
932932
}
933933
}
934934

935+
static bool couldRemoveRelease(SILBasicBlock *SrcBB, SILValue SrcV,
936+
SILBasicBlock *DestBB, SILValue DestV) {
937+
bool IsRetainOfSrc = false;
938+
for (auto *U: SrcV->getUses())
939+
if (U->getUser()->getParent() == SrcBB &&
940+
(isa<StrongRetainInst>(U->getUser()) ||
941+
isa<RetainValueInst>(U->getUser()))) {
942+
IsRetainOfSrc = true;
943+
break;
944+
}
945+
if (!IsRetainOfSrc)
946+
return false;
947+
948+
bool IsReleaseOfDest = false;
949+
for (auto *U: DestV->getUses())
950+
if (U->getUser()->getParent() == DestBB &&
951+
(isa<StrongReleaseInst>(U->getUser()) ||
952+
isa<ReleaseValueInst>(U->getUser()))) {
953+
IsReleaseOfDest = true;
954+
break;
955+
}
956+
957+
return IsReleaseOfDest;
958+
}
959+
935960
/// tryJumpThreading - Check to see if it looks profitable to duplicate the
936961
/// destination of an unconditional jump into the bottom of this block.
937962
bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
@@ -961,6 +986,14 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
961986
int ThreadingBudget = 0;
962987

963988
for (unsigned i = 0, e = BI->getArgs().size(); i != e; ++i) {
989+
// If the value being substituted on is release there is a chance we could
990+
// remove the release after jump threading.
991+
if (couldRemoveRelease(SrcBB, BI->getArg(i), DestBB,
992+
DestBB->getArgument(i))) {
993+
ThreadingBudget = 8;
994+
break;
995+
}
996+
964997
// If the value being substituted is an enum, check to see if there are any
965998
// switches on it.
966999
SILValue Arg = BI->getArg(i);

test/SILOptimizer/simplify_cfg.sil

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,3 +2859,45 @@ bb5(%e : $MyError):
28592859
bb6(%44 : $Builtin.Int8):
28602860
return %44 : $Builtin.Int8
28612861
}
2862+
2863+
// CHECK-LABEL: jump_thread_retain_release
2864+
// CHECK: cond_br
2865+
// CHECK: cond_br %3, bb3, bb4
2866+
2867+
// CHECK: bb3:
2868+
// CHECK: strong_retain %1 : $foo
2869+
// CHECK: strong_release %1 : $foo
2870+
// CHECK: br bb5(%1 : $foo)
2871+
2872+
// CHECK: bb4:
2873+
// CHECK: strong_retain %2 : $foo
2874+
// CHECK: strong_release %2 : $foo
2875+
// CHECK: br bb5(%2 : $foo)
2876+
2877+
sil @jump_thread_retain_release : $@convention(thin) (Builtin.Int1, foo, foo, Builtin.Int1) -> () {
2878+
bb0(%0 : $Builtin.Int1, %1 : $foo, %2 : $foo, %3 : $Builtin.Int1):
2879+
cond_br %0, bb2, bb1
2880+
2881+
bb1:
2882+
br bb6(%2 : $foo)
2883+
2884+
bb2:
2885+
cond_br %3, bb3, bb4
2886+
2887+
bb3:
2888+
strong_retain %1 : $foo
2889+
br bb5(%1 : $foo)
2890+
2891+
bb4:
2892+
strong_retain %2 : $foo
2893+
br bb5(%2 : $foo)
2894+
2895+
bb5(%11 : $foo):
2896+
strong_release %11 : $foo
2897+
br bb6(%11 : $foo)
2898+
2899+
bb6(%14 : $foo):
2900+
strong_release %14 : $foo
2901+
%16 = tuple ()
2902+
return %16 : $()
2903+
}

0 commit comments

Comments
 (0)