I guess pdk rules should have some specific guards. They should not be preceded by any skip instructions.
For example most first one:
replace restart {
mo%3 %1, %2
} by {
; peephole 0 removed dead load into %1 from %2.
} if notUsed(%1), notVolatile(%1), notVolatile(%2)
on following case:
t1sn.io f, c
mov a, #0x10
mov a, #0x39
emit wrong result:
t1sn.io f, c
; peephole 0 removed dead load into a from #0x10.
mov a, #0x39
Similar issues expects in pic ports
Patches: #447
Wiki: NGI0-Entrust-SDCC
Wiki: SDCC 4.3.0 Release
That looks like a serious issue. I guess we need to fix https://sourceforge.net/p/sdcc/bugs/3515/ first, as otherwise the fix here will likely only work when --debug is not specified.
Do we have a C code sample that triggers the bug?
I can produce the issue using the following C sample and peephole rule:
I see two ways to fix this:
1) Make the peephole optimizer not match rules if the preceding instruction is a conditional skip. Ports that have such instructions would need to supply a isCondSkip helper function for this.
2) Have codegen put a label before the instruction after the conditional skip. Since the conditional skip is essentially a conditional relative jump with fixed jump distance this would just treat them the same. Requires no changes in the peephole optimizer, but more in codegen of ports that have conditional skips.
the first looks straightforwardly simple and solid. it can be applied by default unconditionally to all existing rules.
the second is more logical and graceful. to be tested
Fixed for pdk in [r13817].
Related
Commit: [r13817]