In the code below, g_52 should end up being 5. At -O1, avr-gcc-4.5.0 puts 1 into this variable. Verified using AVR Studio. 4.3.3 also seems to have this bug.
reg...@john-home:~/volatile/work0/014255$ avr-gcc -O1 small.c -mmcu=atmega128 -o small.elf reg...@john-home:~/volatile/work0/014255$ avr-gcc -v Using built-in specs. Target: avr Configured with: ../configure --prefix=/home/regehr/avrgcc440/install --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp : (reconfigured) ../configure --prefix=/home/regehr/avrgcc440/install --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp : (reconfigured) ../configure --prefix=/home/regehr/avrgcc440/install --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp Thread model: single gcc version 4.5.0 20090403 (experimental) (GCC) reg...@john-home:~/volatile/work0/014255$ cat small.c #include <stdint.h> #include <limits.h> #define safe_add_macro_int8_t_s_s(si1,si2) \ ((((((int8_t)(si1))>((int8_t)0)) && (((int8_t)(si2))>((int8_t)0)) && (((int8_t)(si1)) > ((INT8_MAX)-((int8_t)(si2))))) \ || ((((int8_t)(si1))<((int8_t)0)) && (((int8_t)(si2))<((int8_t)0)) && (((int8_t)(si1)) < ((INT8_MIN)-((int8_t)(si2)))))) \ ? ((int8_t)(si1)) \ : (((int8_t)(si1)) + ((int8_t)(si2))) \ ) static int8_t safe_add_func_int8_t_s_s(int8_t _si1, int8_t _si2) { return safe_add_macro_int8_t_s_s(_si1,_si2); } uint8_t g_52; void func_1 (void); void func_1 (void) { uint64_t l_116; for (l_116 = 0; l_116 < 13; l_116 = safe_add_func_int8_t_s_s (l_116, 3)) { g_52++; } } static inline void platform_main_end(uint32_t crc); static inline void platform_main_end(uint32_t crc) { uint16_t crc16 = crc ^ (crc >> 16); asm volatile("cli" "\n\t" "mov r30, %A0" "\n\t" "mov r31, %B0" "\n\t" "break" : : "r" (crc16) : "memory" ); } int main (void) { func_1 (); platform_main_end (g_52); return 0; } -- Summary: loop bug on AVR Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: regehr at cs dot utah dot edu GCC build triplet: i486-linux-gnu GCC host triplet: i486-linux-gnu GCC target triplet: avr http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39633