Skip to content

Commit 940b151

Browse files
committed
SimplifyCFG: Modify DestBB before replacing branch target
1 parent c34f61b commit 940b151

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,13 +1278,13 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
12781278
//
12791279
SILBasicBlock *remainingBlock = nullptr, *deletedBlock = nullptr;
12801280
if (BB != Fn.getEntryBlock() && hasLessInstructions(BB, DestBB)) {
1281+
DestBB->spliceAtBegin(BB);
1282+
DestBB->dropAllArguments();
1283+
DestBB->moveArgumentList(BB);
12811284
while (!BB->pred_empty()) {
12821285
SILBasicBlock *pred = *BB->pred_begin();
12831286
replaceBranchTarget(pred->getTerminator(), BB, DestBB, true);
12841287
}
1285-
DestBB->spliceAtBegin(BB);
1286-
DestBB->dropAllArguments();
1287-
DestBB->moveArgumentList(BB);
12881288
remainingBlock = DestBB;
12891289
deletedBlock = BB;
12901290
} else {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplify-cfg
2+
3+
enum FakeOptional<T> {
4+
case some(T)
5+
case none
6+
}
7+
8+
class SuperKlass {
9+
}
10+
11+
class Klass : SuperKlass {
12+
}
13+
14+
sil [ossa] @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
15+
16+
// Test we don't get an assert while replacing branch target of checked_cast_br
17+
sil [ossa] @test_cfg1 : $@convention(thin) (@guaranteed SuperKlass) -> @out FakeOptional<Klass> {
18+
bb0(%0 : $*FakeOptional<Klass>, %1 : @guaranteed $SuperKlass):
19+
%2 = init_enum_data_addr %0 : $*FakeOptional<Klass>, #FakeOptional.some!enumelt
20+
checked_cast_br %1 : $SuperKlass to Klass, bb1, bb2
21+
22+
bb1(%6 : @guaranteed $Klass):
23+
%7 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %6 : $Klass
24+
br bb3(%7 : $FakeOptional<Klass>)
25+
26+
bb2(%9 : @guaranteed $SuperKlass):
27+
%10 = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
28+
br bb3(%10 : $FakeOptional<Klass>)
29+
30+
31+
bb3(%12 : @guaranteed $FakeOptional<Klass>):
32+
switch_enum %12 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb4, case #FakeOptional.none!enumelt: bb6
33+
34+
bb4(%14 : @guaranteed $Klass):
35+
%15 = copy_value %14 : $Klass
36+
store %15 to [init] %2 : $*Klass
37+
inject_enum_addr %0 : $*FakeOptional<Klass>, #FakeOptional.some!enumelt
38+
br bb5
39+
40+
bb5:
41+
%21 = tuple ()
42+
return %21 : $()
43+
44+
bb6:
45+
inject_enum_addr %0 : $*FakeOptional<Klass>, #FakeOptional.none!enumelt
46+
br bb5
47+
}

0 commit comments

Comments
 (0)