1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
43 #include "debugflag.h"
57 #include "coptptrload.h"
58 #include "coptptrstore.h"
62 #include "coptstore.h"
71 /*****************************************************************************/
73 /*****************************************************************************/
88 /*****************************************************************************/
90 /*****************************************************************************/
94 static unsigned OptShift1 (CodeSeg* S)
95 /* A call to the shlaxN routine may get replaced by one or more asl insns
96 * if the value of X is not used later. If X is used later, but it is zero
97 * on entry and it's a shift by one, it may get replaced by:
106 unsigned Changes = 0;
109 /* Generate register info */
112 /* Walk over the entries */
114 while (I < CS_GetEntryCount (S)) {
121 CodeEntry* E = CS_GetEntry (S, I);
123 /* Check for the sequence */
124 if (E->OPC == OP65_JSR &&
125 (strncmp (E->Arg, "shlax", 5) == 0 ||
126 strncmp (E->Arg, "aslax", 5) == 0) &&
127 strlen (E->Arg) == 6 &&
128 IsDigit (E->Arg[5])) {
130 if (!RegXUsed (S, I+1)) {
132 /* Insert shift insns */
133 unsigned Count = E->Arg[5] - '0';
135 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
136 CS_InsertEntry (S, X, I+1);
139 /* Delete the call to shlax */
142 /* Remember, we had changes */
145 } else if (E->RI->In.RegX == 0 &&
147 (N = CS_GetNextEntry (S, I)) != 0) {
150 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
151 CS_InsertEntry (S, X, I+1);
154 L = CS_GenLabel (S, N);
155 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, E->LI);
156 CS_InsertEntry (S, X, I+2);
159 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
160 CS_InsertEntry (S, X, I+3);
162 /* Delete the call to shlax */
165 /* Remember, we had changes */
176 /* Free the register info */
179 /* Return the number of changes made */
185 static unsigned OptShift2(CodeSeg* S)
186 /* A call to the asrax1 routines may get replaced by something simpler, if
187 * X is not used later:
194 unsigned Changes = 0;
197 /* Generate register info */
200 /* Walk over the entries */
202 while (I < CS_GetEntryCount (S)) {
207 CodeEntry* E = CS_GetEntry (S, I);
209 /* Check for the sequence */
210 if (E->OPC == OP65_JSR &&
211 strncmp (E->Arg, "asrax", 5) == 0 &&
212 strlen (E->Arg) == 6 &&
213 IsDigit (E->Arg[5]) &&
214 (Count = (E->Arg[5] - '0')) >= 1 &&
215 Count * 100 <= S->CodeSizeFactor &&
216 !RegXUsed (S, I+1)) {
221 /* Generate the replacement sequence */
224 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, E->LI);
225 CS_InsertEntry (S, X, J++);
228 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, E->LI);
229 CS_InsertEntry (S, X, J++);
232 /* Delete the call to asrax */
235 /* Remember, we had changes */
244 /* Free the register info */
247 /* Return the number of changes made */
253 static unsigned OptShift3 (CodeSeg* S)
260 * may get replaced by
264 * if X is zero on entry and unused later.
267 unsigned Changes = 0;
270 /* Generate register info */
273 /* Walk over the entries */
275 while (I < CS_GetEntryCount (S)) {
280 L[0] = CS_GetEntry (S, I);
282 /* Check for the sequence */
283 if ((L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
285 L[0]->RI->In.RegX == 0 &&
286 CS_GetEntries (S, L+1, I+1, 2) &&
287 L[1]->OPC == OP65_INX &&
288 L[0]->JumpTo->Owner == L[2] &&
289 !CS_RangeHasLabel (S, I, 2) &&
290 CE_IsCallTo (L[2], "shrax1") &&
291 !RegXUsed (S, I+3)) {
293 /* Add the replacement insn instead */
294 CodeEntry* X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
295 CS_InsertEntry (S, X, I+3);
297 /* Remove the bcs/dex/jsr */
298 CS_DelEntries (S, I, 3);
300 /* Remember, we had changes */
310 /* Free the register info */
313 /* Return the number of changes made */
319 static unsigned OptShift4 (CodeSeg* S)
320 /* A call to the shraxN routine may get replaced by one or more lsr insns
321 * if the value of X is zero.
324 unsigned Changes = 0;
327 /* Generate register info */
330 /* Walk over the entries */
332 while (I < CS_GetEntryCount (S)) {
335 CodeEntry* E = CS_GetEntry (S, I);
337 /* Check for the sequence */
338 if (E->OPC == OP65_JSR &&
339 strncmp (E->Arg, "shrax", 5) == 0 &&
340 strlen (E->Arg) == 6 &&
341 IsDigit (E->Arg[5]) &&
342 E->RI->In.RegX == 0) {
344 /* Insert shift insns */
345 unsigned Count = E->Arg[5] - '0';
347 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
348 CS_InsertEntry (S, X, I+1);
351 /* Delete the call to shrax */
354 /* Remember, we had changes */
364 /* Free the register info */
367 /* Return the number of changes made */
373 static unsigned GetShiftType (const char* Sub)
374 /* Helper function for OptShift5 */
377 if (strcmp (Sub+1, "slax1") == 0) {
379 } else if (strcmp (Sub+1, "srax1") == 0) {
382 } else if (*Sub == 's') {
383 if (strcmp (Sub+1, "hlax1") == 0) {
385 } else if (strcmp (Sub+1, "hrax1") == 0) {
394 static unsigned OptShift5 (CodeSeg* S)
395 /* Search for the sequence
399 * jsr aslax1/asrax1/shlax1/shrax1
412 * or similar, provided that a/x is not used later
415 unsigned Changes = 0;
417 /* Walk over the entries */
419 while (I < CS_GetEntryCount (S)) {
425 L[0] = CS_GetEntry (S, I);
427 /* Check for the sequence */
428 if (L[0]->OPC == OP65_LDA &&
429 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
430 CS_GetEntries (S, L+1, I+1, 4) &&
431 !CS_RangeHasLabel (S, I+1, 4) &&
432 L[1]->OPC == OP65_LDX &&
433 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
434 L[2]->OPC == OP65_JSR &&
435 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
436 L[3]->OPC == OP65_STA &&
437 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
438 L[4]->OPC == OP65_STX &&
439 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
440 !RegAXUsed (S, I+5)) {
444 /* Handle the four shift types differently */
448 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
449 CS_InsertEntry (S, X, I+5);
450 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
451 CS_InsertEntry (S, X, I+6);
452 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
453 CS_InsertEntry (S, X, I+7);
454 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
455 CS_InsertEntry (S, X, I+8);
456 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
457 CS_InsertEntry (S, X, I+9);
458 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
459 CS_InsertEntry (S, X, I+10);
460 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
461 CS_InsertEntry (S, X, I+11);
462 CS_DelEntries (S, I, 5);
466 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
467 CS_InsertEntry (S, X, I+5);
468 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
469 CS_InsertEntry (S, X, I+6);
470 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
471 CS_InsertEntry (S, X, I+7);
472 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
473 CS_InsertEntry (S, X, I+8);
474 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
475 CS_InsertEntry (S, X, I+9);
476 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
477 CS_InsertEntry (S, X, I+10);
478 CS_DelEntries (S, I, 5);
483 /* These two are identical */
484 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
485 CS_InsertEntry (S, X, I+1);
486 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
487 CS_InsertEntry (S, X, I+2);
488 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
489 CS_InsertEntry (S, X, I+3);
490 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
491 CS_InsertEntry (S, X, I+4);
492 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
493 CS_InsertEntry (S, X, I+5);
494 CS_DelEntries (S, I+6, 4);
499 /* Remember, we had changes */
509 /* Return the number of changes made */
515 static unsigned OptShift6 (CodeSeg* S)
516 /* Inline the shift subroutines. */
518 unsigned Changes = 0;
520 /* Walk over the entries */
522 while (I < CS_GetEntryCount (S)) {
528 CodeEntry* E = CS_GetEntry (S, I);
530 /* Check for a call to one of the shift routine */
531 if (E->OPC == OP65_JSR &&
532 (strncmp (E->Arg, "shlax", 5) == 0 ||
533 strncmp (E->Arg, "aslax", 5) == 0) &&
534 strlen (E->Arg) == 6 &&
535 IsDigit (E->Arg[5])) {
537 /* Get number of shifts */
538 unsigned ShiftCount = (E->Arg[5] - '0');
545 * (repeat ShiftCount-1 times)
548 * which makes 4 + 3 * ShiftCount bytes, compared to the original
549 * 3 bytes for the subroutine call. However, in most cases, the
550 * final load of the X register gets merged with some other insn
551 * and replaces a txa, so for a shift count of 1, we get a factor
552 * of 200, which matches nicely the CodeSizeFactor enabled with -Oi
554 if (ShiftCount > 1 || S->CodeSizeFactor > 200) {
555 unsigned Size = 4 + 3 * ShiftCount;
556 if ((Size * 100 / 3) > S->CodeSizeFactor) {
562 /* Inline the code. Insertion point is behind the subroutine call */
566 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
567 CS_InsertEntry (S, X, IP++);
569 while (ShiftCount--) {
571 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
572 CS_InsertEntry (S, X, IP++);
575 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
576 CS_InsertEntry (S, X, IP++);
580 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
581 CS_InsertEntry (S, X, IP++);
583 /* Remove the subroutine call */
586 /* Remember, we had changes */
596 /* Return the number of changes made */
602 /*****************************************************************************/
604 /*****************************************************************************/
608 static unsigned OptLoad1 (CodeSeg* S)
609 /* Search for a call to ldaxysp where X is not used later and replace it by
610 * a load of just the A register.
614 unsigned Changes = 0;
616 /* Generate register info */
619 /* Walk over the entries */
621 while (I < CS_GetEntryCount (S)) {
626 E = CS_GetEntry (S, I);
628 /* Check for the sequence */
629 if (CE_IsCallTo (E, "ldaxysp") &&
630 RegValIsKnown (E->RI->In.RegY) &&
631 !RegXUsed (S, I+1)) {
635 /* Reload the Y register */
636 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
637 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
638 CS_InsertEntry (S, X, I+1);
640 /* Load from stack */
641 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
642 CS_InsertEntry (S, X, I+2);
644 /* Now remove the call to the subroutine */
647 /* Remember, we had changes */
657 /* Free the register info */
660 /* Return the number of changes made */
666 static unsigned OptLoad2 (CodeSeg* S)
667 /* Replace calls to ldaxysp by inline code */
670 unsigned Changes = 0;
672 /* Generate register info */
675 /* Walk over the entries */
677 while (I < CS_GetEntryCount (S)) {
682 L[0] = CS_GetEntry (S, I);
684 /* Check for the sequence */
685 if (CE_IsCallTo (L[0], "ldaxysp")) {
689 /* Followed by sta abs/stx abs? */
690 if (CS_GetEntries (S, L+1, I+1, 2) &&
691 L[1]->OPC == OP65_STA &&
692 L[2]->OPC == OP65_STX &&
695 strcmp (L[1]->Arg, L[2]->Arg) != 0) &&
696 !CS_RangeHasLabel (S, I+1, 2) &&
697 !RegXUsed (S, I+3)) {
699 /* A/X are stored into memory somewhere and X is not used
704 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
705 CS_InsertEntry (S, X, I+3);
708 X = NewCodeEntry (OP65_STA, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
709 CS_InsertEntry (S, X, I+4);
712 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[0]->LI);
713 CS_InsertEntry (S, X, I+5);
716 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
717 CS_InsertEntry (S, X, I+6);
720 X = NewCodeEntry (OP65_STA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
721 CS_InsertEntry (S, X, I+7);
723 /* Now remove the call to the subroutine and the sta/stx */
724 CS_DelEntries (S, I, 3);
728 /* Standard replacement */
731 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
732 CS_InsertEntry (S, X, I+1);
735 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
736 CS_InsertEntry (S, X, I+2);
739 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[0]->LI);
740 CS_InsertEntry (S, X, I+3);
743 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
744 CS_InsertEntry (S, X, I+4);
746 /* Now remove the call to the subroutine */
751 /* Remember, we had changes */
760 /* Free the register info */
763 /* Return the number of changes made */
769 /*****************************************************************************/
770 /* Decouple operations */
771 /*****************************************************************************/
775 static unsigned OptDecouple (CodeSeg* S)
776 /* Decouple operations, that is, do the following replacements:
790 * Provided that the register values are known of course.
793 unsigned Changes = 0;
796 /* Generate register info for the following step */
799 /* Walk over the entries */
801 while (I < CS_GetEntryCount (S)) {
805 /* Get next entry and it's input register values */
806 CodeEntry* E = CS_GetEntry (S, I);
807 const RegContents* In = &E->RI->In;
809 /* Assume we have no replacement */
812 /* Check the instruction */
816 if (RegValIsKnown (In->RegA)) {
817 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
818 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
823 if (RegValIsKnown (In->RegX)) {
824 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
825 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
830 if (RegValIsKnown (In->RegY)) {
831 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
832 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
837 if (RegValIsKnown (In->RegA)) {
838 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
839 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
844 if (RegValIsKnown (In->RegX)) {
845 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
846 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
851 if (RegValIsKnown (In->RegY)) {
852 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
853 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
858 if (E->AM == AM65_ZP) {
859 switch (GetKnownReg (E->Use & REG_ZP, In)) {
861 Arg = MakeHexArg (In->Tmp1);
862 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
866 Arg = MakeHexArg (In->Ptr1Lo);
867 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
871 Arg = MakeHexArg (In->Ptr1Hi);
872 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
876 Arg = MakeHexArg (In->SRegLo);
877 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
881 Arg = MakeHexArg (In->SRegHi);
882 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
889 if (E->AM == AM65_ZP) {
890 switch (GetKnownReg (E->Use & REG_ZP, In)) {
892 Arg = MakeHexArg (In->Tmp1);
893 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
897 Arg = MakeHexArg (In->Ptr1Lo);
898 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
902 Arg = MakeHexArg (In->Ptr1Hi);
903 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
907 Arg = MakeHexArg (In->SRegLo);
908 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
912 Arg = MakeHexArg (In->SRegHi);
913 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
920 if (E->AM == AM65_ZP) {
921 switch (GetKnownReg (E->Use, In)) {
923 Arg = MakeHexArg (In->Tmp1);
924 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
928 Arg = MakeHexArg (In->Ptr1Lo);
929 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
933 Arg = MakeHexArg (In->Ptr1Hi);
934 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
938 Arg = MakeHexArg (In->SRegLo);
939 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
943 Arg = MakeHexArg (In->SRegHi);
944 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
951 if (E->RI->In.RegA >= 0) {
952 Arg = MakeHexArg (In->RegA);
953 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
958 if (E->RI->In.RegA >= 0) {
959 Arg = MakeHexArg (In->RegA);
960 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
965 if (E->RI->In.RegX >= 0) {
966 Arg = MakeHexArg (In->RegX);
967 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
972 if (E->RI->In.RegY >= 0) {
973 Arg = MakeHexArg (In->RegY);
974 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
979 /* Avoid gcc warnings */
984 /* Insert the replacement if we have one */
986 CS_InsertEntry (S, X, I+1);
996 /* Free register info */
999 /* Return the number of changes made */
1005 /*****************************************************************************/
1006 /* Optimize stack pointer ops */
1007 /*****************************************************************************/
1011 static unsigned IsDecSP (const CodeEntry* E)
1012 /* Check if this is an insn that decrements the stack pointer. If so, return
1013 * the decrement. If not, return zero.
1014 * The function expects E to be a subroutine call.
1017 if (strncmp (E->Arg, "decsp", 5) == 0) {
1018 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
1019 return (E->Arg[5] - '0');
1021 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
1022 return E->RI->In.RegY;
1025 /* If we come here, it's not a decsp op */
1031 static unsigned OptStackPtrOps (CodeSeg* S)
1032 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
1036 unsigned Changes = 0;
1039 /* Generate register info for the following step */
1042 /* Walk over the entries */
1044 while (I < CS_GetEntryCount (S)) {
1050 /* Get the next entry */
1051 const CodeEntry* E = CS_GetEntry (S, I);
1053 /* Check for decspn or subysp */
1054 if (E->OPC == OP65_JSR &&
1055 (Dec1 = IsDecSP (E)) > 0 &&
1056 (N = CS_GetNextEntry (S, I)) != 0 &&
1057 (Dec2 = IsDecSP (N)) > 0 &&
1058 (Dec1 += Dec2) <= 255 &&
1064 /* We can combine the two */
1066 /* Insert a call to decsp */
1067 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
1068 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
1069 CS_InsertEntry (S, X, I+2);
1071 /* Insert a call to subysp */
1072 const char* Arg = MakeHexArg (Dec1);
1073 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
1074 CS_InsertEntry (S, X, I+2);
1075 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
1076 CS_InsertEntry (S, X, I+3);
1079 /* Delete the old code */
1080 CS_DelEntries (S, I, 2);
1082 /* Regenerate register info */
1085 /* Remember we had changes */
1096 /* Free register info */
1099 /* Return the number of changes made */
1105 /*****************************************************************************/
1106 /* struct OptFunc */
1107 /*****************************************************************************/
1111 typedef struct OptFunc OptFunc;
1113 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1114 const char* Name; /* Name of the function/group */
1115 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1116 unsigned long TotalRuns; /* Total number of runs */
1117 unsigned long LastRuns; /* Last number of runs */
1118 unsigned long TotalChanges; /* Total number of changes */
1119 unsigned long LastChanges; /* Last number of changes */
1120 char Disabled; /* True if function disabled */
1125 /*****************************************************************************/
1127 /*****************************************************************************/
1131 /* A list of all the function descriptions */
1132 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1133 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1134 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1135 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1136 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1137 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1138 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1139 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1140 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1141 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1142 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1143 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1144 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1145 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1146 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1147 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1148 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1149 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1150 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1151 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1152 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1153 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1154 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1155 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1156 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1157 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1158 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1159 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1160 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1161 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1162 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1163 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1164 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1165 static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
1166 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1167 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1168 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1169 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1170 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1171 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1172 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1173 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1174 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1175 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1176 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1177 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1178 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1179 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1180 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1181 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1182 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1183 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1184 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1185 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1186 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1187 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1188 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1189 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1190 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 40, 0, 0, 0, 0, 0 };
1191 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 50, 0, 0, 0, 0, 0 };
1192 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 50, 0, 0, 0, 0, 0 };
1193 static OptFunc DOptPtrStore4 = { OptPtrStore4, "OptPtrStore4", 65, 0, 0, 0, 0, 0 };
1194 static OptFunc DOptPtrStore5 = { OptPtrStore5, "OptPtrStore5", 100, 0, 0, 0, 0, 0 };
1195 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1196 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1197 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1198 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1199 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1200 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 17, 0, 0, 0, 0, 0 };
1201 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 100, 0, 0, 0, 0, 0 };
1202 static OptFunc DOptShift5 = { OptShift5, "OptShift5", 110, 0, 0, 0, 0, 0 };
1203 static OptFunc DOptShift6 = { OptShift6, "OptShift6", 200, 0, 0, 0, 0, 0 };
1204 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1205 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1206 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1207 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
1208 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1209 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 115, 0, 0, 0, 0, 0 };
1210 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1211 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1212 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1213 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1214 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1215 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1216 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1217 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
1218 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
1219 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1220 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1221 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1222 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1223 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1224 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1227 /* Table containing all the steps in alphabetical order */
1228 static OptFunc* OptFuncs[] = {
1323 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1327 static int CmpOptStep (const void* Key, const void* Func)
1328 /* Compare function for bsearch */
1330 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1335 static OptFunc* FindOptFunc (const char* Name)
1336 /* Find an optimizer step by name in the table and return a pointer. Return
1337 * NULL if no such step is found.
1340 /* Search for the function in the list */
1341 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1347 static OptFunc* GetOptFunc (const char* Name)
1348 /* Find an optimizer step by name in the table and return a pointer. Print an
1349 * error and call AbEnd if not found.
1352 /* Search for the function in the list */
1353 OptFunc* F = FindOptFunc (Name);
1356 AbEnd ("Optimization step `%s' not found", Name);
1363 void DisableOpt (const char* Name)
1364 /* Disable the optimization with the given name */
1366 if (strcmp (Name, "any") == 0) {
1368 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1369 OptFuncs[I]->Disabled = 1;
1372 GetOptFunc(Name)->Disabled = 1;
1378 void EnableOpt (const char* Name)
1379 /* Enable the optimization with the given name */
1381 if (strcmp (Name, "any") == 0) {
1383 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1384 OptFuncs[I]->Disabled = 0;
1387 GetOptFunc(Name)->Disabled = 0;
1393 void ListOptSteps (FILE* F)
1394 /* List all optimization steps */
1397 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1398 fprintf (F, "%s\n", OptFuncs[I]->Name);
1404 static void ReadOptStats (const char* Name)
1405 /* Read the optimizer statistics file */
1410 /* Try to open the file */
1411 FILE* F = fopen (Name, "r");
1413 /* Ignore the error */
1417 /* Read and parse the lines */
1419 while (fgets (Buf, sizeof (Buf), F) != 0) {
1427 unsigned long TotalRuns;
1428 unsigned long TotalChanges;
1433 /* Remove trailing white space including the line terminator */
1436 while (Len > 0 && IsSpace (B[Len-1])) {
1441 /* Remove leading whitespace */
1442 while (IsSpace (*B)) {
1446 /* Check for empty and comment lines */
1447 if (*B == '\0' || *B == ';' || *B == '#') {
1451 /* Parse the line */
1452 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1457 /* Search for the optimizer step. */
1458 Func = FindOptFunc (Name);
1464 /* Found the step, set the fields */
1465 Func->TotalRuns = TotalRuns;
1466 Func->TotalChanges = TotalChanges;
1470 /* Close the file, ignore errors here. */
1476 static void WriteOptStats (const char* Name)
1477 /* Write the optimizer statistics file */
1481 /* Try to open the file */
1482 FILE* F = fopen (Name, "w");
1484 /* Ignore the error */
1488 /* Write a header */
1490 "; Optimizer Total Last Total Last\n"
1491 "; Step Runs Runs Chg Chg\n");
1494 /* Write the data */
1495 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1496 const OptFunc* O = OptFuncs[I];
1498 "%-20s %10lu %10lu %10lu %10lu\n",
1506 /* Close the file, ignore errors here. */
1512 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1513 /* Run one optimizer function Max times or until there are no more changes */
1515 unsigned Changes, C;
1517 /* Don't run the function if it is disabled or if it is prohibited by the
1520 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1524 /* Run this until there are no more changes */
1528 /* Run the function */
1530 if (Debug && C > 0) {
1531 printf ("Applied %s: %u changes\n", F->Name, C);
1538 F->TotalChanges += C;
1539 F->LastChanges += C;
1541 } while (--Max && C > 0);
1543 /* Return the number of changes */
1549 static unsigned RunOptGroup1 (CodeSeg* S)
1550 /* Run the first group of optimization steps. These steps translate known
1551 * patterns emitted by the code generator into more optimal patterns. Order
1552 * of the steps is important, because some of the steps done earlier cover
1553 * the same patterns as later steps as subpatterns.
1556 unsigned Changes = 0;
1558 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1559 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1560 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1561 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1562 Changes += RunOptFunc (S, &DOptPtrStore4, 1);
1563 Changes += RunOptFunc (S, &DOptPtrStore5, 1);
1564 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1565 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1566 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1567 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1568 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1569 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1570 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1571 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1572 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1573 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1574 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1575 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1576 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1577 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1578 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1579 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1580 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1581 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1582 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1583 Changes += RunOptFunc (S, &DOptAdd1, 1);
1584 Changes += RunOptFunc (S, &DOptAdd2, 1);
1585 Changes += RunOptFunc (S, &DOptAdd4, 1);
1586 Changes += RunOptFunc (S, &DOptAdd5, 1);
1587 Changes += RunOptFunc (S, &DOptAdd6, 1);
1588 Changes += RunOptFunc (S, &DOptSub1, 1);
1589 Changes += RunOptFunc (S, &DOptSub3, 1);
1590 Changes += RunOptFunc (S, &DOptStore4, 1);
1591 Changes += RunOptFunc (S, &DOptStore5, 1);
1592 Changes += RunOptFunc (S, &DOptShift1, 1);
1593 Changes += RunOptFunc (S, &DOptShift2, 1);
1594 Changes += RunOptFunc (S, &DOptShift4, 1);
1595 Changes += RunOptFunc (S, &DOptShift5, 1);
1596 Changes += RunOptFunc (S, &DOptShift6, 1);
1597 Changes += RunOptFunc (S, &DOptStore1, 1);
1598 Changes += RunOptFunc (S, &DOptStore2, 5);
1599 Changes += RunOptFunc (S, &DOptStore3, 5);
1601 /* Return the number of changes */
1607 static unsigned RunOptGroup2 (CodeSeg* S)
1608 /* Run one group of optimization steps. This step involves just decoupling
1609 * instructions by replacing them by instructions that do not depend on
1610 * previous instructions. This makes it easier to find instructions that
1614 unsigned Changes = 0;
1616 Changes += RunOptFunc (S, &DOptDecouple, 1);
1618 /* Return the number of changes */
1624 static unsigned RunOptGroup3 (CodeSeg* S)
1625 /* Run one group of optimization steps. These steps depend on each other,
1626 * that means that one step may allow another step to do additional work,
1627 * so we will repeat the steps as long as we see any changes.
1630 unsigned Changes, C;
1636 C += RunOptFunc (S, &DOptNegA1, 1);
1637 C += RunOptFunc (S, &DOptNegA2, 1);
1638 C += RunOptFunc (S, &DOptStackOps, 1);
1639 C += RunOptFunc (S, &DOptSub1, 1);
1640 C += RunOptFunc (S, &DOptSub2, 1);
1641 C += RunOptFunc (S, &DOptSub3, 1);
1642 C += RunOptFunc (S, &DOptAdd5, 1);
1643 C += RunOptFunc (S, &DOptAdd6, 1);
1644 C += RunOptFunc (S, &DOptJumpCascades, 1);
1645 C += RunOptFunc (S, &DOptDeadJumps, 1);
1646 C += RunOptFunc (S, &DOptRTS, 1);
1647 C += RunOptFunc (S, &DOptDeadCode, 1);
1648 C += RunOptFunc (S, &DOptBoolTrans, 1);
1649 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1650 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1651 C += RunOptFunc (S, &DOptCondBranches1, 1);
1652 C += RunOptFunc (S, &DOptCondBranches2, 1);
1653 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1654 C += RunOptFunc (S, &DOptCmp1, 1);
1655 C += RunOptFunc (S, &DOptCmp2, 1);
1656 C += RunOptFunc (S, &DOptCmp3, 1);
1657 C += RunOptFunc (S, &DOptCmp4, 1);
1658 C += RunOptFunc (S, &DOptCmp5, 1);
1659 C += RunOptFunc (S, &DOptCmp6, 1);
1660 C += RunOptFunc (S, &DOptCmp7, 1);
1661 C += RunOptFunc (S, &DOptCmp8, 1);
1662 C += RunOptFunc (S, &DOptCmp9, 1);
1663 C += RunOptFunc (S, &DOptTest1, 1);
1664 C += RunOptFunc (S, &DOptLoad1, 1);
1665 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1666 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1667 C += RunOptFunc (S, &DOptUnusedStores, 1);
1668 C += RunOptFunc (S, &DOptDupLoads, 1);
1669 C += RunOptFunc (S, &DOptStoreLoad, 1);
1670 C += RunOptFunc (S, &DOptTransfers1, 1);
1671 C += RunOptFunc (S, &DOptTransfers3, 1);
1672 C += RunOptFunc (S, &DOptTransfers4, 1);
1673 C += RunOptFunc (S, &DOptStore1, 1);
1674 C += RunOptFunc (S, &DOptStore5, 1);
1675 C += RunOptFunc (S, &DOptPushPop, 1);
1676 C += RunOptFunc (S, &DOptPrecalc, 1);
1682 /* Return the number of changes */
1688 static unsigned RunOptGroup4 (CodeSeg* S)
1689 /* Run another round of pattern replacements. These are done late, since there
1690 * may be better replacements before.
1693 unsigned Changes = 0;
1695 /* Repeat some of the steps here */
1696 Changes += RunOptFunc (S, &DOptShift3, 1);
1697 Changes += RunOptFunc (S, &DOptPush1, 1);
1698 Changes += RunOptFunc (S, &DOptPush2, 1);
1699 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1700 Changes += RunOptFunc (S, &DOptTest2, 1);
1701 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1702 Changes += RunOptFunc (S, &DOptLoad2, 1);
1704 /* Return the number of changes */
1710 static unsigned RunOptGroup5 (CodeSeg* S)
1711 /* 65C02 specific optimizations. */
1713 unsigned Changes = 0;
1715 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1716 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1717 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1718 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1720 /* The 65C02 replacement codes do often make the use of a register
1721 * value unnecessary, so if we have changes, run another load
1724 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1728 /* Return the number of changes */
1734 static unsigned RunOptGroup6 (CodeSeg* S)
1735 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1736 * The latter is ony cycle slower, but if we're able to remove the necessary
1737 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1738 * shorten the code by one (transfer) or two bytes (load). So what we do is
1739 * to replace the insns, remove unused loads, and then change back all insns
1740 * where Y is still zero (meaning that the load has not been removed).
1743 unsigned Changes = 0;
1745 /* This group will only run for a standard 6502, because the 65C02 has a
1746 * better addressing mode that covers this case.
1748 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1749 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1750 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1751 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1754 /* Return the number of changes */
1760 static unsigned RunOptGroup7 (CodeSeg* S)
1761 /* The last group of optimization steps. Adjust branches, do size optimizations.
1764 unsigned Changes = 0;
1767 /* Optimize for size, that is replace operations by shorter ones, even
1768 * if this does hinder further optimizations (no problem since we're
1771 C = RunOptFunc (S, &DOptSize1, 1);
1774 /* Run some optimization passes again, since the size optimizations
1775 * may have opened new oportunities.
1777 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1778 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1779 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1780 Changes += RunOptFunc (S, &DOptStore5, 1);
1783 C = RunOptFunc (S, &DOptSize2, 1);
1786 /* Run some optimization passes again, since the size optimizations
1787 * may have opened new oportunities.
1789 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1790 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1791 Changes += RunOptFunc (S, &DOptStore5, 1);
1792 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1795 /* Adjust branch distances */
1796 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1798 /* Replace conditional branches to RTS. If we had changes, we must run dead
1799 * code elimination again, since the change may have introduced dead code.
1801 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1804 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1807 /* Return the number of changes */
1813 void RunOpt (CodeSeg* S)
1814 /* Run the optimizer */
1816 const char* StatFileName;
1818 /* If we shouldn't run the optimizer, bail out */
1823 /* Check if we are requested to write optimizer statistics */
1824 StatFileName = getenv ("CC65_OPTSTATS");
1826 ReadOptStats (StatFileName);
1829 /* Print the name of the function we are working on */
1831 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1833 Print (stdout, 1, "Running optimizer for global code segment\n");
1836 /* Run groups of optimizations */
1845 /* Write statistics */
1847 WriteOptStats (StatFileName);