User account creation filtered due to spam.

Bug 31274 - [4.1 Regression] gcc 4.1 side effect missed (call clobbering is broken)
Summary: [4.1 Regression] gcc 4.1 side effect missed (call clobbering is broken)
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.2
: P3 blocker
Target Milestone: 4.2.0
Assignee: Not yet assigned to anyone
Keywords: alias, wrong-code
Depends on:
Reported: 2007-03-19 23:09 UTC by Vitaly V. Bursov
Modified: 2008-07-04 16:03 UTC (History)
4 users (show)

See Also:
Host: x86_64-unknown-linux-gnu
Known to work: 4.3.0 4.2.0
Known to fail: 4.1.3
Last reconfirmed: 2007-03-20 10:54:58


Note You need to log in before you can comment on or make changes to this bug.
Description Vitaly V. Bursov 2007-03-19 23:09:26 UTC
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
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;
static struct for_state for_stack[MAX_FOR_STACK_DEPTH];
static int for_stack_ptr;

#define MAX_VARNUM 26
int variables[MAX_VARNUM];

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) {

int ubasic_get_variable(int varnum)
    return variables[varnum];

/* // "fixes" problem also
void xxx(int a, int b)
    for_stack[a].to = b;

$ 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

Comment 1 Andrew Pinski 2007-03-20 06:34:01 UTC
Related to PR 20115.  Though in this case we only get VUSE as all the variables listed are really const which is why if you add a function which sets for_stack_ptr, it works as for_stack_ptr is no longer const.
This works on the mainline for some reason, we just use GLOBAL_VAR.
Comment 2 Richard Biener 2007-03-20 10:54:58 UTC
Confirmed.  Also related to PR28778 - call clobbering is broken in 4.1.  I don't
see us fixing this on the 4.1 branch though.
Comment 3 Joseph S. Myers 2008-07-04 16:03:57 UTC
Closing 4.1 branch.