1 /*****************************************************************************/
5 /* Optimize negation sequences */
9 /* (C) 2001-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /* bnega optimizations */
45 /*****************************************************************************/
49 unsigned OptBNegA1 (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_IsCallTo (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 OptBNegA2 (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_IsCallTo (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 /* bnegax optimizations */
164 /*****************************************************************************/
168 unsigned OptBNegAX1 (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 /* Walk over the entries */
179 while (I < CS_GetEntryCount (S)) {
182 CodeEntry* E = CS_GetEntry (S, I);
184 /* Check if this is a call to bnegax, and if X is known and zero */
185 if (E->RI->In.RegX == 0 && CE_IsCallTo (E, "bnegax")) {
187 CodeEntry* X = NewCodeEntry (OP65_JSR, AM65_ABS, "bnega", 0, E->LI);
188 CS_InsertEntry (S, X, I+1);
200 /* Return the number of changes made */
206 unsigned OptBNegAX2 (CodeSeg* S)
207 /* Search for the sequence:
223 unsigned Changes = 0;
225 /* Walk over the entries */
227 while (I < CS_GetEntryCount (S)) {
232 L[0] = CS_GetEntry (S, I);
234 /* Check for the sequence */
235 if (L[0]->OPC == OP65_LDY &&
236 CE_IsConstImm (L[0]) &&
237 !CS_RangeHasLabel (S, I+1, 3) &&
238 CS_GetEntries (S, L+1, I+1, 3) &&
239 CE_IsCallTo (L[1], "ldaxysp") &&
240 CE_IsCallTo (L[2], "bnegax") &&
241 (L[3]->Info & OF_ZBRA) != 0) {
246 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
247 CS_InsertEntry (S, X, I+1);
250 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[1]->LI);
251 CS_InsertEntry (S, X, I+2);
254 X = NewCodeEntry (OP65_ORA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
255 CS_InsertEntry (S, X, I+3);
257 /* Invert the branch */
258 CE_ReplaceOPC (L[3], GetInverseBranch (L[3]->OPC));
260 /* Delete the entries no longer needed. */
261 CS_DelEntries (S, I+4, 2);
263 /* Remember, we had changes */
273 /* Return the number of changes made */
279 unsigned OptBNegAX3 (CodeSeg* S)
280 /* Search for the sequence:
294 unsigned Changes = 0;
296 /* Walk over the entries */
298 while (I < CS_GetEntryCount (S)) {
303 CodeEntry* E = CS_GetEntry (S, I);
305 /* Check for the sequence */
306 if (E->OPC == OP65_LDA &&
307 CS_GetEntries (S, L, I+1, 3) &&
308 L[0]->OPC == OP65_LDX &&
309 !CE_HasLabel (L[0]) &&
310 CE_IsCallTo (L[1], "bnegax") &&
311 !CE_HasLabel (L[1]) &&
312 (L[2]->Info & OF_ZBRA) != 0 &&
313 !CE_HasLabel (L[2])) {
316 CE_ReplaceOPC (L[0], OP65_ORA);
318 /* Invert the branch */
319 CE_ReplaceOPC (L[2], GetInverseBranch (L[2]->OPC));
321 /* Delete the subroutine call */
322 CS_DelEntry (S, I+2);
324 /* Remember, we had changes */
334 /* Return the number of changes made */
340 unsigned OptBNegAX4 (CodeSeg* S)
341 /* Search for the sequence:
354 unsigned Changes = 0;
356 /* Walk over the entries */
358 while (I < CS_GetEntryCount (S)) {
363 CodeEntry* E = CS_GetEntry (S, I);
365 /* Check for the sequence */
366 if (E->OPC == OP65_JSR &&
367 CS_GetEntries (S, L, I+1, 2) &&
368 L[0]->OPC == OP65_JSR &&
369 strncmp (L[0]->Arg,"bnega",5) == 0 &&
370 !CE_HasLabel (L[0]) &&
371 (L[1]->Info & OF_ZBRA) != 0 &&
372 !CE_HasLabel (L[1])) {
376 /* Check if we're calling bnega or bnegax */
377 int ByteSized = (strcmp (L[0]->Arg, "bnega") == 0);
379 /* Insert apropriate test code */
382 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
383 CS_InsertEntry (S, X, I+2);
386 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
387 CS_InsertEntry (S, X, I+2);
388 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, L[0]->LI);
389 CS_InsertEntry (S, X, I+3);
392 /* Delete the subroutine call */
393 CS_DelEntry (S, I+1);
395 /* Invert the branch */
396 CE_ReplaceOPC (L[1], GetInverseBranch (L[1]->OPC));
398 /* Remember, we had changes */
408 /* Return the number of changes made */
414 /*****************************************************************************/
415 /* negax optimizations */
416 /*****************************************************************************/
420 unsigned OptNegAX1 (CodeSeg* S)
421 /* Search for a call to negax and replace it by
427 * if X isn't used later.
430 unsigned Changes = 0;
433 /* Walk over the entries */
435 while (I < CS_GetEntryCount (S)) {
438 CodeEntry* E = CS_GetEntry (S, I);
440 /* Check if this is a call to negax, and if X isn't used later */
441 if (CE_IsCallTo (E, "negax") && !RegXUsed (S, I+1)) {
445 /* Add replacement code behind */
446 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$FF", 0, E->LI);
447 CS_InsertEntry (S, X, I+1);
449 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
450 CS_InsertEntry (S, X, I+2);
452 X = NewCodeEntry (OP65_ADC, AM65_IMM, "$01", 0, E->LI);
453 CS_InsertEntry (S, X, I+3);
455 /* Delete the call to negax */
458 /* Skip the generated code */
470 /* Return the number of changes made */
476 unsigned OptNegAX2 (CodeSeg* S)
477 /* Search for a call to negax and replace it by
487 * if X is known and zero on entry.
490 unsigned Changes = 0;
493 /* Walk over the entries */
495 while (I < CS_GetEntryCount (S)) {
500 CodeEntry* E = CS_GetEntry (S, I);
502 /* Check if this is a call to negax, and if X is known and zero */
503 if (E->RI->In.RegX == 0 &&
504 CE_IsCallTo (E, "negax") &&
505 (P = CS_GetNextEntry (S, I)) != 0) {
510 /* Add replacement code behind */
513 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$FF", 0, E->LI);
514 CS_InsertEntry (S, X, I+1);
517 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$FF", 0, E->LI);
518 CS_InsertEntry (S, X, I+2);
521 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
522 CS_InsertEntry (S, X, I+3);
525 X = NewCodeEntry (OP65_ADC, AM65_IMM, "$01", 0, E->LI);
526 CS_InsertEntry (S, X, I+4);
528 /* Get the label attached to the insn following the call */
529 L = CS_GenLabel (S, P);
532 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, E->LI);
533 CS_InsertEntry (S, X, I+5);
536 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
537 CS_InsertEntry (S, X, I+6);
539 /* Delete the call to negax */
542 /* Skip the generated code */
554 /* Return the number of changes made */