]> git.sur5r.net Git - cc65/blob - src/cc65/codeopt.c
c4e6040a3f6d06c75710c0406ad55ed36e9b57db
[cc65] / src / cc65 / codeopt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeopt.c                                 */
4 /*                                                                           */
5 /*                           Optimizer subroutines                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* b6502 */
37 #include "codeent.h"
38 #include "codeopt.h"
39
40
41
42 /*****************************************************************************/
43 /*                                   Data                                    */
44 /*****************************************************************************/
45
46
47
48 /* Counter for the number of changes in one run. The optimizer process is
49  * repeated until there are no more changes.
50  */
51 static unsigned OptChanges;
52
53
54
55 /*****************************************************************************/
56 /*                             Remove dead jumps                             */
57 /*****************************************************************************/
58
59
60
61 static void OptDeadJumps (CodeSeg* S)
62 /* Remove dead jumps (jumps to the next instruction) */
63 {
64     CodeEntry* E;
65     unsigned I;
66
67     /* Get the number of entries, bail out if we have less than two entries */
68     unsigned Count = CollCount (&S->Entries);
69     if (Count < 2) {
70         return;
71     }
72
73     /* Walk over all entries minus the last one */
74     I = 0;
75     while (I < Count-1) {
76
77         /* Get the next entry */
78         E = CollAt (&S->Entries, I);
79
80         /* Check if it's a branch, if it has a local target, and if the target
81          * is the next instruction.
82          */
83         if (E->AM == AM_BRA) {
84             printf ("BRA on entry %u:\n", I);
85             if (E->JumpTo) {
86                 printf ("  JumpTo ok\n");
87                 if (E->JumpTo->Owner == CollAt (&S->Entries, I+1)) {
88                     printf ("  Branch to next insn\n");
89                 }
90             }
91         }
92
93         if (E->AM == AM_BRA && E->JumpTo && E->JumpTo->Owner == CollAt (&S->Entries, I+1)) {
94
95             /* Remember the label */
96             CodeLabel* L = E->JumpTo;
97
98             /* Jump to next instruction, remove it */
99             unsigned Remaining = RemoveLabelRef (L, E);
100             CollDelete (&S->Entries, I);
101             FreeCodeEntry (E);
102             --Count;
103
104             /* If the label has no more references, remove it */
105             if (Remaining == 0) {
106                 CollDeleteItem (&L->Owner->Labels, L);
107                 FreeCodeLabel (L);
108             }
109
110             /* Remember we had changes */
111             ++OptChanges;
112
113         } else {
114
115             /* Next entry */
116             ++I;
117
118         }
119     }
120 }
121
122
123
124 /*****************************************************************************/
125 /*                                   Code                                    */
126 /*****************************************************************************/
127
128
129
130 void RunOpt (CodeSeg* S)
131 /* Run the optimizer */
132 {
133     printf ("Optimize\n");
134
135     /* Repeat all steps until there are no more changes */
136     do {
137
138         /* Reset the number of changes */
139         OptChanges = 0;
140
141         OptDeadJumps (S);
142
143     } while (OptChanges > 0);
144 }
145
146
147