Hi Rod,

Rod Boyce schrieb:
> I am witting some code for an 8051 and seeing that is looks to be 
> missing an optimisation anyway I have distilled th ecode to this fragment:
> 
> void display_update( unsigned char tens, unsigned char ones );
> 
> 
> void my_func( unsigned char my_value )
> {
>     unsigned char d1;
>     unsigned char d2;
> 
> 
>     d1 = my_value % 10;
>     d2 = my_value / 10;
> 
>     /* Do stuff with d1 & d1 */
>     display_update( d2, d1  );
> }
> 
> The list file shows me this:
>                             110 ;       test51.c:10: d1 = my_value % 10;
>    0002 75 F0 0A            111         mov     b,#0x0A
>    0005 EA                  112         mov     a,r2
>    0006 84                  113         div     ab
>    0007 85 F0*00            114         mov     _display_update_PARM_2,b
>                             115 ;       test51.c:11: d2 = my_value / 10;
>    000A 75 F0 0A            116         mov     b,#0x0A
>    000D EA                  117         mov     a,r2
>    000E 84                  118         div     ab
>    000F F5 82               119         mov     dpl,a
>                             120 ;       test51.c:14: display_update( d2, d1  
> );
>   
> I have tried swapping the modulo and div operation around but it doe snot 
> seem to have any effect.
> So my question is am I missing a command line switch when I compile the file? 
>  

No the optimization you are requesting is not yet implemented.

You can help yourself by using an extra peephole file though.
(I'm appending the file)

replace {
        mov b,%1
        mov a,%2
        div ab
        mov %3,b
        mov b,%1
        mov a,%2
        div ab
        mov %4,a
} by {
        mov b,%1
        mov a,%2
        div ab
        mov %3,b
        ;       peephole xxx.a  optimized modulo and division
        mov %4,a
} if notVolatile(%1 %2), operandsNotRelated(%1 %2 %3)

which you can use with the following command line:
sdcc -c --peep-file my_peeph.def --fverbose-asm divmod.c

and arrive at:

   0002 75 F0 0A            117         mov b,#0x0A
   0005 EA                  118         mov a,r2
   0006 84                  119         div ab
   0007 85 F0*00            120         mov _display_update_PARM_2,b
                            121 ;       peephole xxx.a  optimized modulo and 
division
   000A F5 82               122         mov dpl,a
                            123 ;       divmod.c:15: display_update (d2, d1);


which is probably what you want:)

This optimization (unless others approve) will probably not make it into SDCC 
2.9.0.

Greetings,
Frieder
// see mail on sdcc-user 2009-02-22
replace {
        mov b,%1
        mov a,%2
        div ab
        mov %3,b
        mov b,%1
        mov a,%2
        div ab
        mov %4,a
} by {
        mov b,%1
        mov a,%2
        div ab
        mov %3,b
        ;       peephole xxx.a  optimized modulo and division
        mov %4,a
} if notVolatile(%1 %2), operandsNotRelated(%1 %2 %3)

replace {
        mov b,%1
        mov a,%2
        div ab
        mov %3,a
        mov b,%1
        mov a,%2
        div ab
        mov %4,b
} by {
        mov b,%1
        mov a,%2
        div ab
        mov %3,a
        ;       peephole xxx.b  optimized modulo and division
        mov %4,b
} if notVolatile(%1 %2), operandsNotRelated(%1 %2 %3)

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to