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_calloc (1, sizeof (induction));
53 /*-----------------------------------------------------------------*/
54 /* newRegion - allocate & returns a loop structure */
55 /*-----------------------------------------------------------------*/
61 lp = Safe_calloc (1, 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 */
237 /* put the loop region info in the block */
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 (eBBlock **, ebbs);
308 addSetHead (&ebp->inExprs, cdp);
309 cseBBlock (ebp, 0, ebbs, count);
313 /*-----------------------------------------------------------------*/
314 /* assignmentsToSym - for a set of blocks determine # time assigned */
315 /*-----------------------------------------------------------------*/
317 assignmentsToSym (set * sset, operand * sym)
321 set *blocks = setFromSet (sset);
323 for (ebp = setFirstItem (blocks); ebp;
324 ebp = setNextItem (blocks))
327 /* get all the definitions for this symbol
329 bitVect *defs = bitVectIntersect (ebp->ldefs, OP_DEFS (sym));
330 assigns += bitVectnBitsOn (defs);
331 setToNull ((void **) &defs);
339 /*-----------------------------------------------------------------*/
340 /* isOperandInvariant - determines if an operand is an invariant */
341 /*-----------------------------------------------------------------*/
343 isOperandInvariant (operand * op, region * theLoop, set * lInvars)
346 /* operand is an invariant if it is a */
348 /* b. that have defintions reaching loop entry */
349 /* c. that are already defined as invariant */
350 /* d. has no assignments in the loop */
353 if (IS_OP_LITERAL (op))
355 else if (IS_SYMOP (op) &&
356 OP_SYMBOL (op)->addrtaken)
358 else if (ifDefSymIs (theLoop->entry->inExprs, op))
360 else if (ifDefSymIs (lInvars, op))
362 else if (IS_SYMOP (op) &&
363 !IS_OP_GLOBAL (op) &&
364 !IS_OP_VOLATILE (op) &&
365 assignmentsToSym (theLoop->regBlocks, op) == 0)
374 /*-----------------------------------------------------------------*/
375 /* pointerAssigned - will return 1 if pointer set found */
376 /*-----------------------------------------------------------------*/
377 DEFSETFUNC (pointerAssigned)
380 V_ARG (operand *, op);
382 return ebp->hasFcall || bitVectBitValue (ebp->ptrsSet, op->key);
385 /*-----------------------------------------------------------------*/
386 /* hasNonPtrUse - returns true if operand has non pointer usage */
387 /*-----------------------------------------------------------------*/
388 DEFSETFUNC (hasNonPtrUse)
391 V_ARG (operand *, op);
392 iCode *ic = usedInRemaining (op, ebp->sch);
394 if (ic && !POINTER_SET (ic) && !POINTER_GET (ic))
401 /*-----------------------------------------------------------------*/
402 /* loopInvariants - takes loop invariants out of region */
403 /*-----------------------------------------------------------------*/
405 loopInvariants (region * theLoop, eBBlock ** ebbs, int count)
413 /* if the preHeader does not exist then do nothing */
414 /* or no exits then do nothing ( have to think about this situation */
415 if (theLoop->entry->preHeader == NULL ||
416 theLoop->exits == NULL)
419 /* we will do the elimination for those blocks */
420 /* in the loop that dominates all exits from the loop */
421 for (lBlock = setFirstItem (theLoop->regBlocks); lBlock;
422 lBlock = setNextItem (theLoop->regBlocks))
429 /* mark the dominates all exits flag */
430 domsAllExits = (applyToSet (theLoop->exits, dominatedBy, lBlock) ==
431 elementsInSet (theLoop->exits));
433 /* find out if we have a function call in this block */
434 for (ic = lBlock->sch, fCallsInBlock=0; ic; ic = ic->next) {
440 /* now we go thru the instructions of this block and */
441 /* collect those instructions with invariant operands */
442 for (ic = lBlock->sch; ic; ic = ic->next)
448 /* if there are function calls in this block and this
449 is a pointer get, the function could have changed it
450 so skip, ISO-C99 according to David A. Long */
451 if (fCallsInBlock && POINTER_GET(ic)) {
455 if (SKIP_IC (ic) || POINTER_SET (ic) || ic->op == IFX)
458 /* if result is volatile then skip */
459 if (IC_RESULT (ic) &&
460 (isOperandVolatile (IC_RESULT (ic), TRUE) ||
461 IS_OP_PARM (IC_RESULT (ic))))
464 /* if result depends on a volatile then skip */
465 if ((IC_LEFT(ic) && isOperandVolatile(IC_LEFT(ic), TRUE)) ||
466 (IC_RIGHT(ic) && isOperandVolatile(IC_RIGHT(ic), TRUE)))
472 /* if address of then it is an invariant */
473 if (ic->op == ADDRESS_OF &&
474 IS_SYMOP (IC_LEFT (ic)) &&
475 IS_AGGREGATE (operandType (IC_LEFT (ic))))
478 /* check if left operand is an invariant */
479 if ((lin = isOperandInvariant (IC_LEFT (ic), theLoop, lInvars)))
480 /* if this is a pointer get then make sure
481 that the pointer set does not exist in
483 if (POINTER_GET (ic) &&
484 (applyToSet (theLoop->regBlocks,
485 pointerAssigned, IC_LEFT (ic))))
489 /* do the same for right */
490 rin = isOperandInvariant (IC_RIGHT (ic), theLoop, lInvars);
492 /* if this is a POINTER_GET then special case, make sure all
493 usages within the loop are POINTER_GET any other usage
494 would mean that this is not an invariant , since the pointer
495 could then be passed as a parameter */
496 if (POINTER_GET (ic) &&
497 applyToSet (theLoop->regBlocks, hasNonPtrUse, IC_LEFT (ic)))
500 /* if both the left & right are invariants : then check that */
501 /* this definition exists in the out definition of all the */
502 /* blocks, this will ensure that this is not assigned any */
503 /* other value in the loop , and not used in this block */
504 /* prior to this definition which means only this definition */
505 /* is used in this loop */
506 if (lin && rin && IC_RESULT (ic))
509 set *lSet = setFromSet (theLoop->regBlocks);
511 /* if this block does not dominate all exists */
512 /* make sure this defintion is not used anywhere else */
516 if (isOperandGlobal (IC_RESULT (ic)))
518 /* for successors for all exits */
519 for (sBlock = setFirstItem (theLoop->exits); sBlock;
520 sBlock = setNextItem (theLoop->exits))
523 for (i = 0; i < count; ebbs[i++]->visited = 0);
525 if (applyToSet (sBlock->succList, isDefAlive, ic))
529 /* we have found usage */
534 /* now make sure this is the only definition */
535 for (sBlock = setFirstItem (lSet); sBlock;
536 sBlock = setNextItem (lSet))
538 /* if this is the block make sure the definition */
539 /* reaches the end of the block */
540 if (sBlock == lBlock)
542 if (!ifDiCodeIs (sBlock->outExprs, ic))
545 else if (bitVectBitsInCommon (sBlock->defSet, OP_DEFS (IC_RESULT (ic))))
550 continue; /* another definition present in the block */
552 /* now check if it exists in the in of this block */
553 /* if not then it was killed before this instruction */
554 if (!bitVectBitValue (lBlock->inDefs, ic->key))
557 /* now we know it is a true invariant */
558 /* remove it from the insts chain & put */
559 /* in the invariant set */
560 OP_SYMBOL (IC_RESULT (ic))->isinvariant = 1;
561 remiCodeFromeBBlock (lBlock, ic);
563 /* maintain the data flow */
564 /* this means removing from definition from the */
565 /* defset of this block and adding it to the */
566 /* inexpressions of all blocks within the loop */
567 bitVectUnSetBit (lBlock->defSet, ic->key);
568 bitVectUnSetBit (lBlock->ldefs, ic->key);
569 ivar = newCseDef (IC_RESULT (ic), ic);
570 applyToSet (theLoop->regBlocks, addDefInExprs, ivar, ebbs, count);
571 addSet (&lInvars, ivar);
574 } /* for all loop blocks */
576 /* if we have some invariants then */
579 eBBlock *preHdr = theLoop->entry->preHeader;
580 iCode *icFirst = NULL, *icLast = NULL;
583 /* create an iCode chain from it */
584 for (cdp = setFirstItem (lInvars); cdp; cdp = setNextItem (lInvars))
587 /* maintain data flow .. add it to the */
588 /* ldefs defSet & outExprs of the preheader */
589 preHdr->defSet = bitVectSetBit (preHdr->defSet, cdp->diCode->key);
590 preHdr->ldefs = bitVectSetBit (preHdr->ldefs, cdp->diCode->key);
591 cdp->diCode->lineno = preHdr->ech->lineno;
592 addSetHead (&preHdr->outExprs, cdp);
596 icFirst = cdp->diCode;
599 icLast->next = cdp->diCode;
600 cdp->diCode->prev = icLast;
601 icLast = cdp->diCode;
604 icLast = cdp->diCode;
608 /* add the instruction chain to the end of the
609 preheader for this loop, preheaders will always
610 have atleast a label */
611 preHdr->ech->next = icFirst;
612 icFirst->prev = preHdr->ech;
613 preHdr->ech = icLast;
620 /*-----------------------------------------------------------------*/
621 /* addressTaken - returns true if the symbol is found in the addrof */
622 /*-----------------------------------------------------------------*/
624 addressTaken (set * sset, operand * sym)
630 for (loop = sset; loop; loop = loop->next)
636 if (isOperandEqual ((operand *) loop2->item, sym))
646 /*-----------------------------------------------------------------*/
647 /* findInduction :- returns 1 & the item if the induction is found */
648 /*-----------------------------------------------------------------*/
649 DEFSETFUNC (findInduction)
651 induction *ip = item;
652 V_ARG (operand *, sym);
653 V_ARG (induction **, ipp);
655 if (isOperandEqual (ip->sym, sym))
664 /*-----------------------------------------------------------------*/
665 /* findDefInRegion - finds the definition within the region */
666 /*-----------------------------------------------------------------*/
668 findDefInRegion (set * regBlocks, operand * defOp, eBBlock ** owner)
672 /* for all blocks in the region */
673 for (lBlock = setFirstItem (regBlocks); lBlock;
674 lBlock = setNextItem (regBlocks))
677 /* if a definition for this exists */
678 if (bitVectBitsInCommon (lBlock->defSet, OP_DEFS (defOp)))
682 /* go thru the instruction chain to find it */
683 for (ic = lBlock->sch; ic; ic = ic->next)
684 if (bitVectBitValue (OP_DEFS (defOp), ic->key))
696 /*-----------------------------------------------------------------*/
697 /* basicInduction - finds the basic induction variables in a loop */
698 /*-----------------------------------------------------------------*/
700 basicInduction (region * loopReg, eBBlock ** ebbs, int count)
705 /* i.e. all assignments of the form a := a +/- const */
706 /* for all blocks within the loop do */
707 for (lBlock = setFirstItem (loopReg->regBlocks); lBlock;
708 lBlock = setNextItem (loopReg->regBlocks))
713 /* for all instructions in the blocks do */
714 for (ic = lBlock->sch; ic; ic = ic->next)
718 unsigned long litValue;
721 eBBlock *owner = NULL;
724 /* look for assignments of the form */
725 /* symbolVar := iTempNN */
729 if (!IS_TRUE_SYMOP (IC_RESULT (ic)) &&
730 !OP_SYMBOL (IC_RESULT (ic))->isreqv)
733 if (isOperandGlobal (IC_RESULT (ic)))
736 if (!IS_ITEMP (IC_RIGHT (ic)))
739 /* if it has multiple assignments within the loop then skip */
740 if (assignmentsToSym (loopReg->regBlocks, IC_RESULT (ic)) > 1)
743 /* if the address of this was taken inside the loop then continue */
744 if (addressTaken (loopReg->regBlocks, IC_RESULT (ic)))
747 /* find the definition for the result in the block */
748 if (!(dic = findDefInRegion (setFromSet (loopReg->regBlocks),
749 IC_RIGHT (ic), &owner)))
752 /* if not +/- continue */
753 if (dic->op != '+' && dic->op != '-')
756 /* make sure definition is of the form a +/- c */
757 if (!IS_OP_LITERAL (IC_LEFT (dic)) && !IS_OP_LITERAL (IC_RIGHT (dic)))
760 aSym = (IS_OP_LITERAL (IC_RIGHT (dic)) ?
761 (litValue = (unsigned long) operandLitValue (IC_RIGHT (dic)), IC_LEFT (dic)) :
762 (litValue = (unsigned long) operandLitValue (IC_LEFT (dic)), IC_RIGHT (dic)));
764 if (!isOperandEqual (IC_RESULT (ic), aSym) &&
765 !isOperandEqual (IC_RIGHT (ic), aSym))
768 /* find the definition for this and check */
769 if (!(ddic = findDefInRegion (setFromSet (loopReg->regBlocks),
776 if (!isOperandEqual (IC_RESULT (ddic), aSym) ||
777 !isOperandEqual (IC_RIGHT (ddic), IC_RESULT (ic)))
781 /* if the right hand side has more than one usage then
782 don't make it an induction (will have to think some more) */
783 if (bitVectnBitsOn (OP_USES (IC_RIGHT (ic))) > 1)
786 /* if the definition is volatile then it cannot be
787 an induction object */
788 if (isOperandVolatile (IC_RIGHT (ic), FALSE) ||
789 isOperandVolatile (IC_RESULT (ic), FALSE))
792 /* whew !! that was a lot of work to find the definition */
793 /* create an induction object */
794 indIc = newiCode ('=', NULL, IC_RESULT (ic));
795 indIc->lineno = ic->lineno;
796 IC_RESULT (indIc) = operandFromOperand (IC_RIGHT (ic));
797 IC_RESULT (indIc)->isaddr = 0;
798 OP_SYMBOL (IC_RESULT (indIc))->isind = 1;
799 ip = newInduction (IC_RIGHT (ic), dic->op, litValue, indIc, NULL);
801 /* replace the inducted variable by the iTemp */
802 replaceSymBySym (loopReg->regBlocks, IC_RESULT (ic), IC_RIGHT (ic));
804 /* if it has only one exit then remove it from here
805 and put it in the exit block */
806 nexits = elementsInSet (loopReg->exits);
809 eBBlock *exit = setFirstItem (loopReg->exits);
811 /* if it is the same block then there is no
812 need to move it about */
815 iCode *saveic = ic->prev;
817 remiCodeFromeBBlock (lBlock, ic);
818 /* clear the definition */
819 bitVectUnSetBit (lBlock->defSet, ic->key);
820 /* add it to the exit */
821 addiCodeToeBBlock (exit, ic, NULL);
822 /* set the definition bit */
823 exit->defSet = bitVectSetBit (exit->defSet, ic->key);
828 /* if the number of exits is greater than one then
829 we use another trick ; we will create an intersection
830 of succesors of the exits, then take those that are not
831 part of the loop and have dfNumber greater loop entry
832 and insert a new definition in them */
836 bitVect *loopSuccs = intersectLoopSucc (loopReg->exits, ebbs);
838 /* loopSuccs now contains intersection
839 of all the loops successors */
843 for (i = 0; i < loopSuccs->size; i++)
845 if (bitVectBitValue (loopSuccs, i))
848 eBBlock *eblock = ebbs[i];
850 /* if the successor does not belong to the loop
851 and will be executed after the loop : then
852 add a definition to the block */
853 if (!isinSet (loopReg->regBlocks, eblock) &&
854 eblock->dfnum > loopReg->entry->dfnum)
856 /* create the definition */
857 iCode *newic = newiCode ('=', NULL,
858 operandFromOperand (IC_RIGHT (ic)));
859 IC_RESULT (newic) = operandFromOperand (IC_RESULT (ic));
860 OP_DEFS (IC_RESULT (newic)) =
861 bitVectSetBit (OP_DEFS (IC_RESULT (newic)), newic->key);
862 OP_USES (IC_RIGHT (newic)) =
863 bitVectSetBit (OP_USES (IC_RIGHT (newic)), newic->key);
865 if (eblock->sch && eblock->sch->op == LABEL)
866 addiCodeToeBBlock (eblock, newic, eblock->sch->next);
868 addiCodeToeBBlock (eblock, newic, eblock->sch);
869 /* set the definition bit */
870 eblock->defSet = bitVectSetBit (eblock->defSet, ic->key);
877 addSet (&indVars, ip);
880 } /* end of all blocks for basic induction variables */
885 /*-----------------------------------------------------------------*/
886 /* loopInduction - remove induction variables from a loop */
887 /*-----------------------------------------------------------------*/
889 loopInduction (region * loopReg, eBBlock ** ebbs, int count)
892 eBBlock *lBlock, *lastBlock = NULL;
894 set *basicInd = NULL;
896 if (loopReg->entry->preHeader == NULL)
899 /* we first determine the basic Induction variables */
900 basicInd = setFromSet (indVars = basicInduction (loopReg, ebbs, count));
902 /* find other induction variables : by other we mean definitions of */
903 /* the form x := y (* | / ) <constant> .. we will move this one to */
904 /* beginning of the loop and reduce strength i.e. replace with +/- */
905 /* these expensive expressions: OH! and y must be induction too */
906 for (lBlock = setFirstItem (loopReg->regBlocks), lastBlock = lBlock;
908 lBlock = setNextItem (loopReg->regBlocks))
914 /* last block is the one with the highest block
916 if (lastBlock->bbnum < lBlock->bbnum)
919 for (ic = lBlock->sch; ic; ic = ic->next)
922 unsigned long litVal;
925 /* consider only * & / */
926 if (ic->op != '*' && ic->op != '/')
929 /* if the result has more definitions then */
930 if (assignmentsToSym (loopReg->regBlocks, IC_RESULT (ic)) > 1)
933 /* check if the operands are what we want */
934 /* i.e. one of them an symbol the other a literal */
935 if (!((IS_SYMOP (IC_LEFT (ic)) && IS_OP_LITERAL (IC_RIGHT (ic))) ||
936 (IS_OP_LITERAL (IC_LEFT (ic)) && IS_SYMOP (IC_RIGHT (ic)))))
939 aSym = (IS_SYMOP (IC_LEFT (ic)) ?
940 (lr = 1, litVal = (unsigned long) operandLitValue (IC_RIGHT (ic)), IC_LEFT (ic)) :
941 (litVal = (unsigned long) operandLitValue (IC_LEFT (ic)), IC_RIGHT (ic)));
944 /* check if this is an induction variable */
945 if (!applyToSetFTrue (basicInd, findInduction, aSym, &ip))
948 /* ask port for size not worth if native instruction
949 exist for multiply & divide */
950 if (getSize (operandType (IC_LEFT (ic))) <= (unsigned long) port->support.muldiv ||
951 getSize (operandType (IC_RIGHT (ic))) <= (unsigned long) port->support.muldiv)
954 /* if this is a division then the remainder should be zero
955 for it to be inducted */
956 if (ic->op == '/' && (ip->cval % litVal))
959 /* create the iCode to be placed in the loop header */
960 /* and create the induction object */
962 /* create an instruction */
963 /* this will be put on the loop header */
964 indIc = newiCode (ic->op,
965 operandFromOperand (aSym),
966 operandFromLit (litVal));
967 indIc->lineno = ic->lineno;
968 IC_RESULT (indIc) = operandFromOperand (IC_RESULT (ic));
969 OP_SYMBOL (IC_RESULT (indIc))->isind = 1;
971 /* keep track of the inductions */
972 litVal = (ic->op == '*' ? (litVal * ip->cval) :
973 (ip->cval / litVal));
976 newInduction (IC_RESULT (ic), ip->op, litVal, indIc, NULL));
978 /* now change this instruction */
982 IC_LEFT (ic) = operandFromOperand (IC_RESULT (ic));
983 IC_RIGHT (ic) = operandFromLit (litVal);
987 IC_RIGHT (ic) = operandFromOperand (IC_RESULT (ic));
988 IC_LEFT (ic) = operandFromLit (litVal);
991 /* we need somemore initialisation code */
992 /* we subtract the litVal from itself if increment */
995 indIc = newiCode ('-',
996 operandFromOperand (IC_RESULT (ic)),
997 operandFromLit (litVal));
998 indIc->lineno = ic->lineno;
999 IC_RESULT (indIc) = operandFromOperand (IC_RESULT (ic));
1002 newInduction (IC_RESULT (ic), ip->op, litVal, indIc, NULL));
1007 /* if we have some induction variables then */
1010 eBBlock *preHdr = loopReg->entry->preHeader;
1011 iCode *icFirst = NULL, *icLast = NULL;
1013 bitVect *indVect = NULL;
1015 /* create an iCode chain from it */
1016 for (ip = setFirstItem (indVars);
1018 ip = setNextItem (indVars))
1021 indVect = bitVectSetBit (indVect, ip->ic->key);
1022 ip->ic->lineno = preHdr->ech->lineno;
1027 icLast->next = ip->ic;
1028 ip->ic->prev = icLast;
1036 /* add the instruction chain to the end of the */
1037 /* preheader for this loop */
1038 preHdr->ech->next = icFirst;
1039 icFirst->prev = preHdr->ech;
1040 preHdr->ech = icLast;
1041 icLast->next = NULL;
1043 /* add the induction variable vector to the last
1044 block in the loop */
1045 lastBlock->isLastInLoop = 1;
1046 lastBlock->linds = indVect;
1049 setToNull ((void **) &indVars);
1053 /*-----------------------------------------------------------------*/
1054 /* mergeRegions - will merge region with same entry point */
1055 /*-----------------------------------------------------------------*/
1056 DEFSETFUNC (mergeRegions)
1058 region *theLoop = item;
1059 V_ARG (set *, allRegion);
1062 /* if this has already been merged then do nothing */
1063 if (theLoop->merged)
1066 /* go thru all the region and check if any of them have the */
1067 /* entryPoint as the Loop */
1068 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1074 if (lp->entry == theLoop->entry)
1076 theLoop->regBlocks = unionSets (theLoop->regBlocks,
1077 lp->regBlocks, THROW_BOTH);
1085 /*-----------------------------------------------------------------*/
1086 /* ifMerged - return 1 if the merge flag is 1 */
1087 /*-----------------------------------------------------------------*/
1088 DEFSETFUNC (ifMerged)
1095 /*-----------------------------------------------------------------*/
1096 /* mergeInnerLoops - will merge into body when entry is present */
1097 /*-----------------------------------------------------------------*/
1098 DEFSETFUNC (mergeInnerLoops)
1100 region *theLoop = item;
1101 V_ARG (set *, allRegion);
1102 V_ARG (int *, maxDepth);
1105 /* check if the entry point is present in the body of any */
1106 /* loop then put the body of this loop into the outer loop */
1107 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1113 if (isinSet (lp->regBlocks, theLoop->entry))
1115 lp->containsLoops += theLoop->containsLoops + 1;
1116 if (lp->containsLoops > (*maxDepth))
1117 *maxDepth = lp->containsLoops;
1119 lp->regBlocks = unionSets (lp->regBlocks,
1120 theLoop->regBlocks, THROW_DEST);
1128 /*-----------------------------------------------------------------*/
1129 /* createLoopRegions - will detect and create a set of natural loops */
1130 /*-----------------------------------------------------------------*/
1132 createLoopRegions (eBBlock ** ebbs, int count)
1134 set *allRegion = NULL; /* set of all loops */
1135 hTab *orderedLoops = NULL;
1140 /* get all the back edges in the graph */
1141 if (!applyToSet (graphEdges, backEdges, &bEdges))
1142 return 0; /* found no loops */
1144 /* for each of these back edges get the blocks that */
1145 /* constitute the loops */
1146 applyToSet (bEdges, createLoop, &allRegion);
1148 /* now we will create regions from these loops */
1149 /* loops with the same entry points are considered to be the */
1150 /* same loop & they are merged. If the entry point of a loop */
1151 /* is found in the body of another loop then , all the blocks */
1152 /* in that loop are added to the loops containing the header */
1153 applyToSet (allRegion, mergeRegions, allRegion);
1155 /* delete those already merged */
1156 deleteItemIf (&allRegion, ifMerged);
1158 applyToSet (allRegion, mergeInnerLoops, allRegion, &maxDepth);
1160 /* now create all the exits .. also */
1161 /* create an ordered set of loops */
1162 /* i.e. we process loops in the inner to outer order */
1163 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1165 applyToSet (lp->regBlocks, addToExitsMarkDepth,
1166 lp->regBlocks, &lp->exits,
1167 (maxDepth - lp->containsLoops), lp);
1169 hTabAddItem (&orderedLoops, lp->containsLoops, lp);
1172 return orderedLoops;
1175 /*-----------------------------------------------------------------*/
1176 /* loopOptimizations - identify region & remove invariants & ind */
1177 /*-----------------------------------------------------------------*/
1179 loopOptimizations (hTab * orderedLoops, eBBlock ** ebbs, int count)
1185 /* if no loop optimizations requested */
1186 if (!optimize.loopInvariant &&
1187 !optimize.loopInduction)
1190 /* now we process the loops inner to outer order */
1191 /* this is essential to maintain data flow information */
1192 /* the other choice is an ugly iteration for the depth */
1193 /* of the loops would hate that */
1194 for (lp = hTabFirstItem (orderedLoops, &k); lp;
1195 lp = hTabNextItem (orderedLoops, &k))
1198 if (optimize.loopInvariant)
1199 change += loopInvariants (lp, ebbs, count);
1201 if (optimize.loopInduction)
1202 change += loopInduction (lp, ebbs, count);