PR middle-end/88173: More constant folding of NaN comparisons.
authorRoger Sayle <roger@nextmovesoftware.com>
Sun, 19 Sep 2021 08:07:01 +0000 (09:07 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Sun, 19 Sep 2021 08:07:01 +0000 (09:07 +0100)
commite9e46864cd0695d2dcec5c513f249b1e1ab0056f
tree0e6378f76d71bc4e4ca042a7f21d85f7df2f0b5d
parent69337e7495d09a1fc30442c9c3ade6318f1cf089
PR middle-end/88173: More constant folding of NaN comparisons.

This patch tackles PR middle-end/88173 where the order of operands in
a comparison affects constant folding.  As diagnosed by Jason Merrill,
"match.pd handles these comparisons very differently".  The history is
that the middle end, typically canonicalizes comparisons to place
constants on the right, but when a comparison contains two constants
we need to check/transform both constants, i.e. on both the left and the
right.  Hence the added lines below duplicate for @0 the same transform
applied a few lines above for @1.

Whilst preparing the testcase, I noticed that this transformation is
incorrectly disabled with -fsignaling-nans even when both operands are
known not be be signaling NaNs, so I've corrected that and added a
second test case.  Unfortunately, c-c++-common/pr57371-4.c then starts
failing, as it doesn't distinguish QNaNs (which are quiet) from SNaNs
(which signal), so this patch includes a minor tweak to the expected
behaviour for QNaNs in that existing test.

2021-09-19  Roger Sayle <roger@nextmovesoftware.com>

gcc/ChangeLog
PR middle-end/88173
* match.pd (cmp @0 REAL_CST@1): When @0 is also REAL_CST, apply
the same transformations as to @1.  For comparisons against NaN,
don't check HONOR_SNANS but confirm that neither operand is a
signaling NaN.

gcc/testsuite/ChangeLog
PR middle-end/88173
* c-c++-common/pr57371-4.c: Tweak/correct test case for QNaNs.
* g++.dg/pr88173-1.C: New test case.
* g++.dg/pr88173-2.C: New test case.
gcc/match.pd
gcc/testsuite/c-c++-common/pr57371-4.c
gcc/testsuite/g++.dg/pr88173-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/pr88173-2.C [new file with mode: 0644]