1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-02 04:41:35 +00:00

Fixed a signed char shift optimization so that it won't be used on signed int also.

(It would lose significant bits from the high byte.)
This commit is contained in:
Greg King 2015-12-13 07:17:41 -05:00
parent 377f31d085
commit 4716083f3f
2 changed files with 43 additions and 24 deletions

View File

@ -307,51 +307,63 @@ NextEntry:
unsigned OptShift2(CodeSeg* S) unsigned OptShift2 (CodeSeg* S)
/* A call to the asrax1 routines may get replaced by something simpler, if /* The sequence
** X is not used later: **
** bpl L
** dex
** L: jsr asraxN
**
** might be replaced by N copies of
** **
** cmp #$80 ** cmp #$80
** ror a ** ror a
**
** if X is not used later (X is assumed to be zero on entry).
*/ */
{ {
unsigned Changes = 0; unsigned Changes = 0;
unsigned I; unsigned I = 0;
/* Walk over the entries */ /* Walk over the entries */
I = 0;
while (I < CS_GetEntryCount (S)) { while (I < CS_GetEntryCount (S)) {
unsigned Shift; unsigned Shift;
unsigned Count; unsigned Count;
CodeEntry* L[3];
/* Get next entry */ /* Get next entry */
CodeEntry* E = CS_GetEntry (S, I); L[0] = CS_GetEntry (S, I);
/* Check for the sequence */ /* Check for the sequence */
if (E->OPC == OP65_JSR && if (L[0]->OPC == OP65_BPL &&
(Shift = GetShift (E->Arg)) != SHIFT_NONE && L[0]->JumpTo != 0 &&
SHIFT_TYPE (Shift) == SHIFT_TYPE_ASR && CS_GetEntries (S, L+1, I+1, 2) &&
(Count = SHIFT_COUNT (Shift)) > 0 && L[1]->OPC == OP65_DEX &&
Count * 100 <= S->CodeSizeFactor && L[0]->JumpTo->Owner == L[2] &&
!RegXUsed (S, I+1)) { !CS_RangeHasLabel (S, I, 2) &&
L[2]->OPC == OP65_JSR &&
SHIFT_TYPE (Shift = GetShift (L[2]->Arg)) == SHIFT_TYPE_ASR &&
(Count = SHIFT_COUNT (Shift)) > 0 &&
Count * 100 <= S->CodeSizeFactor &&
!RegXUsed (S, I+3)) {
CodeEntry* X; CodeEntry* X;
unsigned J = I+1; unsigned J = I+2;
/* Generate the replacement sequence */ /* Generate the replacement sequence */
while (Count--) { while (Count--) {
/* cmp #$80 */ /* cmp #$80 */
X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, E->LI); X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
CS_InsertEntry (S, X, J++); CS_InsertEntry (S, X, ++J);
/* ror a */ /* ror a */
X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, E->LI); X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
CS_InsertEntry (S, X, J++); CS_InsertEntry (S, X, ++J);
} }
/* Delete the call to asrax */ /* Remove the bpl/dex/jsr */
CS_DelEntry (S, I); CS_DelEntries (S, I, 3);
/* Remember, we had changes */ /* Remember, we had changes */
++Changes; ++Changes;
@ -412,7 +424,7 @@ unsigned OptShift3 (CodeSeg* S)
(Shift = GetShift (L[2]->Arg)) != SHIFT_NONE && (Shift = GetShift (L[2]->Arg)) != SHIFT_NONE &&
SHIFT_DIR (Shift) == SHIFT_DIR_RIGHT && SHIFT_DIR (Shift) == SHIFT_DIR_RIGHT &&
(Count = SHIFT_COUNT (Shift)) > 0) { (Count = SHIFT_COUNT (Shift)) > 0) {
/* Add the replacement insn instead */ /* Add the replacement insn instead */
CodeEntry* X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI); CodeEntry* X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
CS_InsertEntry (S, X, I+3); CS_InsertEntry (S, X, I+3);
@ -421,7 +433,7 @@ unsigned OptShift3 (CodeSeg* S)
CS_InsertEntry (S, X, I+4); CS_InsertEntry (S, X, I+4);
} }
/* Remove the bcs/dex/jsr */ /* Remove the bcc/inx/jsr */
CS_DelEntries (S, I, 3); CS_DelEntries (S, I, 3);
/* Remember, we had changes */ /* Remember, we had changes */

View File

@ -60,12 +60,19 @@ unsigned OptShift1 (CodeSeg* S);
** L1: ** L1:
*/ */
unsigned OptShift2(CodeSeg* S); unsigned OptShift2 (CodeSeg* S);
/* A call to the asrax1 routines may get replaced by something simpler, if /* The sequence
** X is not used later: **
** bpl L
** dex
** L: jsr asraxN
**
** might be replaced by N copies of
** **
** cmp #$80 ** cmp #$80
** ror a ** ror a
**
** if X is not used later (X is assumed to be zero on entry).
*/ */
unsigned OptShift3 (CodeSeg* S); unsigned OptShift3 (CodeSeg* S);