1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static unsigned OptShift1 (CodeSeg* S)
71 /* A call to the shlaxN routine may get replaced by one or more asl insns
72 * if the value of X is not used later.
77 /* Walk over the entries */
79 while (I < CS_GetEntryCount (S)) {
82 CodeEntry* E = CS_GetEntry (S, I);
84 /* Check for the sequence */
85 if (E->OPC == OP65_JSR &&
86 (strncmp (E->Arg, "shlax", 5) == 0 ||
87 strncmp (E->Arg, "aslax", 5) == 0) &&
88 strlen (E->Arg) == 6 &&
89 IsDigit (E->Arg[5]) &&
92 /* Insert shift insns */
93 unsigned Count = E->Arg[5] - '0';
95 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
96 CS_InsertEntry (S, X, I+1);
99 /* Delete the call to shlax */
102 /* Remember, we had changes */
112 /* Return the number of changes made */
118 /*****************************************************************************/
119 /* Optimize stores through pointers */
120 /*****************************************************************************/
124 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
125 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
127 /* Check for a label attached to the entry */
128 if (CE_HasLabel (L[0])) {
132 /* Check for single insn sub ops */
133 if (L[0]->OPC == OP65_AND ||
134 L[0]->OPC == OP65_EOR ||
135 L[0]->OPC == OP65_ORA ||
136 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
137 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
142 } else if (L[0]->OPC == OP65_CLC &&
143 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
144 L[1]->OPC == OP65_ADC &&
145 !CE_HasLabel (L[1])) {
147 } else if (L[0]->OPC == OP65_SEC &&
148 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
149 L[1]->OPC == OP65_SBC &&
150 !CE_HasLabel (L[1])) {
162 static unsigned OptPtrStore1 (CodeSeg* S)
163 /* Search for the sequence:
184 unsigned Changes = 0;
186 /* Walk over the entries */
188 while (I < CS_GetEntryCount (S)) {
194 L[0] = CS_GetEntry (S, I);
196 /* Check for the sequence */
197 if (CE_IsCall (L[0], "pushax") &&
198 CS_GetEntries (S, L+1, I+1, 3) &&
199 L[1]->OPC == OP65_LDY &&
200 CE_KnownImm (L[1]) &&
201 !CE_HasLabel (L[1]) &&
202 CE_IsCall (L[2], "ldauidx") &&
203 !CE_HasLabel (L[2]) &&
204 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
205 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
206 L[3+K]->OPC == OP65_LDY &&
207 CE_KnownImm (L[3+K]) &&
208 !CE_HasLabel (L[3+K]) &&
209 CE_IsCall (L[4+K], "staspidx") &&
210 !CE_HasLabel (L[4+K])) {
214 /* Create and insert the stores */
215 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
216 CS_InsertEntry (S, X, I+1);
218 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
219 CS_InsertEntry (S, X, I+2);
221 /* Delete the call to pushax */
224 /* Delete the call to ldauidx */
225 CS_DelEntry (S, I+3);
227 /* Insert the load from ptr1 */
228 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
229 CS_InsertEntry (S, X, I+3);
230 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
231 CS_InsertEntry (S, X, I+4);
233 /* Insert the store through ptr1 */
234 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
235 CS_InsertEntry (S, X, I+6+K);
237 /* Delete the call to staspidx */
238 CS_DelEntry (S, I+7+K);
240 /* Remember, we had changes */
250 /* Return the number of changes made */
256 static unsigned OptPtrStore2 (CodeSeg* S)
257 /* Search for the sequence:
273 unsigned Changes = 0;
275 /* Walk over the entries */
277 while (I < CS_GetEntryCount (S)) {
282 L[0] = CS_GetEntry (S, I);
284 /* Check for the sequence */
285 if (CE_IsCall (L[0], "pushax") &&
286 CS_GetEntries (S, L+1, I+1, 3) &&
287 L[1]->OPC == OP65_LDA &&
288 !CE_HasLabel (L[1]) &&
289 L[2]->OPC == OP65_LDY &&
290 !CE_HasLabel (L[2]) &&
291 CE_IsCall (L[3], "staspidx") &&
292 !CE_HasLabel (L[3])) {
296 /* Create and insert the stores */
297 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
298 CS_InsertEntry (S, X, I+1);
300 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
301 CS_InsertEntry (S, X, I+2);
303 /* Delete the call to pushax */
306 /* Insert the store through ptr1 */
307 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
308 CS_InsertEntry (S, X, I+4);
310 /* Delete the call to staspidx */
311 CS_DelEntry (S, I+5);
313 /* Remember, we had changes */
323 /* Return the number of changes made */
329 /*****************************************************************************/
330 /* Optimize loads through pointers */
331 /*****************************************************************************/
335 static unsigned OptPtrLoad1 (CodeSeg* S)
336 /* Search for the sequence:
340 * lda (sp),y # May be any destination
355 unsigned Changes = 0;
357 /* Walk over the entries */
359 while (I < CS_GetEntryCount (S)) {
364 L[0] = CS_GetEntry (S, I);
366 /* Check for the sequence */
367 if (L[0]->OPC == OP65_TAX &&
368 CS_GetEntries (S, L+1, I+1, 4) &&
369 L[1]->OPC == OP65_DEY &&
370 !CE_HasLabel (L[1]) &&
371 L[2]->OPC == OP65_LDA &&
372 !CE_HasLabel (L[2]) &&
373 L[3]->OPC == OP65_LDY &&
374 !CE_HasLabel (L[3]) &&
375 CE_IsCall (L[4], "ldauidx") &&
376 !CE_HasLabel (L[4])) {
380 /* Store the high byte and remove the TAX instead */
381 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
382 CS_InsertEntry (S, X, I+1);
385 /* Store the low byte */
386 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
387 CS_InsertEntry (S, X, I+3);
389 /* Delete the call to ldauidx */
390 CS_DelEntry (S, I+5);
392 /* Load high and low byte */
393 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
394 CS_InsertEntry (S, X, I+5);
395 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
396 CS_InsertEntry (S, X, I+6);
398 /* Remember, we had changes */
408 /* Return the number of changes made */
414 static unsigned OptPtrLoad2 (CodeSeg* S)
415 /* Search for the sequence:
440 unsigned Changes = 0;
442 /* Walk over the entries */
444 while (I < CS_GetEntryCount (S)) {
449 L[0] = CS_GetEntry (S, I);
451 /* Check for the sequence */
452 if (L[0]->OPC == OP65_CLC &&
453 CS_GetEntries (S, L+1, I+1, 8) &&
454 L[1]->OPC == OP65_ADC &&
455 !CE_HasLabel (L[1]) &&
456 L[2]->OPC == OP65_TAY &&
457 !CE_HasLabel (L[2]) &&
458 L[3]->OPC == OP65_TXA &&
459 !CE_HasLabel (L[3]) &&
460 L[4]->OPC == OP65_ADC &&
461 !CE_HasLabel (L[4]) &&
462 L[5]->OPC == OP65_TAX &&
463 !CE_HasLabel (L[5]) &&
464 L[6]->OPC == OP65_TYA &&
465 !CE_HasLabel (L[6]) &&
466 L[7]->OPC == OP65_LDY &&
467 !CE_HasLabel (L[7]) &&
468 CE_IsCall (L[8], "ldauidx") &&
469 !CE_HasLabel (L[8])) {
474 /* Store the low byte and remove the TAY instead */
475 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
476 CS_InsertEntry (S, X, I+2);
477 CS_DelEntry (S, I+3);
479 /* Store the high byte */
480 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
481 CS_InsertEntry (S, X, I+5);
483 /* If the instruction before the adc is a ldx, replace the
484 * txa by and lda with the same location of the ldx.
486 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
487 P->OPC == OP65_LDX &&
490 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
491 CS_InsertEntry (S, X, I+4);
492 CS_DelEntry (S, I+3);
495 /* Delete more transfer insns */
496 CS_DelEntry (S, I+7);
497 CS_DelEntry (S, I+6);
499 /* Delete the call to ldauidx */
500 CS_DelEntry (S, I+7);
502 /* Load high and low byte */
503 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
504 CS_InsertEntry (S, X, I+7);
505 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
506 CS_InsertEntry (S, X, I+8);
508 /* Remember, we had changes */
518 /* Return the number of changes made */
524 static unsigned OptPtrLoad3 (CodeSeg* S)
525 /* Search for the sequence:
550 unsigned Changes = 0;
552 /* Walk over the entries */
554 while (I < CS_GetEntryCount (S)) {
559 L[0] = CS_GetEntry (S, I);
561 /* Check for the sequence */
562 if (L[0]->OPC == OP65_ADC &&
563 CS_GetEntries (S, L+1, I+1, 8) &&
564 L[1]->OPC == OP65_PHA &&
565 !CE_HasLabel (L[1]) &&
566 L[2]->OPC == OP65_TXA &&
567 !CE_HasLabel (L[2]) &&
568 L[3]->OPC == OP65_INY &&
569 !CE_HasLabel (L[3]) &&
570 L[4]->OPC == OP65_ADC &&
571 !CE_HasLabel (L[4]) &&
572 L[5]->OPC == OP65_TAX &&
573 !CE_HasLabel (L[5]) &&
574 L[6]->OPC == OP65_PLA &&
575 !CE_HasLabel (L[6]) &&
576 L[7]->OPC == OP65_LDY &&
577 !CE_HasLabel (L[7]) &&
578 CE_IsCall (L[8], "ldauidx") &&
579 !CE_HasLabel (L[8])) {
583 /* Store the low byte and remove the PHA instead */
584 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
585 CS_InsertEntry (S, X, I+1);
586 CS_DelEntry (S, I+2);
588 /* Store the high byte */
589 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
590 CS_InsertEntry (S, X, I+5);
592 /* Delete more transfer and PLA insns */
593 CS_DelEntry (S, I+7);
594 CS_DelEntry (S, I+6);
596 /* Delete the call to ldauidx */
597 CS_DelEntry (S, I+7);
599 /* Load high and low byte */
600 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
601 CS_InsertEntry (S, X, I+7);
602 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
603 CS_InsertEntry (S, X, I+8);
605 /* Remember, we had changes */
615 /* Return the number of changes made */
621 static unsigned OptPtrLoad4 (CodeSeg* S)
622 /* Search for the sequence:
640 unsigned Changes = 0;
642 /* Walk over the entries */
644 while (I < CS_GetEntryCount (S)) {
650 L[0] = CS_GetEntry (S, I);
652 /* Check for the sequence */
653 if (L[0]->OPC == OP65_LDA &&
654 L[0]->AM == AM65_IMM &&
655 CS_GetEntries (S, L+1, I+1, 7) &&
656 L[1]->OPC == OP65_LDX &&
657 L[1]->AM == AM65_IMM &&
658 !CE_HasLabel (L[1]) &&
659 L[2]->OPC == OP65_CLC &&
660 !CE_HasLabel (L[2]) &&
661 L[3]->OPC == OP65_ADC &&
662 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
663 !CE_HasLabel (L[3]) &&
664 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
666 L[4]->JumpTo->Owner == L[6] &&
667 !CE_HasLabel (L[4]) &&
668 L[5]->OPC == OP65_INX &&
669 !CE_HasLabel (L[5]) &&
670 L[6]->OPC == OP65_LDY &&
671 CE_KnownImm (L[6]) &&
673 CE_IsCall (L[7], "ldauidx") &&
674 !CE_HasLabel (L[7]) &&
675 /* Check the label last because this is quite costly */
676 (Len = strlen (L[0]->Arg)) > 3 &&
677 L[0]->Arg[0] == '<' &&
678 L[0]->Arg[1] == '(' &&
679 strlen (L[1]->Arg) == Len &&
680 L[1]->Arg[0] == '>' &&
681 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
686 /* We will create all the new stuff behind the current one so
687 * we keep the line references.
689 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
690 CS_InsertEntry (S, X, I+8);
692 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
693 CS_InsertEntry (S, X, I+9);
695 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
697 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
698 CS_InsertEntry (S, X, I+10);
701 /* Remove the old code */
702 CS_DelEntries (S, I, 8);
704 /* Remember, we had changes */
714 /* Return the number of changes made */
720 static unsigned OptPtrLoad5 (CodeSeg* S)
721 /* Search for the sequence:
742 unsigned Changes = 0;
744 /* Walk over the entries */
746 while (I < CS_GetEntryCount (S)) {
752 L[0] = CS_GetEntry (S, I);
754 /* Check for the sequence */
755 if (L[0]->OPC == OP65_LDA &&
756 L[0]->AM == AM65_IMM &&
757 CS_GetEntries (S, L+1, I+1, 8) &&
758 L[1]->OPC == OP65_LDX &&
759 L[1]->AM == AM65_IMM &&
760 !CE_HasLabel (L[1]) &&
761 L[2]->OPC == OP65_LDY &&
762 CE_KnownImm (L[2]) &&
763 !CE_HasLabel (L[2]) &&
764 L[3]->OPC == OP65_CLC &&
765 !CE_HasLabel (L[3]) &&
766 L[4]->OPC == OP65_ADC &&
767 L[4]->AM == AM65_ZP_INDY &&
768 !CE_HasLabel (L[4]) &&
769 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
771 L[5]->JumpTo->Owner == L[7] &&
772 !CE_HasLabel (L[5]) &&
773 L[6]->OPC == OP65_INX &&
774 !CE_HasLabel (L[6]) &&
775 L[7]->OPC == OP65_LDY &&
776 CE_KnownImm (L[7]) &&
778 CE_IsCall (L[8], "ldauidx") &&
779 !CE_HasLabel (L[8]) &&
780 /* Check the label last because this is quite costly */
781 (Len = strlen (L[0]->Arg)) > 3 &&
782 L[0]->Arg[0] == '<' &&
783 L[0]->Arg[1] == '(' &&
784 strlen (L[1]->Arg) == Len &&
785 L[1]->Arg[0] == '>' &&
786 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
792 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
793 CS_InsertEntry (S, X, I+3);
796 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
797 CS_InsertEntry (S, X, I+4);
800 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
801 CS_InsertEntry (S, X, I+5);
804 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
806 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
807 CS_InsertEntry (S, X, I+6);
810 /* Remove the old code */
811 CS_DelEntries (S, I, 2);
812 CS_DelEntries (S, I+5, 6);
814 /* Remember, we had changes */
824 /* Return the number of changes made */
830 static unsigned OptPtrLoad6 (CodeSeg* S)
831 /* Search for the sequence
844 * This step must be execute *after* OptPtrLoad1!
847 unsigned Changes = 0;
849 /* Walk over the entries */
851 while (I < CS_GetEntryCount (S)) {
856 L[0] = CS_GetEntry (S, I);
858 /* Check for the sequence */
859 if (L[0]->OPC == OP65_LDY &&
860 CS_GetEntries (S, L+1, I+1, 1) &&
861 CE_IsCall (L[1], "ldauidx") &&
862 !CE_HasLabel (L[1])) {
866 /* Store the high byte */
867 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
868 CS_InsertEntry (S, X, I+1);
870 /* Store the low byte */
871 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
872 CS_InsertEntry (S, X, I+2);
874 /* Delete the call to ldauidx */
875 CS_DelEntry (S, I+3);
877 /* Load the high and low byte */
878 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
879 CS_InsertEntry (S, X, I+3);
880 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
881 CS_InsertEntry (S, X, I+4);
883 /* Remember, we had changes */
893 /* Return the number of changes made */
899 /*****************************************************************************/
900 /* Decouple operations */
901 /*****************************************************************************/
905 static unsigned OptDecouple (CodeSeg* S)
906 /* Decouple operations, that is, do the following replacements:
917 * Provided that the register values are known of course.
920 unsigned Changes = 0;
923 /* Generate register info for the following step */
926 /* Walk over the entries */
928 while (I < CS_GetEntryCount (S)) {
933 CodeEntry* E = CS_GetEntry (S, I);
935 /* Assume we have no replacement */
938 /* Check the instruction */
942 if (E->RI->In.RegX >= 0) {
943 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX - 1) & 0xFF);
944 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
949 if (E->RI->In.RegY >= 0) {
950 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY - 1) & 0xFF);
951 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
956 if (E->RI->In.RegX >= 0) {
957 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX + 1) & 0xFF);
958 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
963 if (E->RI->In.RegY >= 0) {
964 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY + 1) & 0xFF);
965 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
970 if (E->RI->In.RegA >= 0) {
971 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
972 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
977 if (E->RI->In.RegA >= 0) {
978 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
979 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
984 if (E->RI->In.RegX >= 0) {
985 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegX);
986 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
991 if (E->RI->In.RegY >= 0) {
992 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegY);
993 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
998 /* Avoid gcc warnings */
1003 /* Insert the replacement if we have one */
1005 CS_InsertEntry (S, X, I+1);
1015 /* Free register info */
1018 /* Return the number of changes made */
1024 /*****************************************************************************/
1025 /* Size optimization */
1026 /*****************************************************************************/
1030 static unsigned OptSize1 (CodeSeg* S)
1031 /* Do size optimization by calling special subroutines that preload registers.
1032 * This routine does not work standalone, it needs a following register load
1037 static const char* Func = {
1038 "stax0sp", /* staxysp, y = 0 */
1040 "ldax0sp", /* ldaxysp, y = 1 */
1041 "ldeax0sp", /* ldeaxysp, y = 3 */
1042 "push0", /* pushax, a = 0, x = 0 */
1043 "pusha0", /* pushax, x = 0 */
1044 "pushaFF", /* pushax, x = ff */
1045 "pusha0sp", /* pushaysp, y = 0 */
1046 "tosadda0", /* tosaddax, x = 0 */
1047 "tosanda0", /* tosandax, x = 0 */
1048 "tosdiva0", /* tosdivax, x = 0 */
1049 "toseqa0", /* toseqax, x = 0 */
1050 "tosgea0", /* tosgeax, x = 0 */
1051 "tosgta0", /* tosgtax, x = 0 */
1052 "tosadd0ax", /* tosaddeax, sreg = 0 */
1053 "laddeqa", /* laddeq, sreg = 0, x = 0 */
1054 "laddeq1", /* laddeq, sreg = 0, x = 0, a = 1 */
1055 "laddeq0sp", /* laddeqysp, y = 0 */
1056 "tosand0ax", /* tosandeax, sreg = 0 */
1057 "ldaxi", /* ldaxidx, y = 1 */
1058 "ldeaxi", /* ldeaxidx, y = 3 */
1059 "ldeax0sp", /* ldeaxysp, y = 3 */
1060 "tosdiv0ax", /* tosdiveax, sreg = 0 */
1061 "toslea0", /* tosleax, x = 0 */
1062 "tosmod0ax", /* tosmodeax, sreg = 0 */
1063 "tosmul0ax", /* tosmuleax, sreg = 0 */
1064 "tosumul0ax", /* tosumuleax, sreg = 0 */
1065 "tosor0ax", /* tosoreax, sreg = 0 */
1066 "push0ax", /* pusheax, sreg = 0 */
1067 "tosrsub0ax", /* tosrsubeax, sreg = 0 */
1068 "tosshl0ax", /* tosshleax, sreg = 0 */
1069 "tosasl0ax", /* tosasleax, sreg = 0 */
1070 "tosshr0ax", /* tosshreax, sreg = 0 */
1071 "tosasr0ax", /* tosasreax, sreg = 0 */
1072 "tossub0ax", /* tossubeax, sreg = 0 */
1073 "lsubeqa", /* lsubeq, sreg = 0, x = 0 */
1074 "lsubeq1", /* lsubeq, sreg = 0, x = 0, a = 1 */
1075 "lsubeq0sp", /* lsubeqysp, y = 0 */
1076 "toslta0", /* tosltax, x = 0 */
1077 "tosudiv0ax", /* tosudiveax, sreg = 0 */
1078 "tosumod0ax", /* tosumodeax, sreg = 0 */
1079 "tosxor0ax", /* tosxoreax, sreg = 0 */
1080 "tosmoda0", /* tosmodax, x = 0 */
1081 "tosmula0", /* tosmulax, x = 0 */
1082 "tosumula0", /* tosumulax, x = 0 */
1083 "tosnea0", /* tosneax, x = 0 */
1084 "tosora0", /* tosorax, x = 0 */
1085 "push1", /* pushax, x = 0, a = 1 */
1086 "push2", /* pushax, x = 0, a = 2 */
1087 "push3", /* pushax, x = 0, a = 3 */
1088 "push4", /* pushax, x = 0, a = 4 */
1089 "push5", /* pushax, x = 0, a = 5 */
1090 "push6", /* pushax, x = 0, a = 6 */
1091 "push7", /* pushax, x = 0, a = 7 */
1092 "pushc0", /* pusha, a = 0 */
1093 "pushc1", /* pusha, a = 1 */
1094 "pushc2", /* pusha, a = 2 */
1095 "tosrsuba0", /* tosrsubax, x = 0 */
1096 "tosshla0", /* tosshlax, x = 0 */
1097 "tosasla0", /* tosaslax, x = 0 */
1098 "tosshra0", /* tosshrax, x = 0 */
1099 "tosasra0", /* tosasrax, x = 0 */
1100 "steax0sp", /* steaxsp, y = 0 */
1101 "tossuba0", /* tossubax, x = 0 */
1102 "subeq0sp", /* subeqysp, y = 0 */
1103 "tosudiva0", /* tosudivax, x = 0 */
1104 "tosugea0", /* tosugeax, x = 0 */
1105 "tosugta0", /* tosugtax, x = 0 */
1106 "tosulea0", /* tosuleax, x = 0 */
1107 "tosulta0", /* tosultax, x = 0 */
1108 "tosumoda0", /* tosumodax, x = 0 */
1109 "tosxora0", /* tosxorax, x = 0 */
1113 unsigned Changes = 0;
1116 /* Generate register info for the following step */
1119 /* Walk over the entries */
1121 while (I < CS_GetEntryCount (S)) {
1123 /* Get next entry */
1124 CodeEntry* E = CS_GetEntry (S, I);
1132 /* Free register info */
1135 /* Return the number of changes made */
1141 static unsigned OptSize2 (CodeSeg* S)
1142 /* Do size optimization by using shorter code sequences, even if this
1143 * introduces relations between instructions. This step must be one of the
1144 * last steps, because it makes further work much more difficult.
1147 unsigned Changes = 0;
1150 /* Generate register info for the following step */
1153 /* Walk over the entries */
1155 while (I < CS_GetEntryCount (S)) {
1158 /* Get next entry */
1159 CodeEntry* E = CS_GetEntry (S, I);
1161 /* Assume we have no replacement */
1164 /* Check the instruction */
1168 if (CE_KnownImm (E)) {
1169 short Val = (short) E->Num;
1170 if (Val == E->RI->In.RegX) {
1171 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
1172 } else if (Val == E->RI->In.RegY) {
1173 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
1174 } else if (E->RI->In.RegA >= 0 && CPU >= CPU_65C02) {
1175 if (Val == ((E->RI->In.RegA - 1) & 0xFF)) {
1176 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
1177 } else if (Val == ((E->RI->In.RegA + 1) & 0xFF)) {
1178 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
1185 if (CE_KnownImm (E)) {
1186 short Val = (short) E->Num;
1187 if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX - 1) & 0xFF)) {
1188 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
1189 } else if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX + 1) & 0xFF)) {
1190 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
1191 } else if (Val == E->RI->In.RegA) {
1192 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
1198 if (CE_KnownImm (E)) {
1199 short Val = (short) E->Num;
1200 if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY - 1) & 0xFF)) {
1201 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
1202 } else if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY + 1) & 0xFF)) {
1203 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
1204 } else if (Val == E->RI->In.RegA) {
1205 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
1211 /* Avoid gcc warnings */
1216 /* Insert the replacement if we have one */
1218 CS_InsertEntry (S, X, I+1);
1228 /* Free register info */
1231 /* Return the number of changes made */
1237 /*****************************************************************************/
1238 /* struct OptFunc */
1239 /*****************************************************************************/
1243 typedef struct OptFunc OptFunc;
1245 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1246 const char* Name; /* Name of the function/group */
1247 unsigned long TotalRuns; /* Total number of runs */
1248 unsigned long LastRuns; /* Last number of runs */
1249 unsigned long TotalChanges; /* Total number of changes */
1250 unsigned long LastChanges; /* Last number of changes */
1251 char Disabled; /* True if function disabled */
1256 /*****************************************************************************/
1258 /*****************************************************************************/
1262 /* Macro that builds a function description */
1263 #define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
1265 /* A list of all the function descriptions */
1266 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 0, 0, 0, 0, 0 };
1267 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 0, 0, 0, 0, 0 };
1268 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 0, 0, 0, 0, 0 };
1269 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 0, 0, 0, 0, 0 };
1270 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0 };
1271 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 0, 0, 0, 0, 0 };
1272 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 0, 0, 0, 0, 0 };
1273 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 0, 0, 0, 0, 0 };
1274 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 0, 0, 0, 0, 0 };
1275 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 0, 0, 0, 0, 0 };
1276 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 0, 0, 0, 0, 0 };
1277 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 0, 0, 0, 0, 0 };
1278 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 0, 0, 0, 0, 0 };
1279 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 0, 0, 0, 0, 0 };
1280 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 0, 0, 0, 0, 0 };
1281 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 0, 0, 0, 0, 0 };
1282 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0 };
1283 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 0, 0, 0, 0, 0 };
1284 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 0, 0, 0, 0, 0 };
1285 static OptFunc DOptRTS = { OptRTS, "OptRTS", 0, 0, 0, 0, 0 };
1286 static OptFunc DOptRTSJumps = { OptRTSJumps, "OptRTSJumps", 0, 0, 0, 0, 0 };
1287 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 0, 0, 0, 0, 0 };
1288 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 0, 0, 0, 0, 0 };
1289 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 0, 0, 0, 0, 0 };
1290 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 0, 0, 0, 0, 0 };
1291 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 0, 0, 0, 0, 0 };
1292 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 0, 0, 0, 0, 0 };
1293 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 0, 0, 0, 0, 0 };
1294 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 0, 0, 0, 0, 0 };
1295 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 0, 0, 0, 0, 0 };
1296 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 0, 0, 0, 0, 0 };
1297 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 0, 0, 0, 0, 0 };
1298 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 0, 0, 0, 0, 0 };
1299 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 0, 0, 0, 0, 0 };
1300 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 0, 0, 0, 0, 0 };
1301 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 0, 0, 0, 0, 0 };
1302 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 0, 0, 0, 0, 0 };
1303 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 0, 0, 0, 0, 0 };
1304 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 0, 0, 0, 0, 0 };
1305 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0 };
1306 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 0, 0, 0, 0, 0 };
1307 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 0, 0, 0, 0, 0 };
1308 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 0, 0, 0, 0, 0 };
1309 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0 };
1310 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0 };
1311 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0 };
1314 /* Table containing all the steps in alphabetical order */
1315 static OptFunc* OptFuncs[] = {
1363 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1367 static int CmpOptStep (const void* Key, const void* Func)
1368 /* Compare function for bsearch */
1370 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1375 static OptFunc* FindOptFunc (const char* Name)
1376 /* Find an optimizer step by name in the table and return a pointer. Return
1377 * NULL if no such step is found.
1380 /* Search for the function in the list */
1381 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1387 static OptFunc* GetOptFunc (const char* Name)
1388 /* Find an optimizer step by name in the table and return a pointer. Print an
1389 * error and call AbEnd if not found.
1392 /* Search for the function in the list */
1393 OptFunc* F = FindOptFunc (Name);
1396 AbEnd ("Optimization step `%s' not found", Name);
1403 void DisableOpt (const char* Name)
1404 /* Disable the optimization with the given name */
1406 if (strcmp (Name, "any") == 0) {
1408 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1409 OptFuncs[I]->Disabled = 1;
1412 GetOptFunc(Name)->Disabled = 1;
1418 void EnableOpt (const char* Name)
1419 /* Enable the optimization with the given name */
1421 if (strcmp (Name, "any") == 0) {
1423 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1424 OptFuncs[I]->Disabled = 0;
1427 GetOptFunc(Name)->Disabled = 0;
1433 void ListOptSteps (FILE* F)
1434 /* List all optimization steps */
1437 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1438 fprintf (F, "%s\n", OptFuncs[I]->Name);
1444 static void ReadOptStats (const char* Name)
1445 /* Read the optimizer statistics file */
1450 /* Try to open the file */
1451 FILE* F = fopen (Name, "r");
1453 /* Ignore the error */
1457 /* Read and parse the lines */
1459 while (fgets (Buf, sizeof (Buf), F) != 0) {
1467 unsigned long TotalRuns;
1468 unsigned long TotalChanges;
1473 /* Remove trailing white space including the line terminator */
1476 while (Len > 0 && IsSpace (B[Len-1])) {
1481 /* Remove leading whitespace */
1482 while (IsSpace (*B)) {
1486 /* Check for empty and comment lines */
1487 if (*B == '\0' || *B == ';' || *B == '#') {
1491 /* Parse the line */
1492 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1497 /* Search for the optimizer step. */
1498 Func = FindOptFunc (Name);
1504 /* Found the step, set the fields */
1505 Func->TotalRuns = TotalRuns;
1506 Func->TotalChanges = TotalChanges;
1510 /* Close the file, ignore errors here. */
1516 static void WriteOptStats (const char* Name)
1517 /* Write the optimizer statistics file */
1521 /* Try to open the file */
1522 FILE* F = fopen (Name, "w");
1524 /* Ignore the error */
1528 /* Write the file */
1529 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1530 const OptFunc* O = OptFuncs[I];
1532 "%-20s %6lu %6lu %6lu %6lu\n",
1540 /* Close the file, ignore errors here. */
1546 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1547 /* Run one optimizer function Max times or until there are no more changes */
1549 unsigned Changes, C;
1551 /* Don't run the function if it is disabled */
1556 /* Run this until there are no more changes */
1560 /* Run the function */
1567 F->TotalChanges += C;
1568 F->LastChanges += C;
1570 } while (--Max && C > 0);
1572 /* Return the number of changes */
1578 static void RunOptGroup1 (CodeSeg* S)
1579 /* Run the first group of optimization steps. These steps translate known
1580 * patterns emitted by the code generator into more optimal patterns. Order
1581 * of the steps is important, because some of the steps done earlier cover
1582 * the same patterns as later steps as subpatterns.
1585 RunOptFunc (S, &DOptPtrStore1, 1);
1586 RunOptFunc (S, &DOptPtrStore2, 1);
1587 RunOptFunc (S, &DOptPtrLoad1, 1);
1588 RunOptFunc (S, &DOptPtrLoad2, 1);
1589 RunOptFunc (S, &DOptPtrLoad3, 1);
1590 RunOptFunc (S, &DOptPtrLoad4, 1);
1591 RunOptFunc (S, &DOptPtrLoad5, 1);
1592 RunOptFunc (S, &DOptNegAX1, 1);
1593 RunOptFunc (S, &DOptNegAX2, 1);
1594 RunOptFunc (S, &DOptNegAX3, 1);
1595 RunOptFunc (S, &DOptNegAX4, 1);
1596 RunOptFunc (S, &DOptAdd1, 1);
1597 RunOptFunc (S, &DOptAdd2, 1);
1598 RunOptFunc (S, &DOptShift1, 1);
1603 static void RunOptGroup2 (CodeSeg* S)
1604 /* Run one group of optimization steps. This step involves just decoupling
1605 * instructions by replacing them by instructions that do not depend on
1606 * previous instructions. This makes it easier to find instructions that
1610 RunOptFunc (S, &DOptDecouple, 1);
1616 static void RunOptGroup3 (CodeSeg* S)
1617 /* Run one group of optimization steps. These steps depend on each other,
1618 * that means that one step may allow another step to do additional work,
1619 * so we will repeat the steps as long as we see any changes.
1627 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1628 Changes += RunOptFunc (S, &DOptNegA1, 1);
1629 Changes += RunOptFunc (S, &DOptNegA2, 1);
1630 Changes += RunOptFunc (S, &DOptSub1, 1);
1631 Changes += RunOptFunc (S, &DOptSub2, 1);
1632 Changes += RunOptFunc (S, &DOptAdd3, 1);
1633 Changes += RunOptFunc (S, &DOptStackOps, 1);
1634 Changes += RunOptFunc (S, &DOptJumpCascades, 1);
1635 Changes += RunOptFunc (S, &DOptDeadJumps, 1);
1636 Changes += RunOptFunc (S, &DOptRTS, 1);
1637 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1638 Changes += RunOptFunc (S, &DOptJumpTarget, 1);
1639 Changes += RunOptFunc (S, &DOptCondBranches, 1);
1640 Changes += RunOptFunc (S, &DOptRTSJumps, 1);
1641 Changes += RunOptFunc (S, &DOptBoolTrans, 1);
1642 Changes += RunOptFunc (S, &DOptCmp1, 1);
1643 Changes += RunOptFunc (S, &DOptCmp2, 1);
1644 Changes += RunOptFunc (S, &DOptCmp3, 1);
1645 Changes += RunOptFunc (S, &DOptCmp4, 1);
1646 Changes += RunOptFunc (S, &DOptCmp5, 1);
1647 Changes += RunOptFunc (S, &DOptCmp6, 1);
1648 Changes += RunOptFunc (S, &DOptCmp7, 1);
1649 Changes += RunOptFunc (S, &DOptTest1, 1);
1650 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1651 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1652 Changes += RunOptFunc (S, &DOptDupLoads, 1);
1653 Changes += RunOptFunc (S, &DOptStoreLoad, 1);
1654 Changes += RunOptFunc (S, &DOptTransfers, 1);
1661 static void RunOptGroup4 (CodeSeg* S)
1662 /* The last group of optimization steps. Adjust branches.
1665 /* Optimize for size, that is replace operations by shorter ones, even
1666 * if this does hinder further optimizations (no problem since we're
1669 RunOptFunc (S, &DOptSize2, 1);
1671 /* Run the jump target optimization again, since the size optimization
1672 * above may have opened new oportunities.
1674 RunOptFunc (S, &DOptJumpTarget, 5);
1676 /* Finally, adjust branch distances */
1677 RunOptFunc (S, &DOptBranchDist, 3);
1682 void RunOpt (CodeSeg* S)
1683 /* Run the optimizer */
1685 const char* StatFileName;
1687 /* If we shouldn't run the optimizer, bail out */
1692 /* Check if we are requested to write optimizer statistics */
1693 StatFileName = getenv ("CC65_OPTSTATS");
1695 ReadOptStats (StatFileName);
1698 /* Print the name of the function we are working on */
1700 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1702 Print (stdout, 1, "Running optimizer for global code segment\n");
1705 /* Run groups of optimizations */
1711 /* Write statistics */
1713 WriteOptStats (StatFileName);