1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i32 @pow2_multiplier(i32 %A) {
5 ; CHECK-LABEL: @pow2_multiplier(
6 ; CHECK-NEXT: [[B:%.*]] = shl i32 [[A:%.*]], 1
7 ; CHECK-NEXT: ret i32 [[B]]
13 define <2 x i32> @pow2_multiplier_vec(<2 x i32> %A) {
14 ; CHECK-LABEL: @pow2_multiplier_vec(
15 ; CHECK-NEXT: [[B:%.*]] = shl <2 x i32> [[A:%.*]], <i32 3, i32 3>
16 ; CHECK-NEXT: ret <2 x i32> [[B]]
18 %B = mul <2 x i32> %A, <i32 8, i32 8>
22 define i8 @combine_shl(i8 %A) {
23 ; CHECK-LABEL: @combine_shl(
24 ; CHECK-NEXT: [[C:%.*]] = shl i8 [[A:%.*]], 6
25 ; CHECK-NEXT: ret i8 [[C]]
32 define i32 @neg(i32 %i) {
34 ; CHECK-NEXT: [[TMP:%.*]] = sub i32 0, [[I:%.*]]
35 ; CHECK-NEXT: ret i32 [[TMP]]
41 ; Use the sign-bit as a mask:
42 ; (zext (A < 0)) * B --> (A >> 31) & B
44 define i32 @test10(i32 %a, i32 %b) {
45 ; CHECK-LABEL: @test10(
46 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A:%.*]], 31
47 ; CHECK-NEXT: [[E:%.*]] = and i32 [[TMP1]], [[B:%.*]]
48 ; CHECK-NEXT: ret i32 [[E]]
50 %c = icmp slt i32 %a, 0
51 %d = zext i1 %c to i32
56 define i32 @test11(i32 %a, i32 %b) {
57 ; CHECK-LABEL: @test11(
58 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A:%.*]], 31
59 ; CHECK-NEXT: [[E:%.*]] = and i32 [[TMP1]], [[B:%.*]]
60 ; CHECK-NEXT: ret i32 [[E]]
62 %c = icmp sle i32 %a, -1
63 %d = zext i1 %c to i32
68 declare void @use32(i32)
70 define i32 @test12(i32 %a, i32 %b) {
71 ; CHECK-LABEL: @test12(
72 ; CHECK-NEXT: [[A_LOBIT:%.*]] = lshr i32 [[A:%.*]], 31
73 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A]], 31
74 ; CHECK-NEXT: [[E:%.*]] = and i32 [[TMP1]], [[B:%.*]]
75 ; CHECK-NEXT: call void @use32(i32 [[A_LOBIT]])
76 ; CHECK-NEXT: ret i32 [[E]]
78 %c = icmp ugt i32 %a, 2147483647
79 %d = zext i1 %c to i32
81 call void @use32(i32 %d)
86 define i32 @test15(i32 %A, i32 %B) {
87 ; CHECK-LABEL: @test15(
88 ; CHECK-NEXT: [[M:%.*]] = shl i32 [[A:%.*]], [[B:%.*]]
89 ; CHECK-NEXT: ret i32 [[M]]
96 ; X * Y (when Y is a boolean) --> Y ? X : 0
98 define i32 @mul_bool(i32 %x, i1 %y) {
99 ; CHECK-LABEL: @mul_bool(
100 ; CHECK-NEXT: [[M:%.*]] = select i1 [[Y:%.*]], i32 [[X:%.*]], i32 0
101 ; CHECK-NEXT: ret i32 [[M]]
103 %z = zext i1 %y to i32
108 ; Commute and test vector type.
110 define <2 x i32> @mul_bool_vec(<2 x i32> %x, <2 x i1> %y) {
111 ; CHECK-LABEL: @mul_bool_vec(
112 ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i32> [[X:%.*]], <2 x i32> zeroinitializer
113 ; CHECK-NEXT: ret <2 x i32> [[M]]
115 %z = zext <2 x i1> %y to <2 x i32>
116 %m = mul <2 x i32> %x, %z
120 define <2 x i32> @mul_bool_vec_commute(<2 x i32> %x, <2 x i1> %y) {
121 ; CHECK-LABEL: @mul_bool_vec_commute(
122 ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i32> [[X:%.*]], <2 x i32> zeroinitializer
123 ; CHECK-NEXT: ret <2 x i32> [[M]]
125 %z = zext <2 x i1> %y to <2 x i32>
126 %m = mul <2 x i32> %z, %x
130 ; (A >>u 31) * B --> (A >>s 31) & B
132 define i32 @signbit_mul(i32 %a, i32 %b) {
133 ; CHECK-LABEL: @signbit_mul(
134 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A:%.*]], 31
135 ; CHECK-NEXT: [[E:%.*]] = and i32 [[TMP1]], [[B:%.*]]
136 ; CHECK-NEXT: ret i32 [[E]]
143 define i32 @signbit_mul_commute_extra_use(i32 %a, i32 %b) {
144 ; CHECK-LABEL: @signbit_mul_commute_extra_use(
145 ; CHECK-NEXT: [[D:%.*]] = lshr i32 [[A:%.*]], 31
146 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A]], 31
147 ; CHECK-NEXT: [[E:%.*]] = and i32 [[TMP1]], [[B:%.*]]
148 ; CHECK-NEXT: call void @use32(i32 [[D]])
149 ; CHECK-NEXT: ret i32 [[E]]
153 call void @use32(i32 %d)
157 ; (A >>u 31)) * B --> (A >>s 31) & B
159 define <2 x i32> @signbit_mul_vec(<2 x i32> %a, <2 x i32> %b) {
160 ; CHECK-LABEL: @signbit_mul_vec(
161 ; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[A:%.*]], <i32 31, i32 31>
162 ; CHECK-NEXT: [[E:%.*]] = and <2 x i32> [[TMP1]], [[B:%.*]]
163 ; CHECK-NEXT: ret <2 x i32> [[E]]
165 %d = lshr <2 x i32> %a, <i32 31, i32 31>
166 %e = mul <2 x i32> %d, %b
170 define <2 x i32> @signbit_mul_vec_commute(<2 x i32> %a, <2 x i32> %b) {
171 ; CHECK-LABEL: @signbit_mul_vec_commute(
172 ; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[A:%.*]], <i32 31, i32 31>
173 ; CHECK-NEXT: [[E:%.*]] = and <2 x i32> [[TMP1]], [[B:%.*]]
174 ; CHECK-NEXT: ret <2 x i32> [[E]]
176 %d = lshr <2 x i32> %a, <i32 31, i32 31>
177 %e = mul <2 x i32> %b, %d
181 define i32 @test18(i32 %A, i32 %B) {
182 ; CHECK-LABEL: @test18(
183 ; CHECK-NEXT: ret i32 0
192 declare {i32, i1} @llvm.smul.with.overflow.i32(i32, i32)
193 declare void @use(i1)
195 define i32 @test19(i32 %A, i32 %B) {
196 ; CHECK-LABEL: @test19(
197 ; CHECK-NEXT: call void @use(i1 false)
198 ; CHECK-NEXT: ret i32 0
203 ; It would be nice if we also started proving that this doesn't overflow.
204 %E = call {i32, i1} @llvm.smul.with.overflow.i32(i32 %C, i32 %D)
205 %F = extractvalue {i32, i1} %E, 0
206 %G = extractvalue {i32, i1} %E, 1
207 call void @use(i1 %G)
212 define <2 x i64> @test20(<2 x i64> %A) {
213 ; CHECK-LABEL: @test20(
214 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i64> [[A:%.*]], <i64 3, i64 2>
215 ; CHECK-NEXT: [[C:%.*]] = add <2 x i64> [[TMP1]], <i64 36, i64 28>
216 ; CHECK-NEXT: ret <2 x i64> [[C]]
218 %B = add <2 x i64> %A, <i64 12, i64 14>
219 %C = mul <2 x i64> %B, <i64 3, i64 2>
223 define <2 x i1> @test21(<2 x i1> %A, <2 x i1> %B) {
224 ; CHECK-LABEL: @test21(
225 ; CHECK-NEXT: [[C:%.*]] = and <2 x i1> [[A:%.*]], [[B:%.*]]
226 ; CHECK-NEXT: ret <2 x i1> [[C]]
228 %C = mul <2 x i1> %A, %B
232 define i32 @test22(i32 %A) {
233 ; CHECK-LABEL: @test22(
234 ; CHECK-NEXT: [[B:%.*]] = sub nsw i32 0, [[A:%.*]]
235 ; CHECK-NEXT: ret i32 [[B]]
237 %B = mul nsw i32 %A, -1
241 define i32 @test23(i32 %A) {
242 ; CHECK-LABEL: @test23(
243 ; CHECK-NEXT: [[C:%.*]] = mul nuw i32 [[A:%.*]], 6
244 ; CHECK-NEXT: ret i32 [[C]]
246 %B = shl nuw i32 %A, 1
247 %C = mul nuw i32 %B, 3
251 define i32 @test24(i32 %A) {
252 ; CHECK-LABEL: @test24(
253 ; CHECK-NEXT: [[C:%.*]] = mul nsw i32 [[A:%.*]], 6
254 ; CHECK-NEXT: ret i32 [[C]]
256 %B = shl nsw i32 %A, 1
257 %C = mul nsw i32 %B, 3
261 define i32 @neg_neg_mul(i32 %A, i32 %B) {
262 ; CHECK-LABEL: @neg_neg_mul(
263 ; CHECK-NEXT: [[E:%.*]] = mul i32 [[A:%.*]], [[B:%.*]]
264 ; CHECK-NEXT: ret i32 [[E]]
272 define i32 @neg_neg_mul_nsw(i32 %A, i32 %B) {
273 ; CHECK-LABEL: @neg_neg_mul_nsw(
274 ; CHECK-NEXT: [[E:%.*]] = mul nsw i32 [[A:%.*]], [[B:%.*]]
275 ; CHECK-NEXT: ret i32 [[E]]
277 %C = sub nsw i32 0, %A
278 %D = sub nsw i32 0, %B
279 %E = mul nsw i32 %C, %D
283 define i124 @neg_neg_mul_apint(i124 %A, i124 %B) {
284 ; CHECK-LABEL: @neg_neg_mul_apint(
285 ; CHECK-NEXT: [[E:%.*]] = mul i124 [[A:%.*]], [[B:%.*]]
286 ; CHECK-NEXT: ret i124 [[E]]
294 define i32 @neg_mul_constant(i32 %A) {
295 ; CHECK-LABEL: @neg_mul_constant(
296 ; CHECK-NEXT: [[E:%.*]] = mul i32 [[A:%.*]], -7
297 ; CHECK-NEXT: ret i32 [[E]]
304 define i55 @neg_mul_constant_apint(i55 %A) {
305 ; CHECK-LABEL: @neg_mul_constant_apint(
306 ; CHECK-NEXT: [[E:%.*]] = mul i55 [[A:%.*]], -7
307 ; CHECK-NEXT: ret i55 [[E]]
314 define <3 x i8> @neg_mul_constant_vec(<3 x i8> %a) {
315 ; CHECK-LABEL: @neg_mul_constant_vec(
316 ; CHECK-NEXT: [[B:%.*]] = mul <3 x i8> [[A:%.*]], <i8 -5, i8 -5, i8 -5>
317 ; CHECK-NEXT: ret <3 x i8> [[B]]
319 %A = sub <3 x i8> zeroinitializer, %a
320 %B = mul <3 x i8> %A, <i8 5, i8 5, i8 5>
324 define <3 x i4> @neg_mul_constant_vec_weird(<3 x i4> %a) {
325 ; CHECK-LABEL: @neg_mul_constant_vec_weird(
326 ; CHECK-NEXT: [[B:%.*]] = mul <3 x i4> [[A:%.*]], <i4 -5, i4 -5, i4 -5>
327 ; CHECK-NEXT: ret <3 x i4> [[B]]
329 %A = sub <3 x i4> zeroinitializer, %a
330 %B = mul <3 x i4> %A, <i4 5, i4 5, i4 5>
334 define i32 @test26(i32 %A, i32 %B) {
335 ; CHECK-LABEL: @test26(
336 ; CHECK-NEXT: [[D:%.*]] = shl nsw i32 [[A:%.*]], [[B:%.*]]
337 ; CHECK-NEXT: ret i32 [[D]]
339 %C = shl nsw i32 1, %B
340 %D = mul nsw i32 %A, %C
344 define i32 @test27(i32 %A, i32 %B) {
345 ; CHECK-LABEL: @test27(
346 ; CHECK-NEXT: [[D:%.*]] = shl nuw i32 [[A:%.*]], [[B:%.*]]
347 ; CHECK-NEXT: ret i32 [[D]]
350 %D = mul nuw i32 %A, %C
354 define i32 @test28(i32 %A) {
355 ; CHECK-LABEL: @test28(
356 ; CHECK-NEXT: [[B:%.*]] = shl i32 1, [[A:%.*]]
357 ; CHECK-NEXT: [[C:%.*]] = shl i32 [[B]], [[A]]
358 ; CHECK-NEXT: ret i32 [[C]]
361 %C = mul nsw i32 %B, %B
365 define i64 @test29(i31 %A, i31 %B) {
366 ; CHECK-LABEL: @test29(
367 ; CHECK-NEXT: [[C:%.*]] = sext i31 [[A:%.*]] to i64
368 ; CHECK-NEXT: [[D:%.*]] = sext i31 [[B:%.*]] to i64
369 ; CHECK-NEXT: [[E:%.*]] = mul nsw i64 [[C]], [[D]]
370 ; CHECK-NEXT: ret i64 [[E]]
372 %C = sext i31 %A to i64
373 %D = sext i31 %B to i64
378 define i64 @test30(i32 %A, i32 %B) {
379 ; CHECK-LABEL: @test30(
380 ; CHECK-NEXT: [[C:%.*]] = zext i32 [[A:%.*]] to i64
381 ; CHECK-NEXT: [[D:%.*]] = zext i32 [[B:%.*]] to i64
382 ; CHECK-NEXT: [[E:%.*]] = mul nuw i64 [[C]], [[D]]
383 ; CHECK-NEXT: ret i64 [[E]]
385 %C = zext i32 %A to i64
386 %D = zext i32 %B to i64
391 @PR22087 = external global i32
392 define i32 @test31(i32 %V) {
393 ; CHECK-LABEL: @test31(
394 ; CHECK-NEXT: [[MUL:%.*]] = shl i32 [[V:%.*]], zext (i1 icmp ne (i32* inttoptr (i64 1 to i32*), i32* @PR22087) to i32)
395 ; CHECK-NEXT: ret i32 [[MUL]]
397 %mul = mul i32 %V, shl (i32 1, i32 zext (i1 icmp ne (i32* inttoptr (i64 1 to i32*), i32* @PR22087) to i32))
401 define i32 @test32(i32 %X) {
402 ; CHECK-LABEL: @test32(
403 ; CHECK-NEXT: [[MUL:%.*]] = shl i32 [[X:%.*]], 31
404 ; CHECK-NEXT: ret i32 [[MUL]]
406 %mul = mul nsw i32 %X, -2147483648
410 define <2 x i32> @test32vec(<2 x i32> %X) {
411 ; CHECK-LABEL: @test32vec(
412 ; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[X:%.*]], <i32 31, i32 31>
413 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
415 %mul = mul nsw <2 x i32> %X, <i32 -2147483648, i32 -2147483648>
419 define i32 @test33(i32 %X) {
420 ; CHECK-LABEL: @test33(
421 ; CHECK-NEXT: [[MUL:%.*]] = shl nsw i32 [[X:%.*]], 30
422 ; CHECK-NEXT: ret i32 [[MUL]]
424 %mul = mul nsw i32 %X, 1073741824
428 define <2 x i32> @test33vec(<2 x i32> %X) {
429 ; CHECK-LABEL: @test33vec(
430 ; CHECK-NEXT: [[MUL:%.*]] = shl nsw <2 x i32> [[X:%.*]], <i32 30, i32 30>
431 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
433 %mul = mul nsw <2 x i32> %X, <i32 1073741824, i32 1073741824>
437 define i128 @test34(i128 %X) {
438 ; CHECK-LABEL: @test34(
439 ; CHECK-NEXT: [[MUL:%.*]] = shl nsw i128 [[X:%.*]], 1
440 ; CHECK-NEXT: ret i128 [[MUL]]
442 %mul = mul nsw i128 %X, 2
446 define i32 @test_mul_canonicalize_op0(i32 %x, i32 %y) {
447 ; CHECK-LABEL: @test_mul_canonicalize_op0(
448 ; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
449 ; CHECK-NEXT: [[MUL:%.*]] = sub i32 0, [[TMP1]]
450 ; CHECK-NEXT: ret i32 [[MUL]]
453 %mul = mul i32 %neg, %y
457 define i32 @test_mul_canonicalize_op1(i32 %x, i32 %z) {
458 ; CHECK-LABEL: @test_mul_canonicalize_op1(
459 ; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
460 ; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[Y]], [[X:%.*]]
461 ; CHECK-NEXT: [[MUL:%.*]] = sub i32 0, [[TMP1]]
462 ; CHECK-NEXT: ret i32 [[MUL]]
466 %mul = mul i32 %y, %neg
470 define i32 @test_mul_canonicalize_nsw(i32 %x, i32 %y) {
471 ; CHECK-LABEL: @test_mul_canonicalize_nsw(
472 ; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
473 ; CHECK-NEXT: [[MUL:%.*]] = sub i32 0, [[TMP1]]
474 ; CHECK-NEXT: ret i32 [[MUL]]
476 %neg = sub nsw i32 0, %x
477 %mul = mul nsw i32 %neg, %y
481 define <2 x i32> @test_mul_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
482 ; CHECK-LABEL: @test_mul_canonicalize_vec(
483 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i32> [[X:%.*]], [[Y:%.*]]
484 ; CHECK-NEXT: [[MUL:%.*]] = sub <2 x i32> zeroinitializer, [[TMP1]]
485 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
487 %neg = sub <2 x i32> <i32 0, i32 0>, %x
488 %mul = mul <2 x i32> %neg, %y
492 define i32 @test_mul_canonicalize_multiple_uses(i32 %x, i32 %y) {
493 ; CHECK-LABEL: @test_mul_canonicalize_multiple_uses(
494 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
495 ; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[NEG]], [[Y:%.*]]
496 ; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL]], [[NEG]]
497 ; CHECK-NEXT: ret i32 [[MUL2]]
500 %mul = mul i32 %neg, %y
501 %mul2 = mul i32 %mul, %neg
507 define i64 @test_mul_canonicalize_neg_is_not_undone(i64 %L1) {
508 ; Check we do not undo the canonicalization of 0 - (X * Y), if Y is a constant
510 ; CHECK-LABEL: @test_mul_canonicalize_neg_is_not_undone(
511 ; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[L1:%.*]], ptrtoint (i32* @X to i64)
512 ; CHECK-NEXT: [[B4:%.*]] = sub i64 0, [[TMP1]]
513 ; CHECK-NEXT: ret i64 [[B4]]
515 %v1 = ptrtoint i32* @X to i64
517 %B4 = mul i64 %B8, %L1