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 /*-----------------------------------------------------------------*/
36 induction *newInduction (operand *sym, unsigned int op,
37 long constVal, iCode *ic, operand *asym)
41 ip = Safe_calloc(sizeof(induction));
52 /*-----------------------------------------------------------------*/
53 /* newRegion - allocate & returns a loop structure */
54 /*-----------------------------------------------------------------*/
59 lp = Safe_calloc(sizeof(region));
65 /*-----------------------------------------------------------------*/
66 /* pinduction - prints induction */
67 /*-----------------------------------------------------------------*/
68 DEFSETFUNC(pinduction)
74 printOperand(ip->sym,stdout);
75 icTab = getTableEntry(ip->ic->op);
76 icTab->iCodePrint(stdout,ip->ic,icTab->printName);
77 fprintf(stdout," %04d\n",(int)ip->cval);
81 /*-----------------------------------------------------------------*/
82 /* pregion - prints loop information */
83 /*-----------------------------------------------------------------*/
88 printf("================\n");
89 printf(" loop with entry -- > "); printEntryLabel(lp->entry,ap);
91 printf(" loop body --> "); applyToSet(lp->regBlocks,printEntryLabel);
93 printf(" loop exits --> "); applyToSet(lp->exits,printEntryLabel);
98 /*-----------------------------------------------------------------*/
99 /* backEdges - returns a list of back edges */
100 /*-----------------------------------------------------------------*/
101 DEFSETFUNC(backEdges)
104 V_ARG(set **,bEdges);
106 /* if this is a back edge ; to determine this we check */
107 /* to see if the 'to' is in the dominator list of the */
108 /* 'from' if yes then this is a back edge */
109 if (bitVectBitValue (ep->from->domVect,ep->to->bbnum)) {
110 addSetHead (bEdges,ep);
117 /*-----------------------------------------------------------------*/
118 /* intersectLoopSucc - returns intersection of loop Successors */
119 /*-----------------------------------------------------------------*/
120 static bitVect *intersectLoopSucc ( set *lexits, eBBlock **ebbs)
122 bitVect *succVect = NULL;
123 eBBlock *exit = setFirstItem(lexits);
128 succVect = bitVectCopy(exit->succVect);
130 for (exit = setNextItem(lexits); exit ;
131 exit = setNextItem(lexits)) {
132 succVect = bitVectIntersect(succVect,
140 /*-----------------------------------------------------------------*/
141 /* loopInsert will insert a block into the loop set */
142 /*-----------------------------------------------------------------*/
143 static void loopInsert (set **regionSet, eBBlock *block)
145 if (!isinSet (*regionSet,block)) {
146 addSetHead (regionSet,block);
147 STACK_PUSH(regionStack,block);
151 /*-----------------------------------------------------------------*/
152 /* insertIntoLoop - insert item into loop */
153 /*-----------------------------------------------------------------*/
154 DEFSETFUNC(insertIntoLoop)
156 eBBlock *ebp = item ;
157 V_ARG(set **,regionSet);
159 loopInsert (regionSet,ebp);
163 /*-----------------------------------------------------------------*/
164 /* isNotInBlocks - will return 1 if not is blocks */
165 /*-----------------------------------------------------------------*/
166 DEFSETFUNC(isNotInBlocks)
171 if (! isinSet (blocks,ebp))
177 /*-----------------------------------------------------------------*/
178 /* hasIncomingDefs - has definitions coming into the loop. i.e. */
179 /* check to see if the preheaders outDefs has any definitions */
180 /*-----------------------------------------------------------------*/
181 int hasIncomingDefs (region *lreg, operand *op)
183 eBBlock *preHdr = lreg->entry->preHeader;
185 if (preHdr && bitVectBitsInCommon(preHdr->outDefs,OP_DEFS(op)))
190 /*-----------------------------------------------------------------*/
191 /* findLoopEndSeq - will return the sequence number of the last */
192 /* iCode with the maximum dfNumber in the region */
193 /*-----------------------------------------------------------------*/
194 int findLoopEndSeq (region *lreg)
199 for (block = lblock =setFirstItem(lreg->regBlocks); block;
200 block = setNextItem(lreg->regBlocks)) {
201 if (block != lblock && block->lSeq > lblock->lSeq)
208 /*-----------------------------------------------------------------*/
209 /* addToExitsMarkDepth - will add the the exitSet all blocks that */
210 /* have exits, will also update the depth field in the blocks */
211 /*-----------------------------------------------------------------*/
212 DEFSETFUNC(addToExitsMarkDepth)
214 eBBlock *ebp = item ;
215 V_ARG(set *,loopBlocks);
220 /* mark the loop depth of this block */
224 /* put the loop region info in the block */
225 /* NOTE: here we will update only the inner most loop
226 that it is a part of */
227 if (!ebp->partOfLoop)
228 ebp->partOfLoop = lr;
230 /* if any of the successors go out of the loop then */
231 /* we add this one to the exits */
232 if ( applyToSet(ebp->succList,isNotInBlocks,loopBlocks)) {
233 addSetHead (exits,ebp);
240 /*-----------------------------------------------------------------*/
241 /* createLoop - will create a set of region */
242 /*-----------------------------------------------------------------*/
243 DEFSETFUNC(createLoop)
246 V_ARG(set **,allRegion);
247 region *aloop = newRegion();
250 /* make sure regionStack is empty */
251 while (!STACK_EMPTY(regionStack))
252 STACK_POP(regionStack);
254 /* add the entryBlock */
255 addSet (&aloop->regBlocks,ep->to);
256 loopInsert (&aloop->regBlocks,ep->from);
258 while (!STACK_EMPTY(regionStack)) {
259 block = STACK_POP(regionStack);
260 /* if block != entry */
262 applyToSet(block->predList,insertIntoLoop,&aloop->regBlocks);
265 aloop->entry = ep->to ;
267 /* now add it to the set */
268 addSetHead (allRegion,aloop);
272 /*-----------------------------------------------------------------*/
273 /* dominatedBy - will return 1 if item is dominated by block */
274 /*-----------------------------------------------------------------*/
275 DEFSETFUNC(dominatedBy)
278 V_ARG(eBBlock *,block);
280 return bitVectBitValue (ebp->domVect,block->bbnum);
283 /*-----------------------------------------------------------------*/
284 /* addDefInExprs - adds an expression into the inexpressions */
285 /*-----------------------------------------------------------------*/
286 DEFSETFUNC(addDefInExprs)
290 V_ARG(eBBlock **,ebbs);
293 addSetHead(&ebp->inExprs,cdp);
294 cseBBlock (ebp,0,ebbs,count);
298 /*-----------------------------------------------------------------*/
299 /* assignmentsToSym - for a set of blocks determine # time assigned*/
300 /*-----------------------------------------------------------------*/
301 int assignmentsToSym (set *sset, operand *sym)
305 set *blocks = setFromSet(sset);
307 for (ebp = setFirstItem(blocks) ; ebp ;
308 ebp = setNextItem(blocks)) {
310 /* get all the definitions for this symbol
312 bitVect *defs = bitVectIntersect(ebp->ldefs,OP_DEFS(sym));
313 assigns += bitVectnBitsOn(defs);
314 setToNull((void **)&defs);
322 /*-----------------------------------------------------------------*/
323 /* isOperandInvariant - determines if an operand is an invariant */
324 /*-----------------------------------------------------------------*/
325 int isOperandInvariant (operand *op, region *theLoop, set *lInvars)
328 /* operand is an invariant if it is a */
330 /* b. that have defintions reaching loop entry */
331 /* c. that are already defined as invariant */
332 /* d. has no assignments in the loop */
334 if (IS_OP_LITERAL(op))
338 OP_SYMBOL(op)->addrtaken)
341 if (ifDefSymIs(theLoop->entry->inExprs,op))
344 if (ifDefSymIs(lInvars,op))
348 ! IS_OP_GLOBAL(op) &&
349 ! IS_OP_VOLATILE(op) &&
350 assignmentsToSym (theLoop->regBlocks,op) == 0 )
357 /*-----------------------------------------------------------------*/
358 /* pointerAssigned - will return 1 if pointer set found */
359 /*-----------------------------------------------------------------*/
360 DEFSETFUNC(pointerAssigned)
365 return ebp->hasFcall || bitVectBitValue(ebp->ptrsSet,op->key);
368 /*-----------------------------------------------------------------*/
369 /* hasNonPtrUse - returns true if operand has non pointer usage */
370 /*-----------------------------------------------------------------*/
371 DEFSETFUNC(hasNonPtrUse)
375 iCode *ic = usedInRemaining(op,ebp->sch);
377 if (ic && !POINTER_SET(ic) && !POINTER_GET(ic))
384 /*-----------------------------------------------------------------*/
385 /* loopInvariants - takes loop invariants out of region */
386 /*-----------------------------------------------------------------*/
387 int loopInvariants( region *theLoop , eBBlock **ebbs, int count)
390 set *lInvars = NULL ;
394 /* if the preHeader does not exist then do nothing */
395 /* or no exits then do nothing ( have to think about this situation */
396 if (theLoop->entry->preHeader == NULL ||
397 theLoop->exits == NULL )
400 /* we will do the elimination for those blocks */
401 /* in the loop that dominates all exits from the loop */
402 for (lBlock = setFirstItem(theLoop->regBlocks); lBlock;
403 lBlock = setNextItem(theLoop->regBlocks)) {
409 /* mark the dominates all exits flag */
410 domsAllExits = ( applyToSet (theLoop->exits,dominatedBy,lBlock) ==
411 elementsInSet (theLoop->exits));
414 /* now we go thru the instructions of this block and */
415 /* collect those instructions with invariant operands*/
416 for ( ic = lBlock->sch ; ic ; ic = ic->next ) {
421 if (SKIP_IC(ic) || POINTER_SET(ic) || ic->op == IFX)
424 /* if result is volatile then skip */
426 ( isOperandVolatile(IC_RESULT(ic),TRUE) ||
427 IS_OP_PARM(IC_RESULT(ic))))
433 /* if address of then it is an invariant */
434 if (ic->op == ADDRESS_OF &&
435 IS_SYMOP(IC_LEFT(ic)) &&
436 IS_AGGREGATE(operandType(IC_LEFT(ic))))
439 /* check if left operand is an invariant */
440 if ( (lin = isOperandInvariant (IC_LEFT(ic),theLoop,lInvars)))
441 /* if this is a pointer get then make sure
442 that the pointer set does not exist in
444 if (POINTER_GET(ic) &&
445 ( applyToSet (theLoop->regBlocks,pointerAssigned,IC_LEFT(ic)) ))
448 /* do the same for right */
449 rin = isOperandInvariant (IC_RIGHT(ic),theLoop, lInvars);
451 /* if this is a POINTER_GET then special case, make sure all
452 usages within the loop are POINTER_GET any other usage
453 would mean that this is not an invariant , since the pointer
454 could then be passed as a parameter */
455 if (POINTER_GET(ic) &&
456 applyToSet(theLoop->regBlocks,hasNonPtrUse,IC_LEFT(ic)))
459 /* if both the left & right are invariants : then check that*/
460 /* this definition exists in the out definition of all the */
461 /* blocks, this will ensure that this is not assigned any */
462 /* other value in the loop , and not used in this block */
463 /* prior to this definition which means only this definition*/
464 /* is used in this loop */
465 if (lin && rin && IC_RESULT(ic)) {
467 set *lSet = setFromSet(theLoop->regBlocks);
469 /* if this block does not dominate all exists */
470 /* make sure this defintion is not used anywhere else */
473 if (isOperandGlobal(IC_RESULT(ic)))
475 /* for successors for all exits */
476 for ( sBlock = setFirstItem(theLoop->exits); sBlock;
477 sBlock = setNextItem(theLoop->exits)) {
479 for(i=0; i < count; ebbs[i++]->visited = 0);
481 if ( applyToSet (sBlock->succList,isDefAlive,ic))
485 /* we have found usage */
490 /* now make sure this is the only definition */
491 for (sBlock = setFirstItem(lSet); sBlock ;
492 sBlock = setNextItem (lSet)) {
493 /* if this is the block make sure the definition */
494 /* reaches the end of the block */
495 if (sBlock == lBlock ) {
496 if (! ifDiCodeIs (sBlock->outExprs,ic))
500 if (bitVectBitsInCommon (sBlock->defSet,OP_DEFS(IC_RESULT(ic))))
505 continue ; /* another definition present in the block */
507 /* now check if it exists in the in of this block */
508 /* if not then it was killed before this instruction */
509 if (! bitVectBitValue (lBlock->inDefs,ic->key))
512 /* now we know it is a true invariant */
513 /* remove it from the insts chain & put */
514 /* in the invariant set */
515 OP_SYMBOL(IC_RESULT(ic))->isinvariant = 1;
516 remiCodeFromeBBlock (lBlock,ic);
518 /* maintain the data flow */
519 /* this means removing from definition from the */
520 /* defset of this block and adding it to the */
521 /* inexpressions of all blocks within the loop */
522 bitVectUnSetBit (lBlock->defSet,ic->key);
523 bitVectUnSetBit (lBlock->ldefs,ic->key);
524 ivar = newCseDef(IC_RESULT(ic),ic);
525 applyToSet (theLoop->regBlocks, addDefInExprs, ivar,ebbs,count);
526 addSet(&lInvars,ivar);
529 } /* for all loop blocks */
531 /* if we have some invariants then */
533 eBBlock *preHdr= theLoop->entry->preHeader ;
534 iCode *icFirst = NULL , *icLast = NULL ;
537 /* create an iCode chain from it */
538 for (cdp = setFirstItem(lInvars); cdp ; cdp = setNextItem(lInvars)) {
540 /* maintain data flow .. add it to the */
541 /* ldefs defSet & outExprs of the preheader */
542 preHdr->defSet = bitVectSetBit (preHdr->defSet,cdp->diCode->key);
543 preHdr->ldefs = bitVectSetBit (preHdr->ldefs,cdp->diCode->key);
544 cdp->diCode->lineno = preHdr->ech->lineno;
545 addSetHead (&preHdr->outExprs,cdp);
549 icFirst = cdp->diCode;
551 icLast->next = cdp->diCode;
552 cdp->diCode->prev = icLast;
553 icLast = cdp->diCode ;
555 icLast = cdp->diCode;
559 /* add the instruction chain to the end of the
560 preheader for this loop, preheaders will always
561 have atleast a label */
562 preHdr->ech->next = icFirst ;
563 icFirst->prev = preHdr->ech ;
564 preHdr->ech = icLast;
571 /*-----------------------------------------------------------------*/
572 /* addressTaken - returns true if the symbol is found in the addrof*/
573 /*-----------------------------------------------------------------*/
574 int addressTaken (set *sset ,operand *sym)
580 for (loop = sset; loop ; loop = loop->next) {
582 loop2 = ebp->addrOf ;
584 if (isOperandEqual((operand *)loop2->item,sym))
594 /*-----------------------------------------------------------------*/
595 /* findInduction :- returns 1 & the item if the induction is found */
596 /*-----------------------------------------------------------------*/
597 DEFSETFUNC(findInduction)
599 induction *ip = item;
600 V_ARG(operand *,sym);
601 V_ARG(induction **,ipp);
603 if (isOperandEqual(ip->sym,sym)) {
611 /*-----------------------------------------------------------------*/
612 /* findDefInRegion - finds the definition within the region */
613 /*-----------------------------------------------------------------*/
614 iCode *findDefInRegion (set *regBlocks, operand *defOp, eBBlock **owner)
618 /* for all blocks in the region */
619 for (lBlock = setFirstItem(regBlocks); lBlock ;
620 lBlock = setNextItem(regBlocks)) {
622 /* if a definition for this exists */
623 if (bitVectBitsInCommon(lBlock->defSet,OP_DEFS(defOp))) {
626 /* go thru the instruction chain to find it */
627 for (ic = lBlock->sch ; ic ; ic = ic->next )
628 if (bitVectBitValue (OP_DEFS(defOp),ic->key)) {
639 /*-----------------------------------------------------------------*/
640 /* basicInduction - finds the basic induction variables in a loop */
641 /*-----------------------------------------------------------------*/
642 set *basicInduction (region *loopReg , eBBlock **ebbs, int count)
645 set *indVars = NULL ;
647 /* i.e. all assignments of the form a := a +/- const*/
648 /* for all blocks within the loop do */
649 for ( lBlock = setFirstItem(loopReg->regBlocks); lBlock ;
650 lBlock = setNextItem(loopReg->regBlocks)) {
654 /* for all instructions in the blocks do */
655 for ( ic = lBlock->sch ; ic ; ic = ic->next ) {
658 unsigned long litValue ;
661 eBBlock *owner = NULL;
664 /* look for assignments of the form */
665 /* symbolVar := iTempNN */
669 if (!IS_TRUE_SYMOP(IC_RESULT(ic)) &&
670 !OP_SYMBOL(IC_RESULT(ic))->isreqv)
673 if (isOperandGlobal(IC_RESULT(ic)))
676 if (!IS_ITEMP(IC_RIGHT(ic)))
679 /* if it has multiple assignments within the loop then skip */
680 if (assignmentsToSym (loopReg->regBlocks,IC_RESULT(ic)) > 1 )
683 /* if the address of this was taken inside the loop then continue */
684 if (addressTaken (loopReg->regBlocks,IC_RESULT(ic)))
687 /* find the definition for the result in the block */
688 if (! (dic = findDefInRegion (setFromSet(loopReg->regBlocks),
689 IC_RIGHT(ic),&owner)))
692 /* if not +/- continue */
693 if (dic->op != '+' && dic->op != '-')
696 /* make sure definition is of the form a +/- c */
697 if (!IS_OP_LITERAL(IC_LEFT(dic)) && !IS_OP_LITERAL(IC_RIGHT(dic)))
700 aSym = (IS_OP_LITERAL(IC_RIGHT(dic)) ?
701 (litValue = operandLitValue(IC_RIGHT(dic)),IC_LEFT(dic)) :
702 (litValue = operandLitValue(IC_LEFT(dic)),IC_RIGHT(dic)));
704 if (!isOperandEqual(IC_RESULT(ic),aSym) &&
705 !isOperandEqual(IC_RIGHT(ic),aSym)) {
707 /* find the definition for this and check */
708 if (!(ddic = findDefInRegion (setFromSet(loopReg->regBlocks),
715 if (!isOperandEqual(IC_RESULT(ddic),aSym) ||
716 !isOperandEqual(IC_RIGHT(ddic),IC_RESULT(ic)))
720 /* if the right hand side has more than one usage then
721 don't make it an induction (will have to think some more) */
722 if (bitVectnBitsOn(OP_USES(IC_RIGHT(ic))) > 1)
725 /* if the definition is volatile then it cannot be
726 an induction object */
727 if (isOperandVolatile(IC_RIGHT(ic),FALSE) ||
728 isOperandVolatile(IC_RESULT(ic),FALSE))
731 /* whew !! that was a lot of work to find the definition */
732 /* create an induction object */
733 indIc = newiCode('=',NULL,IC_RESULT(ic));
734 indIc->lineno = ic->lineno;
735 IC_RESULT(indIc) = operandFromOperand(IC_RIGHT(ic));
736 IC_RESULT(indIc)->isaddr = 0;
737 OP_SYMBOL(IC_RESULT(indIc))->isind = 1;
738 ip = newInduction (IC_RIGHT(ic),dic->op,litValue,indIc,NULL);
740 /* replace the inducted variable by the iTemp */
741 replaceSymBySym (loopReg->regBlocks,IC_RESULT(ic),IC_RIGHT(ic));
743 /* if it has only one exit then remove it from here
744 and put it in the exit block */
745 nexits = elementsInSet (loopReg->exits);
747 eBBlock *exit = setFirstItem(loopReg->exits);
749 /* if it is the same block then there is no
750 need to move it about */
751 if ( exit != lBlock) {
752 iCode *saveic = ic->prev;
754 remiCodeFromeBBlock(lBlock,ic);
755 /* clear the definition */
756 bitVectUnSetBit(lBlock->defSet,ic->key);
757 /* add it to the exit */
758 addiCodeToeBBlock(exit,ic,NULL);
759 /* set the definition bit */
760 exit->defSet = bitVectSetBit(exit->defSet,ic->key);
765 /* if the number of exits is greater than one then
766 we use another trick ; we will create an intersection
767 of succesors of the exits, then take those that are not
768 part of the loop and have dfNumber greater loop entry
769 and insert a new definition in them */
772 bitVect *loopSuccs = intersectLoopSucc (loopReg->exits,ebbs);
774 /* loopSuccs now contains intersection
775 of all the loops successors */
778 for (i = 0 ; i < loopSuccs->size; i++) {
779 if (bitVectBitValue(loopSuccs,i)) {
781 eBBlock *eblock = ebbs[i];
783 /* if the successor does not belong to the loop
784 and will be executed after the loop : then
785 add a definition to the block */
786 if ( !isinSet(loopReg->regBlocks,eblock) &&
787 eblock->dfnum > loopReg->entry->dfnum) {
788 /* create the definition */
789 iCode *newic = newiCode('=',NULL,
790 operandFromOperand(IC_RIGHT(ic)));
791 IC_RESULT(newic) = operandFromOperand(IC_RESULT(ic));
792 OP_DEFS(IC_RESULT(newic)) =
793 bitVectSetBit(OP_DEFS(IC_RESULT(newic)),newic->key);
794 OP_USES(IC_RIGHT(newic)) =
795 bitVectSetBit(OP_USES(IC_RIGHT(newic)),newic->key);
797 if (eblock->sch && eblock->sch->op == LABEL)
798 addiCodeToeBBlock(eblock,newic,eblock->sch->next);
800 addiCodeToeBBlock(eblock,newic,eblock->sch);
801 /* set the definition bit */
802 eblock->defSet = bitVectSetBit(eblock->defSet,ic->key);
809 addSet (&indVars,ip);
812 } /* end of all blocks for basic induction variables */
817 /*-----------------------------------------------------------------*/
818 /* loopInduction - remove induction variables from a loop */
819 /*-----------------------------------------------------------------*/
820 int loopInduction( region *loopReg, eBBlock **ebbs, int count)
823 eBBlock *lBlock, *lastBlock = NULL;
824 set *indVars = NULL ;
827 if (loopReg->entry->preHeader == NULL)
830 /* we first determine the basic Induction variables */
831 basicInd = setFromSet(indVars = basicInduction(loopReg, ebbs,count));
833 /* find other induction variables : by other we mean definitions of */
834 /* the form x := y (* | / ) <constant> .. we will move this one to */
835 /* beginning of the loop and reduce strength i.e. replace with +/- */
836 /* these expensive expressions: OH! and y must be induction too */
837 for ( lBlock = setFirstItem(loopReg->regBlocks), lastBlock = lBlock;
839 lBlock = setNextItem(loopReg->regBlocks)) {
844 /* last block is the one with the highest block
846 if (lastBlock->bbnum < lBlock->bbnum )
849 for ( ic = lBlock->sch ; ic ; ic = ic->next ) {
851 unsigned long litVal ;
854 /* consider only * & / */
855 if (ic->op != '*' && ic->op != '/')
858 /* if the result has more definitions then */
859 if (assignmentsToSym(loopReg->regBlocks,IC_RESULT(ic)) > 1)
862 /* check if the operands are what we want */
863 /* i.e. one of them an symbol the other a literal */
864 if (! ( (IS_SYMOP(IC_LEFT(ic)) && IS_OP_LITERAL(IC_RIGHT(ic))) ||
865 (IS_OP_LITERAL(IC_LEFT(ic)) && IS_SYMOP(IC_RIGHT(ic))) ))
868 aSym = (IS_SYMOP(IC_LEFT(ic)) ?
869 (lr = 1, litVal = operandLitValue(IC_RIGHT(ic)), IC_LEFT(ic) ) :
870 (litVal= operandLitValue(IC_LEFT(ic)), IC_RIGHT(ic) ) ) ;
873 /* check if this is an induction variable */
874 if (! applyToSetFTrue (basicInd,findInduction,aSym,&ip))
877 /* ask port for size not worth if native instruction
878 exist for multiply & divide */
879 if (getSize(operandType(IC_LEFT(ic))) <= port->muldiv.native_below ||
880 getSize(operandType(IC_RIGHT(ic))) <= port->muldiv.native_below)
883 /* if this is a division then the remainder should be zero
884 for it to be inducted */
885 if (ic->op == '/' && (ip->cval % litVal))
888 /* create the iCode to be placed in the loop header */
889 /* and create the induction object */
891 /* create an instruction */
892 /* this will be put on the loop header */
893 indIc = newiCode(ic->op,
894 operandFromOperand(aSym),
895 operandFromLit(litVal));
896 indIc->lineno = ic->lineno;
897 IC_RESULT(indIc) = operandFromOperand(IC_RESULT(ic));
898 OP_SYMBOL(IC_RESULT(indIc))->isind = 1;
900 /* keep track of the inductions */
901 litVal = (ic->op == '*' ? (litVal * ip->cval) :
902 (ip->cval / litVal));
905 newInduction (IC_RESULT(ic),ip->op,litVal,indIc,NULL));
907 /* now change this instruction */
910 IC_LEFT(ic) = operandFromOperand(IC_RESULT(ic));
911 IC_RIGHT(ic) = operandFromLit(litVal);
913 IC_RIGHT(ic) = operandFromOperand(IC_RESULT(ic));
914 IC_LEFT(ic) = operandFromLit(litVal);
917 /* we need somemore initialisation code */
918 /* we subtract the litVal from itself if increment */
919 if ( ic->op == '+' ) {
920 indIc = newiCode('-',
921 operandFromOperand(IC_RESULT(ic)),
922 operandFromLit(litVal));
923 indIc->lineno = ic->lineno;
924 IC_RESULT(indIc) = operandFromOperand(IC_RESULT(ic));
927 newInduction (IC_RESULT(ic),ip->op,litVal,indIc,NULL));
932 /* if we have some induction variables then */
934 eBBlock *preHdr = loopReg->entry->preHeader ;
935 iCode *icFirst = NULL , *icLast = NULL ;
937 bitVect *indVect = NULL;
939 /* create an iCode chain from it */
940 for (ip = setFirstItem(indVars);
942 ip = setNextItem(indVars)) {
944 indVect = bitVectSetBit(indVect,ip->ic->key);
945 ip->ic->lineno = preHdr->ech->lineno;
949 icLast->next = ip->ic;
950 ip->ic->prev = icLast;
957 /* add the instruction chain to the end of the */
958 /* preheader for this loop */
959 preHdr->ech->next = icFirst ;
960 icFirst->prev = preHdr->ech ;
961 preHdr->ech = icLast;
964 /* add the induction variable vector to the last
966 lastBlock->isLastInLoop = 1;
967 lastBlock->linds = indVect;
970 setToNull ((void **)&indVars);
974 /*-----------------------------------------------------------------*/
975 /* mergeRegions - will merge region with same entry point */
976 /*-----------------------------------------------------------------*/
977 DEFSETFUNC(mergeRegions)
979 region *theLoop = item;
980 V_ARG(set*,allRegion) ;
983 /* if this has already been merged then do nothing */
987 /* go thru all the region and check if any of them have the */
988 /* entryPoint as the Loop */
989 for (lp = setFirstItem(allRegion); lp ; lp = setNextItem(allRegion)) {
994 if (lp->entry == theLoop->entry) {
995 theLoop->regBlocks = unionSets (theLoop->regBlocks,
996 lp->regBlocks,THROW_BOTH);
1004 /*-----------------------------------------------------------------*/
1005 /* ifMerged - return 1 if the merge flag is 1 */
1006 /*-----------------------------------------------------------------*/
1007 DEFSETFUNC(ifMerged)
1014 /*-----------------------------------------------------------------*/
1015 /* mergeInnerLoops - will merge into body when entry is present */
1016 /*-----------------------------------------------------------------*/
1017 DEFSETFUNC(mergeInnerLoops)
1019 region *theLoop = item;
1020 V_ARG(set *,allRegion);
1021 V_ARG(int *,maxDepth);
1024 /* check if the entry point is present in the body of any */
1025 /* loop then put the body of this loop into the outer loop*/
1026 for (lp = setFirstItem(allRegion); lp ; lp = setNextItem(allRegion)) {
1028 if ( lp == theLoop )
1031 if (isinSet(lp->regBlocks, theLoop->entry)) {
1032 lp->containsLoops += theLoop->containsLoops + 1 ;
1033 if ( lp->containsLoops > (*maxDepth))
1034 *maxDepth = lp->containsLoops;
1036 lp->regBlocks = unionSets (lp->regBlocks,
1037 theLoop->regBlocks,THROW_DEST);
1045 /*-----------------------------------------------------------------*/
1046 /* createLoopRegions - will detect and create a set of natural loops */
1047 /*-----------------------------------------------------------------*/
1048 hTab *createLoopRegions (eBBlock **ebbs , int count )
1050 set *allRegion = NULL; /* set of all loops */
1051 hTab *orderedLoops = NULL ;
1056 /* get all the back edges in the graph */
1057 if (! applyToSet(graphEdges,backEdges,&bEdges))
1058 return 0 ; /* found no loops */
1060 /* for each of these back edges get the blocks that */
1061 /* constitute the loops */
1062 applyToSet(bEdges,createLoop,&allRegion);
1064 /* now we will create regions from these loops */
1065 /* loops with the same entry points are considered to be the */
1066 /* same loop & they are merged. If the entry point of a loop */
1067 /* is found in the body of another loop then , all the blocks*/
1068 /* in that loop are added to the loops containing the header */
1069 applyToSet(allRegion, mergeRegions , allRegion);
1071 /* delete those already merged */
1072 deleteItemIf (&allRegion, ifMerged);
1074 applyToSet(allRegion, mergeInnerLoops, allRegion, &maxDepth);
1076 /* now create all the exits .. also */
1077 /* create an ordered set of loops */
1078 /* i.e. we process loops in the inner to outer order */
1079 for (lp = setFirstItem(allRegion) ; lp ; lp = setNextItem(allRegion)) {
1080 applyToSet (lp->regBlocks,addToExitsMarkDepth,
1081 lp->regBlocks,&lp->exits,
1082 (maxDepth - lp->containsLoops),lp);
1084 hTabAddItem (&orderedLoops,lp->containsLoops,lp);
1087 return orderedLoops ;
1090 /*-----------------------------------------------------------------*/
1091 /* loopOptimizations - identify region & remove invariants & ind */
1092 /*-----------------------------------------------------------------*/
1093 int loopOptimizations (hTab *orderedLoops, eBBlock **ebbs, int count)
1100 /* if no loop optimizations requested */
1101 if (! optimize.loopInvariant &&
1102 ! optimize.loopInduction )
1105 /* now we process the loops inner to outer order */
1106 /* this is essential to maintain data flow information */
1107 /* the other choice is an ugly iteration for the depth */
1108 /* of the loops would hate that */
1109 for ( lp = hTabFirstItem(orderedLoops,&k); lp ;
1110 lp = hTabNextItem(orderedLoops,&k)) {
1112 if (optimize.loopInvariant)
1113 change += loopInvariants(lp, ebbs, count);
1115 if (optimize.loopInduction)
1116 change += loopInduction(lp, ebbs, count);