From c437afd62b9e0b532ea8fb5cef7e72a7d1edcc41 Mon Sep 17 00:00:00 2001 From: uz Date: Mon, 26 Oct 2009 10:17:53 +0000 Subject: [PATCH] Added another optimization step. git-svn-id: svn://svn.cc65.org/cc65/trunk@4391 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/codeopt.c | 7 +++-- src/cc65/copttest.c | 67 +++++++++++++++++++++++++++++++++++++-------- src/cc65/copttest.h | 14 +++++++--- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index cce8c1637..aef563973 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -1375,7 +1375,8 @@ static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 }; -static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 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 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 }; @@ -1467,6 +1468,7 @@ static OptFunc* OptFuncs[] = { &DOptSub2, &DOptSub3, &DOptTest1, + &DOptTest2, &DOptTransfers1, &DOptTransfers2, &DOptTransfers3, @@ -1865,10 +1867,11 @@ static unsigned RunOptGroup5 (CodeSeg* S) { unsigned Changes = 0; + /* Repeat some of the steps here */ Changes += RunOptFunc (S, &DOptPush1, 1); Changes += RunOptFunc (S, &DOptPush2, 1); - /* Repeat some of the other optimizations now */ Changes += RunOptFunc (S, &DOptUnusedLoads, 1); + Changes += RunOptFunc (S, &DOptTest2, 1); Changes += RunOptFunc (S, &DOptTransfers2, 1); /* Return the number of changes */ diff --git a/src/cc65/copttest.c b/src/cc65/copttest.c index 7d41b3989..1ba2182c2 100644 --- a/src/cc65/copttest.c +++ b/src/cc65/copttest.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@cc65.org */ +/* (C) 2001-2009, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -94,15 +94,15 @@ unsigned OptTest1 (CodeSeg* S) /* Check if X is zero */ if (L[0]->RI->In.RegX == 0) { - /* Insert the compare */ - CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI); - CS_InsertEntry (S, N, I+2); + /* Insert the compare */ + CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI); + CS_InsertEntry (S, N, I+2); - /* Remove the two other insns */ - CS_DelEntry (S, I+1); - CS_DelEntry (S, I); + /* Remove the two other insns */ + CS_DelEntry (S, I+1); + CS_DelEntry (S, I); - /* We had changes */ + /* We had changes */ ++Changes; /* Check if A is zero */ @@ -135,3 +135,48 @@ unsigned OptTest1 (CodeSeg* S) +unsigned OptTest2 (CodeSeg* S) +/* Search for an inc/dec operation followed by a load and a conditional + * branch based on the flags from the load. Remove the load if the insn + * isn't used later. + */ +{ + unsigned Changes = 0; + + /* Walk over the entries */ + unsigned I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* L[3]; + + /* Get next entry */ + L[0] = CS_GetEntry (S, I); + + /* Check if it's the sequence we're searching for */ + if ((L[0]->OPC == OP65_INC || L[0]->OPC == OP65_DEC) && + CS_GetEntries (S, L+1, I+1, 2) && + !CE_HasLabel (L[1]) && + (L[1]->Info & OF_LOAD) != 0 && + (L[2]->Info & OF_FBRA) != 0 && + L[1]->AM == L[0]->AM && + strcmp (L[0]->Arg, L[1]->Arg) == 0 && + (GetRegInfo (S, I+2, L[1]->Chg) & L[1]->Chg) == 0) { + + printf ("Deleting\n"); + + /* Remove the load */ + CS_DelEntry (S, I+1); + ++Changes; + } + + /* Next entry */ + ++I; + + } + + /* Return the number of changes made */ + return Changes; +} + + + diff --git a/src/cc65/copttest.h b/src/cc65/copttest.h index 0a0af4add..e6e409e89 100644 --- a/src/cc65/copttest.h +++ b/src/cc65/copttest.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@cc65.org */ +/* (C) 2001-2009, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -70,6 +70,12 @@ unsigned OptTest1 (CodeSeg* S); * */ +unsigned OptTest2 (CodeSeg* S); +/* Search for an inc/dec operation followed by a load and a conditional + * branch based on the flags from the load. Remove the load if the insn + * isn't used later. + */ + /* End of copttest.h */ -- 2.39.5