-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Add fold for fabs(-x) -> fabs(x) #95627
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: Vaibhav (VaibhavRumale) ChangesHere is my patch for #94170 for fold transformation for fabs(-x) -> fabs(x)
Proofs: https://alive2.llvm.org/ce/z/pGzLts Full diff: https://github.com/llvm/llvm-project/pull/95627.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 436cdbff75669..f91aea35d1aab 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2511,6 +2511,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
case Intrinsic::fabs: {
Value *Cond, *TVal, *FVal;
+ Value* Arg = II->getArgOperand(0);
+ Value* X;
+ // fabs (-X) --> fabs (X)
+ if (match(Arg, m_FNeg(m_Value(X))))
+ return replaceInstUsesWith(
+ CI, Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X));
+
if (match(II->getArgOperand(0),
m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
diff --git a/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
new file mode 100644
index 0000000000000..bc239f207ef42
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -0,0 +1,138 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine %s | FileCheck %s
+
+define float @fabs_fneg_basic(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_basic(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %neg = fneg float %x
+ %fabs = call float @llvm.fabs.f32(float %neg)
+ ret float %fabs
+}
+
+define <2 x float> @fabs_fneg_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X]])
+; CHECK-NEXT: ret <2 x float> [[FABS]]
+;
+ %neg = fneg <2 x float> %x
+ %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+ ret <2 x float> %fabs
+}
+
+define double @fabs_fneg_f64(double %x) {
+; CHECK-LABEL: define double @fabs_fneg_f64(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X]])
+; CHECK-NEXT: ret double [[FABS]]
+;
+ %neg = fneg double %x
+ %fabs = call double @llvm.fabs.f64(double %neg)
+ ret double %fabs
+}
+
+define <4 x double> @fabs_fneg_v4f64(<4 x double> %x) {
+; CHECK-LABEL: define <4 x double> @fabs_fneg_v4f64(
+; CHECK-SAME: <4 x double> [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[X]])
+; CHECK-NEXT: ret <4 x double> [[FABS]]
+;
+ %neg = fneg <4 x double> %x
+ %fabs = call <4 x double> @llvm.fabs.v4f64(<4 x double> %neg)
+ ret <4 x double> %fabs
+}
+
+define half @fabs_fneg_f16(half %x) {
+; CHECK-LABEL: define half @fabs_fneg_f16(
+; CHECK-SAME: half [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call half @llvm.fabs.f16(half [[X]])
+; CHECK-NEXT: ret half [[FABS]]
+;
+ %neg = fneg half %x
+ %fabs = call half @llvm.fabs.f16(half %neg)
+ ret half %fabs
+}
+
+define float @fabs_fneg_nnan(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_nnan(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %neg = fneg nnan float %x
+ %fabs = call nnan float @llvm.fabs.f32(float %neg)
+ ret float %fabs
+}
+
+define float @fabs_fneg_ninf(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_ninf(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %neg = fneg ninf float %x
+ %fabs = call ninf float @llvm.fabs.f32(float %neg)
+ ret float %fabs
+}
+
+define float @fabs_fneg_no_fabs(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_no_fabs(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X]]
+; CHECK-NEXT: ret float [[NEG]]
+;
+ %neg = fneg float %x
+ ret float %neg
+}
+
+define <2 x float> @fabs_fneg_splat_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_splat_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT: ret <2 x float> <float 2.000000e+00, float 2.000000e+00>
+;
+ %neg = fneg <2 x float> <float -2.0, float -2.0>
+ %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+ ret <2 x float> %fabs
+}
+
+
+define <2 x float> @fabs_fneg_splat_poison_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_splat_poison_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> <float -2.000000e+00, float poison>)
+; CHECK-NEXT: ret <2 x float> [[FABS]]
+;
+ %neg = fneg <2 x float> <float 2.0, float poison>
+ %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+ ret <2 x float> %fabs
+}
+
+define <2 x float> @fabs_fneg_non_splat_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_non_splat_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT: ret <2 x float> <float 2.000000e+00, float 3.000000e+00>
+;
+ %neg = fneg <2 x float> <float 2.0, float 3.0>
+ %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+ ret <2 x float> %fabs
+}
+
+declare void @use(float)
+
+define float @fabs_fneg_multi_use(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_multi_use(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X]]
+; CHECK-NEXT: call void @use(float [[NEG]])
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %neg = fneg float %x
+ call void @use(float %neg)
+ %fabs = call float @llvm.fabs.f32(float %neg)
+ ret float %fabs
+}
+
|
Value* Arg = II->getArgOperand(0); | ||
Value* X; | ||
// fabs (-X) --> fabs (X) | ||
if (match(Arg, m_FNeg(m_Value(X)))) |
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.
The comment #94183 (comment) is not resolved.
✅ With the latest revision this PR passed the C/C++ code formatter. |
Reverse ping? |
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. Thank you!
Value *Arg = II->getArgOperand(0); | ||
Value *X; | ||
// fabs (-X) --> fabs (X) | ||
if (match(Arg, m_FNeg(m_Value(X)))) { |
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.
We can merge this fold with fabs(copysign(x, y)) -> fabs(x)
.
```suggestion
if (match(Arg, m_CombineOr(m_FNeg(m_Value(X)), m_CopySign(m_Value(X), m_Value(Unused))))) {
Sorry for the delay; I was busy with my graduation. All requested changes have been made. Thank you! |
@VaibhavRumale Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
This patch folds `fabs(-x) -> fabs(x)` Closes llvm#94170 Proofs: https://alive2.llvm.org/ce/z/gjzmgf
This patch folds
fabs(-x) -> fabs(x)
Closes #94170
Proofs: https://alive2.llvm.org/ce/z/gjzmgf