pCode peep is fully working for the PIC port.
[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
98 //bogus test for pcode
99 //replace restart {
100 //      movf    %1,w    ;comment at end
101 //%4:   movf    %1,w
102 //      RETURN
103 //      clrf    INDF
104 //      movlw   0xa5
105 //      movf    fsr,w
106 //      incf    indf,f
107 //      %2
108 //} by {
109 //      ; peep test remove redundant move
110 //%4:   movf    %1,w    ;another comment
111 //      %2
112 //} if AYBABTU %3
113
114
115 // peep 2
116 replace restart {
117         movwf   %1
118         movf    %1,w
119 } by {
120         ; peep 2 - Removed redundant move
121         movwf   %1
122 } if NZ
123
124 // peep 3
125 replace restart {
126         decf    %1,f
127         movf    %1,w
128         btfss   status,z
129         goto    %2
130 } by {
131         ; peep 3 - decf/mov/skpz to decfsz
132         decfsz  %1,f
133          goto   %2
134 }
135
136
137 replace restart {
138         movf    %1,w
139         movf    %1,w
140 } by {
141         ; peep 8 - Removed redundant move
142         movf    %1,w
143 }
144
145
146 replace restart {
147         movlw   %1
148         movwf   %2
149         movlw   %1
150 } by {
151         ; peep 10 - Removed redundant move
152         movlw   %1
153         movwf   %2
154 }
155
156 replace restart {
157         movwf   %1
158         movwf   %1
159 } by {
160         ; peep 11 - Removed redundant move
161         movwf   %1
162 }