-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[InstCombine] combine mul(abs(x),abs(y)) to abs(mul(x,y)) #78395
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@llvm/pr-subscribers-llvm-transforms Author: Congcong Cai (HerrCai0907) ChangesFull diff: https://github.com/llvm/llvm-project/pull/78395.diff 2 Files Affected:
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))
;
|
nikic
requested changes
Jan 17, 2024
nikic
reviewed
Jan 17, 2024
dtcxzyw
added a commit
to dtcxzyw/llvm-opt-benchmark
that referenced
this pull request
Jan 17, 2024
nikic
approved these changes
Jan 18, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #78076
Alive2 Proof: https://alive2.llvm.org/ce/z/XEDy0f