[InstCombine] lshr (mul (X, 2^N + 1)), N -> X when X is half-width (#93677)

Alive2 Proof:
https://alive2.llvm.org/ce/z/Yd2CKF
This commit is contained in:
AtariDreams 2024-05-30 03:39:32 -04:00 committed by GitHub
parent 0eb4bf2faf
commit 1034b4d38d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 10 deletions

View File

@ -1464,10 +1464,10 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
if (BitWidth > 2 && (*MulC - 1).isPowerOf2() && if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
MulC->logBase2() == ShAmtC) { MulC->logBase2() == ShAmtC) {
// Look for a "splat" mul pattern - it replicates bits across each half // Look for a "splat" mul pattern - it replicates bits across each half
// of a value, so a right shift is just a mask of the low bits: // of a value, so a right shift simplifies back to just X:
// lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1 // lshr i[2N] (mul nuw X, (2^N)+1), N --> X
if (ShAmtC * 2 == BitWidth) if (ShAmtC * 2 == BitWidth)
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2)); return replaceInstUsesWith(I, X);
// lshr (mul nuw (X, 2^N + 1)), N -> add nuw (X, lshr(X, N)) // lshr (mul nuw (X, 2^N + 1)), N -> add nuw (X, lshr(X, N))
if (Op0->hasOneUse()) { if (Op0->hasOneUse()) {

View File

@ -348,8 +348,7 @@ define <2 x i32> @narrow_lshr_constant(<2 x i8> %x, <2 x i8> %y) {
define i32 @mul_splat_fold(i32 %x) { define i32 @mul_splat_fold(i32 %x) {
; CHECK-LABEL: @mul_splat_fold( ; CHECK-LABEL: @mul_splat_fold(
; CHECK-NEXT: [[T:%.*]] = and i32 [[X:%.*]], 65535 ; CHECK-NEXT: ret i32 [[X:%.*]]
; CHECK-NEXT: ret i32 [[T]]
; ;
%m = mul nuw i32 %x, 65537 %m = mul nuw i32 %x, 65537
%t = lshr i32 %m, 16 %t = lshr i32 %m, 16
@ -362,8 +361,7 @@ define <3 x i14> @mul_splat_fold_vec(<3 x i14> %x) {
; CHECK-LABEL: @mul_splat_fold_vec( ; CHECK-LABEL: @mul_splat_fold_vec(
; CHECK-NEXT: [[M:%.*]] = mul nuw <3 x i14> [[X:%.*]], <i14 129, i14 129, i14 129> ; CHECK-NEXT: [[M:%.*]] = mul nuw <3 x i14> [[X:%.*]], <i14 129, i14 129, i14 129>
; CHECK-NEXT: call void @usevec(<3 x i14> [[M]]) ; CHECK-NEXT: call void @usevec(<3 x i14> [[M]])
; CHECK-NEXT: [[T:%.*]] = and <3 x i14> [[X]], <i14 127, i14 127, i14 127> ; CHECK-NEXT: ret <3 x i14> [[X]]
; CHECK-NEXT: ret <3 x i14> [[T]]
; ;
%m = mul nuw <3 x i14> %x, <i14 129, i14 129, i14 129> %m = mul nuw <3 x i14> %x, <i14 129, i14 129, i14 129>
call void @usevec(<3 x i14> %m) call void @usevec(<3 x i14> %m)
@ -628,8 +626,6 @@ define i32 @mul_splat_fold_wrong_lshr_const(i32 %x) {
ret i32 %t ret i32 %t
} }
; Negative test (but simplifies into a different transform)
define i32 @mul_splat_fold_no_nuw(i32 %x) { define i32 @mul_splat_fold_no_nuw(i32 %x) {
; CHECK-LABEL: @mul_splat_fold_no_nuw( ; CHECK-LABEL: @mul_splat_fold_no_nuw(
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 16 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 16
@ -641,7 +637,7 @@ define i32 @mul_splat_fold_no_nuw(i32 %x) {
ret i32 %t ret i32 %t
} }
; Negative test ; Negative test
define i32 @mul_splat_fold_no_flags(i32 %x) { define i32 @mul_splat_fold_no_flags(i32 %x) {
; CHECK-LABEL: @mul_splat_fold_no_flags( ; CHECK-LABEL: @mul_splat_fold_no_flags(