blob: 6044d689e2d2feef95acd7ad9303cecb49f1e4c9 [file] [log] [blame]
Sanjay Patel8a2bf302016-07-18 20:06:51 +00001; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
David Majnemerc6a5e1d2014-11-27 06:32:46 +00002; RUN: opt < %s -instsimplify -S | FileCheck %s
3
4define i32 @test1(i32 %x) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +00005; CHECK-LABEL: @test1(
6; CHECK-NEXT: ret i32 %x
7;
David Majnemerc6a5e1d2014-11-27 06:32:46 +00008 %and = and i32 %x, 1
9 %cmp = icmp eq i32 %and, 0
10 %and1 = and i32 %x, -2
11 %and1.x = select i1 %cmp, i32 %and1, i32 %x
12 ret i32 %and1.x
David Majnemerc6a5e1d2014-11-27 06:32:46 +000013}
14
15define i32 @test2(i32 %x) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000016; CHECK-LABEL: @test2(
17; CHECK-NEXT: ret i32 %x
18;
David Majnemerc6a5e1d2014-11-27 06:32:46 +000019 %and = and i32 %x, 1
20 %cmp = icmp ne i32 %and, 0
21 %and1 = and i32 %x, -2
22 %and1.x = select i1 %cmp, i32 %x, i32 %and1
23 ret i32 %and1.x
David Majnemerc6a5e1d2014-11-27 06:32:46 +000024}
25
26define i32 @test3(i32 %x) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000027; CHECK-LABEL: @test3(
28; CHECK-NEXT: [[AND1:%.*]] = and i32 %x, -2
29; CHECK-NEXT: ret i32 [[AND1]]
30;
David Majnemerc6a5e1d2014-11-27 06:32:46 +000031 %and = and i32 %x, 1
32 %cmp = icmp ne i32 %and, 0
33 %and1 = and i32 %x, -2
34 %and1.x = select i1 %cmp, i32 %and1, i32 %x
35 ret i32 %and1.x
David Majnemerc6a5e1d2014-11-27 06:32:46 +000036}
37
David Majnemer0b6a0b02014-12-20 03:04:38 +000038define i32 @test4(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000039; CHECK-LABEL: @test4(
40; CHECK-NEXT: [[OR:%.*]] = or i32 %X, -2147483648
41; CHECK-NEXT: ret i32 [[OR]]
42;
David Majnemer0b6a0b02014-12-20 03:04:38 +000043 %cmp = icmp slt i32 %X, 0
44 %or = or i32 %X, -2147483648
45 %cond = select i1 %cmp, i32 %X, i32 %or
46 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000047}
48
Craig Topperba691872017-08-10 01:02:02 +000049; Same as above, but the compare isn't canonical
Craig Topperba691872017-08-10 01:02:02 +000050define i32 @test4noncanon(i32 %X) {
51; CHECK-LABEL: @test4noncanon(
Craig Topperb1e4b1a2017-08-14 22:11:43 +000052; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
53; CHECK-NEXT: ret i32 [[OR]]
Craig Topperba691872017-08-10 01:02:02 +000054;
55 %cmp = icmp sle i32 %X, -1
56 %or = or i32 %X, -2147483648
57 %cond = select i1 %cmp, i32 %X, i32 %or
58 ret i32 %cond
59}
60
David Majnemer0b6a0b02014-12-20 03:04:38 +000061define i32 @test5(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000062; CHECK-LABEL: @test5(
63; CHECK-NEXT: ret i32 %X
64;
David Majnemer0b6a0b02014-12-20 03:04:38 +000065 %cmp = icmp slt i32 %X, 0
66 %or = or i32 %X, -2147483648
67 %cond = select i1 %cmp, i32 %or, i32 %X
68 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000069}
70
71define i32 @test6(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000072; CHECK-LABEL: @test6(
73; CHECK-NEXT: [[AND:%.*]] = and i32 %X, 2147483647
74; CHECK-NEXT: ret i32 [[AND]]
75;
David Majnemer0b6a0b02014-12-20 03:04:38 +000076 %cmp = icmp slt i32 %X, 0
77 %and = and i32 %X, 2147483647
78 %cond = select i1 %cmp, i32 %and, i32 %X
79 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000080}
81
82define i32 @test7(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000083; CHECK-LABEL: @test7(
84; CHECK-NEXT: ret i32 %X
85;
David Majnemer0b6a0b02014-12-20 03:04:38 +000086 %cmp = icmp slt i32 %X, 0
87 %and = and i32 %X, 2147483647
88 %cond = select i1 %cmp, i32 %X, i32 %and
89 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000090}
91
92define i32 @test8(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000093; CHECK-LABEL: @test8(
94; CHECK-NEXT: ret i32 %X
95;
David Majnemer0b6a0b02014-12-20 03:04:38 +000096 %cmp = icmp sgt i32 %X, -1
97 %or = or i32 %X, -2147483648
98 %cond = select i1 %cmp, i32 %X, i32 %or
99 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +0000100}
101
102define i32 @test9(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000103; CHECK-LABEL: @test9(
104; CHECK-NEXT: [[OR:%.*]] = or i32 %X, -2147483648
105; CHECK-NEXT: ret i32 [[OR]]
106;
David Majnemer0b6a0b02014-12-20 03:04:38 +0000107 %cmp = icmp sgt i32 %X, -1
108 %or = or i32 %X, -2147483648
109 %cond = select i1 %cmp, i32 %or, i32 %X
110 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +0000111}
112
Craig Topperba691872017-08-10 01:02:02 +0000113; Same as above, but the compare isn't canonical
Craig Topperba691872017-08-10 01:02:02 +0000114define i32 @test9noncanon(i32 %X) {
115; CHECK-LABEL: @test9noncanon(
Craig Topperb1e4b1a2017-08-14 22:11:43 +0000116; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], -2147483648
117; CHECK-NEXT: ret i32 [[OR]]
Craig Topperba691872017-08-10 01:02:02 +0000118;
119 %cmp = icmp sge i32 %X, 0
120 %or = or i32 %X, -2147483648
121 %cond = select i1 %cmp, i32 %or, i32 %X
122 ret i32 %cond
123}
124
David Majnemer0b6a0b02014-12-20 03:04:38 +0000125define i32 @test10(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000126; CHECK-LABEL: @test10(
127; CHECK-NEXT: ret i32 %X
128;
David Majnemer0b6a0b02014-12-20 03:04:38 +0000129 %cmp = icmp sgt i32 %X, -1
130 %and = and i32 %X, 2147483647
131 %cond = select i1 %cmp, i32 %and, i32 %X
132 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +0000133}
134
135define i32 @test11(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000136; CHECK-LABEL: @test11(
137; CHECK-NEXT: [[AND:%.*]] = and i32 %X, 2147483647
138; CHECK-NEXT: ret i32 [[AND]]
139;
David Majnemer0b6a0b02014-12-20 03:04:38 +0000140 %cmp = icmp sgt i32 %X, -1
141 %and = and i32 %X, 2147483647
142 %cond = select i1 %cmp, i32 %X, i32 %and
143 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +0000144}
145
Sanjay Patel5f3c7032016-07-20 23:40:01 +0000146define <2 x i8> @test11vec(<2 x i8> %X) {
147; CHECK-LABEL: @test11vec(
148; CHECK-NEXT: [[AND:%.*]] = and <2 x i8> %X, <i8 127, i8 127>
149; CHECK-NEXT: ret <2 x i8> [[AND]]
150;
151 %cmp = icmp sgt <2 x i8> %X, <i8 -1, i8 -1>
152 %and = and <2 x i8> %X, <i8 127, i8 127>
153 %sel = select <2 x i1> %cmp, <2 x i8> %X, <2 x i8> %and
154 ret <2 x i8> %sel
155}
156
Craig Topper58def1e2017-08-14 18:49:39 +0000157define i32 @test12(i32 %X) {
158; CHECK-LABEL: @test12(
Craig Topper0aa3a192017-08-14 21:39:51 +0000159; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
160; CHECK-NEXT: ret i32 [[AND]]
Craig Topper58def1e2017-08-14 18:49:39 +0000161;
162 %cmp = icmp ult i32 %X, 4
163 %and = and i32 %X, 3
164 %cond = select i1 %cmp, i32 %X, i32 %and
165 ret i32 %cond
166}
167
168; Same as above, but the compare isn't canonical
Craig Topper58def1e2017-08-14 18:49:39 +0000169define i32 @test12noncanon(i32 %X) {
170; CHECK-LABEL: @test12noncanon(
Craig Topperb1e4b1a2017-08-14 22:11:43 +0000171; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
172; CHECK-NEXT: ret i32 [[AND]]
Craig Topper58def1e2017-08-14 18:49:39 +0000173;
174 %cmp = icmp ule i32 %X, 3
175 %and = and i32 %X, 3
176 %cond = select i1 %cmp, i32 %X, i32 %and
177 ret i32 %cond
178}
179
Craig Topper58def1e2017-08-14 18:49:39 +0000180define i32 @test13(i32 %X) {
181; CHECK-LABEL: @test13(
Craig Topper0aa3a192017-08-14 21:39:51 +0000182; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
183; CHECK-NEXT: ret i32 [[AND]]
Craig Topper58def1e2017-08-14 18:49:39 +0000184;
185 %cmp = icmp ugt i32 %X, 3
186 %and = and i32 %X, 3
187 %cond = select i1 %cmp, i32 %and, i32 %X
188 ret i32 %cond
189}
190
191; Same as above, but the compare isn't canonical
Craig Topper58def1e2017-08-14 18:49:39 +0000192define i32 @test13noncanon(i32 %X) {
193; CHECK-LABEL: @test13noncanon(
Craig Topperb1e4b1a2017-08-14 22:11:43 +0000194; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
195; CHECK-NEXT: ret i32 [[AND]]
Craig Topper58def1e2017-08-14 18:49:39 +0000196;
197 %cmp = icmp uge i32 %X, 4
198 %and = and i32 %X, 3
199 %cond = select i1 %cmp, i32 %and, i32 %X
200 ret i32 %cond
201}
202
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000203define i32 @select_icmp_and_8_eq_0_or_8(i32 %x) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000204; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8(
205; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 8
206; CHECK-NEXT: ret i32 [[OR]]
207;
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000208 %and = and i32 %x, 8
209 %cmp = icmp eq i32 %and, 0
210 %or = or i32 %x, 8
Sanjay Patel47c04f92016-07-19 20:47:00 +0000211 %sel = select i1 %cmp, i32 %or, i32 %x
212 ret i32 %sel
213}
214
215define i32 @select_icmp_and_8_eq_0_or_8_alt(i32 %x) {
216; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8_alt(
217; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 8
218; CHECK-NEXT: ret i32 [[OR]]
219;
220 %and = and i32 %x, 8
221 %cmp = icmp ne i32 %and, 0
222 %or = or i32 %x, 8
223 %sel = select i1 %cmp, i32 %x, i32 %or
224 ret i32 %sel
225}
226
227define i32 @select_icmp_and_8_ne_0_or_8(i32 %x) {
228; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8(
229; CHECK-NEXT: ret i32 %x
230;
231 %and = and i32 %x, 8
232 %cmp = icmp ne i32 %and, 0
233 %or = or i32 %x, 8
234 %sel = select i1 %cmp, i32 %or, i32 %x
235 ret i32 %sel
236}
237
238define i32 @select_icmp_and_8_ne_0_or_8_alt(i32 %x) {
239; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8_alt(
240; CHECK-NEXT: ret i32 %x
241;
242 %and = and i32 %x, 8
243 %cmp = icmp eq i32 %and, 0
244 %or = or i32 %x, 8
245 %sel = select i1 %cmp, i32 %x, i32 %or
246 ret i32 %sel
247}
248
249define i32 @select_icmp_and_8_eq_0_and_not_8(i32 %x) {
250; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8(
251; CHECK-NEXT: [[AND1:%.*]] = and i32 %x, -9
252; CHECK-NEXT: ret i32 [[AND1]]
253;
254 %and = and i32 %x, 8
255 %cmp = icmp eq i32 %and, 0
256 %and1 = and i32 %x, -9
257 %sel = select i1 %cmp, i32 %x, i32 %and1
258 ret i32 %sel
259}
260
261define i32 @select_icmp_and_8_eq_0_and_not_8_alt(i32 %x) {
262; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8_alt(
263; CHECK-NEXT: [[AND1:%.*]] = and i32 %x, -9
264; CHECK-NEXT: ret i32 [[AND1]]
265;
266 %and = and i32 %x, 8
267 %cmp = icmp ne i32 %and, 0
268 %and1 = and i32 %x, -9
269 %sel = select i1 %cmp, i32 %and1, i32 %x
270 ret i32 %sel
271}
272
273define i32 @select_icmp_and_8_ne_0_and_not_8(i32 %x) {
274; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8(
275; CHECK-NEXT: ret i32 %x
276;
277 %and = and i32 %x, 8
278 %cmp = icmp ne i32 %and, 0
279 %and1 = and i32 %x, -9
280 %sel = select i1 %cmp, i32 %x, i32 %and1
281 ret i32 %sel
282}
283
284define i32 @select_icmp_and_8_ne_0_and_not_8_alt(i32 %x) {
285; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_alt(
286; CHECK-NEXT: ret i32 %x
287;
288 %and = and i32 %x, 8
289 %cmp = icmp eq i32 %and, 0
290 %and1 = and i32 %x, -9
291 %sel = select i1 %cmp, i32 %and1, i32 %x
292 ret i32 %sel
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000293}
294
Sanjay Pateld2ff6d72016-07-19 16:49:55 +0000295; PR28466: https://llvm.org/bugs/show_bug.cgi?id=28466
Sanjay Patel47c04f92016-07-19 20:47:00 +0000296; Each of the previous 8 patterns has a variant that replaces the
297; 'and' with a 'trunc' and the icmp eq/ne with icmp slt/sgt.
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000298
Sanjay Patel47c04f92016-07-19 20:47:00 +0000299define i32 @select_icmp_trunc_8_ne_0_or_128(i32 %x) {
300; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128(
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000301; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 128
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000302; CHECK-NEXT: ret i32 [[OR]]
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000303;
304 %trunc = trunc i32 %x to i8
Sanjay Patel47c04f92016-07-19 20:47:00 +0000305 %cmp = icmp sgt i8 %trunc, -1
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000306 %or = or i32 %x, 128
307 %sel = select i1 %cmp, i32 %or, i32 %x
308 ret i32 %sel
309}
Sanjay Pateld2ff6d72016-07-19 16:49:55 +0000310
Sanjay Patel47c04f92016-07-19 20:47:00 +0000311define i32 @select_icmp_trunc_8_ne_0_or_128_alt(i32 %x) {
312; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128_alt(
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000313; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 128
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000314; CHECK-NEXT: ret i32 [[OR]]
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000315;
316 %trunc = trunc i32 %x to i8
317 %cmp = icmp slt i8 %trunc, 0
318 %or = or i32 %x, 128
319 %sel = select i1 %cmp, i32 %x, i32 %or
320 ret i32 %sel
321}
322
Sanjay Patel47c04f92016-07-19 20:47:00 +0000323define i32 @select_icmp_trunc_8_eq_0_or_128(i32 %x) {
324; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128(
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000325; CHECK-NEXT: ret i32 %x
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000326;
Sanjay Patel47c04f92016-07-19 20:47:00 +0000327 %trunc = trunc i32 %x to i8
328 %cmp = icmp slt i8 %trunc, 0
329 %or = or i32 %x, 128
330 %sel = select i1 %cmp, i32 %or, i32 %x
331 ret i32 %sel
332}
333
334define i32 @select_icmp_trunc_8_eq_0_or_128_alt(i32 %x) {
335; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128_alt(
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000336; CHECK-NEXT: ret i32 %x
Sanjay Patel47c04f92016-07-19 20:47:00 +0000337;
338 %trunc = trunc i32 %x to i8
339 %cmp = icmp sgt i8 %trunc, -1
340 %or = or i32 %x, 128
341 %sel = select i1 %cmp, i32 %x, i32 %or
342 ret i32 %sel
343}
344
345define i32 @select_icmp_trunc_8_eq_0_and_not_8(i32 %x) {
346; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8(
Sanjay Patel47c04f92016-07-19 20:47:00 +0000347; CHECK-NEXT: [[AND:%.*]] = and i32 %x, -9
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000348; CHECK-NEXT: ret i32 [[AND]]
Sanjay Patel47c04f92016-07-19 20:47:00 +0000349;
350 %trunc = trunc i32 %x to i4
351 %cmp = icmp sgt i4 %trunc, -1
352 %and = and i32 %x, -9
353 %sel = select i1 %cmp, i32 %x, i32 %and
354 ret i32 %sel
355}
356
357define i32 @select_icmp_trunc_8_eq_0_and_not_8_alt(i32 %x) {
358; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8_alt(
Sanjay Patel47c04f92016-07-19 20:47:00 +0000359; CHECK-NEXT: [[AND:%.*]] = and i32 %x, -9
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000360; CHECK-NEXT: ret i32 [[AND]]
Sanjay Patel47c04f92016-07-19 20:47:00 +0000361;
362 %trunc = trunc i32 %x to i4
363 %cmp = icmp slt i4 %trunc, 0
364 %and = and i32 %x, -9
365 %sel = select i1 %cmp, i32 %and, i32 %x
366 ret i32 %sel
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000367}
368
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000369define i32 @select_icmp_trunc_8_ne_0_and_not_8(i32 %x) {
370; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8(
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000371; CHECK-NEXT: ret i32 %x
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000372;
373 %trunc = trunc i32 %x to i4
374 %cmp = icmp slt i4 %trunc, 0
375 %and = and i32 %x, -9
376 %sel = select i1 %cmp, i32 %x, i32 %and
377 ret i32 %sel
378}
379
Sanjay Patel47c04f92016-07-19 20:47:00 +0000380define i32 @select_icmp_trunc_8_ne_0_and_not_8_alt(i32 %x) {
381; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt(
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000382; CHECK-NEXT: ret i32 %x
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000383;
384 %trunc = trunc i32 %x to i4
Sanjay Patel47c04f92016-07-19 20:47:00 +0000385 %cmp = icmp sgt i4 %trunc, -1
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000386 %and = and i32 %x, -9
387 %sel = select i1 %cmp, i32 %and, i32 %x
388 ret i32 %sel
389}
390
Sanjay Patel9eec5502016-07-21 20:11:08 +0000391; Make sure that at least a few of the same patterns are repeated with vector types.
392
393define <2 x i32> @select_icmp_and_8_ne_0_and_not_8_vec(<2 x i32> %x) {
394; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_vec(
395; CHECK-NEXT: ret <2 x i32> %x
396;
397 %and = and <2 x i32> %x, <i32 8, i32 8>
398 %cmp = icmp ne <2 x i32> %and, zeroinitializer
399 %and1 = and <2 x i32> %x, <i32 -9, i32 -9>
400 %sel = select <2 x i1> %cmp, <2 x i32> %x, <2 x i32> %and1
401 ret <2 x i32> %sel
402}
403
404define <2 x i32> @select_icmp_trunc_8_ne_0_and_not_8_alt_vec(<2 x i32> %x) {
405; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt_vec(
Sanjay Patela3bfb4e2016-07-21 21:26:45 +0000406; CHECK-NEXT: ret <2 x i32> %x
Sanjay Patel9eec5502016-07-21 20:11:08 +0000407;
408 %trunc = trunc <2 x i32> %x to <2 x i4>
409 %cmp = icmp sgt <2 x i4> %trunc, <i4 -1, i4 -1>
410 %and = and <2 x i32> %x, <i32 -9, i32 -9>
411 %sel = select <2 x i1> %cmp, <2 x i32> %and, <2 x i32> %x
412 ret <2 x i32> %sel
413}
414
415; Insert a bit from x into y? This should be possible in InstCombine, but not InstSimplify?
416
417define i32 @select_icmp_x_and_8_eq_0_y_and_not_8(i32 %x, i32 %y) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000418; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_and_not_8(
419; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 8
420; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
Sanjay Patel9eec5502016-07-21 20:11:08 +0000421; CHECK-NEXT: [[AND1:%.*]] = and i32 %y, -9
422; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i32 %y, i32 [[AND1]]
423; CHECK-NEXT: ret i32 [[Y_AND1]]
424;
425 %and = and i32 %x, 8
426 %cmp = icmp eq i32 %and, 0
427 %and1 = and i32 %y, -9
428 %y.and1 = select i1 %cmp, i32 %y, i32 %and1
429 ret i32 %y.and1
430}
431
432define i64 @select_icmp_x_and_8_eq_0_y64_and_not_8(i32 %x, i64 %y) {
433; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y64_and_not_8(
434; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 8
435; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000436; CHECK-NEXT: [[AND1:%.*]] = and i64 %y, -9
437; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i64 %y, i64 [[AND1]]
438; CHECK-NEXT: ret i64 [[Y_AND1]]
439;
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000440 %and = and i32 %x, 8
441 %cmp = icmp eq i32 %and, 0
442 %and1 = and i64 %y, -9
443 %y.and1 = select i1 %cmp, i64 %y, i64 %and1
444 ret i64 %y.and1
445}
446
Sanjay Patel9eec5502016-07-21 20:11:08 +0000447define i64 @select_icmp_x_and_8_ne_0_y64_and_not_8(i32 %x, i64 %y) {
448; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y64_and_not_8(
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000449; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 8
450; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
451; CHECK-NEXT: [[AND1:%.*]] = and i64 %y, -9
452; CHECK-NEXT: [[AND1_Y:%.*]] = select i1 [[CMP]], i64 [[AND1]], i64 %y
453; CHECK-NEXT: ret i64 [[AND1_Y]]
454;
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000455 %and = and i32 %x, 8
456 %cmp = icmp eq i32 %and, 0
457 %and1 = and i64 %y, -9
458 %and1.y = select i1 %cmp, i64 %and1, i64 %y
459 ret i64 %and1.y
460}
461
Sanjay Patele9fc79b2016-07-21 21:56:00 +0000462; Don't crash on a pointer or aggregate type.
463
464define i32* @select_icmp_pointers(i32* %x, i32* %y) {
465; CHECK-LABEL: @select_icmp_pointers(
466; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32* %x, null
467; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32* %x, i32* %y
468; CHECK-NEXT: ret i32* [[SEL]]
469;
470 %cmp = icmp slt i32* %x, null
471 %sel = select i1 %cmp, i32* %x, i32* %y
472 ret i32* %sel
473}
474
Sanjay Patel51783632017-01-13 17:02:42 +0000475; If the condition is known, we don't need to select, but we're not
476; doing this fold here to avoid compile-time cost.
Sanjay Patel95faecb2017-01-05 00:40:52 +0000477
478declare void @llvm.assume(i1)
479
480define i8 @assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
481; CHECK-LABEL: @assume_sel_cond(
482; CHECK-NEXT: call void @llvm.assume(i1 %cond)
483; CHECK-NEXT: [[SEL:%.*]] = select i1 %cond, i8 %x, i8 %y
484; CHECK-NEXT: ret i8 [[SEL]]
485;
486 call void @llvm.assume(i1 %cond)
487 %sel = select i1 %cond, i8 %x, i8 %y
488 ret i8 %sel
489}
490
491define i8 @do_not_assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
492; CHECK-LABEL: @do_not_assume_sel_cond(
493; CHECK-NEXT: [[NOTCOND:%.*]] = icmp eq i1 %cond, false
494; CHECK-NEXT: call void @llvm.assume(i1 [[NOTCOND]])
495; CHECK-NEXT: [[SEL:%.*]] = select i1 %cond, i8 %x, i8 %y
496; CHECK-NEXT: ret i8 [[SEL]]
497;
498 %notcond = icmp eq i1 %cond, false
499 call void @llvm.assume(i1 %notcond)
500 %sel = select i1 %cond, i8 %x, i8 %y
501 ret i8 %sel
502}
503