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 */
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) == 0) {
628 /* Register value is not used, remove the load */
631 /* Remember, we had changes */
643 /* Return the number of changes made */
649 unsigned OptDuplicateLoads (CodeSeg* S)
650 /* Remove loads of registers where the value loaded is already in the register. */
652 unsigned Changes = 0;
655 /* Generate register info for this step */
658 /* Walk over the entries */
660 while (I < CS_GetEntryCount (S)) {
665 CodeEntry* E = CS_GetEntry (S, I);
667 /* Assume we won't delete the entry */
670 /* Handle the different instructions */
674 if (E->RI->In.RegA >= 0 && /* Value of A is known */
675 CE_KnownImm (E) && /* Value to be loaded is known */
676 E->RI->In.RegA == (long) E->Num && /* Both are equal */
677 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
678 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
684 if (E->RI->In.RegX >= 0 && /* Value of X is known */
685 CE_KnownImm (E) && /* Value to be loaded is known */
686 E->RI->In.RegX == (long) E->Num && /* Both are equal */
687 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
688 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
694 if (E->RI->In.RegY >= 0 && /* Value of Y is known */
695 CE_KnownImm (E) && /* Value to be loaded is known */
696 E->RI->In.RegY == (long) E->Num && /* Both are equal */
697 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
698 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
704 /* If the value in the X register is known and the same as
705 * that in the A register, replace the store by a STA. The
706 * optimizer will then remove the load instruction for X
707 * later. STX does support the zeropage,y addressing mode,
708 * so be sure to check for that.
710 if (E->RI->In.RegX >= 0 &&
711 E->RI->In.RegX == E->RI->In.RegA &&
712 E->AM != AM65_ABSY &&
714 /* Use the A register instead */
715 CE_ReplaceOPC (E, OP65_STA);
720 /* If the value in the Y register is known and the same as
721 * that in the A register, replace the store by a STA. The
722 * optimizer will then remove the load instruction for Y
723 * later. If replacement by A is not possible try a
724 * replacement by X, but check for invalid addressing modes
727 if (E->RI->In.RegY >= 0) {
728 if (E->RI->In.RegY == E->RI->In.RegA) {
729 CE_ReplaceOPC (E, OP65_STA);
730 } else if (E->RI->In.RegY == E->RI->In.RegX &&
731 E->AM != AM65_ABSX &&
733 CE_ReplaceOPC (E, OP65_STX);
739 if (E->RI->In.RegA >= 0 &&
740 E->RI->In.RegA == E->RI->In.RegX &&
741 (N = CS_GetNextEntry (S, I)) != 0 &&
742 (N->Info & OF_FBRA) == 0) {
743 /* Value is identical and not followed by a branch */
749 if (E->RI->In.RegA >= 0 &&
750 E->RI->In.RegA == E->RI->In.RegY &&
751 (N = CS_GetNextEntry (S, I)) != 0 &&
752 (N->Info & OF_FBRA) == 0) {
753 /* Value is identical and not followed by a branch */
759 if (E->RI->In.RegX >= 0 &&
760 E->RI->In.RegX == E->RI->In.RegA &&
761 (N = CS_GetNextEntry (S, I)) != 0 &&
762 (N->Info & OF_FBRA) == 0) {
763 /* Value is identical and not followed by a branch */
769 if (E->RI->In.RegY >= 0 &&
770 E->RI->In.RegY == E->RI->In.RegA &&
771 (N = CS_GetNextEntry (S, I)) != 0 &&
772 (N->Info & OF_FBRA) == 0) {
773 /* Value is identical and not followed by a branch */
783 /* Delete the entry if requested */
786 /* Register value is not used, remove the load */
789 /* Remember, we had changes */
801 /* Free register info */
804 /* Return the number of changes made */
810 unsigned OptStoreLoad (CodeSeg* S)
811 /* Remove a store followed by a load from the same location. */
813 unsigned Changes = 0;
815 /* Walk over the entries */
817 while (I < CS_GetEntryCount (S)) {
823 CodeEntry* E = CS_GetEntry (S, I);
825 /* Check if it is a store instruction followed by a load from the
826 * same address which is itself not followed by a conditional branch.
828 if ((E->Info & OF_STORE) != 0 &&
829 (N = CS_GetNextEntry (S, I)) != 0 &&
831 (N->Info & OF_LOAD) != 0 &&
832 strcmp (E->Arg, N->Arg) == 0 &&
833 (X = CS_GetNextEntry (S, I+1)) != 0 &&
834 (X->Info & OF_FBRA) == 0) {
836 /* Register value is not used, remove the load */
837 CS_DelEntry (S, I+1);
839 /* Remember, we had changes */
849 /* Return the number of changes made */
855 unsigned OptTransfers (CodeSeg* S)
856 /* Remove transfers from one register to another and back */
858 unsigned Changes = 0;
860 /* Walk over the entries */
862 while (I < CS_GetEntryCount (S)) {
869 CodeEntry* E = CS_GetEntry (S, I);
871 /* Check if it is a store instruction followed by a load from the
872 * same address which is itself not followed by a conditional branch.
874 if ((E->Info & OF_XFR) != 0 &&
875 (N = CS_GetNextEntry (S, I)) != 0 &&
877 (N->Info & OF_XFR) != 0) {
879 /* Check if it's a transfer and back */
880 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
881 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
882 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
883 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
885 /* If the next insn is a conditional branch, check if the insn
886 * preceeding the first xfr will set the flags right, otherwise we
887 * may not remove the sequence.
889 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
892 if ((X->Info & OF_FBRA) != 0) {
894 /* No preceeding entry */
897 P = CS_GetEntry (S, I-1);
898 if ((P->Info & OF_SETF) == 0) {
899 /* Does not set the flags */
904 /* Remove both transfers */
905 CS_DelEntry (S, I+1);
908 /* Remember, we had changes */
919 /* Return the number of changes made */
925 /*****************************************************************************/
926 /* Optimize branch types */
927 /*****************************************************************************/
931 unsigned OptBranchDist (CodeSeg* S)
932 /* Change branches for the distance needed. */
934 unsigned Changes = 0;
937 /* Get the number of entries, bail out if we have not enough */
938 unsigned Count = CS_GetEntryCount (S);
940 /* Walk over the entries */
945 CodeEntry* E = CS_GetEntry (S, I);
947 /* Check if it's a conditional branch to a local label. */
948 if ((E->Info & OF_CBRA) != 0) {
950 /* Is this a branch to a local symbol? */
951 if (E->JumpTo != 0) {
953 /* Get the index of the branch target */
954 unsigned TI = CS_GetEntryIndex (S, E->JumpTo->Owner);
956 /* Determine the branch distance */
962 CodeEntry* N = CS_GetEntry (S, J++);
966 /* Backward branch */
969 CodeEntry* N = CS_GetEntry (S, J++);
974 /* Make the branch short/long according to distance */
975 if ((E->Info & OF_LBRA) == 0 && Distance > 120) {
976 /* Short branch but long distance */
977 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
979 } else if ((E->Info & OF_LBRA) != 0 && Distance < 120) {
980 /* Long branch but short distance */
981 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
985 } else if ((E->Info & OF_LBRA) == 0) {
987 /* Short branch to external symbol - make it long */
988 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
999 /* Return the number of changes made */