https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67025
Bug ID: 67025 Summary: Missing aggressive loop optimization warning when -fPIC used Product: gcc Version: 5.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: Nikola.Veljkovic at imgtec dot com Target Milestone: --- The issue here is not that the loop is over optimized, but the fact that the warning is missing. Without the warning debugging an issue like this takes much longer. Source code: main.c: #include <stdio.h> struct problem { int* array1[4]; int* array2[4]; }; int* foo() { return 0; } int main(int argc, int** argv) { int i; struct problem p; for(i = 0; i < 8; i++) { printf("i=%d\n", i); p.array1[i] = foo(); if(p.array1[i]) return -1; } return 0; } Compilation (with -fPIC): $ gcc-5 -O2 -fPIC -o for-test main.c Compilation (without -fPIC): $ gcc-5 -O2 -o for-test main.c main.c: In function ‘main’: main.c:15:21: warning: iteration 4u invokes undefined behavior [-Waggressive-loop-optimizations] p.array1[i] = foo(); ^ main.c:13:5: note: containing loop for(i = 0; i < 8; i++) { Compilation is successful in both cases, but the warning is missing when compiling with -fPIC. Execution: The loop executes indefinitely in both cases. Tested on last week's build of gcc-5.2 release built for mips, and on Ubuntu 14.04 (i686 package) gcc-5.1. Should be reproducible on any architecture with compiler version 4.9 and above. I can provide any additional info, if required.