]> git.ipfire.org Git - thirdparty/gcc.git/commit
Fix wrong optimization of complex boolean expression
authorEric Botcazou <ebotcazou@adacore.com>
Fri, 9 May 2025 15:45:27 +0000 (17:45 +0200)
committerEric Botcazou <ebotcazou@adacore.com>
Fri, 9 May 2025 15:49:33 +0000 (17:49 +0200)
commit3ae6b582d629e63e12d0ecfb7cbe44033778f88c
treebac24e9d1a5246c439ba6336cc64b141686c8779
parenta470433732e77ae29a717cf79049ceeea3cbe979
Fix wrong optimization of complex boolean expression

The VRP2 pass turns:

  # prephitmp_3 = PHI <0(4)>
  _1 = prephitmp_3 == 0;
  _5 = stretch_14(D) ^ 1;
  _39 = _1 & _5;
  _40 = _39 | last_20(D);

into

  _5 = stretch_14(D) ^ 1;
  _42 = ~stretch_14(D);
  _39 = _42;
  _40 = last_20(D) | _39;

using the following step:

Folding statement: _1 = prephitmp_3 == 0;
Queued stmt for removal.  Folds to: 1
Folding statement: _5 = stretch_14(D) ^ 1;
Not folded
Folding statement: _39 = _1 & _5;
gimple_simplified to _42 = ~stretch_14(D);
_39 = _42 & 1;
Folded into: _39 = _42;

Folding statement: _40 = _39 | last_20(D);
Folded into: _40 = last_20(D) | _39;

but stretch_14 is a 8-bit boolean so the two forms are not equivalent, that
is to say dropping the "& 1" is wrong.  It's another instance of the issue:
  https://gcc.gnu.org/pipermail/gcc-patches/2020-November/558537.html

Here it's the reverse case: the bitwise NOT (~) is treated as logical by the
machinery in range-op.cc but the bitwise AND (&) is *not* treated as logical
by that of vr-values.cc, leading to the same problematic outcome.

gcc/
* vr-values.cc (simplify_using_ranges::simplify) <BIT_AND_EXPR>:
Do not call simplify_bit_ops_using_ranges for boolean types whose
precision is not 1.

gcc/testsuite/
* gnat.dg/opt106.adb: New test.
* gnat.dg/opt106_pkg1.ads, gnat.dg/opt106_pkg1.adb: New helper.
* gnat.dg/opt106_pkg2.ads, gnat.dg/opt106_pkg2.adb: Likewise.
gcc/testsuite/gnat.dg/opt106.adb [new file with mode: 0644]
gcc/testsuite/gnat.dg/opt106_pkg1.adb [new file with mode: 0644]
gcc/testsuite/gnat.dg/opt106_pkg1.ads [new file with mode: 0644]
gcc/testsuite/gnat.dg/opt106_pkg2.adb [new file with mode: 0644]
gcc/testsuite/gnat.dg/opt106_pkg2.ads [new file with mode: 0644]
gcc/vr-values.cc