[Bug c/37569] New: Loop incorrectly optimized to endless loop with -Os/-O2/-O3
The loop in the example is incorrectly replaced with endless loop when compiling with -Os, -O2 or -O3. It is compiled correctly with -O0 or -O1. uint16 sum=0; uint16 *i; /*sum all words in inspected area*/ i = (uint16 *)0x0FFE; do { --i; sum = sum + *(i); } while ( i != 0 ); (see the full source in the attachment) Target: i686-pc-cygwin Configured with: ../gcc-4.3.2/configure Thread model: single gcc version 4.3.2 (GCC) (the same problem occurs with earlier versions of GCC (e.g. 3.4.3) and G++ ) -- Summary: Loop incorrectly optimized to endless loop with -Os/- O2/-O3 Product: gcc Version: 4.3.2 Status: UNCONFIRMED Severity: major Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: ppr at melexis dot com GCC host triplet: i686-pc-cygwin http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #1 from ppr at melexis dot com 2008-09-18 07:52 --- Created an attachment (id=16352) --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16352&action=view) preprocessed source of the test case -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #2 from ppr at melexis dot com 2008-09-18 07:53 --- Created an attachment (id=16353) --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16353&action=view) The produced assembler source -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #3 from ppr at melexis dot com 2008-09-18 07:53 --- Created an attachment (id=16354) --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16354&action=view) The compiler output -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #4 from ppr at melexis dot com 2008-09-18 07:58 --- The problem seems to be related with the i pointer reaching 0. If the loop condition is e.g. "while ( i != 2 )" everything is ok. The same if the "sum = sum + *(i)" is replaced e.g. with "sum = sum + *(i+1)" -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #6 from ppr at melexis dot com 2008-09-18 08:22 --- (In reply to comment #5) > another vrp bug? -fno-tree-vrp helps. > Yes, -fno-tree-vrp does help -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #9 from ppr at melexis dot com 2008-09-18 10:20 --- (In reply to comment #8) > I think this code is invalid. I seem to recall the argument that it's > undefined behavior to increment or decrement a pointer to 0. Well, the pointer is not NULL initially, it's just decremented until it reaches zero. I agree this code doesn't make much sense in i686-pc environment, but I've discovered the issue while working with GCC port for 16-bit microcontroller. The routine is used to calculate ROM checksum and address 0 is a perfectly valid address at this target. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #11 from ppr at melexis dot com 2008-09-18 10:57 --- (In reply to comment #10) This workaround doesn't work with GCC 3.4.3 (the base for our 16-bit port). It does work with the latest GCC4.3 though. Anyway, thanks for your support, and for explaining the reasons behind the compiler's behaviour. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569
[Bug c/37569] Loop incorrectly optimized to endless loop with -Os/-O2/-O3
--- Comment #13 from ppr at melexis dot com 2008-09-18 12:15 --- (In reply to comment #12) > Note you can do the same as AVR does - force flag_delete_null_pointer_checks > to > zero: > Thanks for the hint, I'll use it! Plamen -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37569