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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static unsigned OptShift1 (CodeSeg* S)
70 /* A call to the shlaxN routine may get replaced by one or more asl insns
71 * if the value of X is not used later.
76 /* Walk over the entries */
78 while (I < CS_GetEntryCount (S)) {
81 CodeEntry* E = CS_GetEntry (S, I);
83 /* Check for the sequence */
84 if (E->OPC == OP65_JSR &&
85 (strncmp (E->Arg, "shlax", 5) == 0 ||
86 strncmp (E->Arg, "aslax", 5) == 0) &&
87 strlen (E->Arg) == 6 &&
88 IsDigit (E->Arg[5]) &&
91 /* Insert shift insns */
92 unsigned Count = E->Arg[5] - '0';
94 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
95 CS_InsertEntry (S, X, I+1);
98 /* Delete the call to shlax */
101 /* Remember, we had changes */
111 /* Return the number of changes made */
117 /*****************************************************************************/
118 /* nega optimizations */
119 /*****************************************************************************/
123 static unsigned OptNegA1 (CodeSeg* S)
130 * Remove the ldx if the lda does not use it.
133 unsigned Changes = 0;
135 /* Walk over the entries */
137 while (I < CS_GetEntryCount (S)) {
142 CodeEntry* E = CS_GetEntry (S, I);
144 /* Check for a ldx */
145 if (E->OPC == OP65_LDX &&
147 (E->Flags & CEF_NUMARG) != 0 &&
149 CS_GetEntries (S, L, I+1, 2) &&
150 L[0]->OPC == OP65_LDA &&
151 (L[0]->Use & REG_X) == 0 &&
152 !CE_HasLabel (L[0]) &&
153 CE_IsCall (L[1], "bnega") &&
154 !CE_HasLabel (L[1])) {
156 /* Remove the ldx instruction */
159 /* Remember, we had changes */
169 /* Return the number of changes made */
175 static unsigned OptNegA2 (CodeSeg* S)
182 * Adjust the conditional branch and remove the call to the subroutine.
185 unsigned Changes = 0;
187 /* Walk over the entries */
189 while (I < CS_GetEntryCount (S)) {
194 CodeEntry* E = CS_GetEntry (S, I);
196 /* Check for the sequence */
197 if ((E->OPC == OP65_ADC ||
198 E->OPC == OP65_AND ||
199 E->OPC == OP65_DEA ||
200 E->OPC == OP65_EOR ||
201 E->OPC == OP65_INA ||
202 E->OPC == OP65_LDA ||
203 E->OPC == OP65_ORA ||
204 E->OPC == OP65_PLA ||
205 E->OPC == OP65_SBC ||
206 E->OPC == OP65_TXA ||
207 E->OPC == OP65_TYA) &&
208 CS_GetEntries (S, L, I+1, 2) &&
209 CE_IsCall (L[0], "bnega") &&
210 !CE_HasLabel (L[0]) &&
211 (L[1]->Info & OF_ZBRA) != 0 &&
212 !CE_HasLabel (L[1])) {
214 /* Invert the branch */
215 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
217 /* Delete the subroutine call */
218 CS_DelEntry (S, I+1);
220 /* Remember, we had changes */
230 /* Return the number of changes made */
236 /*****************************************************************************/
237 /* negax optimizations */
238 /*****************************************************************************/
242 static unsigned OptNegAX1 (CodeSeg* S)
243 /* On a call to bnegax, if X is zero, the result depends only on the value in
244 * A, so change the call to a call to bnega. This will get further optimized
248 unsigned Changes = 0;
251 /* Generate register info for this step */
254 /* Walk over the entries */
256 while (I < CS_GetEntryCount (S)) {
259 CodeEntry* E = CS_GetEntry (S, I);
261 /* Check if this is a call to bnegax, and if X is known and zero */
262 if (E->RI->In.RegX == 0 &&
263 CE_IsCall (E, "bnegax")) {
265 /* We're cheating somewhat here ... */
278 /* Free register info */
281 /* Return the number of changes made */
287 static unsigned OptNegAX2 (CodeSeg* S)
288 /* Search for the sequence:
305 unsigned Changes = 0;
307 /* Walk over the entries */
309 while (I < CS_GetEntryCount (S)) {
314 CodeEntry* E = CS_GetEntry (S, I);
316 /* Check for the sequence */
317 if (E->OPC == OP65_LDA &&
318 E->AM == AM65_ZP_INDY &&
319 CS_GetEntries (S, L, I+1, 5) &&
320 L[0]->OPC == OP65_TAX &&
321 L[1]->OPC == OP65_DEY &&
322 L[2]->OPC == OP65_LDA &&
323 L[2]->AM == AM65_ZP_INDY &&
324 strcmp (L[2]->Arg, E->Arg) == 0 &&
325 !CE_HasLabel (L[2]) &&
326 CE_IsCall (L[3], "bnegax") &&
327 !CE_HasLabel (L[3]) &&
328 (L[4]->Info & OF_ZBRA) != 0 &&
329 !CE_HasLabel (L[4])) {
332 CE_ReplaceOPC (L[2], OP65_ORA);
334 /* Invert the branch */
335 CE_ReplaceOPC (L[4], GetInverseBranch (L[4]->OPC));
337 /* Delete the entries no longer needed. Beware: Deleting entries
338 * will change the indices.
340 CS_DelEntry (S, I+4); /* jsr bnegax */
341 CS_DelEntry (S, I+1); /* tax */
343 /* Remember, we had changes */
353 /* Return the number of changes made */
359 static unsigned OptNegAX3 (CodeSeg* S)
360 /* Search for the sequence:
374 unsigned Changes = 0;
376 /* Walk over the entries */
378 while (I < CS_GetEntryCount (S)) {
383 CodeEntry* E = CS_GetEntry (S, I);
385 /* Check for the sequence */
386 if (E->OPC == OP65_LDA &&
387 CS_GetEntries (S, L, I+1, 3) &&
388 L[0]->OPC == OP65_LDX &&
389 !CE_HasLabel (L[0]) &&
390 CE_IsCall (L[1], "bnegax") &&
391 !CE_HasLabel (L[1]) &&
392 (L[2]->Info & OF_ZBRA) != 0 &&
393 !CE_HasLabel (L[2])) {
396 CE_ReplaceOPC (L[0], OP65_ORA);
398 /* Invert the branch */
399 CE_ReplaceOPC (L[2], GetInverseBranch (L[2]->OPC));
401 /* Delete the subroutine call */
402 CS_DelEntry (S, I+2);
404 /* Remember, we had changes */
414 /* Return the number of changes made */
420 static unsigned OptNegAX4 (CodeSeg* S)
421 /* Search for the sequence:
434 unsigned Changes = 0;
436 /* Walk over the entries */
438 while (I < CS_GetEntryCount (S)) {
443 CodeEntry* E = CS_GetEntry (S, I);
445 /* Check for the sequence */
446 if (E->OPC == OP65_JSR &&
447 CS_GetEntries (S, L, I+1, 2) &&
448 L[0]->OPC == OP65_JSR &&
449 strncmp (L[0]->Arg,"bnega",5) == 0 &&
450 !CE_HasLabel (L[0]) &&
451 (L[1]->Info & OF_ZBRA) != 0 &&
452 !CE_HasLabel (L[1])) {
456 /* Check if we're calling bnega or bnegax */
457 int ByteSized = (strcmp (L[0]->Arg, "bnega") == 0);
459 /* Insert apropriate test code */
462 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
463 CS_InsertEntry (S, X, I+2);
466 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
467 CS_InsertEntry (S, X, I+2);
468 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, L[0]->LI);
469 CS_InsertEntry (S, X, I+3);
472 /* Delete the subroutine call */
473 CS_DelEntry (S, I+1);
475 /* Invert the branch */
476 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
478 /* Remember, we had changes */
488 /* Return the number of changes made */
494 /*****************************************************************************/
495 /* Optimize stores through pointers */
496 /*****************************************************************************/
500 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
501 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
503 /* Check for a label attached to the entry */
504 if (CE_HasLabel (L[0])) {
508 /* Check for single insn sub ops */
509 if (L[0]->OPC == OP65_AND ||
510 L[0]->OPC == OP65_EOR ||
511 L[0]->OPC == OP65_ORA ||
512 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
513 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
518 } else if (L[0]->OPC == OP65_CLC &&
519 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
520 L[1]->OPC == OP65_ADC &&
521 !CE_HasLabel (L[1])) {
523 } else if (L[0]->OPC == OP65_SEC &&
524 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
525 L[1]->OPC == OP65_SBC &&
526 !CE_HasLabel (L[1])) {
538 static unsigned OptPtrStore1 (CodeSeg* S)
539 /* Search for the sequence:
560 unsigned Changes = 0;
562 /* Walk over the entries */
564 while (I < CS_GetEntryCount (S)) {
570 L[0] = CS_GetEntry (S, I);
572 /* Check for the sequence */
573 if (CE_IsCall (L[0], "pushax") &&
574 CS_GetEntries (S, L+1, I+1, 3) &&
575 L[1]->OPC == OP65_LDY &&
576 CE_KnownImm (L[1]) &&
577 !CE_HasLabel (L[1]) &&
578 CE_IsCall (L[2], "ldauidx") &&
579 !CE_HasLabel (L[2]) &&
580 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
581 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
582 L[3+K]->OPC == OP65_LDY &&
583 CE_KnownImm (L[3+K]) &&
584 !CE_HasLabel (L[3+K]) &&
585 CE_IsCall (L[4+K], "staspidx") &&
586 !CE_HasLabel (L[4+K])) {
590 /* Create and insert the stores */
591 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
592 CS_InsertEntry (S, X, I+1);
594 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
595 CS_InsertEntry (S, X, I+2);
597 /* Delete the call to pushax */
600 /* Delete the call to ldauidx */
601 CS_DelEntry (S, I+3);
603 /* Insert the load from ptr1 */
604 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
605 CS_InsertEntry (S, X, I+3);
606 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
607 CS_InsertEntry (S, X, I+4);
609 /* Insert the store through ptr1 */
610 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
611 CS_InsertEntry (S, X, I+6+K);
613 /* Delete the call to staspidx */
614 CS_DelEntry (S, I+7+K);
616 /* Remember, we had changes */
626 /* Return the number of changes made */
632 static unsigned OptPtrStore2 (CodeSeg* S)
633 /* Search for the sequence:
649 unsigned Changes = 0;
651 /* Walk over the entries */
653 while (I < CS_GetEntryCount (S)) {
658 L[0] = CS_GetEntry (S, I);
660 /* Check for the sequence */
661 if (CE_IsCall (L[0], "pushax") &&
662 CS_GetEntries (S, L+1, I+1, 3) &&
663 L[1]->OPC == OP65_LDA &&
664 !CE_HasLabel (L[1]) &&
665 L[2]->OPC == OP65_LDY &&
666 !CE_HasLabel (L[2]) &&
667 CE_IsCall (L[3], "staspidx") &&
668 !CE_HasLabel (L[3])) {
672 /* Create and insert the stores */
673 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
674 CS_InsertEntry (S, X, I+1);
676 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
677 CS_InsertEntry (S, X, I+2);
679 /* Delete the call to pushax */
682 /* Insert the store through ptr1 */
683 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
684 CS_InsertEntry (S, X, I+4);
686 /* Delete the call to staspidx */
687 CS_DelEntry (S, I+5);
689 /* Remember, we had changes */
699 /* Return the number of changes made */
705 /*****************************************************************************/
706 /* Optimize loads through pointers */
707 /*****************************************************************************/
711 static unsigned OptPtrLoad1 (CodeSeg* S)
712 /* Search for the sequence:
716 * lda (sp),y # May be any destination
731 unsigned Changes = 0;
733 /* Walk over the entries */
735 while (I < CS_GetEntryCount (S)) {
740 L[0] = CS_GetEntry (S, I);
742 /* Check for the sequence */
743 if (L[0]->OPC == OP65_TAX &&
744 CS_GetEntries (S, L+1, I+1, 4) &&
745 L[1]->OPC == OP65_DEY &&
746 !CE_HasLabel (L[1]) &&
747 L[2]->OPC == OP65_LDA &&
748 !CE_HasLabel (L[2]) &&
749 L[3]->OPC == OP65_LDY &&
750 !CE_HasLabel (L[3]) &&
751 CE_IsCall (L[4], "ldauidx") &&
752 !CE_HasLabel (L[4])) {
756 /* Store the high byte and remove the TAX instead */
757 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
758 CS_InsertEntry (S, X, I+1);
761 /* Store the low byte */
762 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
763 CS_InsertEntry (S, X, I+3);
765 /* Delete the call to ldauidx */
766 CS_DelEntry (S, I+5);
768 /* Load high and low byte */
769 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
770 CS_InsertEntry (S, X, I+5);
771 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
772 CS_InsertEntry (S, X, I+6);
774 /* Remember, we had changes */
784 /* Return the number of changes made */
790 static unsigned OptPtrLoad2 (CodeSeg* S)
791 /* Search for the sequence:
816 unsigned Changes = 0;
818 /* Walk over the entries */
820 while (I < CS_GetEntryCount (S)) {
825 L[0] = CS_GetEntry (S, I);
827 /* Check for the sequence */
828 if (L[0]->OPC == OP65_CLC &&
829 CS_GetEntries (S, L+1, I+1, 8) &&
830 L[1]->OPC == OP65_ADC &&
831 !CE_HasLabel (L[1]) &&
832 L[2]->OPC == OP65_TAY &&
833 !CE_HasLabel (L[2]) &&
834 L[3]->OPC == OP65_TXA &&
835 !CE_HasLabel (L[3]) &&
836 L[4]->OPC == OP65_ADC &&
837 !CE_HasLabel (L[4]) &&
838 L[5]->OPC == OP65_TAX &&
839 !CE_HasLabel (L[5]) &&
840 L[6]->OPC == OP65_TYA &&
841 !CE_HasLabel (L[6]) &&
842 L[7]->OPC == OP65_LDY &&
843 !CE_HasLabel (L[7]) &&
844 CE_IsCall (L[8], "ldauidx") &&
845 !CE_HasLabel (L[8])) {
850 /* Store the low byte and remove the TAY instead */
851 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
852 CS_InsertEntry (S, X, I+2);
853 CS_DelEntry (S, I+3);
855 /* Store the high byte */
856 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
857 CS_InsertEntry (S, X, I+5);
859 /* If the instruction before the adc is a ldx, replace the
860 * txa by and lda with the same location of the ldx.
862 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
863 P->OPC == OP65_LDX &&
866 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
867 CS_InsertEntry (S, X, I+4);
868 CS_DelEntry (S, I+3);
871 /* Delete more transfer insns */
872 CS_DelEntry (S, I+7);
873 CS_DelEntry (S, I+6);
875 /* Delete the call to ldauidx */
876 CS_DelEntry (S, I+7);
878 /* Load high and low byte */
879 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
880 CS_InsertEntry (S, X, I+7);
881 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
882 CS_InsertEntry (S, X, I+8);
884 /* Remember, we had changes */
894 /* Return the number of changes made */
900 static unsigned OptPtrLoad3 (CodeSeg* S)
901 /* Search for the sequence:
926 unsigned Changes = 0;
928 /* Walk over the entries */
930 while (I < CS_GetEntryCount (S)) {
935 L[0] = CS_GetEntry (S, I);
937 /* Check for the sequence */
938 if (L[0]->OPC == OP65_ADC &&
939 CS_GetEntries (S, L+1, I+1, 8) &&
940 L[1]->OPC == OP65_PHA &&
941 !CE_HasLabel (L[1]) &&
942 L[2]->OPC == OP65_TXA &&
943 !CE_HasLabel (L[2]) &&
944 L[3]->OPC == OP65_INY &&
945 !CE_HasLabel (L[3]) &&
946 L[4]->OPC == OP65_ADC &&
947 !CE_HasLabel (L[4]) &&
948 L[5]->OPC == OP65_TAX &&
949 !CE_HasLabel (L[5]) &&
950 L[6]->OPC == OP65_PLA &&
951 !CE_HasLabel (L[6]) &&
952 L[7]->OPC == OP65_LDY &&
953 !CE_HasLabel (L[7]) &&
954 CE_IsCall (L[8], "ldauidx") &&
955 !CE_HasLabel (L[8])) {
959 /* Store the low byte and remove the PHA instead */
960 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
961 CS_InsertEntry (S, X, I+1);
962 CS_DelEntry (S, I+2);
964 /* Store the high byte */
965 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
966 CS_InsertEntry (S, X, I+5);
968 /* Delete more transfer and PLA insns */
969 CS_DelEntry (S, I+7);
970 CS_DelEntry (S, I+6);
972 /* Delete the call to ldauidx */
973 CS_DelEntry (S, I+7);
975 /* Load high and low byte */
976 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
977 CS_InsertEntry (S, X, I+7);
978 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
979 CS_InsertEntry (S, X, I+8);
981 /* Remember, we had changes */
991 /* Return the number of changes made */
997 static unsigned OptPtrLoad4 (CodeSeg* S)
998 /* Search for the sequence:
1009 * and replace it by:
1016 unsigned Changes = 0;
1018 /* Walk over the entries */
1020 while (I < CS_GetEntryCount (S)) {
1025 /* Get next entry */
1026 L[0] = CS_GetEntry (S, I);
1028 /* Check for the sequence */
1029 if (L[0]->OPC == OP65_LDA &&
1030 L[0]->AM == AM65_IMM &&
1031 CS_GetEntries (S, L+1, I+1, 7) &&
1032 L[1]->OPC == OP65_LDX &&
1033 L[1]->AM == AM65_IMM &&
1034 !CE_HasLabel (L[1]) &&
1035 L[2]->OPC == OP65_CLC &&
1036 !CE_HasLabel (L[2]) &&
1037 L[3]->OPC == OP65_ADC &&
1038 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
1039 !CE_HasLabel (L[3]) &&
1040 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
1041 L[4]->JumpTo != 0 &&
1042 L[4]->JumpTo->Owner == L[6] &&
1043 !CE_HasLabel (L[4]) &&
1044 L[5]->OPC == OP65_INX &&
1045 !CE_HasLabel (L[5]) &&
1046 L[6]->OPC == OP65_LDY &&
1047 CE_KnownImm (L[6]) &&
1049 CE_IsCall (L[7], "ldauidx") &&
1050 !CE_HasLabel (L[7]) &&
1051 /* Check the label last because this is quite costly */
1052 (Len = strlen (L[0]->Arg)) > 3 &&
1053 L[0]->Arg[0] == '<' &&
1054 L[0]->Arg[1] == '(' &&
1055 strlen (L[1]->Arg) == Len &&
1056 L[1]->Arg[0] == '>' &&
1057 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1062 /* We will create all the new stuff behind the current one so
1063 * we keep the line references.
1065 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
1066 CS_InsertEntry (S, X, I+8);
1068 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1069 CS_InsertEntry (S, X, I+9);
1071 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1072 Label[Len-3] = '\0';
1073 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1074 CS_InsertEntry (S, X, I+10);
1077 /* Remove the old code */
1078 CS_DelEntries (S, I, 8);
1080 /* Remember, we had changes */
1090 /* Return the number of changes made */
1096 static unsigned OptPtrLoad5 (CodeSeg* S)
1097 /* Search for the sequence:
1109 * and replace it by:
1118 unsigned Changes = 0;
1120 /* Walk over the entries */
1122 while (I < CS_GetEntryCount (S)) {
1127 /* Get next entry */
1128 L[0] = CS_GetEntry (S, I);
1130 /* Check for the sequence */
1131 if (L[0]->OPC == OP65_LDA &&
1132 L[0]->AM == AM65_IMM &&
1133 CS_GetEntries (S, L+1, I+1, 8) &&
1134 L[1]->OPC == OP65_LDX &&
1135 L[1]->AM == AM65_IMM &&
1136 !CE_HasLabel (L[1]) &&
1137 L[2]->OPC == OP65_LDY &&
1138 CE_KnownImm (L[2]) &&
1139 !CE_HasLabel (L[2]) &&
1140 L[3]->OPC == OP65_CLC &&
1141 !CE_HasLabel (L[3]) &&
1142 L[4]->OPC == OP65_ADC &&
1143 L[4]->AM == AM65_ZP_INDY &&
1144 !CE_HasLabel (L[4]) &&
1145 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
1146 L[5]->JumpTo != 0 &&
1147 L[5]->JumpTo->Owner == L[7] &&
1148 !CE_HasLabel (L[5]) &&
1149 L[6]->OPC == OP65_INX &&
1150 !CE_HasLabel (L[6]) &&
1151 L[7]->OPC == OP65_LDY &&
1152 CE_KnownImm (L[7]) &&
1154 CE_IsCall (L[8], "ldauidx") &&
1155 !CE_HasLabel (L[8]) &&
1156 /* Check the label last because this is quite costly */
1157 (Len = strlen (L[0]->Arg)) > 3 &&
1158 L[0]->Arg[0] == '<' &&
1159 L[0]->Arg[1] == '(' &&
1160 strlen (L[1]->Arg) == Len &&
1161 L[1]->Arg[0] == '>' &&
1162 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1168 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1169 CS_InsertEntry (S, X, I+3);
1172 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1173 CS_InsertEntry (S, X, I+4);
1176 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1177 CS_InsertEntry (S, X, I+5);
1180 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1181 Label[Len-3] = '\0';
1182 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1183 CS_InsertEntry (S, X, I+6);
1186 /* Remove the old code */
1187 CS_DelEntries (S, I, 2);
1188 CS_DelEntries (S, I+5, 6);
1190 /* Remember, we had changes */
1200 /* Return the number of changes made */
1206 static unsigned OptPtrLoad6 (CodeSeg* S)
1207 /* Search for the sequence
1212 * and replace it by:
1220 * This step must be execute *after* OptPtrLoad1!
1223 unsigned Changes = 0;
1225 /* Walk over the entries */
1227 while (I < CS_GetEntryCount (S)) {
1231 /* Get next entry */
1232 L[0] = CS_GetEntry (S, I);
1234 /* Check for the sequence */
1235 if (L[0]->OPC == OP65_LDY &&
1236 CS_GetEntries (S, L+1, I+1, 1) &&
1237 CE_IsCall (L[1], "ldauidx") &&
1238 !CE_HasLabel (L[1])) {
1242 /* Store the high byte */
1243 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1244 CS_InsertEntry (S, X, I+1);
1246 /* Store the low byte */
1247 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1248 CS_InsertEntry (S, X, I+2);
1250 /* Delete the call to ldauidx */
1251 CS_DelEntry (S, I+3);
1253 /* Load the high and low byte */
1254 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1255 CS_InsertEntry (S, X, I+3);
1256 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1257 CS_InsertEntry (S, X, I+4);
1259 /* Remember, we had changes */
1269 /* Return the number of changes made */
1275 /*****************************************************************************/
1276 /* Decouple operations */
1277 /*****************************************************************************/
1281 static unsigned OptDecouple (CodeSeg* S)
1282 /* Decouple operations, that is, do the following replacements:
1293 * Provided that the register values are known of course.
1296 unsigned Changes = 0;
1299 /* Generate register info for the following step */
1302 /* Walk over the entries */
1304 while (I < CS_GetEntryCount (S)) {
1308 /* Get next entry */
1309 CodeEntry* E = CS_GetEntry (S, I);
1311 /* Assume we have no replacement */
1314 /* Check the instruction */
1318 if (E->RI->In.RegX >= 0) {
1319 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX - 1) & 0xFF);
1320 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1325 if (E->RI->In.RegY >= 0) {
1326 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY - 1) & 0xFF);
1327 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1332 if (E->RI->In.RegX >= 0) {
1333 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegX + 1) & 0xFF);
1334 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1339 if (E->RI->In.RegY >= 0) {
1340 xsprintf (Buf, sizeof (Buf), "$%02X", (E->RI->In.RegY + 1) & 0xFF);
1341 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1346 if (E->RI->In.RegA >= 0) {
1347 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1348 X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, E->LI);
1353 if (E->RI->In.RegA >= 0) {
1354 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegA);
1355 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, E->LI);
1360 if (E->RI->In.RegX >= 0) {
1361 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegX);
1362 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1367 if (E->RI->In.RegY >= 0) {
1368 xsprintf (Buf, sizeof (Buf), "$%02X", E->RI->In.RegY);
1369 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, E->LI);
1374 /* Avoid gcc warnings */
1379 /* Insert the replacement if we have one */
1381 CS_InsertEntry (S, X, I+1);
1391 /* Free register info */
1394 /* Return the number of changes made */
1400 /*****************************************************************************/
1401 /* Size optimization */
1402 /*****************************************************************************/
1406 static unsigned OptSize (CodeSeg* S)
1407 /* Do size optimization by using shorter code sequences, even if this
1408 * introduces relations between instructions. This step must be one of the
1409 * last steps, because it makes further work much more difficult.
1412 unsigned Changes = 0;
1415 /* Generate register info for the following step */
1418 /* Walk over the entries */
1420 while (I < CS_GetEntryCount (S)) {
1423 /* Get next entry */
1424 CodeEntry* E = CS_GetEntry (S, I);
1426 /* Assume we have no replacement */
1429 /* Check the instruction */
1433 if (CE_KnownImm (E)) {
1434 short Val = (short) E->Num;
1435 if (Val == E->RI->In.RegX) {
1436 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
1437 } else if (Val == E->RI->In.RegY) {
1438 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
1439 } else if (E->RI->In.RegA >= 0 && CPU >= CPU_65C02) {
1440 if (Val == ((E->RI->In.RegA - 1) & 0xFF)) {
1441 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
1442 } else if (Val == ((E->RI->In.RegA + 1) & 0xFF)) {
1443 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
1450 if (CE_KnownImm (E)) {
1451 short Val = (short) E->Num;
1452 if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX - 1) & 0xFF)) {
1453 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
1454 } else if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX + 1) & 0xFF)) {
1455 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
1456 } else if (Val == E->RI->In.RegA) {
1457 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
1463 if (CE_KnownImm (E)) {
1464 short Val = (short) E->Num;
1465 if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY - 1) & 0xFF)) {
1466 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
1467 } else if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY + 1) & 0xFF)) {
1468 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
1469 } else if (Val == E->RI->In.RegA) {
1470 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
1476 /* Avoid gcc warnings */
1481 /* Insert the replacement if we have one */
1483 CS_InsertEntry (S, X, I+1);
1493 /* Free register info */
1496 /* Return the number of changes made */
1502 /*****************************************************************************/
1503 /* struct OptFunc */
1504 /*****************************************************************************/
1508 typedef struct OptFunc OptFunc;
1510 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1511 const char* Name; /* Name of the function/group */
1512 unsigned long TotalRuns; /* Total number of runs */
1513 unsigned long LastRuns; /* Last number of runs */
1514 unsigned long TotalChanges; /* Total number of changes */
1515 unsigned long LastChanges; /* Last number of changes */
1516 char Disabled; /* True if function disabled */
1521 /*****************************************************************************/
1523 /*****************************************************************************/
1527 /* Macro that builds a function description */
1528 #define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
1530 /* A list of all the function descriptions */
1531 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 0, 0, 0, 0, 0 };
1532 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 0, 0, 0, 0, 0 };
1533 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 0, 0, 0, 0, 0 };
1534 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 0, 0, 0, 0, 0 };
1535 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0 };
1536 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 0, 0, 0, 0, 0 };
1537 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 0, 0, 0, 0, 0 };
1538 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 0, 0, 0, 0, 0 };
1539 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 0, 0, 0, 0, 0 };
1540 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 0, 0, 0, 0, 0 };
1541 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 0, 0, 0, 0, 0 };
1542 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 0, 0, 0, 0, 0 };
1543 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 0, 0, 0, 0, 0 };
1544 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 0, 0, 0, 0, 0 };
1545 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 0, 0, 0, 0, 0 };
1546 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 0, 0, 0, 0, 0 };
1547 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0 };
1548 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 0, 0, 0, 0, 0 };
1549 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 0, 0, 0, 0, 0 };
1550 static OptFunc DOptRTS = { OptRTS, "OptRTS", 0, 0, 0, 0, 0 };
1551 static OptFunc DOptRTSJumps = { OptRTSJumps, "OptRTSJumps", 0, 0, 0, 0, 0 };
1552 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 0, 0, 0, 0, 0 };
1553 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 0, 0, 0, 0, 0 };
1554 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 0, 0, 0, 0, 0 };
1555 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 0, 0, 0, 0, 0 };
1556 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 0, 0, 0, 0, 0 };
1557 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 0, 0, 0, 0, 0 };
1558 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 0, 0, 0, 0, 0 };
1559 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 0, 0, 0, 0, 0 };
1560 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 0, 0, 0, 0, 0 };
1561 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 0, 0, 0, 0, 0 };
1562 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 0, 0, 0, 0, 0 };
1563 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 0, 0, 0, 0, 0 };
1564 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 0, 0, 0, 0, 0 };
1565 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 0, 0, 0, 0, 0 };
1566 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 0, 0, 0, 0, 0 };
1567 static OptFunc DOptSize = { OptSize, "OptSize", 0, 0, 0, 0, 0 };
1568 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 0, 0, 0, 0, 0 };
1569 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0 };
1570 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 0, 0, 0, 0, 0 };
1571 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 0, 0, 0, 0, 0 };
1572 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 0, 0, 0, 0, 0 };
1573 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0 };
1574 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0 };
1575 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0 };
1578 /* Table containing all the steps in alphabetical order */
1579 static OptFunc* OptFuncs[] = {
1626 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1630 static int CmpOptStep (const void* Key, const void* Func)
1631 /* Compare function for bsearch */
1633 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1638 static OptFunc* FindOptFunc (const char* Name)
1639 /* Find an optimizer step by name in the table and return a pointer. Return
1640 * NULL if no such step is found.
1643 /* Search for the function in the list */
1644 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1650 static OptFunc* GetOptFunc (const char* Name)
1651 /* Find an optimizer step by name in the table and return a pointer. Print an
1652 * error and call AbEnd if not found.
1655 /* Search for the function in the list */
1656 OptFunc* F = FindOptFunc (Name);
1659 AbEnd ("Optimization step `%s' not found", Name);
1666 void DisableOpt (const char* Name)
1667 /* Disable the optimization with the given name */
1669 if (strcmp (Name, "any") == 0) {
1671 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1672 OptFuncs[I]->Disabled = 1;
1675 GetOptFunc(Name)->Disabled = 1;
1681 void EnableOpt (const char* Name)
1682 /* Enable the optimization with the given name */
1684 if (strcmp (Name, "any") == 0) {
1686 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1687 OptFuncs[I]->Disabled = 0;
1690 GetOptFunc(Name)->Disabled = 0;
1696 void ListOptSteps (FILE* F)
1697 /* List all optimization steps */
1700 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1701 fprintf (F, "%s\n", OptFuncs[I]->Name);
1707 static void ReadOptStats (const char* Name)
1708 /* Read the optimizer statistics file */
1713 /* Try to open the file */
1714 FILE* F = fopen (Name, "r");
1716 /* Ignore the error */
1720 /* Read and parse the lines */
1722 while (fgets (Buf, sizeof (Buf), F) != 0) {
1730 unsigned long TotalRuns;
1731 unsigned long TotalChanges;
1736 /* Remove trailing white space including the line terminator */
1739 while (Len > 0 && IsSpace (B[Len-1])) {
1744 /* Remove leading whitespace */
1745 while (IsSpace (*B)) {
1749 /* Check for empty and comment lines */
1750 if (*B == '\0' || *B == ';' || *B == '#') {
1754 /* Parse the line */
1755 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1760 /* Search for the optimizer step. */
1761 Func = FindOptFunc (Name);
1767 /* Found the step, set the fields */
1768 Func->TotalRuns = TotalRuns;
1769 Func->TotalChanges = TotalChanges;
1773 /* Close the file, ignore errors here. */
1779 static void WriteOptStats (const char* Name)
1780 /* Write the optimizer statistics file */
1784 /* Try to open the file */
1785 FILE* F = fopen (Name, "w");
1787 /* Ignore the error */
1791 /* Write the file */
1792 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1793 const OptFunc* O = OptFuncs[I];
1795 "%-20s %6lu %6lu %6lu %6lu\n",
1803 /* Close the file, ignore errors here. */
1809 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1810 /* Run one optimizer function Max times or until there are no more changes */
1812 unsigned Changes, C;
1814 /* Don't run the function if it is disabled */
1819 /* Run this until there are no more changes */
1823 /* Run the function */
1830 F->TotalChanges += C;
1831 F->LastChanges += C;
1833 } while (--Max && C > 0);
1835 /* Return the number of changes */
1841 static void RunOptGroup1 (CodeSeg* S)
1842 /* Run the first group of optimization steps. These steps translate known
1843 * patterns emitted by the code generator into more optimal patterns. Order
1844 * of the steps is important, because some of the steps done earlier cover
1845 * the same patterns as later steps as subpatterns.
1848 RunOptFunc (S, &DOptPtrStore1, 1);
1849 RunOptFunc (S, &DOptPtrStore2, 1);
1850 RunOptFunc (S, &DOptPtrLoad1, 1);
1851 RunOptFunc (S, &DOptPtrLoad2, 1);
1852 RunOptFunc (S, &DOptPtrLoad3, 1);
1853 RunOptFunc (S, &DOptPtrLoad4, 1);
1854 RunOptFunc (S, &DOptPtrLoad5, 1);
1855 RunOptFunc (S, &DOptNegAX1, 1);
1856 RunOptFunc (S, &DOptNegAX2, 1);
1857 RunOptFunc (S, &DOptNegAX3, 1);
1858 RunOptFunc (S, &DOptNegAX4, 1);
1859 RunOptFunc (S, &DOptAdd1, 1);
1860 RunOptFunc (S, &DOptAdd2, 1);
1861 RunOptFunc (S, &DOptShift1, 1);
1866 static void RunOptGroup2 (CodeSeg* S)
1867 /* Run one group of optimization steps. This step involves just decoupling
1868 * instructions by replacing them by instructions that do not depend on
1869 * previous instructions. This makes it easier to find instructions that
1873 RunOptFunc (S, &DOptDecouple, 1);
1879 static void RunOptGroup3 (CodeSeg* S)
1880 /* Run one group of optimization steps. These steps depend on each other,
1881 * that means that one step may allow another step to do additional work,
1882 * so we will repeat the steps as long as we see any changes.
1890 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1891 Changes += RunOptFunc (S, &DOptNegA1, 1);
1892 Changes += RunOptFunc (S, &DOptNegA2, 1);
1893 Changes += RunOptFunc (S, &DOptSub1, 1);
1894 Changes += RunOptFunc (S, &DOptSub2, 1);
1895 Changes += RunOptFunc (S, &DOptAdd3, 1);
1896 Changes += RunOptFunc (S, &DOptJumpCascades, 1);
1897 Changes += RunOptFunc (S, &DOptDeadJumps, 1);
1898 Changes += RunOptFunc (S, &DOptRTS, 1);
1899 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1900 Changes += RunOptFunc (S, &DOptJumpTarget, 1);
1901 Changes += RunOptFunc (S, &DOptCondBranches, 1);
1902 Changes += RunOptFunc (S, &DOptRTSJumps, 1);
1903 Changes += RunOptFunc (S, &DOptBoolTrans, 1);
1904 Changes += RunOptFunc (S, &DOptCmp1, 1);
1905 Changes += RunOptFunc (S, &DOptCmp2, 1);
1906 Changes += RunOptFunc (S, &DOptCmp3, 1);
1907 Changes += RunOptFunc (S, &DOptCmp4, 1);
1908 Changes += RunOptFunc (S, &DOptCmp5, 1);
1909 Changes += RunOptFunc (S, &DOptCmp6, 1);
1910 Changes += RunOptFunc (S, &DOptCmp7, 1);
1911 Changes += RunOptFunc (S, &DOptTest1, 1);
1912 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1913 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1914 Changes += RunOptFunc (S, &DOptDupLoads, 1);
1915 Changes += RunOptFunc (S, &DOptStoreLoad, 1);
1916 Changes += RunOptFunc (S, &DOptTransfers, 1);
1917 Changes += RunOptFunc (S, &DOptStackOps, 1);
1924 static void RunOptGroup4 (CodeSeg* S)
1925 /* The last group of optimization steps. Adjust branches.
1928 /* Optimize for size, that is replace operations by shorter ones, even
1929 * if this does hinder further optimizations (no problem since we're
1932 RunOptFunc (S, &DOptSize, 1);
1934 /* Run the jump target optimization again, since the size optimization
1935 * above may have opened new oportunities.
1937 RunOptFunc (S, &DOptJumpTarget, 5);
1939 /* Finally, adjust branch distances */
1940 RunOptFunc (S, &DOptBranchDist, 3);
1945 void RunOpt (CodeSeg* S)
1946 /* Run the optimizer */
1948 const char* StatFileName;
1950 /* If we shouldn't run the optimizer, bail out */
1955 /* Check if we are requested to write optimizer statistics */
1956 StatFileName = getenv ("CC65_OPTSTATS");
1958 ReadOptStats (StatFileName);
1961 /* Print the name of the function we are working on */
1963 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1965 Print (stdout, 1, "Running optimizer for global code segment\n");
1968 /* Run groups of optimizations */
1974 /* Write statistics */
1976 WriteOptStats (StatFileName);