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.
254 /* Return the number of changes made */
260 /*****************************************************************************/
261 /* Optimize jsr/rts */
262 /*****************************************************************************/
266 unsigned OptRTS (CodeSeg* S)
267 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
268 * replaced by a jump. Don't bother to delete the RTS if it does not have a
269 * label, the dead code elimination should take care of it.
272 unsigned Changes = 0;
275 /* Get the number of entries, bail out if we have less than 2 entries */
276 unsigned Count = GetCodeEntryCount (S);
281 /* Walk over all entries minus the last one */
283 while (I < Count-1) {
286 CodeEntry* E = GetCodeEntry (S, I);
288 /* Check if it's a subroutine call and if the following insn is RTS */
289 if (E->OPC == OPC_JSR && GetCodeEntry(S,I+1)->OPC == OPC_RTS) {
291 /* Change the jsr to a jmp and use the additional info for a jump */
294 E->Info = GetOPCInfo (OPC_JMP);
296 /* Remember, we had changes */
306 /* Return the number of changes made */
312 /*****************************************************************************/
313 /* Optimize jump targets */
314 /*****************************************************************************/
318 unsigned OptJumpTarget (CodeSeg* S)
319 /* If the instruction preceeding an unconditional branch is the same as the
320 * instruction preceeding the jump target, the jump target may be moved
321 * one entry back. This is a size optimization, since the instruction before
322 * the branch gets removed.
325 unsigned Changes = 0;
326 CodeEntry* E1; /* Entry 1 */
327 CodeEntry* E2; /* Entry 2 */
328 CodeEntry* T1; /* Jump target entry 1 */
329 CodeEntry* T2; /* Jump target entry 2 */
330 CodeLabel* TL1; /* Target label 1 */
331 unsigned TI; /* Target index */
334 /* Get the number of entries, bail out if we have not enough */
335 unsigned Count = GetCodeEntryCount (S);
340 /* Walk over the entries */
342 while (I < Count-1) {
345 E2 = GetCodeEntry (S, I+1);
347 /* Check if we have a jump or branch, and a matching label */
348 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
350 /* Get the target instruction for the label */
351 T2 = E2->JumpTo->Owner;
353 /* Get the entry preceeding this one (if possible) */
354 TI = GetCodeEntryIndex (S, T2);
356 /* There is no entry before this one */
359 T1 = GetCodeEntry (S, TI-1);
361 /* Get the entry preceeding the jump */
362 E1 = GetCodeEntry (S, I);
364 /* Check if both preceeding instructions are identical */
365 if (!CodeEntriesAreEqual (E1, T1)) {
366 /* Not equal, try next */
370 /* Get the label for the instruction preceeding the jump target.
371 * This routine will create a new label if the instruction does
372 * not already have one.
374 TL1 = GenCodeLabel (S, T1);
376 /* Change the jump target to point to this new label */
377 MoveCodeLabelRef (S, E2, TL1);
379 /* If the instruction preceeding the jump has labels attached,
380 * move references to this label to the new label.
382 if (CodeEntryHasLabel (E1)) {
383 MoveCodeLabels (S, E1, T1);
386 /* Remove the entry preceeding the jump */
390 /* Remember, we had changes */
401 /* Return the number of changes made */
407 /*****************************************************************************/
408 /* Remove conditional jumps never taken */
409 /*****************************************************************************/
413 unsigned OptDeadCondBranches (CodeSeg* S)
414 /* If an immidiate load of a register is followed by a conditional jump that
415 * is never taken because the load of the register sets the flags in such a
416 * manner, remove the conditional branch.
419 unsigned Changes = 0;
422 /* Get the number of entries, bail out if we have not enough */
423 unsigned Count = GetCodeEntryCount (S);
428 /* Walk over the entries */
430 while (I < Count-1) {
433 CodeEntry* E = GetCodeEntry (S, I);
435 /* Check if it's a register load */
436 if ((E->Info & OF_LOAD) != 0 && E->AM == AM_IMM && (E->Flags & CEF_NUMARG) != 0) {
440 /* Immidiate register load, get next instruction */
441 CodeEntry* N = GetCodeEntry (S, I+1);
443 /* Check if the following insn is a conditional branch or if it
444 * has a label attached.
446 if ((N->Info & OF_CBRA) == 0 || CodeEntryHasLabel (E)) {
447 /* No conditional jump or label attached, bail out */
451 /* Get the branch condition */
452 BC = GetBranchCond (N->OPC);
454 /* Check the argument against the branch condition */
455 if ((BC == BC_EQ && E->Num != 0) ||
456 (BC == BC_NE && E->Num == 0) ||
457 (BC == BC_PL && (E->Num & 0x80) != 0) ||
458 (BC == BC_MI && (E->Num & 0x80) == 0)) {
460 /* Remove the conditional branch */
461 DelCodeEntry (S, I+1);
464 /* Remember, we had changes */
476 /* Return the number of changes made */