[Bug target/119120] Unnecessary fldl/stpl pair when assigning real or imaginary part of a complex variable

2025-03-14 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119120

--- Comment #5 from GCC Commits  ---
The master branch has been updated by Jakub Jelinek :

https://gcc.gnu.org/g:d0d7c6632c2913c0243f048a15ff64aec6b6232e

commit r15-8059-gd0d7c6632c2913c0243f048a15ff64aec6b6232e
Author: Jakub Jelinek 
Date:   Fri Mar 14 15:31:47 2025 +0100

c, c++: Set DECL_NOT_GIMPLE_REG_P on *PART_EXPR operand on lhs of
MODIFY_EXPR [PR119120]

The PR119190 patch I've posted regresses the PR119120 testcase (not adding
to testsuite, as it is fairly hard to scan for that problem).
The issue is that for the partial setting of _Complex floating vars
through __real__ on it first and __imag__ later (or vice versa) and since
we forced all complex vars into SSA form we often have undefined (D)
arguments of those COMPLEX_EXPRs.  When we don't DCE them (for -O0 debug
info reasons), their expansion will copy both the real and imag parts
using the floating mode and on some targets like 387 that copying alone can
unfortunately trigger exceptions on sNaNs or other problematic bit patterns
and for uninitialized memory it can be triggered randomly based on whatever
is on the stack before.

The following patch sets DECL_NOT_GIMPLE_REG_P flag in the FEs during
genericization.
I think Fortran doesn't have a way to modify just real or just complex
part separately.

The patch is for code like
  _ComplexT __t;
  __real__ __t = __z.real();
  __imag__ __t = __z.imag();
  _M_value *= __t;
  return *this;
at -O0 which used to appear widely even in libstdc++ before GCC 9
and happens in real-world code.  At -O0 for debug info reasons (see
PR119190) we don't want to aggressively DCE statements and when we
since r0-100845 try to rewrite vars with COMPLEX_TYPE into SSA form
aggressively, the above results in copying of uninitialized data
when expanding COMPLEX_EXPRs added so that the vars can be in SSA form.
The patch detects during genericization the partial initialization and
doesn't rewrite such vars to SSA at -O0.  This has to be done before
gimplification starts, otherwise e.g. the attached testcase ICEs.

2025-03-14  Jakub Jelinek  

PR target/119120
* c-gimplify.cc (c_genericize_control_r): Set DECL_NOT_GIMPLE_REG_P
on {REAL,IMAG}PART_EXPR is_gimple_reg operand at -O0 if it is lhs
of a MODIFY_EXPR.

* cp-gimplify.cc (cp_genericize_r): Set DECL_NOT_GIMPLE_REG_P
on {REAL,IMAG}PART_EXPR is_gimple_reg operand at -O0 if it is lhs
of a MODIFY_EXPR.

* c-c++-common/pr119120.c: New test.

[Bug target/119120] Unnecessary fldl/stpl pair when assigning real or imaginary part of a complex variable

2025-03-10 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119120

--- Comment #4 from Jakub Jelinek  ---
I think this regressed with
r0-100845-gbd2e63a1c4d4159f576c31bee9e4090919462aa5.

[Bug target/119120] Unnecessary fldl/stpl pair when assigning real or imaginary part of a complex variable

2025-03-10 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119120

--- Comment #3 from Andrew Pinski  ---
(In reply to Zoltan Vajda from comment #0)
> This is not an academic problem. The implementation of operator /= of
> std::complex has this behavior until GCC 8.5 (inclusive).

The std::complex's operator /= issue was solved with r9-4332-ge987fb1ebecc74 .

Specifically with this part:
   template
-complex&
+_GLIBCXX20_CONSTEXPR complex&
 operator/=(const complex<_Tp>& __z)
{
- _ComplexT __t;
- __real__ __t = __z.real();
- __imag__ __t = __z.imag();
+ const _ComplexT __t = __z.__rep();
  _M_value /= __t;
  return *this;
}

[Bug target/119120] Unnecessary fldl/stpl pair when assigning real or imaginary part of a complex variable

2025-03-04 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119120

Andrew Pinski  changed:

   What|Removed |Added

 Resolution|--- |FIXED
 Status|UNCONFIRMED |RESOLVED
   Target Milestone|--- |15.0

--- Comment #2 from Andrew Pinski  ---
Fixed on the trunk by r15-3128-gde1923f9f4d534 :

Removing dead stmt:__t_9 = COMPLEX_EXPR <__t$real_1, __t$imag_2>;
Removing dead stmt:__t$imag_2 = __t$imag_12(D);
Removing dead stmt:__t_11 = COMPLEX_EXPR <__t$real_4, __t$imag_3>;
Removing dead stmt:__t$real_4 = __t$real_1;

[Bug target/119120] Unnecessary fldl/stpl pair when assigning real or imaginary part of a complex variable

2025-03-04 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119120

Andrew Pinski  changed:

   What|Removed |Added

  Known to work||15.0

--- Comment #1 from Andrew Pinski  ---
Looks to be fixed on the trunk.