1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2002 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 /*****************************************************************************/
58 #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 /*****************************************************************************/
337 /* Optimize stores through pointers */
338 /*****************************************************************************/
342 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
343 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
345 /* Check for a label attached to the entry */
346 if (CE_HasLabel (L[0])) {
350 /* Check for single insn sub ops */
351 if (L[0]->OPC == OP65_AND ||
352 L[0]->OPC == OP65_EOR ||
353 L[0]->OPC == OP65_ORA ||
354 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
355 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
360 } else if (L[0]->OPC == OP65_CLC &&
361 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
362 L[1]->OPC == OP65_ADC &&
363 !CE_HasLabel (L[1])) {
365 } else if (L[0]->OPC == OP65_SEC &&
366 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
367 L[1]->OPC == OP65_SBC &&
368 !CE_HasLabel (L[1])) {
380 static unsigned OptPtrStore1 (CodeSeg* S)
381 /* Search for the sequence:
402 unsigned Changes = 0;
404 /* Walk over the entries */
406 while (I < CS_GetEntryCount (S)) {
412 L[0] = CS_GetEntry (S, I);
414 /* Check for the sequence */
415 if (CE_IsCallTo (L[0], "pushax") &&
416 CS_GetEntries (S, L+1, I+1, 3) &&
417 L[1]->OPC == OP65_LDY &&
418 CE_KnownImm (L[1]) &&
419 !CE_HasLabel (L[1]) &&
420 CE_IsCallTo (L[2], "ldauidx") &&
421 !CE_HasLabel (L[2]) &&
422 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
423 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
424 L[3+K]->OPC == OP65_LDY &&
425 CE_KnownImm (L[3+K]) &&
426 !CE_HasLabel (L[3+K]) &&
427 CE_IsCallTo (L[4+K], "staspidx") &&
428 !CE_HasLabel (L[4+K])) {
432 /* Create and insert the stores */
433 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
434 CS_InsertEntry (S, X, I+1);
436 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
437 CS_InsertEntry (S, X, I+2);
439 /* Delete the call to pushax */
442 /* Delete the call to ldauidx */
443 CS_DelEntry (S, I+3);
445 /* Insert the load from ptr1 */
446 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
447 CS_InsertEntry (S, X, I+3);
448 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
449 CS_InsertEntry (S, X, I+4);
451 /* Insert the store through ptr1 */
452 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
453 CS_InsertEntry (S, X, I+6+K);
455 /* Delete the call to staspidx */
456 CS_DelEntry (S, I+7+K);
458 /* Remember, we had changes */
468 /* Return the number of changes made */
474 static unsigned OptPtrStore2 (CodeSeg* S)
475 /* Search for the sequence:
491 unsigned Changes = 0;
493 /* Walk over the entries */
495 while (I < CS_GetEntryCount (S)) {
500 L[0] = CS_GetEntry (S, I);
502 /* Check for the sequence */
503 if (CE_IsCallTo (L[0], "pushax") &&
504 CS_GetEntries (S, L+1, I+1, 3) &&
505 L[1]->OPC == OP65_LDA &&
506 !CE_HasLabel (L[1]) &&
507 L[2]->OPC == OP65_LDY &&
508 !CE_HasLabel (L[2]) &&
509 CE_IsCallTo (L[3], "staspidx") &&
510 !CE_HasLabel (L[3])) {
514 /* Create and insert the stores */
515 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
516 CS_InsertEntry (S, X, I+1);
518 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
519 CS_InsertEntry (S, X, I+2);
521 /* Delete the call to pushax */
524 /* Insert the store through ptr1 */
525 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
526 CS_InsertEntry (S, X, I+4);
528 /* Delete the call to staspidx */
529 CS_DelEntry (S, I+5);
531 /* Remember, we had changes */
541 /* Return the number of changes made */
547 /*****************************************************************************/
548 /* Optimize loads through pointers */
549 /*****************************************************************************/
553 static unsigned OptPtrLoad1 (CodeSeg* S)
554 /* Search for the sequence:
558 * lda (sp),y # May be any destination
573 unsigned Changes = 0;
575 /* Walk over the entries */
577 while (I < CS_GetEntryCount (S)) {
582 L[0] = CS_GetEntry (S, I);
584 /* Check for the sequence */
585 if (L[0]->OPC == OP65_TAX &&
586 CS_GetEntries (S, L+1, I+1, 4) &&
587 L[1]->OPC == OP65_DEY &&
588 !CE_HasLabel (L[1]) &&
589 L[2]->OPC == OP65_LDA &&
590 !CE_HasLabel (L[2]) &&
591 L[3]->OPC == OP65_LDY &&
592 !CE_HasLabel (L[3]) &&
593 CE_IsCallTo (L[4], "ldauidx") &&
594 !CE_HasLabel (L[4])) {
598 /* Store the high byte and remove the TAX instead */
599 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
600 CS_InsertEntry (S, X, I+1);
603 /* Store the low byte */
604 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
605 CS_InsertEntry (S, X, I+3);
607 /* Delete the call to ldauidx */
608 CS_DelEntry (S, I+5);
610 /* Load high and low byte */
611 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
612 CS_InsertEntry (S, X, I+5);
613 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
614 CS_InsertEntry (S, X, I+6);
616 /* Remember, we had changes */
626 /* Return the number of changes made */
632 static unsigned OptPtrLoad2 (CodeSeg* S)
633 /* Search for the sequence:
658 unsigned Changes = 0;
660 /* Walk over the entries */
662 while (I < CS_GetEntryCount (S)) {
667 L[0] = CS_GetEntry (S, I);
669 /* Check for the sequence */
670 if (L[0]->OPC == OP65_CLC &&
671 CS_GetEntries (S, L+1, I+1, 8) &&
672 L[1]->OPC == OP65_ADC &&
673 !CE_HasLabel (L[1]) &&
674 L[2]->OPC == OP65_TAY &&
675 !CE_HasLabel (L[2]) &&
676 L[3]->OPC == OP65_TXA &&
677 !CE_HasLabel (L[3]) &&
678 L[4]->OPC == OP65_ADC &&
679 !CE_HasLabel (L[4]) &&
680 L[5]->OPC == OP65_TAX &&
681 !CE_HasLabel (L[5]) &&
682 L[6]->OPC == OP65_TYA &&
683 !CE_HasLabel (L[6]) &&
684 L[7]->OPC == OP65_LDY &&
685 !CE_HasLabel (L[7]) &&
686 CE_IsCallTo (L[8], "ldauidx") &&
687 !CE_HasLabel (L[8])) {
692 /* Store the low byte and remove the TAY instead */
693 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
694 CS_InsertEntry (S, X, I+2);
695 CS_DelEntry (S, I+3);
697 /* Store the high byte */
698 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
699 CS_InsertEntry (S, X, I+5);
701 /* If the instruction before the adc is a ldx, replace the
702 * txa by and lda with the same location of the ldx.
704 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
705 P->OPC == OP65_LDX &&
708 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
709 CS_InsertEntry (S, X, I+4);
710 CS_DelEntry (S, I+3);
713 /* Delete more transfer insns */
714 CS_DelEntry (S, I+7);
715 CS_DelEntry (S, I+6);
717 /* Delete the call to ldauidx */
718 CS_DelEntry (S, I+7);
720 /* Load high and low byte */
721 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
722 CS_InsertEntry (S, X, I+7);
723 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
724 CS_InsertEntry (S, X, I+8);
726 /* Remember, we had changes */
736 /* Return the number of changes made */
742 static unsigned OptPtrLoad3 (CodeSeg* S)
743 /* Search for the sequence:
768 unsigned Changes = 0;
770 /* Walk over the entries */
772 while (I < CS_GetEntryCount (S)) {
777 L[0] = CS_GetEntry (S, I);
779 /* Check for the sequence */
780 if (L[0]->OPC == OP65_ADC &&
781 CS_GetEntries (S, L+1, I+1, 8) &&
782 L[1]->OPC == OP65_PHA &&
783 !CE_HasLabel (L[1]) &&
784 L[2]->OPC == OP65_TXA &&
785 !CE_HasLabel (L[2]) &&
786 L[3]->OPC == OP65_INY &&
787 !CE_HasLabel (L[3]) &&
788 L[4]->OPC == OP65_ADC &&
789 !CE_HasLabel (L[4]) &&
790 L[5]->OPC == OP65_TAX &&
791 !CE_HasLabel (L[5]) &&
792 L[6]->OPC == OP65_PLA &&
793 !CE_HasLabel (L[6]) &&
794 L[7]->OPC == OP65_LDY &&
795 !CE_HasLabel (L[7]) &&
796 CE_IsCallTo (L[8], "ldauidx") &&
797 !CE_HasLabel (L[8])) {
801 /* Store the low byte and remove the PHA instead */
802 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
803 CS_InsertEntry (S, X, I+1);
804 CS_DelEntry (S, I+2);
806 /* Store the high byte */
807 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
808 CS_InsertEntry (S, X, I+5);
810 /* Delete more transfer and PLA insns */
811 CS_DelEntry (S, I+7);
812 CS_DelEntry (S, I+6);
814 /* Delete the call to ldauidx */
815 CS_DelEntry (S, I+7);
817 /* Load high and low byte */
818 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
819 CS_InsertEntry (S, X, I+7);
820 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
821 CS_InsertEntry (S, X, I+8);
823 /* Remember, we had changes */
833 /* Return the number of changes made */
839 static unsigned OptPtrLoad4 (CodeSeg* S)
840 /* Search for the sequence:
858 unsigned Changes = 0;
860 /* Walk over the entries */
862 while (I < CS_GetEntryCount (S)) {
868 L[0] = CS_GetEntry (S, I);
870 /* Check for the sequence */
871 if (L[0]->OPC == OP65_LDA &&
872 L[0]->AM == AM65_IMM &&
873 CS_GetEntries (S, L+1, I+1, 7) &&
874 L[1]->OPC == OP65_LDX &&
875 L[1]->AM == AM65_IMM &&
876 !CE_HasLabel (L[1]) &&
877 L[2]->OPC == OP65_CLC &&
878 !CE_HasLabel (L[2]) &&
879 L[3]->OPC == OP65_ADC &&
880 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
881 !CE_HasLabel (L[3]) &&
882 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
884 L[4]->JumpTo->Owner == L[6] &&
885 !CE_HasLabel (L[4]) &&
886 L[5]->OPC == OP65_INX &&
887 !CE_HasLabel (L[5]) &&
888 L[6]->OPC == OP65_LDY &&
889 CE_KnownImm (L[6]) &&
891 CE_IsCallTo (L[7], "ldauidx") &&
892 !CE_HasLabel (L[7]) &&
893 /* Check the label last because this is quite costly */
894 (Len = strlen (L[0]->Arg)) > 3 &&
895 L[0]->Arg[0] == '<' &&
896 L[0]->Arg[1] == '(' &&
897 strlen (L[1]->Arg) == Len &&
898 L[1]->Arg[0] == '>' &&
899 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
904 /* We will create all the new stuff behind the current one so
905 * we keep the line references.
907 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
908 CS_InsertEntry (S, X, I+8);
910 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
911 CS_InsertEntry (S, X, I+9);
913 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
915 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
916 CS_InsertEntry (S, X, I+10);
919 /* Remove the old code */
920 CS_DelEntries (S, I, 8);
922 /* Remember, we had changes */
932 /* Return the number of changes made */
938 static unsigned OptPtrLoad5 (CodeSeg* S)
939 /* Search for the sequence:
960 unsigned Changes = 0;
962 /* Walk over the entries */
964 while (I < CS_GetEntryCount (S)) {
970 L[0] = CS_GetEntry (S, I);
972 /* Check for the sequence */
973 if (L[0]->OPC == OP65_LDA &&
974 L[0]->AM == AM65_IMM &&
975 CS_GetEntries (S, L+1, I+1, 8) &&
976 L[1]->OPC == OP65_LDX &&
977 L[1]->AM == AM65_IMM &&
978 !CE_HasLabel (L[1]) &&
979 L[2]->OPC == OP65_LDY &&
980 CE_KnownImm (L[2]) &&
981 !CE_HasLabel (L[2]) &&
982 L[3]->OPC == OP65_CLC &&
983 !CE_HasLabel (L[3]) &&
984 L[4]->OPC == OP65_ADC &&
985 L[4]->AM == AM65_ZP_INDY &&
986 !CE_HasLabel (L[4]) &&
987 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
989 L[5]->JumpTo->Owner == L[7] &&
990 !CE_HasLabel (L[5]) &&
991 L[6]->OPC == OP65_INX &&
992 !CE_HasLabel (L[6]) &&
993 L[7]->OPC == OP65_LDY &&
994 CE_KnownImm (L[7]) &&
996 CE_IsCallTo (L[8], "ldauidx") &&
997 !CE_HasLabel (L[8]) &&
998 /* Check the label last because this is quite costly */
999 (Len = strlen (L[0]->Arg)) > 3 &&
1000 L[0]->Arg[0] == '<' &&
1001 L[0]->Arg[1] == '(' &&
1002 strlen (L[1]->Arg) == Len &&
1003 L[1]->Arg[0] == '>' &&
1004 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1010 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1011 CS_InsertEntry (S, X, I+3);
1014 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1015 CS_InsertEntry (S, X, I+4);
1018 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1019 CS_InsertEntry (S, X, I+5);
1022 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1023 Label[Len-3] = '\0';
1024 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1025 CS_InsertEntry (S, X, I+6);
1028 /* Remove the old code */
1029 CS_DelEntries (S, I, 2);
1030 CS_DelEntries (S, I+5, 6);
1032 /* Remember, we had changes */
1042 /* Return the number of changes made */
1048 static unsigned OptPtrLoad6 (CodeSeg* S)
1049 /* Search for the sequence
1054 * and replace it by:
1062 * This step must be execute *after* OptPtrLoad1!
1065 unsigned Changes = 0;
1067 /* Walk over the entries */
1069 while (I < CS_GetEntryCount (S)) {
1073 /* Get next entry */
1074 L[0] = CS_GetEntry (S, I);
1076 /* Check for the sequence */
1077 if (L[0]->OPC == OP65_LDY &&
1078 CS_GetEntries (S, L+1, I+1, 1) &&
1079 CE_IsCallTo (L[1], "ldauidx") &&
1080 !CE_HasLabel (L[1])) {
1084 /* Store the high byte */
1085 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1086 CS_InsertEntry (S, X, I+1);
1088 /* Store the low byte */
1089 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1090 CS_InsertEntry (S, X, I+2);
1092 /* Delete the call to ldauidx */
1093 CS_DelEntry (S, I+3);
1095 /* Load the high and low byte */
1096 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1097 CS_InsertEntry (S, X, I+3);
1098 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1099 CS_InsertEntry (S, X, I+4);
1101 /* Remember, we had changes */
1111 /* Return the number of changes made */
1117 /*****************************************************************************/
1118 /* Decouple operations */
1119 /*****************************************************************************/
1123 static unsigned OptDecouple (CodeSeg* S)
1124 /* Decouple operations, that is, do the following replacements:
1134 * lda zp -> lda #imm
1135 * ldx zp -> ldx #imm
1136 * ldy zp -> ldy #imm
1138 * Provided that the register values are known of course.
1141 unsigned Changes = 0;
1144 /* Generate register info for the following step */
1147 /* Walk over the entries */
1149 while (I < CS_GetEntryCount (S)) {
1153 /* Get next entry and it's input register values */
1154 CodeEntry* E = CS_GetEntry (S, I);
1155 const RegContents* In = &E->RI->In;
1157 /* Assume we have no replacement */
1160 /* Check the instruction */
1164 if (E->RI->In.RegX >= 0) {
1165 Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
1166 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1171 if (E->RI->In.RegY >= 0) {
1172 Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1173 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1178 if (E->RI->In.RegX >= 0) {
1179 Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1180 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1185 if (E->RI->In.RegY >= 0) {
1186 Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1187 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1192 if (E->AM == AM65_ZP) {
1193 switch (GetKnownReg (E->Use, In)) {
1195 Arg = MakeHexArg (In->Tmp1);
1196 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1200 Arg = MakeHexArg (In->SRegLo);
1201 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1205 Arg = MakeHexArg (In->SRegHi);
1206 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1213 if (E->AM == AM65_ZP) {
1214 switch (GetKnownReg (E->Use, In)) {
1216 Arg = MakeHexArg (In->Tmp1);
1217 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1221 Arg = MakeHexArg (In->SRegLo);
1222 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1226 Arg = MakeHexArg (In->SRegHi);
1227 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1234 if (E->AM == AM65_ZP) {
1235 switch (GetKnownReg (E->Use, In)) {
1237 Arg = MakeHexArg (In->Tmp1);
1238 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1242 Arg = MakeHexArg (In->SRegLo);
1243 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1247 Arg = MakeHexArg (In->SRegHi);
1248 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1255 if (E->RI->In.RegA >= 0) {
1256 Arg = MakeHexArg (In->RegA);
1257 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1262 if (E->RI->In.RegA >= 0) {
1263 Arg = MakeHexArg (In->RegA);
1264 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1269 if (E->RI->In.RegX >= 0) {
1270 Arg = MakeHexArg (In->RegX);
1271 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1276 if (E->RI->In.RegY >= 0) {
1277 Arg = MakeHexArg (In->RegY);
1278 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1283 /* Avoid gcc warnings */
1288 /* Insert the replacement if we have one */
1290 CS_InsertEntry (S, X, I+1);
1300 /* Free register info */
1303 /* Return the number of changes made */
1309 /*****************************************************************************/
1310 /* struct OptFunc */
1311 /*****************************************************************************/
1315 typedef struct OptFunc OptFunc;
1317 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1318 const char* Name; /* Name of the function/group */
1319 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1320 unsigned long TotalRuns; /* Total number of runs */
1321 unsigned long LastRuns; /* Last number of runs */
1322 unsigned long TotalChanges; /* Total number of changes */
1323 unsigned long LastChanges; /* Last number of changes */
1324 char Disabled; /* True if function disabled */
1329 /*****************************************************************************/
1331 /*****************************************************************************/
1335 /* A list of all the function descriptions */
1336 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1337 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1338 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1339 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1340 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1341 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 40, 0, 0, 0, 0, 0 };
1342 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1343 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1344 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1345 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1346 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1347 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1348 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1349 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1350 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1351 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1352 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1353 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1354 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1355 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1356 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1357 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1358 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1359 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1360 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1361 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1362 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1363 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1364 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1365 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1366 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1367 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1368 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1369 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1370 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1371 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 100, 0, 0, 0, 0, 0 };
1372 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 100, 0, 0, 0, 0, 0 };
1373 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1374 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 100, 0, 0, 0, 0, 0 };
1375 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1376 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1377 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1378 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1379 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1380 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1381 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1382 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1383 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 220, 0, 0, 0, 0, 0 };
1384 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 120, 0, 0, 0, 0, 0 };
1385 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1386 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1387 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1388 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1389 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0, 0 };
1390 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1391 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1394 /* Table containing all the steps in alphabetical order */
1395 static OptFunc* OptFuncs[] = {
1453 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1457 static int CmpOptStep (const void* Key, const void* Func)
1458 /* Compare function for bsearch */
1460 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1465 static OptFunc* FindOptFunc (const char* Name)
1466 /* Find an optimizer step by name in the table and return a pointer. Return
1467 * NULL if no such step is found.
1470 /* Search for the function in the list */
1471 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1477 static OptFunc* GetOptFunc (const char* Name)
1478 /* Find an optimizer step by name in the table and return a pointer. Print an
1479 * error and call AbEnd if not found.
1482 /* Search for the function in the list */
1483 OptFunc* F = FindOptFunc (Name);
1486 AbEnd ("Optimization step `%s' not found", Name);
1493 void DisableOpt (const char* Name)
1494 /* Disable the optimization with the given name */
1496 if (strcmp (Name, "any") == 0) {
1498 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1499 OptFuncs[I]->Disabled = 1;
1502 GetOptFunc(Name)->Disabled = 1;
1508 void EnableOpt (const char* Name)
1509 /* Enable the optimization with the given name */
1511 if (strcmp (Name, "any") == 0) {
1513 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1514 OptFuncs[I]->Disabled = 0;
1517 GetOptFunc(Name)->Disabled = 0;
1523 void ListOptSteps (FILE* F)
1524 /* List all optimization steps */
1527 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1528 fprintf (F, "%s\n", OptFuncs[I]->Name);
1534 static void ReadOptStats (const char* Name)
1535 /* Read the optimizer statistics file */
1540 /* Try to open the file */
1541 FILE* F = fopen (Name, "r");
1543 /* Ignore the error */
1547 /* Read and parse the lines */
1549 while (fgets (Buf, sizeof (Buf), F) != 0) {
1557 unsigned long TotalRuns;
1558 unsigned long TotalChanges;
1563 /* Remove trailing white space including the line terminator */
1566 while (Len > 0 && IsSpace (B[Len-1])) {
1571 /* Remove leading whitespace */
1572 while (IsSpace (*B)) {
1576 /* Check for empty and comment lines */
1577 if (*B == '\0' || *B == ';' || *B == '#') {
1581 /* Parse the line */
1582 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1587 /* Search for the optimizer step. */
1588 Func = FindOptFunc (Name);
1594 /* Found the step, set the fields */
1595 Func->TotalRuns = TotalRuns;
1596 Func->TotalChanges = TotalChanges;
1600 /* Close the file, ignore errors here. */
1606 static void WriteOptStats (const char* Name)
1607 /* Write the optimizer statistics file */
1611 /* Try to open the file */
1612 FILE* F = fopen (Name, "w");
1614 /* Ignore the error */
1618 /* Write a header */
1620 "; Optimizer Total Last Total Last\n"
1621 "; Step Runs Runs Chg Chg\n");
1624 /* Write the data */
1625 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1626 const OptFunc* O = OptFuncs[I];
1628 "%-20s %6lu %6lu %6lu %6lu\n",
1636 /* Close the file, ignore errors here. */
1642 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1643 /* Run one optimizer function Max times or until there are no more changes */
1645 unsigned Changes, C;
1647 /* Don't run the function if it is disabled or if it is prohibited by the
1650 if (F->Disabled || CodeSizeFactor < F->CodeSizeFactor) {
1654 /* Run this until there are no more changes */
1658 /* Run the function */
1665 F->TotalChanges += C;
1666 F->LastChanges += C;
1668 } while (--Max && C > 0);
1670 /* Return the number of changes */
1676 static unsigned RunOptGroup1 (CodeSeg* S)
1677 /* Run the first group of optimization steps. These steps translate known
1678 * patterns emitted by the code generator into more optimal patterns. Order
1679 * of the steps is important, because some of the steps done earlier cover
1680 * the same patterns as later steps as subpatterns.
1683 unsigned Changes = 0;
1685 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1686 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1687 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1688 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1689 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1690 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1691 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1692 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1693 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1694 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1695 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1696 Changes += RunOptFunc (S, &DOptAdd1, 1);
1697 Changes += RunOptFunc (S, &DOptAdd2, 1);
1698 Changes += RunOptFunc (S, &DOptShift1, 1);
1699 Changes += RunOptFunc (S, &DOptShift2, 1);
1700 Changes += RunOptFunc (S, &DOptShift3, 1);
1701 Changes += RunOptFunc (S, &DOptStore1, 5);
1702 Changes += RunOptFunc (S, &DOptStore2, 5);
1704 /* Return the number of changes */
1710 static unsigned RunOptGroup2 (CodeSeg* S)
1711 /* Run one group of optimization steps. This step involves just decoupling
1712 * instructions by replacing them by instructions that do not depend on
1713 * previous instructions. This makes it easier to find instructions that
1717 unsigned Changes = 0;
1719 Changes += RunOptFunc (S, &DOptDecouple, 1);
1721 /* Return the number of changes */
1727 static unsigned RunOptGroup3 (CodeSeg* S)
1728 /* Run one group of optimization steps. These steps depend on each other,
1729 * that means that one step may allow another step to do additional work,
1730 * so we will repeat the steps as long as we see any changes.
1733 unsigned Changes, C;
1739 C += RunOptFunc (S, &DOptPtrLoad6, 1);
1740 C += RunOptFunc (S, &DOptNegA1, 1);
1741 C += RunOptFunc (S, &DOptNegA2, 1);
1742 C += RunOptFunc (S, &DOptSub1, 1);
1743 C += RunOptFunc (S, &DOptSub2, 1);
1744 C += RunOptFunc (S, &DOptAdd3, 1);
1745 C += RunOptFunc (S, &DOptStackOps, 1);
1746 C += RunOptFunc (S, &DOptJumpCascades, 1);
1747 C += RunOptFunc (S, &DOptDeadJumps, 1);
1748 C += RunOptFunc (S, &DOptRTS, 1);
1749 C += RunOptFunc (S, &DOptDeadCode, 1);
1750 C += RunOptFunc (S, &DOptJumpTarget, 1);
1751 C += RunOptFunc (S, &DOptCondBranches, 1);
1752 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1753 C += RunOptFunc (S, &DOptBoolTrans, 1);
1754 C += RunOptFunc (S, &DOptCmp1, 1);
1755 C += RunOptFunc (S, &DOptCmp2, 1);
1756 C += RunOptFunc (S, &DOptCmp3, 1);
1757 C += RunOptFunc (S, &DOptCmp4, 1);
1758 C += RunOptFunc (S, &DOptCmp5, 1);
1759 C += RunOptFunc (S, &DOptCmp6, 1);
1760 C += RunOptFunc (S, &DOptCmp7, 1);
1761 C += RunOptFunc (S, &DOptTest1, 1);
1762 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1763 C += RunOptFunc (S, &DOptUnusedStores, 1);
1764 C += RunOptFunc (S, &DOptDupLoads, 1);
1765 C += RunOptFunc (S, &DOptStoreLoad, 1);
1766 C += RunOptFunc (S, &DOptTransfers, 1);
1767 C += RunOptFunc (S, &DOptPushPop, 1);
1773 /* Return the number of changes */
1779 static unsigned RunOptGroup4 (CodeSeg* S)
1780 /* 65C02 specific optimizations. */
1782 unsigned Changes = 0;
1784 if (CPU >= CPU_65C02) {
1785 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1786 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1787 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1789 /* The 65C02 replacement codes do often make the use of a register
1790 * value unnecessary, so if we have changes, run another load
1793 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1797 /* Return the number of changes */
1803 static unsigned RunOptGroup5 (CodeSeg* S)
1804 /* Run another round of pattern replacements. These are done late, since there
1805 * may be better replacements before.
1808 unsigned Changes = 0;
1810 Changes += RunOptFunc (S, &DOptPush1, 1);
1812 /* Return the number of changes */
1818 static unsigned RunOptGroup6 (CodeSeg* S)
1819 /* The last group of optimization steps. Adjust branches, do size optimizations.
1822 unsigned Changes = 0;
1825 if (CodeSizeFactor <= 100) {
1826 /* Optimize for size, that is replace operations by shorter ones, even
1827 * if this does hinder further optimizations (no problem since we're
1830 C = RunOptFunc (S, &DOptSize1, 1);
1833 /* Run some optimization passes again, since the size optimizations
1834 * may have opened new oportunities.
1836 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1837 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1840 C = RunOptFunc (S, &DOptSize2, 1);
1843 /* Run some optimization passes again, since the size optimizations
1844 * may have opened new oportunities.
1846 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1847 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1850 /* Adjust branch distances */
1851 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1853 /* Replace conditional branches to RTS. If we had changes, we must run dead
1854 * code elimination again, since the change may have introduced dead code.
1856 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1859 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1862 /* Return the number of changes */
1868 void RunOpt (CodeSeg* S)
1869 /* Run the optimizer */
1871 const char* StatFileName;
1873 /* If we shouldn't run the optimizer, bail out */
1878 /* Check if we are requested to write optimizer statistics */
1879 StatFileName = getenv ("CC65_OPTSTATS");
1881 ReadOptStats (StatFileName);
1884 /* Print the name of the function we are working on */
1886 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1888 Print (stdout, 1, "Running optimizer for global code segment\n");
1891 /* Run groups of optimizations */
1899 /* Write statistics */
1901 WriteOptStats (StatFileName);