]> git.sur5r.net Git - cc65/blob - src/cc65/coptsub.c
Working on the optimizations
[cc65] / src / cc65 / coptsub.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 coptsub.c                                 */
4 /*                                                                           */
5 /*                      Optimize subtraction 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 /* cc65 */
37 #include "codeent.h"
38 #include "codeinfo.h"
39 #include "coptsub.h"
40
41
42
43 /*****************************************************************************/
44 /*                           Optimize subtractions                           */
45 /*****************************************************************************/
46
47
48
49 unsigned OptSub1 (CodeSeg* S)
50 /* Search for the sequence
51  *
52  *      sbc     ...
53  *      bcs     L
54  *      dex
55  * L:
56  *
57  * and remove the handling of the high byte if X is not used later.
58  */
59 {
60     unsigned Changes = 0;
61
62     /* Walk over the entries */
63     unsigned I = 0;
64     while (I < CS_GetEntryCount (S)) {
65
66         CodeEntry* L[3];
67
68         /* Get next entry */
69         CodeEntry* E = CS_GetEntry (S, I);
70
71         /* Check for the sequence */
72         if (E->OPC == OP65_SBC                               &&
73             CS_GetEntries (S, L, I+1, 3)                     &&
74             (L[0]->OPC == OP65_BCS || L[0]->OPC == OP65_JCS) &&
75             L[0]->JumpTo != 0                                &&
76             !CE_HasLabel (L[0])                              &&
77             L[1]->OPC == OP65_DEX                            &&
78             !CE_HasLabel (L[1])                              &&
79             L[0]->JumpTo->Owner == L[2]                      &&
80             !RegXUsed (S, I+3)) {
81
82             /* Remove the bcs/dex */
83             CS_DelEntries (S, I+1, 2);
84
85             /* Remember, we had changes */
86             ++Changes;
87
88         }
89
90         /* Next entry */
91         ++I;
92
93     }
94
95     /* Return the number of changes made */
96     return Changes;
97 }
98
99
100
101 unsigned OptSub2 (CodeSeg* S)
102 /* Search for the sequence
103  *
104  *      lda     xx
105  *      sec
106  *      sta     tmp1
107  *      lda     yy
108  *      sbc     tmp1
109  *      sta     yy
110  *
111  * and replace it by
112  *
113  *      sec
114  *      lda     yy
115  *      sbc     xx
116  *      sta     yy
117  */
118 {
119     unsigned Changes = 0;
120
121     /* Walk over the entries */
122     unsigned I = 0;
123     while (I < CS_GetEntryCount (S)) {
124
125         CodeEntry* L[5];
126
127         /* Get next entry */
128         CodeEntry* E = CS_GetEntry (S, I);
129
130         /* Check for the sequence */
131         if (E->OPC == OP65_LDA                             &&
132             !CS_RangeHasLabel (S, I+1, 5)                  &&
133             CS_GetEntries (S, L, I+1, 5)                   &&
134             L[0]->OPC == OP65_SEC                          &&
135             L[1]->OPC == OP65_STA                          &&
136             strcmp (L[1]->Arg, "tmp1") == 0                &&
137             L[2]->OPC == OP65_LDA                          &&
138             L[3]->OPC == OP65_SBC                          &&
139             strcmp (L[3]->Arg, "tmp1") == 0                &&
140             L[4]->OPC == OP65_STA                          &&
141             strcmp (L[4]->Arg, L[2]->Arg) == 0) {
142
143             /* Remove the store to tmp1 */
144             CS_DelEntry (S, I+2);
145
146             /* Remove the subtraction */
147             CS_DelEntry (S, I+3);
148
149             /* Move the lda to the position of the subtraction and change the
150              * op to SBC.
151              */
152             CS_MoveEntry (S, I, I+3);
153             CE_ReplaceOPC (E, OP65_SBC);
154
155             /* If the sequence head had a label, move this label back to the
156              * head.
157              */
158             if (CE_HasLabel (E)) {
159                 CS_MoveLabels (S, E, L[0]);
160             }
161
162             /* Remember, we had changes */
163             ++Changes;
164
165         }
166
167         /* Next entry */
168         ++I;
169
170     }
171
172     /* Return the number of changes made */
173     return Changes;
174 }
175
176
177
178