Reshaped doc's
[fw/sdcc] / doc / sdccman.html / node39.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2
3 <!--Converted with LaTeX2HTML 99.1 release (March 30, 1999)
4 original version by:  Nikos Drakos, CBLU, University of Leeds
5 * revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
6 * with significant contributions from:
7   Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
8 <HTML>
9 <HEAD>
10 <TITLE>4.1 Optimizations</TITLE>
11 <META NAME="description" CONTENT="4.1 Optimizations">
12 <META NAME="keywords" CONTENT="sdccman">
13 <META NAME="resource-type" CONTENT="document">
14 <META NAME="distribution" CONTENT="global">
15
16 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
17 <META NAME="Generator" CONTENT="LaTeX2HTML v99.1 release">
18 <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
19
20 <LINK REL="STYLESHEET" HREF="sdccman.css">
21
22 <LINK REL="next" HREF="node40.html">
23 <LINK REL="previous" HREF="node38.html">
24 <LINK REL="up" HREF="node38.html">
25 <LINK REL="next" HREF="node40.html">
26 </HEAD>
27
28 <BODY >
29 <!--Navigation Panel-->
30 <A NAME="tex2html841"
31  HREF="node40.html">
32 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A> 
33 <A NAME="tex2html835"
34  HREF="node38.html">
35 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A> 
36 <A NAME="tex2html829"
37  HREF="node38.html">
38 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A> 
39 <A NAME="tex2html837"
40  HREF="node1.html">
41 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A> 
42 <A NAME="tex2html839"
43  HREF="node61.html">
44 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A> 
45 <BR>
46 <B> Next:</B> <A NAME="tex2html842"
47  HREF="node40.html">4.2 Pragmas</A>
48 <B> Up:</B> <A NAME="tex2html836"
49  HREF="node38.html">4. SDCC Technical Data</A>
50 <B> Previous:</B> <A NAME="tex2html830"
51  HREF="node38.html">4. SDCC Technical Data</A>
52  &nbsp <B>  <A NAME="tex2html838"
53  HREF="node1.html">Contents</A></B> 
54  &nbsp <B>  <A NAME="tex2html840"
55  HREF="node61.html">Index</A></B> 
56 <BR>
57 <BR>
58 <!--End of Navigation Panel-->
59 <!--Table of Child-Links-->
60 <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
61
62 <UL>
63 <LI><A NAME="tex2html843"
64  HREF="node39.html#SECTION00051100000000000000">4.1.1 Sub-expression Elimination</A>
65 <LI><A NAME="tex2html844"
66  HREF="node39.html#SECTION00051200000000000000">4.1.2 Dead-Code Elimination</A>
67 <LI><A NAME="tex2html845"
68  HREF="node39.html#SECTION00051300000000000000">4.1.3 Copy-Propagation</A>
69 <LI><A NAME="tex2html846"
70  HREF="node39.html#SECTION00051400000000000000">4.1.4 Loop Optimizations</A>
71 <LI><A NAME="tex2html847"
72  HREF="node39.html#SECTION00051500000000000000">4.1.5 Loop Reversing</A>
73 <LI><A NAME="tex2html848"
74  HREF="node39.html#SECTION00051600000000000000">4.1.6 Algebraic Simplifications</A>
75 <LI><A NAME="tex2html849"
76  HREF="node39.html#SECTION00051700000000000000">4.1.7 'switch' Statements</A>
77 <LI><A NAME="tex2html850"
78  HREF="node39.html#SECTION00051800000000000000">4.1.8 Bit-shifting Operations.</A>
79 <LI><A NAME="tex2html851"
80  HREF="node39.html#SECTION00051900000000000000">4.1.9 Bit-rotation</A>
81 <LI><A NAME="tex2html852"
82  HREF="node39.html#SECTION000511000000000000000">4.1.10 Highest Order Bit</A>
83 <LI><A NAME="tex2html853"
84  HREF="node39.html#SECTION000511100000000000000">4.1.11 Peep-hole Optimizer</A>
85 </UL>
86 <!--End of Table of Child-Links-->
87 <HR>
88
89 <H2><A NAME="SECTION00051000000000000000">
90 4.1 Optimizations</A>
91 </H2>
92
93 <P>
94 SDCC performs a host of standard optimizations in addition to some
95 MCU specific optimizations. 
96
97 <P>
98
99 <H3><A NAME="SECTION00051100000000000000">
100 4.1.1 Sub-expression Elimination</A>
101 </H3>
102
103 <P>
104 The compiler does local and global common subexpression elimination,
105 e.g.: 
106 <BR>
107
108 <BR>
109 <TT>i = x + y + 1; </TT>&nbsp;
110 <BR>
111 <TT>j = x + y;</TT>
112 <BR>
113
114 <BR>
115 will be translated to
116 <BR>
117
118 <BR>
119 <TT>iTemp = x + y </TT>&nbsp;
120 <BR>
121 <TT>i = iTemp + 1 </TT>&nbsp;
122 <BR>
123 <TT>j = iTemp</TT>&nbsp;
124 <BR>
125
126 <BR>
127 Some subexpressions are not as obvious as the above example, e.g.:
128 <BR>
129
130 <BR>
131 <TT>a-&gt;b[i].c = 10; </TT>&nbsp;
132 <BR>
133 <TT>a-&gt;b[i].d = 11;</TT>
134 <BR>
135
136 <BR>
137 In this case the address arithmetic a-&gt;b[i] will be computed only
138 once; the equivalent code in C would be.
139 <BR>
140
141 <BR>
142 <TT>iTemp = a-&gt;b[i]; </TT>&nbsp;
143 <BR>
144 <TT>iTemp.c = 10; </TT>&nbsp;
145 <BR>
146 <TT>iTemp.d = 11;</TT>
147 <BR>
148
149 <BR>
150 The compiler will try to keep these temporary variables in registers.
151
152 <P>
153
154 <H3><A NAME="SECTION00051200000000000000">
155 4.1.2 Dead-Code Elimination</A>
156 </H3>
157
158 <P>
159  
160 <TT>int global; </TT>&nbsp;
161 <BR>
162 <TT>void f () { </TT>&nbsp;
163 <BR>
164 <TT>&nbsp;&nbsp;int i; </TT>&nbsp;
165 <BR>
166 <TT>&nbsp;&nbsp;i = 1; &nbsp;/* dead store */ </TT>&nbsp;
167 <BR>
168 <TT>&nbsp;&nbsp;global = 1;&nbsp;/* dead store */ </TT>&nbsp;
169 <BR>
170 <TT>&nbsp;&nbsp;global = 2; </TT>&nbsp;
171 <BR>
172 <TT>&nbsp;&nbsp;return; </TT>&nbsp;
173 <BR>
174 <TT>&nbsp;&nbsp;global = 3;&nbsp;/* unreachable */ </TT>&nbsp;
175 <BR>
176 <TT>}</TT>
177 <BR>
178
179 <BR>
180 will be changed to
181 <BR>
182
183 <BR>
184 <TT>int global; void f () </TT>&nbsp;
185 <BR>
186 <TT>{</TT>&nbsp;
187 <BR>
188 <TT>&nbsp;&nbsp;global = 2; </TT>&nbsp;
189 <BR>
190 <TT>&nbsp;&nbsp;return; </TT>&nbsp;
191 <BR>
192 <TT>}</TT>
193
194 <P>
195
196 <H3><A NAME="SECTION00051300000000000000">
197 4.1.3 Copy-Propagation</A>
198 </H3>
199
200 <P>
201  
202 <TT>int f() { </TT>&nbsp;
203 <BR>
204 <TT>&nbsp;&nbsp;int i, j; </TT>&nbsp;
205 <BR>
206 <TT>&nbsp;&nbsp;i = 10; </TT>&nbsp;
207 <BR>
208 <TT>&nbsp;&nbsp;j = i; </TT>&nbsp;
209 <BR>
210 <TT>&nbsp;&nbsp;return j; </TT>&nbsp;
211 <BR>
212 <TT>}</TT>
213 <BR>
214
215 <BR>
216 will be changed to 
217 <BR>
218
219 <BR>
220 <TT>int f() { </TT>&nbsp;
221 <BR>
222 <TT>&nbsp; &nbsp; int i,j; </TT>&nbsp;
223 <BR>
224 <TT>&nbsp; &nbsp; i = 10; </TT>&nbsp;
225 <BR>
226 <TT>&nbsp; &nbsp; j = 10; </TT>&nbsp;
227 <BR>
228 <TT>&nbsp; &nbsp; return 10; </TT>&nbsp;
229 <BR>
230 <TT>}</TT>&nbsp;
231 <BR>&nbsp;
232 <BR>
233 Note: the dead stores created by this copy propagation will be eliminated
234 by dead-code elimination.
235
236 <P>
237
238 <H3><A NAME="SECTION00051400000000000000">
239 4.1.4 Loop Optimizations</A>
240 </H3>
241
242 <P>
243 Two types of loop optimizations are done by SDCC loop invariant lifting
244 and strength reduction of loop induction variables. In addition to
245 the strength reduction the optimizer marks the induction variables
246 and the register allocator tries to keep the induction variables in
247 registers for the duration of the loop. Because of this preference
248 of the register allocator, loop induction optimization causes an increase
249 in register pressure, which may cause unwanted spilling of other temporary
250 variables into the stack / data space. The compiler will generate
251 a warning message when it is forced to allocate extra space either
252 on the stack or data space. If this extra space allocation is undesirable
253 then induction optimization can be eliminated either for the entire
254 source file (with -noinduction option) or for a given function only
255 using #pragma&nbsp;NOINDUCTION.
256 <BR>
257
258 <BR>
259 Loop Invariant:
260 <BR>
261
262 <BR>
263 <TT>for (i = 0 ; i &lt; 100 ; i ++) </TT>&nbsp;
264 <BR>
265  <TT>&nbsp; &nbsp;f += k + l;</TT>
266 <BR>
267
268 <BR>
269 changed to
270 <BR>
271
272 <BR>
273 <TT>itemp = k + l; </TT>&nbsp;
274 <BR>
275 <TT>for (i = 0; i &lt; 100; i++) </TT>&nbsp;
276 <BR>
277 <TT>&nbsp;&nbsp;f += itemp;</TT>
278 <BR>
279
280 <BR>
281 As mentioned previously some loop invariants are not as apparent,
282 all static address computations are also moved out of the loop.
283 <BR>
284
285 <BR>
286 Strength Reduction, this optimization substitutes an expression by
287 a cheaper expression:
288 <BR>
289
290 <BR>
291 <TT>for (i=0;i &lt; 100; i++)</TT>&nbsp;
292 <BR>
293 <TT>&nbsp;&nbsp;ar[i*5] = i*3;</TT>
294 <BR>
295
296 <BR>
297 changed to
298 <BR>
299
300 <BR>
301 <TT>itemp1 = 0; </TT>&nbsp;
302 <BR>
303 <TT>itemp2 = 0; </TT>&nbsp;
304 <BR>
305 <TT>for (i=0;i&lt; 100;i++) { </TT>&nbsp;
306 <BR>
307  <TT>&nbsp; &nbsp;ar[itemp1] = itemp2; </TT>&nbsp;
308 <BR>
309  <TT>&nbsp; &nbsp;itemp1 += 5; </TT>&nbsp;
310 <BR>
311  <TT>&nbsp; &nbsp;itemp2 += 3; </TT>&nbsp;
312 <BR>
313 <TT>}</TT>
314 <BR>
315
316 <BR>
317 The more expensive multiplication is changed to a less expensive addition.
318
319 <P>
320
321 <H3><A NAME="SECTION00051500000000000000">
322 4.1.5 Loop Reversing</A>
323 </H3>
324
325 <P>
326 This optimization is done to reduce the overhead of checking loop
327 boundaries for every iteration. Some simple loops can be reversed
328 and implemented using a ``decrement and jump if not zero'' instruction.
329 SDCC checks for the following criterion to determine if a loop is
330 reversible (note: more sophisticated compilers use data-dependency
331 analysis to make this determination, SDCC uses a more simple minded
332 analysis).
333
334 <P>
335
336 <UL>
337 <LI>The 'for' loop is of the form 
338 <BR>
339
340 <BR>
341 <TT>for (&lt;symbol&gt; = &lt;expression&gt; ; &lt;sym&gt; [&lt; | &lt;=] &lt;expression&gt;
342 ; [&lt;sym&gt;++ | &lt;sym&gt; += 1])</TT>&nbsp;
343 <BR>
344 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&lt;for body&gt;</TT></LI>
345 <LI>The &lt;for body&gt; does not contain ``continue'' or 'break''.</LI>
346 <LI>All goto's are contained within the loop.</LI>
347 <LI>No function calls within the loop.</LI>
348 <LI>The loop control variable &lt;sym&gt; is not assigned any value within the
349 loop</LI>
350 <LI>The loop control variable does NOT participate in any arithmetic operation
351 within the loop.</LI>
352 <LI>There are NO switch statements in the loop.</LI>
353 </UL>
354 Note djnz instruction can be used for 8-bit values <I>only</I>, therefore
355 it is advantageous to declare loop control symbols as <I>char</I>.
356 Ofcourse this may not be possible on all situations.
357
358 <P>
359
360 <H3><A NAME="SECTION00051600000000000000">
361 4.1.6 Algebraic Simplifications</A>
362 </H3>
363
364 <P>
365 SDCC does numerous algebraic simplifications, the following is a small
366 sub-set of these optimizations.
367 <BR>
368
369 <BR>
370 <TT>i = j + 0 ; /* changed to */ i = j; </TT>&nbsp;
371 <BR>
372 <TT>i /= 2; /* changed to */ i &gt;&gt;= 1; </TT>&nbsp;
373 <BR>
374 <TT>i = j - j ; /* changed to */ i = 0; </TT>&nbsp;
375 <BR>
376 <TT>i = j / 1 ; /* changed to */ i = j;</TT>
377 <BR>
378
379 <BR>
380 Note the subexpressions given above are generally introduced by macro
381 expansions or as a result of copy/constant propagation.
382
383 <P>
384
385 <H3><A NAME="SECTION00051700000000000000">
386 4.1.7 'switch' Statements</A>
387 </H3>
388
389 <P>
390 SDCC changes switch statements to jump tables when the following conditions
391 are true. 
392
393 <P>
394
395 <UL>
396 <LI>The case labels are in numerical sequence, the labels need not be
397 in order, and the starting number need not be one or zero.
398 <BR>
399
400 <BR>
401 <TT>switch(i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch (i)
402 { </TT>&nbsp;
403 <BR>
404 <TT>case 4:... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1: ... </TT>&nbsp;
405 <BR>
406 <TT>case 5:... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 2: ... </TT>&nbsp;
407 <BR>
408 <TT>case 3:... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 3: ... </TT>&nbsp;
409 <BR>
410 <TT>case 6:... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 4: ... </TT>&nbsp;
411 <BR>
412 <TT>}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</TT>&nbsp;
413 <BR>&nbsp;
414 <BR>
415 Both the above switch statements will be implemented using a jump-table.</LI>
416 <LI>The number of case labels is at least three, since it takes two conditional
417 statements to handle the boundary conditions.</LI>
418 <LI>The number of case labels is less than 84, since each label takes
419 3 bytes and a jump-table can be utmost 256 bytes long. </LI>
420 </UL>
421 Switch statements which have gaps in the numeric sequence or those
422 that have more that 84 case labels can be split into more than one
423 switch statement for efficient code generation, e.g.:
424 <BR>
425
426 <BR>
427 <TT>switch (i) { </TT>&nbsp;
428 <BR>
429 <TT>case 1: ... </TT>&nbsp;
430 <BR>
431 <TT>case 2: ... </TT>&nbsp;
432 <BR>
433 <TT>case 3: ... </TT>&nbsp;
434 <BR>
435 <TT>case 4: ... </TT>&nbsp;
436 <BR>
437 <TT>case 9: ... </TT>&nbsp;
438 <BR>
439 <TT>case 10: ... </TT>&nbsp;
440 <BR>
441 <TT>case 11: ... </TT>&nbsp;
442 <BR>
443 <TT>case 12: ... </TT>&nbsp;
444 <BR>
445 <TT>}</TT>
446 <BR>
447
448 <BR>
449 If the above switch statement is broken down into two switch statements
450 <BR>
451
452 <BR>
453 <TT>switch (i) { </TT>&nbsp;
454 <BR>
455 <TT>case 1: ... </TT>&nbsp;
456 <BR>
457 <TT>case 2: ... </TT>&nbsp;
458 <BR>
459 <TT>case 3: ... </TT>&nbsp;
460 <BR>
461 <TT>case 4: ... </TT>&nbsp;
462 <BR>
463 <TT>}</TT>&nbsp;
464 <BR>&nbsp;
465 <BR>
466 and&nbsp;
467 <BR>&nbsp;
468 <BR>
469 <TT>switch (i) { </TT>&nbsp;
470 <BR>
471 <TT>case 9: &nbsp;... </TT>&nbsp;
472 <BR>
473 <TT>case 10: ... </TT>&nbsp;
474 <BR>
475 <TT>case 11: ... </TT>&nbsp;
476 <BR>
477 <TT>case 12:&nbsp;... </TT>&nbsp;
478 <BR>
479 <TT>}</TT>&nbsp;
480 <BR>&nbsp;
481 <BR>
482 then both the switch statements will be implemented using jump-tables
483 whereas the unmodified switch statement will not be.
484
485 <P>
486
487 <H3><A NAME="SECTION00051800000000000000">
488 4.1.8 Bit-shifting Operations.</A>
489 </H3>
490
491 <P>
492 Bit shifting is one of the most frequently used operation in embedded
493 programming. SDCC tries to implement bit-shift operations in the most
494 efficient way possible, e.g.:
495 <BR>&nbsp;
496 <BR>
497 <TT>unsigned char i;</TT>&nbsp;
498 <BR>
499 <TT>... </TT>&nbsp;
500 <BR>
501 <TT>i&gt;&gt;= 4; </TT>&nbsp;
502 <BR>
503 <TT>...</TT>&nbsp;
504 <BR>
505
506 <BR>
507 generates the following code:
508 <BR>&nbsp;
509 <BR>
510 <TT>mov a,_i </TT>&nbsp;
511 <BR>
512 <TT>swap a </TT>&nbsp;
513 <BR>
514 <TT>anl a,#0x0f </TT>&nbsp;
515 <BR>
516 <TT>mov _i,a</TT>
517 <BR>
518
519 <BR>
520 In general SDCC will never setup a loop if the shift count is known.
521 Another example:
522 <BR>
523
524 <BR>
525 <TT>unsigned int i; </TT>&nbsp;
526 <BR>
527 <TT>... </TT>&nbsp;
528 <BR>
529 <TT>i &gt;&gt;= 9; </TT>&nbsp;
530 <BR>
531 <TT>...</TT>
532 <BR>
533
534 <BR>
535 will generate:
536 <BR>
537
538 <BR>
539 <TT>mov a,(_i + 1) </TT>&nbsp;
540 <BR>
541 <TT>mov (_i + 1),#0x00 </TT>&nbsp;
542 <BR>
543 <TT>clr c </TT>&nbsp;
544 <BR>
545 <TT>rrc a </TT>&nbsp;
546 <BR>
547 <TT>mov _i,a</TT>
548 <BR>
549
550 <BR>
551 Note that SDCC stores numbers in little-endian format (i.e. lowest
552 order first).
553
554 <P>
555
556 <H3><A NAME="SECTION00051900000000000000">
557 4.1.9 Bit-rotation</A>
558 </H3>
559
560 <P>
561 A special case of the bit-shift operation is bit rotation, SDCC recognizes
562 the following expression to be a left bit-rotation:
563 <BR>
564
565 <BR>
566 <TT>unsigned char i; </TT>&nbsp;
567 <BR>
568 <TT>... </TT>&nbsp;
569 <BR>
570 <TT>i = ((i &lt;&lt; 1) | (i &gt;&gt;
571 7));</TT> 
572 <BR>
573 ...
574 <BR>
575
576 <BR>
577 will generate the following code:
578 <BR>
579
580 <BR>
581 <TT>mov a,_i </TT>&nbsp;
582 <BR>
583 <TT>rl a </TT>&nbsp;
584 <BR>
585 <TT>mov _i,a</TT>
586 <BR>
587
588 <BR>
589 SDCC uses pattern matching on the parse tree to determine this operation.Variations
590 of this case will also be recognized as bit-rotation, i.e.: 
591 <BR>
592
593 <BR>
594 <TT>i = ((i &gt;&gt; 7) | (i &lt;&lt;
595 1)); /* left-bit rotation */</TT>
596
597 <P>
598
599 <H3><A NAME="SECTION000511000000000000000">
600 4.1.10 Highest Order Bit</A>
601 </H3>
602
603 <P>
604 It is frequently required to obtain the highest order bit of an integral
605 type (long, int, short or char types). SDCC recognizes the following
606 expression to yield the highest order bit and generates optimized
607 code for it, e.g.:
608 <BR>
609
610 <BR>
611  <TT>unsigned int gint; </TT>&nbsp;
612 <BR>&nbsp;
613 <BR>
614 <TT>foo () { </TT>&nbsp;
615 <BR>
616 <TT>unsigned char hob; </TT>&nbsp;
617 <BR>
618 <TT>&nbsp;&nbsp;... </TT>&nbsp;
619 <BR>
620 <TT>&nbsp;&nbsp;hob = (gint &gt;&gt; 15) &amp; 1; </TT>&nbsp;
621 <BR>
622 <TT>&nbsp;&nbsp;.. </TT>&nbsp;
623 <BR>
624 <TT>}</TT>
625 <BR>
626
627 <BR>
628 will generate the following code:
629 <BR>&nbsp;
630 <BR>
631 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 61
632 ;&nbsp; hob.c 7 </TT>&nbsp;
633 <BR>
634 <TT>&nbsp;&nbsp; 000A E5*01&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 62&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
635 mov&nbsp; a,(_gint + 1) </TT>&nbsp;
636 <BR>
637 <TT>&nbsp;&nbsp; 000C 33&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
638 rlc&nbsp; a </TT>&nbsp;
639 <BR>
640 <TT>&nbsp;&nbsp; 000D E4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
641 clr&nbsp; a </TT>&nbsp;
642 <BR>
643 <TT>&nbsp;&nbsp; 000E 13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
644 rrc&nbsp; a </TT>&nbsp;
645 <BR>
646 <TT>&nbsp;&nbsp; 000F F5*02&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
647 mov&nbsp; _foo_hob_1_1,a</TT>&nbsp;
648 <BR>&nbsp;
649 <BR>
650 Variations of this case however will <I>not</I> be recognized. It
651 is a standard C expression, so I heartily recommend this be the only
652 way to get the highest order bit, (it is portable). Of course it will
653 be recognized even if it is embedded in other expressions, e.g.:
654 <BR>
655
656 <BR>
657 <TT>xyz = gint + ((gint &gt;&gt; 15) &amp; 1);</TT>
658 <BR>
659
660 <BR>
661 will still be recognized.
662
663 <P>
664
665 <H3><A NAME="SECTION000511100000000000000">
666 4.1.11 Peep-hole Optimizer</A>
667 </H3>
668
669 <P>
670 The compiler uses a rule based, pattern matching and re-writing mechanism
671 for peep-hole optimization. It is inspired by <I>copt</I> a peep-hole
672 optimizer by Christopher W. Fraser (cwfraser@microsoft.com). A default
673 set of rules are compiled into the compiler, additional rules may
674 be added with the <I>-peep-file &lt;filename&gt;</I> option. The rule language
675 is best illustrated with examples.
676 <BR>
677
678 <BR>
679 <TT>replace { </TT>&nbsp;
680 <BR>
681 <TT>&nbsp;&nbsp;mov %1,a </TT>&nbsp;
682 <BR>
683 <TT>&nbsp;&nbsp;mov a,%1</TT>&nbsp;
684 <BR>
685 <TT>} by {</TT>&nbsp;
686 <BR>
687 <TT>&nbsp;&nbsp;mov %1,a</TT>&nbsp;
688 <BR>
689 <TT>}</TT>
690 <BR>
691
692 <BR>
693 The above rule will change the following assembly sequence:
694 <BR>
695
696 <BR>
697 <TT>&nbsp;&nbsp;mov r1,a </TT>&nbsp;
698 <BR>
699 <TT>&nbsp;&nbsp;mov a,r1</TT>
700 <BR>
701
702 <BR>
703 to
704 <BR>
705
706 <BR>
707 <TT>mov r1,a</TT>
708 <BR>
709
710 <BR>
711 Note: All occurrences of a <I>%n</I> (pattern variable) must denote
712 the same string. With the above rule, the assembly sequence:
713 <BR>
714
715 <BR>
716 <TT>&nbsp;&nbsp;mov r1,a </TT>&nbsp;
717 <BR>
718 <TT>&nbsp;&nbsp;mov a,r2</TT>
719 <BR>
720
721 <BR>
722 will remain unmodified.
723 <BR>
724
725 <BR>
726 Other special case optimizations may be added by the user (via <I>-peep-file
727 option</I>). E.g. some variants of the 8051 MCU allow only <TT>ajmp</TT>
728 and <TT>acall</TT>. The following two rules will change all <TT>ljmp</TT>
729 and <TT>lcall</TT> to <TT>ajmp</TT> and <TT>acall</TT>
730 <BR>
731
732 <BR>
733 <TT>replace { lcall %1 } by { acall %1 } </TT>&nbsp;
734 <BR>
735 <TT>replace { ljmp %1 } by { ajmp %1 }</TT>
736 <BR>
737
738 <BR>
739 The <I>inline-assembler code</I> is also passed through the peep hole
740 optimizer, thus the peephole optimizer can also be used as an assembly
741 level macro expander. The rules themselves are MCU dependent whereas
742 the rule language infra-structure is MCU independent. Peephole optimization
743 rules for other MCU can be easily programmed using the rule language.
744 <BR>
745
746 <BR>
747 The syntax for a rule is as follows:
748 <BR>
749
750 <BR>
751 <TT>rule := replace [ restart ] '{' &lt;assembly sequence&gt; '&#92;n'
752 </TT>&nbsp;
753 <BR>
754 <TT>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}' by '{' '&#92;n'
755 </TT>&nbsp;
756 <BR>
757 <TT>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;assembly
758 sequence&gt; '&#92;n' </TT>&nbsp;
759 <BR>
760 <TT>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}' [if &lt;functionName&gt;
761 ] '&#92;n' </TT>&nbsp;
762 <BR>
763
764 <BR>
765 &lt;assembly sequence&gt; := assembly instruction (each instruction including
766 labels must be on a separate line).
767 <BR>
768
769 <BR>
770 The optimizer will apply to the rules one by one from the top in the
771 sequence of their appearance, it will terminate when all rules are
772 exhausted. If the 'restart' option is specified, then the optimizer
773 will start matching the rules again from the top, this option for
774 a rule is expensive (performance), it is intended to be used in situations
775 where a transformation will trigger the same rule again. A good example
776 of this the following rule:
777 <BR>
778
779 <BR>
780 <TT>replace restart { </TT>&nbsp;
781 <BR>
782 <TT>&nbsp;&nbsp;pop %1 </TT>&nbsp;
783 <BR>
784 <TT>&nbsp;&nbsp;push %1 } by { </TT>&nbsp;
785 <BR>
786 <TT>&nbsp;&nbsp;; nop </TT>&nbsp;
787 <BR>
788 <TT>}</TT>
789 <BR>
790
791 <BR>
792 Note that the replace pattern cannot be a blank, but can be a comment
793 line. Without the 'restart' option only the inner most 'pop' 'push'
794 pair would be eliminated, i.e.:
795 <BR>
796
797 <BR>
798 <TT>&nbsp;&nbsp;pop ar1 </TT>&nbsp;
799 <BR>
800 <TT>&nbsp;&nbsp;pop ar2 </TT>&nbsp;
801 <BR>
802 <TT>&nbsp;&nbsp;push ar2 </TT>&nbsp;
803 <BR>
804 <TT>&nbsp;&nbsp;push ar1</TT>
805 <BR>
806
807 <BR>
808 would result in:
809 <BR>
810
811 <BR>
812 <TT>&nbsp;&nbsp;pop ar1 </TT>&nbsp;
813 <BR>
814 <TT>&nbsp;&nbsp;; nop </TT>&nbsp;
815 <BR>
816 <TT>&nbsp;&nbsp;push ar1</TT>
817 <BR>
818
819 <BR>
820 <I>with</I> the restart option the rule will be applied again to the
821 resulting code and then all the pop-push pairs will be eliminated
822 to yield:
823 <BR>
824
825 <BR>
826 <TT>&nbsp;&nbsp;; nop </TT>&nbsp;
827 <BR>
828 <TT>&nbsp;&nbsp;; nop</TT>
829 <BR>
830
831 <BR>
832 A conditional function can be attached to a rule. Attaching rules
833 are somewhat more involved, let me illustrate this with an example.
834 <BR>
835
836 <BR>
837 <TT>replace { </TT>&nbsp;
838 <BR>
839 <TT>&nbsp; &nbsp; &nbsp;ljmp %5 </TT>&nbsp;
840 <BR>
841 <TT>%2:</TT>&nbsp;
842 <BR>
843 <TT>} by { </TT>&nbsp;
844 <BR>
845 <TT>&nbsp; &nbsp; &nbsp;sjmp %5 </TT>&nbsp;
846 <BR>
847 <TT>%2:</TT>&nbsp;
848 <BR>
849 <TT>} if labelInRange</TT>
850 <BR>
851
852 <BR>
853 The optimizer does a look-up of a function name table defined in function
854 <I>callFuncByName</I> in the source file SDCCpeeph.c, with the name
855 <I>labelInRange</I>. If it finds a corresponding entry the function
856 is called. Note there can be no parameters specified for these functions,
857 in this case the use of <I>%5</I> is crucial, since the function
858 <I>labelInRange</I> expects to find the label in that particular variable
859 (the hash table containing the variable bindings is passed as a parameter).
860 If you want to code more such functions, take a close look at the
861 function labelInRange and the calling mechanism in source file SDCCpeeph.c.
862 I know this whole thing is a little kludgey, but maybe some day we
863 will have some better means. If you are looking at this file, you
864 will also see the default rules that are compiled into the compiler,
865 you can add your own rules in the default set there if you get tired
866 of specifying the -peep-file option.
867
868 <P>
869 <HR>
870 <!--Navigation Panel-->
871 <A NAME="tex2html841"
872  HREF="node40.html">
873 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A> 
874 <A NAME="tex2html835"
875  HREF="node38.html">
876 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A> 
877 <A NAME="tex2html829"
878  HREF="node38.html">
879 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A> 
880 <A NAME="tex2html837"
881  HREF="node1.html">
882 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A> 
883 <A NAME="tex2html839"
884  HREF="node61.html">
885 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A> 
886 <BR>
887 <B> Next:</B> <A NAME="tex2html842"
888  HREF="node40.html">4.2 Pragmas</A>
889 <B> Up:</B> <A NAME="tex2html836"
890  HREF="node38.html">4. SDCC Technical Data</A>
891 <B> Previous:</B> <A NAME="tex2html830"
892  HREF="node38.html">4. SDCC Technical Data</A>
893  &nbsp <B>  <A NAME="tex2html838"
894  HREF="node1.html">Contents</A></B> 
895  &nbsp <B>  <A NAME="tex2html840"
896  HREF="node61.html">Index</A></B> 
897 <!--End of Navigation Panel-->
898 <ADDRESS>
899 <I>Johan Knol</I>
900 <BR><I>2001-07-13</I>
901 </ADDRESS>
902 </BODY>
903 </HTML>