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 successor blocks use it */
128 for (i = 0; i < count; ebbs[i++]->visited = 0);
129 if (applyToSet (ebp->succList, isOpAlive, op, ebp, ic))
132 /* this is the last use */
136 /*-----------------------------------------------------------------*/
137 /* unionDefsUsed - unions the defsUsed in a block */
138 /*-----------------------------------------------------------------*/
139 DEFSETFUNC (unionDefsUsed)
142 V_ARG (bitVect **, bvp);
149 *bvp = bitVectUnion (*bvp, ebp->usesDefs);
150 applyToSet (ebp->succList, unionDefsUsed, bvp);
154 /*-----------------------------------------------------------------*/
155 /* setFromRange - sets the from range of a given operand */
156 /*-----------------------------------------------------------------*/
158 setFromRange (operand * op, int from)
160 /* only for compiler defined temporaries */
164 hTabAddItemIfNotP (&liveRanges, op->key, OP_SYMBOL (op));
167 OP_SYMBOL (op)->isptr = 1;
169 if (!OP_LIVEFROM (op) ||
170 OP_LIVEFROM (op) > from)
171 OP_LIVEFROM (op) = from;
174 /*-----------------------------------------------------------------*/
175 /* setToRange - set the range to for an operand */
176 /*-----------------------------------------------------------------*/
178 setToRange (operand * op, int to, bool check, int fromLevel)
180 /* only for compiler defined temps */
184 if (fromLevel > OP_SYMBOL(op)->level) {
185 // this is from an inner block and thus has no authority
189 OP_SYMBOL (op)->key = op->key;
190 hTabAddItemIfNotP (&liveRanges, op->key, OP_SYMBOL (op));
193 OP_SYMBOL (op)->isptr = 1;
203 /*-----------------------------------------------------------------*/
204 /* firstDeOf - finds the first definition in seq for op */
205 /*-----------------------------------------------------------------*/
207 firstDefOf (operand * op)
210 iCode *ric = NULL, *lic = NULL;
216 for (i = 0; i < OP_DEFS (op)->size; i++)
218 if (bitVectBitValue (OP_DEFS (op), i) &&
219 (lic = hTabItemWithKey (iCodehTab, i)) &&
229 /*-----------------------------------------------------------------*/
230 /* useDefLoopCheck - check for uses before init inside loops */
231 /*-----------------------------------------------------------------*/
233 useDefLoopCheck (operand * op, iCode * ic)
235 /* this is for situations like the following
245 in this case the definition of 'b' will flow to the usages
246 but register allocator cannot handle these situations.so
247 will mark as spilt */
253 /* get the first definition */
254 if (!(tic = firstDefOf (op)))
258 /* now go thru the usages & make sure they follow
259 the first definition */
260 for (i = 0; i <= OP_USES (op)->size; i++)
262 if (bitVectBitValue (OP_USES (op), i) &&
263 (tic = hTabItemWithKey (iCodehTab, i)) &&
271 /* found a usage without definition */
274 if (OP_SYMBOL (op)->isreqv && SPIL_LOC (op))
277 werror (W_LOCAL_NOINIT,
279 ic->filename, ic->lineno);
284 werror (W_LOCAL_NOINIT,
285 OP_SYMBOL (op)->name,
286 ic->filename, ic->lineno);
288 #if 0 // this will create a segfault: bug #498971
289 OP_SYMBOL (op)->isspilt = 1;
295 /*-----------------------------------------------------------------*/
296 /* operandLUse - check and set the last use for a given operand */
297 /*-----------------------------------------------------------------*/
299 operandLUse (operand * op, eBBlock ** ebbs,
300 int count, iCode * ic, eBBlock * ebp)
302 setFromRange (op, ic->seq);
304 OP_SYMBOL (op)->used += (((unsigned int) 1 << ic->depth) + 1);
306 OP_SYMBOL (op)->used += 1;
308 if (isLastUse (op, ebp, ic->next, ebbs, count) ||
309 (OP_LIVETO (op) && OP_LIVETO (op) < ic->seq))
311 int torange = ic->seq;
313 /* if this is a SEND then the toRange should be extended
315 if (ic->op == SEND) {
317 for (lic = ic->next ; lic ; lic = lic->next) {
318 if (lic->op == CALL || lic->op == PCALL) break;
320 /* found it : mark */
321 if (lic) torange = lic->prev->seq;
324 op = operandFromOperand (op);
325 setToRange (op, torange, FALSE, ebp->entryLabel->level);
327 ic->uses = bitVectSetBit (ic->uses, op->key);
329 if (!OP_SYMBOL (op)->udChked)
331 sym_link *type = operandType (op);
332 sym_link *etype = getSpec (type);
334 OP_SYMBOL (op)->udChked = 1;
335 /* good place to check if unintialised */
336 if ((IS_TRUE_SYMOP (op) || OP_SYMBOL (op)->isreqv) &&
337 OP_SYMBOL (op)->islocal &&
338 !IS_AGGREGATE (type) &&
340 ic->op != ADDRESS_OF &&
344 if (bitVectIsZero (op->usesDefs) && OP_SYMBOL(op)->ival==NULL)
346 OP_SYMBOL (op)->isspilt = 1;
348 if (OP_SYMBOL (op)->isreqv &&
349 !OP_SYMBOL (op)->_isparm && SPIL_LOC (op))
352 werror (W_LOCAL_NOINIT,
354 ic->filename, ic->lineno);
359 werror (W_LOCAL_NOINIT,
360 OP_SYMBOL (op)->name,
361 ic->filename, ic->lineno);
366 if (ebp->depth && op->usesDefs &&
367 !OP_SYMBOL (op)->_isparm)
369 /* check non-inits inside loops */
370 useDefLoopCheck (op, ic);
378 /*-----------------------------------------------------------------*/
379 /* killAllAlive - mark all the definitions living with this seq */
380 /*-----------------------------------------------------------------*/
382 killAllAlive (int seq)
387 for (sym = hTabFirstItem (liveRanges, &k); sym;
388 sym = hTabNextItem (liveRanges, &k))
389 if (!sym->liveTo || (sym->liveTo < sym->liveFrom))
392 /*-----------------------------------------------------------------*/
393 /* defUsedAfterLoop - all definitions & usages before sequence num */
394 /*-----------------------------------------------------------------*/
396 defUsedAfterLoop (operand * op, int seq)
401 /* check for the usages first */
402 if (OP_SYMBOL (op)->uses && !bitVectIsZero (OP_SYMBOL (op)->uses))
404 for (i = 0; i < OP_SYMBOL (op)->uses->size; i++)
407 if (bitVectBitValue (OP_SYMBOL (op)->uses, i) && /* usage found */
408 (ic = hTabItemWithKey (iCodehTab, i)) && /* "" */
409 ic->seq > seq) /* & is after the seq */
417 /*-----------------------------------------------------------------*/
418 /* markLiveRanges - for each operand mark the liveFrom & liveTo */
419 /*-----------------------------------------------------------------*/
421 markLiveRanges (eBBlock * ebp, eBBlock ** ebbs, int count)
424 bitVect *defsUsed = NULL;
425 bitVect *defsNotUsed = NULL;
428 /* for all the instructions */
429 for (ic = ebp->sch; ic; ic = ic->next)
431 if (ic->op == CALL || ic->op == PCALL)
433 setFromRange (IC_RESULT (ic), ic->seq);
434 /* if the result has no usage then
435 mark this as the end of its life too
436 and take it away from the defs for the block */
437 if (bitVectIsZero (OP_SYMBOL (IC_RESULT (ic))->uses))
439 setToRange (IC_RESULT (ic), ic->seq, FALSE,
440 ebp->entryLabel->level);
441 bitVectUnSetBit (ebp->defSet, ic->key);
448 /* take care of the special icodes first */
449 if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
451 operandLUse (IC_JTCOND (ic), ebbs, count, ic, ebp);
455 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
457 operandLUse (IC_COND (ic), ebbs, count, ic, ebp);
461 if (IS_SYMOP (IC_LEFT (ic)))
462 operandLUse (IC_LEFT (ic), ebbs, count, ic, ebp);
464 if (IS_SYMOP (IC_RIGHT (ic)))
465 operandLUse (IC_RIGHT (ic), ebbs, count, ic, ebp);
467 if (POINTER_SET (ic))
468 operandLUse (IC_RESULT (ic), ebbs, count, ic, ebp);
469 else if (IC_RESULT (ic))
470 ic->defKey = IC_RESULT (ic)->key;
474 /* for all the definitions in the block */
475 /* compute and set the live from */
476 if (ebp->ldefs && !bitVectIsZero (ebp->ldefs))
478 for (i = 0; i < ebp->ldefs->size; i++)
482 if (bitVectBitValue (ebp->ldefs, i) &&
483 (dic = hTabItemWithKey (iCodehTab, i)))
486 /* if the definition has a from & it is greater */
487 /* than the defininng iCode->seq then change it */
488 setFromRange (IC_RESULT (dic), dic->seq);
493 /* special case for lastBlock in a loop: here we
494 mark the end of all the induction variables for the
496 if (ebp->isLastInLoop && !bitVectIsZero (ebp->linds))
498 for (i = 0; i <= ebp->linds->size; i++)
502 if (bitVectBitValue (ebp->linds, i) &&
503 (dic = hTabItemWithKey (iCodehTab, i)))
506 /* if this is a register equvalent make sure
507 it is not defined or used anywhere after the loop */
508 if (OP_SYMBOL (IC_RESULT (dic))->isreqv &&
509 defUsedAfterLoop (IC_RESULT (dic), ebp->lSeq))
512 setToRange (IC_RESULT (dic), (ebp->lSeq), FALSE,
513 ebp->entryLabel->level);
518 /* for defnitions coming into the block if they */
519 /* not used by itself & any of its successors */
521 /* first union the definitions used in all successors
523 for (i = 0; i < count; ebbs[i++]->visited = 0);
524 applyToSet (ebp->succList, unionDefsUsed, &defsUsed);
525 defsUsed = bitVectUnion (defsUsed, ebp->usesDefs);
527 /* now subract the result of these unions from */
528 /* the incoming definitions this will give the */
529 /* definitions that are never used in the future */
530 defsNotUsed = bitVectCplAnd (bitVectCopy (ebp->inDefs),
533 /* mark the end of the defintions */
534 if (!bitVectIsZero (defsNotUsed) && ebp->sch)
536 for (i = 0; i < defsNotUsed->size; i++)
540 if (bitVectBitValue (defsNotUsed, i) &&
541 (dic = hTabItemWithKey (iCodehTab, i)))
543 setToRange (IC_RESULT (dic), (ebp->fSeq - 1), TRUE,
544 ebp->entryLabel->level);
550 /* if we reach a lock with noPath to it then kill all
551 the live ranges alive at this point */
552 /* if (ebp->noPath || ebp->entryLabel == returnLabel) */
553 if (ebp->entryLabel == returnLabel)
554 killAllAlive (ebp->fSeq);
557 /*-----------------------------------------------------------------*/
558 /* rlivePoint - for each point compute the ranges that are alive */
559 /*-----------------------------------------------------------------*/
561 rlivePoint (eBBlock ** ebbs, int count)
565 /* for all blocks do */
566 for (i = 0; i < count; i++) {
569 /* for all instruction in the block do */
570 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
574 ic->rlive = newBitVect (operandKey);
575 /* for all symbols in the liverange table */
576 for (lrange = hTabFirstItem (liveRanges, &k); lrange;
577 lrange = hTabNextItem (liveRanges, &k)) {
579 /* if it is live then add the lrange to ic->rlive */
580 if (lrange->liveFrom <= ic->seq &&
581 lrange->liveTo >= ic->seq) {
582 lrange->isLiveFcall |= ((lrange->liveFrom < ic->seq) &&
583 (ic->op == CALL || ic->op == PCALL || ic->op == SEND));
584 if (ic->op == CALL && lrange->liveFrom == ic->seq) continue;
585 ic->rlive = bitVectSetBit (ic->rlive, lrange->key);
589 /* overlapping live ranges should be eliminated */
590 if (ASSIGN_ITEMP_TO_ITEMP (ic)) {
591 if (SPIL_LOC(IC_RIGHT(ic)) == SPIL_LOC(IC_RESULT(ic)) && /* left & right share the same spil location */
592 OP_SYMBOL(IC_RESULT(ic))->isreqv && /* left of assign is a register requivalent */
593 !OP_SYMBOL(IC_RIGHT(ic))->isreqv && /* right side is not */
594 OP_SYMBOL(IC_RIGHT(ic))->liveTo > ic->key && /* right side live beyond this point */
595 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1 ) { /* left has multiple definitions */
596 SPIL_LOC(IC_RIGHT(ic)) = NULL; /* then cannot share */
604 /*-----------------------------------------------------------------*/
605 /* computeClash - find out which live ranges collide with others */
606 /*-----------------------------------------------------------------*/
607 static void computeClash ()
609 hTab *lRangeCopy = newHashTable(hTabMaxKey(liveRanges));
611 symbol *outer, *inner;
614 /* have to make a copy of the liveRanges hashTable :: UGHH .*/
615 for (item = hTabFirstItem(liveRanges,&key); item ;
616 item = hTabNextItem(liveRanges,&key)) {
617 hTabAddItem(&lRangeCopy,key,item);
620 /* outerloop : for each liverange do */
621 for (outer = hTabFirstItem(liveRanges,&key); outer ;
622 outer = hTabNextItem(liveRanges,&key)) {
624 /* if the liveFrom & To are the same then skip,
625 could happen for unused return values from functions */
626 if (outer->liveFrom == outer->liveTo) continue;
628 /* innerloop : for the inner loop we can start from the
629 item after the outer loop */
630 inner = hTabFirstItemWK (lRangeCopy,outer->key);
631 inner = hTabNextItem (lRangeCopy,&key1 );
632 for (; inner ; inner = hTabNextItem( lRangeCopy ,&key1)) {
634 if (inner->liveFrom == inner->liveTo) continue;
635 if (inner->liveTo < outer->liveFrom) continue;
636 if (inner->liveFrom > outer->liveTo) continue;
638 /* if one of them are being defined where the other
639 one end , then no overlap (i.e. they can goto same registers */
640 if (inner->liveFrom == outer->liveTo ||
641 outer->liveFrom == inner->liveTo) continue;
643 /* so they overlap : set both their clashes */
644 inner->clashes = bitVectSetBit(inner->clashes,outer->key);
645 outer->clashes = bitVectSetBit(outer->clashes,inner->key);
647 /* check if they share the same spillocation */
648 if (SYM_SPIL_LOC(inner) && SYM_SPIL_LOC(outer) &&
649 SYM_SPIL_LOC(inner) == SYM_SPIL_LOC(outer)) {
650 if (inner->reqv && !outer->reqv) SYM_SPIL_LOC(outer)=NULL;
651 else if (outer->reqv && !inner->reqv) SYM_SPIL_LOC(inner)=NULL;
652 else if (inner->used > outer->used) SYM_SPIL_LOC(outer)=NULL;
653 else SYM_SPIL_LOC(inner)=NULL;
660 /*-----------------------------------------------------------------*/
661 /* allDefsOutOfRange - all definitions are out of a range */
662 /*-----------------------------------------------------------------*/
664 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
671 for (i = 0; i < defs->size; i++)
675 if (bitVectBitValue (defs, i) &&
676 (ic = hTabItemWithKey (iCodehTab, i)) &&
677 (ic->seq >= fseq && ic->seq <= toseq))
686 /*-----------------------------------------------------------------*/
687 /* notUsedInBlock - not used in this block */
688 /*-----------------------------------------------------------------*/
690 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode *ic)
692 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
693 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq) &&
694 allDefsOutOfRange (sym->uses, ebp->fSeq, ebp->lSeq));
698 /*-----------------------------------------------------------------*/
699 /* computeLiveRanges - computes the live ranges for variables */
700 /*-----------------------------------------------------------------*/
702 computeLiveRanges (eBBlock ** ebbs, int count)
705 /* sequence the code the live ranges are computed
706 in terms of this sequence additionally the
707 routine will also create a hashtable of instructions */
709 setToNull ((void **) &iCodehTab);
710 iCodehTab = newHashTable (iCodeKey);
711 setToNull ((void **) &iCodeSeqhTab);
712 iCodeSeqhTab = newHashTable (iCodeKey);
713 sequenceiCode (ebbs, count);
715 /* call routine to mark the from & to live ranges for
717 setToNull ((void **) &liveRanges);
718 for (i = 0; i < count; i++)
719 markLiveRanges (ebbs[i], ebbs, count);
721 /* mark the ranges live for each point */
722 rlivePoint (ebbs, count);
724 /* compute which overlaps with what */