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