1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001-2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
49 /* Helper functions */
50 /*****************************************************************************/
54 static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
55 /* Get the branch distance between the two entries and return it. The distance
56 * will be negative for backward jumps and positive for forward jumps.
59 /* Get the index of the branch target */
60 unsigned TI = CS_GetEntryIndex (S, To);
62 /* Determine the branch distance */
65 /* Forward branch, do not count the current insn */
68 CodeEntry* N = CS_GetEntry (S, J++);
75 CodeEntry* N = CS_GetEntry (S, J++);
80 /* Return the calculated distance */
86 static int IsShortDist (int Distance)
87 /* Return true if the given distance is a short branch distance */
89 return (Distance >= -125 && Distance <= 125);
94 static short ZPRegVal (unsigned short Use, const RegContents* RC)
95 /* Return the contents of the given zeropage register */
97 if ((Use & REG_TMP1) != 0) {
99 } else if ((Use & REG_PTR1_LO) != 0) {
101 } else if ((Use & REG_PTR1_HI) != 0) {
103 } else if ((Use & REG_SREG_LO) != 0) {
105 } else if ((Use & REG_SREG_HI) != 0) {
108 return UNKNOWN_REGVAL;
114 static short RegVal (unsigned short Use, const RegContents* RC)
115 /* Return the contents of the given register */
117 if ((Use & REG_A) != 0) {
119 } else if ((Use & REG_X) != 0) {
121 } else if ((Use & REG_Y) != 0) {
124 return ZPRegVal (Use, RC);
130 /*****************************************************************************/
131 /* Replace jumps to RTS by RTS */
132 /*****************************************************************************/
136 unsigned OptRTSJumps1 (CodeSeg* S)
137 /* Replace jumps to RTS by RTS */
139 unsigned Changes = 0;
141 /* Walk over all entries minus the last one */
143 while (I < CS_GetEntryCount (S)) {
145 /* Get the next entry */
146 CodeEntry* E = CS_GetEntry (S, I);
148 /* Check if it's an unconditional branch to a local target */
149 if ((E->Info & OF_UBRA) != 0 &&
151 E->JumpTo->Owner->OPC == OP65_RTS) {
153 /* Insert an RTS instruction */
154 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
155 CS_InsertEntry (S, X, I+1);
157 /* Delete the jump */
160 /* Remember, we had changes */
170 /* Return the number of changes made */
176 unsigned OptRTSJumps2 (CodeSeg* S)
177 /* Replace long conditional jumps to RTS */
179 unsigned Changes = 0;
181 /* Walk over all entries minus the last one */
183 while (I < CS_GetEntryCount (S)) {
187 /* Get the next entry */
188 CodeEntry* E = CS_GetEntry (S, I);
190 /* Check if it's an unconditional branch to a local target */
191 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
192 (E->Info & OF_LBRA) != 0 && /* Long branch */
193 E->JumpTo != 0 && /* Local label */
194 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
195 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
201 /* We will create a jump around an RTS instead of the long branch */
202 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
203 CS_InsertEntry (S, X, I+1);
205 /* Get the new branch opcode */
206 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
208 /* Get the label attached to N, create a new one if needed */
209 LN = CS_GenLabel (S, N);
211 /* Generate the branch */
212 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
213 CS_InsertEntry (S, X, I+1);
215 /* Delete the long branch */
218 /* Remember, we had changes */
228 /* Return the number of changes made */
234 /*****************************************************************************/
235 /* Remove dead jumps */
236 /*****************************************************************************/
240 unsigned OptDeadJumps (CodeSeg* S)
241 /* Remove dead jumps (jumps to the next instruction) */
243 unsigned Changes = 0;
245 /* Walk over all entries minus the last one */
247 while (I < CS_GetEntryCount (S)) {
249 /* Get the next entry */
250 CodeEntry* E = CS_GetEntry (S, I);
252 /* Check if it's a branch, if it has a local target, and if the target
253 * is the next instruction.
255 if (E->AM == AM65_BRA &&
257 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
259 /* Delete the dead jump */
262 /* Remember, we had changes */
273 /* Return the number of changes made */
279 /*****************************************************************************/
280 /* Remove dead code */
281 /*****************************************************************************/
285 unsigned OptDeadCode (CodeSeg* S)
286 /* Remove dead code (code that follows an unconditional jump or an rts/rti
290 unsigned Changes = 0;
292 /* Walk over all entries */
294 while (I < CS_GetEntryCount (S)) {
300 CodeEntry* E = CS_GetEntry (S, I);
302 /* Check if it's an unconditional branch, and if the next entry has
303 * no labels attached, or if the label is just used so that the insn
304 * can jump to itself.
306 if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
307 (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
308 (!CE_HasLabel (N) || /* Don't has a label */
309 ((N->Info & OF_UBRA) != 0 && /* Uncond branch */
310 (LN = N->JumpTo) != 0 && /* Jumps to known label */
311 LN->Owner == N && /* Attached to insn */
312 CL_GetRefCount (LN) == 1))) { /* Only reference */
314 /* Delete the next entry */
315 CS_DelEntry (S, I+1);
317 /* Remember, we had changes */
328 /* Return the number of changes made */
334 /*****************************************************************************/
335 /* Optimize jump cascades */
336 /*****************************************************************************/
340 unsigned OptJumpCascades (CodeSeg* S)
341 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
342 * replaced by a jump to the final location. This will in some cases produce
343 * worse code, because some jump targets are no longer reachable by short
344 * branches, but this is quite rare, so there are more advantages than
348 unsigned Changes = 0;
350 /* Walk over all entries */
352 while (I < CS_GetEntryCount (S)) {
358 CodeEntry* E = CS_GetEntry (S, I);
360 /* Check if it's a branch, if it has a jump label, if this jump
361 * label is not attached to the instruction itself, and if the
362 * target instruction is itself a branch.
364 if ((E->Info & OF_BRA) != 0 &&
365 (OldLabel = E->JumpTo) != 0 &&
366 (N = OldLabel->Owner) != E &&
367 (N->Info & OF_BRA) != 0) {
369 /* Check if we can use the final target label. This is the case,
370 * if the target branch is an absolut branch, or if it is a
371 * conditional branch checking the same condition as the first one.
373 if ((N->Info & OF_UBRA) != 0 ||
374 ((E->Info & OF_CBRA) != 0 &&
375 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
377 /* This is a jump cascade and we may jump to the final target,
378 * provided that the other insn does not jump to itself. If
379 * this is the case, we can also jump to ourselves, otherwise
380 * insert a jump to the new instruction and remove the old one.
383 CodeLabel* LN = N->JumpTo;
385 if (LN != 0 && LN->Owner == N) {
387 /* We found a jump to a jump to itself. Replace our jump
388 * by a jump to itself.
390 CodeLabel* LE = CS_GenLabel (S, E);
391 X = NewCodeEntry (E->OPC, E->AM, LE->Name, LE, E->LI);
395 /* Jump to the final jump target */
396 X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
400 /* Insert it behind E */
401 CS_InsertEntry (S, X, I+1);
406 /* Remember, we had changes */
409 /* Check if both are conditional branches, and the condition of
410 * the second is the inverse of that of the first. In this case,
411 * the second branch will never be taken, and we may jump directly
412 * to the instruction behind this one.
414 } else if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
416 CodeEntry* X; /* Instruction behind N */
417 CodeLabel* LX; /* Label attached to X */
419 /* Get the branch conditions of both branches */
420 bc_t BC1 = GetBranchCond (E->OPC);
421 bc_t BC2 = GetBranchCond (N->OPC);
423 /* Check the branch conditions */
424 if (BC1 != GetInverseCond (BC2)) {
425 /* Condition not met */
429 /* We may jump behind this conditional branch. Get the
430 * pointer to the next instruction
432 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
433 /* N is the last entry, bail out */
437 /* Get the label attached to X, create a new one if needed */
438 LX = CS_GenLabel (S, X);
440 /* Move the reference from E to the new label */
441 CS_MoveLabelRef (S, E, LX);
443 /* Remember, we had changes */
455 /* Return the number of changes made */
461 /*****************************************************************************/
462 /* Optimize jsr/rts */
463 /*****************************************************************************/
467 unsigned OptRTS (CodeSeg* S)
468 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
469 * replaced by a jump. Don't bother to delete the RTS if it does not have a
470 * label, the dead code elimination should take care of it.
473 unsigned Changes = 0;
475 /* Walk over all entries minus the last one */
477 while (I < CS_GetEntryCount (S)) {
482 CodeEntry* E = CS_GetEntry (S, I);
484 /* Check if it's a subroutine call and if the following insn is RTS */
485 if (E->OPC == OP65_JSR &&
486 (N = CS_GetNextEntry (S, I)) != 0 &&
487 N->OPC == OP65_RTS) {
489 /* Change the jsr to a jmp and use the additional info for a jump */
491 CE_ReplaceOPC (E, OP65_JMP);
493 /* Remember, we had changes */
503 /* Return the number of changes made */
509 /*****************************************************************************/
510 /* Optimize jump targets */
511 /*****************************************************************************/
515 unsigned OptJumpTarget (CodeSeg* S)
516 /* If the instruction preceeding an unconditional branch is the same as the
517 * instruction preceeding the jump target, the jump target may be moved
518 * one entry back. This is a size optimization, since the instruction before
519 * the branch gets removed.
522 unsigned Changes = 0;
523 CodeEntry* E1; /* Entry 1 */
524 CodeEntry* E2; /* Entry 2 */
525 CodeEntry* T1; /* Jump target entry 1 */
526 CodeLabel* TL1; /* Target label 1 */
528 /* Walk over the entries */
530 while (I < CS_GetEntryCount (S)) {
533 E2 = CS_GetNextEntry (S, I);
535 /* Check if we have a jump or branch, and a matching label, which
536 * is not attached to the jump itself
539 (E2->Info & OF_UBRA) != 0 &&
541 E2->JumpTo->Owner != E2) {
543 /* Get the entry preceeding the branch target */
544 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
546 /* There is no such entry */
550 /* Get the entry preceeding the jump */
551 E1 = CS_GetEntry (S, I);
553 /* Check if both preceeding instructions are identical */
554 if (!CodeEntriesAreEqual (E1, T1)) {
555 /* Not equal, try next */
559 /* Get the label for the instruction preceeding the jump target.
560 * This routine will create a new label if the instruction does
561 * not already have one.
563 TL1 = CS_GenLabel (S, T1);
565 /* Change the jump target to point to this new label */
566 CS_MoveLabelRef (S, E2, TL1);
568 /* If the instruction preceeding the jump has labels attached,
569 * move references to this label to the new label.
571 if (CE_HasLabel (E1)) {
572 CS_MoveLabels (S, E1, T1);
575 /* Remove the entry preceeding the jump */
578 /* Remember, we had changes */
588 /* Return the number of changes made */
594 /*****************************************************************************/
595 /* Optimize conditional branches */
596 /*****************************************************************************/
600 unsigned OptCondBranches (CodeSeg* S)
601 /* Performs several optimization steps:
603 * - If an immidiate load of a register is followed by a conditional jump that
604 * is never taken because the load of the register sets the flags in such a
605 * manner, remove the conditional branch.
606 * - If the conditional branch is always taken because of the register load,
607 * replace it by a jmp.
608 * - If a conditional branch jumps around an unconditional branch, remove the
609 * conditional branch and make the jump a conditional branch with the
610 * inverse condition of the first one.
613 unsigned Changes = 0;
615 /* Walk over the entries */
617 while (I < CS_GetEntryCount (S)) {
623 CodeEntry* E = CS_GetEntry (S, I);
625 /* Check if it's a register load */
626 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
627 E->AM == AM65_IMM && /* ..with immidiate addressing */
628 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
629 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
630 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
631 !CE_HasLabel (N)) { /* ..and does not have a label */
633 /* Get the branch condition */
634 bc_t BC = GetBranchCond (N->OPC);
636 /* Check the argument against the branch condition */
637 if ((BC == BC_EQ && E->Num != 0) ||
638 (BC == BC_NE && E->Num == 0) ||
639 (BC == BC_PL && (E->Num & 0x80) != 0) ||
640 (BC == BC_MI && (E->Num & 0x80) == 0)) {
642 /* Remove the conditional branch */
643 CS_DelEntry (S, I+1);
645 /* Remember, we had changes */
648 } else if ((BC == BC_EQ && E->Num == 0) ||
649 (BC == BC_NE && E->Num != 0) ||
650 (BC == BC_PL && (E->Num & 0x80) == 0) ||
651 (BC == BC_MI && (E->Num & 0x80) != 0)) {
653 /* The branch is always taken, replace it by a jump */
654 CE_ReplaceOPC (N, OP65_JMP);
656 /* Remember, we had changes */
662 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
663 (L = E->JumpTo) != 0 && /* ..referencing a local label */
664 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
665 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
666 !CE_HasLabel (N) && /* ..has no label attached */
667 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
669 /* Replace the jump by a conditional branch with the inverse branch
670 * condition than the branch around it.
672 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
674 /* Remove the conditional branch */
677 /* Remember, we had changes */
687 /* Return the number of changes made */
693 /*****************************************************************************/
694 /* Remove unused loads and stores */
695 /*****************************************************************************/
699 unsigned OptUnusedLoads (CodeSeg* S)
700 /* Remove loads of registers where the value loaded is not used later. */
702 unsigned Changes = 0;
704 /* Walk over the entries */
706 while (I < CS_GetEntryCount (S)) {
711 CodeEntry* E = CS_GetEntry (S, I);
713 /* Check if it's a register load or transfer insn */
714 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
715 (N = CS_GetNextEntry (S, I)) != 0 &&
716 !CE_UseLoadFlags (N)) {
718 /* Check which sort of load or transfer it is */
725 case OP65_TYA: R = REG_A; break;
729 case OP65_TAX: R = REG_X; break;
733 case OP65_TAY: R = REG_Y; break;
734 default: goto NextEntry; /* OOPS */
737 /* Get register usage and check if the register value is used later */
738 if ((GetRegInfo (S, I+1, R) & R) == 0) {
740 /* Register value is not used, remove the load */
743 /* Remember, we had changes. Account the deleted entry in I. */
756 /* Return the number of changes made */
762 unsigned OptUnusedStores (CodeSeg* S)
763 /* Remove stores into zero page registers that aren't used later */
765 unsigned Changes = 0;
767 /* Walk over the entries */
769 while (I < CS_GetEntryCount (S)) {
772 CodeEntry* E = CS_GetEntry (S, I);
774 /* Check if it's a register load or transfer insn */
775 if ((E->Info & OF_STORE) != 0 &&
777 (E->Chg & REG_ZP) != 0) {
779 /* Check for the zero page location. We know that there cannot be
780 * more than one zero page location involved in the store.
782 unsigned R = E->Chg & REG_ZP;
784 /* Get register usage and check if the register value is used later */
785 if ((GetRegInfo (S, I+1, R) & R) == 0) {
787 /* Register value is not used, remove the load */
790 /* Remember, we had changes */
801 /* Return the number of changes made */
807 unsigned OptDupLoads (CodeSeg* S)
808 /* Remove loads of registers where the value loaded is already in the register. */
810 unsigned Changes = 0;
813 /* Generate register info for this step */
816 /* Walk over the entries */
818 while (I < CS_GetEntryCount (S)) {
823 CodeEntry* E = CS_GetEntry (S, I);
825 /* Assume we won't delete the entry */
828 /* Get a pointer to the input registers of the insn */
829 const RegContents* In = &E->RI->In;
831 /* Handle the different instructions */
835 if (RegValIsKnown (In->RegA) && /* Value of A is known */
836 CE_KnownImm (E) && /* Value to be loaded is known */
837 In->RegA == (long) E->Num && /* Both are equal */
838 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
839 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
845 if (RegValIsKnown (In->RegX) && /* Value of X is known */
846 CE_KnownImm (E) && /* Value to be loaded is known */
847 In->RegX == (long) E->Num && /* Both are equal */
848 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
849 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
855 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
856 CE_KnownImm (E) && /* Value to be loaded is known */
857 In->RegY == (long) E->Num && /* Both are equal */
858 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
859 !CE_UseLoadFlags (N)) { /* Which does not use the flags */
865 /* If we store into a known zero page location, and this
866 * location does already contain the value to be stored,
869 if (RegValIsKnown (In->RegA) && /* Value of A is known */
870 E->AM == AM65_ZP && /* Store into zp */
871 In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
878 /* If we store into a known zero page location, and this
879 * location does already contain the value to be stored,
882 if (RegValIsKnown (In->RegX) && /* Value of A is known */
883 E->AM == AM65_ZP && /* Store into zp */
884 In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
888 /* If the value in the X register is known and the same as
889 * that in the A register, replace the store by a STA. The
890 * optimizer will then remove the load instruction for X
891 * later. STX does support the zeropage,y addressing mode,
892 * so be sure to check for that.
894 } else if (RegValIsKnown (In->RegX) &&
895 In->RegX == In->RegA &&
896 E->AM != AM65_ABSY &&
898 /* Use the A register instead */
899 CE_ReplaceOPC (E, OP65_STA);
904 /* If we store into a known zero page location, and this
905 * location does already contain the value to be stored,
908 if (RegValIsKnown (In->RegY) && /* Value of Y is known */
909 E->AM == AM65_ZP && /* Store into zp */
910 In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
914 /* If the value in the Y register is known and the same as
915 * that in the A register, replace the store by a STA. The
916 * optimizer will then remove the load instruction for Y
917 * later. If replacement by A is not possible try a
918 * replacement by X, but check for invalid addressing modes
921 } else if (RegValIsKnown (In->RegY)) {
922 if (In->RegY == In->RegA) {
923 CE_ReplaceOPC (E, OP65_STA);
924 } else if (In->RegY == In->RegX &&
925 E->AM != AM65_ABSX &&
927 CE_ReplaceOPC (E, OP65_STX);
933 /* If we store into a known zero page location, and this
934 * location does already contain the value to be stored,
937 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && E->AM == AM65_ZP) {
938 if (ZPRegVal (E->Chg, In) == 0) {
945 if (RegValIsKnown (In->RegA) &&
946 In->RegA == In->RegX &&
947 (N = CS_GetNextEntry (S, I)) != 0 &&
948 !CE_UseLoadFlags (N)) {
949 /* Value is identical and not followed by a branch */
955 if (RegValIsKnown (In->RegA) &&
956 In->RegA == In->RegY &&
957 (N = CS_GetNextEntry (S, I)) != 0 &&
958 !CE_UseLoadFlags (N)) {
959 /* Value is identical and not followed by a branch */
965 if (RegValIsKnown (In->RegX) &&
966 In->RegX == In->RegA &&
967 (N = CS_GetNextEntry (S, I)) != 0 &&
968 !CE_UseLoadFlags (N)) {
969 /* Value is identical and not followed by a branch */
975 if (RegValIsKnown (In->RegY) &&
976 In->RegY == In->RegA &&
977 (N = CS_GetNextEntry (S, I)) != 0 &&
978 !CE_UseLoadFlags (N)) {
979 /* Value is identical and not followed by a branch */
989 /* Delete the entry if requested */
992 /* Register value is not used, remove the load */
995 /* Remember, we had changes */
1007 /* Free register info */
1010 /* Return the number of changes made */
1016 unsigned OptStoreLoad (CodeSeg* S)
1017 /* Remove a store followed by a load from the same location. */
1019 unsigned Changes = 0;
1021 /* Walk over the entries */
1023 while (I < CS_GetEntryCount (S)) {
1028 /* Get next entry */
1029 CodeEntry* E = CS_GetEntry (S, I);
1031 /* Check if it is a store instruction followed by a load from the
1032 * same address which is itself not followed by a conditional branch.
1034 if ((E->Info & OF_STORE) != 0 &&
1035 (N = CS_GetNextEntry (S, I)) != 0 &&
1038 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1039 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1040 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1041 strcmp (E->Arg, N->Arg) == 0 &&
1042 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1043 !CE_UseLoadFlags (X)) {
1045 /* Register has already the correct value, remove the load */
1046 CS_DelEntry (S, I+1);
1048 /* Remember, we had changes */
1058 /* Return the number of changes made */
1064 unsigned OptTransfers1 (CodeSeg* S)
1065 /* Remove transfers from one register to another and back */
1067 unsigned Changes = 0;
1069 /* Walk over the entries */
1071 while (I < CS_GetEntryCount (S)) {
1077 /* Get next entry */
1078 CodeEntry* E = CS_GetEntry (S, I);
1080 /* Check if we have two transfer instructions */
1081 if ((E->Info & OF_XFR) != 0 &&
1082 (N = CS_GetNextEntry (S, I)) != 0 &&
1084 (N->Info & OF_XFR) != 0) {
1086 /* Check if it's a transfer and back */
1087 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1088 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1089 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1090 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
1092 /* If the next insn is a conditional branch, check if the insn
1093 * preceeding the first xfr will set the flags right, otherwise we
1094 * may not remove the sequence.
1096 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1099 if (CE_UseLoadFlags (X)) {
1101 /* No preceeding entry */
1104 P = CS_GetEntry (S, I-1);
1105 if ((P->Info & OF_SETF) == 0) {
1106 /* Does not set the flags */
1111 /* Remove both transfers */
1112 CS_DelEntry (S, I+1);
1115 /* Remember, we had changes */
1126 /* Return the number of changes made */
1132 unsigned OptTransfers2 (CodeSeg* S)
1133 /* Replace loads followed by a register transfer by a load with the second
1134 * register if possible.
1137 unsigned Changes = 0;
1139 /* Walk over the entries */
1141 while (I < CS_GetEntryCount (S)) {
1145 /* Get next entry */
1146 CodeEntry* E = CS_GetEntry (S, I);
1148 /* Check if we have a load followed by a transfer where the loaded
1149 * register is not used later.
1151 if ((E->Info & OF_LOAD) != 0 &&
1152 (N = CS_GetNextEntry (S, I)) != 0 &&
1154 (N->Info & OF_XFR) != 0 &&
1155 GetRegInfo (S, I+2, E->Chg) != E->Chg) {
1159 if (E->OPC == OP65_LDA && N->OPC == OP65_TAX) {
1160 /* LDA/TAX - check for the right addressing modes */
1161 if (E->AM == AM65_IMM ||
1163 E->AM == AM65_ABS ||
1164 E->AM == AM65_ABSY) {
1166 X = NewCodeEntry (OP65_LDX, E->AM, E->Arg, 0, N->LI);
1168 } else if (E->OPC == OP65_LDA && N->OPC == OP65_TAY) {
1169 /* LDA/TAY - check for the right addressing modes */
1170 if (E->AM == AM65_IMM ||
1172 E->AM == AM65_ZPX ||
1173 E->AM == AM65_ABS ||
1174 E->AM == AM65_ABSX) {
1176 X = NewCodeEntry (OP65_LDY, E->AM, E->Arg, 0, N->LI);
1178 } else if (E->OPC == OP65_LDY && N->OPC == OP65_TYA) {
1179 /* LDY/TYA. LDA supports all addressing modes LDY does */
1180 X = NewCodeEntry (OP65_LDA, E->AM, E->Arg, 0, N->LI);
1181 } else if (E->OPC == OP65_LDX && N->OPC == OP65_TXA) {
1182 /* LDX/TXA. LDA doesn't support zp,y, so we must map it to
1185 am_t AM = (E->AM == AM65_ZPY)? AM65_ABSY : E->AM;
1186 X = NewCodeEntry (OP65_LDA, AM, E->Arg, 0, N->LI);
1189 /* If we have a load entry, add it and remove the old stuff */
1191 CS_InsertEntry (S, X, I+2);
1192 CS_DelEntries (S, I, 2);
1194 --I; /* Correct for one entry less */
1202 /* Return the number of changes made */
1208 unsigned OptPushPop (CodeSeg* S)
1209 /* Remove a PHA/PLA sequence were A is not used later */
1211 unsigned Changes = 0;
1212 unsigned Push = 0; /* Index of push insn */
1213 unsigned Pop = 0; /* Index of pop insn */
1218 } State = Searching;
1220 /* Walk over the entries. Look for a push instruction that is followed by
1221 * a pop later, where the pop is not followed by an conditional branch,
1222 * and where the value of the A register is not used later on.
1223 * Look out for the following problems:
1225 * - There may be another PHA/PLA inside the sequence: Restart it.
1226 * - If the PLA has a label, all jumps to this label must be inside
1227 * the sequence, otherwise we cannot remove the PHA/PLA.
1230 while (I < CS_GetEntryCount (S)) {
1232 /* Get next entry */
1233 CodeEntry* E = CS_GetEntry (S, I);
1238 if (E->OPC == OP65_PHA) {
1239 /* Found start of sequence */
1246 if (E->OPC == OP65_PHA) {
1247 /* Inner push/pop, restart */
1249 } else if (E->OPC == OP65_PLA) {
1250 /* Found a matching pop */
1257 /* Next insn, just check if it is no conditional branch and
1258 * that A is not used later. Check also that the range we have
1259 * found now is a basic block, which means that the PHA is the
1260 * only entrance and the PLA the only exit.
1262 if ((E->Info & OF_CBRA) == 0 &&
1264 CS_IsBasicBlock (S, Push, Pop)) {
1265 /* We can remove the PHA and PLA instructions */
1266 CS_DelEntry (S, Pop);
1267 CS_DelEntry (S, Push);
1268 /* Correct I so we continue with the next insn */
1270 /* Remember we had changes */
1273 /* Go into search mode again */
1283 /* Return the number of changes made */
1289 unsigned OptPrecalc (CodeSeg* S)
1290 /* Replace immediate operations with the accu where the current contents are
1291 * known by a load of the final value.
1294 unsigned Changes = 0;
1297 /* Generate register info for this step */
1300 /* Walk over the entries */
1302 while (I < CS_GetEntryCount (S)) {
1304 /* Get next entry */
1305 CodeEntry* E = CS_GetEntry (S, I);
1307 /* Get a pointer to the output registers of the insn */
1308 const RegContents* Out = &E->RI->Out;
1310 /* Argument for LDA and flag */
1311 const char* Arg = 0;
1313 /* Handle the different instructions */
1317 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegA)) {
1318 /* Result of load is known */
1319 Arg = MakeHexArg (Out->RegA);
1324 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegX)) {
1325 /* Result of load is known */
1326 Arg = MakeHexArg (Out->RegX);
1331 if (E->AM != AM65_IMM && RegValIsKnown (Out->RegY)) {
1332 /* Result of load is known */
1333 Arg = MakeHexArg (Out->RegY);
1342 if (RegValIsKnown (Out->RegA)) {
1343 /* Accu op zp with known contents */
1344 Arg = MakeHexArg (Out->RegA);
1349 if (CE_KnownImm (E) && E->Num == 0xFF) {
1350 /* AND with 0xFF, remove */
1353 } else if (RegValIsKnown (Out->RegA)) {
1354 /* Accu AND zp with known contents */
1355 Arg = MakeHexArg (Out->RegA);
1360 if (CE_KnownImm (E) && E->Num == 0x00) {
1361 /* ORA with zero, remove */
1364 } else if (RegValIsKnown (Out->RegA)) {
1365 /* Accu AND zp with known contents */
1366 Arg = MakeHexArg (Out->RegA);
1375 /* Check if we have to replace the insn by LDA */
1377 CodeEntry* X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1378 CS_InsertEntry (S, X, I+1);
1387 /* Free register info */
1390 /* Return the number of changes made */
1396 /*****************************************************************************/
1397 /* Optimize branch types */
1398 /*****************************************************************************/
1402 unsigned OptBranchDist (CodeSeg* S)
1403 /* Change branches for the distance needed. */
1405 unsigned Changes = 0;
1407 /* Walk over the entries */
1409 while (I < CS_GetEntryCount (S)) {
1411 /* Get next entry */
1412 CodeEntry* E = CS_GetEntry (S, I);
1414 /* Check if it's a conditional branch to a local label. */
1415 if (E->Info & OF_CBRA) {
1417 /* Is this a branch to a local symbol? */
1418 if (E->JumpTo != 0) {
1420 /* Check if the branch distance is short */
1421 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1423 /* Make the branch short/long according to distance */
1424 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1425 /* Short branch but long distance */
1426 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1428 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1429 /* Long branch but short distance */
1430 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1434 } else if ((E->Info & OF_LBRA) == 0) {
1436 /* Short branch to external symbol - make it long */
1437 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1442 } else if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 &&
1443 (E->Info & OF_UBRA) != 0 &&
1445 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1447 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1448 CE_ReplaceOPC (E, OP65_BRA);
1457 /* Return the number of changes made */