From: Torbjörn SVENSSON <[email protected]>
The Thumb-2 soft-float copysignsf3 expander copies the sign operand
to the destination before inserting the magnitude bits. If the
destination overlaps the magnitude operand, this clobbers the
magnitude before it is read.
Build the result in a temporary SImode register and copy it to the
destination after inserting the magnitude bits.
Add a GIMPLE regression test using PHIs to force the result to share
an out-of-SSA partition with either input, covering float and double
variants and constant operands.
Assisted-By: LLM for discovery and testcase generation.
PR target/128868
gcc/ChangeLog:
* config/arm/arm.md (copysignsf3): Use a temporary register to
allow the destination to overlap the magnitude operand.
gcc/testsuite/ChangeLog:
* gcc.dg/copysign-alias-1.c: New test.
Signed-off-by: Torbjörn SVENSSON <[email protected]>
---
gcc/config/arm/arm.md | 10 +-
gcc/testsuite/gcc.dg/copysign-alias-1.c | 181 ++++++++++++++++++++++++
2 files changed, 187 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/copysign-alias-1.c
diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md
index 785d341aa2aa7..c4c606730451b 100644
--- a/gcc/config/arm/arm.md
+++ b/gcc/config/arm/arm.md
@@ -12857,10 +12857,12 @@
(match_operand:SF 2 "register_operand")]
"TARGET_SOFT_FLOAT && arm_arch_thumb2"
"{
- 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),
- simplify_gen_subreg (SImode, operands[1], SFmode, 0)));
+ /* Use a scratch so that operands[0] may overlap operands[1]. */
+ rtx scratch = gen_reg_rtx (SImode);
+ emit_move_insn (scratch, gen_lowpart (SImode, operands[2]));
+ emit_insn (gen_insv_t2 (scratch, GEN_INT (31), GEN_INT (0),
+ gen_lowpart (SImode, operands[1])));
+ emit_move_insn (operands[0], gen_lowpart (SFmode, scratch));
DONE;
}"
)
diff --git a/gcc/testsuite/gcc.dg/copysign-alias-1.c
b/gcc/testsuite/gcc.dg/copysign-alias-1.c
new file mode 100644
index 0000000000000..5b2b41ff1bdbe
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/copysign-alias-1.c
@@ -0,0 +1,181 @@
+/* Check that copysign is correct when the result is assigned the same
+ register as the magnitude or the sign operand. The PHI makes the
+ result and that operand share a partition at out-of-SSA, independent
+ of register allocation. */
+/* { dg-do run } */
+/* { dg-options "-O2 -fgimple -fdump-rtl-expand-details" } */
+
+#define CHECK(expected, call) \
+ do { if ((expected) != (call)) __builtin_abort (); } while (0)
+
+float __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysignf_mag (float x, float y, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ x_5 = __builtin_copysignf (x_1(D), y_2(D));
+ goto __BB4;
+
+ __BB(4):
+ x_6 = __PHI (__BB2: x_1(D), __BB3: x_5);
+ return x_6;
+}
+
+float __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysignf_sign (float x, float y, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ y_5 = __builtin_copysignf (x_1(D), y_2(D));
+ goto __BB4;
+
+ __BB(4):
+ y_6 = __PHI (__BB2: y_2(D), __BB3: y_5);
+ return y_6;
+}
+
+float __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysignf_const_mag (float y, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ y_5 = __builtin_copysignf (2.0f, y_2(D));
+ goto __BB4;
+
+ __BB(4):
+ y_6 = __PHI (__BB2: y_2(D), __BB3: y_5);
+ return y_6;
+}
+
+/* -fabs (x) is canonicalized to copysign (x, -1.0). */
+float __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysignf_const_sign (float x, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ x_5 = __builtin_copysignf (x_1(D), _Literal (float) -1.0f);
+ goto __BB4;
+
+ __BB(4):
+ x_6 = __PHI (__BB2: x_1(D), __BB3: x_5);
+ return x_6;
+}
+
+double __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysign_mag (double x, double y, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ x_5 = __builtin_copysign (x_1(D), y_2(D));
+ goto __BB4;
+
+ __BB(4):
+ x_6 = __PHI (__BB2: x_1(D), __BB3: x_5);
+ return x_6;
+}
+
+double __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysign_sign (double x, double y, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ y_5 = __builtin_copysign (x_1(D), y_2(D));
+ goto __BB4;
+
+ __BB(4):
+ y_6 = __PHI (__BB2: y_2(D), __BB3: y_5);
+ return y_6;
+}
+
+double __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysign_const_mag (double y, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ y_5 = __builtin_copysign (2.0, y_2(D));
+ goto __BB4;
+
+ __BB(4):
+ y_6 = __PHI (__BB2: y_2(D), __BB3: y_5);
+ return y_6;
+}
+
+double __GIMPLE (ssa,startwith("optimized")) __attribute__((noipa))
+copysign_const_sign (double x, int do_copysign)
+{
+ __BB(2):
+ if (do_copysign_3(D) != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ x_5 = __builtin_copysign (x_1(D), _Literal (double) -1.0);
+ goto __BB4;
+
+ __BB(4):
+ x_6 = __PHI (__BB2: x_1(D), __BB3: x_5);
+ return x_6;
+}
+
+int
+main (void)
+{
+ CHECK (-2.0f, copysignf_mag (2.0f, -5.0f, 1));
+ CHECK (2.0f, copysignf_mag (-2.0f, 5.0f, 1));
+ CHECK (-2.0f, copysignf_sign (2.0f, -5.0f, 1));
+ CHECK (2.0f, copysignf_sign (-2.0f, 5.0f, 1));
+ CHECK (-2.0f, copysignf_const_mag (-5.0f, 1));
+ CHECK (2.0f, copysignf_const_mag (5.0f, 1));
+ CHECK (-2.0f, copysignf_const_sign (2.0f, 1));
+
+ CHECK (-2.0, copysign_mag (2.0, -5.0, 1));
+ CHECK (2.0, copysign_mag (-2.0, 5.0, 1));
+ CHECK (-2.0, copysign_sign (2.0, -5.0, 1));
+ CHECK (2.0, copysign_sign (-2.0, 5.0, 1));
+ CHECK (-2.0, copysign_const_mag (-5.0, 1));
+ CHECK (2.0, copysign_const_mag (5.0, 1));
+ CHECK (-2.0, copysign_const_sign (2.0, 1));
+
+ return 0;
+}
+
+/* Each function must make the copysign result share a partition with
+ the operand under test. */
+/* { dg-final { scan-rtl-dump-times {Partition \d+ \(x_1\(D\) - 1 5 6 \)} 4
"expand" } } */
+/* { dg-final { scan-rtl-dump-times {Partition \d+ \(y_2\(D\) - 2 5 6 \)} 4
"expand" } } */
--
2.56.0