1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=lower-switch -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 [[CASE_D:%.*]]
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: [[RESD:%.*]] = call i32 @caseD()
25 ; CHECK-NEXT: br label [[EXIT]]
27 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
28 ; CHECK-NEXT: ret i32 [[RES]]
31 %trunc = trunc i32 %val to i2
32 switch i2 %trunc, label %case.D [
33 i2 1, label %case.1 ; i2 1
34 i2 2, label %case.2 ; i2 -2
36 ; It's known that %val can not be less than -2 or greater than 1
39 %res1 = call i32 @case1()
43 %res2 = call i32 @case2()
47 %resD = call i32 @caseD()
51 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
55 ; Check that we do not generate redundant comparisons that would have results
56 ; known at compile time due to limited range of the value being switch'ed over.
58 ; CHECK-LABEL: @test2(
60 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG0:![0-9]+]]
61 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
63 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 2
64 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
66 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
67 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_D:%.*]]
69 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
70 ; CHECK-NEXT: br label [[EXIT:%.*]]
72 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
73 ; CHECK-NEXT: br label [[EXIT]]
75 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
76 ; CHECK-NEXT: br label [[EXIT]]
78 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
79 ; CHECK-NEXT: ret i32 [[RES]]
82 %val = call i32 @getVal(), !range !0
83 switch i32 %val, label %case.D [
87 ; It's known that %val can not be less than 1
90 %res1 = call i32 @case1()
94 %res2 = call i32 @case2()
98 %resD = call i32 @caseD()
102 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
107 ; 1) some of the non-default cases are unreachable due to the !range constraint,
108 ; 2) the default case is unreachable as non-default cases cover the range fully.
109 define i32 @test3() {
110 ; CHECK-LABEL: @test3(
112 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG1:![0-9]+]]
113 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
115 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
116 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_1:%.*]]
118 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
119 ; CHECK-NEXT: br label [[EXIT:%.*]]
121 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
122 ; CHECK-NEXT: br label [[EXIT]]
124 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ]
125 ; CHECK-NEXT: ret i32 [[RES]]
128 %val = call i32 @getVal(), !range !1
129 switch i32 %val, label %case.D [
136 %res1 = call i32 @case1()
140 %res2 = call i32 @case2()
144 %resD = call i32 @caseD()
148 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
153 ; 1) some of the non-default cases are unreachable due to the !range constraint,
154 ; 2) the default case is still reachable as non-default cases do not cover the
156 define i32 @test4() {
157 ; CHECK-LABEL: @test4(
159 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG2:![0-9]+]]
160 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
162 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 2
163 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
165 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
166 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_D:%.*]]
168 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
169 ; CHECK-NEXT: br label [[EXIT:%.*]]
171 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
172 ; CHECK-NEXT: br label [[EXIT]]
174 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
175 ; CHECK-NEXT: br label [[EXIT]]
177 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
178 ; CHECK-NEXT: ret i32 [[RES]]
181 %val = call i32 @getVal(), !range !2
182 switch i32 %val, label %case.D [
188 %res1 = call i32 @case1()
192 %res2 = call i32 @case2()
196 %resD = call i32 @caseD()
200 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
205 ; 1) some of the non-default cases are unreachable due to the !range constraint,
206 ; 2) the default case appears to be unreachable as non-default cases cover the
207 ; range fully, but its basic block actually is reachable from the switch via
208 ; one of the non-default cases.
209 define i32 @test5(i1 %cond) {
210 ; CHECK-LABEL: @test5(
212 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
214 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG1]]
215 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
217 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 3
218 ; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK:%.*]], label [[CASE_1:%.*]]
220 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
221 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_1]], label [[CASE_D]]
223 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
224 ; CHECK-NEXT: br label [[EXIT:%.*]]
226 ; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[LEAFBLOCK]] ]
227 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
228 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
229 ; CHECK-NEXT: br label [[EXIT]]
231 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RESD]], [[CASE_D]] ]
232 ; CHECK-NEXT: ret i32 [[RES]]
235 br i1 %cond, label %switch, label %case.D
238 %val = call i32 @getVal(), !range !1
239 switch i32 %val, label %case.D [
246 %res1 = call i32 @case1()
250 %delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
251 %resD.tmp = call i32 @caseD()
252 %resD = add i32 %resD.tmp, %delta
256 %res = phi i32 [ %res1, %case.1 ], [ %resD, %case.D ]
261 ; 1) some of the non-default cases are unreachable due to the !range constraint,
262 ; 2) the default case appears to be unreachable as non-default cases cover the
263 ; range fully, but its basic block actually is reachable, though, from a
264 ; different basic block, not the switch itself.
265 define i32 @test6(i1 %cond) {
266 ; CHECK-LABEL: @test6(
268 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
270 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG1]]
271 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
273 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
274 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_1:%.*]]
276 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
277 ; CHECK-NEXT: br label [[EXIT:%.*]]
279 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
280 ; CHECK-NEXT: br label [[EXIT]]
282 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
283 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], 0
284 ; CHECK-NEXT: br label [[EXIT]]
286 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
287 ; CHECK-NEXT: ret i32 [[RES]]
290 br i1 %cond, label %switch, label %case.D
293 %val = call i32 @getVal(), !range !1
294 switch i32 %val, label %case.D [
301 %res1 = call i32 @case1()
305 %res2 = call i32 @case2()
309 %delta = phi i32 [ 0, %entry ], [ 20, %switch ]
310 %resD.tmp = call i32 @caseD()
311 %resD = add i32 %resD.tmp, %delta
315 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
320 ; 1) switch appears to have a non-empty set of non-default cases, but all of
321 ; them reference the default case basic block.
322 define i32 @test7(i1 %cond) {
323 ; CHECK-LABEL: @test7(
325 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
327 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG1]]
328 ; CHECK-NEXT: br label [[CASE_D]]
330 ; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[SWITCH]] ]
331 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
332 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
333 ; CHECK-NEXT: br label [[EXIT:%.*]]
335 ; CHECK-NEXT: ret i32 [[RESD]]
338 br i1 %cond, label %switch, label %case.D
341 %val = call i32 @getVal(), !range !1
342 switch i32 %val, label %case.D [
347 %delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
348 %resD.tmp = call i32 @caseD()
349 %resD = add i32 %resD.tmp, %delta
357 ; 1) some of the non-default cases are unreachable due to the !range constraint,
358 ; 2) the default case appears to be unreachable as non-default cases cover the
359 ; range fully, but its basic block actually is reachable from the switch via
360 ; one of the non-default cases,
361 ; 3) such cases lie at the boundary of the range of values covered by
362 ; non-default cases, and if removed, do not change the fact that the rest of
363 ; the cases fully covers the value range.
364 define i32 @test8(i1 %cond) {
365 ; CHECK-LABEL: @test8(
367 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
369 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range [[RNG3:![0-9]+]]
370 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
372 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
373 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_1:%.*]]
375 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
376 ; CHECK-NEXT: br label [[EXIT:%.*]]
378 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
379 ; CHECK-NEXT: br label [[EXIT]]
381 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
382 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], 0
383 ; CHECK-NEXT: br label [[EXIT]]
385 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
386 ; CHECK-NEXT: ret i32 [[RES]]
389 br i1 %cond, label %switch, label %case.D
392 %val = call i32 @getVal(), !range !3
393 switch i32 %val, label %case.D [
400 %res1 = call i32 @case1()
404 %res2 = call i32 @case2()
408 %delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
409 %resD.tmp = call i32 @caseD()
410 %resD = add i32 %resD.tmp, %delta
414 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
419 ; 1) the default case appears to be unreachable as non-default cases cover the
420 ; range fully, but its basic block actually is reachable from the switch via
421 ; more than one non-default case.
422 define i32 @test9(i1 %cond, i2 %val) {
423 ; CHECK-LABEL: @test9(
425 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
427 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
429 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp sge i2 [[VAL:%.*]], 0
430 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_1:%.*]], label [[CASE_D]]
432 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
433 ; CHECK-NEXT: br label [[EXIT:%.*]]
435 ; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[LEAFBLOCK]] ]
436 ; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
437 ; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
438 ; CHECK-NEXT: br label [[EXIT]]
440 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RESD]], [[CASE_D]] ]
441 ; CHECK-NEXT: ret i32 [[RES]]
444 br i1 %cond, label %switch, label %case.D
447 switch i2 %val, label %case.D [
455 %res1 = call i32 @case1()
459 %delta = phi i32 [20, %switch ], [ 20, %switch ], [ 20, %switch ], [ 0, %entry ]
460 %resD.tmp = call i32 @caseD()
461 %resD = add i32 %resD.tmp, %delta
465 %res = phi i32 [ %res1, %case.1 ], [ %resD, %case.D ]
469 ; Check that we do not generate redundant comparisons that would have results
470 ; known at compile time due to limited range of the value being switch'ed over.
471 define i32 @test10() {
472 ; CHECK-LABEL: @test10(
474 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal()
475 ; CHECK-NEXT: [[COND_LEFT:%.*]] = icmp sge i32 [[VAL]], 1
476 ; CHECK-NEXT: [[COND_RIGHT:%.*]] = icmp sle i32 [[VAL]], 6
477 ; CHECK-NEXT: [[COND:%.*]] = and i1 [[COND_LEFT]], [[COND_RIGHT]]
478 ; CHECK-NEXT: br i1 [[COND]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
480 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
482 ; CHECK-NEXT: [[VAL_OFF:%.*]] = add i32 [[VAL]], -3
483 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp ule i32 [[VAL_OFF]], 1
484 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_1:%.*]]
486 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
487 ; CHECK-NEXT: br label [[EXIT:%.*]]
489 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
490 ; CHECK-NEXT: br label [[EXIT]]
492 ; CHECK-NEXT: br label [[EXIT]]
494 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ 0, [[CASE_D]] ]
495 ; CHECK-NEXT: ret i32 [[RES]]
498 %val = call i32 @getVal()
499 %cond.left = icmp sge i32 %val, 1
500 %cond.right = icmp sle i32 %val, 6
501 %cond = and i1 %cond.left, %cond.right
502 br i1 %cond, label %switch, label %case.D
505 switch i32 %val, label %case.D [
513 ; It's known that %val <- [1, 6]
516 %res1 = call i32 @case1()
520 %res2 = call i32 @case2()
524 %resD = phi i32 [ 20, %switch ], [ 0, %entry ]
528 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
532 ; Check that we do not generate redundant comparisons that would have results
533 ; known at compile time due to limited range of the value being switch'ed over.
534 define i32 @test11() {
535 ; CHECK-LABEL: @test11(
537 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal()
538 ; CHECK-NEXT: [[VAL_ZEXT:%.*]] = zext i32 [[VAL]] to i64
539 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
541 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i64 [[VAL_ZEXT]], 1
542 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
544 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i64 [[VAL_ZEXT]], 1
545 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_D:%.*]]
547 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
548 ; CHECK-NEXT: br label [[EXIT:%.*]]
550 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
551 ; CHECK-NEXT: br label [[EXIT]]
553 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
554 ; CHECK-NEXT: br label [[EXIT]]
556 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
557 ; CHECK-NEXT: ret i32 [[RES]]
560 %val = call i32 @getVal()
561 %val.zext = zext i32 %val to i64
562 switch i64 %val.zext, label %case.D [
566 ; It's known that %val can not be less than 0
569 %res1 = call i32 @case1()
573 %res2 = call i32 @case2()
577 %resD = call i32 @caseD()
581 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
585 ; Check that we do not generate redundant comparisons that would have results
586 ; known at compile time due to limited range of the value being switch'ed over.
587 define void @test12(i1 %arg) {
588 ; CHECK-LABEL: @test12(
590 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
592 ; CHECK-NEXT: [[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[LATCH:%.*]] ]
593 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
595 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[INDVAR]], 1
596 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
598 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[INDVAR]], 1
599 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[LATCH]]
601 ; CHECK-NEXT: br label [[LATCH]]
603 ; CHECK-NEXT: br label [[LATCH]]
605 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[INDVAR]], 1
606 ; CHECK-NEXT: br i1 %arg, label [[EXIT:%.*]], label [[FOR_BODY]]
608 ; CHECK-NEXT: ret void
614 %indvar = phi i32 [ 0, %entry ], [ %inc, %latch ]
615 switch i32 %indvar, label %latch [
619 ; It's known that %indvar can not be less than 0
628 %inc = add nuw nsw i32 %indvar, 1
629 br i1 %arg, label %exit, label %for.body
635 ; Check that we do not generate redundant comparisons that would have results
636 ; known at compile time due to limited range of the value being switch'ed over.
637 define void @test13(i32 %val) {
638 ; CHECK-LABEL: @test13(
640 ; CHECK-NEXT: [[TMP:%.*]] = and i32 [[VAL:%.*]], 7
641 ; CHECK-NEXT: br label [[BB33:%.*]]
643 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
645 ; CHECK-NEXT: [[TMP_OFF:%.*]] = add i32 [[TMP]], -2
646 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp ule i32 [[TMP_OFF]], 1
647 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[BB34:%.*]], label [[BB35:%.*]]
649 ; CHECK-NEXT: br label [[BB38:%.*]]
651 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
653 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[TMP]], 6
654 ; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK1:%.*]], label [[BB37:%.*]]
656 ; CHECK-NEXT: [[SWITCHLEAF2:%.*]] = icmp sle i32 [[TMP]], 1
657 ; CHECK-NEXT: br i1 [[SWITCHLEAF2]], label [[BB37]], label [[BB38]]
659 ; CHECK-NEXT: br label [[BB38]]
661 ; CHECK-NEXT: br label [[BB33]]
664 %tmp = and i32 %val, 7
668 switch i32 %tmp, label %bb35 [
677 switch i32 %tmp, label %bb38 [
683 ; It's known that %tmp <- [0, 1] U [4, 7]
692 ; Check that we do not generate redundant comparisons that would have results
693 ; known at compile time due to limited range of the value being switch'ed over.
694 define i32 @test14() {
695 ; CHECK-LABEL: @test14(
697 ; CHECK-NEXT: [[TMP:%.*]] = call i32 @getVal(), !range [[RNG4:![0-9]+]]
698 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP]])
699 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
701 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 1
702 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
704 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
705 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_D:%.*]]
707 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
708 ; CHECK-NEXT: br label [[EXIT:%.*]]
710 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
711 ; CHECK-NEXT: br label [[EXIT]]
713 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
714 ; CHECK-NEXT: br label [[EXIT]]
716 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
717 ; CHECK-NEXT: ret i32 [[RES]]
720 %tmp = call i32 @getVal(), !range !4
721 %val = call i32 @llvm.ctpop.i32(i32 %tmp)
722 switch i32 %val, label %case.D [
726 ; It's known that %val <- [0, 2]
729 %res1 = call i32 @case1()
733 %res2 = call i32 @case2()
737 %resD = call i32 @caseD()
741 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
745 ; Check that we do not generate redundant comparisons that would have results
746 ; known at compile time due to limited range of the value being switch'ed over.
747 define i32 @test15() {
748 ; CHECK-LABEL: @test15(
750 ; CHECK-NEXT: [[TMP:%.*]] = call i32 @getVal()
751 ; CHECK-NEXT: [[VAL:%.*]] = urem i32 [[TMP]], 3
752 ; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
754 ; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 1
755 ; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
757 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
758 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_D:%.*]]
760 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
761 ; CHECK-NEXT: br label [[EXIT:%.*]]
763 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
764 ; CHECK-NEXT: br label [[EXIT]]
766 ; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
767 ; CHECK-NEXT: br label [[EXIT]]
769 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
770 ; CHECK-NEXT: ret i32 [[RES]]
773 %tmp = call i32 @getVal()
774 %val = urem i32 %tmp, 3
775 switch i32 %val, label %case.D [
779 ; It's known that %val <- [0, 2]
782 %res1 = call i32 @case1()
786 %res2 = call i32 @case2()
790 %resD = call i32 @caseD()
794 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
798 ; Check that we do not generate redundant comparisons that would have results
799 ; known at compile time due to limited range of the value being switch'ed over.
800 define i32 @test16(float %f) {
801 ; CHECK-LABEL: @test16(
803 ; CHECK-NEXT: [[I:%.*]] = fptosi float [[F:%.*]] to i64
804 ; CHECK-NEXT: [[COND_LEFT:%.*]] = icmp slt i64 [[I]], 0
805 ; CHECK-NEXT: [[CLAMP_LEFT:%.*]] = select i1 [[COND_LEFT]], i64 0, i64 [[I]]
806 ; CHECK-NEXT: [[COND_RIGHT:%.*]] = icmp sgt i64 [[I]], 3
807 ; CHECK-NEXT: [[CLAMP:%.*]] = select i1 [[COND_RIGHT]], i64 3, i64 [[CLAMP_LEFT]]
808 ; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
810 ; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp sge i64 [[CLAMP]], 2
811 ; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[CASE_1:%.*]]
813 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
814 ; CHECK-NEXT: br label [[EXIT:%.*]]
816 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
817 ; CHECK-NEXT: br label [[EXIT]]
819 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ]
820 ; CHECK-NEXT: ret i32 [[RES]]
823 %i = fptosi float %f to i64
824 %cond.left = icmp slt i64 %i, 0
825 %clamp.left = select i1 %cond.left, i64 0, i64 %i
826 %cond.right = icmp sgt i64 %i, 3
827 %clamp = select i1 %cond.right, i64 3, i64 %clamp.left
828 switch i64 %clamp, label %case.D [
834 ; It's known that %val <- [0, 3]
837 %res1 = call i32 @case1()
841 %res2 = call i32 @case2()
845 %resD = call i32 @caseD()
849 %res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
856 declare i32 @getVal()
857 declare i32 @llvm.ctpop.i32(i32)
859 !0 = !{i32 1, i32 257}
861 !2 = !{i32 2, i32 257}