From 0647cb11120381e3ec892f199a77f9524aebcef2 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Sun, 20 Jul 2025 12:54:09 +0200 Subject: [PATCH] Merge jsr pushax/j?? popax into nothing or RTS --- src/cc65/codeopt.c | 5 ++++- src/cc65/coptmisc.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/cc65/coptmisc.h | 3 ++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index 728e76d88..326fe8d48 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -218,6 +218,7 @@ static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 }; static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 }; static OptFunc DOptTosLoadPop = { OptTosLoadPop, "OptTosLoadPop", 50, 0, 0, 0, 0, 0 }; +static OptFunc DOptTosPushPop = { OptTosPushPop, "OptTosPushPop", 33, 0, 0, 0, 0, 0 }; static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 }; static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 }; static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 }; @@ -344,11 +345,12 @@ static OptFunc* OptFuncs[] = { &DOptSub3, &DOptTest1, &DOptTest2, + &DOptTosLoadPop, + &DOptTosPushPop, &DOptTransfers1, &DOptTransfers2, &DOptTransfers3, &DOptTransfers4, - &DOptTosLoadPop, &DOptUnusedLoads, &DOptUnusedStores, /* END SORTED_CODEOPT.SH */ @@ -924,6 +926,7 @@ static unsigned RunOptGroup7 (CodeSeg* S) /* Re-optimize JSR/RTS that may now be grouped */ C += RunOptFunc (S, &DOptRTS, 1); C += RunOptFunc (S, &DOptTosLoadPop, 5); + C += RunOptFunc (S, &DOptTosPushPop, 5); Changes += C; /* If we had changes, we must run dead code elimination again, diff --git a/src/cc65/coptmisc.c b/src/cc65/coptmisc.c index 0110dae4e..0e3dd0d56 100644 --- a/src/cc65/coptmisc.c +++ b/src/cc65/coptmisc.c @@ -1201,3 +1201,54 @@ unsigned OptBinOps2 (CodeSeg* S) /* Return the number of changes made */ return Changes; } + + +unsigned OptTosPushPop(CodeSeg *S) +/* Merge jsr pushax/j?? popax */ +{ + unsigned Changes = 0; + unsigned I; + + /* Walk over the entries */ + I = 0; + while (I < CS_GetEntryCount (S)) { + + const CodeEntry* N; + + /* Get the next entry */ + const CodeEntry* E = CS_GetEntry (S, I); + + /* Check for decspn, incspn, subysp or addysp */ + if (E->OPC == OP65_JSR && + strcmp(E->Arg, "pushax") == 0 && + (N = CS_GetNextEntry (S, I)) != 0 && + (N->OPC == OP65_JSR || N->OPC == OP65_JMP) && + strcmp(N->Arg, "popax") == 0 && + !CE_HasLabel (N)) { + + /* Delete the old code */ + CS_DelEntries (S, I, 2); + + /* Insert an rts if jmp popax */ + if (N->OPC == OP65_JMP) { + CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI); + CS_InsertEntry (S, X, I); + } + + /* Regenerate register info */ + CS_GenRegInfo (S); + + /* Remember we had changes */ + ++Changes; + + } else { + + /* Next entry */ + ++I; + } + + } + + /* Return the number of changes made */ + return Changes; +} diff --git a/src/cc65/coptmisc.h b/src/cc65/coptmisc.h index ea678ea67..64c310d20 100644 --- a/src/cc65/coptmisc.h +++ b/src/cc65/coptmisc.h @@ -134,7 +134,8 @@ unsigned OptBinOps2 (CodeSeg* S); unsigned OptTosLoadPop (CodeSeg* S); /* Merge jsr ldax0sp / jsr|jmp incsp2 into jsr|jmp popax */ - +unsigned OptTosPushPop(CodeSeg *S); +/* Merge jsr pushax/j?? popax */ /* End of coptmisc.h */