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