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 /*****************************************************************************/
42 /*****************************************************************************/
44 /*****************************************************************************/
48 /* Counter for the number of changes in one run. The optimizer process is
49 * repeated until there are no more changes.
51 static unsigned OptChanges;
55 /*****************************************************************************/
56 /* Remove dead jumps */
57 /*****************************************************************************/
61 static void OptDeadJumps (CodeSeg* S)
62 /* Remove dead jumps (jumps to the next instruction) */
67 /* Get the number of entries, bail out if we have less than two entries */
68 unsigned Count = CollCount (&S->Entries);
73 /* Walk over all entries minus the last one */
77 /* Get the next entry */
78 E = CollAt (&S->Entries, I);
80 /* Check if it's a branch, if it has a local target, and if the target
81 * is the next instruction.
83 if (E->AM == AM_BRA) {
84 printf ("BRA on entry %u:\n", I);
86 printf (" JumpTo ok\n");
87 if (E->JumpTo->Owner == CollAt (&S->Entries, I+1)) {
88 printf (" Branch to next insn\n");
93 if (E->AM == AM_BRA && E->JumpTo && E->JumpTo->Owner == CollAt (&S->Entries, I+1)) {
95 /* Remember the label */
96 CodeLabel* L = E->JumpTo;
98 /* Jump to next instruction, remove it */
99 unsigned Remaining = RemoveLabelRef (L, E);
100 CollDelete (&S->Entries, I);
104 /* If the label has no more references, remove it */
105 if (Remaining == 0) {
106 CollDeleteItem (&L->Owner->Labels, L);
110 /* Remember we had changes */
124 /*****************************************************************************/
126 /*****************************************************************************/
130 void RunOpt (CodeSeg* S)
131 /* Run the optimizer */
133 printf ("Optimize\n");
135 /* Repeat all steps until there are no more changes */
138 /* Reset the number of changes */
143 } while (OptChanges > 0);