Merge pull request #2260 from colinleroy/optimize-substraction
Optimize integer decrements by 1
This commit is contained in:
@@ -2000,6 +2000,35 @@ void g_subeqstatic (unsigned flags, uintptr_t label, long offs,
|
|||||||
|
|
||||||
case CF_INT:
|
case CF_INT:
|
||||||
if (flags & CF_CONST) {
|
if (flags & CF_CONST) {
|
||||||
|
if (val == 1) {
|
||||||
|
unsigned L = GetLocalLabel();
|
||||||
|
if ((flags & CF_NOKEEP) == 0) {
|
||||||
|
if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0) {
|
||||||
|
AddCodeLine ("lda %s", lbuf);
|
||||||
|
AddCodeLine ("bne %s", LocalLabelName (L));
|
||||||
|
AddCodeLine ("dec %s+1", lbuf);
|
||||||
|
g_defcodelabel (L);
|
||||||
|
AddCodeLine ("dea");
|
||||||
|
AddCodeLine ("sta %s", lbuf);
|
||||||
|
AddCodeLine ("ldx %s+1", lbuf);
|
||||||
|
} else {
|
||||||
|
AddCodeLine ("ldx %s", lbuf);
|
||||||
|
AddCodeLine ("bne %s", LocalLabelName (L));
|
||||||
|
AddCodeLine ("dec %s+1", lbuf);
|
||||||
|
g_defcodelabel (L);
|
||||||
|
AddCodeLine ("dex");
|
||||||
|
AddCodeLine ("stx %s", lbuf);
|
||||||
|
AddCodeLine ("txa");
|
||||||
|
AddCodeLine ("ldx %s+1", lbuf);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AddCodeLine ("lda %s", lbuf);
|
||||||
|
AddCodeLine ("bne %s", LocalLabelName (L));
|
||||||
|
AddCodeLine ("dec %s+1", lbuf);
|
||||||
|
g_defcodelabel (L);
|
||||||
|
AddCodeLine ("dec %s", lbuf);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
AddCodeLine ("lda %s", lbuf);
|
AddCodeLine ("lda %s", lbuf);
|
||||||
AddCodeLine ("sec");
|
AddCodeLine ("sec");
|
||||||
AddCodeLine ("sbc #$%02X", (unsigned char)val);
|
AddCodeLine ("sbc #$%02X", (unsigned char)val);
|
||||||
@@ -2021,6 +2050,7 @@ void g_subeqstatic (unsigned flags, uintptr_t label, long offs,
|
|||||||
AddCodeLine ("lda %s", lbuf);
|
AddCodeLine ("lda %s", lbuf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
AddCodeLine ("eor #$FF");
|
AddCodeLine ("eor #$FF");
|
||||||
AddCodeLine ("sec");
|
AddCodeLine ("sec");
|
||||||
@@ -3693,7 +3723,13 @@ void g_dec (unsigned flags, unsigned long val)
|
|||||||
} else {
|
} else {
|
||||||
/* Inline the code */
|
/* Inline the code */
|
||||||
if (val < 0x300) {
|
if (val < 0x300) {
|
||||||
if ((val & 0xFF) != 0) {
|
if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
|
||||||
|
unsigned L = GetLocalLabel();
|
||||||
|
AddCodeLine ("bne %s", LocalLabelName (L));
|
||||||
|
AddCodeLine ("dex");
|
||||||
|
g_defcodelabel (L);
|
||||||
|
AddCodeLine ("dea");
|
||||||
|
} else if ((val & 0xFF) != 0) {
|
||||||
unsigned L = GetLocalLabel();
|
unsigned L = GetLocalLabel();
|
||||||
AddCodeLine ("sec");
|
AddCodeLine ("sec");
|
||||||
AddCodeLine ("sbc #$%02X", (unsigned char) val);
|
AddCodeLine ("sbc #$%02X", (unsigned char) val);
|
||||||
|
|||||||
@@ -197,6 +197,7 @@ static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0,
|
|||||||
static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 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 DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
|
||||||
static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
|
static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
|
||||||
|
static OptFunc DOptLoadStoreLoad= { OptLoadStoreLoad,"OptLoadStoreLoad", 0, 0, 0, 0, 0, 0 };
|
||||||
static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 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 };
|
static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
|
||||||
static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
|
static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
|
||||||
@@ -307,6 +308,7 @@ static OptFunc* OptFuncs[] = {
|
|||||||
&DOptStore4,
|
&DOptStore4,
|
||||||
&DOptStore5,
|
&DOptStore5,
|
||||||
&DOptStoreLoad,
|
&DOptStoreLoad,
|
||||||
|
&DOptLoadStoreLoad,
|
||||||
&DOptSub1,
|
&DOptSub1,
|
||||||
&DOptSub2,
|
&DOptSub2,
|
||||||
&DOptSub3,
|
&DOptSub3,
|
||||||
@@ -723,6 +725,7 @@ static unsigned RunOptGroup3 (CodeSeg* S)
|
|||||||
C += RunOptFunc (S, &DOptUnusedStores, 1);
|
C += RunOptFunc (S, &DOptUnusedStores, 1);
|
||||||
C += RunOptFunc (S, &DOptDupLoads, 1);
|
C += RunOptFunc (S, &DOptDupLoads, 1);
|
||||||
C += RunOptFunc (S, &DOptStoreLoad, 1);
|
C += RunOptFunc (S, &DOptStoreLoad, 1);
|
||||||
|
C += RunOptFunc (S, &DOptLoadStoreLoad, 1);
|
||||||
C += RunOptFunc (S, &DOptTransfers1, 1);
|
C += RunOptFunc (S, &DOptTransfers1, 1);
|
||||||
C += RunOptFunc (S, &DOptTransfers3, 1);
|
C += RunOptFunc (S, &DOptTransfers3, 1);
|
||||||
C += RunOptFunc (S, &DOptTransfers4, 1);
|
C += RunOptFunc (S, &DOptTransfers4, 1);
|
||||||
|
|||||||
@@ -581,6 +581,60 @@ unsigned OptStoreLoad (CodeSeg* S)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
unsigned OptLoadStoreLoad (CodeSeg* S)
|
||||||
|
/* Search for the sequence
|
||||||
|
**
|
||||||
|
** ld. xx
|
||||||
|
** st. yy
|
||||||
|
** ld. xx
|
||||||
|
**
|
||||||
|
** and remove the useless load.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
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 for the sequence */
|
||||||
|
if ((L[0]->OPC == OP65_LDA ||
|
||||||
|
L[0]->OPC == OP65_LDX ||
|
||||||
|
L[0]->OPC == OP65_LDY) &&
|
||||||
|
(L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
|
||||||
|
!CS_RangeHasLabel (S, I+1, 3) &&
|
||||||
|
CS_GetEntries (S, L+1, I+1, 2) &&
|
||||||
|
(L[1]->OPC == OP65_STA ||
|
||||||
|
L[1]->OPC == OP65_STX ||
|
||||||
|
L[1]->OPC == OP65_STY) &&
|
||||||
|
L[2]->OPC == L[0]->OPC &&
|
||||||
|
L[2]->AM == L[0]->AM &&
|
||||||
|
strcmp (L[0]->Arg, L[2]->Arg) == 0) {
|
||||||
|
|
||||||
|
/* Remove the second load */
|
||||||
|
CS_DelEntries (S, I+2, 1);
|
||||||
|
|
||||||
|
/* Remember, we had changes */
|
||||||
|
++Changes;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Next entry */
|
||||||
|
++I;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return the number of changes made */
|
||||||
|
return Changes;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned OptTransfers1 (CodeSeg* S)
|
unsigned OptTransfers1 (CodeSeg* S)
|
||||||
/* Remove transfers from one register to another and back */
|
/* Remove transfers from one register to another and back */
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -64,6 +64,9 @@ unsigned OptDupLoads (CodeSeg* S);
|
|||||||
unsigned OptStoreLoad (CodeSeg* S);
|
unsigned OptStoreLoad (CodeSeg* S);
|
||||||
/* Remove a store followed by a load from the same location. */
|
/* Remove a store followed by a load from the same location. */
|
||||||
|
|
||||||
|
unsigned OptLoadStoreLoad (CodeSeg* S);
|
||||||
|
/* Remove a load, store followed by a reload of the same location. */
|
||||||
|
|
||||||
unsigned OptTransfers1 (CodeSeg* S);
|
unsigned OptTransfers1 (CodeSeg* S);
|
||||||
/* Remove transfers from one register to another and back */
|
/* Remove transfers from one register to another and back */
|
||||||
|
|
||||||
|
|||||||
192
test/val/sub3.c
Normal file
192
test/val/sub3.c
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
/*
|
||||||
|
!!DESCRIPTION!! Subtraction Test
|
||||||
|
!!ORIGIN!! SDCC regression tests
|
||||||
|
!!LICENCE!! GPL, read COPYING.GPL
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
unsigned char failures=0;
|
||||||
|
|
||||||
|
int int0 = 5;
|
||||||
|
unsigned int int1 = 5;
|
||||||
|
|
||||||
|
void pre_dec_test(void)
|
||||||
|
{
|
||||||
|
if(int0 != 5)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 5)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
--int0;
|
||||||
|
--int1;
|
||||||
|
|
||||||
|
if(int0 != 4)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 4)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
--int0;
|
||||||
|
--int1;
|
||||||
|
--int0;
|
||||||
|
--int1;
|
||||||
|
--int0;
|
||||||
|
--int1;
|
||||||
|
--int0;
|
||||||
|
--int1;
|
||||||
|
|
||||||
|
if(int0 != 0)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 0)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
--int0;
|
||||||
|
--int1;
|
||||||
|
|
||||||
|
if(int0 != -1)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 65535U)
|
||||||
|
failures++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void post_dec_test(void)
|
||||||
|
{
|
||||||
|
if(int0 != 5)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 5)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
int0--;
|
||||||
|
int1--;
|
||||||
|
|
||||||
|
if(int0 != 4)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 4)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
int0--;
|
||||||
|
int1--;
|
||||||
|
int0--;
|
||||||
|
int1--;
|
||||||
|
int0--;
|
||||||
|
int1--;
|
||||||
|
int0--;
|
||||||
|
int1--;
|
||||||
|
|
||||||
|
if(int0 != 0)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 0)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
int0--;
|
||||||
|
int1--;
|
||||||
|
|
||||||
|
if(int0 != -1)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 65535U)
|
||||||
|
failures++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pre_dec_assign_test(void)
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
unsigned int b;
|
||||||
|
if(int0 != 5)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 5)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
a = --int0;
|
||||||
|
b = --int1;
|
||||||
|
|
||||||
|
if(int0 != 4 || a != int0)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 4 || b != int1)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
a = --int0;
|
||||||
|
b = --int1;
|
||||||
|
a = --int0;
|
||||||
|
b = --int1;
|
||||||
|
a = --int0;
|
||||||
|
b = --int1;
|
||||||
|
a = --int0;
|
||||||
|
b = --int1;
|
||||||
|
|
||||||
|
if(int0 != 0 || a != int0)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 0 || b != int1)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
a = --int0;
|
||||||
|
b = --int1;
|
||||||
|
|
||||||
|
if(int0 != -1 || a != int0)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 65535U || b != int1)
|
||||||
|
failures++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void post_dec_assign_test(void)
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
unsigned int b;
|
||||||
|
if(int0 != 5)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 5)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
a = int0--;
|
||||||
|
b = int1--;
|
||||||
|
|
||||||
|
if(int0 != 4 || a != 5)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 4 || b != 5)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
a = int0--;
|
||||||
|
b = int1--;
|
||||||
|
a = int0--;
|
||||||
|
b = int1--;
|
||||||
|
a = int0--;
|
||||||
|
b = int1--;
|
||||||
|
a = int0--;
|
||||||
|
b = int1--;
|
||||||
|
|
||||||
|
if(int0 != 0 || a != 1)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 0 || b != 1)
|
||||||
|
failures++;
|
||||||
|
|
||||||
|
a = int0--;
|
||||||
|
b = int1--;
|
||||||
|
|
||||||
|
if(int0 != -1 || a != 0)
|
||||||
|
failures++;
|
||||||
|
if(int1 != 65535U || b != 0)
|
||||||
|
failures++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int0 = 5;
|
||||||
|
int1 = 5;
|
||||||
|
pre_dec_test();
|
||||||
|
|
||||||
|
int0 = 5;
|
||||||
|
int1 = 5;
|
||||||
|
post_dec_test();
|
||||||
|
|
||||||
|
int0 = 5;
|
||||||
|
int1 = 5;
|
||||||
|
pre_dec_assign_test();
|
||||||
|
|
||||||
|
int0 = 5;
|
||||||
|
int1 = 5;
|
||||||
|
post_dec_assign_test();
|
||||||
|
|
||||||
|
printf("failures: %d\n",failures);
|
||||||
|
|
||||||
|
return failures;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user