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 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Defines for the conditions in a compare */
74 /* Table with the compare suffixes */
75 static const char CmpSuffixTab [][4] = {
76 "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
79 /* Table used to invert a condition, indexed by condition */
80 static const unsigned char CmpInvertTab [] = {
82 CMP_LE, CMP_LT, CMP_GE, CMP_GT,
83 CMP_ULE, CMP_ULT, CMP_UGE, CMP_UGT
86 /* Table to show which compares are signed (use the N flag) */
87 static const char CmpSignedTab [] = {
88 0, 0, 1, 1, 1, 1, 0, 0, 0, 0
93 /*****************************************************************************/
94 /* Helper functions */
95 /*****************************************************************************/
99 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
100 /* Search for a compare condition by the given code using the given length */
105 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
106 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
118 static cmp_t FindBoolCmpCond (const char* Name)
119 /* Map a condition suffix to a code. Return the code or CMP_INV on failure */
121 /* Check for the correct subroutine name */
122 if (strncmp (Name, "bool", 4) == 0) {
123 /* Name is ok, search for the code in the table */
124 return FindCmpCond (Name+4, strlen(Name)-4);
133 static cmp_t FindTosCmpCond (const char* Name)
134 /* Check if this is a call to one of the TOS compare functions (tosgtax).
135 * Return the condition code or CMP_INV on failure.
138 unsigned Len = strlen (Name);
140 /* Check for the correct subroutine name */
141 if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
142 /* Name is ok, search for the code in the table */
143 return FindCmpCond (Name+3, Len-3-2);
152 static void ReplaceCmp (CodeSeg* S, unsigned I, cmp_t Cond)
153 /* Helper function for the replacement of routines that return a boolean
154 * followed by a conditional jump. Instead of the boolean value, the condition
155 * codes are evaluated directly.
156 * I is the index of the conditional branch, the sequence is already checked
164 CodeEntry* E = CS_GetEntry (S, I);
166 /* Replace the conditional branch */
170 CE_ReplaceOPC (E, OP65_JEQ);
174 CE_ReplaceOPC (E, OP65_JNE);
183 if ((N = CS_GetNextEntry (S, I)) == 0) {
185 Internal ("Invalid program flow");
187 L = CS_GenLabel (S, N);
188 N = NewCodeEntry (OP65_BEQ, AM65_BRA, L->Name, L, E->LI);
189 CS_InsertEntry (S, N, I);
190 CE_ReplaceOPC (E, OP65_JPL);
194 CE_ReplaceOPC (E, OP65_JPL);
198 CE_ReplaceOPC (E, OP65_JMI);
206 CE_ReplaceOPC (E, OP65_JMI);
208 N = NewCodeEntry (OP65_JEQ, AM65_BRA, L->Name, L, E->LI);
209 CS_InsertEntry (S, N, I+1);
218 if ((N = CS_GetNextEntry (S, I)) == 0) {
220 Internal ("Invalid program flow");
222 L = CS_GenLabel (S, N);
223 N = NewCodeEntry (OP65_BEQ, AM65_BRA, L->Name, L, E->LI);
224 CS_InsertEntry (S, N, I);
225 CE_ReplaceOPC (E, OP65_JCS);
229 CE_ReplaceOPC (E, OP65_JCS);
233 CE_ReplaceOPC (E, OP65_JCC);
241 CE_ReplaceOPC (E, OP65_JCC);
243 N = NewCodeEntry (OP65_JEQ, AM65_BRA, L->Name, L, E->LI);
244 CS_InsertEntry (S, N, I+1);
248 Internal ("Unknown jump condition: %d", Cond);
256 static int IsCmpToZero (const CodeEntry* E)
257 /* Check if the given instrcuction is a compare to zero instruction */
259 return (E->OPC == OP65_CMP &&
261 (E->Flags & CEF_NUMARG) != 0 &&
267 static int IsSpLoad (const CodeEntry* E)
268 /* Return true if this is the load of A from the stack */
270 return E->OPC == OP65_LDA && E->AM == AM65_ZP_INDY && strcmp (E->Arg, "sp") == 0;
275 static int IsLocalLoad16 (CodeSeg* S, unsigned Index,
276 CodeEntry** L, unsigned Count)
277 /* Check if a 16 bit load of a local variable follows:
285 * If so, read Count entries following the first ldy into L and return true
286 * if this is possible. Otherwise return false.
289 /* Be sure we read enough entries for the check */
292 /* Read the first entry */
293 L[0] = CS_GetEntry (S, Index);
295 /* Check for the sequence */
296 return (L[0]->OPC == OP65_LDY &&
297 L[0]->AM == AM65_IMM &&
298 (L[0]->Flags & CEF_NUMARG) != 0 &&
299 CS_GetEntries (S, L+1, Index+1, Count-1) &&
301 !CE_HasLabel (L[1]) &&
302 L[2]->OPC == OP65_TAX &&
303 !CE_HasLabel (L[2]) &&
304 L[3]->OPC == OP65_DEY &&
305 !CE_HasLabel (L[3]) &&
307 !CE_HasLabel (L[4]));
312 static int IsImmCmp16 (CodeSeg* S, CodeEntry** L)
313 /* Check if the instructions at L are an immidiate compare of a/x:
318 return (L[0]->OPC == OP65_CPX &&
319 L[0]->AM == AM65_IMM &&
320 (L[0]->Flags & CEF_NUMARG) != 0 &&
321 !CE_HasLabel (L[0]) &&
322 (L[1]->OPC == OP65_JNE || L[1]->OPC == OP65_BNE) &&
324 !CE_HasLabel (L[1]) &&
325 L[2]->OPC == OP65_CMP &&
326 L[2]->AM == AM65_IMM &&
327 (L[2]->Flags & CEF_NUMARG) != 0 &&
328 (L[3]->Info & OF_ZBRA) != 0 &&
330 (L[1]->JumpTo->Owner == L[3] || L[1]->JumpTo == L[3]->JumpTo));
335 /*****************************************************************************/
336 /* Remove calls to the bool transformer subroutines */
337 /*****************************************************************************/
341 static unsigned OptBoolTransforms (CodeSeg* S)
342 /* Try to remove the call to boolean transformer routines where the call is
346 unsigned Changes = 0;
348 /* Walk over the entries */
350 while (I < CS_GetEntryCount (S)) {
356 CodeEntry* E = CS_GetEntry (S, I);
358 /* Check for a boolean transformer */
359 if (E->OPC == OP65_JSR &&
360 (Cond = FindBoolCmpCond (E->Arg)) != CMP_INV &&
361 (N = CS_GetNextEntry (S, I)) != 0 &&
362 (N->Info & OF_ZBRA) != 0) {
364 /* Make the boolean transformer unnecessary by changing the
365 * the conditional jump to evaluate the condition flags that
366 * are set after the compare directly. Note: jeq jumps if
367 * the condition is not met, jne jumps if the condition is met.
368 * Invert the code if we jump on condition not met.
370 if (GetBranchCond (N->OPC) == BC_EQ) {
371 /* Jumps if condition false, invert condition */
372 Cond = CmpInvertTab [Cond];
375 /* Check if we can replace the code by something better */
376 ReplaceCmp (S, I+1, Cond);
378 /* Remove the call to the bool transformer */
381 /* Remember, we had changes */
391 /* Return the number of changes made */
397 /*****************************************************************************/
398 /* Optimize subtractions */
399 /*****************************************************************************/
403 static unsigned OptSub1 (CodeSeg* S)
404 /* Search for the sequence
411 * and remove the handling of the high byte if X is not used later.
414 unsigned Changes = 0;
416 /* Walk over the entries */
418 while (I < CS_GetEntryCount (S)) {
423 CodeEntry* E = CS_GetEntry (S, I);
425 /* Check for the sequence */
426 if (E->OPC == OP65_SBC &&
427 CS_GetEntries (S, L, I+1, 3) &&
428 (L[0]->OPC == OP65_BCS || L[0]->OPC == OP65_JCS) &&
430 !CE_HasLabel (L[0]) &&
431 L[1]->OPC == OP65_DEX &&
432 !CE_HasLabel (L[1]) &&
433 L[0]->JumpTo->Owner == L[2] &&
434 !RegXUsed (S, I+3)) {
436 /* Remove the bcs/dex */
437 CS_DelEntries (S, I+1, 2);
439 /* Remember, we had changes */
449 /* Return the number of changes made */
455 static unsigned OptSub2 (CodeSeg* S)
456 /* Search for the sequence
473 unsigned Changes = 0;
475 /* Walk over the entries */
477 while (I < CS_GetEntryCount (S)) {
482 CodeEntry* E = CS_GetEntry (S, I);
484 /* Check for the sequence */
485 if (E->OPC == OP65_LDA &&
486 CS_GetEntries (S, L, I+1, 5) &&
487 L[0]->OPC == OP65_SEC &&
488 !CE_HasLabel (L[0]) &&
489 L[1]->OPC == OP65_STA &&
490 strcmp (L[1]->Arg, "tmp1") == 0 &&
491 !CE_HasLabel (L[1]) &&
492 L[2]->OPC == OP65_LDA &&
493 !CE_HasLabel (L[2]) &&
494 L[3]->OPC == OP65_SBC &&
495 strcmp (L[3]->Arg, "tmp1") == 0 &&
496 !CE_HasLabel (L[3]) &&
497 L[4]->OPC == OP65_STA &&
498 strcmp (L[4]->Arg, L[2]->Arg) == 0 &&
499 !CE_HasLabel (L[4])) {
501 /* Remove the store to tmp1 */
502 CS_DelEntry (S, I+2);
504 /* Remove the subtraction */
505 CS_DelEntry (S, I+3);
507 /* Move the lda to the position of the subtraction and change the
510 CS_MoveEntry (S, I, I+3);
511 CE_ReplaceOPC (E, OP65_SBC);
513 /* If the sequence head had a label, move this label back to the
516 if (CE_HasLabel (E)) {
517 CS_MoveLabels (S, E, L[0]);
520 /* Remember, we had changes */
530 /* Return the number of changes made */
536 /*****************************************************************************/
537 /* Optimize additions */
538 /*****************************************************************************/
542 static unsigned OptAdd1 (CodeSeg* S)
543 /* Search for the sequence
561 unsigned Changes = 0;
563 /* Walk over the entries */
565 while (I < CS_GetEntryCount (S)) {
570 CodeEntry* E = CS_GetEntry (S, I);
572 /* Check for the sequence */
573 if (E->OPC == OP65_JSR &&
574 strcmp (E->Arg, "pushax") == 0 &&
575 CS_GetEntries (S, L, I+1, 5) &&
576 L[0]->OPC == OP65_LDY &&
577 !CE_HasLabel (L[0]) &&
578 L[1]->OPC == OP65_LDX &&
579 CE_KnownImm (L[1]) &&
581 !CE_HasLabel (L[1]) &&
582 L[2]->OPC == OP65_LDA &&
583 !CE_HasLabel (L[2]) &&
584 L[3]->OPC == OP65_JSR &&
585 strcmp (L[3]->Arg, "tosaddax") == 0 &&
586 !CE_HasLabel (L[3])) {
591 /* Remove the call to pushax */
595 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
596 CS_InsertEntry (S, X, I+1);
598 /* Remove the load */
599 CS_DelEntry (S, I+3); /* lda */
600 CS_DelEntry (S, I+2); /* ldx */
603 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
604 CS_InsertEntry (S, X, I+2);
606 /* Generate the branch label and the branch */
607 Label = CS_GenLabel (S, L[4]);
608 X = NewCodeEntry (OP65_BCC, AM65_BRA, Label->Name, Label, L[3]->LI);
609 CS_InsertEntry (S, X, I+3);
611 /* Generate the increment of the high byte */
612 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[3]->LI);
613 CS_InsertEntry (S, X, I+4);
615 /* Delete the call to tosaddax */
616 CS_DelEntry (S, I+5);
618 /* Remember, we had changes */
628 /* Return the number of changes made */
634 static unsigned OptAdd2 (CodeSeg* S)
635 /* Search for the sequence
642 * and remove the handling of the high byte if X is not used later.
645 unsigned Changes = 0;
647 /* Walk over the entries */
649 while (I < CS_GetEntryCount (S)) {
654 CodeEntry* E = CS_GetEntry (S, I);
656 /* Check for the sequence */
657 if (E->OPC == OP65_ADC &&
658 CS_GetEntries (S, L, I+1, 3) &&
659 (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
661 !CE_HasLabel (L[0]) &&
662 L[1]->OPC == OP65_INX &&
663 !CE_HasLabel (L[1]) &&
664 L[0]->JumpTo->Owner == L[2] &&
665 !RegXUsed (S, I+3)) {
667 /* Remove the bcs/dex */
668 CS_DelEntries (S, I+1, 2);
670 /* Remember, we had changes */
680 /* Return the number of changes made */
686 /*****************************************************************************/
687 /* Optimizations for compares */
688 /*****************************************************************************/
692 static unsigned OptCmp1 (CodeSeg* S)
693 /* Search for the sequence
705 unsigned Changes = 0;
707 /* Walk over the entries */
709 while (I < CS_GetEntryCount (S)) {
714 CodeEntry* E = CS_GetEntry (S, I);
716 /* Check for the sequence */
717 if (E->OPC == OP65_STX &&
718 CS_GetEntries (S, L, I+1, 2) &&
719 L[0]->OPC == OP65_STX &&
720 strcmp (L[0]->Arg, "tmp1") == 0 &&
721 !CE_HasLabel (L[0]) &&
722 L[1]->OPC == OP65_ORA &&
723 strcmp (L[1]->Arg, "tmp1") == 0 &&
724 !CE_HasLabel (L[1])) {
726 /* Remove the remaining instructions */
727 CS_DelEntries (S, I+1, 2);
729 /* Insert the ora instead */
730 CS_InsertEntry (S, NewCodeEntry (OP65_ORA, E->AM, E->Arg, 0, E->LI), I+1);
732 /* Remember, we had changes */
742 /* Return the number of changes made */
748 static unsigned OptCmp2 (CodeSeg* S)
751 * lda/and/ora/eor ...
755 * and remove the cmp.
758 unsigned Changes = 0;
760 /* Walk over the entries */
762 while (I < CS_GetEntryCount (S)) {
767 CodeEntry* E = CS_GetEntry (S, I);
769 /* Check for the sequence */
770 if ((E->OPC == OP65_ADC ||
771 E->OPC == OP65_AND ||
772 E->OPC == OP65_DEA ||
773 E->OPC == OP65_EOR ||
774 E->OPC == OP65_INA ||
775 E->OPC == OP65_LDA ||
776 E->OPC == OP65_ORA ||
777 E->OPC == OP65_PLA ||
778 E->OPC == OP65_SBC ||
779 E->OPC == OP65_TXA ||
780 E->OPC == OP65_TYA) &&
781 CS_GetEntries (S, L, I+1, 2) &&
782 IsCmpToZero (L[0]) &&
783 !CE_HasLabel (L[0]) &&
784 (L[1]->Info & OF_FBRA) != 0 &&
785 !CE_HasLabel (L[1])) {
787 /* Remove the compare */
788 CS_DelEntry (S, I+1);
790 /* Remember, we had changes */
800 /* Return the number of changes made */
806 static unsigned OptCmp3 (CodeSeg* S)
816 * If a is zero, we may remove the compare. If a and b are both zero, we may
817 * replace it by the sequence
823 * L1 may be either the label at the branch instruction, or the target label
824 * of this instruction.
827 unsigned Changes = 0;
829 /* Walk over the entries */
831 while (I < CS_GetEntryCount (S)) {
836 CodeEntry* E = CS_GetEntry (S, I);
838 /* Check for the sequence */
839 if (E->OPC == OP65_LDA &&
840 CS_GetEntries (S, L, I+1, 5) &&
841 L[0]->OPC == OP65_LDX &&
842 !CE_HasLabel (L[0]) &&
843 IsImmCmp16 (S, L+1)) {
845 if (L[1]->Num == 0 && L[3]->Num == 0) {
846 /* The value is zero, we may use the simple code version. */
847 CE_ReplaceOPC (L[0], OP65_ORA);
848 CS_DelEntries (S, I+2, 3);
850 /* Move the lda instruction after the first branch. This will
851 * improve speed, since the load is delayed after the first
854 CS_MoveEntry (S, I, I+4);
856 /* We will replace the ldx/cpx by lda/cmp */
857 CE_ReplaceOPC (L[0], OP65_LDA);
858 CE_ReplaceOPC (L[1], OP65_CMP);
860 /* Beware: If the first LDA instruction had a label, we have
861 * to move this label to the top of the sequence again.
863 if (CE_HasLabel (E)) {
864 CS_MoveLabels (S, E, L[0]);
877 /* Return the number of changes made */
883 static unsigned OptCmp4 (CodeSeg* S)
884 /* Optimize compares of local variables:
897 unsigned Changes = 0;
899 /* Walk over the entries */
901 while (I < CS_GetEntryCount (S)) {
905 /* Check for the sequence */
906 if (IsLocalLoad16 (S, I, L, 9) && IsImmCmp16 (S, L+5)) {
908 if (L[5]->Num == 0 && L[7]->Num == 0) {
910 /* The value is zero, we may use the simple code version:
917 CE_ReplaceOPC (L[4], OP65_ORA);
918 CS_DelEntries (S, I+5, 3); /* cpx/bne/cmp */
919 CS_DelEntry (S, I+2); /* tax */
923 /* Change the code to just use the A register. Move the load
924 * of the low byte after the first branch if possible:
935 CS_DelEntry (S, I+2); /* tax */
936 CE_ReplaceOPC (L[5], OP65_CMP); /* cpx -> cmp */
937 CS_MoveEntry (S, I+4, I+2); /* cmp */
938 CS_MoveEntry (S, I+5, I+3); /* bne */
950 /* Return the number of changes made */
956 static unsigned OptCmp5 (CodeSeg* S)
957 /* Search for calls to compare subroutines followed by a conditional branch
958 * and replace them by cheaper versions, since the branch means that the
959 * boolean value returned by these routines is not needed (we may also check
960 * that explicitly, but for the current code generator it is always true).
963 unsigned Changes = 0;
965 /* Walk over the entries */
967 while (I < CS_GetEntryCount (S)) {
973 CodeEntry* E = CS_GetEntry (S, I);
975 /* Check for the sequence */
976 if (E->OPC == OP65_JSR &&
977 (Cond = FindTosCmpCond (E->Arg)) != CMP_INV &&
978 (N = CS_GetNextEntry (S, I)) != 0 &&
979 (N->Info & OF_ZBRA) != 0 &&
982 /* The tos... functions will return a boolean value in a/x and
983 * the Z flag says if this value is zero or not. We will call
984 * a cheaper subroutine instead, one that does not return a
985 * boolean value but only valid flags. Note: jeq jumps if
986 * the condition is not met, jne jumps if the condition is met.
987 * Invert the code if we jump on condition not met.
989 if (GetBranchCond (N->OPC) == BC_EQ) {
990 /* Jumps if condition false, invert condition */
991 Cond = CmpInvertTab [Cond];
994 /* Replace the subroutine call. */
995 E = NewCodeEntry (OP65_JSR, AM65_ABS, "tosicmp", 0, E->LI);
996 CS_InsertEntry (S, E, I+1);
999 /* Replace the conditional branch */
1000 ReplaceCmp (S, I+1, Cond);
1002 /* Remember, we had changes */
1012 /* Return the number of changes made */
1018 static unsigned OptCmp6 (CodeSeg* S)
1019 /* Search for a sequence ldx/txa/branch and remove the txa if A is not
1023 unsigned Changes = 0;
1025 /* Walk over the entries */
1027 while (I < CS_GetEntryCount (S)) {
1031 /* Get next entry */
1032 CodeEntry* E = CS_GetEntry (S, I);
1034 /* Check for the sequence */
1035 if ((E->OPC == OP65_LDX || E->OPC == OP65_TAX) &&
1036 CS_GetEntries (S, L, I+1, 2) &&
1037 L[0]->OPC == OP65_TXA &&
1038 !CE_HasLabel (L[0]) &&
1039 (L[1]->Info & OF_FBRA) != 0 &&
1040 !CE_HasLabel (L[1]) &&
1041 !RegAUsed (S, I+3)) {
1043 /* Remove the txa */
1044 CS_DelEntry (S, I+1);
1046 /* Remember, we had changes */
1056 /* Return the number of changes made */
1062 /*****************************************************************************/
1063 /* Optimize tests */
1064 /*****************************************************************************/
1068 static unsigned OptTest1 (CodeSeg* S)
1075 * if X is zero, the sequence may be changed
1080 * which may be optimized further by another step.
1083 unsigned Changes = 0;
1086 /* Generate register info for this step */
1089 /* Walk over the entries */
1091 while (I < CS_GetEntryCount (S)) {
1095 /* Get next entry */
1096 L[0] = CS_GetEntry (S, I);
1098 /* Check if it's the sequence we're searching for */
1099 if (L[0]->OPC == OP65_STX &&
1100 L[0]->RI->In.RegX == 0 &&
1101 CS_GetEntries (S, L+1, I+1, 2) &&
1102 !CE_HasLabel (L[1]) &&
1103 L[1]->OPC == OP65_ORA &&
1104 strcmp (L[0]->Arg, L[1]->Arg) == 0 &&
1105 !CE_HasLabel (L[2]) &&
1106 (L[2]->Info & OF_ZBRA) != 0) {
1108 /* Insert the compare */
1109 CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI);
1110 CS_InsertEntry (S, N, I+2);
1112 /* Remove the two other insns */
1113 CS_DelEntry (S, I+1);
1116 /* We had changes */
1125 /* Free register info */
1128 /* Return the number of changes made */
1138 /*****************************************************************************/
1139 /* nega optimizations */
1140 /*****************************************************************************/
1144 static unsigned OptNegA1 (CodeSeg* S)
1151 * Remove the ldx if the lda does not use it.
1154 unsigned Changes = 0;
1156 /* Walk over the entries */
1158 while (I < CS_GetEntryCount (S)) {
1162 /* Get next entry */
1163 CodeEntry* E = CS_GetEntry (S, I);
1165 /* Check for a ldx */
1166 if (E->OPC == OP65_LDX &&
1167 E->AM == AM65_IMM &&
1168 (E->Flags & CEF_NUMARG) != 0 &&
1170 CS_GetEntries (S, L, I+1, 2) &&
1171 L[0]->OPC == OP65_LDA &&
1172 (L[0]->Use & REG_X) == 0 &&
1173 !CE_HasLabel (L[0]) &&
1174 L[1]->OPC == OP65_JSR &&
1175 strcmp (L[1]->Arg, "bnega") == 0 &&
1176 !CE_HasLabel (L[1])) {
1178 /* Remove the ldx instruction */
1181 /* Remember, we had changes */
1191 /* Return the number of changes made */
1197 static unsigned OptNegA2 (CodeSeg* S)
1204 * Adjust the conditional branch and remove the call to the subroutine.
1207 unsigned Changes = 0;
1209 /* Walk over the entries */
1211 while (I < CS_GetEntryCount (S)) {
1215 /* Get next entry */
1216 CodeEntry* E = CS_GetEntry (S, I);
1218 /* Check for the sequence */
1219 if ((E->OPC == OP65_ADC ||
1220 E->OPC == OP65_AND ||
1221 E->OPC == OP65_DEA ||
1222 E->OPC == OP65_EOR ||
1223 E->OPC == OP65_INA ||
1224 E->OPC == OP65_LDA ||
1225 E->OPC == OP65_ORA ||
1226 E->OPC == OP65_PLA ||
1227 E->OPC == OP65_SBC ||
1228 E->OPC == OP65_TXA ||
1229 E->OPC == OP65_TYA) &&
1230 CS_GetEntries (S, L, I+1, 2) &&
1231 L[0]->OPC == OP65_JSR &&
1232 strcmp (L[0]->Arg, "bnega") == 0 &&
1233 !CE_HasLabel (L[0]) &&
1234 (L[1]->Info & OF_ZBRA) != 0 &&
1235 !CE_HasLabel (L[1])) {
1237 /* Invert the branch */
1238 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
1240 /* Delete the subroutine call */
1241 CS_DelEntry (S, I+1);
1243 /* Remember, we had changes */
1253 /* Return the number of changes made */
1259 /*****************************************************************************/
1260 /* negax optimizations */
1261 /*****************************************************************************/
1265 static unsigned OptNegAX1 (CodeSeg* S)
1266 /* On a call to bnegax, if X is zero, the result depends only on the value in
1267 * A, so change the call to a call to bnega. This will get further optimized
1268 * later if possible.
1271 unsigned Changes = 0;
1274 /* Generate register info for this step */
1277 /* Walk over the entries */
1279 while (I < CS_GetEntryCount (S)) {
1281 /* Get next entry */
1282 CodeEntry* E = CS_GetEntry (S, I);
1284 /* Check if this is a call to bnegax, and if X is known and zero */
1285 if (E->OPC == OP65_JSR &&
1286 E->RI->In.RegX == 0 &&
1287 strcmp (E->Arg, "bnegax") == 0) {
1289 /* We're cheating somewhat here ... */
1293 /* We had changes */
1302 /* Free register info */
1305 /* Return the number of changes made */
1311 static unsigned OptNegAX2 (CodeSeg* S)
1312 /* Search for the sequence:
1329 unsigned Changes = 0;
1331 /* Walk over the entries */
1333 while (I < CS_GetEntryCount (S)) {
1337 /* Get next entry */
1338 CodeEntry* E = CS_GetEntry (S, I);
1340 /* Check for the sequence */
1341 if (E->OPC == OP65_LDA &&
1342 E->AM == AM65_ZP_INDY &&
1343 CS_GetEntries (S, L, I+1, 5) &&
1344 L[0]->OPC == OP65_TAX &&
1345 L[1]->OPC == OP65_DEY &&
1346 L[2]->OPC == OP65_LDA &&
1347 L[2]->AM == AM65_ZP_INDY &&
1348 strcmp (L[2]->Arg, E->Arg) == 0 &&
1349 !CE_HasLabel (L[2]) &&
1350 L[3]->OPC == OP65_JSR &&
1351 strcmp (L[3]->Arg, "bnegax") == 0 &&
1352 !CE_HasLabel (L[3]) &&
1353 (L[4]->Info & OF_ZBRA) != 0 &&
1354 !CE_HasLabel (L[4])) {
1357 CE_ReplaceOPC (L[2], OP65_ORA);
1359 /* Invert the branch */
1360 CE_ReplaceOPC (L[4], GetInverseBranch (L[4]->OPC));
1362 /* Delete the entries no longer needed. Beware: Deleting entries
1363 * will change the indices.
1365 CS_DelEntry (S, I+4); /* jsr bnegax */
1366 CS_DelEntry (S, I+1); /* tax */
1368 /* Remember, we had changes */
1378 /* Return the number of changes made */
1384 static unsigned OptNegAX3 (CodeSeg* S)
1385 /* Search for the sequence:
1399 unsigned Changes = 0;
1401 /* Walk over the entries */
1403 while (I < CS_GetEntryCount (S)) {
1407 /* Get next entry */
1408 CodeEntry* E = CS_GetEntry (S, I);
1410 /* Check for the sequence */
1411 if (E->OPC == OP65_LDA &&
1412 CS_GetEntries (S, L, I+1, 3) &&
1413 L[0]->OPC == OP65_LDX &&
1414 !CE_HasLabel (L[0]) &&
1415 L[1]->OPC == OP65_JSR &&
1416 strcmp (L[1]->Arg, "bnegax") == 0 &&
1417 !CE_HasLabel (L[1]) &&
1418 (L[2]->Info & OF_ZBRA) != 0 &&
1419 !CE_HasLabel (L[2])) {
1422 CE_ReplaceOPC (L[0], OP65_ORA);
1424 /* Invert the branch */
1425 CE_ReplaceOPC (L[2], GetInverseBranch (L[2]->OPC));
1427 /* Delete the subroutine call */
1428 CS_DelEntry (S, I+2);
1430 /* Remember, we had changes */
1440 /* Return the number of changes made */
1446 static unsigned OptNegAX4 (CodeSeg* S)
1447 /* Search for the sequence:
1453 * and replace it by:
1460 unsigned Changes = 0;
1462 /* Walk over the entries */
1464 while (I < CS_GetEntryCount (S)) {
1468 /* Get next entry */
1469 CodeEntry* E = CS_GetEntry (S, I);
1471 /* Check for the sequence */
1472 if (E->OPC == OP65_JSR &&
1473 CS_GetEntries (S, L, I+1, 2) &&
1474 L[0]->OPC == OP65_JSR &&
1475 strncmp (L[0]->Arg,"bnega",5) == 0 &&
1476 !CE_HasLabel (L[0]) &&
1477 (L[1]->Info & OF_ZBRA) != 0 &&
1478 !CE_HasLabel (L[1])) {
1482 /* Check if we're calling bnega or bnegax */
1483 int ByteSized = (strcmp (L[0]->Arg, "bnega") == 0);
1485 /* Insert apropriate test code */
1488 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
1489 CS_InsertEntry (S, X, I+2);
1492 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
1493 CS_InsertEntry (S, X, I+2);
1494 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, L[0]->LI);
1495 CS_InsertEntry (S, X, I+3);
1498 /* Delete the subroutine call */
1499 CS_DelEntry (S, I+1);
1501 /* Invert the branch */
1502 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
1504 /* Remember, we had changes */
1514 /* Return the number of changes made */
1520 /*****************************************************************************/
1521 /* Optimize stores through pointers */
1522 /*****************************************************************************/
1526 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
1527 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
1529 /* Check for a label attached to the entry */
1530 if (CE_HasLabel (L[0])) {
1534 /* Check for single insn sub ops */
1535 if (L[0]->OPC == OP65_AND ||
1536 L[0]->OPC == OP65_EOR ||
1537 L[0]->OPC == OP65_ORA ||
1538 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
1539 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
1544 } else if (L[0]->OPC == OP65_CLC &&
1545 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
1546 L[1]->OPC == OP65_ADC &&
1547 !CE_HasLabel (L[1])) {
1549 } else if (L[0]->OPC == OP65_SEC &&
1550 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
1551 L[1]->OPC == OP65_SBC &&
1552 !CE_HasLabel (L[1])) {
1564 static unsigned OptPtrStore1 (CodeSeg* S)
1565 /* Search for the sequence:
1574 * and replace it by:
1586 unsigned Changes = 0;
1588 /* Walk over the entries */
1590 while (I < CS_GetEntryCount (S)) {
1595 /* Get next entry */
1596 L[0] = CS_GetEntry (S, I);
1598 /* Check for the sequence */
1599 if (L[0]->OPC == OP65_JSR &&
1600 strcmp (L[0]->Arg, "pushax") == 0 &&
1601 CS_GetEntries (S, L+1, I+1, 3) &&
1602 L[1]->OPC == OP65_LDY &&
1603 CE_KnownImm (L[1]) &&
1604 !CE_HasLabel (L[1]) &&
1605 L[2]->OPC == OP65_JSR &&
1606 strcmp (L[2]->Arg, "ldauidx") == 0 &&
1607 !CE_HasLabel (L[2]) &&
1608 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
1609 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
1610 L[3+K]->OPC == OP65_LDY &&
1611 CE_KnownImm (L[3+K]) &&
1612 !CE_HasLabel (L[3+K]) &&
1613 L[4+K]->OPC == OP65_JSR &&
1614 strcmp (L[4+K]->Arg, "staspidx") == 0 &&
1615 !CE_HasLabel (L[4+K])) {
1619 /* Create and insert the stores */
1620 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1621 CS_InsertEntry (S, X, I+1);
1623 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1624 CS_InsertEntry (S, X, I+2);
1626 /* Delete the call to pushax */
1629 /* Delete the call to ldauidx */
1630 CS_DelEntry (S, I+3);
1632 /* Insert the load from ptr1 */
1633 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1634 CS_InsertEntry (S, X, I+3);
1635 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
1636 CS_InsertEntry (S, X, I+4);
1638 /* Insert the store through ptr1 */
1639 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
1640 CS_InsertEntry (S, X, I+6+K);
1642 /* Delete the call to staspidx */
1643 CS_DelEntry (S, I+7+K);
1645 /* Remember, we had changes */
1655 /* Return the number of changes made */
1661 static unsigned OptPtrStore2 (CodeSeg* S)
1662 /* Search for the sequence:
1669 * and replace it by:
1678 unsigned Changes = 0;
1680 /* Walk over the entries */
1682 while (I < CS_GetEntryCount (S)) {
1686 /* Get next entry */
1687 L[0] = CS_GetEntry (S, I);
1689 /* Check for the sequence */
1690 if (L[0]->OPC == OP65_JSR &&
1691 strcmp (L[0]->Arg, "pushax") == 0 &&
1692 CS_GetEntries (S, L+1, I+1, 3) &&
1693 L[1]->OPC == OP65_LDA &&
1694 !CE_HasLabel (L[1]) &&
1695 L[2]->OPC == OP65_LDY &&
1696 !CE_HasLabel (L[2]) &&
1697 L[3]->OPC == OP65_JSR &&
1698 strcmp (L[3]->Arg, "staspidx") == 0 &&
1699 !CE_HasLabel (L[3])) {
1703 /* Create and insert the stores */
1704 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1705 CS_InsertEntry (S, X, I+1);
1707 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1708 CS_InsertEntry (S, X, I+2);
1710 /* Delete the call to pushax */
1713 /* Insert the store through ptr1 */
1714 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
1715 CS_InsertEntry (S, X, I+4);
1717 /* Delete the call to staspidx */
1718 CS_DelEntry (S, I+5);
1720 /* Remember, we had changes */
1730 /* Return the number of changes made */
1736 /*****************************************************************************/
1737 /* Optimize loads through pointers */
1738 /*****************************************************************************/
1742 static unsigned OptPtrLoad1 (CodeSeg* S)
1743 /* Search for the sequence:
1747 * lda (sp),y # May be any destination
1751 * and replace it by:
1762 unsigned Changes = 0;
1764 /* Walk over the entries */
1766 while (I < CS_GetEntryCount (S)) {
1770 /* Get next entry */
1771 L[0] = CS_GetEntry (S, I);
1773 /* Check for the sequence */
1774 if (L[0]->OPC == OP65_TAX &&
1775 CS_GetEntries (S, L+1, I+1, 4) &&
1776 L[1]->OPC == OP65_DEY &&
1777 !CE_HasLabel (L[1]) &&
1778 L[2]->OPC == OP65_LDA &&
1779 !CE_HasLabel (L[2]) &&
1780 L[3]->OPC == OP65_LDY &&
1781 !CE_HasLabel (L[3]) &&
1782 L[4]->OPC == OP65_JSR &&
1783 strcmp (L[4]->Arg, "ldauidx") == 0 &&
1784 !CE_HasLabel (L[4])) {
1788 /* Store the high byte and remove the TAX instead */
1789 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1790 CS_InsertEntry (S, X, I+1);
1793 /* Store the low byte */
1794 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
1795 CS_InsertEntry (S, X, I+3);
1797 /* Delete the call to ldauidx */
1798 CS_DelEntry (S, I+5);
1800 /* Load high and low byte */
1801 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1802 CS_InsertEntry (S, X, I+5);
1803 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
1804 CS_InsertEntry (S, X, I+6);
1806 /* Remember, we had changes */
1816 /* Return the number of changes made */
1822 static unsigned OptPtrLoad2 (CodeSeg* S)
1823 /* Search for the sequence
1828 * and replace it by:
1836 * This step must be execute *after* OptPtrLoad1!
1839 unsigned Changes = 0;
1841 /* Walk over the entries */
1843 while (I < CS_GetEntryCount (S)) {
1847 /* Get next entry */
1848 L[0] = CS_GetEntry (S, I);
1850 /* Check for the sequence */
1851 if (L[0]->OPC == OP65_LDY &&
1852 CS_GetEntries (S, L+1, I+1, 1) &&
1853 L[1]->OPC == OP65_JSR &&
1854 strcmp (L[1]->Arg, "ldauidx") == 0 &&
1855 !CE_HasLabel (L[1])) {
1859 /* Store the high byte */
1860 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1861 CS_InsertEntry (S, X, I+1);
1863 /* Store the low byte */
1864 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1865 CS_InsertEntry (S, X, I+2);
1867 /* Delete the call to ldauidx */
1868 CS_DelEntry (S, I+3);
1870 /* Load the high and low byte */
1871 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1872 CS_InsertEntry (S, X, I+3);
1873 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1874 CS_InsertEntry (S, X, I+4);
1876 /* Remember, we had changes */
1886 /* Return the number of changes made */
1892 /*****************************************************************************/
1894 /*****************************************************************************/
1898 /* Types of optimization steps */
1900 optPre, /* Repeated once */
1901 optPreMain, /* Repeated more than once */
1903 optPostMain, /* dito */
1904 optPost /* Repeated once */
1907 /* Table with all the optimization functions */
1908 typedef struct OptFunc OptFunc;
1910 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1911 const char* Name; /* Name of optimizer step */
1912 unsigned char Type; /* Type of this step */
1913 char Disabled; /* True if pass disabled */
1916 /* Macro that builds a table entry */
1917 #define OptEntry(func,type) { func, #func, type, 0 }
1919 /* Table with optimizer steps */
1920 static OptFunc OptFuncs [] = {
1921 /* Optimizes stores through pointers */
1922 OptEntry (OptPtrStore1, optPre),
1923 OptEntry (OptPtrStore2, optPre),
1924 /* Optimize loads through pointers */
1925 OptEntry (OptPtrLoad1, optMain),
1926 OptEntry (OptPtrLoad2, optMain),
1927 /* Optimize calls to nega */
1928 OptEntry (OptNegA1, optMain),
1929 OptEntry (OptNegA2, optMain),
1930 /* Optimize calls to negax */
1931 OptEntry (OptNegAX1, optPre),
1932 OptEntry (OptNegAX2, optPre),
1933 OptEntry (OptNegAX3, optPre),
1934 OptEntry (OptNegAX4, optPre),
1935 /* Optimize subtractions */
1936 OptEntry (OptSub1, optMain),
1937 OptEntry (OptSub2, optMain),
1938 /* Optimize additions */
1939 OptEntry (OptAdd1, optPre),
1940 OptEntry (OptAdd2, optMain),
1941 /* Optimize jump cascades */
1942 OptEntry (OptJumpCascades, optMain),
1943 /* Remove dead jumps */
1944 OptEntry (OptDeadJumps, optMain),
1945 /* Change jsr/rts to jmp */
1946 OptEntry (OptRTS, optMain),
1947 /* Remove dead code */
1948 OptEntry (OptDeadCode, optMain),
1949 /* Optimize jump targets */
1950 OptEntry (OptJumpTarget, optMain),
1951 /* Optimize conditional branches */
1952 OptEntry (OptCondBranches, optMain),
1953 /* Replace jumps to RTS by RTS */
1954 OptEntry (OptRTSJumps, optMain),
1955 /* Remove calls to the bool transformer subroutines */
1956 OptEntry (OptBoolTransforms, optMain),
1957 /* Optimize compares */
1958 OptEntry (OptCmp1, optMain),
1959 OptEntry (OptCmp2, optMain),
1960 OptEntry (OptCmp3, optMain),
1961 OptEntry (OptCmp4, optMain),
1962 OptEntry (OptCmp5, optMain),
1963 OptEntry (OptCmp6, optMain),
1964 /* Optimize tests */
1965 OptEntry (OptTest1, optMain),
1966 /* Remove unused loads */
1967 OptEntry (OptUnusedLoads, optMain),
1968 OptEntry (OptDuplicateLoads, optMain),
1969 OptEntry (OptStoreLoad, optMain),
1970 /* Optimize branch distance */
1971 OptEntry (OptBranchDist, optMain),
1976 static OptFunc* FindOptStep (const char* Name)
1977 /* Find an optimizer step by name in the table and return a pointer. Print an
1978 * error and call AbEnd if not found.
1983 /* Run all optimization steps */
1984 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
1985 if (strcmp (OptFuncs[I].Name, Name) == 0) {
1992 AbEnd ("Optimization step `%s' not found", Name);
1998 void DisableOpt (const char* Name)
1999 /* Disable the optimization with the given name */
2001 if (strcmp (Name, "any") == 0) {
2003 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
2004 OptFuncs[I].Disabled = 1;
2007 OptFunc* F = FindOptStep (Name);
2014 void EnableOpt (const char* Name)
2015 /* Enable the optimization with the given name */
2017 if (strcmp (Name, "any") == 0) {
2019 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
2020 OptFuncs[I].Disabled = 0;
2023 OptFunc* F = FindOptStep (Name);
2030 void ListOptSteps (FILE* F)
2031 /* List all optimization steps */
2034 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
2035 fprintf (F, "%s\n", OptFuncs[I].Name);
2041 static void RepeatOptStep (CodeSeg* S, unsigned char Type, unsigned Max)
2042 /* Repeat the optimizer step of type Type at may Max times */
2046 unsigned OptChanges;
2048 /* Repeat max times of until there are no more changes */
2050 /* Reset the number of changes */
2053 /* Keep the user hapy */
2054 Print (stdout, 1, " Optimizer pass %u:\n", ++Pass);
2056 /* Run all optimization steps */
2057 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
2059 /* Get the table entry */
2060 const OptFunc* F = OptFuncs + I;
2062 /* Check if the type matches */
2063 if (F->Type != Type) {
2068 /* Print the name of the following optimizer step */
2069 Print (stdout, 1, " %s:%*s", F->Name, (int) (30-strlen(F->Name)), "");
2071 /* Check if the step is disabled */
2073 Print (stdout, 1, "Disabled\n");
2075 unsigned Changes = F->Func (S);
2076 OptChanges += Changes;
2077 Print (stdout, 1, "%u Changes\n", Changes);
2081 } while (--Max > 0 && OptChanges > 0);
2086 void RunOpt (CodeSeg* S)
2087 /* Run the optimizer */
2090 /* If we shouldn't run the optimizer, bail out */
2095 /* Print the name of the function we are working on */
2097 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2099 Print (stdout, 1, "Running optimizer for global code segment\n");
2102 /* Repeat all steps until there are no more changes */
2103 RepeatOptStep (S, optPre, 1);
2104 RepeatOptStep (S, optPreMain, 0xFFFF);
2105 RepeatOptStep (S, optMain, 0xFFFF);
2106 RepeatOptStep (S, optPostMain, 0xFFFF);
2107 RepeatOptStep (S, optPost, 1);