[llvm] 3ffb751 - [InstCombine] fold copysign with fabs/fneg operand

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 06:24:46 PDT 2020


Author: Sanjay Patel
Date: 2020-08-16T08:53:47-04:00
New Revision: 3ffb751f3dbf059b2ec061fe2f4302c9eba26b43

URL: https://github.com/llvm/llvm-project/commit/3ffb751f3dbf059b2ec061fe2f4302c9eba26b43
DIFF: https://github.com/llvm/llvm-project/commit/3ffb751f3dbf059b2ec061fe2f4302c9eba26b43.diff

LOG: [InstCombine] fold copysign with fabs/fneg operand

We already get this in the backend, but we need to do
it in IR too to consistently get yet more copysign
transforms.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/test/Transforms/InstCombine/copysign.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 864a5f4183c6..6a188f6a4da4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1221,6 +1221,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     if (match(Sign, m_Intrinsic<Intrinsic::copysign>(m_Value(), m_Value(X))))
       return replaceOperand(*II, 1, X);
 
+    // Peek through changes of magnitude's sign-bit. This call rewrites those:
+    // copysign (fabs X), Sign --> copysign X, Sign
+    // copysign (fneg X), Sign --> copysign X, Sign
+    if (match(Mag, m_FAbs(m_Value(X))) || match(Mag, m_FNeg(m_Value(X))))
+      return replaceOperand(*II, 0, X);
+
     break;
   }
   case Intrinsic::fabs: {

diff  --git a/llvm/test/Transforms/InstCombine/copysign.ll b/llvm/test/Transforms/InstCombine/copysign.ll
index f278be71b819..1a8910b31132 100644
--- a/llvm/test/Transforms/InstCombine/copysign.ll
+++ b/llvm/test/Transforms/InstCombine/copysign.ll
@@ -92,8 +92,7 @@ define float @copysign_sign_arg(float %x, float %y, float %z) {
 
 define float @fneg_mag(float %x, float %y) {
 ; CHECK-LABEL: @fneg_mag(
-; CHECK-NEXT:    [[N:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float [[N]], float [[Y:%.*]])
+; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float [[X:%.*]], float [[Y:%.*]])
 ; CHECK-NEXT:    ret float [[R]]
 ;
   %n = fneg float %x
@@ -103,8 +102,7 @@ define float @fneg_mag(float %x, float %y) {
 
 define float @fabs_mag(float %x, float %y) {
 ; CHECK-LABEL: @fabs_mag(
-; CHECK-NEXT:    [[A:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
-; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float [[A]], float [[Y:%.*]])
+; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float [[X:%.*]], float [[Y:%.*]])
 ; CHECK-NEXT:    ret float [[R]]
 ;
   %a = call float @llvm.fabs.f32(float %x)


        


More information about the llvm-commits mailing list