While compiling code below with -O1 second call to ubasic_get_variable() gets optimized out and code works incorrectly as ubasic_set_variable() can modify variables[].
Code runs as expected when compiled with 4.0.2 or with 4.1.1, 4.1.2 (and 4.1 branch) with "-O1 -fno-tree-dominator-opts -fno-tree-fre" switches. Haven't checked 4.2. Happes both on x86-64 and arm-elf. $ arm-elf-gcc -O1 -S test.c ===================================CUT int ubasic_get_variable(int varnum); void ubasic_set_variable(int varum, int value); struct for_state { short line_after_for; short for_variable; int to; }; #define MAX_FOR_STACK_DEPTH 4 static struct for_state for_stack[MAX_FOR_STACK_DEPTH]; static int for_stack_ptr; #define MAX_VARNUM 26 int variables[MAX_VARNUM]; void next_statement(int var) { ubasic_set_variable(var, ubasic_get_variable(var) + 1); if(ubasic_get_variable(var) <= for_stack[for_stack_ptr - 1].to) { jump_line(0); } } int ubasic_get_variable(int varnum) { return variables[varnum]; } /* // "fixes" problem also void xxx(int a, int b) { for_stack[a].to = b; } */ ===================================CUT $ arm-elf-gcc -v Using built-in specs. Target: arm-elf Configured with: /mnt/Sources/GCC/gcc_4/configure --prefix=/opt/arm --target=arm-elf --enable-interwork --enable-multilib --enable-languages=c Thread model: single gcc version 4.1.3 20070319 (prerelease) $ gcc-4.1.2 -v Using built-in specs. Target: x86_64-unknown-linux-gnu Configured with: /mnt/Sources/GCC/gcc_4_1_2_release/configure --prefix=/usr/local --libexecdir=/usr/local/lib64 --program-suffix=-4.1.2 --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --disable-multilib --enable-languages=c,c++,objc,fortran,java,obj-c++ Thread model: posix gcc version 4.1.2 Thanks. -- Summary: gcc 4.1 side effect missed Product: gcc Version: 4.1.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: vitalyb at telenet dot dn dot ua GCC host triplet: x86_64-unknown-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31274