1 /*****************************************************************************/
5 /* Optimizer subroutines */
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 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* Counter for the number of changes in one run. The optimizer process is
55 * repeated until there are no more changes.
57 static unsigned OptChanges;
61 /*****************************************************************************/
62 /* Remove dead jumps */
63 /*****************************************************************************/
67 static void OptDeadJumps (CodeSeg* S)
68 /* Remove dead jumps (jumps to the next instruction) */
73 /* Get the number of entries, bail out if we have less than two entries */
74 unsigned Count = GetCodeEntryCount (S);
79 /* Walk over all entries minus the last one */
83 /* Get the next entry */
84 E = GetCodeEntry (S, I);
86 /* Check if it's a branch, if it has a local target, and if the target
87 * is the next instruction.
89 if (E->AM == AM_BRA && E->JumpTo && E->JumpTo->Owner == GetCodeEntry (S, I+1)) {
91 /* Delete the dead jump */
94 /* Keep the number of entries updated */
97 /* Remember, we had changes */
111 /*****************************************************************************/
112 /* Remove dead code */
113 /*****************************************************************************/
117 static void OptDeadCode (CodeSeg* S)
118 /* Remove dead code (code that follows an unconditional jump or an rts/rti
124 /* Get the number of entries, bail out if we have less than two entries */
125 unsigned Count = GetCodeEntryCount (S);
130 /* Walk over all entries minus the last one */
132 while (I < Count-1) {
135 CodeEntry* E = GetCodeEntry (S, I);
137 /* Check if it's an unconditional branch, and if the next entry has
140 if ((E->Info & OF_DEAD) != 0 && !CodeEntryHasLabel (GetCodeEntry (S, I+1))) {
142 /* Delete the next entry */
143 DelCodeEntry (S, I+1);
145 /* Keep the number of entries updated */
148 /* Remember, we had changes */
162 /*****************************************************************************/
163 /* Optimize jump cascades */
164 /*****************************************************************************/
168 static void OptJumpCascades (CodeSeg* S)
169 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
170 * replaced by a jump to the final location. This will in some cases produce
171 * worse code, because some jump targets are no longer reachable by short
172 * branches, but this is quite rare, so there are more advantages than
178 /* Get the number of entries, bail out if we have no entries */
179 unsigned Count = GetCodeEntryCount (S);
184 /* Walk over all entries */
189 CodeEntry* E = GetCodeEntry (S, I);
191 /* Check if it's a branch, if it has a jump label, and if this jump
192 * label is not attached to the instruction itself.
194 if ((E->Info & OF_BRA) != 0 && E->JumpTo != 0 && E->JumpTo->Owner != E) {
196 /* Get the label this insn is branching to */
197 CodeLabel* OldLabel = E->JumpTo;
199 /* Get the entry we're branching to */
200 CodeEntry* N = OldLabel->Owner;
202 /* If the entry we're branching to is not itself a branch, it is
203 * not what we're searching for.
205 if ((N->Info & OF_BRA) == 0) {
209 /* Check if we can use the final target label. This is the case,
210 * if the target branch is an absolut branch, or if it is a
211 * conditional branch checking the same condition as the first one.
213 if ((N->Info & OF_UBRA) != 0 ||
214 ((E->Info & OF_CBRA) != 0 &&
215 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
217 /* This is a jump cascade and we may jump to the final target.
218 * If we have a label, move the reference to this label. If
219 * we don't have a label, use the argument instead.
222 /* Move the reference to the new insn */
223 MoveCodeLabelRef (S, E, N->JumpTo);
225 /* Remove the reference to the old label */
226 RemoveCodeLabelRef (S, E);
229 /* Use the new argument */
230 CodeEntrySetArg (E, N->Arg);
232 /* Use the usage information from the new instruction */
236 /* Remember, we had changes */
244 /* Check if both are conditional branches, and the condition of
245 * the second is the inverse of that of the first. In this case,
246 * the second branch will never be taken, and we may jump directly
247 * to the instruction behind this one.
262 /*****************************************************************************/
263 /* Optimize jsr/rts */
264 /*****************************************************************************/
268 static void OptRTS (CodeSeg* S)
269 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
270 * replaced by a jump. Don't bother to delete the RTS if it does not have a
271 * label, the dead code elimination should take care of it.
276 /* Get the number of entries, bail out if we have less than 2 entries */
277 unsigned Count = GetCodeEntryCount (S);
282 /* Walk over all entries minus the last one */
284 while (I < Count-1) {
287 CodeEntry* E = GetCodeEntry (S, I);
289 /* Check if it's a subroutine call and if the following insn is RTS */
290 if (E->OPC == OPC_JSR && GetCodeEntry(S,I+1)->OPC == OPC_RTS) {
292 /* Change the jsr to a jmp and use the additional info for a jump */
295 E->Info = GetOPCInfo (OPC_JMP);
297 /* Remember, we had changes */
310 /*****************************************************************************/
311 /* Optimize jump targets */
312 /*****************************************************************************/
316 static void OptJumpTarget (CodeSeg* S)
317 /* If the instruction preceeding an unconditional branch is the same as the
318 * instruction preceeding the jump target, the jump target may be moved
319 * one entry back. This is a size optimization, since the instruction before
320 * the branch gets removed.
323 CodeEntry* E1; /* Entry 1 */
324 CodeEntry* E2; /* Entry 2 */
325 CodeEntry* T1; /* Jump target entry 1 */
326 CodeEntry* T2; /* Jump target entry 2 */
327 CodeLabel* TL1; /* Target label 1 */
328 unsigned TI; /* Target index */
331 /* Get the number of entries, bail out if we have not enough */
332 unsigned Count = GetCodeEntryCount (S);
337 /* Walk over the entries */
339 while (I < Count-1) {
342 E2 = GetCodeEntry (S, I+1);
344 /* Check if we have a jump or branch, and a matching label */
345 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
347 /* Get the target instruction for the label */
348 T2 = E2->JumpTo->Owner;
350 /* Get the entry preceeding this one (if possible) */
351 TI = GetCodeEntryIndex (S, T2);
353 /* There is no entry before this one */
356 T1 = GetCodeEntry (S, TI-1);
358 /* Get the entry preceeding the jump */
359 E1 = GetCodeEntry (S, I);
361 /* Check if both preceeding instructions are identical */
362 if (!CodeEntriesAreEqual (E1, T1)) {
363 /* Not equal, try next */
367 /* Get the label for the instruction preceeding the jump target.
368 * This routine will create a new label if the instruction does
369 * not already have one.
371 TL1 = GenCodeLabel (S, T1);
373 /* Change the jump target to point to this new label */
374 MoveCodeLabelRef (S, E2, TL1);
376 /* If the instruction preceeding the jump has labels attached,
377 * move references to this label to the new label.
379 if (CodeEntryHasLabel (E1)) {
380 MoveCodeLabels (S, E1, T1);
383 /* Remove the entry preceeding the jump */
387 /* Remember, we had changes */
401 /*****************************************************************************/
403 /*****************************************************************************/
407 void RunOpt (CodeSeg* S)
408 /* Run the optimizer */
410 typedef void (*OptFunc) (CodeSeg*);
412 /* Table with optimizer steps - are called in this order */
413 static const OptFunc OptFuncs [] = {
414 OptJumpCascades, /* Optimize jump cascades */
415 OptDeadJumps, /* Remove dead jumps */
416 OptDeadCode, /* Remove dead code */
417 OptRTS, /* Change jsr/rts to jmp */
418 OptJumpTarget, /* Optimize jump targets */
421 /* Repeat all steps until there are no more changes */
427 /* Reset the number of changes */
430 /* Run all optimization steps */
432 for (I = 0; I < sizeof(OptFuncs)/sizeof(OptFuncs[0]); ++I) {
433 if ((OptDisable & Flags) == 0) {
435 } else if (Verbosity > 0 || Debug) {
436 printf ("Optimizer pass %u skipped\n", I);
441 } while (OptChanges > 0);