Bug 39194

Summary: options -O3 and -msse does not cooperate ?
Product: gcc Reporter: Øystein Schønning-Johansen <oysteijo>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: billingd, dannysmith, gcc-bugs, nickc, oysteijo, sherpya, zuxy.meng
Priority: P3 Keywords: wrong-code
Version: 4.3.0   
Target Milestone: ---   
Host: Target: mingw32-*-*
Build: Known to work:
Known to fail: Last reconfirmed:

Description Øystein Schønning-Johansen 2009-02-14 18:49:13 UTC
Hi,

I try the following code:

#define MAX_N 40
#define MAX_R 25

#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif

static unsigned int bin_coeff_tab[ MAX_N ][ MAX_R ];
static int is_calculated = FALSE;

static void
bin_coeff_init( void ) 
{

    int i, j;

    for( i = 0; i < MAX_N; i++ )
        bin_coeff_tab[ i ][ 0 ] = i + 1;
    
    for( j = 1; j < MAX_R; j++ )
        bin_coeff_tab[ 0 ][ j ] = 0;

    for( i = 1; i < MAX_N; i++ )
        for( j = 1; j < MAX_R; j++ )
            bin_coeff_tab[ i ][ j ] = bin_coeff_tab[ i - 1 ][ j - 1 ] +
                bin_coeff_tab[ i - 1 ][ j ];

    is_calculated = TRUE;
}
int main()
{
	bin_coeff_init();
	return 0;
}

(There is no header file included, so I won't include the .i file)

If I try to compile this with both -O3 and -msse, I get a segmentation fault.

> gcc --version
gcc (GCC) 4.3.0 20080305 (alpha-testing) mingw-20080502

the assembler code file becomes:

	.file	"mytest.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
	.p2align 2,,3
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%esi
	pushl	%ebx
	pushl	%ecx
	subl	$12, %esp
	call	___main
	movl	$_bin_coeff_tab, %edx
	xorl	%eax, %eax
	.p2align 2,,3
L2:
	incl	%eax
	movl	%eax, (%edx)
	addl	$100, %edx
	cmpl	$40, %eax
	jne	L2
	movl	$0, _bin_coeff_tab+4
	movl	$0, _bin_coeff_tab+8
	movl	$0, _bin_coeff_tab+12
	xorps	%xmm0, %xmm0
	movaps	%xmm0, _bin_coeff_tab+16
	movaps	%xmm0, _bin_coeff_tab+32
	movaps	%xmm0, _bin_coeff_tab+48
	movaps	%xmm0, _bin_coeff_tab+64
	movaps	%xmm0, _bin_coeff_tab+80
	movl	$0, _bin_coeff_tab+96
	movl	$1, %esi
	.p2align 2,,3
L3:
	leal	(%esi,%esi,4), %eax
	leal	(%eax,%eax,4), %eax
	leal	_bin_coeff_tab-100(,%eax,4), %edx
	leal	_bin_coeff_tab+4(,%eax,4), %ebx
	movl	$1, %ecx
	.p2align 2,,3
L4:
	movl	4(%edx), %eax
	addl	(%edx), %eax
	movl	%eax, (%ebx)
	incl	%ecx
	addl	$4, %edx
	addl	$4, %ebx
	cmpl	$25, %ecx
	jne	L4
	incl	%esi
	cmpl	$40, %esi
	jne	L3
	movl	$1, _is_calculated
	xorl	%eax, %eax
	addl	$12, %esp
	popl	%ecx
	popl	%ebx
	popl	%esi
	leave
	leal	-4(%ecx), %esp
	ret
.lcomm _is_calculated,16
.lcomm _bin_coeff_tab,4000

Best regards,
-Øystein
Comment 1 Richard Biener 2009-02-14 19:24:17 UTC
I suppose _bin_coeff_tab is not properly aligned.  Maybe PE support is not
properly honoring DECL_ALIGN.  Please try also GCC 4.3.3 which has many
bugfixes compared to 4.3.0.
Comment 2 Uroš Bizjak 2009-02-16 07:41:15 UTC
You can workaround this generic PE/COFF problem using -fno-common.

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