This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

optimization/10050: Weak optimization on array references in conditiona blocks


>Number:         10050
>Category:       optimization
>Synopsis:       Weak optimization on array references in conditiona blocks
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Wed Mar 12 21:06:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Toshi Morita
>Release:        unknown-1.0
>Organization:
>Environment:
i386-linux cross sh-elf, but probably all targets
>Description:
Given these functions using the options -O2 -m4 -S:

int flag;

int func1(char *a)

{
        int b[3];

        b[0] = func();
        b[1] = b[0] * 2;

        a[0] = b[0];
        a[1] = b[1];

        return a[0] + b[1] + a[1];
}

int func2(int *a)

{
        int b[3];

        b[0] = func();
        b[1] = b[0] * 2;

        if (flag) {
                a[0] = b[0];
                a[1] = b[1];
        }

        return a[0] + b[1] + a[1];
}

int func3(int *a)

{
        int b[3];
        int temp0, temp1;

        b[0] = func();
        b[1] = b[0] * 2;

        temp0 = a[0];
        temp1 = a[1];

        if (flag) {
                temp0 = b[0];
                temp1 = b[1];
        }

        a[0] = temp0;
        a[1] = temp1;

        return a[0] + b[1] + a[1];
}

GCC is able to keep b[x] in registers for func1:

_func1:
        mov.l   r8,@-r15
        mov     r4,r8
        mov.l   r14,@-r15
        sts.l   pr,@-r15
        mov.l   .L2,r0
        add     #-12,r15
        jsr     @r0
        mov     r15,r14
        mov     r8,r1
        mov.b   r0,@r8
        mov     r0,r2
        add     #1,r1
        add     r2,r2
        mov.b   r2,@r1
        add     #12,r14
        mov.b   @r8,r0
        add     r2,r0
        exts.b  r2,r2
        add     r2,r0
        mov     r14,r15
        lds.l   @r15+,pr
        mov.l   @r15+,r14
        rts

However, gcc generates fairly weak code for func2. It is unable
to retain the values in registers and generates memory writes
within the conditional block:

_func2:
        mov.l   r8,@-r15
        mov     r4,r8
        mov.l   r14,@-r15
        sts.l   pr,@-r15
        mov.l   .L7,r0
        add     #-12,r15
        jsr     @r0
        mov     r15,r14
        mov.l   .L8,r1
        mov     r0,r2
        add     r2,r2
        mov.l   r0,@r14
        mov.l   @r1,r1
        tst     r1,r1
        bt/s    .L5
        mov.l   r2,@(4,r14)	<- (delay slot)
        mov.l   r0,@r8		<- conditional writes
        mov.l   r2,@(4,r8)	<- conditional writes
.L5:
        mov.l   @(4,r14),r1
        add     #12,r14
        mov.l   @r8,r0
        add     r1,r0
        mov.l   @(4,r8),r1
        add     r1,r0
        mov     r14,r15
        lds.l   @r15+,pr
        mov.l   @r15+,r14
        rts
        mov.l   @r15+,r8

To elicit the desired assembly output where no writes are emitted 
in the conditional block, it is necessary to hand-massage the
C source to something like func3, which gives this code:

func3:
        mov.l   r8,@-r15
        mov     r4,r8
        mov.l   r14,@-r15
        sts.l   pr,@-r15
        mov.l   .L12,r0
        add     #-12,r15
        jsr     @r0
        mov     r15,r14
        mov.l   .L13,r1
        mov     r0,r7
        add     r7,r7
        mov     r0,r3
        mov.l   @r1,r1
        mov.l   r0,@r14
        tst     r1,r1
        mov.l   r7,@(4,r14)
        mov.l   @r8,r2
        bt/s    .L11
        mov.l   @(4,r8),r0	(delay slot)
        mov     r3,r2		<- no memory write
        mov     r7,r0		<- no memory write
.L11:
        mov.l   r2,@r8
        add     r7,r2
        mov.l   r0,@(4,r8)
        add     #12,r14
        add     r2,r0
        mov     r14,r15
        lds.l   @r15+,pr
        mov.l   @r15+,r14
        rts
        mov.l   @r15+,r8

Toshi
>How-To-Repeat:
See above.
>Fix:
Don't know.
>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]