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 /*****************************************************************************/
49 /*****************************************************************************/
53 /* Macro to increment and decrement register contents if they're valid */
54 #define INC(reg,val) if ((reg) >= 0) (reg) = ((reg) + val) & 0xFF
55 #define DEC(reg,val) if ((reg) >= 0) (reg) = ((reg) - val) & 0xFF
59 /*****************************************************************************/
61 /*****************************************************************************/
65 static int IsKnownImm (const CodeEntry* E)
66 /* Return true if the argument of E is a known immediate value */
68 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
73 /*****************************************************************************/
74 /* Replace jumps to RTS by RTS */
75 /*****************************************************************************/
79 unsigned OptRTSJumps (CodeSeg* S)
80 /* Replace jumps to RTS by RTS */
84 /* Walk over all entries minus the last one */
86 while (I < CS_GetEntryCount (S)) {
88 /* Get the next entry */
89 CodeEntry* E = CS_GetEntry (S, I);
91 /* Check if it's an unconditional branch to a local target */
92 if ((E->Info & OF_UBRA) != 0 &&
94 E->JumpTo->Owner->OPC == OP65_RTS) {
96 /* Insert an RTS instruction */
97 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
98 CS_InsertEntry (S, X, I+1);
100 /* Delete the jump */
103 /* Remember, we had changes */
113 /* Return the number of changes made */
119 /*****************************************************************************/
120 /* Remove dead jumps */
121 /*****************************************************************************/
125 unsigned OptDeadJumps (CodeSeg* S)
126 /* Remove dead jumps (jumps to the next instruction) */
128 unsigned Changes = 0;
132 /* Get the number of entries, bail out if we have less than two entries */
133 unsigned Count = CS_GetEntryCount (S);
138 /* Walk over all entries minus the last one */
140 while (I < Count-1) {
142 /* Get the next entry */
143 E = CS_GetEntry (S, I);
145 /* Check if it's a branch, if it has a local target, and if the target
146 * is the next instruction.
148 if (E->AM == AM65_BRA && E->JumpTo && E->JumpTo->Owner == CS_GetEntry (S, I+1)) {
150 /* Delete the dead jump */
153 /* Keep the number of entries updated */
156 /* Remember, we had changes */
167 /* Return the number of changes made */
173 /*****************************************************************************/
174 /* Remove dead code */
175 /*****************************************************************************/
179 unsigned OptDeadCode (CodeSeg* S)
180 /* Remove dead code (code that follows an unconditional jump or an rts/rti
184 unsigned Changes = 0;
187 /* Get the number of entries, bail out if we have less than two entries */
188 unsigned Count = CS_GetEntryCount (S);
193 /* Walk over all entries */
200 CodeEntry* E = CS_GetEntry (S, I);
202 /* Check if it's an unconditional branch, and if the next entry has
205 if ((E->Info & OF_DEAD) != 0 &&
206 (N = CS_GetNextEntry (S, I)) != 0 &&
209 /* Delete the next entry */
210 CS_DelEntry (S, I+1);
212 /* Keep the number of entries updated */
215 /* Remember, we had changes */
226 /* Return the number of changes made */
232 /*****************************************************************************/
233 /* Optimize jump cascades */
234 /*****************************************************************************/
238 unsigned OptJumpCascades (CodeSeg* S)
239 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
240 * replaced by a jump to the final location. This will in some cases produce
241 * worse code, because some jump targets are no longer reachable by short
242 * branches, but this is quite rare, so there are more advantages than
246 unsigned Changes = 0;
248 /* Walk over all entries */
250 while (I < CS_GetEntryCount (S)) {
256 CodeEntry* E = CS_GetEntry (S, I);
258 /* Check if it's a branch, if it has a jump label, if this jump
259 * label is not attached to the instruction itself, and if the
260 * target instruction is itself a branch.
262 if ((E->Info & OF_BRA) != 0 &&
263 (OldLabel = E->JumpTo) != 0 &&
264 (N = OldLabel->Owner) != E &&
265 (N->Info & OF_BRA) != 0) {
267 /* Check if we can use the final target label. This is the case,
268 * if the target branch is an absolut branch, or if it is a
269 * conditional branch checking the same condition as the first one.
271 if ((N->Info & OF_UBRA) != 0 ||
272 ((E->Info & OF_CBRA) != 0 &&
273 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
275 /* This is a jump cascade and we may jump to the final target.
276 * Insert a new instruction, then remove the old one
278 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
280 /* Insert it behind E */
281 CS_InsertEntry (S, X, I+1);
286 /* Remember, we had changes */
294 /* Check if both are conditional branches, and the condition of
295 * the second is the inverse of that of the first. In this case,
296 * the second branch will never be taken, and we may jump directly
297 * to the instruction behind this one.
299 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
301 CodeEntry* X; /* Instruction behind N */
302 CodeLabel* LX; /* Label attached to X */
304 /* Get the branch conditions of both branches */
305 bc_t BC1 = GetBranchCond (E->OPC);
306 bc_t BC2 = GetBranchCond (N->OPC);
308 /* Check the branch conditions */
309 if (BC1 != GetInverseCond (BC2)) {
310 /* Condition not met */
314 /* We may jump behind this conditional branch. Get the
315 * pointer to the next instruction
317 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
318 /* N is the last entry, bail out */
322 /* Get the label attached to X, create a new one if needed */
323 LX = CS_GenLabel (S, X);
325 /* Move the reference from E to the new label */
326 CS_MoveLabelRef (S, E, LX);
328 /* Remember, we had changes */
343 /* Return the number of changes made */
349 /*****************************************************************************/
350 /* Optimize jsr/rts */
351 /*****************************************************************************/
355 unsigned OptRTS (CodeSeg* S)
356 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
357 * replaced by a jump. Don't bother to delete the RTS if it does not have a
358 * label, the dead code elimination should take care of it.
361 unsigned Changes = 0;
364 /* Get the number of entries, bail out if we have less than 2 entries */
365 unsigned Count = CS_GetEntryCount (S);
370 /* Walk over all entries minus the last one */
372 while (I < Count-1) {
377 CodeEntry* E = CS_GetEntry (S, I);
379 /* Check if it's a subroutine call and if the following insn is RTS */
380 if (E->OPC == OP65_JSR &&
381 (N = CS_GetNextEntry (S, I)) != 0 &&
382 N->OPC == OP65_RTS) {
384 /* Change the jsr to a jmp and use the additional info for a jump */
386 CE_ReplaceOPC (E, OP65_JMP);
388 /* Remember, we had changes */
398 /* Return the number of changes made */
404 /*****************************************************************************/
405 /* Optimize jump targets */
406 /*****************************************************************************/
410 unsigned OptJumpTarget (CodeSeg* S)
411 /* If the instruction preceeding an unconditional branch is the same as the
412 * instruction preceeding the jump target, the jump target may be moved
413 * one entry back. This is a size optimization, since the instruction before
414 * the branch gets removed.
417 unsigned Changes = 0;
418 CodeEntry* E1; /* Entry 1 */
419 CodeEntry* E2; /* Entry 2 */
420 CodeEntry* T1; /* Jump target entry 1 */
421 CodeEntry* T2; /* Jump target entry 2 */
422 CodeLabel* TL1; /* Target label 1 */
423 unsigned TI; /* Target index */
426 /* Get the number of entries, bail out if we have not enough */
427 unsigned Count = CS_GetEntryCount (S);
432 /* Walk over the entries */
434 while (I < Count-1) {
437 E2 = CS_GetEntry (S, I+1);
439 /* Check if we have a jump or branch, and a matching label */
440 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
442 /* Get the target instruction for the label */
443 T2 = E2->JumpTo->Owner;
445 /* Get the entry preceeding this one (if possible) */
446 TI = CS_GetEntryIndex (S, T2);
448 /* There is no entry before this one */
451 T1 = CS_GetEntry (S, TI-1);
453 /* Get the entry preceeding the jump */
454 E1 = CS_GetEntry (S, I);
456 /* Check if both preceeding instructions are identical */
457 if (!CodeEntriesAreEqual (E1, T1)) {
458 /* Not equal, try next */
462 /* Get the label for the instruction preceeding the jump target.
463 * This routine will create a new label if the instruction does
464 * not already have one.
466 TL1 = CS_GenLabel (S, T1);
468 /* Change the jump target to point to this new label */
469 CS_MoveLabelRef (S, E2, TL1);
471 /* If the instruction preceeding the jump has labels attached,
472 * move references to this label to the new label.
474 if (CE_HasLabel (E1)) {
475 CS_MoveLabels (S, E1, T1);
478 /* Remove the entry preceeding the jump */
482 /* Remember, we had changes */
493 /* Return the number of changes made */
499 /*****************************************************************************/
500 /* Optimize conditional branches */
501 /*****************************************************************************/
505 unsigned OptCondBranches (CodeSeg* S)
506 /* Performs several optimization steps:
508 * - If an immidiate load of a register is followed by a conditional jump that
509 * is never taken because the load of the register sets the flags in such a
510 * manner, remove the conditional branch.
511 * - If the conditional branch is always taken because of the register load,
512 * replace it by a jmp.
513 * - If a conditional branch jumps around an unconditional branch, remove the
514 * conditional branch and make the jump a conditional branch with the
515 * inverse condition of the first one.
518 unsigned Changes = 0;
521 /* Get the number of entries, bail out if we have not enough */
522 unsigned Count = CS_GetEntryCount (S);
527 /* Walk over the entries */
529 while (I < Count-1) {
535 CodeEntry* E = CS_GetEntry (S, I);
537 /* Check if it's a register load */
538 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
539 E->AM == AM65_IMM && /* ..with immidiate addressing */
540 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
541 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
542 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
543 !CE_HasLabel (N)) { /* ..and does not have a label */
545 /* Get the branch condition */
546 bc_t BC = GetBranchCond (N->OPC);
548 /* Check the argument against the branch condition */
549 if ((BC == BC_EQ && E->Num != 0) ||
550 (BC == BC_NE && E->Num == 0) ||
551 (BC == BC_PL && (E->Num & 0x80) != 0) ||
552 (BC == BC_MI && (E->Num & 0x80) == 0)) {
554 /* Remove the conditional branch */
555 CS_DelEntry (S, I+1);
558 /* Remember, we had changes */
561 } else if ((BC == BC_EQ && E->Num == 0) ||
562 (BC == BC_NE && E->Num != 0) ||
563 (BC == BC_PL && (E->Num & 0x80) == 0) ||
564 (BC == BC_MI && (E->Num & 0x80) != 0)) {
566 /* The branch is always taken, replace it by a jump */
567 CE_ReplaceOPC (N, OP65_JMP);
569 /* Remember, we had changes */
575 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
576 (L = E->JumpTo) != 0 && /* ..referencing a local label */
577 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
578 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
579 !CE_HasLabel (N) && /* ..has no label attached */
580 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
582 /* Replace the jump by a conditional branch with the inverse branch
583 * condition than the branch around it.
585 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
587 /* Remove the conditional branch */
591 /* Remember, we had changes */
601 /* Return the number of changes made */
607 /*****************************************************************************/
608 /* Remove unused loads */
609 /*****************************************************************************/
613 unsigned OptUnusedLoads (CodeSeg* S)
614 /* Remove loads of registers where the value loaded is not used later. */
616 unsigned Changes = 0;
618 /* Walk over the entries */
620 while (I < CS_GetEntryCount (S)) {
625 CodeEntry* E = CS_GetEntry (S, I);
627 /* Check if it's a register load or transfer insn */
628 if ((E->Info & (OF_LOAD | OF_XFR)) != 0 &&
629 (N = CS_GetNextEntry (S, I)) != 0 &&
630 (N->Info & OF_FBRA) == 0) {
632 /* Check which sort of load or transfer it is */
637 case OP65_LDA: R = REG_A; break;
639 case OP65_LDX: R = REG_X; break;
641 case OP65_LDY: R = REG_Y; break;
642 default: goto NextEntry; /* OOPS */
645 /* Get register usage and check if the register value is used later */
646 if ((GetRegInfo (S, I+1) & R) == 0) {
648 /* Register value is not used, remove the load */
651 /* Remember, we had changes */
663 /* Return the number of changes made */
669 unsigned OptDuplicateLoads (CodeSeg* S)
670 /* Remove loads of registers where the value loaded is already in the register. */
672 unsigned Changes = 0;
674 /* Remember the last load instructions for all registers */
679 /* Walk over the entries */
681 while (I < CS_GetEntryCount (S)) {
683 unsigned char Use, Chg;
687 CodeEntry* E = CS_GetEntry (S, I);
689 /* Assume we won't delete the entry */
692 /* If this entry has a label attached, remove all knownledge about the
693 * register contents. This may be improved but for now it's ok.
695 if (CE_HasLabel (E)) {
696 RegA = RegX = RegY = -1;
699 /* Handle the different instructions */
703 /* We don't know the value of the carry, so the result is
711 if (IsKnownImm (E)) {
712 RegA &= (int) E->Num;
721 RegA = (RegA << 1) & 0xFF;
796 if (IsKnownImm (E)) {
797 RegA ^= (int) E->Num;
841 /* Get the code info for the function */
842 GetFuncInfo (E->Arg, &Use, &Chg);
861 if (IsKnownImm (E)) {
862 N = CS_GetNextEntry (S, I);
863 if (RegA >= 0 && RegA == E->Num && N && (N->Info & OF_FBRA) == 0) {
866 RegA = (unsigned char) E->Num;
869 /* A is now unknown */
875 if (IsKnownImm (E)) {
876 N = CS_GetNextEntry (S, I);
877 if (RegX >= 0 && RegX == E->Num && N && (N->Info & OF_FBRA) == 0) {
880 RegX = (unsigned char) E->Num;
883 /* X is now unknown */
889 if (IsKnownImm (E)) {
890 N = CS_GetNextEntry (S, I);
891 if (RegY >= 0 && RegY == E->Num && N && (N->Info & OF_FBRA) == 0) {
894 RegY = (unsigned char) E->Num;
897 /* Y is now unknown */
904 RegA = (RegA >> 1) & 0xFF;
913 if (IsKnownImm (E)) {
914 RegA |= (unsigned char) E->Num;
916 /* A is now unknown */
980 /* If the value in the X register is known and the same as
981 * that in the A register, replace the store by a STA. The
982 * optimizer will then remove the load instruction for X
983 * later. STX does support the zeropage,y addressing mode,
984 * so be sure to check for that.
986 if (RegX >= 0 && RegX == RegA &&
987 E->AM != AM65_ABSY && E->AM != AM65_ZPY) {
988 /* Use the A register instead */
989 CE_ReplaceOPC (E, OP65_STA);
994 /* If the value in the Y register is known and the same as
995 * that in the A register, replace the store by a STA. The
996 * optimizer will then remove the load instruction for Y
997 * later. If replacement by A is not possible try a
998 * replacement by X, but check for invalid addressing modes
1003 CE_ReplaceOPC (E, OP65_STA);
1004 } else if (RegY == RegX && E->AM != AM65_ABSX && E->AM != AM65_ZPX) {
1005 CE_ReplaceOPC (E, OP65_STX);
1011 N = CS_GetNextEntry (S, I);
1012 if (RegA >= 0 && RegA == RegX && N && (N->Info & OF_FBRA) == 0) {
1013 /* Value is identical and not followed by a branch */
1021 N = CS_GetNextEntry (S, I);
1022 if (RegA >= 0 && RegA == RegY && N && (N->Info & OF_FBRA) == 0) {
1023 /* Value is identical and not followed by a branch */
1041 N = CS_GetNextEntry (S, I);
1042 if (RegX >= 0 && RegX == RegA && N && (N->Info & OF_FBRA) == 0) {
1043 /* Value is identical and not followed by a branch */
1054 N = CS_GetNextEntry (S, I);
1055 if (RegY >= 0 && RegY == RegA && N && (N->Info & OF_FBRA) == 0) {
1056 /* Value is identical and not followed by a branch */
1068 /* Delete the entry if requested */
1071 /* Register value is not used, remove the load */
1074 /* Remember, we had changes */
1086 /* Return the number of changes made */
1092 unsigned OptStoreLoad (CodeSeg* S)
1093 /* Remove a store followed by a load from the same location. */
1095 unsigned Changes = 0;
1097 /* Walk over the entries */
1099 while (I < CS_GetEntryCount (S)) {
1104 /* Get next entry */
1105 CodeEntry* E = CS_GetEntry (S, I);
1107 /* Check if it is a store instruction followed by a load from the
1108 * same address which is itself not followed by a conditional branch.
1110 if ((E->Info & OF_STORE) != 0 &&
1111 (N = CS_GetNextEntry (S, I)) != 0 &&
1112 (N->Info & OF_LOAD) != 0 &&
1113 strcmp (E->Arg, N->Arg) == 0 &&
1114 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1115 (X->Info & OF_FBRA) == 0) {
1117 /* Register value is not used, remove the load */
1118 CS_DelEntry (S, I+1);
1120 /* Remember, we had changes */
1130 /* Return the number of changes made */
1136 /*****************************************************************************/
1137 /* Optimize branch types */
1138 /*****************************************************************************/
1142 unsigned OptBranchDist (CodeSeg* S)
1143 /* Change branches for the distance needed. */
1145 unsigned Changes = 0;
1148 /* Get the number of entries, bail out if we have not enough */
1149 unsigned Count = CS_GetEntryCount (S);
1151 /* Walk over the entries */
1155 /* Get next entry */
1156 CodeEntry* E = CS_GetEntry (S, I);
1158 /* Check if it's a conditional branch to a local label. */
1159 if ((E->Info & OF_CBRA) != 0) {
1161 /* Is this a branch to a local symbol? */
1162 if (E->JumpTo != 0) {
1164 /* Get the index of the branch target */
1165 unsigned TI = CS_GetEntryIndex (S, E->JumpTo->Owner);
1167 /* Determine the branch distance */
1170 /* Forward branch */
1173 CodeEntry* N = CS_GetEntry (S, J++);
1174 Distance += N->Size;
1177 /* Backward branch */
1180 CodeEntry* N = CS_GetEntry (S, J++);
1181 Distance += N->Size;
1185 /* Make the branch short/long according to distance */
1186 if ((E->Info & OF_LBRA) == 0 && Distance > 120) {
1187 /* Short branch but long distance */
1188 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1190 } else if ((E->Info & OF_LBRA) != 0 && Distance < 120) {
1191 /* Long branch but short distance */
1192 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1196 } else if ((E->Info & OF_LBRA) == 0) {
1198 /* Short branch to external symbol - make it long */
1199 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1210 /* Return the number of changes made */