1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 23:29:39 +00:00

Merge pull request #2260 from colinleroy/optimize-substraction

Optimize integer decrements by 1
This commit is contained in:
Bob Andrews 2023-11-28 14:40:51 +01:00 committed by GitHub
commit 86b09f56a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 305 additions and 17 deletions

View File

@ -2000,25 +2000,55 @@ void g_subeqstatic (unsigned flags, uintptr_t label, long offs,
case CF_INT:
if (flags & CF_CONST) {
AddCodeLine ("lda %s", lbuf);
AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (unsigned char)val);
AddCodeLine ("sta %s", lbuf);
if (val < 0x100) {
unsigned L = GetLocalLabel ();
AddCodeLine ("bcs %s", LocalLabelName (L));
AddCodeLine ("dec %s+1", lbuf);
g_defcodelabel (L);
if (val == 1) {
unsigned L = GetLocalLabel();
if ((flags & CF_NOKEEP) == 0) {
AddCodeLine ("ldx %s+1", lbuf);
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+1", lbuf);
AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
AddCodeLine ("sta %s+1", lbuf);
if ((flags & CF_NOKEEP) == 0) {
AddCodeLine ("tax");
AddCodeLine ("lda %s", lbuf);
AddCodeLine ("lda %s", lbuf);
AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (unsigned char)val);
AddCodeLine ("sta %s", lbuf);
if (val < 0x100) {
unsigned L = GetLocalLabel ();
AddCodeLine ("bcs %s", LocalLabelName (L));
AddCodeLine ("dec %s+1", lbuf);
g_defcodelabel (L);
if ((flags & CF_NOKEEP) == 0) {
AddCodeLine ("ldx %s+1", lbuf);
}
} else {
AddCodeLine ("lda %s+1", lbuf);
AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
AddCodeLine ("sta %s+1", lbuf);
if ((flags & CF_NOKEEP) == 0) {
AddCodeLine ("tax");
AddCodeLine ("lda %s", lbuf);
}
}
}
} else {
@ -3693,7 +3723,13 @@ void g_dec (unsigned flags, unsigned long val)
} else {
/* Inline the code */
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();
AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (unsigned char) val);

View File

@ -197,6 +197,7 @@ static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 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 DOptLoadStoreLoad= { OptLoadStoreLoad,"OptLoadStoreLoad", 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 };
static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
@ -307,6 +308,7 @@ static OptFunc* OptFuncs[] = {
&DOptStore4,
&DOptStore5,
&DOptStoreLoad,
&DOptLoadStoreLoad,
&DOptSub1,
&DOptSub2,
&DOptSub3,
@ -723,6 +725,7 @@ static unsigned RunOptGroup3 (CodeSeg* S)
C += RunOptFunc (S, &DOptUnusedStores, 1);
C += RunOptFunc (S, &DOptDupLoads, 1);
C += RunOptFunc (S, &DOptStoreLoad, 1);
C += RunOptFunc (S, &DOptLoadStoreLoad, 1);
C += RunOptFunc (S, &DOptTransfers1, 1);
C += RunOptFunc (S, &DOptTransfers3, 1);
C += RunOptFunc (S, &DOptTransfers4, 1);

View File

@ -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)
/* Remove transfers from one register to another and back */
{

View File

@ -64,6 +64,9 @@ unsigned OptDupLoads (CodeSeg* S);
unsigned OptStoreLoad (CodeSeg* S);
/* 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);
/* Remove transfers from one register to another and back */

192
test/val/sub3.c Normal file
View 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;
}