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;
105 /* Generate register info */
108 /* Walk over the entries */
110 while (I < CS_GetEntryCount (S)) {
117 CodeEntry* E = CS_GetEntry (S, I);
119 /* Check for the sequence */
120 if (E->OPC == OP65_JSR &&
121 (strncmp (E->Arg, "shlax", 5) == 0 ||
122 strncmp (E->Arg, "aslax", 5) == 0) &&
123 strlen (E->Arg) == 6 &&
124 IsDigit (E->Arg[5])) {
126 if (!RegXUsed (S, I+1)) {
128 /* Insert shift insns */
129 unsigned Count = E->Arg[5] - '0';
131 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
132 CS_InsertEntry (S, X, I+1);
135 /* Delete the call to shlax */
138 /* Remember, we had changes */
141 } else if (E->RI->In.RegX == 0 &&
143 (N = CS_GetNextEntry (S, I)) != 0) {
146 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
147 CS_InsertEntry (S, X, I+1);
150 L = CS_GenLabel (S, N);
151 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, E->LI);
152 CS_InsertEntry (S, X, I+2);
155 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
156 CS_InsertEntry (S, X, I+3);
158 /* Delete the call to shlax */
161 /* Remember, we had changes */
172 /* Free the register info */
175 /* Return the number of changes made */
181 static unsigned OptShift2 (CodeSeg* S)
182 /* A call to the shraxN routine may get replaced by one or more lsr insns
183 * if the value of X is zero.
186 unsigned Changes = 0;
189 /* Generate register info */
192 /* Walk over the entries */
194 while (I < CS_GetEntryCount (S)) {
197 CodeEntry* E = CS_GetEntry (S, I);
199 /* Check for the sequence */
200 if (E->OPC == OP65_JSR &&
201 strncmp (E->Arg, "shrax", 5) == 0 &&
202 strlen (E->Arg) == 6 &&
203 IsDigit (E->Arg[5]) &&
204 E->RI->In.RegX == 0) {
206 /* Insert shift insns */
207 unsigned Count = E->Arg[5] - '0';
209 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
210 CS_InsertEntry (S, X, I+1);
213 /* Delete the call to shlax */
216 /* Remember, we had changes */
226 /* Free the register info */
229 /* Return the number of changes made */
235 static unsigned GetShiftType (const char* Sub)
236 /* Helper function for OptShift3 */
239 if (strcmp (Sub+1, "slax1") == 0) {
241 } else if (strcmp (Sub+1, "srax1") == 0) {
244 } else if (*Sub == 's') {
245 if (strcmp (Sub+1, "hlax1") == 0) {
247 } else if (strcmp (Sub+1, "hrax1") == 0) {
256 static unsigned OptShift3 (CodeSeg* S)
257 /* Search for the sequence
261 * jsr aslax1/asrax1/shlax1/shrax1
274 * or similar, provided that a/x is not used later
277 unsigned Changes = 0;
279 /* Walk over the entries */
281 while (I < CS_GetEntryCount (S)) {
287 L[0] = CS_GetEntry (S, I);
289 /* Check for the sequence */
290 if (L[0]->OPC == OP65_LDA &&
291 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
292 CS_GetEntries (S, L+1, I+1, 4) &&
293 !CS_RangeHasLabel (S, I+1, 4) &&
294 L[1]->OPC == OP65_LDX &&
295 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
296 L[2]->OPC == OP65_JSR &&
297 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
298 L[3]->OPC == OP65_STA &&
299 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
300 L[4]->OPC == OP65_STX &&
301 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
302 !RegAXUsed (S, I+5)) {
306 /* Handle the four shift types differently */
310 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
311 CS_InsertEntry (S, X, I+5);
312 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
313 CS_InsertEntry (S, X, I+6);
314 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
315 CS_InsertEntry (S, X, I+7);
316 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
317 CS_InsertEntry (S, X, I+8);
318 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
319 CS_InsertEntry (S, X, I+9);
320 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
321 CS_InsertEntry (S, X, I+10);
322 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
323 CS_InsertEntry (S, X, I+11);
324 CS_DelEntries (S, I, 5);
328 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
329 CS_InsertEntry (S, X, I+5);
330 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
331 CS_InsertEntry (S, X, I+6);
332 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
333 CS_InsertEntry (S, X, I+7);
334 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
335 CS_InsertEntry (S, X, I+8);
336 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
337 CS_InsertEntry (S, X, I+9);
338 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
339 CS_InsertEntry (S, X, I+10);
340 CS_DelEntries (S, I, 5);
345 /* These two are identical */
346 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
347 CS_InsertEntry (S, X, I+1);
348 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
349 CS_InsertEntry (S, X, I+2);
350 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
351 CS_InsertEntry (S, X, I+3);
352 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
353 CS_InsertEntry (S, X, I+4);
354 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
355 CS_InsertEntry (S, X, I+5);
356 CS_DelEntries (S, I+6, 4);
361 /* Remember, we had changes */
371 /* Return the number of changes made */
377 /*****************************************************************************/
379 /*****************************************************************************/
383 static unsigned OptLoad1 (CodeSeg* S)
384 /* Search for a call to ldaxysp where X is not used later and replace it by
385 * a load of just the A register.
389 unsigned Changes = 0;
391 /* Generate register info */
394 /* Walk over the entries */
396 while (I < CS_GetEntryCount (S)) {
401 E = CS_GetEntry (S, I);
403 /* Check for the sequence */
404 if (CE_IsCallTo (E, "ldaxysp") &&
405 RegValIsKnown (E->RI->In.RegY) &&
406 !RegXUsed (S, I+1)) {
410 /* Reload the Y register */
411 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
412 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
413 CS_InsertEntry (S, X, I+1);
415 /* Load from stack */
416 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
417 CS_InsertEntry (S, X, I+2);
419 /* Now remove the call to the subroutine */
422 /* Remember, we had changes */
432 /* Free the register info */
435 /* Return the number of changes made */
441 /*****************************************************************************/
442 /* Optimize stores through pointers */
443 /*****************************************************************************/
447 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
448 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
450 /* Check for a label attached to the entry */
451 if (CE_HasLabel (L[0])) {
455 /* Check for single insn sub ops */
456 if (L[0]->OPC == OP65_AND ||
457 L[0]->OPC == OP65_EOR ||
458 L[0]->OPC == OP65_ORA ||
459 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
460 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
465 } else if (L[0]->OPC == OP65_CLC &&
466 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
467 L[1]->OPC == OP65_ADC &&
468 !CE_HasLabel (L[1])) {
470 } else if (L[0]->OPC == OP65_SEC &&
471 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
472 L[1]->OPC == OP65_SBC &&
473 !CE_HasLabel (L[1])) {
485 static unsigned OptPtrStore1 (CodeSeg* S)
486 /* Search for the sequence:
506 * In case a/x is loaded from the register bank before the pushax, we can even
507 * use the register bank instead of ptr1.
521 unsigned Changes = 0;
523 /* Walk over the entries */
525 while (I < CS_GetEntryCount (S)) {
531 L[0] = CS_GetEntry (S, I);
533 /* Check for the sequence */
534 if (CE_IsCallTo (L[0], "pushax") &&
535 CS_GetEntries (S, L+1, I+1, 3) &&
536 L[1]->OPC == OP65_LDY &&
537 CE_IsConstImm (L[1]) &&
538 !CE_HasLabel (L[1]) &&
539 CE_IsCallTo (L[2], "ldauidx") &&
540 !CE_HasLabel (L[2]) &&
541 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
542 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
543 L[3+K]->OPC == OP65_LDY &&
544 CE_IsConstImm (L[3+K]) &&
545 !CE_HasLabel (L[3+K]) &&
546 CE_IsCallTo (L[4+K], "staspidx") &&
547 !CE_HasLabel (L[4+K])) {
550 const char* RegBank = 0;
551 const char* ZPLoc = "ptr1";
555 /* Get the preceeding two instructions and check them. We check
562 P[0] = CS_GetEntry (S, I-2);
563 P[1] = CS_GetEntry (S, I-1);
564 if (P[0]->OPC == OP65_LDA &&
565 P[0]->AM == AM65_ZP &&
566 P[1]->OPC == OP65_LDX &&
567 P[1]->AM == AM65_ZP &&
568 !CE_HasLabel (P[1]) &&
569 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
571 unsigned Len = strlen (P[0]->Arg);
573 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
574 P[1]->Arg[Len+0] == '+' &&
575 P[1]->Arg[Len+1] == '1' &&
576 P[1]->Arg[Len+2] == '\0') {
578 /* Ok, found. Use the name of the register bank */
579 RegBank = ZPLoc = P[0]->Arg;
584 /* Insert the load via the zp pointer */
585 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
586 CS_InsertEntry (S, X, I+3);
587 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
588 CS_InsertEntry (S, X, I+4);
590 /* Insert the store through the zp pointer */
591 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
592 CS_InsertEntry (S, X, I+6+K);
594 /* Delete the old code */
595 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
596 CS_DelEntry (S, I+2); /* jsr ldauidx */
598 /* Create and insert the stores into the zp pointer if needed */
600 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
601 CS_InsertEntry (S, X, I+1);
602 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
603 CS_InsertEntry (S, X, I+2);
606 /* Delete more old code. Do it here to keep a label attached to
609 CS_DelEntry (S, I); /* jsr pushax */
611 /* Remember, we had changes */
621 /* Return the number of changes made */
627 static unsigned OptPtrStore2 (CodeSeg* S)
628 /* Search for the sequence:
650 unsigned Changes = 0;
652 /* Walk over the entries */
654 while (I < CS_GetEntryCount (S)) {
660 L[0] = CS_GetEntry (S, I);
662 /* Check for the sequence */
663 if (L[0]->OPC == OP65_LDA &&
664 L[0]->AM == AM65_IMM &&
665 CS_GetEntries (S, L+1, I+1, 10) &&
666 L[1]->OPC == OP65_LDX &&
667 L[1]->AM == AM65_IMM &&
668 L[2]->OPC == OP65_CLC &&
669 L[3]->OPC == OP65_ADC &&
670 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
671 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
673 L[4]->JumpTo->Owner == L[6] &&
674 L[5]->OPC == OP65_INX &&
675 CE_IsCallTo (L[6], "pushax") &&
676 L[7]->OPC == OP65_LDX &&
677 L[8]->OPC == OP65_LDA &&
678 L[9]->OPC == OP65_LDY &&
679 CE_IsKnownImm (L[9], 0) &&
680 CE_IsCallTo (L[10], "staspidx") &&
681 !CS_RangeHasLabel (S, I+1, 5) &&
682 !CS_RangeHasLabel (S, I+7, 4) &&
683 /* Check the label last because this is quite costly */
684 (Len = strlen (L[0]->Arg)) > 3 &&
685 L[0]->Arg[0] == '<' &&
686 L[0]->Arg[1] == '(' &&
687 strlen (L[1]->Arg) == Len &&
688 L[1]->Arg[0] == '>' &&
689 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
694 /* We will create all the new stuff behind the current one so
695 * we keep the line references.
697 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
698 CS_InsertEntry (S, X, I+11);
700 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
701 CS_InsertEntry (S, X, I+12);
703 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
704 CS_InsertEntry (S, X, I+13);
706 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
708 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
709 CS_InsertEntry (S, X, I+14);
712 /* Remove the old code */
713 CS_DelEntries (S, I, 11);
715 /* Remember, we had changes */
725 /* Return the number of changes made */
731 /*****************************************************************************/
732 /* Decouple operations */
733 /*****************************************************************************/
737 static unsigned OptDecouple (CodeSeg* S)
738 /* Decouple operations, that is, do the following replacements:
752 * Provided that the register values are known of course.
755 unsigned Changes = 0;
758 /* Generate register info for the following step */
761 /* Walk over the entries */
763 while (I < CS_GetEntryCount (S)) {
767 /* Get next entry and it's input register values */
768 CodeEntry* E = CS_GetEntry (S, I);
769 const RegContents* In = &E->RI->In;
771 /* Assume we have no replacement */
774 /* Check the instruction */
778 if (RegValIsKnown (In->RegA)) {
779 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
780 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
785 if (RegValIsKnown (In->RegX)) {
786 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
787 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
792 if (RegValIsKnown (In->RegY)) {
793 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
794 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
799 if (RegValIsKnown (In->RegA)) {
800 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
801 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
806 if (RegValIsKnown (In->RegX)) {
807 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
808 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
813 if (RegValIsKnown (In->RegY)) {
814 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
815 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
820 if (E->AM == AM65_ZP) {
821 switch (GetKnownReg (E->Use & REG_ZP, In)) {
823 Arg = MakeHexArg (In->Tmp1);
824 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
828 Arg = MakeHexArg (In->Ptr1Lo);
829 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
833 Arg = MakeHexArg (In->Ptr1Hi);
834 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
838 Arg = MakeHexArg (In->SRegLo);
839 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
843 Arg = MakeHexArg (In->SRegHi);
844 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
851 if (E->AM == AM65_ZP) {
852 switch (GetKnownReg (E->Use & REG_ZP, In)) {
854 Arg = MakeHexArg (In->Tmp1);
855 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
859 Arg = MakeHexArg (In->Ptr1Lo);
860 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
864 Arg = MakeHexArg (In->Ptr1Hi);
865 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
869 Arg = MakeHexArg (In->SRegLo);
870 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
874 Arg = MakeHexArg (In->SRegHi);
875 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
882 if (E->AM == AM65_ZP) {
883 switch (GetKnownReg (E->Use, In)) {
885 Arg = MakeHexArg (In->Tmp1);
886 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
890 Arg = MakeHexArg (In->Ptr1Lo);
891 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
895 Arg = MakeHexArg (In->Ptr1Hi);
896 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
900 Arg = MakeHexArg (In->SRegLo);
901 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
905 Arg = MakeHexArg (In->SRegHi);
906 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
913 if (E->RI->In.RegA >= 0) {
914 Arg = MakeHexArg (In->RegA);
915 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
920 if (E->RI->In.RegA >= 0) {
921 Arg = MakeHexArg (In->RegA);
922 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
927 if (E->RI->In.RegX >= 0) {
928 Arg = MakeHexArg (In->RegX);
929 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
934 if (E->RI->In.RegY >= 0) {
935 Arg = MakeHexArg (In->RegY);
936 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
941 /* Avoid gcc warnings */
946 /* Insert the replacement if we have one */
948 CS_InsertEntry (S, X, I+1);
958 /* Free register info */
961 /* Return the number of changes made */
967 /*****************************************************************************/
969 /*****************************************************************************/
973 typedef struct OptFunc OptFunc;
975 unsigned (*Func) (CodeSeg*); /* Optimizer function */
976 const char* Name; /* Name of the function/group */
977 unsigned CodeSizeFactor; /* Code size factor for this opt func */
978 unsigned long TotalRuns; /* Total number of runs */
979 unsigned long LastRuns; /* Last number of runs */
980 unsigned long TotalChanges; /* Total number of changes */
981 unsigned long LastChanges; /* Last number of changes */
982 char Disabled; /* True if function disabled */
987 /*****************************************************************************/
989 /*****************************************************************************/
993 /* A list of all the function descriptions */
994 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
995 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
996 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
997 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
998 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
999 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1000 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1001 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1002 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1003 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1004 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1005 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1006 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1007 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1008 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1009 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1010 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1011 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1012 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1013 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1014 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1015 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1016 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1017 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1018 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1019 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1020 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1021 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1022 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1023 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1024 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1025 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1026 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1027 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1028 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1029 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1030 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1031 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1032 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1033 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1034 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1035 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1036 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1037 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1038 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1039 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1040 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1041 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1042 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1043 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1044 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1045 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1046 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1047 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1048 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1049 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1050 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1051 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1052 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1053 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1054 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1055 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1056 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1057 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1058 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1059 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1060 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1061 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1062 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1063 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1064 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1065 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1066 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1067 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1068 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1069 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1070 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1071 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1072 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1075 /* Table containing all the steps in alphabetical order */
1076 static OptFunc* OptFuncs[] = {
1157 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1161 static int CmpOptStep (const void* Key, const void* Func)
1162 /* Compare function for bsearch */
1164 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1169 static OptFunc* FindOptFunc (const char* Name)
1170 /* Find an optimizer step by name in the table and return a pointer. Return
1171 * NULL if no such step is found.
1174 /* Search for the function in the list */
1175 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1181 static OptFunc* GetOptFunc (const char* Name)
1182 /* Find an optimizer step by name in the table and return a pointer. Print an
1183 * error and call AbEnd if not found.
1186 /* Search for the function in the list */
1187 OptFunc* F = FindOptFunc (Name);
1190 AbEnd ("Optimization step `%s' not found", Name);
1197 void DisableOpt (const char* Name)
1198 /* Disable the optimization with the given name */
1200 if (strcmp (Name, "any") == 0) {
1202 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1203 OptFuncs[I]->Disabled = 1;
1206 GetOptFunc(Name)->Disabled = 1;
1212 void EnableOpt (const char* Name)
1213 /* Enable the optimization with the given name */
1215 if (strcmp (Name, "any") == 0) {
1217 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1218 OptFuncs[I]->Disabled = 0;
1221 GetOptFunc(Name)->Disabled = 0;
1227 void ListOptSteps (FILE* F)
1228 /* List all optimization steps */
1231 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1232 fprintf (F, "%s\n", OptFuncs[I]->Name);
1238 static void ReadOptStats (const char* Name)
1239 /* Read the optimizer statistics file */
1244 /* Try to open the file */
1245 FILE* F = fopen (Name, "r");
1247 /* Ignore the error */
1251 /* Read and parse the lines */
1253 while (fgets (Buf, sizeof (Buf), F) != 0) {
1261 unsigned long TotalRuns;
1262 unsigned long TotalChanges;
1267 /* Remove trailing white space including the line terminator */
1270 while (Len > 0 && IsSpace (B[Len-1])) {
1275 /* Remove leading whitespace */
1276 while (IsSpace (*B)) {
1280 /* Check for empty and comment lines */
1281 if (*B == '\0' || *B == ';' || *B == '#') {
1285 /* Parse the line */
1286 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1291 /* Search for the optimizer step. */
1292 Func = FindOptFunc (Name);
1298 /* Found the step, set the fields */
1299 Func->TotalRuns = TotalRuns;
1300 Func->TotalChanges = TotalChanges;
1304 /* Close the file, ignore errors here. */
1310 static void WriteOptStats (const char* Name)
1311 /* Write the optimizer statistics file */
1315 /* Try to open the file */
1316 FILE* F = fopen (Name, "w");
1318 /* Ignore the error */
1322 /* Write a header */
1324 "; Optimizer Total Last Total Last\n"
1325 "; Step Runs Runs Chg Chg\n");
1328 /* Write the data */
1329 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1330 const OptFunc* O = OptFuncs[I];
1332 "%-20s %10lu %10lu %10lu %10lu\n",
1340 /* Close the file, ignore errors here. */
1346 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1347 /* Run one optimizer function Max times or until there are no more changes */
1349 unsigned Changes, C;
1351 /* Don't run the function if it is disabled or if it is prohibited by the
1354 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1358 /* Run this until there are no more changes */
1362 /* Run the function */
1369 F->TotalChanges += C;
1370 F->LastChanges += C;
1372 } while (--Max && C > 0);
1374 /* Return the number of changes */
1380 static unsigned RunOptGroup1 (CodeSeg* S)
1381 /* Run the first group of optimization steps. These steps translate known
1382 * patterns emitted by the code generator into more optimal patterns. Order
1383 * of the steps is important, because some of the steps done earlier cover
1384 * the same patterns as later steps as subpatterns.
1387 unsigned Changes = 0;
1389 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1390 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1391 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1392 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1393 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1394 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1395 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1396 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1397 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1398 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1399 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1400 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1401 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1402 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1403 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1404 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1405 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1406 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1407 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1408 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1409 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1410 Changes += RunOptFunc (S, &DOptAdd1, 1);
1411 Changes += RunOptFunc (S, &DOptAdd2, 1);
1412 Changes += RunOptFunc (S, &DOptAdd4, 1);
1413 Changes += RunOptFunc (S, &DOptStore4, 1);
1414 Changes += RunOptFunc (S, &DOptStore5, 1);
1415 Changes += RunOptFunc (S, &DOptShift1, 1);
1416 Changes += RunOptFunc (S, &DOptShift2, 1);
1417 Changes += RunOptFunc (S, &DOptShift3, 1);
1418 Changes += RunOptFunc (S, &DOptStore1, 1);
1419 Changes += RunOptFunc (S, &DOptStore2, 5);
1420 Changes += RunOptFunc (S, &DOptStore3, 5);
1422 /* Return the number of changes */
1428 static unsigned RunOptGroup2 (CodeSeg* S)
1429 /* Run one group of optimization steps. This step involves just decoupling
1430 * instructions by replacing them by instructions that do not depend on
1431 * previous instructions. This makes it easier to find instructions that
1435 unsigned Changes = 0;
1437 Changes += RunOptFunc (S, &DOptDecouple, 1);
1439 /* Return the number of changes */
1445 static unsigned RunOptGroup3 (CodeSeg* S)
1446 /* Run one group of optimization steps. These steps depend on each other,
1447 * that means that one step may allow another step to do additional work,
1448 * so we will repeat the steps as long as we see any changes.
1451 unsigned Changes, C;
1457 C += RunOptFunc (S, &DOptNegA1, 1);
1458 C += RunOptFunc (S, &DOptNegA2, 1);
1459 C += RunOptFunc (S, &DOptSub1, 1);
1460 C += RunOptFunc (S, &DOptSub2, 1);
1461 C += RunOptFunc (S, &DOptSub3, 1);
1462 C += RunOptFunc (S, &DOptAdd5, 1);
1463 C += RunOptFunc (S, &DOptAdd6, 1);
1464 C += RunOptFunc (S, &DOptStackOps, 1);
1465 C += RunOptFunc (S, &DOptJumpCascades, 1);
1466 C += RunOptFunc (S, &DOptDeadJumps, 1);
1467 C += RunOptFunc (S, &DOptRTS, 1);
1468 C += RunOptFunc (S, &DOptDeadCode, 1);
1469 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1470 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1471 C += RunOptFunc (S, &DOptCondBranches, 1);
1472 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1473 C += RunOptFunc (S, &DOptBoolTrans, 1);
1474 C += RunOptFunc (S, &DOptCmp1, 1);
1475 C += RunOptFunc (S, &DOptCmp2, 1);
1476 C += RunOptFunc (S, &DOptCmp3, 1);
1477 C += RunOptFunc (S, &DOptCmp4, 1);
1478 C += RunOptFunc (S, &DOptCmp5, 1);
1479 C += RunOptFunc (S, &DOptCmp6, 1);
1480 C += RunOptFunc (S, &DOptCmp7, 1);
1481 C += RunOptFunc (S, &DOptCmp8, 1);
1482 C += RunOptFunc (S, &DOptTest1, 1);
1483 C += RunOptFunc (S, &DOptLoad1, 1);
1484 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1485 C += RunOptFunc (S, &DOptUnusedStores, 1);
1486 C += RunOptFunc (S, &DOptDupLoads, 1);
1487 C += RunOptFunc (S, &DOptStoreLoad, 1);
1488 C += RunOptFunc (S, &DOptTransfers1, 1);
1489 C += RunOptFunc (S, &DOptTransfers3, 1);
1490 C += RunOptFunc (S, &DOptTransfers4, 1);
1491 C += RunOptFunc (S, &DOptStore1, 1);
1492 C += RunOptFunc (S, &DOptStore5, 1);
1493 C += RunOptFunc (S, &DOptPushPop, 1);
1494 C += RunOptFunc (S, &DOptPrecalc, 1);
1500 /* Return the number of changes */
1506 static unsigned RunOptGroup4 (CodeSeg* S)
1507 /* 65C02 specific optimizations. */
1509 unsigned Changes = 0;
1511 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1512 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1513 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1514 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1516 /* The 65C02 replacement codes do often make the use of a register
1517 * value unnecessary, so if we have changes, run another load
1520 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1524 /* Return the number of changes */
1530 static unsigned RunOptGroup5 (CodeSeg* S)
1531 /* Run another round of pattern replacements. These are done late, since there
1532 * may be better replacements before.
1535 unsigned Changes = 0;
1537 Changes += RunOptFunc (S, &DOptPush1, 1);
1538 Changes += RunOptFunc (S, &DOptPush2, 1);
1539 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1540 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1542 /* Return the number of changes */
1548 static unsigned RunOptGroup6 (CodeSeg* S)
1549 /* The last group of optimization steps. Adjust branches, do size optimizations.
1552 unsigned Changes = 0;
1555 /* Optimize for size, that is replace operations by shorter ones, even
1556 * if this does hinder further optimizations (no problem since we're
1559 C = RunOptFunc (S, &DOptSize1, 1);
1562 /* Run some optimization passes again, since the size optimizations
1563 * may have opened new oportunities.
1565 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1566 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1567 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1568 Changes += RunOptFunc (S, &DOptStore5, 1);
1571 C = RunOptFunc (S, &DOptSize2, 1);
1574 /* Run some optimization passes again, since the size optimizations
1575 * may have opened new oportunities.
1577 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1578 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1579 Changes += RunOptFunc (S, &DOptStore5, 1);
1582 /* Adjust branch distances */
1583 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1585 /* Replace conditional branches to RTS. If we had changes, we must run dead
1586 * code elimination again, since the change may have introduced dead code.
1588 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1591 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1594 /* Return the number of changes */
1600 void RunOpt (CodeSeg* S)
1601 /* Run the optimizer */
1603 const char* StatFileName;
1605 /* If we shouldn't run the optimizer, bail out */
1610 /* Check if we are requested to write optimizer statistics */
1611 StatFileName = getenv ("CC65_OPTSTATS");
1613 ReadOptStats (StatFileName);
1616 /* Print the name of the function we are working on */
1618 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1620 Print (stdout, 1, "Running optimizer for global code segment\n");
1623 /* Run groups of optimizations */
1631 /* Write statistics */
1633 WriteOptStats (StatFileName);