optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc)

makoto@ki.nu makoto@ki.nu
Sun Jul 28 15:26:00 GMT 2002


>Number:         7427
>Category:       optimization
>Synopsis:       (powerpc) -O2 problem for checksum calc.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Sun Jul 28 15:26:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Makoto Fujiwara
>Release:        3.1.1
>Organization:
office.ki.nu
>Environment:
System: NetBSD quick 1.5ZC NetBSD 1.5ZC (GENERIC) #0: Fri Mar 29 19:38:39 JST 2002 tsubai@mahoro:/a/0328/src/sys/arch/macppc/compile/GENERIC macppc


	
host: powerpc--netbsd
build: powerpc--netbsd
target: powerpc--netbsd
configured with:../configure --with-gcc-version-trigger=/export/local-src/gcc-3.1.1/gcc/version.c --host=powerpc-unknown-netbsd1.5ZC --norecursion 

>Description:
	With code attached, say, sum.c, the output with -O2 shows different value with -O or -O1.
   -O  sum = 0x68ac
   -O1 sum = 0x68ac
   -O2 sum = 0x8fb9
>How-To-Repeat:
Compile following file on macppc machine with -O1 and -O2 and execute them respectively.
-------------------------------
#include <stdio.h>

struct buf {
	int data;
};

bug(m)
	struct buf *m;
{
	int sum = 0;
	struct buf tmp;
	unsigned short *w = (void *)&tmp;

	bzero(&tmp, sizeof tmp);
	tmp.data = m->data;

	//asm volatile ("" ::: "memory");

	sum += w[0];
	sum += w[1];

	printf("sum = 0x%x\n", sum);

	return 0;
}

main()
{
	struct buf m;

	m.data = 0x12345678;
	bug(&m);
}
-------------------------------
I will attach the .s code for the reference:
(1) with -O1
.........................................................................
        .file   "sum.c"
        .section        .rodata
        .align 2
.LC0:
        .string "sum = 0x%x\n"
        .section        ".text"
        .align 2
        .globl bug
        .type   bug,@function
bug:
        stwu 1,-16(1)
        mflr 0
        stw 0,20(1)
        lwz 0,0(3)
        stw 0,8(1)
        lhz 4,8(1)
        lhz 0,10(1)
        add 4,4,0
        lis 3,.LC0@ha
        la 3,.LC0@l(3)
        crxor 6,6,6
        bl printf
        li 3,0
        lwz 0,20(1)
        mtlr 0
        addi 1,1,16
        blr
.Lfe1:
        .size   bug,.Lfe1-bug
        .align 2
        .globl main
        .type   main,@function
main:
        stwu 1,-16(1)
        mflr 0
        stw 0,20(1)
        lis 0,0x1234
        ori 0,0,22136
        stw 0,8(1)
        addi 3,1,8
        crxor 6,6,6
        bl bug
        lwz 0,20(1)
        mtlr 0
        addi 1,1,16
        blr
.Lfe2:
        .size   main,.Lfe2-main
        .ident  "GCC: (GNU) 3.1.1"
.........................................................................
(2) with -O2
.........................................................................
        .file   "sum.c"
        .section        .rodata
        .align 2
.LC0:
        .string "sum = 0x%x\n"
        .section        ".text"
        .align 2
        .globl bug
        .type   bug,@function
bug:
        stwu 1,-16(1)
        mflr 0
        stw 0,20(1)
        lhz 4,8(1)
        lhz 0,10(1)
        lwz 9,0(3)
        lis 3,.LC0@ha
        add 4,4,0
        la 3,.LC0@l(3)
        stw 9,8(1)
        crxor 6,6,6
        bl printf
        lwz 0,20(1)
        li 3,0
        addi 1,1,16
        mtlr 0
        blr
.Lfe1:
        .size   bug,.Lfe1-bug
        .align 2
        .globl main
        .type   main,@function
main:
        stwu 1,-16(1)
        mflr 0
        stw 0,20(1)
        addi 3,1,8
        lis 0,0x1234
        ori 0,0,22136
        stw 0,8(1)
        crxor 6,6,6
        bl bug
        lwz 0,20(1)
        addi 1,1,16
        mtlr 0
        blr
.Lfe2:
        .size   main,.Lfe2-main
        .ident  "GCC: (GNU) 3.1.1"
.........................................................................
>Fix:
	Not known at the moment.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-prs mailing list