STM8. Not very good code generation.
Source code:
static void _delay(unsigned char ticks)
{
while(--ticks);
}
Generated code:
__delay:
00101$:
dec a
tnz a
jrne 00101$
ret
TNZ instruction is not needed because DEC already sets N flag.
I am surprised that such fairly old compiler generates ineffective code for such fairly old CPU.
I mean Z flag.
In current trunk, I see codegen generate the tnz, but the peephole optimizer optimizes it out, so the final result is:
Which version of sdcc did you use?
Hi,
I used sdcc-win64 4.4.0 rc3. Options: --opt-code-speed or --opt-code-size
Another one:
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02 TD- 4.4.0 #14620 (Linux)
sdcc -mstm8 --opt-code-speed test01.c
Looks fine in current trunk to me, too.
There were some stm8 peephole optimizer fixes in early March this year. Maybe one of them helped here.
Another interesting case:
the difference of loop code between these two functions.
When using stronger optimization (I tried with --max-allocs-per-node 100000), I get this using sdcc from trunk:
OK, thanks.
ld a, (0x06, sp)
ld (0x01, sp), a - saving copy of <size> on stack looks unnecessary to me.</size>
Yes. I just introduced an optimization for this in [r14867]. It is still very basic (only works for some simple cases and only for stm8), for your first function, I now get:
Related
Commit: [r14867]
Looks like current trunk can generate good enough code for this, so I'm closing the ticket.