[InstSimplify] fold copysign with negated operand
authorSanjay Patel <spatel@rotateright.com>
Sun, 8 Dec 2019 15:05:28 +0000 (10:05 -0500)
committerSanjay Patel <spatel@rotateright.com>
Sun, 8 Dec 2019 15:08:02 +0000 (10:08 -0500)
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.

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

index 7942cb0..d151b78 100644 (file)
@@ -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:
index 4a0dc41..a8a9bf5 100644 (file)
@@ -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)