From c0a1b1b887af536612987847c9344a1226bf4397 Mon Sep 17 00:00:00 2001 From: Kugel Fuhr <98353208+kugelfuhr@users.noreply.github.com> Date: Tue, 24 Jun 2025 18:02:24 +0200 Subject: [PATCH] Add an optimization step that removes compare instructions preceeding an RTS. Since nothing is passed back in the flags, these instructions have no effect. Fixes #2025. --- src/cc65/codeopt.c | 3 +++ src/cc65/coptcmp.c | 37 +++++++++++++++++++++++++++++++++++++ src/cc65/coptcmp.h | 5 +++++ 3 files changed, 45 insertions(+) diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index 19acfde74..f57417526 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -136,6 +136,7 @@ static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 }; static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 }; static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 }; +static OptFunc DOptCmp10 = { OptCmp10, "OptCmp10", 33, 0, 0, 0, 0, 0 }; static OptFunc DOptComplAX1 = { OptComplAX1, "OptComplAX1", 65, 0, 0, 0, 0, 0 }; static OptFunc DOptCondBranch1 = { OptCondBranch1, "OptCondBranch1", 80, 0, 0, 0, 0, 0 }; static OptFunc DOptCondBranch2 = { OptCondBranch2, "OptCondBranch2", 40, 0, 0, 0, 0, 0 }; @@ -251,6 +252,7 @@ static OptFunc* OptFuncs[] = { &DOptBranchDist, &DOptBranchDist2, &DOptCmp1, + &DOptCmp10, &DOptCmp2, &DOptCmp3, &DOptCmp4, @@ -729,6 +731,7 @@ static unsigned RunOptGroup3 (CodeSeg* S) C += RunOptFunc (S, &DOptCondBranch3, 1); C += RunOptFunc (S, &DOptCondBranchC, 1); C += RunOptFunc (S, &DOptRTSJumps1, 1); + C += RunOptFunc (S, &DOptCmp10, 1); /* After OptRTSJumps1 */ C += RunOptFunc (S, &DOptBoolCmp, 1); C += RunOptFunc (S, &DOptBoolTrans, 1); C += RunOptFunc (S, &DOptBNegA2, 1); /* After OptCondBranch's */ diff --git a/src/cc65/coptcmp.c b/src/cc65/coptcmp.c index 2970b363b..25499dc3c 100644 --- a/src/cc65/coptcmp.c +++ b/src/cc65/coptcmp.c @@ -741,3 +741,40 @@ unsigned OptCmp9 (CodeSeg* S) /* Return the number of changes made */ return Changes; } + + + +unsigned OptCmp10 (CodeSeg* S) +/* Remove compare instructions before an RTS. This is safe since no C function +** passes back something in the flags. +*/ +{ + unsigned Changes = 0; + unsigned I; + + /* Walk over the entries */ + I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* N; + + /* Get next entry */ + CodeEntry* E = CS_GetEntry (S, I); + + /* Check for a compare followed by an RTS */ + if ((E->Info & OF_CMP) != 0 && /* Compare insn */ + (N = CS_GetNextEntry (S, I)) != 0 && /* Next entry ... */ + N->OPC == OP65_RTS) { /* ... is RTS */ + + /* Found, remove the compare */ + CS_DelEntry (S, I); + ++Changes; + } + + /* Next entry */ + ++I; + } + + /* Return the number of changes made */ + return Changes; +} diff --git a/src/cc65/coptcmp.h b/src/cc65/coptcmp.h index dd188f7fc..ff58d37df 100644 --- a/src/cc65/coptcmp.h +++ b/src/cc65/coptcmp.h @@ -146,6 +146,11 @@ unsigned OptCmp9 (CodeSeg* S); ** flag instead of the carry flag and remove the asl. */ +unsigned OptCmp10 (CodeSeg* S); +/* Remove compare instructions before an RTS. This is safe since no C function +** passes back something in the flags. +*/ + /* End of coptcmp.h */