• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt %s -lowerswitch -S | FileCheck %s
2
3define void @foo(i32 %x, i32* %p) {
4; Cases 2 and 4 are removed and become the new default case.
5; It is now enough to use two icmps to lower the switch.
6;
7; CHECK-LABEL: @foo
8; CHECK:       icmp slt i32 %x, 5
9; CHECK:       icmp eq i32 %x, 1
10; CHECK-NOT:   icmp
11;
12entry:
13  switch i32 %x, label %default [
14    i32 1, label %bb0
15    i32 2, label %popular
16    i32 4, label %popular
17    i32 5, label %bb1
18  ]
19bb0:
20  store i32 0, i32* %p
21  br label %exit
22bb1:
23  store i32 1, i32* %p
24  br label %exit
25popular:
26  store i32 2, i32* %p
27  br label %exit
28exit:
29  ret void
30default:
31  unreachable
32}
33
34define void @unreachable_gap(i64 %x, i32* %p) {
35; Cases 6 and INT64_MAX become the new default, but we still exploit the fact
36; that 3-4 is unreachable, so four icmps is enough.
37
38; CHECK-LABEL: @unreachable_gap
39; CHECK:       icmp slt i64 %x, 2
40; CHECK:       icmp slt i64 %x, 5
41; CHECK:       icmp eq  i64 %x, 5
42; CHECK:       icmp slt i64 %x, 1
43; CHECK-NOT:   icmp
44
45entry:
46  switch i64 %x, label %default [
47    i64 -9223372036854775808, label %bb0
48    i64 1, label %bb1
49    i64 2, label %bb2
50    i64 5, label %bb3
51    i64 6, label %bb4
52    i64 9223372036854775807, label %bb4
53  ]
54bb0:
55  store i32 0, i32* %p
56  br label %exit
57bb1:
58  store i32 1, i32* %p
59  br label %exit
60bb2:
61  store i32 2, i32* %p
62  br label %exit
63bb3:
64  store i32 3, i32* %p
65  br label %exit
66bb4:
67  store i32 4, i32* %p
68  br label %exit
69exit:
70  ret void
71default:
72  unreachable
73}
74
75
76
77define void @nocases(i32 %x, i32* %p) {
78; Don't fall over when there are no cases.
79;
80; CHECK-LABEL: @nocases
81; CHECK-LABEL: entry
82; CHECK-NEXT:  br label %default
83;
84entry:
85  switch i32 %x, label %default [
86  ]
87default:
88  unreachable
89}
90
91define void @nocasesleft(i32 %x, i32* %p) {
92; Cases 2 and 4 are removed and we are left with no cases.
93;
94; CHECK-LABEL: @nocasesleft
95; CHECK-LABEL: entry
96; CHECK-NEXT:  br label %popular
97;
98entry:
99  switch i32 %x, label %default [
100    i32 2, label %popular
101    i32 4, label %popular
102  ]
103popular:
104  store i32 2, i32* %p
105  br label %exit
106exit:
107  ret void
108default:
109  unreachable
110}
111