1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -lowerswitch -S | FileCheck %s
4 ; Check that we do not generate redundant comparisons that would have results
5 ; known at compile time due to limited range of the value being switch'ed over.
6 define i32 @test1(i32 %val) {
9 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[VAL:%.*]] to i2
10 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
12 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i2 [[TRUNC]], 1
13 ; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK:%.*]], label [[CASE_1:%.*]]
15 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i2 [[TRUNC]], -2
16 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
18 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
19 ; CHECK-NEXT: br label [[EXIT:%.*]]
21 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
22 ; CHECK-NEXT: br label [[EXIT]]
24 ; CHECK-NEXT: br label [[CASE_D:%.*]]
26 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
27 ; CHECK-NEXT: br label [[EXIT]]
29 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
30 ; CHECK-NEXT: ret i32 [[RES]]
33 %trunc = trunc i32 %val to i2
34 switch i2 %trunc, label %case.D [
35 i2 1, label %case.1 ; i2 1
36 i2 2, label %case.2 ; i2 -2
38 ; It's known that %val can not be less than -2 or greater than 1
41 %res1 = call i32 @case1()
45 %res2 = call i32 @case2()
49 %resD = call i32 @caseD()
53 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
57 ; Check that we do not generate redundant comparisons that would have results
58 ; known at compile time due to limited range of the value being switch'ed over.
60 ; CHECK-LABEL: @test2(
62 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !0
63 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
65 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 2
66 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
68 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
69 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
71 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
72 ; CHECK-NEXT: br label [[EXIT:%.*]]
74 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
75 ; CHECK-NEXT: br label [[EXIT]]
77 ; CHECK-NEXT: br label [[CASE_D:%.*]]
79 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
80 ; CHECK-NEXT: br label [[EXIT]]
82 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
83 ; CHECK-NEXT: ret i32 [[RES]]
86 %val = call i32 @getVal(), !range !0
87 switch i32 %val, label %case.D [
91 ; It's known that %val can not be less than 1
94 %res1 = call i32 @case1()
98 %res2 = call i32 @case2()
102 %resD = call i32 @caseD()
106 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
111 ; 1) some of the non-default cases are unreachable due to the !range constraint,
112 ; 2) the default case is unreachable as non-default cases cover the range fully.
113 define i32 @test3() {
114 ; CHECK-LABEL: @test3(
116 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
117 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
119 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
120 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
122 ; CHECK-NEXT: br label [[CASE_1:%.*]]
124 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
125 ; CHECK-NEXT: br label [[EXIT:%.*]]
127 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
128 ; CHECK-NEXT: br label [[EXIT]]
130 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ]
131 ; CHECK-NEXT: ret i32 [[RES]]
134 %val = call i32 @getVal(), !range !1
135 switch i32 %val, label %case.D [
142 %res1 = call i32 @case1()
146 %res2 = call i32 @case2()
150 %resD = call i32 @caseD()
154 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
159 ; 1) some of the non-default cases are unreachable due to the !range constraint,
160 ; 2) the default case is still reachable as non-default cases do not cover the
162 define i32 @test4() {
163 ; CHECK-LABEL: @test4(
165 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !2
166 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
168 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 2
169 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
171 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
172 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
174 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
175 ; CHECK-NEXT: br label [[EXIT:%.*]]
177 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
178 ; CHECK-NEXT: br label [[EXIT]]
180 ; CHECK-NEXT: br label [[CASE_D:%.*]]
182 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
183 ; CHECK-NEXT: br label [[EXIT]]
185 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
186 ; CHECK-NEXT: ret i32 [[RES]]
189 %val = call i32 @getVal(), !range !2
190 switch i32 %val, label %case.D [
196 %res1 = call i32 @case1()
200 %res2 = call i32 @case2()
204 %resD = call i32 @caseD()
208 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
213 ; 1) some of the non-default cases are unreachable due to the !range constraint,
214 ; 2) the default case appears to be unreachable as non-default cases cover the
215 ; range fully, but its basic block actually is reachable from the switch via
216 ; one of the non-default cases.
217 define i32 @test5(i1 %cond) {
218 ; CHECK-LABEL: @test5(
220 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
222 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
223 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
225 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 3
226 ; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK:%.*]], label [[CASE_1:%.*]]
228 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
229 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_1]], label [[NEWDEFAULT:%.*]]
231 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
232 ; CHECK-NEXT: br label [[EXIT:%.*]]
234 ; CHECK-NEXT: br label [[CASE_D]]
236 ; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[NEWDEFAULT]] ]
237 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
238 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
239 ; CHECK-NEXT: br label [[EXIT]]
241 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RESD]], [[CASE_D]] ]
242 ; CHECK-NEXT: ret i32 [[RES]]
245 br i1 %cond, label %switch, label %case.D
248 %val = call i32 @getVal(), !range !1
249 switch i32 %val, label %case.D [
256 %res1 = call i32 @case1()
260 %delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
261 %resD.tmp = call i32 @caseD()
262 %resD = add i32 %resD.tmp, %delta
266 %res = phi i32 [ %res1, %case.1 ], [ %resD, %case.D ]
271 ; 1) some of the non-default cases are unreachable due to the !range constraint,
272 ; 2) the default case appears to be unreachable as non-default cases cover the
273 ; range fully, but its basic block actually is reachable, though, from a
274 ; different basic block, not the switch itself.
275 define i32 @test6(i1 %cond) {
276 ; CHECK-LABEL: @test6(
278 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
280 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
281 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
283 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
284 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
286 ; CHECK-NEXT: br label [[CASE_1:%.*]]
288 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
289 ; CHECK-NEXT: br label [[EXIT:%.*]]
291 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
292 ; CHECK-NEXT: br label [[EXIT]]
294 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
295 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], 0
296 ; CHECK-NEXT: br label [[EXIT]]
298 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
299 ; CHECK-NEXT: ret i32 [[RES]]
302 br i1 %cond, label %switch, label %case.D
305 %val = call i32 @getVal(), !range !1
306 switch i32 %val, label %case.D [
313 %res1 = call i32 @case1()
317 %res2 = call i32 @case2()
321 %delta = phi i32 [ 0, %entry ], [ 20, %switch ]
322 %resD.tmp = call i32 @caseD()
323 %resD = add i32 %resD.tmp, %delta
327 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
332 ; 1) switch appears to have a non-empty set of non-default cases, but all of
333 ; them reference the default case basic block.
334 define i32 @test7(i1 %cond) {
335 ; CHECK-LABEL: @test7(
337 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
339 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
340 ; CHECK-NEXT: br label [[CASE_D]]
342 ; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[SWITCH]] ]
343 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
344 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
345 ; CHECK-NEXT: br label [[EXIT:%.*]]
347 ; CHECK-NEXT: ret i32 [[RESD]]
350 br i1 %cond, label %switch, label %case.D
353 %val = call i32 @getVal(), !range !1
354 switch i32 %val, label %case.D [
359 %delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
360 %resD.tmp = call i32 @caseD()
361 %resD = add i32 %resD.tmp, %delta
369 ; 1) some of the non-default cases are unreachable due to the !range constraint,
370 ; 2) the default case appears to be unreachable as non-default cases cover the
371 ; range fully, but its basic block actually is reachable from the switch via
372 ; one of the non-default cases,
373 ; 3) such cases lie at the boundary of the range of values covered by
374 ; non-default cases, and if removed, do not change the fact that the rest of
375 ; the cases fully covers the value range.
376 define i32 @test8(i1 %cond) {
377 ; CHECK-LABEL: @test8(
379 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
381 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !3
382 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
384 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
385 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
387 ; CHECK-NEXT: br label [[CASE_1:%.*]]
389 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
390 ; CHECK-NEXT: br label [[EXIT:%.*]]
392 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
393 ; CHECK-NEXT: br label [[EXIT]]
395 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
396 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], 0
397 ; CHECK-NEXT: br label [[EXIT]]
399 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
400 ; CHECK-NEXT: ret i32 [[RES]]
403 br i1 %cond, label %switch, label %case.D
406 %val = call i32 @getVal(), !range !3
407 switch i32 %val, label %case.D [
414 %res1 = call i32 @case1()
418 %res2 = call i32 @case2()
422 %delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
423 %resD.tmp = call i32 @caseD()
424 %resD = add i32 %resD.tmp, %delta
428 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
433 ; 1) the default case appears to be unreachable as non-default cases cover the
434 ; range fully, but its basic block actually is reachable from the switch via
435 ; more than one non-default case.
436 define i32 @test9(i1 %cond, i2 %val) {
437 ; CHECK-LABEL: @test9(
439 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
441 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
443 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp sge i2 [[VAL:%.*]], 0
444 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_1:%.*]], label [[NEWDEFAULT:%.*]]
446 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
447 ; CHECK-NEXT: br label [[EXIT:%.*]]
449 ; CHECK-NEXT: br label [[CASE_D]]
451 ; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 20, [[NEWDEFAULT]] ], [ 0, [[ENTRY:%.*]] ]
452 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
453 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
454 ; CHECK-NEXT: br label [[EXIT]]
456 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RESD]], [[CASE_D]] ]
457 ; CHECK-NEXT: ret i32 [[RES]]
460 br i1 %cond, label %switch, label %case.D
463 switch i2 %val, label %case.D [
471 %res1 = call i32 @case1()
475 %delta = phi i32 [20, %switch ], [ 20, %switch ], [ 20, %switch ], [ 0, %entry ]
476 %resD.tmp = call i32 @caseD()
477 %resD = add i32 %resD.tmp, %delta
481 %res = phi i32 [ %res1, %case.1 ], [ %resD, %case.D ]
485 ; Check that we do not generate redundant comparisons that would have results
486 ; known at compile time due to limited range of the value being switch'ed over.
487 define i32 @test10() {
488 ; CHECK-LABEL: @test10(
490 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal()
491 ; CHECK-NEXT: [[COND_LEFT:%.*]] = icmp sge i32 [[VAL]], 1
492 ; CHECK-NEXT: [[COND_RIGHT:%.*]] = icmp sle i32 [[VAL]], 6
493 ; CHECK-NEXT: [[COND:%.*]] = and i1 [[COND_LEFT]], [[COND_RIGHT]]
494 ; CHECK-NEXT: br i1 [[COND]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
496 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
498 ; CHECK-NEXT: [[VAL_OFF:%.*]] = add i32 [[VAL]], -3
499 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp ule i32 [[VAL_OFF]], 1
500 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
502 ; CHECK-NEXT: br label [[CASE_1:%.*]]
504 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
505 ; CHECK-NEXT: br label [[EXIT:%.*]]
507 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
508 ; CHECK-NEXT: br label [[EXIT]]
510 ; CHECK-NEXT: br label [[EXIT]]
512 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ 0, [[CASE_D]] ]
513 ; CHECK-NEXT: ret i32 [[RES]]
516 %val = call i32 @getVal()
517 %cond.left = icmp sge i32 %val, 1
518 %cond.right = icmp sle i32 %val, 6
519 %cond = and i1 %cond.left, %cond.right
520 br i1 %cond, label %switch, label %case.D
523 switch i32 %val, label %case.D [
531 ; It's known that %val <- [1, 6]
534 %res1 = call i32 @case1()
538 %res2 = call i32 @case2()
542 %resD = phi i32 [ 20, %switch ], [ 0, %entry ]
546 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
550 ; Check that we do not generate redundant comparisons that would have results
551 ; known at compile time due to limited range of the value being switch'ed over.
552 define i32 @test11() {
553 ; CHECK-LABEL: @test11(
555 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal()
556 ; CHECK-NEXT: [[VAL_ZEXT:%.*]] = zext i32 [[VAL]] to i64
557 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
559 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i64 [[VAL_ZEXT]], 1
560 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
562 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i64 [[VAL_ZEXT]], 1
563 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
565 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
566 ; CHECK-NEXT: br label [[EXIT:%.*]]
568 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
569 ; CHECK-NEXT: br label [[EXIT]]
571 ; CHECK-NEXT: br label [[CASE_D:%.*]]
573 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
574 ; CHECK-NEXT: br label [[EXIT]]
576 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
577 ; CHECK-NEXT: ret i32 [[RES]]
580 %val = call i32 @getVal()
581 %val.zext = zext i32 %val to i64
582 switch i64 %val.zext, label %case.D [
586 ; It's known that %val can not be less than 0
589 %res1 = call i32 @case1()
593 %res2 = call i32 @case2()
597 %resD = call i32 @caseD()
601 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
605 ; Check that we do not generate redundant comparisons that would have results
606 ; known at compile time due to limited range of the value being switch'ed over.
607 define void @test12() {
608 ; CHECK-LABEL: @test12(
610 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
612 ; CHECK-NEXT: [[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[LATCH:%.*]] ]
613 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
615 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[INDVAR]], 1
616 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
618 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[INDVAR]], 1
619 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
621 ; CHECK-NEXT: br label [[LATCH]]
623 ; CHECK-NEXT: br label [[LATCH]]
625 ; CHECK-NEXT: br label [[LATCH]]
627 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[INDVAR]], 1
628 ; CHECK-NEXT: br i1 undef, label [[EXIT:%.*]], label [[FOR_BODY]]
630 ; CHECK-NEXT: ret void
636 %indvar = phi i32 [ 0, %entry ], [ %inc, %latch ]
637 switch i32 %indvar, label %latch [
641 ; It's known that %indvar can not be less than 0
650 %inc = add nuw nsw i32 %indvar, 1
651 br i1 undef, label %exit, label %for.body
657 ; Check that we do not generate redundant comparisons that would have results
658 ; known at compile time due to limited range of the value being switch'ed over.
659 define void @test13(i32 %val) {
660 ; CHECK-LABEL: @test13(
662 ; CHECK-NEXT: [[TMP:%.*]] = and i32 [[VAL:%.*]], 7
663 ; CHECK-NEXT: br label [[BB33:%.*]]
665 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
667 ; CHECK-NEXT: [[TMP_OFF:%.*]] = add i32 [[TMP]], -2
668 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp ule i32 [[TMP_OFF]], 1
669 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[BB34:%.*]], label [[NEWDEFAULT:%.*]]
671 ; CHECK-NEXT: br label [[BB38:%.*]]
673 ; CHECK-NEXT: br label [[BB35:%.*]]
675 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
677 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[TMP]], 6
678 ; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK2:%.*]], label [[BB37:%.*]]
680 ; CHECK-NEXT: [[SWITCHLEAF3:%.*]] = icmp sle i32 [[TMP]], 1
681 ; CHECK-NEXT: br i1 [[SWITCHLEAF3]], label [[BB37]], label [[NEWDEFAULT1:%.*]]
683 ; CHECK-NEXT: br label [[BB38]]
684 ; CHECK: NewDefault1:
685 ; CHECK-NEXT: br label [[BB38]]
687 ; CHECK-NEXT: br label [[BB33]]
690 %tmp = and i32 %val, 7
694 switch i32 %tmp, label %bb35 [
703 switch i32 %tmp, label %bb38 [
709 ; It's known that %tmp <- [0, 1] U [4, 7]
718 ; Check that we do not generate redundant comparisons that would have results
719 ; known at compile time due to limited range of the value being switch'ed over.
720 define i32 @test14() {
721 ; CHECK-LABEL: @test14(
723 ; CHECK-NEXT: [[TMP:%.*]] = call i32 @getVal(), !range !4
724 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP]])
725 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
727 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 1
728 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
730 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
731 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
733 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
734 ; CHECK-NEXT: br label [[EXIT:%.*]]
736 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
737 ; CHECK-NEXT: br label [[EXIT]]
739 ; CHECK-NEXT: br label [[CASE_D:%.*]]
741 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
742 ; CHECK-NEXT: br label [[EXIT]]
744 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
745 ; CHECK-NEXT: ret i32 [[RES]]
748 %tmp = call i32 @getVal(), !range !4
749 %val = call i32 @llvm.ctpop.i32(i32 %tmp)
750 switch i32 %val, label %case.D [
754 ; It's known that %val <- [0, 2]
757 %res1 = call i32 @case1()
761 %res2 = call i32 @case2()
765 %resD = call i32 @caseD()
769 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
773 ; Check that we do not generate redundant comparisons that would have results
774 ; known at compile time due to limited range of the value being switch'ed over.
775 define i32 @test15() {
776 ; CHECK-LABEL: @test15(
778 ; CHECK-NEXT: [[TMP:%.*]] = call i32 @getVal()
779 ; CHECK-NEXT: [[VAL:%.*]] = urem i32 [[TMP]], 3
780 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
782 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 1
783 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
785 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
786 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
788 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
789 ; CHECK-NEXT: br label [[EXIT:%.*]]
791 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
792 ; CHECK-NEXT: br label [[EXIT]]
794 ; CHECK-NEXT: br label [[CASE_D:%.*]]
796 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
797 ; CHECK-NEXT: br label [[EXIT]]
799 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
800 ; CHECK-NEXT: ret i32 [[RES]]
803 %tmp = call i32 @getVal()
804 %val = urem i32 %tmp, 3
805 switch i32 %val, label %case.D [
809 ; It's known that %val <- [0, 2]
812 %res1 = call i32 @case1()
816 %res2 = call i32 @case2()
820 %resD = call i32 @caseD()
824 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
828 ; Check that we do not generate redundant comparisons that would have results
829 ; known at compile time due to limited range of the value being switch'ed over.
830 define i32 @test16(float %f) {
831 ; CHECK-LABEL: @test16(
833 ; CHECK-NEXT: [[I:%.*]] = fptosi float [[F:%.*]] to i64
834 ; CHECK-NEXT: [[COND_LEFT:%.*]] = icmp slt i64 [[I]], 0
835 ; CHECK-NEXT: [[CLAMP_LEFT:%.*]] = select i1 [[COND_LEFT]], i64 0, i64 [[I]]
836 ; CHECK-NEXT: [[COND_RIGHT:%.*]] = icmp sgt i64 [[I]], 3
837 ; CHECK-NEXT: [[CLAMP:%.*]] = select i1 [[COND_RIGHT]], i64 3, i64 [[CLAMP_LEFT]]
838 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
840 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp sge i64 [[CLAMP]], 2
841 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
843 ; CHECK-NEXT: br label [[CASE_1:%.*]]
845 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
846 ; CHECK-NEXT: br label [[EXIT:%.*]]
848 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
849 ; CHECK-NEXT: br label [[EXIT]]
851 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ]
852 ; CHECK-NEXT: ret i32 [[RES]]
855 %i = fptosi float %f to i64
856 %cond.left = icmp slt i64 %i, 0
857 %clamp.left = select i1 %cond.left, i64 0, i64 %i
858 %cond.right = icmp sgt i64 %i, 3
859 %clamp = select i1 %cond.right, i64 3, i64 %clamp.left
860 switch i64 %clamp, label %case.D [
866 ; It's known that %val <- [0, 3]
869 %res1 = call i32 @case1()
873 %res2 = call i32 @case2()
877 %resD = call i32 @caseD()
881 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
888 declare i32 @getVal()
889 declare i32 @llvm.ctpop.i32(i32)
891 !0 = !{i32 1, i32 257}
893 !2 = !{i32 2, i32 257}