[llvm] 12f39e0 - [InstSimplify] fold copysign with negated operand

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 8 07:09:07 PST 2019


Author: Sanjay Patel
Date: 2019-12-08T10:08:02-05:00
New Revision: 12f39e0fede92ca04c811660530e750585124ed2

URL: https://github.com/llvm/llvm-project/commit/12f39e0fede92ca04c811660530e750585124ed2
DIFF: https://github.com/llvm/llvm-project/commit/12f39e0fede92ca04c811660530e750585124ed2.diff

LOG: [InstSimplify] fold copysign with negated operand

This is another transform suggested in PR44153:
https://bugs.llvm.org/show_bug.cgi?id=44153

The backend for some targets already manages to get
this if it converts copysign to bitwise logic.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/call.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 7942cb09e84c..d151b78f04c0 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5090,6 +5090,9 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
     // copysign X, X --> X
     if (Op0 == Op1)
       return Op0;
+    // copysign -X, X --> X
+    if (match(Op0, m_FNeg(m_Specific(Op1))))
+      return Op1;
     break;
   case Intrinsic::maxnum:
   case Intrinsic::minnum:

diff  --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll
index 4a0dc41accc4..a8a9bf54378e 100644
--- a/llvm/test/Transforms/InstSimplify/call.ll
+++ b/llvm/test/Transforms/InstSimplify/call.ll
@@ -964,9 +964,7 @@ define <2 x double> @negated_sign_arg_vec(<2 x double> %x) {
 
 define float @negated_mag_arg(float %x) {
 ; CHECK-LABEL: @negated_mag_arg(
-; CHECK-NEXT:    [[NEGX:%.*]] = fneg nnan float [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call ninf float @llvm.copysign.f32(float [[NEGX]], float [[X]])
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    ret float [[X:%.*]]
 ;
   %negx = fneg nnan float %x
   %r = call ninf float @llvm.copysign.f32(float %negx, float %x)
@@ -975,9 +973,7 @@ define float @negated_mag_arg(float %x) {
 
 define <2 x double> @negated_mag_arg_vec(<2 x double> %x) {
 ; CHECK-LABEL: @negated_mag_arg_vec(
-; CHECK-NEXT:    [[NEGX:%.*]] = fneg afn <2 x double> [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> [[NEGX]], <2 x double> [[X]])
-; CHECK-NEXT:    ret <2 x double> [[R]]
+; CHECK-NEXT:    ret <2 x double> [[X:%.*]]
 ;
   %negx = fneg afn <2 x double> %x
   %r = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> %negx, <2 x double> %x)


        


More information about the llvm-commits mailing list