cc65: cc65: Regression in code optimization

The following code is missing an optimization when compiled with the current cc65 version compared to 2.19:

int foo(unsigned char bar, unsigned char baz)
{
    return (bar == 0 || baz == 0);
}

This is what cc65 2.19 generates with -Oirs:

        jsr     pusha
        ldy     #$01
        ldx     #$00
        lda     (sp),y
        beq     L0005
        lda     (sp,x)
        beq     L0005
        txa
        jmp     incsp2          ; <--
L0005:  tya
        jmp     incsp2

And this is what the current version generates:

        jsr     pusha
        ldy     #$01
        ldx     #$00
        lda     (sp),y
        beq     L0004
        lda     (sp,x)
        beq     L0004
        txa
        jmp     L0001            ; <--
L0004:  tya
L0001:  jmp     incsp2

The pattern is quite common. It is mostly a speed optimization but there are also cases in which the final jump can be removed, in which case it does also shrink the size of the generated code.

Let me know if you need more information.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@kugelfuhr how did you even spot this, btw?

Easy. I looked at the generated code.

I might try to add some tests for the optimization passes - not sure how exactly that could work right now though ๐Ÿ˜ƒ

sim65 is able to count cycles. So for each test program in your nightly tests, record the number of total cycles it needs to run. Then, flag the test result for manual review if the number of cycles increases. If it decreases, use this number as the new nominal value. Needs some work but technically itโ€™s quite easy.