Menu

#748 SDCC generate unnecessary ORL with 0

None
open
nobody
5
2021-05-22
2020-10-07
Deqing Sun
No

Hi, I'm doing something a bit non-standard. I'm trying to use DPL and DPH to pass 2 8-bit data into a function, to prevent SDCC from using memory for the 2nd parameter.

I tried to use a 16bit parameter, but using "shift" and "or" logic to do the combine process, SDCC generated unnecessary code trying to do ORL with 0 for the unused bytes.

using code like (swap1<<8|swap2) will convert 2 8bit vlaue into one 16 bit. But SDCC insists on doing the OR logic with 2 16bit value, which creates ORL intruction with 0. I also tried to use union but SDCC said compound literals are not supported.

C code:

void test16bit(uint16_t a){

}

void setup() {
    volatile uint8_t swap0 = 1;
    uint8_t swap1 = swap0;  
    uint8_t swap2 = swap0+1;

    test16bit(swap1<<8|swap2);
}

Assembly code:

0000BF                        634 _setup:
                                    635 ;   /var/folders/l6/28chlnbn66b1nflyprjgv26h0000gn/T/arduino_modified_sketch_134383/Blink.ino:33: volatile uint8_t swap0 = 1;
      0000BF 75 12 01         [24]  636     mov _setup_swap0_65536_133,#0x01
                                    637 ;   /var/folders/l6/28chlnbn66b1nflyprjgv26h0000gn/T/arduino_modified_sketch_134383/Blink.ino:34: uint8_t swap1 = swap0;  
      0000C2 AF 12            [24]  638     mov r7,_setup_swap0_65536_133
                                    639 ;   /var/folders/l6/28chlnbn66b1nflyprjgv26h0000gn/T/arduino_modified_sketch_134383/Blink.ino:35: uint8_t swap2 = swap0+1;
      0000C4 AE 12            [24]  640     mov r6,_setup_swap0_65536_133
      0000C6 0E               [12]  641     inc r6
                                    642 ;   /var/folders/l6/28chlnbn66b1nflyprjgv26h0000gn/T/arduino_modified_sketch_134383/Blink.ino:37: test16bit(swap1<<8|swap2);
      0000C7 8F 05            [24]  643     mov ar5,r7
      0000C9 E4               [12]  644     clr a
      0000CA FF               [12]  645     mov r7,a
      0000CB FC               [12]  646     mov r4,a
      0000CC EE               [12]  647     mov a,r6
      0000CD 42 07            [12]  648     orl ar7,a
      0000CF EC               [12]  649     mov a,r4
      0000D0 42 05            [12]  650     orl ar5,a
      0000D2 8F 82            [24]  651     mov dpl,r7
      0000D4 8D 83            [24]  652     mov dph,r5
                                    653 ;   /var/folders/l6/28chlnbn66b1nflyprjgv26h0000gn/T/arduino_modified_sketch_134383/Blink.ino:38: }
      0000D6 02 00 BE         [24]  654     ljmp    _test16bit

Related

Feature Requests: #803

Discussion

  • Deqing Sun

    Deqing Sun - 2020-10-08

    It seems peeph.def didn't handle this issue, maybe it can be improved with additional peephole rule?

     
  • Konstantin Kim

    Konstantin Kim - 2020-11-03

    You may inline test16bit function or even better to replace 'volatile' with 'const'.

     

    Last edit: Konstantin Kim 2020-11-03
  • Oleg Endo

    Oleg Endo - 2021-05-14

    Here's another (maybe simpler) case which shows a nice roundtrip of nops

    __data uint8_t x;
    __data uint8_t y;
    
    uint16_t bleh (void)
    {
      return ((uint16_t)x << 8) | y;
    }
    
    _bleh:
        mov r7,_x
        mov r6,#0x00
        mov r4,_y
        mov r5,#0x00
        mov a,r4
        orl a,r6
        mov dpl,a
        mov a,r5
        orl a,r7
        mov dph,a
        ret
    

    Should be:

    _bleh:
        mov dph, _x
        mov dpl, _y
        ret
    
     
  • Maarten Brock

    Maarten Brock - 2021-05-22

    Ticket moved from /p/sdcc/bugs/3125/

    Can't be converted:

    • _category: MCS51
     
  • Maarten Brock

    Maarten Brock - 2021-05-22

    There is no bug here, only a missed optimization.

     

Log in to post a comment.