Bug 35764

Summary: improper load from volatile
Product: gcc Reporter: John Regehr <regehr>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gcc-bugs, rguenth
Priority: P3 Keywords: wrong-code
Version: 4.3.0   
Target Milestone: 4.7.0   
Host: i686-pc-linux-gnu Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu Known to work: 4.1.2, 4.7.0
Known to fail: 4.0.1, 4.3.0, 4.4.0, 4.6.4 Last reconfirmed: 2008-12-29 03:54:58

Description John Regehr 2008-03-30 04:18:26 UTC
This is for "gcc version 4.3.0 (GCC)" 

This is very likely related to 35729 and 35762.

This code:

extern int bar (void);
volatile int g_156;
int foo (void)
{
  if (bar ())
    return 0;
  else
    return g_156;
}

Compiled with:

  gcc -O1 -S foo.c

Gives:
foo:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        call    bar
        cmpl    $1, %eax
        sbbl    %eax, %eax
        andl    g_156, %eax
        leave
        ret

The unconditional load from volatile g_156 is incorrect.
Comment 1 Andrew Pinski 2008-11-15 00:09:09 UTC
This works for me on the trunk.  I either get a cmove or a branch.
Comment 2 John Regehr 2008-11-16 04:34:06 UTC
I'm not sure what is going on, I get the same code as before using r141907 on Ubuntu Hardy on x86.  What compiler options are you using and what platform?

regehr@john-home:~$ current-gcc -O -S vol.c -o -
	.file	"vol.c"
	.text
.globl foo
	.type	foo, @function
foo:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	call	bar
	cmpl	$1, %eax
	sbbl	%eax, %eax
	andl	g_156, %eax
	leave
	ret
	.size	foo, .-foo
	.comm	g_156,4,4
	.ident	"GCC: (GNU) 4.4.0 20081116 (experimental)"
	.section	.note.GNU-stack,"",@progbits
regehr@john-home:~$ 
regehr@john-home:~$ current-gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --program-prefix=current- --enable-languages=c,c++ --prefix=/home/regehr
Thread model: posix
gcc version 4.4.0 20081116 (experimental) (GCC) 
regehr@john-home:~$ 
Comment 3 Andrew Pinski 2008-12-29 03:51:11 UTC
*** Bug 35762 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2008-12-29 03:54:58 UTC
Confirmed, -march=i386 -mtune=i686 is needed to confirm the bug in general.

GCC is producing a "conditional move" for the code even though it is invalid to produce such a thing.

Comment 5 Andrew Pinski 2021-07-29 22:34:07 UTC
Fixed in GCC 4.7.0 because this is a dup of bug 47698.

*** This bug has been marked as a duplicate of bug 47698 ***