1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static unsigned OptShift1 (CodeSeg* S)
72 /* A call to the shlaxN routine may get replaced by one or more asl insns
73 * if the value of X is not used later.
78 /* Walk over the entries */
80 while (I < CS_GetEntryCount (S)) {
83 CodeEntry* E = CS_GetEntry (S, I);
85 /* Check for the sequence */
86 if (E->OPC == OP65_JSR &&
87 (strncmp (E->Arg, "shlax", 5) == 0 ||
88 strncmp (E->Arg, "aslax", 5) == 0) &&
89 strlen (E->Arg) == 6 &&
90 IsDigit (E->Arg[5]) &&
93 /* Insert shift insns */
94 unsigned Count = E->Arg[5] - '0';
96 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
97 CS_InsertEntry (S, X, I+1);
100 /* Delete the call to shlax */
103 /* Remember, we had changes */
113 /* Return the number of changes made */
119 static unsigned OptShift2 (CodeSeg* S)
120 /* A call to the shraxN routine may get replaced by one or more lsr insns
121 * if the value of X is not used later.
124 unsigned Changes = 0;
126 /* Walk over the entries */
128 while (I < CS_GetEntryCount (S)) {
131 CodeEntry* E = CS_GetEntry (S, I);
133 /* Check for the sequence */
134 if (E->OPC == OP65_JSR &&
135 strncmp (E->Arg, "shrax", 5) == 0 &&
136 strlen (E->Arg) == 6 &&
137 IsDigit (E->Arg[5]) &&
138 !RegXUsed (S, I+1)) {
140 /* Insert shift insns */
141 unsigned Count = E->Arg[5] - '0';
143 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
144 CS_InsertEntry (S, X, I+1);
147 /* Delete the call to shlax */
150 /* Remember, we had changes */
160 /* Return the number of changes made */
166 /*****************************************************************************/
167 /* Optimize stores through pointers */
168 /*****************************************************************************/
172 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
173 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
175 /* Check for a label attached to the entry */
176 if (CE_HasLabel (L[0])) {
180 /* Check for single insn sub ops */
181 if (L[0]->OPC == OP65_AND ||
182 L[0]->OPC == OP65_EOR ||
183 L[0]->OPC == OP65_ORA ||
184 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
185 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
190 } else if (L[0]->OPC == OP65_CLC &&
191 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
192 L[1]->OPC == OP65_ADC &&
193 !CE_HasLabel (L[1])) {
195 } else if (L[0]->OPC == OP65_SEC &&
196 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
197 L[1]->OPC == OP65_SBC &&
198 !CE_HasLabel (L[1])) {
210 static unsigned OptPtrStore1 (CodeSeg* S)
211 /* Search for the sequence:
232 unsigned Changes = 0;
234 /* Walk over the entries */
236 while (I < CS_GetEntryCount (S)) {
242 L[0] = CS_GetEntry (S, I);
244 /* Check for the sequence */
245 if (CE_IsCall (L[0], "pushax") &&
246 CS_GetEntries (S, L+1, I+1, 3) &&
247 L[1]->OPC == OP65_LDY &&
248 CE_KnownImm (L[1]) &&
249 !CE_HasLabel (L[1]) &&
250 CE_IsCall (L[2], "ldauidx") &&
251 !CE_HasLabel (L[2]) &&
252 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
253 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
254 L[3+K]->OPC == OP65_LDY &&
255 CE_KnownImm (L[3+K]) &&
256 !CE_HasLabel (L[3+K]) &&
257 CE_IsCall (L[4+K], "staspidx") &&
258 !CE_HasLabel (L[4+K])) {
262 /* Create and insert the stores */
263 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
264 CS_InsertEntry (S, X, I+1);
266 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
267 CS_InsertEntry (S, X, I+2);
269 /* Delete the call to pushax */
272 /* Delete the call to ldauidx */
273 CS_DelEntry (S, I+3);
275 /* Insert the load from ptr1 */
276 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
277 CS_InsertEntry (S, X, I+3);
278 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
279 CS_InsertEntry (S, X, I+4);
281 /* Insert the store through ptr1 */
282 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
283 CS_InsertEntry (S, X, I+6+K);
285 /* Delete the call to staspidx */
286 CS_DelEntry (S, I+7+K);
288 /* Remember, we had changes */
298 /* Return the number of changes made */
304 static unsigned OptPtrStore2 (CodeSeg* S)
305 /* Search for the sequence:
321 unsigned Changes = 0;
323 /* Walk over the entries */
325 while (I < CS_GetEntryCount (S)) {
330 L[0] = CS_GetEntry (S, I);
332 /* Check for the sequence */
333 if (CE_IsCall (L[0], "pushax") &&
334 CS_GetEntries (S, L+1, I+1, 3) &&
335 L[1]->OPC == OP65_LDA &&
336 !CE_HasLabel (L[1]) &&
337 L[2]->OPC == OP65_LDY &&
338 !CE_HasLabel (L[2]) &&
339 CE_IsCall (L[3], "staspidx") &&
340 !CE_HasLabel (L[3])) {
344 /* Create and insert the stores */
345 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
346 CS_InsertEntry (S, X, I+1);
348 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
349 CS_InsertEntry (S, X, I+2);
351 /* Delete the call to pushax */
354 /* Insert the store through ptr1 */
355 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
356 CS_InsertEntry (S, X, I+4);
358 /* Delete the call to staspidx */
359 CS_DelEntry (S, I+5);
361 /* Remember, we had changes */
371 /* Return the number of changes made */
377 /*****************************************************************************/
378 /* Optimize loads through pointers */
379 /*****************************************************************************/
383 static unsigned OptPtrLoad1 (CodeSeg* S)
384 /* Search for the sequence:
388 * lda (sp),y # May be any destination
403 unsigned Changes = 0;
405 /* Walk over the entries */
407 while (I < CS_GetEntryCount (S)) {
412 L[0] = CS_GetEntry (S, I);
414 /* Check for the sequence */
415 if (L[0]->OPC == OP65_TAX &&
416 CS_GetEntries (S, L+1, I+1, 4) &&
417 L[1]->OPC == OP65_DEY &&
418 !CE_HasLabel (L[1]) &&
419 L[2]->OPC == OP65_LDA &&
420 !CE_HasLabel (L[2]) &&
421 L[3]->OPC == OP65_LDY &&
422 !CE_HasLabel (L[3]) &&
423 CE_IsCall (L[4], "ldauidx") &&
424 !CE_HasLabel (L[4])) {
428 /* Store the high byte and remove the TAX instead */
429 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
430 CS_InsertEntry (S, X, I+1);
433 /* Store the low byte */
434 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
435 CS_InsertEntry (S, X, I+3);
437 /* Delete the call to ldauidx */
438 CS_DelEntry (S, I+5);
440 /* Load high and low byte */
441 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
442 CS_InsertEntry (S, X, I+5);
443 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
444 CS_InsertEntry (S, X, I+6);
446 /* Remember, we had changes */
456 /* Return the number of changes made */
462 static unsigned OptPtrLoad2 (CodeSeg* S)
463 /* Search for the sequence:
488 unsigned Changes = 0;
490 /* Walk over the entries */
492 while (I < CS_GetEntryCount (S)) {
497 L[0] = CS_GetEntry (S, I);
499 /* Check for the sequence */
500 if (L[0]->OPC == OP65_CLC &&
501 CS_GetEntries (S, L+1, I+1, 8) &&
502 L[1]->OPC == OP65_ADC &&
503 !CE_HasLabel (L[1]) &&
504 L[2]->OPC == OP65_TAY &&
505 !CE_HasLabel (L[2]) &&
506 L[3]->OPC == OP65_TXA &&
507 !CE_HasLabel (L[3]) &&
508 L[4]->OPC == OP65_ADC &&
509 !CE_HasLabel (L[4]) &&
510 L[5]->OPC == OP65_TAX &&
511 !CE_HasLabel (L[5]) &&
512 L[6]->OPC == OP65_TYA &&
513 !CE_HasLabel (L[6]) &&
514 L[7]->OPC == OP65_LDY &&
515 !CE_HasLabel (L[7]) &&
516 CE_IsCall (L[8], "ldauidx") &&
517 !CE_HasLabel (L[8])) {
522 /* Store the low byte and remove the TAY instead */
523 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
524 CS_InsertEntry (S, X, I+2);
525 CS_DelEntry (S, I+3);
527 /* Store the high byte */
528 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
529 CS_InsertEntry (S, X, I+5);
531 /* If the instruction before the adc is a ldx, replace the
532 * txa by and lda with the same location of the ldx.
534 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
535 P->OPC == OP65_LDX &&
538 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
539 CS_InsertEntry (S, X, I+4);
540 CS_DelEntry (S, I+3);
543 /* Delete more transfer insns */
544 CS_DelEntry (S, I+7);
545 CS_DelEntry (S, I+6);
547 /* Delete the call to ldauidx */
548 CS_DelEntry (S, I+7);
550 /* Load high and low byte */
551 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
552 CS_InsertEntry (S, X, I+7);
553 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
554 CS_InsertEntry (S, X, I+8);
556 /* Remember, we had changes */
566 /* Return the number of changes made */
572 static unsigned OptPtrLoad3 (CodeSeg* S)
573 /* Search for the sequence:
598 unsigned Changes = 0;
600 /* Walk over the entries */
602 while (I < CS_GetEntryCount (S)) {
607 L[0] = CS_GetEntry (S, I);
609 /* Check for the sequence */
610 if (L[0]->OPC == OP65_ADC &&
611 CS_GetEntries (S, L+1, I+1, 8) &&
612 L[1]->OPC == OP65_PHA &&
613 !CE_HasLabel (L[1]) &&
614 L[2]->OPC == OP65_TXA &&
615 !CE_HasLabel (L[2]) &&
616 L[3]->OPC == OP65_INY &&
617 !CE_HasLabel (L[3]) &&
618 L[4]->OPC == OP65_ADC &&
619 !CE_HasLabel (L[4]) &&
620 L[5]->OPC == OP65_TAX &&
621 !CE_HasLabel (L[5]) &&
622 L[6]->OPC == OP65_PLA &&
623 !CE_HasLabel (L[6]) &&
624 L[7]->OPC == OP65_LDY &&
625 !CE_HasLabel (L[7]) &&
626 CE_IsCall (L[8], "ldauidx") &&
627 !CE_HasLabel (L[8])) {
631 /* Store the low byte and remove the PHA instead */
632 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
633 CS_InsertEntry (S, X, I+1);
634 CS_DelEntry (S, I+2);
636 /* Store the high byte */
637 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
638 CS_InsertEntry (S, X, I+5);
640 /* Delete more transfer and PLA insns */
641 CS_DelEntry (S, I+7);
642 CS_DelEntry (S, I+6);
644 /* Delete the call to ldauidx */
645 CS_DelEntry (S, I+7);
647 /* Load high and low byte */
648 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
649 CS_InsertEntry (S, X, I+7);
650 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
651 CS_InsertEntry (S, X, I+8);
653 /* Remember, we had changes */
663 /* Return the number of changes made */
669 static unsigned OptPtrLoad4 (CodeSeg* S)
670 /* Search for the sequence:
688 unsigned Changes = 0;
690 /* Walk over the entries */
692 while (I < CS_GetEntryCount (S)) {
698 L[0] = CS_GetEntry (S, I);
700 /* Check for the sequence */
701 if (L[0]->OPC == OP65_LDA &&
702 L[0]->AM == AM65_IMM &&
703 CS_GetEntries (S, L+1, I+1, 7) &&
704 L[1]->OPC == OP65_LDX &&
705 L[1]->AM == AM65_IMM &&
706 !CE_HasLabel (L[1]) &&
707 L[2]->OPC == OP65_CLC &&
708 !CE_HasLabel (L[2]) &&
709 L[3]->OPC == OP65_ADC &&
710 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
711 !CE_HasLabel (L[3]) &&
712 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
714 L[4]->JumpTo->Owner == L[6] &&
715 !CE_HasLabel (L[4]) &&
716 L[5]->OPC == OP65_INX &&
717 !CE_HasLabel (L[5]) &&
718 L[6]->OPC == OP65_LDY &&
719 CE_KnownImm (L[6]) &&
721 CE_IsCall (L[7], "ldauidx") &&
722 !CE_HasLabel (L[7]) &&
723 /* Check the label last because this is quite costly */
724 (Len = strlen (L[0]->Arg)) > 3 &&
725 L[0]->Arg[0] == '<' &&
726 L[0]->Arg[1] == '(' &&
727 strlen (L[1]->Arg) == Len &&
728 L[1]->Arg[0] == '>' &&
729 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
734 /* We will create all the new stuff behind the current one so
735 * we keep the line references.
737 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
738 CS_InsertEntry (S, X, I+8);
740 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
741 CS_InsertEntry (S, X, I+9);
743 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
745 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
746 CS_InsertEntry (S, X, I+10);
749 /* Remove the old code */
750 CS_DelEntries (S, I, 8);
752 /* Remember, we had changes */
762 /* Return the number of changes made */
768 static unsigned OptPtrLoad5 (CodeSeg* S)
769 /* Search for the sequence:
790 unsigned Changes = 0;
792 /* Walk over the entries */
794 while (I < CS_GetEntryCount (S)) {
800 L[0] = CS_GetEntry (S, I);
802 /* Check for the sequence */
803 if (L[0]->OPC == OP65_LDA &&
804 L[0]->AM == AM65_IMM &&
805 CS_GetEntries (S, L+1, I+1, 8) &&
806 L[1]->OPC == OP65_LDX &&
807 L[1]->AM == AM65_IMM &&
808 !CE_HasLabel (L[1]) &&
809 L[2]->OPC == OP65_LDY &&
810 CE_KnownImm (L[2]) &&
811 !CE_HasLabel (L[2]) &&
812 L[3]->OPC == OP65_CLC &&
813 !CE_HasLabel (L[3]) &&
814 L[4]->OPC == OP65_ADC &&
815 L[4]->AM == AM65_ZP_INDY &&
816 !CE_HasLabel (L[4]) &&
817 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
819 L[5]->JumpTo->Owner == L[7] &&
820 !CE_HasLabel (L[5]) &&
821 L[6]->OPC == OP65_INX &&
822 !CE_HasLabel (L[6]) &&
823 L[7]->OPC == OP65_LDY &&
824 CE_KnownImm (L[7]) &&
826 CE_IsCall (L[8], "ldauidx") &&
827 !CE_HasLabel (L[8]) &&
828 /* Check the label last because this is quite costly */
829 (Len = strlen (L[0]->Arg)) > 3 &&
830 L[0]->Arg[0] == '<' &&
831 L[0]->Arg[1] == '(' &&
832 strlen (L[1]->Arg) == Len &&
833 L[1]->Arg[0] == '>' &&
834 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
840 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
841 CS_InsertEntry (S, X, I+3);
844 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
845 CS_InsertEntry (S, X, I+4);
848 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
849 CS_InsertEntry (S, X, I+5);
852 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
854 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
855 CS_InsertEntry (S, X, I+6);
858 /* Remove the old code */
859 CS_DelEntries (S, I, 2);
860 CS_DelEntries (S, I+5, 6);
862 /* Remember, we had changes */
872 /* Return the number of changes made */
878 static unsigned OptPtrLoad6 (CodeSeg* S)
879 /* Search for the sequence
892 * This step must be execute *after* OptPtrLoad1!
895 unsigned Changes = 0;
897 /* Walk over the entries */
899 while (I < CS_GetEntryCount (S)) {
904 L[0] = CS_GetEntry (S, I);
906 /* Check for the sequence */
907 if (L[0]->OPC == OP65_LDY &&
908 CS_GetEntries (S, L+1, I+1, 1) &&
909 CE_IsCall (L[1], "ldauidx") &&
910 !CE_HasLabel (L[1])) {
914 /* Store the high byte */
915 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
916 CS_InsertEntry (S, X, I+1);
918 /* Store the low byte */
919 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
920 CS_InsertEntry (S, X, I+2);
922 /* Delete the call to ldauidx */
923 CS_DelEntry (S, I+3);
925 /* Load the high and low byte */
926 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
927 CS_InsertEntry (S, X, I+3);
928 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
929 CS_InsertEntry (S, X, I+4);
931 /* Remember, we had changes */
941 /* Return the number of changes made */
947 /*****************************************************************************/
948 /* Decouple operations */
949 /*****************************************************************************/
953 static unsigned OptDecouple (CodeSeg* S)
954 /* Decouple operations, that is, do the following replacements:
964 * lda sreg -> lda #imm
965 * ldx sreg -> ldx #imm
966 * ldy sreg -> ldy #imm
968 * Provided that the register values are known of course.
971 unsigned Changes = 0;
974 /* Generate register info for the following step */
977 /* Walk over the entries */
979 while (I < CS_GetEntryCount (S)) {
984 CodeEntry* E = CS_GetEntry (S, I);
986 /* Assume we have no replacement */
989 /* Check the instruction */
993 if (E->RI->In.RegX >= 0) {
994 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX - 1) & 0xFF);
995 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1000 if (E->RI->In.RegY >= 0) {
1001 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY - 1) & 0xFF);
1002 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1007 if (E->RI->In.RegX >= 0) {
1008 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX + 1) & 0xFF);
1009 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1014 if (E->RI->In.RegY >= 0) {
1015 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY + 1) & 0xFF);
1016 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1021 if (E->AM == AM65_ZP) {
1022 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1023 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1024 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1025 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1026 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1027 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1033 if (E->AM == AM65_ZP) {
1034 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1035 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1036 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1037 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1038 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1039 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1045 if (E->AM == AM65_ZP) {
1046 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1047 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1048 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1049 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1050 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1051 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1057 if (E->RI->In.RegA >= 0) {
1058 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1059 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1064 if (E->RI->In.RegA >= 0) {
1065 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1066 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1071 if (E->RI->In.RegX >= 0) {
1072 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegX);
1073 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1078 if (E->RI->In.RegY >= 0) {
1079 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegY);
1080 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1085 /* Avoid gcc warnings */
1090 /* Insert the replacement if we have one */
1092 CS_InsertEntry (S, X, I+1);
1102 /* Free register info */
1105 /* Return the number of changes made */
1111 /*****************************************************************************/
1112 /* Size optimization */
1113 /*****************************************************************************/
1118 static unsigned OptSize1 (CodeSeg* S)
1119 /* Do size optimization by calling special subroutines that preload registers.
1120 * This routine does not work standalone, it needs a following register load
1124 static const char* Func = {
1125 "stax0sp", /* staxysp, y = 0 */
1127 "ldax0sp", /* ldaxysp, y = 1 */
1128 "ldeax0sp", /* ldeaxysp, y = 3 */
1129 "push0", /* pushax, a = 0, x = 0 */
1130 "pusha0", /* pushax, x = 0 */
1131 "pushaFF", /* pushax, x = ff */
1132 "pusha0sp", /* pushaysp, y = 0 */
1133 "tosadda0", /* tosaddax, x = 0 */
1134 "tosanda0", /* tosandax, x = 0 */
1135 "tosdiva0", /* tosdivax, x = 0 */
1136 "toseqa0", /* toseqax, x = 0 */
1137 "tosgea0", /* tosgeax, x = 0 */
1138 "tosgta0", /* tosgtax, x = 0 */
1139 "tosadd0ax", /* tosaddeax, sreg = 0 */
1140 "laddeqa", /* laddeq, sreg = 0, x = 0 */
1141 "laddeq1", /* laddeq, sreg = 0, x = 0, a = 1 */
1142 "laddeq0sp", /* laddeqysp, y = 0 */
1143 "tosand0ax", /* tosandeax, sreg = 0 */
1144 "ldaxi", /* ldaxidx, y = 1 */
1145 "ldeaxi", /* ldeaxidx, y = 3 */
1146 "ldeax0sp", /* ldeaxysp, y = 3 */
1147 "tosdiv0ax", /* tosdiveax, sreg = 0 */
1148 "toslea0", /* tosleax, x = 0 */
1149 "tosmod0ax", /* tosmodeax, sreg = 0 */
1150 "tosmul0ax", /* tosmuleax, sreg = 0 */
1151 "tosumul0ax", /* tosumuleax, sreg = 0 */
1152 "tosor0ax", /* tosoreax, sreg = 0 */
1153 "push0ax", /* pusheax, sreg = 0 */
1154 "tosrsub0ax", /* tosrsubeax, sreg = 0 */
1155 "tosshl0ax", /* tosshleax, sreg = 0 */
1156 "tosasl0ax", /* tosasleax, sreg = 0 */
1157 "tosshr0ax", /* tosshreax, sreg = 0 */
1158 "tosasr0ax", /* tosasreax, sreg = 0 */
1159 "tossub0ax", /* tossubeax, sreg = 0 */
1160 "lsubeqa", /* lsubeq, sreg = 0, x = 0 */
1161 "lsubeq1", /* lsubeq, sreg = 0, x = 0, a = 1 */
1162 "lsubeq0sp", /* lsubeqysp, y = 0 */
1163 "toslta0", /* tosltax, x = 0 */
1164 "tosudiv0ax", /* tosudiveax, sreg = 0 */
1165 "tosumod0ax", /* tosumodeax, sreg = 0 */
1166 "tosxor0ax", /* tosxoreax, sreg = 0 */
1167 "tosmoda0", /* tosmodax, x = 0 */
1168 "tosmula0", /* tosmulax, x = 0 */
1169 "tosumula0", /* tosumulax, x = 0 */
1170 "tosnea0", /* tosneax, x = 0 */
1171 "tosora0", /* tosorax, x = 0 */
1172 "push1", /* pushax, x = 0, a = 1 */
1173 "push2", /* pushax, x = 0, a = 2 */
1174 "push3", /* pushax, x = 0, a = 3 */
1175 "push4", /* pushax, x = 0, a = 4 */
1176 "push5", /* pushax, x = 0, a = 5 */
1177 "push6", /* pushax, x = 0, a = 6 */
1178 "push7", /* pushax, x = 0, a = 7 */
1179 "pushc0", /* pusha, a = 0 */
1180 "pushc1", /* pusha, a = 1 */
1181 "pushc2", /* pusha, a = 2 */
1182 "tosrsuba0", /* tosrsubax, x = 0 */
1183 "tosshla0", /* tosshlax, x = 0 */
1184 "tosasla0", /* tosaslax, x = 0 */
1185 "tosshra0", /* tosshrax, x = 0 */
1186 "tosasra0", /* tosasrax, x = 0 */
1187 "steax0sp", /* steaxsp, y = 0 */
1188 "tossuba0", /* tossubax, x = 0 */
1189 "subeq0sp", /* subeqysp, y = 0 */
1190 "tosudiva0", /* tosudivax, x = 0 */
1191 "tosugea0", /* tosugeax, x = 0 */
1192 "tosugta0", /* tosugtax, x = 0 */
1193 "tosulea0", /* tosuleax, x = 0 */
1194 "tosulta0", /* tosultax, x = 0 */
1195 "tosumoda0", /* tosumodax, x = 0 */
1196 "tosxora0", /* tosxorax, x = 0 */
1199 unsigned Changes = 0;
1202 /* Generate register info for the following step */
1205 /* Walk over the entries */
1207 while (I < CS_GetEntryCount (S)) {
1209 /* Get next entry */
1210 CodeEntry* E = CS_GetEntry (S, I);
1218 /* Free register info */
1221 /* Return the number of changes made */
1228 static unsigned OptSize2 (CodeSeg* S)
1229 /* Do size optimization by using shorter code sequences, even if this
1230 * introduces relations between instructions. This step must be one of the
1231 * last steps, because it makes further work much more difficult.
1234 unsigned Changes = 0;
1237 /* Generate register info for the following step */
1240 /* Walk over the entries */
1242 while (I < CS_GetEntryCount (S)) {
1245 /* Get next entry */
1246 CodeEntry* E = CS_GetEntry (S, I);
1248 /* Assume we have no replacement */
1251 /* Check the instruction */
1255 if (CE_KnownImm (E)) {
1256 short Val = (short) E->Num;
1257 if (Val == E->RI->In.RegX) {
1258 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
1259 } else if (Val == E->RI->In.RegY) {
1260 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
1261 } else if (E->RI->In.RegA >= 0 && CPU >= CPU_65C02) {
1262 if (Val == ((E->RI->In.RegA - 1) & 0xFF)) {
1263 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
1264 } else if (Val == ((E->RI->In.RegA + 1) & 0xFF)) {
1265 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
1272 if (CE_KnownImm (E)) {
1273 short Val = (short) E->Num;
1274 if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX - 1) & 0xFF)) {
1275 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
1276 } else if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX + 1) & 0xFF)) {
1277 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
1278 } else if (Val == E->RI->In.RegA) {
1279 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
1285 if (CE_KnownImm (E)) {
1286 short Val = (short) E->Num;
1287 if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY - 1) & 0xFF)) {
1288 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
1289 } else if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY + 1) & 0xFF)) {
1290 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
1291 } else if (Val == E->RI->In.RegA) {
1292 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
1298 /* Avoid gcc warnings */
1303 /* Insert the replacement if we have one */
1305 CS_InsertEntry (S, X, I+1);
1315 /* Free register info */
1318 /* Return the number of changes made */
1324 /*****************************************************************************/
1325 /* struct OptFunc */
1326 /*****************************************************************************/
1330 typedef struct OptFunc OptFunc;
1332 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1333 const char* Name; /* Name of the function/group */
1334 unsigned long TotalRuns; /* Total number of runs */
1335 unsigned long LastRuns; /* Last number of runs */
1336 unsigned long TotalChanges; /* Total number of changes */
1337 unsigned long LastChanges; /* Last number of changes */
1338 char Disabled; /* True if function disabled */
1343 /*****************************************************************************/
1345 /*****************************************************************************/
1349 /* Macro that builds a function description */
1350 #define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
1352 /* A list of all the function descriptions */
1353 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 0, 0, 0, 0, 0 };
1354 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 0, 0, 0, 0, 0 };
1355 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 0, 0, 0, 0, 0 };
1356 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 0, 0, 0, 0, 0 };
1357 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 0, 0, 0, 0, 0 };
1358 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0 };
1359 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 0, 0, 0, 0, 0 };
1360 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 0, 0, 0, 0, 0 };
1361 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 0, 0, 0, 0, 0 };
1362 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 0, 0, 0, 0, 0 };
1363 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 0, 0, 0, 0, 0 };
1364 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 0, 0, 0, 0, 0 };
1365 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 0, 0, 0, 0, 0 };
1366 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 0, 0, 0, 0, 0 };
1367 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 0, 0, 0, 0, 0 };
1368 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 0, 0, 0, 0, 0 };
1369 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 0, 0, 0, 0, 0 };
1370 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0 };
1371 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 0, 0, 0, 0, 0 };
1372 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 0, 0, 0, 0, 0 };
1373 static OptFunc DOptRTS = { OptRTS, "OptRTS", 0, 0, 0, 0, 0 };
1374 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 0, 0, 0, 0, 0 };
1375 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 0, 0, 0, 0, 0 };
1376 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 0, 0, 0, 0, 0 };
1377 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 0, 0, 0, 0, 0 };
1378 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 0, 0, 0, 0, 0 };
1379 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 0, 0, 0, 0, 0 };
1380 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 0, 0, 0, 0, 0 };
1381 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 0, 0, 0, 0, 0 };
1382 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 0, 0, 0, 0, 0 };
1383 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 0, 0, 0, 0, 0 };
1384 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 0, 0, 0, 0, 0 };
1385 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 0, 0, 0, 0, 0 };
1386 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 0, 0, 0, 0, 0 };
1387 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 0, 0, 0, 0, 0 };
1388 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 0, 0, 0, 0, 0 };
1389 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 0, 0, 0, 0, 0 };
1390 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 0, 0, 0, 0, 0 };
1391 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 0, 0, 0, 0, 0 };
1392 /*static OptFunc DOptSize1 = { OptSize1, "OptSize1", 0, 0, 0, 0, 0 };*/
1393 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 0, 0, 0, 0, 0 };
1394 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 0, 0, 0, 0, 0 };
1395 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0 };
1396 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 0, 0, 0, 0, 0 };
1397 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 0, 0, 0, 0, 0 };
1398 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 0, 0, 0, 0, 0 };
1399 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0 };
1400 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0 };
1401 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0 };
1404 /* Table containing all the steps in alphabetical order */
1405 static OptFunc* OptFuncs[] = {
1456 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1460 static int CmpOptStep (const void* Key, const void* Func)
1461 /* Compare function for bsearch */
1463 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1468 static OptFunc* FindOptFunc (const char* Name)
1469 /* Find an optimizer step by name in the table and return a pointer. Return
1470 * NULL if no such step is found.
1473 /* Search for the function in the list */
1474 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1480 static OptFunc* GetOptFunc (const char* Name)
1481 /* Find an optimizer step by name in the table and return a pointer. Print an
1482 * error and call AbEnd if not found.
1485 /* Search for the function in the list */
1486 OptFunc* F = FindOptFunc (Name);
1489 AbEnd ("Optimization step `%s' not found", Name);
1496 void DisableOpt (const char* Name)
1497 /* Disable the optimization with the given name */
1499 if (strcmp (Name, "any") == 0) {
1501 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1502 OptFuncs[I]->Disabled = 1;
1505 GetOptFunc(Name)->Disabled = 1;
1511 void EnableOpt (const char* Name)
1512 /* Enable the optimization with the given name */
1514 if (strcmp (Name, "any") == 0) {
1516 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1517 OptFuncs[I]->Disabled = 0;
1520 GetOptFunc(Name)->Disabled = 0;
1526 void ListOptSteps (FILE* F)
1527 /* List all optimization steps */
1530 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1531 fprintf (F, "%s\n", OptFuncs[I]->Name);
1537 static void ReadOptStats (const char* Name)
1538 /* Read the optimizer statistics file */
1543 /* Try to open the file */
1544 FILE* F = fopen (Name, "r");
1546 /* Ignore the error */
1550 /* Read and parse the lines */
1552 while (fgets (Buf, sizeof (Buf), F) != 0) {
1560 unsigned long TotalRuns;
1561 unsigned long TotalChanges;
1566 /* Remove trailing white space including the line terminator */
1569 while (Len > 0 && IsSpace (B[Len-1])) {
1574 /* Remove leading whitespace */
1575 while (IsSpace (*B)) {
1579 /* Check for empty and comment lines */
1580 if (*B == '\0' || *B == ';' || *B == '#') {
1584 /* Parse the line */
1585 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1590 /* Search for the optimizer step. */
1591 Func = FindOptFunc (Name);
1597 /* Found the step, set the fields */
1598 Func->TotalRuns = TotalRuns;
1599 Func->TotalChanges = TotalChanges;
1603 /* Close the file, ignore errors here. */
1609 static void WriteOptStats (const char* Name)
1610 /* Write the optimizer statistics file */
1614 /* Try to open the file */
1615 FILE* F = fopen (Name, "w");
1617 /* Ignore the error */
1621 /* Write the file */
1622 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1623 const OptFunc* O = OptFuncs[I];
1625 "%-20s %6lu %6lu %6lu %6lu\n",
1633 /* Close the file, ignore errors here. */
1639 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1640 /* Run one optimizer function Max times or until there are no more changes */
1642 unsigned Changes, C;
1644 /* Don't run the function if it is disabled */
1649 /* Run this until there are no more changes */
1653 /* Run the function */
1660 F->TotalChanges += C;
1661 F->LastChanges += C;
1663 } while (--Max && C > 0);
1665 /* Return the number of changes */
1671 static void RunOptGroup1 (CodeSeg* S)
1672 /* Run the first group of optimization steps. These steps translate known
1673 * patterns emitted by the code generator into more optimal patterns. Order
1674 * of the steps is important, because some of the steps done earlier cover
1675 * the same patterns as later steps as subpatterns.
1678 RunOptFunc (S, &DOptPtrStore1, 1);
1679 RunOptFunc (S, &DOptPtrStore2, 1);
1680 RunOptFunc (S, &DOptPtrLoad1, 1);
1681 RunOptFunc (S, &DOptPtrLoad2, 1);
1682 RunOptFunc (S, &DOptPtrLoad3, 1);
1683 RunOptFunc (S, &DOptPtrLoad4, 1);
1684 RunOptFunc (S, &DOptPtrLoad5, 1);
1685 RunOptFunc (S, &DOptNegAX1, 1);
1686 RunOptFunc (S, &DOptNegAX2, 1);
1687 RunOptFunc (S, &DOptNegAX3, 1);
1688 RunOptFunc (S, &DOptNegAX4, 1);
1689 RunOptFunc (S, &DOptAdd1, 1);
1690 RunOptFunc (S, &DOptAdd2, 1);
1691 RunOptFunc (S, &DOptShift1, 1);
1692 RunOptFunc (S, &DOptShift2, 1);
1697 static void RunOptGroup2 (CodeSeg* S)
1698 /* Run one group of optimization steps. This step involves just decoupling
1699 * instructions by replacing them by instructions that do not depend on
1700 * previous instructions. This makes it easier to find instructions that
1704 RunOptFunc (S, &DOptDecouple, 1);
1710 static void RunOptGroup3 (CodeSeg* S)
1711 /* Run one group of optimization steps. These steps depend on each other,
1712 * that means that one step may allow another step to do additional work,
1713 * so we will repeat the steps as long as we see any changes.
1721 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1722 Changes += RunOptFunc (S, &DOptNegA1, 1);
1723 Changes += RunOptFunc (S, &DOptNegA2, 1);
1724 Changes += RunOptFunc (S, &DOptSub1, 1);
1725 Changes += RunOptFunc (S, &DOptSub2, 1);
1726 Changes += RunOptFunc (S, &DOptAdd3, 1);
1727 Changes += RunOptFunc (S, &DOptStackOps, 1);
1728 Changes += RunOptFunc (S, &DOptJumpCascades, 1);
1729 Changes += RunOptFunc (S, &DOptDeadJumps, 1);
1730 Changes += RunOptFunc (S, &DOptRTS, 1);
1731 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1732 Changes += RunOptFunc (S, &DOptJumpTarget, 1);
1733 Changes += RunOptFunc (S, &DOptCondBranches, 1);
1734 Changes += RunOptFunc (S, &DOptRTSJumps1, 1);
1735 Changes += RunOptFunc (S, &DOptBoolTrans, 1);
1736 Changes += RunOptFunc (S, &DOptCmp1, 1);
1737 Changes += RunOptFunc (S, &DOptCmp2, 1);
1738 Changes += RunOptFunc (S, &DOptCmp3, 1);
1739 Changes += RunOptFunc (S, &DOptCmp4, 1);
1740 Changes += RunOptFunc (S, &DOptCmp5, 1);
1741 Changes += RunOptFunc (S, &DOptCmp6, 1);
1742 Changes += RunOptFunc (S, &DOptCmp7, 1);
1743 Changes += RunOptFunc (S, &DOptTest1, 1);
1744 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1745 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1746 Changes += RunOptFunc (S, &DOptDupLoads, 1);
1747 Changes += RunOptFunc (S, &DOptStoreLoad, 1);
1748 Changes += RunOptFunc (S, &DOptTransfers, 1);
1755 static void RunOptGroup4 (CodeSeg* S)
1756 /* 65C02 specific optimizations. */
1758 if (CPU < CPU_65C02) {
1762 /* Replace (zp),y by (zp) if Y is zero. If we have changes, run register
1763 * load optimization again, since loads of Y may have become unnecessary.
1765 if (RunOptFunc (S, &DOpt65C02Ind, 1) > 0) {
1766 RunOptFunc (S, &DOptUnusedLoads, 1);
1772 static void RunOptGroup5 (CodeSeg* S)
1773 /* The last group of optimization steps. Adjust branches, do size optimizations.
1776 /* Optimize for size, that is replace operations by shorter ones, even
1777 * if this does hinder further optimizations (no problem since we're
1780 RunOptFunc (S, &DOptSize2, 1);
1782 /* Run the jump target optimization again, since the size optimization
1783 * above may have opened new oportunities.
1785 RunOptFunc (S, &DOptJumpTarget, 5);
1787 /* Adjust branch distances */
1788 RunOptFunc (S, &DOptBranchDist, 3);
1790 /* Replace conditional branches to RTS. If we had changes, we must run dead
1791 * code elimination again, since the change may have introduced dead code.
1793 if (RunOptFunc (S, &DOptRTSJumps2, 1)) {
1794 RunOptFunc (S, &DOptDeadCode, 1);
1800 void RunOpt (CodeSeg* S)
1801 /* Run the optimizer */
1803 const char* StatFileName;
1805 /* If we shouldn't run the optimizer, bail out */
1810 /* Check if we are requested to write optimizer statistics */
1811 StatFileName = getenv ("CC65_OPTSTATS");
1813 ReadOptStats (StatFileName);
1816 /* Print the name of the function we are working on */
1818 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1820 Print (stdout, 1, "Running optimizer for global code segment\n");
1823 /* Run groups of optimizations */
1830 /* Write statistics */
1832 WriteOptStats (StatFileName);