1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2009 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 /*****************************************************************************/
56 #include "coptptrload.h"
60 #include "coptstore.h"
69 /*****************************************************************************/
71 /*****************************************************************************/
86 /*****************************************************************************/
88 /*****************************************************************************/
92 static unsigned OptShift1 (CodeSeg* S)
93 /* A call to the shlaxN routine may get replaced by one or more asl insns
94 * if the value of X is not used later. If X is used later, but it is zero
95 * on entry and it's a shift by one, it may get replaced by:
104 unsigned Changes = 0;
107 /* Generate register info */
110 /* Walk over the entries */
112 while (I < CS_GetEntryCount (S)) {
119 CodeEntry* E = CS_GetEntry (S, I);
121 /* Check for the sequence */
122 if (E->OPC == OP65_JSR &&
123 (strncmp (E->Arg, "shlax", 5) == 0 ||
124 strncmp (E->Arg, "aslax", 5) == 0) &&
125 strlen (E->Arg) == 6 &&
126 IsDigit (E->Arg[5])) {
128 if (!RegXUsed (S, I+1)) {
130 /* Insert shift insns */
131 unsigned Count = E->Arg[5] - '0';
133 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
134 CS_InsertEntry (S, X, I+1);
137 /* Delete the call to shlax */
140 /* Remember, we had changes */
143 } else if (E->RI->In.RegX == 0 &&
145 (N = CS_GetNextEntry (S, I)) != 0) {
148 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
149 CS_InsertEntry (S, X, I+1);
152 L = CS_GenLabel (S, N);
153 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, E->LI);
154 CS_InsertEntry (S, X, I+2);
157 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
158 CS_InsertEntry (S, X, I+3);
160 /* Delete the call to shlax */
163 /* Remember, we had changes */
174 /* Free the register info */
177 /* Return the number of changes made */
183 static unsigned OptShift2 (CodeSeg* S)
184 /* A call to the shraxN routine may get replaced by one or more lsr insns
185 * if the value of X is zero.
188 unsigned Changes = 0;
191 /* Generate register info */
194 /* Walk over the entries */
196 while (I < CS_GetEntryCount (S)) {
199 CodeEntry* E = CS_GetEntry (S, I);
201 /* Check for the sequence */
202 if (E->OPC == OP65_JSR &&
203 strncmp (E->Arg, "shrax", 5) == 0 &&
204 strlen (E->Arg) == 6 &&
205 IsDigit (E->Arg[5]) &&
206 E->RI->In.RegX == 0) {
208 /* Insert shift insns */
209 unsigned Count = E->Arg[5] - '0';
211 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
212 CS_InsertEntry (S, X, I+1);
215 /* Delete the call to shrax */
218 /* Remember, we had changes */
228 /* Free the register info */
231 /* Return the number of changes made */
237 static unsigned GetShiftType (const char* Sub)
238 /* Helper function for OptShift3 */
241 if (strcmp (Sub+1, "slax1") == 0) {
243 } else if (strcmp (Sub+1, "srax1") == 0) {
246 } else if (*Sub == 's') {
247 if (strcmp (Sub+1, "hlax1") == 0) {
249 } else if (strcmp (Sub+1, "hrax1") == 0) {
258 static unsigned OptShift3 (CodeSeg* S)
259 /* Search for the sequence
263 * jsr aslax1/asrax1/shlax1/shrax1
276 * or similar, provided that a/x is not used later
279 unsigned Changes = 0;
281 /* Walk over the entries */
283 while (I < CS_GetEntryCount (S)) {
289 L[0] = CS_GetEntry (S, I);
291 /* Check for the sequence */
292 if (L[0]->OPC == OP65_LDA &&
293 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
294 CS_GetEntries (S, L+1, I+1, 4) &&
295 !CS_RangeHasLabel (S, I+1, 4) &&
296 L[1]->OPC == OP65_LDX &&
297 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
298 L[2]->OPC == OP65_JSR &&
299 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
300 L[3]->OPC == OP65_STA &&
301 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
302 L[4]->OPC == OP65_STX &&
303 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
304 !RegAXUsed (S, I+5)) {
308 /* Handle the four shift types differently */
312 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
313 CS_InsertEntry (S, X, I+5);
314 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
315 CS_InsertEntry (S, X, I+6);
316 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
317 CS_InsertEntry (S, X, I+7);
318 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
319 CS_InsertEntry (S, X, I+8);
320 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
321 CS_InsertEntry (S, X, I+9);
322 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
323 CS_InsertEntry (S, X, I+10);
324 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
325 CS_InsertEntry (S, X, I+11);
326 CS_DelEntries (S, I, 5);
330 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
331 CS_InsertEntry (S, X, I+5);
332 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
333 CS_InsertEntry (S, X, I+6);
334 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
335 CS_InsertEntry (S, X, I+7);
336 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
337 CS_InsertEntry (S, X, I+8);
338 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
339 CS_InsertEntry (S, X, I+9);
340 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
341 CS_InsertEntry (S, X, I+10);
342 CS_DelEntries (S, I, 5);
347 /* These two are identical */
348 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
349 CS_InsertEntry (S, X, I+1);
350 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
351 CS_InsertEntry (S, X, I+2);
352 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
353 CS_InsertEntry (S, X, I+3);
354 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
355 CS_InsertEntry (S, X, I+4);
356 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
357 CS_InsertEntry (S, X, I+5);
358 CS_DelEntries (S, I+6, 4);
363 /* Remember, we had changes */
373 /* Return the number of changes made */
379 static unsigned OptShift4 (CodeSeg* S)
380 /* Inline the shift subroutines. */
382 unsigned Changes = 0;
384 /* Walk over the entries */
386 while (I < CS_GetEntryCount (S)) {
392 CodeEntry* E = CS_GetEntry (S, I);
394 /* Check for a call to one of the shift routine */
395 if (E->OPC == OP65_JSR &&
396 (strncmp (E->Arg, "shlax", 5) == 0 ||
397 strncmp (E->Arg, "aslax", 5) == 0) &&
398 strlen (E->Arg) == 6 &&
399 IsDigit (E->Arg[5])) {
401 /* Get number of shifts */
402 unsigned ShiftCount = (E->Arg[5] - '0');
409 * (repeat ShiftCount-1 times)
412 * which makes 4 + 3 * ShiftCount bytes, compared to the original
413 * 3 bytes for the subroutine call. However, in most cases, the
414 * final load of the X register gets merged with some other insn
415 * and replaces a txa, so for a shift count of 1, we get a factor
416 * of 200, which matches nicely the CodeSizeFactor enabled with -Oi
418 if (ShiftCount > 1 || S->CodeSizeFactor > 200) {
419 unsigned Size = 4 + 3 * ShiftCount;
420 if ((Size * 100 / 3) > S->CodeSizeFactor) {
426 /* Inline the code. Insertion point is behind the subroutine call */
430 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
431 CS_InsertEntry (S, X, IP++);
433 while (ShiftCount--) {
435 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
436 CS_InsertEntry (S, X, IP++);
439 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
440 CS_InsertEntry (S, X, IP++);
444 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
445 CS_InsertEntry (S, X, IP++);
447 /* Remove the subroutine call */
450 /* Remember, we had changes */
460 /* Return the number of changes made */
466 /*****************************************************************************/
468 /*****************************************************************************/
472 static unsigned OptLoad1 (CodeSeg* S)
473 /* Search for a call to ldaxysp where X is not used later and replace it by
474 * a load of just the A register.
478 unsigned Changes = 0;
480 /* Generate register info */
483 /* Walk over the entries */
485 while (I < CS_GetEntryCount (S)) {
490 E = CS_GetEntry (S, I);
492 /* Check for the sequence */
493 if (CE_IsCallTo (E, "ldaxysp") &&
494 RegValIsKnown (E->RI->In.RegY) &&
495 !RegXUsed (S, I+1)) {
499 /* Reload the Y register */
500 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
501 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
502 CS_InsertEntry (S, X, I+1);
504 /* Load from stack */
505 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
506 CS_InsertEntry (S, X, I+2);
508 /* Now remove the call to the subroutine */
511 /* Remember, we had changes */
521 /* Free the register info */
524 /* Return the number of changes made */
530 /*****************************************************************************/
531 /* Optimize stores through pointers */
532 /*****************************************************************************/
536 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
537 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
539 /* Check for a label attached to the entry */
540 if (CE_HasLabel (L[0])) {
544 /* Check for single insn sub ops */
545 if (L[0]->OPC == OP65_AND ||
546 L[0]->OPC == OP65_EOR ||
547 L[0]->OPC == OP65_ORA ||
548 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
549 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
554 } else if (L[0]->OPC == OP65_CLC &&
555 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
556 L[1]->OPC == OP65_ADC &&
557 !CE_HasLabel (L[1])) {
559 } else if (L[0]->OPC == OP65_SEC &&
560 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
561 L[1]->OPC == OP65_SBC &&
562 !CE_HasLabel (L[1])) {
574 static unsigned OptPtrStore1 (CodeSeg* S)
575 /* Search for the sequence:
595 * In case a/x is loaded from the register bank before the pushax, we can even
596 * use the register bank instead of ptr1.
610 unsigned Changes = 0;
612 /* Walk over the entries */
614 while (I < CS_GetEntryCount (S)) {
620 L[0] = CS_GetEntry (S, I);
622 /* Check for the sequence */
623 if (CE_IsCallTo (L[0], "pushax") &&
624 CS_GetEntries (S, L+1, I+1, 3) &&
625 L[1]->OPC == OP65_LDY &&
626 CE_IsConstImm (L[1]) &&
627 !CE_HasLabel (L[1]) &&
628 CE_IsCallTo (L[2], "ldauidx") &&
629 !CE_HasLabel (L[2]) &&
630 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
631 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
632 L[3+K]->OPC == OP65_LDY &&
633 CE_IsConstImm (L[3+K]) &&
634 !CE_HasLabel (L[3+K]) &&
635 CE_IsCallTo (L[4+K], "staspidx") &&
636 !CE_HasLabel (L[4+K])) {
639 const char* RegBank = 0;
640 const char* ZPLoc = "ptr1";
644 /* Get the preceeding two instructions and check them. We check
651 P[0] = CS_GetEntry (S, I-2);
652 P[1] = CS_GetEntry (S, I-1);
653 if (P[0]->OPC == OP65_LDA &&
654 P[0]->AM == AM65_ZP &&
655 P[1]->OPC == OP65_LDX &&
656 P[1]->AM == AM65_ZP &&
657 !CE_HasLabel (P[1]) &&
658 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
660 unsigned Len = strlen (P[0]->Arg);
662 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
663 P[1]->Arg[Len+0] == '+' &&
664 P[1]->Arg[Len+1] == '1' &&
665 P[1]->Arg[Len+2] == '\0') {
667 /* Ok, found. Use the name of the register bank */
668 RegBank = ZPLoc = P[0]->Arg;
673 /* Insert the load via the zp pointer */
674 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
675 CS_InsertEntry (S, X, I+3);
676 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
677 CS_InsertEntry (S, X, I+4);
679 /* Insert the store through the zp pointer */
680 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
681 CS_InsertEntry (S, X, I+6+K);
683 /* Delete the old code */
684 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
685 CS_DelEntry (S, I+2); /* jsr ldauidx */
687 /* Create and insert the stores into the zp pointer if needed */
689 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
690 CS_InsertEntry (S, X, I+1);
691 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
692 CS_InsertEntry (S, X, I+2);
695 /* Delete more old code. Do it here to keep a label attached to
698 CS_DelEntry (S, I); /* jsr pushax */
700 /* Remember, we had changes */
710 /* Return the number of changes made */
716 static unsigned OptPtrStore2 (CodeSeg* S)
717 /* Search for the sequence:
739 unsigned Changes = 0;
741 /* Walk over the entries */
743 while (I < CS_GetEntryCount (S)) {
749 L[0] = CS_GetEntry (S, I);
751 /* Check for the sequence */
752 if (L[0]->OPC == OP65_LDA &&
753 L[0]->AM == AM65_IMM &&
754 CS_GetEntries (S, L+1, I+1, 10) &&
755 L[1]->OPC == OP65_LDX &&
756 L[1]->AM == AM65_IMM &&
757 L[2]->OPC == OP65_CLC &&
758 L[3]->OPC == OP65_ADC &&
759 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
760 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
762 L[4]->JumpTo->Owner == L[6] &&
763 L[5]->OPC == OP65_INX &&
764 CE_IsCallTo (L[6], "pushax") &&
765 L[7]->OPC == OP65_LDX &&
766 L[8]->OPC == OP65_LDA &&
767 L[9]->OPC == OP65_LDY &&
768 CE_IsKnownImm (L[9], 0) &&
769 CE_IsCallTo (L[10], "staspidx") &&
770 !CS_RangeHasLabel (S, I+1, 5) &&
771 !CS_RangeHasLabel (S, I+7, 4) &&
772 /* Check the label last because this is quite costly */
773 (Len = strlen (L[0]->Arg)) > 3 &&
774 L[0]->Arg[0] == '<' &&
775 L[0]->Arg[1] == '(' &&
776 strlen (L[1]->Arg) == Len &&
777 L[1]->Arg[0] == '>' &&
778 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
783 /* We will create all the new stuff behind the current one so
784 * we keep the line references.
786 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
787 CS_InsertEntry (S, X, I+11);
789 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
790 CS_InsertEntry (S, X, I+12);
792 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
793 CS_InsertEntry (S, X, I+13);
795 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
797 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
798 CS_InsertEntry (S, X, I+14);
801 /* Remove the old code */
802 CS_DelEntries (S, I, 11);
804 /* Remember, we had changes */
814 /* Return the number of changes made */
820 /*****************************************************************************/
821 /* Decouple operations */
822 /*****************************************************************************/
826 static unsigned OptDecouple (CodeSeg* S)
827 /* Decouple operations, that is, do the following replacements:
841 * Provided that the register values are known of course.
844 unsigned Changes = 0;
847 /* Generate register info for the following step */
850 /* Walk over the entries */
852 while (I < CS_GetEntryCount (S)) {
856 /* Get next entry and it's input register values */
857 CodeEntry* E = CS_GetEntry (S, I);
858 const RegContents* In = &E->RI->In;
860 /* Assume we have no replacement */
863 /* Check the instruction */
867 if (RegValIsKnown (In->RegA)) {
868 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
869 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
874 if (RegValIsKnown (In->RegX)) {
875 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
876 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
881 if (RegValIsKnown (In->RegY)) {
882 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
883 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
888 if (RegValIsKnown (In->RegA)) {
889 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
890 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
895 if (RegValIsKnown (In->RegX)) {
896 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
897 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
902 if (RegValIsKnown (In->RegY)) {
903 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
904 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
909 if (E->AM == AM65_ZP) {
910 switch (GetKnownReg (E->Use & REG_ZP, In)) {
912 Arg = MakeHexArg (In->Tmp1);
913 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
917 Arg = MakeHexArg (In->Ptr1Lo);
918 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
922 Arg = MakeHexArg (In->Ptr1Hi);
923 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
927 Arg = MakeHexArg (In->SRegLo);
928 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
932 Arg = MakeHexArg (In->SRegHi);
933 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
940 if (E->AM == AM65_ZP) {
941 switch (GetKnownReg (E->Use & REG_ZP, In)) {
943 Arg = MakeHexArg (In->Tmp1);
944 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
948 Arg = MakeHexArg (In->Ptr1Lo);
949 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
953 Arg = MakeHexArg (In->Ptr1Hi);
954 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
958 Arg = MakeHexArg (In->SRegLo);
959 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
963 Arg = MakeHexArg (In->SRegHi);
964 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
971 if (E->AM == AM65_ZP) {
972 switch (GetKnownReg (E->Use, In)) {
974 Arg = MakeHexArg (In->Tmp1);
975 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
979 Arg = MakeHexArg (In->Ptr1Lo);
980 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
984 Arg = MakeHexArg (In->Ptr1Hi);
985 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
989 Arg = MakeHexArg (In->SRegLo);
990 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
994 Arg = MakeHexArg (In->SRegHi);
995 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1002 if (E->RI->In.RegA >= 0) {
1003 Arg = MakeHexArg (In->RegA);
1004 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1009 if (E->RI->In.RegA >= 0) {
1010 Arg = MakeHexArg (In->RegA);
1011 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1016 if (E->RI->In.RegX >= 0) {
1017 Arg = MakeHexArg (In->RegX);
1018 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1023 if (E->RI->In.RegY >= 0) {
1024 Arg = MakeHexArg (In->RegY);
1025 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1030 /* Avoid gcc warnings */
1035 /* Insert the replacement if we have one */
1037 CS_InsertEntry (S, X, I+1);
1047 /* Free register info */
1050 /* Return the number of changes made */
1056 /*****************************************************************************/
1057 /* Optimize stack pointer ops */
1058 /*****************************************************************************/
1062 static unsigned IsDecSP (const CodeEntry* E)
1063 /* Check if this is an insn that decrements the stack pointer. If so, return
1064 * the decrement. If not, return zero.
1065 * The function expects E to be a subroutine call.
1068 if (strncmp (E->Arg, "decsp", 5) == 0) {
1069 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
1070 return (E->Arg[5] - '0');
1072 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
1073 return E->RI->In.RegY;
1076 /* If we come here, it's not a decsp op */
1082 static unsigned OptStackPtrOps (CodeSeg* S)
1083 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
1087 unsigned Changes = 0;
1090 /* Generate register info for the following step */
1093 /* Walk over the entries */
1095 while (I < CS_GetEntryCount (S)) {
1101 /* Get the next entry */
1102 const CodeEntry* E = CS_GetEntry (S, I);
1104 /* Check for decspn or subysp */
1105 if (E->OPC == OP65_JSR &&
1106 (Dec1 = IsDecSP (E)) > 0 &&
1107 (N = CS_GetNextEntry (S, I)) != 0 &&
1108 (Dec2 = IsDecSP (N)) > 0 &&
1109 (Dec1 += Dec2) <= 255 &&
1115 /* We can combine the two */
1117 /* Insert a call to decsp */
1118 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
1119 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
1120 CS_InsertEntry (S, X, I+2);
1122 /* Insert a call to subysp */
1123 const char* Arg = MakeHexArg (Dec1);
1124 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
1125 CS_InsertEntry (S, X, I+2);
1126 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
1127 CS_InsertEntry (S, X, I+3);
1130 /* Delete the old code */
1131 CS_DelEntries (S, I, 2);
1133 /* Regenerate register info */
1136 /* Remember we had changes */
1147 /* Free register info */
1150 /* Return the number of changes made */
1156 /*****************************************************************************/
1157 /* struct OptFunc */
1158 /*****************************************************************************/
1162 typedef struct OptFunc OptFunc;
1164 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1165 const char* Name; /* Name of the function/group */
1166 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1167 unsigned long TotalRuns; /* Total number of runs */
1168 unsigned long LastRuns; /* Last number of runs */
1169 unsigned long TotalChanges; /* Total number of changes */
1170 unsigned long LastChanges; /* Last number of changes */
1171 char Disabled; /* True if function disabled */
1176 /*****************************************************************************/
1178 /*****************************************************************************/
1182 /* A list of all the function descriptions */
1183 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1184 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1185 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1186 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1187 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1188 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1189 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1190 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1191 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1192 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1193 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1194 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1195 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1196 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1197 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1198 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1199 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1200 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1201 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1202 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1203 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1204 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1205 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1206 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1207 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1208 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1209 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1210 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1211 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1212 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1213 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1214 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1215 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1216 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1217 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1218 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1219 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1220 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1221 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1222 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1223 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1224 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1225 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1226 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1227 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1228 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1229 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1230 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1231 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1232 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1233 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1234 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1235 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1236 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1237 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1238 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1239 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1240 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1241 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1242 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1243 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1244 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1245 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1246 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1247 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1248 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 200, 0, 0, 0, 0, 0 };
1249 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1250 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1251 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1252 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
1253 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1254 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1255 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1256 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1257 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1258 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1259 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1260 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1261 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1262 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1263 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1264 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1265 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1266 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1267 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1268 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1271 /* Table containing all the steps in alphabetical order */
1272 static OptFunc* OptFuncs[] = {
1360 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1364 static int CmpOptStep (const void* Key, const void* Func)
1365 /* Compare function for bsearch */
1367 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1372 static OptFunc* FindOptFunc (const char* Name)
1373 /* Find an optimizer step by name in the table and return a pointer. Return
1374 * NULL if no such step is found.
1377 /* Search for the function in the list */
1378 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1384 static OptFunc* GetOptFunc (const char* Name)
1385 /* Find an optimizer step by name in the table and return a pointer. Print an
1386 * error and call AbEnd if not found.
1389 /* Search for the function in the list */
1390 OptFunc* F = FindOptFunc (Name);
1393 AbEnd ("Optimization step `%s' not found", Name);
1400 void DisableOpt (const char* Name)
1401 /* Disable the optimization with the given name */
1403 if (strcmp (Name, "any") == 0) {
1405 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1406 OptFuncs[I]->Disabled = 1;
1409 GetOptFunc(Name)->Disabled = 1;
1415 void EnableOpt (const char* Name)
1416 /* Enable the optimization with the given name */
1418 if (strcmp (Name, "any") == 0) {
1420 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1421 OptFuncs[I]->Disabled = 0;
1424 GetOptFunc(Name)->Disabled = 0;
1430 void ListOptSteps (FILE* F)
1431 /* List all optimization steps */
1434 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1435 fprintf (F, "%s\n", OptFuncs[I]->Name);
1441 static void ReadOptStats (const char* Name)
1442 /* Read the optimizer statistics file */
1447 /* Try to open the file */
1448 FILE* F = fopen (Name, "r");
1450 /* Ignore the error */
1454 /* Read and parse the lines */
1456 while (fgets (Buf, sizeof (Buf), F) != 0) {
1464 unsigned long TotalRuns;
1465 unsigned long TotalChanges;
1470 /* Remove trailing white space including the line terminator */
1473 while (Len > 0 && IsSpace (B[Len-1])) {
1478 /* Remove leading whitespace */
1479 while (IsSpace (*B)) {
1483 /* Check for empty and comment lines */
1484 if (*B == '\0' || *B == ';' || *B == '#') {
1488 /* Parse the line */
1489 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1494 /* Search for the optimizer step. */
1495 Func = FindOptFunc (Name);
1501 /* Found the step, set the fields */
1502 Func->TotalRuns = TotalRuns;
1503 Func->TotalChanges = TotalChanges;
1507 /* Close the file, ignore errors here. */
1513 static void WriteOptStats (const char* Name)
1514 /* Write the optimizer statistics file */
1518 /* Try to open the file */
1519 FILE* F = fopen (Name, "w");
1521 /* Ignore the error */
1525 /* Write a header */
1527 "; Optimizer Total Last Total Last\n"
1528 "; Step Runs Runs Chg Chg\n");
1531 /* Write the data */
1532 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1533 const OptFunc* O = OptFuncs[I];
1535 "%-20s %10lu %10lu %10lu %10lu\n",
1543 /* Close the file, ignore errors here. */
1549 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1550 /* Run one optimizer function Max times or until there are no more changes */
1552 unsigned Changes, C;
1554 /* Don't run the function if it is disabled or if it is prohibited by the
1557 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1561 /* Run this until there are no more changes */
1565 /* Run the function */
1572 F->TotalChanges += C;
1573 F->LastChanges += C;
1575 } while (--Max && C > 0);
1577 /* Return the number of changes */
1583 static unsigned RunOptGroup1 (CodeSeg* S)
1584 /* Run the first group of optimization steps. These steps translate known
1585 * patterns emitted by the code generator into more optimal patterns. Order
1586 * of the steps is important, because some of the steps done earlier cover
1587 * the same patterns as later steps as subpatterns.
1590 unsigned Changes = 0;
1592 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1593 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1594 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1595 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1596 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1597 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1598 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1599 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1600 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1601 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1602 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1603 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1604 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1605 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1606 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1607 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1608 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1609 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1610 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1611 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1612 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1613 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1614 Changes += RunOptFunc (S, &DOptAdd1, 1);
1615 Changes += RunOptFunc (S, &DOptAdd2, 1);
1616 Changes += RunOptFunc (S, &DOptAdd4, 1);
1617 Changes += RunOptFunc (S, &DOptStore4, 1);
1618 Changes += RunOptFunc (S, &DOptStore5, 1);
1619 Changes += RunOptFunc (S, &DOptShift1, 1);
1620 Changes += RunOptFunc (S, &DOptShift2, 1);
1621 Changes += RunOptFunc (S, &DOptShift3, 1);
1622 Changes += RunOptFunc (S, &DOptShift4, 1);
1623 Changes += RunOptFunc (S, &DOptStore1, 1);
1624 Changes += RunOptFunc (S, &DOptStore2, 5);
1625 Changes += RunOptFunc (S, &DOptStore3, 5);
1627 /* Return the number of changes */
1633 static unsigned RunOptGroup2 (CodeSeg* S)
1634 /* Run one group of optimization steps. This step involves just decoupling
1635 * instructions by replacing them by instructions that do not depend on
1636 * previous instructions. This makes it easier to find instructions that
1640 unsigned Changes = 0;
1642 Changes += RunOptFunc (S, &DOptDecouple, 1);
1644 /* Return the number of changes */
1650 static unsigned RunOptGroup3 (CodeSeg* S)
1651 /* Run one group of optimization steps. These steps depend on each other,
1652 * that means that one step may allow another step to do additional work,
1653 * so we will repeat the steps as long as we see any changes.
1656 unsigned Changes, C;
1662 C += RunOptFunc (S, &DOptNegA1, 1);
1663 C += RunOptFunc (S, &DOptNegA2, 1);
1664 C += RunOptFunc (S, &DOptSub1, 1);
1665 C += RunOptFunc (S, &DOptSub2, 1);
1666 C += RunOptFunc (S, &DOptSub3, 1);
1667 C += RunOptFunc (S, &DOptAdd5, 1);
1668 C += RunOptFunc (S, &DOptAdd6, 1);
1669 C += RunOptFunc (S, &DOptStackOps, 1);
1670 C += RunOptFunc (S, &DOptJumpCascades, 1);
1671 C += RunOptFunc (S, &DOptDeadJumps, 1);
1672 C += RunOptFunc (S, &DOptRTS, 1);
1673 C += RunOptFunc (S, &DOptDeadCode, 1);
1674 C += RunOptFunc (S, &DOptBoolTrans, 1);
1675 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1676 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1677 C += RunOptFunc (S, &DOptJumpTarget3, 1);
1678 C += RunOptFunc (S, &DOptCondBranches1, 1);
1679 C += RunOptFunc (S, &DOptCondBranches2, 1);
1680 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1681 C += RunOptFunc (S, &DOptCmp1, 1);
1682 C += RunOptFunc (S, &DOptCmp2, 1);
1683 C += RunOptFunc (S, &DOptCmp3, 1);
1684 C += RunOptFunc (S, &DOptCmp4, 1);
1685 C += RunOptFunc (S, &DOptCmp5, 1);
1686 C += RunOptFunc (S, &DOptCmp6, 1);
1687 C += RunOptFunc (S, &DOptCmp7, 1);
1688 C += RunOptFunc (S, &DOptCmp8, 1);
1689 C += RunOptFunc (S, &DOptCmp9, 1);
1690 C += RunOptFunc (S, &DOptTest1, 1);
1691 C += RunOptFunc (S, &DOptLoad1, 1);
1692 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1693 C += RunOptFunc (S, &DOptUnusedStores, 1);
1694 C += RunOptFunc (S, &DOptDupLoads, 1);
1695 C += RunOptFunc (S, &DOptStoreLoad, 1);
1696 C += RunOptFunc (S, &DOptTransfers1, 1);
1697 C += RunOptFunc (S, &DOptTransfers3, 1);
1698 C += RunOptFunc (S, &DOptTransfers4, 1);
1699 C += RunOptFunc (S, &DOptStore1, 1);
1700 C += RunOptFunc (S, &DOptStore5, 1);
1701 C += RunOptFunc (S, &DOptPushPop, 1);
1702 C += RunOptFunc (S, &DOptPrecalc, 1);
1708 /* Return the number of changes */
1714 static unsigned RunOptGroup4 (CodeSeg* S)
1715 /* 65C02 specific optimizations. */
1717 unsigned Changes = 0;
1719 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1720 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1721 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1722 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1724 /* The 65C02 replacement codes do often make the use of a register
1725 * value unnecessary, so if we have changes, run another load
1728 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1732 /* Return the number of changes */
1738 static unsigned RunOptGroup5 (CodeSeg* S)
1739 /* Run another round of pattern replacements. These are done late, since there
1740 * may be better replacements before.
1743 unsigned Changes = 0;
1745 Changes += RunOptFunc (S, &DOptPush1, 1);
1746 Changes += RunOptFunc (S, &DOptPush2, 1);
1747 /* Repeat some of the other optimizations now */
1748 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1749 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1751 /* Return the number of changes */
1757 static unsigned RunOptGroup6 (CodeSeg* S)
1758 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1759 * The latter is ony cycle slower, but if we're able to remove the necessary
1760 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1761 * shorten the code by one (transfer) or two bytes (load). So what we do is
1762 * to replace the insns, remove unused loads, and then change back all insns
1763 * where Y is still zero (meaning that the load has not been removed).
1766 unsigned Changes = 0;
1768 /* This group will only run for a standard 6502, because the 65C02 has a
1769 * better addressing mode that covers this case.
1771 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1772 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1773 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1774 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1777 /* Return the number of changes */
1783 static unsigned RunOptGroup7 (CodeSeg* S)
1784 /* The last group of optimization steps. Adjust branches, do size optimizations.
1787 unsigned Changes = 0;
1790 /* Optimize for size, that is replace operations by shorter ones, even
1791 * if this does hinder further optimizations (no problem since we're
1794 C = RunOptFunc (S, &DOptSize1, 1);
1797 /* Run some optimization passes again, since the size optimizations
1798 * may have opened new oportunities.
1800 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1801 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1802 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1803 Changes += RunOptFunc (S, &DOptStore5, 1);
1806 C = RunOptFunc (S, &DOptSize2, 1);
1809 /* Run some optimization passes again, since the size optimizations
1810 * may have opened new oportunities.
1812 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1813 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1814 Changes += RunOptFunc (S, &DOptStore5, 1);
1815 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1818 /* Adjust branch distances */
1819 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1821 /* Replace conditional branches to RTS. If we had changes, we must run dead
1822 * code elimination again, since the change may have introduced dead code.
1824 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1827 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1830 /* Return the number of changes */
1836 void RunOpt (CodeSeg* S)
1837 /* Run the optimizer */
1839 const char* StatFileName;
1841 /* If we shouldn't run the optimizer, bail out */
1846 /* Check if we are requested to write optimizer statistics */
1847 StatFileName = getenv ("CC65_OPTSTATS");
1849 ReadOptStats (StatFileName);
1852 /* Print the name of the function we are working on */
1854 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1856 Print (stdout, 1, "Running optimizer for global code segment\n");
1859 /* Run groups of optimizations */
1868 /* Write statistics */
1870 WriteOptStats (StatFileName);