vect: Ensure both NITERSM1 and NITERS are INTEGER_CSTs or neither of them [PR113210]
commitb1d4e5b51375efd285378173375d634ad6ba8462
authorJakub Jelinek <jakub@redhat.com>
Tue, 9 Jan 2024 09:31:51 +0000 (9 10:31 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 9 Jan 2024 09:31:51 +0000 (9 10:31 +0100)
tree3448d9754e51e55f36534e647fb08db2cb669594
parent4d3fd59953bcf423dd0859a2c6b95b5260aed71d
vect: Ensure both NITERSM1 and NITERS are INTEGER_CSTs or neither of them [PR113210]

On the following testcase e.g. on riscv64 or aarch64 (latter with
-O3 -march=armv8-a+sve ) we ICE, because while NITERS is INTEGER_CST,
NITERSM1 is a complex expression like
(short unsigned int) (a.0_1 + 255) + 1 > 256 ? ~(short unsigned int) (a.0_1 + 255) : 0
where a.0_1 is unsigned char.  The condition is never true, so the above
is equivalent to just 0, but only when trying to fold the above with
PLUS_EXPR 1 we manage to simplify it (first
~(short unsigned int) (a.0_1 + 255)
to
-(short unsigned int) (a.0_1 + 255)
and then
(short unsigned int) (a.0_1 + 255) + 1 > 256 ? -(short unsigned int) (a.0_1 + 255) : 1
to
(short unsigned int) (a.0_1 + 255) >= 256 ? -(short unsigned int) (a.0_1 + 255) : 1
and only at this point we fold the condition to be false.

But the vectorizer seems to assume that if NITERS is known (i.e. suitable
INTEGER_CST) then NITERSM1 also is, so the following hack ensures that if
NITERS folds into INTEGER_CST NITERSM1 will be one as well.

2024-01-09  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/113210
* tree-vect-loop.cc (vect_get_loop_niters): If non-INTEGER_CST
value in *number_of_iterationsm1 PLUS_EXPR 1 is folded into
INTEGER_CST, recompute *number_of_iterationsm1 as the INTEGER_CST
minus 1.

* gcc.c-torture/compile/pr113210.c: New test.
gcc/testsuite/gcc.c-torture/compile/pr113210.c [new file with mode: 0644]
gcc/tree-vect-loop.cc