1 /*****************************************************************************/
5 /* Optimizer subroutines */
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 #include "debugflag.h"
57 #include "coptptrload.h"
58 #include "coptptrstore.h"
60 #include "coptshift.h"
63 #include "coptstore.h"
72 /*****************************************************************************/
74 /*****************************************************************************/
78 static unsigned OptLoad1 (CodeSeg* S)
79 /* Search for a call to ldaxysp where X is not used later and replace it by
80 * a load of just the A register.
86 /* Walk over the entries */
88 while (I < CS_GetEntryCount (S)) {
93 E = CS_GetEntry (S, I);
95 /* Check for the sequence */
96 if (CE_IsCallTo (E, "ldaxysp") &&
97 RegValIsKnown (E->RI->In.RegY) &&
102 /* Reload the Y register */
103 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
104 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
105 CS_InsertEntry (S, X, I+1);
107 /* Load from stack */
108 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
109 CS_InsertEntry (S, X, I+2);
111 /* Now remove the call to the subroutine */
114 /* Remember, we had changes */
124 /* Return the number of changes made */
130 static unsigned OptLoad2 (CodeSeg* S)
131 /* Replace calls to ldaxysp by inline code */
134 unsigned Changes = 0;
136 /* Walk over the entries */
138 while (I < CS_GetEntryCount (S)) {
143 L[0] = CS_GetEntry (S, I);
145 /* Check for the sequence */
146 if (CE_IsCallTo (L[0], "ldaxysp")) {
150 /* Followed by sta abs/stx abs? */
151 if (CS_GetEntries (S, L+1, I+1, 2) &&
152 L[1]->OPC == OP65_STA &&
153 L[2]->OPC == OP65_STX &&
156 strcmp (L[1]->Arg, L[2]->Arg) != 0) &&
157 !CS_RangeHasLabel (S, I+1, 2) &&
158 !RegXUsed (S, I+3)) {
160 /* A/X are stored into memory somewhere and X is not used
165 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
166 CS_InsertEntry (S, X, I+3);
169 X = NewCodeEntry (OP65_STA, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
170 CS_InsertEntry (S, X, I+4);
173 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[0]->LI);
174 CS_InsertEntry (S, X, I+5);
177 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
178 CS_InsertEntry (S, X, I+6);
181 X = NewCodeEntry (OP65_STA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
182 CS_InsertEntry (S, X, I+7);
184 /* Now remove the call to the subroutine and the sta/stx */
185 CS_DelEntries (S, I, 3);
189 /* Standard replacement */
192 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
193 CS_InsertEntry (S, X, I+1);
196 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[0]->LI);
197 CS_InsertEntry (S, X, I+2);
200 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[0]->LI);
201 CS_InsertEntry (S, X, I+3);
204 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[0]->LI);
205 CS_InsertEntry (S, X, I+4);
207 /* Now remove the call to the subroutine */
212 /* Remember, we had changes */
221 /* Return the number of changes made */
227 /*****************************************************************************/
228 /* Decouple operations */
229 /*****************************************************************************/
233 static unsigned OptDecouple (CodeSeg* S)
234 /* Decouple operations, that is, do the following replacements:
248 * Provided that the register values are known of course.
251 unsigned Changes = 0;
254 /* Walk over the entries */
256 while (I < CS_GetEntryCount (S)) {
260 /* Get next entry and it's input register values */
261 CodeEntry* E = CS_GetEntry (S, I);
262 const RegContents* In = &E->RI->In;
264 /* Assume we have no replacement */
267 /* Check the instruction */
271 if (RegValIsKnown (In->RegA)) {
272 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
273 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
278 if (RegValIsKnown (In->RegX)) {
279 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
280 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
285 if (RegValIsKnown (In->RegY)) {
286 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
287 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
292 if (RegValIsKnown (In->RegA)) {
293 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
294 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
299 if (RegValIsKnown (In->RegX)) {
300 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
301 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
306 if (RegValIsKnown (In->RegY)) {
307 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
308 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
313 if (E->AM == AM65_ZP) {
314 switch (GetKnownReg (E->Use & REG_ZP, In)) {
316 Arg = MakeHexArg (In->Tmp1);
317 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
321 Arg = MakeHexArg (In->Ptr1Lo);
322 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
326 Arg = MakeHexArg (In->Ptr1Hi);
327 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
331 Arg = MakeHexArg (In->SRegLo);
332 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
336 Arg = MakeHexArg (In->SRegHi);
337 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
344 if (E->AM == AM65_ZP) {
345 switch (GetKnownReg (E->Use & REG_ZP, In)) {
347 Arg = MakeHexArg (In->Tmp1);
348 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
352 Arg = MakeHexArg (In->Ptr1Lo);
353 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
357 Arg = MakeHexArg (In->Ptr1Hi);
358 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
362 Arg = MakeHexArg (In->SRegLo);
363 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
367 Arg = MakeHexArg (In->SRegHi);
368 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
375 if (E->AM == AM65_ZP) {
376 switch (GetKnownReg (E->Use, In)) {
378 Arg = MakeHexArg (In->Tmp1);
379 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
383 Arg = MakeHexArg (In->Ptr1Lo);
384 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
388 Arg = MakeHexArg (In->Ptr1Hi);
389 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
393 Arg = MakeHexArg (In->SRegLo);
394 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
398 Arg = MakeHexArg (In->SRegHi);
399 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
406 if (E->RI->In.RegA >= 0) {
407 Arg = MakeHexArg (In->RegA);
408 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
413 if (E->RI->In.RegA >= 0) {
414 Arg = MakeHexArg (In->RegA);
415 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
420 if (E->RI->In.RegX >= 0) {
421 Arg = MakeHexArg (In->RegX);
422 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
427 if (E->RI->In.RegY >= 0) {
428 Arg = MakeHexArg (In->RegY);
429 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
434 /* Avoid gcc warnings */
439 /* Insert the replacement if we have one */
441 CS_InsertEntry (S, X, I+1);
451 /* Return the number of changes made */
457 /*****************************************************************************/
458 /* Optimize stack pointer ops */
459 /*****************************************************************************/
463 static unsigned IsDecSP (const CodeEntry* E)
464 /* Check if this is an insn that decrements the stack pointer. If so, return
465 * the decrement. If not, return zero.
466 * The function expects E to be a subroutine call.
469 if (strncmp (E->Arg, "decsp", 5) == 0) {
470 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
471 return (E->Arg[5] - '0');
473 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
474 return E->RI->In.RegY;
477 /* If we come here, it's not a decsp op */
483 static unsigned OptStackPtrOps (CodeSeg* S)
484 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
488 unsigned Changes = 0;
491 /* Walk over the entries */
493 while (I < CS_GetEntryCount (S)) {
499 /* Get the next entry */
500 const CodeEntry* E = CS_GetEntry (S, I);
502 /* Check for decspn or subysp */
503 if (E->OPC == OP65_JSR &&
504 (Dec1 = IsDecSP (E)) > 0 &&
505 (N = CS_GetNextEntry (S, I)) != 0 &&
506 (Dec2 = IsDecSP (N)) > 0 &&
507 (Dec1 += Dec2) <= 255 &&
513 /* We can combine the two */
515 /* Insert a call to decsp */
516 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
517 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
518 CS_InsertEntry (S, X, I+2);
520 /* Insert a call to subysp */
521 const char* Arg = MakeHexArg (Dec1);
522 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
523 CS_InsertEntry (S, X, I+2);
524 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
525 CS_InsertEntry (S, X, I+3);
528 /* Delete the old code */
529 CS_DelEntries (S, I, 2);
531 /* Regenerate register info */
534 /* Remember we had changes */
545 /* Return the number of changes made */
551 /*****************************************************************************/
553 /*****************************************************************************/
557 typedef struct OptFunc OptFunc;
559 unsigned (*Func) (CodeSeg*); /* Optimizer function */
560 const char* Name; /* Name of the function/group */
561 unsigned CodeSizeFactor; /* Code size factor for this opt func */
562 unsigned long TotalRuns; /* Total number of runs */
563 unsigned long LastRuns; /* Last number of runs */
564 unsigned long TotalChanges; /* Total number of changes */
565 unsigned long LastChanges; /* Last number of changes */
566 char Disabled; /* True if function disabled */
571 /*****************************************************************************/
573 /*****************************************************************************/
577 /* A list of all the function descriptions */
578 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
579 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
580 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
581 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
582 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
583 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
584 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
585 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
586 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
587 static OptFunc DOptBNegA1 = { OptBNegA1, "OptBNegA1", 100, 0, 0, 0, 0, 0 };
588 static OptFunc DOptBNegA2 = { OptBNegA2, "OptBNegA2", 100, 0, 0, 0, 0, 0 };
589 static OptFunc DOptBNegAX1 = { OptBNegAX1, "OptBNegAX1", 100, 0, 0, 0, 0, 0 };
590 static OptFunc DOptBNegAX2 = { OptBNegAX2, "OptBNegAX2", 100, 0, 0, 0, 0, 0 };
591 static OptFunc DOptBNegAX3 = { OptBNegAX3, "OptBNegAX3", 100, 0, 0, 0, 0, 0 };
592 static OptFunc DOptBNegAX4 = { OptBNegAX4, "OptBNegAX4", 100, 0, 0, 0, 0, 0 };
593 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
594 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
595 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
596 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
597 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
598 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
599 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
600 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
601 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
602 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
603 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
604 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
605 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
606 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
607 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
608 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
609 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
610 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
611 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
612 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
613 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
614 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
615 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
616 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
617 static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
618 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 165, 0, 0, 0, 0, 0 };
619 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 200, 0, 0, 0, 0, 0 };
620 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
621 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
622 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
623 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
624 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
625 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
626 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
627 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
628 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
629 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
630 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
631 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
632 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
633 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
634 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
635 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
636 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
637 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
638 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 65, 0, 0, 0, 0, 0 };
639 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 65, 0, 0, 0, 0, 0 };
640 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 100, 0, 0, 0, 0, 0 };
641 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
642 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
643 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
644 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
645 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
646 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 17, 0, 0, 0, 0, 0 };
647 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 100, 0, 0, 0, 0, 0 };
648 static OptFunc DOptShift5 = { OptShift5, "OptShift5", 110, 0, 0, 0, 0, 0 };
649 static OptFunc DOptShift6 = { OptShift6, "OptShift6", 200, 0, 0, 0, 0, 0 };
650 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
651 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
652 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
653 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
654 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
655 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 115, 0, 0, 0, 0, 0 };
656 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
657 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
658 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
659 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
660 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
661 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
662 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
663 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
664 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
665 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
666 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
667 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
668 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
669 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
670 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
673 /* Table containing all the steps in alphabetical order */
674 static OptFunc* OptFuncs[] = {
769 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
773 static int CmpOptStep (const void* Key, const void* Func)
774 /* Compare function for bsearch */
776 return strcmp (Key, (*(const OptFunc**)Func)->Name);
781 static OptFunc* FindOptFunc (const char* Name)
782 /* Find an optimizer step by name in the table and return a pointer. Return
783 * NULL if no such step is found.
786 /* Search for the function in the list */
787 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
793 static OptFunc* GetOptFunc (const char* Name)
794 /* Find an optimizer step by name in the table and return a pointer. Print an
795 * error and call AbEnd if not found.
798 /* Search for the function in the list */
799 OptFunc* F = FindOptFunc (Name);
802 AbEnd ("Optimization step `%s' not found", Name);
809 void DisableOpt (const char* Name)
810 /* Disable the optimization with the given name */
812 if (strcmp (Name, "any") == 0) {
814 for (I = 0; I < OPTFUNC_COUNT; ++I) {
815 OptFuncs[I]->Disabled = 1;
818 GetOptFunc(Name)->Disabled = 1;
824 void EnableOpt (const char* Name)
825 /* Enable the optimization with the given name */
827 if (strcmp (Name, "any") == 0) {
829 for (I = 0; I < OPTFUNC_COUNT; ++I) {
830 OptFuncs[I]->Disabled = 0;
833 GetOptFunc(Name)->Disabled = 0;
839 void ListOptSteps (FILE* F)
840 /* List all optimization steps */
843 for (I = 0; I < OPTFUNC_COUNT; ++I) {
844 fprintf (F, "%s\n", OptFuncs[I]->Name);
850 static void ReadOptStats (const char* Name)
851 /* Read the optimizer statistics file */
856 /* Try to open the file */
857 FILE* F = fopen (Name, "r");
859 /* Ignore the error */
863 /* Read and parse the lines */
865 while (fgets (Buf, sizeof (Buf), F) != 0) {
873 unsigned long TotalRuns;
874 unsigned long TotalChanges;
879 /* Remove trailing white space including the line terminator */
882 while (Len > 0 && IsSpace (B[Len-1])) {
887 /* Remove leading whitespace */
888 while (IsSpace (*B)) {
892 /* Check for empty and comment lines */
893 if (*B == '\0' || *B == ';' || *B == '#') {
898 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
903 /* Search for the optimizer step. */
904 Func = FindOptFunc (Name);
910 /* Found the step, set the fields */
911 Func->TotalRuns = TotalRuns;
912 Func->TotalChanges = TotalChanges;
916 /* Close the file, ignore errors here. */
922 static void WriteOptStats (const char* Name)
923 /* Write the optimizer statistics file */
927 /* Try to open the file */
928 FILE* F = fopen (Name, "w");
930 /* Ignore the error */
936 "; Optimizer Total Last Total Last\n"
937 "; Step Runs Runs Chg Chg\n");
941 for (I = 0; I < OPTFUNC_COUNT; ++I) {
942 const OptFunc* O = OptFuncs[I];
944 "%-20s %10lu %10lu %10lu %10lu\n",
952 /* Close the file, ignore errors here. */
958 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
959 /* Run one optimizer function Max times or until there are no more changes */
963 /* Don't run the function if it is disabled or if it is prohibited by the
966 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
970 /* Run this until there are no more changes */
974 /* Run the function */
976 if (Debug && C > 0) {
977 printf ("Applied %s: %u changes\n", F->Name, C);
984 F->TotalChanges += C;
987 /* If we had changes, regenerate register info */
992 } while (--Max && C > 0);
994 /* Return the number of changes */
1000 static unsigned RunOptGroup1 (CodeSeg* S)
1001 /* Run the first group of optimization steps. These steps translate known
1002 * patterns emitted by the code generator into more optimal patterns. Order
1003 * of the steps is important, because some of the steps done earlier cover
1004 * the same patterns as later steps as subpatterns.
1007 unsigned Changes = 0;
1009 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1010 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1011 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1012 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1013 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1014 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1015 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1016 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1017 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1018 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1019 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1020 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1021 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1022 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1023 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1024 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1025 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1026 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1027 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1028 Changes += RunOptFunc (S, &DOptBNegAX1, 1);
1029 Changes += RunOptFunc (S, &DOptBNegAX2, 1);
1030 Changes += RunOptFunc (S, &DOptBNegAX3, 1);
1031 Changes += RunOptFunc (S, &DOptBNegAX4, 1);
1032 Changes += RunOptFunc (S, &DOptAdd1, 1);
1033 Changes += RunOptFunc (S, &DOptAdd2, 1);
1034 Changes += RunOptFunc (S, &DOptAdd4, 1);
1035 Changes += RunOptFunc (S, &DOptAdd5, 1);
1036 Changes += RunOptFunc (S, &DOptAdd6, 1);
1037 Changes += RunOptFunc (S, &DOptSub1, 1);
1038 Changes += RunOptFunc (S, &DOptSub3, 1);
1039 Changes += RunOptFunc (S, &DOptStore4, 1);
1040 Changes += RunOptFunc (S, &DOptStore5, 1);
1041 Changes += RunOptFunc (S, &DOptShift1, 1);
1042 Changes += RunOptFunc (S, &DOptShift2, 1);
1043 Changes += RunOptFunc (S, &DOptShift4, 1);
1044 Changes += RunOptFunc (S, &DOptShift5, 1);
1045 Changes += RunOptFunc (S, &DOptShift6, 1);
1046 Changes += RunOptFunc (S, &DOptStore1, 1);
1047 Changes += RunOptFunc (S, &DOptStore2, 5);
1048 Changes += RunOptFunc (S, &DOptStore3, 5);
1050 /* Return the number of changes */
1056 static unsigned RunOptGroup2 (CodeSeg* S)
1057 /* Run one group of optimization steps. This step involves just decoupling
1058 * instructions by replacing them by instructions that do not depend on
1059 * previous instructions. This makes it easier to find instructions that
1063 unsigned Changes = 0;
1065 Changes += RunOptFunc (S, &DOptDecouple, 1);
1067 /* Return the number of changes */
1073 static unsigned RunOptGroup3 (CodeSeg* S)
1074 /* Run one group of optimization steps. These steps depend on each other,
1075 * that means that one step may allow another step to do additional work,
1076 * so we will repeat the steps as long as we see any changes.
1079 unsigned Changes, C;
1085 C += RunOptFunc (S, &DOptBNegA1, 1);
1086 C += RunOptFunc (S, &DOptBNegA2, 1);
1087 C += RunOptFunc (S, &DOptNegAX1, 1);
1088 C += RunOptFunc (S, &DOptNegAX2, 1);
1089 C += RunOptFunc (S, &DOptStackOps, 3);
1090 C += RunOptFunc (S, &DOptSub1, 1);
1091 C += RunOptFunc (S, &DOptSub2, 1);
1092 C += RunOptFunc (S, &DOptSub3, 1);
1093 C += RunOptFunc (S, &DOptAdd5, 1);
1094 C += RunOptFunc (S, &DOptAdd6, 1);
1095 C += RunOptFunc (S, &DOptJumpCascades, 1);
1096 C += RunOptFunc (S, &DOptDeadJumps, 1);
1097 C += RunOptFunc (S, &DOptRTS, 1);
1098 C += RunOptFunc (S, &DOptDeadCode, 1);
1099 C += RunOptFunc (S, &DOptBoolTrans, 1);
1100 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1101 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1102 C += RunOptFunc (S, &DOptCondBranches1, 1);
1103 C += RunOptFunc (S, &DOptCondBranches2, 1);
1104 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1105 C += RunOptFunc (S, &DOptCmp1, 1);
1106 C += RunOptFunc (S, &DOptCmp2, 1);
1107 C += RunOptFunc (S, &DOptCmp3, 1);
1108 C += RunOptFunc (S, &DOptCmp4, 1);
1109 C += RunOptFunc (S, &DOptCmp5, 1);
1110 C += RunOptFunc (S, &DOptCmp6, 1);
1111 C += RunOptFunc (S, &DOptCmp7, 1);
1112 C += RunOptFunc (S, &DOptCmp8, 1);
1113 C += RunOptFunc (S, &DOptCmp9, 1);
1114 C += RunOptFunc (S, &DOptTest1, 1);
1115 C += RunOptFunc (S, &DOptLoad1, 1);
1116 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1117 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1118 C += RunOptFunc (S, &DOptUnusedStores, 1);
1119 C += RunOptFunc (S, &DOptDupLoads, 1);
1120 C += RunOptFunc (S, &DOptStoreLoad, 1);
1121 C += RunOptFunc (S, &DOptTransfers1, 1);
1122 C += RunOptFunc (S, &DOptTransfers3, 1);
1123 C += RunOptFunc (S, &DOptTransfers4, 1);
1124 C += RunOptFunc (S, &DOptStore1, 1);
1125 C += RunOptFunc (S, &DOptStore5, 1);
1126 C += RunOptFunc (S, &DOptPushPop, 1);
1127 C += RunOptFunc (S, &DOptPrecalc, 1);
1133 /* Return the number of changes */
1139 static unsigned RunOptGroup4 (CodeSeg* S)
1140 /* Run another round of pattern replacements. These are done late, since there
1141 * may be better replacements before.
1144 unsigned Changes = 0;
1146 /* Repeat some of the steps here */
1147 Changes += RunOptFunc (S, &DOptShift3, 1);
1148 Changes += RunOptFunc (S, &DOptPush1, 1);
1149 Changes += RunOptFunc (S, &DOptPush2, 1);
1150 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1151 Changes += RunOptFunc (S, &DOptTest2, 1);
1152 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1153 Changes += RunOptFunc (S, &DOptLoad2, 1);
1155 /* Return the number of changes */
1161 static unsigned RunOptGroup5 (CodeSeg* S)
1162 /* 65C02 specific optimizations. */
1164 unsigned Changes = 0;
1166 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1167 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1168 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1169 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1171 /* The 65C02 replacement codes do often make the use of a register
1172 * value unnecessary, so if we have changes, run another load
1175 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1179 /* Return the number of changes */
1185 static unsigned RunOptGroup6 (CodeSeg* S)
1186 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1187 * The latter is ony cycle slower, but if we're able to remove the necessary
1188 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1189 * shorten the code by one (transfer) or two bytes (load). So what we do is
1190 * to replace the insns, remove unused loads, and then change back all insns
1191 * where Y is still zero (meaning that the load has not been removed).
1194 unsigned Changes = 0;
1196 /* This group will only run for a standard 6502, because the 65C02 has a
1197 * better addressing mode that covers this case.
1199 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1200 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1201 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1202 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1205 /* Return the number of changes */
1211 static unsigned RunOptGroup7 (CodeSeg* S)
1212 /* The last group of optimization steps. Adjust branches, do size optimizations.
1215 unsigned Changes = 0;
1218 /* Optimize for size, that is replace operations by shorter ones, even
1219 * if this does hinder further optimizations (no problem since we're
1222 C = RunOptFunc (S, &DOptSize1, 1);
1225 /* Run some optimization passes again, since the size optimizations
1226 * may have opened new oportunities.
1228 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1229 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1230 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1231 Changes += RunOptFunc (S, &DOptStore5, 1);
1234 C = RunOptFunc (S, &DOptSize2, 1);
1237 /* Run some optimization passes again, since the size optimizations
1238 * may have opened new oportunities.
1240 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1241 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1242 Changes += RunOptFunc (S, &DOptStore5, 1);
1243 Changes += RunOptFunc (S, &DOptTransfers1, 1);
1244 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1247 /* Adjust branch distances */
1248 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1250 /* Replace conditional branches to RTS. If we had changes, we must run dead
1251 * code elimination again, since the change may have introduced dead code.
1253 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1256 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1259 /* Return the number of changes */
1265 void RunOpt (CodeSeg* S)
1266 /* Run the optimizer */
1268 const char* StatFileName;
1270 /* If we shouldn't run the optimizer, bail out */
1275 /* Check if we are requested to write optimizer statistics */
1276 StatFileName = getenv ("CC65_OPTSTATS");
1278 ReadOptStats (StatFileName);
1281 /* Print the name of the function we are working on */
1283 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1285 Print (stdout, 1, "Running optimizer for global code segment\n");
1288 /* Generate register info for all instructions */
1291 /* Run groups of optimizations */
1300 /* Free register info */
1303 /* Write statistics */
1305 WriteOptStats (StatFileName);