On 7/21/2026 2:29 PM, Andrea Pinski wrote:
Since phiopt rejects trapping statements, this has to be done
seperately but it does reuse most of the infastructure to handle
this.
This is designed to handle foating point comparisons mergining
with another one.
An example is:
```
c = false;
if (a > b) c = a >= b;
```
This should merge into just `c = a > b;`.
Which we do already if it was written as `a > b && a >= b`.
In this case this is already handled by DOM/VRP/ranger.

The case I am more interesting in is:
```
bool f1(double a, double b)
{
   if (a == b)
     return 1;
   return a > b;
}
```
Which can/should optimize to `a >= b`.
This comes from `(a <=> b) >= 0` without spaceship_replacement and/or
with a patch that forwprops the phi values into the `>= 0`; replacing
the phi.

That is this is prerequisite to
https://inbox.sourceware.org/gcc-patches/[email protected]/.
and to remove spaceship_replacement in phiopt.

Bootstrapped and tested on x86_64-linux-gnu.

        PR tree-optimization/126138
gcc/ChangeLog:

        * tree-ssa-phiopt.cc (one_feeding_comparison_into_p): New function.
        (comparison_combine): New function.
        (pass_phiopt::execute): Call comparison_combine.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/fp-trapping-cmp-4.c: New test.
        * gcc.dg/tree-ssa/fp-trapping-cmp-5.c: New test.

Signed-off-by: Andrea Pinski <[email protected]>
OK with the comment fix noted below.

---
  .../gcc.dg/tree-ssa/fp-trapping-cmp-4.c       |  28 +++
  .../gcc.dg/tree-ssa/fp-trapping-cmp-5.c       |  30 +++
  gcc/tree-ssa-phiopt.cc                        | 205 ++++++++++++++++++
  3 files changed, 263 insertions(+)
  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-cmp-4.c
  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-cmp-5.c


diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index e12dc7a8b0c..0fe9ace0390 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -813,6 +813,70 @@ gimple_simplify_phiopt (bool early_p, tree type, gimple 
*comp_stmt,
    return NULL;
  }
+/* one_feeding_comparison_into_p returns true it has one comparison
+   statement and it sets STMT to that statement.  The comparison can
+   be trapping too.  */
"returns true *if bb* has one..."?


Jeff

Reply via email to