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 /*****************************************************************************/
61 /*****************************************************************************/
63 /*****************************************************************************/
67 static unsigned OptShift1 (CodeSeg* S)
68 /* A call to the shlaxN routine may get replaced by one or more asl insns
69 * if the value of X is not used later.
74 /* Walk over the entries */
76 while (I < CS_GetEntryCount (S)) {
79 CodeEntry* E = CS_GetEntry (S, I);
81 /* Check for the sequence */
82 if (E->OPC == OP65_JSR &&
83 (strncmp (E->Arg, "shlax", 5) == 0 ||
84 strncmp (E->Arg, "aslax", 5) == 0) &&
85 strlen (E->Arg) == 6 &&
86 IsDigit (E->Arg[5]) &&
89 /* Insert shift insns */
90 unsigned Count = E->Arg[5] - '0';
92 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
93 CS_InsertEntry (S, X, I+1);
96 /* Delete the call to shlax */
99 /* Remember, we had changes */
109 /* Return the number of changes made */
115 /*****************************************************************************/
117 /*****************************************************************************/
121 static unsigned OptTest1 (CodeSeg* S)
128 * if X is zero, the sequence may be changed to
133 * which may be optimized further by another step.
135 * If A is zero, the sequence may be changed to
142 unsigned Changes = 0;
145 /* Generate register info for this step */
148 /* Walk over the entries */
150 while (I < CS_GetEntryCount (S)) {
155 L[0] = CS_GetEntry (S, I);
157 /* Check if it's the sequence we're searching for */
158 if (L[0]->OPC == OP65_STX &&
159 CS_GetEntries (S, L+1, I+1, 2) &&
160 !CE_HasLabel (L[1]) &&
161 L[1]->OPC == OP65_ORA &&
162 strcmp (L[0]->Arg, L[1]->Arg) == 0 &&
163 !CE_HasLabel (L[2]) &&
164 (L[2]->Info & OF_ZBRA) != 0) {
166 /* Check if X is zero */
167 if (L[0]->RI->In.RegX == 0) {
169 /* Insert the compare */
170 CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI);
171 CS_InsertEntry (S, N, I+2);
173 /* Remove the two other insns */
174 CS_DelEntry (S, I+1);
180 /* Check if A is zero */
181 } else if (L[1]->RI->In.RegA == 0) {
184 CodeEntry* N = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[1]->LI);
185 CS_InsertEntry (S, N, I+2);
187 /* Remove the two other insns */
188 CS_DelEntry (S, I+1);
201 /* Free register info */
204 /* Return the number of changes made */
210 /*****************************************************************************/
211 /* nega optimizations */
212 /*****************************************************************************/
216 static unsigned OptNegA1 (CodeSeg* S)
223 * Remove the ldx if the lda does not use it.
226 unsigned Changes = 0;
228 /* Walk over the entries */
230 while (I < CS_GetEntryCount (S)) {
235 CodeEntry* E = CS_GetEntry (S, I);
237 /* Check for a ldx */
238 if (E->OPC == OP65_LDX &&
240 (E->Flags & CEF_NUMARG) != 0 &&
242 CS_GetEntries (S, L, I+1, 2) &&
243 L[0]->OPC == OP65_LDA &&
244 (L[0]->Use & REG_X) == 0 &&
245 !CE_HasLabel (L[0]) &&
246 L[1]->OPC == OP65_JSR &&
247 strcmp (L[1]->Arg, "bnega") == 0 &&
248 !CE_HasLabel (L[1])) {
250 /* Remove the ldx instruction */
253 /* Remember, we had changes */
263 /* Return the number of changes made */
269 static unsigned OptNegA2 (CodeSeg* S)
276 * Adjust the conditional branch and remove the call to the subroutine.
279 unsigned Changes = 0;
281 /* Walk over the entries */
283 while (I < CS_GetEntryCount (S)) {
288 CodeEntry* E = CS_GetEntry (S, I);
290 /* Check for the sequence */
291 if ((E->OPC == OP65_ADC ||
292 E->OPC == OP65_AND ||
293 E->OPC == OP65_DEA ||
294 E->OPC == OP65_EOR ||
295 E->OPC == OP65_INA ||
296 E->OPC == OP65_LDA ||
297 E->OPC == OP65_ORA ||
298 E->OPC == OP65_PLA ||
299 E->OPC == OP65_SBC ||
300 E->OPC == OP65_TXA ||
301 E->OPC == OP65_TYA) &&
302 CS_GetEntries (S, L, I+1, 2) &&
303 L[0]->OPC == OP65_JSR &&
304 strcmp (L[0]->Arg, "bnega") == 0 &&
305 !CE_HasLabel (L[0]) &&
306 (L[1]->Info & OF_ZBRA) != 0 &&
307 !CE_HasLabel (L[1])) {
309 /* Invert the branch */
310 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
312 /* Delete the subroutine call */
313 CS_DelEntry (S, I+1);
315 /* Remember, we had changes */
325 /* Return the number of changes made */
331 /*****************************************************************************/
332 /* negax optimizations */
333 /*****************************************************************************/
337 static unsigned OptNegAX1 (CodeSeg* S)
338 /* On a call to bnegax, if X is zero, the result depends only on the value in
339 * A, so change the call to a call to bnega. This will get further optimized
343 unsigned Changes = 0;
346 /* Generate register info for this step */
349 /* Walk over the entries */
351 while (I < CS_GetEntryCount (S)) {
354 CodeEntry* E = CS_GetEntry (S, I);
356 /* Check if this is a call to bnegax, and if X is known and zero */
357 if (E->OPC == OP65_JSR &&
358 E->RI->In.RegX == 0 &&
359 strcmp (E->Arg, "bnegax") == 0) {
361 /* We're cheating somewhat here ... */
374 /* Free register info */
377 /* Return the number of changes made */
383 static unsigned OptNegAX2 (CodeSeg* S)
384 /* Search for the sequence:
401 unsigned Changes = 0;
403 /* Walk over the entries */
405 while (I < CS_GetEntryCount (S)) {
410 CodeEntry* E = CS_GetEntry (S, I);
412 /* Check for the sequence */
413 if (E->OPC == OP65_LDA &&
414 E->AM == AM65_ZP_INDY &&
415 CS_GetEntries (S, L, I+1, 5) &&
416 L[0]->OPC == OP65_TAX &&
417 L[1]->OPC == OP65_DEY &&
418 L[2]->OPC == OP65_LDA &&
419 L[2]->AM == AM65_ZP_INDY &&
420 strcmp (L[2]->Arg, E->Arg) == 0 &&
421 !CE_HasLabel (L[2]) &&
422 L[3]->OPC == OP65_JSR &&
423 strcmp (L[3]->Arg, "bnegax") == 0 &&
424 !CE_HasLabel (L[3]) &&
425 (L[4]->Info & OF_ZBRA) != 0 &&
426 !CE_HasLabel (L[4])) {
429 CE_ReplaceOPC (L[2], OP65_ORA);
431 /* Invert the branch */
432 CE_ReplaceOPC (L[4], GetInverseBranch (L[4]->OPC));
434 /* Delete the entries no longer needed. Beware: Deleting entries
435 * will change the indices.
437 CS_DelEntry (S, I+4); /* jsr bnegax */
438 CS_DelEntry (S, I+1); /* tax */
440 /* Remember, we had changes */
450 /* Return the number of changes made */
456 static unsigned OptNegAX3 (CodeSeg* S)
457 /* Search for the sequence:
471 unsigned Changes = 0;
473 /* Walk over the entries */
475 while (I < CS_GetEntryCount (S)) {
480 CodeEntry* E = CS_GetEntry (S, I);
482 /* Check for the sequence */
483 if (E->OPC == OP65_LDA &&
484 CS_GetEntries (S, L, I+1, 3) &&
485 L[0]->OPC == OP65_LDX &&
486 !CE_HasLabel (L[0]) &&
487 L[1]->OPC == OP65_JSR &&
488 strcmp (L[1]->Arg, "bnegax") == 0 &&
489 !CE_HasLabel (L[1]) &&
490 (L[2]->Info & OF_ZBRA) != 0 &&
491 !CE_HasLabel (L[2])) {
494 CE_ReplaceOPC (L[0], OP65_ORA);
496 /* Invert the branch */
497 CE_ReplaceOPC (L[2], GetInverseBranch (L[2]->OPC));
499 /* Delete the subroutine call */
500 CS_DelEntry (S, I+2);
502 /* Remember, we had changes */
512 /* Return the number of changes made */
518 static unsigned OptNegAX4 (CodeSeg* S)
519 /* Search for the sequence:
532 unsigned Changes = 0;
534 /* Walk over the entries */
536 while (I < CS_GetEntryCount (S)) {
541 CodeEntry* E = CS_GetEntry (S, I);
543 /* Check for the sequence */
544 if (E->OPC == OP65_JSR &&
545 CS_GetEntries (S, L, I+1, 2) &&
546 L[0]->OPC == OP65_JSR &&
547 strncmp (L[0]->Arg,"bnega",5) == 0 &&
548 !CE_HasLabel (L[0]) &&
549 (L[1]->Info & OF_ZBRA) != 0 &&
550 !CE_HasLabel (L[1])) {
554 /* Check if we're calling bnega or bnegax */
555 int ByteSized = (strcmp (L[0]->Arg, "bnega") == 0);
557 /* Insert apropriate test code */
560 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
561 CS_InsertEntry (S, X, I+2);
564 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
565 CS_InsertEntry (S, X, I+2);
566 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, L[0]->LI);
567 CS_InsertEntry (S, X, I+3);
570 /* Delete the subroutine call */
571 CS_DelEntry (S, I+1);
573 /* Invert the branch */
574 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
576 /* Remember, we had changes */
586 /* Return the number of changes made */
592 /*****************************************************************************/
593 /* Optimize stores through pointers */
594 /*****************************************************************************/
598 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
599 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
601 /* Check for a label attached to the entry */
602 if (CE_HasLabel (L[0])) {
606 /* Check for single insn sub ops */
607 if (L[0]->OPC == OP65_AND ||
608 L[0]->OPC == OP65_EOR ||
609 L[0]->OPC == OP65_ORA ||
610 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
611 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
616 } else if (L[0]->OPC == OP65_CLC &&
617 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
618 L[1]->OPC == OP65_ADC &&
619 !CE_HasLabel (L[1])) {
621 } else if (L[0]->OPC == OP65_SEC &&
622 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
623 L[1]->OPC == OP65_SBC &&
624 !CE_HasLabel (L[1])) {
636 static unsigned OptPtrStore1 (CodeSeg* S)
637 /* Search for the sequence:
658 unsigned Changes = 0;
660 /* Walk over the entries */
662 while (I < CS_GetEntryCount (S)) {
668 L[0] = CS_GetEntry (S, I);
670 /* Check for the sequence */
671 if (L[0]->OPC == OP65_JSR &&
672 strcmp (L[0]->Arg, "pushax") == 0 &&
673 CS_GetEntries (S, L+1, I+1, 3) &&
674 L[1]->OPC == OP65_LDY &&
675 CE_KnownImm (L[1]) &&
676 !CE_HasLabel (L[1]) &&
677 L[2]->OPC == OP65_JSR &&
678 strcmp (L[2]->Arg, "ldauidx") == 0 &&
679 !CE_HasLabel (L[2]) &&
680 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
681 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
682 L[3+K]->OPC == OP65_LDY &&
683 CE_KnownImm (L[3+K]) &&
684 !CE_HasLabel (L[3+K]) &&
685 L[4+K]->OPC == OP65_JSR &&
686 strcmp (L[4+K]->Arg, "staspidx") == 0 &&
687 !CE_HasLabel (L[4+K])) {
691 /* Create and insert the stores */
692 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
693 CS_InsertEntry (S, X, I+1);
695 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
696 CS_InsertEntry (S, X, I+2);
698 /* Delete the call to pushax */
701 /* Delete the call to ldauidx */
702 CS_DelEntry (S, I+3);
704 /* Insert the load from ptr1 */
705 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
706 CS_InsertEntry (S, X, I+3);
707 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
708 CS_InsertEntry (S, X, I+4);
710 /* Insert the store through ptr1 */
711 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
712 CS_InsertEntry (S, X, I+6+K);
714 /* Delete the call to staspidx */
715 CS_DelEntry (S, I+7+K);
717 /* Remember, we had changes */
727 /* Return the number of changes made */
733 static unsigned OptPtrStore2 (CodeSeg* S)
734 /* Search for the sequence:
750 unsigned Changes = 0;
752 /* Walk over the entries */
754 while (I < CS_GetEntryCount (S)) {
759 L[0] = CS_GetEntry (S, I);
761 /* Check for the sequence */
762 if (L[0]->OPC == OP65_JSR &&
763 strcmp (L[0]->Arg, "pushax") == 0 &&
764 CS_GetEntries (S, L+1, I+1, 3) &&
765 L[1]->OPC == OP65_LDA &&
766 !CE_HasLabel (L[1]) &&
767 L[2]->OPC == OP65_LDY &&
768 !CE_HasLabel (L[2]) &&
769 L[3]->OPC == OP65_JSR &&
770 strcmp (L[3]->Arg, "staspidx") == 0 &&
771 !CE_HasLabel (L[3])) {
775 /* Create and insert the stores */
776 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
777 CS_InsertEntry (S, X, I+1);
779 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
780 CS_InsertEntry (S, X, I+2);
782 /* Delete the call to pushax */
785 /* Insert the store through ptr1 */
786 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
787 CS_InsertEntry (S, X, I+4);
789 /* Delete the call to staspidx */
790 CS_DelEntry (S, I+5);
792 /* Remember, we had changes */
802 /* Return the number of changes made */
808 /*****************************************************************************/
809 /* Optimize loads through pointers */
810 /*****************************************************************************/
814 static unsigned OptPtrLoad1 (CodeSeg* S)
815 /* Search for the sequence:
819 * lda (sp),y # May be any destination
834 unsigned Changes = 0;
836 /* Walk over the entries */
838 while (I < CS_GetEntryCount (S)) {
843 L[0] = CS_GetEntry (S, I);
845 /* Check for the sequence */
846 if (L[0]->OPC == OP65_TAX &&
847 CS_GetEntries (S, L+1, I+1, 4) &&
848 L[1]->OPC == OP65_DEY &&
849 !CE_HasLabel (L[1]) &&
850 L[2]->OPC == OP65_LDA &&
851 !CE_HasLabel (L[2]) &&
852 L[3]->OPC == OP65_LDY &&
853 !CE_HasLabel (L[3]) &&
854 L[4]->OPC == OP65_JSR &&
855 strcmp (L[4]->Arg, "ldauidx") == 0 &&
856 !CE_HasLabel (L[4])) {
860 /* Store the high byte and remove the TAX instead */
861 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
862 CS_InsertEntry (S, X, I+1);
865 /* Store the low byte */
866 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
867 CS_InsertEntry (S, X, I+3);
869 /* Delete the call to ldauidx */
870 CS_DelEntry (S, I+5);
872 /* Load high and low byte */
873 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
874 CS_InsertEntry (S, X, I+5);
875 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
876 CS_InsertEntry (S, X, I+6);
878 /* Remember, we had changes */
888 /* Return the number of changes made */
894 static unsigned OptPtrLoad2 (CodeSeg* S)
895 /* Search for the sequence:
918 unsigned Changes = 0;
920 /* Walk over the entries */
922 while (I < CS_GetEntryCount (S)) {
927 L[0] = CS_GetEntry (S, I);
929 /* Check for the sequence */
930 if (L[0]->OPC == OP65_ADC &&
931 CS_GetEntries (S, L+1, I+1, 7) &&
932 L[1]->OPC == OP65_TAY &&
933 !CE_HasLabel (L[1]) &&
934 L[2]->OPC == OP65_TXA &&
935 !CE_HasLabel (L[2]) &&
936 L[3]->OPC == OP65_ADC &&
937 !CE_HasLabel (L[3]) &&
938 L[4]->OPC == OP65_TAX &&
939 !CE_HasLabel (L[4]) &&
940 L[5]->OPC == OP65_TYA &&
941 !CE_HasLabel (L[5]) &&
942 L[6]->OPC == OP65_LDY &&
943 !CE_HasLabel (L[6]) &&
944 L[7]->OPC == OP65_JSR &&
945 strcmp (L[7]->Arg, "ldauidx") == 0 &&
946 !CE_HasLabel (L[7])) {
950 /* Store the low byte and remove the TAY instead */
951 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
952 CS_InsertEntry (S, X, I+1);
953 CS_DelEntry (S, I+2);
955 /* Store the high byte */
956 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[3]->LI);
957 CS_InsertEntry (S, X, I+4);
959 /* Delete more transfer insns */
960 CS_DelEntry (S, I+6);
961 CS_DelEntry (S, I+5);
963 /* Delete the call to ldauidx */
964 CS_DelEntry (S, I+6);
966 /* Load high and low byte */
967 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
968 CS_InsertEntry (S, X, I+6);
969 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
970 CS_InsertEntry (S, X, I+7);
972 /* Remember, we had changes */
982 /* Return the number of changes made */
988 static unsigned OptPtrLoad3 (CodeSeg* S)
989 /* Search for the sequence:
1001 * and replace it by:
1014 unsigned Changes = 0;
1016 /* Walk over the entries */
1018 while (I < CS_GetEntryCount (S)) {
1022 /* Get next entry */
1023 L[0] = CS_GetEntry (S, I);
1025 /* Check for the sequence */
1026 if (L[0]->OPC == OP65_ADC &&
1027 CS_GetEntries (S, L+1, I+1, 8) &&
1028 L[1]->OPC == OP65_PHA &&
1029 !CE_HasLabel (L[1]) &&
1030 L[2]->OPC == OP65_TXA &&
1031 !CE_HasLabel (L[2]) &&
1032 L[3]->OPC == OP65_INY &&
1033 !CE_HasLabel (L[3]) &&
1034 L[4]->OPC == OP65_ADC &&
1035 !CE_HasLabel (L[4]) &&
1036 L[5]->OPC == OP65_TAX &&
1037 !CE_HasLabel (L[5]) &&
1038 L[6]->OPC == OP65_PLA &&
1039 !CE_HasLabel (L[6]) &&
1040 L[7]->OPC == OP65_LDY &&
1041 !CE_HasLabel (L[7]) &&
1042 L[8]->OPC == OP65_JSR &&
1043 strcmp (L[8]->Arg, "ldauidx") == 0 &&
1044 !CE_HasLabel (L[8])) {
1048 /* Store the low byte and remove the PHA instead */
1049 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1050 CS_InsertEntry (S, X, I+1);
1051 CS_DelEntry (S, I+2);
1053 /* Store the high byte */
1054 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
1055 CS_InsertEntry (S, X, I+5);
1057 /* Delete more transfer and PLA insns */
1058 CS_DelEntry (S, I+7);
1059 CS_DelEntry (S, I+6);
1061 /* Delete the call to ldauidx */
1062 CS_DelEntry (S, I+7);
1064 /* Load high and low byte */
1065 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
1066 CS_InsertEntry (S, X, I+7);
1067 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
1068 CS_InsertEntry (S, X, I+8);
1070 /* Remember, we had changes */
1080 /* Return the number of changes made */
1086 static unsigned OptPtrLoad4 (CodeSeg* S)
1087 /* Search for the sequence:
1098 * and replace it by:
1105 unsigned Changes = 0;
1107 /* Walk over the entries */
1109 while (I < CS_GetEntryCount (S)) {
1114 /* Get next entry */
1115 L[0] = CS_GetEntry (S, I);
1117 /* Check for the sequence */
1118 if (L[0]->OPC == OP65_LDA &&
1119 L[0]->AM == AM65_IMM &&
1120 CS_GetEntries (S, L+1, I+1, 7) &&
1121 L[1]->OPC == OP65_LDX &&
1122 L[1]->AM == AM65_IMM &&
1123 !CE_HasLabel (L[1]) &&
1124 L[2]->OPC == OP65_CLC &&
1125 !CE_HasLabel (L[2]) &&
1126 L[3]->OPC == OP65_ADC &&
1127 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
1128 !CE_HasLabel (L[3]) &&
1129 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
1130 L[4]->JumpTo != 0 &&
1131 L[4]->JumpTo->Owner == L[6] &&
1132 !CE_HasLabel (L[4]) &&
1133 L[5]->OPC == OP65_INX &&
1134 !CE_HasLabel (L[5]) &&
1135 L[6]->OPC == OP65_LDY &&
1136 CE_KnownImm (L[6]) &&
1138 L[7]->OPC == OP65_JSR &&
1139 strcmp (L[7]->Arg, "ldauidx") == 0 &&
1140 !CE_HasLabel (L[7]) &&
1141 /* Check the label last because this is quite costly */
1142 (Len = strlen (L[0]->Arg)) > 3 &&
1143 L[0]->Arg[0] == '<' &&
1144 L[0]->Arg[1] == '(' &&
1145 strlen (L[1]->Arg) == Len &&
1146 L[1]->Arg[0] == '>' &&
1147 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1152 /* We will create all the new stuff behind the current one so
1153 * we keep the line references.
1155 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
1156 CS_InsertEntry (S, X, I+8);
1158 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1159 CS_InsertEntry (S, X, I+9);
1161 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1162 Label[Len-3] = '\0';
1163 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1164 CS_InsertEntry (S, X, I+10);
1167 /* Remove the old code */
1168 CS_DelEntries (S, I, 8);
1170 /* Remember, we had changes */
1180 /* Return the number of changes made */
1186 static unsigned OptPtrLoad5 (CodeSeg* S)
1187 /* Search for the sequence:
1199 * and replace it by:
1208 unsigned Changes = 0;
1210 /* Walk over the entries */
1212 while (I < CS_GetEntryCount (S)) {
1217 /* Get next entry */
1218 L[0] = CS_GetEntry (S, I);
1220 /* Check for the sequence */
1221 if (L[0]->OPC == OP65_LDA &&
1222 L[0]->AM == AM65_IMM &&
1223 CS_GetEntries (S, L+1, I+1, 8) &&
1224 L[1]->OPC == OP65_LDX &&
1225 L[1]->AM == AM65_IMM &&
1226 !CE_HasLabel (L[1]) &&
1227 L[2]->OPC == OP65_LDY &&
1228 CE_KnownImm (L[2]) &&
1229 !CE_HasLabel (L[2]) &&
1230 L[3]->OPC == OP65_CLC &&
1231 !CE_HasLabel (L[3]) &&
1232 L[4]->OPC == OP65_ADC &&
1233 L[4]->AM == AM65_ZP_INDY &&
1234 !CE_HasLabel (L[4]) &&
1235 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
1236 L[5]->JumpTo != 0 &&
1237 L[5]->JumpTo->Owner == L[7] &&
1238 !CE_HasLabel (L[5]) &&
1239 L[6]->OPC == OP65_INX &&
1240 !CE_HasLabel (L[6]) &&
1241 L[7]->OPC == OP65_LDY &&
1242 CE_KnownImm (L[7]) &&
1244 L[8]->OPC == OP65_JSR &&
1245 strcmp (L[8]->Arg, "ldauidx") == 0 &&
1246 !CE_HasLabel (L[8]) &&
1247 /* Check the label last because this is quite costly */
1248 (Len = strlen (L[0]->Arg)) > 3 &&
1249 L[0]->Arg[0] == '<' &&
1250 L[0]->Arg[1] == '(' &&
1251 strlen (L[1]->Arg) == Len &&
1252 L[1]->Arg[0] == '>' &&
1253 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1259 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1260 CS_InsertEntry (S, X, I+3);
1263 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1264 CS_InsertEntry (S, X, I+4);
1267 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1268 CS_InsertEntry (S, X, I+5);
1271 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1272 Label[Len-3] = '\0';
1273 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1274 CS_InsertEntry (S, X, I+6);
1277 /* Remove the old code */
1278 CS_DelEntries (S, I, 2);
1279 CS_DelEntries (S, I+5, 6);
1281 /* Remember, we had changes */
1291 /* Return the number of changes made */
1297 static unsigned OptPtrLoad6 (CodeSeg* S)
1298 /* Search for the sequence
1303 * and replace it by:
1311 * This step must be execute *after* OptPtrLoad1!
1314 unsigned Changes = 0;
1316 /* Walk over the entries */
1318 while (I < CS_GetEntryCount (S)) {
1322 /* Get next entry */
1323 L[0] = CS_GetEntry (S, I);
1325 /* Check for the sequence */
1326 if (L[0]->OPC == OP65_LDY &&
1327 CS_GetEntries (S, L+1, I+1, 1) &&
1328 L[1]->OPC == OP65_JSR &&
1329 strcmp (L[1]->Arg, "ldauidx") == 0 &&
1330 !CE_HasLabel (L[1])) {
1334 /* Store the high byte */
1335 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1336 CS_InsertEntry (S, X, I+1);
1338 /* Store the low byte */
1339 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1340 CS_InsertEntry (S, X, I+2);
1342 /* Delete the call to ldauidx */
1343 CS_DelEntry (S, I+3);
1345 /* Load the high and low byte */
1346 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1347 CS_InsertEntry (S, X, I+3);
1348 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1349 CS_InsertEntry (S, X, I+4);
1351 /* Remember, we had changes */
1361 /* Return the number of changes made */
1367 /*****************************************************************************/
1368 /* struct OptFunc */
1369 /*****************************************************************************/
1373 typedef struct OptFunc OptFunc;
1375 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1376 const char* Name; /* Name of the function/group */
1377 unsigned long TotalRuns; /* Total number of runs */
1378 unsigned long LastRuns; /* Last number of runs */
1379 unsigned long TotalChanges; /* Total number of changes */
1380 unsigned long LastChanges; /* Last number of changes */
1381 char Disabled; /* True if function disabled */
1386 /*****************************************************************************/
1388 /*****************************************************************************/
1392 /* Macro that builds a function description */
1393 #define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
1395 /* A list of all the function descriptions */
1396 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 0, 0, 0, 0, 0 };
1397 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 0, 0, 0, 0, 0 };
1398 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 0, 0, 0, 0, 0 };
1399 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 0, 0, 0, 0, 0 };
1400 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 0, 0, 0, 0, 0 };
1401 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 0, 0, 0, 0, 0 };
1402 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 0, 0, 0, 0, 0 };
1403 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 0, 0, 0, 0, 0 };
1404 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 0, 0, 0, 0, 0 };
1405 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 0, 0, 0, 0, 0 };
1406 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 0, 0, 0, 0, 0 };
1407 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 0, 0, 0, 0, 0 };
1408 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 0, 0, 0, 0, 0 };
1409 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 0, 0, 0, 0, 0 };
1410 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 0, 0, 0, 0, 0 };
1411 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 0, 0, 0, 0, 0 };
1412 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 0, 0, 0, 0, 0 };
1413 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 0, 0, 0, 0, 0 };
1414 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 0, 0, 0, 0, 0 };
1415 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 0, 0, 0, 0, 0 };
1416 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 0, 0, 0, 0, 0 };
1417 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 0, 0, 0, 0, 0 };
1418 static OptFunc DOptRTS = { OptRTS, "OptRTS", 0, 0, 0, 0, 0 };
1419 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 0, 0, 0, 0, 0 };
1420 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 0, 0, 0, 0, 0 };
1421 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 0, 0, 0, 0, 0 };
1422 static OptFunc DOptRTSJumps = { OptRTSJumps, "OptRTSJumps", 0, 0, 0, 0, 0 };
1423 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 0, 0, 0, 0, 0 };
1424 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 0, 0, 0, 0, 0 };
1425 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 0, 0, 0, 0, 0 };
1426 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 0, 0, 0, 0, 0 };
1427 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 0, 0, 0, 0, 0 };
1428 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 0, 0, 0, 0, 0 };
1429 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 0, 0, 0, 0, 0 };
1430 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 0, 0, 0, 0, 0 };
1431 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 0, 0, 0, 0, 0 };
1432 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0 };
1433 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0 };
1434 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0 };
1435 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0 };
1436 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0 };
1437 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 0, 0, 0, 0, 0 };
1438 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0 };
1440 /* Table containing all the steps in alphabetical order */
1441 static OptFunc* OptFuncs[] = {
1486 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1490 static int CmpOptStep (const void* Key, const void* Func)
1491 /* Compare function for bsearch */
1493 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1498 static OptFunc* FindOptFunc (const char* Name)
1499 /* Find an optimizer step by name in the table and return a pointer. Return
1500 * NULL if no such step is found.
1503 /* Search for the function in the list */
1504 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1510 static OptFunc* GetOptFunc (const char* Name)
1511 /* Find an optimizer step by name in the table and return a pointer. Print an
1512 * error and call AbEnd if not found.
1515 /* Search for the function in the list */
1516 OptFunc* F = FindOptFunc (Name);
1519 AbEnd ("Optimization step `%s' not found", Name);
1526 void DisableOpt (const char* Name)
1527 /* Disable the optimization with the given name */
1529 if (strcmp (Name, "any") == 0) {
1531 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1532 OptFuncs[I]->Disabled = 1;
1535 GetOptFunc(Name)->Disabled = 1;
1541 void EnableOpt (const char* Name)
1542 /* Enable the optimization with the given name */
1544 if (strcmp (Name, "any") == 0) {
1546 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1547 OptFuncs[I]->Disabled = 0;
1550 GetOptFunc(Name)->Disabled = 0;
1556 void ListOptSteps (FILE* F)
1557 /* List all optimization steps */
1560 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1561 fprintf (F, "%s\n", OptFuncs[I]->Name);
1567 static void ReadOptStats (const char* Name)
1568 /* Read the optimizer statistics file */
1573 /* Try to open the file */
1574 FILE* F = fopen (Name, "r");
1576 /* Ignore the error */
1580 /* Read and parse the lines */
1582 while (fgets (Buf, sizeof (Buf), F) != 0) {
1590 unsigned long TotalRuns;
1591 unsigned long TotalChanges;
1596 /* Remove trailing white space including the line terminator */
1599 while (Len > 0 && IsSpace (B[Len-1])) {
1604 /* Remove leading whitespace */
1605 while (IsSpace (*B)) {
1609 /* Check for empty and comment lines */
1610 if (*B == '\0' || *B == ';' || *B == '#') {
1614 /* Parse the line */
1615 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1620 /* Search for the optimizer step. */
1621 Func = FindOptFunc (Name);
1627 /* Found the step, set the fields */
1628 Func->TotalRuns = TotalRuns;
1629 Func->TotalChanges = TotalChanges;
1633 /* Close the file, ignore errors here. */
1639 static void WriteOptStats (const char* Name)
1640 /* Write the optimizer statistics file */
1644 /* Try to open the file */
1645 FILE* F = fopen (Name, "w");
1647 /* Ignore the error */
1651 /* Write the file */
1652 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1653 const OptFunc* O = OptFuncs[I];
1655 "%s %lu %lu %lu %lu\n",
1663 /* Close the file, ignore errors here. */
1669 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1670 /* Run one optimizer function Max times or until there are no more changes */
1672 unsigned Changes, C;
1674 /* Don't run the function if it is disabled */
1679 /* Run this until there are no more changes */
1683 /* Run the function */
1690 F->TotalChanges += C;
1691 F->LastChanges += C;
1693 } while (--Max && C > 0);
1695 /* Return the number of changes */
1701 static void RunOptGroup1 (CodeSeg* S)
1702 /* Run the first group of optimization steps. These steps translate known
1703 * patterns emitted by the code generator into more optimal patterns. Order
1704 * of the steps is important, because some of the steps done earlier cover
1705 * the same patterns as later steps as subpatterns.
1708 RunOptFunc (S, &DOptPtrStore1, 1);
1709 RunOptFunc (S, &DOptPtrStore2, 1);
1710 RunOptFunc (S, &DOptPtrLoad1, 1);
1711 RunOptFunc (S, &DOptPtrLoad2, 1);
1712 RunOptFunc (S, &DOptPtrLoad3, 1);
1713 RunOptFunc (S, &DOptPtrLoad4, 1);
1714 RunOptFunc (S, &DOptPtrLoad5, 1);
1715 RunOptFunc (S, &DOptNegAX1, 1);
1716 RunOptFunc (S, &DOptNegAX2, 1);
1717 RunOptFunc (S, &DOptNegAX3, 1);
1718 RunOptFunc (S, &DOptNegAX4, 1);
1719 RunOptFunc (S, &DOptAdd1, 1);
1720 RunOptFunc (S, &DOptAdd2, 1);
1721 RunOptFunc (S, &DOptShift1, 1);
1726 static void RunOptGroup2 (CodeSeg* S)
1727 /* Run one group of optimization steps. These steps depend on each other,
1728 * that means that one step may allow another step to do additional work,
1729 * so we will repeat the steps as long as we see any changes.
1737 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1738 Changes += RunOptFunc (S, &DOptNegA1, 1);
1739 Changes += RunOptFunc (S, &DOptNegA2, 1);
1740 Changes += RunOptFunc (S, &DOptSub1, 1);
1741 Changes += RunOptFunc (S, &DOptSub2, 1);
1742 Changes += RunOptFunc (S, &DOptAdd3, 1);
1743 Changes += RunOptFunc (S, &DOptJumpCascades, 1);
1744 Changes += RunOptFunc (S, &DOptDeadJumps, 1);
1745 Changes += RunOptFunc (S, &DOptRTS, 1);
1746 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1747 Changes += RunOptFunc (S, &DOptJumpTarget, 1);
1748 Changes += RunOptFunc (S, &DOptCondBranches, 1);
1749 Changes += RunOptFunc (S, &DOptRTSJumps, 1);
1750 Changes += RunOptFunc (S, &DOptBoolTrans, 1);
1751 Changes += RunOptFunc (S, &DOptCmp1, 1);
1752 Changes += RunOptFunc (S, &DOptCmp2, 1);
1753 Changes += RunOptFunc (S, &DOptCmp3, 1);
1754 Changes += RunOptFunc (S, &DOptCmp4, 1);
1755 Changes += RunOptFunc (S, &DOptCmp5, 1);
1756 Changes += RunOptFunc (S, &DOptCmp6, 1);
1757 Changes += RunOptFunc (S, &DOptCmp7, 1);
1758 Changes += RunOptFunc (S, &DOptTest1, 1);
1759 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1760 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1761 Changes += RunOptFunc (S, &DOptDupLoads, 1);
1762 Changes += RunOptFunc (S, &DOptStoreLoad, 1);
1763 Changes += RunOptFunc (S, &DOptTransfers, 1);
1764 Changes += RunOptFunc (S, &DOptStackOps, 1);
1771 static void RunOptGroup3 (CodeSeg* S)
1772 /* The last group of optimization steps. Adjust branches.
1775 RunOptFunc (S, &DOptBranchDist, 3);
1780 void RunOpt (CodeSeg* S)
1781 /* Run the optimizer */
1783 const char* StatFileName;
1785 /* If we shouldn't run the optimizer, bail out */
1790 /* Check if we are requested to write optimizer statistics */
1791 StatFileName = getenv ("CC65_OPTSTATS");
1793 ReadOptStats (StatFileName);
1796 /* Print the name of the function we are working on */
1798 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1800 Print (stdout, 1, "Running optimizer for global code segment\n");
1803 /* Run groups of optimizations */
1808 /* Write statistics */
1810 WriteOptStats (StatFileName);