1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt %s -S -passes='simplifycfg<switch-to-lookup>' -simplifycfg-require-and-preserve-domtree=1 -switch-range-to-icmp | FileCheck %s
4 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7 define void @test(i1 %a) {
9 ; CHECK-NEXT: [[A_OFF:%.*]] = add i1 [[A:%.*]], true
10 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i1 [[A_OFF]], true
11 ; CHECK-NEXT: br i1 [[SWITCH]], label [[TRUE:%.*]], label [[FALSE:%.*]]
13 ; CHECK-NEXT: ret void
15 ; CHECK-NEXT: call void @foo(i32 1)
16 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
18 ; CHECK-NEXT: call void @foo(i32 3)
19 ; CHECK-NEXT: br label [[COMMON_RET]]
21 switch i1 %a, label %default [i1 1, label %true
34 define void @test2(i2 %a) {
35 ; CHECK-LABEL: @test2(
36 ; CHECK-NEXT: switch i2 [[A:%.*]], label [[DOTUNREACHABLEDEFAULT:%.*]] [
37 ; CHECK-NEXT: i2 0, label [[CASE0:%.*]]
38 ; CHECK-NEXT: i2 1, label [[CASE1:%.*]]
39 ; CHECK-NEXT: i2 -2, label [[CASE2:%.*]]
40 ; CHECK-NEXT: i2 -1, label [[CASE3:%.*]]
43 ; CHECK-NEXT: ret void
45 ; CHECK-NEXT: call void @foo(i32 0)
46 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
48 ; CHECK-NEXT: call void @foo(i32 1)
49 ; CHECK-NEXT: br label [[COMMON_RET]]
51 ; CHECK-NEXT: call void @foo(i32 2)
52 ; CHECK-NEXT: br label [[COMMON_RET]]
54 ; CHECK-NEXT: call void @foo(i32 3)
55 ; CHECK-NEXT: br label [[COMMON_RET]]
56 ; CHECK: .unreachabledefault:
57 ; CHECK-NEXT: unreachable
59 switch i2 %a, label %default [i2 0, label %case0
80 ; This one is a negative test - we know the value of the default,
82 define void @test3(i2 %a) {
83 ; CHECK-LABEL: @test3(
84 ; CHECK-NEXT: switch i2 [[A:%.*]], label [[DEFAULT:%.*]] [
85 ; CHECK-NEXT: i2 0, label [[CASE0:%.*]]
86 ; CHECK-NEXT: i2 1, label [[CASE1:%.*]]
87 ; CHECK-NEXT: i2 -2, label [[CASE2:%.*]]
90 ; CHECK-NEXT: ret void
92 ; CHECK-NEXT: call void @foo(i32 0)
93 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
95 ; CHECK-NEXT: call void @foo(i32 1)
96 ; CHECK-NEXT: br label [[COMMON_RET]]
98 ; CHECK-NEXT: call void @foo(i32 2)
99 ; CHECK-NEXT: br label [[COMMON_RET]]
101 ; CHECK-NEXT: call void @foo(i32 3)
102 ; CHECK-NEXT: br label [[COMMON_RET]]
104 switch i2 %a, label %default [i2 0, label %case0
109 call void @foo(i32 0)
112 call void @foo(i32 1)
115 call void @foo(i32 2)
118 call void @foo(i32 3)
122 ; Negative test - check for possible overflow when computing
123 ; number of possible cases.
124 define void @test4(i128 %a) {
125 ; CHECK-LABEL: @test4(
126 ; CHECK-NEXT: switch i128 [[A:%.*]], label [[DEFAULT:%.*]] [
127 ; CHECK-NEXT: i128 0, label [[CASE0:%.*]]
128 ; CHECK-NEXT: i128 1, label [[CASE1:%.*]]
131 ; CHECK-NEXT: ret void
133 ; CHECK-NEXT: call void @foo(i32 0)
134 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
136 ; CHECK-NEXT: call void @foo(i32 1)
137 ; CHECK-NEXT: br label [[COMMON_RET]]
139 ; CHECK-NEXT: call void @foo(i32 2)
140 ; CHECK-NEXT: br label [[COMMON_RET]]
142 switch i128 %a, label %default [i128 0, label %case0
143 i128 1, label %case1]
146 call void @foo(i32 0)
149 call void @foo(i32 1)
152 call void @foo(i32 2)
156 ; All but one bit known zero
157 define void @test5(i8 %a) {
158 ; CHECK-LABEL: @test5(
159 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[A:%.*]], 2
160 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
161 ; CHECK-NEXT: [[A_OFF:%.*]] = add i8 [[A]], -1
162 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[A_OFF]], 1
163 ; CHECK-NEXT: br i1 [[SWITCH]], label [[TRUE:%.*]], label [[FALSE:%.*]]
165 ; CHECK-NEXT: ret void
167 ; CHECK-NEXT: call void @foo(i32 1)
168 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
170 ; CHECK-NEXT: call void @foo(i32 3)
171 ; CHECK-NEXT: br label [[COMMON_RET]]
173 %cmp = icmp ult i8 %a, 2
174 call void @llvm.assume(i1 %cmp)
175 switch i8 %a, label %default [i8 1, label %true
178 call void @foo(i32 1)
181 call void @foo(i32 3)
184 call void @foo(i32 2)
188 ;; All but one bit known one
189 define void @test6(i8 %a) {
190 ; CHECK-LABEL: @test6(
191 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[A:%.*]], -2
192 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[AND]], -2
193 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
194 ; CHECK-NEXT: [[A_OFF:%.*]] = add i8 [[A]], 1
195 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[A_OFF]], 1
196 ; CHECK-NEXT: br i1 [[SWITCH]], label [[TRUE:%.*]], label [[FALSE:%.*]]
198 ; CHECK-NEXT: ret void
200 ; CHECK-NEXT: call void @foo(i32 1)
201 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
203 ; CHECK-NEXT: call void @foo(i32 3)
204 ; CHECK-NEXT: br label [[COMMON_RET]]
206 %and = and i8 %a, 254
207 %cmp = icmp eq i8 %and, 254
208 call void @llvm.assume(i1 %cmp)
209 switch i8 %a, label %default [i8 255, label %true
210 i8 254, label %false]
212 call void @foo(i32 1)
215 call void @foo(i32 3)
218 call void @foo(i32 2)
222 ; Check that we can eliminate both dead cases and dead defaults
223 ; within a single run of simplifycfg
224 define void @test7(i8 %a) {
225 ; CHECK-LABEL: @test7(
226 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[A:%.*]], -2
227 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[AND]], -2
228 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
229 ; CHECK-NEXT: [[A_OFF:%.*]] = add i8 [[A]], 1
230 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[A_OFF]], 1
231 ; CHECK-NEXT: br i1 [[SWITCH]], label [[TRUE:%.*]], label [[FALSE:%.*]]
233 ; CHECK-NEXT: ret void
235 ; CHECK-NEXT: call void @foo(i32 1)
236 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
238 ; CHECK-NEXT: call void @foo(i32 3)
239 ; CHECK-NEXT: br label [[COMMON_RET]]
241 %and = and i8 %a, 254
242 %cmp = icmp eq i8 %and, 254
243 call void @llvm.assume(i1 %cmp)
244 switch i8 %a, label %default [i8 255, label %true
246 i8 0, label %also_dead]
248 call void @foo(i32 1)
251 call void @foo(i32 3)
254 call void @foo(i32 5)
257 call void @foo(i32 2)
261 declare void @llvm.assume(i1)