]> git.sur5r.net Git - cc65/blob - src/cc65/codeopt.c
Fixed a bug
[cc65] / src / cc65 / codeopt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeopt.c                                 */
4 /*                                                                           */
5 /*                           Optimizer subroutines                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2003 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 <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "abend.h"
41 #include "chartype.h"
42 #include "cpu.h"
43 #include "print.h"
44 #include "xmalloc.h"
45 #include "xsprintf.h"
46
47 /* cc65 */
48 #include "asmlabel.h"
49 #include "codeent.h"
50 #include "codeinfo.h"
51 #include "coptadd.h"
52 #include "coptc02.h"
53 #include "coptcmp.h"
54 #include "coptind.h"
55 #include "coptneg.h"
56 #include "coptpush.h"
57 #include "coptsize.h"
58 #include "coptstop.h"
59 #include "coptstore.h"
60 #include "coptsub.h"
61 #include "copttest.h"
62 #include "error.h"
63 #include "global.h"
64 #include "codeopt.h"
65
66
67
68 /*****************************************************************************/
69 /*                                     Data                                  */
70 /*****************************************************************************/
71
72
73
74 /* Shift types */
75 enum {
76     SHIFT_NONE,
77     SHIFT_ASR_1,
78     SHIFT_ASL_1,
79     SHIFT_LSR_1,
80     SHIFT_LSL_1
81 };
82
83
84
85 /*****************************************************************************/
86 /*                              Optimize shifts                              */
87 /*****************************************************************************/
88
89
90
91 static unsigned OptShift1 (CodeSeg* S)
92 /* A call to the shlaxN routine may get replaced by one or more asl insns
93  * if the value of X is not used later.
94  */
95 {
96     unsigned Changes = 0;
97
98     /* Walk over the entries */
99     unsigned I = 0;
100     while (I < CS_GetEntryCount (S)) {
101
102         /* Get next entry */
103         CodeEntry* E = CS_GetEntry (S, I);
104
105         /* Check for the sequence */
106         if (E->OPC == OP65_JSR                       &&
107             (strncmp (E->Arg, "shlax", 5) == 0 ||
108              strncmp (E->Arg, "aslax", 5) == 0)      &&
109             strlen (E->Arg) == 6                     &&
110             IsDigit (E->Arg[5])                      &&
111             !RegXUsed (S, I+1)) {
112
113             /* Insert shift insns */
114             unsigned Count = E->Arg[5] - '0';
115             while (Count--) {
116                 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
117                 CS_InsertEntry (S, X, I+1);
118             }
119
120             /* Delete the call to shlax */
121             CS_DelEntry (S, I);
122
123             /* Remember, we had changes */
124             ++Changes;
125
126         }
127
128         /* Next entry */
129         ++I;
130
131     }
132
133     /* Return the number of changes made */
134     return Changes;
135 }
136
137
138
139 static unsigned OptShift2 (CodeSeg* S)
140 /* A call to the shraxN routine may get replaced by one or more lsr insns
141  * if the value of X is not used later, or if the value of X is known and
142  * zero.
143  */
144 {
145     unsigned Changes = 0;
146     unsigned I;
147
148     /* Generate register info */
149     CS_GenRegInfo (S);
150
151     /* Walk over the entries */
152     I = 0;
153     while (I < CS_GetEntryCount (S)) {
154
155         /* Get next entry */
156         CodeEntry* E = CS_GetEntry (S, I);
157
158         /* Check for the sequence */
159         if (E->OPC == OP65_JSR                       &&
160             strncmp (E->Arg, "shrax", 5) == 0        &&
161             strlen (E->Arg) == 6                     &&
162             IsDigit (E->Arg[5])                      &&
163             (E->RI->In.RegX == 0 || !RegXUsed (S, I+1))) {
164
165             /* Insert shift insns */
166             unsigned Count = E->Arg[5] - '0';
167             while (Count--) {
168                 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
169                 CS_InsertEntry (S, X, I+1);
170             }
171
172             /* Delete the call to shlax */
173             CS_DelEntry (S, I);
174
175             /* Remember, we had changes */
176             ++Changes;
177
178         }
179
180         /* Next entry */
181         ++I;
182
183     }
184
185     /* Free the register info */
186     CS_FreeRegInfo (S);
187
188     /* Return the number of changes made */
189     return Changes;
190 }
191
192
193
194 static unsigned GetShiftType (const char* Sub)
195 /* Helper function for OptShift3 */
196 {
197     if (*Sub == 'a') {
198         if (strcmp (Sub+1, "slax1") == 0) {
199             return SHIFT_ASL_1;
200         } else if (strcmp (Sub+1, "srax1") == 0) {
201             return SHIFT_ASR_1;
202         }
203     } else if (*Sub == 's') {
204         if (strcmp (Sub+1, "hlax1") == 0) {
205             return SHIFT_LSL_1;
206         } else if (strcmp (Sub+1, "hrax1") == 0) {
207             return SHIFT_LSR_1;
208         }
209     }
210     return SHIFT_NONE;
211 }
212
213
214
215 static unsigned OptShift3 (CodeSeg* S)
216 /* Search for the sequence
217  *
218  *      lda     xxx
219  *      ldx     yyy
220  *      jsr     aslax1/asrax1/shlax1/shrax1
221  *      sta     aaa
222  *      stx     bbb
223  *
224  * and replace it by
225  *
226  *      lda     xxx
227  *      asl     a
228  *      sta     aaa
229  *      lda     yyy
230  *      rol     a
231  *      sta     bbb
232  *
233  * or similar, provided that a/x is not used later
234  */
235 {
236     unsigned Changes = 0;
237
238     /* Walk over the entries */
239     unsigned I = 0;
240     while (I < CS_GetEntryCount (S)) {
241
242         unsigned ShiftType;
243         CodeEntry* L[5];
244
245         /* Get next entry */
246         L[0] = CS_GetEntry (S, I);
247
248         /* Check for the sequence */
249         if (L[0]->OPC == OP65_LDA                               &&
250             (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP)       &&
251             CS_GetEntries (S, L+1, I+1, 4)                      &&
252             !CS_RangeHasLabel (S, I+1, 4)                       &&
253             L[1]->OPC == OP65_LDX                               &&
254             (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP)       &&
255             L[2]->OPC == OP65_JSR                               &&
256             (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
257             L[3]->OPC == OP65_STA                               &&
258             (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP)       &&
259             L[4]->OPC == OP65_STX                               &&
260             (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP)       &&
261             !RegAXUsed (S, I+5)) {
262
263             CodeEntry* X;
264
265             /* Handle the four shift types differently */
266             switch (ShiftType) {
267
268                 case SHIFT_ASR_1:
269                     X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
270                     CS_InsertEntry (S, X, I+5);
271                     X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
272                     CS_InsertEntry (S, X, I+6);
273                     X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
274                     CS_InsertEntry (S, X, I+7);
275                     X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
276                     CS_InsertEntry (S, X, I+8);
277                     X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
278                     CS_InsertEntry (S, X, I+9);
279                     X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
280                     CS_InsertEntry (S, X, I+10);
281                     X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
282                     CS_InsertEntry (S, X, I+11);
283                     CS_DelEntries (S, I, 5);
284                     break;
285
286                 case SHIFT_LSR_1:
287                     X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
288                     CS_InsertEntry (S, X, I+5);
289                     X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
290                     CS_InsertEntry (S, X, I+6);
291                     X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
292                     CS_InsertEntry (S, X, I+7);
293                     X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
294                     CS_InsertEntry (S, X, I+8);
295                     X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
296                     CS_InsertEntry (S, X, I+9);
297                     X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
298                     CS_InsertEntry (S, X, I+10);
299                     CS_DelEntries (S, I, 5);
300                     break;
301
302                 case SHIFT_LSL_1:
303                 case SHIFT_ASL_1:
304                     /* These two are identical */
305                     X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
306                     CS_InsertEntry (S, X, I+1);
307                     X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
308                     CS_InsertEntry (S, X, I+2);
309                     X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
310                     CS_InsertEntry (S, X, I+3);
311                     X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
312                     CS_InsertEntry (S, X, I+4);
313                     X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
314                     CS_InsertEntry (S, X, I+5);
315                     CS_DelEntries (S, I+6, 4);
316                     break;
317
318             }
319
320             /* Remember, we had changes */
321             ++Changes;
322
323         }
324
325         /* Next entry */
326         ++I;
327
328     }
329
330     /* Return the number of changes made */
331     return Changes;
332 }
333
334
335
336 /*****************************************************************************/
337 /*                              Optimize loads                               */
338 /*****************************************************************************/
339
340
341
342 static unsigned OptLoad1 (CodeSeg* S)
343 /* Search for a call to ldaxysp where X is not used later and replace it by
344  * a load of just the A register.
345  */
346 {
347     unsigned I;
348     unsigned Changes = 0;
349
350     /* Generate register info */
351     CS_GenRegInfo (S);
352
353     /* Walk over the entries */
354     I = 0;
355     while (I < CS_GetEntryCount (S)) {
356
357         CodeEntry* E;
358
359         /* Get next entry */
360         E = CS_GetEntry (S, I);
361
362         /* Check for the sequence */
363         if (CE_IsCallTo (E, "ldaxysp")          &&
364             RegValIsKnown (E->RI->In.RegY)      &&
365             !RegXUsed (S, I+1)) {
366
367             CodeEntry* X;
368
369             /* Reload the Y register */
370             const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
371             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
372             CS_InsertEntry (S, X, I+1);
373
374             /* Load from stack */
375             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
376             CS_InsertEntry (S, X, I+2);
377
378             /* Now remove the call to the subroutine */
379             CS_DelEntry (S, I);
380
381             /* Remember, we had changes */
382             ++Changes;
383
384         }
385
386         /* Next entry */
387         ++I;
388
389     }
390
391     /* Free the register info */
392     CS_FreeRegInfo (S);
393
394     /* Return the number of changes made */
395     return Changes;
396 }
397
398
399
400 /*****************************************************************************/
401 /*                     Optimize stores through pointers                      */
402 /*****************************************************************************/
403
404
405
406 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
407 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
408 {
409     /* Check for a label attached to the entry */
410     if (CE_HasLabel (L[0])) {
411         return 0;
412     }
413
414     /* Check for single insn sub ops */
415     if (L[0]->OPC == OP65_AND                                           ||
416         L[0]->OPC == OP65_EOR                                           ||
417         L[0]->OPC == OP65_ORA                                           ||
418         (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
419         (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
420
421         /* One insn */
422         return 1;
423
424     } else if (L[0]->OPC == OP65_CLC                      &&
425                (L[1] = CS_GetNextEntry (S, I)) != 0       &&
426                L[1]->OPC == OP65_ADC                      &&
427                !CE_HasLabel (L[1])) {
428         return 2;
429     } else if (L[0]->OPC == OP65_SEC                      &&
430                (L[1] = CS_GetNextEntry (S, I)) != 0       &&
431                L[1]->OPC == OP65_SBC                      &&
432                !CE_HasLabel (L[1])) {
433         return 2;
434     }
435
436
437
438     /* Not found */
439     return 0;
440 }
441
442
443
444 static unsigned OptPtrStore1 (CodeSeg* S)
445 /* Search for the sequence:
446  *
447  *      jsr     pushax
448  *      ldy     xxx
449  *      jsr     ldauidx
450  *      subop
451  *      ldy     yyy
452  *      jsr     staspidx
453  *
454  * and replace it by:
455  *
456  *      sta     ptr1
457  *      stx     ptr1+1
458  *      ldy     xxx
459  *      ldx     #$00
460  *      lda     (ptr1),y
461  *      subop
462  *      ldy     yyy
463  *      sta     (ptr1),y
464  */
465 {
466     unsigned Changes = 0;
467
468     /* Walk over the entries */
469     unsigned I = 0;
470     while (I < CS_GetEntryCount (S)) {
471
472         unsigned K;
473         CodeEntry* L[10];
474
475         /* Get next entry */
476         L[0] = CS_GetEntry (S, I);
477
478         /* Check for the sequence */
479         if (CE_IsCallTo (L[0], "pushax")            &&
480             CS_GetEntries (S, L+1, I+1, 3)          &&
481             L[1]->OPC == OP65_LDY                   &&
482             CE_KnownImm (L[1])                      &&
483             !CE_HasLabel (L[1])                     &&
484             CE_IsCallTo (L[2], "ldauidx")           &&
485             !CE_HasLabel (L[2])                     &&
486             (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
487             CS_GetEntries (S, L+3+K, I+3+K, 2)      &&
488             L[3+K]->OPC == OP65_LDY                 &&
489             CE_KnownImm (L[3+K])                    &&
490             !CE_HasLabel (L[3+K])                   &&
491             CE_IsCallTo (L[4+K], "staspidx")        &&
492             !CE_HasLabel (L[4+K])) {
493
494             CodeEntry* X;
495
496             /* Create and insert the stores */
497             X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
498             CS_InsertEntry (S, X, I+1);
499
500             X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
501             CS_InsertEntry (S, X, I+2);
502
503             /* Insert the load from ptr1 */
504             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
505             CS_InsertEntry (S, X, I+5);
506             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
507             CS_InsertEntry (S, X, I+6);
508
509             /* Insert the store through ptr1 */
510             X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
511             CS_InsertEntry (S, X, I+8+K);
512
513             /* Delete the old code */
514             CS_DelEntry (S, I+9+K);     /* jsr spaspidx */
515             CS_DelEntry (S, I+4);       /* jsr ldauidx */
516             CS_DelEntry (S, I);         /* jsr pushax */
517
518             /* Remember, we had changes */
519             ++Changes;
520
521         }
522
523         /* Next entry */
524         ++I;
525
526     }
527
528     /* Return the number of changes made */
529     return Changes;
530 }
531
532
533
534 /*****************************************************************************/
535 /*                      Optimize loads through pointers                      */
536 /*****************************************************************************/
537
538
539
540 static unsigned OptPtrLoad1 (CodeSeg* S)
541 /* Search for the sequence:
542  *
543  *      clc
544  *      adc     xxx
545  *      tay
546  *      txa
547  *      adc     yyy
548  *      tax
549  *      tya
550  *      ldy
551  *      jsr     ldauidx
552  *
553  * and replace it by:
554  *
555  *      clc
556  *      adc     xxx
557  *      sta     ptr1
558  *      txa
559  *      adc     yyy
560  *      sta     ptr1+1
561  *      ldy
562  *      ldx     #$00
563  *      lda     (ptr1),y
564  */
565 {
566     unsigned Changes = 0;
567
568     /* Walk over the entries */
569     unsigned I = 0;
570     while (I < CS_GetEntryCount (S)) {
571
572         CodeEntry* L[9];
573
574         /* Get next entry */
575         L[0] = CS_GetEntry (S, I);
576
577         /* Check for the sequence */
578         if (L[0]->OPC == OP65_CLC               &&
579             CS_GetEntries (S, L+1, I+1, 8)      &&
580             L[1]->OPC == OP65_ADC               &&
581             L[2]->OPC == OP65_TAY               &&
582             L[3]->OPC == OP65_TXA               &&
583             L[4]->OPC == OP65_ADC               &&
584             L[5]->OPC == OP65_TAX               &&
585             L[6]->OPC == OP65_TYA               &&
586             L[7]->OPC == OP65_LDY               &&
587             CE_IsCallTo (L[8], "ldauidx")       &&
588             !CS_RangeHasLabel (S, I+1, 8)) {
589
590             CodeEntry* X;
591             CodeEntry* P;
592
593             /* Track the insertion point */
594             unsigned IP = I+2;
595
596             /* sta ptr1 */
597             X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
598             CS_InsertEntry (S, X, IP++);
599
600             /* If the instruction before the clc is a ldx, replace the
601              * txa by an lda with the same location of the ldx. Otherwise
602              * transfer the value in X to A.
603              */
604             if ((P = CS_GetPrevEntry (S, I)) != 0 &&
605                 P->OPC == OP65_LDX                &&
606                 !CE_HasLabel (P)) {
607                 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
608             } else {
609                 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
610             }
611             CS_InsertEntry (S, X, IP++);
612
613             /* adc yyy */
614             X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
615             CS_InsertEntry (S, X, IP++);
616
617             /* sta ptr1+1 */
618             X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
619             CS_InsertEntry (S, X, IP++);
620
621             /* ldy ... */
622             X = NewCodeEntry (OP65_LDY, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
623             CS_InsertEntry (S, X, IP++);
624
625             /* ldx #$00 */
626             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
627             CS_InsertEntry (S, X, IP++);
628
629             /* lda (ptr1),y */
630             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
631             CS_InsertEntry (S, X, IP++);
632
633             /* Remove the old instructions */
634             CS_DelEntries (S, IP, 7);
635
636             /* Remember, we had changes */
637             ++Changes;
638
639         }
640
641         /* Next entry */
642         ++I;
643
644     }
645
646     /* Return the number of changes made */
647     return Changes;
648 }
649
650
651
652 static unsigned OptPtrLoad2 (CodeSeg* S)
653 /* Search for the sequence:
654  *
655  *      adc     xxx
656  *      pha
657  *      txa
658  *      iny
659  *      adc     yyy
660  *      tax
661  *      pla
662  *      ldy
663  *      jsr     ldauidx
664  *
665  * and replace it by:
666  *
667  *      adc     xxx
668  *      sta     ptr1
669  *      txa
670  *      iny
671  *      adc     yyy
672  *      sta     ptr1+1
673  *      ldy
674  *      ldx     #$00
675  *      lda     (ptr1),y
676  *
677  *      adc     xxx
678  *      sta     ptr1
679  *      pha
680  *      txa
681  *      iny
682  *      adc     yyy
683  *      sta     ptr1+1
684  *      tax
685  *      pla
686  *      ldy
687  *      ldx     #$00
688  *      lda     (ptr1),y
689  *      jsr     ldauidx
690  */
691 {
692     unsigned Changes = 0;
693
694     /* Walk over the entries */
695     unsigned I = 0;
696     while (I < CS_GetEntryCount (S)) {
697
698         CodeEntry* L[9];
699
700         /* Get next entry */
701         L[0] = CS_GetEntry (S, I);
702
703         /* Check for the sequence */
704         if (L[0]->OPC == OP65_ADC               &&
705             CS_GetEntries (S, L+1, I+1, 8)      &&
706             L[1]->OPC == OP65_PHA               &&
707             L[2]->OPC == OP65_TXA               &&
708             L[3]->OPC == OP65_INY               &&
709             L[4]->OPC == OP65_ADC               &&
710             L[5]->OPC == OP65_TAX               &&
711             L[6]->OPC == OP65_PLA               &&
712             L[7]->OPC == OP65_LDY               &&
713             CE_IsCallTo (L[8], "ldauidx")       &&
714             !CS_RangeHasLabel (S, I+1, 8)) {
715
716             CodeEntry* X;
717
718             /* Store the low byte and remove the PHA instead */
719             X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
720             CS_InsertEntry (S, X, I+1);
721
722             /* Store the high byte */
723             X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
724             CS_InsertEntry (S, X, I+6);
725
726             /* Load high and low byte */
727             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
728             CS_InsertEntry (S, X, I+10);
729             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
730             CS_InsertEntry (S, X, I+11);
731
732             /* Delete the old code */
733             CS_DelEntry (S, I+12);      /* jsr ldauidx */
734             CS_DelEntry (S, I+8);       /* pla */
735             CS_DelEntry (S, I+7);       /* tax */
736             CS_DelEntry (S, I+2);       /* pha */
737
738             /* Remember, we had changes */
739             ++Changes;
740
741         }
742
743         /* Next entry */
744         ++I;
745
746     }
747
748     /* Return the number of changes made */
749     return Changes;
750 }
751
752
753
754 static unsigned OptPtrLoad3 (CodeSeg* S)
755 /* Search for the sequence:
756  *
757  *      lda     #<(label+0)
758  *      ldx     #>(label+0)
759  *      clc
760  *      adc     xxx
761  *      bcc     L
762  *      inx
763  * L:   ldy     #$00
764  *      jsr     ldauidx
765  *
766  * and replace it by:
767  *
768  *      ldy     xxx
769  *      ldx     #$00
770  *      lda     label,y
771  */
772 {
773     unsigned Changes = 0;
774
775     /* Walk over the entries */
776     unsigned I = 0;
777     while (I < CS_GetEntryCount (S)) {
778
779         CodeEntry* L[8];
780         unsigned Len;
781
782         /* Get next entry */
783         L[0] = CS_GetEntry (S, I);
784
785         /* Check for the sequence */
786         if (L[0]->OPC == OP65_LDA                            &&
787             L[0]->AM == AM65_IMM                             &&
788             CS_GetEntries (S, L+1, I+1, 7)                   &&
789             L[1]->OPC == OP65_LDX                            &&
790             L[1]->AM == AM65_IMM                             &&
791             L[2]->OPC == OP65_CLC                            &&
792             L[3]->OPC == OP65_ADC                            &&
793             (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP)    &&
794             (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
795             L[4]->JumpTo != 0                                &&
796             L[4]->JumpTo->Owner == L[6]                      &&
797             L[5]->OPC == OP65_INX                            &&
798             L[6]->OPC == OP65_LDY                            &&
799             CE_KnownImm (L[6])                               &&
800             L[6]->Num == 0                                   &&
801             CE_IsCallTo (L[7], "ldauidx")                    &&
802             !CS_RangeHasLabel (S, I+1, 7)                    &&
803             /* Check the label last because this is quite costly */
804             (Len = strlen (L[0]->Arg)) > 3                   &&
805             L[0]->Arg[0] == '<'                              &&
806             L[0]->Arg[1] == '('                              &&
807             strlen (L[1]->Arg) == Len                        &&
808             L[1]->Arg[0] == '>'                              &&
809             memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
810
811             CodeEntry* X;
812             char* Label;
813
814             /* We will create all the new stuff behind the current one so
815              * we keep the line references.
816              */
817             X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
818             CS_InsertEntry (S, X, I+8);
819
820             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
821             CS_InsertEntry (S, X, I+9);
822
823             Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
824             Label[Len-3] = '\0';
825             X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
826             CS_InsertEntry (S, X, I+10);
827             xfree (Label);
828
829             /* Remove the old code */
830             CS_DelEntries (S, I, 8);
831
832             /* Remember, we had changes */
833             ++Changes;
834
835         }
836
837         /* Next entry */
838         ++I;
839
840     }
841
842     /* Return the number of changes made */
843     return Changes;
844 }
845
846
847
848 static unsigned OptPtrLoad4 (CodeSeg* S)
849 /* Search for the sequence:
850  *
851  *      lda     #<(label+0)
852  *      ldx     #>(label+0)
853  *      ldy     #$xx
854  *      clc
855  *      adc     (sp),y
856  *      bcc     L
857  *      inx
858  * L:   ldy     #$00
859  *      jsr     ldauidx
860  *
861  * and replace it by:
862  *
863  *      ldy     #$xx
864  *      lda     (sp),y
865  *      tay
866  *      ldx     #$00
867  *      lda     label,y
868  */
869 {
870     unsigned Changes = 0;
871
872     /* Walk over the entries */
873     unsigned I = 0;
874     while (I < CS_GetEntryCount (S)) {
875
876         CodeEntry* L[9];
877         unsigned Len;
878
879         /* Get next entry */
880         L[0] = CS_GetEntry (S, I);
881
882         /* Check for the sequence */
883         if (L[0]->OPC == OP65_LDA                            &&
884             L[0]->AM == AM65_IMM                             &&
885             CS_GetEntries (S, L+1, I+1, 8)                   &&
886             L[1]->OPC == OP65_LDX                            &&
887             L[1]->AM == AM65_IMM                             &&
888             !CE_HasLabel (L[1])                              &&
889             L[2]->OPC == OP65_LDY                            &&
890             CE_KnownImm (L[2])                               &&
891             !CE_HasLabel (L[2])                              &&
892             L[3]->OPC == OP65_CLC                            &&
893             !CE_HasLabel (L[3])                              &&
894             L[4]->OPC == OP65_ADC                            &&
895             L[4]->AM == AM65_ZP_INDY                         &&
896             !CE_HasLabel (L[4])                              &&
897             (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
898             L[5]->JumpTo != 0                                &&
899             L[5]->JumpTo->Owner == L[7]                      &&
900             !CE_HasLabel (L[5])                              &&
901             L[6]->OPC == OP65_INX                            &&
902             !CE_HasLabel (L[6])                              &&
903             L[7]->OPC == OP65_LDY                            &&
904             CE_KnownImm (L[7])                               &&
905             L[7]->Num == 0                                   &&
906             CE_IsCallTo (L[8], "ldauidx")                    &&
907             !CE_HasLabel (L[8])                              &&
908             /* Check the label last because this is quite costly */
909             (Len = strlen (L[0]->Arg)) > 3                   &&
910             L[0]->Arg[0] == '<'                              &&
911             L[0]->Arg[1] == '('                              &&
912             strlen (L[1]->Arg) == Len                        &&
913             L[1]->Arg[0] == '>'                              &&
914             memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
915
916             CodeEntry* X;
917             char* Label;
918
919             /* Add the lda */
920             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
921             CS_InsertEntry (S, X, I+3);
922
923             /* Add the tay */
924             X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
925             CS_InsertEntry (S, X, I+4);
926
927             /* Add the ldx */
928             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
929             CS_InsertEntry (S, X, I+5);
930
931             /* Add the lda */
932             Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
933             Label[Len-3] = '\0';
934             X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
935             CS_InsertEntry (S, X, I+6);
936             xfree (Label);
937
938             /* Remove the old code */
939             CS_DelEntries (S, I, 2);
940             CS_DelEntries (S, I+5, 6);
941
942             /* Remember, we had changes */
943             ++Changes;
944
945         }
946
947         /* Next entry */
948         ++I;
949
950     }
951
952     /* Return the number of changes made */
953     return Changes;
954 }
955
956
957
958 static unsigned OptPtrLoad5 (CodeSeg* S)
959 /* Search for the sequence:
960  *
961  *      lda     zp
962  *      ldx     zp+1
963  *      ldy     xx
964  *      jsr     ldauidx
965  *
966  * and replace it by:
967  *
968  *      ldy     xx
969  *      ldx     #$00
970  *      lda     (zp),y
971  */
972 {
973     unsigned Changes = 0;
974
975     /* Walk over the entries */
976     unsigned I = 0;
977     while (I < CS_GetEntryCount (S)) {
978
979         CodeEntry* L[4];
980         unsigned Len;
981
982         /* Get next entry */
983         L[0] = CS_GetEntry (S, I);
984
985         /* Check for the sequence */
986         if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP        &&
987             CS_GetEntries (S, L+1, I+1, 3)                      &&
988             !CS_RangeHasLabel (S, I+1, 3)                       &&
989             L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP        &&
990             (Len = strlen (L[0]->Arg)) > 0                      &&
991             strncmp (L[0]->Arg, L[1]->Arg, Len) == 0            &&
992             strcmp (L[1]->Arg + Len, "+1") == 0                 &&
993             L[2]->OPC == OP65_LDY                               &&
994             CE_IsCallTo (L[3], "ldauidx")) {
995
996             CodeEntry* X;
997
998             /* ldx #$00 */
999             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1000             CS_InsertEntry (S, X, I+3);
1001
1002             /* lda (zp),y */
1003             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1004             CS_InsertEntry (S, X, I+4);
1005
1006             /* Remove the old code */
1007             CS_DelEntry (S, I+5);
1008             CS_DelEntries (S, I, 2);
1009
1010             /* Remember, we had changes */
1011             ++Changes;
1012
1013         }
1014
1015         /* Next entry */
1016         ++I;
1017
1018     }
1019
1020     /* Return the number of changes made */
1021     return Changes;
1022 }
1023
1024
1025
1026 static unsigned OptPtrLoad6 (CodeSeg* S)
1027 /* Search for the sequence:
1028  *
1029  *      lda     zp
1030  *      ldx     zp+1
1031  *      ldy     xx
1032  *      jsr     ldaxidx
1033  *
1034  * and replace it by:
1035  *
1036  *      ldy     xx
1037  *      lda     (zp),y
1038  *      tax
1039  *      dey
1040  *      lda     (zp),y
1041  */
1042 {
1043     unsigned Changes = 0;
1044
1045     /* Walk over the entries */
1046     unsigned I = 0;
1047     while (I < CS_GetEntryCount (S)) {
1048
1049         CodeEntry* L[4];
1050         unsigned Len;
1051
1052         /* Get next entry */
1053         L[0] = CS_GetEntry (S, I);
1054
1055         /* Check for the sequence */
1056         if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP        &&
1057             CS_GetEntries (S, L+1, I+1, 3)                      &&
1058             !CS_RangeHasLabel (S, I+1, 3)                       &&
1059             L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP        &&
1060             (Len = strlen (L[0]->Arg)) > 0                      &&
1061             strncmp (L[0]->Arg, L[1]->Arg, Len) == 0            &&
1062             strcmp (L[1]->Arg + Len, "+1") == 0                 &&
1063             L[2]->OPC == OP65_LDY                               &&
1064             CE_IsCallTo (L[3], "ldaxidx")) {
1065
1066             CodeEntry* X;
1067
1068             /* lda (zp),y */
1069             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1070             CS_InsertEntry (S, X, I+4);
1071
1072             /* tax */
1073             X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1074             CS_InsertEntry (S, X, I+5);
1075
1076             /* dey */
1077             X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1078             CS_InsertEntry (S, X, I+6);
1079
1080             /* lda (zp),y */
1081             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1082             CS_InsertEntry (S, X, I+7);
1083
1084             /* Remove the old code */
1085             CS_DelEntry (S, I+3);
1086             CS_DelEntries (S, I, 2);
1087
1088             /* Remember, we had changes */
1089             ++Changes;
1090
1091         }
1092
1093         /* Next entry */
1094         ++I;
1095
1096     }
1097
1098     /* Return the number of changes made */
1099     return Changes;
1100 }
1101
1102
1103
1104 static unsigned OptPtrLoad7 (CodeSeg* S)
1105 /* Search for the sequence
1106  *
1107  *      ldy     ...
1108  *      jsr     ldauidx
1109  *
1110  * and replace it by:
1111  *
1112  *      ldy     ...
1113  *      stx     ptr1+1
1114  *      sta     ptr1
1115  *      ldx     #$00
1116  *      lda     (ptr1),y
1117  *
1118  * This step must be execute *after* OptPtrLoad1!
1119  */
1120 {
1121     unsigned Changes = 0;
1122
1123     /* Walk over the entries */
1124     unsigned I = 0;
1125     while (I < CS_GetEntryCount (S)) {
1126
1127         CodeEntry* L[2];
1128
1129         /* Get next entry */
1130         L[0] = CS_GetEntry (S, I);
1131
1132         /* Check for the sequence */
1133         if (L[0]->OPC == OP65_LDY               &&
1134             CS_GetEntries (S, L+1, I+1, 1)      &&
1135             CE_IsCallTo (L[1], "ldauidx")       &&
1136             !CE_HasLabel (L[1])) {
1137
1138             CodeEntry* X;
1139
1140             /* Store the high byte */
1141             X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1142             CS_InsertEntry (S, X, I+1);
1143
1144             /* Store the low byte */
1145             X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1146             CS_InsertEntry (S, X, I+2);
1147
1148             /* Delete the call to ldauidx */
1149             CS_DelEntry (S, I+3);
1150
1151             /* Load the high and low byte */
1152             X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1153             CS_InsertEntry (S, X, I+3);
1154             X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1155             CS_InsertEntry (S, X, I+4);
1156
1157             /* Remember, we had changes */
1158             ++Changes;
1159
1160         }
1161
1162         /* Next entry */
1163         ++I;
1164
1165     }
1166
1167     /* Return the number of changes made */
1168     return Changes;
1169 }
1170
1171
1172
1173 /*****************************************************************************/
1174 /*                            Decouple operations                            */
1175 /*****************************************************************************/
1176
1177
1178
1179 static unsigned OptDecouple (CodeSeg* S)
1180 /* Decouple operations, that is, do the following replacements:
1181  *
1182  *   dex        -> ldx #imm
1183  *   inx        -> ldx #imm
1184  *   dey        -> ldy #imm
1185  *   iny        -> ldy #imm
1186  *   tax        -> ldx #imm
1187  *   txa        -> lda #imm
1188  *   tay        -> ldy #imm
1189  *   tya        -> lda #imm
1190  *   lda zp     -> lda #imm
1191  *   ldx zp     -> ldx #imm
1192  *   ldy zp     -> ldy #imm
1193  *
1194  * Provided that the register values are known of course.
1195  */
1196 {
1197     unsigned Changes = 0;
1198     unsigned I;
1199
1200     /* Generate register info for the following step */
1201     CS_GenRegInfo (S);
1202
1203     /* Walk over the entries */
1204     I = 0;
1205     while (I < CS_GetEntryCount (S)) {
1206
1207         const char* Arg;
1208
1209         /* Get next entry and it's input register values */
1210         CodeEntry* E = CS_GetEntry (S, I);
1211         const RegContents* In = &E->RI->In;
1212
1213         /* Assume we have no replacement */
1214         CodeEntry* X = 0;
1215
1216         /* Check the instruction */
1217         switch (E->OPC) {
1218
1219             case OP65_DEX:
1220                 if (E->RI->In.RegX >= 0) {
1221                     Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
1222                     X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1223                 }
1224                 break;
1225
1226             case OP65_DEY:
1227                 if (E->RI->In.RegY >= 0) {
1228                     Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1229                     X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1230                 }
1231                 break;
1232
1233             case OP65_INX:
1234                 if (E->RI->In.RegX >= 0) {
1235                     Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1236                     X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1237                 }
1238                 break;
1239
1240             case OP65_INY:
1241                 if (E->RI->In.RegY >= 0) {
1242                     Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1243                     X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1244                 }
1245                 break;
1246
1247             case OP65_LDA:
1248                 if (E->AM == AM65_ZP) {
1249                     switch (GetKnownReg (E->Use, In)) {
1250                         case REG_TMP1:
1251                             Arg = MakeHexArg (In->Tmp1);
1252                             X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1253                             break;
1254
1255                         case REG_SREG_LO:
1256                             Arg = MakeHexArg (In->SRegLo);
1257                             X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1258                             break;
1259
1260                         case REG_SREG_HI:
1261                             Arg = MakeHexArg (In->SRegHi);
1262                             X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1263                             break;
1264                     }
1265                 }
1266                 break;
1267
1268             case OP65_LDX:
1269                 if (E->AM == AM65_ZP) {
1270                     switch (GetKnownReg (E->Use, In)) {
1271                         case REG_TMP1:
1272                             Arg = MakeHexArg (In->Tmp1);
1273                             X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1274                             break;
1275
1276                         case REG_SREG_LO:
1277                             Arg = MakeHexArg (In->SRegLo);
1278                             X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1279                             break;
1280
1281                         case REG_SREG_HI:
1282                             Arg = MakeHexArg (In->SRegHi);
1283                             X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1284                             break;
1285                     }
1286                 }
1287                 break;
1288
1289             case OP65_LDY:
1290                 if (E->AM == AM65_ZP) {
1291                     switch (GetKnownReg (E->Use, In)) {
1292                         case REG_TMP1:
1293                             Arg = MakeHexArg (In->Tmp1);
1294                             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1295                             break;
1296
1297                         case REG_SREG_LO:
1298                             Arg = MakeHexArg (In->SRegLo);
1299                             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1300                             break;
1301
1302                         case REG_SREG_HI:
1303                             Arg = MakeHexArg (In->SRegHi);
1304                             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1305                             break;
1306                     }
1307                 }
1308                 break;
1309
1310             case OP65_TAX:
1311                 if (E->RI->In.RegA >= 0) {
1312                     Arg = MakeHexArg (In->RegA);
1313                     X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1314                 }
1315                 break;
1316
1317             case OP65_TAY:
1318                 if (E->RI->In.RegA >= 0) {
1319                     Arg = MakeHexArg (In->RegA);
1320                     X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1321                 }
1322                 break;
1323
1324             case OP65_TXA:
1325                 if (E->RI->In.RegX >= 0) {
1326                     Arg = MakeHexArg (In->RegX);
1327                     X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1328                 }
1329                 break;
1330
1331             case OP65_TYA:
1332                 if (E->RI->In.RegY >= 0) {
1333                     Arg = MakeHexArg (In->RegY);
1334                     X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1335                 }
1336                 break;
1337
1338             default:
1339                 /* Avoid gcc warnings */
1340                 break;
1341
1342         }
1343
1344         /* Insert the replacement if we have one */
1345         if (X) {
1346             CS_InsertEntry (S, X, I+1);
1347             CS_DelEntry (S, I);
1348             ++Changes;
1349         }
1350
1351         /* Next entry */
1352         ++I;
1353
1354     }
1355
1356     /* Free register info */
1357     CS_FreeRegInfo (S);
1358
1359     /* Return the number of changes made */
1360     return Changes;
1361 }
1362
1363
1364
1365 /*****************************************************************************/
1366 /*                              struct OptFunc                               */
1367 /*****************************************************************************/
1368
1369
1370
1371 typedef struct OptFunc OptFunc;
1372 struct OptFunc {
1373     unsigned       (*Func) (CodeSeg*);  /* Optimizer function */
1374     const char*    Name;                /* Name of the function/group */
1375     unsigned       CodeSizeFactor;      /* Code size factor for this opt func */
1376     unsigned long  TotalRuns;           /* Total number of runs */
1377     unsigned long  LastRuns;            /* Last number of runs */
1378     unsigned long  TotalChanges;        /* Total number of changes */
1379     unsigned long  LastChanges;         /* Last number of changes */
1380     char           Disabled;            /* True if function disabled */
1381 };
1382
1383
1384
1385 /*****************************************************************************/
1386 /*                                   Code                                    */
1387 /*****************************************************************************/
1388
1389
1390
1391 /* A list of all the function descriptions */
1392 static OptFunc DOpt65C02BitOps  = { Opt65C02BitOps,  "Opt65C02BitOps",   66, 0, 0, 0, 0, 0 };
1393 static OptFunc DOpt65C02Ind     = { Opt65C02Ind,     "Opt65C02Ind",     100, 0, 0, 0, 0, 0 };
1394 static OptFunc DOpt65C02Stores  = { Opt65C02Stores,  "Opt65C02Stores",  100, 0, 0, 0, 0, 0 };
1395 static OptFunc DOptAdd1         = { OptAdd1,         "OptAdd1",         125, 0, 0, 0, 0, 0 };
1396 static OptFunc DOptAdd2         = { OptAdd2,         "OptAdd2",         200, 0, 0, 0, 0, 0 };
1397 static OptFunc DOptAdd3         = { OptAdd3,         "OptAdd3",          90, 0, 0, 0, 0, 0 };
1398 static OptFunc DOptAdd4         = { OptAdd4,         "OptAdd4",         100, 0, 0, 0, 0, 0 };
1399 static OptFunc DOptAdd5         = { OptAdd5,         "OptAdd5",          40, 0, 0, 0, 0, 0 };
1400 static OptFunc DOptBoolTrans    = { OptBoolTrans,    "OptBoolTrans",    100, 0, 0, 0, 0, 0 };
1401 static OptFunc DOptBranchDist   = { OptBranchDist,   "OptBranchDist",     0, 0, 0, 0, 0, 0 };
1402 static OptFunc DOptCmp1         = { OptCmp1,         "OptCmp1",          85, 0, 0, 0, 0, 0 };
1403 static OptFunc DOptCmp2         = { OptCmp2,         "OptCmp2",          75, 0, 0, 0, 0, 0 };
1404 static OptFunc DOptCmp3         = { OptCmp3,         "OptCmp3",          75, 0, 0, 0, 0, 0 };
1405 static OptFunc DOptCmp4         = { OptCmp4,         "OptCmp4",         100, 0, 0, 0, 0, 0 };
1406 static OptFunc DOptCmp5         = { OptCmp5,         "OptCmp5",         100, 0, 0, 0, 0, 0 };
1407 static OptFunc DOptCmp6         = { OptCmp6,         "OptCmp6",          85, 0, 0, 0, 0, 0 };
1408 static OptFunc DOptCmp7         = { OptCmp7,         "OptCmp7",          50, 0, 0, 0, 0, 0 };
1409 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches",  80, 0, 0, 0, 0, 0 };
1410 static OptFunc DOptDeadCode     = { OptDeadCode,     "OptDeadCode",     100, 0, 0, 0, 0, 0 };
1411 static OptFunc DOptDeadJumps    = { OptDeadJumps,    "OptDeadJumps",    100, 0, 0, 0, 0, 0 };
1412 static OptFunc DOptDecouple     = { OptDecouple,     "OptDecouple",     100, 0, 0, 0, 0, 0 };
1413 static OptFunc DOptDupLoads     = { OptDupLoads,     "OptDupLoads",       0, 0, 0, 0, 0, 0 };
1414 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1415 static OptFunc DOptJumpTarget   = { OptJumpTarget,   "OptJumpTarget",   100, 0, 0, 0, 0, 0 };
1416 static OptFunc DOptLoad1        = { OptLoad1,        "OptLoad1",        100, 0, 0, 0, 0, 0 };
1417 static OptFunc DOptRTS          = { OptRTS,          "OptRTS",          100, 0, 0, 0, 0, 0 };
1418 static OptFunc DOptRTSJumps1    = { OptRTSJumps1,    "OptRTSJumps1",    100, 0, 0, 0, 0, 0 };
1419 static OptFunc DOptRTSJumps2    = { OptRTSJumps2,    "OptRTSJumps2",    100, 0, 0, 0, 0, 0 };
1420 static OptFunc DOptNegA1        = { OptNegA1,        "OptNegA1",        100, 0, 0, 0, 0, 0 };
1421 static OptFunc DOptNegA2        = { OptNegA2,        "OptNegA2",        100, 0, 0, 0, 0, 0 };
1422 static OptFunc DOptNegAX1       = { OptNegAX1,       "OptNegAX1",       100, 0, 0, 0, 0, 0 };
1423 static OptFunc DOptNegAX2       = { OptNegAX2,       "OptNegAX2",       100, 0, 0, 0, 0, 0 };
1424 static OptFunc DOptNegAX3       = { OptNegAX3,       "OptNegAX3",       100, 0, 0, 0, 0, 0 };
1425 static OptFunc DOptNegAX4       = { OptNegAX4,       "OptNegAX4",       100, 0, 0, 0, 0, 0 };
1426 static OptFunc DOptPtrLoad1     = { OptPtrLoad1,     "OptPtrLoad1",     100, 0, 0, 0, 0, 0 };
1427 static OptFunc DOptPtrLoad2     = { OptPtrLoad2,     "OptPtrLoad2",     100, 0, 0, 0, 0, 0 };
1428 static OptFunc DOptPtrLoad3     = { OptPtrLoad3,     "OptPtrLoad3",     100, 0, 0, 0, 0, 0 };
1429 static OptFunc DOptPtrLoad4     = { OptPtrLoad4,     "OptPtrLoad4",     100, 0, 0, 0, 0, 0 };
1430 static OptFunc DOptPtrLoad5     = { OptPtrLoad5,     "OptPtrLoad5",      65, 0, 0, 0, 0, 0 };
1431 static OptFunc DOptPtrLoad6     = { OptPtrLoad6,     "OptPtrLoad6",      86, 0, 0, 0, 0, 0 };
1432 static OptFunc DOptPtrLoad7     = { OptPtrLoad7,     "OptPtrLoad7",     100, 0, 0, 0, 0, 0 };
1433 static OptFunc DOptPtrStore1    = { OptPtrStore1,    "OptPtrStore1",    100, 0, 0, 0, 0, 0 };
1434 static OptFunc DOptPush1        = { OptPush1,        "OptPush1",         65, 0, 0, 0, 0, 0 };
1435 static OptFunc DOptPush2        = { OptPush2,        "OptPush2",         50, 0, 0, 0, 0, 0 };
1436 static OptFunc DOptPushPop      = { OptPushPop,      "OptPushPop",        0, 0, 0, 0, 0, 0 };
1437 static OptFunc DOptShift1       = { OptShift1,       "OptShift1",       100, 0, 0, 0, 0, 0 };
1438 static OptFunc DOptShift2       = { OptShift2,       "OptShift2",       100, 0, 0, 0, 0, 0 };
1439 static OptFunc DOptShift3       = { OptShift3,       "OptShift3",       110, 0, 0, 0, 0, 0 };
1440 static OptFunc DOptSize1        = { OptSize1,        "OptSize1",        100, 0, 0, 0, 0, 0 };
1441 static OptFunc DOptSize2        = { OptSize2,        "OptSize2",        100, 0, 0, 0, 0, 0 };
1442 static OptFunc DOptStackOps     = { OptStackOps,     "OptStackOps",     100, 0, 0, 0, 0, 0 };
1443 static OptFunc DOptStore1       = { OptStore1,       "OptStore1",       220, 0, 0, 0, 0, 0 };
1444 static OptFunc DOptStore2       = { OptStore2,       "OptStore2",       120, 0, 0, 0, 0, 0 };
1445 static OptFunc DOptStoreLoad    = { OptStoreLoad,    "OptStoreLoad",      0, 0, 0, 0, 0, 0 };
1446 static OptFunc DOptSub1         = { OptSub1,         "OptSub1",         100, 0, 0, 0, 0, 0 };
1447 static OptFunc DOptSub2         = { OptSub2,         "OptSub2",         100, 0, 0, 0, 0, 0 };
1448 static OptFunc DOptTest1        = { OptTest1,        "OptTest1",        100, 0, 0, 0, 0, 0 };
1449 static OptFunc DOptTransfers    = { OptTransfers,    "OptTransfers",      0, 0, 0, 0, 0, 0 };
1450 static OptFunc DOptUnusedLoads  = { OptUnusedLoads,  "OptUnusedLoads",    0, 0, 0, 0, 0, 0 };
1451 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores",   0, 0, 0, 0, 0, 0 };
1452
1453
1454 /* Table containing all the steps in alphabetical order */
1455 static OptFunc* OptFuncs[] = {
1456     &DOpt65C02BitOps,
1457     &DOpt65C02Ind,
1458     &DOpt65C02Stores,
1459     &DOptAdd1,
1460     &DOptAdd2,
1461     &DOptAdd3,
1462     &DOptAdd4,
1463     &DOptAdd5,
1464     &DOptBoolTrans,
1465     &DOptBranchDist,
1466     &DOptCmp1,
1467     &DOptCmp2,
1468     &DOptCmp3,
1469     &DOptCmp4,
1470     &DOptCmp5,
1471     &DOptCmp6,
1472     &DOptCmp7,
1473     &DOptCondBranches,
1474     &DOptDeadCode,
1475     &DOptDeadJumps,
1476     &DOptDecouple,
1477     &DOptDupLoads,
1478     &DOptJumpCascades,
1479     &DOptJumpTarget,
1480     &DOptLoad1,
1481     &DOptNegA1,
1482     &DOptNegA2,
1483     &DOptNegAX1,
1484     &DOptNegAX2,
1485     &DOptNegAX3,
1486     &DOptNegAX4,
1487     &DOptPtrLoad1,
1488     &DOptPtrLoad2,
1489     &DOptPtrLoad3,
1490     &DOptPtrLoad4,
1491     &DOptPtrLoad5,
1492     &DOptPtrLoad6,
1493     &DOptPtrLoad7,
1494     &DOptPtrStore1,
1495     &DOptPush1,
1496     &DOptPush2,
1497     &DOptPushPop,
1498     &DOptRTS,
1499     &DOptRTSJumps1,
1500     &DOptRTSJumps2,
1501     &DOptShift1,
1502     &DOptShift2,
1503     &DOptShift3,
1504     &DOptSize1,
1505     &DOptSize2,
1506     &DOptStackOps,
1507     &DOptStore1,
1508     &DOptStore2,
1509     &DOptStoreLoad,
1510     &DOptSub1,
1511     &DOptSub2,
1512     &DOptTest1,
1513     &DOptTransfers,
1514     &DOptUnusedLoads,
1515     &DOptUnusedStores,
1516 };
1517 #define OPTFUNC_COUNT  (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1518
1519
1520
1521 static int CmpOptStep (const void* Key, const void* Func)
1522 /* Compare function for bsearch */
1523 {
1524     return strcmp (Key, (*(const OptFunc**)Func)->Name);
1525 }
1526
1527
1528
1529 static OptFunc* FindOptFunc (const char* Name)
1530 /* Find an optimizer step by name in the table and return a pointer. Return
1531  * NULL if no such step is found.
1532  */
1533 {
1534     /* Search for the function in the list */
1535     OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1536     return O? *O : 0;
1537 }
1538
1539
1540
1541 static OptFunc* GetOptFunc (const char* Name)
1542 /* Find an optimizer step by name in the table and return a pointer. Print an
1543  * error and call AbEnd if not found.
1544  */
1545 {
1546     /* Search for the function in the list */
1547     OptFunc* F = FindOptFunc (Name);
1548     if (F == 0) {
1549         /* Not found */
1550         AbEnd ("Optimization step `%s' not found", Name);
1551     }
1552     return F;
1553 }
1554
1555
1556
1557 void DisableOpt (const char* Name)
1558 /* Disable the optimization with the given name */
1559 {
1560     if (strcmp (Name, "any") == 0) {
1561         unsigned I;
1562         for (I = 0; I < OPTFUNC_COUNT; ++I) {
1563             OptFuncs[I]->Disabled = 1;
1564         }
1565     } else {
1566         GetOptFunc(Name)->Disabled = 1;
1567     }
1568 }
1569
1570
1571
1572 void EnableOpt (const char* Name)
1573 /* Enable the optimization with the given name */
1574 {
1575     if (strcmp (Name, "any") == 0) {
1576         unsigned I;
1577         for (I = 0; I < OPTFUNC_COUNT; ++I) {
1578             OptFuncs[I]->Disabled = 0;
1579         }
1580     } else {
1581         GetOptFunc(Name)->Disabled = 0;
1582     }
1583 }
1584
1585
1586
1587 void ListOptSteps (FILE* F)
1588 /* List all optimization steps */
1589 {
1590     unsigned I;
1591     for (I = 0; I < OPTFUNC_COUNT; ++I) {
1592         fprintf (F, "%s\n", OptFuncs[I]->Name);
1593     }
1594 }
1595
1596
1597
1598 static void ReadOptStats (const char* Name)
1599 /* Read the optimizer statistics file */
1600 {
1601     char Buf [256];
1602     unsigned Lines;
1603
1604     /* Try to open the file */
1605     FILE* F = fopen (Name, "r");
1606     if (F == 0) {
1607         /* Ignore the error */
1608         return;
1609     }
1610
1611     /* Read and parse the lines */
1612     Lines = 0;
1613     while (fgets (Buf, sizeof (Buf), F) != 0) {
1614
1615         char* B;
1616         unsigned Len;
1617         OptFunc* Func;
1618
1619         /* Fields */
1620         char Name[32];
1621         unsigned long  TotalRuns;
1622         unsigned long  TotalChanges;
1623
1624         /* Count lines */
1625         ++Lines;
1626
1627         /* Remove trailing white space including the line terminator */
1628         B = Buf;
1629         Len = strlen (B);
1630         while (Len > 0 && IsSpace (B[Len-1])) {
1631             --Len;
1632         }
1633         B[Len] = '\0';
1634
1635         /* Remove leading whitespace */
1636         while (IsSpace (*B)) {
1637             ++B;
1638         }
1639
1640         /* Check for empty and comment lines */
1641         if (*B == '\0' || *B == ';' || *B == '#') {
1642             continue;
1643         }
1644
1645         /* Parse the line */
1646         if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1647             /* Syntax error */
1648             continue;
1649         }
1650
1651         /* Search for the optimizer step. */
1652         Func = FindOptFunc (Name);
1653         if (Func == 0) {
1654             /* Not found */
1655             continue;
1656         }
1657
1658         /* Found the step, set the fields */
1659         Func->TotalRuns    = TotalRuns;
1660         Func->TotalChanges = TotalChanges;
1661
1662     }
1663
1664     /* Close the file, ignore errors here. */
1665     fclose (F);
1666 }
1667
1668
1669
1670 static void WriteOptStats (const char* Name)
1671 /* Write the optimizer statistics file */
1672 {
1673     unsigned I;
1674
1675     /* Try to open the file */
1676     FILE* F = fopen (Name, "w");
1677     if (F == 0) {
1678         /* Ignore the error */
1679         return;
1680     }
1681
1682     /* Write a header */
1683     fprintf (F,
1684              "; Optimizer           Total  Last   Total  Last\n"
1685              ";   Step              Runs   Runs    Chg   Chg\n");
1686
1687
1688     /* Write the data */
1689     for (I = 0; I < OPTFUNC_COUNT; ++I) {
1690         const OptFunc* O = OptFuncs[I];
1691         fprintf (F,
1692                  "%-20s %6lu %6lu %6lu %6lu\n",
1693                  O->Name,
1694                  O->TotalRuns,
1695                  O->LastRuns,
1696                  O->TotalChanges,
1697                  O->LastChanges);
1698     }
1699
1700     /* Close the file, ignore errors here. */
1701     fclose (F);
1702 }
1703
1704
1705
1706 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1707 /* Run one optimizer function Max times or until there are no more changes */
1708 {
1709     unsigned Changes, C;
1710
1711     /* Don't run the function if it is disabled or if it is prohibited by the
1712      * code size factor
1713      */
1714     if (F->Disabled || F->CodeSizeFactor > CodeSizeFactor) {
1715         return 0;
1716     }
1717
1718     /* Run this until there are no more changes */
1719     Changes = 0;
1720     do {
1721
1722         /* Run the function */
1723         C = F->Func (S);
1724         Changes += C;
1725
1726         /* Do statistics */
1727         ++F->TotalRuns;
1728         ++F->LastRuns;
1729         F->TotalChanges += C;
1730         F->LastChanges  += C;
1731
1732     } while (--Max && C > 0);
1733
1734     /* Return the number of changes */
1735     return Changes;
1736 }
1737
1738
1739
1740 static unsigned RunOptGroup1 (CodeSeg* S)
1741 /* Run the first group of optimization steps. These steps translate known
1742  * patterns emitted by the code generator into more optimal patterns. Order
1743  * of the steps is important, because some of the steps done earlier cover
1744  * the same patterns as later steps as subpatterns.
1745  */
1746 {
1747     unsigned Changes = 0;
1748
1749     Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1750     Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1751     Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1752     Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1753     Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1754     Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1755     Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1756     Changes += RunOptFunc (S, &DOptNegAX1, 1);
1757     Changes += RunOptFunc (S, &DOptNegAX2, 1);
1758     Changes += RunOptFunc (S, &DOptNegAX3, 1);
1759     Changes += RunOptFunc (S, &DOptNegAX4, 1);
1760     Changes += RunOptFunc (S, &DOptAdd1, 1);
1761     Changes += RunOptFunc (S, &DOptAdd2, 1);
1762     Changes += RunOptFunc (S, &DOptAdd3, 1);
1763     Changes += RunOptFunc (S, &DOptShift1, 1);
1764     Changes += RunOptFunc (S, &DOptShift2, 1);
1765     Changes += RunOptFunc (S, &DOptShift3, 1);
1766     Changes += RunOptFunc (S, &DOptStore1, 5);
1767     Changes += RunOptFunc (S, &DOptStore2, 5);
1768
1769     /* Return the number of changes */
1770     return Changes;
1771 }
1772
1773
1774
1775 static unsigned RunOptGroup2 (CodeSeg* S)
1776 /* Run one group of optimization steps. This step involves just decoupling
1777  * instructions by replacing them by instructions that do not depend on
1778  * previous instructions. This makes it easier to find instructions that
1779  * aren't used.
1780  */
1781 {
1782     unsigned Changes = 0;
1783
1784     Changes += RunOptFunc (S, &DOptDecouple, 1);
1785
1786     /* Return the number of changes */
1787     return Changes;
1788 }
1789
1790
1791
1792 static unsigned RunOptGroup3 (CodeSeg* S)
1793 /* Run one group of optimization steps. These steps depend on each other,
1794  * that means that one step may allow another step to do additional work,
1795  * so we will repeat the steps as long as we see any changes.
1796  */
1797 {
1798     unsigned Changes, C;
1799
1800     Changes = 0;
1801     do {
1802         C = 0;
1803
1804         C += RunOptFunc (S, &DOptPtrLoad7, 1);
1805         C += RunOptFunc (S, &DOptNegA1, 1);
1806         C += RunOptFunc (S, &DOptNegA2, 1);
1807         C += RunOptFunc (S, &DOptSub1, 1);
1808         C += RunOptFunc (S, &DOptSub2, 1);
1809         C += RunOptFunc (S, &DOptAdd4, 1);
1810         C += RunOptFunc (S, &DOptAdd5, 1);
1811         C += RunOptFunc (S, &DOptStackOps, 1);
1812         C += RunOptFunc (S, &DOptJumpCascades, 1);
1813         C += RunOptFunc (S, &DOptDeadJumps, 1);
1814         C += RunOptFunc (S, &DOptRTS, 1);
1815         C += RunOptFunc (S, &DOptDeadCode, 1);
1816         C += RunOptFunc (S, &DOptJumpTarget, 1);
1817         C += RunOptFunc (S, &DOptCondBranches, 1);
1818         C += RunOptFunc (S, &DOptRTSJumps1, 1);
1819         C += RunOptFunc (S, &DOptBoolTrans, 1);
1820         C += RunOptFunc (S, &DOptCmp1, 1);
1821         C += RunOptFunc (S, &DOptCmp2, 1);
1822         C += RunOptFunc (S, &DOptCmp3, 1);
1823         C += RunOptFunc (S, &DOptCmp4, 1);
1824         C += RunOptFunc (S, &DOptCmp5, 1);
1825         C += RunOptFunc (S, &DOptCmp6, 1);
1826         C += RunOptFunc (S, &DOptCmp7, 1);
1827         C += RunOptFunc (S, &DOptTest1, 1);
1828         C += RunOptFunc (S, &DOptLoad1, 1);
1829         C += RunOptFunc (S, &DOptUnusedLoads, 1);
1830         C += RunOptFunc (S, &DOptUnusedStores, 1);
1831         C += RunOptFunc (S, &DOptDupLoads, 1);
1832         C += RunOptFunc (S, &DOptStoreLoad, 1);
1833         C += RunOptFunc (S, &DOptTransfers, 1);
1834         C += RunOptFunc (S, &DOptPushPop, 1);
1835
1836         Changes += C;
1837
1838     } while (C);
1839
1840     /* Return the number of changes */
1841     return Changes;
1842 }
1843
1844
1845
1846 static unsigned RunOptGroup4 (CodeSeg* S)
1847 /* 65C02 specific optimizations. */
1848 {
1849     unsigned Changes = 0;
1850
1851     if (CPU >= CPU_65C02) {
1852         Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1853         Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1854         Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1855         if (Changes) {
1856             /* The 65C02 replacement codes do often make the use of a register
1857              * value unnecessary, so if we have changes, run another load
1858              * removal pass.
1859              */
1860             Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1861         }
1862     }
1863
1864     /* Return the number of changes */
1865     return Changes;
1866 }
1867
1868
1869
1870 static unsigned RunOptGroup5 (CodeSeg* S)
1871 /* Run another round of pattern replacements. These are done late, since there
1872  * may be better replacements before.
1873  */
1874 {
1875     unsigned Changes = 0;
1876
1877     Changes += RunOptFunc (S, &DOptPush1, 1);
1878     Changes += RunOptFunc (S, &DOptPush2, 1);
1879     Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1880
1881     /* Return the number of changes */
1882     return Changes;
1883 }
1884
1885
1886
1887 static unsigned RunOptGroup6 (CodeSeg* S)
1888 /* The last group of optimization steps. Adjust branches, do size optimizations.
1889  */
1890 {
1891     unsigned Changes = 0;
1892     unsigned C;
1893
1894     if (CodeSizeFactor <= 100) {
1895         /* Optimize for size, that is replace operations by shorter ones, even
1896          * if this does hinder further optimizations (no problem since we're
1897          * done soon).
1898          */
1899         C = RunOptFunc (S, &DOptSize1, 1);
1900         if (C) {
1901             Changes += C;
1902             /* Run some optimization passes again, since the size optimizations
1903              * may have opened new oportunities.
1904              */
1905             Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1906             Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1907         }
1908     }
1909     C = RunOptFunc (S, &DOptSize2, 1);
1910     if (C) {
1911         Changes += C;
1912         /* Run some optimization passes again, since the size optimizations
1913          * may have opened new oportunities.
1914          */
1915         Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1916         Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1917     }
1918
1919     /* Adjust branch distances */
1920     Changes += RunOptFunc (S, &DOptBranchDist, 3);
1921
1922     /* Replace conditional branches to RTS. If we had changes, we must run dead
1923      * code elimination again, since the change may have introduced dead code.
1924      */
1925     C = RunOptFunc (S, &DOptRTSJumps2, 1);
1926     Changes += C;
1927     if (C) {
1928         Changes += RunOptFunc (S, &DOptDeadCode, 1);
1929     }
1930
1931     /* Return the number of changes */
1932     return Changes;
1933 }
1934
1935
1936
1937 void RunOpt (CodeSeg* S)
1938 /* Run the optimizer */
1939 {
1940     const char* StatFileName;
1941
1942     /* If we shouldn't run the optimizer, bail out */
1943     if (!Optimize) {
1944         return;
1945     }
1946
1947     /* Check if we are requested to write optimizer statistics */
1948     StatFileName = getenv ("CC65_OPTSTATS");
1949     if (StatFileName) {
1950         ReadOptStats (StatFileName);
1951     }
1952
1953     /* Print the name of the function we are working on */
1954     if (S->Func) {
1955         Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1956     } else {
1957         Print (stdout, 1, "Running optimizer for global code segment\n");
1958     }
1959
1960     /* Run groups of optimizations */
1961     RunOptGroup1 (S);
1962     RunOptGroup2 (S);
1963     RunOptGroup3 (S);
1964     RunOptGroup4 (S);
1965     RunOptGroup5 (S);
1966     RunOptGroup6 (S);
1967
1968     /* Write statistics */
1969     if (StatFileName) {
1970         WriteOptStats (StatFileName);
1971     }
1972 }
1973
1974
1975