Skip to content

[InstCombine] combine mul(abs(x),abs(y)) to abs(mul(x,y)) #78395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 18, 2024

Conversation

HerrCai0907
Copy link
Contributor

@HerrCai0907 HerrCai0907 commented Jan 17, 2024

@HerrCai0907 HerrCai0907 requested a review from nikic as a code owner January 17, 2024 05:25
@HerrCai0907 HerrCai0907 linked an issue Jan 17, 2024 that may be closed by this pull request
@HerrCai0907 HerrCai0907 changed the title [instCombine]combine mul(abs(x),abs(y)) to abs(mul(x,y)) [InstCombine]combine mul(abs(x),abs(y)) to abs(mul(x,y)) Jan 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Congcong Cai (HerrCai0907)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/78395.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (+17)
  • (modified) llvm/test/Transforms/InstCombine/mul.ll (+13)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index e7f983a00e3044..36930f24a542a2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -330,6 +330,23 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
       return BinaryOperator::CreateMul(X, X);
   }
 
+  {
+    Value *X, *Y, *Z;
+    // abs(X) * abs(Y) -> abs(X * Y)
+    // nabs(X) * nabs(Y) -> abs(X * Y)
+    SelectPatternFlavor SPF0 = matchSelectPattern(Op0, X, Z).Flavor;
+    SelectPatternFlavor SPF1 = matchSelectPattern(Op1, Y, Z).Flavor;
+    if ((SPF0 == SPF1) && (SPF0 == SPF_ABS || SPF0 == SPF_NABS))
+      return replaceInstUsesWith(
+          I, Builder.CreateBinaryIntrinsic(
+                 Intrinsic::abs, Builder.CreateMul(X, Y), Builder.getTrue()));
+    if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X))) &&
+        match(Op1, m_Intrinsic<Intrinsic::abs>(m_Value(Y))))
+      return replaceInstUsesWith(
+          I, Builder.CreateBinaryIntrinsic(
+                 Intrinsic::abs, Builder.CreateMul(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..0494091b038243 100644
--- a/llvm/test/Transforms/InstCombine/mul.ll
+++ b/llvm/test/Transforms/InstCombine/mul.ll
@@ -1649,6 +1649,19 @@ 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 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 false)
+  %abs_y = call i32 @llvm.abs.i32(i32 %y, i1 false)
+  %mul = mul i32 %abs_x, %abs_y
+  ret i32 %mul
+}
+
 ;
 ; fold mul(sub(x,y),negpow2) -> shl(sub(y,x),log2(pow2))
 ;

@HerrCai0907 HerrCai0907 added the llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes label Jan 17, 2024
@HerrCai0907 HerrCai0907 requested a review from dtcxzyw January 17, 2024 08:34
@HerrCai0907 HerrCai0907 requested a review from nikic January 17, 2024 10:36
dtcxzyw added a commit to dtcxzyw/llvm-opt-benchmark that referenced this pull request Jan 17, 2024
@HerrCai0907 HerrCai0907 requested a review from nikic January 17, 2024 13:59
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic nikic changed the title [InstCombine]combine mul(abs(x),abs(y)) to abs(mul(x,y)) [InstCombine] combine mul(abs(x),abs(y)) to abs(mul(x,y)) Jan 18, 2024
@HerrCai0907 HerrCai0907 merged commit 64e9443 into main Jan 18, 2024
@HerrCai0907 HerrCai0907 deleted the users/ccc/78076-multiplicativity-of-abs branch January 18, 2024 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

multiplicativity of abs
3 participants