https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128747
Bug ID: 128747
Summary: wrong code at -O2: VN inverse converted +- lookup
gives a value whose range lets PRE drop a '& 7' since
r17-2788
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 g[4];
struct T { unsigned u; };
unsigned f0(unsigned *p, int n, unsigned k) {
if (n <= 0) return k;
return *p ^ f0(&g[3], n - 1, 0);
}
unsigned f1(int n, unsigned k, T t) {
return f0(&g[n & 3], n, k) >> (t.u & 7);
}
int main() {
unsigned h = 1;
for (int n = -1; n <= 7; n++)
h = h ^ f0(&g[1], n, 3) ^ f1(n, 85, {(unsigned)n - 2});
printf("%x\n", h);
}
$ g++ -O0 t.cpp && ./a.out
2
$ g++ -O2 t.cpp && ./a.out
1
-O2 and -O3 fail; -O1 is correct. -O3 -fno-tree-pre gives 2. The C form (gcc)
gives the same outputs.
Expected 2. For n = -1 and 0, t.u is 0xfffffffd and 0xfffffffe, so the shift
count is t.u & 7 = 5 and 6.
After f1 is inlined, main has (pre dump, -O2):
<bb 3>: if (n_66 <= 0) goto <bb 4>; else goto <bb 5>;
<bb 4>: n.0_48 = (unsigned int) n_66;
# RANGE [irange] unsigned int [4294967293, 4294967294]
_56 = n.0_48 + 4294967294;
<bb 7>: (n_66 >= 2)
# RANGE [irange] int [0, 5] MASK 0x7 VALUE 0x0
_26 = n_66 + -2;
...
# prephitmp_65 = PHI <_56(4), _127(22), 7(6)>
_71 = _70 >> prephitmp_65;
so on the edge from bb 4 the count is 0xfffffffd/0xfffffffe instead of 5/6.
With -fdump-tree-pre-details:
Value numbering stmt = _68 = n.0_120 + 4294967294;
_26 is available for _26
Inserting name _114 for expression (unsigned int) _26
Setting value number of _68 to _114 (changed)
...
Value numbering stmt = _56 = n.0_48 + 4294967294;
Setting value number of _56 to _114 (changed)
...
simplifying {bit_and_expr,_143,7} translated 29 -> 4 to {nop_expr,_26} to _56
Suspected cause: the PLUS_EXPR/MINUS_EXPR case of visit_nary_op
(tree-ssa-sccvn.cc), added by r17-2788-g7da4eb256f1 (PR126415). At _68 (bb 17,
where _26 is available) it finds (unsigned) n - 2 == (unsigned) _26 and calls
vn_nary_op_insert_stmt (stmt, result). That entry is not tied to the place
where _26 is available. In bb 4 (n <= 0, where _26 is not computed) _56 hits
the same entry by a plain lookup and gets the value _114 = (unsigned) _26. When
PRE phi-translates _143 & 7 into bb 4, vn_nary_simplify simplifies (unsigned)
_26 & 7 to (unsigned) _26 with the range of _26 ([0, 5], only true for n >= 2),
and finds _56 as the leader. So the & 7 is lost on that path.
First bad commit: r17-2788-g7da4eb256f1 (bisected, -O2).
-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 is to record the result of the new
PLUS_EXPR/MINUS_EXPR lookup in visit_nary_op (vn_nary_op_insert_stmt) only when
the narrow leader is available at the statement, so that the value (unsigned)
_26 is not given to statements in blocks where _26 is not computed. Another
option is to not use the range information of an SSA name during PRE
phi-translation (vn_nary_simplify) when its definition does not dominate the
predecessor. We are not sure which one the maintainers prefer.
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