1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static unsigned OptShift1 (CodeSeg* S)
73 /* A call to the shlaxN routine may get replaced by one or more asl insns
74 * if the value of X is not used later.
79 /* Walk over the entries */
81 while (I < CS_GetEntryCount (S)) {
84 CodeEntry* E = CS_GetEntry (S, I);
86 /* Check for the sequence */
87 if (E->OPC == OP65_JSR &&
88 (strncmp (E->Arg, "shlax", 5) == 0 ||
89 strncmp (E->Arg, "aslax", 5) == 0) &&
90 strlen (E->Arg) == 6 &&
91 IsDigit (E->Arg[5]) &&
94 /* Insert shift insns */
95 unsigned Count = E->Arg[5] - '0';
97 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
98 CS_InsertEntry (S, X, I+1);
101 /* Delete the call to shlax */
104 /* Remember, we had changes */
114 /* Return the number of changes made */
120 static unsigned OptShift2 (CodeSeg* S)
121 /* A call to the shraxN routine may get replaced by one or more lsr insns
122 * if the value of X is not used later.
125 unsigned Changes = 0;
127 /* Walk over the entries */
129 while (I < CS_GetEntryCount (S)) {
132 CodeEntry* E = CS_GetEntry (S, I);
134 /* Check for the sequence */
135 if (E->OPC == OP65_JSR &&
136 strncmp (E->Arg, "shrax", 5) == 0 &&
137 strlen (E->Arg) == 6 &&
138 IsDigit (E->Arg[5]) &&
139 !RegXUsed (S, I+1)) {
141 /* Insert shift insns */
142 unsigned Count = E->Arg[5] - '0';
144 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
145 CS_InsertEntry (S, X, I+1);
148 /* Delete the call to shlax */
151 /* Remember, we had changes */
161 /* Return the number of changes made */
167 /*****************************************************************************/
168 /* Optimize stores through pointers */
169 /*****************************************************************************/
173 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
174 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
176 /* Check for a label attached to the entry */
177 if (CE_HasLabel (L[0])) {
181 /* Check for single insn sub ops */
182 if (L[0]->OPC == OP65_AND ||
183 L[0]->OPC == OP65_EOR ||
184 L[0]->OPC == OP65_ORA ||
185 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
186 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
191 } else if (L[0]->OPC == OP65_CLC &&
192 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
193 L[1]->OPC == OP65_ADC &&
194 !CE_HasLabel (L[1])) {
196 } else if (L[0]->OPC == OP65_SEC &&
197 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
198 L[1]->OPC == OP65_SBC &&
199 !CE_HasLabel (L[1])) {
211 static unsigned OptPtrStore1 (CodeSeg* S)
212 /* Search for the sequence:
233 unsigned Changes = 0;
235 /* Walk over the entries */
237 while (I < CS_GetEntryCount (S)) {
243 L[0] = CS_GetEntry (S, I);
245 /* Check for the sequence */
246 if (CE_IsCall (L[0], "pushax") &&
247 CS_GetEntries (S, L+1, I+1, 3) &&
248 L[1]->OPC == OP65_LDY &&
249 CE_KnownImm (L[1]) &&
250 !CE_HasLabel (L[1]) &&
251 CE_IsCall (L[2], "ldauidx") &&
252 !CE_HasLabel (L[2]) &&
253 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
254 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
255 L[3+K]->OPC == OP65_LDY &&
256 CE_KnownImm (L[3+K]) &&
257 !CE_HasLabel (L[3+K]) &&
258 CE_IsCall (L[4+K], "staspidx") &&
259 !CE_HasLabel (L[4+K])) {
263 /* Create and insert the stores */
264 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
265 CS_InsertEntry (S, X, I+1);
267 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
268 CS_InsertEntry (S, X, I+2);
270 /* Delete the call to pushax */
273 /* Delete the call to ldauidx */
274 CS_DelEntry (S, I+3);
276 /* Insert the load from ptr1 */
277 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
278 CS_InsertEntry (S, X, I+3);
279 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
280 CS_InsertEntry (S, X, I+4);
282 /* Insert the store through ptr1 */
283 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
284 CS_InsertEntry (S, X, I+6+K);
286 /* Delete the call to staspidx */
287 CS_DelEntry (S, I+7+K);
289 /* Remember, we had changes */
299 /* Return the number of changes made */
305 static unsigned OptPtrStore2 (CodeSeg* S)
306 /* Search for the sequence:
322 unsigned Changes = 0;
324 /* Walk over the entries */
326 while (I < CS_GetEntryCount (S)) {
331 L[0] = CS_GetEntry (S, I);
333 /* Check for the sequence */
334 if (CE_IsCall (L[0], "pushax") &&
335 CS_GetEntries (S, L+1, I+1, 3) &&
336 L[1]->OPC == OP65_LDA &&
337 !CE_HasLabel (L[1]) &&
338 L[2]->OPC == OP65_LDY &&
339 !CE_HasLabel (L[2]) &&
340 CE_IsCall (L[3], "staspidx") &&
341 !CE_HasLabel (L[3])) {
345 /* Create and insert the stores */
346 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
347 CS_InsertEntry (S, X, I+1);
349 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
350 CS_InsertEntry (S, X, I+2);
352 /* Delete the call to pushax */
355 /* Insert the store through ptr1 */
356 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
357 CS_InsertEntry (S, X, I+4);
359 /* Delete the call to staspidx */
360 CS_DelEntry (S, I+5);
362 /* Remember, we had changes */
372 /* Return the number of changes made */
378 /*****************************************************************************/
379 /* Optimize loads through pointers */
380 /*****************************************************************************/
384 static unsigned OptPtrLoad1 (CodeSeg* S)
385 /* Search for the sequence:
389 * lda (sp),y # May be any destination
404 unsigned Changes = 0;
406 /* Walk over the entries */
408 while (I < CS_GetEntryCount (S)) {
413 L[0] = CS_GetEntry (S, I);
415 /* Check for the sequence */
416 if (L[0]->OPC == OP65_TAX &&
417 CS_GetEntries (S, L+1, I+1, 4) &&
418 L[1]->OPC == OP65_DEY &&
419 !CE_HasLabel (L[1]) &&
420 L[2]->OPC == OP65_LDA &&
421 !CE_HasLabel (L[2]) &&
422 L[3]->OPC == OP65_LDY &&
423 !CE_HasLabel (L[3]) &&
424 CE_IsCall (L[4], "ldauidx") &&
425 !CE_HasLabel (L[4])) {
429 /* Store the high byte and remove the TAX instead */
430 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
431 CS_InsertEntry (S, X, I+1);
434 /* Store the low byte */
435 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
436 CS_InsertEntry (S, X, I+3);
438 /* Delete the call to ldauidx */
439 CS_DelEntry (S, I+5);
441 /* Load high and low byte */
442 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
443 CS_InsertEntry (S, X, I+5);
444 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
445 CS_InsertEntry (S, X, I+6);
447 /* Remember, we had changes */
457 /* Return the number of changes made */
463 static unsigned OptPtrLoad2 (CodeSeg* S)
464 /* Search for the sequence:
489 unsigned Changes = 0;
491 /* Walk over the entries */
493 while (I < CS_GetEntryCount (S)) {
498 L[0] = CS_GetEntry (S, I);
500 /* Check for the sequence */
501 if (L[0]->OPC == OP65_CLC &&
502 CS_GetEntries (S, L+1, I+1, 8) &&
503 L[1]->OPC == OP65_ADC &&
504 !CE_HasLabel (L[1]) &&
505 L[2]->OPC == OP65_TAY &&
506 !CE_HasLabel (L[2]) &&
507 L[3]->OPC == OP65_TXA &&
508 !CE_HasLabel (L[3]) &&
509 L[4]->OPC == OP65_ADC &&
510 !CE_HasLabel (L[4]) &&
511 L[5]->OPC == OP65_TAX &&
512 !CE_HasLabel (L[5]) &&
513 L[6]->OPC == OP65_TYA &&
514 !CE_HasLabel (L[6]) &&
515 L[7]->OPC == OP65_LDY &&
516 !CE_HasLabel (L[7]) &&
517 CE_IsCall (L[8], "ldauidx") &&
518 !CE_HasLabel (L[8])) {
523 /* Store the low byte and remove the TAY instead */
524 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
525 CS_InsertEntry (S, X, I+2);
526 CS_DelEntry (S, I+3);
528 /* Store the high byte */
529 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
530 CS_InsertEntry (S, X, I+5);
532 /* If the instruction before the adc is a ldx, replace the
533 * txa by and lda with the same location of the ldx.
535 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
536 P->OPC == OP65_LDX &&
539 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
540 CS_InsertEntry (S, X, I+4);
541 CS_DelEntry (S, I+3);
544 /* Delete more transfer insns */
545 CS_DelEntry (S, I+7);
546 CS_DelEntry (S, I+6);
548 /* Delete the call to ldauidx */
549 CS_DelEntry (S, I+7);
551 /* Load high and low byte */
552 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
553 CS_InsertEntry (S, X, I+7);
554 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
555 CS_InsertEntry (S, X, I+8);
557 /* Remember, we had changes */
567 /* Return the number of changes made */
573 static unsigned OptPtrLoad3 (CodeSeg* S)
574 /* Search for the sequence:
599 unsigned Changes = 0;
601 /* Walk over the entries */
603 while (I < CS_GetEntryCount (S)) {
608 L[0] = CS_GetEntry (S, I);
610 /* Check for the sequence */
611 if (L[0]->OPC == OP65_ADC &&
612 CS_GetEntries (S, L+1, I+1, 8) &&
613 L[1]->OPC == OP65_PHA &&
614 !CE_HasLabel (L[1]) &&
615 L[2]->OPC == OP65_TXA &&
616 !CE_HasLabel (L[2]) &&
617 L[3]->OPC == OP65_INY &&
618 !CE_HasLabel (L[3]) &&
619 L[4]->OPC == OP65_ADC &&
620 !CE_HasLabel (L[4]) &&
621 L[5]->OPC == OP65_TAX &&
622 !CE_HasLabel (L[5]) &&
623 L[6]->OPC == OP65_PLA &&
624 !CE_HasLabel (L[6]) &&
625 L[7]->OPC == OP65_LDY &&
626 !CE_HasLabel (L[7]) &&
627 CE_IsCall (L[8], "ldauidx") &&
628 !CE_HasLabel (L[8])) {
632 /* Store the low byte and remove the PHA instead */
633 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
634 CS_InsertEntry (S, X, I+1);
635 CS_DelEntry (S, I+2);
637 /* Store the high byte */
638 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
639 CS_InsertEntry (S, X, I+5);
641 /* Delete more transfer and PLA insns */
642 CS_DelEntry (S, I+7);
643 CS_DelEntry (S, I+6);
645 /* Delete the call to ldauidx */
646 CS_DelEntry (S, I+7);
648 /* Load high and low byte */
649 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
650 CS_InsertEntry (S, X, I+7);
651 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
652 CS_InsertEntry (S, X, I+8);
654 /* Remember, we had changes */
664 /* Return the number of changes made */
670 static unsigned OptPtrLoad4 (CodeSeg* S)
671 /* Search for the sequence:
689 unsigned Changes = 0;
691 /* Walk over the entries */
693 while (I < CS_GetEntryCount (S)) {
699 L[0] = CS_GetEntry (S, I);
701 /* Check for the sequence */
702 if (L[0]->OPC == OP65_LDA &&
703 L[0]->AM == AM65_IMM &&
704 CS_GetEntries (S, L+1, I+1, 7) &&
705 L[1]->OPC == OP65_LDX &&
706 L[1]->AM == AM65_IMM &&
707 !CE_HasLabel (L[1]) &&
708 L[2]->OPC == OP65_CLC &&
709 !CE_HasLabel (L[2]) &&
710 L[3]->OPC == OP65_ADC &&
711 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
712 !CE_HasLabel (L[3]) &&
713 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
715 L[4]->JumpTo->Owner == L[6] &&
716 !CE_HasLabel (L[4]) &&
717 L[5]->OPC == OP65_INX &&
718 !CE_HasLabel (L[5]) &&
719 L[6]->OPC == OP65_LDY &&
720 CE_KnownImm (L[6]) &&
722 CE_IsCall (L[7], "ldauidx") &&
723 !CE_HasLabel (L[7]) &&
724 /* Check the label last because this is quite costly */
725 (Len = strlen (L[0]->Arg)) > 3 &&
726 L[0]->Arg[0] == '<' &&
727 L[0]->Arg[1] == '(' &&
728 strlen (L[1]->Arg) == Len &&
729 L[1]->Arg[0] == '>' &&
730 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
735 /* We will create all the new stuff behind the current one so
736 * we keep the line references.
738 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
739 CS_InsertEntry (S, X, I+8);
741 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
742 CS_InsertEntry (S, X, I+9);
744 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
746 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
747 CS_InsertEntry (S, X, I+10);
750 /* Remove the old code */
751 CS_DelEntries (S, I, 8);
753 /* Remember, we had changes */
763 /* Return the number of changes made */
769 static unsigned OptPtrLoad5 (CodeSeg* S)
770 /* Search for the sequence:
791 unsigned Changes = 0;
793 /* Walk over the entries */
795 while (I < CS_GetEntryCount (S)) {
801 L[0] = CS_GetEntry (S, I);
803 /* Check for the sequence */
804 if (L[0]->OPC == OP65_LDA &&
805 L[0]->AM == AM65_IMM &&
806 CS_GetEntries (S, L+1, I+1, 8) &&
807 L[1]->OPC == OP65_LDX &&
808 L[1]->AM == AM65_IMM &&
809 !CE_HasLabel (L[1]) &&
810 L[2]->OPC == OP65_LDY &&
811 CE_KnownImm (L[2]) &&
812 !CE_HasLabel (L[2]) &&
813 L[3]->OPC == OP65_CLC &&
814 !CE_HasLabel (L[3]) &&
815 L[4]->OPC == OP65_ADC &&
816 L[4]->AM == AM65_ZP_INDY &&
817 !CE_HasLabel (L[4]) &&
818 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
820 L[5]->JumpTo->Owner == L[7] &&
821 !CE_HasLabel (L[5]) &&
822 L[6]->OPC == OP65_INX &&
823 !CE_HasLabel (L[6]) &&
824 L[7]->OPC == OP65_LDY &&
825 CE_KnownImm (L[7]) &&
827 CE_IsCall (L[8], "ldauidx") &&
828 !CE_HasLabel (L[8]) &&
829 /* Check the label last because this is quite costly */
830 (Len = strlen (L[0]->Arg)) > 3 &&
831 L[0]->Arg[0] == '<' &&
832 L[0]->Arg[1] == '(' &&
833 strlen (L[1]->Arg) == Len &&
834 L[1]->Arg[0] == '>' &&
835 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
841 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
842 CS_InsertEntry (S, X, I+3);
845 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
846 CS_InsertEntry (S, X, I+4);
849 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
850 CS_InsertEntry (S, X, I+5);
853 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
855 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
856 CS_InsertEntry (S, X, I+6);
859 /* Remove the old code */
860 CS_DelEntries (S, I, 2);
861 CS_DelEntries (S, I+5, 6);
863 /* Remember, we had changes */
873 /* Return the number of changes made */
879 static unsigned OptPtrLoad6 (CodeSeg* S)
880 /* Search for the sequence
893 * This step must be execute *after* OptPtrLoad1!
896 unsigned Changes = 0;
898 /* Walk over the entries */
900 while (I < CS_GetEntryCount (S)) {
905 L[0] = CS_GetEntry (S, I);
907 /* Check for the sequence */
908 if (L[0]->OPC == OP65_LDY &&
909 CS_GetEntries (S, L+1, I+1, 1) &&
910 CE_IsCall (L[1], "ldauidx") &&
911 !CE_HasLabel (L[1])) {
915 /* Store the high byte */
916 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
917 CS_InsertEntry (S, X, I+1);
919 /* Store the low byte */
920 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
921 CS_InsertEntry (S, X, I+2);
923 /* Delete the call to ldauidx */
924 CS_DelEntry (S, I+3);
926 /* Load the high and low byte */
927 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
928 CS_InsertEntry (S, X, I+3);
929 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
930 CS_InsertEntry (S, X, I+4);
932 /* Remember, we had changes */
942 /* Return the number of changes made */
948 /*****************************************************************************/
949 /* Decouple operations */
950 /*****************************************************************************/
954 static unsigned OptDecouple (CodeSeg* S)
955 /* Decouple operations, that is, do the following replacements:
969 * Provided that the register values are known of course.
972 unsigned Changes = 0;
975 /* Generate register info for the following step */
978 /* Walk over the entries */
980 while (I < CS_GetEntryCount (S)) {
984 /* Get next entry and it's input register values */
985 CodeEntry* E = CS_GetEntry (S, I);
986 const RegContents* In = &E->RI->In;
988 /* Assume we have no replacement */
991 /* Check the instruction */
995 if (E->RI->In.RegX >= 0) {
996 Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
997 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1002 if (E->RI->In.RegY >= 0) {
1003 Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1004 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1009 if (E->RI->In.RegX >= 0) {
1010 Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1011 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1016 if (E->RI->In.RegY >= 0) {
1017 Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1018 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1023 if (E->AM == AM65_ZP) {
1024 switch (GetKnownReg (E->Use, In)) {
1026 Arg = MakeHexArg (In->Tmp1);
1027 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1031 Arg = MakeHexArg (In->SRegLo);
1032 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1036 Arg = MakeHexArg (In->SRegHi);
1037 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1044 if (E->AM == AM65_ZP) {
1045 switch (GetKnownReg (E->Use, In)) {
1047 Arg = MakeHexArg (In->Tmp1);
1048 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1052 Arg = MakeHexArg (In->SRegLo);
1053 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1057 Arg = MakeHexArg (In->SRegHi);
1058 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1065 if (E->AM == AM65_ZP) {
1066 switch (GetKnownReg (E->Use, In)) {
1068 Arg = MakeHexArg (In->Tmp1);
1069 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1073 Arg = MakeHexArg (In->SRegLo);
1074 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1078 Arg = MakeHexArg (In->SRegHi);
1079 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1086 if (E->RI->In.RegA >= 0) {
1087 Arg = MakeHexArg (In->RegA);
1088 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1093 if (E->RI->In.RegA >= 0) {
1094 Arg = MakeHexArg (In->RegA);
1095 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1100 if (E->RI->In.RegX >= 0) {
1101 Arg = MakeHexArg (In->RegX);
1102 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1107 if (E->RI->In.RegY >= 0) {
1108 Arg = MakeHexArg (In->RegY);
1109 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1114 /* Avoid gcc warnings */
1119 /* Insert the replacement if we have one */
1121 CS_InsertEntry (S, X, I+1);
1131 /* Free register info */
1134 /* Return the number of changes made */
1140 /*****************************************************************************/
1141 /* Size optimization */
1142 /*****************************************************************************/
1147 static unsigned OptSize1 (CodeSeg* S)
1148 /* Do size optimization by calling special subroutines that preload registers.
1149 * This routine does not work standalone, it needs a following register load
1153 static const char* Func = {
1154 "stax0sp", /* staxysp, y = 0 */
1156 "ldax0sp", /* ldaxysp, y = 1 */
1157 "ldeax0sp", /* ldeaxysp, y = 3 */
1158 "push0", /* pushax, a = 0, x = 0 */
1159 "pusha0", /* pushax, x = 0 */
1160 "pushaFF", /* pushax, x = ff */
1161 "pusha0sp", /* pushaysp, y = 0 */
1162 "tosadda0", /* tosaddax, x = 0 */
1163 "tosanda0", /* tosandax, x = 0 */
1164 "tosdiva0", /* tosdivax, x = 0 */
1165 "toseqa0", /* toseqax, x = 0 */
1166 "tosgea0", /* tosgeax, x = 0 */
1167 "tosgta0", /* tosgtax, x = 0 */
1168 "tosadd0ax", /* tosaddeax, sreg = 0 */
1169 "laddeqa", /* laddeq, sreg = 0, x = 0 */
1170 "laddeq1", /* laddeq, sreg = 0, x = 0, a = 1 */
1171 "laddeq0sp", /* laddeqysp, y = 0 */
1172 "tosand0ax", /* tosandeax, sreg = 0 */
1173 "ldaxi", /* ldaxidx, y = 1 */
1174 "ldeaxi", /* ldeaxidx, y = 3 */
1175 "ldeax0sp", /* ldeaxysp, y = 3 */
1176 "tosdiv0ax", /* tosdiveax, sreg = 0 */
1177 "toslea0", /* tosleax, x = 0 */
1178 "tosmod0ax", /* tosmodeax, sreg = 0 */
1179 "tosmul0ax", /* tosmuleax, sreg = 0 */
1180 "tosumul0ax", /* tosumuleax, sreg = 0 */
1181 "tosor0ax", /* tosoreax, sreg = 0 */
1182 "push0ax", /* pusheax, sreg = 0 */
1183 "tosrsub0ax", /* tosrsubeax, sreg = 0 */
1184 "tosshl0ax", /* tosshleax, sreg = 0 */
1185 "tosasl0ax", /* tosasleax, sreg = 0 */
1186 "tosshr0ax", /* tosshreax, sreg = 0 */
1187 "tosasr0ax", /* tosasreax, sreg = 0 */
1188 "tossub0ax", /* tossubeax, sreg = 0 */
1189 "lsubeqa", /* lsubeq, sreg = 0, x = 0 */
1190 "lsubeq1", /* lsubeq, sreg = 0, x = 0, a = 1 */
1191 "lsubeq0sp", /* lsubeqysp, y = 0 */
1192 "toslta0", /* tosltax, x = 0 */
1193 "tosudiv0ax", /* tosudiveax, sreg = 0 */
1194 "tosumod0ax", /* tosumodeax, sreg = 0 */
1195 "tosxor0ax", /* tosxoreax, sreg = 0 */
1196 "tosmoda0", /* tosmodax, x = 0 */
1197 "tosmula0", /* tosmulax, x = 0 */
1198 "tosumula0", /* tosumulax, x = 0 */
1199 "tosnea0", /* tosneax, x = 0 */
1200 "tosora0", /* tosorax, x = 0 */
1201 "push1", /* pushax, x = 0, a = 1 */
1202 "push2", /* pushax, x = 0, a = 2 */
1203 "push3", /* pushax, x = 0, a = 3 */
1204 "push4", /* pushax, x = 0, a = 4 */
1205 "push5", /* pushax, x = 0, a = 5 */
1206 "push6", /* pushax, x = 0, a = 6 */
1207 "push7", /* pushax, x = 0, a = 7 */
1208 "pushc0", /* pusha, a = 0 */
1209 "pushc1", /* pusha, a = 1 */
1210 "pushc2", /* pusha, a = 2 */
1211 "tosrsuba0", /* tosrsubax, x = 0 */
1212 "tosshla0", /* tosshlax, x = 0 */
1213 "tosasla0", /* tosaslax, x = 0 */
1214 "tosshra0", /* tosshrax, x = 0 */
1215 "tosasra0", /* tosasrax, x = 0 */
1216 "steax0sp", /* steaxsp, y = 0 */
1217 "tossuba0", /* tossubax, x = 0 */
1218 "subeq0sp", /* subeqysp, y = 0 */
1219 "tosudiva0", /* tosudivax, x = 0 */
1220 "tosugea0", /* tosugeax, x = 0 */
1221 "tosugta0", /* tosugtax, x = 0 */
1222 "tosulea0", /* tosuleax, x = 0 */
1223 "tosulta0", /* tosultax, x = 0 */
1224 "tosumoda0", /* tosumodax, x = 0 */
1225 "tosxora0", /* tosxorax, x = 0 */
1228 unsigned Changes = 0;
1231 /* Generate register info for the following step */
1234 /* Walk over the entries */
1236 while (I < CS_GetEntryCount (S)) {
1238 /* Get next entry */
1239 CodeEntry* E = CS_GetEntry (S, I);
1241 /* Check if it's a subroutine call */
1242 if (E->OPC == OP65_JSR) {
1244 /* Check for any of the known functions */
1255 /* Free register info */
1258 /* Return the number of changes made */
1265 static unsigned OptSize2 (CodeSeg* S)
1266 /* Do size optimization by using shorter code sequences, even if this
1267 * introduces relations between instructions. This step must be one of the
1268 * last steps, because it makes further work much more difficult.
1271 unsigned Changes = 0;
1274 /* Generate register info for the following step */
1277 /* Walk over the entries */
1279 while (I < CS_GetEntryCount (S)) {
1282 /* Get next entry */
1283 CodeEntry* E = CS_GetEntry (S, I);
1285 /* Assume we have no replacement */
1288 /* Check the instruction */
1292 if (CE_KnownImm (E)) {
1293 short Val = (short) E->Num;
1294 if (Val == E->RI->In.RegX) {
1295 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
1296 } else if (Val == E->RI->In.RegY) {
1297 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
1298 } else if (E->RI->In.RegA >= 0 && CPU >= CPU_65C02) {
1299 if (Val == ((E->RI->In.RegA - 1) & 0xFF)) {
1300 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
1301 } else if (Val == ((E->RI->In.RegA + 1) & 0xFF)) {
1302 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
1309 if (CE_KnownImm (E)) {
1310 short Val = (short) E->Num;
1311 if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX - 1) & 0xFF)) {
1312 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
1313 } else if (E->RI->In.RegX >= 0 && Val == ((E->RI->In.RegX + 1) & 0xFF)) {
1314 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
1315 } else if (Val == E->RI->In.RegA) {
1316 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
1322 if (CE_KnownImm (E)) {
1323 short Val = (short) E->Num;
1324 if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY - 1) & 0xFF)) {
1325 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
1326 } else if (E->RI->In.RegY >= 0 && Val == ((E->RI->In.RegY + 1) & 0xFF)) {
1327 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
1328 } else if (Val == E->RI->In.RegA) {
1329 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
1335 /* Avoid gcc warnings */
1340 /* Insert the replacement if we have one */
1342 CS_InsertEntry (S, X, I+1);
1352 /* Free register info */
1355 /* Return the number of changes made */
1361 /*****************************************************************************/
1362 /* struct OptFunc */
1363 /*****************************************************************************/
1367 typedef struct OptFunc OptFunc;
1369 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1370 const char* Name; /* Name of the function/group */
1371 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1372 unsigned long TotalRuns; /* Total number of runs */
1373 unsigned long LastRuns; /* Last number of runs */
1374 unsigned long TotalChanges; /* Total number of changes */
1375 unsigned long LastChanges; /* Last number of changes */
1376 char Disabled; /* True if function disabled */
1381 /*****************************************************************************/
1383 /*****************************************************************************/
1387 /* A list of all the function descriptions */
1388 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1389 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1390 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1391 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1392 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1393 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 40, 0, 0, 0, 0, 0 };
1394 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1395 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1396 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1397 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1398 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1399 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1400 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1401 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1402 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1403 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1404 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1405 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1406 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1407 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1408 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1409 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1410 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1411 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1412 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1413 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1414 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1415 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1416 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1417 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1418 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1419 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1420 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1421 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1422 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1423 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 100, 0, 0, 0, 0, 0 };
1424 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 100, 0, 0, 0, 0, 0 };
1425 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1426 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 100, 0, 0, 0, 0, 0 };
1427 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1428 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1429 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1430 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1431 /*static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };*/
1432 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1433 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1434 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1435 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1436 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1437 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1438 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0, 0 };
1439 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1440 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1443 /* Table containing all the steps in alphabetical order */
1444 static OptFunc* OptFuncs[] = {
1499 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1503 static int CmpOptStep (const void* Key, const void* Func)
1504 /* Compare function for bsearch */
1506 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1511 static OptFunc* FindOptFunc (const char* Name)
1512 /* Find an optimizer step by name in the table and return a pointer. Return
1513 * NULL if no such step is found.
1516 /* Search for the function in the list */
1517 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1523 static OptFunc* GetOptFunc (const char* Name)
1524 /* Find an optimizer step by name in the table and return a pointer. Print an
1525 * error and call AbEnd if not found.
1528 /* Search for the function in the list */
1529 OptFunc* F = FindOptFunc (Name);
1532 AbEnd ("Optimization step `%s' not found", Name);
1539 void DisableOpt (const char* Name)
1540 /* Disable the optimization with the given name */
1542 if (strcmp (Name, "any") == 0) {
1544 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1545 OptFuncs[I]->Disabled = 1;
1548 GetOptFunc(Name)->Disabled = 1;
1554 void EnableOpt (const char* Name)
1555 /* Enable the optimization with the given name */
1557 if (strcmp (Name, "any") == 0) {
1559 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1560 OptFuncs[I]->Disabled = 0;
1563 GetOptFunc(Name)->Disabled = 0;
1569 void ListOptSteps (FILE* F)
1570 /* List all optimization steps */
1573 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1574 fprintf (F, "%s\n", OptFuncs[I]->Name);
1580 static void ReadOptStats (const char* Name)
1581 /* Read the optimizer statistics file */
1586 /* Try to open the file */
1587 FILE* F = fopen (Name, "r");
1589 /* Ignore the error */
1593 /* Read and parse the lines */
1595 while (fgets (Buf, sizeof (Buf), F) != 0) {
1603 unsigned long TotalRuns;
1604 unsigned long TotalChanges;
1609 /* Remove trailing white space including the line terminator */
1612 while (Len > 0 && IsSpace (B[Len-1])) {
1617 /* Remove leading whitespace */
1618 while (IsSpace (*B)) {
1622 /* Check for empty and comment lines */
1623 if (*B == '\0' || *B == ';' || *B == '#') {
1627 /* Parse the line */
1628 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1633 /* Search for the optimizer step. */
1634 Func = FindOptFunc (Name);
1640 /* Found the step, set the fields */
1641 Func->TotalRuns = TotalRuns;
1642 Func->TotalChanges = TotalChanges;
1646 /* Close the file, ignore errors here. */
1652 static void WriteOptStats (const char* Name)
1653 /* Write the optimizer statistics file */
1657 /* Try to open the file */
1658 FILE* F = fopen (Name, "w");
1660 /* Ignore the error */
1664 /* Write a header */
1666 "; Optimizer Total Last Total Last\n"
1667 "; Step Runs Runs Chg Chg\n");
1670 /* Write the data */
1671 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1672 const OptFunc* O = OptFuncs[I];
1674 "%-20s %6lu %6lu %6lu %6lu\n",
1682 /* Close the file, ignore errors here. */
1688 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1689 /* Run one optimizer function Max times or until there are no more changes */
1691 unsigned Changes, C;
1693 /* Don't run the function if it is disabled or if it is prohibited by the
1696 if (F->Disabled || CodeSizeFactor < F->CodeSizeFactor) {
1700 /* Run this until there are no more changes */
1704 /* Run the function */
1711 F->TotalChanges += C;
1712 F->LastChanges += C;
1714 } while (--Max && C > 0);
1716 /* Return the number of changes */
1722 static unsigned RunOptGroup1 (CodeSeg* S)
1723 /* Run the first group of optimization steps. These steps translate known
1724 * patterns emitted by the code generator into more optimal patterns. Order
1725 * of the steps is important, because some of the steps done earlier cover
1726 * the same patterns as later steps as subpatterns.
1729 unsigned Changes = 0;
1731 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1732 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1733 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1734 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1735 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1736 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1737 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1738 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1739 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1740 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1741 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1742 Changes += RunOptFunc (S, &DOptAdd1, 1);
1743 Changes += RunOptFunc (S, &DOptAdd2, 1);
1744 Changes += RunOptFunc (S, &DOptShift1, 1);
1745 Changes += RunOptFunc (S, &DOptShift2, 1);
1747 /* Return the number of changes */
1753 static unsigned RunOptGroup2 (CodeSeg* S)
1754 /* Run one group of optimization steps. This step involves just decoupling
1755 * instructions by replacing them by instructions that do not depend on
1756 * previous instructions. This makes it easier to find instructions that
1760 unsigned Changes = 0;
1762 Changes += RunOptFunc (S, &DOptDecouple, 1);
1764 /* Return the number of changes */
1770 static unsigned RunOptGroup3 (CodeSeg* S)
1771 /* Run one group of optimization steps. These steps depend on each other,
1772 * that means that one step may allow another step to do additional work,
1773 * so we will repeat the steps as long as we see any changes.
1776 unsigned Changes, C;
1782 C += RunOptFunc (S, &DOptPtrLoad6, 1);
1783 C += RunOptFunc (S, &DOptNegA1, 1);
1784 C += RunOptFunc (S, &DOptNegA2, 1);
1785 C += RunOptFunc (S, &DOptSub1, 1);
1786 C += RunOptFunc (S, &DOptSub2, 1);
1787 C += RunOptFunc (S, &DOptAdd3, 1);
1788 C += RunOptFunc (S, &DOptStackOps, 1);
1789 C += RunOptFunc (S, &DOptJumpCascades, 1);
1790 C += RunOptFunc (S, &DOptDeadJumps, 1);
1791 C += RunOptFunc (S, &DOptRTS, 1);
1792 C += RunOptFunc (S, &DOptDeadCode, 1);
1793 C += RunOptFunc (S, &DOptJumpTarget, 1);
1794 C += RunOptFunc (S, &DOptCondBranches, 1);
1795 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1796 C += RunOptFunc (S, &DOptBoolTrans, 1);
1797 C += RunOptFunc (S, &DOptCmp1, 1);
1798 C += RunOptFunc (S, &DOptCmp2, 1);
1799 C += RunOptFunc (S, &DOptCmp3, 1);
1800 C += RunOptFunc (S, &DOptCmp4, 1);
1801 C += RunOptFunc (S, &DOptCmp5, 1);
1802 C += RunOptFunc (S, &DOptCmp6, 1);
1803 C += RunOptFunc (S, &DOptCmp7, 1);
1804 C += RunOptFunc (S, &DOptTest1, 1);
1805 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1806 C += RunOptFunc (S, &DOptUnusedStores, 1);
1807 C += RunOptFunc (S, &DOptDupLoads, 1);
1808 C += RunOptFunc (S, &DOptStoreLoad, 1);
1809 C += RunOptFunc (S, &DOptTransfers, 1);
1810 C += RunOptFunc (S, &DOptPushPop, 1);
1816 /* Return the number of changes */
1822 static unsigned RunOptGroup4 (CodeSeg* S)
1823 /* 65C02 specific optimizations. */
1825 unsigned Changes = 0;
1827 if (CPU >= CPU_65C02) {
1828 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1829 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1830 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1832 /* The 65C02 replacement codes do often make the use of a register
1833 * value unnecessary, so if we have changes, run another load
1836 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1840 /* Return the number of changes */
1846 static unsigned RunOptGroup5 (CodeSeg* S)
1847 /* Run another round of pattern replacements. These are done late, since there
1848 * may be better replacements before.
1851 unsigned Changes = 0;
1853 Changes += RunOptFunc (S, &DOptPush1, 1);
1855 /* Return the number of changes */
1861 static unsigned RunOptGroup6 (CodeSeg* S)
1862 /* The last group of optimization steps. Adjust branches, do size optimizations.
1865 unsigned Changes = 0;
1868 /* Optimize for size, that is replace operations by shorter ones, even
1869 * if this does hinder further optimizations (no problem since we're
1872 Changes += RunOptFunc (S, &DOptSize2, 1);
1874 /* Run the jump target optimization again, since the size optimization
1875 * above may have opened new oportunities.
1877 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1879 /* Adjust branch distances */
1880 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1882 /* Replace conditional branches to RTS. If we had changes, we must run dead
1883 * code elimination again, since the change may have introduced dead code.
1885 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1888 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1891 /* Return the number of changes */
1897 void RunOpt (CodeSeg* S)
1898 /* Run the optimizer */
1900 const char* StatFileName;
1902 /* If we shouldn't run the optimizer, bail out */
1907 /* Check if we are requested to write optimizer statistics */
1908 StatFileName = getenv ("CC65_OPTSTATS");
1910 ReadOptStats (StatFileName);
1913 /* Print the name of the function we are working on */
1915 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1917 Print (stdout, 1, "Running optimizer for global code segment\n");
1920 /* Run groups of optimizations */
1928 /* Write statistics */
1930 WriteOptStats (StatFileName);