Skip to content

Commit 134b8ca

Browse files
authored
Merge pull request #26721 from eeckstein/fix-simplifycfg
2 parents 94d1e5e + 496ca10 commit 134b8ca

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,11 @@ getSwitchEnumPred(SILBasicBlock *BB, SILBasicBlock *PostBB,
31563156

31573157
// Check if BB is reachable from multiple enum cases. This means that there is
31583158
// a single-branch block for each enum case which branch to BB.
3159+
// Usually in this case BB has no arguments. If there are any arguments, bail,
3160+
// because the argument may be used by other instructions.
3161+
if (BB->getNumArguments() != 0)
3162+
return nullptr;
3163+
31593164
SILBasicBlock *CommonPredPredBB = nullptr;
31603165
for (auto PredBB : BB->getPredecessorBlocks()) {
31613166
TermInst *PredTerm = PredBB->getTerminator();

test/SILOptimizer/simplify_cfg_args.sil

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,47 @@ bb4(%20 : $Builtin.Int32): // Preds: bb1 bb2 bb3
411411
return %21 : $Int32
412412
}
413413

414+
enum TwoCases {
415+
case A
416+
case B
417+
}
418+
419+
struct S {
420+
var x: Builtin.Int32
421+
}
422+
423+
sil @useSandInt : $@convention(thin) (S, Builtin.Int32) -> ()
424+
425+
// CHECK-LABEL: sil @dont_opt_switch_enum_with_arg_bb
426+
sil @dont_opt_switch_enum_with_arg_bb : $@convention(thin) (TwoCases, S, S) -> Builtin.Int32 {
427+
bb0(%0 : $TwoCases, %1 : $S, %2 : $S):
428+
%3 = integer_literal $Builtin.Int32, 3
429+
%4 = integer_literal $Builtin.Int32, 4
430+
// CHECK: switch_enum
431+
switch_enum %0 : $TwoCases, case #TwoCases.A!enumelt: bb1, case #TwoCases.B!enumelt.1: bb2
432+
433+
bb1:
434+
br bb3(%1 : $S)
435+
436+
bb2:
437+
br bb3(%2 : $S)
438+
439+
bb3(%10 : $S):
440+
br bb4(%3 : $Builtin.Int32)
441+
442+
bb4(%20 : $Builtin.Int32):
443+
%11 = function_ref @useSandInt : $@convention(thin) (S, Builtin.Int32) -> ()
444+
%12 = apply %11(%10, %20) : $@convention(thin) (S, Builtin.Int32) -> ()
445+
cond_br undef, bb5, bb6
446+
447+
bb5:
448+
br bb4(%4 : $Builtin.Int32)
449+
450+
bb6:
451+
// CHECK: return
452+
return %20 : $Builtin.Int32
453+
}
454+
414455
enum E {
415456
case Nope, Yup(Builtin.Int1)
416457
}

0 commit comments

Comments
 (0)