Menu

#485 sdcc poor codegeneration (z80)

None
closed-fixed
None
5
2016-01-30
2015-10-19
Alan Cox
No

Most of the time it gets these right, but sometimes it decides to compare an 8bit value with a constant that will fit in 8bits by writing bizarre code where it loads registers with 0, ors them and carefully checks for zero/nonzero.

I finally managed to narrow down an example showing the bad case

sdcc #9369

sdcc -mz80 --max-allocs-per-node 200000 /tmp/silly.c

note how the second "if" is evaluated !

1 Attachments

Discussion

  • Philipp Klaus Krause

    TOKENIZER_COMMA is an integer constant. SDCC casts the uint8_t to int, then compares. We are lucky that somehow SDCC manages to optimize the comparison into an unsigned int comparison (as signed comparisons on z80 are inefficient).

    SDCC should optimize the comparison into an 8-bit comparison.

    Workaround until SDCC is improved: Make TOKENIZER_COMMA an uint8_t.

    Philipp

     
  • Peter Dons Tychsen

    Hi,

    I just compiled this to see if it affected stm8 too... but it does not seem to fail here currently for z80. At least i do not see any zeros loaded regs or the silly orring.

    Did this disappear or move around?.

    ;tests/alan.c:29: t = current_token;
            ld      hl,#_current_token + 0
            ld      b, (hl)
    ;tests/alan.c:30: if (t == TOKENIZER_COMMA)
            ld      a,b
            sub     a, #0x2C
            jr      NZ,00108$
    ;tests/alan.c:31: accept_tok(t);
            push    bc
            push    bc
            inc     sp
            call    _accept_tok
            inc     sp
            pop     bc
            jr      00111$
    

    Thanks,

    /pedro

     
  • Ben Shi

    Ben Shi - 2016-01-28
    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
    • Category: other --> Front-end
     
  • Ben Shi

    Ben Shi - 2016-01-28

    I though this optimization has already been done by Philipp in reversion #9442 or reversion #9447.

    TOKENIZER_COMMA is an unsigned int in AST but then optimized to unsigned char in the icode list.

    Here is the AST and icode list by current SDCC.

    silly.c:0:    IF (002609D0)
    silly.c:27:      EQ(==) (0025BF70) type (char fixed)
    silly.c:27:        SYMBOL (L1 B6 t=0025BEB0 @ 00254550) type (unsigned-char auto)
    silly.c:27:        CONSTANT (0025BF10) value = 44, 0x2c, 44.000000 type (const-int literal)
    silly.c:0:      NE(!=) 0 goto __iftrue_2
    silly.c:0:      EQ(==) 0 goto __iffalse_2
    (null):0:    LABEL (00260670)
    silly.c:0:      SYMBOL (L2 B7 __iftrue_2=00260610 @ 0025EF48)
    silly.c:28:    CALL (0025C090) type (void fixed)
    silly.c:28:      SYMBOL (L0 B0 accept_tok=0025BFD0 @ 00253C18) type (void function ( unsigned-char fixed) fixed)
    silly.c:28:      SYMBOL (L1 B6 t=0025C030 @ 00254550) type (unsigned-char auto)
    silly.c:0:    GOTO (00260730)
    silly.c:0:      SYMBOL (L2 B7 __ifend_2=002606D0 @ 0025ED70)
    (null):0:    LABEL (00260850)
    silly.c:0:      SYMBOL (L2 B7 __iffalse_2=002607F0 @ 0025EB98)
    
    ;silly.c:27: if (t == TOKENIZER_COMMA)
    ;ic:18:         iTemp5 [k11 lr17:18 so:0]{ ia0 a2p0 re0 rm0 nos0 ru0 dp0}{char fixed} = iTemp0 [k2 lr16:27 so:0]{ ia0 a2p0 re1 rm0 nos0 ru0 dp0}{unsigned-char fixed}{ sir@ _data_statement_t_1_6}[b ] == 0x2c {unsigned-char literal}
            ld      a,b
            sub     a, #0x2C
            jr      NZ,00108$
    ;ic:19:         if iTemp5 [k11 lr17:18 so:0]{ ia0 a2p0 re0 rm0 nos0 ru0 dp0}{char fixed} == 0 goto __iffalse_2($8)
    ;silly.c:28: accept_tok(t);
    ;ic:22:         push iTemp0 [k2 lr16:27 so:0]{ ia0 a2p0 re1 rm0 nos0 ru0 dp0}{unsigned-char fixed}{ sir@ _data_statement_t_1_6}[b ]{parmPush = 1}
            push    bc
            push    bc
            inc     sp
    ;ic:23:         iTemp6 [k14 lr20:20 so:0]{ ia0 a2p0 re0 rm0 nos0 ru0 dp0}{void fixed} = call _accept_tok [k12 lr0:0 so:0]{ ia0 a2p0 re0 rm0 nos0 ru0 dp0}{void function ( unsigned-char fixed) fixed}
            call    _accept_tok
            inc     sp
            pop     bc
    ;ic:24:          goto _docontinue_0($11)
            jr      00111$
    ;ic:25:  __iffalse_2($8) :
    
     
  • Maarten Brock

    Maarten Brock - 2016-01-30

    Ticket moved from /p/sdcc/bugs/2429/

    Can't be converted:

    • _category: Front-end
     
  • Maarten Brock

    Maarten Brock - 2016-01-30

    A bit late, but still I move this to feature requests because there was no bad code generated. Only a missed optimization.

     

Log in to post a comment.