https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71769
Bug ID: 71769 Summary: Invalid warning from -Wunsafe-loop-optimizations for a finite loop Product: gcc Version: 6.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: nightstrike at gmail dot com Target Milestone: --- Created attachment 38834 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38834&action=edit Testcase In the attached testcase, function f compiles without warning for -O[023], but function g warns for -O[0123]. The warning is "warning: cannot optimize possibly infinite loops [-Wunsafe-loop-optimizations]". The functions are very similar, and I don't think it should warn in either case, as the conditionals verify that the loop isn't infinite. $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/opt/gcc6/bin/../libexec/gcc/x86_64-pc-linux-gnu/6.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc-6.1.0/configure --prefix=/gcc6 --enable-libstdcxx-time=rt --disable-multilib --enable-languages=all,ada,obj-c++ --enable-plugins --with-gnu-as --with-gnu-ld Thread model: posix gcc version 6.1.0 (GCC) $ gcc -c -std=gnu99 a.c -Wunsafe-loop-optimizations -O3 a.c: In function āgā: a.c:18:2: warning: cannot optimize possibly infinite loops [-Wunsafe-loop-optimizations] for(int i = a; i <= b; ++i) ^~~ $ cat a.c // Warning with -Wunsafe-loop-optimizations and -O1 only void f(int * x, int a, int b) { if (b < a) b = a; if (a < 1) return; for (int i = a; i <= b; ++i) x[i] += 1; } struct S { unsigned int * data; unsigned int * d2; }; // Warning with -Wunsafe-loop-optimizations and -O1 or above void g(struct S * x, int a, int b) { if (b < a) b = a; if (a < 1) return; for(int i = a; i <= b; ++i) __builtin_memcpy(x->data, x->d2, 5); }