[InstSimplify] fold copysign with negated operand, part 2
authorSanjay Patel <spatel@rotateright.com>
Sun, 8 Dec 2019 15:13:16 +0000 (10:13 -0500)
committerSanjay Patel <spatel@rotateright.com>
Sun, 8 Dec 2019 15:16:29 +0000 (10:16 -0500)
This is another transform suggested in PR44153:
https://bugs.llvm.org/show_bug.cgi?id=44153

Unlike rG12f39e0fede9, it doesn't look like the
backend matches this variant.

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

index d151b78..22a76a0 100644 (file)
@@ -5091,7 +5091,9 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
     if (Op0 == Op1)
       return Op0;
     // copysign -X, X --> X
-    if (match(Op0, m_FNeg(m_Specific(Op1))))
+    // copysign X, -X --> -X
+    if (match(Op0, m_FNeg(m_Specific(Op1))) ||
+        match(Op1, m_FNeg(m_Specific(Op0))))
       return Op1;
     break;
   case Intrinsic::maxnum:
index a8a9bf5..15c56d2 100644 (file)
@@ -943,8 +943,7 @@ define <2 x double> @copysign_same_operand_vec(<2 x double> %x) {
 define float @negated_sign_arg(float %x) {
 ; CHECK-LABEL: @negated_sign_arg(
 ; CHECK-NEXT:    [[NEGX:%.*]] = fsub ninf float -0.000000e+00, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call arcp float @llvm.copysign.f32(float [[X]], float [[NEGX]])
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    ret float [[NEGX]]
 ;
   %negx = fsub ninf float -0.0, %x
   %r = call arcp float @llvm.copysign.f32(float %x, float %negx)
@@ -954,8 +953,7 @@ define float @negated_sign_arg(float %x) {
 define <2 x double> @negated_sign_arg_vec(<2 x double> %x) {
 ; CHECK-LABEL: @negated_sign_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> [[X]], <2 x double> [[NEGX]])
-; CHECK-NEXT:    ret <2 x double> [[R]]
+; CHECK-NEXT:    ret <2 x double> [[NEGX]]
 ;
   %negx = fneg afn <2 x double> %x
   %r = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> %x, <2 x double> %negx)