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. For shift counts > 1, more
271 unsigned Changes = 0;
274 /* Generate register info */
277 /* Walk over the entries */
279 while (I < CS_GetEntryCount (S)) {
284 L[0] = CS_GetEntry (S, I);
286 /* Check for the sequence */
287 if ((L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
289 L[0]->RI->In.RegX == 0 &&
290 CS_GetEntries (S, L+1, I+1, 2) &&
291 L[1]->OPC == OP65_INX &&
292 L[0]->JumpTo->Owner == L[2] &&
293 !CS_RangeHasLabel (S, I, 2) &&
294 L[2]->OPC == OP65_JSR &&
295 strlen (L[2]->Arg) == 6 &&
296 memcmp (L[2]->Arg, "shrax", 5) == 0 &&
297 IsDigit (L[2]->Arg[5]) &&
298 !RegXUsed (S, I+3)) {
300 unsigned ShiftCount = (L[2]->Arg[5] - '0');
302 /* Add the replacement insn instead */
303 CodeEntry* X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
304 CS_InsertEntry (S, X, I+3);
305 while (--ShiftCount) {
306 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
307 CS_InsertEntry (S, X, I+4);
310 /* Remove the bcs/dex/jsr */
311 CS_DelEntries (S, I, 3);
313 /* Remember, we had changes */
323 /* Free the register info */
326 /* Return the number of changes made */
332 static unsigned OptShift4 (CodeSeg* S)
333 /* A call to the shraxN routine may get replaced by one or more lsr insns
334 * if the value of X is zero.
337 unsigned Changes = 0;
340 /* Generate register info */
343 /* Walk over the entries */
345 while (I < CS_GetEntryCount (S)) {
348 CodeEntry* E = CS_GetEntry (S, I);
350 /* Check for the sequence */
351 if (E->OPC == OP65_JSR &&
352 strncmp (E->Arg, "shrax", 5) == 0 &&
353 strlen (E->Arg) == 6 &&
354 IsDigit (E->Arg[5]) &&
355 E->RI->In.RegX == 0) {
357 /* Insert shift insns */
358 unsigned Count = E->Arg[5] - '0';
360 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
361 CS_InsertEntry (S, X, I+1);
364 /* Delete the call to shrax */
367 /* Remember, we had changes */
377 /* Free the register info */
380 /* Return the number of changes made */
386 static unsigned GetShiftType (const char* Sub)
387 /* Helper function for OptShift5 */
390 if (strcmp (Sub+1, "slax1") == 0) {
392 } else if (strcmp (Sub+1, "srax1") == 0) {
395 } else if (*Sub == 's') {
396 if (strcmp (Sub+1, "hlax1") == 0) {
398 } else if (strcmp (Sub+1, "hrax1") == 0) {
407 static unsigned OptShift5 (CodeSeg* S)
408 /* Search for the sequence
412 * jsr aslax1/asrax1/shlax1/shrax1
425 * or similar, provided that a/x is not used later
428 unsigned Changes = 0;
430 /* Walk over the entries */
432 while (I < CS_GetEntryCount (S)) {
438 L[0] = CS_GetEntry (S, I);
440 /* Check for the sequence */
441 if (L[0]->OPC == OP65_LDA &&
442 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
443 CS_GetEntries (S, L+1, I+1, 4) &&
444 !CS_RangeHasLabel (S, I+1, 4) &&
445 L[1]->OPC == OP65_LDX &&
446 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
447 L[2]->OPC == OP65_JSR &&
448 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
449 L[3]->OPC == OP65_STA &&
450 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
451 L[4]->OPC == OP65_STX &&
452 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
453 !RegAXUsed (S, I+5)) {
457 /* Handle the four shift types differently */
461 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
462 CS_InsertEntry (S, X, I+5);
463 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
464 CS_InsertEntry (S, X, I+6);
465 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
466 CS_InsertEntry (S, X, I+7);
467 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
468 CS_InsertEntry (S, X, I+8);
469 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
470 CS_InsertEntry (S, X, I+9);
471 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
472 CS_InsertEntry (S, X, I+10);
473 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
474 CS_InsertEntry (S, X, I+11);
475 CS_DelEntries (S, I, 5);
479 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
480 CS_InsertEntry (S, X, I+5);
481 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
482 CS_InsertEntry (S, X, I+6);
483 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
484 CS_InsertEntry (S, X, I+7);
485 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
486 CS_InsertEntry (S, X, I+8);
487 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
488 CS_InsertEntry (S, X, I+9);
489 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
490 CS_InsertEntry (S, X, I+10);
491 CS_DelEntries (S, I, 5);
496 /* These two are identical */
497 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
498 CS_InsertEntry (S, X, I+1);
499 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
500 CS_InsertEntry (S, X, I+2);
501 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
502 CS_InsertEntry (S, X, I+3);
503 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
504 CS_InsertEntry (S, X, I+4);
505 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
506 CS_InsertEntry (S, X, I+5);
507 CS_DelEntries (S, I+6, 4);
512 /* Remember, we had changes */
522 /* Return the number of changes made */
528 static unsigned OptShift6 (CodeSeg* S)
529 /* Inline the shift subroutines. */
531 unsigned Changes = 0;
533 /* Walk over the entries */
535 while (I < CS_GetEntryCount (S)) {
541 CodeEntry* E = CS_GetEntry (S, I);
543 /* Check for a call to one of the shift routine */
544 if (E->OPC == OP65_JSR &&
545 (strncmp (E->Arg, "shlax", 5) == 0 ||
546 strncmp (E->Arg, "aslax", 5) == 0) &&
547 strlen (E->Arg) == 6 &&
548 IsDigit (E->Arg[5])) {
550 /* Get number of shifts */
551 unsigned ShiftCount = (E->Arg[5] - '0');
558 * (repeat ShiftCount-1 times)
561 * which makes 4 + 3 * ShiftCount bytes, compared to the original
562 * 3 bytes for the subroutine call. However, in most cases, the
563 * final load of the X register gets merged with some other insn
564 * and replaces a txa, so for a shift count of 1, we get a factor
565 * of 200, which matches nicely the CodeSizeFactor enabled with -Oi
567 if (ShiftCount > 1 || S->CodeSizeFactor > 200) {
568 unsigned Size = 4 + 3 * ShiftCount;
569 if ((Size * 100 / 3) > S->CodeSizeFactor) {
575 /* Inline the code. Insertion point is behind the subroutine call */
579 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
580 CS_InsertEntry (S, X, IP++);
582 while (ShiftCount--) {
584 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
585 CS_InsertEntry (S, X, IP++);
588 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
589 CS_InsertEntry (S, X, IP++);
593 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
594 CS_InsertEntry (S, X, IP++);
596 /* Remove the subroutine call */
599 /* Remember, we had changes */
609 /* Return the number of changes made */
615 /*****************************************************************************/
617 /*****************************************************************************/
621 static unsigned OptLoad1 (CodeSeg* S)
622 /* Search for a call to ldaxysp where X is not used later and replace it by
623 * a load of just the A register.
627 unsigned Changes = 0;
629 /* Generate register info */
632 /* Walk over the entries */
634 while (I < CS_GetEntryCount (S)) {
639 E = CS_GetEntry (S, I);
641 /* Check for the sequence */
642 if (CE_IsCallTo (E, "ldaxysp") &&
643 RegValIsKnown (E->RI->In.RegY) &&
644 !RegXUsed (S, I+1)) {
648 /* Reload the Y register */
649 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
650 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
651 CS_InsertEntry (S, X, I+1);
653 /* Load from stack */
654 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
655 CS_InsertEntry (S, X, I+2);
657 /* Now remove the call to the subroutine */
660 /* Remember, we had changes */
670 /* Free the register info */
673 /* Return the number of changes made */
679 static unsigned OptLoad2 (CodeSeg* S)
680 /* Replace calls to ldaxysp by inline code */
683 unsigned Changes = 0;
685 /* Generate register info */
688 /* Walk over the entries */
690 while (I < CS_GetEntryCount (S)) {
695 L[0] = CS_GetEntry (S, I);
697 /* Check for the sequence */
698 if (CE_IsCallTo (L[0], "ldaxysp")) {
702 /* Followed by sta abs/stx abs? */
703 if (CS_GetEntries (S, L+1, I+1, 2) &&
704 L[1]->OPC == OP65_STA &&
705 L[2]->OPC == OP65_STX &&
708 strcmp (L[1]->Arg, L[2]->Arg) != 0) &&
709 !CS_RangeHasLabel (S, I+1, 2) &&
710 !RegXUsed (S, I+3)) {
712 /* A/X are stored into memory somewhere and X is not used
717 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
718 CS_InsertEntry (S, X, I+3);
721 X = NewCodeEntry (OP65_STA, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
722 CS_InsertEntry (S, X, I+4);
725 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[0]->LI);
726 CS_InsertEntry (S, X, I+5);
729 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
730 CS_InsertEntry (S, X, I+6);
733 X = NewCodeEntry (OP65_STA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
734 CS_InsertEntry (S, X, I+7);
736 /* Now remove the call to the subroutine and the sta/stx */
737 CS_DelEntries (S, I, 3);
741 /* Standard replacement */
744 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
745 CS_InsertEntry (S, X, I+1);
748 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
749 CS_InsertEntry (S, X, I+2);
752 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[0]->LI);
753 CS_InsertEntry (S, X, I+3);
756 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
757 CS_InsertEntry (S, X, I+4);
759 /* Now remove the call to the subroutine */
764 /* Remember, we had changes */
773 /* Free the register info */
776 /* Return the number of changes made */
782 /*****************************************************************************/
783 /* Decouple operations */
784 /*****************************************************************************/
788 static unsigned OptDecouple (CodeSeg* S)
789 /* Decouple operations, that is, do the following replacements:
803 * Provided that the register values are known of course.
806 unsigned Changes = 0;
809 /* Generate register info for the following step */
812 /* Walk over the entries */
814 while (I < CS_GetEntryCount (S)) {
818 /* Get next entry and it's input register values */
819 CodeEntry* E = CS_GetEntry (S, I);
820 const RegContents* In = &E->RI->In;
822 /* Assume we have no replacement */
825 /* Check the instruction */
829 if (RegValIsKnown (In->RegA)) {
830 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
831 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
836 if (RegValIsKnown (In->RegX)) {
837 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
838 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
843 if (RegValIsKnown (In->RegY)) {
844 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
845 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
850 if (RegValIsKnown (In->RegA)) {
851 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
852 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
857 if (RegValIsKnown (In->RegX)) {
858 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
859 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
864 if (RegValIsKnown (In->RegY)) {
865 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
866 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
871 if (E->AM == AM65_ZP) {
872 switch (GetKnownReg (E->Use & REG_ZP, In)) {
874 Arg = MakeHexArg (In->Tmp1);
875 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
879 Arg = MakeHexArg (In->Ptr1Lo);
880 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
884 Arg = MakeHexArg (In->Ptr1Hi);
885 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
889 Arg = MakeHexArg (In->SRegLo);
890 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
894 Arg = MakeHexArg (In->SRegHi);
895 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
902 if (E->AM == AM65_ZP) {
903 switch (GetKnownReg (E->Use & REG_ZP, In)) {
905 Arg = MakeHexArg (In->Tmp1);
906 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
910 Arg = MakeHexArg (In->Ptr1Lo);
911 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
915 Arg = MakeHexArg (In->Ptr1Hi);
916 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
920 Arg = MakeHexArg (In->SRegLo);
921 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
925 Arg = MakeHexArg (In->SRegHi);
926 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
933 if (E->AM == AM65_ZP) {
934 switch (GetKnownReg (E->Use, In)) {
936 Arg = MakeHexArg (In->Tmp1);
937 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
941 Arg = MakeHexArg (In->Ptr1Lo);
942 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
946 Arg = MakeHexArg (In->Ptr1Hi);
947 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
951 Arg = MakeHexArg (In->SRegLo);
952 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
956 Arg = MakeHexArg (In->SRegHi);
957 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
964 if (E->RI->In.RegA >= 0) {
965 Arg = MakeHexArg (In->RegA);
966 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
971 if (E->RI->In.RegA >= 0) {
972 Arg = MakeHexArg (In->RegA);
973 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
978 if (E->RI->In.RegX >= 0) {
979 Arg = MakeHexArg (In->RegX);
980 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
985 if (E->RI->In.RegY >= 0) {
986 Arg = MakeHexArg (In->RegY);
987 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
992 /* Avoid gcc warnings */
997 /* Insert the replacement if we have one */
999 CS_InsertEntry (S, X, I+1);
1009 /* Free register info */
1012 /* Return the number of changes made */
1018 /*****************************************************************************/
1019 /* Optimize stack pointer ops */
1020 /*****************************************************************************/
1024 static unsigned IsDecSP (const CodeEntry* E)
1025 /* Check if this is an insn that decrements the stack pointer. If so, return
1026 * the decrement. If not, return zero.
1027 * The function expects E to be a subroutine call.
1030 if (strncmp (E->Arg, "decsp", 5) == 0) {
1031 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
1032 return (E->Arg[5] - '0');
1034 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
1035 return E->RI->In.RegY;
1038 /* If we come here, it's not a decsp op */
1044 static unsigned OptStackPtrOps (CodeSeg* S)
1045 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
1049 unsigned Changes = 0;
1052 /* Generate register info for the following step */
1055 /* Walk over the entries */
1057 while (I < CS_GetEntryCount (S)) {
1063 /* Get the next entry */
1064 const CodeEntry* E = CS_GetEntry (S, I);
1066 /* Check for decspn or subysp */
1067 if (E->OPC == OP65_JSR &&
1068 (Dec1 = IsDecSP (E)) > 0 &&
1069 (N = CS_GetNextEntry (S, I)) != 0 &&
1070 (Dec2 = IsDecSP (N)) > 0 &&
1071 (Dec1 += Dec2) <= 255 &&
1077 /* We can combine the two */
1079 /* Insert a call to decsp */
1080 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
1081 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
1082 CS_InsertEntry (S, X, I+2);
1084 /* Insert a call to subysp */
1085 const char* Arg = MakeHexArg (Dec1);
1086 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
1087 CS_InsertEntry (S, X, I+2);
1088 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
1089 CS_InsertEntry (S, X, I+3);
1092 /* Delete the old code */
1093 CS_DelEntries (S, I, 2);
1095 /* Regenerate register info */
1098 /* Remember we had changes */
1109 /* Free register info */
1112 /* Return the number of changes made */
1118 /*****************************************************************************/
1119 /* struct OptFunc */
1120 /*****************************************************************************/
1124 typedef struct OptFunc OptFunc;
1126 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1127 const char* Name; /* Name of the function/group */
1128 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1129 unsigned long TotalRuns; /* Total number of runs */
1130 unsigned long LastRuns; /* Last number of runs */
1131 unsigned long TotalChanges; /* Total number of changes */
1132 unsigned long LastChanges; /* Last number of changes */
1133 char Disabled; /* True if function disabled */
1138 /*****************************************************************************/
1140 /*****************************************************************************/
1144 /* A list of all the function descriptions */
1145 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1146 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1147 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1148 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1149 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1150 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1151 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1152 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1153 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1154 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1155 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1156 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1157 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1158 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1159 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1160 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1161 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1162 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1163 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1164 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1165 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1166 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1167 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1168 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1169 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1170 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1171 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1172 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1173 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1174 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1175 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1176 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1177 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1178 static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
1179 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1180 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1181 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1182 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1183 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1184 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1185 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1186 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1187 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1188 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1189 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1190 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1191 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1192 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1193 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1194 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1195 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1196 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1197 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1198 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1199 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1200 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1201 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1202 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1203 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 40, 0, 0, 0, 0, 0 };
1204 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 50, 0, 0, 0, 0, 0 };
1205 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 50, 0, 0, 0, 0, 0 };
1206 static OptFunc DOptPtrStore4 = { OptPtrStore4, "OptPtrStore4", 65, 0, 0, 0, 0, 0 };
1207 static OptFunc DOptPtrStore5 = { OptPtrStore5, "OptPtrStore5", 65, 0, 0, 0, 0, 0 };
1208 static OptFunc DOptPtrStore6 = { OptPtrStore6, "OptPtrStore6", 100, 0, 0, 0, 0, 0 };
1209 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1210 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1211 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1212 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1213 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1214 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 17, 0, 0, 0, 0, 0 };
1215 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 100, 0, 0, 0, 0, 0 };
1216 static OptFunc DOptShift5 = { OptShift5, "OptShift5", 110, 0, 0, 0, 0, 0 };
1217 static OptFunc DOptShift6 = { OptShift6, "OptShift6", 200, 0, 0, 0, 0, 0 };
1218 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1219 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1220 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1221 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
1222 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1223 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 115, 0, 0, 0, 0, 0 };
1224 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1225 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1226 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1227 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1228 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1229 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1230 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1231 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
1232 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
1233 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1234 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1235 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1236 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1237 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1238 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1241 /* Table containing all the steps in alphabetical order */
1242 static OptFunc* OptFuncs[] = {
1338 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1342 static int CmpOptStep (const void* Key, const void* Func)
1343 /* Compare function for bsearch */
1345 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1350 static OptFunc* FindOptFunc (const char* Name)
1351 /* Find an optimizer step by name in the table and return a pointer. Return
1352 * NULL if no such step is found.
1355 /* Search for the function in the list */
1356 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1362 static OptFunc* GetOptFunc (const char* Name)
1363 /* Find an optimizer step by name in the table and return a pointer. Print an
1364 * error and call AbEnd if not found.
1367 /* Search for the function in the list */
1368 OptFunc* F = FindOptFunc (Name);
1371 AbEnd ("Optimization step `%s' not found", Name);
1378 void DisableOpt (const char* Name)
1379 /* Disable the optimization with the given name */
1381 if (strcmp (Name, "any") == 0) {
1383 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1384 OptFuncs[I]->Disabled = 1;
1387 GetOptFunc(Name)->Disabled = 1;
1393 void EnableOpt (const char* Name)
1394 /* Enable the optimization with the given name */
1396 if (strcmp (Name, "any") == 0) {
1398 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1399 OptFuncs[I]->Disabled = 0;
1402 GetOptFunc(Name)->Disabled = 0;
1408 void ListOptSteps (FILE* F)
1409 /* List all optimization steps */
1412 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1413 fprintf (F, "%s\n", OptFuncs[I]->Name);
1419 static void ReadOptStats (const char* Name)
1420 /* Read the optimizer statistics file */
1425 /* Try to open the file */
1426 FILE* F = fopen (Name, "r");
1428 /* Ignore the error */
1432 /* Read and parse the lines */
1434 while (fgets (Buf, sizeof (Buf), F) != 0) {
1442 unsigned long TotalRuns;
1443 unsigned long TotalChanges;
1448 /* Remove trailing white space including the line terminator */
1451 while (Len > 0 && IsSpace (B[Len-1])) {
1456 /* Remove leading whitespace */
1457 while (IsSpace (*B)) {
1461 /* Check for empty and comment lines */
1462 if (*B == '\0' || *B == ';' || *B == '#') {
1466 /* Parse the line */
1467 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1472 /* Search for the optimizer step. */
1473 Func = FindOptFunc (Name);
1479 /* Found the step, set the fields */
1480 Func->TotalRuns = TotalRuns;
1481 Func->TotalChanges = TotalChanges;
1485 /* Close the file, ignore errors here. */
1491 static void WriteOptStats (const char* Name)
1492 /* Write the optimizer statistics file */
1496 /* Try to open the file */
1497 FILE* F = fopen (Name, "w");
1499 /* Ignore the error */
1503 /* Write a header */
1505 "; Optimizer Total Last Total Last\n"
1506 "; Step Runs Runs Chg Chg\n");
1509 /* Write the data */
1510 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1511 const OptFunc* O = OptFuncs[I];
1513 "%-20s %10lu %10lu %10lu %10lu\n",
1521 /* Close the file, ignore errors here. */
1527 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1528 /* Run one optimizer function Max times or until there are no more changes */
1530 unsigned Changes, C;
1532 /* Don't run the function if it is disabled or if it is prohibited by the
1535 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1539 /* Run this until there are no more changes */
1543 /* Run the function */
1545 if (Debug && C > 0) {
1546 printf ("Applied %s: %u changes\n", F->Name, C);
1553 F->TotalChanges += C;
1554 F->LastChanges += C;
1556 } while (--Max && C > 0);
1558 /* Return the number of changes */
1564 static unsigned RunOptGroup1 (CodeSeg* S)
1565 /* Run the first group of optimization steps. These steps translate known
1566 * patterns emitted by the code generator into more optimal patterns. Order
1567 * of the steps is important, because some of the steps done earlier cover
1568 * the same patterns as later steps as subpatterns.
1571 unsigned Changes = 0;
1573 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1574 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1575 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1576 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1577 Changes += RunOptFunc (S, &DOptPtrStore4, 1);
1578 Changes += RunOptFunc (S, &DOptPtrStore5, 1);
1579 Changes += RunOptFunc (S, &DOptPtrStore6, 1);
1580 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1581 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1582 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1583 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1584 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1585 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1586 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1587 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1588 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1589 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1590 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1591 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1592 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1593 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1594 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1595 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1596 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1597 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1598 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1599 Changes += RunOptFunc (S, &DOptAdd1, 1);
1600 Changes += RunOptFunc (S, &DOptAdd2, 1);
1601 Changes += RunOptFunc (S, &DOptAdd4, 1);
1602 Changes += RunOptFunc (S, &DOptAdd5, 1);
1603 Changes += RunOptFunc (S, &DOptAdd6, 1);
1604 Changes += RunOptFunc (S, &DOptSub1, 1);
1605 Changes += RunOptFunc (S, &DOptSub3, 1);
1606 Changes += RunOptFunc (S, &DOptStore4, 1);
1607 Changes += RunOptFunc (S, &DOptStore5, 1);
1608 Changes += RunOptFunc (S, &DOptShift1, 1);
1609 Changes += RunOptFunc (S, &DOptShift2, 1);
1610 Changes += RunOptFunc (S, &DOptShift4, 1);
1611 Changes += RunOptFunc (S, &DOptShift5, 1);
1612 Changes += RunOptFunc (S, &DOptShift6, 1);
1613 Changes += RunOptFunc (S, &DOptStore1, 1);
1614 Changes += RunOptFunc (S, &DOptStore2, 5);
1615 Changes += RunOptFunc (S, &DOptStore3, 5);
1617 /* Return the number of changes */
1623 static unsigned RunOptGroup2 (CodeSeg* S)
1624 /* Run one group of optimization steps. This step involves just decoupling
1625 * instructions by replacing them by instructions that do not depend on
1626 * previous instructions. This makes it easier to find instructions that
1630 unsigned Changes = 0;
1632 Changes += RunOptFunc (S, &DOptDecouple, 1);
1634 /* Return the number of changes */
1640 static unsigned RunOptGroup3 (CodeSeg* S)
1641 /* Run one group of optimization steps. These steps depend on each other,
1642 * that means that one step may allow another step to do additional work,
1643 * so we will repeat the steps as long as we see any changes.
1646 unsigned Changes, C;
1652 C += RunOptFunc (S, &DOptNegA1, 1);
1653 C += RunOptFunc (S, &DOptNegA2, 1);
1654 C += RunOptFunc (S, &DOptStackOps, 1);
1655 C += RunOptFunc (S, &DOptSub1, 1);
1656 C += RunOptFunc (S, &DOptSub2, 1);
1657 C += RunOptFunc (S, &DOptSub3, 1);
1658 C += RunOptFunc (S, &DOptAdd5, 1);
1659 C += RunOptFunc (S, &DOptAdd6, 1);
1660 C += RunOptFunc (S, &DOptJumpCascades, 1);
1661 C += RunOptFunc (S, &DOptDeadJumps, 1);
1662 C += RunOptFunc (S, &DOptRTS, 1);
1663 C += RunOptFunc (S, &DOptDeadCode, 1);
1664 C += RunOptFunc (S, &DOptBoolTrans, 1);
1665 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1666 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1667 C += RunOptFunc (S, &DOptCondBranches1, 1);
1668 C += RunOptFunc (S, &DOptCondBranches2, 1);
1669 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1670 C += RunOptFunc (S, &DOptCmp1, 1);
1671 C += RunOptFunc (S, &DOptCmp2, 1);
1672 C += RunOptFunc (S, &DOptCmp3, 1);
1673 C += RunOptFunc (S, &DOptCmp4, 1);
1674 C += RunOptFunc (S, &DOptCmp5, 1);
1675 C += RunOptFunc (S, &DOptCmp6, 1);
1676 C += RunOptFunc (S, &DOptCmp7, 1);
1677 C += RunOptFunc (S, &DOptCmp8, 1);
1678 C += RunOptFunc (S, &DOptCmp9, 1);
1679 C += RunOptFunc (S, &DOptTest1, 1);
1680 C += RunOptFunc (S, &DOptLoad1, 1);
1681 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1682 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1683 C += RunOptFunc (S, &DOptUnusedStores, 1);
1684 C += RunOptFunc (S, &DOptDupLoads, 1);
1685 C += RunOptFunc (S, &DOptStoreLoad, 1);
1686 C += RunOptFunc (S, &DOptTransfers1, 1);
1687 C += RunOptFunc (S, &DOptTransfers3, 1);
1688 C += RunOptFunc (S, &DOptTransfers4, 1);
1689 C += RunOptFunc (S, &DOptStore1, 1);
1690 C += RunOptFunc (S, &DOptStore5, 1);
1691 C += RunOptFunc (S, &DOptPushPop, 1);
1692 C += RunOptFunc (S, &DOptPrecalc, 1);
1698 /* Return the number of changes */
1704 static unsigned RunOptGroup4 (CodeSeg* S)
1705 /* Run another round of pattern replacements. These are done late, since there
1706 * may be better replacements before.
1709 unsigned Changes = 0;
1711 /* Repeat some of the steps here */
1712 Changes += RunOptFunc (S, &DOptShift3, 1);
1713 Changes += RunOptFunc (S, &DOptPush1, 1);
1714 Changes += RunOptFunc (S, &DOptPush2, 1);
1715 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1716 Changes += RunOptFunc (S, &DOptTest2, 1);
1717 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1718 Changes += RunOptFunc (S, &DOptLoad2, 1);
1720 /* Return the number of changes */
1726 static unsigned RunOptGroup5 (CodeSeg* S)
1727 /* 65C02 specific optimizations. */
1729 unsigned Changes = 0;
1731 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1732 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1733 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1734 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1736 /* The 65C02 replacement codes do often make the use of a register
1737 * value unnecessary, so if we have changes, run another load
1740 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1744 /* Return the number of changes */
1750 static unsigned RunOptGroup6 (CodeSeg* S)
1751 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1752 * The latter is ony cycle slower, but if we're able to remove the necessary
1753 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1754 * shorten the code by one (transfer) or two bytes (load). So what we do is
1755 * to replace the insns, remove unused loads, and then change back all insns
1756 * where Y is still zero (meaning that the load has not been removed).
1759 unsigned Changes = 0;
1761 /* This group will only run for a standard 6502, because the 65C02 has a
1762 * better addressing mode that covers this case.
1764 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1765 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1766 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1767 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1770 /* Return the number of changes */
1776 static unsigned RunOptGroup7 (CodeSeg* S)
1777 /* The last group of optimization steps. Adjust branches, do size optimizations.
1780 unsigned Changes = 0;
1783 /* Optimize for size, that is replace operations by shorter ones, even
1784 * if this does hinder further optimizations (no problem since we're
1787 C = RunOptFunc (S, &DOptSize1, 1);
1790 /* Run some optimization passes again, since the size optimizations
1791 * may have opened new oportunities.
1793 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1794 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1795 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1796 Changes += RunOptFunc (S, &DOptStore5, 1);
1799 C = RunOptFunc (S, &DOptSize2, 1);
1802 /* Run some optimization passes again, since the size optimizations
1803 * may have opened new oportunities.
1805 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1806 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1807 Changes += RunOptFunc (S, &DOptStore5, 1);
1808 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1811 /* Adjust branch distances */
1812 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1814 /* Replace conditional branches to RTS. If we had changes, we must run dead
1815 * code elimination again, since the change may have introduced dead code.
1817 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1820 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1823 /* Return the number of changes */
1829 void RunOpt (CodeSeg* S)
1830 /* Run the optimizer */
1832 const char* StatFileName;
1834 /* If we shouldn't run the optimizer, bail out */
1839 /* Check if we are requested to write optimizer statistics */
1840 StatFileName = getenv ("CC65_OPTSTATS");
1842 ReadOptStats (StatFileName);
1845 /* Print the name of the function we are working on */
1847 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1849 Print (stdout, 1, "Running optimizer for global code segment\n");
1852 /* Run groups of optimizations */
1861 /* Write statistics */
1863 WriteOptStats (StatFileName);