GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gimple-fold

gimple-fold: Fix NaN handling in comparison inversion

Ensures NaN values are correctly handled when inverting comparisons in optimization passes.

This commit corrects an issue in gimple-fold where helper functions for AND/OR comparisons incorrectly handled NaNs when inverting comparison operations. This could lead to incorrect optimization results, particularly when combined with -fnon-call-exceptions. The fix involves passing NaN-handling information to the comparison inversion logic and giving up if the inverse doesn’t exist, along with new test cases.

In Details

The maybe_fold_and_comparisons, maybe_fold_or_comparisons, and_var_with_comparison, and or_var_with_comparison functions in gimple-fold.cc previously inverted comparisons without considering NaNs. This commit modifies them to pass the HONOR_NANS flag to invert_tree_comparison and to return NULL_TREE when the inverse does not exist. This fixes issues where incorrect folding occurred due to faulty NaN propagation, especially in conjunction with -fnon-call-exceptions and the ifcombine pass.

For Context
gimple-fold
A GCC optimization pass that simplifies and transforms GIMPLE intermediate representation code.
NaN
Not a Number. A special floating-point value used to represent undefined or unrepresentable results, such as the result of 0/0. Comparisons involving NaNs have specific rules.
ifcombine
A GCC optimization pass that combines conditional branches and boolean expressions.
GIMPLE
Generic Intermediate Representation for the Masses. GCC's primary three-address code intermediate representation used for optimization.
Filed Under: optimizationgimplebugfixfloating-point