1 /*****************************************************************************/
5 /* Optimize compares */
9 /* (C) 2001-2005, Ullrich von Bassewitz */
10 /* Römerstrasse 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
59 /* Table to show which compares are signed (use the N flag) */
60 static const char CmpSignedTab [] = {
61 0, 0, 1, 1, 1, 1, 0, 0, 0, 0
66 /*****************************************************************************/
67 /* Helper functions */
68 /*****************************************************************************/
72 static void ReplaceCmp (CodeSeg* S, unsigned I, cmp_t Cond)
73 /* Helper function for the replacement of routines that return a boolean
74 * followed by a conditional jump. Instead of the boolean value, the condition
75 * codes are evaluated directly.
76 * I is the index of the conditional branch, the sequence is already checked
84 CodeEntry* E = CS_GetEntry (S, I);
86 /* Replace the conditional branch */
90 CE_ReplaceOPC (E, OP65_JEQ);
94 CE_ReplaceOPC (E, OP65_JNE);
103 if ((N = CS_GetNextEntry (S, I)) == 0) {
105 Internal ("Invalid program flow");
107 L = CS_GenLabel (S, N);
108 N = NewCodeEntry (OP65_BEQ, AM65_BRA, L->Name, L, E->LI);
109 CS_InsertEntry (S, N, I);
110 CE_ReplaceOPC (E, OP65_JPL);
114 CE_ReplaceOPC (E, OP65_JPL);
118 CE_ReplaceOPC (E, OP65_JMI);
126 CE_ReplaceOPC (E, OP65_JMI);
128 N = NewCodeEntry (OP65_JEQ, AM65_BRA, L->Name, L, E->LI);
129 CS_InsertEntry (S, N, I+1);
138 if ((N = CS_GetNextEntry (S, I)) == 0) {
140 Internal ("Invalid program flow");
142 L = CS_GenLabel (S, N);
143 N = NewCodeEntry (OP65_BEQ, AM65_BRA, L->Name, L, E->LI);
144 CS_InsertEntry (S, N, I);
145 CE_ReplaceOPC (E, OP65_JCS);
149 CE_ReplaceOPC (E, OP65_JCS);
153 CE_ReplaceOPC (E, OP65_JCC);
161 CE_ReplaceOPC (E, OP65_JCC);
163 N = NewCodeEntry (OP65_JEQ, AM65_BRA, L->Name, L, E->LI);
164 CS_InsertEntry (S, N, I+1);
168 Internal ("Unknown jump condition: %d", Cond);
176 static int IsImmCmp16 (CodeEntry** L)
177 /* Check if the instructions at L are an immidiate compare of a/x:
182 return (L[0]->OPC == OP65_CPX &&
183 L[0]->AM == AM65_IMM &&
184 (L[0]->Flags & CEF_NUMARG) != 0 &&
185 !CE_HasLabel (L[0]) &&
186 (L[1]->OPC == OP65_JNE || L[1]->OPC == OP65_BNE) &&
188 !CE_HasLabel (L[1]) &&
189 L[2]->OPC == OP65_CMP &&
190 L[2]->AM == AM65_IMM &&
191 (L[2]->Flags & CEF_NUMARG) != 0 &&
192 (L[3]->Info & OF_CBRA) != 0 &&
194 (L[1]->JumpTo->Owner == L[3] || L[1]->JumpTo == L[3]->JumpTo));
199 static int GetCmpRegVal (const CodeEntry* E)
200 /* Return the register value for an immediate compare */
203 case OP65_CMP: return E->RI->In.RegA;
204 case OP65_CPX: return E->RI->In.RegX;
205 case OP65_CPY: return E->RI->In.RegY;
206 default: Internal ("Invalid opcode in GetCmpRegVal");
207 return 0; /* Not reached */
213 /*****************************************************************************/
214 /* Remove calls to the bool transformer subroutines */
215 /*****************************************************************************/
219 unsigned OptBoolTrans (CodeSeg* S)
220 /* Try to remove the call to boolean transformer routines where the call is
224 unsigned Changes = 0;
226 /* Walk over the entries */
228 while (I < CS_GetEntryCount (S)) {
234 CodeEntry* E = CS_GetEntry (S, I);
236 /* Check for a boolean transformer */
237 if (E->OPC == OP65_JSR &&
238 (Cond = FindBoolCmpCond (E->Arg)) != CMP_INV &&
239 (N = CS_GetNextEntry (S, I)) != 0 &&
240 (N->Info & OF_ZBRA) != 0) {
242 /* Make the boolean transformer unnecessary by changing the
243 * the conditional jump to evaluate the condition flags that
244 * are set after the compare directly. Note: jeq jumps if
245 * the condition is not met, jne jumps if the condition is met.
246 * Invert the code if we jump on condition not met.
248 if (GetBranchCond (N->OPC) == BC_EQ) {
249 /* Jumps if condition false, invert condition */
250 Cond = CmpInvertTab [Cond];
253 /* Check if we can replace the code by something better */
254 ReplaceCmp (S, I+1, Cond);
256 /* Remove the call to the bool transformer */
259 /* Remember, we had changes */
269 /* Return the number of changes made */
275 /*****************************************************************************/
276 /* Optimizations for compares */
277 /*****************************************************************************/
281 unsigned OptCmp1 (CodeSeg* S)
282 /* Search for the sequence
293 unsigned Changes = 0;
295 /* Walk over the entries */
297 while (I < CS_GetEntryCount (S)) {
302 L[0] = CS_GetEntry (S, I);
304 /* Check for the sequence */
305 if (L[0]->OPC == OP65_LDX &&
306 !CS_RangeHasLabel (S, I+1, 2) &&
307 CS_GetEntries (S, L+1, I+1, 2) &&
308 L[1]->OPC == OP65_STX &&
309 strcmp (L[1]->Arg, "tmp1") == 0 &&
310 L[2]->OPC == OP65_ORA &&
311 strcmp (L[2]->Arg, "tmp1") == 0) {
315 /* Insert the ora instead */
316 X = NewCodeEntry (OP65_ORA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
317 CS_InsertEntry (S, X, I);
319 /* Remove all other instructions */
320 CS_DelEntries (S, I+1, 3);
322 /* Remember, we had changes */
332 /* Return the number of changes made */
338 unsigned OptCmp2 (CodeSeg* S)
339 /* Search for the sequence
351 unsigned Changes = 0;
353 /* Walk over the entries */
355 while (I < CS_GetEntryCount (S)) {
360 CodeEntry* E = CS_GetEntry (S, I);
362 /* Check for the sequence */
363 if (E->OPC == OP65_STX &&
364 !CS_RangeHasLabel (S, I+1, 2) &&
365 CS_GetEntries (S, L, I+1, 2) &&
366 L[0]->OPC == OP65_STX &&
367 strcmp (L[0]->Arg, "tmp1") == 0 &&
368 L[1]->OPC == OP65_ORA &&
369 strcmp (L[1]->Arg, "tmp1") == 0) {
371 /* Remove the remaining instructions */
372 CS_DelEntries (S, I+1, 2);
374 /* Insert the ora instead */
375 CS_InsertEntry (S, NewCodeEntry (OP65_ORA, E->AM, E->Arg, 0, E->LI), I+1);
377 /* Remember, we had changes */
387 /* Return the number of changes made */
393 unsigned OptCmp3 (CodeSeg* S)
396 * lda/and/ora/eor ...
400 * lda/and/ora/eor ...
404 * and remove the cmp.
407 unsigned Changes = 0;
409 /* Walk over the entries */
411 while (I < CS_GetEntryCount (S)) {
416 L[0] = CS_GetEntry (S, I);
418 /* Check for the sequence */
419 if ((L[0]->OPC == OP65_ADC ||
420 L[0]->OPC == OP65_AND ||
421 L[0]->OPC == OP65_ASL ||
422 L[0]->OPC == OP65_DEA ||
423 L[0]->OPC == OP65_EOR ||
424 L[0]->OPC == OP65_INA ||
425 L[0]->OPC == OP65_LDA ||
426 L[0]->OPC == OP65_LSR ||
427 L[0]->OPC == OP65_ORA ||
428 L[0]->OPC == OP65_PLA ||
429 L[0]->OPC == OP65_SBC ||
430 L[0]->OPC == OP65_TXA ||
431 L[0]->OPC == OP65_TYA) &&
432 !CS_RangeHasLabel (S, I+1, 2) &&
433 CS_GetEntries (S, L+1, I+1, 2) &&
434 L[1]->OPC == OP65_CMP &&
435 CE_IsKnownImm (L[1], 0)) {
437 /* Check for the call to boolxx. We only remove the compare if
438 * the carry flag is evaluated later, because the load will not
439 * set the carry flag.
441 if (L[2]->OPC == OP65_JSR) {
442 switch (FindBoolCmpCond (L[2]->Arg)) {
450 /* Remove the compare */
451 CS_DelEntry (S, I+1);
466 /* Check for a branch on conditions that are set by the load.
467 * Beware: The insn may branch to another conditional branch
468 * that evaluates other flags, so check that.
473 if ((E->Info & (OF_CBRA|OF_UBRA)) != 0) {
474 /* A conditional branch. Check if it jumps on a
475 * condition not set by the load.
477 if ((E->Info & (OF_FBRA|OF_UBRA)) == 0) {
480 } else if (E->JumpTo == 0) {
481 /* Jump to external */
485 /* Check target of branch */
486 E = E->JumpTo->Owner;
489 /* Some other insn */
495 /* Delete the compare if we can */
497 CS_DelEntry (S, I+1);
508 /* Return the number of changes made */
514 unsigned OptCmp4 (CodeSeg* S)
524 * If a is zero, we may remove the compare. If a and b are both zero, we may
525 * replace it by the sequence
531 * L1 may be either the label at the branch instruction, or the target label
532 * of this instruction.
535 unsigned Changes = 0;
537 /* Walk over the entries */
539 while (I < CS_GetEntryCount (S)) {
544 CodeEntry* E = CS_GetEntry (S, I);
546 /* Check for the sequence */
547 if (E->OPC == OP65_LDA &&
548 CS_GetEntries (S, L, I+1, 5) &&
549 L[0]->OPC == OP65_LDX &&
550 !CE_HasLabel (L[0]) &&
552 !RegAXUsed (S, I+6)) {
554 if ((L[4]->Info & OF_FBRA) != 0 && L[1]->Num == 0 && L[3]->Num == 0) {
555 /* The value is zero, we may use the simple code version. */
556 CE_ReplaceOPC (L[0], OP65_ORA);
557 CS_DelEntries (S, I+2, 3);
559 /* Move the lda instruction after the first branch. This will
560 * improve speed, since the load is delayed after the first
563 CS_MoveEntry (S, I, I+4);
565 /* We will replace the ldx/cpx by lda/cmp */
566 CE_ReplaceOPC (L[0], OP65_LDA);
567 CE_ReplaceOPC (L[1], OP65_CMP);
569 /* Beware: If the first LDA instruction had a label, we have
570 * to move this label to the top of the sequence again.
572 if (CE_HasLabel (E)) {
573 CS_MoveLabels (S, E, L[0]);
586 /* Return the number of changes made */
592 unsigned OptCmp5 (CodeSeg* S)
593 /* Optimize compares of local variables:
603 unsigned Changes = 0;
605 /* Walk over the entries */
607 while (I < CS_GetEntryCount (S)) {
611 /* Get the next entry */
612 L[0] = CS_GetEntry (S, I);
614 /* Check for the sequence */
615 if (L[0]->OPC == OP65_LDY &&
616 CE_IsConstImm (L[0]) &&
617 CS_GetEntries (S, L+1, I+1, 5) &&
618 !CE_HasLabel (L[1]) &&
619 CE_IsCallTo (L[1], "ldaxysp") &&
622 if ((L[5]->Info & OF_FBRA) != 0 && L[2]->Num == 0 && L[4]->Num == 0) {
627 /* The value is zero, we may use the simple code version:
634 sprintf (Buf, "$%02X", (int)(L[0]->Num-1));
635 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[0]->LI);
636 CS_InsertEntry (S, X, I+1);
638 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
639 CS_InsertEntry (S, X, I+2);
641 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
642 CS_InsertEntry (S, X, I+3);
644 X = NewCodeEntry (OP65_ORA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
645 CS_InsertEntry (S, X, I+4);
647 CS_DelEntries (S, I+5, 3); /* cpx/bne/cmp */
648 CS_DelEntry (S, I); /* ldy */
655 /* Change the code to just use the A register. Move the load
656 * of the low byte after the first branch if possible:
667 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
668 CS_InsertEntry (S, X, I+3);
670 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
671 CS_InsertEntry (S, X, I+4);
673 X = NewCodeEntry (OP65_CMP, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
674 CS_InsertEntry (S, X, I+5);
676 sprintf (Buf, "$%02X", (int)(L[0]->Num-1));
677 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[0]->LI);
678 CS_InsertEntry (S, X, I+7);
680 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
681 CS_InsertEntry (S, X, I+8);
683 CS_DelEntries (S, I, 3); /* ldy/jsr/cpx */
695 /* Return the number of changes made */
701 unsigned OptCmp6 (CodeSeg* S)
702 /* Search for calls to compare subroutines followed by a conditional branch
703 * and replace them by cheaper versions, since the branch means that the
704 * boolean value returned by these routines is not needed (we may also check
705 * that explicitly, but for the current code generator it is always true).
708 unsigned Changes = 0;
710 /* Walk over the entries */
712 while (I < CS_GetEntryCount (S)) {
718 CodeEntry* E = CS_GetEntry (S, I);
720 /* Check for the sequence */
721 if (E->OPC == OP65_JSR &&
722 (Cond = FindTosCmpCond (E->Arg)) != CMP_INV &&
723 (N = CS_GetNextEntry (S, I)) != 0 &&
724 (N->Info & OF_ZBRA) != 0 &&
727 /* The tos... functions will return a boolean value in a/x and
728 * the Z flag says if this value is zero or not. We will call
729 * a cheaper subroutine instead, one that does not return a
730 * boolean value but only valid flags. Note: jeq jumps if
731 * the condition is not met, jne jumps if the condition is met.
732 * Invert the code if we jump on condition not met.
734 if (GetBranchCond (N->OPC) == BC_EQ) {
735 /* Jumps if condition false, invert condition */
736 Cond = CmpInvertTab [Cond];
739 /* Replace the subroutine call. */
740 E = NewCodeEntry (OP65_JSR, AM65_ABS, "tosicmp", 0, E->LI);
741 CS_InsertEntry (S, E, I+1);
744 /* Replace the conditional branch */
745 ReplaceCmp (S, I+1, Cond);
747 /* Remember, we had changes */
757 /* Return the number of changes made */
763 unsigned OptCmp7 (CodeSeg* S)
764 /* Search for a sequence ldx/txa/branch and remove the txa if A is not
768 unsigned Changes = 0;
770 /* Walk over the entries */
772 while (I < CS_GetEntryCount (S)) {
777 CodeEntry* E = CS_GetEntry (S, I);
779 /* Check for the sequence */
780 if ((E->OPC == OP65_LDX) &&
781 CS_GetEntries (S, L, I+1, 2) &&
782 L[0]->OPC == OP65_TXA &&
783 !CE_HasLabel (L[0]) &&
784 (L[1]->Info & OF_FBRA) != 0 &&
785 !CE_HasLabel (L[1]) &&
786 !RegAUsed (S, I+3)) {
789 CS_DelEntry (S, I+1);
791 /* Remember, we had changes */
801 /* Return the number of changes made */
807 unsigned OptCmp8 (CodeSeg* S)
808 /* Check for register compares where the contents of the register and therefore
809 * the result of the compare is known.
812 unsigned Changes = 0;
815 /* Generate register info for this step */
818 /* Walk over the entries */
820 while (I < CS_GetEntryCount (S)) {
825 CodeEntry* E = CS_GetEntry (S, I);
827 /* Check for a compare against an immediate value */
828 if ((E->Info & OF_CMP) != 0 &&
829 (RegVal = GetCmpRegVal (E)) >= 0 &&
832 /* We are able to evaluate the compare at compile time. Check if
833 * one or more branches are ahead.
835 unsigned JumpsChanged = 0;
837 while ((N = CS_GetNextEntry (S, I)) != 0 && /* Followed by something.. */
838 (N->Info & OF_CBRA) != 0 && /* ..that is a cond branch.. */
839 !CE_HasLabel (N)) { /* ..and has no label */
841 /* Evaluate the branch condition */
843 switch (GetBranchCond (N->OPC)) {
845 Cond = ((unsigned char)RegVal) < ((unsigned char)E->Num);
849 Cond = ((unsigned char)RegVal) >= ((unsigned char)E->Num);
853 Cond = ((unsigned char)RegVal) == ((unsigned char)E->Num);
857 Cond = ((signed char)RegVal) < ((signed char)E->Num);
861 Cond = ((unsigned char)RegVal) != ((unsigned char)E->Num);
865 Cond = ((signed char)RegVal) >= ((signed char)E->Num);
870 /* Not set by the compare operation, bail out (Note:
871 * Just skipping anything here is rather stupid, but
872 * the sequence is never generated by the compiler,
873 * so it's quite safe to skip).
878 Internal ("Unknown branch condition");
882 /* If the condition is false, we may remove the jump. Otherwise
883 * the branch will always be taken, so we may replace it by a
884 * jump (and bail out).
887 CS_DelEntry (S, I+1);
889 CodeLabel* L = N->JumpTo;
890 const char* LabelName = L? L->Name : N->Arg;
891 CodeEntry* X = NewCodeEntry (OP65_JMP, AM65_BRA, LabelName, L, N->LI);
892 CS_InsertEntry (S, X, I+2);
893 CS_DelEntry (S, I+1);
896 /* Remember, we had changes */
901 /* If we have made changes above, we may also remove the compare */
914 /* Free register info */
917 /* Return the number of changes made */