Update svn merge history.
[sdcc.git] / sdcc / src / pic14 / peeph.def
blob20d10e0d4b301d77b37ab971abf4c32e75672ef5
1 // PIC Port Peep rules
2 //
3 //
4 // INTRODUCTION:
5 //
6 // The peep hole optimizer searchs the
7 // the SDCC generated code for small snippets
8 // that can be optimized. As a user, you have
9 // control over this optimization process without
10 // having to learn the SDCC source code. (However
11 // you'll still need access to the source since
12 // these rules are compiled into the source.)
14 // The way it works is you specify the target
15 // snippet that you want replaced with a more
16 // efficient snippet that you write. Wild card
17 // variables allow the rules to be parameterized.
18 //
19 // In all of the SDCC ports, labels and operands
20 // can be wild cards. However, in the PIC even the
21 // instructions can be wild cards.
23 // EXAMPLE:
25 // Consider Peep Rule 1 as an example. This rule
26 // replaces some code like:
28 // skpz ;i.e. btfss status,Z
29 // goto lab1
30 // clrw
31 //lab1:
33 // with:
35 // skpnz ;i.e. btfsc status,Z
36 // clrw
37 //lab1
39 // However, the Rule has four wild cards.
40 // The first allows the btfss instruction operator
41 // be anything, not just the Z bit in status register.
42 // The second wild card applies to a label.
43 // The third wild card is for an instruction - any
44 // single instruction can be substituted.
45 // The fourth wild card is also an instruction. It's
46 // just an instruction place holder associated with
47 // a label (think of it as the PIC Port author's laziness
48 // imposed upon the user).
51 // CONDITIONS
53 // There are certain instances where a peep rule may not
54 // be applicable. Consider this subtle example:
56 // movwf R0
57 // movf R0,W
59 // It would seem that the second move is unnecessary. But
60 // be careful! The movf instruction affects the 'Z' bit.
61 // So if this sequence is followed by a btfsc status,Z, you
62 // will have to leave the second move in.
64 // To get around this proble, the peep rule can be followed
65 // by a condition: "if NZ". Which is to say, apply the rule
66 // if Z bit is not needed in the code that follows. The optimizer
67 // is smart enough to look more than one instruction past the
68 // target block...
70 // Special commands
73 // _NOTBITSKIP_ %1 - Creates a wild card instruction that
74 // will match all instructions except for
75 // bit skip instructions (btfsc or btfss)
76 // _BITSKIP_ %1 - Creates a wild card instruction that only
77 // will match a bit skip instruction (btfsc
78 // or btfss)
79 // _INVERTBITSKIP_ %1 - For the wild card instruction %1, invert
80 // the state of the bit skip. If %1 is not
81 // a bit skip instruction, then there's an
82 // error in the peep rule.
84 //
88 // Peep 1
89 // Converts
91 // btfss reg1,bit
92 // goto label
93 // incf reg2,f
94 //label
96 // Into:
98 // btfsc reg1,bit
99 // incf reg2,f
100 //label
102 // Notice that wild cards will allow any instruction
103 // besides incf to be used in the above.
105 // Also, notice that this snippet is not valid if
106 // it follows another skip
108 replace restart {
109 _NOTBITSKIP_ %1
110 _BITSKIP_ %2
111 goto %3
113 %3: %5
114 } by {
115 ; peep 1 - test/jump to test/skip
117 _INVERTBITSKIP_ %2
119 %3: %5
122 replace restart {
123 _NOTBITSKIP_ %1
124 _BITSKIP_ %2
125 goto %3
126 %4: %5
127 %3: %6
128 } by {
129 ; peep 1b - test/jump to test/skip
131 _INVERTBITSKIP_ %2
132 %4: %5
133 %3: %6
137 //bogus test for pcode
138 //replace restart {
139 // movf %1,w ;comment at end
140 //%4: movf %1,w
141 // RETURN
142 // clrf INDF
143 // movlw 0xa5
144 // movf fsr,w
145 // incf indf,f
146 // %2
147 //} by {
148 // ; peep test remove redundant move
149 //%4: movf %1,w ;another comment
150 // %2
151 //} if AYBABTU %3
154 // peep 2
155 replace restart {
156 movwf %1
157 movf %1,w
158 } by {
159 ; peep 2 - Removed redundant move
160 movwf %1
161 } if NZ
163 // peep 3
164 replace restart {
165 decf %1,f
166 movf %1,w
167 btfss STATUS,z
168 goto %2
169 } by {
170 ; peep 3 - decf/mov/skpz to decfsz
171 decfsz %1,f
172 goto %2
176 replace restart {
177 movf %1,w
178 movf %1,w
179 } by {
180 ; peep 4 - Removed redundant move
181 movf %1,w
185 replace restart {
186 movlw %1
187 movwf %2
188 movlw %1
189 } by {
190 ; peep 5 - Removed redundant move
191 movlw %1
192 movwf %2
195 replace restart {
196 movwf %1
197 movwf %1
198 } by {
199 ; peep 6 - Removed redundant move
200 movwf %1
203 replace restart {
204 movlw 0
205 iorwf %1,w
206 } by {
207 ; peep 7 - Removed redundant move
208 movf %1,w
211 replace restart {
212 movf %1,w
213 movwf %2
214 decf %2,f
215 } by {
216 ; peep 8 - Removed redundant move
217 decf %1,w
218 movwf %2
221 replace restart {
222 movwf %1
223 movf %2,w
224 xorwf %1,w
225 } by {
226 ; peep 9a - Removed redundant move
227 movwf %1
228 xorwf %2,w
231 replace restart {
232 movwf %1
233 movf %2,w
234 iorwf %1,w
235 } by {
236 ; peep 9b - Removed redundant move
237 movwf %1
238 iorwf %2,w
241 replace restart {
242 movf %1,w
243 movwf %2
244 movf %2,w
245 } by {
246 ; peep 9c - Removed redundant move
247 movf %1,w
248 movwf %2
251 replace restart {
252 movwf %1
253 movf %1,w
254 movwf %2
255 } by {
256 ; peep 9d - Removed redundant move
257 movwf %1
258 movwf %2
259 } if NZ
261 // From: Frieder Ferlemann
263 replace restart {
264 iorlw 0
265 } by {
266 ; peep 10a - Removed unnecessary iorlw
267 } if NZ
269 // From: Frieder Ferlemann
271 replace restart {
272 xorlw 0
273 } by {
274 ; peep 10b - Removed unnecessary xorlw
275 } if NZ
277 // From: Frieder Ferlemann
279 replace restart {
280 movf %1,w
281 movwf %1
282 } by {
283 ; peep 11 - Removed redundant move
284 movf %1,w
287 replace restart {
288 clrf %1
289 rlf %1,f
290 movlw 0x01
291 xorwf %1,f
292 movf %1,w
293 btfss STATUS,2
294 goto %2
296 } by {
297 ; peep 13 - Optimized carry sequence
298 clrf %1
299 incf %1,F
300 btfss status,C
301 goto %2
302 clrf %1
306 replace restart {
307 clrf %1
308 rlf %1,f
309 movlw 0x01
310 xorwf %1,f
311 movf %1,w
312 btfsc STATUS,2
313 goto %2
315 } by {
316 ; peep 13a - Optimized carry sequence
317 clrf %1
318 incf %1,F
319 btfsc status,C
320 goto %2
321 clrf %1