Skip to content

Commit 5bfa560

Browse files
committed
Update SimplifyCFG's replacement of phi with its incoming value
Previously we replaced phi with its incoming values when all incoming values were the same. If the phi had itself as incoming value, it wasn't optimized. This PR handles such cases.
1 parent bc9af2a commit 5bfa560

File tree

6 files changed

+72
-25
lines changed

6 files changed

+72
-25
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,12 +3794,24 @@ static void tryToReplaceArgWithIncomingValue(SILBasicBlock *BB, unsigned i,
37943794
if (!A->getIncomingPhiValues(Incoming) || Incoming.empty())
37953795
return;
37963796

3797-
SILValue V = Incoming[0];
3798-
for (size_t Idx = 1, Size = Incoming.size(); Idx < Size; ++Idx) {
3799-
if (Incoming[Idx] != V)
3797+
SILValue V;
3798+
for (size_t Idx = 0; Idx < Incoming.size(); ++Idx) {
3799+
if (Incoming[Idx] == A) {
3800+
continue;
3801+
}
3802+
if (!V) {
3803+
V = Incoming[Idx];
3804+
continue;
3805+
}
3806+
if (Incoming[Idx] != V) {
38003807
return;
3808+
}
38013809
}
38023810

3811+
if (!V) {
3812+
return;
3813+
}
3814+
38033815
// If the incoming values of all predecessors are equal usually this means
38043816
// that the common incoming value dominates the BB. But: this might be not
38053817
// the case if BB is unreachable. Therefore we still have to check it.

test/SILOptimizer/simplify_cfg.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ bb7(%r : $Builtin.Int32, %carg2 : $Builtin.Int32):
14741474
// CHECK-LABEL: sil @jumpthread_switch_enum5
14751475
// CHECK: bb0:
14761476
// CHECK: br bb1
1477-
// CHECK: bb1({{.*}}):
1477+
// CHECK: bb1:
14781478
// CHECK-NEXT: cond_br undef, bb2, bb3
14791479
// CHECK: bb2:
14801480
// CHECK: br bb1
@@ -2190,17 +2190,17 @@ sil @f_use : $@convention(thin) (Builtin.Int32) -> ()
21902190
// CHECK: switch_enum {{.*}} case #Optional.some!enumelt: bb3
21912191

21922192
// CHECK: bb3{{.*}}
2193-
// CHECK: br bb5({{.*}} : $Builtin.Int32, %2 : $Builtin.Int32, [[EXT]]
2193+
// CHECK: br bb5(%2 : $Builtin.Int32, [[EXT]]
21942194

2195-
// CHECK: bb5({{.*}} : $Builtin.Int32, [[CUR:%.*]] : $Builtin.Int32, [[NEXT:%.*]] : $Builtin.Int32
2195+
// CHECK: bb5([[CUR:%.*]] : $Builtin.Int32, [[NEXT:%.*]] : $Builtin.Int32
21962196
// CHECK: [[F:%.*]] = function_ref @f
21972197
// CHECK: apply [[F]]([[CUR]])
21982198
// CHECK: cond_br {{.*}}, bb7, bb6
21992199

22002200
// CHECK: bb7:
22012201
// CHECK: [[VARADD:%.*]] = builtin "sadd_with_overflow_Int32"([[NEXT]] : $Builtin.Int32
22022202
// CHECK: [[NEXT2:%.*]] = tuple_extract [[VARADD]]
2203-
// CHECK: br bb5({{.*}} : $Builtin.Int32, [[NEXT]] : $Builtin.Int32, [[NEXT2]]
2203+
// CHECK: br bb5([[NEXT]] : $Builtin.Int32, [[NEXT2]]
22042204

22052205

22062206
sil @switch_enum_jumpthreading_bug : $@convention(thin) (Optional<Builtin.Int32>, Builtin.Int1, Builtin.Int32, Builtin.Int1) -> Builtin.Int32 {

test/SILOptimizer/simplify_cfg_args_crash.sil

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,33 @@ struct Pair {
4848
// CHECK-LABEL: @simplify_args_crash
4949
sil @simplify_args_crash : $@convention(thin) (Pair) -> () {
5050
bb0(%1 : $Pair):
51-
// CHECK: [[SECOND:%.*]] = struct_extract %0 : $Pair, #Pair.second
52-
// CHECK: [[FIRST:%.*]] = struct_extract %0 : $Pair, #Pair.first
53-
// CHECK: br bb1([[FIRST]] : $C, [[SECOND]] : $C)
51+
// CHECK: bb0
52+
// CHECK-NEXT: br bb1
5453
br bb1(%1 : $Pair)
5554

56-
// CHECK: bb1([[FIRST2:%.*]] : $C, [[SECOND2:%.*]] : $C):
5755
bb1(%2 : $Pair):
58-
// CHECK: [[STRUCT:%.*]] = struct $Pair ([[FIRST2]] : $C, [[SECOND2]] : $C)
59-
// CHECK: [[SECOND3:%.*]] = struct_extract [[STRUCT]] : $Pair, #Pair.second
60-
// CHECK: [[FIRST3:%.*]] = struct_extract [[STRUCT]] : $Pair, #Pair.first
61-
// CHECK: br bb1([[FIRST3]] : $C, [[SECOND3]] : $C)
6256
br bb1(%2 : $Pair)
6357
}
58+
59+
// CHECK-LABEL: sil [ossa] @redundant_phi_unreachable : $@convention(thin) () -> () {
60+
// CHECK: bb1({{.*}}):
61+
// CHECK-LABEL: } // end sil function 'redundant_phi_unreachable'
62+
sil [ossa] @redundant_phi_unreachable : $@convention(thin) () -> () {
63+
bb0:
64+
%1 = integer_literal $Builtin.Int32, 37
65+
br bb3
66+
67+
bb1(%3 : $Builtin.Int32):
68+
cond_br undef, bb2, bb4
69+
70+
bb2:
71+
br bb1(%3 : $Builtin.Int32)
72+
73+
bb4:
74+
br bb3
75+
76+
bb3:
77+
%r = tuple()
78+
return %r : $()
79+
}
80+

test/SILOptimizer/simplify_cfg_args_ossa.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,22 @@ bb8:
790790
%25 = tuple ()
791791
return %25 : $()
792792
}
793+
794+
// CHECK-LABEL: sil [ossa] @replace_with_incoming_value : $@convention(thin) () -> () {
795+
// CHECK: bb1:
796+
// CHECK-LABEL: } // end sil function 'replace_with_incoming_value'
797+
sil [ossa] @replace_with_incoming_value : $@convention(thin) () -> () {
798+
bb0:
799+
%1 = integer_literal $Builtin.Int32, 37
800+
br bb1(%1 : $Builtin.Int32)
801+
802+
bb1(%3 : $Builtin.Int32):
803+
cond_br undef, bb2, bb3
804+
805+
bb2:
806+
br bb1(%3 : $Builtin.Int32)
807+
808+
bb3:
809+
%r = tuple()
810+
return %r : $()
811+
}

test/SILOptimizer/simplify_cfg_ossa_dom_jumpthread.sil

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,11 @@ bb7(%r : $Builtin.Int32, %carg2 : $Builtin.Int32):
506506

507507
// CHECK-LABEL: sil [ossa] @jumpthread_switch_enum5
508508
// CHECK: bb0:
509-
// CHECK: br bb1
510-
// CHECK: bb1({{.*}}):
511-
// CHECK-NEXT: cond_br undef, bb2, bb3
509+
// CHECK: br bb2
510+
// CHECK: bb1:
511+
// CHECK-NEXT: br bb2
512512
// CHECK: bb2:
513-
// CHECK: br bb1
513+
// CHECK: cond_br undef, bb1, bb3
514514
// CHECK: bb3:
515515
// CHECK-NEXT: tuple
516516
// CHECK-NEXT: return
@@ -650,17 +650,17 @@ sil [ossa] @f_use : $@convention(thin) (Builtin.Int32) -> ()
650650
// CHECK: switch_enum {{.*}} case #Optional.some!enumelt: bb2
651651

652652
// CHECK: bb2{{.*}}
653-
// CHECK: br bb4({{.*}} : $Builtin.Int32, %2 : $Builtin.Int32, [[EXT]]
653+
// CHECK: br bb4(%2 : $Builtin.Int32, [[EXT]]
654654

655-
// CHECK: bb4({{.*}} : $Builtin.Int32, [[CUR:%.*]] : $Builtin.Int32, [[NEXT:%.*]] : $Builtin.Int32
655+
// CHECK: bb4([[CUR:%.*]] : $Builtin.Int32, [[NEXT:%.*]] : $Builtin.Int32
656656
// CHECK: [[F:%.*]] = function_ref @f
657657
// CHECK: apply [[F]]([[CUR]])
658658
// CHECK: cond_br {{.*}}, bb5, bb7
659659

660660
// CHECK: bb5:
661661
// CHECK: [[VARADD:%.*]] = builtin "sadd_with_overflow_Int32"([[NEXT]] : $Builtin.Int32
662662
// CHECK: [[NEXT2:%.*]] = tuple_extract [[VARADD]]
663-
// CHECK: br bb4({{.*}} : $Builtin.Int32, [[NEXT]] : $Builtin.Int32, [[NEXT2]]
663+
// CHECK: br bb4([[NEXT]] : $Builtin.Int32, [[NEXT2]]
664664
sil [ossa] @switch_enum_jumpthreading_bug : $@convention(thin) (Optional<Builtin.Int32>, Builtin.Int1, Builtin.Int32, Builtin.Int1) -> Builtin.Int32 {
665665
bb0(%0 : $Optional<Builtin.Int32>, %1 : $Builtin.Int1, %2: $Builtin.Int32, %3 : $Builtin.Int1):
666666
cond_br %1, bb2, bb10a

test/SILOptimizer/string_optimization.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ public func testQualifiedTypeName() -> String {
8787
return _typeName(Outer.Inner.self, qualified: true)
8888
}
8989

90-
// This test needs an updated SimplifyCFG optimization. Disable until we have that.
9190
// CHECK-LABEL: sil [noinline] @$s4test0A20UnqualifiedLocalTypeSSyF
92-
// TODO-CHECK-NOT: apply
93-
// TODO-CHECK-NOT: bb1
91+
// CHECK-NOT: apply
92+
// CHECK-NOT: bb1
9493
// CHECK: } // end sil function '$s4test0A20UnqualifiedLocalTypeSSyF'
9594
@inline(never)
9695
public func testUnqualifiedLocalType() -> String {

0 commit comments

Comments
 (0)