1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001-2003 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 /*****************************************************************************/
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 */
764 /* Return the number of changes made */
770 unsigned OptUnusedStores (CodeSeg* S)
771 /* Remove stores into zero page registers that aren't used later */
773 unsigned Changes = 0;
775 /* Walk over the entries */
777 while (I < CS_GetEntryCount (S)) {
780 CodeEntry* E = CS_GetEntry (S, I);
782 /* Check if it's a register load or transfer insn */
783 if ((E->Info & OF_STORE) != 0 &&
785 (E->Chg & REG_ZP) != 0) {
787 /* Check for the zero page location. We know that there cannot be
788 * more than one zero page location involved in the store.
790 unsigned R = E->Chg & REG_ZP;
792 /* Get register usage and check if the register value is used later */
793 if ((GetRegInfo (S, I+1, R) & R) == 0) {
795 /* Register value is not used, remove the load */
798 /* Remember, we had changes */
809 /* Return the number of changes made */
815 unsigned OptDupLoads (CodeSeg* S)
816 /* Remove loads of registers where the value loaded is already in the register. */
818 unsigned Changes = 0;
821 /* Generate register info for this step */
824 /* Walk over the entries */
826 while (I < CS_GetEntryCount (S)) {
831 CodeEntry* E = CS_GetEntry (S, I);
833 /* Assume we won't delete the entry */
836 /* Get a pointer to the input registers of the insn */
837 const RegContents* In = &E->RI->In;
839 /* Handle the different instructions */
843 if (RegValIsKnown (In->RegA) && /* Value of A is known */
844 CE_KnownImm (E) && /* Value to be loaded is known */
845 In->RegA == (long) E->Num && /* Both are equal */
846 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
847 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
853 if (RegValIsKnown (In->RegX) && /* Value of X is known */
854 CE_KnownImm (E) && /* Value to be loaded is known */
855 In->RegX == (long) E->Num && /* Both are equal */
856 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
857 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
863 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
864 CE_KnownImm (E) && /* Value to be loaded is known */
865 In->RegY == (long) E->Num && /* Both are equal */
866 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
867 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
873 /* If we store into a known zero page location, and this
874 * location does already contain the value to be stored,
877 if (RegValIsKnown (In->RegA) && /* Value of A is known */
878 E->AM == AM65_ZP && /* Store into zp */
879 In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
886 /* If we store into a known zero page location, and this
887 * location does already contain the value to be stored,
890 if (RegValIsKnown (In->RegX) && /* Value of A is known */
891 E->AM == AM65_ZP && /* Store into zp */
892 In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
896 /* If the value in the X register is known and the same as
897 * that in the A register, replace the store by a STA. The
898 * optimizer will then remove the load instruction for X
899 * later. STX does support the zeropage,y addressing mode,
900 * so be sure to check for that.
902 } else if (RegValIsKnown (In->RegX) &&
903 In->RegX == In->RegA &&
904 E->AM != AM65_ABSY &&
906 /* Use the A register instead */
907 CE_ReplaceOPC (E, OP65_STA);
912 /* If we store into a known zero page location, and this
913 * location does already contain the value to be stored,
916 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
917 E->AM == AM65_ZP && /* Store into zp */
918 In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
922 /* If the value in the Y register is known and the same as
923 * that in the A register, replace the store by a STA. The
924 * optimizer will then remove the load instruction for Y
925 * later. If replacement by A is not possible try a
926 * replacement by X, but check for invalid addressing modes
929 } else if (RegValIsKnown (In->RegY)) {
930 if (In->RegY == In->RegA) {
931 CE_ReplaceOPC (E, OP65_STA);
932 } else if (In->RegY == In->RegX &&
933 E->AM != AM65_ABSX &&
935 CE_ReplaceOPC (E, OP65_STX);
941 /* If we store into a known zero page location, and this
942 * location does already contain the value to be stored,
945 if (CPU >= CPU_65C02 && E->AM == AM65_ZP) {
946 if (ZPRegVal (E->Chg, In) == 0) {
953 if (RegValIsKnown (In->RegA) &&
954 In->RegA == In->RegX &&
955 (N = CS_GetNextEntry (S, I)) != 0 &&
956 !CE_UseLoadFlags (N)) {
957 /* Value is identical and not followed by a branch */
963 if (RegValIsKnown (In->RegA) &&
964 In->RegA == In->RegY &&
965 (N = CS_GetNextEntry (S, I)) != 0 &&
966 !CE_UseLoadFlags (N)) {
967 /* Value is identical and not followed by a branch */
973 if (RegValIsKnown (In->RegX) &&
974 In->RegX == In->RegA &&
975 (N = CS_GetNextEntry (S, I)) != 0 &&
976 !CE_UseLoadFlags (N)) {
977 /* Value is identical and not followed by a branch */
983 if (RegValIsKnown (In->RegY) &&
984 In->RegY == In->RegA &&
985 (N = CS_GetNextEntry (S, I)) != 0 &&
986 !CE_UseLoadFlags (N)) {
987 /* Value is identical and not followed by a branch */
997 /* Delete the entry if requested */
1000 /* Register value is not used, remove the load */
1003 /* Remember, we had changes */
1015 /* Free register info */
1018 /* Return the number of changes made */
1024 unsigned OptStoreLoad (CodeSeg* S)
1025 /* Remove a store followed by a load from the same location. */
1027 unsigned Changes = 0;
1029 /* Walk over the entries */
1031 while (I < CS_GetEntryCount (S)) {
1036 /* Get next entry */
1037 CodeEntry* E = CS_GetEntry (S, I);
1039 /* Check if it is a store instruction followed by a load from the
1040 * same address which is itself not followed by a conditional branch.
1042 if ((E->Info & OF_STORE) != 0 &&
1043 (N = CS_GetNextEntry (S, I)) != 0 &&
1046 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1047 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1048 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1049 strcmp (E->Arg, N->Arg) == 0 &&
1050 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1051 !CE_UseLoadFlags (X)) {
1053 /* Register has already the correct value, remove the load */
1054 CS_DelEntry (S, I+1);
1056 /* Remember, we had changes */
1066 /* Return the number of changes made */
1072 unsigned OptTransfers1 (CodeSeg* S)
1073 /* Remove transfers from one register to another and back */
1075 unsigned Changes = 0;
1077 /* Walk over the entries */
1079 while (I < CS_GetEntryCount (S)) {
1085 /* Get next entry */
1086 CodeEntry* E = CS_GetEntry (S, I);
1088 /* Check if we have two transfer instructions */
1089 if ((E->Info & OF_XFR) != 0 &&
1090 (N = CS_GetNextEntry (S, I)) != 0 &&
1092 (N->Info & OF_XFR) != 0) {
1094 /* Check if it's a transfer and back */
1095 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1096 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1097 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1098 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1100 /* If the next insn is a conditional branch, check if the insn
1101 * preceeding the first xfr will set the flags right, otherwise we
1102 * may not remove the sequence.
1104 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1107 if (CE_UseLoadFlags (X)) {
1109 /* No preceeding entry */
1112 P = CS_GetEntry (S, I-1);
1113 if ((P->Info & OF_SETF) == 0) {
1114 /* Does not set the flags */
1119 /* Remove both transfers */
1120 CS_DelEntry (S, I+1);
1123 /* Remember, we had changes */
1134 /* Return the number of changes made */
1140 unsigned OptTransfers2 (CodeSeg* S)
1141 /* Replace loads followed by a register transfer by a load with the second
1142 * register if possible.
1145 unsigned Changes = 0;
1147 /* Walk over the entries */
1149 while (I < CS_GetEntryCount (S)) {
1153 /* Get next entry */
1154 CodeEntry* E = CS_GetEntry (S, I);
1156 /* Check if we have a load followed by a transfer where the loaded
1157 * register is not used later.
1159 if ((E->Info & OF_LOAD) != 0 &&
1160 (N = CS_GetNextEntry (S, I)) != 0 &&
1162 (N->Info & OF_XFR) != 0 &&
1163 GetRegInfo (S, I+2, E->Chg) != E->Chg) {
1167 if (E->OPC == OP65_LDA && N->OPC == OP65_TAX) {
1168 /* LDA/TAX - check for the right addressing modes */
1169 if (E->AM == AM65_IMM ||
1171 E->AM == AM65_ABS ||
1172 E->AM == AM65_ABSY) {
1174 X = NewCodeEntry (OP65_LDX, E->AM, E->Arg, 0, N->LI);
1176 } else if (E->OPC == OP65_LDA && N->OPC == OP65_TAY) {
1177 /* LDA/TAY - check for the right addressing modes */
1178 if (E->AM == AM65_IMM ||
1180 E->AM == AM65_ZPX ||
1181 E->AM == AM65_ABS ||
1182 E->AM == AM65_ABSX) {
1184 X = NewCodeEntry (OP65_LDY, E->AM, E->Arg, 0, N->LI);
1186 } else if (E->OPC == OP65_LDY && N->OPC == OP65_TYA) {
1187 /* LDY/TYA. LDA supports all addressing modes LDY does */
1188 X = NewCodeEntry (OP65_LDA, E->AM, E->Arg, 0, N->LI);
1189 } else if (E->OPC == OP65_LDX && N->OPC == OP65_TXA) {
1190 /* LDX/TXA. LDA doesn't support zp,y, so we must map it to
1193 am_t AM = (E->AM == AM65_ZPY)? AM65_ABSY : E->AM;
1194 X = NewCodeEntry (OP65_LDA, AM, E->Arg, 0, N->LI);
1197 /* If we have a load entry, add it and remove the old stuff */
1199 CS_InsertEntry (S, X, I+2);
1200 CS_DelEntries (S, I, 2);
1202 --I; /* Correct for one entry less */
1210 /* Return the number of changes made */
1216 unsigned OptPushPop (CodeSeg* S)
1217 /* Remove a PHA/PLA sequence were A is not used later */
1219 unsigned Changes = 0;
1220 unsigned Push = 0; /* Index of push insn */
1221 unsigned Pop = 0; /* Index of pop insn */
1226 } State = Searching;
1228 /* Walk over the entries. Look for a push instruction that is followed by
1229 * a pop later, where the pop is not followed by an conditional branch,
1230 * and where the value of the A register is not used later on.
1231 * Look out for the following problems:
1233 * - There may be another PHA/PLA inside the sequence: Restart it.
1234 * - If the PLA has a label, all jumps to this label must be inside
1235 * the sequence, otherwise we cannot remove the PHA/PLA.
1238 while (I < CS_GetEntryCount (S)) {
1240 /* Get next entry */
1241 CodeEntry* E = CS_GetEntry (S, I);
1246 if (E->OPC == OP65_PHA) {
1247 /* Found start of sequence */
1254 if (E->OPC == OP65_PHA) {
1255 /* Inner push/pop, restart */
1257 } else if (E->OPC == OP65_PLA) {
1258 /* Found a matching pop */
1265 /* Next insn, just check if it is no conditional branch and
1266 * that A is not used later. Check also that the range we have
1267 * found now is a basic block, which means that the PHA is the
1268 * only entrance and the PLA the only exit.
1270 if ((E->Info & OF_CBRA) == 0 &&
1272 CS_IsBasicBlock (S, Push, Pop)) {
1273 /* We can remove the PHA and PLA instructions */
1274 CS_DelEntry (S, Pop);
1275 CS_DelEntry (S, Push);
1276 /* Correct I so we continue with the next insn */
1278 /* Remember we had changes */
1281 /* Go into search mode again */
1291 /* Return the number of changes made */
1297 unsigned OptPrecalc (CodeSeg* S)
1298 /* Replace immediate operations with the accu where the current contents are
1299 * known by a load of the final value.
1302 unsigned Changes = 0;
1305 /* Generate register info for this step */
1308 /* Walk over the entries */
1310 while (I < CS_GetEntryCount (S)) {
1312 /* Get next entry */
1313 CodeEntry* E = CS_GetEntry (S, I);
1315 /* Get a pointer to the output registers of the insn */
1316 const RegContents* Out = &E->RI->Out;
1318 /* Handle the different instructions */
1328 if (RegValIsKnown (Out->RegA)) {
1329 /* Accu AND zp with known contents */
1330 const char* Arg = MakeHexArg (Out->RegA);
1331 CodeEntry* X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1332 CS_InsertEntry (S, X, I+1);
1347 /* Free register info */
1350 /* Return the number of changes made */
1356 /*****************************************************************************/
1357 /* Optimize branch types */
1358 /*****************************************************************************/
1362 unsigned OptBranchDist (CodeSeg* S)
1363 /* Change branches for the distance needed. */
1365 unsigned Changes = 0;
1367 /* Walk over the entries */
1369 while (I < CS_GetEntryCount (S)) {
1371 /* Get next entry */
1372 CodeEntry* E = CS_GetEntry (S, I);
1374 /* Check if it's a conditional branch to a local label. */
1375 if (E->Info & OF_CBRA) {
1377 /* Is this a branch to a local symbol? */
1378 if (E->JumpTo != 0) {
1380 /* Check if the branch distance is short */
1381 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1383 /* Make the branch short/long according to distance */
1384 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1385 /* Short branch but long distance */
1386 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1388 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1389 /* Long branch but short distance */
1390 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1394 } else if ((E->Info & OF_LBRA) == 0) {
1396 /* Short branch to external symbol - make it long */
1397 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1402 } else if (CPU == CPU_65C02 &&
1403 (E->Info & OF_UBRA) != 0 &&
1405 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1407 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1408 CE_ReplaceOPC (E, OP65_BRA);
1417 /* Return the number of changes made */