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 /*****************************************************************************/
55 #include "coptptrload.h"
59 #include "coptstore.h"
68 /*****************************************************************************/
70 /*****************************************************************************/
85 /*****************************************************************************/
87 /*****************************************************************************/
91 static unsigned OptShift1 (CodeSeg* S)
92 /* A call to the shlaxN routine may get replaced by one or more asl insns
93 * if the value of X is not used later. If X is used later, but it is zero
94 * on entry and it's a shift by one, it may get replaced by:
103 unsigned Changes = 0;
106 /* Generate register info */
109 /* Walk over the entries */
111 while (I < CS_GetEntryCount (S)) {
118 CodeEntry* E = CS_GetEntry (S, I);
120 /* Check for the sequence */
121 if (E->OPC == OP65_JSR &&
122 (strncmp (E->Arg, "shlax", 5) == 0 ||
123 strncmp (E->Arg, "aslax", 5) == 0) &&
124 strlen (E->Arg) == 6 &&
125 IsDigit (E->Arg[5])) {
127 if (!RegXUsed (S, I+1)) {
129 /* Insert shift insns */
130 unsigned Count = E->Arg[5] - '0';
132 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
133 CS_InsertEntry (S, X, I+1);
136 /* Delete the call to shlax */
139 /* Remember, we had changes */
142 } else if (E->RI->In.RegX == 0 &&
144 (N = CS_GetNextEntry (S, I)) != 0) {
147 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
148 CS_InsertEntry (S, X, I+1);
151 L = CS_GenLabel (S, N);
152 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, E->LI);
153 CS_InsertEntry (S, X, I+2);
156 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
157 CS_InsertEntry (S, X, I+3);
159 /* Delete the call to shlax */
162 /* Remember, we had changes */
173 /* Free the register info */
176 /* Return the number of changes made */
182 static unsigned OptShift2 (CodeSeg* S)
183 /* A call to the shraxN routine may get replaced by one or more lsr insns
184 * if the value of X is zero.
187 unsigned Changes = 0;
190 /* Generate register info */
193 /* Walk over the entries */
195 while (I < CS_GetEntryCount (S)) {
198 CodeEntry* E = CS_GetEntry (S, I);
200 /* Check for the sequence */
201 if (E->OPC == OP65_JSR &&
202 strncmp (E->Arg, "shrax", 5) == 0 &&
203 strlen (E->Arg) == 6 &&
204 IsDigit (E->Arg[5]) &&
205 E->RI->In.RegX == 0) {
207 /* Insert shift insns */
208 unsigned Count = E->Arg[5] - '0';
210 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
211 CS_InsertEntry (S, X, I+1);
214 /* Delete the call to shrax */
217 /* Remember, we had changes */
227 /* Free the register info */
230 /* Return the number of changes made */
236 static unsigned GetShiftType (const char* Sub)
237 /* Helper function for OptShift3 */
240 if (strcmp (Sub+1, "slax1") == 0) {
242 } else if (strcmp (Sub+1, "srax1") == 0) {
245 } else if (*Sub == 's') {
246 if (strcmp (Sub+1, "hlax1") == 0) {
248 } else if (strcmp (Sub+1, "hrax1") == 0) {
257 static unsigned OptShift3 (CodeSeg* S)
258 /* Search for the sequence
262 * jsr aslax1/asrax1/shlax1/shrax1
275 * or similar, provided that a/x is not used later
278 unsigned Changes = 0;
280 /* Walk over the entries */
282 while (I < CS_GetEntryCount (S)) {
288 L[0] = CS_GetEntry (S, I);
290 /* Check for the sequence */
291 if (L[0]->OPC == OP65_LDA &&
292 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
293 CS_GetEntries (S, L+1, I+1, 4) &&
294 !CS_RangeHasLabel (S, I+1, 4) &&
295 L[1]->OPC == OP65_LDX &&
296 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
297 L[2]->OPC == OP65_JSR &&
298 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
299 L[3]->OPC == OP65_STA &&
300 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
301 L[4]->OPC == OP65_STX &&
302 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
303 !RegAXUsed (S, I+5)) {
307 /* Handle the four shift types differently */
311 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
312 CS_InsertEntry (S, X, I+5);
313 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
314 CS_InsertEntry (S, X, I+6);
315 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
316 CS_InsertEntry (S, X, I+7);
317 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
318 CS_InsertEntry (S, X, I+8);
319 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
320 CS_InsertEntry (S, X, I+9);
321 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
322 CS_InsertEntry (S, X, I+10);
323 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
324 CS_InsertEntry (S, X, I+11);
325 CS_DelEntries (S, I, 5);
329 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
330 CS_InsertEntry (S, X, I+5);
331 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
332 CS_InsertEntry (S, X, I+6);
333 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
334 CS_InsertEntry (S, X, I+7);
335 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
336 CS_InsertEntry (S, X, I+8);
337 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
338 CS_InsertEntry (S, X, I+9);
339 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
340 CS_InsertEntry (S, X, I+10);
341 CS_DelEntries (S, I, 5);
346 /* These two are identical */
347 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
348 CS_InsertEntry (S, X, I+1);
349 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
350 CS_InsertEntry (S, X, I+2);
351 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
352 CS_InsertEntry (S, X, I+3);
353 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
354 CS_InsertEntry (S, X, I+4);
355 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
356 CS_InsertEntry (S, X, I+5);
357 CS_DelEntries (S, I+6, 4);
362 /* Remember, we had changes */
372 /* Return the number of changes made */
378 static unsigned OptShift4 (CodeSeg* S)
379 /* Inline the shift subroutines. */
381 unsigned Changes = 0;
383 /* Walk over the entries */
385 while (I < CS_GetEntryCount (S)) {
391 CodeEntry* E = CS_GetEntry (S, I);
393 /* Check for a call to one of the shift routine */
394 if (E->OPC == OP65_JSR &&
395 (strncmp (E->Arg, "shlax", 5) == 0 ||
396 strncmp (E->Arg, "aslax", 5) == 0) &&
397 strlen (E->Arg) == 6 &&
398 IsDigit (E->Arg[5])) {
400 /* Get number of shifts */
401 unsigned ShiftCount = (E->Arg[5] - '0');
408 * (repeat ShiftCount-1 times)
411 * which makes 4 + 3 * ShiftCount bytes, compared to the original
412 * 3 bytes for the subroutine call. However, in most cases, the
413 * final load of the X register gets merged with some other insn
414 * and replaces a txa, so for a shift count of 1, we get a factor
415 * of 200, which matches nicely the CodeSizeFactor enabled with -Oi
417 if (ShiftCount > 1 || S->CodeSizeFactor > 200) {
418 unsigned Size = 4 + 3 * ShiftCount;
419 if ((Size * 100 / 3) > S->CodeSizeFactor) {
425 /* Inline the code. Insertion point is behind the subroutine call */
429 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
430 CS_InsertEntry (S, X, IP++);
432 while (ShiftCount--) {
434 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
435 CS_InsertEntry (S, X, IP++);
438 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
439 CS_InsertEntry (S, X, IP++);
443 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
444 CS_InsertEntry (S, X, IP++);
446 /* Remove the subroutine call */
449 /* Remember, we had changes */
459 /* Return the number of changes made */
465 /*****************************************************************************/
467 /*****************************************************************************/
471 static unsigned OptLoad1 (CodeSeg* S)
472 /* Search for a call to ldaxysp where X is not used later and replace it by
473 * a load of just the A register.
477 unsigned Changes = 0;
479 /* Generate register info */
482 /* Walk over the entries */
484 while (I < CS_GetEntryCount (S)) {
489 E = CS_GetEntry (S, I);
491 /* Check for the sequence */
492 if (CE_IsCallTo (E, "ldaxysp") &&
493 RegValIsKnown (E->RI->In.RegY) &&
494 !RegXUsed (S, I+1)) {
498 /* Reload the Y register */
499 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
500 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
501 CS_InsertEntry (S, X, I+1);
503 /* Load from stack */
504 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
505 CS_InsertEntry (S, X, I+2);
507 /* Now remove the call to the subroutine */
510 /* Remember, we had changes */
520 /* Free the register info */
523 /* Return the number of changes made */
529 /*****************************************************************************/
530 /* Optimize stores through pointers */
531 /*****************************************************************************/
535 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
536 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
538 /* Check for a label attached to the entry */
539 if (CE_HasLabel (L[0])) {
543 /* Check for single insn sub ops */
544 if (L[0]->OPC == OP65_AND ||
545 L[0]->OPC == OP65_EOR ||
546 L[0]->OPC == OP65_ORA ||
547 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
548 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
553 } else if (L[0]->OPC == OP65_CLC &&
554 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
555 L[1]->OPC == OP65_ADC &&
556 !CE_HasLabel (L[1])) {
558 } else if (L[0]->OPC == OP65_SEC &&
559 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
560 L[1]->OPC == OP65_SBC &&
561 !CE_HasLabel (L[1])) {
573 static unsigned OptPtrStore1 (CodeSeg* S)
574 /* Search for the sequence:
594 * In case a/x is loaded from the register bank before the pushax, we can even
595 * use the register bank instead of ptr1.
609 unsigned Changes = 0;
611 /* Walk over the entries */
613 while (I < CS_GetEntryCount (S)) {
619 L[0] = CS_GetEntry (S, I);
621 /* Check for the sequence */
622 if (CE_IsCallTo (L[0], "pushax") &&
623 CS_GetEntries (S, L+1, I+1, 3) &&
624 L[1]->OPC == OP65_LDY &&
625 CE_IsConstImm (L[1]) &&
626 !CE_HasLabel (L[1]) &&
627 CE_IsCallTo (L[2], "ldauidx") &&
628 !CE_HasLabel (L[2]) &&
629 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
630 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
631 L[3+K]->OPC == OP65_LDY &&
632 CE_IsConstImm (L[3+K]) &&
633 !CE_HasLabel (L[3+K]) &&
634 CE_IsCallTo (L[4+K], "staspidx") &&
635 !CE_HasLabel (L[4+K])) {
638 const char* RegBank = 0;
639 const char* ZPLoc = "ptr1";
643 /* Get the preceeding two instructions and check them. We check
650 P[0] = CS_GetEntry (S, I-2);
651 P[1] = CS_GetEntry (S, I-1);
652 if (P[0]->OPC == OP65_LDA &&
653 P[0]->AM == AM65_ZP &&
654 P[1]->OPC == OP65_LDX &&
655 P[1]->AM == AM65_ZP &&
656 !CE_HasLabel (P[1]) &&
657 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
659 unsigned Len = strlen (P[0]->Arg);
661 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
662 P[1]->Arg[Len+0] == '+' &&
663 P[1]->Arg[Len+1] == '1' &&
664 P[1]->Arg[Len+2] == '\0') {
666 /* Ok, found. Use the name of the register bank */
667 RegBank = ZPLoc = P[0]->Arg;
672 /* Insert the load via the zp pointer */
673 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
674 CS_InsertEntry (S, X, I+3);
675 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
676 CS_InsertEntry (S, X, I+4);
678 /* Insert the store through the zp pointer */
679 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
680 CS_InsertEntry (S, X, I+6+K);
682 /* Delete the old code */
683 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
684 CS_DelEntry (S, I+2); /* jsr ldauidx */
686 /* Create and insert the stores into the zp pointer if needed */
688 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
689 CS_InsertEntry (S, X, I+1);
690 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
691 CS_InsertEntry (S, X, I+2);
694 /* Delete more old code. Do it here to keep a label attached to
697 CS_DelEntry (S, I); /* jsr pushax */
699 /* Remember, we had changes */
709 /* Return the number of changes made */
715 static unsigned OptPtrStore2 (CodeSeg* S)
716 /* Search for the sequence:
738 unsigned Changes = 0;
740 /* Walk over the entries */
742 while (I < CS_GetEntryCount (S)) {
748 L[0] = CS_GetEntry (S, I);
750 /* Check for the sequence */
751 if (L[0]->OPC == OP65_LDA &&
752 L[0]->AM == AM65_IMM &&
753 CS_GetEntries (S, L+1, I+1, 10) &&
754 L[1]->OPC == OP65_LDX &&
755 L[1]->AM == AM65_IMM &&
756 L[2]->OPC == OP65_CLC &&
757 L[3]->OPC == OP65_ADC &&
758 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
759 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
761 L[4]->JumpTo->Owner == L[6] &&
762 L[5]->OPC == OP65_INX &&
763 CE_IsCallTo (L[6], "pushax") &&
764 L[7]->OPC == OP65_LDX &&
765 L[8]->OPC == OP65_LDA &&
766 L[9]->OPC == OP65_LDY &&
767 CE_IsKnownImm (L[9], 0) &&
768 CE_IsCallTo (L[10], "staspidx") &&
769 !CS_RangeHasLabel (S, I+1, 5) &&
770 !CS_RangeHasLabel (S, I+7, 4) &&
771 /* Check the label last because this is quite costly */
772 (Len = strlen (L[0]->Arg)) > 3 &&
773 L[0]->Arg[0] == '<' &&
774 L[0]->Arg[1] == '(' &&
775 strlen (L[1]->Arg) == Len &&
776 L[1]->Arg[0] == '>' &&
777 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
782 /* We will create all the new stuff behind the current one so
783 * we keep the line references.
785 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
786 CS_InsertEntry (S, X, I+11);
788 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
789 CS_InsertEntry (S, X, I+12);
791 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
792 CS_InsertEntry (S, X, I+13);
794 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
796 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
797 CS_InsertEntry (S, X, I+14);
800 /* Remove the old code */
801 CS_DelEntries (S, I, 11);
803 /* Remember, we had changes */
813 /* Return the number of changes made */
819 /*****************************************************************************/
820 /* Decouple operations */
821 /*****************************************************************************/
825 static unsigned OptDecouple (CodeSeg* S)
826 /* Decouple operations, that is, do the following replacements:
840 * Provided that the register values are known of course.
843 unsigned Changes = 0;
846 /* Generate register info for the following step */
849 /* Walk over the entries */
851 while (I < CS_GetEntryCount (S)) {
855 /* Get next entry and it's input register values */
856 CodeEntry* E = CS_GetEntry (S, I);
857 const RegContents* In = &E->RI->In;
859 /* Assume we have no replacement */
862 /* Check the instruction */
866 if (RegValIsKnown (In->RegA)) {
867 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
868 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
873 if (RegValIsKnown (In->RegX)) {
874 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
875 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
880 if (RegValIsKnown (In->RegY)) {
881 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
882 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
887 if (RegValIsKnown (In->RegA)) {
888 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
889 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
894 if (RegValIsKnown (In->RegX)) {
895 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
896 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
901 if (RegValIsKnown (In->RegY)) {
902 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
903 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
908 if (E->AM == AM65_ZP) {
909 switch (GetKnownReg (E->Use & REG_ZP, In)) {
911 Arg = MakeHexArg (In->Tmp1);
912 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
916 Arg = MakeHexArg (In->Ptr1Lo);
917 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
921 Arg = MakeHexArg (In->Ptr1Hi);
922 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
926 Arg = MakeHexArg (In->SRegLo);
927 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
931 Arg = MakeHexArg (In->SRegHi);
932 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
939 if (E->AM == AM65_ZP) {
940 switch (GetKnownReg (E->Use & REG_ZP, In)) {
942 Arg = MakeHexArg (In->Tmp1);
943 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
947 Arg = MakeHexArg (In->Ptr1Lo);
948 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
952 Arg = MakeHexArg (In->Ptr1Hi);
953 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
957 Arg = MakeHexArg (In->SRegLo);
958 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
962 Arg = MakeHexArg (In->SRegHi);
963 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
970 if (E->AM == AM65_ZP) {
971 switch (GetKnownReg (E->Use, In)) {
973 Arg = MakeHexArg (In->Tmp1);
974 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
978 Arg = MakeHexArg (In->Ptr1Lo);
979 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
983 Arg = MakeHexArg (In->Ptr1Hi);
984 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
988 Arg = MakeHexArg (In->SRegLo);
989 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
993 Arg = MakeHexArg (In->SRegHi);
994 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1001 if (E->RI->In.RegA >= 0) {
1002 Arg = MakeHexArg (In->RegA);
1003 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1008 if (E->RI->In.RegA >= 0) {
1009 Arg = MakeHexArg (In->RegA);
1010 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1015 if (E->RI->In.RegX >= 0) {
1016 Arg = MakeHexArg (In->RegX);
1017 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1022 if (E->RI->In.RegY >= 0) {
1023 Arg = MakeHexArg (In->RegY);
1024 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1029 /* Avoid gcc warnings */
1034 /* Insert the replacement if we have one */
1036 CS_InsertEntry (S, X, I+1);
1046 /* Free register info */
1049 /* Return the number of changes made */
1055 /*****************************************************************************/
1056 /* struct OptFunc */
1057 /*****************************************************************************/
1061 typedef struct OptFunc OptFunc;
1063 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1064 const char* Name; /* Name of the function/group */
1065 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1066 unsigned long TotalRuns; /* Total number of runs */
1067 unsigned long LastRuns; /* Last number of runs */
1068 unsigned long TotalChanges; /* Total number of changes */
1069 unsigned long LastChanges; /* Last number of changes */
1070 char Disabled; /* True if function disabled */
1075 /*****************************************************************************/
1077 /*****************************************************************************/
1081 /* A list of all the function descriptions */
1082 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1083 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1084 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1085 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1086 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1087 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1088 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1089 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1090 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1091 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1092 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1093 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1094 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1095 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1096 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1097 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1098 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1099 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1100 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1101 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1102 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1103 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1104 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1105 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1106 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1107 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1108 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1109 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1110 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1111 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1112 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1113 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1114 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1115 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1116 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1117 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1118 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1119 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1120 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1121 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1122 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1123 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1124 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1125 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1126 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1127 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1128 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1129 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1130 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1131 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1132 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1133 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1134 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1135 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1136 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1137 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1138 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1139 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1140 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1141 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1142 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1143 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1144 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1145 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1146 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1147 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 200, 0, 0, 0, 0, 0 };
1148 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1149 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1150 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1151 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1152 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1153 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1154 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1155 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1156 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1157 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1158 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1159 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1160 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1161 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1162 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1163 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1164 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1165 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1166 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1169 /* Table containing all the steps in alphabetical order */
1170 static OptFunc* OptFuncs[] = {
1257 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1261 static int CmpOptStep (const void* Key, const void* Func)
1262 /* Compare function for bsearch */
1264 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1269 static OptFunc* FindOptFunc (const char* Name)
1270 /* Find an optimizer step by name in the table and return a pointer. Return
1271 * NULL if no such step is found.
1274 /* Search for the function in the list */
1275 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1281 static OptFunc* GetOptFunc (const char* Name)
1282 /* Find an optimizer step by name in the table and return a pointer. Print an
1283 * error and call AbEnd if not found.
1286 /* Search for the function in the list */
1287 OptFunc* F = FindOptFunc (Name);
1290 AbEnd ("Optimization step `%s' not found", Name);
1297 void DisableOpt (const char* Name)
1298 /* Disable the optimization with the given name */
1300 if (strcmp (Name, "any") == 0) {
1302 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1303 OptFuncs[I]->Disabled = 1;
1306 GetOptFunc(Name)->Disabled = 1;
1312 void EnableOpt (const char* Name)
1313 /* Enable the optimization with the given name */
1315 if (strcmp (Name, "any") == 0) {
1317 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1318 OptFuncs[I]->Disabled = 0;
1321 GetOptFunc(Name)->Disabled = 0;
1327 void ListOptSteps (FILE* F)
1328 /* List all optimization steps */
1331 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1332 fprintf (F, "%s\n", OptFuncs[I]->Name);
1338 static void ReadOptStats (const char* Name)
1339 /* Read the optimizer statistics file */
1344 /* Try to open the file */
1345 FILE* F = fopen (Name, "r");
1347 /* Ignore the error */
1351 /* Read and parse the lines */
1353 while (fgets (Buf, sizeof (Buf), F) != 0) {
1361 unsigned long TotalRuns;
1362 unsigned long TotalChanges;
1367 /* Remove trailing white space including the line terminator */
1370 while (Len > 0 && IsSpace (B[Len-1])) {
1375 /* Remove leading whitespace */
1376 while (IsSpace (*B)) {
1380 /* Check for empty and comment lines */
1381 if (*B == '\0' || *B == ';' || *B == '#') {
1385 /* Parse the line */
1386 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1391 /* Search for the optimizer step. */
1392 Func = FindOptFunc (Name);
1398 /* Found the step, set the fields */
1399 Func->TotalRuns = TotalRuns;
1400 Func->TotalChanges = TotalChanges;
1404 /* Close the file, ignore errors here. */
1410 static void WriteOptStats (const char* Name)
1411 /* Write the optimizer statistics file */
1415 /* Try to open the file */
1416 FILE* F = fopen (Name, "w");
1418 /* Ignore the error */
1422 /* Write a header */
1424 "; Optimizer Total Last Total Last\n"
1425 "; Step Runs Runs Chg Chg\n");
1428 /* Write the data */
1429 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1430 const OptFunc* O = OptFuncs[I];
1432 "%-20s %10lu %10lu %10lu %10lu\n",
1440 /* Close the file, ignore errors here. */
1446 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1447 /* Run one optimizer function Max times or until there are no more changes */
1449 unsigned Changes, C;
1451 /* Don't run the function if it is disabled or if it is prohibited by the
1454 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1458 /* Run this until there are no more changes */
1462 /* Run the function */
1469 F->TotalChanges += C;
1470 F->LastChanges += C;
1472 } while (--Max && C > 0);
1474 /* Return the number of changes */
1480 static unsigned RunOptGroup1 (CodeSeg* S)
1481 /* Run the first group of optimization steps. These steps translate known
1482 * patterns emitted by the code generator into more optimal patterns. Order
1483 * of the steps is important, because some of the steps done earlier cover
1484 * the same patterns as later steps as subpatterns.
1487 unsigned Changes = 0;
1489 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1490 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1491 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1492 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1493 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1494 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1495 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1496 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1497 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1498 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1499 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1500 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1501 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1502 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1503 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1504 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1505 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1506 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1507 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1508 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1509 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1510 Changes += RunOptFunc (S, &DOptAdd1, 1);
1511 Changes += RunOptFunc (S, &DOptAdd2, 1);
1512 Changes += RunOptFunc (S, &DOptAdd4, 1);
1513 Changes += RunOptFunc (S, &DOptStore4, 1);
1514 Changes += RunOptFunc (S, &DOptStore5, 1);
1515 Changes += RunOptFunc (S, &DOptShift1, 1);
1516 Changes += RunOptFunc (S, &DOptShift2, 1);
1517 Changes += RunOptFunc (S, &DOptShift3, 1);
1518 Changes += RunOptFunc (S, &DOptShift4, 1);
1519 Changes += RunOptFunc (S, &DOptStore1, 1);
1520 Changes += RunOptFunc (S, &DOptStore2, 5);
1521 Changes += RunOptFunc (S, &DOptStore3, 5);
1523 /* Return the number of changes */
1529 static unsigned RunOptGroup2 (CodeSeg* S)
1530 /* Run one group of optimization steps. This step involves just decoupling
1531 * instructions by replacing them by instructions that do not depend on
1532 * previous instructions. This makes it easier to find instructions that
1536 unsigned Changes = 0;
1538 Changes += RunOptFunc (S, &DOptDecouple, 1);
1540 /* Return the number of changes */
1546 static unsigned RunOptGroup3 (CodeSeg* S)
1547 /* Run one group of optimization steps. These steps depend on each other,
1548 * that means that one step may allow another step to do additional work,
1549 * so we will repeat the steps as long as we see any changes.
1552 unsigned Changes, C;
1558 C += RunOptFunc (S, &DOptNegA1, 1);
1559 C += RunOptFunc (S, &DOptNegA2, 1);
1560 C += RunOptFunc (S, &DOptSub1, 1);
1561 C += RunOptFunc (S, &DOptSub2, 1);
1562 C += RunOptFunc (S, &DOptSub3, 1);
1563 C += RunOptFunc (S, &DOptAdd5, 1);
1564 C += RunOptFunc (S, &DOptAdd6, 1);
1565 C += RunOptFunc (S, &DOptStackOps, 1);
1566 C += RunOptFunc (S, &DOptJumpCascades, 1);
1567 C += RunOptFunc (S, &DOptDeadJumps, 1);
1568 C += RunOptFunc (S, &DOptRTS, 1);
1569 C += RunOptFunc (S, &DOptDeadCode, 1);
1570 C += RunOptFunc (S, &DOptBoolTrans, 1);
1571 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1572 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1573 C += RunOptFunc (S, &DOptJumpTarget3, 1);
1574 C += RunOptFunc (S, &DOptCondBranches1, 1);
1575 C += RunOptFunc (S, &DOptCondBranches2, 1);
1576 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1577 C += RunOptFunc (S, &DOptCmp1, 1);
1578 C += RunOptFunc (S, &DOptCmp2, 1);
1579 C += RunOptFunc (S, &DOptCmp3, 1);
1580 C += RunOptFunc (S, &DOptCmp4, 1);
1581 C += RunOptFunc (S, &DOptCmp5, 1);
1582 C += RunOptFunc (S, &DOptCmp6, 1);
1583 C += RunOptFunc (S, &DOptCmp7, 1);
1584 C += RunOptFunc (S, &DOptCmp8, 1);
1585 C += RunOptFunc (S, &DOptCmp9, 1);
1586 C += RunOptFunc (S, &DOptTest1, 1);
1587 C += RunOptFunc (S, &DOptLoad1, 1);
1588 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1589 C += RunOptFunc (S, &DOptUnusedStores, 1);
1590 C += RunOptFunc (S, &DOptDupLoads, 1);
1591 C += RunOptFunc (S, &DOptStoreLoad, 1);
1592 C += RunOptFunc (S, &DOptTransfers1, 1);
1593 C += RunOptFunc (S, &DOptTransfers3, 1);
1594 C += RunOptFunc (S, &DOptTransfers4, 1);
1595 C += RunOptFunc (S, &DOptStore1, 1);
1596 C += RunOptFunc (S, &DOptStore5, 1);
1597 C += RunOptFunc (S, &DOptPushPop, 1);
1598 C += RunOptFunc (S, &DOptPrecalc, 1);
1604 /* Return the number of changes */
1610 static unsigned RunOptGroup4 (CodeSeg* S)
1611 /* 65C02 specific optimizations. */
1613 unsigned Changes = 0;
1615 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1616 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1617 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1618 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1620 /* The 65C02 replacement codes do often make the use of a register
1621 * value unnecessary, so if we have changes, run another load
1624 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1628 /* Return the number of changes */
1634 static unsigned RunOptGroup5 (CodeSeg* S)
1635 /* Run another round of pattern replacements. These are done late, since there
1636 * may be better replacements before.
1639 unsigned Changes = 0;
1641 Changes += RunOptFunc (S, &DOptPush1, 1);
1642 Changes += RunOptFunc (S, &DOptPush2, 1);
1643 /* Repeat some of the other optimizations now */
1644 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1645 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1647 /* Return the number of changes */
1653 static unsigned RunOptGroup6 (CodeSeg* S)
1654 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1655 * The latter is ony cycle slower, but if we're able to remove the necessary
1656 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1657 * shorten the code by one (transfer) or two bytes (load). So what we do is
1658 * to replace the insns, remove unused loads, and then change back all insns
1659 * where Y is still zero (meaning that the load has not been removed).
1662 unsigned Changes = 0;
1664 /* This group will only run for a standard 6502, because the 65C02 has a
1665 * better addressing mode that covers this case.
1667 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1668 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1669 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1670 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1673 /* Return the number of changes */
1679 static unsigned RunOptGroup7 (CodeSeg* S)
1680 /* The last group of optimization steps. Adjust branches, do size optimizations.
1683 unsigned Changes = 0;
1686 /* Optimize for size, that is replace operations by shorter ones, even
1687 * if this does hinder further optimizations (no problem since we're
1690 C = RunOptFunc (S, &DOptSize1, 1);
1693 /* Run some optimization passes again, since the size optimizations
1694 * may have opened new oportunities.
1696 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1697 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1698 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1699 Changes += RunOptFunc (S, &DOptStore5, 1);
1702 C = RunOptFunc (S, &DOptSize2, 1);
1705 /* Run some optimization passes again, since the size optimizations
1706 * may have opened new oportunities.
1708 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1709 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1710 Changes += RunOptFunc (S, &DOptStore5, 1);
1711 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1714 /* Adjust branch distances */
1715 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1717 /* Replace conditional branches to RTS. If we had changes, we must run dead
1718 * code elimination again, since the change may have introduced dead code.
1720 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1723 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1726 /* Return the number of changes */
1732 void RunOpt (CodeSeg* S)
1733 /* Run the optimizer */
1735 const char* StatFileName;
1737 /* If we shouldn't run the optimizer, bail out */
1742 /* Check if we are requested to write optimizer statistics */
1743 StatFileName = getenv ("CC65_OPTSTATS");
1745 ReadOptStats (StatFileName);
1748 /* Print the name of the function we are working on */
1750 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1752 Print (stdout, 1, "Running optimizer for global code segment\n");
1755 /* Run groups of optimizations */
1764 /* Write statistics */
1766 WriteOptStats (StatFileName);