Skip to content

Commit 1034b4d

Browse files
authored
[InstCombine] lshr (mul (X, 2^N + 1)), N -> X when X is half-width (#93677)
Alive2 Proof: https://alive2.llvm.org/ce/z/Yd2CKF
1 parent 0eb4bf2 commit 1034b4d

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,10 +1464,10 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
14641464
if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
14651465
MulC->logBase2() == ShAmtC) {
14661466
// Look for a "splat" mul pattern - it replicates bits across each half
1467-
// of a value, so a right shift is just a mask of the low bits:
1468-
// lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1467+
// of a value, so a right shift simplifies back to just X:
1468+
// lshr i[2N] (mul nuw X, (2^N)+1), N --> X
14691469
if (ShAmtC * 2 == BitWidth)
1470-
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
1470+
return replaceInstUsesWith(I, X);
14711471

14721472
// lshr (mul nuw (X, 2^N + 1)), N -> add nuw (X, lshr(X, N))
14731473
if (Op0->hasOneUse()) {

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ define <2 x i32> @narrow_lshr_constant(<2 x i8> %x, <2 x i8> %y) {
348348

349349
define i32 @mul_splat_fold(i32 %x) {
350350
; CHECK-LABEL: @mul_splat_fold(
351-
; CHECK-NEXT: [[T:%.*]] = and i32 [[X:%.*]], 65535
352-
; CHECK-NEXT: ret i32 [[T]]
351+
; CHECK-NEXT: ret i32 [[X:%.*]]
353352
;
354353
%m = mul nuw i32 %x, 65537
355354
%t = lshr i32 %m, 16
@@ -362,8 +361,7 @@ define <3 x i14> @mul_splat_fold_vec(<3 x i14> %x) {
362361
; CHECK-LABEL: @mul_splat_fold_vec(
363362
; CHECK-NEXT: [[M:%.*]] = mul nuw <3 x i14> [[X:%.*]], <i14 129, i14 129, i14 129>
364363
; CHECK-NEXT: call void @usevec(<3 x i14> [[M]])
365-
; CHECK-NEXT: [[T:%.*]] = and <3 x i14> [[X]], <i14 127, i14 127, i14 127>
366-
; CHECK-NEXT: ret <3 x i14> [[T]]
364+
; CHECK-NEXT: ret <3 x i14> [[X]]
367365
;
368366
%m = mul nuw <3 x i14> %x, <i14 129, i14 129, i14 129>
369367
call void @usevec(<3 x i14> %m)
@@ -628,8 +626,6 @@ define i32 @mul_splat_fold_wrong_lshr_const(i32 %x) {
628626
ret i32 %t
629627
}
630628

631-
; Negative test (but simplifies into a different transform)
632-
633629
define i32 @mul_splat_fold_no_nuw(i32 %x) {
634630
; CHECK-LABEL: @mul_splat_fold_no_nuw(
635631
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 16
@@ -641,7 +637,7 @@ define i32 @mul_splat_fold_no_nuw(i32 %x) {
641637
ret i32 %t
642638
}
643639

644-
; Negative test
640+
; Negative test
645641

646642
define i32 @mul_splat_fold_no_flags(i32 %x) {
647643
; CHECK-LABEL: @mul_splat_fold_no_flags(

0 commit comments

Comments
 (0)