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 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static unsigned OptShift1 (CodeSeg* S)
71 /* A call to the shlaxN routine may get replaced by one or more asl insns
72 * if the value of X is not used later.
77 /* Walk over the entries */
79 while (I < CS_GetEntryCount (S)) {
82 CodeEntry* E = CS_GetEntry (S, I);
84 /* Check for the sequence */
85 if (E->OPC == OP65_JSR &&
86 (strncmp (E->Arg, "shlax", 5) == 0 ||
87 strncmp (E->Arg, "aslax", 5) == 0) &&
88 strlen (E->Arg) == 6 &&
89 IsDigit (E->Arg[5]) &&
92 /* Insert shift insns */
93 unsigned Count = E->Arg[5] - '0';
95 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
96 CS_InsertEntry (S, X, I+1);
99 /* Delete the call to shlax */
102 /* Remember, we had changes */
112 /* Return the number of changes made */
118 static unsigned OptShift2 (CodeSeg* S)
119 /* A call to the shraxN routine may get replaced by one or more lsr insns
120 * if the value of X is not used later.
123 unsigned Changes = 0;
125 /* Walk over the entries */
127 while (I < CS_GetEntryCount (S)) {
130 CodeEntry* E = CS_GetEntry (S, I);
132 /* Check for the sequence */
133 if (E->OPC == OP65_JSR &&
134 strncmp (E->Arg, "shrax", 5) == 0 &&
135 strlen (E->Arg) == 6 &&
136 IsDigit (E->Arg[5]) &&
137 !RegXUsed (S, I+1)) {
139 /* Insert shift insns */
140 unsigned Count = E->Arg[5] - '0';
142 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
143 CS_InsertEntry (S, X, I+1);
146 /* Delete the call to shlax */
149 /* Remember, we had changes */
159 /* Return the number of changes made */
165 /*****************************************************************************/
166 /* Optimize stores through pointers */
167 /*****************************************************************************/
171 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
172 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
174 /* Check for a label attached to the entry */
175 if (CE_HasLabel (L[0])) {
179 /* Check for single insn sub ops */
180 if (L[0]->OPC == OP65_AND ||
181 L[0]->OPC == OP65_EOR ||
182 L[0]->OPC == OP65_ORA ||
183 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
184 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
189 } else if (L[0]->OPC == OP65_CLC &&
190 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
191 L[1]->OPC == OP65_ADC &&
192 !CE_HasLabel (L[1])) {
194 } else if (L[0]->OPC == OP65_SEC &&
195 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
196 L[1]->OPC == OP65_SBC &&
197 !CE_HasLabel (L[1])) {
209 static unsigned OptPtrStore1 (CodeSeg* S)
210 /* Search for the sequence:
231 unsigned Changes = 0;
233 /* Walk over the entries */
235 while (I < CS_GetEntryCount (S)) {
241 L[0] = CS_GetEntry (S, I);
243 /* Check for the sequence */
244 if (CE_IsCall (L[0], "pushax") &&
245 CS_GetEntries (S, L+1, I+1, 3) &&
246 L[1]->OPC == OP65_LDY &&
247 CE_KnownImm (L[1]) &&
248 !CE_HasLabel (L[1]) &&
249 CE_IsCall (L[2], "ldauidx") &&
250 !CE_HasLabel (L[2]) &&
251 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
252 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
253 L[3+K]->OPC == OP65_LDY &&
254 CE_KnownImm (L[3+K]) &&
255 !CE_HasLabel (L[3+K]) &&
256 CE_IsCall (L[4+K], "staspidx") &&
257 !CE_HasLabel (L[4+K])) {
261 /* Create and insert the stores */
262 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
263 CS_InsertEntry (S, X, I+1);
265 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
266 CS_InsertEntry (S, X, I+2);
268 /* Delete the call to pushax */
271 /* Delete the call to ldauidx */
272 CS_DelEntry (S, I+3);
274 /* Insert the load from ptr1 */
275 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
276 CS_InsertEntry (S, X, I+3);
277 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
278 CS_InsertEntry (S, X, I+4);
280 /* Insert the store through ptr1 */
281 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
282 CS_InsertEntry (S, X, I+6+K);
284 /* Delete the call to staspidx */
285 CS_DelEntry (S, I+7+K);
287 /* Remember, we had changes */
297 /* Return the number of changes made */
303 static unsigned OptPtrStore2 (CodeSeg* S)
304 /* Search for the sequence:
320 unsigned Changes = 0;
322 /* Walk over the entries */
324 while (I < CS_GetEntryCount (S)) {
329 L[0] = CS_GetEntry (S, I);
331 /* Check for the sequence */
332 if (CE_IsCall (L[0], "pushax") &&
333 CS_GetEntries (S, L+1, I+1, 3) &&
334 L[1]->OPC == OP65_LDA &&
335 !CE_HasLabel (L[1]) &&
336 L[2]->OPC == OP65_LDY &&
337 !CE_HasLabel (L[2]) &&
338 CE_IsCall (L[3], "staspidx") &&
339 !CE_HasLabel (L[3])) {
343 /* Create and insert the stores */
344 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
345 CS_InsertEntry (S, X, I+1);
347 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
348 CS_InsertEntry (S, X, I+2);
350 /* Delete the call to pushax */
353 /* Insert the store through ptr1 */
354 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
355 CS_InsertEntry (S, X, I+4);
357 /* Delete the call to staspidx */
358 CS_DelEntry (S, I+5);
360 /* Remember, we had changes */
370 /* Return the number of changes made */
376 /*****************************************************************************/
377 /* Optimize loads through pointers */
378 /*****************************************************************************/
382 static unsigned OptPtrLoad1 (CodeSeg* S)
383 /* Search for the sequence:
387 * lda (sp),y # May be any destination
402 unsigned Changes = 0;
404 /* Walk over the entries */
406 while (I < CS_GetEntryCount (S)) {
411 L[0] = CS_GetEntry (S, I);
413 /* Check for the sequence */
414 if (L[0]->OPC == OP65_TAX &&
415 CS_GetEntries (S, L+1, I+1, 4) &&
416 L[1]->OPC == OP65_DEY &&
417 !CE_HasLabel (L[1]) &&
418 L[2]->OPC == OP65_LDA &&
419 !CE_HasLabel (L[2]) &&
420 L[3]->OPC == OP65_LDY &&
421 !CE_HasLabel (L[3]) &&
422 CE_IsCall (L[4], "ldauidx") &&
423 !CE_HasLabel (L[4])) {
427 /* Store the high byte and remove the TAX instead */
428 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
429 CS_InsertEntry (S, X, I+1);
432 /* Store the low byte */
433 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
434 CS_InsertEntry (S, X, I+3);
436 /* Delete the call to ldauidx */
437 CS_DelEntry (S, I+5);
439 /* Load high and low byte */
440 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
441 CS_InsertEntry (S, X, I+5);
442 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
443 CS_InsertEntry (S, X, I+6);
445 /* Remember, we had changes */
455 /* Return the number of changes made */
461 static unsigned OptPtrLoad2 (CodeSeg* S)
462 /* Search for the sequence:
487 unsigned Changes = 0;
489 /* Walk over the entries */
491 while (I < CS_GetEntryCount (S)) {
496 L[0] = CS_GetEntry (S, I);
498 /* Check for the sequence */
499 if (L[0]->OPC == OP65_CLC &&
500 CS_GetEntries (S, L+1, I+1, 8) &&
501 L[1]->OPC == OP65_ADC &&
502 !CE_HasLabel (L[1]) &&
503 L[2]->OPC == OP65_TAY &&
504 !CE_HasLabel (L[2]) &&
505 L[3]->OPC == OP65_TXA &&
506 !CE_HasLabel (L[3]) &&
507 L[4]->OPC == OP65_ADC &&
508 !CE_HasLabel (L[4]) &&
509 L[5]->OPC == OP65_TAX &&
510 !CE_HasLabel (L[5]) &&
511 L[6]->OPC == OP65_TYA &&
512 !CE_HasLabel (L[6]) &&
513 L[7]->OPC == OP65_LDY &&
514 !CE_HasLabel (L[7]) &&
515 CE_IsCall (L[8], "ldauidx") &&
516 !CE_HasLabel (L[8])) {
521 /* Store the low byte and remove the TAY instead */
522 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
523 CS_InsertEntry (S, X, I+2);
524 CS_DelEntry (S, I+3);
526 /* Store the high byte */
527 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
528 CS_InsertEntry (S, X, I+5);
530 /* If the instruction before the adc is a ldx, replace the
531 * txa by and lda with the same location of the ldx.
533 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
534 P->OPC == OP65_LDX &&
537 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
538 CS_InsertEntry (S, X, I+4);
539 CS_DelEntry (S, I+3);
542 /* Delete more transfer insns */
543 CS_DelEntry (S, I+7);
544 CS_DelEntry (S, I+6);
546 /* Delete the call to ldauidx */
547 CS_DelEntry (S, I+7);
549 /* Load high and low byte */
550 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
551 CS_InsertEntry (S, X, I+7);
552 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
553 CS_InsertEntry (S, X, I+8);
555 /* Remember, we had changes */
565 /* Return the number of changes made */
571 static unsigned OptPtrLoad3 (CodeSeg* S)
572 /* Search for the sequence:
597 unsigned Changes = 0;
599 /* Walk over the entries */
601 while (I < CS_GetEntryCount (S)) {
606 L[0] = CS_GetEntry (S, I);
608 /* Check for the sequence */
609 if (L[0]->OPC == OP65_ADC &&
610 CS_GetEntries (S, L+1, I+1, 8) &&
611 L[1]->OPC == OP65_PHA &&
612 !CE_HasLabel (L[1]) &&
613 L[2]->OPC == OP65_TXA &&
614 !CE_HasLabel (L[2]) &&
615 L[3]->OPC == OP65_INY &&
616 !CE_HasLabel (L[3]) &&
617 L[4]->OPC == OP65_ADC &&
618 !CE_HasLabel (L[4]) &&
619 L[5]->OPC == OP65_TAX &&
620 !CE_HasLabel (L[5]) &&
621 L[6]->OPC == OP65_PLA &&
622 !CE_HasLabel (L[6]) &&
623 L[7]->OPC == OP65_LDY &&
624 !CE_HasLabel (L[7]) &&
625 CE_IsCall (L[8], "ldauidx") &&
626 !CE_HasLabel (L[8])) {
630 /* Store the low byte and remove the PHA instead */
631 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
632 CS_InsertEntry (S, X, I+1);
633 CS_DelEntry (S, I+2);
635 /* Store the high byte */
636 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
637 CS_InsertEntry (S, X, I+5);
639 /* Delete more transfer and PLA insns */
640 CS_DelEntry (S, I+7);
641 CS_DelEntry (S, I+6);
643 /* Delete the call to ldauidx */
644 CS_DelEntry (S, I+7);
646 /* Load high and low byte */
647 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
648 CS_InsertEntry (S, X, I+7);
649 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
650 CS_InsertEntry (S, X, I+8);
652 /* Remember, we had changes */
662 /* Return the number of changes made */
668 static unsigned OptPtrLoad4 (CodeSeg* S)
669 /* Search for the sequence:
687 unsigned Changes = 0;
689 /* Walk over the entries */
691 while (I < CS_GetEntryCount (S)) {
697 L[0] = CS_GetEntry (S, I);
699 /* Check for the sequence */
700 if (L[0]->OPC == OP65_LDA &&
701 L[0]->AM == AM65_IMM &&
702 CS_GetEntries (S, L+1, I+1, 7) &&
703 L[1]->OPC == OP65_LDX &&
704 L[1]->AM == AM65_IMM &&
705 !CE_HasLabel (L[1]) &&
706 L[2]->OPC == OP65_CLC &&
707 !CE_HasLabel (L[2]) &&
708 L[3]->OPC == OP65_ADC &&
709 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
710 !CE_HasLabel (L[3]) &&
711 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
713 L[4]->JumpTo->Owner == L[6] &&
714 !CE_HasLabel (L[4]) &&
715 L[5]->OPC == OP65_INX &&
716 !CE_HasLabel (L[5]) &&
717 L[6]->OPC == OP65_LDY &&
718 CE_KnownImm (L[6]) &&
720 CE_IsCall (L[7], "ldauidx") &&
721 !CE_HasLabel (L[7]) &&
722 /* Check the label last because this is quite costly */
723 (Len = strlen (L[0]->Arg)) > 3 &&
724 L[0]->Arg[0] == '<' &&
725 L[0]->Arg[1] == '(' &&
726 strlen (L[1]->Arg) == Len &&
727 L[1]->Arg[0] == '>' &&
728 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
733 /* We will create all the new stuff behind the current one so
734 * we keep the line references.
736 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
737 CS_InsertEntry (S, X, I+8);
739 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
740 CS_InsertEntry (S, X, I+9);
742 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
744 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
745 CS_InsertEntry (S, X, I+10);
748 /* Remove the old code */
749 CS_DelEntries (S, I, 8);
751 /* Remember, we had changes */
761 /* Return the number of changes made */
767 static unsigned OptPtrLoad5 (CodeSeg* S)
768 /* Search for the sequence:
789 unsigned Changes = 0;
791 /* Walk over the entries */
793 while (I < CS_GetEntryCount (S)) {
799 L[0] = CS_GetEntry (S, I);
801 /* Check for the sequence */
802 if (L[0]->OPC == OP65_LDA &&
803 L[0]->AM == AM65_IMM &&
804 CS_GetEntries (S, L+1, I+1, 8) &&
805 L[1]->OPC == OP65_LDX &&
806 L[1]->AM == AM65_IMM &&
807 !CE_HasLabel (L[1]) &&
808 L[2]->OPC == OP65_LDY &&
809 CE_KnownImm (L[2]) &&
810 !CE_HasLabel (L[2]) &&
811 L[3]->OPC == OP65_CLC &&
812 !CE_HasLabel (L[3]) &&
813 L[4]->OPC == OP65_ADC &&
814 L[4]->AM == AM65_ZP_INDY &&
815 !CE_HasLabel (L[4]) &&
816 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
818 L[5]->JumpTo->Owner == L[7] &&
819 !CE_HasLabel (L[5]) &&
820 L[6]->OPC == OP65_INX &&
821 !CE_HasLabel (L[6]) &&
822 L[7]->OPC == OP65_LDY &&
823 CE_KnownImm (L[7]) &&
825 CE_IsCall (L[8], "ldauidx") &&
826 !CE_HasLabel (L[8]) &&
827 /* Check the label last because this is quite costly */
828 (Len = strlen (L[0]->Arg)) > 3 &&
829 L[0]->Arg[0] == '<' &&
830 L[0]->Arg[1] == '(' &&
831 strlen (L[1]->Arg) == Len &&
832 L[1]->Arg[0] == '>' &&
833 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
839 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
840 CS_InsertEntry (S, X, I+3);
843 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
844 CS_InsertEntry (S, X, I+4);
847 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
848 CS_InsertEntry (S, X, I+5);
851 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
853 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
854 CS_InsertEntry (S, X, I+6);
857 /* Remove the old code */
858 CS_DelEntries (S, I, 2);
859 CS_DelEntries (S, I+5, 6);
861 /* Remember, we had changes */
871 /* Return the number of changes made */
877 static unsigned OptPtrLoad6 (CodeSeg* S)
878 /* Search for the sequence
891 * This step must be execute *after* OptPtrLoad1!
894 unsigned Changes = 0;
896 /* Walk over the entries */
898 while (I < CS_GetEntryCount (S)) {
903 L[0] = CS_GetEntry (S, I);
905 /* Check for the sequence */
906 if (L[0]->OPC == OP65_LDY &&
907 CS_GetEntries (S, L+1, I+1, 1) &&
908 CE_IsCall (L[1], "ldauidx") &&
909 !CE_HasLabel (L[1])) {
913 /* Store the high byte */
914 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
915 CS_InsertEntry (S, X, I+1);
917 /* Store the low byte */
918 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
919 CS_InsertEntry (S, X, I+2);
921 /* Delete the call to ldauidx */
922 CS_DelEntry (S, I+3);
924 /* Load the high and low byte */
925 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
926 CS_InsertEntry (S, X, I+3);
927 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
928 CS_InsertEntry (S, X, I+4);
930 /* Remember, we had changes */
940 /* Return the number of changes made */
946 /*****************************************************************************/
947 /* Decouple operations */
948 /*****************************************************************************/
952 static unsigned OptDecouple (CodeSeg* S)
953 /* Decouple operations, that is, do the following replacements:
963 * lda sreg -> lda #imm
964 * ldx sreg -> ldx #imm
965 * ldy sreg -> ldy #imm
967 * Provided that the register values are known of course.
970 unsigned Changes = 0;
973 /* Generate register info for the following step */
976 /* Walk over the entries */
978 while (I < CS_GetEntryCount (S)) {
983 CodeEntry* E = CS_GetEntry (S, I);
985 /* Assume we have no replacement */
988 /* Check the instruction */
992 if (E->RI->In.RegX >= 0) {
993 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX - 1) & 0xFF);
994 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
999 if (E->RI->In.RegY >= 0) {
1000 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY - 1) & 0xFF);
1001 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1006 if (E->RI->In.RegX >= 0) {
1007 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX + 1) & 0xFF);
1008 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1013 if (E->RI->In.RegY >= 0) {
1014 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY + 1) & 0xFF);
1015 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1020 if (E->AM == AM65_ZP) {
1021 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1022 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1023 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1024 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1025 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1026 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1032 if (E->AM == AM65_ZP) {
1033 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1034 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1035 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1036 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1037 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1038 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1044 if (E->AM == AM65_ZP) {
1045 if ((E->Use & REG_SREG_LO) != 0 && E->RI->In.SRegLo >= 0) {
1046 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegLo);
1047 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1048 } else if ((E->Use & REG_SREG_HI) != 0 && E->RI->In.SRegHi >= 0) {
1049 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.SRegHi);
1050 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1056 if (E->RI->In.RegA >= 0) {
1057 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1058 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1063 if (E->RI->In.RegA >= 0) {
1064 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1065 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1070 if (E->RI->In.RegX >= 0) {
1071 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegX);
1072 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1077 if (E->RI->In.RegY >= 0) {
1078 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegY);
1079 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1084 /* Avoid gcc warnings */
1089 /* Insert the replacement if we have one */
1091 CS_InsertEntry (S, X, I+1);
1101 /* Free register info */
1104 /* Return the number of changes made */
1110 /*****************************************************************************/
1111 /* Size optimization */
1112 /*****************************************************************************/
1116 static unsigned OptSize1 (CodeSeg* S)
1117 /* Do size optimization by calling special subroutines that preload registers.
1118 * This routine does not work standalone, it needs a following register load
1123 static const char* Func = {
1124 "stax0sp", /* staxysp, y = 0 */
1126 "ldax0sp", /* ldaxysp, y = 1 */
1127 "ldeax0sp", /* ldeaxysp, y = 3 */
1128 "push0", /* pushax, a = 0, x = 0 */
1129 "pusha0", /* pushax, x = 0 */
1130 "pushaFF", /* pushax, x = ff */
1131 "pusha0sp", /* pushaysp, y = 0 */
1132 "tosadda0", /* tosaddax, x = 0 */
1133 "tosanda0", /* tosandax, x = 0 */
1134 "tosdiva0", /* tosdivax, x = 0 */
1135 "toseqa0", /* toseqax, x = 0 */
1136 "tosgea0", /* tosgeax, x = 0 */
1137 "tosgta0", /* tosgtax, x = 0 */
1138 "tosadd0ax", /* tosaddeax, sreg = 0 */
1139 "laddeqa", /* laddeq, sreg = 0, x = 0 */
1140 "laddeq1", /* laddeq, sreg = 0, x = 0, a = 1 */
1141 "laddeq0sp", /* laddeqysp, y = 0 */
1142 "tosand0ax", /* tosandeax, sreg = 0 */
1143 "ldaxi", /* ldaxidx, y = 1 */
1144 "ldeaxi", /* ldeaxidx, y = 3 */
1145 "ldeax0sp", /* ldeaxysp, y = 3 */
1146 "tosdiv0ax", /* tosdiveax, sreg = 0 */
1147 "toslea0", /* tosleax, x = 0 */
1148 "tosmod0ax", /* tosmodeax, sreg = 0 */
1149 "tosmul0ax", /* tosmuleax, sreg = 0 */
1150 "tosumul0ax", /* tosumuleax, sreg = 0 */
1151 "tosor0ax", /* tosoreax, sreg = 0 */
1152 "push0ax", /* pusheax, sreg = 0 */
1153 "tosrsub0ax", /* tosrsubeax, sreg = 0 */
1154 "tosshl0ax", /* tosshleax, sreg = 0 */
1155 "tosasl0ax", /* tosasleax, sreg = 0 */
1156 "tosshr0ax", /* tosshreax, sreg = 0 */
1157 "tosasr0ax", /* tosasreax, sreg = 0 */
1158 "tossub0ax", /* tossubeax, sreg = 0 */
1159 "lsubeqa", /* lsubeq, sreg = 0, x = 0 */
1160 "lsubeq1", /* lsubeq, sreg = 0, x = 0, a = 1 */
1161 "lsubeq0sp", /* lsubeqysp, y = 0 */
1162 "toslta0", /* tosltax, x = 0 */
1163 "tosudiv0ax", /* tosudiveax, sreg = 0 */
1164 "tosumod0ax", /* tosumodeax, sreg = 0 */
1165 "tosxor0ax", /* tosxoreax, sreg = 0 */
1166 "tosmoda0", /* tosmodax, x = 0 */
1167 "tosmula0", /* tosmulax, x = 0 */
1168 "tosumula0", /* tosumulax, x = 0 */
1169 "tosnea0", /* tosneax, x = 0 */
1170 "tosora0", /* tosorax, x = 0 */
1171 "push1", /* pushax, x = 0, a = 1 */
1172 "push2", /* pushax, x = 0, a = 2 */
1173 "push3", /* pushax, x = 0, a = 3 */
1174 "push4", /* pushax, x = 0, a = 4 */
1175 "push5", /* pushax, x = 0, a = 5 */
1176 "push6", /* pushax, x = 0, a = 6 */
1177 "push7", /* pushax, x = 0, a = 7 */
1178 "pushc0", /* pusha, a = 0 */
1179 "pushc1", /* pusha, a = 1 */
1180 "pushc2", /* pusha, a = 2 */
1181 "tosrsuba0", /* tosrsubax, x = 0 */
1182 "tosshla0", /* tosshlax, x = 0 */
1183 "tosasla0", /* tosaslax, x = 0 */
1184 "tosshra0", /* tosshrax, x = 0 */
1185 "tosasra0", /* tosasrax, x = 0 */
1186 "steax0sp", /* steaxsp, y = 0 */
1187 "tossuba0", /* tossubax, x = 0 */
1188 "subeq0sp", /* subeqysp, y = 0 */
1189 "tosudiva0", /* tosudivax, x = 0 */
1190 "tosugea0", /* tosugeax, x = 0 */
1191 "tosugta0", /* tosugtax, x = 0 */
1192 "tosulea0", /* tosuleax, x = 0 */
1193 "tosulta0", /* tosultax, x = 0 */
1194 "tosumoda0", /* tosumodax, x = 0 */
1195 "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 */
1227 static unsigned OptSize2 (CodeSeg* S)
1228 /* Do size optimization by using shorter code sequences, even if this
1229 * introduces relations between instructions. This step must be one of the
1230 * last steps, because it makes further work much more difficult.
1233 unsigned Changes = 0;
1236 /* Generate register info for the following step */
1239 /* Walk over the entries */
1241 while (I < CS_GetEntryCount (S)) {
1244 /* Get next entry */
1245 CodeEntry* E = CS_GetEntry (S, I);
1247 /* Assume we have no replacement */
1250 /* Check the instruction */
1254 if (CE_KnownImm (E)) {
1255 short Val = (short) E->Num;
1256 if (Val == E->RI->In.RegX) {
1257 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
1258 } else if (Val == E->RI->In.RegY) {
1259 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
1260 } else if (E->RI->In.RegA >= 0 && CPU >= CPU_65C02) {
1261 if (Val == ((E->RI->In.RegA - 1) & 0xFF)) {
1262 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
1263 } else if (Val == ((E->RI->In.RegA + 1) & 0xFF)) {
1264 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
1271 if (CE_KnownImm (E)) {
1272 short Val = (short) E->Num;
1273 if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX - 1) & 0xFF)) {
1274 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
1275 } else if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX + 1) & 0xFF)) {
1276 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
1277 } else if (Val == E->RI->In.RegA) {
1278 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
1284 if (CE_KnownImm (E)) {
1285 short Val = (short) E->Num;
1286 if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY - 1) & 0xFF)) {
1287 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
1288 } else if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY + 1) & 0xFF)) {
1289 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
1290 } else if (Val == E->RI->In.RegA) {
1291 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
1297 /* Avoid gcc warnings */
1302 /* Insert the replacement if we have one */
1304 CS_InsertEntry (S, X, I+1);
1314 /* Free register info */
1317 /* Return the number of changes made */
1323 /*****************************************************************************/
1324 /* struct OptFunc */
1325 /*****************************************************************************/
1329 typedef struct OptFunc OptFunc;
1331 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1332 const char* Name; /* Name of the function/group */
1333 unsigned long TotalRuns; /* Total number of runs */
1334 unsigned long LastRuns; /* Last number of runs */
1335 unsigned long TotalChanges; /* Total number of changes */
1336 unsigned long LastChanges; /* Last number of changes */
1337 char Disabled; /* True if function disabled */
1342 /*****************************************************************************/
1344 /*****************************************************************************/
1348 /* Macro that builds a function description */
1349 #define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
1351 /* A list of all the function descriptions */
1352 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 0, 0, 0, 0, 0 };
1353 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 0, 0, 0, 0, 0 };
1354 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 0, 0, 0, 0, 0 };
1355 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 0, 0, 0, 0, 0 };
1356 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0 };
1357 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 0, 0, 0, 0, 0 };
1358 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 0, 0, 0, 0, 0 };
1359 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 0, 0, 0, 0, 0 };
1360 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 0, 0, 0, 0, 0 };
1361 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 0, 0, 0, 0, 0 };
1362 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 0, 0, 0, 0, 0 };
1363 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 0, 0, 0, 0, 0 };
1364 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 0, 0, 0, 0, 0 };
1365 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 0, 0, 0, 0, 0 };
1366 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 0, 0, 0, 0, 0 };
1367 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 0, 0, 0, 0, 0 };
1368 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0 };
1369 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 0, 0, 0, 0, 0 };
1370 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 0, 0, 0, 0, 0 };
1371 static OptFunc DOptRTS = { OptRTS, "OptRTS", 0, 0, 0, 0, 0 };
1372 static OptFunc DOptRTSJumps = { OptRTSJumps, "OptRTSJumps", 0, 0, 0, 0, 0 };
1373 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 0, 0, 0, 0, 0 };
1374 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 0, 0, 0, 0, 0 };
1375 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 0, 0, 0, 0, 0 };
1376 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 0, 0, 0, 0, 0 };
1377 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 0, 0, 0, 0, 0 };
1378 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 0, 0, 0, 0, 0 };
1379 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 0, 0, 0, 0, 0 };
1380 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 0, 0, 0, 0, 0 };
1381 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 0, 0, 0, 0, 0 };
1382 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 0, 0, 0, 0, 0 };
1383 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 0, 0, 0, 0, 0 };
1384 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 0, 0, 0, 0, 0 };
1385 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 0, 0, 0, 0, 0 };
1386 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 0, 0, 0, 0, 0 };
1387 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 0, 0, 0, 0, 0 };
1388 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 0, 0, 0, 0, 0 };
1389 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 0, 0, 0, 0, 0 };
1390 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 0, 0, 0, 0, 0 };
1391 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 0, 0, 0, 0, 0 };
1392 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0 };
1393 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 0, 0, 0, 0, 0 };
1394 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 0, 0, 0, 0, 0 };
1395 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 0, 0, 0, 0, 0 };
1396 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0 };
1397 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0 };
1398 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0 };
1401 /* Table containing all the steps in alphabetical order */
1402 static OptFunc* OptFuncs[] = {
1451 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1455 static int CmpOptStep (const void* Key, const void* Func)
1456 /* Compare function for bsearch */
1458 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1463 static OptFunc* FindOptFunc (const char* Name)
1464 /* Find an optimizer step by name in the table and return a pointer. Return
1465 * NULL if no such step is found.
1468 /* Search for the function in the list */
1469 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1475 static OptFunc* GetOptFunc (const char* Name)
1476 /* Find an optimizer step by name in the table and return a pointer. Print an
1477 * error and call AbEnd if not found.
1480 /* Search for the function in the list */
1481 OptFunc* F = FindOptFunc (Name);
1484 AbEnd ("Optimization step `%s' not found", Name);
1491 void DisableOpt (const char* Name)
1492 /* Disable the optimization with the given name */
1494 if (strcmp (Name, "any") == 0) {
1496 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1497 OptFuncs[I]->Disabled = 1;
1500 GetOptFunc(Name)->Disabled = 1;
1506 void EnableOpt (const char* Name)
1507 /* Enable the optimization with the given name */
1509 if (strcmp (Name, "any") == 0) {
1511 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1512 OptFuncs[I]->Disabled = 0;
1515 GetOptFunc(Name)->Disabled = 0;
1521 void ListOptSteps (FILE* F)
1522 /* List all optimization steps */
1525 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1526 fprintf (F, "%s\n", OptFuncs[I]->Name);
1532 static void ReadOptStats (const char* Name)
1533 /* Read the optimizer statistics file */
1538 /* Try to open the file */
1539 FILE* F = fopen (Name, "r");
1541 /* Ignore the error */
1545 /* Read and parse the lines */
1547 while (fgets (Buf, sizeof (Buf), F) != 0) {
1555 unsigned long TotalRuns;
1556 unsigned long TotalChanges;
1561 /* Remove trailing white space including the line terminator */
1564 while (Len > 0 && IsSpace (B[Len-1])) {
1569 /* Remove leading whitespace */
1570 while (IsSpace (*B)) {
1574 /* Check for empty and comment lines */
1575 if (*B == '\0' || *B == ';' || *B == '#') {
1579 /* Parse the line */
1580 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1585 /* Search for the optimizer step. */
1586 Func = FindOptFunc (Name);
1592 /* Found the step, set the fields */
1593 Func->TotalRuns = TotalRuns;
1594 Func->TotalChanges = TotalChanges;
1598 /* Close the file, ignore errors here. */
1604 static void WriteOptStats (const char* Name)
1605 /* Write the optimizer statistics file */
1609 /* Try to open the file */
1610 FILE* F = fopen (Name, "w");
1612 /* Ignore the error */
1616 /* Write the file */
1617 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1618 const OptFunc* O = OptFuncs[I];
1620 "%-20s %6lu %6lu %6lu %6lu\n",
1628 /* Close the file, ignore errors here. */
1634 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1635 /* Run one optimizer function Max times or until there are no more changes */
1637 unsigned Changes, C;
1639 /* Don't run the function if it is disabled */
1644 /* Run this until there are no more changes */
1648 /* Run the function */
1655 F->TotalChanges += C;
1656 F->LastChanges += C;
1658 } while (--Max && C > 0);
1660 /* Return the number of changes */
1666 static void RunOptGroup1 (CodeSeg* S)
1667 /* Run the first group of optimization steps. These steps translate known
1668 * patterns emitted by the code generator into more optimal patterns. Order
1669 * of the steps is important, because some of the steps done earlier cover
1670 * the same patterns as later steps as subpatterns.
1673 RunOptFunc (S, &DOptPtrStore1, 1);
1674 RunOptFunc (S, &DOptPtrStore2, 1);
1675 RunOptFunc (S, &DOptPtrLoad1, 1);
1676 RunOptFunc (S, &DOptPtrLoad2, 1);
1677 RunOptFunc (S, &DOptPtrLoad3, 1);
1678 RunOptFunc (S, &DOptPtrLoad4, 1);
1679 RunOptFunc (S, &DOptPtrLoad5, 1);
1680 RunOptFunc (S, &DOptNegAX1, 1);
1681 RunOptFunc (S, &DOptNegAX2, 1);
1682 RunOptFunc (S, &DOptNegAX3, 1);
1683 RunOptFunc (S, &DOptNegAX4, 1);
1684 RunOptFunc (S, &DOptAdd1, 1);
1685 RunOptFunc (S, &DOptAdd2, 1);
1686 RunOptFunc (S, &DOptShift1, 1);
1687 RunOptFunc (S, &DOptShift2, 1);
1692 static void RunOptGroup2 (CodeSeg* S)
1693 /* Run one group of optimization steps. This step involves just decoupling
1694 * instructions by replacing them by instructions that do not depend on
1695 * previous instructions. This makes it easier to find instructions that
1699 RunOptFunc (S, &DOptDecouple, 1);
1705 static void RunOptGroup3 (CodeSeg* S)
1706 /* Run one group of optimization steps. These steps depend on each other,
1707 * that means that one step may allow another step to do additional work,
1708 * so we will repeat the steps as long as we see any changes.
1716 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1717 Changes += RunOptFunc (S, &DOptNegA1, 1);
1718 Changes += RunOptFunc (S, &DOptNegA2, 1);
1719 Changes += RunOptFunc (S, &DOptSub1, 1);
1720 Changes += RunOptFunc (S, &DOptSub2, 1);
1721 Changes += RunOptFunc (S, &DOptAdd3, 1);
1722 Changes += RunOptFunc (S, &DOptStackOps, 1);
1723 Changes += RunOptFunc (S, &DOptJumpCascades, 1);
1724 Changes += RunOptFunc (S, &DOptDeadJumps, 1);
1725 Changes += RunOptFunc (S, &DOptRTS, 1);
1726 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1727 Changes += RunOptFunc (S, &DOptJumpTarget, 1);
1728 Changes += RunOptFunc (S, &DOptCondBranches, 1);
1729 Changes += RunOptFunc (S, &DOptRTSJumps, 1);
1730 Changes += RunOptFunc (S, &DOptBoolTrans, 1);
1731 Changes += RunOptFunc (S, &DOptCmp1, 1);
1732 Changes += RunOptFunc (S, &DOptCmp2, 1);
1733 Changes += RunOptFunc (S, &DOptCmp3, 1);
1734 Changes += RunOptFunc (S, &DOptCmp4, 1);
1735 Changes += RunOptFunc (S, &DOptCmp5, 1);
1736 Changes += RunOptFunc (S, &DOptCmp6, 1);
1737 Changes += RunOptFunc (S, &DOptCmp7, 1);
1738 Changes += RunOptFunc (S, &DOptTest1, 1);
1739 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1740 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1741 Changes += RunOptFunc (S, &DOptDupLoads, 1);
1742 Changes += RunOptFunc (S, &DOptStoreLoad, 1);
1743 Changes += RunOptFunc (S, &DOptTransfers, 1);
1750 static void RunOptGroup4 (CodeSeg* S)
1751 /* The last group of optimization steps. Adjust branches.
1754 /* Optimize for size, that is replace operations by shorter ones, even
1755 * if this does hinder further optimizations (no problem since we're
1758 RunOptFunc (S, &DOptSize2, 1);
1760 /* Run the jump target optimization again, since the size optimization
1761 * above may have opened new oportunities.
1763 RunOptFunc (S, &DOptJumpTarget, 5);
1765 /* Finally, adjust branch distances */
1766 RunOptFunc (S, &DOptBranchDist, 3);
1771 void RunOpt (CodeSeg* S)
1772 /* Run the optimizer */
1774 const char* StatFileName;
1776 /* If we shouldn't run the optimizer, bail out */
1781 /* Check if we are requested to write optimizer statistics */
1782 StatFileName = getenv ("CC65_OPTSTATS");
1784 ReadOptStats (StatFileName);
1787 /* Print the name of the function we are working on */
1789 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1791 Print (stdout, 1, "Running optimizer for global code segment\n");
1794 /* Run groups of optimizations */
1800 /* Write statistics */
1802 WriteOptStats (StatFileName);