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 /*****************************************************************************/
45 /*****************************************************************************/
46 /* Replace jumps to RTS by RTS */
47 /*****************************************************************************/
51 unsigned OptRTSJumps (CodeSeg* S)
52 /* Replace jumps to RTS by RTS */
56 /* Walk over all entries minus the last one */
58 while (I < CS_GetEntryCount (S)) {
60 /* Get the next entry */
61 CodeEntry* E = CS_GetEntry (S, I);
63 /* Check if it's an unconditional branch to a local target */
64 if ((E->Info & OF_UBRA) != 0 &&
66 E->JumpTo->Owner->OPC == OP65_RTS) {
68 /* Insert an RTS instruction */
69 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
70 CS_InsertEntry (S, X, I+1);
75 /* Remember, we had changes */
85 /* Return the number of changes made */
91 /*****************************************************************************/
92 /* Remove dead jumps */
93 /*****************************************************************************/
97 unsigned OptDeadJumps (CodeSeg* S)
98 /* Remove dead jumps (jumps to the next instruction) */
100 unsigned Changes = 0;
104 /* Get the number of entries, bail out if we have less than two entries */
105 unsigned Count = CS_GetEntryCount (S);
110 /* Walk over all entries minus the last one */
112 while (I < Count-1) {
114 /* Get the next entry */
115 E = CS_GetEntry (S, I);
117 /* Check if it's a branch, if it has a local target, and if the target
118 * is the next instruction.
120 if (E->AM == AM65_BRA && E->JumpTo && E->JumpTo->Owner == CS_GetEntry (S, I+1)) {
122 /* Delete the dead jump */
125 /* Keep the number of entries updated */
128 /* Remember, we had changes */
139 /* Return the number of changes made */
145 /*****************************************************************************/
146 /* Remove dead code */
147 /*****************************************************************************/
151 unsigned OptDeadCode (CodeSeg* S)
152 /* Remove dead code (code that follows an unconditional jump or an rts/rti
156 unsigned Changes = 0;
159 /* Get the number of entries, bail out if we have less than two entries */
160 unsigned Count = CS_GetEntryCount (S);
165 /* Walk over all entries */
172 CodeEntry* E = CS_GetEntry (S, I);
174 /* Check if it's an unconditional branch, and if the next entry has
177 if ((E->Info & OF_DEAD) != 0 &&
178 (N = CS_GetNextEntry (S, I)) != 0 &&
181 /* Delete the next entry */
182 CS_DelEntry (S, I+1);
184 /* Keep the number of entries updated */
187 /* Remember, we had changes */
198 /* Return the number of changes made */
204 /*****************************************************************************/
205 /* Optimize jump cascades */
206 /*****************************************************************************/
210 unsigned OptJumpCascades (CodeSeg* S)
211 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
212 * replaced by a jump to the final location. This will in some cases produce
213 * worse code, because some jump targets are no longer reachable by short
214 * branches, but this is quite rare, so there are more advantages than
218 unsigned Changes = 0;
220 /* Walk over all entries */
222 while (I < CS_GetEntryCount (S)) {
228 CodeEntry* E = CS_GetEntry (S, I);
230 /* Check if it's a branch, if it has a jump label, if this jump
231 * label is not attached to the instruction itself, and if the
232 * target instruction is itself a branch.
234 if ((E->Info & OF_BRA) != 0 &&
235 (OldLabel = E->JumpTo) != 0 &&
236 (N = OldLabel->Owner) != E &&
237 (N->Info & OF_BRA) != 0) {
239 /* Check if we can use the final target label. This is the case,
240 * if the target branch is an absolut branch, or if it is a
241 * conditional branch checking the same condition as the first one.
243 if ((N->Info & OF_UBRA) != 0 ||
244 ((E->Info & OF_CBRA) != 0 &&
245 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
247 /* This is a jump cascade and we may jump to the final target.
248 * Insert a new instruction, then remove the old one
250 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
252 /* Insert it behind E */
253 CS_InsertEntry (S, X, I+1);
258 /* Remember, we had changes */
266 /* Check if both are conditional branches, and the condition of
267 * the second is the inverse of that of the first. In this case,
268 * the second branch will never be taken, and we may jump directly
269 * to the instruction behind this one.
271 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
273 CodeEntry* X; /* Instruction behind N */
274 CodeLabel* LX; /* Label attached to X */
276 /* Get the branch conditions of both branches */
277 bc_t BC1 = GetBranchCond (E->OPC);
278 bc_t BC2 = GetBranchCond (N->OPC);
280 /* Check the branch conditions */
281 if (BC1 != GetInverseCond (BC2)) {
282 /* Condition not met */
286 /* We may jump behind this conditional branch. Get the
287 * pointer to the next instruction
289 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
290 /* N is the last entry, bail out */
294 /* Get the label attached to X, create a new one if needed */
295 LX = CS_GenLabel (S, X);
297 /* Move the reference from E to the new label */
298 CS_MoveLabelRef (S, E, LX);
300 /* Remember, we had changes */
315 /* Return the number of changes made */
321 /*****************************************************************************/
322 /* Optimize jsr/rts */
323 /*****************************************************************************/
327 unsigned OptRTS (CodeSeg* S)
328 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
329 * replaced by a jump. Don't bother to delete the RTS if it does not have a
330 * label, the dead code elimination should take care of it.
333 unsigned Changes = 0;
336 /* Get the number of entries, bail out if we have less than 2 entries */
337 unsigned Count = CS_GetEntryCount (S);
342 /* Walk over all entries minus the last one */
344 while (I < Count-1) {
349 CodeEntry* E = CS_GetEntry (S, I);
351 /* Check if it's a subroutine call and if the following insn is RTS */
352 if (E->OPC == OP65_JSR &&
353 (N = CS_GetNextEntry (S, I)) != 0 &&
354 N->OPC == OP65_RTS) {
356 /* Change the jsr to a jmp and use the additional info for a jump */
358 CE_ReplaceOPC (E, OP65_JMP);
360 /* Remember, we had changes */
370 /* Return the number of changes made */
376 /*****************************************************************************/
377 /* Optimize jump targets */
378 /*****************************************************************************/
382 unsigned OptJumpTarget (CodeSeg* S)
383 /* If the instruction preceeding an unconditional branch is the same as the
384 * instruction preceeding the jump target, the jump target may be moved
385 * one entry back. This is a size optimization, since the instruction before
386 * the branch gets removed.
389 unsigned Changes = 0;
390 CodeEntry* E1; /* Entry 1 */
391 CodeEntry* E2; /* Entry 2 */
392 CodeEntry* T1; /* Jump target entry 1 */
393 CodeEntry* T2; /* Jump target entry 2 */
394 CodeLabel* TL1; /* Target label 1 */
395 unsigned TI; /* Target index */
398 /* Get the number of entries, bail out if we have not enough */
399 unsigned Count = CS_GetEntryCount (S);
404 /* Walk over the entries */
406 while (I < Count-1) {
409 E2 = CS_GetEntry (S, I+1);
411 /* Check if we have a jump or branch, and a matching label */
412 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
414 /* Get the target instruction for the label */
415 T2 = E2->JumpTo->Owner;
417 /* Get the entry preceeding this one (if possible) */
418 TI = CS_GetEntryIndex (S, T2);
420 /* There is no entry before this one */
423 T1 = CS_GetEntry (S, TI-1);
425 /* Get the entry preceeding the jump */
426 E1 = CS_GetEntry (S, I);
428 /* Check if both preceeding instructions are identical */
429 if (!CodeEntriesAreEqual (E1, T1)) {
430 /* Not equal, try next */
434 /* Get the label for the instruction preceeding the jump target.
435 * This routine will create a new label if the instruction does
436 * not already have one.
438 TL1 = CS_GenLabel (S, T1);
440 /* Change the jump target to point to this new label */
441 CS_MoveLabelRef (S, E2, TL1);
443 /* If the instruction preceeding the jump has labels attached,
444 * move references to this label to the new label.
446 if (CE_HasLabel (E1)) {
447 CS_MoveLabels (S, E1, T1);
450 /* Remove the entry preceeding the jump */
454 /* Remember, we had changes */
465 /* Return the number of changes made */
471 /*****************************************************************************/
472 /* Optimize conditional branches */
473 /*****************************************************************************/
477 unsigned OptCondBranches (CodeSeg* S)
478 /* Performs several optimization steps:
480 * - If an immidiate load of a register is followed by a conditional jump that
481 * is never taken because the load of the register sets the flags in such a
482 * manner, remove the conditional branch.
483 * - If the conditional branch is always taken because of the register load,
484 * replace it by a jmp.
485 * - If a conditional branch jumps around an unconditional branch, remove the
486 * conditional branch and make the jump a conditional branch with the
487 * inverse condition of the first one.
490 unsigned Changes = 0;
493 /* Get the number of entries, bail out if we have not enough */
494 unsigned Count = CS_GetEntryCount (S);
499 /* Walk over the entries */
501 while (I < Count-1) {
507 CodeEntry* E = CS_GetEntry (S, I);
509 /* Check if it's a register load */
510 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
511 E->AM == AM65_IMM && /* ..with immidiate addressing */
512 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
513 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
514 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
515 !CE_HasLabel (N)) { /* ..and does not have a label */
517 /* Get the branch condition */
518 bc_t BC = GetBranchCond (N->OPC);
520 /* Check the argument against the branch condition */
521 if ((BC == BC_EQ && E->Num != 0) ||
522 (BC == BC_NE && E->Num == 0) ||
523 (BC == BC_PL && (E->Num & 0x80) != 0) ||
524 (BC == BC_MI && (E->Num & 0x80) == 0)) {
526 /* Remove the conditional branch */
527 CS_DelEntry (S, I+1);
530 /* Remember, we had changes */
533 } else if ((BC == BC_EQ && E->Num == 0) ||
534 (BC == BC_NE && E->Num != 0) ||
535 (BC == BC_PL && (E->Num & 0x80) == 0) ||
536 (BC == BC_MI && (E->Num & 0x80) != 0)) {
538 /* The branch is always taken, replace it by a jump */
539 CE_ReplaceOPC (N, OP65_JMP);
541 /* Remember, we had changes */
547 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
548 (L = E->JumpTo) != 0 && /* ..referencing a local label */
549 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
550 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
551 !CE_HasLabel (N) && /* ..has no label attached */
552 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
554 /* Replace the jump by a conditional branch with the inverse branch
555 * condition than the branch around it.
557 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
559 /* Remove the conditional branch */
563 /* Remember, we had changes */
573 /* Return the number of changes made */
579 /*****************************************************************************/
580 /* Remove unused loads and stores */
581 /*****************************************************************************/
585 unsigned OptUnusedLoads (CodeSeg* S)
586 /* Remove loads of registers where the value loaded is not used later. */
588 unsigned Changes = 0;
590 /* Walk over the entries */
592 while (I < CS_GetEntryCount (S)) {
597 CodeEntry* E = CS_GetEntry (S, I);
599 /* Check if it's a register load or transfer insn */
600 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
601 (N = CS_GetNextEntry (S, I)) != 0 &&
602 (N->Info & OF_FBRA) == 0) {
604 /* Check which sort of load or transfer it is */
611 case OP65_TYA: R = REG_A; break;
615 case OP65_TAX: R = REG_X; break;
619 case OP65_TAY: R = REG_Y; break;
620 default: goto NextEntry; /* OOPS */
623 /* Get register usage and check if the register value is used later */
624 if ((GetRegInfo (S, I+1, R) & R) == 0) {
626 /* Register value is not used, remove the load */
629 /* Remember, we had changes */
641 /* Return the number of changes made */
647 unsigned OptUnusedStores (CodeSeg* S)
648 /* Remove stores into zero page registers that aren't used later */
650 unsigned Changes = 0;
652 /* Walk over the entries */
654 while (I < CS_GetEntryCount (S)) {
657 CodeEntry* E = CS_GetEntry (S, I);
659 /* Check if it's a register load or transfer insn */
660 if ((E->Info & OF_STORE) != 0 &&
662 (E->Chg & REG_ZP) != 0) {
664 /* Check for the zero page location. We know that there cannot be
665 * more than one zero page location involved in the store.
667 unsigned R = E->Chg & REG_ZP;
669 /* Get register usage and check if the register value is used later */
670 if ((GetRegInfo (S, I+1, R) & R) == 0) {
672 /* Register value is not used, remove the load */
675 /* Remember, we had changes */
686 /* Return the number of changes made */
692 unsigned OptDupLoads (CodeSeg* S)
693 /* Remove loads of registers where the value loaded is already in the register. */
695 unsigned Changes = 0;
698 /* Generate register info for this step */
701 /* Walk over the entries */
703 while (I < CS_GetEntryCount (S)) {
708 CodeEntry* E = CS_GetEntry (S, I);
710 /* Assume we won't delete the entry */
713 /* Get a pointer to the input registers of the insn */
714 const RegContents* In = &E->RI->In;
716 /* Handle the different instructions */
720 if (In->RegA >= 0 && /* Value of A is known */
721 CE_KnownImm (E) && /* Value to be loaded is known */
722 In->RegA == (long) E->Num && /* Both are equal */
723 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
724 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
730 if (In->RegX >= 0 && /* Value of X is known */
731 CE_KnownImm (E) && /* Value to be loaded is known */
732 In->RegX == (long) E->Num && /* Both are equal */
733 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
734 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
740 if (In->RegY >= 0 && /* Value of Y is known */
741 CE_KnownImm (E) && /* Value to be loaded is known */
742 In->RegY == (long) E->Num && /* Both are equal */
743 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
744 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
750 /* If we store into a known zero page location, and this
751 * location does already contain the value to be stored,
754 if (In->RegA >= 0 && /* Value of A is known */
755 E->AM == AM65_ZP && /* Store into zp */
756 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
757 In->RegA == In->SRegLo) || /* Value identical */
758 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
759 In->RegA == In->SRegHi))) { /* Value identical */
765 /* If we store into a known zero page location, and this
766 * location does already contain the value to be stored,
769 if (In->RegX >= 0 && /* Value of A is known */
770 E->AM == AM65_ZP && /* Store into zp */
771 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
772 In->RegX == In->SRegLo) || /* Value identical */
773 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
774 In->RegX == In->SRegHi))) { /* Value identical */
777 /* If the value in the X register is known and the same as
778 * that in the A register, replace the store by a STA. The
779 * optimizer will then remove the load instruction for X
780 * later. STX does support the zeropage,y addressing mode,
781 * so be sure to check for that.
783 } else if (In->RegX >= 0 &&
784 In->RegX == In->RegA &&
785 E->AM != AM65_ABSY &&
787 /* Use the A register instead */
788 CE_ReplaceOPC (E, OP65_STA);
793 /* If we store into a known zero page location, and this
794 * location does already contain the value to be stored,
797 if (In->RegX >= 0 && /* Value of A is known */
798 E->AM == AM65_ZP && /* Store into zp */
799 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
800 In->RegX == In->SRegLo) || /* Value identical */
801 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
802 In->RegX == In->SRegHi))) { /* Value identical */
804 /* If the value in the Y register is known and the same as
805 * that in the A register, replace the store by a STA. The
806 * optimizer will then remove the load instruction for Y
807 * later. If replacement by A is not possible try a
808 * replacement by X, but check for invalid addressing modes
811 } else if (In->RegY >= 0) {
812 if (In->RegY == In->RegA) {
813 CE_ReplaceOPC (E, OP65_STA);
814 } else if (In->RegY == In->RegX &&
815 E->AM != AM65_ABSX &&
817 CE_ReplaceOPC (E, OP65_STX);
824 In->RegA == In->RegX &&
825 (N = CS_GetNextEntry (S, I)) != 0 &&
826 (N->Info & OF_FBRA) == 0) {
827 /* Value is identical and not followed by a branch */
834 In->RegA == In->RegY &&
835 (N = CS_GetNextEntry (S, I)) != 0 &&
836 (N->Info & OF_FBRA) == 0) {
837 /* Value is identical and not followed by a branch */
844 In->RegX == In->RegA &&
845 (N = CS_GetNextEntry (S, I)) != 0 &&
846 (N->Info & OF_FBRA) == 0) {
847 /* Value is identical and not followed by a branch */
854 In->RegY == In->RegA &&
855 (N = CS_GetNextEntry (S, I)) != 0 &&
856 (N->Info & OF_FBRA) == 0) {
857 /* Value is identical and not followed by a branch */
867 /* Delete the entry if requested */
870 /* Register value is not used, remove the load */
873 /* Remember, we had changes */
885 /* Free register info */
888 /* Return the number of changes made */
894 unsigned OptStoreLoad (CodeSeg* S)
895 /* Remove a store followed by a load from the same location. */
897 unsigned Changes = 0;
899 /* Walk over the entries */
901 while (I < CS_GetEntryCount (S)) {
907 CodeEntry* E = CS_GetEntry (S, I);
909 /* Check if it is a store instruction followed by a load from the
910 * same address which is itself not followed by a conditional branch.
912 if ((E->Info & OF_STORE) != 0 &&
913 (N = CS_GetNextEntry (S, I)) != 0 &&
915 (N->Info & OF_LOAD) != 0 &&
916 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
917 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
918 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
919 strcmp (E->Arg, N->Arg) == 0 &&
920 (X = CS_GetNextEntry (S, I+1)) != 0 &&
921 (X->Info & OF_FBRA) == 0) {
923 /* Register value is not used, remove the load */
924 CS_DelEntry (S, I+1);
926 /* Remember, we had changes */
936 /* Return the number of changes made */
942 unsigned OptTransfers (CodeSeg* S)
943 /* Remove transfers from one register to another and back */
945 unsigned Changes = 0;
947 /* Walk over the entries */
949 while (I < CS_GetEntryCount (S)) {
956 CodeEntry* E = CS_GetEntry (S, I);
958 /* Check if it is a store instruction followed by a load from the
959 * same address which is itself not followed by a conditional branch.
961 if ((E->Info & OF_XFR) != 0 &&
962 (N = CS_GetNextEntry (S, I)) != 0 &&
964 (N->Info & OF_XFR) != 0) {
966 /* Check if it's a transfer and back */
967 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
968 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
969 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
970 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
972 /* If the next insn is a conditional branch, check if the insn
973 * preceeding the first xfr will set the flags right, otherwise we
974 * may not remove the sequence.
976 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
979 if ((X->Info & OF_FBRA) != 0) {
981 /* No preceeding entry */
984 P = CS_GetEntry (S, I-1);
985 if ((P->Info & OF_SETF) == 0) {
986 /* Does not set the flags */
991 /* Remove both transfers */
992 CS_DelEntry (S, I+1);
995 /* Remember, we had changes */
1006 /* Return the number of changes made */
1012 /*****************************************************************************/
1013 /* Optimize branch types */
1014 /*****************************************************************************/
1018 unsigned OptBranchDist (CodeSeg* S)
1019 /* Change branches for the distance needed. */
1021 unsigned Changes = 0;
1024 /* Get the number of entries, bail out if we have not enough */
1025 unsigned Count = CS_GetEntryCount (S);
1027 /* Walk over the entries */
1031 /* Get next entry */
1032 CodeEntry* E = CS_GetEntry (S, I);
1034 /* Check if it's a conditional branch to a local label. */
1035 if ((E->Info & OF_CBRA) != 0) {
1037 /* Is this a branch to a local symbol? */
1038 if (E->JumpTo != 0) {
1040 /* Get the index of the branch target */
1041 unsigned TI = CS_GetEntryIndex (S, E->JumpTo->Owner);
1043 /* Determine the branch distance */
1046 /* Forward branch */
1049 CodeEntry* N = CS_GetEntry (S, J++);
1050 Distance += N->Size;
1053 /* Backward branch */
1056 CodeEntry* N = CS_GetEntry (S, J++);
1057 Distance += N->Size;
1061 /* Make the branch short/long according to distance */
1062 if ((E->Info & OF_LBRA) == 0 && Distance > 120) {
1063 /* Short branch but long distance */
1064 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1066 } else if ((E->Info & OF_LBRA) != 0 && Distance < 120) {
1067 /* Long branch but short distance */
1068 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1072 } else if ((E->Info & OF_LBRA) == 0) {
1074 /* Short branch to external symbol - make it long */
1075 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1086 /* Return the number of changes made */