[llvm] 64e9443 - [InstCombine] combine mul(abs(x),abs(y)) to abs(mul(x,y)) (#78395)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 18 04:12:04 PST 2024
Author: Congcong Cai
Date: 2024-01-18T20:12:00+08:00
New Revision: 64e94438a416b2ac630f247606a992442d6b0e20
URL: https://github.com/llvm/llvm-project/commit/64e94438a416b2ac630f247606a992442d6b0e20
DIFF: https://github.com/llvm/llvm-project/commit/64e94438a416b2ac630f247606a992442d6b0e20.diff
LOG: [InstCombine] combine mul(abs(x),abs(y)) to abs(mul(x,y)) (#78395)
Fixes: https://github.com/llvm/llvm-project/issues/78076
Alive2 Proof: https://alive2.llvm.org/ce/z/XEDy0f
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/mul.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index e7f983a00e3044..6c3adf00c189a8 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -330,6 +330,19 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
return BinaryOperator::CreateMul(X, X);
}
+ {
+ Value *X, *Y;
+ // abs(X) * abs(Y) -> abs(X * Y)
+ if (I.hasNoSignedWrap() &&
+ match(Op0,
+ m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(X), m_One()))) &&
+ match(Op1, m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(Y), m_One()))))
+ return replaceInstUsesWith(
+ I, Builder.CreateBinaryIntrinsic(Intrinsic::abs,
+ Builder.CreateNSWMul(X, Y),
+ Builder.getTrue()));
+ }
+
// -X * C --> X * -C
Value *X, *Y;
Constant *Op1C;
diff --git a/llvm/test/Transforms/InstCombine/mul.ll b/llvm/test/Transforms/InstCombine/mul.ll
index b404fcffbf4226..e7141d7c25ad21 100644
--- a/llvm/test/Transforms/InstCombine/mul.ll
+++ b/llvm/test/Transforms/InstCombine/mul.ll
@@ -1649,6 +1649,73 @@ define <vscale x 2 x i64> @mul_scalable_splat_zero(<vscale x 2 x i64> %z) {
ret <vscale x 2 x i64> %t3
}
+; fold mul(abs(x),abs(y)) -> abs(mul(x,y))
+define i32 @combine_mul_abs_x_abs_y(i32 %x, i32 %y) {
+; CHECK-LABEL: @combine_mul_abs_x_abs_y(
+; CHECK-NEXT: [[TMP1:%.*]] = mul nsw i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL:%.*]] = call i32 @llvm.abs.i32(i32 [[TMP1]], i1 true)
+; CHECK-NEXT: ret i32 [[MUL]]
+;
+ %abs_x = call i32 @llvm.abs.i32(i32 %x, i1 true)
+ %abs_y = call i32 @llvm.abs.i32(i32 %y, i1 true)
+ %mul = mul nsw i32 %abs_x, %abs_y
+ ret i32 %mul
+}
+
+define i32 @combine_mul_abs_x_abs_y_no_nsw(i32 %x, i32 %y) {
+; CHECK-LABEL: @combine_mul_abs_x_abs_y_no_nsw(
+; CHECK-NEXT: [[ABS_X:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
+; CHECK-NEXT: [[ABS_Y:%.*]] = call i32 @llvm.abs.i32(i32 [[Y:%.*]], i1 true)
+; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[ABS_X]], [[ABS_Y]]
+; CHECK-NEXT: ret i32 [[MUL]]
+;
+ %abs_x = call i32 @llvm.abs.i32(i32 %x, i1 true)
+ %abs_y = call i32 @llvm.abs.i32(i32 %y, i1 true)
+ %mul = mul i32 %abs_x, %abs_y
+ ret i32 %mul
+}
+
+define i32 @combine_mul_abs_x_abs_y_poison_1(i32 %x, i32 %y) {
+; CHECK-LABEL: @combine_mul_abs_x_abs_y_poison_1(
+; CHECK-NEXT: [[ABS_X:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
+; CHECK-NEXT: [[ABS_Y:%.*]] = call i32 @llvm.abs.i32(i32 [[Y:%.*]], i1 false)
+; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[ABS_X]], [[ABS_Y]]
+; CHECK-NEXT: ret i32 [[MUL]]
+;
+ %abs_x = call i32 @llvm.abs.i32(i32 %x, i1 true)
+ %abs_y = call i32 @llvm.abs.i32(i32 %y, i1 false)
+ %mul = mul nsw i32 %abs_x, %abs_y
+ ret i32 %mul
+}
+
+define i32 @combine_mul_abs_x_abs_y_poison_2(i32 %x, i32 %y) {
+; CHECK-LABEL: @combine_mul_abs_x_abs_y_poison_2(
+; CHECK-NEXT: [[ABS_X:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT: [[ABS_Y:%.*]] = call i32 @llvm.abs.i32(i32 [[Y:%.*]], i1 false)
+; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[ABS_X]], [[ABS_Y]]
+; CHECK-NEXT: ret i32 [[MUL]]
+;
+ %abs_x = call i32 @llvm.abs.i32(i32 %x, i1 false)
+ %abs_y = call i32 @llvm.abs.i32(i32 %y, i1 false)
+ %mul = mul nsw i32 %abs_x, %abs_y
+ ret i32 %mul
+}
+
+define i32 @combine_mul_abs_x_abs_y_not_oneuse(i32 %x, i32 %y) {
+; CHECK-LABEL: @combine_mul_abs_x_abs_y_not_oneuse(
+; CHECK-NEXT: [[ABS_X:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
+; CHECK-NEXT: [[ABS_Y:%.*]] = call i32 @llvm.abs.i32(i32 [[Y:%.*]], i1 true)
+; CHECK-NEXT: [[ABS_X1:%.*]] = add nuw i32 [[ABS_Y]], 1
+; CHECK-NEXT: [[RET:%.*]] = mul i32 [[ABS_X]], [[ABS_X1]]
+; CHECK-NEXT: ret i32 [[RET]]
+;
+ %abs_x = call i32 @llvm.abs.i32(i32 %x, i1 true)
+ %abs_y = call i32 @llvm.abs.i32(i32 %y, i1 true)
+ %mul = mul nsw i32 %abs_x, %abs_y
+ %ret = add i32 %mul, %abs_x
+ ret i32 %ret
+}
+
;
; fold mul(sub(x,y),negpow2) -> shl(sub(y,x),log2(pow2))
;
More information about the llvm-commits
mailing list