https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129278
Bug ID: 129278
Summary: [14/15/16/17 Regression] wrong code at -O1
-fgraphite-identity: extra iteration of a stride-2
loop
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
Tested with gcc version 17.0.0 20261203 (experimental), r17-7143-g592f605117e,
x86_64-pc-linux-gnu.
```c++
extern "C" int printf(const char *, ...);
int a[16], n = 8;
int main() {
for (int i = 0; i < 2; i++)
for (int j = 0; j < n; j++)
if (j % 2 == 0)
a[j] = 1;
printf("%d\n", a[8]);
}
```
$ g++ -O0 t.cc && ./a.out
0
$ g++ -O1 -fgraphite-identity t.cc && ./a.out
1
(-O2 -floop-nest-optimize and -O3 -fgraphite-identity: also 1.) Without
-fgraphite-identity / -floop-nest-optimize: 0.
Bisected to r7-153-ge6d62b46c8d ("Handle BIT_AND_EXPR" in SCEV); its parent is
fine. That commit only makes `(j & 1) == 0` affine, so Graphite now builds the
SCoP; isl emits `for (c1 = 0; c1 < P; c1 += 2)`, which is correct. The fault is
in the isl AST to GIMPLE code of r5-1782-ga78cfa7fc14:
translate_isl_ast_node_for takes ub = P - 1 and create_empty_loop_on_edge emits
the latch test `iv_before < ub`, which is equal to `iv_after <= ub` only for
stride 1. For stride 2 and P = 8: 6 < 7, so c1 = 8 also runs and writes a[8].
Graphite dump (`-fdump-tree-graphite-details`): isl AST `for (int c1 = 0; c1 <
P_18; c1 += 2)`; generated exit test `if (graphite_IV.7_14 < _16)` with `_16 =
_15 + -1` (P - 1) and step `+ 2`.