Skip to content

Commit 2fba469

Browse files
authored
[DFAJumpThreading] Don't thread switch without multiple successors (#71060)
Fixes #56882. Fixes #60254. When switch has only one successor, it make no sense to thread it. And computing the cost of it brings div-by-zero exception. We prevent it in this patch.
1 parent 23ad865 commit 2fba469

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ struct TransformDFA {
730730
CodeMetrics Metrics;
731731
SwitchInst *Switch = SwitchPaths->getSwitchInst();
732732

733+
// Don't thread switch without multiple successors.
734+
if (Switch->getNumSuccessors() <= 1)
735+
return false;
736+
733737
// Note that DuplicateBlockMap is not being used as intended here. It is
734738
// just being used to ensure (BB, State) pairs are only counted once.
735739
DuplicateBlockMap DuplicateMap;
@@ -807,6 +811,8 @@ struct TransformDFA {
807811
// using binary search, hence the LogBase2().
808812
unsigned CondBranches =
809813
APInt(32, Switch->getNumSuccessors()).ceilLogBase2();
814+
assert(CondBranches > 0 &&
815+
"The threaded switch must have multiple branches");
810816
DuplicationCost = Metrics.NumInsts / CondBranches;
811817
} else {
812818
// Compared with jump tables, the DFA optimizer removes an indirect branch
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2+
; RUN: opt -S -passes=dfa-jump-threading %s | FileCheck %s
3+
4+
define void @pr60254() {
5+
; CHECK-LABEL: define void @pr60254() {
6+
; CHECK-NEXT: entry_1:
7+
; CHECK-NEXT: br label [[BB_2:%.*]]
8+
; CHECK: bb_2:
9+
; CHECK-NEXT: [[PTR_I32_25_0:%.*]] = phi i32 [ 0, [[BB_4:%.*]] ], [ 0, [[BB_2]] ], [ 0, [[ENTRY_1:%.*]] ]
10+
; CHECK-NEXT: switch i32 [[PTR_I32_25_0]], label [[BB_2]] [
11+
; CHECK-NEXT: ]
12+
; CHECK: bb_4:
13+
; CHECK-NEXT: br label [[BB_2]]
14+
;
15+
entry_1:
16+
br label %bb_2
17+
18+
bb_2: ; preds = %bb_4, %bb_2, %entry_1
19+
%ptr_i32_25.0 = phi i32 [ 0, %bb_4 ], [ 0, %bb_2 ], [ 0, %entry_1 ]
20+
switch i32 %ptr_i32_25.0, label %bb_2 [
21+
]
22+
23+
bb_4: ; No predecessors!
24+
br label %bb_2
25+
}
26+
27+
define void @pr56882() {
28+
; CHECK-LABEL: define void @pr56882() {
29+
; CHECK-NEXT: entry_1:
30+
; CHECK-NEXT: br label [[BB_2:%.*]]
31+
; CHECK: bb_2:
32+
; CHECK-NEXT: [[PTR_I64_16_0:%.*]] = phi i64 [ -1317805584074026212, [[ENTRY_1:%.*]] ], [ -158622699357888703, [[BB_2]] ]
33+
; CHECK-NEXT: switch i64 [[PTR_I64_16_0]], label [[BB_2]] [
34+
; CHECK-NEXT: ]
35+
;
36+
entry_1:
37+
br label %bb_2
38+
39+
bb_2: ; preds = %bb_2, %entry_1
40+
%ptr_i64_16.0 = phi i64 [ -1317805584074026212, %entry_1 ], [ -158622699357888703, %bb_2 ]
41+
switch i64 %ptr_i64_16.0, label %bb_2 [
42+
]
43+
}

0 commit comments

Comments
 (0)