https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128754
Bug ID: 128754
Summary: wrong code at -O1: -A CMP CST -> A SCMP -CST folds a
vector constant with an INT_MIN lane since r8-5166
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
int printf(const char *, ...);
typedef int V __attribute__((vector_size(16)));
V z;
int main(void) {
V s = { 0, -2147483647 - 1 };
V r = -z >= s;
printf("%d\n", r[1]);
}
$ gcc -O0 t.c && ./a.out
-1
$ gcc -O1 t.c && ./a.out
0
-O1, -O2, -O3 all fail. The C++ form (same code with <cstdio>) fails in the
same way. -fwrapv gives -1.
Expected -1: z is 0, so -z is 0 (no overflow), and 0 >= INT_MIN is true in lane
1.
ccp1 changes `_3 = s <= _2` (with _2 = -z) into
_3 = z.0_1 <= { 0, -2147483648(OVF), 0, 0 };
Cause: the match.pd rule "-A CMP CST -> A SCMP -CST" (the (cmp (negate @0)
CONSTANT_CLASS_P@1) simplify in match.pd) checks `tem && !TREE_OVERFLOW (tem)`
on the negated constant. For a VECTOR_CST the overflow is only on the element
(the dump shows -2147483648(OVF)), and TREE_OVERFLOW of the VECTOR_CST itself
is never set. The old build_vector ORed TREE_OVERFLOW of the elements into the
vector; r8-5166-g734914b6e23 ("New VECTOR_CST layout") removed that. 7.5 is
correct and 8.5 fails, which agrees with this. (Not bisected; inferred from the
source and the age test.)
This is the same class as PR110495 (fixed in r14-2282 for the associate pattern
only; comment 1 there notes that more patterns rely on TREE_OVERFLOW for
VECTOR_CST). The negate-compare pattern is one that is still affected.
A second trigger, trunk only: since r17-5677-ge88c76e0a7e (vect-generic: signed
division by a negative power of two), expand_vector_divmod expands z / -2 as
-(z / 2), so this also fails:
int printf(const char *, ...);
typedef int V __attribute__((vector_size(16)));
V z;
int main(void) {
V s = { 0, -2147483647 - 1 };
V r = z / -2 >= s;
printf("%d\n", r[1]);
}
$ gcc -O0 t.c && ./a.out
-1
$ gcc -O1 t.c && ./a.out
0
(gcc 16.2 gives -1 for this second form.)
-O0 -fsanitize=undefined,address is clean.
gcc -Wall -Wextra gives no warnings. -fwrapv gives the right output only
because the folded pattern is guarded by TYPE_OVERFLOW_UNDEFINED; the program
has no overflow (z is all zeros); -fno-strict-aliasing does not change the
output.
Possible fix: One possible fix is to reject the fold in the "-A CMP CST -> A
SCMP -CST" pattern (match.pd) when the constant is a VECTOR_CST and one of its
encoded elements has TREE_OVERFLOW set after the negation. A more general fix
would be to set TREE_OVERFLOW on a fixed-length VECTOR_CST again when an
element overflows (as build_vector did before r8-5166); PR110495 shows that
other patterns also rely on this flag.
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