https://gcc.gnu.org/g:4ecca2eba5a6932251248195cb4b2c348b240a01

commit r17-6673-g4ecca2eba5a6932251248195cb4b2c348b240a01
Author: Igor Shevlyakov <[email protected]>
Date:   Sun Nov 22 14:50:50 2026 -0800

    match.pd: Fold sign-bit operations on reinterpreted floats [PR64897]
    
    Code that works on the bits of a floating-point value clears, flips or
    sets its sign bit through an integer of the same size:
    
        (T)((I)x & ~signbit), (T)((I)x ^ signbit), (T)((I)x | signbit)
    
    GIMPLE keeps the VIEW_CONVERT_EXPRs and the integer operation, so the
    value is usually moved to an integer register and back, as in the PR.
    These are exactly fabs, negation and the negation of fabs, including for
    NaNs and signed zeros, so canonicalize them to ABS_EXPR, NEGATE_EXPR and
    the NEGATE_EXPR of an ABS_EXPR; the last becomes copysign (x, -1.0)
    where the target has copysign, as -fabs (x) does.  They then lower to
    the target's native instructions and combine with surrounding
    floating-point code like any other abs or negate.  Where the target has
    no abs or neg pattern (or, as MIPS with -mabs=legacy, cannot use it when
    NaNs matter) expand lowers them to the same mask.  With
    -ffinite-math-only or -fno-signed-zeros they follow the rules for the
    type like any other abs or negation: MIPS -mabs=legacy then uses
    abs.fmt, and PA 1.x negates by subtracting from zero.
    
    The sign bit is taken from REAL_MODE_FORMAT (mode)->signbit_rw rather
    than sign_mask_for, so the fold also covers bfloat16, whose format has
    ieee_bits == 0.  It is restricted to scalar binary floating-point modes
    without padding bits whose sign bit is the most significant bit of the
    integer, with the same word order as integers.  With -fsignaling-nans
    it is not done: the integer operation keeps a signaling NaN as it is,
    while the abs or negation may go through an FP unit that quiets it, as
    the x87 does.  The fold is done on GIMPLE only: in GENERIC the operand
    can be a reference with reverse storage order, whose VIEW_CONVERT_EXPR
    reads its bytes without swapping them, so the MSB of the integer is not
    the sign bit of the value.
    
    In a register the floating-point forms are better than the integer
    ones: for setting the sign bit, AArch64 uses one orr for float, POWER
    fnabs, x86 orps and RISC-V a constant and fsgnj, instead of moving the
    value to an integer register and back.
    
    Assisted-by: Claude Opus 5.5
    
            PR target/64897
    
    gcc/ChangeLog:
    
            * match.pd ((T)((I)x & ~signbit) -> abs (x)): New simplification,
            also folding ^ signbit to -x and | signbit to -abs (x).
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr64897.c: New test.
            * gcc.dg/tree-ssa/pr64897-2.c: New test.
            * gcc.dg/tree-ssa/pr64897-3.c: New test.
            * gcc.dg/tree-ssa/pr64897-4.c: New test.
            * gcc.dg/tree-ssa/pr64897-5.c: New test.
            * gcc.dg/tree-ssa/pr64897-6.c: New test.
            * gcc.dg/tree-ssa/pr64897-7.c: New test.
            * gcc.dg/torture/pr64897.c: New test.
    
    Signed-off-by: Igor Shevlyakov <[email protected]>

Diff:
---
 gcc/match.pd                              | 35 ++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr64897.c    | 61 +++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897-2.c | 28 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897-3.c | 28 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897-4.c | 30 +++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897-5.c | 30 +++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897-6.c | 18 +++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897-7.c | 24 ++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr64897.c   | 24 ++++++++++++
 9 files changed, 278 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 9e717a95ae04..2ccb7d1ff282 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -604,6 +604,41 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr2))
     (minmax @1 (plus @2 @0))))))
 
