From: Torbjörn SVENSSON <[email protected]>

The scalar copysign expanders currently only support soft float. Enable
them for hardware floating point too, so the existing tree optimizations
can use them on Thumb-2-capable targets.

Use VABS and conditional VNEG to avoid moving the magnitude through
integer registers. Accept general input operands so constant signs can
be folded.

        PR target/124346
        PR target/124695
        PR testsuite/113425

gcc/ChangeLog:

        * config/arm/arm.md (copysignsf3): Add hardware floating-point
        support and accept general input operands.
        (copysigndf3): Likewise, requiring double-precision VFP support.

gcc/testsuite/ChangeLog:

        * lib/target-supports.exp
        (check_effective_target_ifn_copysignf): Check ARM scalar
        copysign support instead of NEON availability.
        (check_effective_target_ifn_copysignd): Exclude ARM targets with
        single-precision-only hardware floating-point support.
        * gcc.dg/tree-ssa/abs-4.c: Expect three COPYSIGN operations on
        ARM.
        * gcc.dg/tree-ssa/backprop-6.c: Expect three COPYSIGN deletions
        on ARM.

Signed-off-by: Torbjörn SVENSSON <[email protected]>
---
 gcc/config/arm/arm.md                      | 138 ++++++++++++++++++++-
 gcc/testsuite/gcc.dg/tree-ssa/abs-4.c      |   8 +-
 gcc/testsuite/gcc.dg/tree-ssa/backprop-6.c |   8 +-
 gcc/testsuite/lib/target-supports.exp      |  12 +-
 4 files changed, 149 insertions(+), 17 deletions(-)

diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md
index 785d341aa2aa7..d1e6f01df78aa 100644
--- a/gcc/config/arm/arm.md
+++ b/gcc/config/arm/arm.md
@@ -12853,10 +12853,68 @@
 
 (define_expand "copysignsf3"
   [(match_operand:SF 0 "register_operand")
-   (match_operand:SF 1 "register_operand")
-   (match_operand:SF 2 "register_operand")]
-  "TARGET_SOFT_FLOAT && arm_arch_thumb2"
+   (match_operand:SF 1 "general_operand")
+   (match_operand:SF 2 "general_operand")]
+  "(TARGET_SOFT_FLOAT || TARGET_HARD_FLOAT) && arm_arch_thumb2"
   "{
