* src/SDCClrange.c (findNextUseSym): fixed a live range bug with
[fw/sdcc] / src / SDCClrange.c
1 /*-------------------------------------------------------------------------
2
3   SDCClrange.c - source file for live range computations
4
5              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!  
24 -------------------------------------------------------------------------*/
25
26 #include "common.h"
27 #include "limits.h"
28
29 int iCodeSeq = 0;
30 hTab *liveRanges = NULL;
31 hTab *iCodehTab = NULL;
32 hTab *iCodeSeqhTab = NULL;
33
34 /*-----------------------------------------------------------------*/
35 /* hashiCodeKeys - add all iCodes to the hash table                */
36 /*-----------------------------------------------------------------*/
37 void 
38 hashiCodeKeys (eBBlock ** ebbs, int count)
39 {
40   int i;
41
42   for (i = 0; i < count; i++)
43     {
44       iCode *ic;
45       for (ic = ebbs[i]->sch; ic; ic = ic->next)
46         hTabAddItem (&iCodehTab, ic->key, ic);
47     }
48 }
49
50 /*-----------------------------------------------------------------*/
51 /* sequenceiCode - creates a sequence number for the iCode & add   */
52 /*-----------------------------------------------------------------*/
53 void 
54 sequenceiCode (eBBlock ** ebbs, int count)
55 {
56   int i;
57
58   for (i = 0; i < count; i++)
59     {
60
61       iCode *ic;
62       ebbs[i]->fSeq = iCodeSeq + 1;
63       for (ic = ebbs[i]->sch; ic; ic = ic->next)
64         {
65           ic->seq = ++iCodeSeq;
66           ic->depth = ebbs[i]->depth;
67           //hTabAddItem (&iCodehTab, ic->key, ic);
68           hTabAddItem (&iCodeSeqhTab, ic->seq, ic);
69         }
70       ebbs[i]->lSeq = iCodeSeq;
71     }
72 }
73
74 /*-----------------------------------------------------------------*/
75 /* setFromRange - sets the from range of a given operand           */
76 /*-----------------------------------------------------------------*/
77 void
78 setFromRange (operand * op, int from)
79 {
80   /* only for compiler defined temporaries */
81   if (!IS_ITEMP (op))
82     return;
83
84   hTabAddItemIfNotP (&liveRanges, op->key, OP_SYMBOL (op));
85
86   if (op->isaddr)
87     OP_SYMBOL (op)->isptr = 1;
88
89   if (!OP_LIVEFROM (op) ||
90       OP_LIVEFROM (op) > from)
91     OP_LIVEFROM (op) = from;
92 }
93
94 /*-----------------------------------------------------------------*/
95 /* setToRange - set the range to for an operand                    */
96 /*-----------------------------------------------------------------*/
97 void 
98 setToRange (operand * op, int to, bool check)
99 {
100   /* only for compiler defined temps */
101   if (!IS_ITEMP (op))
102     return;
103
104   OP_SYMBOL (op)->key = op->key;
105   hTabAddItemIfNotP (&liveRanges, op->key, OP_SYMBOL (op));
106
107   if (op->isaddr)
108     OP_SYMBOL (op)->isptr = 1;
109
110   if (check)
111     if (!OP_LIVETO (op))
112       OP_LIVETO (op) = to;
113     else;
114   else
115     OP_LIVETO (op) = to;
116 }
117
118 /*-----------------------------------------------------------------*/
119 /* setFromRange - sets the from range of a given operand           */
120 /*-----------------------------------------------------------------*/
121 void
122 setLiveFrom (symbol * sym, int from)
123 {
124   if (!sym->liveFrom || sym->liveFrom > from)
125     sym->liveFrom = from;
126 }
127
128 /*-----------------------------------------------------------------*/
129 /* setToRange - set the range to for an operand                    */
130 /*-----------------------------------------------------------------*/
131 void
132 setLiveTo (symbol * sym, int to)
133 {
134   if (!sym->liveTo || sym->liveTo < to)
135     sym->liveTo = to;
136 }
137
138 /*-----------------------------------------------------------------*/
139 /* markLiveRanges - for each operand mark the liveFrom & liveTo    */
140 /*-----------------------------------------------------------------*/
141 void
142 markLiveRanges (eBBlock ** ebbs, int count)
143 {
144   int i, key;
145   symbol *sym;
146   
147   for (i = 0; i < count; i++)
148     {
149       iCode *ic;
150       
151       for (ic = ebbs[i]->sch; ic; ic = ic->next)
152         {
153           if (ic->op == CALL || ic->op == PCALL)
154               if (bitVectIsZero (OP_SYMBOL (IC_RESULT (ic))->uses))
155                 bitVectUnSetBit (ebbs[i]->defSet, ic->key);
156
157           /* for all iTemps alive at this iCode */
158           for (key = 1; key < ic->rlive->size; key++)
159             {
160               if (!bitVectBitValue(ic->rlive, key))
161                 continue;
162
163               sym = hTabItemWithKey(liveRanges, key);
164               setLiveTo(sym, ic->seq);
165               setLiveFrom(sym, ic->seq);
166             }
167
168         }
169     }
170 }
171
172 /*-----------------------------------------------------------------*/
173 /* markAlive - marks the operand as alive between sic and eic      */
174 /*-----------------------------------------------------------------*/
175 void
176 markAlive (iCode * sic, iCode * eic, int key)
177 {
178   iCode *dic;
179
180   for (dic = sic; dic != eic->next; dic = dic->next)
181     {
182       dic->rlive = bitVectSetBit (dic->rlive, key);
183     }
184 }
185
186 /*-----------------------------------------------------------------*/
187 /* findNextUseSym - finds the next use of the symbol and marks it  */
188 /*                  alive in between                               */
189 /*-----------------------------------------------------------------*/
190 int
191 findNextUseSym (eBBlock *ebp, iCode *ic, symbol * sym)
192 {
193   int retval = 0;
194   iCode *uic;
195   eBBlock *succ;
196
197   hTabAddItemIfNotP (&liveRanges, sym->key, sym);
198
199   if (!ic)
200     goto check_successors;
201
202   /* if we check a complete block and the symbol */
203   /* is alive at the beginning of the block */
204   /* and not defined in the first instructions */
205   /* then a next use exists (return 1) */
206   if ((ic == ebp->sch) && bitVectBitValue(ic->rlive, sym->key))
207     {
208       /* check if the first instruction is a def of this op */
209       if (ic->op == JUMPTABLE || ic->op == IFX || SKIP_IC2(ic))
210         return 1;
211
212       if (IS_ITEMP(IC_RESULT(ic)))
213         if (IC_RESULT(ic)->key == sym->key)
214           return 0;
215
216       return 1;
217     }
218
219   if (ebp->visited)
220     return 0;
221
222   if (ic == ebp->sch)
223     ebp->visited = 1;
224
225   /* for all remaining instructions in current block */
226   for (uic = ic; uic; uic = uic->next)
227     {
228
229       if (SKIP_IC2(uic))
230         continue;
231
232       if (uic->op == JUMPTABLE)
233         {
234           if (IS_ITEMP(IC_JTCOND(uic)) && IC_JTCOND(uic)->key == sym->key)
235             {
236               markAlive(ic, uic, sym->key);
237               return 1;
238             }
239            continue;
240          }
241
242       if (uic->op == IFX)
243         {
244           if (IS_ITEMP(IC_COND(uic)) && IC_COND(uic)->key == sym->key)
245             {
246               markAlive(ic, uic, sym->key);
247               return 1;
248             }
249            continue;
250          }
251
252       if (IS_ITEMP (IC_LEFT (uic)))
253         if (IC_LEFT (uic)->key == sym->key)
254           {
255             markAlive(ic, uic, sym->key);
256             return 1;
257           }
258
259       if (IS_ITEMP (IC_RIGHT (uic)))
260         if (IC_RIGHT (uic)->key == sym->key)
261           {
262             markAlive (ic, uic, sym->key);
263             return 1;
264           }
265
266       if (IS_ITEMP (IC_RESULT (uic)))
267         if (IC_RESULT (uic)->key == sym->key)
268           {
269             if (POINTER_SET (uic))
270               {
271                 markAlive (ic, uic, sym->key);
272                 return 1;
273               }
274             else
275               return 0;
276           }
277
278     }
279
280   /* check all successors */
281 check_successors:
282
283   succ = setFirstItem (ebp->succList);
284   for (; succ; succ = setNextItem (ebp->succList))
285     {
286       retval += findNextUseSym (succ, succ->sch, sym);
287     }
288
289   if (retval)
290     {
291       if (ic) markAlive (ic, ebp->ech, sym->key);
292       return 1;
293     }
294
295   return 0;
296 }
297
298 /*-----------------------------------------------------------------*/
299 /* findNextUse - finds the next use of the operand and marks it    */
300 /*               alive in between                                  */
301 /*-----------------------------------------------------------------*/
302 int
303 findNextUse (eBBlock *ebp, iCode *ic, operand *op)
304 {
305   if (op->isaddr)
306     OP_SYMBOL (op)->isptr = 1;
307
308   OP_SYMBOL (op)->key = op->key;
309
310   return findNextUseSym (ebp, ic, OP_SYMBOL(op));
311 }
312
313 /*-----------------------------------------------------------------*/
314 /* unvisitBlocks - clears visited in all blocks                    */
315 /*-----------------------------------------------------------------*/
316 void unvisitBlocks (eBBlock ** ebbs, int count)
317 {
318   int i;
319
320   for (i = 0; i < count; i++)
321     ebbs[i]->visited = 0;
322 }
323
324 /*-----------------------------------------------------------------*/
325 /* unvisitBlocks - clears visited in all blocks                    */
326 /*-----------------------------------------------------------------*/
327 void
328 incUsed (iCode *ic, operand *op)
329 {
330   if (ic->depth)
331     OP_SYMBOL (op)->used += (((unsigned int) 1 << ic->depth) + 1);
332   else
333     OP_SYMBOL (op)->used += 1;
334 }
335
336 /*-----------------------------------------------------------------*/
337 /* rliveClear - clears the rlive bitVectors                        */
338 /*-----------------------------------------------------------------*/
339 void
340 rliveClear (eBBlock ** ebbs, int count)
341 {
342   int i;
343
344   /* for all blocks do */
345   for (i = 0; i < count; i++)
346     {
347       iCode *ic;
348
349       /* for all instructions in this block do */
350       for (ic = ebbs[i]->sch; ic; ic = ic->next)
351         {
352           freeBitVect (ic->rlive);
353           ic->rlive = NULL;
354         }
355     }
356 }
357
358 /*-----------------------------------------------------------------*/
359 /* rlivePoint - for each point compute the ranges that are alive   */
360 /*-----------------------------------------------------------------*/
361 void
362 rlivePoint (eBBlock ** ebbs, int count)
363 {
364   int i, key;
365   eBBlock *succ;
366   bitVect *alive;
367
368   /* for all blocks do */
369   for (i = 0; i < count; i++)
370     {
371       iCode *ic;
372
373       /* for all instructions in this block do */
374       for (ic = ebbs[i]->sch; ic; ic = ic->next)
375         {
376
377           if (!ic->rlive)
378             ic->rlive = newBitVect (operandKey);
379
380           if (SKIP_IC2(ic))
381             continue;
382
383           if (ic->op == JUMPTABLE && IS_SYMOP(IC_JTCOND(ic)))
384             {
385               incUsed (ic, IC_JTCOND(ic));
386
387               if (!IS_ITEMP(IC_JTCOND(ic)))
388                 continue;
389
390               unvisitBlocks(ebbs, count);
391               ic->rlive = bitVectSetBit (ic->rlive, IC_JTCOND(ic)->key);
392               findNextUse (ebbs[i], ic->next, IC_JTCOND(ic));
393
394               continue;
395             }
396
397           if (ic->op == IFX && IS_SYMOP(IC_COND(ic)))
398             {
399               incUsed (ic, IC_COND(ic));
400
401               if (!IS_ITEMP(IC_COND(ic)))
402                 continue;
403
404               unvisitBlocks (ebbs, count);
405               ic->rlive = bitVectSetBit (ic->rlive, IC_COND(ic)->key);
406               findNextUse (ebbs[i], ic->next, IC_COND(ic));
407
408               continue;
409             }
410
411           if (IS_SYMOP(IC_LEFT(ic)))
412             {
413               incUsed (ic, IC_LEFT(ic));
414               if (IS_ITEMP(IC_LEFT(ic)))
415                 {
416
417                   unvisitBlocks(ebbs, count);
418                   ic->rlive = bitVectSetBit (ic->rlive, IC_LEFT(ic)->key);
419                   findNextUse (ebbs[i], ic->next, IC_LEFT(ic));
420
421                   /* if this is a send extend the LR to the call */
422                   if (ic->op == SEND)
423                     {
424                       iCode *lic;
425                       for (lic = ic; lic; lic = lic->next)
426                         {
427                           if (lic->op == CALL || lic->op == PCALL)
428                             {
429                               markAlive (ic, lic->prev, IC_LEFT (ic)->key);
430                               break;
431                             }
432                         }
433                     }
434                 }
435             }
436
437           if (IS_SYMOP(IC_RIGHT(ic)))
438             {
439               incUsed (ic, IC_RIGHT(ic));
440               if (IS_ITEMP(IC_RIGHT(ic)))
441                 {
442                   unvisitBlocks(ebbs, count);
443                   ic->rlive = bitVectSetBit (ic->rlive, IC_RIGHT(ic)->key);
444                   findNextUse (ebbs[i], ic->next, IC_RIGHT(ic));
445                 }
446             }
447
448           if (POINTER_SET(ic) && IS_SYMOP(IC_RESULT(ic)))
449             incUsed (ic, IC_RESULT(ic));
450
451           if (IS_ITEMP(IC_RESULT(ic)))
452             {
453               unvisitBlocks(ebbs, count);
454               ic->rlive = bitVectSetBit (ic->rlive, IC_RESULT(ic)->key);
455               findNextUse (ebbs[i], ic->next, IC_RESULT(ic));
456             }
457
458           if (!POINTER_SET(ic) && IC_RESULT(ic))
459             ic->defKey = IC_RESULT(ic)->key;
460
461         }
462
463       /* check all symbols that are alive in the last instruction */
464       /* but are not alive in all successors */
465
466       succ = setFirstItem (ebbs[i]->succList);
467       if (!succ)
468         continue;
469
470       alive = succ->sch->rlive;
471       while ((succ = setNextItem (ebbs[i]->succList)))
472         {
473           if (succ->sch)
474             alive = bitVectIntersect (alive, succ->sch->rlive);
475         }
476
477       if (ebbs[i]->ech)
478         alive = bitVectCplAnd ( bitVectCopy (ebbs[i]->ech->rlive), alive);
479
480       for (key = 1; key < alive->size; key++)
481         {
482           if (!bitVectBitValue (alive, key))
483             continue;
484
485           unvisitBlocks(ebbs, count);
486           findNextUseSym (ebbs[i], NULL, hTabItemWithKey (liveRanges, key));
487         }
488
489     }
490 }
491
492 /*-----------------------------------------------------------------*/
493 /* computeClash - find out which live ranges collide with others   */
494 /*-----------------------------------------------------------------*/
495 static void
496 computeClash (eBBlock ** ebbs, int count)
497 {
498   int i;
499
500   /* for all blocks do */
501   for (i = 0; i < count; i++)
502     {
503       iCode *ic;
504
505       /* for every iCode do */
506       for (ic = ebbs[i]->sch; ic; ic = ic->next)
507         {
508           symbol *sym1, *sym2;
509           int key1, key2;
510
511           /* for all iTemps alive at this iCode */
512           for (key1 = 1; key1 < ic->rlive->size; key1++)
513             {
514               if (!bitVectBitValue(ic->rlive, key1))
515                 continue;
516
517               sym1 = hTabItemWithKey(liveRanges, key1);
518
519               if (!sym1->isitmp)
520                 continue;
521
522               /* for all other iTemps alive at this iCode */
523               for (key2 = key1+1; key2 < ic->rlive->size; key2++)
524                 {
525                   if (!bitVectBitValue(ic->rlive, key2))
526                     continue;
527
528                   sym2 = hTabItemWithKey(liveRanges, key2);
529                   
530                   if (!sym2->isitmp)
531                     continue;
532
533                   /* if the result and left or right is an iTemp */
534                   /* than possibly the iTemps do not clash */
535                   if ((ic->op != JUMPTABLE) && (ic->op != IFX) &&
536                       IS_ITEMP(IC_RESULT(ic)) &&
537                       (IS_ITEMP(IC_LEFT(ic)) || IS_ITEMP(IC_RIGHT(ic))))
538                     {
539                       if (OP_SYMBOL(IC_RESULT(ic))->key == key1
540                           && sym1->liveFrom == ic->seq
541                           && sym2->liveTo == ic->seq)
542                         {
543                           if (IS_SYMOP(IC_LEFT(ic)))
544                             if (OP_SYMBOL(IC_LEFT(ic))->key == key2)
545                               continue;
546                           if (IS_SYMOP(IC_RIGHT(ic)))
547                             if (OP_SYMBOL(IC_RIGHT(ic))->key == key2)
548                               continue;
549                         }
550
551                       if (OP_SYMBOL(IC_RESULT(ic))->key == key2
552                           && sym2->liveFrom == ic->seq
553                           && sym1->liveTo == ic->seq)
554                         {
555                           if (IS_SYMOP(IC_LEFT(ic)))
556                             if (OP_SYMBOL(IC_LEFT(ic))->key == key1)
557                               continue;
558                           if (IS_SYMOP(IC_RIGHT(ic)))
559                             if (OP_SYMBOL(IC_RIGHT(ic))->key == key1)
560                               continue;
561                         }
562                     }
563
564                   /* the iTemps do clash. set the bits in clashes */
565                   sym1->clashes = bitVectSetBit (sym1->clashes, key2);
566                   sym2->clashes = bitVectSetBit (sym2->clashes, key1);
567
568                   /* check if they share the same spill location */
569                   /* what is this good for? */
570                   if (SYM_SPIL_LOC(sym1) && SYM_SPIL_LOC(sym2) &&
571                       SYM_SPIL_LOC(sym1) == SYM_SPIL_LOC(sym2))
572                     {
573                       if (sym1->reqv && !sym2->reqv) SYM_SPIL_LOC(sym2)=NULL;
574                       else if (sym2->reqv && !sym1->reqv) SYM_SPIL_LOC(sym1)=NULL;
575                       else if (sym1->used > sym2->used) SYM_SPIL_LOC(sym2)=NULL;
576                       else SYM_SPIL_LOC(sym1)=NULL;
577                     }
578                 }
579             }
580         }
581     }
582 }
583
584 /*-----------------------------------------------------------------*/
585 /* allDefsOutOfRange - all definitions are out of a range          */
586 /*-----------------------------------------------------------------*/
587 bool
588 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
589 {
590   int i;
591
592   if (!defs)
593     return TRUE;
594
595   for (i = 0; i < defs->size; i++)
596     {
597       iCode *ic;
598
599       if (bitVectBitValue (defs, i) &&
600           (ic = hTabItemWithKey (iCodehTab, i)) &&
601           (ic->seq >= fseq && ic->seq <= toseq))
602
603         return FALSE;
604
605     }
606
607   return TRUE;
608 }
609
610 /*-----------------------------------------------------------------*/
611 /* notUsedInBlock - not used in this block                         */
612 /*-----------------------------------------------------------------*/
613 int
614 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode *ic)
615 {
616   return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
617           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq) &&
618           allDefsOutOfRange (sym->uses, ebp->fSeq, ebp->lSeq));
619 }
620
621 /*-----------------------------------------------------------------*/
622 /* adjustIChain - correct the sch and ech pointers                 */
623 /*-----------------------------------------------------------------*/
624 void
625 adjustIChain (eBBlock ** ebbs, int count)
626 {
627   int i;
628   
629   for (i = 0; i < count; i++)
630     {
631       iCode *ic;
632       
633       if (ebbs[i]->noPath)
634         continue;
635
636       ic = ebbs[i]->sch;
637
638       /* is there any code for this eBBlock? (e.g. ROM assignment) */
639       if(!ic)continue;
640
641       while (ic->prev)
642         ic = ic->prev;
643       ebbs[i]->sch = ic;
644       
645       ic = ebbs[i]->ech;
646       while (ic->next)
647         ic = ic->next;
648       ebbs[i]->ech = ic;
649     }
650 }
651
652 /*-----------------------------------------------------------------*/
653 /* computeLiveRanges - computes the live ranges for variables      */
654 /*-----------------------------------------------------------------*/
655 void
656 computeLiveRanges (eBBlock ** ebbs, int count)
657 {
658   /* first look through all blocks and adjust the
659      sch and ech pointers */
660   adjustIChain (ebbs, count);
661
662   /* sequence the code the live ranges are computed
663      in terms of this sequence additionally the
664      routine will also create a hashtable of instructions */
665   iCodeSeq = 0;
666   setToNull ((void *) &iCodehTab);
667   iCodehTab = newHashTable (iCodeKey);
668   hashiCodeKeys (ebbs, count);
669   setToNull ((void *) &iCodeSeqhTab);
670   iCodeSeqhTab = newHashTable (iCodeKey);
671   sequenceiCode (ebbs, count);
672
673   /* mark the ranges live for each point */
674   setToNull ((void *) &liveRanges);
675   rlivePoint (ebbs, count);
676
677   /* mark the from & to live ranges for variables used */
678   markLiveRanges (ebbs, count);
679
680   /* compute which overlaps with what */
681   computeClash(ebbs, count);
682 }
683
684 /*-----------------------------------------------------------------*/
685 /* recomputeLiveRanges - recomputes the live ranges for variables  */
686 /*-----------------------------------------------------------------*/
687 void
688 recomputeLiveRanges (eBBlock ** ebbs, int count)
689 {
690   symbol * sym;
691   int key;
692
693   /* clear all rlive bitVectors */
694   rliveClear (ebbs, count);
695
696   sym = hTabFirstItem (liveRanges, &key);
697   if (sym)
698     {
699       do {
700         sym->used = 0;
701         sym->liveFrom = 0;
702         sym->liveTo = 0;
703         freeBitVect (sym->clashes);
704         sym->clashes = NULL;
705       } while ( (sym = hTabNextItem (liveRanges, &key)));
706     }
707
708   /* do the LR computation again */
709   computeLiveRanges (ebbs, count);
710 }
711