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 static unsigned OptLoad3 (CodeSeg* S)
228 /* Remove repeated loads from one and the same memory location */
230 unsigned Changes = 0;
233 /* Walk over the entries */
235 while (I < CS_GetEntryCount (S)) {
238 CodeEntry* E = CS_GetEntry (S, I);
240 /* Forget a preceeding load if we have a label */
241 if (Load && CE_HasLabel (E)) {
245 /* Check if this insn is a load */
246 if (E->Info & OF_LOAD) {
248 /* If we had a preceeding load that is identical, remove this one.
249 * If it is not identical, or we didn't have one, remember it.
252 E->OPC == Load->OPC &&
254 ((E->Arg == 0 && Load->Arg == 0) ||
255 strcmp (E->Arg, Load->Arg) == 0)) {
257 /* Now remove the call to the subroutine */
260 /* Remember, we had changes */
272 } else if ((E->Info & OF_CMP) == 0 && (E->Info & OF_CBRA) == 0) {
273 /* Forget the first load on occurance of any insn we don't like */
281 /* Return the number of changes made */
287 /*****************************************************************************/
288 /* Decouple operations */
289 /*****************************************************************************/
293 static unsigned OptDecouple (CodeSeg* S)
294 /* Decouple operations, that is, do the following replacements:
308 * Provided that the register values are known of course.
311 unsigned Changes = 0;
314 /* Walk over the entries */
316 while (I < CS_GetEntryCount (S)) {
320 /* Get next entry and it's input register values */
321 CodeEntry* E = CS_GetEntry (S, I);
322 const RegContents* In = &E->RI->In;
324 /* Assume we have no replacement */
327 /* Check the instruction */
331 if (RegValIsKnown (In->RegA)) {
332 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
333 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
338 if (RegValIsKnown (In->RegX)) {
339 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
340 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
345 if (RegValIsKnown (In->RegY)) {
346 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
347 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
352 if (RegValIsKnown (In->RegA)) {
353 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
354 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
359 if (RegValIsKnown (In->RegX)) {
360 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
361 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
366 if (RegValIsKnown (In->RegY)) {
367 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
368 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
373 if (E->AM == AM65_ZP) {
374 switch (GetKnownReg (E->Use & REG_ZP, In)) {
376 Arg = MakeHexArg (In->Tmp1);
377 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
381 Arg = MakeHexArg (In->Ptr1Lo);
382 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
386 Arg = MakeHexArg (In->Ptr1Hi);
387 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
391 Arg = MakeHexArg (In->SRegLo);
392 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
396 Arg = MakeHexArg (In->SRegHi);
397 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
404 if (E->AM == AM65_ZP) {
405 switch (GetKnownReg (E->Use & REG_ZP, In)) {
407 Arg = MakeHexArg (In->Tmp1);
408 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
412 Arg = MakeHexArg (In->Ptr1Lo);
413 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
417 Arg = MakeHexArg (In->Ptr1Hi);
418 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
422 Arg = MakeHexArg (In->SRegLo);
423 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
427 Arg = MakeHexArg (In->SRegHi);
428 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
435 if (E->AM == AM65_ZP) {
436 switch (GetKnownReg (E->Use, In)) {
438 Arg = MakeHexArg (In->Tmp1);
439 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
443 Arg = MakeHexArg (In->Ptr1Lo);
444 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
448 Arg = MakeHexArg (In->Ptr1Hi);
449 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
453 Arg = MakeHexArg (In->SRegLo);
454 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
458 Arg = MakeHexArg (In->SRegHi);
459 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
466 if (E->RI->In.RegA >= 0) {
467 Arg = MakeHexArg (In->RegA);
468 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
473 if (E->RI->In.RegA >= 0) {
474 Arg = MakeHexArg (In->RegA);
475 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
480 if (E->RI->In.RegX >= 0) {
481 Arg = MakeHexArg (In->RegX);
482 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
487 if (E->RI->In.RegY >= 0) {
488 Arg = MakeHexArg (In->RegY);
489 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
494 /* Avoid gcc warnings */
499 /* Insert the replacement if we have one */
501 CS_InsertEntry (S, X, I+1);
511 /* Return the number of changes made */
517 /*****************************************************************************/
518 /* Optimize stack pointer ops */
519 /*****************************************************************************/
523 static unsigned IsDecSP (const CodeEntry* E)
524 /* Check if this is an insn that decrements the stack pointer. If so, return
525 * the decrement. If not, return zero.
526 * The function expects E to be a subroutine call.
529 if (strncmp (E->Arg, "decsp", 5) == 0) {
530 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
531 return (E->Arg[5] - '0');
533 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
534 return E->RI->In.RegY;
537 /* If we come here, it's not a decsp op */
543 static unsigned OptStackPtrOps (CodeSeg* S)
544 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
548 unsigned Changes = 0;
551 /* Walk over the entries */
553 while (I < CS_GetEntryCount (S)) {
559 /* Get the next entry */
560 const CodeEntry* E = CS_GetEntry (S, I);
562 /* Check for decspn or subysp */
563 if (E->OPC == OP65_JSR &&
564 (Dec1 = IsDecSP (E)) > 0 &&
565 (N = CS_GetNextEntry (S, I)) != 0 &&
566 (Dec2 = IsDecSP (N)) > 0 &&
567 (Dec1 += Dec2) <= 255 &&
573 /* We can combine the two */
575 /* Insert a call to decsp */
576 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
577 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
578 CS_InsertEntry (S, X, I+2);
580 /* Insert a call to subysp */
581 const char* Arg = MakeHexArg (Dec1);
582 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
583 CS_InsertEntry (S, X, I+2);
584 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
585 CS_InsertEntry (S, X, I+3);
588 /* Delete the old code */
589 CS_DelEntries (S, I, 2);
591 /* Regenerate register info */
594 /* Remember we had changes */
605 /* Return the number of changes made */
611 /*****************************************************************************/
613 /*****************************************************************************/
617 typedef struct OptFunc OptFunc;
619 unsigned (*Func) (CodeSeg*); /* Optimizer function */
620 const char* Name; /* Name of the function/group */
621 unsigned CodeSizeFactor; /* Code size factor for this opt func */
622 unsigned long TotalRuns; /* Total number of runs */
623 unsigned long LastRuns; /* Last number of runs */
624 unsigned long TotalChanges; /* Total number of changes */
625 unsigned long LastChanges; /* Last number of changes */
626 char Disabled; /* True if function disabled */
631 /*****************************************************************************/
633 /*****************************************************************************/
637 /* A list of all the function descriptions */
638 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
639 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
640 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
641 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
642 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
643 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
644 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
645 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
646 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
647 static OptFunc DOptBNegA1 = { OptBNegA1, "OptBNegA1", 100, 0, 0, 0, 0, 0 };
648 static OptFunc DOptBNegA2 = { OptBNegA2, "OptBNegA2", 100, 0, 0, 0, 0, 0 };
649 static OptFunc DOptBNegAX1 = { OptBNegAX1, "OptBNegAX1", 100, 0, 0, 0, 0, 0 };
650 static OptFunc DOptBNegAX2 = { OptBNegAX2, "OptBNegAX2", 100, 0, 0, 0, 0, 0 };
651 static OptFunc DOptBNegAX3 = { OptBNegAX3, "OptBNegAX3", 100, 0, 0, 0, 0, 0 };
652 static OptFunc DOptBNegAX4 = { OptBNegAX4, "OptBNegAX4", 100, 0, 0, 0, 0, 0 };
653 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
654 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
655 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
656 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
657 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
658 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
659 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
660 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
661 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
662 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
663 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
664 static OptFunc DOptComplAX1 = { OptComplAX1, "OptComplAX1", 65, 0, 0, 0, 0, 0 };
665 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
666 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
667 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
668 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
669 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
670 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
671 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
672 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
673 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
674 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
675 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
676 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
677 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
678 static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
679 static OptFunc DOptLoad3 = { OptLoad3, "OptLoad3", 0, 0, 0, 0, 0, 0 };
680 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 165, 0, 0, 0, 0, 0 };
681 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 200, 0, 0, 0, 0, 0 };
682 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
683 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
684 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
685 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
686 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
687 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
688 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
689 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
690 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
691 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
692 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
693 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
694 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
695 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
696 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
697 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
698 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
699 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
700 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 65, 0, 0, 0, 0, 0 };
701 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 65, 0, 0, 0, 0, 0 };
702 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 100, 0, 0, 0, 0, 0 };
703 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
704 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
705 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
706 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
707 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
708 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 17, 0, 0, 0, 0, 0 };
709 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 100, 0, 0, 0, 0, 0 };
710 static OptFunc DOptShift5 = { OptShift5, "OptShift5", 110, 0, 0, 0, 0, 0 };
711 static OptFunc DOptShift6 = { OptShift6, "OptShift6", 200, 0, 0, 0, 0, 0 };
712 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
713 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
714 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
715 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
716 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
717 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 115, 0, 0, 0, 0, 0 };
718 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
719 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
720 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
721 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
722 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
723 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
724 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
725 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
726 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
727 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
728 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
729 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
730 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
731 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
732 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
735 /* Table containing all the steps in alphabetical order */
736 static OptFunc* OptFuncs[] = {
833 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
837 static int CmpOptStep (const void* Key, const void* Func)
838 /* Compare function for bsearch */
840 return strcmp (Key, (*(const OptFunc**)Func)->Name);
845 static OptFunc* FindOptFunc (const char* Name)
846 /* Find an optimizer step by name in the table and return a pointer. Return
847 * NULL if no such step is found.
850 /* Search for the function in the list */
851 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
857 static OptFunc* GetOptFunc (const char* Name)
858 /* Find an optimizer step by name in the table and return a pointer. Print an
859 * error and call AbEnd if not found.
862 /* Search for the function in the list */
863 OptFunc* F = FindOptFunc (Name);
866 AbEnd ("Optimization step `%s' not found", Name);
873 void DisableOpt (const char* Name)
874 /* Disable the optimization with the given name */
876 if (strcmp (Name, "any") == 0) {
878 for (I = 0; I < OPTFUNC_COUNT; ++I) {
879 OptFuncs[I]->Disabled = 1;
882 GetOptFunc(Name)->Disabled = 1;
888 void EnableOpt (const char* Name)
889 /* Enable the optimization with the given name */
891 if (strcmp (Name, "any") == 0) {
893 for (I = 0; I < OPTFUNC_COUNT; ++I) {
894 OptFuncs[I]->Disabled = 0;
897 GetOptFunc(Name)->Disabled = 0;
903 void ListOptSteps (FILE* F)
904 /* List all optimization steps */
907 for (I = 0; I < OPTFUNC_COUNT; ++I) {
908 fprintf (F, "%s\n", OptFuncs[I]->Name);
914 static void ReadOptStats (const char* Name)
915 /* Read the optimizer statistics file */
920 /* Try to open the file */
921 FILE* F = fopen (Name, "r");
923 /* Ignore the error */
927 /* Read and parse the lines */
929 while (fgets (Buf, sizeof (Buf), F) != 0) {
937 unsigned long TotalRuns;
938 unsigned long TotalChanges;
943 /* Remove trailing white space including the line terminator */
946 while (Len > 0 && IsSpace (B[Len-1])) {
951 /* Remove leading whitespace */
952 while (IsSpace (*B)) {
956 /* Check for empty and comment lines */
957 if (*B == '\0' || *B == ';' || *B == '#') {
962 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
967 /* Search for the optimizer step. */
968 Func = FindOptFunc (Name);
974 /* Found the step, set the fields */
975 Func->TotalRuns = TotalRuns;
976 Func->TotalChanges = TotalChanges;
980 /* Close the file, ignore errors here. */
986 static void WriteOptStats (const char* Name)
987 /* Write the optimizer statistics file */
991 /* Try to open the file */
992 FILE* F = fopen (Name, "w");
994 /* Ignore the error */
1000 "; Optimizer Total Last Total Last\n"
1001 "; Step Runs Runs Chg Chg\n");
1004 /* Write the data */
1005 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1006 const OptFunc* O = OptFuncs[I];
1008 "%-20s %10lu %10lu %10lu %10lu\n",
1016 /* Close the file, ignore errors here. */
1022 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1023 /* Run one optimizer function Max times or until there are no more changes */
1025 unsigned Changes, C;
1027 /* Don't run the function if it is disabled or if it is prohibited by the
1030 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1034 /* Run this until there are no more changes */
1038 /* Run the function */
1040 if (Debug && C > 0) {
1041 printf ("Applied %s: %u changes\n", F->Name, C);
1048 F->TotalChanges += C;
1049 F->LastChanges += C;
1051 /* If we had changes, regenerate register info */
1056 } while (--Max && C > 0);
1058 /* Return the number of changes */
1064 static unsigned RunOptGroup1 (CodeSeg* S)
1065 /* Run the first group of optimization steps. These steps translate known
1066 * patterns emitted by the code generator into more optimal patterns. Order
1067 * of the steps is important, because some of the steps done earlier cover
1068 * the same patterns as later steps as subpatterns.
1071 unsigned Changes = 0;
1073 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1074 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1075 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1076 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1077 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1078 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1079 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1080 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1081 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1082 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1083 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1084 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1085 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1086 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1087 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1088 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1089 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1090 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1091 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1092 Changes += RunOptFunc (S, &DOptBNegAX1, 1);
1093 Changes += RunOptFunc (S, &DOptBNegAX2, 1);
1094 Changes += RunOptFunc (S, &DOptBNegAX3, 1);
1095 Changes += RunOptFunc (S, &DOptBNegAX4, 1);
1096 Changes += RunOptFunc (S, &DOptAdd1, 1);
1097 Changes += RunOptFunc (S, &DOptAdd2, 1);
1098 Changes += RunOptFunc (S, &DOptAdd4, 1);
1099 Changes += RunOptFunc (S, &DOptAdd5, 1);
1100 Changes += RunOptFunc (S, &DOptAdd6, 1);
1101 Changes += RunOptFunc (S, &DOptSub1, 1);
1102 Changes += RunOptFunc (S, &DOptSub3, 1);
1103 Changes += RunOptFunc (S, &DOptStore4, 1);
1104 Changes += RunOptFunc (S, &DOptStore5, 1);
1105 Changes += RunOptFunc (S, &DOptShift1, 1);
1106 Changes += RunOptFunc (S, &DOptShift2, 1);
1107 Changes += RunOptFunc (S, &DOptShift5, 1);
1108 Changes += RunOptFunc (S, &DOptShift6, 1);
1109 Changes += RunOptFunc (S, &DOptStore1, 1);
1110 Changes += RunOptFunc (S, &DOptStore2, 5);
1111 Changes += RunOptFunc (S, &DOptStore3, 5);
1113 /* Return the number of changes */
1119 static unsigned RunOptGroup2 (CodeSeg* S)
1120 /* Run one group of optimization steps. This step involves just decoupling
1121 * instructions by replacing them by instructions that do not depend on
1122 * previous instructions. This makes it easier to find instructions that
1126 unsigned Changes = 0;
1128 Changes += RunOptFunc (S, &DOptDecouple, 1);
1130 /* Return the number of changes */
1136 static unsigned RunOptGroup3 (CodeSeg* S)
1137 /* Run one group of optimization steps. These steps depend on each other,
1138 * that means that one step may allow another step to do additional work,
1139 * so we will repeat the steps as long as we see any changes.
1142 unsigned Changes, C;
1148 C += RunOptFunc (S, &DOptBNegA1, 1);
1149 C += RunOptFunc (S, &DOptBNegA2, 1);
1150 C += RunOptFunc (S, &DOptNegAX1, 1);
1151 C += RunOptFunc (S, &DOptNegAX2, 1);
1152 C += RunOptFunc (S, &DOptStackOps, 3);
1153 C += RunOptFunc (S, &DOptShift1, 1);
1154 C += RunOptFunc (S, &DOptShift4, 1);
1155 C += RunOptFunc (S, &DOptComplAX1, 1);
1156 C += RunOptFunc (S, &DOptSub1, 1);
1157 C += RunOptFunc (S, &DOptSub2, 1);
1158 C += RunOptFunc (S, &DOptSub3, 1);
1159 C += RunOptFunc (S, &DOptAdd5, 1);
1160 C += RunOptFunc (S, &DOptAdd6, 1);
1161 C += RunOptFunc (S, &DOptJumpCascades, 1);
1162 C += RunOptFunc (S, &DOptDeadJumps, 1);
1163 C += RunOptFunc (S, &DOptRTS, 1);
1164 C += RunOptFunc (S, &DOptDeadCode, 1);
1165 C += RunOptFunc (S, &DOptBoolTrans, 1);
1166 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1167 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1168 C += RunOptFunc (S, &DOptCondBranches1, 1);
1169 C += RunOptFunc (S, &DOptCondBranches2, 1);
1170 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1171 C += RunOptFunc (S, &DOptCmp1, 1);
1172 C += RunOptFunc (S, &DOptCmp2, 1);
1173 C += RunOptFunc (S, &DOptCmp8, 1); /* Must run before OptCmp3 */
1174 C += RunOptFunc (S, &DOptCmp3, 1);
1175 C += RunOptFunc (S, &DOptCmp4, 1);
1176 C += RunOptFunc (S, &DOptCmp5, 1);
1177 C += RunOptFunc (S, &DOptCmp6, 1);
1178 C += RunOptFunc (S, &DOptCmp7, 1);
1179 C += RunOptFunc (S, &DOptCmp9, 1);
1180 C += RunOptFunc (S, &DOptTest1, 1);
1181 C += RunOptFunc (S, &DOptLoad1, 1);
1182 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1183 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1184 C += RunOptFunc (S, &DOptUnusedStores, 1);
1185 C += RunOptFunc (S, &DOptDupLoads, 1);
1186 C += RunOptFunc (S, &DOptStoreLoad, 1);
1187 C += RunOptFunc (S, &DOptTransfers1, 1);
1188 C += RunOptFunc (S, &DOptTransfers3, 1);
1189 C += RunOptFunc (S, &DOptTransfers4, 1);
1190 C += RunOptFunc (S, &DOptStore1, 1);
1191 C += RunOptFunc (S, &DOptStore5, 1);
1192 C += RunOptFunc (S, &DOptPushPop, 1);
1193 C += RunOptFunc (S, &DOptPrecalc, 1);
1199 /* Return the number of changes */
1205 static unsigned RunOptGroup4 (CodeSeg* S)
1206 /* Run another round of pattern replacements. These are done late, since there
1207 * may be better replacements before.
1210 unsigned Changes = 0;
1212 /* Repeat some of the steps here */
1213 Changes += RunOptFunc (S, &DOptShift3, 1);
1214 Changes += RunOptFunc (S, &DOptPush1, 1);
1215 Changes += RunOptFunc (S, &DOptPush2, 1);
1216 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1217 Changes += RunOptFunc (S, &DOptTest2, 1);
1218 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1219 Changes += RunOptFunc (S, &DOptLoad2, 1);
1220 Changes += RunOptFunc (S, &DOptLoad3, 1);
1222 /* Return the number of changes */
1228 static unsigned RunOptGroup5 (CodeSeg* S)
1229 /* 65C02 specific optimizations. */
1231 unsigned Changes = 0;
1233 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1234 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1235 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1236 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1238 /* The 65C02 replacement codes do often make the use of a register
1239 * value unnecessary, so if we have changes, run another load
1242 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1246 /* Return the number of changes */
1252 static unsigned RunOptGroup6 (CodeSeg* S)
1253 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1254 * The latter is ony cycle slower, but if we're able to remove the necessary
1255 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1256 * shorten the code by one (transfer) or two bytes (load). So what we do is
1257 * to replace the insns, remove unused loads, and then change back all insns
1258 * where Y is still zero (meaning that the load has not been removed).
1261 unsigned Changes = 0;
1263 /* This group will only run for a standard 6502, because the 65C02 has a
1264 * better addressing mode that covers this case.
1266 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1267 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1268 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1269 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1272 /* Return the number of changes */
1278 static unsigned RunOptGroup7 (CodeSeg* S)
1279 /* The last group of optimization steps. Adjust branches, do size optimizations.
1282 unsigned Changes = 0;
1285 /* Optimize for size, that is replace operations by shorter ones, even
1286 * if this does hinder further optimizations (no problem since we're
1289 C = RunOptFunc (S, &DOptSize1, 1);
1292 /* Run some optimization passes again, since the size optimizations
1293 * may have opened new oportunities.
1295 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1296 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1297 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1298 Changes += RunOptFunc (S, &DOptStore5, 1);
1301 C = RunOptFunc (S, &DOptSize2, 1);
1304 /* Run some optimization passes again, since the size optimizations
1305 * may have opened new oportunities.
1307 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1308 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1309 Changes += RunOptFunc (S, &DOptStore5, 1);
1310 Changes += RunOptFunc (S, &DOptTransfers1, 1);
1311 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1314 /* Adjust branch distances */
1315 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1317 /* Replace conditional branches to RTS. If we had changes, we must run dead
1318 * code elimination again, since the change may have introduced dead code.
1320 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1323 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1326 /* Return the number of changes */
1332 void RunOpt (CodeSeg* S)
1333 /* Run the optimizer */
1335 const char* StatFileName;
1337 /* If we shouldn't run the optimizer, bail out */
1342 /* Check if we are requested to write optimizer statistics */
1343 StatFileName = getenv ("CC65_OPTSTATS");
1345 ReadOptStats (StatFileName);
1348 /* Print the name of the function we are working on */
1350 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1352 Print (stdout, 1, "Running optimizer for global code segment\n");
1355 /* Generate register info for all instructions */
1358 /* Run groups of optimizations */
1367 /* Free register info */
1370 /* Write statistics */
1372 WriteOptStats (StatFileName);