Imported Upstream version 2.9.0
[debian/cc1111] / src / pic / peeph.def
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.)
13 //
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.
22 //
23 // EXAMPLE:
24 //
25 // Consider Peep Rule 1 as an example. This rule
26 // replaces some code like:
27 //
28 //   skpz           ;i.e. btfss status,Z
29 //    goto    lab1
30 //   clrw
31 //lab1:
32 //
33 // with:
34 //
35 //   skpnz     ;i.e. btfsc status,Z
36 //    clrw
37 //lab1
38 //
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).
49 //
50 //
51 // CONDITIONS
52 //
53 // There are certain instances where a peep rule may not
54 // be applicable. Consider this subtle example:
55 //
56 //     movwf    R0
57 //     movf     R0,W
58 //
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.
63 //
64 // To get around this proble, the peep rule can be followed
65 // by a conditon:  "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...
69 //
70 // Special commands
71 //
72 //
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.
83 //
84 // 
85 //
86
87
88 // Peep 1 
89 //   Converts 
90 //
91 //    btfss   reg1,bit
92 //     goto   label
93 //    incf    reg2,f
94 //label
95 //
96 // Into:
97 //
98 //    btfsc   reg1,bit
99 //     incf   reg2,f
100 //label
101 //
102 // Notice that wild cards will allow any instruction
103 // besides incf to be used in the above.
104 //
105 // Also, notice that this snippet is not valid if
106 // it follows another skip
107
108 replace restart {
109         _NOTBITSKIP_    %1
110         _BITSKIP_       %2
111         goto    %3
112         %4
113 %3:     %5
114 } by {
115         ;     peep 1 - test/jump to test/skip
116         %1
117         _INVERTBITSKIP_ %2
118         %4
119 %3:     %5
120
121
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
130         %1
131         _INVERTBITSKIP_ %2
132 %4:     %5
133 %3:     %6
134
135
136
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
152
153
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
162
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
173 }
174
175
176 replace restart {
177         movf    %1,w
178         movf    %1,w
179 } by {
180         ;     peep 4 - Removed redundant move
181         movf    %1,w
182 }
183
184
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
193 }
194
195 replace restart {
196         movwf   %1
197         movwf   %1
198 } by {
199         ;     peep 6 - Removed redundant move
200         movwf   %1
201 }
202
203 replace restart {
204         movlw   0
205         iorwf   %1,w
206 } by {
207         ;     peep 7 - Removed redundant move
208         movf    %1,w
209 }
210
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
219 }
220
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
229 }
230
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
239 }
240
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
249 }
250
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
260
261 // From: Frieder Ferlemann
262
263 replace restart {
264         iorlw   0
265 } by {
266         ;     peep 10a - Removed unnecessary iorlw
267 } if NZ
268
269 // From: Frieder Ferlemann
270
271 replace restart {
272         xorlw   0
273 } by {
274         ;     peep 10b - Removed unnecessary xorlw
275 } if NZ
276
277 // From: Frieder Ferlemann
278
279 replace restart {
280         movf    %1,w
281         movwf   %1
282 } by {
283         ;     peep 11 - Removed redundant move
284         movf    %1,w
285 }
286
287 replace restart {
288         comf    %1,w
289         movwf   %1
290 } by {
291         ;     peep 12 - Removed redundant move
292         comf    %1,f
293 }
294
295 replace restart {
296         clrf    %1
297         rlf     %1,f
298         movlw   0x01
299         xorwf   %1,f
300         movf    %1,w
301         btfss   STATUS,2
302         goto    %2
303
304 } by {
305         ;     peep 13 - Optimized carry sequence
306         clrf    %1
307         incf    %1,F
308         btfss   status,C
309         goto    %2
310         clrf    %1
311         
312 }
313
314 replace restart {
315         clrf    %1
316         rlf     %1,f
317         movlw   0x01
318         xorwf   %1,f
319         movf    %1,w
320         btfsc   STATUS,2
321         goto    %2
322
323 } by {
324         ;     peep 13a - Optimized carry sequence
325         clrf    %1
326         incf    %1,F
327         btfsc   status,C
328         goto    %2
329         clrf    %1
330         
331 }