+/* (T)((I)x & ~signbit) -> abs (x), (T)((I)x ^ signbit) -> -x and
+   (T)((I)x | signbit) -> -abs (x) when the sign bit is the MSB, as these
+   only change the sign bit.  -abs (x) becomes copysign (x, -1) where the
+   target has copysign.  Without abs and neg patterns expand turns them
+   back into the same mask.  Not with signaling NaNs honored: the integer
+   operation keeps them as they are, while the abs or neg can go through
+   an FP unit that quiets them (x87).  Decimal floats are left alone.
+   GIMPLE only: in GENERIC @0 can be a reference with reverse storage
+   order, whose VIEW_CONVERT_EXPR reads the bytes without swapping them,
+   so the MSB of the integer is not the sign bit of the value.  */
+(for bitop (bit_and bit_xor bit_ior)
+ (simplify
+  (view_convert (bitop:s (view_convert @0) INTEGER_CST@1))
+  (if (SCALAR_FLOAT_TYPE_P (type)
+       && !DECIMAL_FLOAT_TYPE_P (type)
+       && !HONOR_SNANS (type)
+       && types_match (type, TREE_TYPE (@0)))
+   (with { scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (type);
+          unsigned int prec = TYPE_PRECISION (TREE_TYPE (@1)); }
+    /* No padding bits (m68k XFmode has some), the sign bit where the
+       integer has its MSB, and words in the same order.  */
+    (if (known_eq (GET_MODE_BITSIZE (fmode), prec)
+        && known_eq (GET_MODE_PRECISION (fmode), prec)
+        && REAL_MODE_FORMAT (fmode)->signbit_rw == (int) prec - 1
+        && (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN
+            || known_le (GET_MODE_SIZE (fmode), UNITS_PER_WORD)))
+     (with { wide_int sign = wi::set_bit_in_zero (prec - 1, prec); }
+      (switch
+       (if (bitop == BIT_AND_EXPR && wi::to_wide (@1) == ~sign)
+       (abs @0))
+       (if (bitop == BIT_XOR_EXPR && wi::to_wide (@1) == sign)
+       (negate @0))
+       (if (bitop == BIT_IOR_EXPR && wi::to_wide (@1) == sign)
+       (negate (abs @0))))))))))
+
 #endif
 
 /* X * 1, X / 1 -> X.  */
