https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128745
Bug ID: 128745
Summary: wrong code at -O2: VN drops a zero-extension mask
using the nonzero bits of a non-dominating leader from
the other arm; code hoisting makes it wrong
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: jeffchen006 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
#include <cstdio>
unsigned long long w = 1ULL << 32;
int main() {
for (int n = 0; n < 3; n++)
w = w > 4 ? w - (1 + (unsigned)w) : w + 1;
printf("%llx\n", w);
}
$ g++ -O0 t.cpp && ./a.out
ffffffff
$ g++ -O2 t.cpp && ./a.out
ffffffffffffffff
-O2, -O3, -Os fail; -O1 and -O2 -fno-code-hoisting give ffffffff. -fno-tree-pre
does not help (hoisting stays on); -fwrapv does not help (unsigned only).
Expected ffffffff: w = 2^32 -> 2^32 - 1 = 0xffffffff; then (unsigned)w + 1
wraps to 0, so w stays 0xffffffff.
VN (pre dump, -fdump-tree-pre-details):
Value numbering stmt = iftmp.0_11 = w.1_1 + 1; (bb 5, the w <= 4
arm)
...
Value numbering stmt = _3 = _2 + 1; (bb 4, the w > 4
arm; _2 = (unsigned int) w.1_1)
Value numbering stmt = _4 = (long long unsigned int) _3;
Setting value number of _4 to iftmp.0_11 (changed)
...
Inserting expression in block 3 for code hoisting: {plus_expr,w.1_1,1} (0008)
Replaced (long long unsigned int) _3 with _13 in all uses of _4 = (long long
unsigned int) _3;
After PRE the w > 4 arm computes w - (w + 1) = 0xffffffffffffffff in 64 bits:
_13 = w_lsm.9_5 + 1;
if (w_lsm.9_5 > 4) ... iftmp.0_12 = 18446744073709551615;
Suspected cause: visit_nary_op, CASE_CONVERT (tree-ssa-sccvn.cc). The zero
extension (u64)((unsigned)w + 1) is looked up as (w + 1) & 0xffffffff. The
lookup finds the wide operation iftmp.0_11 = w + 1, defined in the other arm
(bb 5), which does not dominate bb 4. vn_nary_build_or_lookup_1 lets the
simplifier see that name: rpo_vn_valueize returns a name that is its own value
number without checking that it is available here. The match.pd rule x & C -> x
then uses get_nonzero_bits of iftmp.0_11 (from its range [1, 5], true only in
the w <= 4 arm) and drops the mask. Code hoisting then puts w + 1 above the
branch and uses it in the w > 4 arm. The range (and nonzero bits) of an SSA
name should not be used where its definition is not available.
8.5 is correct; 10.5 fails. The GCC 8.5 fre dump makes the same wrong
equivalence, then prints "clearing range info of iftmp.0_11" and takes it back
on the next iteration. The RPO VN rewrite r9-2635-g78ea9abc201 (2018) removed
that clearing for non-dominating leaders, so it is the likely exposing change
(not bisected with release builds). The mask lookup itself is from r7-6906
(PR45397).
-O0 -fsanitize=undefined,address is clean.
gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.
Possible fix: One possible fix, which we tested locally (not bootstrapped), is
to ask the availability tracker for a leader before simplifying an SSA operand
in RPO VN (vn_nary_build_or_lookup_1 / rpo_vn_valueize), and to skip the
simplification when no leader is available, so that range and nonzero-bits
facts of a non-dominating name are not used. It fixes this test (C and C++, -O1
to -O3); pr98640.c, pr126415.c, pr124545-2.c and pr45397.c still pass.
gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release