1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001-2004 Ullrich von Bassewitz */
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 /*****************************************************************************/
48 /*****************************************************************************/
49 /* Helper functions */
50 /*****************************************************************************/
54 static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
55 /* Get the branch distance between the two entries and return it. The distance
56 * will be negative for backward jumps and positive for forward jumps.
59 /* Get the index of the branch target */
60 unsigned TI = CS_GetEntryIndex (S, To);
62 /* Determine the branch distance */
65 /* Forward branch, do not count the current insn */
68 CodeEntry* N = CS_GetEntry (S, J++);
75 CodeEntry* N = CS_GetEntry (S, J++);
80 /* Return the calculated distance */
86 static int IsShortDist (int Distance)
87 /* Return true if the given distance is a short branch distance */
89 return (Distance >= -125 && Distance <= 125);
94 static short ZPRegVal (unsigned short Use, const RegContents* RC)
95 /* Return the contents of the given zeropage register */
97 if ((Use & REG_TMP1) != 0) {
99 } else if ((Use & REG_PTR1_LO) != 0) {
101 } else if ((Use & REG_PTR1_HI) != 0) {
103 } else if ((Use & REG_SREG_LO) != 0) {
105 } else if ((Use & REG_SREG_HI) != 0) {
108 return UNKNOWN_REGVAL;
114 static short RegVal (unsigned short Use, const RegContents* RC)
115 /* Return the contents of the given register */
117 if ((Use & REG_A) != 0) {
119 } else if ((Use & REG_X) != 0) {
121 } else if ((Use & REG_Y) != 0) {
124 return ZPRegVal (Use, RC);
130 /*****************************************************************************/
131 /* Replace jumps to RTS by RTS */
132 /*****************************************************************************/
136 unsigned OptRTSJumps1 (CodeSeg* S)
137 /* Replace jumps to RTS by RTS */
139 unsigned Changes = 0;
141 /* Walk over all entries minus the last one */
143 while (I < CS_GetEntryCount (S)) {
145 /* Get the next entry */
146 CodeEntry* E = CS_GetEntry (S, I);
148 /* Check if it's an unconditional branch to a local target */
149 if ((E->Info & OF_UBRA) != 0 &&
151 E->JumpTo->Owner->OPC == OP65_RTS) {
153 /* Insert an RTS instruction */
154 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
155 CS_InsertEntry (S, X, I+1);
157 /* Delete the jump */
160 /* Remember, we had changes */
170 /* Return the number of changes made */
176 unsigned OptRTSJumps2 (CodeSeg* S)
177 /* Replace long conditional jumps to RTS */
179 unsigned Changes = 0;
181 /* Walk over all entries minus the last one */
183 while (I < CS_GetEntryCount (S)) {
187 /* Get the next entry */
188 CodeEntry* E = CS_GetEntry (S, I);
190 /* Check if it's an unconditional branch to a local target */
191 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
192 (E->Info & OF_LBRA) != 0 && /* Long branch */
193 E->JumpTo != 0 && /* Local label */
194 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
195 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
201 /* We will create a jump around an RTS instead of the long branch */
202 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
203 CS_InsertEntry (S, X, I+1);
205 /* Get the new branch opcode */
206 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
208 /* Get the label attached to N, create a new one if needed */
209 LN = CS_GenLabel (S, N);
211 /* Generate the branch */
212 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
213 CS_InsertEntry (S, X, I+1);
215 /* Delete the long branch */
218 /* Remember, we had changes */
228 /* Return the number of changes made */
234 /*****************************************************************************/
235 /* Remove dead jumps */
236 /*****************************************************************************/
240 unsigned OptDeadJumps (CodeSeg* S)
241 /* Remove dead jumps (jumps to the next instruction) */
243 unsigned Changes = 0;
245 /* Walk over all entries minus the last one */
247 while (I < CS_GetEntryCount (S)) {
249 /* Get the next entry */
250 CodeEntry* E = CS_GetEntry (S, I);
252 /* Check if it's a branch, if it has a local target, and if the target
253 * is the next instruction.
255 if (E->AM == AM65_BRA &&
257 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
259 /* Delete the dead jump */
262 /* Remember, we had changes */
273 /* Return the number of changes made */
279 /*****************************************************************************/
280 /* Remove dead code */
281 /*****************************************************************************/
285 unsigned OptDeadCode (CodeSeg* S)
286 /* Remove dead code (code that follows an unconditional jump or an rts/rti
290 unsigned Changes = 0;
292 /* Walk over all entries */
294 while (I < CS_GetEntryCount (S)) {
300 CodeEntry* E = CS_GetEntry (S, I);
302 /* Check if it's an unconditional branch, and if the next entry has
303 * no labels attached, or if the label is just used so that the insn
304 * can jump to itself.
306 if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
307 (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
308 (!CE_HasLabel (N) || /* Don't has a label */
309 ((N->Info & OF_UBRA) != 0 && /* Uncond branch */
310 (LN = N->JumpTo) != 0 && /* Jumps to known label */
311 LN->Owner == N && /* Attached to insn */
312 CL_GetRefCount (LN) == 1))) { /* Only reference */
314 /* Delete the next entry */
315 CS_DelEntry (S, I+1);
317 /* Remember, we had changes */
328 /* Return the number of changes made */
334 /*****************************************************************************/
335 /* Optimize jump cascades */
336 /*****************************************************************************/
340 unsigned OptJumpCascades (CodeSeg* S)
341 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
342 * replaced by a jump to the final location. This will in some cases produce
343 * worse code, because some jump targets are no longer reachable by short
344 * branches, but this is quite rare, so there are more advantages than
348 unsigned Changes = 0;
350 /* Walk over all entries */
352 while (I < CS_GetEntryCount (S)) {
358 CodeEntry* E = CS_GetEntry (S, I);
360 /* Check if it's a branch, if it has a jump label, if this jump
361 * label is not attached to the instruction itself, and if the
362 * target instruction is itself a branch.
364 if ((E->Info & OF_BRA) != 0 &&
365 (OldLabel = E->JumpTo) != 0 &&
366 (N = OldLabel->Owner) != E &&
367 (N->Info & OF_BRA) != 0) {
369 /* Check if we can use the final target label. This is the case,
370 * if the target branch is an absolut branch, or if it is a
371 * conditional branch checking the same condition as the first one.
373 if ((N->Info & OF_UBRA) != 0 ||
374 ((E->Info & OF_CBRA) != 0 &&
375 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
377 /* This is a jump cascade and we may jump to the final target,
378 * provided that the other insn does not jump to itself. If
379 * this is the case, we can also jump to ourselves, otherwise
380 * insert a jump to the new instruction and remove the old one.
383 CodeLabel* LN = N->JumpTo;
385 if (LN != 0 && LN->Owner == N) {
387 /* We found a jump to a jump to itself. Replace our jump
388 * by a jump to itself.
390 CodeLabel* LE = CS_GenLabel (S, E);
391 X = NewCodeEntry (E->OPC, E->AM, LE->Name, LE, E->LI);
395 /* Jump to the final jump target */
396 X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
400 /* Insert it behind E */
401 CS_InsertEntry (S, X, I+1);
406 /* Remember, we had changes */
414 /* Check if both are conditional branches, and the condition of
415 * the second is the inverse of that of the first. In this case,
416 * the second branch will never be taken, and we may jump directly
417 * to the instruction behind this one.
419 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
421 CodeEntry* X; /* Instruction behind N */
422 CodeLabel* LX; /* Label attached to X */
424 /* Get the branch conditions of both branches */
425 bc_t BC1 = GetBranchCond (E->OPC);
426 bc_t BC2 = GetBranchCond (N->OPC);
428 /* Check the branch conditions */
429 if (BC1 != GetInverseCond (BC2)) {
430 /* Condition not met */
434 /* We may jump behind this conditional branch. Get the
435 * pointer to the next instruction
437 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
438 /* N is the last entry, bail out */
442 /* Get the label attached to X, create a new one if needed */
443 LX = CS_GenLabel (S, X);
445 /* Move the reference from E to the new label */
446 CS_MoveLabelRef (S, E, LX);
448 /* Remember, we had changes */
463 /* Return the number of changes made */
469 /*****************************************************************************/
470 /* Optimize jsr/rts */
471 /*****************************************************************************/
475 unsigned OptRTS (CodeSeg* S)
476 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
477 * replaced by a jump. Don't bother to delete the RTS if it does not have a
478 * label, the dead code elimination should take care of it.
481 unsigned Changes = 0;
483 /* Walk over all entries minus the last one */
485 while (I < CS_GetEntryCount (S)) {
490 CodeEntry* E = CS_GetEntry (S, I);
492 /* Check if it's a subroutine call and if the following insn is RTS */
493 if (E->OPC == OP65_JSR &&
494 (N = CS_GetNextEntry (S, I)) != 0 &&
495 N->OPC == OP65_RTS) {
497 /* Change the jsr to a jmp and use the additional info for a jump */
499 CE_ReplaceOPC (E, OP65_JMP);
501 /* Remember, we had changes */
511 /* Return the number of changes made */
517 /*****************************************************************************/
518 /* Optimize jump targets */
519 /*****************************************************************************/
523 unsigned OptJumpTarget (CodeSeg* S)
524 /* If the instruction preceeding an unconditional branch is the same as the
525 * instruction preceeding the jump target, the jump target may be moved
526 * one entry back. This is a size optimization, since the instruction before
527 * the branch gets removed.
530 unsigned Changes = 0;
531 CodeEntry* E1; /* Entry 1 */
532 CodeEntry* E2; /* Entry 2 */
533 CodeEntry* T1; /* Jump target entry 1 */
534 CodeLabel* TL1; /* Target label 1 */
536 /* Walk over the entries */
538 while (I < CS_GetEntryCount (S)) {
541 E2 = CS_GetNextEntry (S, I);
543 /* Check if we have a jump or branch, and a matching label, which
544 * is not attached to the jump itself
547 (E2->Info & OF_UBRA) != 0 &&
549 E2->JumpTo->Owner != E2) {
551 /* Get the entry preceeding the branch target */
552 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
554 /* There is no such entry */
558 /* Get the entry preceeding the jump */
559 E1 = CS_GetEntry (S, I);
561 /* Check if both preceeding instructions are identical */
562 if (!CodeEntriesAreEqual (E1, T1)) {
563 /* Not equal, try next */
567 /* Get the label for the instruction preceeding the jump target.
568 * This routine will create a new label if the instruction does
569 * not already have one.
571 TL1 = CS_GenLabel (S, T1);
573 /* Change the jump target to point to this new label */
574 CS_MoveLabelRef (S, E2, TL1);
576 /* If the instruction preceeding the jump has labels attached,
577 * move references to this label to the new label.
579 if (CE_HasLabel (E1)) {
580 CS_MoveLabels (S, E1, T1);
583 /* Remove the entry preceeding the jump */
586 /* Remember, we had changes */
597 /* Return the number of changes made */
603 /*****************************************************************************/
604 /* Optimize conditional branches */
605 /*****************************************************************************/
609 unsigned OptCondBranches (CodeSeg* S)
610 /* Performs several optimization steps:
612 * - If an immidiate load of a register is followed by a conditional jump that
613 * is never taken because the load of the register sets the flags in such a
614 * manner, remove the conditional branch.
615 * - If the conditional branch is always taken because of the register load,
616 * replace it by a jmp.
617 * - If a conditional branch jumps around an unconditional branch, remove the
618 * conditional branch and make the jump a conditional branch with the
619 * inverse condition of the first one.
622 unsigned Changes = 0;
624 /* Walk over the entries */
626 while (I < CS_GetEntryCount (S)) {
632 CodeEntry* E = CS_GetEntry (S, I);
634 /* Check if it's a register load */
635 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
636 E->AM == AM65_IMM && /* ..with immidiate addressing */
637 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
638 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
639 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
640 !CE_HasLabel (N)) { /* ..and does not have a label */
642 /* Get the branch condition */
643 bc_t BC = GetBranchCond (N->OPC);
645 /* Check the argument against the branch condition */
646 if ((BC == BC_EQ && E->Num != 0) ||
647 (BC == BC_NE && E->Num == 0) ||
648 (BC == BC_PL && (E->Num & 0x80) != 0) ||
649 (BC == BC_MI && (E->Num & 0x80) == 0)) {
651 /* Remove the conditional branch */
652 CS_DelEntry (S, I+1);
654 /* Remember, we had changes */
657 } else if ((BC == BC_EQ && E->Num == 0) ||
658 (BC == BC_NE && E->Num != 0) ||
659 (BC == BC_PL && (E->Num & 0x80) == 0) ||
660 (BC == BC_MI && (E->Num & 0x80) != 0)) {
662 /* The branch is always taken, replace it by a jump */
663 CE_ReplaceOPC (N, OP65_JMP);
665 /* Remember, we had changes */
671 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
672 (L = E->JumpTo) != 0 && /* ..referencing a local label */
673 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
674 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
675 !CE_HasLabel (N) && /* ..has no label attached */
676 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
678 /* Replace the jump by a conditional branch with the inverse branch
679 * condition than the branch around it.
681 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
683 /* Remove the conditional branch */
686 /* Remember, we had changes */
696 /* Return the number of changes made */
702 /*****************************************************************************/
703 /* Remove unused loads and stores */
704 /*****************************************************************************/
708 unsigned OptUnusedLoads (CodeSeg* S)
709 /* Remove loads of registers where the value loaded is not used later. */
711 unsigned Changes = 0;
713 /* Walk over the entries */
715 while (I < CS_GetEntryCount (S)) {
720 CodeEntry* E = CS_GetEntry (S, I);
722 /* Check if it's a register load or transfer insn */
723 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
724 (N = CS_GetNextEntry (S, I)) != 0 &&
725 !CE_UseLoadFlags (N)) {
727 /* Check which sort of load or transfer it is */
734 case OP65_TYA: R = REG_A; break;
738 case OP65_TAX: R = REG_X; break;
742 case OP65_TAY: R = REG_Y; break;
743 default: goto NextEntry; /* OOPS */
746 /* Get register usage and check if the register value is used later */
747 if ((GetRegInfo (S, I+1, R) & R) == 0) {
749 /* Register value is not used, remove the load */
752 /* Remember, we had changes. Account the deleted entry in I. */
765 /* Return the number of changes made */
771 unsigned OptUnusedStores (CodeSeg* S)
772 /* Remove stores into zero page registers that aren't used later */
774 unsigned Changes = 0;
776 /* Walk over the entries */
778 while (I < CS_GetEntryCount (S)) {
781 CodeEntry* E = CS_GetEntry (S, I);
783 /* Check if it's a register load or transfer insn */
784 if ((E->Info & OF_STORE) != 0 &&
786 (E->Chg & REG_ZP) != 0) {
788 /* Check for the zero page location. We know that there cannot be
789 * more than one zero page location involved in the store.
791 unsigned R = E->Chg & REG_ZP;
793 /* Get register usage and check if the register value is used later */
794 if ((GetRegInfo (S, I+1, R) & R) == 0) {
796 /* Register value is not used, remove the load */
799 /* Remember, we had changes */
810 /* Return the number of changes made */
816 unsigned OptDupLoads (CodeSeg* S)
817 /* Remove loads of registers where the value loaded is already in the register. */
819 unsigned Changes = 0;
822 /* Generate register info for this step */
825 /* Walk over the entries */
827 while (I < CS_GetEntryCount (S)) {
832 CodeEntry* E = CS_GetEntry (S, I);
834 /* Assume we won't delete the entry */
837 /* Get a pointer to the input registers of the insn */
838 const RegContents* In = &E->RI->In;
840 /* Handle the different instructions */
844 if (RegValIsKnown (In->RegA) && /* Value of A is known */
845 CE_KnownImm (E) && /* Value to be loaded is known */
846 In->RegA == (long) E->Num && /* Both are equal */
847 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
848 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
854 if (RegValIsKnown (In->RegX) && /* Value of X is known */
855 CE_KnownImm (E) && /* Value to be loaded is known */
856 In->RegX == (long) E->Num && /* Both are equal */
857 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
858 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
864 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
865 CE_KnownImm (E) && /* Value to be loaded is known */
866 In->RegY == (long) E->Num && /* Both are equal */
867 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
868 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
874 /* If we store into a known zero page location, and this
875 * location does already contain the value to be stored,
878 if (RegValIsKnown (In->RegA) && /* Value of A is known */
879 E->AM == AM65_ZP && /* Store into zp */
880 In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
887 /* If we store into a known zero page location, and this
888 * location does already contain the value to be stored,
891 if (RegValIsKnown (In->RegX) && /* Value of A is known */
892 E->AM == AM65_ZP && /* Store into zp */
893 In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
897 /* If the value in the X register is known and the same as
898 * that in the A register, replace the store by a STA. The
899 * optimizer will then remove the load instruction for X
900 * later. STX does support the zeropage,y addressing mode,
901 * so be sure to check for that.
903 } else if (RegValIsKnown (In->RegX) &&
904 In->RegX == In->RegA &&
905 E->AM != AM65_ABSY &&
907 /* Use the A register instead */
908 CE_ReplaceOPC (E, OP65_STA);
913 /* If we store into a known zero page location, and this
914 * location does already contain the value to be stored,
917 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
918 E->AM == AM65_ZP && /* Store into zp */
919 In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
923 /* If the value in the Y register is known and the same as
924 * that in the A register, replace the store by a STA. The
925 * optimizer will then remove the load instruction for Y
926 * later. If replacement by A is not possible try a
927 * replacement by X, but check for invalid addressing modes
930 } else if (RegValIsKnown (In->RegY)) {
931 if (In->RegY == In->RegA) {
932 CE_ReplaceOPC (E, OP65_STA);
933 } else if (In->RegY == In->RegX &&
934 E->AM != AM65_ABSX &&
936 CE_ReplaceOPC (E, OP65_STX);
942 /* If we store into a known zero page location, and this
943 * location does already contain the value to be stored,
946 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && E->AM == AM65_ZP) {
947 if (ZPRegVal (E->Chg, In) == 0) {
954 if (RegValIsKnown (In->RegA) &&
955 In->RegA == In->RegX &&
956 (N = CS_GetNextEntry (S, I)) != 0 &&
957 !CE_UseLoadFlags (N)) {
958 /* Value is identical and not followed by a branch */
964 if (RegValIsKnown (In->RegA) &&
965 In->RegA == In->RegY &&
966 (N = CS_GetNextEntry (S, I)) != 0 &&
967 !CE_UseLoadFlags (N)) {
968 /* Value is identical and not followed by a branch */
974 if (RegValIsKnown (In->RegX) &&
975 In->RegX == In->RegA &&
976 (N = CS_GetNextEntry (S, I)) != 0 &&
977 !CE_UseLoadFlags (N)) {
978 /* Value is identical and not followed by a branch */
984 if (RegValIsKnown (In->RegY) &&
985 In->RegY == In->RegA &&
986 (N = CS_GetNextEntry (S, I)) != 0 &&
987 !CE_UseLoadFlags (N)) {
988 /* Value is identical and not followed by a branch */
998 /* Delete the entry if requested */
1001 /* Register value is not used, remove the load */
1004 /* Remember, we had changes */
1016 /* Free register info */
1019 /* Return the number of changes made */
1025 unsigned OptStoreLoad (CodeSeg* S)
1026 /* Remove a store followed by a load from the same location. */
1028 unsigned Changes = 0;
1030 /* Walk over the entries */
1032 while (I < CS_GetEntryCount (S)) {
1037 /* Get next entry */
1038 CodeEntry* E = CS_GetEntry (S, I);
1040 /* Check if it is a store instruction followed by a load from the
1041 * same address which is itself not followed by a conditional branch.
1043 if ((E->Info & OF_STORE) != 0 &&
1044 (N = CS_GetNextEntry (S, I)) != 0 &&
1047 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1048 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1049 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1050 strcmp (E->Arg, N->Arg) == 0 &&
1051 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1052 !CE_UseLoadFlags (X)) {
1054 /* Register has already the correct value, remove the load */
1055 CS_DelEntry (S, I+1);
1057 /* Remember, we had changes */
1067 /* Return the number of changes made */
1073 unsigned OptTransfers1 (CodeSeg* S)
1074 /* Remove transfers from one register to another and back */
1076 unsigned Changes = 0;
1078 /* Walk over the entries */
1080 while (I < CS_GetEntryCount (S)) {
1086 /* Get next entry */
1087 CodeEntry* E = CS_GetEntry (S, I);
1089 /* Check if we have two transfer instructions */
1090 if ((E->Info & OF_XFR) != 0 &&
1091 (N = CS_GetNextEntry (S, I)) != 0 &&
1093 (N->Info & OF_XFR) != 0) {
1095 /* Check if it's a transfer and back */
1096 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1097 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1098 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1099 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1101 /* If the next insn is a conditional branch, check if the insn
1102 * preceeding the first xfr will set the flags right, otherwise we
1103 * may not remove the sequence.
1105 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1108 if (CE_UseLoadFlags (X)) {
1110 /* No preceeding entry */
1113 P = CS_GetEntry (S, I-1);
1114 if ((P->Info & OF_SETF) == 0) {
1115 /* Does not set the flags */
1120 /* Remove both transfers */
1121 CS_DelEntry (S, I+1);
1124 /* Remember, we had changes */
1135 /* Return the number of changes made */
1141 unsigned OptTransfers2 (CodeSeg* S)
1142 /* Replace loads followed by a register transfer by a load with the second
1143 * register if possible.
1146 unsigned Changes = 0;
1148 /* Walk over the entries */
1150 while (I < CS_GetEntryCount (S)) {
1154 /* Get next entry */
1155 CodeEntry* E = CS_GetEntry (S, I);
1157 /* Check if we have a load followed by a transfer where the loaded
1158 * register is not used later.
1160 if ((E->Info & OF_LOAD) != 0 &&
1161 (N = CS_GetNextEntry (S, I)) != 0 &&
1163 (N->Info & OF_XFR) != 0 &&
1164 GetRegInfo (S, I+2, E->Chg) != E->Chg) {
1168 if (E->OPC == OP65_LDA && N->OPC == OP65_TAX) {
1169 /* LDA/TAX - check for the right addressing modes */
1170 if (E->AM == AM65_IMM ||
1172 E->AM == AM65_ABS ||
1173 E->AM == AM65_ABSY) {
1175 X = NewCodeEntry (OP65_LDX, E->AM, E->Arg, 0, N->LI);
1177 } else if (E->OPC == OP65_LDA && N->OPC == OP65_TAY) {
1178 /* LDA/TAY - check for the right addressing modes */
1179 if (E->AM == AM65_IMM ||
1181 E->AM == AM65_ZPX ||
1182 E->AM == AM65_ABS ||
1183 E->AM == AM65_ABSX) {
1185 X = NewCodeEntry (OP65_LDY, E->AM, E->Arg, 0, N->LI);
1187 } else if (E->OPC == OP65_LDY && N->OPC == OP65_TYA) {
1188 /* LDY/TYA. LDA supports all addressing modes LDY does */
1189 X = NewCodeEntry (OP65_LDA, E->AM, E->Arg, 0, N->LI);
1190 } else if (E->OPC == OP65_LDX && N->OPC == OP65_TXA) {
1191 /* LDX/TXA. LDA doesn't support zp,y, so we must map it to
1194 am_t AM = (E->AM == AM65_ZPY)? AM65_ABSY : E->AM;
1195 X = NewCodeEntry (OP65_LDA, AM, E->Arg, 0, N->LI);
1198 /* If we have a load entry, add it and remove the old stuff */
1200 CS_InsertEntry (S, X, I+2);
1201 CS_DelEntries (S, I, 2);
1203 --I; /* Correct for one entry less */
1211 /* Return the number of changes made */
1217 unsigned OptPushPop (CodeSeg* S)
1218 /* Remove a PHA/PLA sequence were A is not used later */
1220 unsigned Changes = 0;
1221 unsigned Push = 0; /* Index of push insn */
1222 unsigned Pop = 0; /* Index of pop insn */
1227 } State = Searching;
1229 /* Walk over the entries. Look for a push instruction that is followed by
1230 * a pop later, where the pop is not followed by an conditional branch,
1231 * and where the value of the A register is not used later on.
1232 * Look out for the following problems:
1234 * - There may be another PHA/PLA inside the sequence: Restart it.
1235 * - If the PLA has a label, all jumps to this label must be inside
1236 * the sequence, otherwise we cannot remove the PHA/PLA.
1239 while (I < CS_GetEntryCount (S)) {
1241 /* Get next entry */
1242 CodeEntry* E = CS_GetEntry (S, I);
1247 if (E->OPC == OP65_PHA) {
1248 /* Found start of sequence */
1255 if (E->OPC == OP65_PHA) {
1256 /* Inner push/pop, restart */
1258 } else if (E->OPC == OP65_PLA) {
1259 /* Found a matching pop */
1266 /* Next insn, just check if it is no conditional branch and
1267 * that A is not used later. Check also that the range we have
1268 * found now is a basic block, which means that the PHA is the
1269 * only entrance and the PLA the only exit.
1271 if ((E->Info & OF_CBRA) == 0 &&
1273 CS_IsBasicBlock (S, Push, Pop)) {
1274 /* We can remove the PHA and PLA instructions */
1275 CS_DelEntry (S, Pop);
1276 CS_DelEntry (S, Push);
1277 /* Correct I so we continue with the next insn */
1279 /* Remember we had changes */
1282 /* Go into search mode again */
1292 /* Return the number of changes made */
1298 unsigned OptPrecalc (CodeSeg* S)
1299 /* Replace immediate operations with the accu where the current contents are
1300 * known by a load of the final value.
1303 unsigned Changes = 0;
1306 /* Generate register info for this step */
1309 /* Walk over the entries */
1311 while (I < CS_GetEntryCount (S)) {
1313 /* Get next entry */
1314 CodeEntry* E = CS_GetEntry (S, I);
1316 /* Get a pointer to the output registers of the insn */
1317 const RegContents* Out = &E->RI->Out;
1319 /* Argument for LDA and flag */
1320 const char* Arg = 0;
1322 /* Handle the different instructions */
1326 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegA)) {
1327 /* Result of load is known */
1328 Arg = MakeHexArg (Out->RegA);
1333 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegX)) {
1334 /* Result of load is known */
1335 Arg = MakeHexArg (Out->RegX);
1340 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegY)) {
1341 /* Result of load is known */
1342 Arg = MakeHexArg (Out->RegY);
1351 if (RegValIsKnown (Out->RegA)) {
1352 /* Accu op zp with known contents */
1353 Arg = MakeHexArg (Out->RegA);
1358 if (CE_KnownImm (E) && E->Num == 0xFF) {
1359 /* AND with 0xFF, remove */
1362 } else if (RegValIsKnown (Out->RegA)) {
1363 /* Accu AND zp with known contents */
1364 Arg = MakeHexArg (Out->RegA);
1369 if (CE_KnownImm (E) && E->Num == 0x00) {
1370 /* ORA with zero, remove */
1373 } else if (RegValIsKnown (Out->RegA)) {
1374 /* Accu AND zp with known contents */
1375 Arg = MakeHexArg (Out->RegA);
1384 /* Check if we have to replace the insn by LDA */
1386 CodeEntry* X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1387 CS_InsertEntry (S, X, I+1);
1396 /* Free register info */
1399 /* Return the number of changes made */
1405 /*****************************************************************************/
1406 /* Optimize branch types */
1407 /*****************************************************************************/
1411 unsigned OptBranchDist (CodeSeg* S)
1412 /* Change branches for the distance needed. */
1414 unsigned Changes = 0;
1416 /* Walk over the entries */
1418 while (I < CS_GetEntryCount (S)) {
1420 /* Get next entry */
1421 CodeEntry* E = CS_GetEntry (S, I);
1423 /* Check if it's a conditional branch to a local label. */
1424 if (E->Info & OF_CBRA) {
1426 /* Is this a branch to a local symbol? */
1427 if (E->JumpTo != 0) {
1429 /* Check if the branch distance is short */
1430 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1432 /* Make the branch short/long according to distance */
1433 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1434 /* Short branch but long distance */
1435 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1437 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1438 /* Long branch but short distance */
1439 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1443 } else if ((E->Info & OF_LBRA) == 0) {
1445 /* Short branch to external symbol - make it long */
1446 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1451 } else if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 &&
1452 (E->Info & OF_UBRA) != 0 &&
1454 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1456 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1457 CE_ReplaceOPC (E, OP65_BRA);
1466 /* Return the number of changes made */