1 /*****************************************************************************/
9 /* (C) 2001-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
42 #include "coptshift.h"
46 /*****************************************************************************/
48 /*****************************************************************************/
52 /* Shift types. Shift type is in the first byte, shift count in the second */
57 SHIFT_MASK_COUNT = 0x00FF,
58 SHIFT_MASK_DIR = 0x0F00,
59 SHIFT_MASK_MODE = 0xF000, /* Arithmetic or logical */
60 SHIFT_MASK_TYPE = SHIFT_MASK_DIR | SHIFT_MASK_MODE,
63 SHIFT_COUNT_Y = 0x0000, /* Count is in Y register */
64 SHIFT_COUNT_1 = 0x0001,
65 SHIFT_COUNT_2 = 0x0002,
66 SHIFT_COUNT_3 = 0x0003,
67 SHIFT_COUNT_4 = 0x0004,
68 SHIFT_COUNT_5 = 0x0005,
69 SHIFT_COUNT_6 = 0x0006,
70 SHIFT_COUNT_7 = 0x0007,
72 /* Shift directions */
73 SHIFT_DIR_LEFT = 0x0100,
74 SHIFT_DIR_RIGHT = 0x0200,
77 SHIFT_MODE_ARITH = 0x1000,
78 SHIFT_MODE_LOGICAL = 0x2000,
81 SHIFT_TYPE_ASL = SHIFT_DIR_LEFT | SHIFT_MODE_ARITH,
82 SHIFT_TYPE_ASR = SHIFT_DIR_RIGHT | SHIFT_MODE_ARITH,
83 SHIFT_TYPE_LSL = SHIFT_DIR_LEFT | SHIFT_MODE_LOGICAL,
84 SHIFT_TYPE_LSR = SHIFT_DIR_RIGHT | SHIFT_MODE_LOGICAL,
87 SHIFT_ASL_Y = SHIFT_TYPE_ASL | SHIFT_COUNT_Y,
88 SHIFT_ASR_Y = SHIFT_TYPE_ASR | SHIFT_COUNT_Y,
89 SHIFT_LSL_Y = SHIFT_TYPE_LSL | SHIFT_COUNT_Y,
90 SHIFT_LSR_Y = SHIFT_TYPE_LSR | SHIFT_COUNT_Y,
92 SHIFT_ASL_1 = SHIFT_TYPE_ASL | SHIFT_COUNT_1,
93 SHIFT_ASR_1 = SHIFT_TYPE_ASR | SHIFT_COUNT_1,
94 SHIFT_LSL_1 = SHIFT_TYPE_LSL | SHIFT_COUNT_1,
95 SHIFT_LSR_1 = SHIFT_TYPE_LSR | SHIFT_COUNT_1,
97 SHIFT_ASL_2 = SHIFT_TYPE_ASL | SHIFT_COUNT_2,
98 SHIFT_ASR_2 = SHIFT_TYPE_ASR | SHIFT_COUNT_2,
99 SHIFT_LSL_2 = SHIFT_TYPE_LSL | SHIFT_COUNT_2,
100 SHIFT_LSR_2 = SHIFT_TYPE_LSR | SHIFT_COUNT_2,
102 SHIFT_ASL_3 = SHIFT_TYPE_ASL | SHIFT_COUNT_3,
103 SHIFT_ASR_3 = SHIFT_TYPE_ASR | SHIFT_COUNT_3,
104 SHIFT_LSL_3 = SHIFT_TYPE_LSL | SHIFT_COUNT_3,
105 SHIFT_LSR_3 = SHIFT_TYPE_LSR | SHIFT_COUNT_3,
107 SHIFT_ASL_4 = SHIFT_TYPE_ASL | SHIFT_COUNT_4,
108 SHIFT_ASR_4 = SHIFT_TYPE_ASR | SHIFT_COUNT_4,
109 SHIFT_LSL_4 = SHIFT_TYPE_LSL | SHIFT_COUNT_4,
110 SHIFT_LSR_4 = SHIFT_TYPE_LSR | SHIFT_COUNT_4,
112 SHIFT_ASL_5 = SHIFT_TYPE_ASL | SHIFT_COUNT_5,
113 SHIFT_ASR_5 = SHIFT_TYPE_ASR | SHIFT_COUNT_5,
114 SHIFT_LSL_5 = SHIFT_TYPE_LSL | SHIFT_COUNT_5,
115 SHIFT_LSR_5 = SHIFT_TYPE_LSR | SHIFT_COUNT_5,
117 SHIFT_ASL_6 = SHIFT_TYPE_ASL | SHIFT_COUNT_6,
118 SHIFT_ASR_6 = SHIFT_TYPE_ASR | SHIFT_COUNT_6,
119 SHIFT_LSL_6 = SHIFT_TYPE_LSL | SHIFT_COUNT_6,
120 SHIFT_LSR_6 = SHIFT_TYPE_LSR | SHIFT_COUNT_6,
122 SHIFT_ASL_7 = SHIFT_TYPE_ASL | SHIFT_COUNT_7,
123 SHIFT_ASR_7 = SHIFT_TYPE_ASR | SHIFT_COUNT_7,
124 SHIFT_LSL_7 = SHIFT_TYPE_LSL | SHIFT_COUNT_7,
125 SHIFT_LSR_7 = SHIFT_TYPE_LSR | SHIFT_COUNT_7,
130 /* Macros to extract values from a shift type */
131 #define SHIFT_COUNT(S) ((S) & SHIFT_MASK_COUNT)
132 #define SHIFT_DIR(S) ((S) & SHIFT_MASK_DIR)
133 #define SHIFT_MODE(S) ((S) & SHIFT_MASK_MODE)
134 #define SHIFT_TYPE(S) ((S) & SHIFT_MASK_TYPE)
138 /*****************************************************************************/
139 /* Helper routines */
140 /*****************************************************************************/
144 static unsigned GetShift (const char* Name)
145 /* Determine the shift from the name of the subroutine */
149 if (strncmp (Name, "aslax", 5) == 0) {
150 Type = SHIFT_TYPE_ASL;
151 } else if (strncmp (Name, "asrax", 5) == 0) {
152 Type = SHIFT_TYPE_ASR;
153 } else if (strncmp (Name, "shlax", 5) == 0) {
154 Type = SHIFT_TYPE_LSL;
155 } else if (strncmp (Name, "shrax", 5) == 0) {
156 Type = SHIFT_TYPE_LSR;
158 /* Nothing we know */
164 case 'y': Type |= SHIFT_COUNT_Y; break;
165 case '1': Type |= SHIFT_COUNT_1; break;
166 case '2': Type |= SHIFT_COUNT_2; break;
167 case '3': Type |= SHIFT_COUNT_3; break;
168 case '4': Type |= SHIFT_COUNT_4; break;
169 case '5': Type |= SHIFT_COUNT_5; break;
170 case '6': Type |= SHIFT_COUNT_6; break;
171 case '7': Type |= SHIFT_COUNT_7; break;
172 default: return SHIFT_NONE;
175 /* Make sure nothing follows */
176 if (Name[6] == '\0') {
185 /*****************************************************************************/
186 /* Optimize shifts */
187 /*****************************************************************************/
191 unsigned OptShift1 (CodeSeg* S)
192 /* A call to the shlaxN routine may get replaced by one or more asl insns
193 ** if the value of X is not used later. If X is used later, but it is zero
194 ** on entry and it's a shift by one, it may get replaced by:
202 unsigned Changes = 0;
205 /* Walk over the entries */
207 while (I < CS_GetEntryCount (S)) {
215 CodeEntry* E = CS_GetEntry (S, I);
217 /* Check for the sequence */
218 if (E->OPC == OP65_JSR &&
219 (Shift = GetShift (E->Arg)) != SHIFT_NONE &&
220 SHIFT_DIR (Shift) == SHIFT_DIR_LEFT) {
223 unsigned Count = SHIFT_COUNT (Shift);
224 if (!RegXUsed (S, I+1)) {
226 if (Count == SHIFT_COUNT_Y) {
230 if (S->CodeSizeFactor < 200) {
243 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
244 CS_InsertEntry (S, X, I+1);
245 L = CS_GenLabel (S, X);
248 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
249 CS_InsertEntry (S, X, I+2);
252 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, E->LI);
253 CS_InsertEntry (S, X, I+3);
256 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, E->LI);
257 CS_InsertEntry (S, X, I+4);
260 /* Insert shift insns */
262 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
263 CS_InsertEntry (S, X, I+1);
267 } else if (E->RI->In.RegX == 0 &&
269 (N = CS_GetNextEntry (S, I)) != 0) {
272 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
273 CS_InsertEntry (S, X, I+1);
276 L = CS_GenLabel (S, N);
277 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, E->LI);
278 CS_InsertEntry (S, X, I+2);
281 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
282 CS_InsertEntry (S, X, I+3);
286 /* We won't handle this one */
291 /* Delete the call to shlax */
294 /* Remember, we had changes */
304 /* Return the number of changes made */
310 unsigned OptShift2(CodeSeg* S)
311 /* A call to the asrax1 routines may get replaced by something simpler, if
312 ** X is not used later:
318 unsigned Changes = 0;
321 /* Walk over the entries */
323 while (I < CS_GetEntryCount (S)) {
329 CodeEntry* E = CS_GetEntry (S, I);
331 /* Check for the sequence */
332 if (E->OPC == OP65_JSR &&
333 (Shift = GetShift (E->Arg)) != SHIFT_NONE &&
334 SHIFT_TYPE (Shift) == SHIFT_TYPE_ASR &&
335 (Count = SHIFT_COUNT (Shift)) > 0 &&
336 Count * 100 <= S->CodeSizeFactor &&
337 !RegXUsed (S, I+1)) {
342 /* Generate the replacement sequence */
345 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, E->LI);
346 CS_InsertEntry (S, X, J++);
349 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, E->LI);
350 CS_InsertEntry (S, X, J++);
353 /* Delete the call to asrax */
356 /* Remember, we had changes */
365 /* Return the number of changes made */
371 unsigned OptShift3 (CodeSeg* S)
378 ** may get replaced by
382 ** if X is zero on entry. For shift counts > 1, more
389 unsigned Changes = 0;
392 /* Walk over the entries */
394 while (I < CS_GetEntryCount (S)) {
401 L[0] = CS_GetEntry (S, I);
403 /* Check for the sequence */
404 if ((L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
406 L[0]->RI->In.RegX == 0 &&
407 CS_GetEntries (S, L+1, I+1, 2) &&
408 L[1]->OPC == OP65_INX &&
409 L[0]->JumpTo->Owner == L[2] &&
410 !CS_RangeHasLabel (S, I, 2) &&
411 L[2]->OPC == OP65_JSR &&
412 (Shift = GetShift (L[2]->Arg)) != SHIFT_NONE &&
413 SHIFT_DIR (Shift) == SHIFT_DIR_RIGHT &&
414 (Count = SHIFT_COUNT (Shift)) > 0) {
416 /* Add the replacement insn instead */
417 CodeEntry* X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
418 CS_InsertEntry (S, X, I+3);
420 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
421 CS_InsertEntry (S, X, I+4);
424 /* Remove the bcs/dex/jsr */
425 CS_DelEntries (S, I, 3);
427 /* Remember, we had changes */
437 /* Return the number of changes made */
443 unsigned OptShift4 (CodeSeg* S)
444 /* Calls to the asraxN or shraxN routines may get replaced by one or more lsr
445 ** insns if the value of X is zero.
448 unsigned Changes = 0;
451 /* Walk over the entries */
453 while (I < CS_GetEntryCount (S)) {
459 CodeEntry* E = CS_GetEntry (S, I);
461 /* Check for the sequence */
462 if (E->OPC == OP65_JSR &&
463 (Shift = GetShift (E->Arg)) != SHIFT_NONE &&
464 SHIFT_DIR (Shift) == SHIFT_DIR_RIGHT &&
465 E->RI->In.RegX == 0) {
469 /* Shift count may be in Y */
470 Count = SHIFT_COUNT (Shift);
471 if (Count == SHIFT_COUNT_Y) {
475 if (S->CodeSizeFactor < 200) {
487 ** A negative shift count or one that is greater or equal than
488 ** the bit width of the left operand (which is promoted to
489 ** integer before the operation) causes undefined behaviour, so
490 ** above transformation is safe.
494 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
495 CS_InsertEntry (S, X, I+1);
496 L = CS_GenLabel (S, X);
499 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
500 CS_InsertEntry (S, X, I+2);
503 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, E->LI);
504 CS_InsertEntry (S, X, I+3);
507 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, E->LI);
508 CS_InsertEntry (S, X, I+4);
511 /* Insert shift insns */
513 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
514 CS_InsertEntry (S, X, I+1);
519 /* Delete the call to shrax */
522 /* Remember, we had changes */
533 /* Return the number of changes made */
539 unsigned OptShift5 (CodeSeg* S)
540 /* Search for the sequence
544 ** jsr aslax1/asrax1/shlax1/shrax1
557 ** or similar, provided that a/x is not used later
560 unsigned Changes = 0;
562 /* Walk over the entries */
564 while (I < CS_GetEntryCount (S)) {
570 L[0] = CS_GetEntry (S, I);
572 /* Check for the sequence */
573 if (L[0]->OPC == OP65_LDA &&
574 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
575 CS_GetEntries (S, L+1, I+1, 4) &&
576 !CS_RangeHasLabel (S, I+1, 4) &&
577 L[1]->OPC == OP65_LDX &&
578 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
579 L[2]->OPC == OP65_JSR &&
580 (ShiftType = GetShift (L[2]->Arg)) != SHIFT_NONE &&
581 SHIFT_COUNT(ShiftType) == 1 &&
582 L[3]->OPC == OP65_STA &&
583 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
584 L[4]->OPC == OP65_STX &&
585 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
586 !RegAXUsed (S, I+5)) {
590 /* Handle the four shift types differently */
594 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
595 CS_InsertEntry (S, X, I+5);
596 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
597 CS_InsertEntry (S, X, I+6);
598 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
599 CS_InsertEntry (S, X, I+7);
600 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
601 CS_InsertEntry (S, X, I+8);
602 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
603 CS_InsertEntry (S, X, I+9);
604 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
605 CS_InsertEntry (S, X, I+10);
606 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
607 CS_InsertEntry (S, X, I+11);
608 CS_DelEntries (S, I, 5);
612 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
613 CS_InsertEntry (S, X, I+5);
614 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
615 CS_InsertEntry (S, X, I+6);
616 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
617 CS_InsertEntry (S, X, I+7);
618 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
619 CS_InsertEntry (S, X, I+8);
620 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
621 CS_InsertEntry (S, X, I+9);
622 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
623 CS_InsertEntry (S, X, I+10);
624 CS_DelEntries (S, I, 5);
629 /* These two are identical */
630 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
631 CS_InsertEntry (S, X, I+1);
632 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
633 CS_InsertEntry (S, X, I+2);
634 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
635 CS_InsertEntry (S, X, I+3);
636 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
637 CS_InsertEntry (S, X, I+4);
638 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
639 CS_InsertEntry (S, X, I+5);
640 CS_DelEntries (S, I+6, 4);
645 /* Remember, we had changes */
655 /* Return the number of changes made */
661 unsigned OptShift6 (CodeSeg* S)
662 /* Inline the shift subroutines. */
664 unsigned Changes = 0;
666 /* Walk over the entries */
668 while (I < CS_GetEntryCount (S)) {
676 CodeEntry* E = CS_GetEntry (S, I);
678 /* Check for a call to one of the shift routine */
679 if (E->OPC == OP65_JSR &&
680 (Shift = GetShift (E->Arg)) != SHIFT_NONE &&
681 SHIFT_DIR (Shift) == SHIFT_DIR_LEFT &&
682 (Count = SHIFT_COUNT (Shift)) > 0) {
689 ** (repeat ShiftCount-1 times)
692 ** which makes 4 + 3 * ShiftCount bytes, compared to the original
693 ** 3 bytes for the subroutine call. However, in most cases, the
694 ** final load of the X register gets merged with some other insn
695 ** and replaces a txa, so for a shift count of 1, we get a factor
696 ** of 200, which matches nicely the CodeSizeFactor enabled with -Oi
698 if (Count > 1 || S->CodeSizeFactor > 200) {
699 unsigned Size = 4 + 3 * Count;
700 if ((Size * 100 / 3) > S->CodeSizeFactor) {
706 /* Inline the code. Insertion point is behind the subroutine call */
710 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
711 CS_InsertEntry (S, X, IP++);
715 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
716 CS_InsertEntry (S, X, IP++);
719 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
720 CS_InsertEntry (S, X, IP++);
724 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
725 CS_InsertEntry (S, X, IP++);
727 /* Remove the subroutine call */
730 /* Remember, we had changes */
740 /* Return the number of changes made */