[llvm] [InstCombine] Add fold for fabs(-x) -> fabs(x) (PR #95627)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 15 05:19:48 PDT 2024


https://github.com/VaibhavRumale updated https://github.com/llvm/llvm-project/pull/95627

>From f310f627537dea75f8c4cd0e6e6cc20418bbf849 Mon Sep 17 00:00:00 2001
From: VaibhavRumale <vaibhav.rumale at gmail.com>
Date: Fri, 14 Jun 2024 18:45:45 -0700
Subject: [PATCH 1/5] [InstCombine] Add tests for fold: fabs(-x) -> fabs(x)

---
 .../Transforms/InstCombine/fabs-fneg-fold.ll  | 145 ++++++++++++++++++
 1 file changed, 145 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll

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..1f6b9cfb065da
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -0,0 +1,145 @@
+; 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:    [[NEG:%.*]] = fneg float [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[NEG]])
+; 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:    [[NEG:%.*]] = fneg <2 x float> [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[NEG]])
+; 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:    [[NEG:%.*]] = fneg double [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call double @llvm.fabs.f64(double [[NEG]])
+; 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:    [[NEG:%.*]] = fneg <4 x double> [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[NEG]])
+; 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:    [[NEG:%.*]] = fneg half [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call half @llvm.fabs.f16(half [[NEG]])
+; 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:    [[NEG:%.*]] = fneg nnan float [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call nnan float @llvm.fabs.f32(float [[NEG]])
+; 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:    [[NEG:%.*]] = fneg ninf float [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call ninf float @llvm.fabs.f32(float [[NEG]])
+; 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 [[NEG]])
+; 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
+}
+

>From c8956e0fe72a9422a15a57e397d54ab6cc8a9749 Mon Sep 17 00:00:00 2001
From: VaibhavRumale <vaibhav.rumale at gmail.com>
Date: Fri, 14 Jun 2024 19:25:49 -0700
Subject: [PATCH 2/5] [InstCombine] Add fold transformation: fabs(-x) ->
 fabs(x)

---
 .../InstCombine/InstCombineCalls.cpp          |  7 ++++++
 .../Transforms/InstCombine/fabs-fneg-fold.ll  | 23 +++++++------------
 2 files changed, 15 insertions(+), 15 deletions(-)

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
index 1f6b9cfb065da..bc239f207ef42 100644
--- a/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -4,8 +4,7 @@
 define float @fabs_fneg_basic(float %x) {
 ; CHECK-LABEL: define float @fabs_fneg_basic(
 ; CHECK-SAME: float [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg float [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg float %x
@@ -16,8 +15,7 @@ define float @fabs_fneg_basic(float %x) {
 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:    [[NEG:%.*]] = fneg <2 x float> [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[NEG]])
+; 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
@@ -28,8 +26,7 @@ define <2 x float> @fabs_fneg_v2f32(<2 x float> %x) {
 define double @fabs_fneg_f64(double %x) {
 ; CHECK-LABEL: define double @fabs_fneg_f64(
 ; CHECK-SAME: double [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg double [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call double @llvm.fabs.f64(double [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X]])
 ; CHECK-NEXT:    ret double [[FABS]]
 ;
   %neg = fneg double %x
@@ -40,8 +37,7 @@ define double @fabs_fneg_f64(double %x) {
 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:    [[NEG:%.*]] = fneg <4 x double> [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[NEG]])
+; 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
@@ -52,8 +48,7 @@ define <4 x double> @fabs_fneg_v4f64(<4 x double> %x) {
 define half @fabs_fneg_f16(half %x) {
 ; CHECK-LABEL: define half @fabs_fneg_f16(
 ; CHECK-SAME: half [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg half [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call half @llvm.fabs.f16(half [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call half @llvm.fabs.f16(half [[X]])
 ; CHECK-NEXT:    ret half [[FABS]]
 ;
   %neg = fneg half %x
@@ -64,8 +59,7 @@ define half @fabs_fneg_f16(half %x) {
 define float @fabs_fneg_nnan(float %x) {
 ; CHECK-LABEL: define float @fabs_fneg_nnan(
 ; CHECK-SAME: float [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg nnan float [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call nnan float @llvm.fabs.f32(float [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg nnan float %x
@@ -76,8 +70,7 @@ define float @fabs_fneg_nnan(float %x) {
 define float @fabs_fneg_ninf(float %x) {
 ; CHECK-LABEL: define float @fabs_fneg_ninf(
 ; CHECK-SAME: float [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg ninf float [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call ninf float @llvm.fabs.f32(float [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg ninf float %x
@@ -134,7 +127,7 @@ define float @fabs_fneg_multi_use(float %x) {
 ; 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 [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg float %x

>From bc5a50fe78ce88d7ab24c8bda945deee630da426 Mon Sep 17 00:00:00 2001
From: VaibhavRumale <vaibhav.rumale at gmail.com>
Date: Fri, 14 Jun 2024 19:50:02 -0700
Subject: [PATCH 3/5] [InstCombine] Use Arg variable in fabs select match

---
 llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index f91aea35d1aab..71af5eca4343d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2518,16 +2518,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         return replaceInstUsesWith(
             CI, Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X));
 
-    if (match(II->getArgOperand(0),
-              m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
+    if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
       // fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
       if (isa<Constant>(TVal) || isa<Constant>(FVal)) {
         CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
         CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
         SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);
         FastMathFlags FMF1 = II->getFastMathFlags();
-        FastMathFlags FMF2 =
-            cast<SelectInst>(II->getArgOperand(0))->getFastMathFlags();
+        FastMathFlags FMF2 = cast<SelectInst>(Arg)->getFastMathFlags();
         FMF2.setNoSignedZeros(false);
         SI->setFastMathFlags(FMF1 | FMF2);
         return SI;

>From ae4ba74b2ef4203a7a7deece6974c2818f7f468f Mon Sep 17 00:00:00 2001
From: VaibhavRumale <vaibhav.rumale at gmail.com>
Date: Sat, 15 Jun 2024 04:47:01 -0700
Subject: [PATCH 4/5] [InstCombine] Add tests with copysign instead of fneg

---
 .../Transforms/InstCombine/fabs-fneg-fold.ll  | 52 ++++++++++++-------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
index bc239f207ef42..9922343c68b27 100644
--- a/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -4,7 +4,8 @@
 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:    [[NEG:%.*]] = fneg float [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[NEG]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg float %x
@@ -15,7 +16,8 @@ define float @fabs_fneg_basic(float %x) {
 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:    [[NEG:%.*]] = fneg <2 x float> [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[NEG]])
 ; CHECK-NEXT:    ret <2 x float> [[FABS]]
 ;
   %neg = fneg <2 x float> %x
@@ -26,7 +28,8 @@ define <2 x float> @fabs_fneg_v2f32(<2 x float> %x) {
 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:    [[NEG:%.*]] = fneg double [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call double @llvm.fabs.f64(double [[NEG]])
 ; CHECK-NEXT:    ret double [[FABS]]
 ;
   %neg = fneg double %x
@@ -37,7 +40,8 @@ define double @fabs_fneg_f64(double %x) {
 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:    [[NEG:%.*]] = fneg <4 x double> [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[NEG]])
 ; CHECK-NEXT:    ret <4 x double> [[FABS]]
 ;
   %neg = fneg <4 x double> %x
@@ -48,7 +52,8 @@ define <4 x double> @fabs_fneg_v4f64(<4 x double> %x) {
 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:    [[NEG:%.*]] = fneg half [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = call half @llvm.fabs.f16(half [[NEG]])
 ; CHECK-NEXT:    ret half [[FABS]]
 ;
   %neg = fneg half %x
@@ -56,25 +61,36 @@ define half @fabs_fneg_f16(half %x) {
   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]])
+define float @fabs_copysign_nnan(float %x, float %y) {
+; CHECK-LABEL: define float @fabs_copysign_nnan(
+; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
+; CHECK-NEXT:    [[FABS:%.*]] = call nnan 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)
+  %copysign = call nnan float @llvm.copysign.f32(float %x, float %y)
+  %fabs = call nnan float @llvm.fabs.f32(float %copysign)
   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]])
+define float @fabs_copysign_ninf(float %x, float %y) {
+; CHECK-LABEL: define float @fabs_copysign_ninf(
+; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
+; CHECK-NEXT:    [[FABS:%.*]] = call ninf float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT:    ret float [[FABS]]
+;
+  %copysign = call ninf float @llvm.copysign.f32(float %x, float %y)
+  %fabs = call ninf float @llvm.fabs.f32(float %copysign)
+  ret float %fabs
+}
+
+define float @fabs_copysign_nsz(float %x, float %y) {
+; CHECK-LABEL: define float @fabs_copysign_nsz(
+; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
+; CHECK-NEXT:    [[FABS:%.*]] = call nsz 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)
+  %copysign = call nsz float @llvm.copysign.f32(float %x, float %y)
+  %fabs = call nsz float @llvm.fabs.f32(float %copysign)
   ret float %fabs
 }
 
@@ -127,7 +143,7 @@ define float @fabs_fneg_multi_use(float %x) {
 ; 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:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[NEG]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg float %x

>From 1c6e39fbc3feb794e3d0108a5e21fb5d447fb4d3 Mon Sep 17 00:00:00 2001
From: VaibhavRumale <vaibhav.rumale at gmail.com>
Date: Sat, 15 Jun 2024 05:13:15 -0700
Subject: [PATCH 5/5] [InstCombine] Ensure FMF preservation in fabs
 transformation

---
 .../Transforms/InstCombine/InstCombineCalls.cpp |  8 +++++---
 .../Transforms/InstCombine/fabs-fneg-fold.ll    | 17 ++++++-----------
 2 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 71af5eca4343d..c4a3ab9c181e1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2514,9 +2514,11 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     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(Arg, m_FNeg(m_Value(X)))) {
+      CallInst *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X);
+      Fabs->copyFastMathFlags(II);
+      return replaceInstUsesWith(CI, Fabs);
+    }
 
     if (match(Arg, 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
index 9922343c68b27..ffa26bee22e3f 100644
--- a/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -4,8 +4,7 @@
 define float @fabs_fneg_basic(float %x) {
 ; CHECK-LABEL: define float @fabs_fneg_basic(
 ; CHECK-SAME: float [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg float [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg float %x
@@ -16,8 +15,7 @@ define float @fabs_fneg_basic(float %x) {
 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:    [[NEG:%.*]] = fneg <2 x float> [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[NEG]])
+; 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
@@ -28,8 +26,7 @@ define <2 x float> @fabs_fneg_v2f32(<2 x float> %x) {
 define double @fabs_fneg_f64(double %x) {
 ; CHECK-LABEL: define double @fabs_fneg_f64(
 ; CHECK-SAME: double [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg double [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call double @llvm.fabs.f64(double [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X]])
 ; CHECK-NEXT:    ret double [[FABS]]
 ;
   %neg = fneg double %x
@@ -40,8 +37,7 @@ define double @fabs_fneg_f64(double %x) {
 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:    [[NEG:%.*]] = fneg <4 x double> [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[NEG]])
+; 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
@@ -52,8 +48,7 @@ define <4 x double> @fabs_fneg_v4f64(<4 x double> %x) {
 define half @fabs_fneg_f16(half %x) {
 ; CHECK-LABEL: define half @fabs_fneg_f16(
 ; CHECK-SAME: half [[X:%.*]]) {
-; CHECK-NEXT:    [[NEG:%.*]] = fneg half [[X]]
-; CHECK-NEXT:    [[FABS:%.*]] = call half @llvm.fabs.f16(half [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call half @llvm.fabs.f16(half [[X]])
 ; CHECK-NEXT:    ret half [[FABS]]
 ;
   %neg = fneg half %x
@@ -143,7 +138,7 @@ define float @fabs_fneg_multi_use(float %x) {
 ; 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 [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
 ; CHECK-NEXT:    ret float [[FABS]]
 ;
   %neg = fneg float %x



More information about the llvm-commits mailing list