https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64897
--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Igor Shevlyakov <[email protected]>: 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]>
