1 /*****************************************************************************/
5 /* Optimize negation sequences */
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 /*****************************************************************************/
43 /*****************************************************************************/
44 /* nega optimizations */
45 /*****************************************************************************/
49 unsigned OptNegA1 (CodeSeg* S)
56 * Remove the ldx if the lda does not use it.
61 /* Walk over the entries */
63 while (I < CS_GetEntryCount (S)) {
68 CodeEntry* E = CS_GetEntry (S, I);
71 if (E->OPC == OP65_LDX &&
73 (E->Flags & CEF_NUMARG) != 0 &&
75 CS_GetEntries (S, L, I+1, 2) &&
76 L[0]->OPC == OP65_LDA &&
77 (L[0]->Use & REG_X) == 0 &&
78 !CE_HasLabel (L[0]) &&
79 CE_IsCall (L[1], "bnega") &&
80 !CE_HasLabel (L[1])) {
82 /* Remove the ldx instruction */
85 /* Remember, we had changes */
95 /* Return the number of changes made */
101 unsigned OptNegA2 (CodeSeg* S)
108 * Adjust the conditional branch and remove the call to the subroutine.
111 unsigned Changes = 0;
113 /* Walk over the entries */
115 while (I < CS_GetEntryCount (S)) {
120 CodeEntry* E = CS_GetEntry (S, I);
122 /* Check for the sequence */
123 if ((E->OPC == OP65_ADC ||
124 E->OPC == OP65_AND ||
125 E->OPC == OP65_DEA ||
126 E->OPC == OP65_EOR ||
127 E->OPC == OP65_INA ||
128 E->OPC == OP65_LDA ||
129 E->OPC == OP65_ORA ||
130 E->OPC == OP65_PLA ||
131 E->OPC == OP65_SBC ||
132 E->OPC == OP65_TXA ||
133 E->OPC == OP65_TYA) &&
134 CS_GetEntries (S, L, I+1, 2) &&
135 CE_IsCall (L[0], "bnega") &&
136 !CE_HasLabel (L[0]) &&
137 (L[1]->Info & OF_ZBRA) != 0 &&
138 !CE_HasLabel (L[1])) {
140 /* Invert the branch */
141 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
143 /* Delete the subroutine call */
144 CS_DelEntry (S, I+1);
146 /* Remember, we had changes */
156 /* Return the number of changes made */
162 /*****************************************************************************/
163 /* negax optimizations */
164 /*****************************************************************************/
168 unsigned OptNegAX1 (CodeSeg* S)
169 /* On a call to bnegax, if X is zero, the result depends only on the value in
170 * A, so change the call to a call to bnega. This will get further optimized
174 unsigned Changes = 0;
177 /* Generate register info for this step */
180 /* Walk over the entries */
182 while (I < CS_GetEntryCount (S)) {
185 CodeEntry* E = CS_GetEntry (S, I);
187 /* Check if this is a call to bnegax, and if X is known and zero */
188 if (E->RI->In.RegX == 0 && CE_IsCall (E, "bnegax")) {
190 CodeEntry* X = NewCodeEntry (OP65_JSR, AM65_ABS, "bnega", 0, E->LI);
191 CS_InsertEntry (S, X, I+1);
203 /* Free register info */
206 /* Return the number of changes made */
212 unsigned OptNegAX2 (CodeSeg* S)
213 /* Search for the sequence:
229 unsigned Changes = 0;
231 /* Walk over the entries */
233 while (I < CS_GetEntryCount (S)) {
238 L[0] = CS_GetEntry (S, I);
240 /* Check for the sequence */
241 if (L[0]->OPC == OP65_LDY &&
242 CE_KnownImm (L[0]) &&
243 !CS_RangeHasLabel (S, I+1, 3) &&
244 CS_GetEntries (S, L+1, I+1, 3) &&
245 CE_IsCall (L[1], "ldaxysp") &&
246 CE_IsCall (L[2], "bnegax") &&
247 (L[3]->Info & OF_ZBRA) != 0) {
252 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
253 CS_InsertEntry (S, X, I+1);
256 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[1]->LI);
257 CS_InsertEntry (S, X, I+2);
260 X = NewCodeEntry (OP65_ORA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
261 CS_InsertEntry (S, X, I+3);
263 /* Invert the branch */
264 CE_ReplaceOPC (L[3], GetInverseBranch (L[3]->OPC));
266 /* Delete the entries no longer needed. */
267 CS_DelEntries (S, I+4, 2);
269 /* Remember, we had changes */
279 /* Return the number of changes made */
285 unsigned OptNegAX3 (CodeSeg* S)
286 /* Search for the sequence:
300 unsigned Changes = 0;
302 /* Walk over the entries */
304 while (I < CS_GetEntryCount (S)) {
309 CodeEntry* E = CS_GetEntry (S, I);
311 /* Check for the sequence */
312 if (E->OPC == OP65_LDA &&
313 CS_GetEntries (S, L, I+1, 3) &&
314 L[0]->OPC == OP65_LDX &&
315 !CE_HasLabel (L[0]) &&
316 CE_IsCall (L[1], "bnegax") &&
317 !CE_HasLabel (L[1]) &&
318 (L[2]->Info & OF_ZBRA) != 0 &&
319 !CE_HasLabel (L[2])) {
322 CE_ReplaceOPC (L[0], OP65_ORA);
324 /* Invert the branch */
325 CE_ReplaceOPC (L[2], GetInverseBranch (L[2]->OPC));
327 /* Delete the subroutine call */
328 CS_DelEntry (S, I+2);
330 /* Remember, we had changes */
340 /* Return the number of changes made */
346 unsigned OptNegAX4 (CodeSeg* S)
347 /* Search for the sequence:
360 unsigned Changes = 0;
362 /* Walk over the entries */
364 while (I < CS_GetEntryCount (S)) {
369 CodeEntry* E = CS_GetEntry (S, I);
371 /* Check for the sequence */
372 if (E->OPC == OP65_JSR &&
373 CS_GetEntries (S, L, I+1, 2) &&
374 L[0]->OPC == OP65_JSR &&
375 strncmp (L[0]->Arg,"bnega",5) == 0 &&
376 !CE_HasLabel (L[0]) &&
377 (L[1]->Info & OF_ZBRA) != 0 &&
378 !CE_HasLabel (L[1])) {
382 /* Check if we're calling bnega or bnegax */
383 int ByteSized = (strcmp (L[0]->Arg, "bnega") == 0);
385 /* Insert apropriate test code */
388 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
389 CS_InsertEntry (S, X, I+2);
392 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
393 CS_InsertEntry (S, X, I+2);
394 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, L[0]->LI);
395 CS_InsertEntry (S, X, I+3);
398 /* Delete the subroutine call */
399 CS_DelEntry (S, I+1);
401 /* Invert the branch */
402 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
404 /* Remember, we had changes */
414 /* Return the number of changes made */