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 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
84 /*****************************************************************************/
86 /*****************************************************************************/
90 static unsigned OptShift1 (CodeSeg* S)
91 /* A call to the shlaxN routine may get replaced by one or more asl insns
92 * if the value of X is not used later.
97 /* Walk over the entries */
99 while (I < CS_GetEntryCount (S)) {
102 CodeEntry* E = CS_GetEntry (S, I);
104 /* Check for the sequence */
105 if (E->OPC == OP65_JSR &&
106 (strncmp (E->Arg, "shlax", 5) == 0 ||
107 strncmp (E->Arg, "aslax", 5) == 0) &&
108 strlen (E->Arg) == 6 &&
109 IsDigit (E->Arg[5]) &&
110 !RegXUsed (S, I+1)) {
112 /* Insert shift insns */
113 unsigned Count = E->Arg[5] - '0';
115 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
116 CS_InsertEntry (S, X, I+1);
119 /* Delete the call to shlax */
122 /* Remember, we had changes */
132 /* Return the number of changes made */
138 static unsigned OptShift2 (CodeSeg* S)
139 /* A call to the shraxN routine may get replaced by one or more lsr insns
140 * if the value of X is not used later.
143 unsigned Changes = 0;
145 /* Walk over the entries */
147 while (I < CS_GetEntryCount (S)) {
150 CodeEntry* E = CS_GetEntry (S, I);
152 /* Check for the sequence */
153 if (E->OPC == OP65_JSR &&
154 strncmp (E->Arg, "shrax", 5) == 0 &&
155 strlen (E->Arg) == 6 &&
156 IsDigit (E->Arg[5]) &&
157 !RegXUsed (S, I+1)) {
159 /* Insert shift insns */
160 unsigned Count = E->Arg[5] - '0';
162 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
163 CS_InsertEntry (S, X, I+1);
166 /* Delete the call to shlax */
169 /* Remember, we had changes */
179 /* Return the number of changes made */
185 static unsigned GetShiftType (const char* Sub)
186 /* Helper function for OptShift3 */
189 if (strcmp (Sub+1, "slax1") == 0) {
191 } else if (strcmp (Sub+1, "srax1") == 0) {
194 } else if (*Sub == 's') {
195 if (strcmp (Sub+1, "hlax1") == 0) {
197 } else if (strcmp (Sub+1, "hrax1") == 0) {
206 static unsigned OptShift3 (CodeSeg* S)
207 /* Search for the sequence
211 * jsr aslax1/asrax1/shlax1/shrax1
224 * or similar, provided that a/x is not used later
227 unsigned Changes = 0;
229 /* Walk over the entries */
231 while (I < CS_GetEntryCount (S)) {
237 L[0] = CS_GetEntry (S, I);
239 /* Check for the sequence */
240 if (L[0]->OPC == OP65_LDA &&
241 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
242 CS_GetEntries (S, L+1, I+1, 4) &&
243 !CS_RangeHasLabel (S, I+1, 4) &&
244 L[1]->OPC == OP65_LDX &&
245 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
246 L[2]->OPC == OP65_JSR &&
247 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
248 L[3]->OPC == OP65_STA &&
249 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
250 L[4]->OPC == OP65_STX &&
251 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
252 !RegAXUsed (S, I+5)) {
256 /* Handle the four shift types differently */
260 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
261 CS_InsertEntry (S, X, I+5);
262 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
263 CS_InsertEntry (S, X, I+6);
264 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
265 CS_InsertEntry (S, X, I+7);
266 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
267 CS_InsertEntry (S, X, I+8);
268 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
269 CS_InsertEntry (S, X, I+9);
270 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
271 CS_InsertEntry (S, X, I+10);
272 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
273 CS_InsertEntry (S, X, I+11);
274 CS_DelEntries (S, I, 5);
278 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
279 CS_InsertEntry (S, X, I+5);
280 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
281 CS_InsertEntry (S, X, I+6);
282 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
283 CS_InsertEntry (S, X, I+7);
284 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
285 CS_InsertEntry (S, X, I+8);
286 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
287 CS_InsertEntry (S, X, I+9);
288 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
289 CS_InsertEntry (S, X, I+10);
290 CS_DelEntries (S, I, 5);
295 /* These two are identical */
296 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
297 CS_InsertEntry (S, X, I+1);
298 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
299 CS_InsertEntry (S, X, I+2);
300 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
301 CS_InsertEntry (S, X, I+3);
302 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
303 CS_InsertEntry (S, X, I+4);
304 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
305 CS_InsertEntry (S, X, I+5);
306 CS_DelEntries (S, I+6, 4);
311 /* Remember, we had changes */
321 /* Return the number of changes made */
327 /*****************************************************************************/
328 /* Optimize stores through pointers */
329 /*****************************************************************************/
333 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
334 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
336 /* Check for a label attached to the entry */
337 if (CE_HasLabel (L[0])) {
341 /* Check for single insn sub ops */
342 if (L[0]->OPC == OP65_AND ||
343 L[0]->OPC == OP65_EOR ||
344 L[0]->OPC == OP65_ORA ||
345 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
346 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
351 } else if (L[0]->OPC == OP65_CLC &&
352 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
353 L[1]->OPC == OP65_ADC &&
354 !CE_HasLabel (L[1])) {
356 } else if (L[0]->OPC == OP65_SEC &&
357 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
358 L[1]->OPC == OP65_SBC &&
359 !CE_HasLabel (L[1])) {
371 static unsigned OptPtrStore1 (CodeSeg* S)
372 /* Search for the sequence:
393 unsigned Changes = 0;
395 /* Walk over the entries */
397 while (I < CS_GetEntryCount (S)) {
403 L[0] = CS_GetEntry (S, I);
405 /* Check for the sequence */
406 if (CE_IsCallTo (L[0], "pushax") &&
407 CS_GetEntries (S, L+1, I+1, 3) &&
408 L[1]->OPC == OP65_LDY &&
409 CE_KnownImm (L[1]) &&
410 !CE_HasLabel (L[1]) &&
411 CE_IsCallTo (L[2], "ldauidx") &&
412 !CE_HasLabel (L[2]) &&
413 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
414 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
415 L[3+K]->OPC == OP65_LDY &&
416 CE_KnownImm (L[3+K]) &&
417 !CE_HasLabel (L[3+K]) &&
418 CE_IsCallTo (L[4+K], "staspidx") &&
419 !CE_HasLabel (L[4+K])) {
423 /* Create and insert the stores */
424 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
425 CS_InsertEntry (S, X, I+1);
427 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
428 CS_InsertEntry (S, X, I+2);
430 /* Delete the call to pushax */
433 /* Delete the call to ldauidx */
434 CS_DelEntry (S, I+3);
436 /* Insert the load from ptr1 */
437 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
438 CS_InsertEntry (S, X, I+3);
439 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
440 CS_InsertEntry (S, X, I+4);
442 /* Insert the store through ptr1 */
443 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
444 CS_InsertEntry (S, X, I+6+K);
446 /* Delete the call to staspidx */
447 CS_DelEntry (S, I+7+K);
449 /* Remember, we had changes */
459 /* Return the number of changes made */
465 static unsigned OptPtrStore2 (CodeSeg* S)
466 /* Search for the sequence:
482 unsigned Changes = 0;
484 /* Walk over the entries */
486 while (I < CS_GetEntryCount (S)) {
491 L[0] = CS_GetEntry (S, I);
493 /* Check for the sequence */
494 if (CE_IsCallTo (L[0], "pushax") &&
495 CS_GetEntries (S, L+1, I+1, 3) &&
496 L[1]->OPC == OP65_LDA &&
497 !CE_HasLabel (L[1]) &&
498 L[2]->OPC == OP65_LDY &&
499 !CE_HasLabel (L[2]) &&
500 CE_IsCallTo (L[3], "staspidx") &&
501 !CE_HasLabel (L[3])) {
505 /* Create and insert the stores */
506 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
507 CS_InsertEntry (S, X, I+1);
509 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
510 CS_InsertEntry (S, X, I+2);
512 /* Delete the call to pushax */
515 /* Insert the store through ptr1 */
516 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
517 CS_InsertEntry (S, X, I+4);
519 /* Delete the call to staspidx */
520 CS_DelEntry (S, I+5);
522 /* Remember, we had changes */
532 /* Return the number of changes made */
538 /*****************************************************************************/
539 /* Optimize loads through pointers */
540 /*****************************************************************************/
544 static unsigned OptPtrLoad1 (CodeSeg* S)
545 /* Search for the sequence:
549 * lda (sp),y # May be any destination
564 unsigned Changes = 0;
566 /* Walk over the entries */
568 while (I < CS_GetEntryCount (S)) {
573 L[0] = CS_GetEntry (S, I);
575 /* Check for the sequence */
576 if (L[0]->OPC == OP65_TAX &&
577 CS_GetEntries (S, L+1, I+1, 4) &&
578 L[1]->OPC == OP65_DEY &&
579 !CE_HasLabel (L[1]) &&
580 L[2]->OPC == OP65_LDA &&
581 !CE_HasLabel (L[2]) &&
582 L[3]->OPC == OP65_LDY &&
583 !CE_HasLabel (L[3]) &&
584 CE_IsCallTo (L[4], "ldauidx") &&
585 !CE_HasLabel (L[4])) {
589 /* Store the high byte and remove the TAX instead */
590 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
591 CS_InsertEntry (S, X, I+1);
594 /* Store the low byte */
595 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
596 CS_InsertEntry (S, X, I+3);
598 /* Delete the call to ldauidx */
599 CS_DelEntry (S, I+5);
601 /* Load high and low byte */
602 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
603 CS_InsertEntry (S, X, I+5);
604 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
605 CS_InsertEntry (S, X, I+6);
607 /* Remember, we had changes */
617 /* Return the number of changes made */
623 static unsigned OptPtrLoad2 (CodeSeg* S)
624 /* 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 (L[0]->OPC == OP65_CLC &&
662 CS_GetEntries (S, L+1, I+1, 8) &&
663 L[1]->OPC == OP65_ADC &&
664 !CE_HasLabel (L[1]) &&
665 L[2]->OPC == OP65_TAY &&
666 !CE_HasLabel (L[2]) &&
667 L[3]->OPC == OP65_TXA &&
668 !CE_HasLabel (L[3]) &&
669 L[4]->OPC == OP65_ADC &&
670 !CE_HasLabel (L[4]) &&
671 L[5]->OPC == OP65_TAX &&
672 !CE_HasLabel (L[5]) &&
673 L[6]->OPC == OP65_TYA &&
674 !CE_HasLabel (L[6]) &&
675 L[7]->OPC == OP65_LDY &&
676 !CE_HasLabel (L[7]) &&
677 CE_IsCallTo (L[8], "ldauidx") &&
678 !CE_HasLabel (L[8])) {
683 /* Store the low byte and remove the TAY instead */
684 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
685 CS_InsertEntry (S, X, I+2);
686 CS_DelEntry (S, I+3);
688 /* Store the high byte */
689 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
690 CS_InsertEntry (S, X, I+5);
692 /* If the instruction before the adc is a ldx, replace the
693 * txa by and lda with the same location of the ldx.
695 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
696 P->OPC == OP65_LDX &&
699 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
700 CS_InsertEntry (S, X, I+4);
701 CS_DelEntry (S, I+3);
704 /* Delete more transfer insns */
705 CS_DelEntry (S, I+7);
706 CS_DelEntry (S, I+6);
708 /* Delete the call to ldauidx */
709 CS_DelEntry (S, I+7);
711 /* Load high and low byte */
712 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
713 CS_InsertEntry (S, X, I+7);
714 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
715 CS_InsertEntry (S, X, I+8);
717 /* Remember, we had changes */
727 /* Return the number of changes made */
733 static unsigned OptPtrLoad3 (CodeSeg* S)
734 /* Search for the sequence:
759 unsigned Changes = 0;
761 /* Walk over the entries */
763 while (I < CS_GetEntryCount (S)) {
768 L[0] = CS_GetEntry (S, I);
770 /* Check for the sequence */
771 if (L[0]->OPC == OP65_ADC &&
772 CS_GetEntries (S, L+1, I+1, 8) &&
773 L[1]->OPC == OP65_PHA &&
774 !CE_HasLabel (L[1]) &&
775 L[2]->OPC == OP65_TXA &&
776 !CE_HasLabel (L[2]) &&
777 L[3]->OPC == OP65_INY &&
778 !CE_HasLabel (L[3]) &&
779 L[4]->OPC == OP65_ADC &&
780 !CE_HasLabel (L[4]) &&
781 L[5]->OPC == OP65_TAX &&
782 !CE_HasLabel (L[5]) &&
783 L[6]->OPC == OP65_PLA &&
784 !CE_HasLabel (L[6]) &&
785 L[7]->OPC == OP65_LDY &&
786 !CE_HasLabel (L[7]) &&
787 CE_IsCallTo (L[8], "ldauidx") &&
788 !CE_HasLabel (L[8])) {
792 /* Store the low byte and remove the PHA instead */
793 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
794 CS_InsertEntry (S, X, I+1);
795 CS_DelEntry (S, I+2);
797 /* Store the high byte */
798 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
799 CS_InsertEntry (S, X, I+5);
801 /* Delete more transfer and PLA insns */
802 CS_DelEntry (S, I+7);
803 CS_DelEntry (S, I+6);
805 /* Delete the call to ldauidx */
806 CS_DelEntry (S, I+7);
808 /* Load high and low byte */
809 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
810 CS_InsertEntry (S, X, I+7);
811 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
812 CS_InsertEntry (S, X, I+8);
814 /* Remember, we had changes */
824 /* Return the number of changes made */
830 static unsigned OptPtrLoad4 (CodeSeg* S)
831 /* Search for the sequence:
849 unsigned Changes = 0;
851 /* Walk over the entries */
853 while (I < CS_GetEntryCount (S)) {
859 L[0] = CS_GetEntry (S, I);
861 /* Check for the sequence */
862 if (L[0]->OPC == OP65_LDA &&
863 L[0]->AM == AM65_IMM &&
864 CS_GetEntries (S, L+1, I+1, 7) &&
865 L[1]->OPC == OP65_LDX &&
866 L[1]->AM == AM65_IMM &&
867 !CE_HasLabel (L[1]) &&
868 L[2]->OPC == OP65_CLC &&
869 !CE_HasLabel (L[2]) &&
870 L[3]->OPC == OP65_ADC &&
871 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
872 !CE_HasLabel (L[3]) &&
873 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
875 L[4]->JumpTo->Owner == L[6] &&
876 !CE_HasLabel (L[4]) &&
877 L[5]->OPC == OP65_INX &&
878 !CE_HasLabel (L[5]) &&
879 L[6]->OPC == OP65_LDY &&
880 CE_KnownImm (L[6]) &&
882 CE_IsCallTo (L[7], "ldauidx") &&
883 !CE_HasLabel (L[7]) &&
884 /* Check the label last because this is quite costly */
885 (Len = strlen (L[0]->Arg)) > 3 &&
886 L[0]->Arg[0] == '<' &&
887 L[0]->Arg[1] == '(' &&
888 strlen (L[1]->Arg) == Len &&
889 L[1]->Arg[0] == '>' &&
890 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
895 /* We will create all the new stuff behind the current one so
896 * we keep the line references.
898 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
899 CS_InsertEntry (S, X, I+8);
901 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
902 CS_InsertEntry (S, X, I+9);
904 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
906 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
907 CS_InsertEntry (S, X, I+10);
910 /* Remove the old code */
911 CS_DelEntries (S, I, 8);
913 /* Remember, we had changes */
923 /* Return the number of changes made */
929 static unsigned OptPtrLoad5 (CodeSeg* S)
930 /* Search for the sequence:
951 unsigned Changes = 0;
953 /* Walk over the entries */
955 while (I < CS_GetEntryCount (S)) {
961 L[0] = CS_GetEntry (S, I);
963 /* Check for the sequence */
964 if (L[0]->OPC == OP65_LDA &&
965 L[0]->AM == AM65_IMM &&
966 CS_GetEntries (S, L+1, I+1, 8) &&
967 L[1]->OPC == OP65_LDX &&
968 L[1]->AM == AM65_IMM &&
969 !CE_HasLabel (L[1]) &&
970 L[2]->OPC == OP65_LDY &&
971 CE_KnownImm (L[2]) &&
972 !CE_HasLabel (L[2]) &&
973 L[3]->OPC == OP65_CLC &&
974 !CE_HasLabel (L[3]) &&
975 L[4]->OPC == OP65_ADC &&
976 L[4]->AM == AM65_ZP_INDY &&
977 !CE_HasLabel (L[4]) &&
978 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
980 L[5]->JumpTo->Owner == L[7] &&
981 !CE_HasLabel (L[5]) &&
982 L[6]->OPC == OP65_INX &&
983 !CE_HasLabel (L[6]) &&
984 L[7]->OPC == OP65_LDY &&
985 CE_KnownImm (L[7]) &&
987 CE_IsCallTo (L[8], "ldauidx") &&
988 !CE_HasLabel (L[8]) &&
989 /* Check the label last because this is quite costly */
990 (Len = strlen (L[0]->Arg)) > 3 &&
991 L[0]->Arg[0] == '<' &&
992 L[0]->Arg[1] == '(' &&
993 strlen (L[1]->Arg) == Len &&
994 L[1]->Arg[0] == '>' &&
995 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1001 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
1002 CS_InsertEntry (S, X, I+3);
1005 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1006 CS_InsertEntry (S, X, I+4);
1009 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1010 CS_InsertEntry (S, X, I+5);
1013 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1014 Label[Len-3] = '\0';
1015 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1016 CS_InsertEntry (S, X, I+6);
1019 /* Remove the old code */
1020 CS_DelEntries (S, I, 2);
1021 CS_DelEntries (S, I+5, 6);
1023 /* Remember, we had changes */
1033 /* Return the number of changes made */
1039 static unsigned OptPtrLoad6 (CodeSeg* S)
1040 /* Search for the sequence
1045 * and replace it by:
1053 * This step must be execute *after* OptPtrLoad1!
1056 unsigned Changes = 0;
1058 /* Walk over the entries */
1060 while (I < CS_GetEntryCount (S)) {
1064 /* Get next entry */
1065 L[0] = CS_GetEntry (S, I);
1067 /* Check for the sequence */
1068 if (L[0]->OPC == OP65_LDY &&
1069 CS_GetEntries (S, L+1, I+1, 1) &&
1070 CE_IsCallTo (L[1], "ldauidx") &&
1071 !CE_HasLabel (L[1])) {
1075 /* Store the high byte */
1076 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1077 CS_InsertEntry (S, X, I+1);
1079 /* Store the low byte */
1080 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1081 CS_InsertEntry (S, X, I+2);
1083 /* Delete the call to ldauidx */
1084 CS_DelEntry (S, I+3);
1086 /* Load the high and low byte */
1087 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1088 CS_InsertEntry (S, X, I+3);
1089 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1090 CS_InsertEntry (S, X, I+4);
1092 /* Remember, we had changes */
1102 /* Return the number of changes made */
1108 /*****************************************************************************/
1109 /* Decouple operations */
1110 /*****************************************************************************/
1114 static unsigned OptDecouple (CodeSeg* S)
1115 /* Decouple operations, that is, do the following replacements:
1125 * lda zp -> lda #imm
1126 * ldx zp -> ldx #imm
1127 * ldy zp -> ldy #imm
1129 * Provided that the register values are known of course.
1132 unsigned Changes = 0;
1135 /* Generate register info for the following step */
1138 /* Walk over the entries */
1140 while (I < CS_GetEntryCount (S)) {
1144 /* Get next entry and it's input register values */
1145 CodeEntry* E = CS_GetEntry (S, I);
1146 const RegContents* In = &E->RI->In;
1148 /* Assume we have no replacement */
1151 /* Check the instruction */
1155 if (E->RI->In.RegX >= 0) {
1156 Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
1157 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1162 if (E->RI->In.RegY >= 0) {
1163 Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1164 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1169 if (E->RI->In.RegX >= 0) {
1170 Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1171 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1176 if (E->RI->In.RegY >= 0) {
1177 Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1178 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1183 if (E->AM == AM65_ZP) {
1184 switch (GetKnownReg (E->Use, In)) {
1186 Arg = MakeHexArg (In->Tmp1);
1187 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1191 Arg = MakeHexArg (In->SRegLo);
1192 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1196 Arg = MakeHexArg (In->SRegHi);
1197 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1204 if (E->AM == AM65_ZP) {
1205 switch (GetKnownReg (E->Use, In)) {
1207 Arg = MakeHexArg (In->Tmp1);
1208 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1212 Arg = MakeHexArg (In->SRegLo);
1213 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1217 Arg = MakeHexArg (In->SRegHi);
1218 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1225 if (E->AM == AM65_ZP) {
1226 switch (GetKnownReg (E->Use, In)) {
1228 Arg = MakeHexArg (In->Tmp1);
1229 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1233 Arg = MakeHexArg (In->SRegLo);
1234 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1238 Arg = MakeHexArg (In->SRegHi);
1239 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1246 if (E->RI->In.RegA >= 0) {
1247 Arg = MakeHexArg (In->RegA);
1248 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1253 if (E->RI->In.RegA >= 0) {
1254 Arg = MakeHexArg (In->RegA);
1255 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1260 if (E->RI->In.RegX >= 0) {
1261 Arg = MakeHexArg (In->RegX);
1262 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1267 if (E->RI->In.RegY >= 0) {
1268 Arg = MakeHexArg (In->RegY);
1269 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1274 /* Avoid gcc warnings */
1279 /* Insert the replacement if we have one */
1281 CS_InsertEntry (S, X, I+1);
1291 /* Free register info */
1294 /* Return the number of changes made */
1300 /*****************************************************************************/
1301 /* struct OptFunc */
1302 /*****************************************************************************/
1306 typedef struct OptFunc OptFunc;
1308 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1309 const char* Name; /* Name of the function/group */
1310 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1311 unsigned long TotalRuns; /* Total number of runs */
1312 unsigned long LastRuns; /* Last number of runs */
1313 unsigned long TotalChanges; /* Total number of changes */
1314 unsigned long LastChanges; /* Last number of changes */
1315 char Disabled; /* True if function disabled */
1320 /*****************************************************************************/
1322 /*****************************************************************************/
1326 /* A list of all the function descriptions */
1327 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1328 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1329 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1330 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1331 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1332 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 40, 0, 0, 0, 0, 0 };
1333 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1334 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1335 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1336 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1337 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1338 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1339 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1340 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1341 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1342 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1343 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1344 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1345 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1346 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1347 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1348 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1349 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1350 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1351 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1352 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1353 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1354 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1355 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1356 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1357 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1358 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1359 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1360 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1361 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1362 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 100, 0, 0, 0, 0, 0 };
1363 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 100, 0, 0, 0, 0, 0 };
1364 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1365 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 100, 0, 0, 0, 0, 0 };
1366 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1367 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1368 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1369 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1370 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1371 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1372 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1373 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1374 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1375 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1376 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1377 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1378 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0, 0 };
1379 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1380 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1383 /* Table containing all the steps in alphabetical order */
1384 static OptFunc* OptFuncs[] = {
1440 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1444 static int CmpOptStep (const void* Key, const void* Func)
1445 /* Compare function for bsearch */
1447 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1452 static OptFunc* FindOptFunc (const char* Name)
1453 /* Find an optimizer step by name in the table and return a pointer. Return
1454 * NULL if no such step is found.
1457 /* Search for the function in the list */
1458 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1464 static OptFunc* GetOptFunc (const char* Name)
1465 /* Find an optimizer step by name in the table and return a pointer. Print an
1466 * error and call AbEnd if not found.
1469 /* Search for the function in the list */
1470 OptFunc* F = FindOptFunc (Name);
1473 AbEnd ("Optimization step `%s' not found", Name);
1480 void DisableOpt (const char* Name)
1481 /* Disable the optimization with the given name */
1483 if (strcmp (Name, "any") == 0) {
1485 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1486 OptFuncs[I]->Disabled = 1;
1489 GetOptFunc(Name)->Disabled = 1;
1495 void EnableOpt (const char* Name)
1496 /* Enable the optimization with the given name */
1498 if (strcmp (Name, "any") == 0) {
1500 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1501 OptFuncs[I]->Disabled = 0;
1504 GetOptFunc(Name)->Disabled = 0;
1510 void ListOptSteps (FILE* F)
1511 /* List all optimization steps */
1514 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1515 fprintf (F, "%s\n", OptFuncs[I]->Name);
1521 static void ReadOptStats (const char* Name)
1522 /* Read the optimizer statistics file */
1527 /* Try to open the file */
1528 FILE* F = fopen (Name, "r");
1530 /* Ignore the error */
1534 /* Read and parse the lines */
1536 while (fgets (Buf, sizeof (Buf), F) != 0) {
1544 unsigned long TotalRuns;
1545 unsigned long TotalChanges;
1550 /* Remove trailing white space including the line terminator */
1553 while (Len > 0 && IsSpace (B[Len-1])) {
1558 /* Remove leading whitespace */
1559 while (IsSpace (*B)) {
1563 /* Check for empty and comment lines */
1564 if (*B == '\0' || *B == ';' || *B == '#') {
1568 /* Parse the line */
1569 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1574 /* Search for the optimizer step. */
1575 Func = FindOptFunc (Name);
1581 /* Found the step, set the fields */
1582 Func->TotalRuns = TotalRuns;
1583 Func->TotalChanges = TotalChanges;
1587 /* Close the file, ignore errors here. */
1593 static void WriteOptStats (const char* Name)
1594 /* Write the optimizer statistics file */
1598 /* Try to open the file */
1599 FILE* F = fopen (Name, "w");
1601 /* Ignore the error */
1605 /* Write a header */
1607 "; Optimizer Total Last Total Last\n"
1608 "; Step Runs Runs Chg Chg\n");
1611 /* Write the data */
1612 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1613 const OptFunc* O = OptFuncs[I];
1615 "%-20s %6lu %6lu %6lu %6lu\n",
1623 /* Close the file, ignore errors here. */
1629 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1630 /* Run one optimizer function Max times or until there are no more changes */
1632 unsigned Changes, C;
1634 /* Don't run the function if it is disabled or if it is prohibited by the
1637 if (F->Disabled || CodeSizeFactor < F->CodeSizeFactor) {
1641 /* Run this until there are no more changes */
1645 /* Run the function */
1652 F->TotalChanges += C;
1653 F->LastChanges += C;
1655 } while (--Max && C > 0);
1657 /* Return the number of changes */
1663 static unsigned RunOptGroup1 (CodeSeg* S)
1664 /* Run the first group of optimization steps. These steps translate known
1665 * patterns emitted by the code generator into more optimal patterns. Order
1666 * of the steps is important, because some of the steps done earlier cover
1667 * the same patterns as later steps as subpatterns.
1670 unsigned Changes = 0;
1672 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1673 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1674 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1675 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1676 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1677 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1678 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1679 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1680 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1681 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1682 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1683 Changes += RunOptFunc (S, &DOptAdd1, 1);
1684 Changes += RunOptFunc (S, &DOptAdd2, 1);
1685 Changes += RunOptFunc (S, &DOptShift1, 1);
1686 Changes += RunOptFunc (S, &DOptShift2, 1);
1687 Changes += RunOptFunc (S, &DOptShift3, 1);
1689 /* Return the number of changes */
1695 static unsigned RunOptGroup2 (CodeSeg* S)
1696 /* Run one group of optimization steps. This step involves just decoupling
1697 * instructions by replacing them by instructions that do not depend on
1698 * previous instructions. This makes it easier to find instructions that
1702 unsigned Changes = 0;
1704 Changes += RunOptFunc (S, &DOptDecouple, 1);
1706 /* Return the number of changes */
1712 static unsigned RunOptGroup3 (CodeSeg* S)
1713 /* Run one group of optimization steps. These steps depend on each other,
1714 * that means that one step may allow another step to do additional work,
1715 * so we will repeat the steps as long as we see any changes.
1718 unsigned Changes, C;
1724 C += RunOptFunc (S, &DOptPtrLoad6, 1);
1725 C += RunOptFunc (S, &DOptNegA1, 1);
1726 C += RunOptFunc (S, &DOptNegA2, 1);
1727 C += RunOptFunc (S, &DOptSub1, 1);
1728 C += RunOptFunc (S, &DOptSub2, 1);
1729 C += RunOptFunc (S, &DOptAdd3, 1);
1730 C += RunOptFunc (S, &DOptStackOps, 1);
1731 C += RunOptFunc (S, &DOptJumpCascades, 1);
1732 C += RunOptFunc (S, &DOptDeadJumps, 1);
1733 C += RunOptFunc (S, &DOptRTS, 1);
1734 C += RunOptFunc (S, &DOptDeadCode, 1);
1735 C += RunOptFunc (S, &DOptJumpTarget, 1);
1736 C += RunOptFunc (S, &DOptCondBranches, 1);
1737 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1738 C += RunOptFunc (S, &DOptBoolTrans, 1);
1739 C += RunOptFunc (S, &DOptCmp1, 1);
1740 C += RunOptFunc (S, &DOptCmp2, 1);
1741 C += RunOptFunc (S, &DOptCmp3, 1);
1742 C += RunOptFunc (S, &DOptCmp4, 1);
1743 C += RunOptFunc (S, &DOptCmp5, 1);
1744 C += RunOptFunc (S, &DOptCmp6, 1);
1745 C += RunOptFunc (S, &DOptCmp7, 1);
1746 C += RunOptFunc (S, &DOptTest1, 1);
1747 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1748 C += RunOptFunc (S, &DOptUnusedStores, 1);
1749 C += RunOptFunc (S, &DOptDupLoads, 1);
1750 C += RunOptFunc (S, &DOptStoreLoad, 1);
1751 C += RunOptFunc (S, &DOptTransfers, 1);
1752 C += RunOptFunc (S, &DOptPushPop, 1);
1758 /* Return the number of changes */
1764 static unsigned RunOptGroup4 (CodeSeg* S)
1765 /* 65C02 specific optimizations. */
1767 unsigned Changes = 0;
1769 if (CPU >= CPU_65C02) {
1770 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1771 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1772 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1774 /* The 65C02 replacement codes do often make the use of a register
1775 * value unnecessary, so if we have changes, run another load
1778 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1782 /* Return the number of changes */
1788 static unsigned RunOptGroup5 (CodeSeg* S)
1789 /* Run another round of pattern replacements. These are done late, since there
1790 * may be better replacements before.
1793 unsigned Changes = 0;
1795 Changes += RunOptFunc (S, &DOptPush1, 1);
1797 /* Return the number of changes */
1803 static unsigned RunOptGroup6 (CodeSeg* S)
1804 /* The last group of optimization steps. Adjust branches, do size optimizations.
1807 unsigned Changes = 0;
1810 if (CodeSizeFactor <= 100) {
1811 /* Optimize for size, that is replace operations by shorter ones, even
1812 * if this does hinder further optimizations (no problem since we're
1815 C = RunOptFunc (S, &DOptSize1, 1);
1818 /* Run some optimization passes again, since the size optimizations
1819 * may have opened new oportunities.
1821 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1822 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1825 C = RunOptFunc (S, &DOptSize2, 1);
1828 /* Run some optimization passes again, since the size optimizations
1829 * may have opened new oportunities.
1831 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1832 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1835 /* Adjust branch distances */
1836 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1838 /* Replace conditional branches to RTS. If we had changes, we must run dead
1839 * code elimination again, since the change may have introduced dead code.
1841 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1844 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1847 /* Return the number of changes */
1853 void RunOpt (CodeSeg* S)
1854 /* Run the optimizer */
1856 const char* StatFileName;
1858 /* If we shouldn't run the optimizer, bail out */
1863 /* Check if we are requested to write optimizer statistics */
1864 StatFileName = getenv ("CC65_OPTSTATS");
1866 ReadOptStats (StatFileName);
1869 /* Print the name of the function we are working on */
1871 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1873 Print (stdout, 1, "Running optimizer for global code segment\n");
1876 /* Run groups of optimizations */
1884 /* Write statistics */
1886 WriteOptStats (StatFileName);