1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
59 #include "coptstore.h"
68 /*****************************************************************************/
70 /*****************************************************************************/
85 /*****************************************************************************/
87 /*****************************************************************************/
91 static unsigned OptShift1 (CodeSeg* S)
92 /* A call to the shlaxN routine may get replaced by one or more asl insns
93 * if the value of X is not used later.
98 /* Walk over the entries */
100 while (I < CS_GetEntryCount (S)) {
103 CodeEntry* E = CS_GetEntry (S, I);
105 /* Check for the sequence */
106 if (E->OPC == OP65_JSR &&
107 (strncmp (E->Arg, "shlax", 5) == 0 ||
108 strncmp (E->Arg, "aslax", 5) == 0) &&
109 strlen (E->Arg) == 6 &&
110 IsDigit (E->Arg[5]) &&
111 !RegXUsed (S, I+1)) {
113 /* Insert shift insns */
114 unsigned Count = E->Arg[5] - '0';
116 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
117 CS_InsertEntry (S, X, I+1);
120 /* Delete the call to shlax */
123 /* Remember, we had changes */
133 /* Return the number of changes made */
139 static unsigned OptShift2 (CodeSeg* S)
140 /* A call to the shraxN routine may get replaced by one or more lsr insns
141 * if the value of X is not used later, or if the value of X is known and
145 unsigned Changes = 0;
148 /* Generate register info */
151 /* Walk over the entries */
153 while (I < CS_GetEntryCount (S)) {
156 CodeEntry* E = CS_GetEntry (S, I);
158 /* Check for the sequence */
159 if (E->OPC == OP65_JSR &&
160 strncmp (E->Arg, "shrax", 5) == 0 &&
161 strlen (E->Arg) == 6 &&
162 IsDigit (E->Arg[5]) &&
163 (E->RI->In.RegX == 0 || !RegXUsed (S, I+1))) {
165 /* Insert shift insns */
166 unsigned Count = E->Arg[5] - '0';
168 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
169 CS_InsertEntry (S, X, I+1);
172 /* Delete the call to shlax */
175 /* Remember, we had changes */
185 /* Free the register info */
188 /* Return the number of changes made */
194 static unsigned GetShiftType (const char* Sub)
195 /* Helper function for OptShift3 */
198 if (strcmp (Sub+1, "slax1") == 0) {
200 } else if (strcmp (Sub+1, "srax1") == 0) {
203 } else if (*Sub == 's') {
204 if (strcmp (Sub+1, "hlax1") == 0) {
206 } else if (strcmp (Sub+1, "hrax1") == 0) {
215 static unsigned OptShift3 (CodeSeg* S)
216 /* Search for the sequence
220 * jsr aslax1/asrax1/shlax1/shrax1
233 * or similar, provided that a/x is not used later
236 unsigned Changes = 0;
238 /* Walk over the entries */
240 while (I < CS_GetEntryCount (S)) {
246 L[0] = CS_GetEntry (S, I);
248 /* Check for the sequence */
249 if (L[0]->OPC == OP65_LDA &&
250 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
251 CS_GetEntries (S, L+1, I+1, 4) &&
252 !CS_RangeHasLabel (S, I+1, 4) &&
253 L[1]->OPC == OP65_LDX &&
254 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
255 L[2]->OPC == OP65_JSR &&
256 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
257 L[3]->OPC == OP65_STA &&
258 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
259 L[4]->OPC == OP65_STX &&
260 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
261 !RegAXUsed (S, I+5)) {
265 /* Handle the four shift types differently */
269 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
270 CS_InsertEntry (S, X, I+5);
271 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
272 CS_InsertEntry (S, X, I+6);
273 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
274 CS_InsertEntry (S, X, I+7);
275 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
276 CS_InsertEntry (S, X, I+8);
277 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
278 CS_InsertEntry (S, X, I+9);
279 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
280 CS_InsertEntry (S, X, I+10);
281 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
282 CS_InsertEntry (S, X, I+11);
283 CS_DelEntries (S, I, 5);
287 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
288 CS_InsertEntry (S, X, I+5);
289 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
290 CS_InsertEntry (S, X, I+6);
291 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
292 CS_InsertEntry (S, X, I+7);
293 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
294 CS_InsertEntry (S, X, I+8);
295 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
296 CS_InsertEntry (S, X, I+9);
297 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
298 CS_InsertEntry (S, X, I+10);
299 CS_DelEntries (S, I, 5);
304 /* These two are identical */
305 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
306 CS_InsertEntry (S, X, I+1);
307 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
308 CS_InsertEntry (S, X, I+2);
309 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
310 CS_InsertEntry (S, X, I+3);
311 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
312 CS_InsertEntry (S, X, I+4);
313 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
314 CS_InsertEntry (S, X, I+5);
315 CS_DelEntries (S, I+6, 4);
320 /* Remember, we had changes */
330 /* Return the number of changes made */
336 /*****************************************************************************/
338 /*****************************************************************************/
342 static unsigned OptLoad1 (CodeSeg* S)
343 /* Search for a call to ldaxysp where X is not used later and replace it by
344 * a load of just the A register.
348 unsigned Changes = 0;
350 /* Generate register info */
353 /* Walk over the entries */
355 while (I < CS_GetEntryCount (S)) {
360 E = CS_GetEntry (S, I);
362 /* Check for the sequence */
363 if (CE_IsCallTo (E, "ldaxysp") &&
364 RegValIsKnown (E->RI->In.RegY) &&
365 !RegXUsed (S, I+1)) {
369 /* Reload the Y register */
370 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
371 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
372 CS_InsertEntry (S, X, I+1);
374 /* Load from stack */
375 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
376 CS_InsertEntry (S, X, I+2);
378 /* Now remove the call to the subroutine */
381 /* Remember, we had changes */
391 /* Free the register info */
394 /* Return the number of changes made */
400 /*****************************************************************************/
401 /* Optimize stores through pointers */
402 /*****************************************************************************/
406 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
407 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
409 /* Check for a label attached to the entry */
410 if (CE_HasLabel (L[0])) {
414 /* Check for single insn sub ops */
415 if (L[0]->OPC == OP65_AND ||
416 L[0]->OPC == OP65_EOR ||
417 L[0]->OPC == OP65_ORA ||
418 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
419 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
424 } else if (L[0]->OPC == OP65_CLC &&
425 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
426 L[1]->OPC == OP65_ADC &&
427 !CE_HasLabel (L[1])) {
429 } else if (L[0]->OPC == OP65_SEC &&
430 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
431 L[1]->OPC == OP65_SBC &&
432 !CE_HasLabel (L[1])) {
444 static unsigned OptPtrStore1 (CodeSeg* S)
445 /* Search for the sequence:
466 unsigned Changes = 0;
468 /* Walk over the entries */
470 while (I < CS_GetEntryCount (S)) {
476 L[0] = CS_GetEntry (S, I);
478 /* Check for the sequence */
479 if (CE_IsCallTo (L[0], "pushax") &&
480 CS_GetEntries (S, L+1, I+1, 3) &&
481 L[1]->OPC == OP65_LDY &&
482 CE_KnownImm (L[1]) &&
483 !CE_HasLabel (L[1]) &&
484 CE_IsCallTo (L[2], "ldauidx") &&
485 !CE_HasLabel (L[2]) &&
486 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
487 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
488 L[3+K]->OPC == OP65_LDY &&
489 CE_KnownImm (L[3+K]) &&
490 !CE_HasLabel (L[3+K]) &&
491 CE_IsCallTo (L[4+K], "staspidx") &&
492 !CE_HasLabel (L[4+K])) {
496 /* Create and insert the stores */
497 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
498 CS_InsertEntry (S, X, I+1);
500 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
501 CS_InsertEntry (S, X, I+2);
503 /* Insert the load from ptr1 */
504 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
505 CS_InsertEntry (S, X, I+5);
506 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
507 CS_InsertEntry (S, X, I+6);
509 /* Insert the store through ptr1 */
510 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
511 CS_InsertEntry (S, X, I+8+K);
513 /* Delete the old code */
514 CS_DelEntry (S, I+9+K); /* jsr spaspidx */
515 CS_DelEntry (S, I+4); /* jsr ldauidx */
516 CS_DelEntry (S, I); /* jsr pushax */
518 /* Remember, we had changes */
528 /* Return the number of changes made */
534 static unsigned OptPtrStore2 (CodeSeg* S)
535 /* Search for the sequence:
557 unsigned Changes = 0;
559 /* Walk over the entries */
561 while (I < CS_GetEntryCount (S)) {
567 L[0] = CS_GetEntry (S, I);
569 /* Check for the sequence */
570 if (L[0]->OPC == OP65_LDA &&
571 L[0]->AM == AM65_IMM &&
572 CS_GetEntries (S, L+1, I+1, 10) &&
573 L[1]->OPC == OP65_LDX &&
574 L[1]->AM == AM65_IMM &&
575 L[2]->OPC == OP65_CLC &&
576 L[3]->OPC == OP65_ADC &&
577 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
578 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
580 L[4]->JumpTo->Owner == L[6] &&
581 L[5]->OPC == OP65_INX &&
582 CE_IsCallTo (L[6], "pushax") &&
583 L[7]->OPC == OP65_LDX &&
584 L[8]->OPC == OP65_LDA &&
585 L[9]->OPC == OP65_LDY &&
586 CE_KnownImm (L[9]) &&
588 CE_IsCallTo (L[10], "staspidx") &&
589 !CS_RangeHasLabel (S, I+1, 5) &&
590 !CS_RangeHasLabel (S, I+7, 4) &&
591 /* Check the label last because this is quite costly */
592 (Len = strlen (L[0]->Arg)) > 3 &&
593 L[0]->Arg[0] == '<' &&
594 L[0]->Arg[1] == '(' &&
595 strlen (L[1]->Arg) == Len &&
596 L[1]->Arg[0] == '>' &&
597 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
602 /* We will create all the new stuff behind the current one so
603 * we keep the line references.
605 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
606 CS_InsertEntry (S, X, I+11);
608 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
609 CS_InsertEntry (S, X, I+12);
611 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
612 CS_InsertEntry (S, X, I+13);
614 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
616 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
617 CS_InsertEntry (S, X, I+14);
620 /* Remove the old code */
621 CS_DelEntries (S, I, 11);
623 /* Remember, we had changes */
633 /* Return the number of changes made */
639 /*****************************************************************************/
640 /* Optimize loads through pointers */
641 /*****************************************************************************/
645 static unsigned OptPtrLoad1 (CodeSeg* S)
646 /* Search for the sequence:
671 unsigned Changes = 0;
673 /* Walk over the entries */
675 while (I < CS_GetEntryCount (S)) {
680 L[0] = CS_GetEntry (S, I);
682 /* Check for the sequence */
683 if (L[0]->OPC == OP65_CLC &&
684 CS_GetEntries (S, L+1, I+1, 8) &&
685 L[1]->OPC == OP65_ADC &&
686 L[2]->OPC == OP65_TAY &&
687 L[3]->OPC == OP65_TXA &&
688 L[4]->OPC == OP65_ADC &&
689 L[5]->OPC == OP65_TAX &&
690 L[6]->OPC == OP65_TYA &&
691 L[7]->OPC == OP65_LDY &&
692 CE_IsCallTo (L[8], "ldauidx") &&
693 !CS_RangeHasLabel (S, I+1, 8)) {
698 /* Track the insertion point */
702 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
703 CS_InsertEntry (S, X, IP++);
705 /* If the instruction before the clc is a ldx, replace the
706 * txa by an lda with the same location of the ldx. Otherwise
707 * transfer the value in X to A.
709 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
710 P->OPC == OP65_LDX &&
712 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
714 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
716 CS_InsertEntry (S, X, IP++);
719 X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
720 CS_InsertEntry (S, X, IP++);
723 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
724 CS_InsertEntry (S, X, IP++);
727 X = NewCodeEntry (OP65_LDY, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
728 CS_InsertEntry (S, X, IP++);
731 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
732 CS_InsertEntry (S, X, IP++);
735 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
736 CS_InsertEntry (S, X, IP++);
738 /* Remove the old instructions */
739 CS_DelEntries (S, IP, 7);
741 /* Remember, we had changes */
751 /* Return the number of changes made */
757 static unsigned OptPtrLoad2 (CodeSeg* S)
758 /* Search for the sequence:
783 unsigned Changes = 0;
785 /* Walk over the entries */
787 while (I < CS_GetEntryCount (S)) {
792 L[0] = CS_GetEntry (S, I);
794 /* Check for the sequence */
795 if (L[0]->OPC == OP65_ADC &&
796 CS_GetEntries (S, L+1, I+1, 8) &&
797 L[1]->OPC == OP65_PHA &&
798 L[2]->OPC == OP65_TXA &&
799 L[3]->OPC == OP65_INY &&
800 L[4]->OPC == OP65_ADC &&
801 L[5]->OPC == OP65_TAX &&
802 L[6]->OPC == OP65_PLA &&
803 L[7]->OPC == OP65_LDY &&
804 CE_IsCallTo (L[8], "ldauidx") &&
805 !CS_RangeHasLabel (S, I+1, 8)) {
809 /* Store the low byte and remove the PHA instead */
810 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
811 CS_InsertEntry (S, X, I+1);
813 /* Store the high byte */
814 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
815 CS_InsertEntry (S, X, I+6);
817 /* Load high and low byte */
818 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
819 CS_InsertEntry (S, X, I+10);
820 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
821 CS_InsertEntry (S, X, I+11);
823 /* Delete the old code */
824 CS_DelEntry (S, I+12); /* jsr ldauidx */
825 CS_DelEntry (S, I+8); /* pla */
826 CS_DelEntry (S, I+7); /* tax */
827 CS_DelEntry (S, I+2); /* pha */
829 /* Remember, we had changes */
839 /* Return the number of changes made */
845 static unsigned OptPtrLoad3 (CodeSeg* S)
846 /* Search for the sequence:
864 unsigned Changes = 0;
866 /* Walk over the entries */
868 while (I < CS_GetEntryCount (S)) {
874 L[0] = CS_GetEntry (S, I);
876 /* Check for the sequence */
877 if (L[0]->OPC == OP65_LDA &&
878 L[0]->AM == AM65_IMM &&
879 CS_GetEntries (S, L+1, I+1, 7) &&
880 L[1]->OPC == OP65_LDX &&
881 L[1]->AM == AM65_IMM &&
882 L[2]->OPC == OP65_CLC &&
883 L[3]->OPC == OP65_ADC &&
884 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
885 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
887 L[4]->JumpTo->Owner == L[6] &&
888 L[5]->OPC == OP65_INX &&
889 L[6]->OPC == OP65_LDY &&
890 CE_KnownImm (L[6]) &&
892 CE_IsCallTo (L[7], "ldauidx") &&
893 !CS_RangeHasLabel (S, I+1, 5) &&
894 !CE_HasLabel (L[7]) &&
895 /* Check the label last because this is quite costly */
896 (Len = strlen (L[0]->Arg)) > 3 &&
897 L[0]->Arg[0] == '<' &&
898 L[0]->Arg[1] == '(' &&
899 strlen (L[1]->Arg) == Len &&
900 L[1]->Arg[0] == '>' &&
901 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
906 /* We will create all the new stuff behind the current one so
907 * we keep the line references.
909 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
910 CS_InsertEntry (S, X, I+8);
912 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
913 CS_InsertEntry (S, X, I+9);
915 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
917 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
918 CS_InsertEntry (S, X, I+10);
921 /* Remove the old code */
922 CS_DelEntries (S, I, 8);
924 /* Remember, we had changes */
934 /* Return the number of changes made */
940 static unsigned OptPtrLoad4 (CodeSeg* S)
941 /* Search for the sequence:
962 unsigned Changes = 0;
964 /* Walk over the entries */
966 while (I < CS_GetEntryCount (S)) {
972 L[0] = CS_GetEntry (S, I);
974 /* Check for the sequence */
975 if (L[0]->OPC == OP65_LDA &&
976 L[0]->AM == AM65_IMM &&
977 CS_GetEntries (S, L+1, I+1, 8) &&
978 L[1]->OPC == OP65_LDX &&
979 L[1]->AM == AM65_IMM &&
980 !CE_HasLabel (L[1]) &&
981 L[2]->OPC == OP65_LDY &&
982 CE_KnownImm (L[2]) &&
983 !CE_HasLabel (L[2]) &&
984 L[3]->OPC == OP65_CLC &&
985 !CE_HasLabel (L[3]) &&
986 L[4]->OPC == OP65_ADC &&
987 L[4]->AM == AM65_ZP_INDY &&
988 !CE_HasLabel (L[4]) &&
989 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
991 L[5]->JumpTo->Owner == L[7] &&
992 !CE_HasLabel (L[5]) &&
993 L[6]->OPC == OP65_INX &&
994 !CE_HasLabel (L[6]) &&
995 L[7]->OPC == OP65_LDY &&
996 CE_KnownImm (L[7]) &&
998 CE_IsCallTo (L[8], "ldauidx") &&
999 !CE_HasLabel (L[8]) &&
1000 /* Check the label last because this is quite costly */
1001 (Len = strlen (L[0]->Arg)) > 3 &&
1002 L[0]->Arg[0] == '<' &&
1003 L[0]->Arg[1] == '(' &&
1004 strlen (L[1]->Arg) == Len &&
1005 L[1]->Arg[0] == '>' &&
1006 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1012 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1013 CS_InsertEntry (S, X, I+3);
1016 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1017 CS_InsertEntry (S, X, I+4);
1020 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1021 CS_InsertEntry (S, X, I+5);
1024 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1025 Label[Len-3] = '\0';
1026 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1027 CS_InsertEntry (S, X, I+6);
1030 /* Remove the old code */
1031 CS_DelEntries (S, I, 2);
1032 CS_DelEntries (S, I+5, 6);
1034 /* Remember, we had changes */
1044 /* Return the number of changes made */
1050 static unsigned OptPtrLoad5 (CodeSeg* S)
1051 /* Search for the sequence:
1058 * and replace it by:
1065 unsigned Changes = 0;
1067 /* Walk over the entries */
1069 while (I < CS_GetEntryCount (S)) {
1074 /* Get next entry */
1075 L[0] = CS_GetEntry (S, I);
1077 /* Check for the sequence */
1078 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1079 CS_GetEntries (S, L+1, I+1, 3) &&
1080 !CS_RangeHasLabel (S, I+1, 3) &&
1081 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1082 (Len = strlen (L[0]->Arg)) > 0 &&
1083 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1084 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1085 L[2]->OPC == OP65_LDY &&
1086 CE_IsCallTo (L[3], "ldauidx")) {
1091 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1092 CS_InsertEntry (S, X, I+3);
1095 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1096 CS_InsertEntry (S, X, I+4);
1098 /* Remove the old code */
1099 CS_DelEntry (S, I+5);
1100 CS_DelEntries (S, I, 2);
1102 /* Remember, we had changes */
1112 /* Return the number of changes made */
1118 static unsigned OptPtrLoad6 (CodeSeg* S)
1119 /* Search for the sequence:
1126 * and replace it by:
1135 unsigned Changes = 0;
1137 /* Walk over the entries */
1139 while (I < CS_GetEntryCount (S)) {
1144 /* Get next entry */
1145 L[0] = CS_GetEntry (S, I);
1147 /* Check for the sequence */
1148 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1149 CS_GetEntries (S, L+1, I+1, 3) &&
1150 !CS_RangeHasLabel (S, I+1, 3) &&
1151 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1152 (Len = strlen (L[0]->Arg)) > 0 &&
1153 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1154 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1155 L[2]->OPC == OP65_LDY &&
1156 CE_IsCallTo (L[3], "ldaxidx")) {
1161 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1162 CS_InsertEntry (S, X, I+4);
1165 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1166 CS_InsertEntry (S, X, I+5);
1169 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1170 CS_InsertEntry (S, X, I+6);
1173 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1174 CS_InsertEntry (S, X, I+7);
1176 /* Remove the old code */
1177 CS_DelEntry (S, I+3);
1178 CS_DelEntries (S, I, 2);
1180 /* Remember, we had changes */
1190 /* Return the number of changes made */
1196 static unsigned OptPtrLoad7 (CodeSeg* S)
1197 /* Search for the sequence
1202 * and replace it by:
1210 * This step must be execute *after* OptPtrLoad1!
1213 unsigned Changes = 0;
1215 /* Walk over the entries */
1217 while (I < CS_GetEntryCount (S)) {
1221 /* Get next entry */
1222 L[0] = CS_GetEntry (S, I);
1224 /* Check for the sequence */
1225 if (L[0]->OPC == OP65_LDY &&
1226 CS_GetEntries (S, L+1, I+1, 1) &&
1227 CE_IsCallTo (L[1], "ldauidx") &&
1228 !CE_HasLabel (L[1])) {
1232 /* Store the high byte */
1233 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1234 CS_InsertEntry (S, X, I+1);
1236 /* Store the low byte */
1237 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1238 CS_InsertEntry (S, X, I+2);
1240 /* Delete the call to ldauidx */
1241 CS_DelEntry (S, I+3);
1243 /* Load the high and low byte */
1244 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1245 CS_InsertEntry (S, X, I+3);
1246 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1247 CS_InsertEntry (S, X, I+4);
1249 /* Remember, we had changes */
1259 /* Return the number of changes made */
1265 /*****************************************************************************/
1266 /* Decouple operations */
1267 /*****************************************************************************/
1271 static unsigned OptDecouple (CodeSeg* S)
1272 /* Decouple operations, that is, do the following replacements:
1282 * lda zp -> lda #imm
1283 * ldx zp -> ldx #imm
1284 * ldy zp -> ldy #imm
1286 * Provided that the register values are known of course.
1289 unsigned Changes = 0;
1292 /* Generate register info for the following step */
1295 /* Walk over the entries */
1297 while (I < CS_GetEntryCount (S)) {
1301 /* Get next entry and it's input register values */
1302 CodeEntry* E = CS_GetEntry (S, I);
1303 const RegContents* In = &E->RI->In;
1305 /* Assume we have no replacement */
1308 /* Check the instruction */
1312 if (E->RI->In.RegX >= 0) {
1313 Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
1314 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1319 if (E->RI->In.RegY >= 0) {
1320 Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1321 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1326 if (E->RI->In.RegX >= 0) {
1327 Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1328 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1333 if (E->RI->In.RegY >= 0) {
1334 Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1335 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1340 if (E->AM == AM65_ZP) {
1341 switch (GetKnownReg (E->Use, In)) {
1343 Arg = MakeHexArg (In->Tmp1);
1344 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1348 Arg = MakeHexArg (In->SRegLo);
1349 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1353 Arg = MakeHexArg (In->SRegHi);
1354 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1361 if (E->AM == AM65_ZP) {
1362 switch (GetKnownReg (E->Use, In)) {
1364 Arg = MakeHexArg (In->Tmp1);
1365 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1369 Arg = MakeHexArg (In->SRegLo);
1370 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1374 Arg = MakeHexArg (In->SRegHi);
1375 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1382 if (E->AM == AM65_ZP) {
1383 switch (GetKnownReg (E->Use, In)) {
1385 Arg = MakeHexArg (In->Tmp1);
1386 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1390 Arg = MakeHexArg (In->SRegLo);
1391 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1395 Arg = MakeHexArg (In->SRegHi);
1396 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1403 if (E->RI->In.RegA >= 0) {
1404 Arg = MakeHexArg (In->RegA);
1405 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1410 if (E->RI->In.RegA >= 0) {
1411 Arg = MakeHexArg (In->RegA);
1412 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1417 if (E->RI->In.RegX >= 0) {
1418 Arg = MakeHexArg (In->RegX);
1419 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1424 if (E->RI->In.RegY >= 0) {
1425 Arg = MakeHexArg (In->RegY);
1426 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1431 /* Avoid gcc warnings */
1436 /* Insert the replacement if we have one */
1438 CS_InsertEntry (S, X, I+1);
1448 /* Free register info */
1451 /* Return the number of changes made */
1457 /*****************************************************************************/
1458 /* struct OptFunc */
1459 /*****************************************************************************/
1463 typedef struct OptFunc OptFunc;
1465 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1466 const char* Name; /* Name of the function/group */
1467 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1468 unsigned long TotalRuns; /* Total number of runs */
1469 unsigned long LastRuns; /* Last number of runs */
1470 unsigned long TotalChanges; /* Total number of changes */
1471 unsigned long LastChanges; /* Last number of changes */
1472 char Disabled; /* True if function disabled */
1477 /*****************************************************************************/
1479 /*****************************************************************************/
1483 /* A list of all the function descriptions */
1484 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1485 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1486 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1487 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1488 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1489 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 90, 0, 0, 0, 0, 0 };
1490 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 100, 0, 0, 0, 0, 0 };
1491 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 40, 0, 0, 0, 0, 0 };
1492 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1493 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1494 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1495 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1496 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1497 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1498 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1499 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1500 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1501 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1502 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1503 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1504 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1505 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1506 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1507 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1508 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1509 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1510 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1511 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1512 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1513 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1514 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1515 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1516 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1517 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1518 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1519 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1520 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1521 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1522 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1523 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 65, 0, 0, 0, 0, 0 };
1524 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 86, 0, 0, 0, 0, 0 };
1525 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 100, 0, 0, 0, 0, 0 };
1526 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1527 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1528 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1529 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1530 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1531 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1532 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1533 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1534 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1535 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1536 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1537 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1538 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1539 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1540 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1541 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1542 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1543 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1544 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1545 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1546 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1547 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1548 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1551 /* Table containing all the steps in alphabetical order */
1552 static OptFunc* OptFuncs[] = {
1619 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1623 static int CmpOptStep (const void* Key, const void* Func)
1624 /* Compare function for bsearch */
1626 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1631 static OptFunc* FindOptFunc (const char* Name)
1632 /* Find an optimizer step by name in the table and return a pointer. Return
1633 * NULL if no such step is found.
1636 /* Search for the function in the list */
1637 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1643 static OptFunc* GetOptFunc (const char* Name)
1644 /* Find an optimizer step by name in the table and return a pointer. Print an
1645 * error and call AbEnd if not found.
1648 /* Search for the function in the list */
1649 OptFunc* F = FindOptFunc (Name);
1652 AbEnd ("Optimization step `%s' not found", Name);
1659 void DisableOpt (const char* Name)
1660 /* Disable the optimization with the given name */
1662 if (strcmp (Name, "any") == 0) {
1664 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1665 OptFuncs[I]->Disabled = 1;
1668 GetOptFunc(Name)->Disabled = 1;
1674 void EnableOpt (const char* Name)
1675 /* Enable the optimization with the given name */
1677 if (strcmp (Name, "any") == 0) {
1679 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1680 OptFuncs[I]->Disabled = 0;
1683 GetOptFunc(Name)->Disabled = 0;
1689 void ListOptSteps (FILE* F)
1690 /* List all optimization steps */
1693 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1694 fprintf (F, "%s\n", OptFuncs[I]->Name);
1700 static void ReadOptStats (const char* Name)
1701 /* Read the optimizer statistics file */
1706 /* Try to open the file */
1707 FILE* F = fopen (Name, "r");
1709 /* Ignore the error */
1713 /* Read and parse the lines */
1715 while (fgets (Buf, sizeof (Buf), F) != 0) {
1723 unsigned long TotalRuns;
1724 unsigned long TotalChanges;
1729 /* Remove trailing white space including the line terminator */
1732 while (Len > 0 && IsSpace (B[Len-1])) {
1737 /* Remove leading whitespace */
1738 while (IsSpace (*B)) {
1742 /* Check for empty and comment lines */
1743 if (*B == '\0' || *B == ';' || *B == '#') {
1747 /* Parse the line */
1748 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1753 /* Search for the optimizer step. */
1754 Func = FindOptFunc (Name);
1760 /* Found the step, set the fields */
1761 Func->TotalRuns = TotalRuns;
1762 Func->TotalChanges = TotalChanges;
1766 /* Close the file, ignore errors here. */
1772 static void WriteOptStats (const char* Name)
1773 /* Write the optimizer statistics file */
1777 /* Try to open the file */
1778 FILE* F = fopen (Name, "w");
1780 /* Ignore the error */
1784 /* Write a header */
1786 "; Optimizer Total Last Total Last\n"
1787 "; Step Runs Runs Chg Chg\n");
1790 /* Write the data */
1791 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1792 const OptFunc* O = OptFuncs[I];
1794 "%-20s %10lu %10lu %10lu %10lu\n",
1802 /* Close the file, ignore errors here. */
1808 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1809 /* Run one optimizer function Max times or until there are no more changes */
1811 unsigned Changes, C;
1813 /* Don't run the function if it is disabled or if it is prohibited by the
1816 if (F->Disabled || F->CodeSizeFactor > CodeSizeFactor) {
1820 /* Run this until there are no more changes */
1824 /* Run the function */
1831 F->TotalChanges += C;
1832 F->LastChanges += C;
1834 } while (--Max && C > 0);
1836 /* Return the number of changes */
1842 static unsigned RunOptGroup1 (CodeSeg* S)
1843 /* Run the first group of optimization steps. These steps translate known
1844 * patterns emitted by the code generator into more optimal patterns. Order
1845 * of the steps is important, because some of the steps done earlier cover
1846 * the same patterns as later steps as subpatterns.
1849 unsigned Changes = 0;
1851 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1852 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1853 Changes += RunOptFunc (S, &DOptPrecalc, 1);
1854 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1855 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1856 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1857 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1858 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1859 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1860 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1861 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1862 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1863 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1864 Changes += RunOptFunc (S, &DOptAdd1, 1);
1865 Changes += RunOptFunc (S, &DOptAdd2, 1);
1866 Changes += RunOptFunc (S, &DOptAdd3, 1);
1867 Changes += RunOptFunc (S, &DOptShift1, 1);
1868 Changes += RunOptFunc (S, &DOptShift2, 1);
1869 Changes += RunOptFunc (S, &DOptShift3, 1);
1870 Changes += RunOptFunc (S, &DOptStore1, 1);
1871 Changes += RunOptFunc (S, &DOptStore2, 5);
1872 Changes += RunOptFunc (S, &DOptStore3, 5);
1873 Changes += RunOptFunc (S, &DOptStore4, 1);
1875 /* Return the number of changes */
1881 static unsigned RunOptGroup2 (CodeSeg* S)
1882 /* Run one group of optimization steps. This step involves just decoupling
1883 * instructions by replacing them by instructions that do not depend on
1884 * previous instructions. This makes it easier to find instructions that
1888 unsigned Changes = 0;
1890 Changes += RunOptFunc (S, &DOptDecouple, 1);
1892 /* Return the number of changes */
1898 static unsigned RunOptGroup3 (CodeSeg* S)
1899 /* Run one group of optimization steps. These steps depend on each other,
1900 * that means that one step may allow another step to do additional work,
1901 * so we will repeat the steps as long as we see any changes.
1904 unsigned Changes, C;
1910 C += RunOptFunc (S, &DOptPtrLoad7, 1);
1911 C += RunOptFunc (S, &DOptNegA1, 1);
1912 C += RunOptFunc (S, &DOptNegA2, 1);
1913 C += RunOptFunc (S, &DOptSub1, 1);
1914 C += RunOptFunc (S, &DOptSub2, 1);
1915 C += RunOptFunc (S, &DOptAdd4, 1);
1916 C += RunOptFunc (S, &DOptAdd5, 1);
1917 C += RunOptFunc (S, &DOptStackOps, 1);
1918 C += RunOptFunc (S, &DOptJumpCascades, 1);
1919 C += RunOptFunc (S, &DOptDeadJumps, 1);
1920 C += RunOptFunc (S, &DOptRTS, 1);
1921 C += RunOptFunc (S, &DOptDeadCode, 1);
1922 C += RunOptFunc (S, &DOptJumpTarget, 1);
1923 C += RunOptFunc (S, &DOptCondBranches, 1);
1924 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1925 C += RunOptFunc (S, &DOptBoolTrans, 1);
1926 C += RunOptFunc (S, &DOptCmp1, 1);
1927 C += RunOptFunc (S, &DOptCmp2, 1);
1928 C += RunOptFunc (S, &DOptCmp3, 1);
1929 C += RunOptFunc (S, &DOptCmp4, 1);
1930 C += RunOptFunc (S, &DOptCmp5, 1);
1931 C += RunOptFunc (S, &DOptCmp6, 1);
1932 C += RunOptFunc (S, &DOptCmp7, 1);
1933 C += RunOptFunc (S, &DOptTest1, 1);
1934 C += RunOptFunc (S, &DOptLoad1, 1);
1935 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1936 C += RunOptFunc (S, &DOptUnusedStores, 1);
1937 C += RunOptFunc (S, &DOptDupLoads, 1);
1938 C += RunOptFunc (S, &DOptStoreLoad, 1);
1939 C += RunOptFunc (S, &DOptTransfers1, 1);
1940 C += RunOptFunc (S, &DOptPushPop, 1);
1946 /* Return the number of changes */
1952 static unsigned RunOptGroup4 (CodeSeg* S)
1953 /* 65C02 specific optimizations. */
1955 unsigned Changes = 0;
1957 if (CPU >= CPU_65C02) {
1958 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1959 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1960 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1962 /* The 65C02 replacement codes do often make the use of a register
1963 * value unnecessary, so if we have changes, run another load
1966 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1970 /* Return the number of changes */
1976 static unsigned RunOptGroup5 (CodeSeg* S)
1977 /* Run another round of pattern replacements. These are done late, since there
1978 * may be better replacements before.
1981 unsigned Changes = 0;
1983 Changes += RunOptFunc (S, &DOptPush1, 1);
1984 Changes += RunOptFunc (S, &DOptPush2, 1);
1985 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1986 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1988 /* Return the number of changes */
1994 static unsigned RunOptGroup6 (CodeSeg* S)
1995 /* The last group of optimization steps. Adjust branches, do size optimizations.
1998 unsigned Changes = 0;
2001 if (CodeSizeFactor <= 100) {
2002 /* Optimize for size, that is replace operations by shorter ones, even
2003 * if this does hinder further optimizations (no problem since we're
2006 C = RunOptFunc (S, &DOptSize1, 1);
2009 /* Run some optimization passes again, since the size optimizations
2010 * may have opened new oportunities.
2012 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2013 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
2016 C = RunOptFunc (S, &DOptSize2, 1);
2019 /* Run some optimization passes again, since the size optimizations
2020 * may have opened new oportunities.
2022 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2023 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
2026 /* Adjust branch distances */
2027 Changes += RunOptFunc (S, &DOptBranchDist, 3);
2029 /* Replace conditional branches to RTS. If we had changes, we must run dead
2030 * code elimination again, since the change may have introduced dead code.
2032 C = RunOptFunc (S, &DOptRTSJumps2, 1);
2035 Changes += RunOptFunc (S, &DOptDeadCode, 1);
2038 /* Return the number of changes */
2044 void RunOpt (CodeSeg* S)
2045 /* Run the optimizer */
2047 const char* StatFileName;
2049 /* If we shouldn't run the optimizer, bail out */
2054 /* Check if we are requested to write optimizer statistics */
2055 StatFileName = getenv ("CC65_OPTSTATS");
2057 ReadOptStats (StatFileName);
2060 /* Print the name of the function we are working on */
2062 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2064 Print (stdout, 1, "Running optimizer for global code segment\n");
2067 /* Run groups of optimizations */
2075 /* Write statistics */
2077 WriteOptStats (StatFileName);