https://gcc.gnu.org/g:79cc525f7f64ca2e1de25c6a9d225c4c6950fef8
commit r17-6672-g79cc525f7f64ca2e1de25c6a9d225c4c6950fef8 Author: Igor Shevlyakov <[email protected]> Date: Sun Nov 22 14:50:49 2026 -0800 i386: Change the sign of a float in memory in place Code such as *p = fabs (*p) or x = -x on a float or double in memory is expanded as a load into an SSE register, an andps/xorps with a sign mask from the constant pool and a store, or with x87 math as fld, fabs/fchs and fstp. The value does not need to go through the FP unit: add a pattern for (set (mem) (abs/neg (mem))), which combine forms from the load, the operation and the store, and split it after reload with ix86_split_fp_absneg_operator, the splitter of the general register alternative of the abs/neg patterns. It changes the sign bit in memory with btr/btc, or with and/xor on the word that holds the sign bit: btrq $63, (%rdi) instead of movsd (%rdi), %xmm0 andpd .LC0(%rip), %xmm0 movsd %xmm0, (%rdi) Tunings without use_bt get movabs and andq/xorq/orq on memory instead, from the existing peephole2s for btr/btc/bts. A second pattern does the same for *p = -fabs (*p), with bts or or. With SSE math that is expanded from .COPYSIGN (x, -1.0) as an IOR with the mask, which combine cannot turn into a store to memory; ix86_expand_copysign now emits a negative constant sign as the existing *nabs<mode>2_1 form instead, as the abs and neg expanders do for theirs. That form splits into the same IOR. The patterns cover SFmode, and DFmode only with 64-bit integers: without them the operation would change the upper word of the double alone, which stalls a following 8-byte load of it and needs an offsettable address. A volatile value is changed in place too: the access has the width of the value, as for other read-modify-write operations. When only the read or only the write is volatile the patterns do not match, since a single access cannot be both; the split rebuilds the operation from the destination and would lose the volatile read. With x87 math, fabs, negation and -fabs of a float through memory used to load the value with fld, which quiets a signaling NaN. Now only the sign bit changes and a signaling NaN stays signaling, as with SSE math. fabsneg-2.c checked that three such updates share the load of the sign mask; they no longer need one, so it now reads other variables than it writes. builtin-fabs-2.c (PR99930) gets the same instructions, but the mask now lands in another register, so it no longer checks the register numbers. Assisted-by: Claude Opus 5.5 gcc/ChangeLog: * config/i386/i386-expand.cc (ix86_expand_copysign): Emit a negative constant sign as NEG of ABS with a USE of the mask. * config/i386/i386.md (*<code><mode>2_mem): New define_insn_and_split. (*nabs<mode>2_mem): Likewise. gcc/testsuite/ChangeLog: * gcc.target/i386/builtin-fabs-2.c: Do not check register numbers. * gcc.target/i386/fabsneg-2.c: Read other variables than written. * gcc.target/i386/fabsneg-3.c: New test. * gcc.target/i386/fabsneg-4.c: New test. * gcc.target/i386/fabsneg-5.c: New test. Signed-off-by: Igor Shevlyakov <[email protected]> Diff: --- gcc/config/i386/i386-expand.cc | 15 ++++++++ gcc/config/i386/i386.md | 49 +++++++++++++++++++++++++ gcc/testsuite/gcc.target/i386/builtin-fabs-2.c | 6 +-- gcc/testsuite/gcc.target/i386/fabsneg-2.c | 13 ++++--- gcc/testsuite/gcc.target/i386/fabsneg-3.c | 23 ++++++++++++ gcc/testsuite/gcc.target/i386/fabsneg-4.c | 51 ++++++++++++++++++++++++++ gcc/testsuite/gcc.target/i386/fabsneg-5.c | 27 ++++++++++++++ 7 files changed, 175 insertions(+), 9 deletions(-) diff --git a/gcc/config/i386/i386-expand.cc b/gcc/config/i386/i386-expand.cc index 2c4da41a2431..8653e0434d4e 100644 --- a/gcc/config/i386/i386-expand.cc +++ b/gcc/config/i386/i386-expand.cc @@ -2368,6 +2368,21 @@ ix86_expand_copysign (rtx operands[]) return; } + /* copysign (a, negative) is -fabs (a). */ + if ((mode == SFmode || mode == DFmode) + && CONST_DOUBLE_P (operands[2]) + && real_isneg (CONST_DOUBLE_REAL_VALUE (operands[2]))) + { + rtx src = gen_rtx_NEG (mode, + gen_rtx_ABS (mode, + force_reg (mode, operands[1]))); + mask = ix86_build_signbit_mask (vmode, false, false); + rtvec par = gen_rtvec (2, gen_rtx_SET (operands[0], src), + gen_rtx_USE (VOIDmode, mask)); + emit_insn (gen_rtx_PARALLEL (VOIDmode, par)); + return; + } + dest = operands[0]; vdest = lowpart_subreg (vmode, dest, mode); if (vdest == NULL_RTX) diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 35dfbf1c9fc7..9da466d21ef1 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -16089,6 +16089,55 @@ [(const_int 0)] "ix86_split_fp_absneg_operator (<CODE>, <MODE>mode, operands); DONE;") +;; ABS or NEG of a value in memory stored back in place. +(define_insn_and_split "*<code><mode>2_mem" + [(set (match_operand:MODEF 0 "memory_operand" "=m") + (absneg:MODEF (match_operand:MODEF 1 "memory_operand" "0"))) + (clobber (reg:CC FLAGS_REG))] + "(TARGET_80387 || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)) + && <MODE_SIZE> <= UNITS_PER_WORD + && rtx_equal_p (operands[0], operands[1]) + /* rtx_equal_p does not compare MEM_VOLATILE_P. */ + && MEM_VOLATILE_P (operands[0]) == MEM_VOLATILE_P (operands[1])" + "#" + "&& reload_completed" + [(const_int 0)] +{ + ix86_split_fp_absneg_operator (<CODE>, <MODE>mode, operands); + DONE; +}) + +;; Likewise for -fabs: set the sign bit in memory. +(define_insn_and_split "*nabs<mode>2_mem" + [(set (match_operand:MODEF 0 "memory_operand" "=m") + (neg:MODEF (abs:MODEF (match_operand:MODEF 1 "memory_operand" "0")))) + (clobber (reg:CC FLAGS_REG))] + "(TARGET_80387 || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)) + && <MODE_SIZE> <= UNITS_PER_WORD + && rtx_equal_p (operands[0], operands[1]) + /* rtx_equal_p does not compare MEM_VOLATILE_P. */ + && MEM_VOLATILE_P (operands[0]) == MEM_VOLATILE_P (operands[1])" + "#" + "&& reload_completed" + [(parallel + [(set (match_dup 1) (match_dup 2)) + (clobber (reg:CC FLAGS_REG))])] +{ + if (<MODE>mode == SFmode) + { + operands[1] = gen_lowpart (SImode, operands[0]); + operands[2] = gen_rtx_IOR (SImode, operands[1], + gen_int_mode (0x80000000, SImode)); + } + else + { + operands[1] = gen_rtx_ZERO_EXTRACT (DImode, + gen_lowpart (DImode, operands[0]), + const1_rtx, GEN_INT (63)); + operands[2] = const1_rtx; + } +}) + (define_insn_and_split "*nabs<mode>2_1" [(set (match_operand:MODEF 0 "register_operand" "=x,x,Yv") (neg:MODEF diff --git a/gcc/testsuite/gcc.target/i386/builtin-fabs-2.c b/gcc/testsuite/gcc.target/i386/builtin-fabs-2.c index dba3f8f4b532..e4f52d0f7910 100644 --- a/gcc/testsuite/gcc.target/i386/builtin-fabs-2.c +++ b/gcc/testsuite/gcc.target/i386/builtin-fabs-2.c @@ -8,9 +8,9 @@ **foo: **... ** ja .L[0-9]+ -** movss 4\(%(e|r)di\), %xmm1 -** orps %xmm1, %xmm0 -** comiss %xmm0, %xmm2 +** movss 4\(%(e|r)di\), %xmm[0-9]+ +** orps %xmm[0-9]+, %xmm[0-9]+ +** comiss %xmm[0-9]+, %xmm[0-9]+ ** seta %al ** ret **... diff --git a/gcc/testsuite/gcc.target/i386/fabsneg-2.c b/gcc/testsuite/gcc.target/i386/fabsneg-2.c index 95906ddd71e9..a8888b7ce41e 100644 --- a/gcc/testsuite/gcc.target/i386/fabsneg-2.c +++ b/gcc/testsuite/gcc.target/i386/fabsneg-2.c @@ -1,22 +1,23 @@ /* { dg-do compile } */ /* { dg-options "-O2 -mfpmath=sse -march=skylake" } */ +float a, b, c; float x; float y; float z; void foo() { - x = -x; - y = -y; - z = -z; + x = -a; + y = -b; + z = -c; } void bar() { - x = __builtin_fabsf(x); - y = __builtin_fabsf(y); - z = __builtin_fabsf(z); + x = __builtin_fabsf(a); + y = __builtin_fabsf(b); + z = __builtin_fabsf(c); } /* { dg-final { scan-assembler-times "\[lL\]C0\[,(\]" 1 } } */ diff --git a/gcc/testsuite/gcc.target/i386/fabsneg-3.c b/gcc/testsuite/gcc.target/i386/fabsneg-3.c new file mode 100644 index 000000000000..2f7bd1b71ff5 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/fabsneg-3.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune=generic" } */ + +/* fabs, negation and -fabs of a value in memory, stored back in place, + change the sign bit in memory without loading the value. For double only + with 64-bit integers: otherwise the upper word would be changed alone, + which stalls a following load of the whole value. */ + +void abs_f (float *p) { *p = __builtin_fabsf (*p); } +void neg_f (float *p) { *p = -*p; } +void abs_d (double *p) { *p = __builtin_fabs (*p); } +void neg_d (double *p) { *p = -*p; } +void nabs_f (float *p) { *p = -__builtin_fabsf (*p); } +void nabs_d (double *p) { *p = -__builtin_fabs (*p); } + +/* { dg-final { scan-assembler-times "andl\t\\\$2147483647, \\(" 1 } } */ +/* { dg-final { scan-assembler-times "xorl\t\\\$-2147483648, \\(" 1 } } */ +/* { dg-final { scan-assembler-times "\torl\t\\\$-2147483648, \\(" 1 } } */ +/* { dg-final { scan-assembler-times "btrq\t\\\$63, \\(" 1 { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-times "btcq\t\\\$63, \\(" 1 { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-times "btsq\t\\\$63, \\(" 1 { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-not "movs\[sd\]|andp\[sd\]|xorp\[sd\]|orp\[sd\]" { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-not "\tf(ld|st|abs|chs)" { target { ! ia32 } } } } */ diff --git a/gcc/testsuite/gcc.target/i386/fabsneg-4.c b/gcc/testsuite/gcc.target/i386/fabsneg-4.c new file mode 100644 index 000000000000..6180a1284231 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/fabsneg-4.c @@ -0,0 +1,51 @@ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +/* fabs, negation and -fabs of a value in memory, stored back in place, + change only the sign bit. */ + +#define SF 0x80000000u +#define SD 0x8000000000000000ull + +__attribute__ ((noipa)) void abs_d (double *p) { *p = __builtin_fabs (*p); } +__attribute__ ((noipa)) void neg_d (double *p) { *p = -*p; } +__attribute__ ((noipa)) void abs_f (float *p) { *p = __builtin_fabsf (*p); } +__attribute__ ((noipa)) void neg_f (float *p) { *p = -*p; } +__attribute__ ((noipa)) void nabs_d (double *p) { *p = -__builtin_fabs (*p); } +__attribute__ ((noipa)) void nabs_f (float *p) { *p = -__builtin_fabsf (*p); } + +/* Zeros, normals, a subnormal, infinities and quiet NaNs with payloads + (no signaling NaNs: an x87 load would quiet them). */ +static const unsigned int vf[] = { + 0, SF, 0x3fc00000u, 0xbfc00000u, 0x00000001u, 0x80000001u, + 0x7f800000u, 0xff800000u, 0x7fc00123u, 0xffc00123u +}; +static const unsigned long long 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; \ + I b; \ + __builtin_memcpy (&x, &v[n], sizeof x); \ + fn (&x); \ + __builtin_memcpy (&b, &x, sizeof b); \ + if (b != (expect)) \ + __builtin_abort (); \ + } + +int +main () +{ + CHECK (abs_f, float, unsigned int, vf, vf[n] & ~SF); + CHECK (neg_f, float, unsigned int, vf, vf[n] ^ SF); + CHECK (abs_d, double, unsigned long long, vd, vd[n] & ~SD); + CHECK (neg_d, double, unsigned long long, vd, vd[n] ^ SD); + CHECK (nabs_f, float, unsigned int, vf, vf[n] | SF); + CHECK (nabs_d, double, unsigned long long, vd, vd[n] | SD); + return 0; +} diff --git a/gcc/testsuite/gcc.target/i386/fabsneg-5.c b/gcc/testsuite/gcc.target/i386/fabsneg-5.c new file mode 100644 index 000000000000..4dd0a40962c7 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/fabsneg-5.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune=generic" } */ + +/* A volatile value in memory is changed in place too: the access has the + width of the value, as for other read-modify-write operations. */ + +void abs_f (volatile float *p) { *p = __builtin_fabsf (*p); } +void neg_f (volatile float *p) { *p = -*p; } +void nabs_f (volatile float *p) { *p = -__builtin_fabsf (*p); } +void abs_d (volatile double *p) { *p = __builtin_fabs (*p); } +void neg_d (volatile double *p) { *p = -*p; } +void nabs_d (volatile double *p) { *p = -__builtin_fabs (*p); } + +/* But not when only the read or only the write is volatile: the single + access could not be both. */ + +void neg_rf (float *p) { *p = -*(volatile float *) p; } +void neg_wf (float *p) { *(volatile float *) p = -*p; } +void neg_rd (double *p) { *p = -*(volatile double *) p; } +void neg_wd (double *p) { *(volatile double *) p = -*p; } + +/* { dg-final { scan-assembler-times "andl\t\\\$2147483647, \\(" 1 } } */ +/* { dg-final { scan-assembler-times "xorl\t\\\$-2147483648, \\(" 1 } } */ +/* { dg-final { scan-assembler-times "\torl\t\\\$-2147483648, \\(" 1 } } */ +/* { dg-final { scan-assembler-times "btrq\t\\\$63, \\(" 1 { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-times "btcq\t\\\$63, \\(" 1 { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-times "btsq\t\\\$63, \\(" 1 { target { ! ia32 } } } } */
