1 /*-------------------------------------------------------------------------
3 SDCCloop.c - source file for loop detection & optimizations
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 -------------------------------------------------------------------------*/
29 DEFSETFUNC (isDefAlive);
31 STACK_DCL (regionStack, eBBlock *, MAX_NEST_LEVEL * 10);
33 /*-----------------------------------------------------------------*/
34 /* newInduction - creates a new induction variable */
35 /*-----------------------------------------------------------------*/
37 newInduction (operand * sym, unsigned int op,
38 long constVal, iCode * ic, operand * asym)
42 ip = Safe_alloc ( sizeof (induction));
49 //updateSpillLocation(ic,1);
53 /*-----------------------------------------------------------------*/
54 /* newRegion - allocate & returns a loop structure */
55 /*-----------------------------------------------------------------*/
61 lp = Safe_alloc ( sizeof (region));
67 /*-----------------------------------------------------------------*/
68 /* pinduction - prints induction */
69 /*-----------------------------------------------------------------*/
70 DEFSETFUNC (pinduction)
75 fprintf (stdout, "\t");
76 printOperand (ip->sym, stdout);
77 icTab = getTableEntry (ip->ic->op);
78 icTab->iCodePrint (stdout, ip->ic, icTab->printName);
79 fprintf (stdout, " %04d\n", (int) ip->cval);
83 /*-----------------------------------------------------------------*/
84 /* pregion - prints loop information */
85 /*-----------------------------------------------------------------*/
90 printf ("================\n");
91 printf (" loop with entry -- > ");
92 printEntryLabel (lp->entry, ap);
94 printf (" loop body --> ");
95 applyToSet (lp->regBlocks, printEntryLabel);
97 printf (" loop exits --> ");
98 applyToSet (lp->exits, printEntryLabel);
103 /*-----------------------------------------------------------------*/
104 /* backEdges - returns a list of back edges */
105 /*-----------------------------------------------------------------*/
106 DEFSETFUNC (backEdges)
109 V_ARG (set **, bEdges);
111 /* if this is a back edge ; to determine this we check */
112 /* to see if the 'to' is in the dominator list of the */
113 /* 'from' if yes then this is a back edge */
114 if (bitVectBitValue (ep->from->domVect, ep->to->bbnum))
116 addSetHead (bEdges, ep);
123 /*-----------------------------------------------------------------*/
124 /* intersectLoopSucc - returns intersection of loop Successors */
125 /*-----------------------------------------------------------------*/
127 intersectLoopSucc (set * lexits, eBBlock ** ebbs)
129 bitVect *succVect = NULL;
130 eBBlock *exit = setFirstItem (lexits);
135 succVect = bitVectCopy (exit->succVect);
137 for (exit = setNextItem (lexits); exit;
138 exit = setNextItem (lexits))
140 succVect = bitVectIntersect (succVect,
148 /*-----------------------------------------------------------------*/
149 /* loopInsert will insert a block into the loop set */
150 /*-----------------------------------------------------------------*/
152 loopInsert (set ** regionSet, eBBlock * block)
154 if (!isinSet (*regionSet, block))
156 addSetHead (regionSet, block);
157 STACK_PUSH (regionStack, block);
161 /*-----------------------------------------------------------------*/
162 /* insertIntoLoop - insert item into loop */
163 /*-----------------------------------------------------------------*/
164 DEFSETFUNC (insertIntoLoop)
167 V_ARG (set **, regionSet);
169 loopInsert (regionSet, ebp);
173 /*-----------------------------------------------------------------*/
174 /* isNotInBlocks - will return 1 if not is blocks */
175 /*-----------------------------------------------------------------*/
176 DEFSETFUNC (isNotInBlocks)
179 V_ARG (set *, blocks);
181 if (!isinSet (blocks, ebp))
187 /*-----------------------------------------------------------------*/
188 /* hasIncomingDefs - has definitions coming into the loop. i.e. */
189 /* check to see if the preheaders outDefs has any definitions */
190 /*-----------------------------------------------------------------*/
192 hasIncomingDefs (region * lreg, operand * op)
194 eBBlock *preHdr = lreg->entry->preHeader;
196 if (preHdr && bitVectBitsInCommon (preHdr->outDefs, OP_DEFS (op)))
201 /*-----------------------------------------------------------------*/
202 /* findLoopEndSeq - will return the sequence number of the last */
203 /* iCode with the maximum dfNumber in the region */
204 /*-----------------------------------------------------------------*/
206 findLoopEndSeq (region * lreg)
211 for (block = lblock = setFirstItem (lreg->regBlocks); block;
212 block = setNextItem (lreg->regBlocks))
214 if (block != lblock && block->lSeq > lblock->lSeq)
221 /*-----------------------------------------------------------------*/
222 /* addToExitsMarkDepth - will add the the exitSet all blocks that */
223 /* have exits, will also update the depth field in the blocks */
224 /*-----------------------------------------------------------------*/
225 DEFSETFUNC (addToExitsMarkDepth)
228 V_ARG (set *, loopBlocks);
229 V_ARG (set **, exits);
231 V_ARG (region *, lr);
233 /* mark the loop depth of this block */
235 if (ebp->depth<depth)
238 /* NOTE: here we will update only the inner most loop
239 that it is a part of */
240 if (!ebp->partOfLoop)
241 ebp->partOfLoop = lr;
243 /* if any of the successors go out of the loop then */
244 /* we add this one to the exits */
245 if (applyToSet (ebp->succList, isNotInBlocks, loopBlocks))
247 addSetHead (exits, ebp);
254 /*-----------------------------------------------------------------*/
255 /* createLoop - will create a set of region */
256 /*-----------------------------------------------------------------*/
257 DEFSETFUNC (createLoop)
260 V_ARG (set **, allRegion);
261 region *aloop = newRegion ();
264 /* make sure regionStack is empty */
265 while (!STACK_EMPTY (regionStack))
266 STACK_POP (regionStack);
268 /* add the entryBlock */
269 addSet (&aloop->regBlocks, ep->to);
270 loopInsert (&aloop->regBlocks, ep->from);
272 while (!STACK_EMPTY (regionStack))
274 block = STACK_POP (regionStack);
275 /* if block != entry */
277 applyToSet (block->predList, insertIntoLoop, &aloop->regBlocks);
280 aloop->entry = ep->to;
282 /* now add it to the set */
283 addSetHead (allRegion, aloop);
287 /*-----------------------------------------------------------------*/
288 /* dominatedBy - will return 1 if item is dominated by block */
289 /*-----------------------------------------------------------------*/
290 DEFSETFUNC (dominatedBy)
293 V_ARG (eBBlock *, block);
295 return bitVectBitValue (ebp->domVect, block->bbnum);
298 /*-----------------------------------------------------------------*/
299 /* addDefInExprs - adds an expression into the inexpressions */
300 /*-----------------------------------------------------------------*/
301 DEFSETFUNC (addDefInExprs)
304 V_ARG (cseDef *, cdp);
305 V_ARG (ebbIndex *, ebbi);
307 addSetHead (&ebp->inExprs, cdp);
308 cseBBlock (ebp, optimize.global_cse, ebbi);
312 /*-----------------------------------------------------------------*/
313 /* assignmentsToSym - for a set of blocks determine # time assigned */
314 /*-----------------------------------------------------------------*/
316 assignmentsToSym (set * sset, operand * sym)
320 set *blocks = setFromSet (sset);
322 for (ebp = setFirstItem (blocks); ebp;
323 ebp = setNextItem (blocks))
326 /* get all the definitions for this symbol
328 bitVect *defs = bitVectIntersect (ebp->ldefs, OP_DEFS (sym));
329 assigns += bitVectnBitsOn (defs);
330 setToNull ((void *) &defs);
338 /*-----------------------------------------------------------------*/
339 /* isOperandInvariant - determines if an operand is an invariant */
340 /*-----------------------------------------------------------------*/
342 isOperandInvariant (operand * op, region * theLoop, set * lInvars)
345 /* operand is an invariant if it is a */
347 /* b. that have defintions reaching loop entry */
348 /* c. that are already defined as invariant */
349 /* d. has no assignments in the loop */
352 if (IS_OP_LITERAL (op))
354 else if (IS_SYMOP (op) &&
355 OP_SYMBOL (op)->addrtaken)
357 else if (ifDefSymIs (theLoop->entry->inExprs, op))
359 else if (ifDefSymIs (lInvars, op))
361 else if (IS_SYMOP (op) &&
362 !IS_OP_GLOBAL (op) &&
363 !IS_OP_VOLATILE (op) &&
364 assignmentsToSym (theLoop->regBlocks, op) == 0)
373 /*-----------------------------------------------------------------*/
374 /* pointerAssigned - will return 1 if pointer set found */
375 /*-----------------------------------------------------------------*/
376 DEFSETFUNC (pointerAssigned)
379 V_ARG (operand *, op);
384 if (bitVectBitValue (ebp->ptrsSet, op->key))
387 /* Unfortunately, one of the other pointer set operations */
388 /* may be using an alias of this operand, and the above */
389 /* test would miss it. To be thorough, some aliasing */
390 /* analysis should be done here. In the meantime, be */
391 /* conservative and assume any other pointer set operation */
393 if (!bitVectIsZero (ebp->ptrsSet))
399 /*-----------------------------------------------------------------*/
400 /* hasNonPtrUse - returns true if operand has non pointer usage */
401 /*-----------------------------------------------------------------*/
402 DEFSETFUNC (hasNonPtrUse)
405 V_ARG (operand *, op);
406 iCode *ic = usedInRemaining (op, ebp->sch);
408 if (ic && !POINTER_SET (ic) && !POINTER_GET (ic))
415 /*-----------------------------------------------------------------*/
416 /* loopInvariants - takes loop invariants out of region */
417 /*-----------------------------------------------------------------*/
419 loopInvariants (region * theLoop, ebbIndex * ebbi)
421 eBBlock ** ebbs = ebbi->dfOrder;
422 int count = ebbi->count;
429 /* if the preHeader does not exist then do nothing */
430 /* or no exits then do nothing ( have to think about this situation */
431 if (theLoop->entry->preHeader == NULL ||
432 theLoop->exits == NULL)
435 /* we will do the elimination for those blocks */
436 /* in the loop that dominate all exits from the loop */
437 for (lBlock = setFirstItem (theLoop->regBlocks); lBlock;
438 lBlock = setNextItem (theLoop->regBlocks))
445 /* mark the dominates all exits flag */
446 domsAllExits = (applyToSet (theLoop->exits, dominatedBy, lBlock) ==
447 elementsInSet (theLoop->exits));
449 /* find out if we have a function call in this block */
450 for (ic = lBlock->sch, fCallsInBlock=0; ic; ic = ic->next) {
456 /* now we go thru the instructions of this block and */
457 /* collect those instructions with invariant operands */
458 for (ic = lBlock->sch; ic; ic = ic->next)
464 /* TODO this is only needed if the call is between
465 here and the definition, but I am too lazy to do that now */
467 /* if there are function calls in this block */
470 /* if this is a pointer get */
471 if (POINTER_GET(ic)) {
475 /* if this is an assignment from a global */
476 if (ic->op=='=' && isOperandGlobal(IC_RIGHT(ic))) {
481 if (SKIP_IC (ic) || POINTER_SET (ic) || ic->op == IFX)
484 /* iTemp assignment from a literal may be invariant, but it
485 will needlessly increase register pressure if the
486 iCode(s) that use this iTemp are not also invariant */
487 if (ic->op=='=' && IS_ITEMP (IC_RESULT (ic))
488 && IS_OP_LITERAL (IC_RIGHT (ic)))
491 /* if result is volatile then skip */
492 if (IC_RESULT (ic) &&
493 (isOperandVolatile (IC_RESULT (ic), TRUE) ||
494 IS_OP_PARM (IC_RESULT (ic))))
497 /* if result depends on a volatile then skip */
498 if ((IC_LEFT(ic) && isOperandVolatile(IC_LEFT(ic), TRUE)) ||
499 (IC_RIGHT(ic) && isOperandVolatile(IC_RIGHT(ic), TRUE)))
505 /* if address of then it is an invariant */
506 if (ic->op == ADDRESS_OF &&
507 IS_SYMOP (IC_LEFT (ic)) &&
508 IS_AGGREGATE (operandType (IC_LEFT (ic))))
511 /* check if left operand is an invariant */
512 if ((lin = isOperandInvariant (IC_LEFT (ic), theLoop, lInvars)))
513 /* if this is a pointer get then make sure
514 that the pointer set does not exist in
516 if (POINTER_GET (ic) &&
517 (applyToSet (theLoop->regBlocks,
518 pointerAssigned, IC_LEFT (ic))))
522 /* do the same for right */
523 rin = isOperandInvariant (IC_RIGHT (ic), theLoop, lInvars);
525 /* if this is a POINTER_GET then special case, make sure all
526 usages within the loop are POINTER_GET any other usage
527 would mean that this is not an invariant , since the pointer
528 could then be passed as a parameter */
529 if (POINTER_GET (ic) &&
530 applyToSet (theLoop->regBlocks, hasNonPtrUse, IC_LEFT (ic)))
534 /* if both the left & right are invariants : then check that */
535 /* this definition exists in the out definition of all the */
536 /* blocks, this will ensure that this is not assigned any */
537 /* other value in the loop, and not used in this block */
538 /* prior to this definition which means only this definition */
539 /* is used in this loop */
540 if (lin && rin && IC_RESULT (ic))
543 set *lSet = setFromSet (theLoop->regBlocks);
545 /* if this block does not dominate all exits */
546 /* make sure this defintion is not used anywhere else */
550 if (isOperandGlobal (IC_RESULT (ic)))
552 /* for successors for all exits */
553 for (sBlock = setFirstItem (theLoop->exits); sBlock;
554 sBlock = setNextItem (theLoop->exits))
557 for (i = 0; i < count; ebbs[i++]->visited = 0);
559 if (applyToSet (sBlock->succList, isDefAlive, ic))
563 /* we have found usage */
568 /* now make sure this is the only definition */
569 for (sBlock = setFirstItem (lSet); sBlock;
570 sBlock = setNextItem (lSet))
572 /* if this is the block make sure the definition */
573 /* reaches the end of the block */
574 if (sBlock == lBlock)
576 if (!ifDiCodeIs (sBlock->outExprs, ic))
579 else if (bitVectBitsInCommon (sBlock->defSet, OP_DEFS (IC_RESULT (ic))))
585 /* check that this definition is not assigned */
586 /* any other value in this block */
587 for (ic2 = sBlock->sch; ic2; ic2 = ic2->next)
589 if ((ic != ic2) && (isOperandEqual(IC_RESULT(ic), IC_RESULT(ic2))))
592 if (ic2) /* found another definition */
598 continue; /* another definition present in the block */
600 /* now check if it exists in the in of this block */
601 /* if not then it was killed before this instruction */
602 if (!bitVectBitValue (lBlock->inDefs, ic->key))
605 /* now we know it is a true invariant */
606 /* remove it from the insts chain & put */
607 /* in the invariant set */
609 OP_SYMBOL (IC_RESULT (ic))->isinvariant = 1;
610 SPIL_LOC (IC_RESULT (ic)) = NULL;
611 remiCodeFromeBBlock (lBlock, ic);
613 /* maintain the data flow */
614 /* this means removing from definition from the */
615 /* defset of this block and adding it to the */
616 /* inexpressions of all blocks within the loop */
617 bitVectUnSetBit (lBlock->defSet, ic->key);
618 bitVectUnSetBit (lBlock->ldefs, ic->key);
619 ivar = newCseDef (IC_RESULT (ic), ic);
620 applyToSet (theLoop->regBlocks, addDefInExprs, ivar, ebbi);
621 addSet (&lInvars, ivar);
624 } /* for all loop blocks */
626 /* if we have some invariants then */
629 eBBlock *preHdr = theLoop->entry->preHeader;
630 iCode *icFirst = NULL, *icLast = NULL;
633 /* create an iCode chain from it */
634 for (cdp = setFirstItem (lInvars); cdp; cdp = setNextItem (lInvars))
637 /* maintain data flow .. add it to the */
638 /* ldefs defSet & outExprs of the preheader */
639 preHdr->defSet = bitVectSetBit (preHdr->defSet, cdp->diCode->key);
640 preHdr->ldefs = bitVectSetBit (preHdr->ldefs, cdp->diCode->key);
641 cdp->diCode->lineno = preHdr->ech->lineno;
642 addSetHead (&preHdr->outExprs, cdp);
646 icFirst = cdp->diCode;
649 icLast->next = cdp->diCode;
650 cdp->diCode->prev = icLast;
651 icLast = cdp->diCode;
654 icLast = cdp->diCode;
658 /* add the instruction chain to the end of the
659 preheader for this loop, preheaders will always
660 have atleast a label */
661 preHdr->ech->next = icFirst;
662 icFirst->prev = preHdr->ech;
663 preHdr->ech = icLast;
670 /*-----------------------------------------------------------------*/
671 /* addressTaken - returns true if the symbol is found in the addrof */
672 /*-----------------------------------------------------------------*/
674 addressTaken (set * sset, operand * sym)
680 for (loop = sset; loop; loop = loop->next)
686 if (isOperandEqual ((operand *) loop2->item, sym))
696 /*-----------------------------------------------------------------*/
697 /* findInduction :- returns 1 & the item if the induction is found */
698 /*-----------------------------------------------------------------*/
699 DEFSETFUNC (findInduction)
701 induction *ip = item;
702 V_ARG (operand *, sym);
703 V_ARG (induction **, ipp);
705 if (isOperandEqual (ip->sym, sym))
714 /*-----------------------------------------------------------------*/
715 /* findDefInRegion - finds the definition within the region */
716 /*-----------------------------------------------------------------*/
718 findDefInRegion (set * regBlocks, operand * defOp, eBBlock ** owner)
722 /* for all blocks in the region */
723 for (lBlock = setFirstItem (regBlocks); lBlock;
724 lBlock = setNextItem (regBlocks))
727 /* if a definition for this exists */
728 if (bitVectBitsInCommon (lBlock->defSet, OP_DEFS (defOp)))
732 /* go thru the instruction chain to find it */
733 for (ic = lBlock->sch; ic; ic = ic->next)
734 if (bitVectBitValue (OP_DEFS (defOp), ic->key))
746 /*-----------------------------------------------------------------*/
747 /* basicInduction - finds the basic induction variables in a loop */
748 /*-----------------------------------------------------------------*/
750 basicInduction (region * loopReg, eBBlock ** ebbs, int count)
755 /* i.e. all assignments of the form a := a +/- const */
756 /* for all blocks within the loop do */
757 for (lBlock = setFirstItem (loopReg->regBlocks); lBlock;
758 lBlock = setNextItem (loopReg->regBlocks))
763 /* for all instructions in the blocks do */
764 for (ic = lBlock->sch; ic; ic = ic->next)
771 eBBlock *owner = NULL;
775 /* To find an induction variable, we need to */
776 /* find within the loop three iCodes in this */
779 /* ddic: iTempB := symbolVar */
780 /* dic: iTempA := iTempB + lit */
781 /* or iTempA := iTempB - lit */
782 /* or iTempA := lit + iTempB */
783 /* ic: symbolVar := iTempA */
785 /* (symbolVar may also be an iTemp if it is */
786 /* register equivalent) */
788 /* look for assignments of the form */
789 /* symbolVar := iTempNN */
793 if (!IS_TRUE_SYMOP (IC_RESULT (ic)) &&
794 !OP_SYMBOL (IC_RESULT (ic))->isreqv)
797 if (isOperandGlobal (IC_RESULT (ic)))
800 if (!IS_ITEMP (IC_RIGHT (ic)))
803 /* if it has multiple assignments within the loop then skip */
804 if (assignmentsToSym (loopReg->regBlocks, IC_RESULT (ic)) > 1)
807 /* if the address of this was taken inside the loop then continue */
808 if (addressTaken (loopReg->regBlocks, IC_RESULT (ic)))
811 /* Only consider variables with integral type. */
812 /* (2004/12/06 - EEP - ds390 fails regression tests unless */
813 /* pointers are also considered for induction (due to some */
814 /* register alloctaion bugs). Remove !IS_PTR clause when */
815 /* that gets fixed) */
816 optype = operandType (IC_RIGHT (ic));
817 if (!IS_INTEGRAL (optype) && !IS_PTR (optype))
820 /* find the definition for the result in the block */
821 if (!(dic = findDefInRegion (setFromSet (loopReg->regBlocks),
822 IC_RIGHT (ic), &owner)))
825 /* if not +/- continue */
826 if (dic->op != '+' && dic->op != '-')
829 /* make sure definition is of the form a +/- c */
830 if (!IS_OP_LITERAL (IC_LEFT (dic)) && !IS_OP_LITERAL (IC_RIGHT (dic)))
833 /* make sure the definition found is the only one */
834 if (assignmentsToSym (loopReg->regBlocks, IC_RIGHT (ic)) > 1)
837 if (IS_OP_LITERAL (IC_RIGHT (dic)))
839 aSym = IC_LEFT (dic);
840 litValue = (long) operandLitValue (IC_RIGHT (dic));
844 /* For minus, the literal must not be on the left side. */
845 /* (Actually, this case could be handled, but it's probably */
846 /* not worth the extra code) */
849 aSym = IC_RIGHT (dic);
850 litValue = (long) operandLitValue (IC_LEFT (dic));
853 if (!isOperandEqual (IC_RESULT (ic), aSym) &&
854 !isOperandEqual (IC_RIGHT (ic), aSym))
857 /* find the definition for this and check */
858 if (!(ddic = findDefInRegion (setFromSet (loopReg->regBlocks),
865 if (!isOperandEqual (IC_RESULT (ddic), aSym) ||
866 !isOperandEqual (IC_RIGHT (ddic), IC_RESULT (ic)))
870 /* if the right hand side has more than one usage then
871 don't make it an induction (will have to think some more) */
872 if (bitVectnBitsOn (OP_USES (IC_RIGHT (ic))) > 1)
875 /* if the definition is volatile then it cannot be
876 an induction object */
877 if (isOperandVolatile (IC_RIGHT (ic), FALSE) ||
878 isOperandVolatile (IC_RESULT (ic), FALSE))
881 /* whew !! that was a lot of work to find the definition */
882 /* create an induction object */
883 indIc = newiCode ('=', NULL, IC_RESULT (ic));
884 indIc->lineno = ic->lineno;
885 IC_RESULT (indIc) = operandFromOperand (IC_RIGHT (ic));
886 IC_RESULT (indIc)->isaddr = 0;
887 OP_SYMBOL (IC_RESULT (indIc))->isind = 1;
888 ip = newInduction (IC_RIGHT (ic), dic->op, litValue, indIc, NULL);
890 /* replace the inducted variable by the iTemp */
891 replaceSymBySym (loopReg->regBlocks, IC_RESULT (ic), IC_RIGHT (ic));
893 /* if it has only one exit then remove it from here
894 and put it in the exit block */
895 nexits = elementsInSet (loopReg->exits);
898 eBBlock *exit = setFirstItem (loopReg->exits);
899 /* if it is the same block then there is no
900 need to move it about */
903 iCode *saveic = ic->prev;
905 remiCodeFromeBBlock (lBlock, ic);
906 /* clear the definition */
907 bitVectUnSetBit (lBlock->defSet, ic->key);
908 /* add it to the exit */
909 addiCodeToeBBlock (exit, ic, NULL);
910 /* set the definition bit */
911 exit->defSet = bitVectSetBit (exit->defSet, ic->key);
916 /* if the number of exits is greater than one then
917 we use another trick ; we will create an intersection
918 of succesors of the exits, then take those that are not
919 part of the loop and have dfNumber greater loop entry
920 and insert a new definition in them */
924 bitVect *loopSuccs = intersectLoopSucc (loopReg->exits, ebbs);
926 /* loopSuccs now contains intersection
927 of all the loops successors */
931 for (i = 0; i < loopSuccs->size; i++)
933 if (bitVectBitValue (loopSuccs, i))
936 eBBlock *eblock = NULL;
939 /* Need to search for bbnum == i since ebbs is */
940 /* sorted by dfnum; a direct index won't do. */
941 for (j=0; j<count; j++)
942 if (ebbs[j]->bbnum == i)
949 /* if the successor does not belong to the loop
950 and will be executed after the loop : then
951 add a definition to the block */
952 if (!isinSet (loopReg->regBlocks, eblock) &&
953 eblock->dfnum > loopReg->entry->dfnum)
955 /* create the definition */
956 iCode *newic = newiCode ('=', NULL,
957 operandFromOperand (IC_RIGHT (ic)));
958 IC_RESULT (newic) = operandFromOperand (IC_RESULT (ic));
959 OP_DEFS(IC_RESULT (newic))=
960 bitVectSetBit (OP_DEFS (IC_RESULT (newic)), newic->key);
961 OP_USES(IC_RIGHT (newic))=
962 bitVectSetBit (OP_USES (IC_RIGHT (newic)), newic->key);
964 if (eblock->sch && eblock->sch->op == LABEL)
965 addiCodeToeBBlock (eblock, newic, eblock->sch->next);
967 addiCodeToeBBlock (eblock, newic, eblock->sch);
968 /* set the definition bit */
969 eblock->defSet = bitVectSetBit (eblock->defSet, ic->key);
976 addSet (&indVars, ip);
979 } /* end of all blocks for basic induction variables */
984 /*-----------------------------------------------------------------*/
985 /* loopInduction - remove induction variables from a loop */
986 /*-----------------------------------------------------------------*/
988 loopInduction (region * loopReg, ebbIndex * ebbi)
990 eBBlock ** ebbs = ebbi->dfOrder;
991 int count = ebbi->count;
993 eBBlock *lBlock, *owner, *lastBlock = NULL;
995 set *basicInd = NULL;
997 if (loopReg->entry->preHeader == NULL)
1000 /* we first determine the basic Induction variables */
1001 basicInd = setFromSet (indVars = basicInduction (loopReg, ebbs, count));
1003 /* find other induction variables : by other we mean definitions of */
1004 /* the form x := y (* | / ) <constant> .. we will move this one to */
1005 /* beginning of the loop and reduce strength i.e. replace with +/- */
1006 /* these expensive expressions: OH! and y must be induction too */
1007 for (lBlock = setFirstItem (loopReg->regBlocks), lastBlock = lBlock;
1009 lBlock = setNextItem (loopReg->regBlocks))
1012 iCode *ic, *indIc, *dic;
1015 /* last block is the one with the highest block
1017 if (lastBlock->bbnum < lBlock->bbnum)
1020 for (ic = lBlock->sch; ic; ic = ic->next)
1025 /* consider only * & / */
1026 if (ic->op != '*' && ic->op != '/')
1029 /* only consider variables with integral type */
1030 if (!IS_INTEGRAL (operandType (IC_RESULT (ic))))
1033 /* if the result has more definitions then */
1034 if (assignmentsToSym (loopReg->regBlocks, IC_RESULT (ic)) > 1)
1037 /* check if the operands are what we want */
1038 /* i.e. one of them an symbol the other a literal */
1039 if (!((IS_SYMOP (IC_LEFT (ic)) && IS_OP_LITERAL (IC_RIGHT (ic))) ||
1040 (IS_OP_LITERAL (IC_LEFT (ic)) && IS_SYMOP (IC_RIGHT (ic)))))
1043 if (IS_SYMOP (IC_LEFT (ic)))
1045 aSym = IC_LEFT (ic);
1046 litVal = (long) operandLitValue (IC_RIGHT (ic));
1050 /* For division, the literal must not be on the left side */
1053 aSym = IC_RIGHT (ic);
1054 litVal = (long) operandLitValue (IC_LEFT (ic));
1058 /* check if this is an induction variable */
1059 if (!applyToSetFTrue (basicInd, findInduction, aSym, &ip))
1062 /* ask port for size not worth if native instruction
1063 exist for multiply & divide */
1064 if (getSize (operandType (IC_LEFT (ic))) <= (unsigned long) port->support.muldiv ||
1065 getSize (operandType (IC_RIGHT (ic))) <= (unsigned long) port->support.muldiv)
1068 /* if this is a division then the remainder should be zero
1069 for it to be inducted */
1070 if (ic->op == '/' && (ip->cval % litVal))
1073 /* create the iCode to be placed in the loop header */
1074 /* and create the induction object */
1076 /* create an instruction */
1077 /* this will be put on the loop header */
1078 indIc = newiCode (ic->op,
1079 operandFromOperand (aSym),
1080 operandFromLit (litVal));
1081 indIc->lineno = ic->lineno;
1082 IC_RESULT (indIc) = operandFromOperand (IC_RESULT (ic));
1083 OP_SYMBOL (IC_RESULT (indIc))->isind = 1;
1085 /* keep track of the inductions */
1086 litVal = (ic->op == '*' ? (litVal * ip->cval) :
1087 (ip->cval / litVal));
1090 newInduction (IC_RESULT (ic), ip->op, litVal, indIc, NULL));
1092 /* Replace mul/div with assignment to self; killDeadCode() will */
1093 /* clean this up since we can't use remiCodeFromeBBlock() here. */
1095 IC_LEFT (ic) = NULL;
1096 IC_RIGHT (ic) = IC_RESULT (ic);
1098 /* Insert an update of the induction variable just before */
1099 /* the update of the basic induction variable. */
1100 indIc = newiCode (ip->op,
1101 operandFromOperand (IC_RESULT (ic)),
1102 operandFromLit (litVal));
1103 IC_RESULT (indIc) = operandFromOperand (IC_RESULT (ic));
1105 dic = findDefInRegion (setFromSet (loopReg->regBlocks), aSym, &owner);
1107 addiCodeToeBBlock (owner, indIc, dic);
1112 /* if we have some induction variables then */
1115 eBBlock *preHdr = loopReg->entry->preHeader;
1116 iCode *icFirst = NULL, *icLast = NULL;
1118 bitVect *indVect = NULL;
1120 /* create an iCode chain from it */
1121 for (ip = setFirstItem (indVars);
1123 ip = setNextItem (indVars))
1126 indVect = bitVectSetBit (indVect, ip->ic->key);
1127 ip->ic->lineno = preHdr->ech->lineno;
1132 icLast->next = ip->ic;
1133 ip->ic->prev = icLast;
1141 /* add the instruction chain to the end of the */
1142 /* preheader for this loop */
1143 preHdr->ech->next = icFirst;
1144 icFirst->prev = preHdr->ech;
1145 preHdr->ech = icLast;
1146 icLast->next = NULL;
1148 /* add the induction variable vector to the last
1149 block in the loop */
1150 lastBlock->isLastInLoop = 1;
1151 lastBlock->linds = bitVectUnion(lastBlock->linds,indVect);
1154 setToNull ((void *) &indVars);
1158 /*-----------------------------------------------------------------*/
1159 /* mergeRegions - will merge region with same entry point */
1160 /*-----------------------------------------------------------------*/
1161 DEFSETFUNC (mergeRegions)
1163 region *theLoop = item;
1164 V_ARG (set *, allRegion);
1167 /* if this has already been merged then do nothing */
1168 if (theLoop->merged)
1171 /* go thru all the region and check if any of them have the */
1172 /* entryPoint as the Loop */
1173 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1179 if (lp->entry == theLoop->entry)
1181 theLoop->regBlocks = unionSets (theLoop->regBlocks,
1182 lp->regBlocks, THROW_DEST);
1190 /*-----------------------------------------------------------------*/
1191 /* ifMerged - return 1 if the merge flag is 1 */
1192 /*-----------------------------------------------------------------*/
1193 DEFSETFUNC (ifMerged)
1200 /*-----------------------------------------------------------------*/
1201 /* mergeInnerLoops - will merge into body when entry is present */
1202 /*-----------------------------------------------------------------*/
1203 DEFSETFUNC (mergeInnerLoops)
1205 region *theLoop = item;
1206 V_ARG (set *, allRegion);
1207 V_ARG (int *, maxDepth);
1210 /* check if the entry point is present in the body of any */
1211 /* loop then put the body of this loop into the outer loop */
1212 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1218 if (isinSet (lp->regBlocks, theLoop->entry))
1220 lp->containsLoops += theLoop->containsLoops + 1;
1221 if (lp->containsLoops > (*maxDepth))
1222 *maxDepth = lp->containsLoops;
1224 lp->regBlocks = unionSets (lp->regBlocks,
1225 theLoop->regBlocks, THROW_DEST);
1233 /*-----------------------------------------------------------------*/
1234 /* createLoopRegions - will detect and create a set of natural loops */
1235 /*-----------------------------------------------------------------*/
1237 createLoopRegions (ebbIndex * ebbi)
1239 set *allRegion = NULL; /* set of all loops */
1240 hTab *orderedLoops = NULL;
1245 /* get all the back edges in the graph */
1246 if (!applyToSet (graphEdges, backEdges, &bEdges))
1247 return 0; /* found no loops */
1249 /* for each of these back edges get the blocks that */
1250 /* constitute the loops */
1251 applyToSet (bEdges, createLoop, &allRegion);
1253 /* now we will create regions from these loops */
1254 /* loops with the same entry points are considered to be the */
1255 /* same loop & they are merged. If the entry point of a loop */
1256 /* is found in the body of another loop then , all the blocks */
1257 /* in that loop are added to the loops containing the header */
1258 applyToSet (allRegion, mergeRegions, allRegion);
1260 /* delete those already merged */
1261 deleteItemIf (&allRegion, ifMerged);
1263 applyToSet (allRegion, mergeInnerLoops, allRegion, &maxDepth);
1266 /* now create all the exits .. also */
1267 /* create an ordered set of loops */
1268 /* i.e. we process loops in the inner to outer order */
1269 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1271 applyToSet (lp->regBlocks, addToExitsMarkDepth,
1272 lp->regBlocks, &lp->exits,
1273 (maxDepth - lp->containsLoops), lp);
1275 hTabAddItem (&orderedLoops, lp->containsLoops, lp);
1278 return orderedLoops;
1281 /*-----------------------------------------------------------------*/
1282 /* loopOptimizations - identify region & remove invariants & ind */
1283 /*-----------------------------------------------------------------*/
1285 loopOptimizations (hTab * orderedLoops, ebbIndex * ebbi)
1291 /* if no loop optimizations requested */
1292 if (!optimize.loopInvariant &&
1293 !optimize.loopInduction)
1296 /* now we process the loops inner to outer order */
1297 /* this is essential to maintain data flow information */
1298 /* the other choice is an ugly iteration for the depth */
1299 /* of the loops would hate that */
1300 for (lp = hTabFirstItem (orderedLoops, &k); lp;
1301 lp = hTabNextItem (orderedLoops, &k))
1304 if (optimize.loopInvariant)
1305 change += loopInvariants (lp, ebbi);
1307 if (optimize.loopInduction)
1308 change += loopInduction (lp, ebbi);