On Tue, 29 Sep 2026, Richard Biener wrote:
> In combine_comparisons, under flag_trapping_math, we check whether the
> combined
> comparison traps under the same conditions as the input comparisons:
>
> bool trap = (compcode & COMPCODE_UNORD) == 0
> && (compcode != COMPCODE_EQ)
> && (compcode != COMPCODE_ORD);
>
> When compcode is COMPCODE_FALSE or COMPCODE_TRUE trap is computed as true,
> and combine_comparisons errorneously eliminates IEEE exceptions.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu.
>
> OK?
Jeff approved the followup to tree-ssa-ifcombine.cc and this one
is almost obvious, so I pushed both together now.
Richard.
> Thanks,
> Richard.
>
> PR tree-optimization/53805
> * fold-const.cc (combine_comparisons): Do not treat COMPCODE_FALSE
> or COMPCODE_TRUE as trapping.
>
> * gcc.dg/tree-ssa/fp-trapping-and-1.c: New testcase.
> ---
> gcc/fold-const.cc | 4 +++-
> gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-and-1.c | 11 +++++++++++
> 2 files changed, 14 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-and-1.c
>
> diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
> index 8c5e558647c..cfeafd5c1d1 100644
> --- a/gcc/fold-const.cc
> +++ b/gcc/fold-const.cc
> @@ -3085,7 +3085,9 @@ combine_comparisons (enum tree_code code, enum
> tree_code lcode,
> && (rcompcode != COMPCODE_ORD);
> bool trap = (compcode & COMPCODE_UNORD) == 0
> && (compcode != COMPCODE_EQ)
> - && (compcode != COMPCODE_ORD);
> + && (compcode != COMPCODE_ORD)
> + && (compcode != COMPCODE_FALSE)
> + && (compcode != COMPCODE_TRUE);
>
> /* In a short-circuited boolean expression the LHS might be
> such that the RHS, if evaluated, will never trap. For
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-and-1.c
> b/gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-and-1.c
> new file mode 100644
> index 00000000000..84946dc7cc1
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-and-1.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -ftrapping-math -fdump-tree-optimized" } */
> +
> +int
> +f (double a, double b)
> +{
> + return (a > b) & (a < b);
> +}
> +
> +/* { dg-final { scan-tree-dump-times " > " 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times " < " 1 "optimized" } } */
>
--
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)