1 /*-------------------------------------------------------------------------
3 SDCClrange.c - source file for live range computations
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
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
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.
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.
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 -------------------------------------------------------------------------*/
30 hTab *liveRanges = NULL;
31 hTab *iCodehTab = NULL;
32 hTab *iCodeSeqhTab = NULL;
34 /*-----------------------------------------------------------------*/
35 /* sequenceiCode - creates a sequence number for the iCode & add */
36 /*-----------------------------------------------------------------*/
38 sequenceiCode (eBBlock ** ebbs, int count)
42 for (i = 0; i < count; i++)
46 ebbs[i]->fSeq = iCodeSeq + 1;
47 for (ic = ebbs[i]->sch; ic; ic = ic->next)
50 ic->depth = ebbs[i]->depth;
51 hTabAddItem (&iCodehTab, ic->key, ic);
52 hTabAddItem (&iCodeSeqhTab, ic->seq, ic);
54 ebbs[i]->lSeq = iCodeSeq;
58 /*-----------------------------------------------------------------*/
59 /* markVisited - will set the visited flag for the given Block */
60 /*-----------------------------------------------------------------*/
61 DEFSETFUNC (markVisited)
68 applyToSet (ebp->succList, markVisited);
72 /*-----------------------------------------------------------------*/
73 /* isOpAlive - checks to see if the usage in this block is the */
74 /* uses the same definitions as this one */
75 /*-----------------------------------------------------------------*/
76 DEFSETFUNC (isOpAlive)
79 V_ARG (operand *, op);
80 V_ARG (eBBlock *, orig);
88 /* if we have reached the originating block */
89 /* or this block has some definiton for it */
90 /* then check if it is used between start & */
93 bitVectBitsInCommon (OP_DEFS (op), ebp->defSet))
94 if (usedBetweenPoints (op, ebp->sch, ic))
98 applyToSet (ebp->succList, markVisited);
102 /* choosing this more expensive one since
103 killDeadCode will take away some definitions
104 but there is not way right now to take away
105 the usage information for the corresponding
106 usages, this will lead to longer live ranges */
107 if (usedInRemaining (op, ebp->sch))
111 return (applyToSet (ebp->succList, isOpAlive, op, orig, ic));
114 /*-----------------------------------------------------------------*/
115 /* isLastUse - return TRUE if no usage of this operand after this */
116 /*-----------------------------------------------------------------*/
118 isLastUse (operand * op, eBBlock * ebp, iCode * ic,
119 eBBlock ** ebbs, int count)
123 /* if this is used in the remaining */
124 if (usedInRemaining (op, ic))
127 /* if not then check any of the following blocks use it */
128 for (i = 0; i < count; i++)
130 if (ebbs[i]->fSeq <= ebp->fSeq)
132 if (usedInRemaining (op, ebbs[i]->sch))
136 /* this is the last use */
140 /*-----------------------------------------------------------------*/
141 /* unionDefsUsed - unions the defsUsed in a block */
142 /*-----------------------------------------------------------------*/
143 DEFSETFUNC (unionDefsUsed)
146 V_ARG (bitVect **, bvp);
153 *bvp = bitVectUnion (*bvp, ebp->usesDefs);
154 applyToSet (ebp->succList, unionDefsUsed, bvp);
158 /*-----------------------------------------------------------------*/
159 /* setFromRange - sets the from range of a given operand */
160 /*-----------------------------------------------------------------*/
162 setFromRange (operand * op, int from)
164 /* only for compiler defined temporaries */
168 hTabAddItemIfNotP (&liveRanges, op->key, OP_SYMBOL (op));
171 OP_SYMBOL (op)->isptr = 1;
173 if (!OP_LIVEFROM (op) ||
174 OP_LIVEFROM (op) > from)
175 OP_LIVEFROM (op) = from;
178 /*-----------------------------------------------------------------*/
179 /* setToRange - set the range to for an operand */
180 /*-----------------------------------------------------------------*/
182 setToRange (operand * op, int to, bool check)
184 /* only for compiler defined temps */
188 OP_SYMBOL (op)->key = op->key;
189 hTabAddItemIfNotP (&liveRanges, op->key, OP_SYMBOL (op));
192 OP_SYMBOL (op)->isptr = 1;
202 /*-----------------------------------------------------------------*/
203 /* firstDeOf - finds the first definition in seq for op */
204 /*-----------------------------------------------------------------*/
206 firstDefOf (operand * op)
209 iCode *ric = NULL, *lic = NULL;
215 for (i = 0; i < OP_DEFS (op)->size; i++)
217 if (bitVectBitValue (OP_DEFS (op), i) &&
218 (lic = hTabItemWithKey (iCodehTab, i)) &&
228 /*-----------------------------------------------------------------*/
229 /* useDefLoopCheck - check for uses before init inside loops */
230 /*-----------------------------------------------------------------*/
232 useDefLoopCheck (operand * op, iCode * ic)
234 /* this is for situations like the following
244 in this case the definition of 'b' will flow to the usages
245 but register allocator cannot handle these situations.so
246 will mark as spilt */
252 /* get the first definition */
253 if (!(tic = firstDefOf (op)))
257 /* now go thru the usages & make sure they follow
258 the first definition */
259 for (i = 0; i <= OP_USES (op)->size; i++)
261 if (bitVectBitValue (OP_USES (op), i) &&
262 (tic = hTabItemWithKey (iCodehTab, i)) &&
270 /* found a usage without definition */
273 if (OP_SYMBOL (op)->isreqv && SPIL_LOC (op))
276 werror (W_LOCAL_NOINIT,
278 ic->filename, ic->lineno);
283 werror (W_LOCAL_NOINIT,
284 OP_SYMBOL (op)->name,
285 ic->filename, ic->lineno);
287 #if 0 // this will create a segfault: bug #498971
288 OP_SYMBOL (op)->isspilt = 1;
294 /*-----------------------------------------------------------------*/
295 /* operandLUse - check and set the last use for a given operand */
296 /*-----------------------------------------------------------------*/
298 operandLUse (operand * op, eBBlock ** ebbs,
299 int count, iCode * ic, eBBlock * ebp)
301 setFromRange (op, ic->seq);
303 OP_SYMBOL (op)->used += (((unsigned int) 1 << ic->depth) + 1);
305 OP_SYMBOL (op)->used += 1;
307 if (isLastUse (op, ebp, ic->next, ebbs, count) ||
308 (OP_LIVETO (op) && OP_LIVETO (op) < ic->seq))
310 int torange = ic->seq;
312 /* if this is a SEND then the toRange should be extended
317 for (lic = ic->next ; lic ; lic = lic->next)
319 if (lic->op == CALL || lic->op == PCALL)
322 /* found it : mark */
324 torange = lic->prev->seq;
326 /* if this is the last use then if this block belongs
327 to a loop & some definition comes into the loop
328 then extend the live range to the end of the loop */
333 aloop = setFirstItem (ebp->partOfLoop);
334 for (; aloop; aloop = setNextItem (ebp->partOfLoop))
336 if (hasIncomingDefs (aloop, op))
337 torange = findLoopEndSeq (aloop);
341 op = operandFromOperand (op);
342 setToRange (op, torange, FALSE);
344 ic->uses = bitVectSetBit (ic->uses, op->key);
346 if (!OP_SYMBOL (op)->udChked)
348 sym_link *type = operandType (op);
349 sym_link *etype = getSpec (type);
351 OP_SYMBOL (op)->udChked = 1;
352 /* good place to check if unintialised */
353 if ((IS_TRUE_SYMOP (op) || OP_SYMBOL (op)->isreqv) &&
354 OP_SYMBOL (op)->islocal &&
355 !IS_AGGREGATE (type) &&
357 ic->op != ADDRESS_OF &&
361 if (bitVectIsZero (op->usesDefs) && OP_SYMBOL(op)->ival==NULL)
363 OP_SYMBOL (op)->isspilt = 1;
365 if (OP_SYMBOL (op)->isreqv &&
366 !OP_SYMBOL (op)->_isparm && SPIL_LOC (op))
369 werror (W_LOCAL_NOINIT,
371 ic->filename, ic->lineno);
376 werror (W_LOCAL_NOINIT,
377 OP_SYMBOL (op)->name,
378 ic->filename, ic->lineno);
383 if (ebp->depth && op->usesDefs &&
384 !OP_SYMBOL (op)->_isparm)
386 /* check non-inits inside loops */
387 useDefLoopCheck (op, ic);
395 /*-----------------------------------------------------------------*/
396 /* killAllAlive - mark all the definitions living with this seq */
397 /*-----------------------------------------------------------------*/
399 killAllAlive (int seq)
404 for (sym = hTabFirstItem (liveRanges, &k); sym;
405 sym = hTabNextItem (liveRanges, &k))
406 if (!sym->liveTo || (sym->liveTo < sym->liveFrom))
409 /*-----------------------------------------------------------------*/
410 /* defUsedAfterLoop - all definitions & usages before sequence num */
411 /*-----------------------------------------------------------------*/
413 defUsedAfterLoop (operand * op, int seq)
418 /* check for the usages first */
419 if (OP_SYMBOL (op)->uses && !bitVectIsZero (OP_SYMBOL (op)->uses))
421 for (i = 0; i < OP_SYMBOL (op)->uses->size; i++)
424 if (bitVectBitValue (OP_SYMBOL (op)->uses, i) && /* usage found */
425 (ic = hTabItemWithKey (iCodehTab, i)) && /* "" */
426 ic->seq > seq) /* & is after the seq */
434 /*-----------------------------------------------------------------*/
435 /* markLiveRanges - for each operand mark the liveFrom & liveTo */
436 /*-----------------------------------------------------------------*/
438 markLiveRanges (eBBlock * ebp, eBBlock ** ebbs, int count)
441 bitVect *defsUsed = NULL;
442 bitVect *defsNotUsed = NULL;
445 /* for all the instructions */
446 for (ic = ebp->sch; ic; ic = ic->next)
448 if (ic->op == CALL || ic->op == PCALL)
450 setFromRange (IC_RESULT (ic), ic->seq);
451 /* if the result has no usage then
452 mark this as the end of its life too
453 and take it away from the defs for the block */
454 if (bitVectIsZero (OP_SYMBOL (IC_RESULT (ic))->uses))
456 setToRange (IC_RESULT (ic), ic->seq, FALSE);
457 bitVectUnSetBit (ebp->defSet, ic->key);
464 /* take care of the special icodes first */
465 if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
467 operandLUse (IC_JTCOND (ic), ebbs, count, ic, ebp);
471 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
473 operandLUse (IC_COND (ic), ebbs, count, ic, ebp);
477 if (IS_SYMOP (IC_LEFT (ic)))
478 operandLUse (IC_LEFT (ic), ebbs, count, ic, ebp);
480 if (IS_SYMOP (IC_RIGHT (ic)))
481 operandLUse (IC_RIGHT (ic), ebbs, count, ic, ebp);
483 if (POINTER_SET (ic))
484 operandLUse (IC_RESULT (ic), ebbs, count, ic, ebp);
485 else if (IC_RESULT (ic))
486 ic->defKey = IC_RESULT (ic)->key;
490 /* for all the definitions in the block */
491 /* compute and set the live from */
492 if (ebp->ldefs && !bitVectIsZero (ebp->ldefs))
494 for (i = 0; i < ebp->ldefs->size; i++)
498 if (bitVectBitValue (ebp->ldefs, i) &&
499 (dic = hTabItemWithKey (iCodehTab, i)))
502 /* if the definition has a from & it is greater */
503 /* than the defininng iCode->seq then change it */
504 setFromRange (IC_RESULT (dic), dic->seq);
509 /* special case for lastBlock in a loop: here we
510 mark the end of all the induction variables for the
512 if (ebp->isLastInLoop && !bitVectIsZero (ebp->linds))
514 for (i = 0; i <= ebp->linds->size; i++)
518 if (bitVectBitValue (ebp->linds, i) &&
519 (dic = hTabItemWithKey (iCodehTab, i)))
522 /* if this is a register equvalent make sure
523 it is not defined or used anywhere after the loop */
524 if (OP_SYMBOL (IC_RESULT (dic))->isreqv &&
525 defUsedAfterLoop (IC_RESULT (dic), ebp->lSeq))
528 setToRange (IC_RESULT (dic), (ebp->lSeq), FALSE);
533 /* for defnitions coming into the block if they */
534 /* not used by itself & any of its successors */
536 /* first union the definitions used in all successors
538 for (i = 0; i < count; ebbs[i++]->visited = 0);
539 applyToSet (ebp->succList, unionDefsUsed, &defsUsed);
540 defsUsed = bitVectUnion (defsUsed, ebp->usesDefs);
542 /* now subract the result of these unions from */
543 /* the incoming definitions this will give the */
544 /* definitions that are never used in the future */
545 defsNotUsed = bitVectCplAnd (bitVectCopy (ebp->inDefs),
548 /* mark the end of the defintions */
549 if (!bitVectIsZero (defsNotUsed) && ebp->sch)
551 for (i = 0; i < defsNotUsed->size; i++)
555 if (bitVectBitValue (defsNotUsed, i) &&
556 (dic = hTabItemWithKey (iCodehTab, i)))
558 setToRange (IC_RESULT (dic), (ebp->fSeq - 1), TRUE);
564 /* if we reach a lock with noPath to it then kill all
565 the live ranges alive at this point */
566 /* if (ebp->noPath || ebp->entryLabel == returnLabel) */
567 if (ebp->entryLabel == returnLabel)
568 killAllAlive (ebp->fSeq);
571 /*-----------------------------------------------------------------*/
572 /* rlivePoint - for each point compute the ranges that are alive */
573 /*-----------------------------------------------------------------*/
575 rlivePoint (eBBlock ** ebbs, int count)
579 /* for all blocks do */
580 for (i = 0; i < count; i++) {
583 /* for all instruction in the block do */
584 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
588 ic->rlive = newBitVect (operandKey);
589 /* for all symbols in the liverange table */
590 for (lrange = hTabFirstItem (liveRanges, &k); lrange;
591 lrange = hTabNextItem (liveRanges, &k)) {
593 /* if it is live then add the lrange to ic->rlive */
594 if (lrange->liveFrom <= ic->seq &&
595 lrange->liveTo >= ic->seq) {
596 lrange->isLiveFcall |= ((lrange->liveFrom < ic->seq) &&
597 (ic->op == CALL || ic->op == PCALL || ic->op == SEND));
598 if (ic->op == CALL && lrange->liveFrom == ic->seq) continue;
599 ic->rlive = bitVectSetBit (ic->rlive, lrange->key);
603 /* overlapping live ranges should be eliminated */
604 if (ASSIGN_ITEMP_TO_ITEMP (ic)) {
605 if (SPIL_LOC(IC_RIGHT(ic)) == SPIL_LOC(IC_RESULT(ic)) && /* left & right share the same spil location */
606 OP_SYMBOL(IC_RESULT(ic))->isreqv && /* left of assign is a register requivalent */
607 !OP_SYMBOL(IC_RIGHT(ic))->isreqv && /* right side is not */
608 OP_SYMBOL(IC_RIGHT(ic))->liveTo > ic->key && /* right side live beyond this point */
609 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1 ) { /* left has multiple definitions */
610 SPIL_LOC(IC_RIGHT(ic)) = NULL; /* then cannot share */
618 /*-----------------------------------------------------------------*/
619 /* computeClash - find out which live ranges collide with others */
620 /*-----------------------------------------------------------------*/
621 static void computeClash ()
623 hTab *lRangeCopy = newHashTable(hTabMaxKey(liveRanges));
625 symbol *outer, *inner;
628 /* have to make a copy of the liveRanges hashTable :: UGHH .*/
629 for (item = hTabFirstItem(liveRanges,&key); item ;
630 item = hTabNextItem(liveRanges,&key)) {
631 hTabAddItem(&lRangeCopy,key,item);
634 /* outerloop : for each liverange do */
635 for (outer = hTabFirstItem(liveRanges,&key); outer ;
636 outer = hTabNextItem(liveRanges,&key)) {
638 /* if the liveFrom & To are the same then skip,
639 could happen for unused return values from functions */
640 if (outer->liveFrom == outer->liveTo) continue;
642 /* innerloop : for the inner loop we can start from the
643 item after the outer loop */
644 inner = hTabFirstItemWK (lRangeCopy,outer->key);
645 inner = hTabNextItem (lRangeCopy,&key1 );
646 for (; inner ; inner = hTabNextItem( lRangeCopy ,&key1)) {
648 if (inner->liveFrom == inner->liveTo) continue;
649 if (inner->liveTo < outer->liveFrom) continue;
650 if (inner->liveFrom > outer->liveTo) continue;
652 /* if one of them are being defined where the other
653 one end , then no overlap (i.e. they can goto same registers */
654 if (inner->liveFrom == outer->liveTo ||
655 outer->liveFrom == inner->liveTo) continue;
657 /* so they overlap : set both their clashes */
658 inner->clashes = bitVectSetBit(inner->clashes,outer->key);
659 outer->clashes = bitVectSetBit(outer->clashes,inner->key);
661 /* check if they share the same spillocation */
662 if (SYM_SPIL_LOC(inner) && SYM_SPIL_LOC(outer) &&
663 SYM_SPIL_LOC(inner) == SYM_SPIL_LOC(outer)) {
664 if (inner->reqv && !outer->reqv) SYM_SPIL_LOC(outer)=NULL;
665 else if (outer->reqv && !inner->reqv) SYM_SPIL_LOC(inner)=NULL;
666 else if (inner->used > outer->used) SYM_SPIL_LOC(outer)=NULL;
667 else SYM_SPIL_LOC(inner)=NULL;
674 /*-----------------------------------------------------------------*/
675 /* allDefsOutOfRange - all definitions are out of a range */
676 /*-----------------------------------------------------------------*/
678 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
685 for (i = 0; i < defs->size; i++)
689 if (bitVectBitValue (defs, i) &&
690 (ic = hTabItemWithKey (iCodehTab, i)) &&
691 (ic->seq >= fseq && ic->seq <= toseq))
700 /*-----------------------------------------------------------------*/
701 /* notUsedInBlock - not used in this block */
702 /*-----------------------------------------------------------------*/
704 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode *ic)
706 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
707 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq) &&
708 allDefsOutOfRange (sym->uses, ebp->fSeq, ebp->lSeq));
712 /*-----------------------------------------------------------------*/
713 /* computeLiveRanges - computes the live ranges for variables */
714 /*-----------------------------------------------------------------*/
716 computeLiveRanges (eBBlock ** ebbs, int count)
719 /* sequence the code the live ranges are computed
720 in terms of this sequence additionally the
721 routine will also create a hashtable of instructions */
723 setToNull ((void **) &iCodehTab);
724 iCodehTab = newHashTable (iCodeKey);
725 setToNull ((void **) &iCodeSeqhTab);
726 iCodeSeqhTab = newHashTable (iCodeKey);
727 sequenceiCode (ebbs, count);
729 /* add blocks between loop blocks as part of that loop */
730 addLoopBlocks (ebbs, count);
732 /* call routine to mark the from & to live ranges for
734 setToNull ((void **) &liveRanges);
735 for (i = 0; i < count; i++)
736 markLiveRanges (ebbs[i], ebbs, count);
738 /* mark the ranges live for each point */
739 rlivePoint (ebbs, count);
741 /* compute which overlaps with what */