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 -------------------------------------------------------------------------*/
28 #include "SDCCglobl.h"
32 #include "SDCChasht.h"
35 #include "SDCCicode.h"
36 #include "SDCClabel.h"
37 #include "SDCCBBlock.h"
40 #include "SDCCcflow.h"
41 #include "SDCCdflow.h"
42 #include "SDCClrange.h"
45 hTab *liveRanges = NULL;
46 hTab *iCodehTab = NULL;
48 /*-----------------------------------------------------------------*/
49 /* sequenceiCode - creates a sequence number for the iCode & add */
50 /*-----------------------------------------------------------------*/
51 void sequenceiCode (eBBlock **ebbs, int count)
55 for (i = 0 ; i < count ; i++ ) {
58 ebbs[i]->fSeq = iCodeSeq + 1;
59 for ( ic = ebbs[i]->sch ; ic ; ic= ic->next) {
61 ic->depth = ebbs[i]->depth;
62 hTabAddItem(&iCodehTab,ic->key,ic);
64 ebbs[i]->lSeq = iCodeSeq ;
68 /*-----------------------------------------------------------------*/
69 /* markVisited - will set the visited flag for the given Block */
70 /*-----------------------------------------------------------------*/
71 DEFSETFUNC(markVisited)
78 applyToSet(ebp->succList,markVisited);
82 /*-----------------------------------------------------------------*/
83 /* isOpAlive - checks to see if the usage in this block is the */
84 /* uses the same definitions as this one */
85 /*-----------------------------------------------------------------*/
90 V_ARG(eBBlock *,orig);
98 /* if we have reached the originating block */
99 /* or this block has some definiton for it */
100 /* then check if it is used between start & */
103 bitVectBitsInCommon(OP_DEFS(op),ebp->defSet))
104 if (usedBetweenPoints (op,ebp->sch,ic))
107 applyToSet(ebp->succList,markVisited);
111 /* choosing this more expensive one since
112 killDeadCode will take away some definitions
113 but there is not way right now to take away
114 the usage information for the corresponding
115 usages, this will lead to longer live ranges */
116 if (usedInRemaining(op,ebp->sch))
120 return (applyToSet(ebp->succList,isOpAlive,op,orig,ic));
123 /*-----------------------------------------------------------------*/
124 /* isLastUse - return TRUE if no usage of this operand after this */
125 /*-----------------------------------------------------------------*/
126 int isLastUse (operand *op, eBBlock *ebp, iCode *ic,
127 eBBlock **ebbs, int count)
131 /* if this is used in the remaining */
132 if (usedInRemaining(op,ic))
135 /* if not then check any of the successor blocks use it */
136 for (i = 0 ; i < count ; ebbs[i++]->visited = 0);
137 if (applyToSet(ebp->succList,isOpAlive,op,ebp,ic))
140 /* this is the last use */
144 /*-----------------------------------------------------------------*/
145 /* unionDefsUsed - unions the defsUsed in a block */
146 /*-----------------------------------------------------------------*/
147 DEFSETFUNC(unionDefsUsed)
150 V_ARG(bitVect **,bvp);
157 *bvp = bitVectUnion(*bvp, ebp->usesDefs);
158 applyToSet(ebp->succList,unionDefsUsed,bvp);
162 /*-----------------------------------------------------------------*/
163 /* setFromRange - sets the from range of a given operand */
164 /*-----------------------------------------------------------------*/
165 void setFromRange (operand *op, int from)
167 /* only for compiler defined temporaries */
171 hTabAddItemIfNotP (&liveRanges,op->key,OP_SYMBOL(op));
174 OP_SYMBOL(op)->isptr = 1;
176 if (!OP_LIVEFROM(op) ||
177 OP_LIVEFROM(op) > from)
178 OP_LIVEFROM(op) = from;
181 /*-----------------------------------------------------------------*/
182 /* setToRange - set the range to for an operand */
183 /*-----------------------------------------------------------------*/
184 void setToRange (operand *op,int to, bool check)
186 /* only for compiler defined temps */
190 OP_SYMBOL(op)->key = op->key;
191 hTabAddItemIfNotP (&liveRanges,op->key,OP_SYMBOL(op));
194 OP_SYMBOL(op)->isptr = 1;
205 /*-----------------------------------------------------------------*/
206 /* operandLUse - check and set the last use for a given operand */
207 /*-----------------------------------------------------------------*/
208 operand *operandLUse (operand *op, eBBlock **ebbs,
209 int count, iCode *ic, eBBlock *ebp)
211 setFromRange(op,ic->seq);
213 OP_SYMBOL(op)->used += (((unsigned int)1 << ic->depth) + 1);
215 OP_SYMBOL(op)->used += 1;
217 if (isLastUse(op,ebp,ic->next,ebbs,count) ||
218 (OP_LIVETO(op) && OP_LIVETO(op) < ic->seq)) {
219 int torange = ic->seq;
220 /* if this is the last use then if this block belongs
221 to a loop & some definition comes into the loop
222 then extend the live range to the end of the loop */
223 if (ebp->partOfLoop &&
224 hasIncomingDefs(ebp->partOfLoop,op)) {
225 torange = findLoopEndSeq(ebp->partOfLoop);
227 op = operandFromOperand(op);
228 setToRange(op,torange,FALSE);
230 ic->uses = bitVectSetBit(ic->uses,op->key);
233 link *type = operandType(op);
234 link *etype = getSpec(type);
236 /* good place to check if unintialised */
237 if ((IS_TRUE_SYMOP(op) || OP_SYMBOL(op)->isreqv) &&
238 OP_SYMBOL(op)->islocal &&
239 !IS_AGGREGATE(type) &&
241 ic->op != ADDRESS_OF &&
242 !IS_STATIC(etype) ) {
244 /* if (OP_SYMBOL(op)->isreqv && !OP_SYMBOL(op)->_isparm){ */
246 /* if (SPIL_LOC(op) && */
247 /* bitVectIsZero(SPIL_LOC(op)->defs)) { */
248 /* OP_SYMBOL(op)->isspilt = 1; */
249 /* werror(W_LOCAL_NOINIT, */
250 /* SPIL_LOC(op)->name, */
251 /* ic->filename,ic->lineno); */
255 if (bitVectIsZero(op->usesDefs)) {
256 OP_SYMBOL(op)->isspilt = 1;
257 werror(W_LOCAL_NOINIT,
259 ic->filename,ic->lineno);
267 /*-----------------------------------------------------------------*/
268 /* compLastUse - computes the last usage with certainty */
269 /*-----------------------------------------------------------------*/
274 /* the strategy here is to look for live ranges that are not
275 induction variables, and find out the usage icode with the
276 highest sequence number then set the to range to that icode
278 /* once fully tested we can use only this routine to mark the
279 to range that will reduce a lot of computations in the
280 markLiveRanges routine */
281 for (sym = hTabFirstItem(liveRanges,&key) ; sym;
282 sym = hTabNextItem(liveRanges,&key)) {
293 /* go thru all the usages of this live range and find out
294 the maximum sequence number of the icode that used it */
295 for (i = 0 ; i < sym->uses->size ; i++ ) {
298 if (!bitVectBitValue(sym->uses,i))
301 if ((uic = hTabItemWithKey(iCodehTab,i)))
302 maxKey = ( uic->seq > maxKey ? uic->seq : maxKey );
307 sym->liveTo = maxKey;
311 /*-----------------------------------------------------------------*/
312 /* killAllAlive - mark all the definitions living with this seq */
313 /*-----------------------------------------------------------------*/
314 void killAllAlive (int seq)
319 for (sym = hTabFirstItem(liveRanges,&k); sym;
320 sym = hTabNextItem(liveRanges,&k))
321 if (!sym->liveTo || (sym->liveTo < sym->liveFrom))
324 /*-----------------------------------------------------------------*/
325 /* defUsedAfterLoop - all definitions & usages before sequence num */
326 /*-----------------------------------------------------------------*/
327 bool defUsedAfterLoop (operand *op, int seq)
332 /* check for the usages first */
333 if (OP_SYMBOL(op)->uses && !bitVectIsZero(OP_SYMBOL(op)->uses)) {
334 for (i = 0 ; i < OP_SYMBOL(op)->uses->size ; i++ ) {
336 if (bitVectBitValue( OP_SYMBOL(op)->uses,i) && /* usage found */
337 (ic = hTabItemWithKey(iCodehTab,i)) && /* "" */
338 ic->seq > seq ) /* & is after the seq */
346 /*-----------------------------------------------------------------*/
347 /* markLiveRanges - for each operand mark the liveFrom & liveTo */
348 /*-----------------------------------------------------------------*/
349 void markLiveRanges (eBBlock *ebp, eBBlock **ebbs, int count)
352 bitVect *defsUsed = NULL;
353 bitVect *defsNotUsed = NULL;
355 /* for all the instructions */
356 for (ic = ebp->sch ; ic ; ic = ic->next ) {
358 if (ic->op == CALL || ic->op == PCALL) {
359 setFromRange(IC_RESULT(ic),ic->seq);
360 /* if the result has no usage then
361 mark this as the end of its life too
362 and take it away from the defs for the block*/
363 if (bitVectIsZero(OP_SYMBOL(IC_RESULT(ic))->uses)) {
364 setToRange(IC_RESULT(ic),ic->seq,FALSE);
365 bitVectUnSetBit(ebp->defSet,ic->key);
372 /* take care of the special icodes first */
373 if (ic->op == JUMPTABLE && IS_SYMOP(IC_JTCOND(ic))) {
374 operandLUse (IC_JTCOND(ic),ebbs,count,ic,ebp);
378 if (ic->op == IFX && IS_SYMOP(IC_COND(ic))) {
379 operandLUse (IC_COND(ic),ebbs,count,ic,ebp);
383 if (IS_SYMOP(IC_LEFT(ic)))
384 operandLUse (IC_LEFT(ic),ebbs,count,ic,ebp);
386 if (IS_SYMOP(IC_RIGHT(ic)))
387 operandLUse (IC_RIGHT(ic),ebbs,count,ic,ebp);
390 operandLUse(IC_RESULT(ic),ebbs,count,ic,ebp);
393 ic->defKey = IC_RESULT(ic)->key ;
397 /* for all the definitions in the block */
398 /* compute and set the live from */
399 if ( ebp->defSet && ! bitVectIsZero(ebp->defSet)) {
400 for ( i = 0 ; i < ebp->defSet->size ; i++ ) {
403 if (bitVectBitValue(ebp->defSet,i) &&
404 (dic = hTabItemWithKey(iCodehTab,i))) {
406 /* if the definition has a from & it is greater */
407 /* than the defininng iCode->seq then change it */
408 setFromRange(IC_RESULT(dic),dic->seq);
413 /* special case for lastBlock in a loop: here we
414 mark the end of all the induction variables for the
416 if (ebp->isLastInLoop && !bitVectIsZero(ebp->linds)) {
417 for ( i = 0 ; i <= ebp->linds->size ; i++ ) {
420 if (bitVectBitValue(ebp->linds,i) &&
421 (dic = hTabItemWithKey(iCodehTab,i))) {
423 /* if this is a register equvalent make sure
424 it is not defined or used anywhere after the loop */
425 if (OP_SYMBOL(IC_RESULT(dic))->isreqv &&
426 defUsedAfterLoop(IC_RESULT(dic), ebp->lSeq))
429 setToRange(IC_RESULT(dic),( ebp->lSeq ),FALSE);
434 /* for defnitions coming into the block if they */
435 /* not used by itself & any of its successors */
437 /* first union the definitions used in all successors
439 for (i = 0; i < count ; ebbs[i++]->visited = 0);
440 applyToSet(ebp->succList,unionDefsUsed,&defsUsed);
441 defsUsed = bitVectUnion(defsUsed,ebp->usesDefs);
443 /* now subract the result of these unions from */
444 /* the incoming definitions this will give the */
445 /* definitions that are never used in the future */
446 defsNotUsed = bitVectCplAnd(bitVectCopy(ebp->inDefs),
449 /* mark the end of the defintions */
450 if ( !bitVectIsZero(defsNotUsed) && ebp->sch ) {
451 for ( i = 0 ; i < defsNotUsed->size; i++ ) {
454 if (bitVectBitValue(defsNotUsed,i) &&
455 (dic = hTabItemWithKey(iCodehTab,i))) {
457 setToRange(IC_RESULT(dic),( ebp->fSeq - 1),TRUE);
463 /* if we reach a lock with noPath to it then kill all
464 the live ranges alive at this point */
465 /* if (ebp->noPath || ebp->entryLabel == returnLabel) */
466 if (ebp->entryLabel == returnLabel)
467 killAllAlive(ebp->fSeq);
470 /*-----------------------------------------------------------------*/
471 /* rlivePoint - for each point compute the ranges that are alive */
472 /*-----------------------------------------------------------------*/
473 void rlivePoint (eBBlock **ebbs, int count)
477 /* for all blocks do */
478 for ( i = 0 ; i < count ; i++ ) {
481 /* for all instruction in the block do */
482 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
486 ic->rlive = newBitVect(operandKey);
487 /* for all symbols in the liverange table */
488 for ( lrange = hTabFirstItem(liveRanges,&k); lrange;
489 lrange = hTabNextItem(liveRanges,&k)) {
491 if (lrange->liveFrom <= ic->seq &&
492 lrange->liveTo >= ic->seq )
493 ic->rlive = bitVectSetBit(ic->rlive,lrange->key);
500 /*-----------------------------------------------------------------*/
501 /* computeLiveRanges - computes the live ranges for variables */
502 /*-----------------------------------------------------------------*/
503 void computeLiveRanges (eBBlock **ebbs, int count )
506 /* sequence the code the live ranges are computed
507 in terms of this sequence additionally the
508 routine will also create a hashtable of instructions */
510 setToNull((void **)&iCodehTab);
511 iCodehTab = newHashTable (iCodeKey);
512 sequenceiCode(ebbs,count);
514 /* call routine to mark the from & to live ranges for
516 setToNull((void **)&liveRanges);
517 for ( i = 0 ; i < count; i++ )
518 markLiveRanges (ebbs[i],ebbs,count);
521 /* mark the ranges live for each point */
522 rlivePoint (ebbs,count);