1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2003 Ullrich von Bassewitz */
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 /*****************************************************************************/
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.
98 /* Walk over the entries */
100 while (I < CS_GetEntryCount (S)) {
103 CodeEntry* E = CS_GetEntry (S, I);
105 /* Check for the sequence */
106 if (E->OPC == OP65_JSR &&
107 (strncmp (E->Arg, "shlax", 5) == 0 ||
108 strncmp (E->Arg, "aslax", 5) == 0) &&
109 strlen (E->Arg) == 6 &&
110 IsDigit (E->Arg[5]) &&
111 !RegXUsed (S, I+1)) {
113 /* Insert shift insns */
114 unsigned Count = E->Arg[5] - '0';
116 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
117 CS_InsertEntry (S, X, I+1);
120 /* Delete the call to shlax */
123 /* Remember, we had changes */
133 /* Return the number of changes made */
139 static unsigned OptShift2 (CodeSeg* S)
140 /* A call to the shraxN routine may get replaced by one or more lsr insns
141 * if the value of X is zero.
144 unsigned Changes = 0;
147 /* Generate register info */
150 /* Walk over the entries */
152 while (I < CS_GetEntryCount (S)) {
155 CodeEntry* E = CS_GetEntry (S, I);
157 /* Check for the sequence */
158 if (E->OPC == OP65_JSR &&
159 strncmp (E->Arg, "shrax", 5) == 0 &&
160 strlen (E->Arg) == 6 &&
161 IsDigit (E->Arg[5]) &&
162 E->RI->In.RegX == 0) {
164 /* Insert shift insns */
165 unsigned Count = E->Arg[5] - '0';
167 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
168 CS_InsertEntry (S, X, I+1);
171 /* Delete the call to shlax */
174 /* Remember, we had changes */
184 /* Free the register info */
187 /* Return the number of changes made */
193 static unsigned GetShiftType (const char* Sub)
194 /* Helper function for OptShift3 */
197 if (strcmp (Sub+1, "slax1") == 0) {
199 } else if (strcmp (Sub+1, "srax1") == 0) {
202 } else if (*Sub == 's') {
203 if (strcmp (Sub+1, "hlax1") == 0) {
205 } else if (strcmp (Sub+1, "hrax1") == 0) {
214 static unsigned OptShift3 (CodeSeg* S)
215 /* Search for the sequence
219 * jsr aslax1/asrax1/shlax1/shrax1
232 * or similar, provided that a/x is not used later
235 unsigned Changes = 0;
237 /* Walk over the entries */
239 while (I < CS_GetEntryCount (S)) {
245 L[0] = CS_GetEntry (S, I);
247 /* Check for the sequence */
248 if (L[0]->OPC == OP65_LDA &&
249 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
250 CS_GetEntries (S, L+1, I+1, 4) &&
251 !CS_RangeHasLabel (S, I+1, 4) &&
252 L[1]->OPC == OP65_LDX &&
253 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
254 L[2]->OPC == OP65_JSR &&
255 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
256 L[3]->OPC == OP65_STA &&
257 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
258 L[4]->OPC == OP65_STX &&
259 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
260 !RegAXUsed (S, I+5)) {
264 /* Handle the four shift types differently */
268 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
269 CS_InsertEntry (S, X, I+5);
270 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
271 CS_InsertEntry (S, X, I+6);
272 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
273 CS_InsertEntry (S, X, I+7);
274 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
275 CS_InsertEntry (S, X, I+8);
276 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
277 CS_InsertEntry (S, X, I+9);
278 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
279 CS_InsertEntry (S, X, I+10);
280 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
281 CS_InsertEntry (S, X, I+11);
282 CS_DelEntries (S, I, 5);
286 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
287 CS_InsertEntry (S, X, I+5);
288 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
289 CS_InsertEntry (S, X, I+6);
290 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
291 CS_InsertEntry (S, X, I+7);
292 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
293 CS_InsertEntry (S, X, I+8);
294 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
295 CS_InsertEntry (S, X, I+9);
296 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
297 CS_InsertEntry (S, X, I+10);
298 CS_DelEntries (S, I, 5);
303 /* These two are identical */
304 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
305 CS_InsertEntry (S, X, I+1);
306 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
307 CS_InsertEntry (S, X, I+2);
308 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
309 CS_InsertEntry (S, X, I+3);
310 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
311 CS_InsertEntry (S, X, I+4);
312 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
313 CS_InsertEntry (S, X, I+5);
314 CS_DelEntries (S, I+6, 4);
319 /* Remember, we had changes */
329 /* Return the number of changes made */
335 /*****************************************************************************/
337 /*****************************************************************************/
341 static unsigned OptLoad1 (CodeSeg* S)
342 /* Search for a call to ldaxysp where X is not used later and replace it by
343 * a load of just the A register.
347 unsigned Changes = 0;
349 /* Generate register info */
352 /* Walk over the entries */
354 while (I < CS_GetEntryCount (S)) {
359 E = CS_GetEntry (S, I);
361 /* Check for the sequence */
362 if (CE_IsCallTo (E, "ldaxysp") &&
363 RegValIsKnown (E->RI->In.RegY) &&
364 !RegXUsed (S, I+1)) {
368 /* Reload the Y register */
369 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
370 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
371 CS_InsertEntry (S, X, I+1);
373 /* Load from stack */
374 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
375 CS_InsertEntry (S, X, I+2);
377 /* Now remove the call to the subroutine */
380 /* Remember, we had changes */
390 /* Free the register info */
393 /* Return the number of changes made */
399 /*****************************************************************************/
400 /* Optimize stores through pointers */
401 /*****************************************************************************/
405 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
406 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
408 /* Check for a label attached to the entry */
409 if (CE_HasLabel (L[0])) {
413 /* Check for single insn sub ops */
414 if (L[0]->OPC == OP65_AND ||
415 L[0]->OPC == OP65_EOR ||
416 L[0]->OPC == OP65_ORA ||
417 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
418 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
423 } else if (L[0]->OPC == OP65_CLC &&
424 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
425 L[1]->OPC == OP65_ADC &&
426 !CE_HasLabel (L[1])) {
428 } else if (L[0]->OPC == OP65_SEC &&
429 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
430 L[1]->OPC == OP65_SBC &&
431 !CE_HasLabel (L[1])) {
443 static unsigned OptPtrStore1 (CodeSeg* S)
444 /* Search for the sequence:
464 * In case a/x is loaded from the register bank before the pushax, we can even
465 * use the register bank instead of ptr1.
479 unsigned Changes = 0;
481 /* Walk over the entries */
483 while (I < CS_GetEntryCount (S)) {
489 L[0] = CS_GetEntry (S, I);
491 /* Check for the sequence */
492 if (CE_IsCallTo (L[0], "pushax") &&
493 CS_GetEntries (S, L+1, I+1, 3) &&
494 L[1]->OPC == OP65_LDY &&
495 CE_KnownImm (L[1]) &&
496 !CE_HasLabel (L[1]) &&
497 CE_IsCallTo (L[2], "ldauidx") &&
498 !CE_HasLabel (L[2]) &&
499 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
500 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
501 L[3+K]->OPC == OP65_LDY &&
502 CE_KnownImm (L[3+K]) &&
503 !CE_HasLabel (L[3+K]) &&
504 CE_IsCallTo (L[4+K], "staspidx") &&
505 !CE_HasLabel (L[4+K])) {
508 const char* RegBank = 0;
509 const char* ZPLoc = "ptr1";
513 /* Get the preceeding two instructions and check them. We check
520 P[0] = CS_GetEntry (S, I-2);
521 P[1] = CS_GetEntry (S, I-1);
522 if (P[0]->OPC == OP65_LDA &&
523 P[0]->AM == AM65_ZP &&
524 P[1]->OPC == OP65_LDX &&
525 P[1]->AM == AM65_ZP &&
526 !CE_HasLabel (P[1]) &&
527 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
529 unsigned Len = strlen (P[0]->Arg);
531 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
532 P[1]->Arg[Len+0] == '+' &&
533 P[1]->Arg[Len+1] == '1' &&
534 P[1]->Arg[Len+2] == '\0') {
536 /* Ok, found. Use the name of the register bank */
537 RegBank = ZPLoc = P[0]->Arg;
542 /* Insert the load via the zp pointer */
543 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
544 CS_InsertEntry (S, X, I+3);
545 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
546 CS_InsertEntry (S, X, I+4);
548 /* Insert the store through the zp pointer */
549 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
550 CS_InsertEntry (S, X, I+6+K);
552 /* Delete the old code */
553 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
554 CS_DelEntry (S, I+2); /* jsr ldauidx */
556 /* Create and insert the stores into the zp pointer if needed */
558 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
559 CS_InsertEntry (S, X, I+1);
560 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
561 CS_InsertEntry (S, X, I+2);
564 /* Delete more old code. Do it here to keep a label attached to
567 CS_DelEntry (S, I); /* jsr pushax */
569 /* Remember, we had changes */
579 /* Return the number of changes made */
585 static unsigned OptPtrStore2 (CodeSeg* S)
586 /* Search for the sequence:
608 unsigned Changes = 0;
610 /* Walk over the entries */
612 while (I < CS_GetEntryCount (S)) {
618 L[0] = CS_GetEntry (S, I);
620 /* Check for the sequence */
621 if (L[0]->OPC == OP65_LDA &&
622 L[0]->AM == AM65_IMM &&
623 CS_GetEntries (S, L+1, I+1, 10) &&
624 L[1]->OPC == OP65_LDX &&
625 L[1]->AM == AM65_IMM &&
626 L[2]->OPC == OP65_CLC &&
627 L[3]->OPC == OP65_ADC &&
628 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
629 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
631 L[4]->JumpTo->Owner == L[6] &&
632 L[5]->OPC == OP65_INX &&
633 CE_IsCallTo (L[6], "pushax") &&
634 L[7]->OPC == OP65_LDX &&
635 L[8]->OPC == OP65_LDA &&
636 L[9]->OPC == OP65_LDY &&
637 CE_KnownImm (L[9]) &&
639 CE_IsCallTo (L[10], "staspidx") &&
640 !CS_RangeHasLabel (S, I+1, 5) &&
641 !CS_RangeHasLabel (S, I+7, 4) &&
642 /* Check the label last because this is quite costly */
643 (Len = strlen (L[0]->Arg)) > 3 &&
644 L[0]->Arg[0] == '<' &&
645 L[0]->Arg[1] == '(' &&
646 strlen (L[1]->Arg) == Len &&
647 L[1]->Arg[0] == '>' &&
648 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
653 /* We will create all the new stuff behind the current one so
654 * we keep the line references.
656 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
657 CS_InsertEntry (S, X, I+11);
659 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
660 CS_InsertEntry (S, X, I+12);
662 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
663 CS_InsertEntry (S, X, I+13);
665 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
667 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
668 CS_InsertEntry (S, X, I+14);
671 /* Remove the old code */
672 CS_DelEntries (S, I, 11);
674 /* Remember, we had changes */
684 /* Return the number of changes made */
690 /*****************************************************************************/
691 /* Optimize loads through pointers */
692 /*****************************************************************************/
696 static unsigned OptPtrLoad1 (CodeSeg* S)
697 /* Search for the sequence:
722 unsigned Changes = 0;
724 /* Walk over the entries */
726 while (I < CS_GetEntryCount (S)) {
731 L[0] = CS_GetEntry (S, I);
733 /* Check for the sequence */
734 if (L[0]->OPC == OP65_CLC &&
735 CS_GetEntries (S, L+1, I+1, 8) &&
736 L[1]->OPC == OP65_ADC &&
737 L[2]->OPC == OP65_TAY &&
738 L[3]->OPC == OP65_TXA &&
739 L[4]->OPC == OP65_ADC &&
740 L[5]->OPC == OP65_TAX &&
741 L[6]->OPC == OP65_TYA &&
742 L[7]->OPC == OP65_LDY &&
743 CE_IsCallTo (L[8], "ldauidx") &&
744 !CS_RangeHasLabel (S, I+1, 8)) {
749 /* Track the insertion point */
753 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
754 CS_InsertEntry (S, X, IP++);
756 /* If the instruction before the clc is a ldx, replace the
757 * txa by an lda with the same location of the ldx. Otherwise
758 * transfer the value in X to A.
760 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
761 P->OPC == OP65_LDX &&
763 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
765 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
767 CS_InsertEntry (S, X, IP++);
770 X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
771 CS_InsertEntry (S, X, IP++);
774 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
775 CS_InsertEntry (S, X, IP++);
778 X = NewCodeEntry (OP65_LDY, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
779 CS_InsertEntry (S, X, IP++);
782 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
783 CS_InsertEntry (S, X, IP++);
786 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
787 CS_InsertEntry (S, X, IP++);
789 /* Remove the old instructions */
790 CS_DelEntries (S, IP, 7);
792 /* Remember, we had changes */
802 /* Return the number of changes made */
808 static unsigned OptPtrLoad2 (CodeSeg* S)
809 /* Search for the sequence:
834 unsigned Changes = 0;
836 /* Walk over the entries */
838 while (I < CS_GetEntryCount (S)) {
843 L[0] = CS_GetEntry (S, I);
845 /* Check for the sequence */
846 if (L[0]->OPC == OP65_ADC &&
847 CS_GetEntries (S, L+1, I+1, 8) &&
848 L[1]->OPC == OP65_PHA &&
849 L[2]->OPC == OP65_TXA &&
850 L[3]->OPC == OP65_INY &&
851 L[4]->OPC == OP65_ADC &&
852 L[5]->OPC == OP65_TAX &&
853 L[6]->OPC == OP65_PLA &&
854 L[7]->OPC == OP65_LDY &&
855 CE_IsCallTo (L[8], "ldauidx") &&
856 !CS_RangeHasLabel (S, I+1, 8)) {
860 /* Store the low byte and remove the PHA instead */
861 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
862 CS_InsertEntry (S, X, I+1);
864 /* Store the high byte */
865 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
866 CS_InsertEntry (S, X, I+6);
868 /* Load high and low byte */
869 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
870 CS_InsertEntry (S, X, I+10);
871 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
872 CS_InsertEntry (S, X, I+11);
874 /* Delete the old code */
875 CS_DelEntry (S, I+12); /* jsr ldauidx */
876 CS_DelEntry (S, I+8); /* pla */
877 CS_DelEntry (S, I+7); /* tax */
878 CS_DelEntry (S, I+2); /* pha */
880 /* Remember, we had changes */
890 /* Return the number of changes made */
896 static unsigned OptPtrLoad3 (CodeSeg* S)
897 /* Search for the sequence:
915 unsigned Changes = 0;
917 /* Walk over the entries */
919 while (I < CS_GetEntryCount (S)) {
925 L[0] = CS_GetEntry (S, I);
927 /* Check for the sequence */
928 if (L[0]->OPC == OP65_LDA &&
929 L[0]->AM == AM65_IMM &&
930 CS_GetEntries (S, L+1, I+1, 7) &&
931 L[1]->OPC == OP65_LDX &&
932 L[1]->AM == AM65_IMM &&
933 L[2]->OPC == OP65_CLC &&
934 L[3]->OPC == OP65_ADC &&
935 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
936 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
938 L[4]->JumpTo->Owner == L[6] &&
939 L[5]->OPC == OP65_INX &&
940 L[6]->OPC == OP65_LDY &&
941 CE_KnownImm (L[6]) &&
943 CE_IsCallTo (L[7], "ldauidx") &&
944 !CS_RangeHasLabel (S, I+1, 5) &&
945 !CE_HasLabel (L[7]) &&
946 /* Check the label last because this is quite costly */
947 (Len = strlen (L[0]->Arg)) > 3 &&
948 L[0]->Arg[0] == '<' &&
949 L[0]->Arg[1] == '(' &&
950 strlen (L[1]->Arg) == Len &&
951 L[1]->Arg[0] == '>' &&
952 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
957 /* We will create all the new stuff behind the current one so
958 * we keep the line references.
960 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
961 CS_InsertEntry (S, X, I+8);
963 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
964 CS_InsertEntry (S, X, I+9);
966 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
968 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
969 CS_InsertEntry (S, X, I+10);
972 /* Remove the old code */
973 CS_DelEntries (S, I, 8);
975 /* Remember, we had changes */
985 /* Return the number of changes made */
991 static unsigned OptPtrLoad4 (CodeSeg* S)
992 /* Search for the sequence:
1004 * and replace it by:
1013 unsigned Changes = 0;
1015 /* Walk over the entries */
1017 while (I < CS_GetEntryCount (S)) {
1022 /* Get next entry */
1023 L[0] = CS_GetEntry (S, I);
1025 /* Check for the sequence */
1026 if (L[0]->OPC == OP65_LDA &&
1027 L[0]->AM == AM65_IMM &&
1028 CS_GetEntries (S, L+1, I+1, 8) &&
1029 L[1]->OPC == OP65_LDX &&
1030 L[1]->AM == AM65_IMM &&
1031 !CE_HasLabel (L[1]) &&
1032 L[2]->OPC == OP65_LDY &&
1033 CE_KnownImm (L[2]) &&
1034 !CE_HasLabel (L[2]) &&
1035 L[3]->OPC == OP65_CLC &&
1036 !CE_HasLabel (L[3]) &&
1037 L[4]->OPC == OP65_ADC &&
1038 L[4]->AM == AM65_ZP_INDY &&
1039 !CE_HasLabel (L[4]) &&
1040 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
1041 L[5]->JumpTo != 0 &&
1042 L[5]->JumpTo->Owner == L[7] &&
1043 !CE_HasLabel (L[5]) &&
1044 L[6]->OPC == OP65_INX &&
1045 !CE_HasLabel (L[6]) &&
1046 L[7]->OPC == OP65_LDY &&
1047 CE_KnownImm (L[7]) &&
1049 CE_IsCallTo (L[8], "ldauidx") &&
1050 !CE_HasLabel (L[8]) &&
1051 /* Check the label last because this is quite costly */
1052 (Len = strlen (L[0]->Arg)) > 3 &&
1053 L[0]->Arg[0] == '<' &&
1054 L[0]->Arg[1] == '(' &&
1055 strlen (L[1]->Arg) == Len &&
1056 L[1]->Arg[0] == '>' &&
1057 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1063 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1064 CS_InsertEntry (S, X, I+3);
1067 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1068 CS_InsertEntry (S, X, I+4);
1071 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1072 CS_InsertEntry (S, X, I+5);
1075 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1076 Label[Len-3] = '\0';
1077 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1078 CS_InsertEntry (S, X, I+6);
1081 /* Remove the old code */
1082 CS_DelEntries (S, I, 2);
1083 CS_DelEntries (S, I+5, 6);
1085 /* Remember, we had changes */
1095 /* Return the number of changes made */
1101 static unsigned OptPtrLoad5 (CodeSeg* S)
1102 /* Search for the sequence:
1109 * and replace it by:
1116 unsigned Changes = 0;
1118 /* Walk over the entries */
1120 while (I < CS_GetEntryCount (S)) {
1125 /* Get next entry */
1126 L[0] = CS_GetEntry (S, I);
1128 /* Check for the sequence */
1129 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1130 CS_GetEntries (S, L+1, I+1, 3) &&
1131 !CS_RangeHasLabel (S, I+1, 3) &&
1132 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1133 (Len = strlen (L[0]->Arg)) > 0 &&
1134 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1135 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1136 L[2]->OPC == OP65_LDY &&
1137 CE_IsCallTo (L[3], "ldauidx")) {
1142 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1143 CS_InsertEntry (S, X, I+3);
1146 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1147 CS_InsertEntry (S, X, I+4);
1149 /* Remove the old code */
1150 CS_DelEntry (S, I+5);
1151 CS_DelEntries (S, I, 2);
1153 /* Remember, we had changes */
1163 /* Return the number of changes made */
1169 static unsigned OptPtrLoad6 (CodeSeg* S)
1170 /* Search for the sequence:
1177 * and replace it by:
1186 unsigned Changes = 0;
1188 /* Walk over the entries */
1190 while (I < CS_GetEntryCount (S)) {
1195 /* Get next entry */
1196 L[0] = CS_GetEntry (S, I);
1198 /* Check for the sequence */
1199 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1200 CS_GetEntries (S, L+1, I+1, 3) &&
1201 !CS_RangeHasLabel (S, I+1, 3) &&
1202 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1203 (Len = strlen (L[0]->Arg)) > 0 &&
1204 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1205 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1206 L[2]->OPC == OP65_LDY &&
1207 CE_IsCallTo (L[3], "ldaxidx")) {
1212 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1213 CS_InsertEntry (S, X, I+4);
1216 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1217 CS_InsertEntry (S, X, I+5);
1220 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1221 CS_InsertEntry (S, X, I+6);
1224 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1225 CS_InsertEntry (S, X, I+7);
1227 /* Remove the old code */
1228 CS_DelEntry (S, I+3);
1229 CS_DelEntries (S, I, 2);
1231 /* Remember, we had changes */
1241 /* Return the number of changes made */
1247 static unsigned OptPtrLoad7 (CodeSeg* S)
1248 /* Search for the sequence
1253 * and replace it by:
1261 * This step must be execute *after* OptPtrLoad1!
1264 unsigned Changes = 0;
1266 /* Walk over the entries */
1268 while (I < CS_GetEntryCount (S)) {
1272 /* Get next entry */
1273 L[0] = CS_GetEntry (S, I);
1275 /* Check for the sequence */
1276 if (L[0]->OPC == OP65_LDY &&
1277 CS_GetEntries (S, L+1, I+1, 1) &&
1278 CE_IsCallTo (L[1], "ldauidx") &&
1279 !CE_HasLabel (L[1])) {
1283 /* Store the high byte */
1284 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1285 CS_InsertEntry (S, X, I+1);
1287 /* Store the low byte */
1288 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1289 CS_InsertEntry (S, X, I+2);
1291 /* Delete the call to ldauidx */
1292 CS_DelEntry (S, I+3);
1294 /* Load the high and low byte */
1295 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1296 CS_InsertEntry (S, X, I+3);
1297 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1298 CS_InsertEntry (S, X, I+4);
1300 /* Remember, we had changes */
1310 /* Return the number of changes made */
1316 /*****************************************************************************/
1317 /* Decouple operations */
1318 /*****************************************************************************/
1322 static unsigned OptDecouple (CodeSeg* S)
1323 /* Decouple operations, that is, do the following replacements:
1333 * lda zp -> lda #imm
1334 * ldx zp -> ldx #imm
1335 * ldy zp -> ldy #imm
1337 * Provided that the register values are known of course.
1340 unsigned Changes = 0;
1343 /* Generate register info for the following step */
1346 /* Walk over the entries */
1348 while (I < CS_GetEntryCount (S)) {
1352 /* Get next entry and it's input register values */
1353 CodeEntry* E = CS_GetEntry (S, I);
1354 const RegContents* In = &E->RI->In;
1356 /* Assume we have no replacement */
1359 /* Check the instruction */
1363 if (RegValIsKnown (In->RegA)) {
1364 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
1365 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1370 if (RegValIsKnown (In->RegX)) {
1371 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
1372 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1377 if (RegValIsKnown (In->RegY)) {
1378 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
1379 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1384 if (RegValIsKnown (In->RegA)) {
1385 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
1386 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1391 if (RegValIsKnown (In->RegX)) {
1392 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
1393 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1398 if (RegValIsKnown (In->RegY)) {
1399 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
1400 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1405 if (E->AM == AM65_ZP) {
1406 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1408 Arg = MakeHexArg (In->Tmp1);
1409 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1413 Arg = MakeHexArg (In->Ptr1Lo);
1414 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1418 Arg = MakeHexArg (In->Ptr1Hi);
1419 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1423 Arg = MakeHexArg (In->SRegLo);
1424 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1428 Arg = MakeHexArg (In->SRegHi);
1429 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1436 if (E->AM == AM65_ZP) {
1437 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1439 Arg = MakeHexArg (In->Tmp1);
1440 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1444 Arg = MakeHexArg (In->Ptr1Lo);
1445 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1449 Arg = MakeHexArg (In->Ptr1Hi);
1450 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1454 Arg = MakeHexArg (In->SRegLo);
1455 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1459 Arg = MakeHexArg (In->SRegHi);
1460 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1467 if (E->AM == AM65_ZP) {
1468 switch (GetKnownReg (E->Use, In)) {
1470 Arg = MakeHexArg (In->Tmp1);
1471 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1475 Arg = MakeHexArg (In->Ptr1Lo);
1476 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1480 Arg = MakeHexArg (In->Ptr1Hi);
1481 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1485 Arg = MakeHexArg (In->SRegLo);
1486 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1490 Arg = MakeHexArg (In->SRegHi);
1491 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1498 if (E->RI->In.RegA >= 0) {
1499 Arg = MakeHexArg (In->RegA);
1500 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1505 if (E->RI->In.RegA >= 0) {
1506 Arg = MakeHexArg (In->RegA);
1507 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1512 if (E->RI->In.RegX >= 0) {
1513 Arg = MakeHexArg (In->RegX);
1514 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1519 if (E->RI->In.RegY >= 0) {
1520 Arg = MakeHexArg (In->RegY);
1521 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1526 /* Avoid gcc warnings */
1531 /* Insert the replacement if we have one */
1533 CS_InsertEntry (S, X, I+1);
1543 /* Free register info */
1546 /* Return the number of changes made */
1552 /*****************************************************************************/
1553 /* struct OptFunc */
1554 /*****************************************************************************/
1558 typedef struct OptFunc OptFunc;
1560 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1561 const char* Name; /* Name of the function/group */
1562 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1563 unsigned long TotalRuns; /* Total number of runs */
1564 unsigned long LastRuns; /* Last number of runs */
1565 unsigned long TotalChanges; /* Total number of changes */
1566 unsigned long LastChanges; /* Last number of changes */
1567 char Disabled; /* True if function disabled */
1572 /*****************************************************************************/
1574 /*****************************************************************************/
1578 /* A list of all the function descriptions */
1579 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1580 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1581 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1582 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1583 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1584 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 90, 0, 0, 0, 0, 0 };
1585 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 100, 0, 0, 0, 0, 0 };
1586 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 40, 0, 0, 0, 0, 0 };
1587 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1588 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1589 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1590 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1591 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1592 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1593 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1594 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1595 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1596 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1597 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1598 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1599 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1600 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1601 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1602 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1603 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1604 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1605 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1606 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1607 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1608 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1609 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1610 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1611 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1612 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1613 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1614 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1615 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1616 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1617 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1618 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 65, 0, 0, 0, 0, 0 };
1619 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 86, 0, 0, 0, 0, 0 };
1620 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 100, 0, 0, 0, 0, 0 };
1621 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1622 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1623 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1624 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1625 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1626 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1627 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1628 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1629 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1630 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1631 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1632 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1633 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1634 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1635 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1636 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1637 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1638 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1639 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1640 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1641 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1642 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1643 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1646 /* Table containing all the steps in alphabetical order */
1647 static OptFunc* OptFuncs[] = {
1714 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1718 static int CmpOptStep (const void* Key, const void* Func)
1719 /* Compare function for bsearch */
1721 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1726 static OptFunc* FindOptFunc (const char* Name)
1727 /* Find an optimizer step by name in the table and return a pointer. Return
1728 * NULL if no such step is found.
1731 /* Search for the function in the list */
1732 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1738 static OptFunc* GetOptFunc (const char* Name)
1739 /* Find an optimizer step by name in the table and return a pointer. Print an
1740 * error and call AbEnd if not found.
1743 /* Search for the function in the list */
1744 OptFunc* F = FindOptFunc (Name);
1747 AbEnd ("Optimization step `%s' not found", Name);
1754 void DisableOpt (const char* Name)
1755 /* Disable the optimization with the given name */
1757 if (strcmp (Name, "any") == 0) {
1759 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1760 OptFuncs[I]->Disabled = 1;
1763 GetOptFunc(Name)->Disabled = 1;
1769 void EnableOpt (const char* Name)
1770 /* Enable the optimization with the given name */
1772 if (strcmp (Name, "any") == 0) {
1774 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1775 OptFuncs[I]->Disabled = 0;
1778 GetOptFunc(Name)->Disabled = 0;
1784 void ListOptSteps (FILE* F)
1785 /* List all optimization steps */
1788 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1789 fprintf (F, "%s\n", OptFuncs[I]->Name);
1795 static void ReadOptStats (const char* Name)
1796 /* Read the optimizer statistics file */
1801 /* Try to open the file */
1802 FILE* F = fopen (Name, "r");
1804 /* Ignore the error */
1808 /* Read and parse the lines */
1810 while (fgets (Buf, sizeof (Buf), F) != 0) {
1818 unsigned long TotalRuns;
1819 unsigned long TotalChanges;
1824 /* Remove trailing white space including the line terminator */
1827 while (Len > 0 && IsSpace (B[Len-1])) {
1832 /* Remove leading whitespace */
1833 while (IsSpace (*B)) {
1837 /* Check for empty and comment lines */
1838 if (*B == '\0' || *B == ';' || *B == '#') {
1842 /* Parse the line */
1843 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1848 /* Search for the optimizer step. */
1849 Func = FindOptFunc (Name);
1855 /* Found the step, set the fields */
1856 Func->TotalRuns = TotalRuns;
1857 Func->TotalChanges = TotalChanges;
1861 /* Close the file, ignore errors here. */
1867 static void WriteOptStats (const char* Name)
1868 /* Write the optimizer statistics file */
1872 /* Try to open the file */
1873 FILE* F = fopen (Name, "w");
1875 /* Ignore the error */
1879 /* Write a header */
1881 "; Optimizer Total Last Total Last\n"
1882 "; Step Runs Runs Chg Chg\n");
1885 /* Write the data */
1886 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1887 const OptFunc* O = OptFuncs[I];
1889 "%-20s %10lu %10lu %10lu %10lu\n",
1897 /* Close the file, ignore errors here. */
1903 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1904 /* Run one optimizer function Max times or until there are no more changes */
1906 unsigned Changes, C;
1908 /* Don't run the function if it is disabled or if it is prohibited by the
1911 if (F->Disabled || F->CodeSizeFactor > CodeSizeFactor) {
1915 /* Run this until there are no more changes */
1919 /* Run the function */
1926 F->TotalChanges += C;
1927 F->LastChanges += C;
1929 } while (--Max && C > 0);
1931 /* Return the number of changes */
1937 static unsigned RunOptGroup1 (CodeSeg* S)
1938 /* Run the first group of optimization steps. These steps translate known
1939 * patterns emitted by the code generator into more optimal patterns. Order
1940 * of the steps is important, because some of the steps done earlier cover
1941 * the same patterns as later steps as subpatterns.
1944 unsigned Changes = 0;
1946 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1947 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1948 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1949 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1950 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1951 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1952 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1953 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1954 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1955 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1956 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1957 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1958 Changes += RunOptFunc (S, &DOptAdd1, 1);
1959 Changes += RunOptFunc (S, &DOptAdd2, 1);
1960 Changes += RunOptFunc (S, &DOptAdd3, 1);
1961 Changes += RunOptFunc (S, &DOptStore4, 1);
1962 Changes += RunOptFunc (S, &DOptShift1, 1);
1963 Changes += RunOptFunc (S, &DOptShift2, 1);
1964 Changes += RunOptFunc (S, &DOptShift3, 1);
1965 Changes += RunOptFunc (S, &DOptStore1, 1);
1966 Changes += RunOptFunc (S, &DOptStore2, 5);
1967 Changes += RunOptFunc (S, &DOptStore3, 5);
1969 /* Return the number of changes */
1975 static unsigned RunOptGroup2 (CodeSeg* S)
1976 /* Run one group of optimization steps. This step involves just decoupling
1977 * instructions by replacing them by instructions that do not depend on
1978 * previous instructions. This makes it easier to find instructions that
1982 unsigned Changes = 0;
1984 Changes += RunOptFunc (S, &DOptDecouple, 1);
1986 /* Return the number of changes */
1992 static unsigned RunOptGroup3 (CodeSeg* S)
1993 /* Run one group of optimization steps. These steps depend on each other,
1994 * that means that one step may allow another step to do additional work,
1995 * so we will repeat the steps as long as we see any changes.
1998 unsigned Changes, C;
2004 C += RunOptFunc (S, &DOptPtrLoad7, 1);
2005 C += RunOptFunc (S, &DOptNegA1, 1);
2006 C += RunOptFunc (S, &DOptNegA2, 1);
2007 C += RunOptFunc (S, &DOptSub1, 1);
2008 C += RunOptFunc (S, &DOptSub2, 1);
2009 C += RunOptFunc (S, &DOptAdd4, 1);
2010 C += RunOptFunc (S, &DOptAdd5, 1);
2011 C += RunOptFunc (S, &DOptStackOps, 1);
2012 C += RunOptFunc (S, &DOptJumpCascades, 1);
2013 C += RunOptFunc (S, &DOptDeadJumps, 1);
2014 C += RunOptFunc (S, &DOptRTS, 1);
2015 C += RunOptFunc (S, &DOptDeadCode, 1);
2016 C += RunOptFunc (S, &DOptJumpTarget, 1);
2017 C += RunOptFunc (S, &DOptCondBranches, 1);
2018 C += RunOptFunc (S, &DOptRTSJumps1, 1);
2019 C += RunOptFunc (S, &DOptBoolTrans, 1);
2020 C += RunOptFunc (S, &DOptCmp1, 1);
2021 C += RunOptFunc (S, &DOptCmp2, 1);
2022 C += RunOptFunc (S, &DOptCmp3, 1);
2023 C += RunOptFunc (S, &DOptCmp4, 1);
2024 C += RunOptFunc (S, &DOptCmp5, 1);
2025 C += RunOptFunc (S, &DOptCmp6, 1);
2026 C += RunOptFunc (S, &DOptCmp7, 1);
2027 C += RunOptFunc (S, &DOptTest1, 1);
2028 C += RunOptFunc (S, &DOptLoad1, 1);
2029 C += RunOptFunc (S, &DOptUnusedLoads, 1);
2030 C += RunOptFunc (S, &DOptUnusedStores, 1);
2031 C += RunOptFunc (S, &DOptDupLoads, 1);
2032 C += RunOptFunc (S, &DOptStoreLoad, 1);
2033 C += RunOptFunc (S, &DOptTransfers1, 1);
2034 C += RunOptFunc (S, &DOptPushPop, 1);
2035 C += RunOptFunc (S, &DOptPrecalc, 1);
2041 /* Return the number of changes */
2047 static unsigned RunOptGroup4 (CodeSeg* S)
2048 /* 65C02 specific optimizations. */
2050 unsigned Changes = 0;
2052 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
2053 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
2054 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
2055 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
2057 /* The 65C02 replacement codes do often make the use of a register
2058 * value unnecessary, so if we have changes, run another load
2061 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2065 /* Return the number of changes */
2071 static unsigned RunOptGroup5 (CodeSeg* S)
2072 /* Run another round of pattern replacements. These are done late, since there
2073 * may be better replacements before.
2076 unsigned Changes = 0;
2078 Changes += RunOptFunc (S, &DOptPush1, 1);
2079 Changes += RunOptFunc (S, &DOptPush2, 1);
2080 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2081 Changes += RunOptFunc (S, &DOptTransfers2, 1);
2083 /* Return the number of changes */
2089 static unsigned RunOptGroup6 (CodeSeg* S)
2090 /* The last group of optimization steps. Adjust branches, do size optimizations.
2093 unsigned Changes = 0;
2096 if (CodeSizeFactor <= 100) {
2097 /* Optimize for size, that is replace operations by shorter ones, even
2098 * if this does hinder further optimizations (no problem since we're
2101 C = RunOptFunc (S, &DOptSize1, 1);
2104 /* Run some optimization passes again, since the size optimizations
2105 * may have opened new oportunities.
2107 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2108 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
2111 C = RunOptFunc (S, &DOptSize2, 1);
2114 /* Run some optimization passes again, since the size optimizations
2115 * may have opened new oportunities.
2117 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2118 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
2121 /* Adjust branch distances */
2122 Changes += RunOptFunc (S, &DOptBranchDist, 3);
2124 /* Replace conditional branches to RTS. If we had changes, we must run dead
2125 * code elimination again, since the change may have introduced dead code.
2127 C = RunOptFunc (S, &DOptRTSJumps2, 1);
2130 Changes += RunOptFunc (S, &DOptDeadCode, 1);
2133 /* Return the number of changes */
2139 void RunOpt (CodeSeg* S)
2140 /* Run the optimizer */
2142 const char* StatFileName;
2144 /* If we shouldn't run the optimizer, bail out */
2149 /* Check if we are requested to write optimizer statistics */
2150 StatFileName = getenv ("CC65_OPTSTATS");
2152 ReadOptStats (StatFileName);
2155 /* Print the name of the function we are working on */
2157 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2159 Print (stdout, 1, "Running optimizer for global code segment\n");
2162 /* Run groups of optimizations */
2170 /* Write statistics */
2172 WriteOptStats (StatFileName);