I had been receiving warnings about loops in my C code when -Wunsafe-loop-optimizations is enabled:
warning: cannot optimize possibly infinite loops [-Wunsafe-loop-optimizations]
The warning seems to be very finnicky, and is difficult to reproduce in a minimal example (beyond trivally-obvious infinite loops). Moving to a new compiler version also changed the warning behavior. This led me to wondering about how the warning is actually implemented.
The GCC manual says:
Warn if the loop cannot be optimized because the compiler cannot assume anything on the bounds of the loop indices. With -funsafe-loop-optimizations warn if the compiler makes such assumptions.
I can't find any documentation on how exactly GCC makes assumptions about the bounds of the loop indices. I have seen other questions about specific cases where this warning was emitted, but I am asking about how the compiler determines whether to emit it or not in general. Clang doesn't have this warning, and the similar clang-tidy bugprone-infinite-loops clearly spells out the criteria for warning.