Bug 31274

Summary: [4.1 Regression] gcc 4.1 side effect missed (call clobbering is broken)
Product: gcc Reporter: Vitaly V. Bursov <vitalyb>
Component: tree-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: blocker CC: dberlin, fang, gcc-bugs, rguenth
Priority: P3 Keywords: alias, wrong-code
Version: 4.1.2   
Target Milestone: 4.2.0   
Host: x86_64-unknown-linux-gnu Target:
Build: Known to work: 4.3.0 4.2.0
Known to fail: 4.1.3 Last reconfirmed: 2007-03-20 10:54:58

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
===================================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.
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.