Suggestion for better code

Tom Haywood thomas_haywood@aus.hp.com
Wed May 12 19:20:00 GMT 1999


To whomever,

This is not a bug report, but a suggestion for better assembler output.
If this is the wrong place to send improvement suggestions, please advise
me where to send this.

egcs version : 1.1.2
target       : Hitachi SH-3

I have been investigating code dealing with char comparisons. What I have
noticed is that before the comparison is performed, the registers involved
are always sign extended before hand.

e.g.
...
mov.b    @r0, r2
exts.b    r2, r2
mov.b    @r1, r3
exts.b    r3, r3
cmp/ge   r2,r3

Reading the Hitach manuals, this sign extension is unneccesary. Whenever
a mov instruction is excuted involving either a byte or a word, the data
is sign extended before being written into the register. Because of this
the exts.b instruction is redundant and hence a waste of a cycle. In big
loops involving chars, the saving can be quite dramatic.

So, my suggestion is to modify egcs for the SH-3 target, to not emit code
to perform sign extensions when dealing with chars. I believe this would
apply to shorts as well, but I have not investigated this. I do not know
the SH-1, SH-2 or SH-4, but I believe that this will apply for them as well.

Attached below is a commented example of where the improvement could be made.

Regards,

Tom Haywood,
R&D Software Engineer,
Hewlett-Packard Australia Ltd.


foo.c:
------
/*
 * An implementation of strcpy
 */
char *tstrcpy(char *s1, const char *s2)
{
    char* result = s1;
    while ((*s1++ = *s2++) != '\0')
        ;

    return result;
}


foo.s:
------
/*
 * Compiler output for sh-coff-gcc -O2 -m2 -m3 -S foo.c
 */
        .file   "foo.c"
        .data
gcc2_compiled.:
___gnu_compiled_c:
        .text
        .align 4
        .global _tstrcpy
_tstrcpy:
        mov.l   r14,@-r15
        mov     r4,r0
        mov.b   @r5+,r1 <--- Data is sign extended by the mov.b instruction
        mov     r0,r2
        mov.b   r1,@r2
        exts.b  r1,r1   <--- r1 is already sign extended, no need for this
        bra     L6
        mov     r15,r14
        .align 4
L2:
        mov.b   @r5+,r1
        mov.b   r1,@r0
        exts.b  r1,r1   <--- This instruction is redundant as well.
L6:
        tst     r1,r1
        bf.s    L2
        add     #1,r0
        mov     r2,r0
        mov     r14,r15
        rts
        mov.l   @r15+,r14

The loop could be reduced from 6 cycles to 5 cycles per execution,
giving a 17% improvement for the code.


More information about the Gcc-bugs mailing list