diff --git a/gcc/testsuite/gcc.dg/torture/pr64897.c 
b/gcc/testsuite/gcc.dg/torture/pr64897.c
new file mode 100644
index 000000000000..4a6bb5555405
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr64897.c
@@ -0,0 +1,61 @@
+/* PR target/64897 */
+/* { dg-do run } */
+/* { dg-require-effective-target double64 } */
+/* { dg-skip-if "not IEEE" { vax-*-* pdp11-*-* } } */
+/* { dg-add-options ieee } */
+
+#define F(name, T, I, OP, C)                   \
+  __attribute__((noipa)) T name (T x)          \
+  {                                            \
+    I i;                                       \
+    __builtin_memcpy (&i, &x, sizeof i);       \
+    i OP C;                                    \
+    __builtin_memcpy (&x, &i, sizeof x);       \
+    return x;                                  \
+  }
+
+#define SF 0x80000000u
+#define SD 0x8000000000000000ull
+
+F (abs_f, float, __UINT32_TYPE__, &=, ~SF)
+F (abs_d, double, __UINT64_TYPE__, &=, ~SD)
+F (neg_f, float, __UINT32_TYPE__, ^=, SF)
+F (neg_d, double, __UINT64_TYPE__, ^=, SD)
+F (nabs_f, float, __UINT32_TYPE__, |=, SF)
+F (nabs_d, double, __UINT64_TYPE__, |=, SD)
+
+/* Zeros, normals, a subnormal, infinities and quiet NaNs with payloads
+   (no signaling NaNs: the ia32 ABI returns them quieted).  */
+static const __UINT32_TYPE__ vf[] = {
+  0, SF, 0x3fc00000u, 0xbfc00000u, 0x00000001u, 0x80000001u,
+  0x7f800000u, 0xff800000u, 0x7fc00123u, 0xffc00123u
+};
+static const __UINT64_TYPE__ vd[] = {
+  0, SD, 0x3ff8000000000000ull, 0xbff8000000000000ull, 1, SD | 1,
+  0x7ff0000000000000ull, 0xfff0000000000000ull, 0x7ff8000000000123ull,
+  0xfff8000000000123ull
+};
+
+#define CHECK(fn, T, I, v, expect)                             \
+  for (unsigned n = 0; n < sizeof v / sizeof v[0]; n++)        \
+    {                                                          \
+      T x, r;                                                  \
+      I b;                                                     \
+      __builtin_memcpy (&x, &v[n], sizeof x);                  \
+      r = fn (x);                                              \
+      __builtin_memcpy (&b, &r, sizeof b);                     \
+      if (b != (expect))                                       \
+       __builtin_abort ();                                     \
+    }
+
+int
+main ()
+{
+  CHECK (abs_f, float, __UINT32_TYPE__, vf, vf[n] & ~SF);
+  CHECK (neg_f, float, __UINT32_TYPE__, vf, vf[n] ^ SF);
+  CHECK (abs_d, double, __UINT64_TYPE__, vd, vd[n] & ~SD);
+  CHECK (neg_d, double, __UINT64_TYPE__, vd, vd[n] ^ SD);
+  CHECK (nabs_f, float, __UINT32_TYPE__, vf, vf[n] | SF);
+  CHECK (nabs_d, double, __UINT64_TYPE__, vd, vd[n] | SD);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-2.c
new file mode 100644
index 000000000000..a4c19f640821
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-2.c
@@ -0,0 +1,28 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target double64 } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Only clearing, flipping or setting the sign bit alone is abs, negation
+   or -abs.  */
+
+#define F(name, T, I, OP, C)                   \
+  T name (T x)                                 \
+  {                                            \
+    I i;                                       \
+    __builtin_memcpy (&i, &x, sizeof i);       \
+    i OP C;                                    \
+    __builtin_memcpy (&x, &i, sizeof x);       \
+    return x;                                  \
+  }
+
+F (and_f, float, __UINT32_TYPE__, &=, 0x7ffffffeu)
+F (xor_f, float, __UINT32_TYPE__, ^=, 0x40000000u)
+F (ior_f, float, __UINT32_TYPE__, |=, 0x40000000u)
+F (and_d, double, __UINT64_TYPE__, &=, 0x7fffffff7fffffffull)
+F (xor_d, double, __UINT64_TYPE__, ^=, 0x80000000ull)
+F (ior_d, double, __UINT64_TYPE__, |=, 0x80000000ull)
+
+/* { dg-final { scan-tree-dump-not "ABS_EXPR" "optimized" } } */
+/* { dg-final { scan-tree-dump-not " = -x_" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "\\.COPYSIGN" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897-3.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-3.c
new file mode 100644
index 000000000000..5b0a9193d75c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-3.c
@@ -0,0 +1,28 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target double64 } */
+/* { dg-options "-O2 -fsignaling-nans -fdump-tree-optimized" } */
+
+/* With signaling NaNs honored the integer operations stay: abs and
+   negation can be done in an FP unit that quiets them.  */
+
+#define F(name, T, I, OP, C)                   \
+  T name (T x)                                 \
+  {                                            \
+    I i;                                       \
+    __builtin_memcpy (&i, &x, sizeof i);       \
+    i OP C;                                    \
+    __builtin_memcpy (&x, &i, sizeof x);       \
+    return x;                                  \
+  }
+
+F (abs_f, float, __UINT32_TYPE__, &=, 0x7fffffffu)
+F (abs_d, double, __UINT64_TYPE__, &=, 0x7fffffffffffffffull)
+F (neg_f, float, __UINT32_TYPE__, ^=, 0x80000000u)
+F (neg_d, double, __UINT64_TYPE__, ^=, 0x8000000000000000ull)
+F (nabs_f, float, __UINT32_TYPE__, |=, 0x80000000u)
+F (nabs_d, double, __UINT64_TYPE__, |=, 0x8000000000000000ull)
+
+/* { dg-final { scan-tree-dump-not "ABS_EXPR" "optimized" } } */
+/* { dg-final { scan-tree-dump-not " = -x_" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "\\.COPYSIGN" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897-4.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-4.c
new file mode 100644
index 000000000000..db41be131850
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-4.c
@@ -0,0 +1,30 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target float16 } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options float16 } */
+
+/* The same for _Float16, whose sign bit is bit 15.  */
+
+_Float16
+abs_h (_Float16 x)
+{
+  __UINT16_TYPE__ i;
+  __builtin_memcpy (&i, &x, sizeof i);
+  i &= 0x7fff;
+  __builtin_memcpy (&x, &i, sizeof x);
+  return x;
+}
+
+_Float16
+neg_h (_Float16 x)
+{
+  __UINT16_TYPE__ i;
+  __builtin_memcpy (&i, &x, sizeof i);
+  i ^= 0x8000;
+  __builtin_memcpy (&x, &i, sizeof x);
+  return x;
+}
+
+/* { dg-final { scan-tree-dump-times "ABS_EXPR" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " = -x_" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897-5.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-5.c
new file mode 100644
index 000000000000..b1e35d4507fa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-5.c
@@ -0,0 +1,30 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target bfloat16 } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options bfloat16 } */
+
+/* The same for __bf16, whose sign bit is bit 15.  */
+
+__bf16
+abs_h (__bf16 x)
+{
+  __UINT16_TYPE__ i;
+  __builtin_memcpy (&i, &x, sizeof i);
+  i &= 0x7fff;
+  __builtin_memcpy (&x, &i, sizeof x);
+  return x;
+}
+
+__bf16
+neg_h (__bf16 x)
+{
+  __UINT16_TYPE__ i;
+  __builtin_memcpy (&i, &x, sizeof i);
+  i ^= 0x8000;
+  __builtin_memcpy (&x, &i, sizeof x);
+  return x;
+}
+
+/* { dg-final { scan-tree-dump-times "ABS_EXPR" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " = -x_" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897-6.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-6.c
new file mode 100644
index 000000000000..3a7a80904021
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-6.c
@@ -0,0 +1,18 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target dfp } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Decimal floats are left alone.  */
+
+_Decimal64
+abs_dd (_Decimal64 x)
+{
+  __UINT64_TYPE__ i;
+  __builtin_memcpy (&i, &x, sizeof i);
+  i &= 0x7fffffffffffffffull;
+  __builtin_memcpy (&x, &i, sizeof x);
+  return x;
+}
+
+/* { dg-final { scan-tree-dump-not "ABS_EXPR" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897-7.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-7.c
new file mode 100644
index 000000000000..598aaca5f269
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897-7.c
@@ -0,0 +1,24 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target double64 } */
+/* { dg-skip-if "not IEEE" { vax-*-* pdp11-*-* } } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Setting the sign bit is -abs (x), which is copysign (x, -1) on targets
+   that have copysign.  */
+
+#define F(name, T, I, OP, C)                   \
+  T name (T x)                                 \
+  {                                            \
+    I i;                                       \
+    __builtin_memcpy (&i, &x, sizeof i);       \
+    i OP C;                                    \
+    __builtin_memcpy (&x, &i, sizeof x);       \
+    return x;                                  \
+  }
+
+F (nabs_f, float, __UINT32_TYPE__, |=, 0x80000000u)
+F (nabs_d, double, __UINT64_TYPE__, |=, 0x8000000000000000ull)
+
+/* { dg-final { scan-tree-dump-times "ABS_EXPR|\\.COPYSIGN" 2 "optimized" } } 
*/
+/* { dg-final { scan-tree-dump-not "VIEW_CONVERT_EXPR" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64897.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr64897.c
new file mode 100644
index 000000000000..39f6cac8efac
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64897.c
@@ -0,0 +1,24 @@
+/* PR target/64897 */
+/* { dg-do compile } */
+/* { dg-require-effective-target double64 } */
+/* { dg-skip-if "not IEEE" { vax-*-* pdp11-*-* } } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#define F(name, T, I, OP, C)                   \
+  T name (T x)                                 \
+  {                                            \
+    I i;                                       \
+    __builtin_memcpy (&i, &x, sizeof i);       \
+    i OP C;                                    \
+    __builtin_memcpy (&x, &i, sizeof x);       \
+    return x;                                  \
+  }
+
+F (abs_f, float, __UINT32_TYPE__, &=, 0x7fffffffu)
+F (abs_d, double, __UINT64_TYPE__, &=, 0x7fffffffffffffffull)
+F (neg_f, float, __UINT32_TYPE__, ^=, 0x80000000u)
+F (neg_d, double, __UINT64_TYPE__, ^=, 0x8000000000000000ull)
+
+/* { dg-final { scan-tree-dump-times "ABS_EXPR" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " = -x_" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "VIEW_CONVERT_EXPR" "optimized" } } */

Reply via email to