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 /* Remove dead jumps */
49 /*****************************************************************************/
53 unsigned OptDeadJumps (CodeSeg* S)
54 /* Remove dead jumps (jumps to the next instruction) */
60 /* Get the number of entries, bail out if we have less than two entries */
61 unsigned Count = GetCodeEntryCount (S);
66 /* Walk over all entries minus the last one */
70 /* Get the next entry */
71 E = GetCodeEntry (S, I);
73 /* Check if it's a branch, if it has a local target, and if the target
74 * is the next instruction.
76 if (E->AM == AM_BRA && E->JumpTo && E->JumpTo->Owner == GetCodeEntry (S, I+1)) {
78 /* Delete the dead jump */
81 /* Keep the number of entries updated */
84 /* Remember, we had changes */
95 /* Return the number of changes made */
101 /*****************************************************************************/
102 /* Remove dead code */
103 /*****************************************************************************/
107 unsigned OptDeadCode (CodeSeg* S)
108 /* Remove dead code (code that follows an unconditional jump or an rts/rti
112 unsigned Changes = 0;
115 /* Get the number of entries, bail out if we have less than two entries */
116 unsigned Count = GetCodeEntryCount (S);
121 /* Walk over all entries minus the last one */
123 while (I < Count-1) {
126 CodeEntry* E = GetCodeEntry (S, I);
128 /* Check if it's an unconditional branch, and if the next entry has
131 if ((E->Info & OF_DEAD) != 0 && !CodeEntryHasLabel (GetCodeEntry (S, I+1))) {
133 /* Delete the next entry */
134 DelCodeEntry (S, I+1);
136 /* Keep the number of entries updated */
139 /* Remember, we had changes */
150 /* Return the number of changes made */
156 /*****************************************************************************/
157 /* Optimize jump cascades */
158 /*****************************************************************************/
162 unsigned OptJumpCascades (CodeSeg* S)
163 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
164 * replaced by a jump to the final location. This will in some cases produce
165 * worse code, because some jump targets are no longer reachable by short
166 * branches, but this is quite rare, so there are more advantages than
170 unsigned Changes = 0;
173 /* Get the number of entries, bail out if we have no entries */
174 unsigned Count = GetCodeEntryCount (S);
179 /* Walk over all entries */
184 CodeEntry* E = GetCodeEntry (S, I);
186 /* Check if it's a branch, if it has a jump label, and if this jump
187 * label is not attached to the instruction itself.
189 if ((E->Info & OF_BRA) != 0 && E->JumpTo != 0 && E->JumpTo->Owner != E) {
191 /* Get the label this insn is branching to */
192 CodeLabel* OldLabel = E->JumpTo;
194 /* Get the entry we're branching to */
195 CodeEntry* N = OldLabel->Owner;
197 /* If the entry we're branching to is not itself a branch, it is
198 * not what we're searching for.
200 if ((N->Info & OF_BRA) == 0) {
204 /* Check if we can use the final target label. This is the case,
205 * if the target branch is an absolut branch, or if it is a
206 * conditional branch checking the same condition as the first one.
208 if ((N->Info & OF_UBRA) != 0 ||
209 ((E->Info & OF_CBRA) != 0 &&
210 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
212 /* This is a jump cascade and we may jump to the final target.
213 * If we have a label, move the reference to this label. If
214 * we don't have a label, use the argument instead.
217 /* Move the reference to the new insn */
218 MoveCodeLabelRef (S, E, N->JumpTo);
220 /* Remove the reference to the old label */
221 RemoveCodeLabelRef (S, E);
224 /* Use the new argument */
225 CodeEntrySetArg (E, N->Arg);
227 /* Use the usage information from the new instruction */
231 /* Remember, we had changes */
239 /* Check if both are conditional branches, and the condition of
240 * the second is the inverse of that of the first. In this case,
241 * the second branch will never be taken, and we may jump directly
242 * to the instruction behind this one.
244 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
246 unsigned NI; /* Index of N */
247 CodeEntry* X; /* Instruction behind N */
248 CodeLabel* LX; /* Label attached to X */
250 /* Get the branch conditions of both branches */
251 bc_t BC1 = GetBranchCond (E->OPC);
252 bc_t BC2 = GetBranchCond (N->OPC);
254 /* Check the branch conditions */
255 if (BC1 != GetInverseCond (BC2)) {
256 /* Condition not met */
260 /* We may jump behind this conditional branch. This means that
261 * N may not be the last entry.
263 NI = GetCodeEntryIndex (S, N);
265 /* N is last entry */
269 /* Get the pointer to the next instruction */
270 X = GetCodeEntry (S, NI+1);
272 /* Get the label attached to X, create a new one if needed */
273 LX = GenCodeLabel (S, X);
275 /* Move the reference from E to the new label */
276 MoveCodeLabelRef (S, E, LX);
278 /* Remember, we had changes */
293 /* Return the number of changes made */
299 /*****************************************************************************/
300 /* Optimize jsr/rts */
301 /*****************************************************************************/
305 unsigned OptRTS (CodeSeg* S)
306 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
307 * replaced by a jump. Don't bother to delete the RTS if it does not have a
308 * label, the dead code elimination should take care of it.
311 unsigned Changes = 0;
314 /* Get the number of entries, bail out if we have less than 2 entries */
315 unsigned Count = GetCodeEntryCount (S);
320 /* Walk over all entries minus the last one */
322 while (I < Count-1) {
325 CodeEntry* E = GetCodeEntry (S, I);
327 /* Check if it's a subroutine call and if the following insn is RTS */
328 if (E->OPC == OPC_JSR && GetCodeEntry(S,I+1)->OPC == OPC_RTS) {
330 /* Change the jsr to a jmp and use the additional info for a jump */
333 E->Info = GetOPCInfo (OPC_JMP);
335 /* Remember, we had changes */
345 /* Return the number of changes made */
351 /*****************************************************************************/
352 /* Optimize jump targets */
353 /*****************************************************************************/
357 unsigned OptJumpTarget (CodeSeg* S)
358 /* If the instruction preceeding an unconditional branch is the same as the
359 * instruction preceeding the jump target, the jump target may be moved
360 * one entry back. This is a size optimization, since the instruction before
361 * the branch gets removed.
364 unsigned Changes = 0;
365 CodeEntry* E1; /* Entry 1 */
366 CodeEntry* E2; /* Entry 2 */
367 CodeEntry* T1; /* Jump target entry 1 */
368 CodeEntry* T2; /* Jump target entry 2 */
369 CodeLabel* TL1; /* Target label 1 */
370 unsigned TI; /* Target index */
373 /* Get the number of entries, bail out if we have not enough */
374 unsigned Count = GetCodeEntryCount (S);
379 /* Walk over the entries */
381 while (I < Count-1) {
384 E2 = GetCodeEntry (S, I+1);
386 /* Check if we have a jump or branch, and a matching label */
387 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
389 /* Get the target instruction for the label */
390 T2 = E2->JumpTo->Owner;
392 /* Get the entry preceeding this one (if possible) */
393 TI = GetCodeEntryIndex (S, T2);
395 /* There is no entry before this one */
398 T1 = GetCodeEntry (S, TI-1);
400 /* Get the entry preceeding the jump */
401 E1 = GetCodeEntry (S, I);
403 /* Check if both preceeding instructions are identical */
404 if (!CodeEntriesAreEqual (E1, T1)) {
405 /* Not equal, try next */
409 /* Get the label for the instruction preceeding the jump target.
410 * This routine will create a new label if the instruction does
411 * not already have one.
413 TL1 = GenCodeLabel (S, T1);
415 /* Change the jump target to point to this new label */
416 MoveCodeLabelRef (S, E2, TL1);
418 /* If the instruction preceeding the jump has labels attached,
419 * move references to this label to the new label.
421 if (CodeEntryHasLabel (E1)) {
422 MoveCodeLabels (S, E1, T1);
425 /* Remove the entry preceeding the jump */
429 /* Remember, we had changes */
440 /* Return the number of changes made */
446 /*****************************************************************************/
447 /* Optimize conditional branches */
448 /*****************************************************************************/
452 unsigned OptCondBranches (CodeSeg* S)
453 /* Performs several optimization steps:
455 * - If an immidiate load of a register is followed by a conditional jump that
456 * is never taken because the load of the register sets the flags in such a
457 * manner, remove the conditional branch.
458 * - If the conditional branch is always taken because of the register load,
459 * replace it by a jmp.
460 * - If a conditional branch jumps around an unconditional branch, remove the
461 * conditional branch and make the jump a conditional branch with the
462 * inverse condition of the first one.
465 unsigned Changes = 0;
468 /* Get the number of entries, bail out if we have not enough */
469 unsigned Count = GetCodeEntryCount (S);
474 /* Walk over the entries */
476 while (I < Count-1) {
482 CodeEntry* E = GetCodeEntry (S, I);
484 /* Check if it's a register load */
485 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
486 E->AM == AM_IMM && /* ..with immidiate addressing */
487 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
488 (N = GetNextCodeEntry (S, I)) != 0 && /* There is a following entry */
489 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
490 !CodeEntryHasLabel (N)) { /* ..and does not have a label */
492 /* Get the branch condition */
493 bc_t BC = GetBranchCond (N->OPC);
495 /* Check the argument against the branch condition */
496 if ((BC == BC_EQ && E->Num != 0) ||
497 (BC == BC_NE && E->Num == 0) ||
498 (BC == BC_PL && (E->Num & 0x80) != 0) ||
499 (BC == BC_MI && (E->Num & 0x80) == 0)) {
501 /* Remove the conditional branch */
502 DelCodeEntry (S, I+1);
505 /* Remember, we had changes */
508 } else if ((BC == BC_EQ && E->Num == 0) ||
509 (BC == BC_NE && E->Num != 0) ||
510 (BC == BC_PL && (E->Num & 0x80) == 0) ||
511 (BC == BC_MI && (E->Num & 0x80) != 0)) {
513 /* The branch is always taken, replace it by a jump */
514 ReplaceOPC (N, OPC_JMP);
516 /* Remember, we had changes */
522 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
523 (L = E->JumpTo) != 0 && /* ..referencing a local label */
524 (N = GetNextCodeEntry (S, I)) != 0 && /* There is a following entry */
525 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
526 !CodeEntryHasLabel (N) && /* ..has no label attached */
527 L->Owner == GetNextCodeEntry (S, I+1)) {/* ..and jump target follows */
529 /* Replace the jump by a conditional branch with the inverse branch
530 * condition than the branch around it.
532 ReplaceOPC (N, GetInverseBranch (E->OPC));
534 /* Remove the conditional branch */
538 /* Remember, we had changes */
548 /* Return the number of changes made */
554 /*****************************************************************************/
555 /* Remove unused loads */
556 /*****************************************************************************/
560 unsigned OptUnusedLoads (CodeSeg* S)
561 /* Remove loads of registers where the value loaded is not used later. */
563 unsigned Changes = 0;
566 /* Get the number of entries, bail out if we have not enough */
567 unsigned Count = GetCodeEntryCount (S);
572 /* Walk over the entries */
574 while (I < Count-1) {
577 CodeEntry* E = GetCodeEntry (S, I);
579 /* Check if it's a register load */
580 if ((E->Info & OF_LOAD) != 0) {
584 /* Get the next instruction, it must not be a conditional branch */
585 CodeEntry* N = GetCodeEntry (S, I+1);
586 if ((N->Info & OF_CBRA) != 0) {
590 /* Check which sort of load it is */
592 case OPC_LDA: R = REG_A; break;
593 case OPC_LDX: R = REG_X; break;
594 case OPC_LDY: R = REG_Y; break;
595 default: goto NextEntry; /* OOPS */
598 /* Get register usage and check if the register value is used later */
599 if ((GetRegInfo (S, I+1) & R) == 0) {
601 /* Register value is not used, remove the load */
605 /* Remember, we had changes */
617 /* Return the number of changes made */