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
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
; If we have a relational comparison with a constant, and said comparison is
; used in a select, and there is a constant in select, see if we can make
; those constants match.
; We can't ever get non-canonical scalar predicates.
; Likewise, while we can get non-canonical vector predicates, there must be an
; extra use on that `icmp`, which precludes the fold from happening.
;------------------------------------------------------------------------------;
; Canonical scalar predicates
;------------------------------------------------------------------------------;
!0 = !{!"branch_weights", i32 2000, i32 1}
define i32 @p0_ult_65536(i32 %x, i32 %y) {
; CHECK-LABEL: @p0_ult_65536(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 65535, i32 [[Y:%.*]], !prof !0
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
%r = select i1 %t, i32 %y, i32 65535, !prof !0
ret i32 %r
}
define i32 @p1_ugt(i32 %x, i32 %y) {
; CHECK-LABEL: @p1_ugt(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ult i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 65535, i32 [[Y:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ugt i32 %x, 65534
%r = select i1 %t, i32 %y, i32 65535
ret i32 %r
}
define i32 @p2_slt_65536(i32 %x, i32 %y) {
; CHECK-LABEL: @p2_slt_65536(
; CHECK-NEXT: [[T_INV:%.*]] = icmp sgt i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 65535, i32 [[Y:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp slt i32 %x, 65536
%r = select i1 %t, i32 %y, i32 65535
ret i32 %r
}
define i32 @p3_sgt(i32 %x, i32 %y) {
; CHECK-LABEL: @p3_sgt(
; CHECK-NEXT: [[T_INV:%.*]] = icmp slt i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 65535, i32 [[Y:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp sgt i32 %x, 65534
%r = select i1 %t, i32 %y, i32 65535
ret i32 %r
}
;------------------------------------------------------------------------------;
; Vectors
;------------------------------------------------------------------------------;
define <2 x i32> @p4_vec_splat_ult_65536(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p4_vec_splat_ult_65536(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 65535, i32 65535>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 65535>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ult <2 x i32> %x, <i32 65536, i32 65536>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 65535>
ret <2 x i32> %r
}
define <2 x i32> @p5_vec_splat_ugt(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p5_vec_splat_ugt(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ult <2 x i32> [[X:%.*]], <i32 65535, i32 65535>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 65535>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ugt <2 x i32> %x, <i32 65534, i32 65534>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 65535>
ret <2 x i32> %r
}
define <2 x i32> @p6_vec_splat_slt_65536(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p6_vec_splat_slt_65536(
; CHECK-NEXT: [[T_INV:%.*]] = icmp sgt <2 x i32> [[X:%.*]], <i32 65535, i32 65535>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 65535>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp slt <2 x i32> %x, <i32 65536, i32 65536>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 65535>
ret <2 x i32> %r
}
define <2 x i32> @p7_vec_splat_sgt(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p7_vec_splat_sgt(
; CHECK-NEXT: [[T_INV:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 65535, i32 65535>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 65535>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp sgt <2 x i32> %x, <i32 65534, i32 65534>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 65535>
ret <2 x i32> %r
}
; Vectors with undef
define <2 x i32> @p8_vec_nonsplat_undef0(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p8_vec_nonsplat_undef0(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 65535, i32 undef>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 65535>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ult <2 x i32> %x, <i32 65536, i32 undef>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 65535>
ret <2 x i32> %r
}
define <2 x i32> @p9_vec_nonsplat_undef1(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p9_vec_nonsplat_undef1(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 65535, i32 65535>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 undef>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ult <2 x i32> %x, <i32 65536, i32 65536>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 undef>
ret <2 x i32> %r
}
define <2 x i32> @p10_vec_nonsplat_undef2(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p10_vec_nonsplat_undef2(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 65535, i32 undef>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 undef>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ult <2 x i32> %x, <i32 65536, i32 undef>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 undef>
ret <2 x i32> %r
}
; Non-splat vectors
define <2 x i32> @p11_vec_nonsplat(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @p11_vec_nonsplat(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 65535, i32 32767>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T_INV]], <2 x i32> <i32 65535, i32 32767>, <2 x i32> [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ult <2 x i32> %x, <i32 65536, i32 32768>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 32767>
ret <2 x i32> %r
}
;------------------------------------------------------------------------------;
; Extra uses prevent the fold.
;------------------------------------------------------------------------------;
declare void @use1(i1)
define i32 @n12_extrause(i32 %x, i32 %y) {
; CHECK-LABEL: @n12_extrause(
; CHECK-NEXT: [[T:%.*]] = icmp ult i32 [[X:%.*]], 65536
; CHECK-NEXT: call void @use1(i1 [[T]])
; CHECK-NEXT: [[R:%.*]] = select i1 [[T]], i32 [[Y:%.*]], i32 65535
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
call void @use1(i1 %t)
%r = select i1 %t, i32 %y, i32 65535
ret i32 %r
}
;------------------------------------------------------------------------------;
; Commutativity
;------------------------------------------------------------------------------;
; We don't care if the constant in select is true value or false value
define i32 @p13_commutativity0(i32 %x, i32 %y) {
; CHECK-LABEL: @p13_commutativity0(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 [[Y:%.*]], i32 65535
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
%r = select i1 %t, i32 65535, i32 %y
ret i32 %r
}
; Which means, if both possibilities are constants, we must check both of them.
define i32 @p14_commutativity1(i32 %x, i32 %y) {
; CHECK-LABEL: @p14_commutativity1(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 42, i32 65535
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
%r = select i1 %t, i32 65535, i32 42
ret i32 %r
}
define i32 @p15_commutativity2(i32 %x, i32 %y) {
; CHECK-LABEL: @p15_commutativity2(
; CHECK-NEXT: [[T_INV:%.*]] = icmp ugt i32 [[X:%.*]], 65535
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 65535, i32 42
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
%r = select i1 %t, i32 42, i32 65535
ret i32 %r
}
;------------------------------------------------------------------------------;
; Negative tests
;------------------------------------------------------------------------------;
; For vectors, make sure we handle edge cases correctly
define <2 x i32> @n17_ult_zero(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @n17_ult_zero(
; CHECK-NEXT: [[T:%.*]] = icmp ult <2 x i32> [[X:%.*]], <i32 65536, i32 0>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T]], <2 x i32> [[Y:%.*]], <2 x i32> <i32 65535, i32 -1>
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ult <2 x i32> %x, <i32 65536, i32 0>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 -1>
ret <2 x i32> %r
}
define <2 x i32> @n18_ugt_allones(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @n18_ugt_allones(
; CHECK-NEXT: [[T:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 65534, i32 -1>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T]], <2 x i32> [[Y:%.*]], <2 x i32> <i32 65535, i32 0>
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp ugt <2 x i32> %x, <i32 65534, i32 -1>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 0>
ret <2 x i32> %r
}
define <2 x i32> @n19_slt_int_min(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @n19_slt_int_min(
; CHECK-NEXT: [[T:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 65536, i32 -2147483648>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T]], <2 x i32> [[Y:%.*]], <2 x i32> <i32 65535, i32 2147483647>
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp slt <2 x i32> %x, <i32 65536, i32 -2147483648>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 2147483647>
ret <2 x i32> %r
}
define <2 x i32> @n20_sgt_int_max(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @n20_sgt_int_max(
; CHECK-NEXT: [[T:%.*]] = icmp sgt <2 x i32> [[X:%.*]], <i32 65534, i32 2147483647>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[T]], <2 x i32> [[Y:%.*]], <2 x i32> <i32 65535, i32 -2147483648>
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%t = icmp sgt <2 x i32> %x, <i32 65534, i32 2147483647>
%r = select <2 x i1> %t, <2 x i32> %y, <2 x i32> <i32 65535, i32 -2147483648>
ret <2 x i32> %r
}
; We don't do anything for non-relational comparisons.
define i32 @n21_equality(i32 %x, i32 %y) {
; CHECK-LABEL: @n21_equality(
; CHECK-NEXT: [[T:%.*]] = icmp eq i32 [[X:%.*]], -2147483648
; CHECK-NEXT: [[R:%.*]] = select i1 [[T]], i32 2147483647, i32 [[Y:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp eq i32 %x, -2147483648
%r = select i1 %t, i32 2147483647, i32 %y
ret i32 %r
}
; There is nothing special about sign-bit-tests, we can fold them.
define i32 @t22_sign_check(i32 %x, i32 %y) {
; CHECK-LABEL: @t22_sign_check(
; CHECK-NEXT: [[T_INV:%.*]] = icmp sgt i32 [[X:%.*]], -1
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 [[Y:%.*]], i32 -1
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp slt i32 %x, 0
%r = select i1 %t, i32 -1, i32 %y
ret i32 %r
}
define i32 @t22_sign_check2(i32 %x, i32 %y) {
; CHECK-LABEL: @t22_sign_check2(
; CHECK-NEXT: [[T_INV:%.*]] = icmp slt i32 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = select i1 [[T_INV]], i32 [[Y:%.*]], i32 0
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp sgt i32 %x, -1
%r = select i1 %t, i32 0, i32 %y
ret i32 %r
}
; If the types don't match we currently don't do anything.
define i32 @n23_type_mismatch(i64 %x, i32 %y) {
; CHECK-LABEL: @n23_type_mismatch(
; CHECK-NEXT: [[T:%.*]] = icmp ult i64 [[X:%.*]], 65536
; CHECK-NEXT: [[R:%.*]] = select i1 [[T]], i32 [[Y:%.*]], i32 65535
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i64 %x, 65536
%r = select i1 %t, i32 %y, i32 65535
ret i32 %r
}
; Don't do wrong tranform
define i32 @n24_ult_65534(i32 %x, i32 %y) {
; CHECK-LABEL: @n24_ult_65534(
; CHECK-NEXT: [[T:%.*]] = icmp ult i32 [[X:%.*]], 65534
; CHECK-NEXT: [[R:%.*]] = select i1 [[T]], i32 [[Y:%.*]], i32 65535
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65534
%r = select i1 %t, i32 %y, i32 65535
ret i32 %r
}
; If we already have a match, it's good enough.
define i32 @n25_all_good0(i32 %x, i32 %y) {
; CHECK-LABEL: @n25_all_good0(
; CHECK-NEXT: [[T:%.*]] = icmp ult i32 [[X:%.*]], 65536
; CHECK-NEXT: [[R:%.*]] = select i1 [[T]], i32 65535, i32 65536
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
%r = select i1 %t, i32 65535, i32 65536
ret i32 %r
}
define i32 @n26_all_good1(i32 %x, i32 %y) {
; CHECK-LABEL: @n26_all_good1(
; CHECK-NEXT: [[T:%.*]] = icmp ult i32 [[X:%.*]], 65536
; CHECK-NEXT: [[R:%.*]] = select i1 [[T]], i32 65536, i32 65535
; CHECK-NEXT: ret i32 [[R]]
;
%t = icmp ult i32 %x, 65536
%r = select i1 %t, i32 65536, i32 65535
ret i32 %r
}
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2000}
|