]> git.sur5r.net Git - cc65/blob - src/cc65/copttest.c
New/changed optimizations
[cc65] / src / cc65 / copttest.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                copttest.c                                 */
4 /*                                                                           */
5 /*                          Optimize test sequences                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      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 /* cc65 */
37 #include "codeent.h"
38 #include "codeinfo.h"
39 #include "copttest.h"
40
41
42
43 /*****************************************************************************/
44 /*                              Optimize tests                               */
45 /*****************************************************************************/
46
47
48
49 unsigned OptTest1 (CodeSeg* S)
50 /* Given a sequence
51  *
52  *     stx     xxx
53  *     ora     xxx
54  *     beq/bne ...
55  *
56  * If X is zero, the sequence may be changed to
57  *
58  *     cmp     #$00
59  *     beq/bne ...
60  *
61  * which may be optimized further by another step.
62  *
63  * If A is zero, the sequence may be changed to
64  *
65  *     txa
66  *     beq/bne ...
67  *
68  */
69 {
70     unsigned Changes = 0;
71     unsigned I;
72
73     /* Generate register info for this step */
74     CS_GenRegInfo (S);
75
76     /* Walk over the entries */
77     I = 0;
78     while (I < CS_GetEntryCount (S)) {
79
80         CodeEntry* L[3];
81
82         /* Get next entry */
83         L[0] = CS_GetEntry (S, I);
84
85         /* Check if it's the sequence we're searching for */
86         if (L[0]->OPC == OP65_STX              &&
87             CS_GetEntries (S, L+1, I+1, 2)     &&
88             !CE_HasLabel (L[1])                &&
89             L[1]->OPC == OP65_ORA              &&
90             strcmp (L[0]->Arg, L[1]->Arg) == 0 &&
91             !CE_HasLabel (L[2])                &&
92             (L[2]->Info & OF_ZBRA) != 0) {
93
94             /* Check if X is zero */
95             if (L[0]->RI->In.RegX == 0) {
96
97                 /* Insert the compare */
98                 CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI);
99                 CS_InsertEntry (S, N, I+2);
100
101                 /* Remove the two other insns */
102                 CS_DelEntry (S, I+1);
103                 CS_DelEntry (S, I);
104
105                 /* We had changes */
106                 ++Changes;
107
108             /* Check if A is zero */
109             } else if (L[1]->RI->In.RegA == 0) {
110
111                 /* Insert the txa */
112                 CodeEntry* N = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[1]->LI);
113                 CS_InsertEntry (S, N, I+2);
114
115                 /* Remove the two other insns */
116                 CS_DelEntry (S, I+1);
117                 CS_DelEntry (S, I);
118
119                 /* We had changes */
120                 ++Changes;
121             }
122         }
123
124         /* Next entry */
125         ++I;
126
127     }
128
129     /* Free register info */
130     CS_FreeRegInfo (S);
131
132     /* Return the number of changes made */
133     return Changes;
134 }
135
136
137