1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-30 20:29:25 +00:00

Minor improvement of optimizations

git-svn-id: svn://svn.cc65.org/cc65/trunk@1440 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2002-10-04 19:27:15 +00:00
parent 89e8aae7d7
commit bc1085997d

View File

@ -44,6 +44,18 @@
/*****************************************************************************/
/* Data */
/*****************************************************************************/
/* Flags returned by DirectOp */
#define OP_DIRECT 0x01 /* Direct op may be used */
#define OP_ONSTACK 0x02 /* Operand is on stack */
/*****************************************************************************/ /*****************************************************************************/
/* Helpers */ /* Helpers */
/*****************************************************************************/ /*****************************************************************************/
@ -111,6 +123,30 @@ static unsigned AdjustStackOffset (CodeSeg* S, unsigned Start, unsigned Stop,
static unsigned DirectOp (CodeEntry* E)
/* Check if the given entry is a lda instruction with an addressing mode
* that allows us to replace it by another operation (like ora). If so, we may
* use this location for the or and must not save the value in the zero
* page location.
*/
{
unsigned Flags = 0;
if (E->OPC == OP65_LDA) {
if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
/* These insns are all ok and replaceable */
Flags |= OP_DIRECT;
} else if (E->AM == AM65_ZP_INDY &&
E->RI->In.RegY >= 0 &&
strcmp (E->Arg, "sp") == 0) {
/* Load from stack with known offset is also ok */
Flags |= (OP_DIRECT | OP_ONSTACK);
}
}
return Flags;
}
/*****************************************************************************/ /*****************************************************************************/
/* Actual optimization functions */ /* Actual optimization functions */
/*****************************************************************************/ /*****************************************************************************/
@ -209,7 +245,7 @@ static unsigned Opt_tosaddax (CodeSeg* S, unsigned Push, unsigned Add,
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* AddEntry; CodeEntry* AddEntry;
int DirectAdd; unsigned Flags;
/* We need the entry behind the add */ /* We need the entry behind the add */
@ -221,19 +257,18 @@ static unsigned Opt_tosaddax (CodeSeg* S, unsigned Push, unsigned Add,
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Check the entry before the push, if it's a lda instruction with an /* Check the entry before the push. If it's a lda instruction with an
* addressing mode that does not use an additional index register. If * addressing mode that allows us to replace it, we may use this
* so, we may use this location for the add and must not save the * location for the op and must not save the value in the zero page
* value in the zero page location. * location.
*/ */
DirectAdd = (P->OPC == OP65_LDA && Flags = DirectOp (P);
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Store the value into the zeropage instead of pushing it */ /* Store the value into the zeropage instead of pushing it */
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++Add; /* Correct the index */ ++Add; /* Correct the index */
if (!DirectAdd) { if ((Flags & OP_DIRECT) == 0) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++Add; /* Correct the index */ ++Add; /* Correct the index */
@ -245,8 +280,15 @@ static unsigned Opt_tosaddax (CodeSeg* S, unsigned Push, unsigned Add,
/* Inline the add */ /* Inline the add */
X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, AddEntry->LI); X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, AddEntry->LI);
CS_InsertEntry (S, X, Add+1); CS_InsertEntry (S, X, Add+1);
if (DirectAdd) { if ((Flags & OP_DIRECT) != 0) {
/* Add from variable location */ /* Add a variable location. If the location is on the stack, we
* need to reload the Y register.
*/
if ((Flags & OP_ONSTACK) != 0) {
X = NewCodeEntry (OP65_LDY, AM65_IMM, MakeHexArg (P->RI->In.RegY), 0, AddEntry->LI);
CS_InsertEntry (S, X, Add);
++Add;
}
X = NewCodeEntry (OP65_ADC, P->AM, P->Arg, 0, AddEntry->LI); X = NewCodeEntry (OP65_ADC, P->AM, P->Arg, 0, AddEntry->LI);
} else { } else {
/* Add from temp storage */ /* Add from temp storage */
@ -309,7 +351,7 @@ static unsigned Opt_tosandax (CodeSeg* S, unsigned Push, unsigned And,
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* AndEntry; CodeEntry* AndEntry;
int DirectAnd; unsigned Flags;
/* Get the entry before the push */ /* Get the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0); CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
@ -317,19 +359,18 @@ static unsigned Opt_tosandax (CodeSeg* S, unsigned Push, unsigned And,
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Check the entry before the push, if it's a lda instruction with an /* Check the entry before the push. If it's a lda instruction with an
* addressing mode that does not use an additional index register. If * addressing mode that allows us to replace it, we may use this
* so, we may use this location for the and and must not save the * location for the op and must not save the value in the zero page
* value in the zero page location. * location.
*/ */
DirectAnd = (P->OPC == OP65_LDA && Flags = DirectOp (P);
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Store the value into the zeropage instead of pushing it */ /* Store the value into the zeropage instead of pushing it */
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++And; /* Correct the index */ ++And; /* Correct the index */
if (!DirectAnd) { if ((Flags & OP_DIRECT) == 0) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++And; /* Correct the index */ ++And; /* Correct the index */
@ -339,8 +380,15 @@ static unsigned Opt_tosandax (CodeSeg* S, unsigned Push, unsigned And,
AndEntry = CS_GetEntry (S, And); AndEntry = CS_GetEntry (S, And);
/* Inline the and */ /* Inline the and */
if (DirectAnd) { if ((Flags & OP_DIRECT) != 0) {
/* And with variable location */ /* And with variable location. If the location is on the stack, we
* need to reload the Y register.
*/
if ((Flags & OP_ONSTACK) != 0) {
X = NewCodeEntry (OP65_LDY, AM65_IMM, MakeHexArg (P->RI->In.RegY), 0, AndEntry->LI);
CS_InsertEntry (S, X, And);
++And;
}
X = NewCodeEntry (OP65_AND, P->AM, P->Arg, 0, AndEntry->LI); X = NewCodeEntry (OP65_AND, P->AM, P->Arg, 0, AndEntry->LI);
} else { } else {
/* And with temp storage */ /* And with temp storage */
@ -383,7 +431,7 @@ static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or,
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* OrEntry; CodeEntry* OrEntry;
int DirectOr; unsigned Flags;
/* Get the entry before the push */ /* Get the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0); CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
@ -391,19 +439,18 @@ static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or,
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Check the entry before the push, if it's a lda instruction with an /* Check the entry before the push. If it's a lda instruction with an
* addressing mode that does not use an additional index register. If * addressing mode that allows us to replace it, we may use this
* so, we may use this location for the or and must not save the * location for the op and must not save the value in the zero page
* value in the zero page location. * location.
*/ */
DirectOr = (P->OPC == OP65_LDA && Flags = DirectOp (P);
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Store the value into the zeropage instead of pushing it */ /* Store the value into the zeropage instead of pushing it */
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++Or; /* Correct the index */ ++Or; /* Correct the index */
if (!DirectOr) { if ((Flags & OP_DIRECT) == 0) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++Or; /* Correct the index */ ++Or; /* Correct the index */
@ -413,10 +460,18 @@ static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or,
OrEntry = CS_GetEntry (S, Or); OrEntry = CS_GetEntry (S, Or);
/* Inline the or */ /* Inline the or */
if (DirectOr) { if ((Flags & OP_DIRECT) != 0) {
/* Or with variable location */ /* Or with variable location. If the location is on the stack, we
* need to reload the Y register.
*/
if ((Flags & OP_ONSTACK) != 0) {
X = NewCodeEntry (OP65_LDY, AM65_IMM, MakeHexArg (P->RI->In.RegY), 0, OrEntry->LI);
CS_InsertEntry (S, X, Or);
++Or;
}
X = NewCodeEntry (OP65_ORA, P->AM, P->Arg, 0, OrEntry->LI); X = NewCodeEntry (OP65_ORA, P->AM, P->Arg, 0, OrEntry->LI);
} else { } else {
/* Or with temp storage */
X = NewCodeEntry (OP65_ORA, AM65_ZP, ZPLo, 0, OrEntry->LI); X = NewCodeEntry (OP65_ORA, AM65_ZP, ZPLo, 0, OrEntry->LI);
} }
CS_InsertEntry (S, X, Or+1); CS_InsertEntry (S, X, Or+1);
@ -439,7 +494,7 @@ static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or,
CS_InsertEntry (S, X, Or+6); CS_InsertEntry (S, X, Or+6);
} }
/* Remove the push and the call to the tosandax function */ /* Remove the push and the call to the tosorax function */
CS_DelEntry (S, Or); CS_DelEntry (S, Or);
CS_DelEntry (S, Push); CS_DelEntry (S, Push);
@ -457,7 +512,7 @@ static unsigned Opt_tosxorax (CodeSeg* S, unsigned Push, unsigned Xor,
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* XorEntry; CodeEntry* XorEntry;
int DirectXor; unsigned Flags;
/* Get the entry before the push */ /* Get the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0); CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
@ -465,19 +520,18 @@ static unsigned Opt_tosxorax (CodeSeg* S, unsigned Push, unsigned Xor,
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Check the entry before the push, if it's a lda instruction with an /* Check the entry before the push. If it's a lda instruction with an
* addressing mode that does not use an additional index register. If * addressing mode that allows us to replace it, we may use this
* so, we may use this location for the xor and must not save the * location for the op and must not save the value in the zero page
* value in the zero page location. * location.
*/ */
DirectXor = (P->OPC == OP65_LDA && Flags = DirectOp (P);
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Store the value into the zeropage instead of pushing it */ /* Store the value into the zeropage instead of pushing it */
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++Xor; /* Correct the index */ ++Xor; /* Correct the index */
if (!DirectXor) { if ((Flags & OP_DIRECT) != 0) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1); CS_InsertEntry (S, X, Push+1);
++Xor; /* Correct the index */ ++Xor; /* Correct the index */
@ -487,8 +541,15 @@ static unsigned Opt_tosxorax (CodeSeg* S, unsigned Push, unsigned Xor,
XorEntry = CS_GetEntry (S, Xor); XorEntry = CS_GetEntry (S, Xor);
/* Inline the xor */ /* Inline the xor */
if (DirectXor) { if ((Flags & OP_DIRECT) != 0) {
/* Xor with variable location */ /* Xor with a variable location. If the location is on the stack, we
* need to reload the Y register.
*/
if ((Flags & OP_ONSTACK) != 0) {
X = NewCodeEntry (OP65_LDY, AM65_IMM, MakeHexArg (P->RI->In.RegY), 0, XorEntry->LI);
CS_InsertEntry (S, X, Xor);
++Xor;
}
X = NewCodeEntry (OP65_EOR, P->AM, P->Arg, 0, XorEntry->LI); X = NewCodeEntry (OP65_EOR, P->AM, P->Arg, 0, XorEntry->LI);
} else { } else {
/* Xor with temp storage */ /* Xor with temp storage */