https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129189
--- Comment #4 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:733b36f2cf2a1fc94e34fd38ffe3576d4598e017 commit r17-7031-g733b36f2cf2a1fc94e34fd38ffe3576d4598e017 Author: Igor Shevlyakov <[email protected]> Date: Tue Dec 1 11:48:28 2026 -0800 i386: Tie the sign mask of an SSE ABS or NEG to the operation [PR129189] With SSE math, ABS, NEG and -fabs are patterns that carry their mask in a USE: ~signmask for ABS (an AND), the sign mask for NEG (an XOR) and for -fabs (an IOR). The splitters trust the mask they are given. When combine merges a NEG into the -fabs before it, it simplifies -(-fabs (x)) into fabs (x) but keeps the USE of the outer NEG, and the result matches the ABS pattern, which then ANDs with the sign mask and returns +-0 instead of |x|. This was latent: the tree passes fold -(-fabs (x)), and before r17-6672 nothing at RTL produced -fabs from anything but a combined ABS and NEG. r17-6672 expands copysign (x, -1.0) as -fabs (x), so -copysign (x, -1.0) now miscompiles at -O1 and above. The same RTL from the GIMPLE FE miscompiles with GCC 15 and 16 too, for SF, DF, TF and HF. Wrap the mask in the USE in an UNSPEC_ABS_MASK or UNSPEC_NEG_MASK, so that an ABS with the mask of a NEG no longer matches the ABS patterns. Instead of letting combine fail on it, add a pre-reload split for that form which expands it again as an ABS with its own mask, so that -(-fabs (x)) becomes a single AND, where GCC 16 emits an OR and an XOR for -copysign (x, -1.0). Assisted-by: Claude Opus 5.5 gcc/ChangeLog: PR target/129189 * config/i386/i386-expand.cc (ix86_absneg_mask_unspec): New. (ix86_expand_fp_absneg_operator): Use it. (ix86_expand_copysign): Likewise. * config/i386/i386.cc (ix86_insn_cost): Look through UNSPEC_ABS_MASK and UNSPEC_NEG_MASK in a USE. * config/i386/i386.md (UNSPEC_ABS_MASK, UNSPEC_NEG_MASK): New. (absneg_mask): New code attribute. (*<code>tf2_1, *<code>hf2_1, *<code><mode>2_1): Wrap the mask operand of the USE in <absneg_mask>. (*nabstf2_1, *nabs<mode>2_1): Wrap the mask operand of the USE in UNSPEC_NEG_MASK. (absneg splitter to SSE logic): Likewise with <absneg_mask>. (*abstf2_neg_mask, *abshf2_neg_mask, *abs<mode>2_neg_mask): New define_insn_and_split. * config/i386/mmx.md (*mmx_<code>v2sf2, *mmx_<code><mode>): Wrap the mask operand of the USE in <absneg_mask>. (*mmx_nabsv2sf2, *mmx_nabs<mode>2): Likewise with UNSPEC_NEG_MASK. (*mmx_absv2sf2_neg_mask, *mmx_abs<mode>2_neg_mask): New define_insn_and_split. * config/i386/sse.md (*<code><mode>2): Wrap the mask operand of the USE in <absneg_mask>. (*nabs<mode>2): Likewise with UNSPEC_NEG_MASK. (*abs<mode>2_neg_mask): New define_insn_and_split. gcc/testsuite/ChangeLog: PR target/129189 * gcc.dg/rtl/x86_64/pr129189.c: New test. * gcc.dg/torture/pr129189.c: Likewise. * gcc.target/i386/pr129189-1.c: Likewise. * gcc.target/i386/pr129189-2.c: Likewise. * gcc.target/i386/pr129189-3.c: Likewise. * gcc.target/i386/pr129189-4.c: Likewise. Signed-off-by: Igor Shevlyakov <[email protected]>
