https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103724
Jakub Jelinek <jakub at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Seems the reason we warn is that we first unswitch the loop because of the (useless) b != 0 test in the loop, and so end up with: if (b == 0) for (int c = 0; c < 11; c++) d = a[c][c + 1]; else for (int c = 0; c < 11; c++) { d = a[c >= b ? c - b : c][c + 1 >= b ? c + 1 - b : c + 1]; if (b && c) d = c; } (the c >= b and c + 1 >= b comparisons in the first loop can be simplified because of the [0, 11] range for c), and on the first loop it indeed would invoke UB in 7th iteration. So the warning although it isn't clear from it (and the compiler doesn't know either) is about that if b is 0, which is something the user loop tests for, then it invokes UB, otherwise it isn't known whether it does or doesn't. This is a general problem with middle-end warnings, after unswitching, jump threading and similar optimizations it is unclear what code has been specialized on something that really ever happens in the code at runtime and what is dead.