Menu

#491 Wrong code optimization

None
closed-fixed
None
5
2016-03-12
2016-02-20
No

cat bug.c

__sbit __at(0xA0) P2_0;

void main(void) {

  // toggle pin

  for(;;) {
    if(P2_0) {
        P2_0=0;
    } else {
        P2_0=1;
    }
  }
}

compile:
sdcc -mmcs51 --opt-code-size --model-small --Werror -I. --std-sdcc99 bug.c

cat bug.rst

                                      1 ;--------------------------------------------------------
                                      2 ; File Created by SDCC : free open source ANSI-C Compiler
                                      3 ; Version 3.5.0 #9253 (Jun 20 2015) (MINGW64)
                                      4 ; This file was generated Sat Feb 20 16:35:42 2016
                                      5 ;--------------------------------------------------------
                                      6     .module bug
                                      7     .optsdcc -mmcs51 --model-small
----8<---8<---
      000062                        123 _main:
                           000007   124     ar7 = 0x07
                           000006   125     ar6 = 0x06
                           000005   126     ar5 = 0x05
                           000004   127     ar4 = 0x04
                           000003   128     ar3 = 0x03
                           000002   129     ar2 = 0x02
                           000001   130     ar1 = 0x01
                           000000   131     ar0 = 0x00
      000062                        132 00105$:
                                    133 ;   bug.c:8: if(P2_0) {
                                    134 ;   bug.c:9: P2_0=0;
      000062 10 A0 02         [24]  135     jbc _P2_0,00115$
      000065 80 02            [24]  136     sjmp    00102$
      000067                        137 00115$:
      000067 80 F9            [24]  138     sjmp    00105$
      000069                        139 00102$:
                                    140 ;   bug.c:11: P2_0=1;
      000069 D2 A0            [12]  141     setb    _P2_0
      00006B 80 F5            [24]  142     sjmp    00105$

Dmitriy
kharpost@altlinux.org

2 Attachments

Discussion

  • Ben Shi

    Ben Shi - 2016-02-20

    The generated assembly is right though not quite efficient.

    ;       a.c:3: void main(void)
    ;       -----------------------------------------
    ;        function main
    ;       -----------------------------------------
    _main:
            ar7 = 0x07
            ar6 = 0x06
            ar5 = 0x05
            ar4 = 0x04
            ar3 = 0x03
            ar2 = 0x02
            ar1 = 0x01
            ar0 = 0x00
    00105$:
    ;       a.c:6: if (P2_0) P2_0 = 0;
            jbc     _P2_0,00115$
            sjmp    00102$
    00115$:
            sjmp    00105$
    00102$:
    ;       a.c:7: else P2_0 = 1;
            setb    _P2_0
            sjmp    00105$
    

    A better optimized one could be

    ;       a.c:3: void main(void)
    ;       -----------------------------------------
    ;        function main
    ;       -----------------------------------------
    _main:
            ar7 = 0x07
            ar6 = 0x06
            ar5 = 0x05
            ar4 = 0x04
            ar3 = 0x03
            ar2 = 0x02
            ar1 = 0x01
            ar0 = 0x00
    00105$:
            jbc     _P2_0,00105$
            setb    _P2_0
            sjmp    00105$
    
     
    • Kharitonov Dmitry

       

      Last edit: Kharitonov Dmitry 2016-02-24
    • Kharitonov Dmitry

       

      Last edit: Kharitonov Dmitry 2016-02-25
  • Maarten Brock

    Maarten Brock - 2016-02-22

    This should be fixed in the peephole rules. This is where the jbc is inserted. It needs a jump-jump optimization afterwards. Maybe reorder the rules or add a restart.

     

    Last edit: Maarten Brock 2016-02-25
  • Maarten Brock

    Maarten Brock - 2016-02-22
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,25 +1,28 @@
     cat bug.c
    
    +~~~~
     __sbit __at(0xA0) P2_0;
    
     void main(void) {
    
    -// toggle pin
    
    +  // toggle pin
    
    -for(;;) {
    
    +  for(;;) {
         if(P2_0) {
             P2_0=0;
         } else {
             P2_0=1;
         }
    +  }
     }
    -
    -}
    +~~~~
    
     compile:
     sdcc -mmcs51 --opt-code-size --model-small --Werror -I. --std-sdcc99 bug.c
    
     cat bug.rst
    +
    +~~~~
                                           1 ;--------------------------------------------------------
                                           2 ; File Created by SDCC : free open source ANSI-C Compiler
                                           3 ; Version 3.5.0 #9253 (Jun 20 2015) (MINGW64)
    @@ -48,6 +51,7 @@
                                         140 ;  bug.c:11: P2_0=1;
           000069 D2 A0            [12]  141    setb    _P2_0
           00006B 80 F5            [24]  142    sjmp    00105$
    
    -      
    +~~~~
    +
     Dmitriy
     kharpost@altlinux.org
    
    • Group: -->
     
  • Kharitonov Dmitry

    The compiler generated code is a non-working
    A better optimized one could be

    00105$:
            cpl     _P2_0
            sjmp    00105$
    
     
    • Maarten Brock

      Maarten Brock - 2016-03-12

      The new peephole rules do not go as far to use CPL, but stop at JBC, SETB.

      If you want a real complement, you should write:

      P2_0 = !P2_0;
      
       
  • Kharitonov Dmitry

    ... or

    00105$:
            jbc     _P2_0,00115$
            sjmp    00102$
    00115$:
            **clrb    _P2_0**
            sjmp    00105$
    00102$:
            setb    _P2_0
            sjmp    00105$
    
     
  • Maarten Brock

    Maarten Brock - 2016-02-25

    Look up what JBC stands for and you'll know why the generated code is ok.

     
  • Kharitonov Dmitry

    I'm sorry, I'm confused JBC with JNB.

     
  • Maarten Brock

    Maarten Brock - 2016-03-12
    • status: open --> closed-fixed
    • assigned_to: Maarten Brock
     
  • Maarten Brock

    Maarten Brock - 2016-03-12

    Implemented by peephole rules 257.x and 259.x in [r9519].

     

Log in to post a comment.

MongoDB Logo MongoDB