blob: adc9392deed528532c9d184dffa35e323208dd65 [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
49define i32 @test5(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000050; CHECK-LABEL: @test5(
51; CHECK-NEXT: ret i32 %X
52;
David Majnemer0b6a0b02014-12-20 03:04:38 +000053 %cmp = icmp slt i32 %X, 0
54 %or = or i32 %X, -2147483648
55 %cond = select i1 %cmp, i32 %or, i32 %X
56 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000057}
58
59define i32 @test6(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000060; CHECK-LABEL: @test6(
61; CHECK-NEXT: [[AND:%.*]] = and i32 %X, 2147483647
62; CHECK-NEXT: ret i32 [[AND]]
63;
David Majnemer0b6a0b02014-12-20 03:04:38 +000064 %cmp = icmp slt i32 %X, 0
65 %and = and i32 %X, 2147483647
66 %cond = select i1 %cmp, i32 %and, i32 %X
67 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000068}
69
70define i32 @test7(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000071; CHECK-LABEL: @test7(
72; CHECK-NEXT: ret i32 %X
73;
David Majnemer0b6a0b02014-12-20 03:04:38 +000074 %cmp = icmp slt i32 %X, 0
75 %and = and i32 %X, 2147483647
76 %cond = select i1 %cmp, i32 %X, i32 %and
77 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000078}
79
80define i32 @test8(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000081; CHECK-LABEL: @test8(
82; CHECK-NEXT: ret i32 %X
83;
David Majnemer0b6a0b02014-12-20 03:04:38 +000084 %cmp = icmp sgt i32 %X, -1
85 %or = or i32 %X, -2147483648
86 %cond = select i1 %cmp, i32 %X, i32 %or
87 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000088}
89
90define i32 @test9(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +000091; CHECK-LABEL: @test9(
92; CHECK-NEXT: [[OR:%.*]] = or i32 %X, -2147483648
93; CHECK-NEXT: ret i32 [[OR]]
94;
David Majnemer0b6a0b02014-12-20 03:04:38 +000095 %cmp = icmp sgt i32 %X, -1
96 %or = or i32 %X, -2147483648
97 %cond = select i1 %cmp, i32 %or, i32 %X
98 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +000099}
100
101define i32 @test10(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000102; CHECK-LABEL: @test10(
103; CHECK-NEXT: ret i32 %X
104;
David Majnemer0b6a0b02014-12-20 03:04:38 +0000105 %cmp = icmp sgt i32 %X, -1
106 %and = and i32 %X, 2147483647
107 %cond = select i1 %cmp, i32 %and, i32 %X
108 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +0000109}
110
111define i32 @test11(i32 %X) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000112; CHECK-LABEL: @test11(
113; CHECK-NEXT: [[AND:%.*]] = and i32 %X, 2147483647
114; CHECK-NEXT: ret i32 [[AND]]
115;
David Majnemer0b6a0b02014-12-20 03:04:38 +0000116 %cmp = icmp sgt i32 %X, -1
117 %and = and i32 %X, 2147483647
118 %cond = select i1 %cmp, i32 %X, i32 %and
119 ret i32 %cond
David Majnemer0b6a0b02014-12-20 03:04:38 +0000120}
121
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000122define i32 @select_icmp_and_8_eq_0_or_8(i32 %x) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000123; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8(
124; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 8
125; CHECK-NEXT: ret i32 [[OR]]
126;
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000127 %and = and i32 %x, 8
128 %cmp = icmp eq i32 %and, 0
129 %or = or i32 %x, 8
Sanjay Patel47c04f92016-07-19 20:47:00 +0000130 %sel = select i1 %cmp, i32 %or, i32 %x
131 ret i32 %sel
132}
133
134define i32 @select_icmp_and_8_eq_0_or_8_alt(i32 %x) {
135; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8_alt(
136; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 8
137; CHECK-NEXT: ret i32 [[OR]]
138;
139 %and = and i32 %x, 8
140 %cmp = icmp ne i32 %and, 0
141 %or = or i32 %x, 8
142 %sel = select i1 %cmp, i32 %x, i32 %or
143 ret i32 %sel
144}
145
146define i32 @select_icmp_and_8_ne_0_or_8(i32 %x) {
147; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8(
148; CHECK-NEXT: ret i32 %x
149;
150 %and = and i32 %x, 8
151 %cmp = icmp ne i32 %and, 0
152 %or = or i32 %x, 8
153 %sel = select i1 %cmp, i32 %or, i32 %x
154 ret i32 %sel
155}
156
157define i32 @select_icmp_and_8_ne_0_or_8_alt(i32 %x) {
158; CHECK-LABEL: @select_icmp_and_8_ne_0_or_8_alt(
159; CHECK-NEXT: ret i32 %x
160;
161 %and = and i32 %x, 8
162 %cmp = icmp eq i32 %and, 0
163 %or = or i32 %x, 8
164 %sel = select i1 %cmp, i32 %x, i32 %or
165 ret i32 %sel
166}
167
168define i32 @select_icmp_and_8_eq_0_and_not_8(i32 %x) {
169; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8(
170; CHECK-NEXT: [[AND1:%.*]] = and i32 %x, -9
171; CHECK-NEXT: ret i32 [[AND1]]
172;
173 %and = and i32 %x, 8
174 %cmp = icmp eq i32 %and, 0
175 %and1 = and i32 %x, -9
176 %sel = select i1 %cmp, i32 %x, i32 %and1
177 ret i32 %sel
178}
179
180define i32 @select_icmp_and_8_eq_0_and_not_8_alt(i32 %x) {
181; CHECK-LABEL: @select_icmp_and_8_eq_0_and_not_8_alt(
182; CHECK-NEXT: [[AND1:%.*]] = and i32 %x, -9
183; CHECK-NEXT: ret i32 [[AND1]]
184;
185 %and = and i32 %x, 8
186 %cmp = icmp ne i32 %and, 0
187 %and1 = and i32 %x, -9
188 %sel = select i1 %cmp, i32 %and1, i32 %x
189 ret i32 %sel
190}
191
192define i32 @select_icmp_and_8_ne_0_and_not_8(i32 %x) {
193; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8(
194; CHECK-NEXT: ret i32 %x
195;
196 %and = and i32 %x, 8
197 %cmp = icmp ne i32 %and, 0
198 %and1 = and i32 %x, -9
199 %sel = select i1 %cmp, i32 %x, i32 %and1
200 ret i32 %sel
201}
202
203define i32 @select_icmp_and_8_ne_0_and_not_8_alt(i32 %x) {
204; CHECK-LABEL: @select_icmp_and_8_ne_0_and_not_8_alt(
205; CHECK-NEXT: ret i32 %x
206;
207 %and = and i32 %x, 8
208 %cmp = icmp eq i32 %and, 0
209 %and1 = and i32 %x, -9
210 %sel = select i1 %cmp, i32 %and1, i32 %x
211 ret i32 %sel
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000212}
213
Sanjay Pateld2ff6d72016-07-19 16:49:55 +0000214; PR28466: https://llvm.org/bugs/show_bug.cgi?id=28466
Sanjay Patel47c04f92016-07-19 20:47:00 +0000215; Each of the previous 8 patterns has a variant that replaces the
216; 'and' with a 'trunc' and the icmp eq/ne with icmp slt/sgt.
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000217
Sanjay Patel47c04f92016-07-19 20:47:00 +0000218define i32 @select_icmp_trunc_8_ne_0_or_128(i32 %x) {
219; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128(
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000220; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i8
Sanjay Patel47c04f92016-07-19 20:47:00 +0000221; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[TRUNC]], -1
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000222; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 128
223; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[OR]], i32 %x
224; CHECK-NEXT: ret i32 [[SEL]]
225;
226 %trunc = trunc i32 %x to i8
Sanjay Patel47c04f92016-07-19 20:47:00 +0000227 %cmp = icmp sgt i8 %trunc, -1
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000228 %or = or i32 %x, 128
229 %sel = select i1 %cmp, i32 %or, i32 %x
230 ret i32 %sel
231}
Sanjay Pateld2ff6d72016-07-19 16:49:55 +0000232
Sanjay Patel47c04f92016-07-19 20:47:00 +0000233define i32 @select_icmp_trunc_8_ne_0_or_128_alt(i32 %x) {
234; CHECK-LABEL: @select_icmp_trunc_8_ne_0_or_128_alt(
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000235; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i8
236; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[TRUNC]], 0
237; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 128
238; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 [[OR]]
239; CHECK-NEXT: ret i32 [[SEL]]
240;
241 %trunc = trunc i32 %x to i8
242 %cmp = icmp slt i8 %trunc, 0
243 %or = or i32 %x, 128
244 %sel = select i1 %cmp, i32 %x, i32 %or
245 ret i32 %sel
246}
247
Sanjay Patel47c04f92016-07-19 20:47:00 +0000248define i32 @select_icmp_trunc_8_eq_0_or_128(i32 %x) {
249; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128(
250; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i8
251; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[TRUNC]], 0
252; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 128
253; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[OR]], i32 %x
254; CHECK-NEXT: ret i32 [[SEL]]
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000255;
Sanjay Patel47c04f92016-07-19 20:47:00 +0000256 %trunc = trunc i32 %x to i8
257 %cmp = icmp slt i8 %trunc, 0
258 %or = or i32 %x, 128
259 %sel = select i1 %cmp, i32 %or, i32 %x
260 ret i32 %sel
261}
262
263define i32 @select_icmp_trunc_8_eq_0_or_128_alt(i32 %x) {
264; CHECK-LABEL: @select_icmp_trunc_8_eq_0_or_128_alt(
265; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i8
266; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[TRUNC]], -1
267; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 128
268; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 [[OR]]
269; CHECK-NEXT: ret i32 [[SEL]]
270;
271 %trunc = trunc i32 %x to i8
272 %cmp = icmp sgt i8 %trunc, -1
273 %or = or i32 %x, 128
274 %sel = select i1 %cmp, i32 %x, i32 %or
275 ret i32 %sel
276}
277
278define i32 @select_icmp_trunc_8_eq_0_and_not_8(i32 %x) {
279; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8(
280; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i4
281; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i4 [[TRUNC]], -1
282; CHECK-NEXT: [[AND:%.*]] = and i32 %x, -9
283; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 [[AND]]
284; CHECK-NEXT: ret i32 [[SEL]]
285;
286 %trunc = trunc i32 %x to i4
287 %cmp = icmp sgt i4 %trunc, -1
288 %and = and i32 %x, -9
289 %sel = select i1 %cmp, i32 %x, i32 %and
290 ret i32 %sel
291}
292
293define i32 @select_icmp_trunc_8_eq_0_and_not_8_alt(i32 %x) {
294; CHECK-LABEL: @select_icmp_trunc_8_eq_0_and_not_8_alt(
295; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i4
296; CHECK-NEXT: [[CMP:%.*]] = icmp slt i4 [[TRUNC]], 0
297; CHECK-NEXT: [[AND:%.*]] = and i32 %x, -9
298; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[AND]], i32 %x
299; CHECK-NEXT: ret i32 [[SEL]]
300;
301 %trunc = trunc i32 %x to i4
302 %cmp = icmp slt i4 %trunc, 0
303 %and = and i32 %x, -9
304 %sel = select i1 %cmp, i32 %and, i32 %x
305 ret i32 %sel
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000306}
307
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000308define i32 @select_icmp_trunc_8_ne_0_and_not_8(i32 %x) {
309; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8(
310; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i4
311; CHECK-NEXT: [[CMP:%.*]] = icmp slt i4 [[TRUNC]], 0
312; CHECK-NEXT: [[AND:%.*]] = and i32 %x, -9
313; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 [[AND]]
314; CHECK-NEXT: ret i32 [[SEL]]
315;
316 %trunc = trunc i32 %x to i4
317 %cmp = icmp slt i4 %trunc, 0
318 %and = and i32 %x, -9
319 %sel = select i1 %cmp, i32 %x, i32 %and
320 ret i32 %sel
321}
322
Sanjay Patel47c04f92016-07-19 20:47:00 +0000323define i32 @select_icmp_trunc_8_ne_0_and_not_8_alt(i32 %x) {
324; CHECK-LABEL: @select_icmp_trunc_8_ne_0_and_not_8_alt(
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000325; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %x to i4
Sanjay Patel47c04f92016-07-19 20:47:00 +0000326; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i4 [[TRUNC]], -1
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000327; CHECK-NEXT: [[AND:%.*]] = and i32 %x, -9
328; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[AND]], i32 %x
329; CHECK-NEXT: ret i32 [[SEL]]
330;
331 %trunc = trunc i32 %x to i4
Sanjay Patel47c04f92016-07-19 20:47:00 +0000332 %cmp = icmp sgt i4 %trunc, -1
Sanjay Patel8b76ebe2016-07-19 17:07:35 +0000333 %and = and i32 %x, -9
334 %sel = select i1 %cmp, i32 %and, i32 %x
335 ret i32 %sel
336}
337
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000338define i64 @select_icmp_x_and_8_eq_0_y_and_not_8(i32 %x, i64 %y) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000339; CHECK-LABEL: @select_icmp_x_and_8_eq_0_y_and_not_8(
340; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 8
341; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
342; CHECK-NEXT: [[AND1:%.*]] = and i64 %y, -9
343; CHECK-NEXT: [[Y_AND1:%.*]] = select i1 [[CMP]], i64 %y, i64 [[AND1]]
344; CHECK-NEXT: ret i64 [[Y_AND1]]
345;
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000346 %and = and i32 %x, 8
347 %cmp = icmp eq i32 %and, 0
348 %and1 = and i64 %y, -9
349 %y.and1 = select i1 %cmp, i64 %y, i64 %and1
350 ret i64 %y.and1
351}
352
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000353define i64 @select_icmp_x_and_8_ne_0_y_and_not_8(i32 %x, i64 %y) {
Sanjay Patel8a2bf302016-07-18 20:06:51 +0000354; CHECK-LABEL: @select_icmp_x_and_8_ne_0_y_and_not_8(
355; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 8
356; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
357; CHECK-NEXT: [[AND1:%.*]] = and i64 %y, -9
358; CHECK-NEXT: [[AND1_Y:%.*]] = select i1 [[CMP]], i64 [[AND1]], i64 %y
359; CHECK-NEXT: ret i64 [[AND1_Y]]
360;
David Majnemerc6a5e1d2014-11-27 06:32:46 +0000361 %and = and i32 %x, 8
362 %cmp = icmp eq i32 %and, 0
363 %and1 = and i64 %y, -9
364 %and1.y = select i1 %cmp, i64 %and1, i64 %y
365 ret i64 %and1.y
366}
367