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 /*****************************************************************************/
46 /*****************************************************************************/
47 /* Helper functions */
48 /*****************************************************************************/
52 static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
53 /* Get the branch distance between the two entries and return it. The distance
54 * will be negative for backward jumps and positive for forward jumps.
57 /* Get the index of the branch target */
58 unsigned TI = CS_GetEntryIndex (S, To);
60 /* Determine the branch distance */
63 /* Forward branch, do not count the current insn */
66 CodeEntry* N = CS_GetEntry (S, J++);
73 CodeEntry* N = CS_GetEntry (S, J++);
78 /* Return the calculated distance */
84 static int IsShortDist (int Distance)
85 /* Return true if the given distance is a short branch distance */
87 return (Distance >= -125 && Distance <= 125);
92 static short RegVal (unsigned short Use, const RegContents* RC)
93 /* Return the contents of the given register */
95 if ((Use & REG_A) != 0) {
97 } else if ((Use & REG_X) != 0) {
99 } else if ((Use & REG_Y) != 0) {
101 } else if ((Use & REG_TMP1) != 0) {
103 } else if ((Use & REG_SREG_LO) != 0) {
105 } else if ((Use & REG_SREG_HI) != 0) {
114 /*****************************************************************************/
115 /* Replace jumps to RTS by RTS */
116 /*****************************************************************************/
120 unsigned OptRTSJumps1 (CodeSeg* S)
121 /* Replace jumps to RTS by RTS */
123 unsigned Changes = 0;
125 /* Walk over all entries minus the last one */
127 while (I < CS_GetEntryCount (S)) {
129 /* Get the next entry */
130 CodeEntry* E = CS_GetEntry (S, I);
132 /* Check if it's an unconditional branch to a local target */
133 if ((E->Info & OF_UBRA) != 0 &&
135 E->JumpTo->Owner->OPC == OP65_RTS) {
137 /* Insert an RTS instruction */
138 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
139 CS_InsertEntry (S, X, I+1);
141 /* Delete the jump */
144 /* Remember, we had changes */
154 /* Return the number of changes made */
160 unsigned OptRTSJumps2 (CodeSeg* S)
161 /* Replace long conditional jumps to RTS */
163 unsigned Changes = 0;
165 /* Walk over all entries minus the last one */
167 while (I < CS_GetEntryCount (S)) {
171 /* Get the next entry */
172 CodeEntry* E = CS_GetEntry (S, I);
174 /* Check if it's an unconditional branch to a local target */
175 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
176 (E->Info & OF_LBRA) != 0 && /* Long branch */
177 E->JumpTo != 0 && /* Local label */
178 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
179 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
185 /* We will create a jump around an RTS instead of the long branch */
186 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
187 CS_InsertEntry (S, X, I+1);
189 /* Get the new branch opcode */
190 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
192 /* Get the label attached to N, create a new one if needed */
193 LN = CS_GenLabel (S, N);
195 /* Generate the branch */
196 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
197 CS_InsertEntry (S, X, I+1);
199 /* Delete the long branch */
202 /* Remember, we had changes */
212 /* Return the number of changes made */
218 /*****************************************************************************/
219 /* Remove dead jumps */
220 /*****************************************************************************/
224 unsigned OptDeadJumps (CodeSeg* S)
225 /* Remove dead jumps (jumps to the next instruction) */
227 unsigned Changes = 0;
229 /* Walk over all entries minus the last one */
231 while (I < CS_GetEntryCount (S)) {
233 /* Get the next entry */
234 CodeEntry* E = CS_GetEntry (S, I);
236 /* Check if it's a branch, if it has a local target, and if the target
237 * is the next instruction.
239 if (E->AM == AM65_BRA &&
241 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
243 /* Delete the dead jump */
246 /* Remember, we had changes */
257 /* Return the number of changes made */
263 /*****************************************************************************/
264 /* Remove dead code */
265 /*****************************************************************************/
269 unsigned OptDeadCode (CodeSeg* S)
270 /* Remove dead code (code that follows an unconditional jump or an rts/rti
274 unsigned Changes = 0;
276 /* Walk over all entries */
278 while (I < CS_GetEntryCount (S)) {
283 CodeEntry* E = CS_GetEntry (S, I);
285 /* Check if it's an unconditional branch, and if the next entry has
288 if ((E->Info & OF_DEAD) != 0 &&
289 (N = CS_GetNextEntry (S, I)) != 0 &&
292 /* Delete the next entry */
293 CS_DelEntry (S, I+1);
295 /* Remember, we had changes */
306 /* Return the number of changes made */
312 /*****************************************************************************/
313 /* Optimize jump cascades */
314 /*****************************************************************************/
318 unsigned OptJumpCascades (CodeSeg* S)
319 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
320 * replaced by a jump to the final location. This will in some cases produce
321 * worse code, because some jump targets are no longer reachable by short
322 * branches, but this is quite rare, so there are more advantages than
326 unsigned Changes = 0;
328 /* Walk over all entries */
330 while (I < CS_GetEntryCount (S)) {
336 CodeEntry* E = CS_GetEntry (S, I);
338 /* Check if it's a branch, if it has a jump label, if this jump
339 * label is not attached to the instruction itself, and if the
340 * target instruction is itself a branch.
342 if ((E->Info & OF_BRA) != 0 &&
343 (OldLabel = E->JumpTo) != 0 &&
344 (N = OldLabel->Owner) != E &&
345 (N->Info & OF_BRA) != 0) {
347 /* Check if we can use the final target label. This is the case,
348 * if the target branch is an absolut branch, or if it is a
349 * conditional branch checking the same condition as the first one.
351 if ((N->Info & OF_UBRA) != 0 ||
352 ((E->Info & OF_CBRA) != 0 &&
353 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
355 /* This is a jump cascade and we may jump to the final target.
356 * Insert a new instruction, then remove the old one
358 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
360 /* Insert it behind E */
361 CS_InsertEntry (S, X, I+1);
366 /* Remember, we had changes */
374 /* Check if both are conditional branches, and the condition of
375 * the second is the inverse of that of the first. In this case,
376 * the second branch will never be taken, and we may jump directly
377 * to the instruction behind this one.
379 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
381 CodeEntry* X; /* Instruction behind N */
382 CodeLabel* LX; /* Label attached to X */
384 /* Get the branch conditions of both branches */
385 bc_t BC1 = GetBranchCond (E->OPC);
386 bc_t BC2 = GetBranchCond (N->OPC);
388 /* Check the branch conditions */
389 if (BC1 != GetInverseCond (BC2)) {
390 /* Condition not met */
394 /* We may jump behind this conditional branch. Get the
395 * pointer to the next instruction
397 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
398 /* N is the last entry, bail out */
402 /* Get the label attached to X, create a new one if needed */
403 LX = CS_GenLabel (S, X);
405 /* Move the reference from E to the new label */
406 CS_MoveLabelRef (S, E, LX);
408 /* Remember, we had changes */
423 /* Return the number of changes made */
429 /*****************************************************************************/
430 /* Optimize jsr/rts */
431 /*****************************************************************************/
435 unsigned OptRTS (CodeSeg* S)
436 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
437 * replaced by a jump. Don't bother to delete the RTS if it does not have a
438 * label, the dead code elimination should take care of it.
441 unsigned Changes = 0;
443 /* Walk over all entries minus the last one */
445 while (I < CS_GetEntryCount (S)) {
450 CodeEntry* E = CS_GetEntry (S, I);
452 /* Check if it's a subroutine call and if the following insn is RTS */
453 if (E->OPC == OP65_JSR &&
454 (N = CS_GetNextEntry (S, I)) != 0 &&
455 N->OPC == OP65_RTS) {
457 /* Change the jsr to a jmp and use the additional info for a jump */
459 CE_ReplaceOPC (E, OP65_JMP);
461 /* Remember, we had changes */
471 /* Return the number of changes made */
477 /*****************************************************************************/
478 /* Optimize jump targets */
479 /*****************************************************************************/
483 unsigned OptJumpTarget (CodeSeg* S)
484 /* If the instruction preceeding an unconditional branch is the same as the
485 * instruction preceeding the jump target, the jump target may be moved
486 * one entry back. This is a size optimization, since the instruction before
487 * the branch gets removed.
490 unsigned Changes = 0;
491 CodeEntry* E1; /* Entry 1 */
492 CodeEntry* E2; /* Entry 2 */
493 CodeEntry* T1; /* Jump target entry 1 */
494 CodeLabel* TL1; /* Target label 1 */
496 /* Walk over the entries */
498 while (I < CS_GetEntryCount (S)) {
501 E2 = CS_GetNextEntry (S, I);
503 /* Check if we have a jump or branch, and a matching label, which
504 * is not attached to the jump itself
507 (E2->Info & OF_UBRA) != 0 &&
509 E2->JumpTo->Owner != E2) {
511 /* Get the entry preceeding the branch target */
512 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
514 /* There is no such entry */
518 /* Get the entry preceeding the jump */
519 E1 = CS_GetEntry (S, I);
521 /* Check if both preceeding instructions are identical */
522 if (!CodeEntriesAreEqual (E1, T1)) {
523 /* Not equal, try next */
527 /* Get the label for the instruction preceeding the jump target.
528 * This routine will create a new label if the instruction does
529 * not already have one.
531 TL1 = CS_GenLabel (S, T1);
533 /* Change the jump target to point to this new label */
534 CS_MoveLabelRef (S, E2, TL1);
536 /* If the instruction preceeding the jump has labels attached,
537 * move references to this label to the new label.
539 if (CE_HasLabel (E1)) {
540 CS_MoveLabels (S, E1, T1);
543 /* Remove the entry preceeding the jump */
546 /* Remember, we had changes */
557 /* Return the number of changes made */
563 /*****************************************************************************/
564 /* Optimize conditional branches */
565 /*****************************************************************************/
569 unsigned OptCondBranches (CodeSeg* S)
570 /* Performs several optimization steps:
572 * - If an immidiate load of a register is followed by a conditional jump that
573 * is never taken because the load of the register sets the flags in such a
574 * manner, remove the conditional branch.
575 * - If the conditional branch is always taken because of the register load,
576 * replace it by a jmp.
577 * - If a conditional branch jumps around an unconditional branch, remove the
578 * conditional branch and make the jump a conditional branch with the
579 * inverse condition of the first one.
582 unsigned Changes = 0;
584 /* Walk over the entries */
586 while (I < CS_GetEntryCount (S)) {
592 CodeEntry* E = CS_GetEntry (S, I);
594 /* Check if it's a register load */
595 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
596 E->AM == AM65_IMM && /* ..with immidiate addressing */
597 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
598 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
599 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
600 !CE_HasLabel (N)) { /* ..and does not have a label */
602 /* Get the branch condition */
603 bc_t BC = GetBranchCond (N->OPC);
605 /* Check the argument against the branch condition */
606 if ((BC == BC_EQ && E->Num != 0) ||
607 (BC == BC_NE && E->Num == 0) ||
608 (BC == BC_PL && (E->Num & 0x80) != 0) ||
609 (BC == BC_MI && (E->Num & 0x80) == 0)) {
611 /* Remove the conditional branch */
612 CS_DelEntry (S, I+1);
614 /* Remember, we had changes */
617 } else if ((BC == BC_EQ && E->Num == 0) ||
618 (BC == BC_NE && E->Num != 0) ||
619 (BC == BC_PL && (E->Num & 0x80) == 0) ||
620 (BC == BC_MI && (E->Num & 0x80) != 0)) {
622 /* The branch is always taken, replace it by a jump */
623 CE_ReplaceOPC (N, OP65_JMP);
625 /* Remember, we had changes */
631 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
632 (L = E->JumpTo) != 0 && /* ..referencing a local label */
633 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
634 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
635 !CE_HasLabel (N) && /* ..has no label attached */
636 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
638 /* Replace the jump by a conditional branch with the inverse branch
639 * condition than the branch around it.
641 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
643 /* Remove the conditional branch */
646 /* Remember, we had changes */
656 /* Return the number of changes made */
662 /*****************************************************************************/
663 /* Remove unused loads and stores */
664 /*****************************************************************************/
668 unsigned OptUnusedLoads (CodeSeg* S)
669 /* Remove loads of registers where the value loaded is not used later. */
671 unsigned Changes = 0;
673 /* Walk over the entries */
675 while (I < CS_GetEntryCount (S)) {
680 CodeEntry* E = CS_GetEntry (S, I);
682 /* Check if it's a register load or transfer insn */
683 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
684 (N = CS_GetNextEntry (S, I)) != 0 &&
685 !CE_UseLoadFlags (N)) {
687 /* Check which sort of load or transfer it is */
694 case OP65_TYA: R = REG_A; break;
698 case OP65_TAX: R = REG_X; break;
702 case OP65_TAY: R = REG_Y; break;
703 default: goto NextEntry; /* OOPS */
706 /* Get register usage and check if the register value is used later */
707 if ((GetRegInfo (S, I+1, R) & R) == 0) {
709 /* Register value is not used, remove the load */
712 /* Remember, we had changes */
724 /* Return the number of changes made */
730 unsigned OptUnusedStores (CodeSeg* S)
731 /* Remove stores into zero page registers that aren't used later */
733 unsigned Changes = 0;
735 /* Walk over the entries */
737 while (I < CS_GetEntryCount (S)) {
740 CodeEntry* E = CS_GetEntry (S, I);
742 /* Check if it's a register load or transfer insn */
743 if ((E->Info & OF_STORE) != 0 &&
745 (E->Chg & REG_ZP) != 0) {
747 /* Check for the zero page location. We know that there cannot be
748 * more than one zero page location involved in the store.
750 unsigned R = E->Chg & REG_ZP;
752 /* Get register usage and check if the register value is used later */
753 if ((GetRegInfo (S, I+1, R) & R) == 0) {
755 /* Register value is not used, remove the load */
758 /* Remember, we had changes */
769 /* Return the number of changes made */
775 unsigned OptDupLoads (CodeSeg* S)
776 /* Remove loads of registers where the value loaded is already in the register. */
778 unsigned Changes = 0;
781 /* Generate register info for this step */
784 /* Walk over the entries */
786 while (I < CS_GetEntryCount (S)) {
791 CodeEntry* E = CS_GetEntry (S, I);
793 /* Assume we won't delete the entry */
796 /* Get a pointer to the input registers of the insn */
797 const RegContents* In = &E->RI->In;
799 /* Handle the different instructions */
803 if (In->RegA >= 0 && /* Value of A is known */
804 CE_KnownImm (E) && /* Value to be loaded is known */
805 In->RegA == (long) E->Num && /* Both are equal */
806 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
807 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
813 if (In->RegX >= 0 && /* Value of X is known */
814 CE_KnownImm (E) && /* Value to be loaded is known */
815 In->RegX == (long) E->Num && /* Both are equal */
816 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
817 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
823 if (In->RegY >= 0 && /* Value of Y is known */
824 CE_KnownImm (E) && /* Value to be loaded is known */
825 In->RegY == (long) E->Num && /* Both are equal */
826 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
827 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
833 /* If we store into a known zero page location, and this
834 * location does already contain the value to be stored,
837 if (In->RegA >= 0 && /* Value of A is known */
838 E->AM == AM65_ZP && /* Store into zp */
839 In->RegA == RegVal (E->Chg, In)) { /* Value identical */
846 /* If we store into a known zero page location, and this
847 * location does already contain the value to be stored,
850 if (In->RegX >= 0 && /* Value of A is known */
851 E->AM == AM65_ZP && /* Store into zp */
852 In->RegX == RegVal (E->Chg, In)) { /* Value identical */
856 /* If the value in the X register is known and the same as
857 * that in the A register, replace the store by a STA. The
858 * optimizer will then remove the load instruction for X
859 * later. STX does support the zeropage,y addressing mode,
860 * so be sure to check for that.
862 } else if (In->RegX >= 0 &&
863 In->RegX == In->RegA &&
864 E->AM != AM65_ABSY &&
866 /* Use the A register instead */
867 CE_ReplaceOPC (E, OP65_STA);
872 /* If we store into a known zero page location, and this
873 * location does already contain the value to be stored,
876 if (In->RegY >= 0 && /* Value of Y is known */
877 E->AM == AM65_ZP && /* Store into zp */
878 In->RegY == RegVal (E->Chg, In)) { /* Value identical */
882 /* If the value in the Y 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 Y
885 * later. If replacement by A is not possible try a
886 * replacement by X, but check for invalid addressing modes
889 } else if (In->RegY >= 0) {
890 if (In->RegY == In->RegA) {
891 CE_ReplaceOPC (E, OP65_STA);
892 } else if (In->RegY == In->RegX &&
893 E->AM != AM65_ABSX &&
895 CE_ReplaceOPC (E, OP65_STX);
901 /* If we store into a known zero page location, and this
902 * location does already contain the value to be stored,
905 if (CPU >= CPU_65C02 && E->AM == AM65_ZP) {
906 if (RegVal (E->Chg, In) == 0) {
914 In->RegA == In->RegX &&
915 (N = CS_GetNextEntry (S, I)) != 0 &&
916 !CE_UseLoadFlags (N)) {
917 /* Value is identical and not followed by a branch */
924 In->RegA == In->RegY &&
925 (N = CS_GetNextEntry (S, I)) != 0 &&
926 !CE_UseLoadFlags (N)) {
927 /* Value is identical and not followed by a branch */
934 In->RegX == In->RegA &&
935 (N = CS_GetNextEntry (S, I)) != 0 &&
936 !CE_UseLoadFlags (N)) {
937 /* Value is identical and not followed by a branch */
944 In->RegY == In->RegA &&
945 (N = CS_GetNextEntry (S, I)) != 0 &&
946 !CE_UseLoadFlags (N)) {
947 /* Value is identical and not followed by a branch */
957 /* Delete the entry if requested */
960 /* Register value is not used, remove the load */
963 /* Remember, we had changes */
975 /* Free register info */
978 /* Return the number of changes made */
984 unsigned OptStoreLoad (CodeSeg* S)
985 /* Remove a store followed by a load from the same location. */
987 unsigned Changes = 0;
989 /* Walk over the entries */
991 while (I < CS_GetEntryCount (S)) {
997 CodeEntry* E = CS_GetEntry (S, I);
999 /* Check if it is a store instruction followed by a load from the
1000 * same address which is itself not followed by a conditional branch.
1002 if ((E->Info & OF_STORE) != 0 &&
1003 (N = CS_GetNextEntry (S, I)) != 0 &&
1006 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1007 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1008 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1009 strcmp (E->Arg, N->Arg) == 0 &&
1010 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1011 !CE_UseLoadFlags (X)) {
1013 /* Register has already the correct value, remove the load */
1014 CS_DelEntry (S, I+1);
1016 /* Remember, we had changes */
1026 /* Return the number of changes made */
1032 unsigned OptTransfers (CodeSeg* S)
1033 /* Remove transfers from one register to another and back */
1035 unsigned Changes = 0;
1037 /* Walk over the entries */
1039 while (I < CS_GetEntryCount (S)) {
1045 /* Get next entry */
1046 CodeEntry* E = CS_GetEntry (S, I);
1048 /* Check if it is a store instruction followed by a load from the
1049 * same address which is itself not followed by a conditional branch.
1051 if ((E->Info & OF_XFR) != 0 &&
1052 (N = CS_GetNextEntry (S, I)) != 0 &&
1054 (N->Info & OF_XFR) != 0) {
1056 /* Check if it's a transfer and back */
1057 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1058 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1059 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1060 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1062 /* If the next insn is a conditional branch, check if the insn
1063 * preceeding the first xfr will set the flags right, otherwise we
1064 * may not remove the sequence.
1066 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1069 if (CE_UseLoadFlags (X)) {
1071 /* No preceeding entry */
1074 P = CS_GetEntry (S, I-1);
1075 if ((P->Info & OF_SETF) == 0) {
1076 /* Does not set the flags */
1081 /* Remove both transfers */
1082 CS_DelEntry (S, I+1);
1085 /* Remember, we had changes */
1096 /* Return the number of changes made */
1102 unsigned OptPushPop (CodeSeg* S)
1103 /* Remove a PHA/PLA sequence were A is not used later */
1105 unsigned Changes = 0;
1106 unsigned Push = 0; /* Index of push insn */
1107 unsigned Pop = 0; /* Index of pop insn */
1112 } State = Searching;
1114 /* Walk over the entries. Look for a push instruction that is followed by
1115 * a pop later, where the pop is not followed by an conditional branch,
1116 * and where the value of the A register is not used later on.
1117 * Look out for the following problems:
1119 * - There may be another PHA/PLA inside the sequence: Restart it.
1120 * - If the PLA has a label, all jumps to this label must be inside
1121 * the sequence, otherwise we cannot remove the PHA/PLA.
1124 while (I < CS_GetEntryCount (S)) {
1126 /* Get next entry */
1127 CodeEntry* E = CS_GetEntry (S, I);
1132 if (E->OPC == OP65_PHA) {
1133 /* Found start of sequence */
1140 if (E->OPC == OP65_PHA) {
1141 /* Inner push/pop, restart */
1143 } else if (E->OPC == OP65_PLA) {
1144 /* Found a matching pop */
1151 /* Next insn, just check if it is no conditional branch and
1152 * that A is not used later. Check also that the range we have
1153 * found now is a basic block, which means that the PHA is the
1154 * only entrance and the PLA the only exit.
1156 if ((E->Info & OF_CBRA) == 0 &&
1158 CS_IsBasicBlock (S, Push, Pop)) {
1159 /* We can remove the PHA and PLA instructions */
1160 CS_DelEntry (S, Pop);
1161 CS_DelEntry (S, Push);
1162 /* Correct I so we continue with the next insn */
1164 /* Remember we had changes */
1167 /* Go into search mode again */
1177 /* Return the number of changes made */
1183 /*****************************************************************************/
1184 /* Optimize branch types */
1185 /*****************************************************************************/
1189 unsigned OptBranchDist (CodeSeg* S)
1190 /* Change branches for the distance needed. */
1192 unsigned Changes = 0;
1194 /* Walk over the entries */
1196 while (I < CS_GetEntryCount (S)) {
1198 /* Get next entry */
1199 CodeEntry* E = CS_GetEntry (S, I);
1201 /* Check if it's a conditional branch to a local label. */
1202 if (E->Info & OF_CBRA) {
1204 /* Is this a branch to a local symbol? */
1205 if (E->JumpTo != 0) {
1207 /* Check if the branch distance is short */
1208 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1210 /* Make the branch short/long according to distance */
1211 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1212 /* Short branch but long distance */
1213 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1215 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1216 /* Long branch but short distance */
1217 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1221 } else if ((E->Info & OF_LBRA) == 0) {
1223 /* Short branch to external symbol - make it long */
1224 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1229 } else if (CPU == CPU_65C02 &&
1230 (E->Info & OF_UBRA) != 0 &&
1232 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1234 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1235 CE_ReplaceOPC (E, OP65_BRA);
1244 /* Return the number of changes made */