Skip to content

Commit 64be382

Browse files
committed
[SimplifyCFG] Fix the compile crash when default branch is unreachable
This modification will squash to the previous commit if accepted. Seperate it just to make it more clearly to review.
1 parent e86a303 commit 64be382

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6631,7 +6631,9 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
66316631
BranchInst *RangeCheckBranch = nullptr;
66326632

66336633
// Grow the table to cover all possible index values to avoid the range check.
6634-
if (UseSwitchConditionAsTableIndex) {
6634+
// It will use the default result to fill in the table hole later, so make
6635+
// sure it exist.
6636+
if (UseSwitchConditionAsTableIndex && HasDefaultResults) {
66356637
ConstantRange CR = computeConstantRange(TableIndex, /* ForSigned */ false);
66366638
// Grow the table shouldn't have any size impact by checking
66376639
// WouldFitInRegister.
@@ -6641,7 +6643,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
66416643
return SwitchLookupTable::WouldFitInRegister(
66426644
DL, CR.getUpper().getLimitedValue(), KV.second /* ResultType */);
66436645
})) {
6644-
// The default branch is unreachable when we enlarge the lookup table.
6646+
// The default branch is unreachable after we enlarge the lookup table.
66456647
// Adjust DefaultIsReachable to reuse code path.
66466648
TableSize = CR.getUpper().getZExtValue();
66476649
DefaultIsReachable = false;

llvm/test/Transforms/SimplifyCFG/switch_mask.ll

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
55

6+
declare i1 @foo()
7+
68
; https://alive2.llvm.org/ce/z/tuxLhJ
79
define i1 @switch_lookup_with_small_i1(i64 %x) {
810
; CHECK-LABEL: @switch_lookup_with_small_i1(
@@ -103,3 +105,71 @@ lor.end:
103105
%0 = phi i8 [ 15, %sw.bb0 ], [ 6, %sw.bb1 ], [ 7, %sw.bb2 ], [ 0, %default ]
104106
ret i8 %0
105107
}
108+
109+
; Negative test: The default branch is unreachable, also it has no result.
110+
define i1 @switch_lookup_with_small_i1_default_unreachable(i32 %x) {
111+
; CHECK-LABEL: @switch_lookup_with_small_i1_default_unreachable(
112+
; CHECK-NEXT: entry:
113+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
114+
; CHECK-NEXT: ret i1 false
115+
;
116+
entry:
117+
%and = and i32 %x, 15
118+
switch i32 %and, label %default [
119+
i32 4, label %phi.end
120+
i32 2, label %phi.end
121+
i32 10, label %phi.end
122+
i32 9, label %phi.end
123+
i32 1, label %sw.bb1.i
124+
i32 3, label %sw.bb1.i
125+
i32 5, label %sw.bb1.i
126+
i32 0, label %sw.bb1.i
127+
i32 6, label %sw.bb1.i
128+
i32 7, label %sw.bb1.i
129+
i32 8, label %sw.bb1.i
130+
]
131+
132+
sw.bb1.i: ; preds = %entry, %entry, %entry, %entry, %entry, %entry, %entry
133+
br label %phi.end
134+
135+
default: ; preds = %entry
136+
unreachable
137+
138+
phi.end: ; preds = %sw.bb1.i, %entry, %entry, %entry, %entry
139+
%retval = phi i1 [ false, %sw.bb1.i ], [ false, %entry ], [ false, %entry ], [ false, %entry ], [ false, %entry ]
140+
ret i1 %retval
141+
}
142+
143+
; Negative test: The result in default reachable, but its value is not const.
144+
define i1 @switch_lookup_with_small_i1_default_nonconst(i64 %x) {
145+
; CHECK-LABEL: @switch_lookup_with_small_i1_default_nonconst(
146+
; CHECK-NEXT: entry:
147+
; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 15
148+
; CHECK-NEXT: switch i64 [[AND]], label [[DEFAULT:%.*]] [
149+
; CHECK-NEXT: i64 10, label [[LOR_END:%.*]]
150+
; CHECK-NEXT: i64 1, label [[LOR_END]]
151+
; CHECK-NEXT: i64 2, label [[LOR_END]]
152+
; CHECK-NEXT: ]
153+
; CHECK: default:
154+
; CHECK-NEXT: [[CALL:%.*]] = tail call i1 @foo()
155+
; CHECK-NEXT: br label [[LOR_END]]
156+
; CHECK: lor.end:
157+
; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ [[CALL]], [[DEFAULT]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
158+
; CHECK-NEXT: ret i1 [[TMP0]]
159+
;
160+
entry:
161+
%and = and i64 %x, 15
162+
switch i64 %and, label %default [
163+
i64 10, label %lor.end
164+
i64 1, label %lor.end
165+
i64 2, label %lor.end
166+
]
167+
168+
default: ; preds = %entry
169+
%call = tail call i1 @foo()
170+
br label %lor.end
171+
172+
lor.end: ; preds = %entry, %entry, %entry, %default
173+
%0 = phi i1 [ true, %entry ], [ %call, %default ], [ true, %entry ], [ true, %entry ]
174+
ret i1 %0
175+
}

0 commit comments

Comments
 (0)