https://gcc.gnu.org/g:b7e0201915d0cc64c615bde968b907383819e1eb
commit r17-6094-gb7e0201915d0cc64c615bde968b907383819e1eb Author: Kyrylo Tkachov <[email protected]> Date: Wed Oct 14 15:50:54 2026 +0200 gimple-fold: Honour NaNs when inverting comparisons The helpers behind maybe_fold_and_comparisons and maybe_fold_or_comparisons invert comparisons as if NaNs did not exist. With -fnon-call-exceptions a trapping comparison is a separate statement, and ifcombine turns _4 = a_2(D) < b_3(D); if (_4 != 0) goto <bb 5>; ... if (a_2(D) == b_3(D)) into _6 = a_2(D) > b_3(D); _7 = ~_6; if (_7 != 0) It combines !_4 && a_2(D) != b_3(D) as !(a_2(D) <= b_3(D)), and canonicalize_bool inverts a_2(D) <= b_3(D) to a_2(D) > b_3(D). The inverse is a_2(D) u> b_3(D), so for a NaN operand the combined test is true although both original conditions are false. and_var_with_comparison and or_var_with_comparison make the same mistake when they invert their comparison. Pass HONOR_NANS of the compared operands to invert_tree_comparison in these functions and give up when the inverse does not exist. Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu. gcc/ChangeLog: * gimple-fold.cc (canonicalize_bool): Honour NaNs when inverting a comparison. Return NULL_TREE if the inverse does not exist. (and_var_with_comparison): Likewise. (or_var_with_comparison): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/reassoc-54.c: New test. * gcc.dg/tree-ssa/ssa-ifcombine-nan-1.c: New test. * gcc.dg/tree-ssa/ssa-ifcombine-nan-2.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/gimple-fold.cc | 34 ++++++++++++----- gcc/testsuite/gcc.dg/tree-ssa/reassoc-54.c | 31 ++++++++++++++++ .../gcc.dg/tree-ssa/ssa-ifcombine-nan-1.c | 24 ++++++++++++ .../gcc.dg/tree-ssa/ssa-ifcombine-nan-2.c | 43 ++++++++++++++++++++++ 4 files changed, 122 insertions(+), 10 deletions(-) diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 71dd8f1da74f..1fdcc0c465fd 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -7243,10 +7243,16 @@ canonicalize_bool (tree expr, bool invert) return fold_build2 (EQ_EXPR, boolean_type_node, expr, build_int_cst (TREE_TYPE (expr), 0)); else if (COMPARISON_CLASS_P (expr)) - return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false), - boolean_type_node, - TREE_OPERAND (expr, 0), - TREE_OPERAND (expr, 1)); + { + tree_code code + = invert_tree_comparison (TREE_CODE (expr), + HONOR_NANS (TREE_OPERAND (expr, 0))); + if (code == ERROR_MARK) + return NULL_TREE; + return fold_build2 (code, boolean_type_node, + TREE_OPERAND (expr, 0), + TREE_OPERAND (expr, 1)); + } else return NULL_TREE; } @@ -7422,9 +7428,13 @@ and_var_with_comparison (tree type, enum tree_code code, tree var, !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b)) Then we only have to consider the simpler non-inverted cases. */ if (invert) - t = or_var_with_comparison_1 (type, truth_andor_compl (code), stmt, - invert_tree_comparison (code2, false), - op2a, op2b, outer_cond_bb); + { + code2 = invert_tree_comparison (code2, HONOR_NANS (op2a)); + if (code2 == ERROR_MARK) + return NULL_TREE; + t = or_var_with_comparison_1 (type, truth_andor_compl (code), stmt, + code2, op2a, op2b, outer_cond_bb); + } else t = and_var_with_comparison_1 (type, code, stmt, code2, op2a, op2b, outer_cond_bb); @@ -9403,9 +9413,13 @@ or_var_with_comparison (tree type, enum tree_code code, tree var, !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b)) Then we only have to consider the simpler non-inverted cases. */ if (invert) - t = and_var_with_comparison_1 (type, truth_andor_compl (code), stmt, - invert_tree_comparison (code2, false), - op2a, op2b, outer_cond_bb); + { + code2 = invert_tree_comparison (code2, HONOR_NANS (op2a)); + if (code2 == ERROR_MARK) + return NULL_TREE; + t = and_var_with_comparison_1 (type, truth_andor_compl (code), stmt, + code2, op2a, op2b, outer_cond_bb); + } else t = or_var_with_comparison_1 (type, code, stmt, code2, op2a, op2b, outer_cond_bb); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-54.c b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-54.c new file mode 100644 index 000000000000..9d1a1de19a1f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-54.c @@ -0,0 +1,31 @@ +/* !(a unord b) | a < b is a ord b, not true. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fgimple -fno-trapping-math" } */ +/* { dg-add-options ieee } */ + +int __GIMPLE (ssa,startwith("reassoc1")) __attribute__((noipa)) +f (double a, double b) +{ + _Bool M; + _Bool N; + _Bool L; + _Bool R; + int r; + + __BB(2): + M_1 = a_2(D) __UNORDERED b_3(D); + N_4 = M_1 == _Literal (_Bool) 0; + L_5 = a_2(D) < b_3(D); + R_6 = N_4 | L_5; + r_7 = (int) R_6; + return r_7; +} + +int +main (void) +{ + if (f (__builtin_nan (""), 0.0) != 0 || f (0.0, 1.0) != 1 + || f (1.0, 0.0) != 1) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-1.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-1.c new file mode 100644 index 000000000000..e4216ad70292 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-1.c @@ -0,0 +1,24 @@ +/* With non-call exceptions the first comparison is a separate statement. + Combining it with the second one must not invert a <= b to a > b. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fnon-call-exceptions" } */ +/* { dg-add-options ieee } */ +/* { dg-require-effective-target exceptions } */ + +__attribute__ ((noipa)) int +f (double a, double b) +{ + if (a < b) + return 2; + if (a == b) + return 2; + return 0; +} + +int +main (void) +{ + if (f (0.0, __builtin_nan ("")) != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-2.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-2.c new file mode 100644 index 000000000000..f93887d12bfb --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-2.c @@ -0,0 +1,43 @@ +/* !(a < b) && a <= b is a == b, not a u== b. */ +/* { dg-do run } */ +/* { dg-options "-O1 -fgimple -fno-trapping-math" } */ +/* { dg-add-options ieee } */ + +int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa)) +f (double a, double b) +{ + _Bool M; + int r; + + __BB(2): + M_1 = a_2(D) < b_3(D); + if (M_1 != _Literal (_Bool) 0) + goto __BB5; + else + goto __BB3; + + __BB(3): + if (a_2(D) <= b_3(D)) + goto __BB4; + else + goto __BB5; + + __BB(4): + goto __BB6; + + __BB(5): + goto __BB6; + + __BB(6): + r_4 = __PHI (__BB4: 1, __BB5: 0); + return r_4; +} + +int +main (void) +{ + if (f (__builtin_nan (""), 0.0) != 0 || f (1.0, 1.0) != 1 + || f (0.0, 1.0) != 0) + __builtin_abort (); + return 0; +}
