1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i55 @test6(i55 %A) {
6 ; CHECK-NEXT: [[C:%.*]] = mul i55 [[A:%.*]], 6
7 ; CHECK-NEXT: ret i55 [[C]]
14 ; (X * C2) << C1 --> X * (C2 << C1)
16 define i55 @test6a(i55 %A) {
17 ; CHECK-LABEL: @test6a(
18 ; CHECK-NEXT: [[C:%.*]] = mul i55 [[A:%.*]], 6
19 ; CHECK-NEXT: ret i55 [[C]]
26 declare void @use(i55 %0)
28 define i55 @test6a_negative_oneuse(i55 %A) {
29 ; CHECK-LABEL: @test6a_negative_oneuse(
30 ; CHECK-NEXT: [[B:%.*]] = mul i55 [[A:%.*]], 3
31 ; CHECK-NEXT: [[C:%.*]] = mul i55 [[A]], 6
32 ; CHECK-NEXT: call void @use(i55 [[B]])
33 ; CHECK-NEXT: ret i55 [[C]]
37 call void @use(i55 %B)
41 ; (X * C2) << C1 --> X * (C2 << C1)
43 define <2 x i55> @test6a_vec(<2 x i55> %A) {
44 ; CHECK-LABEL: @test6a_vec(
45 ; CHECK-NEXT: [[C:%.*]] = mul <2 x i55> [[A:%.*]], <i55 6, i55 48>
46 ; CHECK-NEXT: ret <2 x i55> [[C]]
48 %B = mul <2 x i55> %A, <i55 3, i55 12>
49 %C = shl <2 x i55> %B, <i55 1, i55 2>
53 define i29 @test7(i8 %X) {
54 ; CHECK-LABEL: @test7(
55 ; CHECK-NEXT: ret i29 -1
57 %A = zext i8 %X to i29
62 define i7 @test8(i7 %A) {
63 ; CHECK-LABEL: @test8(
64 ; CHECK-NEXT: ret i7 0
71 define i17 @test9(i17 %A) {
72 ; CHECK-LABEL: @test9(
73 ; CHECK-NEXT: [[B:%.*]] = and i17 [[A:%.*]], 1
74 ; CHECK-NEXT: ret i17 [[B]]
81 ; shl (lshr X, C), C --> and X, C'
83 define i19 @test10(i19 %X) {
84 ; CHECK-LABEL: @test10(
85 ; CHECK-NEXT: [[SH1:%.*]] = and i19 [[X:%.*]], -262144
86 ; CHECK-NEXT: ret i19 [[SH1]]
88 %sh1 = lshr i19 %X, 18
89 %sh2 = shl i19 %sh1, 18
93 ; Two right shifts in the same direction:
94 ; lshr (lshr X, C1), C2 --> lshr X, C1 + C2
96 define <2 x i19> @lshr_lshr_splat_vec(<2 x i19> %X) {
97 ; CHECK-LABEL: @lshr_lshr_splat_vec(
98 ; CHECK-NEXT: [[SH1:%.*]] = lshr <2 x i19> [[X:%.*]], splat (i19 5)
99 ; CHECK-NEXT: ret <2 x i19> [[SH1]]
101 %sh1 = lshr <2 x i19> %X, <i19 3, i19 3>
102 %sh2 = lshr <2 x i19> %sh1, <i19 2, i19 2>
106 define i9 @multiuse_lshr_lshr(i9 %x) {
107 ; CHECK-LABEL: @multiuse_lshr_lshr(
108 ; CHECK-NEXT: [[SH1:%.*]] = lshr i9 [[X:%.*]], 2
109 ; CHECK-NEXT: [[SH2:%.*]] = lshr i9 [[X]], 5
110 ; CHECK-NEXT: [[MUL:%.*]] = mul i9 [[SH1]], [[SH2]]
111 ; CHECK-NEXT: ret i9 [[MUL]]
114 %sh2 = lshr i9 %sh1, 3
115 %mul = mul i9 %sh1, %sh2
119 define <2 x i9> @multiuse_lshr_lshr_splat(<2 x i9> %x) {
120 ; CHECK-LABEL: @multiuse_lshr_lshr_splat(
121 ; CHECK-NEXT: [[SH1:%.*]] = lshr <2 x i9> [[X:%.*]], splat (i9 2)
122 ; CHECK-NEXT: [[SH2:%.*]] = lshr <2 x i9> [[X]], splat (i9 5)
123 ; CHECK-NEXT: [[MUL:%.*]] = mul <2 x i9> [[SH1]], [[SH2]]
124 ; CHECK-NEXT: ret <2 x i9> [[MUL]]
126 %sh1 = lshr <2 x i9> %x, <i9 2, i9 2>
127 %sh2 = lshr <2 x i9> %sh1, <i9 3, i9 3>
128 %mul = mul <2 x i9> %sh1, %sh2
132 ; Two left shifts in the same direction:
133 ; shl (shl X, C1), C2 --> shl X, C1 + C2
135 define <2 x i19> @shl_shl_splat_vec(<2 x i19> %X) {
136 ; CHECK-LABEL: @shl_shl_splat_vec(
137 ; CHECK-NEXT: [[SH1:%.*]] = shl <2 x i19> [[X:%.*]], splat (i19 5)
138 ; CHECK-NEXT: ret <2 x i19> [[SH1]]
140 %sh1 = shl <2 x i19> %X, <i19 3, i19 3>
141 %sh2 = shl <2 x i19> %sh1, <i19 2, i19 2>
145 define i42 @multiuse_shl_shl(i42 %x) {
146 ; CHECK-LABEL: @multiuse_shl_shl(
147 ; CHECK-NEXT: [[SH1:%.*]] = shl i42 [[X:%.*]], 8
148 ; CHECK-NEXT: [[SH2:%.*]] = shl i42 [[X]], 17
149 ; CHECK-NEXT: [[MUL:%.*]] = mul i42 [[SH1]], [[SH2]]
150 ; CHECK-NEXT: ret i42 [[MUL]]
153 %sh2 = shl i42 %sh1, 9
154 %mul = mul i42 %sh1, %sh2
158 define <2 x i42> @multiuse_shl_shl_splat(<2 x i42> %x) {
159 ; CHECK-LABEL: @multiuse_shl_shl_splat(
160 ; CHECK-NEXT: [[SH1:%.*]] = shl <2 x i42> [[X:%.*]], splat (i42 8)
161 ; CHECK-NEXT: [[SH2:%.*]] = shl <2 x i42> [[X]], splat (i42 17)
162 ; CHECK-NEXT: [[MUL:%.*]] = mul <2 x i42> [[SH1]], [[SH2]]
163 ; CHECK-NEXT: ret <2 x i42> [[MUL]]
165 %sh1 = shl <2 x i42> %x, <i42 8, i42 8>
166 %sh2 = shl <2 x i42> %sh1, <i42 9, i42 9>
167 %mul = mul <2 x i42> %sh1, %sh2
171 ; Equal shift amounts in opposite directions become bitwise 'and':
172 ; lshr (shl X, C), C --> and X, C'
174 define <2 x i19> @eq_shl_lshr_splat_vec(<2 x i19> %X) {
175 ; CHECK-LABEL: @eq_shl_lshr_splat_vec(
176 ; CHECK-NEXT: [[SH1:%.*]] = and <2 x i19> [[X:%.*]], splat (i19 65535)
177 ; CHECK-NEXT: ret <2 x i19> [[SH1]]
179 %sh1 = shl <2 x i19> %X, <i19 3, i19 3>
180 %sh2 = lshr <2 x i19> %sh1, <i19 3, i19 3>
184 ; Equal shift amounts in opposite directions become bitwise 'and':
185 ; shl (lshr X, C), C --> and X, C'
187 define <2 x i19> @eq_lshr_shl_splat_vec(<2 x i19> %X) {
188 ; CHECK-LABEL: @eq_lshr_shl_splat_vec(
189 ; CHECK-NEXT: [[SH1:%.*]] = and <2 x i19> [[X:%.*]], splat (i19 -8)
190 ; CHECK-NEXT: ret <2 x i19> [[SH1]]
192 %sh1 = lshr <2 x i19> %X, <i19 3, i19 3>
193 %sh2 = shl <2 x i19> %sh1, <i19 3, i19 3>
197 ; In general, we would need an 'and' for this transform, but the masked-off bits are known zero.
198 ; shl (lshr X, C1), C2 --> lshr X, C1 - C2
200 define <2 x i7> @lshr_shl_splat_vec(<2 x i7> %X) {
201 ; CHECK-LABEL: @lshr_shl_splat_vec(
202 ; CHECK-NEXT: [[DOTNEG:%.*]] = mul <2 x i7> [[X:%.*]], splat (i7 60)
203 ; CHECK-NEXT: [[SH2:%.*]] = and <2 x i7> [[DOTNEG]], splat (i7 60)
204 ; CHECK-NEXT: ret <2 x i7> [[SH2]]
206 %mul = mul <2 x i7> %X, <i7 -8, i7 -8>
207 %sh1 = lshr exact <2 x i7> %mul, <i7 3, i7 3>
208 %sh2 = shl nuw nsw <2 x i7> %sh1, <i7 2, i7 2>
212 ; In general, we would need an 'and' for this transform, but the masked-off bits are known zero.
213 ; lshr (shl X, C1), C2 --> shl X, C1 - C2
215 define <2 x i7> @shl_lshr_splat_vec(<2 x i7> %X) {
216 ; CHECK-LABEL: @shl_lshr_splat_vec(
217 ; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i7> [[X:%.*]], splat (i7 9)
218 ; CHECK-NEXT: [[SH1:%.*]] = shl nuw nsw <2 x i7> [[DIV]], splat (i7 1)
219 ; CHECK-NEXT: ret <2 x i7> [[SH1]]
221 %div = udiv <2 x i7> %X, <i7 9, i7 9>
222 %sh1 = shl nuw <2 x i7> %div, <i7 3, i7 3>
223 %sh2 = lshr exact <2 x i7> %sh1, <i7 2, i7 2>
227 define i23 @test11(i23 %x) {
228 ; CHECK-LABEL: @test11(
229 ; CHECK-NEXT: [[TMP1:%.*]] = mul i23 [[X:%.*]], 6
230 ; CHECK-NEXT: [[C:%.*]] = and i23 [[TMP1]], -4096
231 ; CHECK-NEXT: ret i23 [[C]]
239 ; shl (ashr X, C), C --> and X, C'
241 define i47 @test12(i47 %X) {
242 ; CHECK-LABEL: @test12(
243 ; CHECK-NEXT: [[SH1:%.*]] = and i47 [[X:%.*]], -256
244 ; CHECK-NEXT: ret i47 [[SH1]]
246 %sh1 = ashr i47 %X, 8
247 %sh2 = shl i47 %sh1, 8
251 define <2 x i47> @test12_splat_vec(<2 x i47> %X) {
252 ; CHECK-LABEL: @test12_splat_vec(
253 ; CHECK-NEXT: [[SH1:%.*]] = and <2 x i47> [[X:%.*]], splat (i47 -256)
254 ; CHECK-NEXT: ret <2 x i47> [[SH1]]
256 %sh1 = ashr <2 x i47> %X, <i47 8, i47 8>
257 %sh2 = shl <2 x i47> %sh1, <i47 8, i47 8>
261 define i18 @test13(i18 %x) {
262 ; CHECK-LABEL: @test13(
263 ; CHECK-NEXT: [[TMP1:%.*]] = mul i18 [[X:%.*]], 6
264 ; CHECK-NEXT: [[C:%.*]] = and i18 [[TMP1]], -512
265 ; CHECK-NEXT: ret i18 [[C]]
273 define i35 @test14(i35 %A) {
274 ; CHECK-LABEL: @test14(
275 ; CHECK-NEXT: [[B:%.*]] = and i35 [[A:%.*]], -19760
276 ; CHECK-NEXT: [[C:%.*]] = or disjoint i35 [[B]], 19744
277 ; CHECK-NEXT: ret i35 [[C]]
285 define i79 @test14a(i79 %A) {
286 ; CHECK-LABEL: @test14a(
287 ; CHECK-NEXT: [[C:%.*]] = and i79 [[A:%.*]], 77
288 ; CHECK-NEXT: ret i79 [[C]]
291 %C = and i79 %B, 1234
296 define i45 @test15(i1 %C) {
297 ; CHECK-LABEL: @test15(
298 ; CHECK-NEXT: [[A:%.*]] = select i1 [[C:%.*]], i45 12, i45 4
299 ; CHECK-NEXT: ret i45 [[A]]
301 %A = select i1 %C, i45 3, i45 1
306 define i53 @test15a(i1 %X) {
307 ; CHECK-LABEL: @test15a(
308 ; CHECK-NEXT: [[V:%.*]] = select i1 [[X:%.*]], i53 512, i53 128
309 ; CHECK-NEXT: ret i53 [[V]]
311 %A = select i1 %X, i8 3, i8 1
312 %B = zext i8 %A to i53
317 define i1 @test16(i84 %X) {
318 ; CHECK-LABEL: @test16(
319 ; CHECK-NEXT: [[TMP1:%.*]] = and i84 [[X:%.*]], 16
320 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i84 [[TMP1]], 0
321 ; CHECK-NEXT: ret i1 [[CMP]]
323 %shr = ashr i84 %X, 4
324 %and = and i84 %shr, 1
325 %cmp = icmp ne i84 %and, 0
329 define <2 x i1> @test16vec(<2 x i84> %X) {
330 ; CHECK-LABEL: @test16vec(
331 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i84> [[X:%.*]], splat (i84 16)
332 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i84> [[TMP1]], zeroinitializer
333 ; CHECK-NEXT: ret <2 x i1> [[CMP]]
335 %shr = ashr <2 x i84> %X, <i84 4, i84 4>
336 %and = and <2 x i84> %shr, <i84 1, i84 1>
337 %cmp = icmp ne <2 x i84> %and, zeroinitializer
341 define <2 x i1> @test16vec_nonuniform(<2 x i84> %X) {
342 ; CHECK-LABEL: @test16vec_nonuniform(
343 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i84> [[X:%.*]], <i84 16, i84 4>
344 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i84> [[TMP1]], zeroinitializer
345 ; CHECK-NEXT: ret <2 x i1> [[CMP]]
347 %shr = ashr <2 x i84> %X, <i84 4, i84 2>
348 %and = and <2 x i84> %shr, <i84 1, i84 1>
349 %cmp = icmp ne <2 x i84> %and, zeroinitializer
353 define <2 x i1> @test16vec_undef(<2 x i84> %X) {
354 ; CHECK-LABEL: @test16vec_undef(
355 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i84> [[X:%.*]], <i84 16, i84 poison>
356 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i84> [[TMP1]], zeroinitializer
357 ; CHECK-NEXT: ret <2 x i1> [[CMP]]
359 %shr = ashr <2 x i84> %X, <i84 4, i84 undef>
360 %and = and <2 x i84> %shr, <i84 1, i84 1>
361 %cmp = icmp ne <2 x i84> %and, zeroinitializer
365 define i1 @test17(i106 %A) {
366 ; CHECK-LABEL: @test17(
367 ; CHECK-NEXT: [[B_MASK:%.*]] = and i106 [[A:%.*]], -8
368 ; CHECK-NEXT: [[C:%.*]] = icmp eq i106 [[B_MASK]], 9872
369 ; CHECK-NEXT: ret i1 [[C]]
372 %C = icmp eq i106 %B, 1234
376 define <2 x i1> @test17vec(<2 x i106> %A) {
377 ; CHECK-LABEL: @test17vec(
378 ; CHECK-NEXT: [[B_MASK:%.*]] = and <2 x i106> [[A:%.*]], splat (i106 -8)
379 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i106> [[B_MASK]], splat (i106 9872)
380 ; CHECK-NEXT: ret <2 x i1> [[C]]
382 %B = lshr <2 x i106> %A, <i106 3, i106 3>
383 %C = icmp eq <2 x i106> %B, <i106 1234, i106 1234>
387 define i1 @test18(i11 %A) {
388 ; CHECK-LABEL: @test18(
389 ; CHECK-NEXT: ret i1 false
392 %C = icmp eq i11 %B, 123
396 define i1 @test19(i37 %A) {
397 ; CHECK-LABEL: @test19(
398 ; CHECK-NEXT: [[C:%.*]] = icmp ult i37 [[A:%.*]], 4
399 ; CHECK-NEXT: ret i1 [[C]]
402 %C = icmp eq i37 %B, 0
406 define <2 x i1> @test19vec(<2 x i37> %A) {
407 ; CHECK-LABEL: @test19vec(
408 ; CHECK-NEXT: [[C:%.*]] = icmp ult <2 x i37> [[A:%.*]], splat (i37 4)
409 ; CHECK-NEXT: ret <2 x i1> [[C]]
411 %B = ashr <2 x i37> %A, <i37 2, i37 2>
412 %C = icmp eq <2 x i37> %B, zeroinitializer
416 define i1 @test19a(i39 %A) {
417 ; CHECK-LABEL: @test19a(
418 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i39 [[A:%.*]], -5
419 ; CHECK-NEXT: ret i1 [[C]]
422 %C = icmp eq i39 %B, -1
426 define <2 x i1> @test19a_vec(<2 x i39> %A) {
427 ; CHECK-LABEL: @test19a_vec(
428 ; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i39> [[A:%.*]], splat (i39 -5)
429 ; CHECK-NEXT: ret <2 x i1> [[C]]
431 %B = ashr <2 x i39> %A, <i39 2, i39 2>
432 %C = icmp eq <2 x i39> %B, <i39 -1, i39 -1>
436 define i1 @test20(i13 %A) {
437 ; CHECK-LABEL: @test20(
438 ; CHECK-NEXT: ret i1 false
441 %C = icmp eq i13 %B, 123
445 define i1 @test21(i12 %A) {
446 ; CHECK-LABEL: @test21(
447 ; CHECK-NEXT: [[B_MASK:%.*]] = and i12 [[A:%.*]], 63
448 ; CHECK-NEXT: [[C:%.*]] = icmp eq i12 [[B_MASK]], 62
449 ; CHECK-NEXT: ret i1 [[C]]
452 %C = icmp eq i12 %B, -128
456 define i1 @test22(i14 %A) {
457 ; CHECK-LABEL: @test22(
458 ; CHECK-NEXT: [[B_MASK:%.*]] = and i14 [[A:%.*]], 127
459 ; CHECK-NEXT: [[C:%.*]] = icmp eq i14 [[B_MASK]], 0
460 ; CHECK-NEXT: ret i1 [[C]]
463 %C = icmp eq i14 %B, 0
467 define i11 @test23(i44 %A) {
468 ; CHECK-LABEL: @test23(
469 ; CHECK-NEXT: [[D:%.*]] = trunc i44 [[A:%.*]] to i11
470 ; CHECK-NEXT: ret i11 [[D]]
474 %D = trunc i44 %C to i11
478 ; Fold lshr (shl X, C), C -> and X, C' regardless of the number of uses of the shl.
480 define i44 @shl_lshr_eq_amt_multi_use(i44 %A) {
481 ; CHECK-LABEL: @shl_lshr_eq_amt_multi_use(
482 ; CHECK-NEXT: [[B:%.*]] = shl i44 [[A:%.*]], 33
483 ; CHECK-NEXT: [[C:%.*]] = and i44 [[A]], 2047
484 ; CHECK-NEXT: [[D:%.*]] = or disjoint i44 [[B]], [[C]]
485 ; CHECK-NEXT: ret i44 [[D]]
493 ; Fold vector lshr (shl X, C), C -> and X, C' regardless of the number of uses of the shl.
495 define <2 x i44> @shl_lshr_eq_amt_multi_use_splat_vec(<2 x i44> %A) {
496 ; CHECK-LABEL: @shl_lshr_eq_amt_multi_use_splat_vec(
497 ; CHECK-NEXT: [[B:%.*]] = shl <2 x i44> [[A:%.*]], splat (i44 33)
498 ; CHECK-NEXT: [[C:%.*]] = and <2 x i44> [[A]], splat (i44 2047)
499 ; CHECK-NEXT: [[D:%.*]] = or disjoint <2 x i44> [[B]], [[C]]
500 ; CHECK-NEXT: ret <2 x i44> [[D]]
502 %B = shl <2 x i44> %A, <i44 33, i44 33>
503 %C = lshr <2 x i44> %B, <i44 33, i44 33>
504 %D = add <2 x i44> %B, %C
508 ; Fold shl (lshr X, C), C -> and X, C' regardless of the number of uses of the lshr.
510 define i43 @lshr_shl_eq_amt_multi_use(i43 %A) {
511 ; CHECK-LABEL: @lshr_shl_eq_amt_multi_use(
512 ; CHECK-NEXT: [[B:%.*]] = lshr i43 [[A:%.*]], 23
513 ; CHECK-NEXT: [[C:%.*]] = and i43 [[A]], -8388608
514 ; CHECK-NEXT: [[D:%.*]] = mul i43 [[B]], [[C]]
515 ; CHECK-NEXT: ret i43 [[D]]
523 ; Fold vector shl (lshr X, C), C -> and X, C' regardless of the number of uses of the lshr.
525 define <2 x i43> @lshr_shl_eq_amt_multi_use_splat_vec(<2 x i43> %A) {
526 ; CHECK-LABEL: @lshr_shl_eq_amt_multi_use_splat_vec(
527 ; CHECK-NEXT: [[B:%.*]] = lshr <2 x i43> [[A:%.*]], splat (i43 23)
528 ; CHECK-NEXT: [[C:%.*]] = and <2 x i43> [[A]], splat (i43 -8388608)
529 ; CHECK-NEXT: [[D:%.*]] = mul <2 x i43> [[B]], [[C]]
530 ; CHECK-NEXT: ret <2 x i43> [[D]]
532 %B = lshr <2 x i43> %A, <i43 23, i43 23>
533 %C = shl <2 x i43> %B, <i43 23, i43 23>
534 %D = mul <2 x i43> %B, %C
538 define i37 @test25(i37 %AA, i37 %BB) {
539 ; CHECK-LABEL: @test25(
540 ; CHECK-NEXT: [[D:%.*]] = and i37 [[AA:%.*]], -131072
541 ; CHECK-NEXT: [[C2:%.*]] = add i37 [[BB:%.*]], [[D]]
542 ; CHECK-NEXT: [[F:%.*]] = and i37 [[C2]], -131072
543 ; CHECK-NEXT: ret i37 [[F]]
545 %C = lshr i37 %BB, 17
546 %D = lshr i37 %AA, 17
552 define i40 @test26(i40 %A) {
553 ; CHECK-LABEL: @test26(
554 ; CHECK-NEXT: [[B:%.*]] = and i40 [[A:%.*]], -2
555 ; CHECK-NEXT: ret i40 [[B]]
558 %C = bitcast i40 %B to i40
564 ; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=9880
565 define i177 @ossfuzz_9880(i177 %X) {
566 ; CHECK-LABEL: @ossfuzz_9880(
567 ; CHECK-NEXT: ret i177 0
570 %L1 = load i177, ptr %A
572 %B5 = udiv i177 %L1, %B
573 %B4 = add i177 %B5, %B
574 %B2 = add i177 %B, %B4
575 %B6 = mul i177 %B5, %B2
576 %B20 = shl i177 %L1, %B6
577 %B14 = sub i177 %B20, %B5
578 %B1 = udiv i177 %B14, %B6