]> git.sur5r.net Git - cc65/blob - src/cc65/coptadd.c
Add code size factor for optimizer routines
[cc65] / src / cc65 / coptadd.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 coptadd.c                                 */
4 /*                                                                           */
5 /*                        Optimize addition sequences                        */
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 /* common */
37 #include "xsprintf.h"
38
39 /* cc65 */
40 #include "codeent.h"
41 #include "codeinfo.h"
42 #include "coptadd.h"
43
44
45
46 /*****************************************************************************/
47 /*                            Optimize additions                             */
48 /*****************************************************************************/
49
50
51
52 unsigned OptAdd1 (CodeSeg* S)
53 /* Search for the sequence
54  *
55  *      jsr     pushax
56  *      ldy     xxx
57  *      ldx     #$00
58  *      lda     (sp),y
59  *      jsr     tosaddax
60  *
61  * and replace it by:
62  *
63  *      ldy     xxx-2
64  *      clc
65  *      adc     (sp),y
66  *      bcc     L
67  *      inx
68  * L:
69  */
70 {
71     unsigned Changes = 0;
72
73     /* Walk over the entries */
74     unsigned I = 0;
75     while (I < CS_GetEntryCount (S)) {
76
77         CodeEntry* L[5];
78
79         /* Get next entry */
80         CodeEntry* E = CS_GetEntry (S, I);
81
82         /* Check for the sequence */
83         if (CE_IsCall (E, "pushax")          &&
84             CS_GetEntries (S, L, I+1, 5)     &&
85             L[0]->OPC == OP65_LDY            &&
86             CE_KnownImm (L[0])               &&
87             !CE_HasLabel (L[0])              &&
88             L[1]->OPC == OP65_LDX            &&
89             CE_KnownImm (L[1])               &&
90             L[1]->Num == 0                   &&
91             !CE_HasLabel (L[1])              &&
92             L[2]->OPC == OP65_LDA            &&
93             !CE_HasLabel (L[2])              &&
94             CE_IsCall (L[3], "tosaddax")     &&
95             !CE_HasLabel (L[3])) {
96
97             CodeEntry* X;
98             CodeLabel* Label;
99
100             /* Remove the call to pushax */
101             CS_DelEntry (S, I);
102
103             /* Correct the stack offset (needed since pushax was removed) */
104             CE_SetNumArg (L[0], L[0]->Num - 2);
105
106             /* Add the clc . */
107             X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
108             CS_InsertEntry (S, X, I+1);
109
110             /* Remove the load */
111             CS_DelEntry (S, I+3);      /* lda */
112             CS_DelEntry (S, I+2);      /* ldx */
113
114             /* Add the adc */
115             X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
116             CS_InsertEntry (S, X, I+2);
117
118             /* Generate the branch label and the branch */
119             Label = CS_GenLabel (S, L[4]);
120             X = NewCodeEntry (OP65_BCC, AM65_BRA, Label->Name, Label, L[3]->LI);
121             CS_InsertEntry (S, X, I+3);
122
123             /* Generate the increment of the high byte */
124             X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[3]->LI);
125             CS_InsertEntry (S, X, I+4);
126
127             /* Delete the call to tosaddax */
128             CS_DelEntry (S, I+5);
129
130             /* Remember, we had changes */
131             ++Changes;
132
133         }
134
135         /* Next entry */
136         ++I;
137
138     }
139
140     /* Return the number of changes made */
141     return Changes;
142 }
143
144
145
146 unsigned OptAdd2 (CodeSeg* S)
147 /* Search for the sequence
148  *
149  *      ldy     #xx
150  *      jsr     ldaxysp
151  *      ldy     #yy
152  *      jsr     addeqysp
153  *
154  * and replace it by:
155  *
156  *      ldy     #xx-1
157  *      lda     (sp),y
158  *      ldy     #yy
159  *      clc
160  *      adc     (sp),y
161  *      sta     (sp),y
162  *      ldy     #xx
163  *      lda     (sp),y
164  *      ldy     #yy+1
165  *      adc     (sp),y
166  *      sta     (sp),y
167  *
168  * provided that a/x is not used later.
169  */
170 {
171     unsigned Changes = 0;
172
173     /* Walk over the entries */
174     unsigned I = 0;
175     while (I < CS_GetEntryCount (S)) {
176
177         CodeEntry* L[4];
178
179         /* Get next entry */
180         L[0] = CS_GetEntry (S, I);
181
182         /* Check for the sequence */
183         if (L[0]->OPC == OP65_LDY               &&
184             CE_KnownImm (L[0])                  &&
185             !CS_RangeHasLabel (S, I+1, 3)       &&
186             CS_GetEntries (S, L+1, I+1, 3)      &&
187             CE_IsCall (L[1], "ldaxysp")         &&
188             L[2]->OPC == OP65_LDY               &&
189             CE_KnownImm (L[2])                  &&
190             CE_IsCall (L[3], "addeqysp")        &&
191             (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
192
193             /* Insert new code behind the addeqysp */
194             char Buf [20];
195             CodeEntry* X;
196
197             /* ldy     #xx-1 */
198             xsprintf (Buf, sizeof (Buf), "$%02X", (int)(L[0]->Num-1));
199             X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[0]->LI);
200             CS_InsertEntry (S, X, I+4);
201
202             /* lda     (sp),y */
203             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
204             CS_InsertEntry (S, X, I+5);
205
206             /* ldy     #yy */
207             X = NewCodeEntry (OP65_LDY, AM65_IMM, L[2]->Arg, 0, L[2]->LI);
208             CS_InsertEntry (S, X, I+6);
209
210             /* clc */
211             X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
212             CS_InsertEntry (S, X, I+7);
213
214             /* adc     (sp),y */
215             X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
216             CS_InsertEntry (S, X, I+8);
217
218             /* sta     (sp),y */
219             X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
220             CS_InsertEntry (S, X, I+9);
221
222             /* ldy     #xx */
223             X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
224             CS_InsertEntry (S, X, I+10);
225
226             /* lda     (sp),y */
227             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
228             CS_InsertEntry (S, X, I+11);
229
230             /* ldy     #yy+1 */
231             xsprintf (Buf, sizeof (Buf), "$%02X", (int)(L[2]->Num+1));
232             X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[2]->LI);
233             CS_InsertEntry (S, X, I+12);
234
235             /* adc     (sp),y */
236             X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
237             CS_InsertEntry (S, X, I+13);
238
239             /* sta     (sp),y */
240             X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
241             CS_InsertEntry (S, X, I+14);
242
243             /* Delete the old code */
244             CS_DelEntries (S, I, 4);
245
246             /* Remember, we had changes */
247             ++Changes;
248
249         }
250
251         /* Next entry */
252         ++I;
253
254     }
255
256     /* Return the number of changes made */
257     return Changes;
258 }
259
260
261
262 unsigned OptAdd3 (CodeSeg* S)
263 /* Search for the sequence
264  *
265  *      adc     ...
266  *      bcc     L
267  *      inx
268  * L:
269  *
270  * and remove the handling of the high byte if X is not used later.
271  */
272 {
273     unsigned Changes = 0;
274
275     /* Walk over the entries */
276     unsigned I = 0;
277     while (I < CS_GetEntryCount (S)) {
278
279         CodeEntry* L[3];
280
281         /* Get next entry */
282         CodeEntry* E = CS_GetEntry (S, I);
283
284         /* Check for the sequence */
285         if (E->OPC == OP65_ADC                               &&
286             CS_GetEntries (S, L, I+1, 3)                     &&
287             (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
288             L[0]->JumpTo != 0                                &&
289             !CE_HasLabel (L[0])                              &&
290             L[1]->OPC == OP65_INX                            &&
291             !CE_HasLabel (L[1])                              &&
292             L[0]->JumpTo->Owner == L[2]                      &&
293             !RegXUsed (S, I+3)) {
294
295             /* Remove the bcs/dex */
296             CS_DelEntries (S, I+1, 2);
297
298             /* Remember, we had changes */
299             ++Changes;
300
301         }
302
303         /* Next entry */
304         ++I;
305
306     }
307
308     /* Return the number of changes made */
309     return Changes;
310 }
311
312
313
314