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 = GetCodeEntry (S, I);
166 /* Replace the conditional branch */
170 ReplaceOPC (E, OPC_JEQ);
174 ReplaceOPC (E, OPC_JNE);
183 if ((N = GetNextCodeEntry (S, I)) == 0) {
185 Internal ("Invalid program flow");
187 L = GenCodeLabel (S, N);
188 N = NewCodeEntry (OPC_BEQ, AM_BRA, L->Name, L, E->LI);
189 InsertCodeEntry (S, N, I);
190 ReplaceOPC (E, OPC_JPL);
194 ReplaceOPC (E, OPC_JPL);
198 ReplaceOPC (E, OPC_JMI);
206 ReplaceOPC (E, OPC_JMI);
208 N = NewCodeEntry (OPC_JEQ, AM_BRA, L->Name, L, E->LI);
209 InsertCodeEntry (S, N, I+1);
218 if ((N = GetNextCodeEntry (S, I)) == 0) {
220 Internal ("Invalid program flow");
222 L = GenCodeLabel (S, N);
223 N = NewCodeEntry (OPC_BEQ, AM_BRA, L->Name, L, E->LI);
224 InsertCodeEntry (S, N, I);
225 ReplaceOPC (E, OPC_JCS);
229 ReplaceOPC (E, OPC_JCS);
233 ReplaceOPC (E, OPC_JCC);
241 ReplaceOPC (E, OPC_JCC);
243 N = NewCodeEntry (OPC_JEQ, AM_BRA, L->Name, L, E->LI);
244 InsertCodeEntry (S, N, I+1);
248 Internal ("Unknown jump condition: %d", Cond);
256 static int IsUnsignedCmp (int Code)
257 /* Check if this is an unsigned compare */
260 return CmpSignedTab [Code] == 0;
265 static int IsBitOp (const CodeEntry* E)
266 /* Check if E is one of the bit operations (and, or, eor) */
268 return (E->OPC == OPC_AND || E->OPC == OPC_ORA || E->OPC == OPC_EOR);
273 static int IsCmpToZero (const CodeEntry* E)
274 /* Check if the given instrcuction is a compare to zero instruction */
276 return (E->OPC == OPC_CMP &&
278 (E->Flags & CEF_NUMARG) != 0 &&
284 static int IsSpLoad (const CodeEntry* E)
285 /* Return true if this is the load of A from the stack */
287 return E->OPC == OPC_LDA && E->AM == AM_ZP_INDY && strcmp (E->Arg, "sp") == 0;
292 static int IsLocalLoad16 (CodeSeg* S, unsigned Index,
293 CodeEntry** L, unsigned Count)
294 /* Check if a 16 bit load of a local variable follows:
302 * If so, read Count entries following the first ldy into L and return true
303 * if this is possible. Otherwise return false.
306 /* Be sure we read enough entries for the check */
309 /* Read the first entry */
310 L[0] = GetCodeEntry (S, Index);
312 /* Check for the sequence */
313 return (L[0]->OPC == OPC_LDY &&
314 L[0]->AM == AM_IMM &&
315 (L[0]->Flags & CEF_NUMARG) != 0 &&
316 GetCodeEntries (S, L+1, Index+1, Count-1) &&
318 !CodeEntryHasLabel (L[1]) &&
319 L[2]->OPC == OPC_TAX &&
320 !CodeEntryHasLabel (L[2]) &&
321 L[3]->OPC == OPC_DEY &&
322 !CodeEntryHasLabel (L[3]) &&
324 !CodeEntryHasLabel (L[4]));
329 static int IsImmCmp16 (CodeSeg* S, CodeEntry** L)
330 /* Check if the instructions at L are an immidiate compare of a/x:
335 return (L[0]->OPC == OPC_CPX &&
336 L[0]->AM == AM_IMM &&
337 (L[0]->Flags & CEF_NUMARG) != 0 &&
338 !CodeEntryHasLabel (L[0]) &&
339 (L[1]->OPC == OPC_JNE || L[1]->OPC == OPC_BNE) &&
341 !CodeEntryHasLabel (L[1]) &&
342 L[2]->OPC == OPC_CMP &&
343 L[2]->AM == AM_IMM &&
344 (L[2]->Flags & CEF_NUMARG) != 0 &&
345 (L[3]->Info & OF_ZBRA) != 0 &&
347 (L[1]->JumpTo->Owner == L[3] || L[1]->JumpTo == L[3]->JumpTo));
352 /*****************************************************************************/
353 /* Remove calls to the bool transformer subroutines */
354 /*****************************************************************************/
358 static unsigned OptBoolTransforms (CodeSeg* S)
359 /* Try to remove the call to boolean transformer routines where the call is
363 unsigned Changes = 0;
365 /* Walk over the entries */
367 while (I < GetCodeEntryCount (S)) {
373 CodeEntry* E = GetCodeEntry (S, I);
375 /* Check for a boolean transformer */
376 if (E->OPC == OPC_JSR &&
377 (Cond = FindBoolCmpCond (E->Arg)) != CMP_INV &&
378 (N = GetNextCodeEntry (S, I)) != 0 &&
379 (N->Info & OF_ZBRA) != 0) {
381 /* Make the boolean transformer unnecessary by changing the
382 * the conditional jump to evaluate the condition flags that
383 * are set after the compare directly. Note: jeq jumps if
384 * the condition is not met, jne jumps if the condition is met.
385 * Invert the code if we jump on condition not met.
387 if (GetBranchCond (N->OPC) == BC_EQ) {
388 /* Jumps if condition false, invert condition */
389 Cond = CmpInvertTab [Cond];
392 /* Check if we can replace the code by something better */
393 ReplaceCmp (S, I+1, Cond);
395 /* Remove the call to the bool transformer */
398 /* Remember, we had changes */
408 /* Return the number of changes made */
414 /*****************************************************************************/
415 /* Optimize subtractions */
416 /*****************************************************************************/
420 static unsigned OptSub1 (CodeSeg* S)
421 /* Search for the sequence
428 * and remove the handling of the high byte if X is not used later.
431 unsigned Changes = 0;
433 /* Walk over the entries */
435 while (I < GetCodeEntryCount (S)) {
440 CodeEntry* E = GetCodeEntry (S, I);
442 /* Check for the sequence */
443 if (E->OPC == OPC_SBC &&
444 GetCodeEntries (S, L, I+1, 3) &&
445 (L[0]->OPC == OPC_BCS || L[0]->OPC == OPC_JCS) &&
447 !CodeEntryHasLabel (L[0]) &&
448 L[1]->OPC == OPC_DEX &&
449 !CodeEntryHasLabel (L[1]) &&
450 L[0]->JumpTo->Owner == L[2] &&
451 !RegXUsed (S, I+3)) {
453 /* Remove the bcs/dex */
454 DelCodeEntries (S, I+1, 2);
456 /* Remember, we had changes */
466 /* Return the number of changes made */
472 static unsigned OptSub2 (CodeSeg* S)
473 /* Search for the sequence
490 unsigned Changes = 0;
492 /* Walk over the entries */
494 while (I < GetCodeEntryCount (S)) {
499 CodeEntry* E = GetCodeEntry (S, I);
501 /* Check for the sequence */
502 if (E->OPC == OPC_LDA &&
503 GetCodeEntries (S, L, I+1, 5) &&
504 L[0]->OPC == OPC_SEC &&
505 !CodeEntryHasLabel (L[0]) &&
506 L[1]->OPC == OPC_STA &&
507 strcmp (L[1]->Arg, "tmp1") == 0 &&
508 !CodeEntryHasLabel (L[1]) &&
509 L[2]->OPC == OPC_LDA &&
510 !CodeEntryHasLabel (L[2]) &&
511 L[3]->OPC == OPC_SBC &&
512 strcmp (L[3]->Arg, "tmp1") == 0 &&
513 !CodeEntryHasLabel (L[3]) &&
514 L[4]->OPC == OPC_STA &&
515 strcmp (L[4]->Arg, L[2]->Arg) == 0 &&
516 !CodeEntryHasLabel (L[4])) {
518 /* Remove the store to tmp1 */
519 DelCodeEntry (S, I+2);
521 /* Remove the subtraction */
522 DelCodeEntry (S, I+3);
524 /* Move the lda to the position of the subtraction and change the
527 MoveCodeEntry (S, I, I+3);
528 ReplaceOPC (E, OPC_SBC);
530 /* Remember, we had changes */
540 /* Return the number of changes made */
546 /*****************************************************************************/
547 /* Optimize additions */
548 /*****************************************************************************/
552 static unsigned OptAdd1 (CodeSeg* S)
553 /* Search for the sequence
560 * and remove the handling of the high byte if X is not used later.
563 unsigned Changes = 0;
565 /* Walk over the entries */
567 while (I < GetCodeEntryCount (S)) {
572 CodeEntry* E = GetCodeEntry (S, I);
574 /* Check for the sequence */
575 if (E->OPC == OPC_ADC &&
576 GetCodeEntries (S, L, I+1, 3) &&
577 (L[0]->OPC == OPC_BCC || L[0]->OPC == OPC_JCC) &&
579 !CodeEntryHasLabel (L[0]) &&
580 L[1]->OPC == OPC_INX &&
581 !CodeEntryHasLabel (L[1]) &&
582 L[0]->JumpTo->Owner == L[2] &&
583 !RegXUsed (S, I+3)) {
585 /* Remove the bcs/dex */
586 DelCodeEntries (S, I+1, 2);
588 /* Remember, we had changes */
598 /* Return the number of changes made */
604 /*****************************************************************************/
605 /* Optimizations for compares */
606 /*****************************************************************************/
610 static unsigned OptCmp1 (CodeSeg* S)
611 /* Search for the sequence
623 unsigned Changes = 0;
625 /* Walk over the entries */
627 while (I < GetCodeEntryCount (S)) {
632 CodeEntry* E = GetCodeEntry (S, I);
634 /* Check for the sequence */
635 if (E->OPC == OPC_STX &&
636 GetCodeEntries (S, L, I+1, 2) &&
637 L[0]->OPC == OPC_STX &&
638 strcmp (L[0]->Arg, "tmp1") == 0 &&
639 !CodeEntryHasLabel (L[0]) &&
640 L[1]->OPC == OPC_ORA &&
641 strcmp (L[1]->Arg, "tmp1") == 0 &&
642 !CodeEntryHasLabel (L[1])) {
644 /* Remove the remaining instructions */
645 DelCodeEntries (S, I+1, 2);
647 /* Insert the ora instead */
648 InsertCodeEntry (S, NewCodeEntry (OPC_ORA, E->AM, E->Arg, 0, E->LI), I+1);
650 /* Remember, we had changes */
660 /* Return the number of changes made */
666 static unsigned OptCmp2 (CodeSeg* S)
669 * lda/and/ora/eor ...
673 * and remove the cmp.
676 unsigned Changes = 0;
678 /* Walk over the entries */
680 while (I < GetCodeEntryCount (S)) {
685 CodeEntry* E = GetCodeEntry (S, I);
687 /* Check for the sequence */
688 if ((E->OPC == OPC_LDA || IsBitOp (E)) &&
689 GetCodeEntries (S, L, I+1, 2) &&
690 IsCmpToZero (L[0]) &&
691 !CodeEntryHasLabel (L[0]) &&
692 (L[1]->Info & OF_FBRA) != 0 &&
693 !CodeEntryHasLabel (L[1])) {
695 /* Remove the compare */
696 DelCodeEntry (S, I+1);
698 /* Remember, we had changes */
708 /* Return the number of changes made */
714 static unsigned OptCmp3 (CodeSeg* S)
724 * If a is zero, we may remove the compare. If a and b are both zero, we may
725 * replace it by the sequence
731 * L1 may be either the label at the branch instruction, or the target label
732 * of this instruction.
735 unsigned Changes = 0;
737 /* Walk over the entries */
739 while (I < GetCodeEntryCount (S)) {
744 CodeEntry* E = GetCodeEntry (S, I);
746 /* Check for the sequence */
747 if (E->OPC == OPC_LDA &&
748 GetCodeEntries (S, L, I+1, 5) &&
749 L[0]->OPC == OPC_LDX &&
750 !CodeEntryHasLabel (L[0]) &&
751 IsImmCmp16 (S, L+1)) {
753 if (L[1]->Num == 0 && L[3]->Num == 0) {
754 /* The value is zero, we may use the simple code version. */
755 ReplaceOPC (L[0], OPC_ORA);
756 DelCodeEntries (S, I+2, 3);
758 /* Move the lda instruction after the first branch. This will
759 * improve speed, since the load is delayed after the first
762 MoveCodeEntry (S, I, I+4);
764 /* We will replace the ldx/cpx by lda/cmp */
765 ReplaceOPC (L[0], OPC_LDA);
766 ReplaceOPC (L[1], OPC_CMP);
778 /* Return the number of changes made */
784 static unsigned OptCmp4 (CodeSeg* S)
785 /* Optimize compares of local variables:
798 unsigned Changes = 0;
800 /* Walk over the entries */
802 while (I < GetCodeEntryCount (S)) {
806 /* Check for the sequence */
807 if (IsLocalLoad16 (S, I, L, 9) && IsImmCmp16 (S, L+5)) {
809 if (L[5]->Num == 0 && L[7]->Num == 0) {
811 /* The value is zero, we may use the simple code version:
818 ReplaceOPC (L[4], OPC_ORA);
819 DelCodeEntries (S, I+5, 3); /* cpx/bne/cmp */
820 DelCodeEntry (S, I+2); /* tax */
824 /* Change the code to just use the A register. Move the load
825 * of the low byte after the first branch if possible:
836 DelCodeEntry (S, I+2); /* tax */
837 ReplaceOPC (L[5], OPC_CMP); /* cpx -> cmp */
838 MoveCodeEntry (S, I+4, I+2); /* cmp */
839 MoveCodeEntry (S, I+5, I+3); /* bne */
851 /* Return the number of changes made */
857 static unsigned OptCmp5 (CodeSeg* S)
858 /* Search for calls to compare subroutines followed by a conditional branch
859 * and replace them by cheaper versions, since the branch means that the
860 * boolean value returned by these routines is not needed (we may also check
861 * that explicitly, but for the current code generator it is always true).
864 unsigned Changes = 0;
866 /* Walk over the entries */
868 while (I < GetCodeEntryCount (S)) {
874 CodeEntry* E = GetCodeEntry (S, I);
876 /* Check for the sequence */
877 if (E->OPC == OPC_JSR &&
878 (Cond = FindTosCmpCond (E->Arg)) != CMP_INV &&
879 (N = GetNextCodeEntry (S, I)) != 0 &&
880 (N->Info & OF_ZBRA) != 0 &&
881 !CodeEntryHasLabel (N)) {
883 /* The tos... functions will return a boolean value in a/x and
884 * the Z flag says if this value is zero or not. We will call
885 * a cheaper subroutine instead, one that does not return a
886 * boolean value but only valid flags. Note: jeq jumps if
887 * the condition is not met, jne jumps if the condition is met.
888 * Invert the code if we jump on condition not met.
890 if (GetBranchCond (N->OPC) == BC_EQ) {
891 /* Jumps if condition false, invert condition */
892 Cond = CmpInvertTab [Cond];
895 /* Replace the subroutine call. */
896 E = NewCodeEntry (OPC_JSR, AM_ABS, "tosicmp", 0, E->LI);
897 InsertCodeEntry (S, E, I+1);
900 /* Replace the conditional branch */
901 ReplaceCmp (S, I+1, Cond);
903 /* Remember, we had changes */
913 /* Return the number of changes made */
919 /*****************************************************************************/
920 /* nega optimizations */
921 /*****************************************************************************/
925 static unsigned OptNegA1 (CodeSeg* S)
932 * Remove the ldx if the lda does not use it.
935 unsigned Changes = 0;
937 /* Walk over the entries */
939 while (I < GetCodeEntryCount (S)) {
944 CodeEntry* E = GetCodeEntry (S, I);
946 /* Check for a ldx */
947 if (E->OPC == OPC_LDX &&
949 (E->Flags & CEF_NUMARG) != 0 &&
951 GetCodeEntries (S, L, I+1, 2) &&
952 L[0]->OPC == OPC_LDA &&
953 (L[0]->Use & REG_X) == 0 &&
954 L[1]->OPC == OPC_JSR &&
955 strcmp (L[1]->Arg, "bnega") == 0) {
957 /* Remove the ldx instruction */
960 /* Remember, we had changes */
970 /* Return the number of changes made */
976 static unsigned OptNegA2 (CodeSeg* S)
983 * Adjust the conditional branch and remove the call to the subroutine.
986 unsigned Changes = 0;
988 /* Walk over the entries */
990 while (I < GetCodeEntryCount (S)) {
995 CodeEntry* E = GetCodeEntry (S, I);
997 /* Check for the sequence */
998 if (E->OPC == OPC_LDA &&
999 GetCodeEntries (S, L, I+1, 2) &&
1000 L[0]->OPC == OPC_JSR &&
1001 strcmp (L[0]->Arg, "bnega") == 0 &&
1002 !CodeEntryHasLabel (L[0]) &&
1003 (L[1]->Info & OF_ZBRA) != 0) {
1005 /* Invert the branch */
1006 ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
1008 /* Delete the subroutine call */
1009 DelCodeEntry (S, I+1);
1011 /* Remember, we had changes */
1021 /* Return the number of changes made */
1027 /*****************************************************************************/
1028 /* negax optimizations */
1029 /*****************************************************************************/
1033 static unsigned OptNegAX1 (CodeSeg* S)
1034 /* Search for the sequence:
1051 unsigned Changes = 0;
1053 /* Walk over the entries */
1055 while (I < GetCodeEntryCount (S)) {
1059 /* Get next entry */
1060 CodeEntry* E = GetCodeEntry (S, I);
1062 /* Check for the sequence */
1063 if (E->OPC == OPC_LDA &&
1064 E->AM == AM_ZP_INDY &&
1065 GetCodeEntries (S, L, I+1, 5) &&
1066 L[0]->OPC == OPC_TAX &&
1067 L[1]->OPC == OPC_DEY &&
1068 L[2]->OPC == OPC_LDA &&
1069 L[2]->AM == AM_ZP_INDY &&
1070 strcmp (L[2]->Arg, E->Arg) == 0 &&
1071 !CodeEntryHasLabel (L[2]) &&
1072 L[3]->OPC == OPC_JSR &&
1073 strcmp (L[3]->Arg, "bnegax") == 0 &&
1074 !CodeEntryHasLabel (L[3]) &&
1075 (L[4]->Info & OF_ZBRA) != 0) {
1078 ReplaceOPC (L[2], OPC_ORA);
1080 /* Invert the branch */
1081 ReplaceOPC (L[4], GetInverseBranch (L[4]->OPC));
1083 /* Delete the entries no longer needed. Beware: Deleting entries
1084 * will change the indices.
1086 DelCodeEntry (S, I+4); /* jsr bnegax */
1087 DelCodeEntry (S, I+1); /* tax */
1089 /* Remember, we had changes */
1099 /* Return the number of changes made */
1105 static unsigned OptNegAX2 (CodeSeg* S)
1106 /* Search for the sequence:
1120 unsigned Changes = 0;
1122 /* Walk over the entries */
1124 while (I < GetCodeEntryCount (S)) {
1128 /* Get next entry */
1129 CodeEntry* E = GetCodeEntry (S, I);
1131 /* Check for the sequence */
1132 if (E->OPC == OPC_LDA &&
1133 GetCodeEntries (S, L, I+1, 3) &&
1134 L[0]->OPC == OPC_LDX &&
1135 !CodeEntryHasLabel (L[0]) &&
1136 L[1]->OPC == OPC_JSR &&
1137 strcmp (L[1]->Arg, "bnegax") == 0 &&
1138 !CodeEntryHasLabel (L[1]) &&
1139 (L[2]->Info & OF_ZBRA) != 0) {
1142 ReplaceOPC (L[0], OPC_ORA);
1144 /* Invert the branch */
1145 ReplaceOPC (L[2], GetInverseBranch (L[2]->OPC));
1147 /* Delete the subroutine call */
1148 DelCodeEntry (S, I+2);
1150 /* Remember, we had changes */
1160 /* Return the number of changes made */
1166 static unsigned OptNegAX3 (CodeSeg* S)
1167 /* Search for the sequence:
1173 * and replace it by:
1180 unsigned Changes = 0;
1182 /* Walk over the entries */
1184 while (I < GetCodeEntryCount (S)) {
1188 /* Get next entry */
1189 CodeEntry* E = GetCodeEntry (S, I);
1191 /* Check for the sequence */
1192 if (E->OPC == OPC_JSR &&
1194 GetCodeEntries (S, L, I+1, 2) &&
1195 L[0]->OPC == OPC_JSR &&
1196 strncmp (L[0]->Arg,"bnega",5) == 0 &&
1197 !CodeEntryHasLabel (L[0]) &&
1198 (L[1]->Info & OF_ZBRA) != 0) {
1202 /* Check if we're calling bnega or bnegax */
1203 int ByteSized = (strcmp (L[0]->Arg, "bnega") == 0);
1205 /* Insert apropriate test code */
1208 X = NewCodeEntry (OPC_TAX, AM_IMP, 0, 0, L[0]->LI);
1209 InsertCodeEntry (S, X, I+2);
1212 X = NewCodeEntry (OPC_STX, AM_ZP, "tmp1", 0, L[0]->LI);
1213 InsertCodeEntry (S, X, I+2);
1214 X = NewCodeEntry (OPC_ORA, AM_ZP, "tmp1", 0, L[0]->LI);
1215 InsertCodeEntry (S, X, I+3);
1218 /* Delete the subroutine call */
1219 DelCodeEntry (S, I+1);
1221 /* Invert the branch */
1222 ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
1224 /* Remember, we had changes */
1234 /* Return the number of changes made */
1240 /*****************************************************************************/
1242 /*****************************************************************************/
1246 /* Table with all the optimization functions */
1247 typedef struct OptFunc OptFunc;
1249 unsigned (*Func) (CodeSeg*);/* Optimizer function */
1250 const char* Name; /* Name of optimizer step */
1251 char Disabled; /* True if pass disabled */
1256 /* Table with optimizer steps - are called in this order */
1257 static OptFunc OptFuncs [] = {
1258 /* Optimize subtractions */
1259 { OptSub1, "OptSub1", 0 },
1260 { OptSub2, "OptSub2", 0 },
1261 /* Optimize additions */
1262 { OptAdd1, "OptAdd1", 0 },
1263 /* Optimize jump cascades */
1264 { OptJumpCascades, "OptJumpCascades", 0 },
1265 /* Remove dead jumps */
1266 { OptDeadJumps, "OptDeadJumps", 0 },
1267 /* Change jsr/rts to jmp */
1268 { OptRTS, "OptRTS", 0 },
1269 /* Remove dead code */
1270 { OptDeadCode, "OptDeadCode", 0 },
1271 /* Optimize jump targets */
1272 { OptJumpTarget, "OptJumpTarget", 0 },
1273 /* Optimize conditional branches */
1274 { OptCondBranches, "OptCondBranches", 0 },
1275 /* Replace jumps to RTS by RTS */
1276 { OptRTSJumps, "OptRTSJumps", 0 },
1277 /* Remove calls to the bool transformer subroutines */
1278 { OptBoolTransforms, "OptBoolTransforms", 0 },
1279 /* Optimize calls to nega */
1280 { OptNegA1, "OptNegA1", 0 },
1281 { OptNegA2, "OptNegA2", 0 },
1282 /* Optimize calls to negax */
1283 { OptNegAX1, "OptNegAX1", 0 },
1284 { OptNegAX2, "OptNegAX2", 0 },
1285 { OptNegAX3, "OptNegAX3", 0 },
1286 /* Optimize compares */
1287 { OptCmp1, "OptCmp1", 0 },
1288 { OptCmp2, "OptCmp2", 0 },
1289 { OptCmp3, "OptCmp3", 0 },
1290 { OptCmp4, "OptCmp4", 0 },
1291 { OptCmp5, "OptCmp5", 0 },
1292 /* Remove unused loads */
1293 { OptUnusedLoads, "OptUnusedLoads", 0 },
1294 /* Optimize branch distance */
1295 { OptBranchDist, "OptBranchDist", 0 },
1300 static OptFunc* FindOptStep (const char* Name)
1301 /* Find an optimizer step by name in the table and return a pointer. Print an
1302 * error and call AbEnd if not found.
1307 /* Run all optimization steps */
1308 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
1309 if (strcmp (OptFuncs[I].Name, Name) == 0) {
1316 AbEnd ("Optimization step `%s' not found", Name);
1322 void DisableOpt (const char* Name)
1323 /* Disable the optimization with the given name */
1325 if (strcmp (Name, "any") == 0) {
1327 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
1328 OptFuncs[I].Disabled = 1;
1331 OptFunc* F = FindOptStep (Name);
1338 void EnableOpt (const char* Name)
1339 /* Enable the optimization with the given name */
1341 if (strcmp (Name, "any") == 0) {
1343 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
1344 OptFuncs[I].Disabled = 0;
1347 OptFunc* F = FindOptStep (Name);
1354 void RunOpt (CodeSeg* S)
1355 /* Run the optimizer */
1359 unsigned OptChanges;
1361 /* If we shouldn't run the optimizer, bail out */
1366 /* Print the name of the function we are working on */
1368 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1370 Print (stdout, 1, "Running optimizer for global code segment\n");
1373 /* Repeat all steps until there are no more changes */
1375 /* Reset the number of changes */
1378 /* Keep the user hapy */
1379 Print (stdout, 1, " Optimizer pass %u:\n", ++Pass);
1381 /* Run all optimization steps */
1382 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
1384 /* Print the name of the following optimizer step */
1385 Print (stdout, 1, " %s:%*s", OptFuncs[I].Name,
1386 (int) (30-strlen(OptFuncs[I].Name)), "");
1388 /* Check if the step is disabled */
1389 if (OptFuncs[I].Disabled) {
1390 Print (stdout, 1, "Disabled\n");
1392 unsigned Changes = OptFuncs[I].Func (S);
1393 OptChanges += Changes;
1394 Print (stdout, 1, "%u Changes\n", Changes);
1398 } while (OptChanges > 0);