1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 RegVal (unsigned short Use, const RegContents* RC)
95 /* Return the contents of the given register */
97 if ((Use & REG_A) != 0) {
99 } else if ((Use & REG_X) != 0) {
101 } else if ((Use & REG_Y) != 0) {
103 } else if ((Use & REG_TMP1) != 0) {
105 } else if ((Use & REG_SREG_LO) != 0) {
107 } else if ((Use & REG_SREG_HI) != 0) {
110 return UNKNOWN_REGVAL;
116 /*****************************************************************************/
117 /* Replace jumps to RTS by RTS */
118 /*****************************************************************************/
122 unsigned OptRTSJumps1 (CodeSeg* S)
123 /* Replace jumps to RTS by RTS */
125 unsigned Changes = 0;
127 /* Walk over all entries minus the last one */
129 while (I < CS_GetEntryCount (S)) {
131 /* Get the next entry */
132 CodeEntry* E = CS_GetEntry (S, I);
134 /* Check if it's an unconditional branch to a local target */
135 if ((E->Info & OF_UBRA) != 0 &&
137 E->JumpTo->Owner->OPC == OP65_RTS) {
139 /* Insert an RTS instruction */
140 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
141 CS_InsertEntry (S, X, I+1);
143 /* Delete the jump */
146 /* Remember, we had changes */
156 /* Return the number of changes made */
162 unsigned OptRTSJumps2 (CodeSeg* S)
163 /* Replace long conditional jumps to RTS */
165 unsigned Changes = 0;
167 /* Walk over all entries minus the last one */
169 while (I < CS_GetEntryCount (S)) {
173 /* Get the next entry */
174 CodeEntry* E = CS_GetEntry (S, I);
176 /* Check if it's an unconditional branch to a local target */
177 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
178 (E->Info & OF_LBRA) != 0 && /* Long branch */
179 E->JumpTo != 0 && /* Local label */
180 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
181 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
187 /* We will create a jump around an RTS instead of the long branch */
188 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
189 CS_InsertEntry (S, X, I+1);
191 /* Get the new branch opcode */
192 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
194 /* Get the label attached to N, create a new one if needed */
195 LN = CS_GenLabel (S, N);
197 /* Generate the branch */
198 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
199 CS_InsertEntry (S, X, I+1);
201 /* Delete the long branch */
204 /* Remember, we had changes */
214 /* Return the number of changes made */
220 /*****************************************************************************/
221 /* Remove dead jumps */
222 /*****************************************************************************/
226 unsigned OptDeadJumps (CodeSeg* S)
227 /* Remove dead jumps (jumps to the next instruction) */
229 unsigned Changes = 0;
231 /* Walk over all entries minus the last one */
233 while (I < CS_GetEntryCount (S)) {
235 /* Get the next entry */
236 CodeEntry* E = CS_GetEntry (S, I);
238 /* Check if it's a branch, if it has a local target, and if the target
239 * is the next instruction.
241 if (E->AM == AM65_BRA &&
243 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
245 /* Delete the dead jump */
248 /* Remember, we had changes */
259 /* Return the number of changes made */
265 /*****************************************************************************/
266 /* Remove dead code */
267 /*****************************************************************************/
271 unsigned OptDeadCode (CodeSeg* S)
272 /* Remove dead code (code that follows an unconditional jump or an rts/rti
276 unsigned Changes = 0;
278 /* Walk over all entries */
280 while (I < CS_GetEntryCount (S)) {
286 CodeEntry* E = CS_GetEntry (S, I);
288 /* Check if it's an unconditional branch, and if the next entry has
289 * no labels attached, or if the label is just used so that the insn
290 * can jump to itself.
292 if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
293 (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
294 (!CE_HasLabel (N) || /* Don't has a label */
295 ((N->Info & OF_UBRA) != 0 && /* Uncond branch */
296 (LN = N->JumpTo) != 0 && /* Jumps to known label */
297 LN->Owner == N && /* Attached to insn */
298 CL_GetRefCount (LN) == 1))) { /* Only reference */
300 /* Delete the next entry */
301 CS_DelEntry (S, I+1);
303 /* Remember, we had changes */
314 /* Return the number of changes made */
320 /*****************************************************************************/
321 /* Optimize jump cascades */
322 /*****************************************************************************/
326 unsigned OptJumpCascades (CodeSeg* S)
327 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
328 * replaced by a jump to the final location. This will in some cases produce
329 * worse code, because some jump targets are no longer reachable by short
330 * branches, but this is quite rare, so there are more advantages than
334 unsigned Changes = 0;
336 /* Walk over all entries */
338 while (I < CS_GetEntryCount (S)) {
344 CodeEntry* E = CS_GetEntry (S, I);
346 /* Check if it's a branch, if it has a jump label, if this jump
347 * label is not attached to the instruction itself, and if the
348 * target instruction is itself a branch.
350 if ((E->Info & OF_BRA) != 0 &&
351 (OldLabel = E->JumpTo) != 0 &&
352 (N = OldLabel->Owner) != E &&
353 (N->Info & OF_BRA) != 0) {
355 /* Check if we can use the final target label. This is the case,
356 * if the target branch is an absolut branch, or if it is a
357 * conditional branch checking the same condition as the first one.
359 if ((N->Info & OF_UBRA) != 0 ||
360 ((E->Info & OF_CBRA) != 0 &&
361 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
363 /* This is a jump cascade and we may jump to the final target,
364 * provided that the other insn does not jump to itself. If
365 * this is the case, we can also jump to ourselves, otherwise
366 * insert a jump to the new instruction and remove the old one.
369 CodeLabel* LN = N->JumpTo;
371 if (LN != 0 && LN->Owner == N) {
373 /* We found a jump to a jump to itself. Replace our jump
374 * by a jump to itself.
376 CodeLabel* LE = CS_GenLabel (S, E);
377 X = NewCodeEntry (E->OPC, E->AM, LE->Name, LE, E->LI);
381 /* Jump to the final jump target */
382 X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
386 /* Insert it behind E */
387 CS_InsertEntry (S, X, I+1);
392 /* Remember, we had changes */
400 /* Check if both are conditional branches, and the condition of
401 * the second is the inverse of that of the first. In this case,
402 * the second branch will never be taken, and we may jump directly
403 * to the instruction behind this one.
405 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
407 CodeEntry* X; /* Instruction behind N */
408 CodeLabel* LX; /* Label attached to X */
410 /* Get the branch conditions of both branches */
411 bc_t BC1 = GetBranchCond (E->OPC);
412 bc_t BC2 = GetBranchCond (N->OPC);
414 /* Check the branch conditions */
415 if (BC1 != GetInverseCond (BC2)) {
416 /* Condition not met */
420 /* We may jump behind this conditional branch. Get the
421 * pointer to the next instruction
423 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
424 /* N is the last entry, bail out */
428 /* Get the label attached to X, create a new one if needed */
429 LX = CS_GenLabel (S, X);
431 /* Move the reference from E to the new label */
432 CS_MoveLabelRef (S, E, LX);
434 /* Remember, we had changes */
449 /* Return the number of changes made */
455 /*****************************************************************************/
456 /* Optimize jsr/rts */
457 /*****************************************************************************/
461 unsigned OptRTS (CodeSeg* S)
462 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
463 * replaced by a jump. Don't bother to delete the RTS if it does not have a
464 * label, the dead code elimination should take care of it.
467 unsigned Changes = 0;
469 /* Walk over all entries minus the last one */
471 while (I < CS_GetEntryCount (S)) {
476 CodeEntry* E = CS_GetEntry (S, I);
478 /* Check if it's a subroutine call and if the following insn is RTS */
479 if (E->OPC == OP65_JSR &&
480 (N = CS_GetNextEntry (S, I)) != 0 &&
481 N->OPC == OP65_RTS) {
483 /* Change the jsr to a jmp and use the additional info for a jump */
485 CE_ReplaceOPC (E, OP65_JMP);
487 /* Remember, we had changes */
497 /* Return the number of changes made */
503 /*****************************************************************************/
504 /* Optimize jump targets */
505 /*****************************************************************************/
509 unsigned OptJumpTarget (CodeSeg* S)
510 /* If the instruction preceeding an unconditional branch is the same as the
511 * instruction preceeding the jump target, the jump target may be moved
512 * one entry back. This is a size optimization, since the instruction before
513 * the branch gets removed.
516 unsigned Changes = 0;
517 CodeEntry* E1; /* Entry 1 */
518 CodeEntry* E2; /* Entry 2 */
519 CodeEntry* T1; /* Jump target entry 1 */
520 CodeLabel* TL1; /* Target label 1 */
522 /* Walk over the entries */
524 while (I < CS_GetEntryCount (S)) {
527 E2 = CS_GetNextEntry (S, I);
529 /* Check if we have a jump or branch, and a matching label, which
530 * is not attached to the jump itself
533 (E2->Info & OF_UBRA) != 0 &&
535 E2->JumpTo->Owner != E2) {
537 /* Get the entry preceeding the branch target */
538 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
540 /* There is no such entry */
544 /* Get the entry preceeding the jump */
545 E1 = CS_GetEntry (S, I);
547 /* Check if both preceeding instructions are identical */
548 if (!CodeEntriesAreEqual (E1, T1)) {
549 /* Not equal, try next */
553 /* Get the label for the instruction preceeding the jump target.
554 * This routine will create a new label if the instruction does
555 * not already have one.
557 TL1 = CS_GenLabel (S, T1);
559 /* Change the jump target to point to this new label */
560 CS_MoveLabelRef (S, E2, TL1);
562 /* If the instruction preceeding the jump has labels attached,
563 * move references to this label to the new label.
565 if (CE_HasLabel (E1)) {
566 CS_MoveLabels (S, E1, T1);
569 /* Remove the entry preceeding the jump */
572 /* Remember, we had changes */
583 /* Return the number of changes made */
589 /*****************************************************************************/
590 /* Optimize conditional branches */
591 /*****************************************************************************/
595 unsigned OptCondBranches (CodeSeg* S)
596 /* Performs several optimization steps:
598 * - If an immidiate load of a register is followed by a conditional jump that
599 * is never taken because the load of the register sets the flags in such a
600 * manner, remove the conditional branch.
601 * - If the conditional branch is always taken because of the register load,
602 * replace it by a jmp.
603 * - If a conditional branch jumps around an unconditional branch, remove the
604 * conditional branch and make the jump a conditional branch with the
605 * inverse condition of the first one.
608 unsigned Changes = 0;
610 /* Walk over the entries */
612 while (I < CS_GetEntryCount (S)) {
618 CodeEntry* E = CS_GetEntry (S, I);
620 /* Check if it's a register load */
621 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
622 E->AM == AM65_IMM && /* ..with immidiate addressing */
623 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
624 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
625 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
626 !CE_HasLabel (N)) { /* ..and does not have a label */
628 /* Get the branch condition */
629 bc_t BC = GetBranchCond (N->OPC);
631 /* Check the argument against the branch condition */
632 if ((BC == BC_EQ && E->Num != 0) ||
633 (BC == BC_NE && E->Num == 0) ||
634 (BC == BC_PL && (E->Num & 0x80) != 0) ||
635 (BC == BC_MI && (E->Num & 0x80) == 0)) {
637 /* Remove the conditional branch */
638 CS_DelEntry (S, I+1);
640 /* Remember, we had changes */
643 } else if ((BC == BC_EQ && E->Num == 0) ||
644 (BC == BC_NE && E->Num != 0) ||
645 (BC == BC_PL && (E->Num & 0x80) == 0) ||
646 (BC == BC_MI && (E->Num & 0x80) != 0)) {
648 /* The branch is always taken, replace it by a jump */
649 CE_ReplaceOPC (N, OP65_JMP);
651 /* Remember, we had changes */
657 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
658 (L = E->JumpTo) != 0 && /* ..referencing a local label */
659 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
660 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
661 !CE_HasLabel (N) && /* ..has no label attached */
662 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
664 /* Replace the jump by a conditional branch with the inverse branch
665 * condition than the branch around it.
667 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
669 /* Remove the conditional branch */
672 /* Remember, we had changes */
682 /* Return the number of changes made */
688 /*****************************************************************************/
689 /* Remove unused loads and stores */
690 /*****************************************************************************/
694 unsigned OptUnusedLoads (CodeSeg* S)
695 /* Remove loads of registers where the value loaded is not used later. */
697 unsigned Changes = 0;
699 /* Walk over the entries */
701 while (I < CS_GetEntryCount (S)) {
706 CodeEntry* E = CS_GetEntry (S, I);
708 /* Check if it's a register load or transfer insn */
709 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
710 (N = CS_GetNextEntry (S, I)) != 0 &&
711 !CE_UseLoadFlags (N)) {
713 /* Check which sort of load or transfer it is */
720 case OP65_TYA: R = REG_A; break;
724 case OP65_TAX: R = REG_X; break;
728 case OP65_TAY: R = REG_Y; break;
729 default: goto NextEntry; /* OOPS */
732 /* Get register usage and check if the register value is used later */
733 if ((GetRegInfo (S, I+1, R) & R) == 0) {
735 /* Register value is not used, remove the load */
738 /* Remember, we had changes */
750 /* Return the number of changes made */
756 unsigned OptUnusedStores (CodeSeg* S)
757 /* Remove stores into zero page registers that aren't used later */
759 unsigned Changes = 0;
761 /* Walk over the entries */
763 while (I < CS_GetEntryCount (S)) {
766 CodeEntry* E = CS_GetEntry (S, I);
768 /* Check if it's a register load or transfer insn */
769 if ((E->Info & OF_STORE) != 0 &&
771 (E->Chg & REG_ZP) != 0) {
773 /* Check for the zero page location. We know that there cannot be
774 * more than one zero page location involved in the store.
776 unsigned R = E->Chg & REG_ZP;
778 /* Get register usage and check if the register value is used later */
779 if ((GetRegInfo (S, I+1, R) & R) == 0) {
781 /* Register value is not used, remove the load */
784 /* Remember, we had changes */
795 /* Return the number of changes made */
801 unsigned OptDupLoads (CodeSeg* S)
802 /* Remove loads of registers where the value loaded is already in the register. */
804 unsigned Changes = 0;
807 /* Generate register info for this step */
810 /* Walk over the entries */
812 while (I < CS_GetEntryCount (S)) {
817 CodeEntry* E = CS_GetEntry (S, I);
819 /* Assume we won't delete the entry */
822 /* Get a pointer to the input registers of the insn */
823 const RegContents* In = &E->RI->In;
825 /* Handle the different instructions */
829 if (RegValIsKnown (In->RegA) && /* Value of A is known */
830 CE_KnownImm (E) && /* Value to be loaded is known */
831 In->RegA == (long) E->Num && /* Both are equal */
832 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
833 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
839 if (RegValIsKnown (In->RegX) && /* Value of X is known */
840 CE_KnownImm (E) && /* Value to be loaded is known */
841 In->RegX == (long) E->Num && /* Both are equal */
842 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
843 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
849 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
850 CE_KnownImm (E) && /* Value to be loaded is known */
851 In->RegY == (long) E->Num && /* Both are equal */
852 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
853 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
859 /* If we store into a known zero page location, and this
860 * location does already contain the value to be stored,
863 if (RegValIsKnown (In->RegA) && /* Value of A is known */
864 E->AM == AM65_ZP && /* Store into zp */
865 In->RegA == RegVal (E->Chg, In)) { /* Value identical */
872 /* If we store into a known zero page location, and this
873 * location does already contain the value to be stored,
876 if (RegValIsKnown (In->RegX) && /* Value of A is known */
877 E->AM == AM65_ZP && /* Store into zp */
878 In->RegX == RegVal (E->Chg, In)) { /* Value identical */
882 /* If the value in the X register is known and the same as
883 * that in the A register, replace the store by a STA. The
884 * optimizer will then remove the load instruction for X
885 * later. STX does support the zeropage,y addressing mode,
886 * so be sure to check for that.
888 } else if (RegValIsKnown (In->RegX) &&
889 In->RegX == In->RegA &&
890 E->AM != AM65_ABSY &&
892 /* Use the A register instead */
893 CE_ReplaceOPC (E, OP65_STA);
898 /* If we store into a known zero page location, and this
899 * location does already contain the value to be stored,
902 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
903 E->AM == AM65_ZP && /* Store into zp */
904 In->RegY == RegVal (E->Chg, In)) { /* Value identical */
908 /* If the value in the Y register is known and the same as
909 * that in the A register, replace the store by a STA. The
910 * optimizer will then remove the load instruction for Y
911 * later. If replacement by A is not possible try a
912 * replacement by X, but check for invalid addressing modes
915 } else if (RegValIsKnown (In->RegY)) {
916 if (In->RegY == In->RegA) {
917 CE_ReplaceOPC (E, OP65_STA);
918 } else if (In->RegY == In->RegX &&
919 E->AM != AM65_ABSX &&
921 CE_ReplaceOPC (E, OP65_STX);
927 /* If we store into a known zero page location, and this
928 * location does already contain the value to be stored,
931 if (CPU >= CPU_65C02 && E->AM == AM65_ZP) {
932 if (RegVal (E->Chg, In) == 0) {
939 if (RegValIsKnown (In->RegA) &&
940 In->RegA == In->RegX &&
941 (N = CS_GetNextEntry (S, I)) != 0 &&
942 !CE_UseLoadFlags (N)) {
943 /* Value is identical and not followed by a branch */
949 if (RegValIsKnown (In->RegA) &&
950 In->RegA == In->RegY &&
951 (N = CS_GetNextEntry (S, I)) != 0 &&
952 !CE_UseLoadFlags (N)) {
953 /* Value is identical and not followed by a branch */
959 if (RegValIsKnown (In->RegX) &&
960 In->RegX == In->RegA &&
961 (N = CS_GetNextEntry (S, I)) != 0 &&
962 !CE_UseLoadFlags (N)) {
963 /* Value is identical and not followed by a branch */
969 if (RegValIsKnown (In->RegY) &&
970 In->RegY == In->RegA &&
971 (N = CS_GetNextEntry (S, I)) != 0 &&
972 !CE_UseLoadFlags (N)) {
973 /* Value is identical and not followed by a branch */
983 /* Delete the entry if requested */
986 /* Register value is not used, remove the load */
989 /* Remember, we had changes */
1001 /* Free register info */
1004 /* Return the number of changes made */
1010 unsigned OptStoreLoad (CodeSeg* S)
1011 /* Remove a store followed by a load from the same location. */
1013 unsigned Changes = 0;
1015 /* Walk over the entries */
1017 while (I < CS_GetEntryCount (S)) {
1022 /* Get next entry */
1023 CodeEntry* E = CS_GetEntry (S, I);
1025 /* Check if it is a store instruction followed by a load from the
1026 * same address which is itself not followed by a conditional branch.
1028 if ((E->Info & OF_STORE) != 0 &&
1029 (N = CS_GetNextEntry (S, I)) != 0 &&
1032 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1033 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1034 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1035 strcmp (E->Arg, N->Arg) == 0 &&
1036 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1037 !CE_UseLoadFlags (X)) {
1039 /* Register has already the correct value, remove the load */
1040 CS_DelEntry (S, I+1);
1042 /* Remember, we had changes */
1052 /* Return the number of changes made */
1058 unsigned OptTransfers (CodeSeg* S)
1059 /* Remove transfers from one register to another and back */
1061 unsigned Changes = 0;
1063 /* Walk over the entries */
1065 while (I < CS_GetEntryCount (S)) {
1071 /* Get next entry */
1072 CodeEntry* E = CS_GetEntry (S, I);
1074 /* Check if it is a store instruction followed by a load from the
1075 * same address which is itself not followed by a conditional branch.
1077 if ((E->Info & OF_XFR) != 0 &&
1078 (N = CS_GetNextEntry (S, I)) != 0 &&
1080 (N->Info & OF_XFR) != 0) {
1082 /* Check if it's a transfer and back */
1083 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1084 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1085 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1086 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1088 /* If the next insn is a conditional branch, check if the insn
1089 * preceeding the first xfr will set the flags right, otherwise we
1090 * may not remove the sequence.
1092 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1095 if (CE_UseLoadFlags (X)) {
1097 /* No preceeding entry */
1100 P = CS_GetEntry (S, I-1);
1101 if ((P->Info & OF_SETF) == 0) {
1102 /* Does not set the flags */
1107 /* Remove both transfers */
1108 CS_DelEntry (S, I+1);
1111 /* Remember, we had changes */
1122 /* Return the number of changes made */
1128 unsigned OptPushPop (CodeSeg* S)
1129 /* Remove a PHA/PLA sequence were A is not used later */
1131 unsigned Changes = 0;
1132 unsigned Push = 0; /* Index of push insn */
1133 unsigned Pop = 0; /* Index of pop insn */
1138 } State = Searching;
1140 /* Walk over the entries. Look for a push instruction that is followed by
1141 * a pop later, where the pop is not followed by an conditional branch,
1142 * and where the value of the A register is not used later on.
1143 * Look out for the following problems:
1145 * - There may be another PHA/PLA inside the sequence: Restart it.
1146 * - If the PLA has a label, all jumps to this label must be inside
1147 * the sequence, otherwise we cannot remove the PHA/PLA.
1150 while (I < CS_GetEntryCount (S)) {
1152 /* Get next entry */
1153 CodeEntry* E = CS_GetEntry (S, I);
1158 if (E->OPC == OP65_PHA) {
1159 /* Found start of sequence */
1166 if (E->OPC == OP65_PHA) {
1167 /* Inner push/pop, restart */
1169 } else if (E->OPC == OP65_PLA) {
1170 /* Found a matching pop */
1177 /* Next insn, just check if it is no conditional branch and
1178 * that A is not used later. Check also that the range we have
1179 * found now is a basic block, which means that the PHA is the
1180 * only entrance and the PLA the only exit.
1182 if ((E->Info & OF_CBRA) == 0 &&
1184 CS_IsBasicBlock (S, Push, Pop)) {
1185 /* We can remove the PHA and PLA instructions */
1186 CS_DelEntry (S, Pop);
1187 CS_DelEntry (S, Push);
1188 /* Correct I so we continue with the next insn */
1190 /* Remember we had changes */
1193 /* Go into search mode again */
1203 /* Return the number of changes made */
1209 /*****************************************************************************/
1210 /* Optimize branch types */
1211 /*****************************************************************************/
1215 unsigned OptBranchDist (CodeSeg* S)
1216 /* Change branches for the distance needed. */
1218 unsigned Changes = 0;
1220 /* Walk over the entries */
1222 while (I < CS_GetEntryCount (S)) {
1224 /* Get next entry */
1225 CodeEntry* E = CS_GetEntry (S, I);
1227 /* Check if it's a conditional branch to a local label. */
1228 if (E->Info & OF_CBRA) {
1230 /* Is this a branch to a local symbol? */
1231 if (E->JumpTo != 0) {
1233 /* Check if the branch distance is short */
1234 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1236 /* Make the branch short/long according to distance */
1237 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1238 /* Short branch but long distance */
1239 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1241 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1242 /* Long branch but short distance */
1243 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1247 } else if ((E->Info & OF_LBRA) == 0) {
1249 /* Short branch to external symbol - make it long */
1250 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1255 } else if (CPU == CPU_65C02 &&
1256 (E->Info & OF_UBRA) != 0 &&
1258 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1260 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1261 CE_ReplaceOPC (E, OP65_BRA);
1270 /* Return the number of changes made */