switch.ll
6.14 KB
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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -ipsccp < %s | FileCheck %s
; Make sure we always consider the default edge executable for a switch
; with no cases.
declare void @foo()
define void @test1() {
; CHECK-LABEL: @test1(
; CHECK-NEXT: switch i32 undef, label [[D:%.*]] [
; CHECK-NEXT: ]
; CHECK: d:
; CHECK-NEXT: call void @foo()
; CHECK-NEXT: ret void
;
switch i32 undef, label %d []
d:
call void @foo()
ret void
}
define i32 @test_duplicate_successors_phi(i1 %c, i32 %x) {
; CHECK-LABEL: @test_duplicate_successors_phi(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C:%.*]], label [[SWITCH:%.*]], label [[END:%.*]]
; CHECK: switch:
; CHECK-NEXT: br label [[SWITCH_DEFAULT:%.*]]
; CHECK: switch.default:
; CHECK-NEXT: ret i32 -1
; CHECK: end:
; CHECK-NEXT: ret i32 [[X:%.*]]
;
entry:
br i1 %c, label %switch, label %end
switch:
switch i32 -1, label %switch.default [
i32 0, label %end
i32 1, label %end
]
switch.default:
ret i32 -1
end:
%phi = phi i32 [ %x, %entry ], [ 1, %switch ], [ 1, %switch ]
ret i32 %phi
}
define i32 @test_duplicate_successors_phi_2(i1 %c, i32 %x) {
; CHECK-LABEL: @test_duplicate_successors_phi_2(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C:%.*]], label [[SWITCH:%.*]], label [[END:%.*]]
; CHECK: switch:
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[X:%.*]], [[ENTRY:%.*]] ], [ 1, [[SWITCH]] ]
; CHECK-NEXT: ret i32 [[PHI]]
;
entry:
br i1 %c, label %switch, label %end
switch:
switch i32 0, label %switch.default [
i32 0, label %end
i32 1, label %end
]
switch.default:
ret i32 -1
end:
%phi = phi i32 [ %x, %entry ], [ 1, %switch ], [ 1, %switch ]
ret i32 %phi
}
define i32 @test_duplicate_successors_phi_3(i1 %c1, i32* %p, i32 %y) {
; CHECK-LABEL: @test_duplicate_successors_phi_3(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C1:%.*]], label [[SWITCH:%.*]], label [[SWITCH_1:%.*]]
; CHECK: switch:
; CHECK-NEXT: [[X:%.*]] = load i32, i32* [[P:%.*]], align 4, !range !0
; CHECK-NEXT: switch i32 [[X]], label [[SWITCH_DEFAULT:%.*]] [
; CHECK-NEXT: i32 0, label [[SWITCH_DEFAULT]]
; CHECK-NEXT: i32 1, label [[SWITCH_0:%.*]]
; CHECK-NEXT: i32 2, label [[SWITCH_0]]
; CHECK-NEXT: ]
; CHECK: switch.default:
; CHECK-NEXT: ret i32 -1
; CHECK: switch.0:
; CHECK-NEXT: ret i32 0
; CHECK: switch.1:
; CHECK-NEXT: ret i32 [[Y:%.*]]
;
entry:
br i1 %c1, label %switch, label %switch.1
switch:
%x = load i32, i32* %p, !range !{i32 0, i32 3}
switch i32 %x, label %switch.default [
i32 0, label %switch.default
i32 1, label %switch.0
i32 2, label %switch.0
i32 3, label %switch.1
i32 4, label %switch.1
]
switch.default:
ret i32 -1
switch.0:
ret i32 0
switch.1:
%phi = phi i32 [ %y, %entry ], [ 0, %switch ], [ 0, %switch ]
ret i32 %phi
}
; TODO: Determine that the default destination is dead.
define i32 @test_local_range(i32* %p) {
; CHECK-LABEL: @test_local_range(
; CHECK-NEXT: [[X:%.*]] = load i32, i32* [[P:%.*]], align 4, !range !0
; CHECK-NEXT: switch i32 [[X]], label [[SWITCH_DEFAULT:%.*]] [
; CHECK-NEXT: i32 0, label [[SWITCH_0:%.*]]
; CHECK-NEXT: i32 1, label [[SWITCH_1:%.*]]
; CHECK-NEXT: i32 2, label [[SWITCH_2:%.*]]
; CHECK-NEXT: ]
; CHECK: switch.default:
; CHECK-NEXT: ret i32 -1
; CHECK: switch.0:
; CHECK-NEXT: ret i32 0
; CHECK: switch.1:
; CHECK-NEXT: ret i32 1
; CHECK: switch.2:
; CHECK-NEXT: ret i32 2
;
%x = load i32, i32* %p, !range !{i32 0, i32 3}
switch i32 %x, label %switch.default [
i32 0, label %switch.0
i32 1, label %switch.1
i32 2, label %switch.2
i32 3, label %switch.3
]
switch.default:
ret i32 -1
switch.0:
ret i32 0
switch.1:
ret i32 1
switch.2:
ret i32 2
switch.3:
ret i32 3
}
; TODO: Determine that case i3 is dead, even though the edge is shared?
define i32 @test_duplicate_successors(i32* %p) {
; CHECK-LABEL: @test_duplicate_successors(
; CHECK-NEXT: [[X:%.*]] = load i32, i32* [[P:%.*]], align 4, !range !0
; CHECK-NEXT: switch i32 [[X]], label [[SWITCH_DEFAULT:%.*]] [
; CHECK-NEXT: i32 0, label [[SWITCH_0:%.*]]
; CHECK-NEXT: i32 1, label [[SWITCH_0]]
; CHECK-NEXT: i32 2, label [[SWITCH_1:%.*]]
; CHECK-NEXT: i32 3, label [[SWITCH_1]]
; CHECK-NEXT: ]
; CHECK: switch.default:
; CHECK-NEXT: ret i32 -1
; CHECK: switch.0:
; CHECK-NEXT: ret i32 0
; CHECK: switch.1:
; CHECK-NEXT: ret i32 1
;
%x = load i32, i32* %p, !range !{i32 0, i32 3}
switch i32 %x, label %switch.default [
i32 0, label %switch.0
i32 1, label %switch.0
i32 2, label %switch.1
i32 3, label %switch.1
i32 4, label %switch.2
i32 5, label %switch.2
]
switch.default:
ret i32 -1
switch.0:
ret i32 0
switch.1:
ret i32 1
switch.2:
ret i32 2
}
; Case i32 2 is dead as well, but this cannot be determined based on
; range information.
define internal i32 @test_ip_range(i32 %x) {
; CHECK-LABEL: @test_ip_range(
; CHECK-NEXT: switch i32 [[X:%.*]], label [[SWITCH_DEFAULT:%.*]] [
; CHECK-NEXT: i32 3, label [[SWITCH_3:%.*]]
; CHECK-NEXT: i32 1, label [[SWITCH_1:%.*]]
; CHECK-NEXT: i32 2, label [[SWITCH_2:%.*]]
; CHECK-NEXT: ], !prof !1
; CHECK: switch.default:
; CHECK-NEXT: ret i32 -1
; CHECK: switch.1:
; CHECK-NEXT: ret i32 1
; CHECK: switch.2:
; CHECK-NEXT: ret i32 2
; CHECK: switch.3:
; CHECK-NEXT: ret i32 3
;
switch i32 %x, label %switch.default [
i32 0, label %switch.0
i32 1, label %switch.1
i32 2, label %switch.2
i32 3, label %switch.3
], !prof !{!"branch_weights", i32 1, i32 2, i32 3, i32 4, i32 5}
switch.default:
ret i32 -1
switch.0:
ret i32 0
switch.1:
ret i32 1
switch.2:
ret i32 2
switch.3:
ret i32 3
}
define void @call_test_ip_range() {
; CHECK-LABEL: @call_test_ip_range(
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @test_ip_range(i32 1)
; CHECK-NEXT: [[TMP2:%.*]] = call i32 @test_ip_range(i32 3)
; CHECK-NEXT: ret void
;
call i32 @test_ip_range(i32 1)
call i32 @test_ip_range(i32 3)
ret void
}
declare void @llvm.assume(i1)
; CHECK: !1 = !{!"branch_weights", i32 1, i32 5, i32 3, i32 4}