d6a80a0e98c17d71c130ef1b786c23edbe24353e
[fw/sdcc] / 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 wild cards four wild cards.
40 // The first allows the btfss instruction operate
41 // on 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 //
71
72
73 replace restart {
74         btfss   %1
75         goto    %2
76         %3
77 %2:     %4
78 } by {
79         ;peep 1 - test/jump to test/skip
80         btfsc   %1
81         %3
82 %2:     %4
83 }
84
85 replace restart {
86         btfsc   %1
87         goto    %2
88         %3
89 %2:     %4
90 } by {
91         ;peep 1a - test/jump to test/skip
92         btfss   %1
93         %3
94 %2:     %4
95 }
96
97 replace restart {
98         btfss   %1
99         goto    %4
100 %2:     %3
101 %4:     %5
102 } by {
103         ;peep 1b - test/jump to test/skip
104         btfsc   %1
105 %2:     %3
106 %4:     %5
107 }
108
109 replace restart {
110         btfsc   %1
111         goto    %4
112 %2:     %3
113 %4:     %5
114 } by {
115         ;peep 1c - test/jump to test/skip
116         btfss   %1
117 %2:     %3
118 %4:     %5
119 }
120
121
122 //bogus test for pcode
123 //replace restart {
124 //      movf    %1,w    ;comment at end
125 //%4:   movf    %1,w
126 //      RETURN
127 //      clrf    INDF
128 //      movlw   0xa5
129 //      movf    fsr,w
130 //      incf    indf,f
131 //      %2
132 //} by {
133 //      ; peep test remove redundant move
134 //%4:   movf    %1,w    ;another comment
135 //      %2
136 //} if AYBABTU %3
137
138
139 // peep 2
140 replace restart {
141         movwf   %1
142         movf    %1,w
143 } by {
144         ; peep 2 - Removed redundant move
145         movwf   %1
146 } if NZ
147
148 // peep 3
149 replace restart {
150         decf    %1,f
151         movf    %1,w
152         btfss   status,z
153         goto    %2
154 } by {
155         ; peep 3 - decf/mov/skpz to decfsz
156         decfsz  %1,f
157          goto   %2
158 }
159
160
161 replace restart {
162         movf    %1,w
163         movf    %1,w
164 } by {
165         ; peep 8 - Removed redundant move
166         movf    %1,w
167 }
168
169
170 replace restart {
171         movlw   %1
172         movwf   %2
173         movlw   %1
174 } by {
175         ; peep 10 - Removed redundant move
176         movlw   %1
177         movwf   %2
178 }
179
180 replace restart {
181         movwf   %1
182         movwf   %1
183 } by {
184         ; peep 11 - Removed redundant move
185         movwf   %1
186 }