From c156b20738271dfd502c175a5007b603686f804d Mon Sep 17 00:00:00 2001 From: cuz Date: Sat, 15 Apr 2006 20:31:43 +0000 Subject: [PATCH] Added some code rewriting (may enable optimizations in a few cases). git-svn-id: svn://svn.cc65.org/cc65/trunk@3727 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/codeopt.c | 4 +++ src/cc65/coptstore.c | 68 +++++++++++++++++++++++++++++++++++++++++++- src/cc65/coptstore.h | 27 +++++++++++++++--- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index eef303e59..0a5a227cd 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -1865,6 +1865,7 @@ static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 }; static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 }; static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 }; +static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 }; static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 }; @@ -1939,6 +1940,7 @@ static OptFunc* OptFuncs[] = { &DOptStore2, &DOptStore3, &DOptStore4, + &DOptStore5, &DOptStoreLoad, &DOptSub1, &DOptSub2, @@ -2200,6 +2202,7 @@ static unsigned RunOptGroup1 (CodeSeg* S) Changes += RunOptFunc (S, &DOptAdd2, 1); Changes += RunOptFunc (S, &DOptAdd4, 1); Changes += RunOptFunc (S, &DOptStore4, 1); + Changes += RunOptFunc (S, &DOptStore5, 1); Changes += RunOptFunc (S, &DOptShift1, 1); Changes += RunOptFunc (S, &DOptShift2, 1); Changes += RunOptFunc (S, &DOptShift3, 1); @@ -2274,6 +2277,7 @@ static unsigned RunOptGroup3 (CodeSeg* S) C += RunOptFunc (S, &DOptDupLoads, 1); C += RunOptFunc (S, &DOptStoreLoad, 1); C += RunOptFunc (S, &DOptTransfers1, 1); + C += RunOptFunc (S, &DOptStore5, 1); C += RunOptFunc (S, &DOptPushPop, 1); C += RunOptFunc (S, &DOptPrecalc, 1); diff --git a/src/cc65/coptstore.c b/src/cc65/coptstore.c index b738c38db..d78810cde 100644 --- a/src/cc65/coptstore.c +++ b/src/cc65/coptstore.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2002-2005, Ullrich von Bassewitz */ +/* (C) 2002-2006, Ullrich von Bassewitz */ /* Römerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -386,3 +386,69 @@ unsigned OptStore4 (CodeSeg* S) +unsigned OptStore5 (CodeSeg* S) +/* Search for the sequence + * + * lda foo + * ldx bar + * sta something + * stx something-else + * + * and replace it by + * + * lda foo + * sta something + * lda bar + * sta something-else + * + * if X is not used later. This replacement doesn't save any cycles or bytes, + * but it keeps the value of X, which may be reused later. + */ +{ + unsigned Changes = 0; + + /* Walk over the entries */ + unsigned I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* L[4]; + + /* Get next entry */ + L[0] = CS_GetEntry (S, I); + + /* Check for the sequence */ + if (L[0]->OPC == OP65_LDA && + !CS_RangeHasLabel (S, I+1, 3) && + CS_GetEntries (S, L+1, I+1, 3) && + L[1]->OPC == OP65_LDX && + L[2]->OPC == OP65_STA && + L[3]->OPC == OP65_STX && + !RegXUsed (S, I+4)) { + + CodeEntry* X; + + /* Insert the code after the sequence */ + X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI); + CS_InsertEntry (S, X, I+4); + X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI); + CS_InsertEntry (S, X, I+5); + + /* Delete the old code */ + CS_DelEntry (S, I+3); + CS_DelEntry (S, I+1); + + /* Remember, we had changes */ + ++Changes; + } + + /* Next entry */ + ++I; + + } + + /* Return the number of changes made */ + return Changes; +} + + + diff --git a/src/cc65/coptstore.h b/src/cc65/coptstore.h index 85042279f..b6ac18353 100644 --- a/src/cc65/coptstore.h +++ b/src/cc65/coptstore.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002-2003 Ullrich von Bassewitz */ -/* Römerstrasse 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 2002-2006, Ullrich von Bassewitz */ +/* Römerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -85,6 +85,25 @@ unsigned OptStore4 (CodeSeg* S); * from the load. */ +unsigned OptStore5 (CodeSeg* S); +/* Search for the sequence + * + * lda foo + * ldx bar + * sta something + * stx something-else + * + * and replace it by + * + * lda foo + * sta something + * lda bar + * sta something-else + * + * if X is not used later. This replacement doesn't save any cycles or bytes, + * but it keeps the value of X, which may be reused later. + */ + /* End of coptstore.h */ -- 2.39.5