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 */
252 unsigned Changes = 0;
254 /* Walk over all entries minus the last one */
256 while (I < CS_GetEntryCount (S)) {
260 /* Get the next entry */
261 CodeEntry* E = CS_GetEntry (S, I);
263 /* Check if it's an unconditional branch to a local target */
264 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
265 (E->Info & OF_LBRA) != 0 && /* Long branch */
266 E->JumpTo != 0 && /* Local label */
267 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
268 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
274 /* We will create a jump around an RTS instead of the long branch */
275 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
276 CS_InsertEntry (S, X, I+1);
278 /* Get the new branch opcode */
279 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
281 /* Get the label attached to N, create a new one if needed */
282 LN = CS_GenLabel (S, N);
284 /* Generate the branch */
285 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
286 CS_InsertEntry (S, X, I+1);
288 /* Delete the long branch */
291 /* Remember, we had changes */
301 /* Return the number of changes made */
307 /*****************************************************************************/
308 /* Remove dead jumps */
309 /*****************************************************************************/
313 unsigned OptDeadJumps (CodeSeg* S)
314 /* Remove dead jumps (jumps to the next instruction) */
316 unsigned Changes = 0;
318 /* Walk over all entries minus the last one */
320 while (I < CS_GetEntryCount (S)) {
322 /* Get the next entry */
323 CodeEntry* E = CS_GetEntry (S, I);
325 /* Check if it's a branch, if it has a local target, and if the target
326 * is the next instruction.
328 if (E->AM == AM65_BRA &&
330 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
332 /* Delete the dead jump */
335 /* Remember, we had changes */
346 /* Return the number of changes made */
352 /*****************************************************************************/
353 /* Remove dead code */
354 /*****************************************************************************/
358 unsigned OptDeadCode (CodeSeg* S)
359 /* Remove dead code (code that follows an unconditional jump or an rts/rti
363 unsigned Changes = 0;
365 /* Walk over all entries */
367 while (I < CS_GetEntryCount (S)) {
373 CodeEntry* E = CS_GetEntry (S, I);
375 /* Check if it's an unconditional branch, and if the next entry has
376 * no labels attached, or if the label is just used so that the insn
377 * can jump to itself.
379 if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
380 (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
381 (!CE_HasLabel (N) || /* Don't has a label */
382 ((N->Info & OF_UBRA) != 0 && /* Uncond branch */
383 (LN = N->JumpTo) != 0 && /* Jumps to known label */
384 LN->Owner == N && /* Attached to insn */
385 CL_GetRefCount (LN) == 1))) { /* Only reference */
387 /* Delete the next entry */
388 CS_DelEntry (S, I+1);
390 /* Remember, we had changes */
401 /* Return the number of changes made */
407 /*****************************************************************************/
408 /* Optimize jump cascades */
409 /*****************************************************************************/
413 unsigned OptJumpCascades (CodeSeg* S)
414 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
415 * replaced by a jump to the final location. This will in some cases produce
416 * worse code, because some jump targets are no longer reachable by short
417 * branches, but this is quite rare, so there are more advantages than
421 unsigned Changes = 0;
423 /* Walk over all entries */
425 while (I < CS_GetEntryCount (S)) {
431 CodeEntry* E = CS_GetEntry (S, I);
433 /* Check if it's a branch, if it has a jump label, if this jump
434 * label is not attached to the instruction itself, and if the
435 * target instruction is itself a branch.
437 if ((E->Info & OF_BRA) != 0 &&
438 (OldLabel = E->JumpTo) != 0 &&
439 (N = OldLabel->Owner) != E &&
440 (N->Info & OF_BRA) != 0) {
442 /* Check if we can use the final target label. This is the case,
443 * if the target branch is an absolut branch, or if it is a
444 * conditional branch checking the same condition as the first one.
446 if ((N->Info & OF_UBRA) != 0 ||
447 ((E->Info & OF_CBRA) != 0 &&
448 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
450 /* This is a jump cascade and we may jump to the final target,
451 * provided that the other insn does not jump to itself. If
452 * this is the case, we can also jump to ourselves, otherwise
453 * insert a jump to the new instruction and remove the old one.
456 CodeLabel* LN = N->JumpTo;
458 if (LN != 0 && LN->Owner == N) {
460 /* We found a jump to a jump to itself. Replace our jump
461 * by a jump to itself.
463 CodeLabel* LE = CS_GenLabel (S, E);
464 X = NewCodeEntry (E->OPC, E->AM, LE->Name, LE, E->LI);
468 /* Jump to the final jump target */
469 X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
473 /* Insert it behind E */
474 CS_InsertEntry (S, X, I+1);
479 /* Remember, we had changes */
482 /* Check if both are conditional branches, and the condition of
483 * the second is the inverse of that of the first. In this case,
484 * the second branch will never be taken, and we may jump directly
485 * to the instruction behind this one.
487 } else if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
489 CodeEntry* X; /* Instruction behind N */
490 CodeLabel* LX; /* Label attached to X */
492 /* Get the branch conditions of both branches */
493 bc_t BC1 = GetBranchCond (E->OPC);
494 bc_t BC2 = GetBranchCond (N->OPC);
496 /* Check the branch conditions */
497 if (BC1 != GetInverseCond (BC2)) {
498 /* Condition not met */
502 /* We may jump behind this conditional branch. Get the
503 * pointer to the next instruction
505 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
506 /* N is the last entry, bail out */
510 /* Get the label attached to X, create a new one if needed */
511 LX = CS_GenLabel (S, X);
513 /* Move the reference from E to the new label */
514 CS_MoveLabelRef (S, E, LX);
516 /* Remember, we had changes */
527 /* Return the number of changes made */
533 /*****************************************************************************/
534 /* Optimize jsr/rts */
535 /*****************************************************************************/
539 unsigned OptRTS (CodeSeg* S)
540 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
541 * replaced by a jump. Don't bother to delete the RTS if it does not have a
542 * label, the dead code elimination should take care of it.
545 unsigned Changes = 0;
547 /* Walk over all entries minus the last one */
549 while (I < CS_GetEntryCount (S)) {
554 CodeEntry* E = CS_GetEntry (S, I);
556 /* Check if it's a subroutine call and if the following insn is RTS */
557 if (E->OPC == OP65_JSR &&
558 (N = CS_GetNextEntry (S, I)) != 0 &&
559 N->OPC == OP65_RTS) {
561 /* Change the jsr to a jmp and use the additional info for a jump */
563 CE_ReplaceOPC (E, OP65_JMP);
565 /* Remember, we had changes */
575 /* Return the number of changes made */
581 /*****************************************************************************/
582 /* Optimize jump targets */
583 /*****************************************************************************/
587 unsigned OptJumpTarget1 (CodeSeg* S)
588 /* If the instruction preceeding an unconditional branch is the same as the
589 * instruction preceeding the jump target, the jump target may be moved
590 * one entry back. This is a size optimization, since the instruction before
591 * the branch gets removed.
594 unsigned Changes = 0;
595 CodeEntry* E1; /* Entry 1 */
596 CodeEntry* E2; /* Entry 2 */
597 CodeEntry* T1; /* Jump target entry 1 */
598 CodeLabel* TL1; /* Target label 1 */
600 /* Walk over the entries */
602 while (I < CS_GetEntryCount (S)) {
605 E2 = CS_GetNextEntry (S, I);
607 /* Check if we have a jump or branch without a label attached, and
608 * a jump target, which is not attached to the jump itself
611 (E2->Info & OF_UBRA) != 0 &&
614 E2->JumpTo->Owner != E2) {
616 /* Get the entry preceeding the branch target */
617 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
619 /* There is no such entry */
623 /* The entry preceeding the branch target may not be the branch
630 /* Get the entry preceeding the jump */
631 E1 = CS_GetEntry (S, I);
633 /* Check if both preceeding instructions are identical */
634 if (!CodeEntriesAreEqual (E1, T1)) {
635 /* Not equal, try next */
639 /* Get the label for the instruction preceeding the jump target.
640 * This routine will create a new label if the instruction does
641 * not already have one.
643 TL1 = CS_GenLabel (S, T1);
645 /* Change the jump target to point to this new label */
646 CS_MoveLabelRef (S, E2, TL1);
648 /* If the instruction preceeding the jump has labels attached,
649 * move references to this label to the new label.
651 if (CE_HasLabel (E1)) {
652 CS_MoveLabels (S, E1, T1);
655 /* Remove the entry preceeding the jump */
658 /* Remember, we had changes */
668 /* Return the number of changes made */
674 unsigned OptJumpTarget2 (CodeSeg* S)
675 /* If a bcs jumps to a sec insn or a bcc jumps to clc, skip this insn, since
676 * it's job is already done.
679 unsigned Changes = 0;
681 /* Walk over the entries */
683 while (I < CS_GetEntryCount (S)) {
685 /* OP that may be skipped */
688 /* Jump target insn, old and new */
696 CodeEntry* E = CS_GetEntry (S, I);
698 /* Check if this is a bcc insn */
699 if (E->OPC == OP65_BCC || E->OPC == OP65_JCC) {
701 } else if (E->OPC == OP65_BCS || E->OPC == OP65_JCS) {
704 /* Not what we're looking for */
708 /* Must have a jump target */
709 if (E->JumpTo == 0) {
713 /* Get the owner insn of the jump target and check if it's the one, we
714 * will skip if present.
716 T = E->JumpTo->Owner;
721 /* Get the entry following the branch target */
722 N = CS_GetNextEntry (S, CS_GetEntryIndex (S, T));
724 /* There is no such entry */
728 /* Get the label for the instruction following the jump target.
729 * This routine will create a new label if the instruction does
730 * not already have one.
732 L = CS_GenLabel (S, N);
734 /* Change the jump target to point to this new label */
735 CS_MoveLabelRef (S, E, L);
737 /* Remember that we had changes */
745 /* Return the number of changes made */
751 unsigned OptJumpTarget3 (CodeSeg* S)
752 /* Jumps to load instructions of a register, that do already have the matching
753 * register contents may skip the load instruction, since it's job is already
757 unsigned Changes = 0;
760 /* Generate register info for this step */
763 /* Walk over the entries */
765 while (I < CS_GetEntryCount (S)) {
774 CodeEntry* E = CS_GetEntry (S, I);
776 /* Check if this is a load insn with a label and the next insn is not
777 * a conditional branch that needs the flags from the load.
779 if ((E->Info & OF_LOAD) != 0 &&
782 (N = CS_GetNextEntry (S, I)) != 0 &&
783 !CE_UseLoadFlags (N)) {
785 /* Walk over all insn that jump here */
786 for (J = 0; J < CE_GetLabelCount (E); ++J) {
789 CodeLabel* L = CE_GetLabel (E, J);
790 for (K = 0; K < CL_GetRefCount (L); ++K) {
792 /* Get the entry that jumps here */
793 CodeEntry* Jump = CL_GetRef (L, K);
795 /* Get the register info from this insn */
796 short Val = RegVal (E->Chg, &Jump->RI->Out2);
798 /* Check if the outgoing value is the one thatr's loaded */
799 if (Val == (unsigned char) E->Num) {
801 /* Ok, skip the insn. First, generate a label */
803 LN = CS_GenLabel (S, N);
806 /* Change the jump target to point to this new label */
807 CS_MoveLabelRef (S, Jump, LN);
809 /* Remember that we had changes */
821 /* Free register info */
824 /* Return the number of changes made */
830 /*****************************************************************************/
831 /* Optimize conditional branches */
832 /*****************************************************************************/
836 unsigned OptCondBranches1 (CodeSeg* S)
837 /* Performs several optimization steps:
839 * - If an immidiate load of a register is followed by a conditional jump that
840 * is never taken because the load of the register sets the flags in such a
841 * manner, remove the conditional branch.
842 * - If the conditional branch is always taken because of the register load,
843 * replace it by a jmp.
844 * - If a conditional branch jumps around an unconditional branch, remove the
845 * conditional branch and make the jump a conditional branch with the
846 * inverse condition of the first one.
849 unsigned Changes = 0;
851 /* Walk over the entries */
853 while (I < CS_GetEntryCount (S)) {
859 CodeEntry* E = CS_GetEntry (S, I);
861 /* Check if it's a register load */
862 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
863 E->AM == AM65_IMM && /* ..with immidiate addressing */
864 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
865 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
866 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
867 !CE_HasLabel (N)) { /* ..and does not have a label */
869 /* Get the branch condition */
870 bc_t BC = GetBranchCond (N->OPC);
872 /* Check the argument against the branch condition */
873 if ((BC == BC_EQ && E->Num != 0) ||
874 (BC == BC_NE && E->Num == 0) ||
875 (BC == BC_PL && (E->Num & 0x80) != 0) ||
876 (BC == BC_MI && (E->Num & 0x80) == 0)) {
878 /* Remove the conditional branch */
879 CS_DelEntry (S, I+1);
881 /* Remember, we had changes */
884 } else if ((BC == BC_EQ && E->Num == 0) ||
885 (BC == BC_NE && E->Num != 0) ||
886 (BC == BC_PL && (E->Num & 0x80) == 0) ||
887 (BC == BC_MI && (E->Num & 0x80) != 0)) {
889 /* The branch is always taken, replace it by a jump */
890 CE_ReplaceOPC (N, OP65_JMP);
892 /* Remember, we had changes */
898 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
899 (L = E->JumpTo) != 0 && /* ..referencing a local label */
900 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
901 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
902 !CE_HasLabel (N) && /* ..has no label attached */
903 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
905 /* Replace the jump by a conditional branch with the inverse branch
906 * condition than the branch around it.
908 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
910 /* Remove the conditional branch */
913 /* Remember, we had changes */
923 /* Return the number of changes made */
929 unsigned OptCondBranches2 (CodeSeg* S)
930 /* If on entry to a "rol a" instruction the accu is zero, and a beq/bne follows,
931 * we can remove the rol and branch on the state of the carry flag.
934 unsigned Changes = 0;
937 /* Generate register info for this step */
940 /* Walk over the entries */
942 while (I < CS_GetEntryCount (S)) {
947 CodeEntry* E = CS_GetEntry (S, I);
949 /* Check if it's a rol insn with A in accu and a branch follows */
950 if (E->OPC == OP65_ROL &&
952 E->RI->In.RegA == 0 &&
954 (N = CS_GetNextEntry (S, I)) != 0 &&
955 (N->Info & OF_ZBRA) != 0 &&
956 !RegAUsed (S, I+1)) {
958 /* Replace the branch condition */
959 switch (GetBranchCond (N->OPC)) {
960 case BC_EQ: CE_ReplaceOPC (N, OP65_JCC); break;
961 case BC_NE: CE_ReplaceOPC (N, OP65_JCS); break;
962 default: Internal ("Unknown branch condition in OptCondBranches2");
965 /* Delete the rol insn */
968 /* Remember, we had changes */
976 /* Free register info */
979 /* Return the number of changes made */
985 /*****************************************************************************/
986 /* Remove unused loads and stores */
987 /*****************************************************************************/
991 unsigned OptUnusedLoads (CodeSeg* S)
992 /* Remove loads of registers where the value loaded is not used later. */
994 unsigned Changes = 0;
996 /* Walk over the entries */
998 while (I < CS_GetEntryCount (S)) {
1002 /* Get next entry */
1003 CodeEntry* E = CS_GetEntry (S, I);
1005 /* Check if it's a register load or transfer insn */
1006 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
1007 (N = CS_GetNextEntry (S, I)) != 0 &&
1008 !CE_UseLoadFlags (N)) {
1010 /* Check which sort of load or transfer it is */
1017 case OP65_TYA: R = REG_A; break;
1021 case OP65_TAX: R = REG_X; break;
1025 case OP65_TAY: R = REG_Y; break;
1026 default: goto NextEntry; /* OOPS */
1029 /* Get register usage and check if the register value is used later */
1030 if ((GetRegInfo (S, I+1, R) & R) == 0) {
1032 /* Register value is not used, remove the load */
1035 /* Remember, we had changes. Account the deleted entry in I. */
1048 /* Return the number of changes made */
1054 unsigned OptUnusedStores (CodeSeg* S)
1055 /* Remove stores into zero page registers that aren't used later */
1057 unsigned Changes = 0;
1059 /* Walk over the entries */
1061 while (I < CS_GetEntryCount (S)) {
1063 /* Get next entry */
1064 CodeEntry* E = CS_GetEntry (S, I);
1066 /* Check if it's a register load or transfer insn */
1067 if ((E->Info & OF_STORE) != 0 &&
1069 (E->Chg & REG_ZP) != 0) {
1071 /* Check for the zero page location. We know that there cannot be
1072 * more than one zero page location involved in the store.
1074 unsigned R = E->Chg & REG_ZP;
1076 /* Get register usage and check if the register value is used later */
1077 if ((GetRegInfo (S, I+1, R) & R) == 0) {
1079 /* Register value is not used, remove the load */
1082 /* Remember, we had changes */
1085 /* Continue with next insn */
1095 /* Return the number of changes made */
1101 unsigned OptDupLoads (CodeSeg* S)
1102 /* Remove loads of registers where the value loaded is already in the register. */
1104 unsigned Changes = 0;
1107 /* Generate register info for this step */
1110 /* Walk over the entries */
1112 while (I < CS_GetEntryCount (S)) {
1116 /* Get next entry */
1117 CodeEntry* E = CS_GetEntry (S, I);
1119 /* Assume we won't delete the entry */
1122 /* Get a pointer to the input registers of the insn */
1123 const RegContents* In = &E->RI->In;
1125 /* Handle the different instructions */
1129 if (RegValIsKnown (In->RegA) && /* Value of A is known */
1130 CE_IsKnownImm (E, In->RegA) && /* Value to be loaded is known */
1131 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
1132 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
1138 if (RegValIsKnown (In->RegX) && /* Value of X is known */
1139 CE_IsKnownImm (E, In->RegX) && /* Value to be loaded is known */
1140 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
1141 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
1147 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
1148 CE_IsKnownImm (E, In->RegY) && /* Value to be loaded is known */
1149 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
1150 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
1156 /* If we store into a known zero page location, and this
1157 * location does already contain the value to be stored,
1160 if (RegValIsKnown (In->RegA) && /* Value of A is known */
1161 E->AM == AM65_ZP && /* Store into zp */
1162 In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
1169 /* If we store into a known zero page location, and this
1170 * location does already contain the value to be stored,
1173 if (RegValIsKnown (In->RegX) && /* Value of A is known */
1174 E->AM == AM65_ZP && /* Store into zp */
1175 In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
1179 /* If the value in the X register is known and the same as
1180 * that in the A register, replace the store by a STA. The
1181 * optimizer will then remove the load instruction for X
1182 * later. STX does support the zeropage,y addressing mode,
1183 * so be sure to check for that.
1185 } else if (RegValIsKnown (In->RegX) &&
1186 In->RegX == In->RegA &&
1187 E->AM != AM65_ABSY &&
1188 E->AM != AM65_ZPY) {
1189 /* Use the A register instead */
1190 CE_ReplaceOPC (E, OP65_STA);
1195 /* If we store into a known zero page location, and this
1196 * location does already contain the value to be stored,
1199 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
1200 E->AM == AM65_ZP && /* Store into zp */
1201 In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
1205 /* If the value in the Y register is known and the same as
1206 * that in the A register, replace the store by a STA. The
1207 * optimizer will then remove the load instruction for Y
1208 * later. If replacement by A is not possible try a
1209 * replacement by X, but check for invalid addressing modes
1212 } else if (RegValIsKnown (In->RegY)) {
1213 if (In->RegY == In->RegA) {
1214 CE_ReplaceOPC (E, OP65_STA);
1215 } else if (In->RegY == In->RegX &&
1216 E->AM != AM65_ABSX &&
1217 E->AM != AM65_ZPX) {
1218 CE_ReplaceOPC (E, OP65_STX);
1224 /* If we store into a known zero page location, and this
1225 * location does already contain the value to be stored,
1228 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && E->AM == AM65_ZP) {
1229 if (ZPRegVal (E->Chg, In) == 0) {
1236 if (RegValIsKnown (In->RegA) &&
1237 In->RegA == In->RegX &&
1238 (N = CS_GetNextEntry (S, I)) != 0 &&
1239 !CE_UseLoadFlags (N)) {
1240 /* Value is identical and not followed by a branch */
1246 if (RegValIsKnown (In->RegA) &&
1247 In->RegA == In->RegY &&
1248 (N = CS_GetNextEntry (S, I)) != 0 &&
1249 !CE_UseLoadFlags (N)) {
1250 /* Value is identical and not followed by a branch */
1256 if (RegValIsKnown (In->RegX) &&
1257 In->RegX == In->RegA &&
1258 (N = CS_GetNextEntry (S, I)) != 0 &&
1259 !CE_UseLoadFlags (N)) {
1260 /* Value is identical and not followed by a branch */
1266 if (RegValIsKnown (In->RegY) &&
1267 In->RegY == In->RegA &&
1268 (N = CS_GetNextEntry (S, I)) != 0 &&
1269 !CE_UseLoadFlags (N)) {
1270 /* Value is identical and not followed by a branch */
1280 /* Delete the entry if requested */
1283 /* Register value is not used, remove the load */
1286 /* Remember, we had changes */
1298 /* Free register info */
1301 /* Return the number of changes made */
1307 unsigned OptStoreLoad (CodeSeg* S)
1308 /* Remove a store followed by a load from the same location. */
1310 unsigned Changes = 0;
1312 /* Walk over the entries */
1314 while (I < CS_GetEntryCount (S)) {
1319 /* Get next entry */
1320 CodeEntry* E = CS_GetEntry (S, I);
1322 /* Check if it is a store instruction followed by a load from the
1323 * same address which is itself not followed by a conditional branch.
1325 if ((E->Info & OF_STORE) != 0 &&
1326 (N = CS_GetNextEntry (S, I)) != 0 &&
1329 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1330 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1331 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1332 strcmp (E->Arg, N->Arg) == 0 &&
1333 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1334 !CE_UseLoadFlags (X)) {
1336 /* Register has already the correct value, remove the load */
1337 CS_DelEntry (S, I+1);
1339 /* Remember, we had changes */
1349 /* Return the number of changes made */
1355 unsigned OptTransfers1 (CodeSeg* S)
1356 /* Remove transfers from one register to another and back */
1358 unsigned Changes = 0;
1360 /* Walk over the entries */
1362 while (I < CS_GetEntryCount (S)) {
1368 /* Get next entry */
1369 CodeEntry* E = CS_GetEntry (S, I);
1371 /* Check if we have two transfer instructions */
1372 if ((E->Info & OF_XFR) != 0 &&
1373 (N = CS_GetNextEntry (S, I)) != 0 &&
1375 (N->Info & OF_XFR) != 0) {
1377 /* Check if it's a transfer and back */
1378 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1379 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1380 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1381 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1383 /* If the next insn is a conditional branch, check if the insn
1384 * preceeding the first xfr will set the flags right, otherwise we
1385 * may not remove the sequence.
1387 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1390 if (CE_UseLoadFlags (X)) {
1392 /* No preceeding entry */
1395 P = CS_GetEntry (S, I-1);
1396 if ((P->Info & OF_SETF) == 0) {
1397 /* Does not set the flags */
1402 /* Remove both transfers */
1403 CS_DelEntry (S, I+1);
1406 /* Remember, we had changes */
1417 /* Return the number of changes made */
1423 unsigned OptTransfers2 (CodeSeg* S)
1424 /* Replace loads followed by a register transfer by a load with the second
1425 * register if possible.
1428 unsigned Changes = 0;
1430 /* Walk over the entries */
1432 while (I < CS_GetEntryCount (S)) {
1436 /* Get next entry */
1437 CodeEntry* E = CS_GetEntry (S, I);
1439 /* Check if we have a load followed by a transfer where the loaded
1440 * register is not used later.
1442 if ((E->Info & OF_LOAD) != 0 &&
1443 (N = CS_GetNextEntry (S, I)) != 0 &&
1445 (N->Info & OF_XFR) != 0 &&
1446 GetRegInfo (S, I+2, E->Chg) != E->Chg) {
1450 if (E->OPC == OP65_LDA && N->OPC == OP65_TAX) {
1451 /* LDA/TAX - check for the right addressing modes */
1452 if (E->AM == AM65_IMM ||
1454 E->AM == AM65_ABS ||
1455 E->AM == AM65_ABSY) {
1457 X = NewCodeEntry (OP65_LDX, E->AM, E->Arg, 0, N->LI);
1459 } else if (E->OPC == OP65_LDA && N->OPC == OP65_TAY) {
1460 /* LDA/TAY - check for the right addressing modes */
1461 if (E->AM == AM65_IMM ||
1463 E->AM == AM65_ZPX ||
1464 E->AM == AM65_ABS ||
1465 E->AM == AM65_ABSX) {
1467 X = NewCodeEntry (OP65_LDY, E->AM, E->Arg, 0, N->LI);
1469 } else if (E->OPC == OP65_LDY && N->OPC == OP65_TYA) {
1470 /* LDY/TYA. LDA supports all addressing modes LDY does */
1471 X = NewCodeEntry (OP65_LDA, E->AM, E->Arg, 0, N->LI);
1472 } else if (E->OPC == OP65_LDX && N->OPC == OP65_TXA) {
1473 /* LDX/TXA. LDA doesn't support zp,y, so we must map it to
1476 am_t AM = (E->AM == AM65_ZPY)? AM65_ABSY : E->AM;
1477 X = NewCodeEntry (OP65_LDA, AM, E->Arg, 0, N->LI);
1480 /* If we have a load entry, add it and remove the old stuff */
1482 CS_InsertEntry (S, X, I+2);
1483 CS_DelEntries (S, I, 2);
1485 --I; /* Correct for one entry less */
1493 /* Return the number of changes made */
1499 unsigned OptTransfers3 (CodeSeg* S)
1500 /* Replace a register transfer followed by a store of the second register by a
1501 * store of the first register if this is possible.
1504 unsigned Changes = 0;
1505 unsigned UsedRegs = REG_NONE; /* Track used registers */
1506 unsigned Xfer = 0; /* Index of transfer insn */
1507 unsigned Store = 0; /* Index of store insn */
1508 CodeEntry* XferEntry = 0; /* Pointer to xfer insn */
1509 CodeEntry* StoreEntry = 0; /* Pointer to store insn */
1516 } State = Initialize;
1518 /* Walk over the entries. Look for a xfer instruction that is followed by
1519 * a store later, where the value of the register is not used later.
1522 while (I < CS_GetEntryCount (S)) {
1524 /* Get next entry */
1525 CodeEntry* E = CS_GetEntry (S, I);
1530 /* Clear the list of used registers */
1531 UsedRegs = REG_NONE;
1535 if (E->Info & OF_XFR) {
1536 /* Found start of sequence */
1544 /* If we find a conditional jump, abort the sequence, since
1545 * handling them makes things really complicated.
1547 if (E->Info & OF_CBRA) {
1549 /* Switch back to searching */
1553 /* Does this insn use the target register of the transfer? */
1554 } else if ((E->Use & XferEntry->Chg) != 0) {
1556 /* It it's a store instruction, and the block is a basic
1557 * block, proceed. Otherwise restart
1559 if ((E->Info & OF_STORE) != 0 &&
1560 CS_IsBasicBlock (S, Xfer, I)) {
1569 /* Does this insn change the target register of the transfer? */
1570 } else if (E->Chg & XferEntry->Chg) {
1572 /* We *may* add code here to remove the transfer, but I'm
1573 * currently not sure about the consequences, so I won't
1574 * do that and bail out instead.
1579 /* Does this insn have a label? */
1580 } else if (CE_HasLabel (E)) {
1582 /* Too complex to handle - bail out */
1587 /* Track used registers */
1593 /* We are at the instruction behind the store. If the register
1594 * isn't used later, and we have an address mode match, we can
1595 * replace the transfer by a store and remove the store here.
1597 if ((GetRegInfo (S, I, XferEntry->Chg) & XferEntry->Chg) == 0 &&
1598 (StoreEntry->AM == AM65_ABS ||
1599 StoreEntry->AM == AM65_ZP) &&
1600 (StoreEntry->AM != AM65_ZP ||
1601 (StoreEntry->Chg & UsedRegs) == 0) &&
1602 !MemAccess (S, Xfer+1, Store-1, StoreEntry)) {
1604 /* Generate the replacement store insn */
1606 switch (XferEntry->OPC) {
1609 X = NewCodeEntry (OP65_STX,
1617 X = NewCodeEntry (OP65_STA,
1625 X = NewCodeEntry (OP65_STY,
1633 X = NewCodeEntry (OP65_STA,
1644 /* If we have a replacement store, change the code */
1646 /* Insert after the xfer insn */
1647 CS_InsertEntry (S, X, Xfer+1);
1649 /* Remove the xfer instead */
1650 CS_DelEntry (S, Xfer);
1652 /* Remove the final store */
1653 CS_DelEntry (S, Store);
1655 /* Correct I so we continue with the next insn */
1658 /* Remember we had changes */
1661 /* Restart after last xfer insn */
1665 /* Restart after last xfer insn */
1677 /* Return the number of changes made */
1683 unsigned OptTransfers4 (CodeSeg* S)
1684 /* Replace a load of a register followed by a transfer insn of the same register
1685 * by a load of the second register if possible.
1688 unsigned Changes = 0;
1689 unsigned Load = 0; /* Index of load insn */
1690 unsigned Xfer = 0; /* Index of transfer insn */
1691 CodeEntry* LoadEntry = 0; /* Pointer to load insn */
1692 CodeEntry* XferEntry = 0; /* Pointer to xfer insn */
1700 /* Walk over the entries. Look for a load instruction that is followed by
1704 while (I < CS_GetEntryCount (S)) {
1706 /* Get next entry */
1707 CodeEntry* E = CS_GetEntry (S, I);
1712 if (E->Info & OF_LOAD) {
1713 /* Found start of sequence */
1721 /* If we find a conditional jump, abort the sequence, since
1722 * handling them makes things really complicated.
1724 if (E->Info & OF_CBRA) {
1726 /* Switch back to searching */
1730 /* Does this insn use the target register of the load? */
1731 } else if ((E->Use & LoadEntry->Chg) != 0) {
1733 /* It it's a xfer instruction, and the block is a basic
1734 * block, proceed. Otherwise restart
1736 if ((E->Info & OF_XFR) != 0 &&
1737 CS_IsBasicBlock (S, Load, I)) {
1746 /* Does this insn change the target register of the load? */
1747 } else if (E->Chg & LoadEntry->Chg) {
1749 /* We *may* add code here to remove the load, but I'm
1750 * currently not sure about the consequences, so I won't
1751 * do that and bail out instead.
1759 /* We are at the instruction behind the xfer. If the register
1760 * isn't used later, and we have an address mode match, we can
1761 * replace the transfer by a load and remove the initial load.
1763 if ((GetRegInfo (S, I, LoadEntry->Chg) & LoadEntry->Chg) == 0 &&
1764 (LoadEntry->AM == AM65_ABS ||
1765 LoadEntry->AM == AM65_ZP ||
1766 LoadEntry->AM == AM65_IMM) &&
1767 !MemAccess (S, Load+1, Xfer-1, LoadEntry)) {
1769 /* Generate the replacement load insn */
1771 switch (XferEntry->OPC) {
1775 X = NewCodeEntry (OP65_LDA,
1783 X = NewCodeEntry (OP65_LDX,
1791 X = NewCodeEntry (OP65_LDY,
1802 /* If we have a replacement load, change the code */
1804 /* Insert after the xfer insn */
1805 CS_InsertEntry (S, X, Xfer+1);
1807 /* Remove the xfer instead */
1808 CS_DelEntry (S, Xfer);
1810 /* Remove the initial load */
1811 CS_DelEntry (S, Load);
1813 /* Correct I so we continue with the next insn */
1816 /* Remember we had changes */
1819 /* Restart after last xfer insn */
1823 /* Restart after last xfer insn */
1835 /* Return the number of changes made */
1841 unsigned OptPushPop (CodeSeg* S)
1842 /* Remove a PHA/PLA sequence were A is not used later */
1844 unsigned Changes = 0;
1845 unsigned Push = 0; /* Index of push insn */
1846 unsigned Pop = 0; /* Index of pop insn */
1847 unsigned ChgA = 0; /* Flag for A changed */
1852 } State = Searching;
1854 /* Walk over the entries. Look for a push instruction that is followed by
1855 * a pop later, where the pop is not followed by an conditional branch,
1856 * and where the value of the A register is not used later on.
1857 * Look out for the following problems:
1859 * - There may be another PHA/PLA inside the sequence: Restart it.
1860 * - If the PLA has a label, all jumps to this label must be inside
1861 * the sequence, otherwise we cannot remove the PHA/PLA.
1864 while (I < CS_GetEntryCount (S)) {
1868 /* Get next entry */
1869 CodeEntry* E = CS_GetEntry (S, I);
1874 if (E->OPC == OP65_PHA) {
1875 /* Found start of sequence */
1883 if (E->OPC == OP65_PHA) {
1884 /* Inner push/pop, restart */
1887 } else if (E->OPC == OP65_PLA) {
1888 /* Found a matching pop */
1890 /* Check that the block between Push and Pop is a basic
1891 * block (one entry, one exit). Otherwise ignore it.
1893 if (CS_IsBasicBlock (S, Push, Pop)) {
1896 /* Go into searching mode again */
1899 } else if (E->Chg & REG_A) {
1905 /* We're at the instruction after the PLA.
1906 * Check for the following conditions:
1907 * - If this instruction is a store of A, does not have a
1908 * label, and A is not used later, we may replace the PHA
1909 * by the store and remove pla if several other conditions
1911 * - If this instruction is not a conditional branch, and A
1912 * is either unused later, or not changed by the code
1913 * between push and pop, we may remove PHA and PLA.
1915 if (E->OPC == OP65_STA &&
1917 !RegAUsed (S, I+1) &&
1918 !MemAccess (S, Push+1, Pop-1, E)) {
1920 /* Insert a STA after the PHA */
1921 X = NewCodeEntry (E->OPC, E->AM, E->Arg, E->JumpTo, E->LI);
1922 CS_InsertEntry (S, X, Push+1);
1924 /* Remove the PHA instead */
1925 CS_DelEntry (S, Push);
1927 /* Remove the PLA/STA sequence */
1928 CS_DelEntries (S, Pop, 2);
1930 /* Correct I so we continue with the next insn */
1933 /* Remember we had changes */
1936 } else if ((E->Info & OF_CBRA) == 0 &&
1937 (!RegAUsed (S, I) || !ChgA)) {
1939 /* We can remove the PHA and PLA instructions */
1940 CS_DelEntry (S, Pop);
1941 CS_DelEntry (S, Push);
1943 /* Correct I so we continue with the next insn */
1946 /* Remember we had changes */
1950 /* Go into search mode again */
1960 /* Return the number of changes made */
1966 unsigned OptPrecalc (CodeSeg* S)
1967 /* Replace immediate operations with the accu where the current contents are
1968 * known by a load of the final value.
1971 unsigned Changes = 0;
1974 /* Generate register info for this step */
1977 /* Walk over the entries */
1979 while (I < CS_GetEntryCount (S)) {
1981 /* Get next entry */
1982 CodeEntry* E = CS_GetEntry (S, I);
1984 /* Get pointers to the input and output registers of the insn */
1985 const RegContents* Out = &E->RI->Out;
1986 const RegContents* In = &E->RI->In;
1988 /* Argument for LDn and flag */
1989 const char* Arg = 0;
1990 opc_t OPC = OP65_LDA;
1992 /* Handle the different instructions */
1996 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegA)) {
1997 /* Result of load is known */
1998 Arg = MakeHexArg (Out->RegA);
2003 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegX)) {
2004 /* Result of load is known but register is X */
2005 Arg = MakeHexArg (Out->RegX);
2011 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegY)) {
2012 /* Result of load is known but register is Y */
2013 Arg = MakeHexArg (Out->RegY);
2019 if (RegValIsKnown (Out->RegA)) {
2020 /* Accu op zp with known contents */
2021 Arg = MakeHexArg (Out->RegA);
2027 /* If this is an operation with an immediate operand of zero,
2028 * and the register is zero, the operation won't give us any
2029 * results we don't already have (including the flags), so
2030 * remove it. Something like this is generated as a result of
2031 * a compare where parts of the values are known to be zero.
2033 if (In->RegA == 0 && CE_IsKnownImm (E, 0x00)) {
2034 /* 0-0 or 0+0 -> remove */
2041 if (CE_IsKnownImm (E, 0xFF)) {
2042 /* AND with 0xFF, remove */
2045 } else if (CE_IsKnownImm (E, 0x00)) {
2046 /* AND with 0x00, replace by lda #$00 */
2047 Arg = MakeHexArg (0x00);
2048 } else if (RegValIsKnown (Out->RegA)) {
2049 /* Accu AND zp with known contents */
2050 Arg = MakeHexArg (Out->RegA);
2051 } else if (In->RegA == 0xFF) {
2052 /* AND but A contains 0xFF - replace by lda */
2053 CE_ReplaceOPC (E, OP65_LDA);
2059 if (CE_IsKnownImm (E, 0x00)) {
2060 /* ORA with zero, remove */
2063 } else if (CE_IsKnownImm (E, 0xFF)) {
2064 /* ORA with 0xFF, replace by lda #$ff */
2065 Arg = MakeHexArg (0xFF);
2066 } else if (RegValIsKnown (Out->RegA)) {
2067 /* Accu AND zp with known contents */
2068 Arg = MakeHexArg (Out->RegA);
2069 } else if (In->RegA == 0) {
2070 /* ORA but A contains 0x00 - replace by lda */
2071 CE_ReplaceOPC (E, OP65_LDA);
2081 /* Check if we have to replace the insn by LDA */
2083 CodeEntry* X = NewCodeEntry (OPC, AM65_IMM, Arg, 0, E->LI);
2084 CS_InsertEntry (S, X, I+1);
2093 /* Free register info */
2096 /* Return the number of changes made */
2102 /*****************************************************************************/
2103 /* Optimize branch types */
2104 /*****************************************************************************/
2108 unsigned OptBranchDist (CodeSeg* S)
2109 /* Change branches for the distance needed. */
2111 unsigned Changes = 0;
2113 /* Walk over the entries */
2115 while (I < CS_GetEntryCount (S)) {
2117 /* Get next entry */
2118 CodeEntry* E = CS_GetEntry (S, I);
2120 /* Check if it's a conditional branch to a local label. */
2121 if (E->Info & OF_CBRA) {
2123 /* Is this a branch to a local symbol? */
2124 if (E->JumpTo != 0) {
2126 /* Check if the branch distance is short */
2127 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
2129 /* Make the branch short/long according to distance */
2130 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
2131 /* Short branch but long distance */
2132 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
2134 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
2135 /* Long branch but short distance */
2136 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
2140 } else if ((E->Info & OF_LBRA) == 0) {
2142 /* Short branch to external symbol - make it long */
2143 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
2148 } else if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 &&
2149 (E->Info & OF_UBRA) != 0 &&
2151 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
2153 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
2154 CE_ReplaceOPC (E, OP65_BRA);
2163 /* Return the number of changes made */
2169 /*****************************************************************************/
2170 /* Optimize indirect loads */
2171 /*****************************************************************************/
2175 unsigned OptIndLoads1 (CodeSeg* S)
2184 * provided that x and y are both zero.
2187 unsigned Changes = 0;
2190 /* Generate register info for this step */
2193 /* Walk over the entries */
2195 while (I < CS_GetEntryCount (S)) {
2197 /* Get next entry */
2198 CodeEntry* E = CS_GetEntry (S, I);
2200 /* Check if it's what we're looking for */
2201 if (E->OPC == OP65_LDA &&
2202 E->AM == AM65_ZP_INDY &&
2203 E->RI->In.RegY == 0 &&
2204 E->RI->In.RegX == 0) {
2206 /* Replace by the same insn with other addressing mode */
2207 CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZPX_IND, E->Arg, 0, E->LI);
2208 CS_InsertEntry (S, X, I+1);
2210 /* Remove the old insn */
2220 /* Free register info */
2223 /* Return the number of changes made */
2229 unsigned OptIndLoads2 (CodeSeg* S)
2238 * provided that x and y are both zero.
2241 unsigned Changes = 0;
2244 /* Generate register info for this step */
2247 /* Walk over the entries */
2249 while (I < CS_GetEntryCount (S)) {
2251 /* Get next entry */
2252 CodeEntry* E = CS_GetEntry (S, I);
2254 /* Check if it's what we're looking for */
2255 if (E->OPC == OP65_LDA &&
2256 E->AM == AM65_ZPX_IND &&
2257 E->RI->In.RegY == 0 &&
2258 E->RI->In.RegX == 0) {
2260 /* Replace by the same insn with other addressing mode */
2261 CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZP_INDY, E->Arg, 0, E->LI);
2262 CS_InsertEntry (S, X, I+1);
2264 /* Remove the old insn */
2274 /* Free register info */
2277 /* Return the number of changes made */