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