1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2006 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 /*****************************************************************************/
58 #include "coptstore.h"
67 /*****************************************************************************/
69 /*****************************************************************************/
84 /*****************************************************************************/
86 /*****************************************************************************/
90 static unsigned OptShift1 (CodeSeg* S)
91 /* A call to the shlaxN routine may get replaced by one or more asl insns
92 * if the value of X is not used later.
97 /* Walk over the entries */
99 while (I < CS_GetEntryCount (S)) {
102 CodeEntry* E = CS_GetEntry (S, I);
104 /* Check for the sequence */
105 if (E->OPC == OP65_JSR &&
106 (strncmp (E->Arg, "shlax", 5) == 0 ||
107 strncmp (E->Arg, "aslax", 5) == 0) &&
108 strlen (E->Arg) == 6 &&
109 IsDigit (E->Arg[5]) &&
110 !RegXUsed (S, I+1)) {
112 /* Insert shift insns */
113 unsigned Count = E->Arg[5] - '0';
115 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
116 CS_InsertEntry (S, X, I+1);
119 /* Delete the call to shlax */
122 /* Remember, we had changes */
132 /* Return the number of changes made */
138 static unsigned OptShift2 (CodeSeg* S)
139 /* A call to the shraxN routine may get replaced by one or more lsr insns
140 * if the value of X is zero.
143 unsigned Changes = 0;
146 /* Generate register info */
149 /* Walk over the entries */
151 while (I < CS_GetEntryCount (S)) {
154 CodeEntry* E = CS_GetEntry (S, I);
156 /* Check for the sequence */
157 if (E->OPC == OP65_JSR &&
158 strncmp (E->Arg, "shrax", 5) == 0 &&
159 strlen (E->Arg) == 6 &&
160 IsDigit (E->Arg[5]) &&
161 E->RI->In.RegX == 0) {
163 /* Insert shift insns */
164 unsigned Count = E->Arg[5] - '0';
166 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
167 CS_InsertEntry (S, X, I+1);
170 /* Delete the call to shlax */
173 /* Remember, we had changes */
183 /* Free the register info */
186 /* Return the number of changes made */
192 static unsigned GetShiftType (const char* Sub)
193 /* Helper function for OptShift3 */
196 if (strcmp (Sub+1, "slax1") == 0) {
198 } else if (strcmp (Sub+1, "srax1") == 0) {
201 } else if (*Sub == 's') {
202 if (strcmp (Sub+1, "hlax1") == 0) {
204 } else if (strcmp (Sub+1, "hrax1") == 0) {
213 static unsigned OptShift3 (CodeSeg* S)
214 /* Search for the sequence
218 * jsr aslax1/asrax1/shlax1/shrax1
231 * or similar, provided that a/x is not used later
234 unsigned Changes = 0;
236 /* Walk over the entries */
238 while (I < CS_GetEntryCount (S)) {
244 L[0] = CS_GetEntry (S, I);
246 /* Check for the sequence */
247 if (L[0]->OPC == OP65_LDA &&
248 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
249 CS_GetEntries (S, L+1, I+1, 4) &&
250 !CS_RangeHasLabel (S, I+1, 4) &&
251 L[1]->OPC == OP65_LDX &&
252 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
253 L[2]->OPC == OP65_JSR &&
254 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
255 L[3]->OPC == OP65_STA &&
256 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
257 L[4]->OPC == OP65_STX &&
258 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
259 !RegAXUsed (S, I+5)) {
263 /* Handle the four shift types differently */
267 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
268 CS_InsertEntry (S, X, I+5);
269 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
270 CS_InsertEntry (S, X, I+6);
271 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
272 CS_InsertEntry (S, X, I+7);
273 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
274 CS_InsertEntry (S, X, I+8);
275 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
276 CS_InsertEntry (S, X, I+9);
277 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
278 CS_InsertEntry (S, X, I+10);
279 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
280 CS_InsertEntry (S, X, I+11);
281 CS_DelEntries (S, I, 5);
285 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
286 CS_InsertEntry (S, X, I+5);
287 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
288 CS_InsertEntry (S, X, I+6);
289 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
290 CS_InsertEntry (S, X, I+7);
291 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
292 CS_InsertEntry (S, X, I+8);
293 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
294 CS_InsertEntry (S, X, I+9);
295 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
296 CS_InsertEntry (S, X, I+10);
297 CS_DelEntries (S, I, 5);
302 /* These two are identical */
303 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
304 CS_InsertEntry (S, X, I+1);
305 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
306 CS_InsertEntry (S, X, I+2);
307 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
308 CS_InsertEntry (S, X, I+3);
309 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
310 CS_InsertEntry (S, X, I+4);
311 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
312 CS_InsertEntry (S, X, I+5);
313 CS_DelEntries (S, I+6, 4);
318 /* Remember, we had changes */
328 /* Return the number of changes made */
334 /*****************************************************************************/
336 /*****************************************************************************/
340 static unsigned OptLoad1 (CodeSeg* S)
341 /* Search for a call to ldaxysp where X is not used later and replace it by
342 * a load of just the A register.
346 unsigned Changes = 0;
348 /* Generate register info */
351 /* Walk over the entries */
353 while (I < CS_GetEntryCount (S)) {
358 E = CS_GetEntry (S, I);
360 /* Check for the sequence */
361 if (CE_IsCallTo (E, "ldaxysp") &&
362 RegValIsKnown (E->RI->In.RegY) &&
363 !RegXUsed (S, I+1)) {
367 /* Reload the Y register */
368 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
369 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
370 CS_InsertEntry (S, X, I+1);
372 /* Load from stack */
373 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
374 CS_InsertEntry (S, X, I+2);
376 /* Now remove the call to the subroutine */
379 /* Remember, we had changes */
389 /* Free the register info */
392 /* Return the number of changes made */
398 /*****************************************************************************/
399 /* Optimize stores through pointers */
400 /*****************************************************************************/
404 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
405 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
407 /* Check for a label attached to the entry */
408 if (CE_HasLabel (L[0])) {
412 /* Check for single insn sub ops */
413 if (L[0]->OPC == OP65_AND ||
414 L[0]->OPC == OP65_EOR ||
415 L[0]->OPC == OP65_ORA ||
416 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
417 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
422 } else if (L[0]->OPC == OP65_CLC &&
423 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
424 L[1]->OPC == OP65_ADC &&
425 !CE_HasLabel (L[1])) {
427 } else if (L[0]->OPC == OP65_SEC &&
428 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
429 L[1]->OPC == OP65_SBC &&
430 !CE_HasLabel (L[1])) {
442 static unsigned OptPtrStore1 (CodeSeg* S)
443 /* Search for the sequence:
463 * In case a/x is loaded from the register bank before the pushax, we can even
464 * use the register bank instead of ptr1.
478 unsigned Changes = 0;
480 /* Walk over the entries */
482 while (I < CS_GetEntryCount (S)) {
488 L[0] = CS_GetEntry (S, I);
490 /* Check for the sequence */
491 if (CE_IsCallTo (L[0], "pushax") &&
492 CS_GetEntries (S, L+1, I+1, 3) &&
493 L[1]->OPC == OP65_LDY &&
494 CE_IsConstImm (L[1]) &&
495 !CE_HasLabel (L[1]) &&
496 CE_IsCallTo (L[2], "ldauidx") &&
497 !CE_HasLabel (L[2]) &&
498 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
499 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
500 L[3+K]->OPC == OP65_LDY &&
501 CE_IsConstImm (L[3+K]) &&
502 !CE_HasLabel (L[3+K]) &&
503 CE_IsCallTo (L[4+K], "staspidx") &&
504 !CE_HasLabel (L[4+K])) {
507 const char* RegBank = 0;
508 const char* ZPLoc = "ptr1";
512 /* Get the preceeding two instructions and check them. We check
519 P[0] = CS_GetEntry (S, I-2);
520 P[1] = CS_GetEntry (S, I-1);
521 if (P[0]->OPC == OP65_LDA &&
522 P[0]->AM == AM65_ZP &&
523 P[1]->OPC == OP65_LDX &&
524 P[1]->AM == AM65_ZP &&
525 !CE_HasLabel (P[1]) &&
526 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
528 unsigned Len = strlen (P[0]->Arg);
530 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
531 P[1]->Arg[Len+0] == '+' &&
532 P[1]->Arg[Len+1] == '1' &&
533 P[1]->Arg[Len+2] == '\0') {
535 /* Ok, found. Use the name of the register bank */
536 RegBank = ZPLoc = P[0]->Arg;
541 /* Insert the load via the zp pointer */
542 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
543 CS_InsertEntry (S, X, I+3);
544 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
545 CS_InsertEntry (S, X, I+4);
547 /* Insert the store through the zp pointer */
548 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
549 CS_InsertEntry (S, X, I+6+K);
551 /* Delete the old code */
552 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
553 CS_DelEntry (S, I+2); /* jsr ldauidx */
555 /* Create and insert the stores into the zp pointer if needed */
557 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
558 CS_InsertEntry (S, X, I+1);
559 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
560 CS_InsertEntry (S, X, I+2);
563 /* Delete more old code. Do it here to keep a label attached to
566 CS_DelEntry (S, I); /* jsr pushax */
568 /* Remember, we had changes */
578 /* Return the number of changes made */
584 static unsigned OptPtrStore2 (CodeSeg* S)
585 /* Search for the sequence:
607 unsigned Changes = 0;
609 /* Walk over the entries */
611 while (I < CS_GetEntryCount (S)) {
617 L[0] = CS_GetEntry (S, I);
619 /* Check for the sequence */
620 if (L[0]->OPC == OP65_LDA &&
621 L[0]->AM == AM65_IMM &&
622 CS_GetEntries (S, L+1, I+1, 10) &&
623 L[1]->OPC == OP65_LDX &&
624 L[1]->AM == AM65_IMM &&
625 L[2]->OPC == OP65_CLC &&
626 L[3]->OPC == OP65_ADC &&
627 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
628 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
630 L[4]->JumpTo->Owner == L[6] &&
631 L[5]->OPC == OP65_INX &&
632 CE_IsCallTo (L[6], "pushax") &&
633 L[7]->OPC == OP65_LDX &&
634 L[8]->OPC == OP65_LDA &&
635 L[9]->OPC == OP65_LDY &&
636 CE_IsKnownImm (L[9], 0) &&
637 CE_IsCallTo (L[10], "staspidx") &&
638 !CS_RangeHasLabel (S, I+1, 5) &&
639 !CS_RangeHasLabel (S, I+7, 4) &&
640 /* Check the label last because this is quite costly */
641 (Len = strlen (L[0]->Arg)) > 3 &&
642 L[0]->Arg[0] == '<' &&
643 L[0]->Arg[1] == '(' &&
644 strlen (L[1]->Arg) == Len &&
645 L[1]->Arg[0] == '>' &&
646 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
651 /* We will create all the new stuff behind the current one so
652 * we keep the line references.
654 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
655 CS_InsertEntry (S, X, I+11);
657 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
658 CS_InsertEntry (S, X, I+12);
660 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
661 CS_InsertEntry (S, X, I+13);
663 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
665 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
666 CS_InsertEntry (S, X, I+14);
669 /* Remove the old code */
670 CS_DelEntries (S, I, 11);
672 /* Remember, we had changes */
682 /* Return the number of changes made */
688 /*****************************************************************************/
689 /* Optimize loads through pointers */
690 /*****************************************************************************/
694 static unsigned OptPtrLoad1 (CodeSeg* S)
695 /* Search for the sequence:
720 unsigned Changes = 0;
722 /* Walk over the entries */
724 while (I < CS_GetEntryCount (S)) {
729 L[0] = CS_GetEntry (S, I);
731 /* Check for the sequence */
732 if (L[0]->OPC == OP65_CLC &&
733 CS_GetEntries (S, L+1, I+1, 8) &&
734 L[1]->OPC == OP65_ADC &&
735 L[2]->OPC == OP65_TAY &&
736 L[3]->OPC == OP65_TXA &&
737 L[4]->OPC == OP65_ADC &&
738 L[5]->OPC == OP65_TAX &&
739 L[6]->OPC == OP65_TYA &&
740 L[7]->OPC == OP65_LDY &&
741 CE_IsCallTo (L[8], "ldauidx") &&
742 !CS_RangeHasLabel (S, I+1, 8)) {
747 /* Track the insertion point */
751 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
752 CS_InsertEntry (S, X, IP++);
754 /* If the instruction before the clc is a ldx, replace the
755 * txa by an lda with the same location of the ldx. Otherwise
756 * transfer the value in X to A.
758 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
759 P->OPC == OP65_LDX &&
761 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
763 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
765 CS_InsertEntry (S, X, IP++);
768 X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
769 CS_InsertEntry (S, X, IP++);
772 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
773 CS_InsertEntry (S, X, IP++);
776 X = NewCodeEntry (OP65_LDY, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
777 CS_InsertEntry (S, X, IP++);
780 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
781 CS_InsertEntry (S, X, IP++);
784 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
785 CS_InsertEntry (S, X, IP++);
787 /* Remove the old instructions */
788 CS_DelEntries (S, IP, 7);
790 /* Remember, we had changes */
800 /* Return the number of changes made */
806 static unsigned OptPtrLoad2 (CodeSeg* S)
807 /* Search for the sequence:
832 unsigned Changes = 0;
834 /* Walk over the entries */
836 while (I < CS_GetEntryCount (S)) {
841 L[0] = CS_GetEntry (S, I);
843 /* Check for the sequence */
844 if (L[0]->OPC == OP65_ADC &&
845 CS_GetEntries (S, L+1, I+1, 8) &&
846 L[1]->OPC == OP65_PHA &&
847 L[2]->OPC == OP65_TXA &&
848 L[3]->OPC == OP65_INY &&
849 L[4]->OPC == OP65_ADC &&
850 L[5]->OPC == OP65_TAX &&
851 L[6]->OPC == OP65_PLA &&
852 L[7]->OPC == OP65_LDY &&
853 CE_IsCallTo (L[8], "ldauidx") &&
854 !CS_RangeHasLabel (S, I+1, 8)) {
858 /* Store the low byte and remove the PHA instead */
859 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
860 CS_InsertEntry (S, X, I+1);
862 /* Store the high byte */
863 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
864 CS_InsertEntry (S, X, I+6);
866 /* Load high and low byte */
867 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
868 CS_InsertEntry (S, X, I+10);
869 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
870 CS_InsertEntry (S, X, I+11);
872 /* Delete the old code */
873 CS_DelEntry (S, I+12); /* jsr ldauidx */
874 CS_DelEntry (S, I+8); /* pla */
875 CS_DelEntry (S, I+7); /* tax */
876 CS_DelEntry (S, I+2); /* pha */
878 /* Remember, we had changes */
888 /* Return the number of changes made */
894 static unsigned OptPtrLoad3 (CodeSeg* S)
895 /* Search for the sequence:
913 unsigned Changes = 0;
915 /* Walk over the entries */
917 while (I < CS_GetEntryCount (S)) {
923 L[0] = CS_GetEntry (S, I);
925 /* Check for the sequence */
926 if (L[0]->OPC == OP65_LDA &&
927 L[0]->AM == AM65_IMM &&
928 CS_GetEntries (S, L+1, I+1, 7) &&
929 L[1]->OPC == OP65_LDX &&
930 L[1]->AM == AM65_IMM &&
931 L[2]->OPC == OP65_CLC &&
932 L[3]->OPC == OP65_ADC &&
933 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
934 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
936 L[4]->JumpTo->Owner == L[6] &&
937 L[5]->OPC == OP65_INX &&
938 L[6]->OPC == OP65_LDY &&
939 CE_IsKnownImm (L[6], 0) &&
940 CE_IsCallTo (L[7], "ldauidx") &&
941 !CS_RangeHasLabel (S, I+1, 5) &&
942 !CE_HasLabel (L[7]) &&
943 /* Check the label last because this is quite costly */
944 (Len = strlen (L[0]->Arg)) > 3 &&
945 L[0]->Arg[0] == '<' &&
946 L[0]->Arg[1] == '(' &&
947 strlen (L[1]->Arg) == Len &&
948 L[1]->Arg[0] == '>' &&
949 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
954 /* We will create all the new stuff behind the current one so
955 * we keep the line references.
957 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
958 CS_InsertEntry (S, X, I+8);
960 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
961 CS_InsertEntry (S, X, I+9);
963 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
965 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
966 CS_InsertEntry (S, X, I+10);
969 /* Remove the old code */
970 CS_DelEntries (S, I, 8);
972 /* Remember, we had changes */
982 /* Return the number of changes made */
988 static unsigned OptPtrLoad4 (CodeSeg* S)
989 /* Search for the sequence:
1001 * and replace it by:
1010 unsigned Changes = 0;
1012 /* Walk over the entries */
1014 while (I < CS_GetEntryCount (S)) {
1019 /* Get next entry */
1020 L[0] = CS_GetEntry (S, I);
1022 /* Check for the sequence */
1023 if (L[0]->OPC == OP65_LDA &&
1024 L[0]->AM == AM65_IMM &&
1025 CS_GetEntries (S, L+1, I+1, 8) &&
1026 L[1]->OPC == OP65_LDX &&
1027 L[1]->AM == AM65_IMM &&
1028 !CE_HasLabel (L[1]) &&
1029 L[2]->OPC == OP65_LDY &&
1030 CE_IsConstImm (L[2]) &&
1031 !CE_HasLabel (L[2]) &&
1032 L[3]->OPC == OP65_CLC &&
1033 !CE_HasLabel (L[3]) &&
1034 L[4]->OPC == OP65_ADC &&
1035 L[4]->AM == AM65_ZP_INDY &&
1036 !CE_HasLabel (L[4]) &&
1037 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
1038 L[5]->JumpTo != 0 &&
1039 L[5]->JumpTo->Owner == L[7] &&
1040 !CE_HasLabel (L[5]) &&
1041 L[6]->OPC == OP65_INX &&
1042 !CE_HasLabel (L[6]) &&
1043 L[7]->OPC == OP65_LDY &&
1044 CE_IsKnownImm (L[7], 0) &&
1045 CE_IsCallTo (L[8], "ldauidx") &&
1046 !CE_HasLabel (L[8]) &&
1047 /* Check the label last because this is quite costly */
1048 (Len = strlen (L[0]->Arg)) > 3 &&
1049 L[0]->Arg[0] == '<' &&
1050 L[0]->Arg[1] == '(' &&
1051 strlen (L[1]->Arg) == Len &&
1052 L[1]->Arg[0] == '>' &&
1053 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1059 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1060 CS_InsertEntry (S, X, I+3);
1063 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1064 CS_InsertEntry (S, X, I+4);
1067 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1068 CS_InsertEntry (S, X, I+5);
1071 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1072 Label[Len-3] = '\0';
1073 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1074 CS_InsertEntry (S, X, I+6);
1077 /* Remove the old code */
1078 CS_DelEntries (S, I, 2);
1079 CS_DelEntries (S, I+5, 6);
1081 /* Remember, we had changes */
1091 /* Return the number of changes made */
1097 static unsigned OptPtrLoad5 (CodeSeg* S)
1098 /* Search for the sequence:
1107 * and replace it by:
1116 unsigned Changes = 0;
1118 /* Walk over the entries */
1120 while (I < CS_GetEntryCount (S)) {
1124 /* Get next entry */
1125 L[0] = CS_GetEntry (S, I);
1127 /* Check for the sequence */
1128 if (L[0]->OPC == OP65_CLC &&
1129 CS_GetEntries (S, L+1, I+1, 5) &&
1130 L[1]->OPC == OP65_ADC &&
1131 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP || L[1]->AM == AM65_IMM) &&
1132 (L[2]->OPC == OP65_BCC || L[2]->OPC == OP65_JCC) &&
1133 L[2]->JumpTo != 0 &&
1134 L[2]->JumpTo->Owner == L[4] &&
1135 L[3]->OPC == OP65_INX &&
1136 L[4]->OPC == OP65_LDY &&
1137 CE_IsKnownImm (L[4], 0) &&
1138 CE_IsCallTo (L[5], "ldauidx") &&
1139 !CS_RangeHasLabel (S, I+1, 3) &&
1140 !CE_HasLabel (L[5])) {
1144 /* We will create all the new stuff behind the current one so
1145 * we keep the line references.
1147 X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[0]->LI);
1148 CS_InsertEntry (S, X, I+6);
1151 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1152 CS_InsertEntry (S, X, I+7);
1155 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1156 CS_InsertEntry (S, X, I+8);
1159 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1160 CS_InsertEntry (S, X, I+9);
1162 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1163 CS_InsertEntry (S, X, I+10);
1165 /* Remove the old code */
1166 CS_DelEntries (S, I, 6);
1168 /* Remember, we had changes */
1178 /* Return the number of changes made */
1184 static unsigned OptPtrLoad6 (CodeSeg* S)
1185 /* Search for the sequence:
1202 * and replace it by:
1210 * L1: tay <- only if flags are used
1212 * This function must execute before OptPtrLoad7!
1216 unsigned Changes = 0;
1218 /* Walk over the entries */
1220 while (I < CS_GetEntryCount (S)) {
1225 /* Get next entry */
1226 L[0] = CS_GetEntry (S, I);
1228 /* Check for the sequence */
1229 if (L[0]->OPC == OP65_LDA &&
1230 L[0]->AM == AM65_ZP &&
1231 strncmp (L[0]->Arg, "regbank+", 8) == 0 &&
1232 (Len = strlen (L[0]->Arg)) > 0 &&
1233 CS_GetEntries (S, L+1, I+1, 14) &&
1234 !CS_RangeHasLabel (S, I+1, 7) &&
1235 !CS_RangeHasLabel (S, I+9, 5) &&
1236 L[1]->OPC == OP65_LDX &&
1237 L[1]->AM == AM65_ZP &&
1238 strncmp (L[1]->Arg, L[0]->Arg, Len) == 0 &&
1239 strcmp (L[1]->Arg+Len, "+1") == 0 &&
1240 L[2]->OPC == OP65_STA &&
1241 L[2]->AM == AM65_ZP &&
1242 strcmp (L[2]->Arg, "regsave") == 0 &&
1243 L[3]->OPC == OP65_STX &&
1244 L[3]->AM == AM65_ZP &&
1245 strcmp (L[3]->Arg, "regsave+1") == 0 &&
1246 L[4]->OPC == OP65_CLC &&
1247 L[5]->OPC == OP65_ADC &&
1248 CE_IsKnownImm (L[5], 1) &&
1249 L[6]->OPC == OP65_BCC &&
1250 L[6]->JumpTo != 0 &&
1251 L[6]->JumpTo->Owner == L[8] &&
1252 L[7]->OPC == OP65_INX &&
1253 L[8]->OPC == OP65_STA &&
1254 L[8]->AM == AM65_ZP &&
1255 strcmp (L[8]->Arg, L[0]->Arg) == 0 &&
1256 L[9]->OPC == OP65_STX &&
1257 L[9]->AM == AM65_ZP &&
1258 strcmp (L[9]->Arg, L[1]->Arg) == 0 &&
1259 L[10]->OPC == OP65_LDA &&
1260 L[10]->AM == AM65_ZP &&
1261 strcmp (L[10]->Arg, "regsave") == 0 &&
1262 L[11]->OPC == OP65_LDX &&
1263 L[11]->AM == AM65_ZP &&
1264 strcmp (L[11]->Arg, "regsave+1") == 0 &&
1265 L[12]->OPC == OP65_LDY &&
1266 CE_IsConstImm (L[12]) &&
1267 CE_IsCallTo (L[13], "ldauidx")) {
1272 /* Check if the instruction following the sequence uses the flags
1273 * set by the load. If so, insert a test of the value in the
1276 if (CE_UseLoadFlags (L[14])) {
1277 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[13]->LI);
1278 CS_InsertEntry (S, X, I+14);
1281 /* Attach a label to L[14]. This may be either the just inserted
1282 * instruction, or the one following the sequence.
1284 Label = CS_GenLabel (S, L[14]);
1287 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[12]->Arg, 0, L[12]->LI);
1288 CS_InsertEntry (S, X, I+14);
1291 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[13]->LI);
1292 CS_InsertEntry (S, X, I+15);
1294 /* lda (regbank+n),y */
1295 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[13]->LI);
1296 CS_InsertEntry (S, X, I+16);
1299 X = NewCodeEntry (OP65_INC, AM65_ZP, L[0]->Arg, 0, L[5]->LI);
1300 CS_InsertEntry (S, X, I+17);
1303 X = NewCodeEntry (OP65_BNE, AM65_BRA, Label->Name, Label, L[6]->LI);
1304 CS_InsertEntry (S, X, I+18);
1306 /* inc regbank+n+1 */
1307 X = NewCodeEntry (OP65_INC, AM65_ZP, L[1]->Arg, 0, L[7]->LI);
1308 CS_InsertEntry (S, X, I+19);
1310 /* Delete the old code */
1311 CS_DelEntries (S, I, 14);
1313 /* Remember, we had changes */
1323 /* Return the number of changes made */
1329 static unsigned OptPtrLoad7 (CodeSeg* S)
1330 /* Search for the sequence:
1337 * and replace it by:
1344 unsigned Changes = 0;
1346 /* Walk over the entries */
1348 while (I < CS_GetEntryCount (S)) {
1353 /* Get next entry */
1354 L[0] = CS_GetEntry (S, I);
1356 /* Check for the sequence */
1357 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1358 CS_GetEntries (S, L+1, I+1, 3) &&
1359 !CS_RangeHasLabel (S, I+1, 3) &&
1360 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1361 (Len = strlen (L[0]->Arg)) > 0 &&
1362 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1363 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1364 L[2]->OPC == OP65_LDY &&
1365 CE_IsCallTo (L[3], "ldauidx")) {
1370 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1371 CS_InsertEntry (S, X, I+3);
1374 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1375 CS_InsertEntry (S, X, I+4);
1377 /* Remove the old code */
1378 CS_DelEntry (S, I+5);
1379 CS_DelEntries (S, I, 2);
1381 /* Remember, we had changes */
1391 /* Return the number of changes made */
1397 static unsigned OptPtrLoad8 (CodeSeg* S)
1398 /* Search for the sequence:
1405 * and replace it by:
1414 unsigned Changes = 0;
1416 /* Walk over the entries */
1418 while (I < CS_GetEntryCount (S)) {
1423 /* Get next entry */
1424 L[0] = CS_GetEntry (S, I);
1426 /* Check for the sequence */
1427 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1428 CS_GetEntries (S, L+1, I+1, 3) &&
1429 !CS_RangeHasLabel (S, I+1, 3) &&
1430 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1431 (Len = strlen (L[0]->Arg)) > 0 &&
1432 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1433 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1434 L[2]->OPC == OP65_LDY &&
1435 CE_IsCallTo (L[3], "ldaxidx")) {
1440 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1441 CS_InsertEntry (S, X, I+4);
1444 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1445 CS_InsertEntry (S, X, I+5);
1448 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1449 CS_InsertEntry (S, X, I+6);
1452 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1453 CS_InsertEntry (S, X, I+7);
1455 /* Remove the old code */
1456 CS_DelEntry (S, I+3);
1457 CS_DelEntries (S, I, 2);
1459 /* Remember, we had changes */
1469 /* Return the number of changes made */
1475 static unsigned OptPtrLoad9 (CodeSeg* S)
1476 /* Search for the sequence
1481 * and replace it by:
1489 * This step must be executed *after* OptPtrLoad1!
1492 unsigned Changes = 0;
1494 /* Walk over the entries */
1496 while (I < CS_GetEntryCount (S)) {
1500 /* Get next entry */
1501 L[0] = CS_GetEntry (S, I);
1503 /* Check for the sequence */
1504 if (L[0]->OPC == OP65_LDY &&
1505 CS_GetEntries (S, L+1, I+1, 1) &&
1506 CE_IsCallTo (L[1], "ldauidx") &&
1507 !CE_HasLabel (L[1])) {
1511 /* Store the high byte */
1512 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1513 CS_InsertEntry (S, X, I+1);
1515 /* Store the low byte */
1516 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1517 CS_InsertEntry (S, X, I+2);
1519 /* Delete the call to ldauidx */
1520 CS_DelEntry (S, I+3);
1522 /* Load the high and low byte */
1523 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1524 CS_InsertEntry (S, X, I+3);
1525 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1526 CS_InsertEntry (S, X, I+4);
1528 /* Remember, we had changes */
1538 /* Return the number of changes made */
1544 /*****************************************************************************/
1545 /* Decouple operations */
1546 /*****************************************************************************/
1550 static unsigned OptDecouple (CodeSeg* S)
1551 /* Decouple operations, that is, do the following replacements:
1561 * lda zp -> lda #imm
1562 * ldx zp -> ldx #imm
1563 * ldy zp -> ldy #imm
1565 * Provided that the register values are known of course.
1568 unsigned Changes = 0;
1571 /* Generate register info for the following step */
1574 /* Walk over the entries */
1576 while (I < CS_GetEntryCount (S)) {
1580 /* Get next entry and it's input register values */
1581 CodeEntry* E = CS_GetEntry (S, I);
1582 const RegContents* In = &E->RI->In;
1584 /* Assume we have no replacement */
1587 /* Check the instruction */
1591 if (RegValIsKnown (In->RegA)) {
1592 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
1593 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1598 if (RegValIsKnown (In->RegX)) {
1599 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
1600 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1605 if (RegValIsKnown (In->RegY)) {
1606 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
1607 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1612 if (RegValIsKnown (In->RegA)) {
1613 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
1614 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1619 if (RegValIsKnown (In->RegX)) {
1620 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
1621 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1626 if (RegValIsKnown (In->RegY)) {
1627 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
1628 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1633 if (E->AM == AM65_ZP) {
1634 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1636 Arg = MakeHexArg (In->Tmp1);
1637 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1641 Arg = MakeHexArg (In->Ptr1Lo);
1642 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1646 Arg = MakeHexArg (In->Ptr1Hi);
1647 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1651 Arg = MakeHexArg (In->SRegLo);
1652 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1656 Arg = MakeHexArg (In->SRegHi);
1657 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1664 if (E->AM == AM65_ZP) {
1665 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1667 Arg = MakeHexArg (In->Tmp1);
1668 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1672 Arg = MakeHexArg (In->Ptr1Lo);
1673 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1677 Arg = MakeHexArg (In->Ptr1Hi);
1678 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1682 Arg = MakeHexArg (In->SRegLo);
1683 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1687 Arg = MakeHexArg (In->SRegHi);
1688 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1695 if (E->AM == AM65_ZP) {
1696 switch (GetKnownReg (E->Use, In)) {
1698 Arg = MakeHexArg (In->Tmp1);
1699 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1703 Arg = MakeHexArg (In->Ptr1Lo);
1704 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1708 Arg = MakeHexArg (In->Ptr1Hi);
1709 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1713 Arg = MakeHexArg (In->SRegLo);
1714 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1718 Arg = MakeHexArg (In->SRegHi);
1719 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1726 if (E->RI->In.RegA >= 0) {
1727 Arg = MakeHexArg (In->RegA);
1728 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1733 if (E->RI->In.RegA >= 0) {
1734 Arg = MakeHexArg (In->RegA);
1735 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1740 if (E->RI->In.RegX >= 0) {
1741 Arg = MakeHexArg (In->RegX);
1742 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1747 if (E->RI->In.RegY >= 0) {
1748 Arg = MakeHexArg (In->RegY);
1749 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1754 /* Avoid gcc warnings */
1759 /* Insert the replacement if we have one */
1761 CS_InsertEntry (S, X, I+1);
1771 /* Free register info */
1774 /* Return the number of changes made */
1780 /*****************************************************************************/
1781 /* struct OptFunc */
1782 /*****************************************************************************/
1786 typedef struct OptFunc OptFunc;
1788 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1789 const char* Name; /* Name of the function/group */
1790 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1791 unsigned long TotalRuns; /* Total number of runs */
1792 unsigned long LastRuns; /* Last number of runs */
1793 unsigned long TotalChanges; /* Total number of changes */
1794 unsigned long LastChanges; /* Last number of changes */
1795 char Disabled; /* True if function disabled */
1800 /*****************************************************************************/
1802 /*****************************************************************************/
1806 /* A list of all the function descriptions */
1807 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1808 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1809 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1810 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1811 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1812 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1813 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1814 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1815 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1816 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1817 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1818 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1819 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1820 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1821 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1822 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1823 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1824 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1825 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1826 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1827 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1828 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1829 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1830 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1831 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1832 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1833 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1834 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1835 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1836 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1837 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1838 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1839 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1840 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1841 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1842 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1843 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1844 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1845 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1846 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1847 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1848 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 92, 0, 0, 0, 0, 0 };
1849 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 50, 0, 0, 0, 0, 0 };
1850 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 65, 0, 0, 0, 0, 0 };
1851 static OptFunc DOptPtrLoad8 = { OptPtrLoad8, "OptPtrLoad8", 86, 0, 0, 0, 0, 0 };
1852 static OptFunc DOptPtrLoad9 = { OptPtrLoad9, "OptPtrLoad9", 100, 0, 0, 0, 0, 0 };
1853 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1854 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1855 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1856 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1857 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1858 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1859 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1860 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1861 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1862 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1863 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1864 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1865 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1866 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1867 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1868 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1869 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1870 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1871 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1872 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1873 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1874 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1875 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1876 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1877 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1880 /* Table containing all the steps in alphabetical order */
1881 static OptFunc* OptFuncs[] = {
1954 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1958 static int CmpOptStep (const void* Key, const void* Func)
1959 /* Compare function for bsearch */
1961 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1966 static OptFunc* FindOptFunc (const char* Name)
1967 /* Find an optimizer step by name in the table and return a pointer. Return
1968 * NULL if no such step is found.
1971 /* Search for the function in the list */
1972 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1978 static OptFunc* GetOptFunc (const char* Name)
1979 /* Find an optimizer step by name in the table and return a pointer. Print an
1980 * error and call AbEnd if not found.
1983 /* Search for the function in the list */
1984 OptFunc* F = FindOptFunc (Name);
1987 AbEnd ("Optimization step `%s' not found", Name);
1994 void DisableOpt (const char* Name)
1995 /* Disable the optimization with the given name */
1997 if (strcmp (Name, "any") == 0) {
1999 for (I = 0; I < OPTFUNC_COUNT; ++I) {
2000 OptFuncs[I]->Disabled = 1;
2003 GetOptFunc(Name)->Disabled = 1;
2009 void EnableOpt (const char* Name)
2010 /* Enable the optimization with the given name */
2012 if (strcmp (Name, "any") == 0) {
2014 for (I = 0; I < OPTFUNC_COUNT; ++I) {
2015 OptFuncs[I]->Disabled = 0;
2018 GetOptFunc(Name)->Disabled = 0;
2024 void ListOptSteps (FILE* F)
2025 /* List all optimization steps */
2028 for (I = 0; I < OPTFUNC_COUNT; ++I) {
2029 fprintf (F, "%s\n", OptFuncs[I]->Name);
2035 static void ReadOptStats (const char* Name)
2036 /* Read the optimizer statistics file */
2041 /* Try to open the file */
2042 FILE* F = fopen (Name, "r");
2044 /* Ignore the error */
2048 /* Read and parse the lines */
2050 while (fgets (Buf, sizeof (Buf), F) != 0) {
2058 unsigned long TotalRuns;
2059 unsigned long TotalChanges;
2064 /* Remove trailing white space including the line terminator */
2067 while (Len > 0 && IsSpace (B[Len-1])) {
2072 /* Remove leading whitespace */
2073 while (IsSpace (*B)) {
2077 /* Check for empty and comment lines */
2078 if (*B == '\0' || *B == ';' || *B == '#') {
2082 /* Parse the line */
2083 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
2088 /* Search for the optimizer step. */
2089 Func = FindOptFunc (Name);
2095 /* Found the step, set the fields */
2096 Func->TotalRuns = TotalRuns;
2097 Func->TotalChanges = TotalChanges;
2101 /* Close the file, ignore errors here. */
2107 static void WriteOptStats (const char* Name)
2108 /* Write the optimizer statistics file */
2112 /* Try to open the file */
2113 FILE* F = fopen (Name, "w");
2115 /* Ignore the error */
2119 /* Write a header */
2121 "; Optimizer Total Last Total Last\n"
2122 "; Step Runs Runs Chg Chg\n");
2125 /* Write the data */
2126 for (I = 0; I < OPTFUNC_COUNT; ++I) {
2127 const OptFunc* O = OptFuncs[I];
2129 "%-20s %10lu %10lu %10lu %10lu\n",
2137 /* Close the file, ignore errors here. */
2143 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
2144 /* Run one optimizer function Max times or until there are no more changes */
2146 unsigned Changes, C;
2148 /* Don't run the function if it is disabled or if it is prohibited by the
2151 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
2155 /* Run this until there are no more changes */
2159 /* Run the function */
2166 F->TotalChanges += C;
2167 F->LastChanges += C;
2169 } while (--Max && C > 0);
2171 /* Return the number of changes */
2177 static unsigned RunOptGroup1 (CodeSeg* S)
2178 /* Run the first group of optimization steps. These steps translate known
2179 * patterns emitted by the code generator into more optimal patterns. Order
2180 * of the steps is important, because some of the steps done earlier cover
2181 * the same patterns as later steps as subpatterns.
2184 unsigned Changes = 0;
2186 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
2187 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
2188 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
2189 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
2190 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
2191 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
2192 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
2193 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
2194 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
2195 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
2196 Changes += RunOptFunc (S, &DOptPtrLoad8, 1);
2197 Changes += RunOptFunc (S, &DOptNegAX1, 1);
2198 Changes += RunOptFunc (S, &DOptNegAX2, 1);
2199 Changes += RunOptFunc (S, &DOptNegAX3, 1);
2200 Changes += RunOptFunc (S, &DOptNegAX4, 1);
2201 Changes += RunOptFunc (S, &DOptAdd1, 1);
2202 Changes += RunOptFunc (S, &DOptAdd2, 1);
2203 Changes += RunOptFunc (S, &DOptAdd4, 1);
2204 Changes += RunOptFunc (S, &DOptStore4, 1);
2205 Changes += RunOptFunc (S, &DOptStore5, 1);
2206 Changes += RunOptFunc (S, &DOptShift1, 1);
2207 Changes += RunOptFunc (S, &DOptShift2, 1);
2208 Changes += RunOptFunc (S, &DOptShift3, 1);
2209 Changes += RunOptFunc (S, &DOptStore1, 1);
2210 Changes += RunOptFunc (S, &DOptStore2, 5);
2211 Changes += RunOptFunc (S, &DOptStore3, 5);
2213 /* Return the number of changes */
2219 static unsigned RunOptGroup2 (CodeSeg* S)
2220 /* Run one group of optimization steps. This step involves just decoupling
2221 * instructions by replacing them by instructions that do not depend on
2222 * previous instructions. This makes it easier to find instructions that
2226 unsigned Changes = 0;
2228 Changes += RunOptFunc (S, &DOptDecouple, 1);
2230 /* Return the number of changes */
2236 static unsigned RunOptGroup3 (CodeSeg* S)
2237 /* Run one group of optimization steps. These steps depend on each other,
2238 * that means that one step may allow another step to do additional work,
2239 * so we will repeat the steps as long as we see any changes.
2242 unsigned Changes, C;
2248 C += RunOptFunc (S, &DOptPtrLoad9, 1);
2249 C += RunOptFunc (S, &DOptNegA1, 1);
2250 C += RunOptFunc (S, &DOptNegA2, 1);
2251 C += RunOptFunc (S, &DOptSub1, 1);
2252 C += RunOptFunc (S, &DOptSub2, 1);
2253 C += RunOptFunc (S, &DOptSub3, 1);
2254 C += RunOptFunc (S, &DOptAdd5, 1);
2255 C += RunOptFunc (S, &DOptAdd6, 1);
2256 C += RunOptFunc (S, &DOptStackOps, 1);
2257 C += RunOptFunc (S, &DOptJumpCascades, 1);
2258 C += RunOptFunc (S, &DOptDeadJumps, 1);
2259 C += RunOptFunc (S, &DOptRTS, 1);
2260 C += RunOptFunc (S, &DOptDeadCode, 1);
2261 C += RunOptFunc (S, &DOptJumpTarget, 1);
2262 C += RunOptFunc (S, &DOptCondBranches, 1);
2263 C += RunOptFunc (S, &DOptRTSJumps1, 1);
2264 C += RunOptFunc (S, &DOptBoolTrans, 1);
2265 C += RunOptFunc (S, &DOptCmp1, 1);
2266 C += RunOptFunc (S, &DOptCmp2, 1);
2267 C += RunOptFunc (S, &DOptCmp3, 1);
2268 C += RunOptFunc (S, &DOptCmp4, 1);
2269 C += RunOptFunc (S, &DOptCmp5, 1);
2270 C += RunOptFunc (S, &DOptCmp6, 1);
2271 C += RunOptFunc (S, &DOptCmp7, 1);
2272 C += RunOptFunc (S, &DOptCmp8, 1);
2273 C += RunOptFunc (S, &DOptTest1, 1);
2274 C += RunOptFunc (S, &DOptLoad1, 1);
2275 C += RunOptFunc (S, &DOptUnusedLoads, 1);
2276 C += RunOptFunc (S, &DOptUnusedStores, 1);
2277 C += RunOptFunc (S, &DOptDupLoads, 1);
2278 C += RunOptFunc (S, &DOptStoreLoad, 1);
2279 C += RunOptFunc (S, &DOptTransfers1, 1);
2280 C += RunOptFunc (S, &DOptStore1, 1);
2281 C += RunOptFunc (S, &DOptStore5, 1);
2282 C += RunOptFunc (S, &DOptPushPop, 1);
2283 C += RunOptFunc (S, &DOptPrecalc, 1);
2289 /* Return the number of changes */
2295 static unsigned RunOptGroup4 (CodeSeg* S)
2296 /* 65C02 specific optimizations. */
2298 unsigned Changes = 0;
2300 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
2301 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
2302 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
2303 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
2305 /* The 65C02 replacement codes do often make the use of a register
2306 * value unnecessary, so if we have changes, run another load
2309 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2313 /* Return the number of changes */
2319 static unsigned RunOptGroup5 (CodeSeg* S)
2320 /* Run another round of pattern replacements. These are done late, since there
2321 * may be better replacements before.
2324 unsigned Changes = 0;
2326 Changes += RunOptFunc (S, &DOptPush1, 1);
2327 Changes += RunOptFunc (S, &DOptPush2, 1);
2328 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2329 Changes += RunOptFunc (S, &DOptTransfers2, 1);
2331 /* Return the number of changes */
2337 static unsigned RunOptGroup6 (CodeSeg* S)
2338 /* The last group of optimization steps. Adjust branches, do size optimizations.
2341 unsigned Changes = 0;
2344 if (S->CodeSizeFactor <= 100) {
2345 /* Optimize for size, that is replace operations by shorter ones, even
2346 * if this does hinder further optimizations (no problem since we're
2349 C = RunOptFunc (S, &DOptSize1, 1);
2352 /* Run some optimization passes again, since the size optimizations
2353 * may have opened new oportunities.
2355 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2356 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
2357 Changes += RunOptFunc (S, &DOptStore5, 1);
2360 C = RunOptFunc (S, &DOptSize2, 1);
2363 /* Run some optimization passes again, since the size optimizations
2364 * may have opened new oportunities.
2366 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2367 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
2368 Changes += RunOptFunc (S, &DOptStore5, 1);
2371 /* Adjust branch distances */
2372 Changes += RunOptFunc (S, &DOptBranchDist, 3);
2374 /* Replace conditional branches to RTS. If we had changes, we must run dead
2375 * code elimination again, since the change may have introduced dead code.
2377 C = RunOptFunc (S, &DOptRTSJumps2, 1);
2380 Changes += RunOptFunc (S, &DOptDeadCode, 1);
2383 /* Return the number of changes */
2389 void RunOpt (CodeSeg* S)
2390 /* Run the optimizer */
2392 const char* StatFileName;
2394 /* If we shouldn't run the optimizer, bail out */
2399 /* Check if we are requested to write optimizer statistics */
2400 StatFileName = getenv ("CC65_OPTSTATS");
2402 ReadOptStats (StatFileName);
2405 /* Print the name of the function we are working on */
2407 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2409 Print (stdout, 1, "Running optimizer for global code segment\n");
2412 /* Run groups of optimizations */
2420 /* Write statistics */
2422 WriteOptStats (StatFileName);