]> git.sur5r.net Git - cc65/blob - src/cc65/copttest.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / cc65 / copttest.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                copttest.c                                 */
4 /*                                                                           */
5 /*                          Optimize test sequences                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2012, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
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     /* Walk over the entries */
74     I = 0;
75     while (I < CS_GetEntryCount (S)) {
76
77         CodeEntry* L[3];
78
79         /* Get next entry */
80         L[0] = CS_GetEntry (S, I);
81
82         /* Check if it's the sequence we're searching for */
83         if (L[0]->OPC == OP65_STX              &&
84             CS_GetEntries (S, L+1, I+1, 2)     &&
85             !CE_HasLabel (L[1])                &&
86             L[1]->OPC == OP65_ORA              &&
87             strcmp (L[0]->Arg, L[1]->Arg) == 0 &&
88             !CE_HasLabel (L[2])                &&
89             (L[2]->Info & OF_ZBRA) != 0) {
90
91             /* Check if X is zero */
92             if (L[0]->RI->In.RegX == 0) {
93
94                 /* Insert the compare */
95                 CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI);
96                 CS_InsertEntry (S, N, I+2);
97
98                 /* Remove the two other insns */
99                 CS_DelEntry (S, I+1);
100                 CS_DelEntry (S, I);
101
102                 /* We had changes */
103                 ++Changes;
104
105             /* Check if A is zero */
106             } else if (L[1]->RI->In.RegA == 0) {
107
108                 /* Insert the txa */
109                 CodeEntry* N = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[1]->LI);
110                 CS_InsertEntry (S, N, I+2);
111
112                 /* Remove the two other insns */
113                 CS_DelEntry (S, I+1);
114                 CS_DelEntry (S, I);
115
116                 /* We had changes */
117                 ++Changes;
118             }
119         }
120
121         /* Next entry */
122         ++I;
123
124     }
125
126     /* Return the number of changes made */
127     return Changes;
128 }
129
130
131
132 unsigned OptTest2 (CodeSeg* S)
133 /* Search for an inc/dec operation followed by a load and a conditional
134  * branch based on the flags from the load. Remove the load if the insn
135  * isn't used later.
136  */
137 {
138     unsigned Changes = 0;
139
140     /* Walk over the entries */
141     unsigned I = 0;
142     while (I < CS_GetEntryCount (S)) {
143
144         CodeEntry* L[3];
145
146         /* Get next entry */
147         L[0] = CS_GetEntry (S, I);
148
149         /* Check if it's the sequence we're searching for */
150         if ((L[0]->OPC == OP65_INC || L[0]->OPC == OP65_DEC)    &&
151             CS_GetEntries (S, L+1, I+1, 2)                      &&
152             !CE_HasLabel (L[1])                                 &&
153             (L[1]->Info & OF_LOAD) != 0                         &&
154             (L[2]->Info & OF_FBRA) != 0                         &&
155             L[1]->AM == L[0]->AM                                &&
156             strcmp (L[0]->Arg, L[1]->Arg) == 0                  &&
157             (GetRegInfo (S, I+2, L[1]->Chg) & L[1]->Chg) == 0) {
158
159             /* Remove the load */
160             CS_DelEntry (S, I+1);
161              ++Changes;
162         }
163
164         /* Next entry */
165         ++I;
166
167     }
168
169     /* Return the number of changes made */
170     return Changes;
171 }