1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001 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 /*****************************************************************************/
47 /*****************************************************************************/
48 /* Replace jumps to RTS by RTS */
49 /*****************************************************************************/
53 unsigned OptRTSJumps (CodeSeg* S)
54 /* Replace jumps to RTS by RTS */
58 /* Walk over all entries minus the last one */
60 while (I < CS_GetEntryCount (S)) {
62 /* Get the next entry */
63 CodeEntry* E = CS_GetEntry (S, I);
65 /* Check if it's an unconditional branch to a local target */
66 if ((E->Info & OF_UBRA) != 0 &&
68 E->JumpTo->Owner->OPC == OP65_RTS) {
70 /* Insert an RTS instruction */
71 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
72 CS_InsertEntry (S, X, I+1);
77 /* Remember, we had changes */
87 /* Return the number of changes made */
93 /*****************************************************************************/
94 /* Remove dead jumps */
95 /*****************************************************************************/
99 unsigned OptDeadJumps (CodeSeg* S)
100 /* Remove dead jumps (jumps to the next instruction) */
102 unsigned Changes = 0;
106 /* Get the number of entries, bail out if we have less than two entries */
107 unsigned Count = CS_GetEntryCount (S);
112 /* Walk over all entries minus the last one */
114 while (I < Count-1) {
116 /* Get the next entry */
117 E = CS_GetEntry (S, I);
119 /* Check if it's a branch, if it has a local target, and if the target
120 * is the next instruction.
122 if (E->AM == AM65_BRA && E->JumpTo && E->JumpTo->Owner == CS_GetEntry (S, I+1)) {
124 /* Delete the dead jump */
127 /* Keep the number of entries updated */
130 /* Remember, we had changes */
141 /* Return the number of changes made */
147 /*****************************************************************************/
148 /* Remove dead code */
149 /*****************************************************************************/
153 unsigned OptDeadCode (CodeSeg* S)
154 /* Remove dead code (code that follows an unconditional jump or an rts/rti
158 unsigned Changes = 0;
161 /* Get the number of entries, bail out if we have less than two entries */
162 unsigned Count = CS_GetEntryCount (S);
167 /* Walk over all entries */
174 CodeEntry* E = CS_GetEntry (S, I);
176 /* Check if it's an unconditional branch, and if the next entry has
179 if ((E->Info & OF_DEAD) != 0 &&
180 (N = CS_GetNextEntry (S, I)) != 0 &&
183 /* Delete the next entry */
184 CS_DelEntry (S, I+1);
186 /* Keep the number of entries updated */
189 /* Remember, we had changes */
200 /* Return the number of changes made */
206 /*****************************************************************************/
207 /* Optimize jump cascades */
208 /*****************************************************************************/
212 unsigned OptJumpCascades (CodeSeg* S)
213 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
214 * replaced by a jump to the final location. This will in some cases produce
215 * worse code, because some jump targets are no longer reachable by short
216 * branches, but this is quite rare, so there are more advantages than
220 unsigned Changes = 0;
222 /* Walk over all entries */
224 while (I < CS_GetEntryCount (S)) {
230 CodeEntry* E = CS_GetEntry (S, I);
232 /* Check if it's a branch, if it has a jump label, if this jump
233 * label is not attached to the instruction itself, and if the
234 * target instruction is itself a branch.
236 if ((E->Info & OF_BRA) != 0 &&
237 (OldLabel = E->JumpTo) != 0 &&
238 (N = OldLabel->Owner) != E &&
239 (N->Info & OF_BRA) != 0) {
241 /* Check if we can use the final target label. This is the case,
242 * if the target branch is an absolut branch, or if it is a
243 * conditional branch checking the same condition as the first one.
245 if ((N->Info & OF_UBRA) != 0 ||
246 ((E->Info & OF_CBRA) != 0 &&
247 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
249 /* This is a jump cascade and we may jump to the final target.
250 * Insert a new instruction, then remove the old one
252 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
254 /* Insert it behind E */
255 CS_InsertEntry (S, X, I+1);
260 /* Remember, we had changes */
268 /* Check if both are conditional branches, and the condition of
269 * the second is the inverse of that of the first. In this case,
270 * the second branch will never be taken, and we may jump directly
271 * to the instruction behind this one.
273 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
275 CodeEntry* X; /* Instruction behind N */
276 CodeLabel* LX; /* Label attached to X */
278 /* Get the branch conditions of both branches */
279 bc_t BC1 = GetBranchCond (E->OPC);
280 bc_t BC2 = GetBranchCond (N->OPC);
282 /* Check the branch conditions */
283 if (BC1 != GetInverseCond (BC2)) {
284 /* Condition not met */
288 /* We may jump behind this conditional branch. Get the
289 * pointer to the next instruction
291 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
292 /* N is the last entry, bail out */
296 /* Get the label attached to X, create a new one if needed */
297 LX = CS_GenLabel (S, X);
299 /* Move the reference from E to the new label */
300 CS_MoveLabelRef (S, E, LX);
302 /* Remember, we had changes */
317 /* Return the number of changes made */
323 /*****************************************************************************/
324 /* Optimize jsr/rts */
325 /*****************************************************************************/
329 unsigned OptRTS (CodeSeg* S)
330 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
331 * replaced by a jump. Don't bother to delete the RTS if it does not have a
332 * label, the dead code elimination should take care of it.
335 unsigned Changes = 0;
338 /* Get the number of entries, bail out if we have less than 2 entries */
339 unsigned Count = CS_GetEntryCount (S);
344 /* Walk over all entries minus the last one */
346 while (I < Count-1) {
351 CodeEntry* E = CS_GetEntry (S, I);
353 /* Check if it's a subroutine call and if the following insn is RTS */
354 if (E->OPC == OP65_JSR &&
355 (N = CS_GetNextEntry (S, I)) != 0 &&
356 N->OPC == OP65_RTS) {
358 /* Change the jsr to a jmp and use the additional info for a jump */
360 CE_ReplaceOPC (E, OP65_JMP);
362 /* Remember, we had changes */
372 /* Return the number of changes made */
378 /*****************************************************************************/
379 /* Optimize jump targets */
380 /*****************************************************************************/
384 unsigned OptJumpTarget (CodeSeg* S)
385 /* If the instruction preceeding an unconditional branch is the same as the
386 * instruction preceeding the jump target, the jump target may be moved
387 * one entry back. This is a size optimization, since the instruction before
388 * the branch gets removed.
391 unsigned Changes = 0;
392 CodeEntry* E1; /* Entry 1 */
393 CodeEntry* E2; /* Entry 2 */
394 CodeEntry* T1; /* Jump target entry 1 */
395 CodeEntry* T2; /* Jump target entry 2 */
396 CodeLabel* TL1; /* Target label 1 */
397 unsigned TI; /* Target index */
400 /* Get the number of entries, bail out if we have not enough */
401 unsigned Count = CS_GetEntryCount (S);
406 /* Walk over the entries */
408 while (I < Count-1) {
411 E2 = CS_GetEntry (S, I+1);
413 /* Check if we have a jump or branch, and a matching label */
414 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
416 /* Get the target instruction for the label */
417 T2 = E2->JumpTo->Owner;
419 /* Get the entry preceeding this one (if possible) */
420 TI = CS_GetEntryIndex (S, T2);
422 /* There is no entry before this one */
425 T1 = CS_GetEntry (S, TI-1);
427 /* Get the entry preceeding the jump */
428 E1 = CS_GetEntry (S, I);
430 /* Check if both preceeding instructions are identical */
431 if (!CodeEntriesAreEqual (E1, T1)) {
432 /* Not equal, try next */
436 /* Get the label for the instruction preceeding the jump target.
437 * This routine will create a new label if the instruction does
438 * not already have one.
440 TL1 = CS_GenLabel (S, T1);
442 /* Change the jump target to point to this new label */
443 CS_MoveLabelRef (S, E2, TL1);
445 /* If the instruction preceeding the jump has labels attached,
446 * move references to this label to the new label.
448 if (CE_HasLabel (E1)) {
449 CS_MoveLabels (S, E1, T1);
452 /* Remove the entry preceeding the jump */
456 /* Remember, we had changes */
467 /* Return the number of changes made */
473 /*****************************************************************************/
474 /* Optimize conditional branches */
475 /*****************************************************************************/
479 unsigned OptCondBranches (CodeSeg* S)
480 /* Performs several optimization steps:
482 * - If an immidiate load of a register is followed by a conditional jump that
483 * is never taken because the load of the register sets the flags in such a
484 * manner, remove the conditional branch.
485 * - If the conditional branch is always taken because of the register load,
486 * replace it by a jmp.
487 * - If a conditional branch jumps around an unconditional branch, remove the
488 * conditional branch and make the jump a conditional branch with the
489 * inverse condition of the first one.
492 unsigned Changes = 0;
495 /* Get the number of entries, bail out if we have not enough */
496 unsigned Count = CS_GetEntryCount (S);
501 /* Walk over the entries */
503 while (I < Count-1) {
509 CodeEntry* E = CS_GetEntry (S, I);
511 /* Check if it's a register load */
512 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
513 E->AM == AM65_IMM && /* ..with immidiate addressing */
514 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
515 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
516 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
517 !CE_HasLabel (N)) { /* ..and does not have a label */
519 /* Get the branch condition */
520 bc_t BC = GetBranchCond (N->OPC);
522 /* Check the argument against the branch condition */
523 if ((BC == BC_EQ && E->Num != 0) ||
524 (BC == BC_NE && E->Num == 0) ||
525 (BC == BC_PL && (E->Num & 0x80) != 0) ||
526 (BC == BC_MI && (E->Num & 0x80) == 0)) {
528 /* Remove the conditional branch */
529 CS_DelEntry (S, I+1);
532 /* Remember, we had changes */
535 } else if ((BC == BC_EQ && E->Num == 0) ||
536 (BC == BC_NE && E->Num != 0) ||
537 (BC == BC_PL && (E->Num & 0x80) == 0) ||
538 (BC == BC_MI && (E->Num & 0x80) != 0)) {
540 /* The branch is always taken, replace it by a jump */
541 CE_ReplaceOPC (N, OP65_JMP);
543 /* Remember, we had changes */
549 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
550 (L = E->JumpTo) != 0 && /* ..referencing a local label */
551 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
552 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
553 !CE_HasLabel (N) && /* ..has no label attached */
554 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
556 /* Replace the jump by a conditional branch with the inverse branch
557 * condition than the branch around it.
559 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
561 /* Remove the conditional branch */
565 /* Remember, we had changes */
575 /* Return the number of changes made */
581 /*****************************************************************************/
582 /* Remove unused loads and stores */
583 /*****************************************************************************/
587 unsigned OptUnusedLoads (CodeSeg* S)
588 /* Remove loads of registers where the value loaded is not used later. */
590 unsigned Changes = 0;
592 /* Walk over the entries */
594 while (I < CS_GetEntryCount (S)) {
599 CodeEntry* E = CS_GetEntry (S, I);
601 /* Check if it's a register load or transfer insn */
602 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
603 (N = CS_GetNextEntry (S, I)) != 0 &&
604 (N->Info & OF_FBRA) == 0) {
606 /* Check which sort of load or transfer it is */
613 case OP65_TYA: R = REG_A; break;
617 case OP65_TAX: R = REG_X; break;
621 case OP65_TAY: R = REG_Y; break;
622 default: goto NextEntry; /* OOPS */
625 /* Get register usage and check if the register value is used later */
626 if ((GetRegInfo (S, I+1, R) & R) == 0) {
628 /* Register value is not used, remove the load */
631 /* Remember, we had changes */
643 /* Return the number of changes made */
649 unsigned OptUnusedStores (CodeSeg* S)
650 /* Remove stores into zero page registers that aren't used later */
652 unsigned Changes = 0;
654 /* Walk over the entries */
656 while (I < CS_GetEntryCount (S)) {
659 CodeEntry* E = CS_GetEntry (S, I);
661 /* Check if it's a register load or transfer insn */
662 if ((E->Info & OF_STORE) != 0 &&
664 (E->Chg & REG_ZP) != 0) {
666 /* Check for the zero page location. We know that there cannot be
667 * more than one zero page location involved in the store.
669 unsigned R = E->Chg & REG_ZP;
671 /* Get register usage and check if the register value is used later */
672 if ((GetRegInfo (S, I+1, R) & R) == 0) {
674 /* Register value is not used, remove the load */
677 /* Remember, we had changes */
688 /* Return the number of changes made */
694 unsigned OptDuplicateLoads (CodeSeg* S)
695 /* Remove loads of registers where the value loaded is already in the register. */
697 unsigned Changes = 0;
700 /* Generate register info for this step */
703 /* Walk over the entries */
705 while (I < CS_GetEntryCount (S)) {
710 CodeEntry* E = CS_GetEntry (S, I);
712 /* Assume we won't delete the entry */
715 /* Get a pointer to the input registers of the insn */
716 const RegContents* In = &E->RI->In;
718 /* Handle the different instructions */
722 if (In->RegA >= 0 && /* Value of A is known */
723 CE_KnownImm (E) && /* Value to be loaded is known */
724 In->RegA == (long) E->Num && /* Both are equal */
725 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
726 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
732 if (In->RegX >= 0 && /* Value of X is known */
733 CE_KnownImm (E) && /* Value to be loaded is known */
734 In->RegX == (long) E->Num && /* Both are equal */
735 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
736 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
742 if (In->RegY >= 0 && /* Value of Y is known */
743 CE_KnownImm (E) && /* Value to be loaded is known */
744 In->RegY == (long) E->Num && /* Both are equal */
745 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
746 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
752 /* If we store into a known zero page location, and this
753 * location does already contain the value to be stored,
756 if (In->RegA >= 0 && /* Value of A is known */
757 E->AM == AM65_ZP && /* Store into zp */
758 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
759 In->RegA == In->SRegLo) || /* Value identical */
760 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
761 In->RegA == In->SRegHi))) { /* Value identical */
767 /* If we store into a known zero page location, and this
768 * location does already contain the value to be stored,
771 if (In->RegX >= 0 && /* Value of A is known */
772 E->AM == AM65_ZP && /* Store into zp */
773 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
774 In->RegX == In->SRegLo) || /* Value identical */
775 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
776 In->RegX == In->SRegHi))) { /* Value identical */
779 /* If the value in the X register is known and the same as
780 * that in the A register, replace the store by a STA. The
781 * optimizer will then remove the load instruction for X
782 * later. STX does support the zeropage,y addressing mode,
783 * so be sure to check for that.
785 } else if (In->RegX >= 0 &&
786 In->RegX == In->RegA &&
787 E->AM != AM65_ABSY &&
789 /* Use the A register instead */
790 CE_ReplaceOPC (E, OP65_STA);
795 /* If we store into a known zero page location, and this
796 * location does already contain the value to be stored,
799 if (In->RegX >= 0 && /* Value of A is known */
800 E->AM == AM65_ZP && /* Store into zp */
801 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
802 In->RegX == In->SRegLo) || /* Value identical */
803 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
804 In->RegX == In->SRegHi))) { /* Value identical */
806 /* If the value in the Y register is known and the same as
807 * that in the A register, replace the store by a STA. The
808 * optimizer will then remove the load instruction for Y
809 * later. If replacement by A is not possible try a
810 * replacement by X, but check for invalid addressing modes
813 } else if (In->RegY >= 0) {
814 if (In->RegY == In->RegA) {
815 CE_ReplaceOPC (E, OP65_STA);
816 } else if (In->RegY == In->RegX &&
817 E->AM != AM65_ABSX &&
819 CE_ReplaceOPC (E, OP65_STX);
826 In->RegA == In->RegX &&
827 (N = CS_GetNextEntry (S, I)) != 0 &&
828 (N->Info & OF_FBRA) == 0) {
829 /* Value is identical and not followed by a branch */
836 In->RegA == In->RegY &&
837 (N = CS_GetNextEntry (S, I)) != 0 &&
838 (N->Info & OF_FBRA) == 0) {
839 /* Value is identical and not followed by a branch */
846 In->RegX == In->RegA &&
847 (N = CS_GetNextEntry (S, I)) != 0 &&
848 (N->Info & OF_FBRA) == 0) {
849 /* Value is identical and not followed by a branch */
856 In->RegY == In->RegA &&
857 (N = CS_GetNextEntry (S, I)) != 0 &&
858 (N->Info & OF_FBRA) == 0) {
859 /* Value is identical and not followed by a branch */
869 /* Delete the entry if requested */
872 /* Register value is not used, remove the load */
875 /* Remember, we had changes */
887 /* Free register info */
890 /* Return the number of changes made */
896 unsigned OptStoreLoad (CodeSeg* S)
897 /* Remove a store followed by a load from the same location. */
899 unsigned Changes = 0;
901 /* Walk over the entries */
903 while (I < CS_GetEntryCount (S)) {
909 CodeEntry* E = CS_GetEntry (S, I);
911 /* Check if it is a store instruction followed by a load from the
912 * same address which is itself not followed by a conditional branch.
914 if ((E->Info & OF_STORE) != 0 &&
915 (N = CS_GetNextEntry (S, I)) != 0 &&
917 (N->Info & OF_LOAD) != 0 &&
918 strcmp (E->Arg, N->Arg) == 0 &&
919 (X = CS_GetNextEntry (S, I+1)) != 0 &&
920 (X->Info & OF_FBRA) == 0) {
922 /* Register value is not used, remove the load */
923 CS_DelEntry (S, I+1);
925 /* Remember, we had changes */
935 /* Return the number of changes made */
941 unsigned OptTransfers (CodeSeg* S)
942 /* Remove transfers from one register to another and back */
944 unsigned Changes = 0;
946 /* Walk over the entries */
948 while (I < CS_GetEntryCount (S)) {
955 CodeEntry* E = CS_GetEntry (S, I);
957 /* Check if it is a store instruction followed by a load from the
958 * same address which is itself not followed by a conditional branch.
960 if ((E->Info & OF_XFR) != 0 &&
961 (N = CS_GetNextEntry (S, I)) != 0 &&
963 (N->Info & OF_XFR) != 0) {
965 /* Check if it's a transfer and back */
966 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
967 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
968 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
969 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
971 /* If the next insn is a conditional branch, check if the insn
972 * preceeding the first xfr will set the flags right, otherwise we
973 * may not remove the sequence.
975 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
978 if ((X->Info & OF_FBRA) != 0) {
980 /* No preceeding entry */
983 P = CS_GetEntry (S, I-1);
984 if ((P->Info & OF_SETF) == 0) {
985 /* Does not set the flags */
990 /* Remove both transfers */
991 CS_DelEntry (S, I+1);
994 /* Remember, we had changes */
1005 /* Return the number of changes made */
1011 /*****************************************************************************/
1012 /* Optimize branch types */
1013 /*****************************************************************************/
1017 unsigned OptBranchDist (CodeSeg* S)
1018 /* Change branches for the distance needed. */
1020 unsigned Changes = 0;
1023 /* Get the number of entries, bail out if we have not enough */
1024 unsigned Count = CS_GetEntryCount (S);
1026 /* Walk over the entries */
1030 /* Get next entry */
1031 CodeEntry* E = CS_GetEntry (S, I);
1033 /* Check if it's a conditional branch to a local label. */
1034 if ((E->Info & OF_CBRA) != 0) {
1036 /* Is this a branch to a local symbol? */
1037 if (E->JumpTo != 0) {
1039 /* Get the index of the branch target */
1040 unsigned TI = CS_GetEntryIndex (S, E->JumpTo->Owner);
1042 /* Determine the branch distance */
1045 /* Forward branch */
1048 CodeEntry* N = CS_GetEntry (S, J++);
1049 Distance += N->Size;
1052 /* Backward branch */
1055 CodeEntry* N = CS_GetEntry (S, J++);
1056 Distance += N->Size;
1060 /* Make the branch short/long according to distance */
1061 if ((E->Info & OF_LBRA) == 0 && Distance > 120) {
1062 /* Short branch but long distance */
1063 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1065 } else if ((E->Info & OF_LBRA) != 0 && Distance < 120) {
1066 /* Long branch but short distance */
1067 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1071 } else if ((E->Info & OF_LBRA) == 0) {
1073 /* Short branch to external symbol - make it long */
1074 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1085 /* Return the number of changes made */