1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2002 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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static unsigned OptShift1 (CodeSeg* S)
73 /* A call to the shlaxN routine may get replaced by one or more asl insns
74 * if the value of X is not used later.
79 /* Walk over the entries */
81 while (I < CS_GetEntryCount (S)) {
84 CodeEntry* E = CS_GetEntry (S, I);
86 /* Check for the sequence */
87 if (E->OPC == OP65_JSR &&
88 (strncmp (E->Arg, "shlax", 5) == 0 ||
89 strncmp (E->Arg, "aslax", 5) == 0) &&
90 strlen (E->Arg) == 6 &&
91 IsDigit (E->Arg[5]) &&
94 /* Insert shift insns */
95 unsigned Count = E->Arg[5] - '0';
97 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
98 CS_InsertEntry (S, X, I+1);
101 /* Delete the call to shlax */
104 /* Remember, we had changes */
114 /* Return the number of changes made */
120 static unsigned OptShift2 (CodeSeg* S)
121 /* A call to the shraxN routine may get replaced by one or more lsr insns
122 * if the value of X is not used later.
125 unsigned Changes = 0;
127 /* Walk over the entries */
129 while (I < CS_GetEntryCount (S)) {
132 CodeEntry* E = CS_GetEntry (S, I);
134 /* Check for the sequence */
135 if (E->OPC == OP65_JSR &&
136 strncmp (E->Arg, "shrax", 5) == 0 &&
137 strlen (E->Arg) == 6 &&
138 IsDigit (E->Arg[5]) &&
139 !RegXUsed (S, I+1)) {
141 /* Insert shift insns */
142 unsigned Count = E->Arg[5] - '0';
144 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
145 CS_InsertEntry (S, X, I+1);
148 /* Delete the call to shlax */
151 /* Remember, we had changes */
161 /* Return the number of changes made */
167 /*****************************************************************************/
168 /* Optimize stores through pointers */
169 /*****************************************************************************/
173 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
174 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
176 /* Check for a label attached to the entry */
177 if (CE_HasLabel (L[0])) {
181 /* Check for single insn sub ops */
182 if (L[0]->OPC == OP65_AND ||
183 L[0]->OPC == OP65_EOR ||
184 L[0]->OPC == OP65_ORA ||
185 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
186 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
191 } else if (L[0]->OPC == OP65_CLC &&
192 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
193 L[1]->OPC == OP65_ADC &&
194 !CE_HasLabel (L[1])) {
196 } else if (L[0]->OPC == OP65_SEC &&
197 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
198 L[1]->OPC == OP65_SBC &&
199 !CE_HasLabel (L[1])) {
211 static unsigned OptPtrStore1 (CodeSeg* S)
212 /* Search for the sequence:
233 unsigned Changes = 0;
235 /* Walk over the entries */
237 while (I < CS_GetEntryCount (S)) {
243 L[0] = CS_GetEntry (S, I);
245 /* Check for the sequence */
246 if (CE_IsCall (L[0], "pushax") &&
247 CS_GetEntries (S, L+1, I+1, 3) &&
248 L[1]->OPC == OP65_LDY &&
249 CE_KnownImm (L[1]) &&
250 !CE_HasLabel (L[1]) &&
251 CE_IsCall (L[2], "ldauidx") &&
252 !CE_HasLabel (L[2]) &&
253 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
254 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
255 L[3+K]->OPC == OP65_LDY &&
256 CE_KnownImm (L[3+K]) &&
257 !CE_HasLabel (L[3+K]) &&
258 CE_IsCall (L[4+K], "staspidx") &&
259 !CE_HasLabel (L[4+K])) {
263 /* Create and insert the stores */
264 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
265 CS_InsertEntry (S, X, I+1);
267 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
268 CS_InsertEntry (S, X, I+2);
270 /* Delete the call to pushax */
273 /* Delete the call to ldauidx */
274 CS_DelEntry (S, I+3);
276 /* Insert the load from ptr1 */
277 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
278 CS_InsertEntry (S, X, I+3);
279 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
280 CS_InsertEntry (S, X, I+4);
282 /* Insert the store through ptr1 */
283 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
284 CS_InsertEntry (S, X, I+6+K);
286 /* Delete the call to staspidx */
287 CS_DelEntry (S, I+7+K);
289 /* Remember, we had changes */
299 /* Return the number of changes made */
305 static unsigned OptPtrStore2 (CodeSeg* S)
306 /* Search for the sequence:
322 unsigned Changes = 0;
324 /* Walk over the entries */
326 while (I < CS_GetEntryCount (S)) {
331 L[0] = CS_GetEntry (S, I);
333 /* Check for the sequence */
334 if (CE_IsCall (L[0], "pushax") &&
335 CS_GetEntries (S, L+1, I+1, 3) &&
336 L[1]->OPC == OP65_LDA &&
337 !CE_HasLabel (L[1]) &&
338 L[2]->OPC == OP65_LDY &&
339 !CE_HasLabel (L[2]) &&
340 CE_IsCall (L[3], "staspidx") &&
341 !CE_HasLabel (L[3])) {
345 /* Create and insert the stores */
346 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
347 CS_InsertEntry (S, X, I+1);
349 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
350 CS_InsertEntry (S, X, I+2);
352 /* Delete the call to pushax */
355 /* Insert the store through ptr1 */
356 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
357 CS_InsertEntry (S, X, I+4);
359 /* Delete the call to staspidx */
360 CS_DelEntry (S, I+5);
362 /* Remember, we had changes */
372 /* Return the number of changes made */
378 /*****************************************************************************/
379 /* Optimize loads through pointers */
380 /*****************************************************************************/
384 static unsigned OptPtrLoad1 (CodeSeg* S)
385 /* Search for the sequence:
389 * lda (sp),y # May be any destination
404 unsigned Changes = 0;
406 /* Walk over the entries */
408 while (I < CS_GetEntryCount (S)) {
413 L[0] = CS_GetEntry (S, I);
415 /* Check for the sequence */
416 if (L[0]->OPC == OP65_TAX &&
417 CS_GetEntries (S, L+1, I+1, 4) &&
418 L[1]->OPC == OP65_DEY &&
419 !CE_HasLabel (L[1]) &&
420 L[2]->OPC == OP65_LDA &&
421 !CE_HasLabel (L[2]) &&
422 L[3]->OPC == OP65_LDY &&
423 !CE_HasLabel (L[3]) &&
424 CE_IsCall (L[4], "ldauidx") &&
425 !CE_HasLabel (L[4])) {
429 /* Store the high byte and remove the TAX instead */
430 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
431 CS_InsertEntry (S, X, I+1);
434 /* Store the low byte */
435 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
436 CS_InsertEntry (S, X, I+3);
438 /* Delete the call to ldauidx */
439 CS_DelEntry (S, I+5);
441 /* Load high and low byte */
442 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
443 CS_InsertEntry (S, X, I+5);
444 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
445 CS_InsertEntry (S, X, I+6);
447 /* Remember, we had changes */
457 /* Return the number of changes made */
463 static unsigned OptPtrLoad2 (CodeSeg* S)
464 /* Search for the sequence:
489 unsigned Changes = 0;
491 /* Walk over the entries */
493 while (I < CS_GetEntryCount (S)) {
498 L[0] = CS_GetEntry (S, I);
500 /* Check for the sequence */
501 if (L[0]->OPC == OP65_CLC &&
502 CS_GetEntries (S, L+1, I+1, 8) &&
503 L[1]->OPC == OP65_ADC &&
504 !CE_HasLabel (L[1]) &&
505 L[2]->OPC == OP65_TAY &&
506 !CE_HasLabel (L[2]) &&
507 L[3]->OPC == OP65_TXA &&
508 !CE_HasLabel (L[3]) &&
509 L[4]->OPC == OP65_ADC &&
510 !CE_HasLabel (L[4]) &&
511 L[5]->OPC == OP65_TAX &&
512 !CE_HasLabel (L[5]) &&
513 L[6]->OPC == OP65_TYA &&
514 !CE_HasLabel (L[6]) &&
515 L[7]->OPC == OP65_LDY &&
516 !CE_HasLabel (L[7]) &&
517 CE_IsCall (L[8], "ldauidx") &&
518 !CE_HasLabel (L[8])) {
523 /* Store the low byte and remove the TAY instead */
524 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
525 CS_InsertEntry (S, X, I+2);
526 CS_DelEntry (S, I+3);
528 /* Store the high byte */
529 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
530 CS_InsertEntry (S, X, I+5);
532 /* If the instruction before the adc is a ldx, replace the
533 * txa by and lda with the same location of the ldx.
535 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
536 P->OPC == OP65_LDX &&
539 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
540 CS_InsertEntry (S, X, I+4);
541 CS_DelEntry (S, I+3);
544 /* Delete more transfer insns */
545 CS_DelEntry (S, I+7);
546 CS_DelEntry (S, I+6);
548 /* Delete the call to ldauidx */
549 CS_DelEntry (S, I+7);
551 /* Load high and low byte */
552 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
553 CS_InsertEntry (S, X, I+7);
554 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
555 CS_InsertEntry (S, X, I+8);
557 /* Remember, we had changes */
567 /* Return the number of changes made */
573 static unsigned OptPtrLoad3 (CodeSeg* S)
574 /* Search for the sequence:
599 unsigned Changes = 0;
601 /* Walk over the entries */
603 while (I < CS_GetEntryCount (S)) {
608 L[0] = CS_GetEntry (S, I);
610 /* Check for the sequence */
611 if (L[0]->OPC == OP65_ADC &&
612 CS_GetEntries (S, L+1, I+1, 8) &&
613 L[1]->OPC == OP65_PHA &&
614 !CE_HasLabel (L[1]) &&
615 L[2]->OPC == OP65_TXA &&
616 !CE_HasLabel (L[2]) &&
617 L[3]->OPC == OP65_INY &&
618 !CE_HasLabel (L[3]) &&
619 L[4]->OPC == OP65_ADC &&
620 !CE_HasLabel (L[4]) &&
621 L[5]->OPC == OP65_TAX &&
622 !CE_HasLabel (L[5]) &&
623 L[6]->OPC == OP65_PLA &&
624 !CE_HasLabel (L[6]) &&
625 L[7]->OPC == OP65_LDY &&
626 !CE_HasLabel (L[7]) &&
627 CE_IsCall (L[8], "ldauidx") &&
628 !CE_HasLabel (L[8])) {
632 /* Store the low byte and remove the PHA instead */
633 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
634 CS_InsertEntry (S, X, I+1);
635 CS_DelEntry (S, I+2);
637 /* Store the high byte */
638 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
639 CS_InsertEntry (S, X, I+5);
641 /* Delete more transfer and PLA insns */
642 CS_DelEntry (S, I+7);
643 CS_DelEntry (S, I+6);
645 /* Delete the call to ldauidx */
646 CS_DelEntry (S, I+7);
648 /* Load high and low byte */
649 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
650 CS_InsertEntry (S, X, I+7);
651 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
652 CS_InsertEntry (S, X, I+8);
654 /* Remember, we had changes */
664 /* Return the number of changes made */
670 static unsigned OptPtrLoad4 (CodeSeg* S)
671 /* Search for the sequence:
689 unsigned Changes = 0;
691 /* Walk over the entries */
693 while (I < CS_GetEntryCount (S)) {
699 L[0] = CS_GetEntry (S, I);
701 /* Check for the sequence */
702 if (L[0]->OPC == OP65_LDA &&
703 L[0]->AM == AM65_IMM &&
704 CS_GetEntries (S, L+1, I+1, 7) &&
705 L[1]->OPC == OP65_LDX &&
706 L[1]->AM == AM65_IMM &&
707 !CE_HasLabel (L[1]) &&
708 L[2]->OPC == OP65_CLC &&
709 !CE_HasLabel (L[2]) &&
710 L[3]->OPC == OP65_ADC &&
711 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
712 !CE_HasLabel (L[3]) &&
713 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
715 L[4]->JumpTo->Owner == L[6] &&
716 !CE_HasLabel (L[4]) &&
717 L[5]->OPC == OP65_INX &&
718 !CE_HasLabel (L[5]) &&
719 L[6]->OPC == OP65_LDY &&
720 CE_KnownImm (L[6]) &&
722 CE_IsCall (L[7], "ldauidx") &&
723 !CE_HasLabel (L[7]) &&
724 /* Check the label last because this is quite costly */
725 (Len = strlen (L[0]->Arg)) > 3 &&
726 L[0]->Arg[0] == '<' &&
727 L[0]->Arg[1] == '(' &&
728 strlen (L[1]->Arg) == Len &&
729 L[1]->Arg[0] == '>' &&
730 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
735 /* We will create all the new stuff behind the current one so
736 * we keep the line references.
738 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
739 CS_InsertEntry (S, X, I+8);
741 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
742 CS_InsertEntry (S, X, I+9);
744 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
746 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
747 CS_InsertEntry (S, X, I+10);
750 /* Remove the old code */
751 CS_DelEntries (S, I, 8);
753 /* Remember, we had changes */
763 /* Return the number of changes made */
769 static unsigned OptPtrLoad5 (CodeSeg* S)
770 /* Search for the sequence:
791 unsigned Changes = 0;
793 /* Walk over the entries */
795 while (I < CS_GetEntryCount (S)) {
801 L[0] = CS_GetEntry (S, I);
803 /* Check for the sequence */
804 if (L[0]->OPC == OP65_LDA &&
805 L[0]->AM == AM65_IMM &&
806 CS_GetEntries (S, L+1, I+1, 8) &&
807 L[1]->OPC == OP65_LDX &&
808 L[1]->AM == AM65_IMM &&
809 !CE_HasLabel (L[1]) &&
810 L[2]->OPC == OP65_LDY &&
811 CE_KnownImm (L[2]) &&
812 !CE_HasLabel (L[2]) &&
813 L[3]->OPC == OP65_CLC &&
814 !CE_HasLabel (L[3]) &&
815 L[4]->OPC == OP65_ADC &&
816 L[4]->AM == AM65_ZP_INDY &&
817 !CE_HasLabel (L[4]) &&
818 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
820 L[5]->JumpTo->Owner == L[7] &&
821 !CE_HasLabel (L[5]) &&
822 L[6]->OPC == OP65_INX &&
823 !CE_HasLabel (L[6]) &&
824 L[7]->OPC == OP65_LDY &&
825 CE_KnownImm (L[7]) &&
827 CE_IsCall (L[8], "ldauidx") &&
828 !CE_HasLabel (L[8]) &&
829 /* Check the label last because this is quite costly */
830 (Len = strlen (L[0]->Arg)) > 3 &&
831 L[0]->Arg[0] == '<' &&
832 L[0]->Arg[1] == '(' &&
833 strlen (L[1]->Arg) == Len &&
834 L[1]->Arg[0] == '>' &&
835 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
841 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
842 CS_InsertEntry (S, X, I+3);
845 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
846 CS_InsertEntry (S, X, I+4);
849 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
850 CS_InsertEntry (S, X, I+5);
853 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
855 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
856 CS_InsertEntry (S, X, I+6);
859 /* Remove the old code */
860 CS_DelEntries (S, I, 2);
861 CS_DelEntries (S, I+5, 6);
863 /* Remember, we had changes */
873 /* Return the number of changes made */
879 static unsigned OptPtrLoad6 (CodeSeg* S)
880 /* Search for the sequence
893 * This step must be execute *after* OptPtrLoad1!
896 unsigned Changes = 0;
898 /* Walk over the entries */
900 while (I < CS_GetEntryCount (S)) {
905 L[0] = CS_GetEntry (S, I);
907 /* Check for the sequence */
908 if (L[0]->OPC == OP65_LDY &&
909 CS_GetEntries (S, L+1, I+1, 1) &&
910 CE_IsCall (L[1], "ldauidx") &&
911 !CE_HasLabel (L[1])) {
915 /* Store the high byte */
916 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
917 CS_InsertEntry (S, X, I+1);
919 /* Store the low byte */
920 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
921 CS_InsertEntry (S, X, I+2);
923 /* Delete the call to ldauidx */
924 CS_DelEntry (S, I+3);
926 /* Load the high and low byte */
927 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
928 CS_InsertEntry (S, X, I+3);
929 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
930 CS_InsertEntry (S, X, I+4);
932 /* Remember, we had changes */
942 /* Return the number of changes made */
948 /*****************************************************************************/
949 /* Decouple operations */
950 /*****************************************************************************/
954 static unsigned OptDecouple (CodeSeg* S)
955 /* Decouple operations, that is, do the following replacements:
965 * lda sreg -> lda #imm
966 * ldx sreg -> ldx #imm
967 * ldy sreg -> ldy #imm
969 * Provided that the register values are known of course.
972 unsigned Changes = 0;
975 /* Generate register info for the following step */
978 /* Walk over the entries */
980 while (I < CS_GetEntryCount (S)) {
985 CodeEntry* E = CS_GetEntry (S, I);
987 /* Assume we have no replacement */
990 /* Check the instruction */
994 if (E->RI->In.RegX >= 0) {
995 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX - 1) & 0xFF);
996 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1001 if (E->RI->In.RegY >= 0) {
1002 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY - 1) & 0xFF);
1003 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1008 if (E->RI->In.RegX >= 0) {
1009 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX + 1) & 0xFF);
1010 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1015 if (E->RI->In.RegY >= 0) {
1016 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY + 1) & 0xFF);
1017 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1022 if (E->AM == AM65_ZP) {
1023 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1024 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1025 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1026 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1027 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1028 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1034 if (E->AM == AM65_ZP) {
1035 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1036 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1037 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1038 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1039 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1040 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1046 if (E->AM == AM65_ZP) {
1047 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1048 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1049 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1050 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1051 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1052 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1058 if (E->RI->In.RegA >= 0) {
1059 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1060 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1065 if (E->RI->In.RegA >= 0) {
1066 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1067 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1072 if (E->RI->In.RegX >= 0) {
1073 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegX);
1074 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1079 if (E->RI->In.RegY >= 0) {
1080 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegY);
1081 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1086 /* Avoid gcc warnings */
1091 /* Insert the replacement if we have one */
1093 CS_InsertEntry (S, X, I+1);
1103 /* Free register info */
1106 /* Return the number of changes made */
1112 /*****************************************************************************/
1113 /* Size optimization */
1114 /*****************************************************************************/
1119 static unsigned OptSize1 (CodeSeg* S)
1120 /* Do size optimization by calling special subroutines that preload registers.
1121 * This routine does not work standalone, it needs a following register load
1125 static const char* Func = {
1126 "stax0sp", /* staxysp, y = 0 */
1128 "ldax0sp", /* ldaxysp, y = 1 */
1129 "ldeax0sp", /* ldeaxysp, y = 3 */
1130 "push0", /* pushax, a = 0, x = 0 */
1131 "pusha0", /* pushax, x = 0 */
1132 "pushaFF", /* pushax, x = ff */
1133 "pusha0sp", /* pushaysp, y = 0 */
1134 "tosadda0", /* tosaddax, x = 0 */
1135 "tosanda0", /* tosandax, x = 0 */
1136 "tosdiva0", /* tosdivax, x = 0 */
1137 "toseqa0", /* toseqax, x = 0 */
1138 "tosgea0", /* tosgeax, x = 0 */
1139 "tosgta0", /* tosgtax, x = 0 */
1140 "tosadd0ax", /* tosaddeax, sreg = 0 */
1141 "laddeqa", /* laddeq, sreg = 0, x = 0 */
1142 "laddeq1", /* laddeq, sreg = 0, x = 0, a = 1 */
1143 "laddeq0sp", /* laddeqysp, y = 0 */
1144 "tosand0ax", /* tosandeax, sreg = 0 */
1145 "ldaxi", /* ldaxidx, y = 1 */
1146 "ldeaxi", /* ldeaxidx, y = 3 */
1147 "ldeax0sp", /* ldeaxysp, y = 3 */
1148 "tosdiv0ax", /* tosdiveax, sreg = 0 */
1149 "toslea0", /* tosleax, x = 0 */
1150 "tosmod0ax", /* tosmodeax, sreg = 0 */
1151 "tosmul0ax", /* tosmuleax, sreg = 0 */
1152 "tosumul0ax", /* tosumuleax, sreg = 0 */
1153 "tosor0ax", /* tosoreax, sreg = 0 */
1154 "push0ax", /* pusheax, sreg = 0 */
1155 "tosrsub0ax", /* tosrsubeax, sreg = 0 */
1156 "tosshl0ax", /* tosshleax, sreg = 0 */
1157 "tosasl0ax", /* tosasleax, sreg = 0 */
1158 "tosshr0ax", /* tosshreax, sreg = 0 */
1159 "tosasr0ax", /* tosasreax, sreg = 0 */
1160 "tossub0ax", /* tossubeax, sreg = 0 */
1161 "lsubeqa", /* lsubeq, sreg = 0, x = 0 */
1162 "lsubeq1", /* lsubeq, sreg = 0, x = 0, a = 1 */
1163 "lsubeq0sp", /* lsubeqysp, y = 0 */
1164 "toslta0", /* tosltax, x = 0 */
1165 "tosudiv0ax", /* tosudiveax, sreg = 0 */
1166 "tosumod0ax", /* tosumodeax, sreg = 0 */
1167 "tosxor0ax", /* tosxoreax, sreg = 0 */
1168 "tosmoda0", /* tosmodax, x = 0 */
1169 "tosmula0", /* tosmulax, x = 0 */
1170 "tosumula0", /* tosumulax, x = 0 */
1171 "tosnea0", /* tosneax, x = 0 */
1172 "tosora0", /* tosorax, x = 0 */
1173 "push1", /* pushax, x = 0, a = 1 */
1174 "push2", /* pushax, x = 0, a = 2 */
1175 "push3", /* pushax, x = 0, a = 3 */
1176 "push4", /* pushax, x = 0, a = 4 */
1177 "push5", /* pushax, x = 0, a = 5 */
1178 "push6", /* pushax, x = 0, a = 6 */
1179 "push7", /* pushax, x = 0, a = 7 */
1180 "pushc0", /* pusha, a = 0 */
1181 "pushc1", /* pusha, a = 1 */
1182 "pushc2", /* pusha, a = 2 */
1183 "tosrsuba0", /* tosrsubax, x = 0 */
1184 "tosshla0", /* tosshlax, x = 0 */
1185 "tosasla0", /* tosaslax, x = 0 */
1186 "tosshra0", /* tosshrax, x = 0 */
1187 "tosasra0", /* tosasrax, x = 0 */
1188 "steax0sp", /* steaxsp, y = 0 */
1189 "tossuba0", /* tossubax, x = 0 */
1190 "subeq0sp", /* subeqysp, y = 0 */
1191 "tosudiva0", /* tosudivax, x = 0 */
1192 "tosugea0", /* tosugeax, x = 0 */
1193 "tosugta0", /* tosugtax, x = 0 */
1194 "tosulea0", /* tosuleax, x = 0 */
1195 "tosulta0", /* tosultax, x = 0 */
1196 "tosumoda0", /* tosumodax, x = 0 */
1197 "tosxora0", /* tosxorax, x = 0 */
1200 unsigned Changes = 0;
1203 /* Generate register info for the following step */
1206 /* Walk over the entries */
1208 while (I < CS_GetEntryCount (S)) {
1210 /* Get next entry */
1211 CodeEntry* E = CS_GetEntry (S, I);
1219 /* Free register info */
1222 /* Return the number of changes made */
1229 static unsigned OptSize2 (CodeSeg* S)
1230 /* Do size optimization by using shorter code sequences, even if this
1231 * introduces relations between instructions. This step must be one of the
1232 * last steps, because it makes further work much more difficult.
1235 unsigned Changes = 0;
1238 /* Generate register info for the following step */
1241 /* Walk over the entries */
1243 while (I < CS_GetEntryCount (S)) {
1246 /* Get next entry */
1247 CodeEntry* E = CS_GetEntry (S, I);
1249 /* Assume we have no replacement */
1252 /* Check the instruction */
1256 if (CE_KnownImm (E)) {
1257 short Val = (short) E->Num;
1258 if (Val == E->RI->In.RegX) {
1259 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
1260 } else if (Val == E->RI->In.RegY) {
1261 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
1262 } else if (E->RI->In.RegA >= 0 && CPU >= CPU_65C02) {
1263 if (Val == ((E->RI->In.RegA - 1) & 0xFF)) {
1264 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
1265 } else if (Val == ((E->RI->In.RegA + 1) & 0xFF)) {
1266 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
1273 if (CE_KnownImm (E)) {
1274 short Val = (short) E->Num;
1275 if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX - 1) & 0xFF)) {
1276 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
1277 } else if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX + 1) & 0xFF)) {
1278 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
1279 } else if (Val == E->RI->In.RegA) {
1280 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
1286 if (CE_KnownImm (E)) {
1287 short Val = (short) E->Num;
1288 if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY - 1) & 0xFF)) {
1289 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
1290 } else if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY + 1) & 0xFF)) {
1291 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
1292 } else if (Val == E->RI->In.RegA) {
1293 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
1299 /* Avoid gcc warnings */
1304 /* Insert the replacement if we have one */
1306 CS_InsertEntry (S, X, I+1);
1316 /* Free register info */
1319 /* Return the number of changes made */
1325 /*****************************************************************************/
1326 /* struct OptFunc */
1327 /*****************************************************************************/
1331 typedef struct OptFunc OptFunc;
1333 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1334 const char* Name; /* Name of the function/group */
1335 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1336 unsigned long TotalRuns; /* Total number of runs */
1337 unsigned long LastRuns; /* Last number of runs */
1338 unsigned long TotalChanges; /* Total number of changes */
1339 unsigned long LastChanges; /* Last number of changes */
1340 char Disabled; /* True if function disabled */
1345 /*****************************************************************************/
1347 /*****************************************************************************/
1351 /* Macro that builds a function description */
1352 #define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
1354 /* A list of all the function descriptions */
1355 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1356 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 60, 0, 0, 0, 0, 0 };
1357 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1358 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 40, 0, 0, 0, 0, 0 };
1359 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1360 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1361 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1362 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1363 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1364 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1365 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1366 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1367 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1368 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1369 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1370 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1371 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1372 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1373 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1374 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1375 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1376 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1377 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1378 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1379 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1380 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1381 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1382 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1383 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1384 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1385 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1386 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1387 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1388 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 100, 0, 0, 0, 0, 0 };
1389 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 100, 0, 0, 0, 0, 0 };
1390 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1391 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 100, 0, 0, 0, 0, 0 };
1392 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1393 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1394 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1395 /*static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };*/
1396 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1397 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1398 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1399 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1400 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1401 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1402 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0, 0 };
1403 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1404 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1407 /* Table containing all the steps in alphabetical order */
1408 static OptFunc* OptFuncs[] = {
1460 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1464 static int CmpOptStep (const void* Key, const void* Func)
1465 /* Compare function for bsearch */
1467 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1472 static OptFunc* FindOptFunc (const char* Name)
1473 /* Find an optimizer step by name in the table and return a pointer. Return
1474 * NULL if no such step is found.
1477 /* Search for the function in the list */
1478 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1484 static OptFunc* GetOptFunc (const char* Name)
1485 /* Find an optimizer step by name in the table and return a pointer. Print an
1486 * error and call AbEnd if not found.
1489 /* Search for the function in the list */
1490 OptFunc* F = FindOptFunc (Name);
1493 AbEnd ("Optimization step `%s' not found", Name);
1500 void DisableOpt (const char* Name)
1501 /* Disable the optimization with the given name */
1503 if (strcmp (Name, "any") == 0) {
1505 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1506 OptFuncs[I]->Disabled = 1;
1509 GetOptFunc(Name)->Disabled = 1;
1515 void EnableOpt (const char* Name)
1516 /* Enable the optimization with the given name */
1518 if (strcmp (Name, "any") == 0) {
1520 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1521 OptFuncs[I]->Disabled = 0;
1524 GetOptFunc(Name)->Disabled = 0;
1530 void ListOptSteps (FILE* F)
1531 /* List all optimization steps */
1534 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1535 fprintf (F, "%s\n", OptFuncs[I]->Name);
1541 static void ReadOptStats (const char* Name)
1542 /* Read the optimizer statistics file */
1547 /* Try to open the file */
1548 FILE* F = fopen (Name, "r");
1550 /* Ignore the error */
1554 /* Read and parse the lines */
1556 while (fgets (Buf, sizeof (Buf), F) != 0) {
1564 unsigned long TotalRuns;
1565 unsigned long TotalChanges;
1570 /* Remove trailing white space including the line terminator */
1573 while (Len > 0 && IsSpace (B[Len-1])) {
1578 /* Remove leading whitespace */
1579 while (IsSpace (*B)) {
1583 /* Check for empty and comment lines */
1584 if (*B == '\0' || *B == ';' || *B == '#') {
1588 /* Parse the line */
1589 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1594 /* Search for the optimizer step. */
1595 Func = FindOptFunc (Name);
1601 /* Found the step, set the fields */
1602 Func->TotalRuns = TotalRuns;
1603 Func->TotalChanges = TotalChanges;
1607 /* Close the file, ignore errors here. */
1613 static void WriteOptStats (const char* Name)
1614 /* Write the optimizer statistics file */
1618 /* Try to open the file */
1619 FILE* F = fopen (Name, "w");
1621 /* Ignore the error */
1625 /* Write the file */
1626 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1627 const OptFunc* O = OptFuncs[I];
1629 "%-20s %6lu %6lu %6lu %6lu\n",
1637 /* Close the file, ignore errors here. */
1643 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1644 /* Run one optimizer function Max times or until there are no more changes */
1646 unsigned Changes, C;
1648 /* Don't run the function if it is disabled or if it is prohibited by the
1651 if (F->Disabled || CodeSizeFactor < F->CodeSizeFactor) {
1655 /* Run this until there are no more changes */
1659 /* Run the function */
1666 F->TotalChanges += C;
1667 F->LastChanges += C;
1669 } while (--Max && C > 0);
1671 /* Return the number of changes */
1677 static unsigned RunOptGroup1 (CodeSeg* S)
1678 /* Run the first group of optimization steps. These steps translate known
1679 * patterns emitted by the code generator into more optimal patterns. Order
1680 * of the steps is important, because some of the steps done earlier cover
1681 * the same patterns as later steps as subpatterns.
1684 unsigned Changes = 0;
1686 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1687 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1688 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1689 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1690 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1691 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1692 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1693 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1694 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1695 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1696 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1697 Changes += RunOptFunc (S, &DOptAdd1, 1);
1698 Changes += RunOptFunc (S, &DOptAdd2, 1);
1699 Changes += RunOptFunc (S, &DOptShift1, 1);
1700 Changes += RunOptFunc (S, &DOptShift2, 1);
1702 /* Return the number of changes */
1708 static unsigned RunOptGroup2 (CodeSeg* S)
1709 /* Run one group of optimization steps. This step involves just decoupling
1710 * instructions by replacing them by instructions that do not depend on
1711 * previous instructions. This makes it easier to find instructions that
1715 unsigned Changes = 0;
1717 Changes += RunOptFunc (S, &DOptDecouple, 1);
1719 /* Return the number of changes */
1725 static unsigned RunOptGroup3 (CodeSeg* S)
1726 /* Run one group of optimization steps. These steps depend on each other,
1727 * that means that one step may allow another step to do additional work,
1728 * so we will repeat the steps as long as we see any changes.
1731 unsigned Changes, C;
1737 C += RunOptFunc (S, &DOptPtrLoad6, 1);
1738 C += RunOptFunc (S, &DOptNegA1, 1);
1739 C += RunOptFunc (S, &DOptNegA2, 1);
1740 C += RunOptFunc (S, &DOptSub1, 1);
1741 C += RunOptFunc (S, &DOptSub2, 1);
1742 C += RunOptFunc (S, &DOptAdd3, 1);
1743 C += RunOptFunc (S, &DOptStackOps, 1);
1744 C += RunOptFunc (S, &DOptJumpCascades, 1);
1745 C += RunOptFunc (S, &DOptDeadJumps, 1);
1746 C += RunOptFunc (S, &DOptRTS, 1);
1747 C += RunOptFunc (S, &DOptDeadCode, 1);
1748 C += RunOptFunc (S, &DOptJumpTarget, 1);
1749 C += RunOptFunc (S, &DOptCondBranches, 1);
1750 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1751 C += RunOptFunc (S, &DOptBoolTrans, 1);
1752 C += RunOptFunc (S, &DOptCmp1, 1);
1753 C += RunOptFunc (S, &DOptCmp2, 1);
1754 C += RunOptFunc (S, &DOptCmp3, 1);
1755 C += RunOptFunc (S, &DOptCmp4, 1);
1756 C += RunOptFunc (S, &DOptCmp5, 1);
1757 C += RunOptFunc (S, &DOptCmp6, 1);
1758 C += RunOptFunc (S, &DOptCmp7, 1);
1759 C += RunOptFunc (S, &DOptTest1, 1);
1760 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1761 C += RunOptFunc (S, &DOptUnusedStores, 1);
1762 C += RunOptFunc (S, &DOptDupLoads, 1);
1763 C += RunOptFunc (S, &DOptStoreLoad, 1);
1764 C += RunOptFunc (S, &DOptTransfers, 1);
1770 /* Return the number of changes */
1776 static unsigned RunOptGroup4 (CodeSeg* S)
1777 /* 65C02 specific optimizations. */
1779 unsigned Changes = 0;
1781 if (CPU >= CPU_65C02) {
1782 /* Replace (zp),y by (zp) if Y is zero. If we have changes, run register
1783 * load optimization again, since loads of Y may have become unnecessary.
1785 unsigned C = RunOptFunc (S, &DOpt65C02Ind, 1);
1788 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1792 /* Return the number of changes */
1798 static unsigned RunOptGroup5 (CodeSeg* S)
1799 /* Run another round of pattern replacements. These are done late, since there
1800 * may be better replacements before.
1803 unsigned Changes = 0;
1805 Changes += RunOptFunc (S, &DOptPush1, 1);
1807 /* Return the number of changes */
1813 static unsigned RunOptGroup6 (CodeSeg* S)
1814 /* The last group of optimization steps. Adjust branches, do size optimizations.
1817 unsigned Changes = 0;
1820 /* Optimize for size, that is replace operations by shorter ones, even
1821 * if this does hinder further optimizations (no problem since we're
1824 Changes += RunOptFunc (S, &DOptSize2, 1);
1826 /* Run the jump target optimization again, since the size optimization
1827 * above may have opened new oportunities.
1829 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1831 /* Adjust branch distances */
1832 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1834 /* Replace conditional branches to RTS. If we had changes, we must run dead
1835 * code elimination again, since the change may have introduced dead code.
1837 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1840 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1843 /* Return the number of changes */
1849 void RunOpt (CodeSeg* S)
1850 /* Run the optimizer */
1852 const char* StatFileName;
1854 /* If we shouldn't run the optimizer, bail out */
1859 /* Check if we are requested to write optimizer statistics */
1860 StatFileName = getenv ("CC65_OPTSTATS");
1862 ReadOptStats (StatFileName);
1865 /* Print the name of the function we are working on */
1867 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1869 Print (stdout, 1, "Running optimizer for global code segment\n");
1872 /* Run groups of optimizations */
1880 /* Write statistics */
1882 WriteOptStats (StatFileName);