1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
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 /*****************************************************************************/
48 /*****************************************************************************/
49 /* Helper functions */
50 /*****************************************************************************/
54 static int MemAccess (CodeSeg* S, unsigned From, unsigned To, const CodeEntry* N)
55 /* Checks a range of code entries if there are any memory accesses to N->Arg */
57 /* Get the length of the argument */
58 unsigned NLen = strlen (N->Arg);
60 /* What to check for? */
63 Base = 0x01, /* Check for location without "+1" */
64 Word = 0x02, /* Check for location with "+1" added */
68 /* If the argument of N is a zero page location that ends with "+1", we
69 * must also check for word accesses to the location without +1.
71 if (N->AM == AM65_ZP && NLen > 2 && strcmp (N->Arg + NLen - 2, "+1") == 0) {
75 /* If the argument is zero page indirect, we must also check for accesses
78 if (N->AM == AM65_ZP_INDY || N->AM == AM65_ZPX_IND || N->AM == AM65_ZP_IND) {
82 /* Walk over all code entries */
85 /* Get the next entry */
86 CodeEntry* E = CS_GetEntry (S, From);
88 /* Check if there is an argument and if this argument equals Arg in
91 if (E->Arg[0] != '\0') {
95 if (strcmp (E->Arg, N->Arg) == 0) {
100 ELen = strlen (E->Arg);
101 if ((What & Base) != 0) {
102 if (ELen == NLen - 2 && strncmp (E->Arg, N->Arg, NLen-2) == 0) {
103 /* Found an access */
108 if ((What & Word) != 0) {
109 if (ELen == NLen + 2 && strncmp (E->Arg, N->Arg, NLen) == 0 &&
110 E->Arg[NLen] == '+' && E->Arg[NLen+1] == '1') {
111 /* Found an access */
127 static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
128 /* Get the branch distance between the two entries and return it. The distance
129 * will be negative for backward jumps and positive for forward jumps.
132 /* Get the index of the branch target */
133 unsigned TI = CS_GetEntryIndex (S, To);
135 /* Determine the branch distance */
138 /* Forward branch, do not count the current insn */
141 CodeEntry* N = CS_GetEntry (S, J++);
145 /* Backward branch */
148 CodeEntry* N = CS_GetEntry (S, J++);
153 /* Return the calculated distance */
159 static int IsShortDist (int Distance)
160 /* Return true if the given distance is a short branch distance */
162 return (Distance >= -125 && Distance <= 125);
167 static short ZPRegVal (unsigned short Use, const RegContents* RC)
168 /* Return the contents of the given zeropage register */
170 if ((Use & REG_TMP1) != 0) {
172 } else if ((Use & REG_PTR1_LO) != 0) {
174 } else if ((Use & REG_PTR1_HI) != 0) {
176 } else if ((Use & REG_SREG_LO) != 0) {
178 } else if ((Use & REG_SREG_HI) != 0) {
181 return UNKNOWN_REGVAL;
187 static short RegVal (unsigned short Use, const RegContents* RC)
188 /* Return the contents of the given register */
190 if ((Use & REG_A) != 0) {
192 } else if ((Use & REG_X) != 0) {
194 } else if ((Use & REG_Y) != 0) {
197 return ZPRegVal (Use, RC);
203 /*****************************************************************************/
204 /* Replace jumps to RTS by RTS */
205 /*****************************************************************************/
209 unsigned OptRTSJumps1 (CodeSeg* S)
210 /* Replace jumps to RTS by RTS */
212 unsigned Changes = 0;
214 /* Walk over all entries minus the last one */
216 while (I < CS_GetEntryCount (S)) {
218 /* Get the next entry */
219 CodeEntry* E = CS_GetEntry (S, I);
221 /* Check if it's an unconditional branch to a local target */
222 if ((E->Info & OF_UBRA) != 0 &&
224 E->JumpTo->Owner->OPC == OP65_RTS) {
226 /* Insert an RTS instruction */
227 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
228 CS_InsertEntry (S, X, I+1);
230 /* Delete the jump */
233 /* Remember, we had changes */
243 /* Return the number of changes made */
249 unsigned OptRTSJumps2 (CodeSeg* S)
250 /* Replace long conditional jumps to RTS or to a final target */
252 unsigned Changes = 0;
254 /* Walk over all entries minus the last one */
256 while (I < CS_GetEntryCount (S) - 1) {
258 /* Get the next entry */
259 CodeEntry* E = CS_GetEntry (S, I);
261 /* Check if it's an conditional branch to a local target */
262 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
263 (E->Info & OF_LBRA) != 0 && /* Long branch */
264 E->JumpTo != 0) { /* Local label */
267 /* Get the jump target and the next entry. There's always a next
268 * entry, because we don't cover the last entry in the loop.
271 CodeEntry* T = E->JumpTo->Owner;
272 CodeEntry* N = CS_GetNextEntry (S, I);
274 /* Check if it's a jump to an RTS insn */
275 if (T->OPC == OP65_RTS) {
277 /* It's a jump to RTS. Create a conditional branch around an
280 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, T->LI);
282 } else if (T->OPC == OP65_JMP && T->JumpTo == 0) {
284 /* It's a jump to a label outside the function. Create a
285 * conditional branch around a jump to the external label.
287 X = NewCodeEntry (OP65_JMP, AM65_ABS, T->Arg, T->JumpTo, T->LI);
291 /* If we have a replacement insn, insert it */
297 /* Insert the new insn */
298 CS_InsertEntry (S, X, I+1);
300 /* Create a conditional branch with the inverse condition
301 * around the replacement insn
304 /* Get the new branch opcode */
305 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
307 /* Get the label attached to N, create a new one if needed */
308 LN = CS_GenLabel (S, N);
310 /* Generate the branch */
311 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
312 CS_InsertEntry (S, X, I+1);
314 /* Delete the long branch */
317 /* Remember, we had changes */
328 /* Return the number of changes made */
334 /*****************************************************************************/
335 /* Remove dead jumps */
336 /*****************************************************************************/
340 unsigned OptDeadJumps (CodeSeg* S)
341 /* Remove dead jumps (jumps to the next instruction) */
343 unsigned Changes = 0;
345 /* Walk over all entries minus the last one */
347 while (I < CS_GetEntryCount (S)) {
349 /* Get the next entry */
350 CodeEntry* E = CS_GetEntry (S, I);
352 /* Check if it's a branch, if it has a local target, and if the target
353 * is the next instruction.
355 if (E->AM == AM65_BRA &&
357 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
359 /* Delete the dead jump */
362 /* Remember, we had changes */
373 /* Return the number of changes made */
379 /*****************************************************************************/
380 /* Remove dead code */
381 /*****************************************************************************/
385 unsigned OptDeadCode (CodeSeg* S)
386 /* Remove dead code (code that follows an unconditional jump or an rts/rti
390 unsigned Changes = 0;
392 /* Walk over all entries */
394 while (I < CS_GetEntryCount (S)) {
400 CodeEntry* E = CS_GetEntry (S, I);
402 /* Check if it's an unconditional branch, and if the next entry has
403 * no labels attached, or if the label is just used so that the insn
404 * can jump to itself.
406 if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
407 (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
408 (!CE_HasLabel (N) || /* Don't has a label */
409 ((N->Info & OF_UBRA) != 0 && /* Uncond branch */
410 (LN = N->JumpTo) != 0 && /* Jumps to known label */
411 LN->Owner == N && /* Attached to insn */
412 CL_GetRefCount (LN) == 1))) { /* Only reference */
414 /* Delete the next entry */
415 CS_DelEntry (S, I+1);
417 /* Remember, we had changes */
428 /* Return the number of changes made */
434 /*****************************************************************************/
435 /* Optimize jump cascades */
436 /*****************************************************************************/
440 unsigned OptJumpCascades (CodeSeg* S)
441 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
442 * replaced by a jump to the final location. This will in some cases produce
443 * worse code, because some jump targets are no longer reachable by short
444 * branches, but this is quite rare, so there are more advantages than
448 unsigned Changes = 0;
450 /* Walk over all entries */
452 while (I < CS_GetEntryCount (S)) {
458 CodeEntry* E = CS_GetEntry (S, I);
461 * - if it's a branch,
462 * - if it has a jump label,
463 * - if this jump label is not attached to the instruction itself,
464 * - if the target instruction is itself a branch,
465 * - if either the first branch is unconditional or the target of
466 * the second branch is internal to the function.
467 * The latter condition will avoid conditional branches to targets
468 * outside of the function (usually incspx), which won't simplify the
469 * code, since conditional far branches are emulated by a short branch
472 if ((E->Info & OF_BRA) != 0 &&
473 (OldLabel = E->JumpTo) != 0 &&
474 (N = OldLabel->Owner) != E &&
475 (N->Info & OF_BRA) != 0 &&
476 ((E->Info & OF_CBRA) == 0 ||
479 /* Check if we can use the final target label. This is the case,
480 * if the target branch is an absolut branch, or if it is a
481 * conditional branch checking the same condition as the first one.
483 if ((N->Info & OF_UBRA) != 0 ||
484 ((E->Info & OF_CBRA) != 0 &&
485 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
487 /* This is a jump cascade and we may jump to the final target,
488 * provided that the other insn does not jump to itself. If
489 * this is the case, we can also jump to ourselves, otherwise
490 * insert a jump to the new instruction and remove the old one.
493 CodeLabel* LN = N->JumpTo;
495 if (LN != 0 && LN->Owner == N) {
497 /* We found a jump to a jump to itself. Replace our jump
498 * by a jump to itself.
500 CodeLabel* LE = CS_GenLabel (S, E);
501 X = NewCodeEntry (E->OPC, E->AM, LE->Name, LE, E->LI);
505 /* Jump to the final jump target */
506 X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
510 /* Insert it behind E */
511 CS_InsertEntry (S, X, I+1);
516 /* Remember, we had changes */
519 /* Check if both are conditional branches, and the condition of
520 * the second is the inverse of that of the first. In this case,
521 * the second branch will never be taken, and we may jump directly
522 * to the instruction behind this one.
524 } else if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
526 CodeEntry* X; /* Instruction behind N */
527 CodeLabel* LX; /* Label attached to X */
529 /* Get the branch conditions of both branches */
530 bc_t BC1 = GetBranchCond (E->OPC);
531 bc_t BC2 = GetBranchCond (N->OPC);
533 /* Check the branch conditions */
534 if (BC1 != GetInverseCond (BC2)) {
535 /* Condition not met */
539 /* We may jump behind this conditional branch. Get the
540 * pointer to the next instruction
542 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
543 /* N is the last entry, bail out */
547 /* Get the label attached to X, create a new one if needed */
548 LX = CS_GenLabel (S, X);
550 /* Move the reference from E to the new label */
551 CS_MoveLabelRef (S, E, LX);
553 /* Remember, we had changes */
564 /* Return the number of changes made */
570 /*****************************************************************************/
571 /* Optimize jsr/rts */
572 /*****************************************************************************/
576 unsigned OptRTS (CodeSeg* S)
577 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
578 * replaced by a jump. Don't bother to delete the RTS if it does not have a
579 * label, the dead code elimination should take care of it.
582 unsigned Changes = 0;
584 /* Walk over all entries minus the last one */
586 while (I < CS_GetEntryCount (S)) {
591 CodeEntry* E = CS_GetEntry (S, I);
593 /* Check if it's a subroutine call and if the following insn is RTS */
594 if (E->OPC == OP65_JSR &&
595 (N = CS_GetNextEntry (S, I)) != 0 &&
596 N->OPC == OP65_RTS) {
598 /* Change the jsr to a jmp and use the additional info for a jump */
600 CE_ReplaceOPC (E, OP65_JMP);
602 /* Remember, we had changes */
612 /* Return the number of changes made */
618 /*****************************************************************************/
619 /* Optimize jump targets */
620 /*****************************************************************************/
624 unsigned OptJumpTarget1 (CodeSeg* S)
625 /* If the instruction preceeding an unconditional branch is the same as the
626 * instruction preceeding the jump target, the jump target may be moved
627 * one entry back. This is a size optimization, since the instruction before
628 * the branch gets removed.
631 unsigned Changes = 0;
632 CodeEntry* E1; /* Entry 1 */
633 CodeEntry* E2; /* Entry 2 */
634 CodeEntry* T1; /* Jump target entry 1 */
635 CodeLabel* TL1; /* Target label 1 */
637 /* Walk over the entries */
639 while (I < CS_GetEntryCount (S)) {
642 E2 = CS_GetNextEntry (S, I);
644 /* Check if we have a jump or branch without a label attached, and
645 * a jump target, which is not attached to the jump itself
648 (E2->Info & OF_UBRA) != 0 &&
651 E2->JumpTo->Owner != E2) {
653 /* Get the entry preceeding the branch target */
654 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
656 /* There is no such entry */
660 /* The entry preceeding the branch target may not be the branch
667 /* Get the entry preceeding the jump */
668 E1 = CS_GetEntry (S, I);
670 /* Check if both preceeding instructions are identical */
671 if (!CodeEntriesAreEqual (E1, T1)) {
672 /* Not equal, try next */
676 /* Get the label for the instruction preceeding the jump target.
677 * This routine will create a new label if the instruction does
678 * not already have one.
680 TL1 = CS_GenLabel (S, T1);
682 /* Change the jump target to point to this new label */
683 CS_MoveLabelRef (S, E2, TL1);
685 /* If the instruction preceeding the jump has labels attached,
686 * move references to this label to the new label.
688 if (CE_HasLabel (E1)) {
689 CS_MoveLabels (S, E1, T1);
692 /* Remove the entry preceeding the jump */
695 /* Remember, we had changes */
705 /* Return the number of changes made */
711 unsigned OptJumpTarget2 (CodeSeg* S)
712 /* If a bcs jumps to a sec insn or a bcc jumps to clc, skip this insn, since
713 * it's job is already done.
716 unsigned Changes = 0;
718 /* Walk over the entries */
720 while (I < CS_GetEntryCount (S)) {
722 /* OP that may be skipped */
725 /* Jump target insn, old and new */
733 CodeEntry* E = CS_GetEntry (S, I);
735 /* Check if this is a bcc insn */
736 if (E->OPC == OP65_BCC || E->OPC == OP65_JCC) {
738 } else if (E->OPC == OP65_BCS || E->OPC == OP65_JCS) {
741 /* Not what we're looking for */
745 /* Must have a jump target */
746 if (E->JumpTo == 0) {
750 /* Get the owner insn of the jump target and check if it's the one, we
751 * will skip if present.
753 T = E->JumpTo->Owner;
758 /* Get the entry following the branch target */
759 N = CS_GetNextEntry (S, CS_GetEntryIndex (S, T));
761 /* There is no such entry */
765 /* Get the label for the instruction following the jump target.
766 * This routine will create a new label if the instruction does
767 * not already have one.
769 L = CS_GenLabel (S, N);
771 /* Change the jump target to point to this new label */
772 CS_MoveLabelRef (S, E, L);
774 /* Remember that we had changes */
782 /* Return the number of changes made */
788 unsigned OptJumpTarget3 (CodeSeg* S)
789 /* Jumps to load instructions of a register, that do already have the matching
790 * register contents may skip the load instruction, since it's job is already
794 unsigned Changes = 0;
797 /* Walk over the entries */
799 while (I < CS_GetEntryCount (S)) {
804 CodeEntry* E = CS_GetEntry (S, I);
806 /* Check if this is a load insn with a label and the next insn is not
807 * a conditional branch that needs the flags from the load.
809 if ((E->Info & OF_LOAD) != 0 &&
812 (N = CS_GetNextEntry (S, I)) != 0 &&
813 !CE_UseLoadFlags (N)) {
821 /* Walk over all insn that jump here */
822 for (J = 0; J < CE_GetLabelCount (E); ++J) {
825 CodeLabel* L = CE_GetLabel (E, J);
827 /* Loop over all insn that reference this label. Since we may
828 * eventually remove a reference in the loop, we must loop
829 * from end down to start.
831 for (K = CL_GetRefCount (L) - 1; K >= 0; --K) {
833 /* Get the entry that jumps here */
834 CodeEntry* Jump = CL_GetRef (L, K);
836 /* Get the register info from this insn */
837 short Val = RegVal (E->Chg, &Jump->RI->Out2);
839 /* Check if the outgoing value is the one thats's loaded */
840 if (Val == (unsigned char) E->Num) {
842 /* Ok, skip the insn. First, generate a label for the
846 LN = CS_GenLabel (S, N);
849 /* Change the jump target to point to this new label */
850 CS_MoveLabelRef (S, Jump, LN);
852 /* Remember that we had changes */
864 /* Return the number of changes made */
870 /*****************************************************************************/
871 /* Optimize conditional branches */
872 /*****************************************************************************/
876 unsigned OptCondBranches1 (CodeSeg* S)
877 /* Performs several optimization steps:
879 * - If an immidiate load of a register is followed by a conditional jump that
880 * is never taken because the load of the register sets the flags in such a
881 * manner, remove the conditional branch.
882 * - If the conditional branch is always taken because of the register load,
883 * replace it by a jmp.
884 * - If a conditional branch jumps around an unconditional branch, remove the
885 * conditional branch and make the jump a conditional branch with the
886 * inverse condition of the first one.
889 unsigned Changes = 0;
891 /* Walk over the entries */
893 while (I < CS_GetEntryCount (S)) {
899 CodeEntry* E = CS_GetEntry (S, I);
901 /* Check if it's a register load */
902 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
903 E->AM == AM65_IMM && /* ..with immidiate addressing */
904 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
905 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
906 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
907 !CE_HasLabel (N)) { /* ..and does not have a label */
909 /* Get the branch condition */
910 bc_t BC = GetBranchCond (N->OPC);
912 /* Check the argument against the branch condition */
913 if ((BC == BC_EQ && E->Num != 0) ||
914 (BC == BC_NE && E->Num == 0) ||
915 (BC == BC_PL && (E->Num & 0x80) != 0) ||
916 (BC == BC_MI && (E->Num & 0x80) == 0)) {
918 /* Remove the conditional branch */
919 CS_DelEntry (S, I+1);
921 /* Remember, we had changes */
924 } else if ((BC == BC_EQ && E->Num == 0) ||
925 (BC == BC_NE && E->Num != 0) ||
926 (BC == BC_PL && (E->Num & 0x80) == 0) ||
927 (BC == BC_MI && (E->Num & 0x80) != 0)) {
929 /* The branch is always taken, replace it by a jump */
930 CE_ReplaceOPC (N, OP65_JMP);
932 /* Remember, we had changes */
938 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
939 (L = E->JumpTo) != 0 && /* ..referencing a local label */
940 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
941 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
942 !CE_HasLabel (N) && /* ..has no label attached */
943 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
945 /* Replace the jump by a conditional branch with the inverse branch
946 * condition than the branch around it.
948 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
950 /* Remove the conditional branch */
953 /* Remember, we had changes */
963 /* Return the number of changes made */
969 unsigned OptCondBranches2 (CodeSeg* S)
970 /* If on entry to a "rol a" instruction the accu is zero, and a beq/bne follows,
971 * we can remove the rol and branch on the state of the carry flag.
974 unsigned Changes = 0;
977 /* Walk over the entries */
979 while (I < CS_GetEntryCount (S)) {
984 CodeEntry* E = CS_GetEntry (S, I);
986 /* Check if it's a rol insn with A in accu and a branch follows */
987 if (E->OPC == OP65_ROL &&
989 E->RI->In.RegA == 0 &&
991 (N = CS_GetNextEntry (S, I)) != 0 &&
992 (N->Info & OF_ZBRA) != 0 &&
993 !RegAUsed (S, I+1)) {
995 /* Replace the branch condition */
996 switch (GetBranchCond (N->OPC)) {
997 case BC_EQ: CE_ReplaceOPC (N, OP65_JCC); break;
998 case BC_NE: CE_ReplaceOPC (N, OP65_JCS); break;
999 default: Internal ("Unknown branch condition in OptCondBranches2");
1002 /* Delete the rol insn */
1005 /* Remember, we had changes */
1013 /* Return the number of changes made */
1019 /*****************************************************************************/
1020 /* Remove unused loads and stores */
1021 /*****************************************************************************/
1025 unsigned OptUnusedLoads (CodeSeg* S)
1026 /* Remove loads of registers where the value loaded is not used later. */
1028 unsigned Changes = 0;
1030 /* Walk over the entries */
1032 while (I < CS_GetEntryCount (S)) {
1036 /* Get next entry */
1037 CodeEntry* E = CS_GetEntry (S, I);
1039 /* Check if it's a register load or transfer insn */
1040 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
1041 (N = CS_GetNextEntry (S, I)) != 0 &&
1042 !CE_UseLoadFlags (N)) {
1044 /* Check which sort of load or transfer it is */
1051 case OP65_TYA: R = REG_A; break;
1055 case OP65_TAX: R = REG_X; break;
1059 case OP65_TAY: R = REG_Y; break;
1060 default: goto NextEntry; /* OOPS */
1063 /* Get register usage and check if the register value is used later */
1064 if ((GetRegInfo (S, I+1, R) & R) == 0) {
1066 /* Register value is not used, remove the load */
1069 /* Remember, we had changes. Account the deleted entry in I. */
1082 /* Return the number of changes made */
1088 unsigned OptUnusedStores (CodeSeg* S)
1089 /* Remove stores into zero page registers that aren't used later */
1091 unsigned Changes = 0;
1093 /* Walk over the entries */
1095 while (I < CS_GetEntryCount (S)) {
1097 /* Get next entry */
1098 CodeEntry* E = CS_GetEntry (S, I);
1100 /* Check if it's a register load or transfer insn */
1101 if ((E->Info & OF_STORE) != 0 &&
1103 (E->Chg & REG_ZP) != 0) {
1105 /* Check for the zero page location. We know that there cannot be
1106 * more than one zero page location involved in the store.
1108 unsigned R = E->Chg & REG_ZP;
1110 /* Get register usage and check if the register value is used later */
1111 if ((GetRegInfo (S, I+1, R) & R) == 0) {
1113 /* Register value is not used, remove the load */
1116 /* Remember, we had changes */
1119 /* Continue with next insn */
1129 /* Return the number of changes made */
1135 unsigned OptDupLoads (CodeSeg* S)
1136 /* Remove loads of registers where the value loaded is already in the register. */
1138 unsigned Changes = 0;
1141 /* Walk over the entries */
1143 while (I < CS_GetEntryCount (S)) {
1147 /* Get next entry */
1148 CodeEntry* E = CS_GetEntry (S, I);
1150 /* Assume we won't delete the entry */
1153 /* Get a pointer to the input registers of the insn */
1154 const RegContents* In = &E->RI->In;
1156 /* Handle the different instructions */
1160 if (RegValIsKnown (In->RegA) && /* Value of A is known */
1161 CE_IsKnownImm (E, In->RegA) && /* Value to be loaded is known */
1162 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
1163 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
1169 if (RegValIsKnown (In->RegX) && /* Value of X is known */
1170 CE_IsKnownImm (E, In->RegX) && /* Value to be loaded is known */
1171 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
1172 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
1178 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
1179 CE_IsKnownImm (E, In->RegY) && /* Value to be loaded is known */
1180 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
1181 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
1187 /* If we store into a known zero page location, and this
1188 * location does already contain the value to be stored,
1191 if (RegValIsKnown (In->RegA) && /* Value of A is known */
1192 E->AM == AM65_ZP && /* Store into zp */
1193 In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
1200 /* If we store into a known zero page location, and this
1201 * location does already contain the value to be stored,
1204 if (RegValIsKnown (In->RegX) && /* Value of A is known */
1205 E->AM == AM65_ZP && /* Store into zp */
1206 In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
1210 /* If the value in the X register is known and the same as
1211 * that in the A register, replace the store by a STA. The
1212 * optimizer will then remove the load instruction for X
1213 * later. STX does support the zeropage,y addressing mode,
1214 * so be sure to check for that.
1216 } else if (RegValIsKnown (In->RegX) &&
1217 In->RegX == In->RegA &&
1218 E->AM != AM65_ABSY &&
1219 E->AM != AM65_ZPY) {
1220 /* Use the A register instead */
1221 CE_ReplaceOPC (E, OP65_STA);
1226 /* If we store into a known zero page location, and this
1227 * location does already contain the value to be stored,
1230 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
1231 E->AM == AM65_ZP && /* Store into zp */
1232 In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
1236 /* If the value in the Y register is known and the same as
1237 * that in the A register, replace the store by a STA. The
1238 * optimizer will then remove the load instruction for Y
1239 * later. If replacement by A is not possible try a
1240 * replacement by X, but check for invalid addressing modes
1243 } else if (RegValIsKnown (In->RegY)) {
1244 if (In->RegY == In->RegA) {
1245 CE_ReplaceOPC (E, OP65_STA);
1246 } else if (In->RegY == In->RegX &&
1247 E->AM != AM65_ABSX &&
1248 E->AM != AM65_ZPX) {
1249 CE_ReplaceOPC (E, OP65_STX);
1255 /* If we store into a known zero page location, and this
1256 * location does already contain the value to be stored,
1259 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && E->AM == AM65_ZP) {
1260 if (ZPRegVal (E->Chg, In) == 0) {
1267 if (RegValIsKnown (In->RegA) &&
1268 In->RegA == In->RegX &&
1269 (N = CS_GetNextEntry (S, I)) != 0 &&
1270 !CE_UseLoadFlags (N)) {
1271 /* Value is identical and not followed by a branch */
1277 if (RegValIsKnown (In->RegA) &&
1278 In->RegA == In->RegY &&
1279 (N = CS_GetNextEntry (S, I)) != 0 &&
1280 !CE_UseLoadFlags (N)) {
1281 /* Value is identical and not followed by a branch */
1287 if (RegValIsKnown (In->RegX) &&
1288 In->RegX == In->RegA &&
1289 (N = CS_GetNextEntry (S, I)) != 0 &&
1290 !CE_UseLoadFlags (N)) {
1291 /* Value is identical and not followed by a branch */
1297 if (RegValIsKnown (In->RegY) &&
1298 In->RegY == In->RegA &&
1299 (N = CS_GetNextEntry (S, I)) != 0 &&
1300 !CE_UseLoadFlags (N)) {
1301 /* Value is identical and not followed by a branch */
1311 /* Delete the entry if requested */
1314 /* Register value is not used, remove the load */
1317 /* Remember, we had changes */
1329 /* Return the number of changes made */
1335 unsigned OptStoreLoad (CodeSeg* S)
1336 /* Remove a store followed by a load from the same location. */
1338 unsigned Changes = 0;
1340 /* Walk over the entries */
1342 while (I < CS_GetEntryCount (S)) {
1347 /* Get next entry */
1348 CodeEntry* E = CS_GetEntry (S, I);
1350 /* Check if it is a store instruction followed by a load from the
1351 * same address which is itself not followed by a conditional branch.
1353 if ((E->Info & OF_STORE) != 0 &&
1354 (N = CS_GetNextEntry (S, I)) != 0 &&
1357 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1358 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1359 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1360 strcmp (E->Arg, N->Arg) == 0 &&
1361 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1362 !CE_UseLoadFlags (X)) {
1364 /* Register has already the correct value, remove the load */
1365 CS_DelEntry (S, I+1);
1367 /* Remember, we had changes */
1377 /* Return the number of changes made */
1383 unsigned OptTransfers1 (CodeSeg* S)
1384 /* Remove transfers from one register to another and back */
1386 unsigned Changes = 0;
1388 /* Walk over the entries */
1390 while (I < CS_GetEntryCount (S)) {
1396 /* Get next entry */
1397 CodeEntry* E = CS_GetEntry (S, I);
1399 /* Check if we have two transfer instructions */
1400 if ((E->Info & OF_XFR) != 0 &&
1401 (N = CS_GetNextEntry (S, I)) != 0 &&
1403 (N->Info & OF_XFR) != 0) {
1405 /* Check if it's a transfer and back */
1406 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1407 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1408 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1409 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1411 /* If the next insn is a conditional branch, check if the insn
1412 * preceeding the first xfr will set the flags right, otherwise we
1413 * may not remove the sequence.
1415 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1418 if (CE_UseLoadFlags (X)) {
1420 /* No preceeding entry */
1423 P = CS_GetEntry (S, I-1);
1424 if ((P->Info & OF_SETF) == 0) {
1425 /* Does not set the flags */
1430 /* Remove both transfers */
1431 CS_DelEntry (S, I+1);
1434 /* Remember, we had changes */
1445 /* Return the number of changes made */
1451 unsigned OptTransfers2 (CodeSeg* S)
1452 /* Replace loads followed by a register transfer by a load with the second
1453 * register if possible.
1456 unsigned Changes = 0;
1458 /* Walk over the entries */
1460 while (I < CS_GetEntryCount (S)) {
1464 /* Get next entry */
1465 CodeEntry* E = CS_GetEntry (S, I);
1467 /* Check if we have a load followed by a transfer where the loaded
1468 * register is not used later.
1470 if ((E->Info & OF_LOAD) != 0 &&
1471 (N = CS_GetNextEntry (S, I)) != 0 &&
1473 (N->Info & OF_XFR) != 0 &&
1474 GetRegInfo (S, I+2, E->Chg) != E->Chg) {
1478 if (E->OPC == OP65_LDA && N->OPC == OP65_TAX) {
1479 /* LDA/TAX - check for the right addressing modes */
1480 if (E->AM == AM65_IMM ||
1482 E->AM == AM65_ABS ||
1483 E->AM == AM65_ABSY) {
1485 X = NewCodeEntry (OP65_LDX, E->AM, E->Arg, 0, N->LI);
1487 } else if (E->OPC == OP65_LDA && N->OPC == OP65_TAY) {
1488 /* LDA/TAY - check for the right addressing modes */
1489 if (E->AM == AM65_IMM ||
1491 E->AM == AM65_ZPX ||
1492 E->AM == AM65_ABS ||
1493 E->AM == AM65_ABSX) {
1495 X = NewCodeEntry (OP65_LDY, E->AM, E->Arg, 0, N->LI);
1497 } else if (E->OPC == OP65_LDY && N->OPC == OP65_TYA) {
1498 /* LDY/TYA. LDA supports all addressing modes LDY does */
1499 X = NewCodeEntry (OP65_LDA, E->AM, E->Arg, 0, N->LI);
1500 } else if (E->OPC == OP65_LDX && N->OPC == OP65_TXA) {
1501 /* LDX/TXA. LDA doesn't support zp,y, so we must map it to
1504 am_t AM = (E->AM == AM65_ZPY)? AM65_ABSY : E->AM;
1505 X = NewCodeEntry (OP65_LDA, AM, E->Arg, 0, N->LI);
1508 /* If we have a load entry, add it and remove the old stuff */
1510 CS_InsertEntry (S, X, I+2);
1511 CS_DelEntries (S, I, 2);
1513 --I; /* Correct for one entry less */
1521 /* Return the number of changes made */
1527 unsigned OptTransfers3 (CodeSeg* S)
1528 /* Replace a register transfer followed by a store of the second register by a
1529 * store of the first register if this is possible.
1532 unsigned Changes = 0;
1533 unsigned UsedRegs = REG_NONE; /* Track used registers */
1534 unsigned Xfer = 0; /* Index of transfer insn */
1535 unsigned Store = 0; /* Index of store insn */
1536 CodeEntry* XferEntry = 0; /* Pointer to xfer insn */
1537 CodeEntry* StoreEntry = 0; /* Pointer to store insn */
1544 } State = Initialize;
1546 /* Walk over the entries. Look for a xfer instruction that is followed by
1547 * a store later, where the value of the register is not used later.
1550 while (I < CS_GetEntryCount (S)) {
1552 /* Get next entry */
1553 CodeEntry* E = CS_GetEntry (S, I);
1558 /* Clear the list of used registers */
1559 UsedRegs = REG_NONE;
1563 if (E->Info & OF_XFR) {
1564 /* Found start of sequence */
1572 /* If we find a conditional jump, abort the sequence, since
1573 * handling them makes things really complicated.
1575 if (E->Info & OF_CBRA) {
1577 /* Switch back to searching */
1581 /* Does this insn use the target register of the transfer? */
1582 } else if ((E->Use & XferEntry->Chg) != 0) {
1584 /* It it's a store instruction, and the block is a basic
1585 * block, proceed. Otherwise restart
1587 if ((E->Info & OF_STORE) != 0 &&
1588 CS_IsBasicBlock (S, Xfer, I)) {
1597 /* Does this insn change the target register of the transfer? */
1598 } else if (E->Chg & XferEntry->Chg) {
1600 /* We *may* add code here to remove the transfer, but I'm
1601 * currently not sure about the consequences, so I won't
1602 * do that and bail out instead.
1607 /* Does this insn have a label? */
1608 } else if (CE_HasLabel (E)) {
1610 /* Too complex to handle - bail out */
1615 /* Track used registers */
1621 /* We are at the instruction behind the store. If the register
1622 * isn't used later, and we have an address mode match, we can
1623 * replace the transfer by a store and remove the store here.
1625 if ((GetRegInfo (S, I, XferEntry->Chg) & XferEntry->Chg) == 0 &&
1626 (StoreEntry->AM == AM65_ABS ||
1627 StoreEntry->AM == AM65_ZP) &&
1628 (StoreEntry->AM != AM65_ZP ||
1629 (StoreEntry->Chg & UsedRegs) == 0) &&
1630 !MemAccess (S, Xfer+1, Store-1, StoreEntry)) {
1632 /* Generate the replacement store insn */
1634 switch (XferEntry->OPC) {
1637 X = NewCodeEntry (OP65_STX,
1645 X = NewCodeEntry (OP65_STA,
1653 X = NewCodeEntry (OP65_STY,
1661 X = NewCodeEntry (OP65_STA,
1672 /* If we have a replacement store, change the code */
1674 /* Insert after the xfer insn */
1675 CS_InsertEntry (S, X, Xfer+1);
1677 /* Remove the xfer instead */
1678 CS_DelEntry (S, Xfer);
1680 /* Remove the final store */
1681 CS_DelEntry (S, Store);
1683 /* Correct I so we continue with the next insn */
1686 /* Remember we had changes */
1689 /* Restart after last xfer insn */
1693 /* Restart after last xfer insn */
1705 /* Return the number of changes made */
1711 unsigned OptTransfers4 (CodeSeg* S)
1712 /* Replace a load of a register followed by a transfer insn of the same register
1713 * by a load of the second register if possible.
1716 unsigned Changes = 0;
1717 unsigned Load = 0; /* Index of load insn */
1718 unsigned Xfer = 0; /* Index of transfer insn */
1719 CodeEntry* LoadEntry = 0; /* Pointer to load insn */
1720 CodeEntry* XferEntry = 0; /* Pointer to xfer insn */
1728 /* Walk over the entries. Look for a load instruction that is followed by
1732 while (I < CS_GetEntryCount (S)) {
1734 /* Get next entry */
1735 CodeEntry* E = CS_GetEntry (S, I);
1740 if (E->Info & OF_LOAD) {
1741 /* Found start of sequence */
1749 /* If we find a conditional jump, abort the sequence, since
1750 * handling them makes things really complicated.
1752 if (E->Info & OF_CBRA) {
1754 /* Switch back to searching */
1758 /* Does this insn use the target register of the load? */
1759 } else if ((E->Use & LoadEntry->Chg) != 0) {
1761 /* It it's a xfer instruction, and the block is a basic
1762 * block, proceed. Otherwise restart
1764 if ((E->Info & OF_XFR) != 0 &&
1765 CS_IsBasicBlock (S, Load, I)) {
1774 /* Does this insn change the target register of the load? */
1775 } else if (E->Chg & LoadEntry->Chg) {
1777 /* We *may* add code here to remove the load, but I'm
1778 * currently not sure about the consequences, so I won't
1779 * do that and bail out instead.
1787 /* We are at the instruction behind the xfer. If the register
1788 * isn't used later, and we have an address mode match, we can
1789 * replace the transfer by a load and remove the initial load.
1791 if ((GetRegInfo (S, I, LoadEntry->Chg) & LoadEntry->Chg) == 0 &&
1792 (LoadEntry->AM == AM65_ABS ||
1793 LoadEntry->AM == AM65_ZP ||
1794 LoadEntry->AM == AM65_IMM) &&
1795 !MemAccess (S, Load+1, Xfer-1, LoadEntry)) {
1797 /* Generate the replacement load insn */
1799 switch (XferEntry->OPC) {
1803 X = NewCodeEntry (OP65_LDA,
1811 X = NewCodeEntry (OP65_LDX,
1819 X = NewCodeEntry (OP65_LDY,
1830 /* If we have a replacement load, change the code */
1832 /* Insert after the xfer insn */
1833 CS_InsertEntry (S, X, Xfer+1);
1835 /* Remove the xfer instead */
1836 CS_DelEntry (S, Xfer);
1838 /* Remove the initial load */
1839 CS_DelEntry (S, Load);
1841 /* Correct I so we continue with the next insn */
1844 /* Remember we had changes */
1847 /* Restart after last xfer insn */
1851 /* Restart after last xfer insn */
1863 /* Return the number of changes made */
1869 unsigned OptPushPop (CodeSeg* S)
1870 /* Remove a PHA/PLA sequence were A is not used later */
1872 unsigned Changes = 0;
1873 unsigned Push = 0; /* Index of push insn */
1874 unsigned Pop = 0; /* Index of pop insn */
1875 unsigned ChgA = 0; /* Flag for A changed */
1880 } State = Searching;
1882 /* Walk over the entries. Look for a push instruction that is followed by
1883 * a pop later, where the pop is not followed by an conditional branch,
1884 * and where the value of the A register is not used later on.
1885 * Look out for the following problems:
1887 * - There may be another PHA/PLA inside the sequence: Restart it.
1888 * - If the PLA has a label, all jumps to this label must be inside
1889 * the sequence, otherwise we cannot remove the PHA/PLA.
1892 while (I < CS_GetEntryCount (S)) {
1896 /* Get next entry */
1897 CodeEntry* E = CS_GetEntry (S, I);
1902 if (E->OPC == OP65_PHA) {
1903 /* Found start of sequence */
1911 if (E->OPC == OP65_PHA) {
1912 /* Inner push/pop, restart */
1915 } else if (E->OPC == OP65_PLA) {
1916 /* Found a matching pop */
1918 /* Check that the block between Push and Pop is a basic
1919 * block (one entry, one exit). Otherwise ignore it.
1921 if (CS_IsBasicBlock (S, Push, Pop)) {
1924 /* Go into searching mode again */
1927 } else if (E->Chg & REG_A) {
1933 /* We're at the instruction after the PLA.
1934 * Check for the following conditions:
1935 * - If this instruction is a store of A that doesn't use
1936 * another register, if the instruction does not have a
1937 * label, and A is not used later, we may replace the PHA
1938 * by the store and remove pla if several other conditions
1940 * - If this instruction is not a conditional branch, and A
1941 * is either unused later, or not changed by the code
1942 * between push and pop, we may remove PHA and PLA.
1944 if (E->OPC == OP65_STA &&
1945 (E->AM == AM65_ABS || E->AM == AM65_ZP) &&
1947 !RegAUsed (S, I+1) &&
1948 !MemAccess (S, Push+1, Pop-1, E)) {
1950 /* Insert a STA after the PHA */
1951 X = NewCodeEntry (E->OPC, E->AM, E->Arg, E->JumpTo, E->LI);
1952 CS_InsertEntry (S, X, Push+1);
1954 /* Remove the PHA instead */
1955 CS_DelEntry (S, Push);
1957 /* Remove the PLA/STA sequence */
1958 CS_DelEntries (S, Pop, 2);
1960 /* Correct I so we continue with the next insn */
1963 /* Remember we had changes */
1966 } else if ((E->Info & OF_CBRA) == 0 &&
1967 (!RegAUsed (S, I) || !ChgA)) {
1969 /* We can remove the PHA and PLA instructions */
1970 CS_DelEntry (S, Pop);
1971 CS_DelEntry (S, Push);
1973 /* Correct I so we continue with the next insn */
1976 /* Remember we had changes */
1980 /* Go into search mode again */
1990 /* Return the number of changes made */
1996 unsigned OptPrecalc (CodeSeg* S)
1997 /* Replace immediate operations with the accu where the current contents are
1998 * known by a load of the final value.
2001 unsigned Changes = 0;
2004 /* Walk over the entries */
2006 while (I < CS_GetEntryCount (S)) {
2008 /* Get next entry */
2009 CodeEntry* E = CS_GetEntry (S, I);
2011 /* Get pointers to the input and output registers of the insn */
2012 const RegContents* Out = &E->RI->Out;
2013 const RegContents* In = &E->RI->In;
2015 /* Argument for LDn and flag */
2016 const char* Arg = 0;
2017 opc_t OPC = OP65_LDA;
2019 /* Handle the different instructions */
2023 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegA)) {
2024 /* Result of load is known */
2025 Arg = MakeHexArg (Out->RegA);
2030 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegX)) {
2031 /* Result of load is known but register is X */
2032 Arg = MakeHexArg (Out->RegX);
2038 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegY)) {
2039 /* Result of load is known but register is Y */
2040 Arg = MakeHexArg (Out->RegY);
2046 if (RegValIsKnown (Out->RegA)) {
2047 /* Accu op zp with known contents */
2048 Arg = MakeHexArg (Out->RegA);
2054 /* If this is an operation with an immediate operand of zero,
2055 * and the register is zero, the operation won't give us any
2056 * results we don't already have (including the flags), so
2057 * remove it. Something like this is generated as a result of
2058 * a compare where parts of the values are known to be zero.
2060 if (In->RegA == 0 && CE_IsKnownImm (E, 0x00)) {
2061 /* 0-0 or 0+0 -> remove */
2068 if (CE_IsKnownImm (E, 0xFF)) {
2069 /* AND with 0xFF, remove */
2072 } else if (CE_IsKnownImm (E, 0x00)) {
2073 /* AND with 0x00, replace by lda #$00 */
2074 Arg = MakeHexArg (0x00);
2075 } else if (RegValIsKnown (Out->RegA)) {
2076 /* Accu AND zp with known contents */
2077 Arg = MakeHexArg (Out->RegA);
2078 } else if (In->RegA == 0xFF) {
2079 /* AND but A contains 0xFF - replace by lda */
2080 CE_ReplaceOPC (E, OP65_LDA);
2086 if (CE_IsKnownImm (E, 0x00)) {
2087 /* ORA with zero, remove */
2090 } else if (CE_IsKnownImm (E, 0xFF)) {
2091 /* ORA with 0xFF, replace by lda #$ff */
2092 Arg = MakeHexArg (0xFF);
2093 } else if (RegValIsKnown (Out->RegA)) {
2094 /* Accu AND zp with known contents */
2095 Arg = MakeHexArg (Out->RegA);
2096 } else if (In->RegA == 0) {
2097 /* ORA but A contains 0x00 - replace by lda */
2098 CE_ReplaceOPC (E, OP65_LDA);
2108 /* Check if we have to replace the insn by LDA */
2110 CodeEntry* X = NewCodeEntry (OPC, AM65_IMM, Arg, 0, E->LI);
2111 CS_InsertEntry (S, X, I+1);
2120 /* Return the number of changes made */
2126 /*****************************************************************************/
2127 /* Optimize branch types */
2128 /*****************************************************************************/
2132 unsigned OptBranchDist (CodeSeg* S)
2133 /* Change branches for the distance needed. */
2135 unsigned Changes = 0;
2137 /* Walk over the entries */
2139 while (I < CS_GetEntryCount (S)) {
2141 /* Get next entry */
2142 CodeEntry* E = CS_GetEntry (S, I);
2144 /* Check if it's a conditional branch to a local label. */
2145 if (E->Info & OF_CBRA) {
2147 /* Is this a branch to a local symbol? */
2148 if (E->JumpTo != 0) {
2150 /* Check if the branch distance is short */
2151 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
2153 /* Make the branch short/long according to distance */
2154 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
2155 /* Short branch but long distance */
2156 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
2158 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
2159 /* Long branch but short distance */
2160 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
2164 } else if ((E->Info & OF_LBRA) == 0) {
2166 /* Short branch to external symbol - make it long */
2167 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
2172 } else if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 &&
2173 (E->Info & OF_UBRA) != 0 &&
2175 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
2177 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
2178 CE_ReplaceOPC (E, OP65_BRA);
2187 /* Return the number of changes made */
2193 /*****************************************************************************/
2194 /* Optimize indirect loads */
2195 /*****************************************************************************/
2199 unsigned OptIndLoads1 (CodeSeg* S)
2208 * provided that x and y are both zero.
2211 unsigned Changes = 0;
2214 /* Walk over the entries */
2216 while (I < CS_GetEntryCount (S)) {
2218 /* Get next entry */
2219 CodeEntry* E = CS_GetEntry (S, I);
2221 /* Check if it's what we're looking for */
2222 if (E->OPC == OP65_LDA &&
2223 E->AM == AM65_ZP_INDY &&
2224 E->RI->In.RegY == 0 &&
2225 E->RI->In.RegX == 0) {
2227 /* Replace by the same insn with other addressing mode */
2228 CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZPX_IND, E->Arg, 0, E->LI);
2229 CS_InsertEntry (S, X, I+1);
2231 /* Remove the old insn */
2241 /* Return the number of changes made */
2247 unsigned OptIndLoads2 (CodeSeg* S)
2256 * provided that x and y are both zero.
2259 unsigned Changes = 0;
2262 /* Walk over the entries */
2264 while (I < CS_GetEntryCount (S)) {
2266 /* Get next entry */
2267 CodeEntry* E = CS_GetEntry (S, I);
2269 /* Check if it's what we're looking for */
2270 if (E->OPC == OP65_LDA &&
2271 E->AM == AM65_ZPX_IND &&
2272 E->RI->In.RegY == 0 &&
2273 E->RI->In.RegX == 0) {
2275 /* Replace by the same insn with other addressing mode */
2276 CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZP_INDY, E->Arg, 0, E->LI);
2277 CS_InsertEntry (S, X, I+1);
2279 /* Remove the old insn */
2289 /* Return the number of changes made */