1 /*****************************************************************************/
5 /* Optimize compares */
9 /* (C) 2001-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 /* Table used to invert a condition, indexed by condition */
53 static const unsigned char CmpInvertTab [] = {
55 CMP_LE, CMP_LT, CMP_GE, CMP_GT,
56 CMP_ULE, CMP_ULT, CMP_UGE, CMP_UGT
61 /*****************************************************************************/
62 /* Helper functions */
63 /*****************************************************************************/
67 static void ReplaceCmp (CodeSeg* S, unsigned I, cmp_t Cond)
68 /* Helper function for the replacement of routines that return a boolean
69 * followed by a conditional jump. Instead of the boolean value, the condition
70 * codes are evaluated directly.
71 * I is the index of the conditional branch, the sequence is already checked
79 CodeEntry* E = CS_GetEntry (S, I);
81 /* Replace the conditional branch */
85 CE_ReplaceOPC (E, OP65_JEQ);
89 CE_ReplaceOPC (E, OP65_JNE);
98 if ((N = CS_GetNextEntry (S, I)) == 0) {
100 Internal ("Invalid program flow");
102 L = CS_GenLabel (S, N);
103 N = NewCodeEntry (OP65_BEQ, AM65_BRA, L->Name, L, E->LI);
104 CS_InsertEntry (S, N, I);
105 CE_ReplaceOPC (E, OP65_JPL);
109 CE_ReplaceOPC (E, OP65_JPL);
113 CE_ReplaceOPC (E, OP65_JMI);
121 CE_ReplaceOPC (E, OP65_JMI);
123 N = NewCodeEntry (OP65_JEQ, AM65_BRA, L->Name, L, E->LI);
124 CS_InsertEntry (S, N, I+1);
133 if ((N = CS_GetNextEntry (S, I)) == 0) {
135 Internal ("Invalid program flow");
137 L = CS_GenLabel (S, N);
138 N = NewCodeEntry (OP65_BEQ, AM65_BRA, L->Name, L, E->LI);
139 CS_InsertEntry (S, N, I);
140 CE_ReplaceOPC (E, OP65_JCS);
144 CE_ReplaceOPC (E, OP65_JCS);
148 CE_ReplaceOPC (E, OP65_JCC);
156 CE_ReplaceOPC (E, OP65_JCC);
158 N = NewCodeEntry (OP65_JEQ, AM65_BRA, L->Name, L, E->LI);
159 CS_InsertEntry (S, N, I+1);
163 Internal ("Unknown jump condition: %d", Cond);
171 static int IsImmCmp16 (CodeEntry** L)
172 /* Check if the instructions at L are an immidiate compare of a/x:
177 return (L[0]->OPC == OP65_CPX &&
178 L[0]->AM == AM65_IMM &&
179 (L[0]->Flags & CEF_NUMARG) != 0 &&
180 !CE_HasLabel (L[0]) &&
181 (L[1]->OPC == OP65_JNE || L[1]->OPC == OP65_BNE) &&
183 !CE_HasLabel (L[1]) &&
184 L[2]->OPC == OP65_CMP &&
185 L[2]->AM == AM65_IMM &&
186 (L[2]->Flags & CEF_NUMARG) != 0 &&
187 (L[3]->Info & OF_CBRA) != 0 &&
189 (L[1]->JumpTo->Owner == L[3] || L[1]->JumpTo == L[3]->JumpTo));
194 static int GetCmpRegVal (const CodeEntry* E)
195 /* Return the register value for an immediate compare */
198 case OP65_CMP: return E->RI->In.RegA;
199 case OP65_CPX: return E->RI->In.RegX;
200 case OP65_CPY: return E->RI->In.RegY;
201 default: Internal ("Invalid opcode in GetCmpRegVal");
202 return 0; /* Not reached */
208 /*****************************************************************************/
209 /* Remove calls to the bool transformer subroutines */
210 /*****************************************************************************/
214 unsigned OptBoolTrans (CodeSeg* S)
215 /* Try to remove the call to boolean transformer routines where the call is
219 unsigned Changes = 0;
221 /* Walk over the entries */
223 while (I < CS_GetEntryCount (S)) {
229 CodeEntry* E = CS_GetEntry (S, I);
231 /* Check for a boolean transformer */
232 if (E->OPC == OP65_JSR &&
233 (Cond = FindBoolCmpCond (E->Arg)) != CMP_INV &&
234 (N = CS_GetNextEntry (S, I)) != 0 &&
235 (N->Info & OF_ZBRA) != 0) {
237 /* Make the boolean transformer unnecessary by changing the
238 * the conditional jump to evaluate the condition flags that
239 * are set after the compare directly. Note: jeq jumps if
240 * the condition is not met, jne jumps if the condition is met.
241 * Invert the code if we jump on condition not met.
243 if (GetBranchCond (N->OPC) == BC_EQ) {
244 /* Jumps if condition false, invert condition */
245 Cond = CmpInvertTab [Cond];
248 /* Check if we can replace the code by something better */
249 ReplaceCmp (S, I+1, Cond);
251 /* Remove the call to the bool transformer */
254 /* Remember, we had changes */
264 /* Return the number of changes made */
270 /*****************************************************************************/
271 /* Optimizations for compares */
272 /*****************************************************************************/
276 unsigned OptCmp1 (CodeSeg* S)
277 /* Search for the sequence
288 unsigned Changes = 0;
290 /* Walk over the entries */
292 while (I < CS_GetEntryCount (S)) {
297 L[0] = CS_GetEntry (S, I);
299 /* Check for the sequence */
300 if (L[0]->OPC == OP65_LDX &&
301 !CS_RangeHasLabel (S, I+1, 2) &&
302 CS_GetEntries (S, L+1, I+1, 2) &&
303 L[1]->OPC == OP65_STX &&
304 strcmp (L[1]->Arg, "tmp1") == 0 &&
305 L[2]->OPC == OP65_ORA &&
306 strcmp (L[2]->Arg, "tmp1") == 0) {
310 /* Insert the ora instead */
311 X = NewCodeEntry (OP65_ORA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
312 CS_InsertEntry (S, X, I);
314 /* Remove all other instructions */
315 CS_DelEntries (S, I+1, 3);
317 /* Remember, we had changes */
327 /* Return the number of changes made */
333 unsigned OptCmp2 (CodeSeg* S)
334 /* Search for the sequence
346 unsigned Changes = 0;
348 /* Walk over the entries */
350 while (I < CS_GetEntryCount (S)) {
355 CodeEntry* E = CS_GetEntry (S, I);
357 /* Check for the sequence */
358 if (E->OPC == OP65_STX &&
359 !CS_RangeHasLabel (S, I+1, 2) &&
360 CS_GetEntries (S, L, I+1, 2) &&
361 L[0]->OPC == OP65_STX &&
362 strcmp (L[0]->Arg, "tmp1") == 0 &&
363 L[1]->OPC == OP65_ORA &&
364 strcmp (L[1]->Arg, "tmp1") == 0) {
366 /* Remove the remaining instructions */
367 CS_DelEntries (S, I+1, 2);
369 /* Insert the ora instead */
370 CS_InsertEntry (S, NewCodeEntry (OP65_ORA, E->AM, E->Arg, 0, E->LI), I+1);
372 /* Remember, we had changes */
382 /* Return the number of changes made */
388 unsigned OptCmp3 (CodeSeg* S)
391 * lda/and/ora/eor ...
395 * lda/and/ora/eor ...
399 * and remove the cmp.
402 unsigned Changes = 0;
404 /* Walk over the entries */
406 while (I < CS_GetEntryCount (S)) {
411 L[0] = CS_GetEntry (S, I);
413 /* Check for the sequence */
414 if ((L[0]->OPC == OP65_ADC ||
415 L[0]->OPC == OP65_AND ||
416 L[0]->OPC == OP65_ASL ||
417 L[0]->OPC == OP65_DEA ||
418 L[0]->OPC == OP65_EOR ||
419 L[0]->OPC == OP65_INA ||
420 L[0]->OPC == OP65_LDA ||
421 L[0]->OPC == OP65_LSR ||
422 L[0]->OPC == OP65_ORA ||
423 L[0]->OPC == OP65_PLA ||
424 L[0]->OPC == OP65_SBC ||
425 L[0]->OPC == OP65_TXA ||
426 L[0]->OPC == OP65_TYA) &&
427 !CS_RangeHasLabel (S, I+1, 2) &&
428 CS_GetEntries (S, L+1, I+1, 2) &&
429 L[1]->OPC == OP65_CMP &&
430 CE_IsKnownImm (L[1], 0)) {
434 /* Check for the call to boolxx. We only remove the compare if
435 * the carry flag is evaluated later, because the load will not
436 * set the carry flag.
438 if (L[2]->OPC == OP65_JSR) {
439 switch (FindBoolCmpCond (L[2]->Arg)) {
447 /* Remove the compare */
460 } else if ((L[2]->Info & OF_FBRA) != 0) {
462 /* The following insn branches on the condition of a load, so
463 * the compare instruction can be removed.
469 /* Delete the compare if we can */
471 CS_DelEntry (S, I+1);
481 /* Return the number of changes made */
487 unsigned OptCmp4 (CodeSeg* S)
497 * If a is zero, we may remove the compare. If a and b are both zero, we may
498 * replace it by the sequence
504 * L1 may be either the label at the branch instruction, or the target label
505 * of this instruction.
508 unsigned Changes = 0;
510 /* Walk over the entries */
512 while (I < CS_GetEntryCount (S)) {
517 CodeEntry* E = CS_GetEntry (S, I);
519 /* Check for the sequence */
520 if (E->OPC == OP65_LDA &&
521 CS_GetEntries (S, L, I+1, 5) &&
522 L[0]->OPC == OP65_LDX &&
523 !CE_HasLabel (L[0]) &&
525 !RegAXUsed (S, I+6)) {
527 if ((L[4]->Info & OF_FBRA) != 0 && L[1]->Num == 0 && L[3]->Num == 0) {
528 /* The value is zero, we may use the simple code version. */
529 CE_ReplaceOPC (L[0], OP65_ORA);
530 CS_DelEntries (S, I+2, 3);
532 /* Move the lda instruction after the first branch. This will
533 * improve speed, since the load is delayed after the first
536 CS_MoveEntry (S, I, I+4);
538 /* We will replace the ldx/cpx by lda/cmp */
539 CE_ReplaceOPC (L[0], OP65_LDA);
540 CE_ReplaceOPC (L[1], OP65_CMP);
542 /* Beware: If the first LDA instruction had a label, we have
543 * to move this label to the top of the sequence again.
545 if (CE_HasLabel (E)) {
546 CS_MoveLabels (S, E, L[0]);
559 /* Return the number of changes made */
565 unsigned OptCmp5 (CodeSeg* S)
566 /* Optimize compares of local variables:
576 unsigned Changes = 0;
578 /* Walk over the entries */
580 while (I < CS_GetEntryCount (S)) {
584 /* Get the next entry */
585 L[0] = CS_GetEntry (S, I);
587 /* Check for the sequence */
588 if (L[0]->OPC == OP65_LDY &&
589 CE_IsConstImm (L[0]) &&
590 CS_GetEntries (S, L+1, I+1, 5) &&
591 !CE_HasLabel (L[1]) &&
592 CE_IsCallTo (L[1], "ldaxysp") &&
595 if ((L[5]->Info & OF_FBRA) != 0 && L[2]->Num == 0 && L[4]->Num == 0) {
600 /* The value is zero, we may use the simple code version:
607 sprintf (Buf, "$%02X", (int)(L[0]->Num-1));
608 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[0]->LI);
609 CS_InsertEntry (S, X, I+1);
611 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
612 CS_InsertEntry (S, X, I+2);
614 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
615 CS_InsertEntry (S, X, I+3);
617 X = NewCodeEntry (OP65_ORA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
618 CS_InsertEntry (S, X, I+4);
620 CS_DelEntries (S, I+5, 3); /* cpx/bne/cmp */
621 CS_DelEntry (S, I); /* ldy */
628 /* Change the code to just use the A register. Move the load
629 * of the low byte after the first branch if possible:
640 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
641 CS_InsertEntry (S, X, I+3);
643 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
644 CS_InsertEntry (S, X, I+4);
646 X = NewCodeEntry (OP65_CMP, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
647 CS_InsertEntry (S, X, I+5);
649 sprintf (Buf, "$%02X", (int)(L[0]->Num-1));
650 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[0]->LI);
651 CS_InsertEntry (S, X, I+7);
653 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
654 CS_InsertEntry (S, X, I+8);
656 CS_DelEntries (S, I, 3); /* ldy/jsr/cpx */
668 /* Return the number of changes made */
674 unsigned OptCmp6 (CodeSeg* S)
675 /* Search for calls to compare subroutines followed by a conditional branch
676 * and replace them by cheaper versions, since the branch means that the
677 * boolean value returned by these routines is not needed (we may also check
678 * that explicitly, but for the current code generator it is always true).
681 unsigned Changes = 0;
683 /* Walk over the entries */
685 while (I < CS_GetEntryCount (S)) {
691 CodeEntry* E = CS_GetEntry (S, I);
693 /* Check for the sequence */
694 if (E->OPC == OP65_JSR &&
695 (Cond = FindTosCmpCond (E->Arg)) != CMP_INV &&
696 (N = CS_GetNextEntry (S, I)) != 0 &&
697 (N->Info & OF_ZBRA) != 0 &&
700 /* The tos... functions will return a boolean value in a/x and
701 * the Z flag says if this value is zero or not. We will call
702 * a cheaper subroutine instead, one that does not return a
703 * boolean value but only valid flags. Note: jeq jumps if
704 * the condition is not met, jne jumps if the condition is met.
705 * Invert the code if we jump on condition not met.
707 if (GetBranchCond (N->OPC) == BC_EQ) {
708 /* Jumps if condition false, invert condition */
709 Cond = CmpInvertTab [Cond];
712 /* Replace the subroutine call. */
713 E = NewCodeEntry (OP65_JSR, AM65_ABS, "tosicmp", 0, E->LI);
714 CS_InsertEntry (S, E, I+1);
717 /* Replace the conditional branch */
718 ReplaceCmp (S, I+1, Cond);
720 /* Remember, we had changes */
730 /* Return the number of changes made */
736 unsigned OptCmp7 (CodeSeg* S)
737 /* Search for a sequence ldx/txa/branch and remove the txa if A is not
741 unsigned Changes = 0;
743 /* Walk over the entries */
745 while (I < CS_GetEntryCount (S)) {
750 CodeEntry* E = CS_GetEntry (S, I);
752 /* Check for the sequence */
753 if ((E->OPC == OP65_LDX) &&
754 CS_GetEntries (S, L, I+1, 2) &&
755 L[0]->OPC == OP65_TXA &&
756 !CE_HasLabel (L[0]) &&
757 (L[1]->Info & OF_FBRA) != 0 &&
758 !CE_HasLabel (L[1]) &&
759 !RegAUsed (S, I+3)) {
762 CS_DelEntry (S, I+1);
764 /* Remember, we had changes */
774 /* Return the number of changes made */
780 unsigned OptCmp8 (CodeSeg* S)
781 /* Check for register compares where the contents of the register and therefore
782 * the result of the compare is known.
785 unsigned Changes = 0;
788 /* Generate register info for this step */
791 /* Walk over the entries */
793 while (I < CS_GetEntryCount (S)) {
798 CodeEntry* E = CS_GetEntry (S, I);
800 /* Check for a compare against an immediate value */
801 if ((E->Info & OF_CMP) != 0 &&
802 (RegVal = GetCmpRegVal (E)) >= 0 &&
805 /* We are able to evaluate the compare at compile time. Check if
806 * one or more branches are ahead.
808 unsigned JumpsChanged = 0;
810 while ((N = CS_GetNextEntry (S, I)) != 0 && /* Followed by something.. */
811 (N->Info & OF_CBRA) != 0 && /* ..that is a cond branch.. */
812 !CE_HasLabel (N)) { /* ..and has no label */
814 /* Evaluate the branch condition */
816 switch (GetBranchCond (N->OPC)) {
818 Cond = ((unsigned char)RegVal) < ((unsigned char)E->Num);
822 Cond = ((unsigned char)RegVal) >= ((unsigned char)E->Num);
826 Cond = ((unsigned char)RegVal) == ((unsigned char)E->Num);
830 Cond = ((signed char)RegVal) < ((signed char)E->Num);
834 Cond = ((unsigned char)RegVal) != ((unsigned char)E->Num);
838 Cond = ((signed char)RegVal) >= ((signed char)E->Num);
843 /* Not set by the compare operation, bail out (Note:
844 * Just skipping anything here is rather stupid, but
845 * the sequence is never generated by the compiler,
846 * so it's quite safe to skip).
851 Internal ("Unknown branch condition");
855 /* If the condition is false, we may remove the jump. Otherwise
856 * the branch will always be taken, so we may replace it by a
857 * jump (and bail out).
860 CS_DelEntry (S, I+1);
862 CodeLabel* L = N->JumpTo;
863 const char* LabelName = L? L->Name : N->Arg;
864 CodeEntry* X = NewCodeEntry (OP65_JMP, AM65_BRA, LabelName, L, N->LI);
865 CS_InsertEntry (S, X, I+2);
866 CS_DelEntry (S, I+1);
869 /* Remember, we had changes */
874 /* If we have made changes above, we may also remove the compare */
887 /* Free register info */
890 /* Return the number of changes made */
896 unsigned OptCmp9 (CodeSeg* S)
897 /* Search for the sequence
905 * If A is not used later (which should be the case), we can branch on the N
906 * flag instead of the carry flag and remove the asl.
909 unsigned Changes = 0;
912 /* Walk over the entries */
914 while (I < CS_GetEntryCount (S)) {
919 L[0] = CS_GetEntry (S, I);
921 /* Check for the sequence */
922 if (L[0]->OPC == OP65_SBC &&
923 CS_GetEntries (S, L+1, I+1, 4) &&
924 (L[1]->OPC == OP65_BVC ||
925 L[1]->OPC == OP65_BVS) &&
927 L[1]->JumpTo->Owner == L[3] &&
928 L[2]->OPC == OP65_EOR &&
929 CE_IsKnownImm (L[2], 0x80) &&
930 L[3]->OPC == OP65_ASL &&
931 L[3]->AM == AM65_ACC &&
932 (L[4]->OPC == OP65_BCC ||
933 L[4]->OPC == OP65_BCS ||
934 L[4]->OPC == OP65_JCC ||
935 L[4]->OPC == OP65_JCS) &&
936 !CE_HasLabel (L[4]) &&
937 !RegAUsed (S, I+4)) {
939 /* Replace the branch condition */
940 switch (GetBranchCond (L[4]->OPC)) {
941 case BC_CC: CE_ReplaceOPC (L[4], OP65_JPL); break;
942 case BC_CS: CE_ReplaceOPC (L[4], OP65_JMI); break;
943 default: Internal ("Unknown branch condition in OptCmp9");
946 /* Delete the asl insn */
947 CS_DelEntry (S, I+3);
949 /* Next sequence is somewhat ahead (if any) */
952 /* Remember, we had changes */
960 /* Return the number of changes made */