i found a simple solution now:

diff -r 46d745fbf479 sys/src/cmd/6c/mul.c
--- a/sys/src/cmd/6c/mul.c      Thu Mar 20 16:44:01 2014 +0100
+++ b/sys/src/cmd/6c/mul.c      Fri Mar 21 00:03:49 2014 +0100
@@ -312,6 +312,11 @@
        Mparam *p;
        Node nod, nods;
 
+       if(v == 0){
+               zeroregm(n);
+               return 1;
+       }
+
        for(i = 0; i < nelem(multab); i++) {
                p = &multab[i];
                if(p->value == v)


as expected, the code generated *without* optimization does
some stupid moves:

term% ./6.out -SN /tmp/a.c
        TEXT    bar+0(SB),0,$0
        MOVL    BP,a+0(FP)
        MOVL    a+0(FP),AX
        MOVL    $0,AX
        MOVL    AX,a+0(FP)
        MOVL    a+0(FP),AX
        NOP     ,X0
        RET     ,
        NOP     ,AX
        NOP     ,X0
        RET     ,
        TEXT    foo+0(SB),0,$16
        MOVL    BP,a+0(FP)
        MOVL    a+0(FP),BP
        CALL    ,bar+0(SB)
        MOVL    $0,AX
        NOP     ,X0
        RET     ,
        NOP     ,AX
        NOP     ,X0
        RET     ,
        TEXT    main+0(SB),0,$16
        MOVL    BP,argc+0(FP)
        MOVL    $1,BP
        CALL    ,foo+0(SB)
        NOP     ,AX
        NOP     ,X0
        RET     ,
        END     ,

with optimization, the stupid moves are gone:

term% ./6.out -S /tmp/a.c
        TEXT    bar+0(SB),0,$0
        MOVL    $0,AX
        RET     ,
        RET     ,
        TEXT    foo+0(SB),0,$16
        CALL    ,bar+0(SB)
        MOVL    $0,AX
        RET     ,
        RET     ,
        TEXT    main+0(SB),0,$16
        MOVL    $1,BP
        CALL    ,foo+0(SB)
        RET     ,
        END     ,

so this is as good as the initial attempt just that
the change is just 4 lines in one location where we
do the actual multiplication.

--
cinap

Reply via email to