Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 5d8f37f

Browse files
committed
[SimplifyCFG] Use known bits to eliminate dead switch defaults
This is a follow up to http://reviews.llvm.org/D11995 implementing the suggestion by Hans. If we know some of the bits of the value being switched on, we know that the maximum number of unique cases covers the unknown bits. This allows to eliminate switch defaults for large integers (i32) when most bits in the value are known. Note that I had to make the transform contingent on not having any dead cases. This is conservatively correct with the old code, but required for the new code since we might have a dead case which varies one of the known bits. Counting that towards our number of covering cases would be bad. If we do have dead cases, we'll eliminate them first, then revisit the possibly dead default. Differential Revision: http://reviews.llvm.org/D12497 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247309 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 4b5f4e5 commit 5d8f37f

File tree

2 files changed

+105
-7
lines changed

2 files changed

+105
-7
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,11 +3423,17 @@ static bool EliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
34233423
}
34243424

34253425
// If we can prove that the cases must cover all possible values, the
3426-
// default destination becomes dead and we can remove it.
3426+
// default destination becomes dead and we can remove it. If we know some
3427+
// of the bits in the value, we can use that to more precisely compute the
3428+
// number of possible unique case values.
34273429
bool HasDefault =
34283430
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
3429-
if (HasDefault && Bits < 64 /* avoid overflow */ &&
3430-
SI->getNumCases() == (1ULL << Bits)) {
3431+
const unsigned NumUnknownBits = Bits -
3432+
(KnownZero.Or(KnownOne)).countPopulation();
3433+
assert(0 <= NumUnknownBits && NumUnknownBits <= Bits);
3434+
if (HasDefault && DeadCases.empty() &&
3435+
NumUnknownBits < 64 /* avoid overflow */ &&
3436+
SI->getNumCases() == (1ULL << NumUnknownBits)) {
34313437
DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
34323438
BasicBlock *NewDefault = SplitBlockPredecessors(SI->getDefaultDest(),
34333439
SI->getParent(), "");

test/Transforms/SimplifyCFG/switch-dead-default.ll

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
declare void @foo(i32)
33

44
define void @test(i1 %a) {
5-
; CHECK-LABEL @test
5+
; CHECK-LABEL: @test
66
; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
77
switch i1 %a, label %default [i1 1, label %true
88
i1 0, label %false]
@@ -18,7 +18,7 @@ default:
1818
}
1919

2020
define void @test2(i2 %a) {
21-
; CHECK-LABEL @test2
21+
; CHECK-LABEL: @test2
2222
switch i2 %a, label %default [i2 0, label %case0
2323
i2 1, label %case1
2424
i2 2, label %case2
@@ -45,7 +45,7 @@ default:
4545
; This one is a negative test - we know the value of the default,
4646
; but that's about it
4747
define void @test3(i2 %a) {
48-
; CHECK-LABEL @test3
48+
; CHECK-LABEL: @test3
4949
switch i2 %a, label %default [i2 0, label %case0
5050
i2 1, label %case1
5151
i2 2, label %case2]
@@ -69,7 +69,7 @@ default:
6969
; Negative test - check for possible overflow when computing
7070
; number of possible cases.
7171
define void @test4(i128 %a) {
72-
; CHECK-LABEL @test4
72+
; CHECK-LABEL: @test4
7373
switch i128 %a, label %default [i128 0, label %case0
7474
i128 1, label %case1]
7575

@@ -85,3 +85,95 @@ default:
8585
call void @foo(i32 0)
8686
ret void
8787
}
88+
89+
; All but one bit known zero
90+
define void @test5(i8 %a) {
91+
; CHECK-LABEL: @test5
92+
; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
93+
%cmp = icmp ult i8 %a, 2
94+
call void @llvm.assume(i1 %cmp)
95+
switch i8 %a, label %default [i8 1, label %true
96+
i8 0, label %false]
97+
true:
98+
call void @foo(i32 1)
99+
ret void
100+
false:
101+
call void @foo(i32 3)
102+
ret void
103+
default:
104+
call void @foo(i32 2)
105+
ret void
106+
}
107+
108+
;; All but one bit known one
109+
define void @test6(i8 %a) {
110+
; CHECK-LABEL: @test6
111+
; CHECK: @llvm.assume
112+
; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
113+
%and = and i8 %a, 254
114+
%cmp = icmp eq i8 %and, 254
115+
call void @llvm.assume(i1 %cmp)
116+
switch i8 %a, label %default [i8 255, label %true
117+
i8 254, label %false]
118+
true:
119+
call void @foo(i32 1)
120+
ret void
121+
false:
122+
call void @foo(i32 3)
123+
ret void
124+
default:
125+
call void @foo(i32 2)
126+
ret void
127+
}
128+
129+
; Check that we can eliminate both dead cases and dead defaults
130+
; within a single run of simplify-cfg
131+
define void @test7(i8 %a) {
132+
; CHECK-LABEL: @test7
133+
; CHECK: @llvm.assume
134+
; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
135+
%and = and i8 %a, 254
136+
%cmp = icmp eq i8 %and, 254
137+
call void @llvm.assume(i1 %cmp)
138+
switch i8 %a, label %default [i8 255, label %true
139+
i8 254, label %false
140+
i8 0, label %also_dead]
141+
true:
142+
call void @foo(i32 1)
143+
ret void
144+
false:
145+
call void @foo(i32 3)
146+
ret void
147+
also_dead:
148+
call void @foo(i32 5)
149+
ret void
150+
default:
151+
call void @foo(i32 2)
152+
ret void
153+
}
154+
155+
;; All but one bit known undef
156+
;; Note: This is currently testing an optimization which doesn't trigger. The
157+
;; case this is protecting against is that a bit could be assumed both zero
158+
;; *or* one given we know it's undef. ValueTracking doesn't do this today,
159+
;; but it doesn't hurt to confirm.
160+
define void @test8(i8 %a) {
161+
; CHECK-LABEL: @test8(
162+
; CHECK: switch i8
163+
%and = and i8 %a, 254
164+
%cmp = icmp eq i8 %and, undef
165+
call void @llvm.assume(i1 %cmp)
166+
switch i8 %a, label %default [i8 255, label %true
167+
i8 254, label %false]
168+
true:
169+
call void @foo(i32 1)
170+
ret void
171+
false:
172+
call void @foo(i32 3)
173+
ret void
174+
default:
175+
call void @foo(i32 2)
176+
ret void
177+
}
178+
179+
declare void @llvm.assume(i1)

0 commit comments

Comments
 (0)