+     if (TARGET_HARD_FLOAT)
+       {
+       rtx magnitude = simplify_gen_unary (ABS, SFmode, operands[1], SFmode);
+       if (CONST_DOUBLE_AS_FLOAT_P (operands[2]))
+         {
+           /* A known sign needs no conditional negation.  */
+           if (real_isneg (CONST_DOUBLE_REAL_VALUE (operands[2])))
+             magnitude = simplify_gen_unary (NEG, SFmode, magnitude, SFmode);
+           emit_move_insn (operands[0],
+                           force_operand (magnitude, operands[0]));
+           DONE;
+         }
+
+       /* Prepare the sign test without unnecessary register transfers.  */
+       const bool constant_magnitude = CONST_DOUBLE_AS_FLOAT_P (magnitude);
+       rtx sign = gen_lowpart (SImode, operands[2]);
+       if (constant_magnitude)
+         sign = copy_to_mode_reg (SImode, sign);
+       else if (!s_register_operand (sign, SImode))
+         sign = force_reg (SImode, sign);
+
+       const rtx_code code = GE;
+       rtx cc_reg;
+       if (constant_magnitude)
+         {
+           /* Load the constant before the sign test to select +C or -C instead
+              of conditionally negating the loaded value.  */
+           emit_move_insn (operands[0],
+                           force_operand (magnitude, operands[0]));
+           cc_reg = arm_gen_compare_reg (code, sign, const0_rtx, NULL_RTX);
+         }
+       else
+         {
+           /* Test before ABS so the destination can reuse the sign input's
+              register without an extra copy.  */
+           cc_reg = arm_gen_compare_reg (code, sign, const0_rtx, NULL_RTX);
+           emit_move_insn (operands[0],
+                           force_operand (magnitude, operands[0]));
+           magnitude = operands[0];
+         }
+
+       rtx_code_label *label = gen_label_rtx ();
+       rtx condition = gen_rtx_fmt_ee (code, VOIDmode, cc_reg, const0_rtx);
+       rtx branch = gen_arm_cond_branch (label, condition, cc_reg);
+       rtx_insn *jump = emit_jump_insn (branch);
+
+       /* Treat both signs as equally likely.  */
+       add_reg_br_prob_note (jump, profile_probability::even ());
+
+       /* Apply the negative sign, folding constant negation when possible.  */
+       rtx negative = simplify_gen_unary (NEG, SFmode, magnitude, SFmode);
+       emit_move_insn (operands[0], force_operand (negative, operands[0]));
+       emit_label (label);
+       DONE;
+       }
+
+     operands[1] = force_reg (SFmode, operands[1]);
+     operands[2] = force_reg (SFmode, operands[2]);
      emit_move_insn (operands[0], operands[2]);
      emit_insn (gen_insv_t2 (simplify_gen_subreg (SImode, operands[0], SFmode, 
0),
                GEN_INT (31), GEN_INT (0),
@@ -12867,10 +12925,78 @@
 
 (define_expand "copysigndf3"
   [(match_operand:DF 0 "register_operand")
-   (match_operand:DF 1 "register_operand")
-   (match_operand:DF 2 "register_operand")]
-  "TARGET_SOFT_FLOAT && arm_arch_thumb2"
+   (match_operand:DF 1 "general_operand")
+   (match_operand:DF 2 "general_operand")]
+  "(TARGET_SOFT_FLOAT || (TARGET_HARD_FLOAT && TARGET_VFP_DOUBLE))
+   && arm_arch_thumb2"
   "{
+     if (TARGET_HARD_FLOAT)
+       {
+       rtx magnitude = simplify_gen_unary (ABS, DFmode, operands[1], DFmode);
+       if (CONST_DOUBLE_AS_FLOAT_P (operands[2]))
+         {
+           /* A known sign needs no conditional negation.  */
+           if (real_isneg (CONST_DOUBLE_REAL_VALUE (operands[2])))
+             magnitude = simplify_gen_unary (NEG, DFmode, magnitude, DFmode);
+           emit_move_insn (operands[0],
+                           force_operand (magnitude, operands[0]));
+           DONE;
+         }
+
+       /* Prepare the sign test without unnecessary register transfers.  */
+       const bool constant_magnitude = CONST_DOUBLE_AS_FLOAT_P (magnitude);
+       const int sign_word = FLOAT_WORDS_BIG_ENDIAN ? 0 : 1;
+       rtx sign = operand_subword_force (operands[2], sign_word, DFmode);
+       if (!s_register_operand (sign, SImode))
+         sign = force_reg (SImode, sign);
+
+       rtx_code code = GE;
+       if (constant_magnitude)
+         {
+           /* Use a mask to keep the sign source in core registers.  */
+           rtx sign_mask = gen_int_mode (HOST_WIDE_INT_1U << 31, SImode);
+           rtx masked_sign = gen_reg_rtx (SImode);
+           emit_insn (gen_andsi3 (masked_sign, sign, sign_mask));
+           sign = masked_sign;
+           code = EQ;
+         }
+
+       rtx cc_reg;
+       if (constant_magnitude)
+         {
+           /* Load the constant before the sign test to select +C or -C instead
+              of conditionally negating the loaded value.  */
+           emit_move_insn (operands[0],
+                           force_operand (magnitude, operands[0]));
+           cc_reg = arm_gen_compare_reg (code, sign, const0_rtx, NULL_RTX);
+         }
+       else
+         {
+           /* Test before ABS so the destination can reuse the sign input's
+              register without an extra copy.  */
+           cc_reg = arm_gen_compare_reg (code, sign, const0_rtx, NULL_RTX);
+           emit_move_insn (operands[0],
+                           force_operand (magnitude, operands[0]));
+           magnitude = operands[0];
+         }
+
+       rtx_code_label *label = gen_label_rtx ();
+       rtx condition = gen_rtx_fmt_ee (code, VOIDmode, cc_reg, const0_rtx);
+       rtx branch = gen_arm_cond_branch (label, condition, cc_reg);
+       rtx_insn *jump = emit_jump_insn (branch);
+
+       /* Treat both signs as equally likely.  */
+       add_reg_br_prob_note (jump, profile_probability::even ());
+
+       /* Apply the negative sign, folding constant negation when possible.  */
+       rtx negative = simplify_gen_unary (NEG, DFmode, magnitude, DFmode);
+       emit_move_insn (operands[0], force_operand (negative, operands[0]));
+       emit_label (label);
+       DONE;
+       }
+
+     operands[1] = force_reg (DFmode, operands[1]);
+     operands[2] = force_reg (DFmode, operands[2]);
      rtx op0_low = gen_lowpart (SImode, operands[0]);
      rtx op0_high = gen_highpart (SImode, operands[0]);
      rtx op1_low = gen_lowpart (SImode, operands[1]);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c 
b/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c
index 06482c5b967e2..eea99b7ffe532 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c
@@ -10,10 +10,10 @@ long double abs_ld(long double x) { return 
__builtin_signbit(x) ? x : -x; }
 
 /* __builtin_signbit(x) ? x : -x. Should be convert into - ABS_EXP<x> */
 /* { dg-final { scan-tree-dump-not "signbit" "optimized"} } */
-/* { dg-final { scan-tree-dump-times "= ABS_EXPR" 1 "optimized" { target { 
ifn_copysignd && { ! { s390*-*-* } } } } } } */
-/* { dg-final { scan-tree-dump-times "= -" 1 "optimized" { target { 
ifn_copysignd && { ! { s390*-*-* } } } } } } */
-/* { dg-final { scan-tree-dump-times "= \.COPYSIGN" 2 "optimized" { target { 
ifn_copysignd && { ! { s390*-*-* } } } } } } */
-/* { dg-final { scan-tree-dump-times "= \.COPYSIGN" 3 "optimized" { target { 
ifn_copysignd && s390*-*-* } } } } */
+/* { dg-final { scan-tree-dump-times "= ABS_EXPR" 1 "optimized" { target { 
ifn_copysignd && { ! { s390*-*-* arm*-*-* } } } } } } */
+/* { dg-final { scan-tree-dump-times "= -" 1 "optimized" { target { 
ifn_copysignd && { ! { s390*-*-* arm*-*-* } } } } } } */
+/* { dg-final { scan-tree-dump-times "= \.COPYSIGN" 2 "optimized" { target { 
ifn_copysignd && { ! { s390*-*-* arm*-*-* } } } } } } */
+/* { dg-final { scan-tree-dump-times "= \.COPYSIGN" 3 "optimized" { target { 
ifn_copysignd && { s390*-*-* arm*-*-* } } } } } */
 /* { dg-final { scan-tree-dump-times "= ABS_EXPR" 3 "optimized" { target { ! 
ifn_copysignf } } } } */
 /* { dg-final { scan-tree-dump-times "= -" 3 "optimized" { target { ! 
ifn_copysignf } } } } */
 /* { dg-final { scan-tree-dump-times "= ABS_EXPR" 2 "optimized" { target { 
ifn_copysignf && { ! ifn_copysignd } } } } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/backprop-6.c 
b/gcc/testsuite/gcc.dg/tree-ssa/backprop-6.c
index afc3b67a170c2..11ab85d470b5f 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/backprop-6.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/backprop-6.c
@@ -27,10 +27,10 @@ TEST_FUNCTION (float, f)
 TEST_FUNCTION (double, )
 TEST_FUNCTION (long double, l)
 
-/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = -} 4 "backprop" { 
target { ifn_copysignd && { ! { s390*-*-* } } } } } } */
-/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = \.COPYSIGN} 2 
"backprop" { target { ifn_copysignd && { ! { s390*-*-* } } } } } } */
-/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = ABS_EXPR <} 1 
"backprop" { target { ifn_copysignd && { ! { s390*-*-* } } } } } } */
-/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = \.COPYSIGN} 3 
"backprop" { target { ifn_copysignd && s390*-*-* } } } } */
+/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = -} 4 "backprop" { 
target { ifn_copysignd && { ! { s390*-*-* arm*-*-* } } } } } } */
+/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = \.COPYSIGN} 2 
"backprop" { target { ifn_copysignd && { ! { s390*-*-* arm*-*-* } } } } } } */
+/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = ABS_EXPR <} 1 
"backprop" { target { ifn_copysignd && { ! { s390*-*-* arm*-*-* } } } } } } */
+/* { dg-final { scan-tree-dump-times {Deleting[^\n]* = \.COPYSIGN} 3 
"backprop" { target { ifn_copysignd && { s390*-*-* arm*-*-* } } } } } */
 /* { dg-final { scan-tree-dump-times {Deleting[^\n]* = -} 6 "backprop" { 
target { ! ifn_copysignf } } } } */
 /* { dg-final { scan-tree-dump-times {Deleting[^\n]* = ABS_EXPR <} 3 
"backprop" { target { ! ifn_copysignf } } } } */
 /* { dg-final { scan-tree-dump-times {Deleting[^\n]* = -} 5 "backprop" { 
target { ifn_copysignf && { ! ifn_copysignd } } } } } */
diff --git a/gcc/testsuite/lib/target-supports.exp 
b/gcc/testsuite/lib/target-supports.exp
index c8f44e364925c..4ec491c23a3df 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -9095,7 +9095,10 @@ proc check_effective_target_ifn_copysignf { } {
         || [istarget powerpc*-*-*]
         || [istarget alpha*-*-*]
         || [istarget aarch64*-*-*]
-        || [is-effective-target arm_neon]
+        || ([istarget arm*-*-*]
+            && [check_effective_target_arm_thumb2_ok]
+            && ([check_effective_target_arm_softfloat]
+                || [check_effective_target_hard_float]))
         || ([istarget s390*-*-*]
             && [check_effective_target_s390_vx])
         || ([istarget riscv*-*-*]
@@ -9110,8 +9113,11 @@ proc check_effective_target_ifn_copysignf { } {
 
 proc check_effective_target_ifn_copysignd { } {
     return [check_cached_effective_target ifn_copysignd {
-       check_effective_target_ifn_copysignf
-    }]
+      expr {
+        [check_effective_target_ifn_copysignf]
+        && !([istarget arm*-*-*]
+             && [check_effective_target_arm_mixed_fp])
+       }}]
 }
 
 # Return 1 if the target plus current options supports a vector
-- 
2.56.0

Reply via email to