1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -lowerswitch -S | FileCheck %s
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test1(i32 %val) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[VAL:%.*]] to i2
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i2 [[TRUNC]], 1
; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK:%.*]], label [[CASE_1:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i2 [[TRUNC]], -2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D:%.*]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%trunc = trunc i32 %val to i2
switch i2 %trunc, label %case.D [
i2 1, label %case.1 ; i2 1
i2 2, label %case.2 ; i2 -2
]
; It's known that %val can not be less than -2 or greater than 1
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test2() {
; CHECK-LABEL: @test2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !0
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D:%.*]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%val = call i32 @getVal(), !range !0
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.2
]
; It's known that %val can not be less than 1
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Corner case:
; 1) some of the non-default cases are unreachable due to the !range constraint,
; 2) the default case is unreachable as non-default cases cover the range fully.
define i32 @test3() {
; CHECK-LABEL: @test3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_1:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%val = call i32 @getVal(), !range !1
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.2
i32 3, label %case.1
]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Corner case:
; 1) some of the non-default cases are unreachable due to the !range constraint,
; 2) the default case is still reachable as non-default cases do not cover the
; range fully.
define i32 @test4() {
; CHECK-LABEL: @test4(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !2
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D:%.*]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%val = call i32 @getVal(), !range !2
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.2
]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Corner case:
; 1) some of the non-default cases are unreachable due to the !range constraint,
; 2) the default case appears to be unreachable as non-default cases cover the
; range fully, but its basic block actually is reachable from the switch via
; one of the non-default cases.
define i32 @test5(i1 %cond) {
; CHECK-LABEL: @test5(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
; CHECK: switch:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 3
; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK:%.*]], label [[CASE_1:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_1]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D]]
; CHECK: case.D:
; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[NEWDEFAULT]] ]
; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
br i1 %cond, label %switch, label %case.D
switch:
%val = call i32 @getVal(), !range !1
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.D
i32 3, label %case.1
]
case.1:
%res1 = call i32 @case1()
br label %exit
case.D:
%delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
%resD.tmp = call i32 @caseD()
%resD = add i32 %resD.tmp, %delta
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %resD, %case.D ]
ret i32 %res
}
; Corner case:
; 1) some of the non-default cases are unreachable due to the !range constraint,
; 2) the default case appears to be unreachable as non-default cases cover the
; range fully, but its basic block actually is reachable, though, from a
; different basic block, not the switch itself.
define i32 @test6(i1 %cond) {
; CHECK-LABEL: @test6(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
; CHECK: switch:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_1:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], 0
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
br i1 %cond, label %switch, label %case.D
switch:
%val = call i32 @getVal(), !range !1
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.2
i32 3, label %case.1
]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%delta = phi i32 [ 0, %entry ], [ 20, %switch ]
%resD.tmp = call i32 @caseD()
%resD = add i32 %resD.tmp, %delta
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Corner case:
; 1) switch appears to have a non-empty set of non-default cases, but all of
; them reference the default case basic block.
define i32 @test7(i1 %cond) {
; CHECK-LABEL: @test7(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
; CHECK: switch:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !1
; CHECK-NEXT: br label [[CASE_D]]
; CHECK: case.D:
; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 20, [[SWITCH]] ]
; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret i32 [[RESD]]
;
entry:
br i1 %cond, label %switch, label %case.D
switch:
%val = call i32 @getVal(), !range !1
switch i32 %val, label %case.D [
i32 2, label %case.D
]
case.D:
%delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
%resD.tmp = call i32 @caseD()
%resD = add i32 %resD.tmp, %delta
br label %exit
exit:
ret i32 %resD
}
; Corner case:
; 1) some of the non-default cases are unreachable due to the !range constraint,
; 2) the default case appears to be unreachable as non-default cases cover the
; range fully, but its basic block actually is reachable from the switch via
; one of the non-default cases,
; 3) such cases lie at the boundary of the range of values covered by
; non-default cases, and if removed, do not change the fact that the rest of
; the cases fully covers the value range.
define i32 @test8(i1 %cond) {
; CHECK-LABEL: @test8(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
; CHECK: switch:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal(), !range !3
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_1:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], 0
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
br i1 %cond, label %switch, label %case.D
switch:
%val = call i32 @getVal(), !range !3
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.2
i32 3, label %case.D
]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%delta = phi i32 [ 0, %entry ], [ 20, %switch ], [ 20, %switch ]
%resD.tmp = call i32 @caseD()
%resD = add i32 %resD.tmp, %delta
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Corner case:
; 1) the default case appears to be unreachable as non-default cases cover the
; range fully, but its basic block actually is reachable from the switch via
; more than one non-default case.
define i32 @test9(i1 %cond, i2 %val) {
; CHECK-LABEL: @test9(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
; CHECK: switch:
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp sge i2 [[VAL:%.*]], 0
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_1:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D]]
; CHECK: case.D:
; CHECK-NEXT: [[DELTA:%.*]] = phi i32 [ 20, [[NEWDEFAULT]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[RESD_TMP:%.*]] = call i32 @caseD()
; CHECK-NEXT: [[RESD:%.*]] = add i32 [[RESD_TMP]], [[DELTA]]
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
br i1 %cond, label %switch, label %case.D
switch:
switch i2 %val, label %case.D [
i2 0, label %case.1
i2 1, label %case.1
i2 2, label %case.D
i2 3, label %case.D
]
case.1:
%res1 = call i32 @case1()
br label %exit
case.D:
%delta = phi i32 [20, %switch ], [ 20, %switch ], [ 20, %switch ], [ 0, %entry ]
%resD.tmp = call i32 @caseD()
%resD = add i32 %resD.tmp, %delta
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %resD, %case.D ]
ret i32 %res
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test10() {
; CHECK-LABEL: @test10(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal()
; CHECK-NEXT: [[COND_LEFT:%.*]] = icmp sge i32 [[VAL]], 1
; CHECK-NEXT: [[COND_RIGHT:%.*]] = icmp sle i32 [[VAL]], 6
; CHECK-NEXT: [[COND:%.*]] = and i1 [[COND_LEFT]], [[COND_RIGHT]]
; CHECK-NEXT: br i1 [[COND]], label [[SWITCH:%.*]], label [[CASE_D:%.*]]
; CHECK: switch:
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[VAL_OFF:%.*]] = add i32 [[VAL]], -3
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp ule i32 [[VAL_OFF]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_1:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: case.D:
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ 0, [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%val = call i32 @getVal()
%cond.left = icmp sge i32 %val, 1
%cond.right = icmp sle i32 %val, 6
%cond = and i1 %cond.left, %cond.right
br i1 %cond, label %switch, label %case.D
switch:
switch i32 %val, label %case.D [
i32 1, label %case.1
i32 2, label %case.1
i32 3, label %case.2
i32 4, label %case.2
i32 5, label %case.1
i32 6, label %case.1
]
; It's known that %val <- [1, 6]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = phi i32 [ 20, %switch ], [ 0, %entry ]
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test11() {
; CHECK-LABEL: @test11(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[VAL:%.*]] = call i32 @getVal()
; CHECK-NEXT: [[VAL_ZEXT:%.*]] = zext i32 [[VAL]] to i64
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i64 [[VAL_ZEXT]], 1
; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i64 [[VAL_ZEXT]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D:%.*]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%val = call i32 @getVal()
%val.zext = zext i32 %val to i64
switch i64 %val.zext, label %case.D [
i64 0, label %case.1
i64 1, label %case.2
]
; It's known that %val can not be less than 0
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define void @test12() {
; CHECK-LABEL: @test12(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
; CHECK: for.body:
; CHECK-NEXT: [[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[LATCH:%.*]] ]
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[INDVAR]], 1
; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[INDVAR]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: br label [[LATCH]]
; CHECK: case.2:
; CHECK-NEXT: br label [[LATCH]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[LATCH]]
; CHECK: latch:
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[INDVAR]], 1
; CHECK-NEXT: br i1 undef, label [[EXIT:%.*]], label [[FOR_BODY]]
; CHECK: exit:
; CHECK-NEXT: ret void
;
entry:
br label %for.body
for.body:
%indvar = phi i32 [ 0, %entry ], [ %inc, %latch ]
switch i32 %indvar, label %latch [
i32 0, label %case.1
i32 1, label %case.2
]
; It's known that %indvar can not be less than 0
case.1:
br label %latch
case.2:
br label %latch
latch:
%inc = add nuw nsw i32 %indvar, 1
br i1 undef, label %exit, label %for.body
exit:
ret void
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define void @test13(i32 %val) {
; CHECK-LABEL: @test13(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP:%.*]] = and i32 [[VAL:%.*]], 7
; CHECK-NEXT: br label [[BB33:%.*]]
; CHECK: bb33:
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[TMP_OFF:%.*]] = add i32 [[TMP]], -2
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp ule i32 [[TMP_OFF]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[BB34:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: bb34:
; CHECK-NEXT: br label [[BB38:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[BB35:%.*]]
; CHECK: bb35:
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[TMP]], 6
; CHECK-NEXT: br i1 [[PIVOT]], label [[LEAFBLOCK2:%.*]], label [[BB37:%.*]]
; CHECK: LeafBlock2:
; CHECK-NEXT: [[SWITCHLEAF3:%.*]] = icmp sle i32 [[TMP]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF3]], label [[BB37]], label [[NEWDEFAULT1:%.*]]
; CHECK: bb37:
; CHECK-NEXT: br label [[BB38]]
; CHECK: NewDefault1:
; CHECK-NEXT: br label [[BB38]]
; CHECK: bb38:
; CHECK-NEXT: br label [[BB33]]
;
entry:
%tmp = and i32 %val, 7
br label %bb33
bb33:
switch i32 %tmp, label %bb35 [
i32 2, label %bb34
i32 3, label %bb34
]
bb34:
br label %bb38
bb35:
switch i32 %tmp, label %bb38 [
i32 0, label %bb37
i32 1, label %bb37
i32 6, label %bb37
i32 7, label %bb37
]
; It's known that %tmp <- [0, 1] U [4, 7]
bb37:
br label %bb38
bb38:
br label %bb33
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test14() {
; CHECK-LABEL: @test14(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP:%.*]] = call i32 @getVal(), !range !4
; CHECK-NEXT: [[VAL:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP]])
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 1
; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D:%.*]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%tmp = call i32 @getVal(), !range !4
%val = call i32 @llvm.ctpop.i32(i32 %tmp)
switch i32 %val, label %case.D [
i32 0, label %case.1
i32 1, label %case.2
]
; It's known that %val <- [0, 2]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test15() {
; CHECK-LABEL: @test15(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP:%.*]] = call i32 @getVal()
; CHECK-NEXT: [[VAL:%.*]] = urem i32 [[TMP]], 3
; CHECK-NEXT: br label [[NODEBLOCK:%.*]]
; CHECK: NodeBlock:
; CHECK-NEXT: [[PIVOT:%.*]] = icmp slt i32 [[VAL]], 1
; CHECK-NEXT: br i1 [[PIVOT]], label [[CASE_1:%.*]], label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp eq i32 [[VAL]], 1
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_D:%.*]]
; CHECK: case.D:
; CHECK-NEXT: [[RESD:%.*]] = call i32 @caseD()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ], [ [[RESD]], [[CASE_D]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%tmp = call i32 @getVal()
%val = urem i32 %tmp, 3
switch i32 %val, label %case.D [
i32 0, label %case.1
i32 1, label %case.2
]
; It's known that %val <- [0, 2]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
; Check that we do not generate redundant comparisons that would have results
; known at compile time due to limited range of the value being switch'ed over.
define i32 @test16(float %f) {
; CHECK-LABEL: @test16(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[I:%.*]] = fptosi float [[F:%.*]] to i64
; CHECK-NEXT: [[COND_LEFT:%.*]] = icmp slt i64 [[I]], 0
; CHECK-NEXT: [[CLAMP_LEFT:%.*]] = select i1 [[COND_LEFT]], i64 0, i64 [[I]]
; CHECK-NEXT: [[COND_RIGHT:%.*]] = icmp sgt i64 [[I]], 3
; CHECK-NEXT: [[CLAMP:%.*]] = select i1 [[COND_RIGHT]], i64 3, i64 [[CLAMP_LEFT]]
; CHECK-NEXT: br label [[LEAFBLOCK:%.*]]
; CHECK: LeafBlock:
; CHECK-NEXT: [[SWITCHLEAF:%.*]] = icmp sge i64 [[CLAMP]], 2
; CHECK-NEXT: br i1 [[SWITCHLEAF]], label [[CASE_2:%.*]], label [[NEWDEFAULT:%.*]]
; CHECK: NewDefault:
; CHECK-NEXT: br label [[CASE_1:%.*]]
; CHECK: case.1:
; CHECK-NEXT: [[RES1:%.*]] = call i32 @case1()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: case.2:
; CHECK-NEXT: [[RES2:%.*]] = call i32 @case2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[RES1]], [[CASE_1]] ], [ [[RES2]], [[CASE_2]] ]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%i = fptosi float %f to i64
%cond.left = icmp slt i64 %i, 0
%clamp.left = select i1 %cond.left, i64 0, i64 %i
%cond.right = icmp sgt i64 %i, 3
%clamp = select i1 %cond.right, i64 3, i64 %clamp.left
switch i64 %clamp, label %case.D [
i64 0, label %case.1
i64 1, label %case.1
i64 2, label %case.2
i64 3, label %case.2
]
; It's known that %val <- [0, 3]
case.1:
%res1 = call i32 @case1()
br label %exit
case.2:
%res2 = call i32 @case2()
br label %exit
case.D:
%resD = call i32 @caseD()
br label %exit
exit:
%res = phi i32 [ %res1, %case.1 ], [ %res2, %case.2 ], [ %resD, %case.D ]
ret i32 %res
}
declare i32 @case1()
declare i32 @case2()
declare i32 @caseD()
declare i32 @getVal()
declare i32 @llvm.ctpop.i32(i32)
!0 = !{i32 1, i32 257}
!1 = !{i32 2, i32 3}
!2 = !{i32 2, i32 257}
!3 = !{i32 1, i32 3}
!4 = !{i32 0, i32 4}
|