+//#define LIVERANGEHUNT
+#ifdef LIVERANGEHUNT
+ #define LRH(x) x
+#else
+ #define LRH(x)
+#endif
/*-------------------------------------------------------------------------
SDCCloop.c - source file for loop detection & optimizations
{
induction *ip;
- ip = Safe_calloc (1, sizeof (induction));
+ ip = Safe_alloc ( sizeof (induction));
ip->sym = sym;
ip->asym = asym;
ip->op = op;
ip->cval = constVal;
ip->ic = ic;
-
+ updateSpillLocation(ic,1);
return ip;
}
{
region *lp;
- lp = Safe_calloc (1, sizeof (region));
+ lp = Safe_alloc ( sizeof (region));
return lp;
}
{
if (!isinSet (*regionSet, block))
{
+ LRH(printf ("loopInsert: %s\n", block->entryLabel->name));
addSetHead (regionSet, block);
STACK_PUSH (regionStack, block);
}
V_ARG (set **, exits);
V_ARG (int, depth);
V_ARG (region *, lr);
-
+ LRH(printf ("addToExitsMarkDepth: %s %d\n", ebp->entryLabel->name, depth));
/* mark the loop depth of this block */
- if (!ebp->depth)
+ //if (!ebp->depth)
+ if (ebp->depth<depth)
ebp->depth = depth;
/* put the loop region info in the block */
{
edge *ep = item;
V_ARG (set **, allRegion);
+ V_ARG (eBBlock **,ebbs);
+ V_ARG (int,count);
region *aloop = newRegion ();
eBBlock *block;
+ int dfMin = count ,dfMax =0, i;
+ LRH(printf("CreateLoop\n"));
/* make sure regionStack is empty */
while (!STACK_EMPTY (regionStack))
STACK_POP (regionStack);
/* add the entryBlock */
addSet (&aloop->regBlocks, ep->to);
+#ifdef LIVERANGEHUNT
+ // print regBlocks jwk
+ {
+ eBBlock *ebp;
+ region *lp=aloop;
+ for (ebp=setFirstItem(lp->regBlocks); ebp; ebp=setNextItem(lp->regBlocks)) {
+ printf ("cl1 %s ", ebp->entryLabel->name);
+ }
+ printf (" %d\n", count);
+ }
+#endif
loopInsert (&aloop->regBlocks, ep->from);
+#ifdef LIVERANGEHUNT
+ // print regBlocks jwk
+ {
+ eBBlock *ebp;
+ region *lp=aloop;
+ for (ebp=setFirstItem(lp->regBlocks); ebp; ebp=setNextItem(lp->regBlocks)) {
+ printf ("cl2 %s ", ebp->entryLabel->name);
+ }
+ printf (" %d\n", count);
+ }
+#endif
while (!STACK_EMPTY (regionStack))
{
applyToSet (block->predList, insertIntoLoop, &aloop->regBlocks);
}
+#ifdef LIVERANGEHUNT
+ // print regBlocks jwk
+ {
+ eBBlock *ebp;
+ region *lp=aloop;
+ for (ebp=setFirstItem(lp->regBlocks); ebp; ebp=setNextItem(lp->regBlocks)) {
+ printf ("cl3 %s ", ebp->entryLabel->name);
+ }
+ printf (" %d\n", count);
+ }
+#endif
+
aloop->entry = ep->to;
+ /* set max & min dfNum for loopRegion */
+ for ( block = setFirstItem(aloop->regBlocks); block;
+ block = setNextItem(aloop->regBlocks)) {
+ if (block->dfnum > dfMax) dfMax = block->dfnum;
+ if (block->dfnum < dfMin) dfMin = block->dfnum;
+ }
+
+ /* all blocks that have dfnumbers between dfMin & dfMax are also
+ part of loop */
+ for (i = 0 ; i < count ; i++) {
+ if (ebbs[i]->dfnum > dfMin &&
+ ebbs[i]->dfnum < dfMax &&
+ !isinSet(aloop->regBlocks,ebbs[i])) {
+ if (!ebbs[i]->partOfLoop) {
+#if !defined(LIVERANGEHUNT)
+ ebbs[i]->partOfLoop = aloop;
+#else
+ loopInsert(&aloop->regBlocks,ebbs[i]);
+#endif
+ }
+ }
+ }
+
+#ifdef LIVERANGEHUNT
+ printf ("================\n");
+ printf (" loop with entry -- > ");
+ printEntryLabel (aloop->entry, ap);
+ printf ("\n");
+ printf (" loop body --> ");
+ applyToSet (aloop->regBlocks, printEntryLabel);
+ printf ("\n");
+ printf (" loop exits --> ");
+ applyToSet (aloop->exits, printEntryLabel);
+ printf ("\n");
+#endif
+
+ /* and if this is a conditional block, the other side of the IFX
+ (if any, that could have a greater dfnum) is too */
+ {
+ // just a burp, but I'm getting close :)
+ }
+
/* now add it to the set */
addSetHead (allRegion, aloop);
!IS_OP_VOLATILE (op) &&
assignmentsToSym (theLoop->regBlocks, op) == 0)
opin = 1;
+ LRH(if (opin && IS_SYMOP(op)) {
+ printf("isOperandInvariant: %s\n", OP_SYMBOL(op)->name);
+ });
}
else
opin++;
set *lInvars = NULL;
int change = 0;
+ int fCallsInBlock;
/* if the preHeader does not exist then do nothing */
/* or no exits then do nothing ( have to think about this situation */
domsAllExits = (applyToSet (theLoop->exits, dominatedBy, lBlock) ==
elementsInSet (theLoop->exits));
+ /* find out if we have a function call in this block */
+ for (ic = lBlock->sch, fCallsInBlock=0; ic; ic = ic->next) {
+ if (SKIP_IC(ic)) {
+ fCallsInBlock++;
+ }
+ }
/* now we go thru the instructions of this block and */
/* collect those instructions with invariant operands */
int lin, rin;
cseDef *ivar;
+ /* jwk: TODO this is only needed if the call is between
+ here and the definition, but I am too lazy to do that now */
+
+ /* if there are function calls in this block */
+ if (fCallsInBlock) {
+
+ /* if this is a pointer get */
+ if (POINTER_GET(ic)) {
+ continue;
+ }
+
+ /* if this is an assignment from a global */
+ if (ic->op=='=' && isOperandGlobal(IC_RIGHT(ic))) {
+ continue;
+ }
+ }
+
if (SKIP_IC (ic) || POINTER_SET (ic) || ic->op == IFX)
continue;
continue;
lin = rin = 0;
-
+
/* special case */
/* if address of then it is an invariant */
if (ic->op == ADDRESS_OF &&
IS_SYMOP (IC_LEFT (ic)) &&
IS_AGGREGATE (operandType (IC_LEFT (ic))))
lin++;
- else
+ else {
/* check if left operand is an invariant */
- if ((lin = isOperandInvariant (IC_LEFT (ic), theLoop, lInvars)))
- /* if this is a pointer get then make sure
- that the pointer set does not exist in
- any of the blocks */
- if (POINTER_GET (ic) &&
- (applyToSet (theLoop->regBlocks, pointerAssigned, IC_LEFT (ic))))
- lin = 0;
-
+ if ((lin = isOperandInvariant (IC_LEFT (ic), theLoop, lInvars)))
+ /* if this is a pointer get then make sure
+ that the pointer set does not exist in
+ any of the blocks */
+ if (POINTER_GET (ic) &&
+ (applyToSet (theLoop->regBlocks,
+ pointerAssigned, IC_LEFT (ic))))
+ lin = 0;
+ }
+
/* do the same for right */
rin = isOperandInvariant (IC_RIGHT (ic), theLoop, lInvars);
-
+
/* if this is a POINTER_GET then special case, make sure all
usages within the loop are POINTER_GET any other usage
would mean that this is not an invariant , since the pointer
continue;
aSym = (IS_OP_LITERAL (IC_RIGHT (dic)) ?
- (litValue = operandLitValue (IC_RIGHT (dic)), IC_LEFT (dic)) :
- (litValue = operandLitValue (IC_LEFT (dic)), IC_RIGHT (dic)));
+ (litValue = (unsigned long) operandLitValue (IC_RIGHT (dic)), IC_LEFT (dic)) :
+ (litValue = (unsigned long) operandLitValue (IC_LEFT (dic)), IC_RIGHT (dic)));
if (!isOperandEqual (IC_RESULT (ic), aSym) &&
!isOperandEqual (IC_RIGHT (ic), aSym))
iCode *newic = newiCode ('=', NULL,
operandFromOperand (IC_RIGHT (ic)));
IC_RESULT (newic) = operandFromOperand (IC_RESULT (ic));
- OP_DEFS (IC_RESULT (newic)) =
+ OP_DEFS(IC_RESULT (newic))=
bitVectSetBit (OP_DEFS (IC_RESULT (newic)), newic->key);
- OP_USES (IC_RIGHT (newic)) =
+ OP_USES(IC_RIGHT (newic))=
bitVectSetBit (OP_USES (IC_RIGHT (newic)), newic->key);
/* and add it */
if (eblock->sch && eblock->sch->op == LABEL)
continue;
aSym = (IS_SYMOP (IC_LEFT (ic)) ?
- (lr = 1, litVal = operandLitValue (IC_RIGHT (ic)), IC_LEFT (ic)) :
- (litVal = operandLitValue (IC_LEFT (ic)), IC_RIGHT (ic)));
+ (lr = 1, litVal = (unsigned long) operandLitValue (IC_RIGHT (ic)), IC_LEFT (ic)) :
+ (litVal = (unsigned long) operandLitValue (IC_LEFT (ic)), IC_RIGHT (ic)));
ip = NULL;
/* check if this is an induction variable */
/* ask port for size not worth if native instruction
exist for multiply & divide */
- if (getSize (operandType (IC_LEFT (ic))) <= port->muldiv.native_below ||
- getSize (operandType (IC_RIGHT (ic))) <= port->muldiv.native_below)
+ if (getSize (operandType (IC_LEFT (ic))) <= (unsigned long) port->support.muldiv ||
+ getSize (operandType (IC_RIGHT (ic))) <= (unsigned long) port->support.muldiv)
continue;
/* if this is a division then the remainder should be zero
/* add the induction variable vector to the last
block in the loop */
lastBlock->isLastInLoop = 1;
- lastBlock->linds = indVect;
+ lastBlock->linds = bitVectUnion(lastBlock->linds,indVect);
}
setToNull ((void **) &indVars);
int maxDepth = 0;
region *lp;
+ LRH(printf ("createLoopRegions: %p\n", ebbs));
/* get all the back edges in the graph */
if (!applyToSet (graphEdges, backEdges, &bEdges))
return 0; /* found no loops */
/* for each of these back edges get the blocks that */
/* constitute the loops */
- applyToSet (bEdges, createLoop, &allRegion);
+ applyToSet (bEdges, createLoop, &allRegion, ebbs,count);
+#ifdef LIVERANGEHUNT
+ // print regBlocks
+ {
+ eBBlock *ebp;
+ lp=setFirstItem(allRegion);
+ printf ("createLoopRegions: ");
+ for (ebp=setFirstItem(lp->regBlocks); ebp; ebp=setNextItem(lp->regBlocks)) {
+ printf ("%s ", ebp->entryLabel->name);
+ }
+ printf (" %d\n", count);
+ }
+#endif
/* now we will create regions from these loops */
/* loops with the same entry points are considered to be the */
applyToSet (allRegion, mergeInnerLoops, allRegion, &maxDepth);
maxDepth++;
+
/* now create all the exits .. also */
/* create an ordered set of loops */
/* i.e. we process loops in the inner to outer order */