1 /*****************************************************************************/
5 /* Optimize test sequences */
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 /*****************************************************************************/
45 /*****************************************************************************/
49 unsigned OptTest1 (CodeSeg* S)
56 ** If X is zero, the sequence may be changed to
61 ** which may be optimized further by another step.
63 ** If A is zero, the sequence may be changed to
72 /* Walk over the entries */
74 while (I < CS_GetEntryCount (S)) {
79 L[0] = CS_GetEntry (S, I);
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) {
90 /* Check if X is zero */
91 if (L[0]->RI->In.RegX == 0) {
93 /* Insert the compare */
94 CodeEntry* N = NewCodeEntry (OP65_CMP, AM65_IMM, "$00", 0, L[0]->LI);
95 CS_InsertEntry (S, N, I+2);
97 /* Remove the two other insns */
104 /* Check if A is zero */
105 } else if (L[1]->RI->In.RegA == 0) {
108 CodeEntry* N = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[1]->LI);
109 CS_InsertEntry (S, N, I+2);
111 /* Remove the two other insns */
112 CS_DelEntry (S, I+1);
125 /* Return the number of changes made */
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
137 unsigned Changes = 0;
139 /* Walk over the entries */
141 while (I < CS_GetEntryCount (S)) {
146 L[0] = CS_GetEntry (S, I);
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) {
158 /* Remove the load */
159 CS_DelEntry (S, I+1);
168 /* Return the number of changes made */