xref: /aosp_15_r20/external/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt %s -S -simplifycfg | FileCheck %s
2*9880d681SAndroid Build Coastguard Workerdeclare void @foo(i32)
3*9880d681SAndroid Build Coastguard Worker
4*9880d681SAndroid Build Coastguard Workerdefine void @test(i1 %a) {
5*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test
6*9880d681SAndroid Build Coastguard Worker; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
7*9880d681SAndroid Build Coastguard Worker  switch i1 %a, label %default [i1 1, label %true
8*9880d681SAndroid Build Coastguard Worker                                i1 0, label %false]
9*9880d681SAndroid Build Coastguard Workertrue:
10*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
11*9880d681SAndroid Build Coastguard Worker  ret void
12*9880d681SAndroid Build Coastguard Workerfalse:
13*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 3)
14*9880d681SAndroid Build Coastguard Worker  ret void
15*9880d681SAndroid Build Coastguard Workerdefault:
16*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
17*9880d681SAndroid Build Coastguard Worker  ret void
18*9880d681SAndroid Build Coastguard Worker}
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Workerdefine void @test2(i2 %a) {
21*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test2
22*9880d681SAndroid Build Coastguard Worker  switch i2 %a, label %default [i2 0, label %case0
23*9880d681SAndroid Build Coastguard Worker                                i2 1, label %case1
24*9880d681SAndroid Build Coastguard Worker                                i2 2, label %case2
25*9880d681SAndroid Build Coastguard Worker                                i2 3, label %case3]
26*9880d681SAndroid Build Coastguard Workercase0:
27*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 0)
28*9880d681SAndroid Build Coastguard Worker  ret void
29*9880d681SAndroid Build Coastguard Workercase1:
30*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
31*9880d681SAndroid Build Coastguard Worker  ret void
32*9880d681SAndroid Build Coastguard Workercase2:
33*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
34*9880d681SAndroid Build Coastguard Worker  ret void
35*9880d681SAndroid Build Coastguard Workercase3:
36*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 3)
37*9880d681SAndroid Build Coastguard Worker  ret void
38*9880d681SAndroid Build Coastguard Workerdefault:
39*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: default1:
40*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: unreachable
41*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 4)
42*9880d681SAndroid Build Coastguard Worker  ret void
43*9880d681SAndroid Build Coastguard Worker}
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker; This one is a negative test - we know the value of the default,
46*9880d681SAndroid Build Coastguard Worker; but that's about it
47*9880d681SAndroid Build Coastguard Workerdefine void @test3(i2 %a) {
48*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test3
49*9880d681SAndroid Build Coastguard Worker  switch i2 %a, label %default [i2 0, label %case0
50*9880d681SAndroid Build Coastguard Worker                                i2 1, label %case1
51*9880d681SAndroid Build Coastguard Worker                                i2 2, label %case2]
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Workercase0:
54*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 0)
55*9880d681SAndroid Build Coastguard Worker  ret void
56*9880d681SAndroid Build Coastguard Workercase1:
57*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
58*9880d681SAndroid Build Coastguard Worker  ret void
59*9880d681SAndroid Build Coastguard Workercase2:
60*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
61*9880d681SAndroid Build Coastguard Worker  ret void
62*9880d681SAndroid Build Coastguard Workerdefault:
63*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: default:
64*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: call void @foo
65*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 0)
66*9880d681SAndroid Build Coastguard Worker  ret void
67*9880d681SAndroid Build Coastguard Worker}
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker; Negative test - check for possible overflow when computing
70*9880d681SAndroid Build Coastguard Worker; number of possible cases.
71*9880d681SAndroid Build Coastguard Workerdefine void @test4(i128 %a) {
72*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test4
73*9880d681SAndroid Build Coastguard Worker  switch i128 %a, label %default [i128 0, label %case0
74*9880d681SAndroid Build Coastguard Worker                                  i128 1, label %case1]
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Workercase0:
77*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 0)
78*9880d681SAndroid Build Coastguard Worker  ret void
79*9880d681SAndroid Build Coastguard Workercase1:
80*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
81*9880d681SAndroid Build Coastguard Worker  ret void
82*9880d681SAndroid Build Coastguard Workerdefault:
83*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: default:
84*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: call void @foo
85*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 0)
86*9880d681SAndroid Build Coastguard Worker  ret void
87*9880d681SAndroid Build Coastguard Worker}
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker; All but one bit known zero
90*9880d681SAndroid Build Coastguard Workerdefine void @test5(i8 %a) {
91*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test5
92*9880d681SAndroid Build Coastguard Worker; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
93*9880d681SAndroid Build Coastguard Worker  %cmp = icmp ult i8 %a, 2
94*9880d681SAndroid Build Coastguard Worker  call void @llvm.assume(i1 %cmp)
95*9880d681SAndroid Build Coastguard Worker  switch i8 %a, label %default [i8 1, label %true
96*9880d681SAndroid Build Coastguard Worker                                i8 0, label %false]
97*9880d681SAndroid Build Coastguard Workertrue:
98*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
99*9880d681SAndroid Build Coastguard Worker  ret void
100*9880d681SAndroid Build Coastguard Workerfalse:
101*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 3)
102*9880d681SAndroid Build Coastguard Worker  ret void
103*9880d681SAndroid Build Coastguard Workerdefault:
104*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
105*9880d681SAndroid Build Coastguard Worker  ret void
106*9880d681SAndroid Build Coastguard Worker}
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker;; All but one bit known one
109*9880d681SAndroid Build Coastguard Workerdefine void @test6(i8 %a) {
110*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test6
111*9880d681SAndroid Build Coastguard Worker; CHECK: @llvm.assume
112*9880d681SAndroid Build Coastguard Worker; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
113*9880d681SAndroid Build Coastguard Worker  %and = and i8 %a, 254
114*9880d681SAndroid Build Coastguard Worker  %cmp = icmp eq i8 %and, 254
115*9880d681SAndroid Build Coastguard Worker  call void @llvm.assume(i1 %cmp)
116*9880d681SAndroid Build Coastguard Worker  switch i8 %a, label %default [i8 255, label %true
117*9880d681SAndroid Build Coastguard Worker                                i8 254, label %false]
118*9880d681SAndroid Build Coastguard Workertrue:
119*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
120*9880d681SAndroid Build Coastguard Worker  ret void
121*9880d681SAndroid Build Coastguard Workerfalse:
122*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 3)
123*9880d681SAndroid Build Coastguard Worker  ret void
124*9880d681SAndroid Build Coastguard Workerdefault:
125*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
126*9880d681SAndroid Build Coastguard Worker  ret void
127*9880d681SAndroid Build Coastguard Worker}
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker; Check that we can eliminate both dead cases and dead defaults
130*9880d681SAndroid Build Coastguard Worker; within a single run of simplify-cfg
131*9880d681SAndroid Build Coastguard Workerdefine void @test7(i8 %a) {
132*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test7
133*9880d681SAndroid Build Coastguard Worker; CHECK: @llvm.assume
134*9880d681SAndroid Build Coastguard Worker; CHECK: br i1 [[IGNORE:%.*]], label %true, label %false
135*9880d681SAndroid Build Coastguard Worker  %and = and i8 %a, 254
136*9880d681SAndroid Build Coastguard Worker  %cmp = icmp eq i8 %and, 254
137*9880d681SAndroid Build Coastguard Worker  call void @llvm.assume(i1 %cmp)
138*9880d681SAndroid Build Coastguard Worker  switch i8 %a, label %default [i8 255, label %true
139*9880d681SAndroid Build Coastguard Worker                                i8 254, label %false
140*9880d681SAndroid Build Coastguard Worker                                i8 0, label %also_dead]
141*9880d681SAndroid Build Coastguard Workertrue:
142*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
143*9880d681SAndroid Build Coastguard Worker  ret void
144*9880d681SAndroid Build Coastguard Workerfalse:
145*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 3)
146*9880d681SAndroid Build Coastguard Worker  ret void
147*9880d681SAndroid Build Coastguard Workeralso_dead:
148*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 5)
149*9880d681SAndroid Build Coastguard Worker  ret void
150*9880d681SAndroid Build Coastguard Workerdefault:
151*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
152*9880d681SAndroid Build Coastguard Worker  ret void
153*9880d681SAndroid Build Coastguard Worker}
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker;; All but one bit known undef
156*9880d681SAndroid Build Coastguard Worker;; Note: This is currently testing an optimization which doesn't trigger. The
157*9880d681SAndroid Build Coastguard Worker;; case this is protecting against is that a bit could be assumed both zero
158*9880d681SAndroid Build Coastguard Worker;; *or* one given we know it's undef.  ValueTracking doesn't do this today,
159*9880d681SAndroid Build Coastguard Worker;; but it doesn't hurt to confirm.
160*9880d681SAndroid Build Coastguard Workerdefine void @test8(i8 %a) {
161*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test8(
162*9880d681SAndroid Build Coastguard Worker; CHECK: switch i8
163*9880d681SAndroid Build Coastguard Worker  %and = and i8 %a, 254
164*9880d681SAndroid Build Coastguard Worker  %cmp = icmp eq i8 %and, undef
165*9880d681SAndroid Build Coastguard Worker  call void @llvm.assume(i1 %cmp)
166*9880d681SAndroid Build Coastguard Worker  switch i8 %a, label %default [i8 255, label %true
167*9880d681SAndroid Build Coastguard Worker                                i8 254, label %false]
168*9880d681SAndroid Build Coastguard Workertrue:
169*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 1)
170*9880d681SAndroid Build Coastguard Worker  ret void
171*9880d681SAndroid Build Coastguard Workerfalse:
172*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 3)
173*9880d681SAndroid Build Coastguard Worker  ret void
174*9880d681SAndroid Build Coastguard Workerdefault:
175*9880d681SAndroid Build Coastguard Worker  call void @foo(i32 2)
176*9880d681SAndroid Build Coastguard Worker  ret void
177*9880d681SAndroid Build Coastguard Worker}
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Workerdeclare void @llvm.assume(i1)
180