Update svn merge history.
[sdcc.git] / sdcc / src / SDCCloop.c
blob4ab03dbc7751358c22c61324577138a287d5a584
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
10 later version.
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 -------------------------------------------------------------------------*/
26 #include "common.h"
27 #include "newalloc.h"
29 DEFSETFUNC (isDefAlive);
31 STACK_DCL (regionStack, eBBlock *, MAX_NEST_LEVEL * 10)
33 /*-----------------------------------------------------------------*/
34 /* newInduction - creates a new induction variable */
35 /*-----------------------------------------------------------------*/
36 static induction *
37 newInduction (operand * sym, unsigned int op, long constVal, iCode * ic, operand * asym)
39 induction *ip;
41 ip = Safe_alloc (sizeof (induction));
43 ip->sym = sym;
44 ip->asym = asym;
45 ip->op = op;
46 ip->cval = constVal;
47 ip->ic = ic;
48 //updateSpillLocation(ic,1);
49 return ip;
52 /*-----------------------------------------------------------------*/
53 /* newRegion - allocate & returns a loop structure */
54 /*-----------------------------------------------------------------*/
55 static region *
56 newRegion ()
58 region *lp;
60 lp = Safe_alloc (sizeof (region));
62 return lp;
66 #if 0
67 /*-----------------------------------------------------------------*/
68 /* pinduction - prints induction */
69 /*-----------------------------------------------------------------*/
70 static
71 DEFSETFUNC (pinduction)
73 induction *ip = item;
74 iCodeTable *icTab;
76 fprintf (stdout, "\t");
77 printOperand (ip->sym, stdout);
78 icTab = getTableEntry (ip->ic->op);
79 icTab->iCodePrint (stdout, ip->ic, icTab->printName);
80 fprintf (stdout, " %04d\n", (int) ip->cval);
81 return 0;
84 /*-----------------------------------------------------------------*/
85 /* pregion - prints loop information */
86 /*-----------------------------------------------------------------*/
87 static
88 DEFSETFUNC (pregion)
90 region *lp = item;
92 printf ("================\n");
93 printf (" loop with entry -- > ");
94 printEntryLabel (lp->entry, ap);
95 printf ("\n");
96 printf (" loop body --> ");
97 applyToSet (lp->regBlocks, printEntryLabel);
98 printf ("\n");
99 printf (" loop exits --> ");
100 applyToSet (lp->exits, printEntryLabel);
101 printf ("\n");
102 return 0;
104 #endif
106 /*-----------------------------------------------------------------*/
107 /* backEdges - returns a list of back edges */
108 /*-----------------------------------------------------------------*/
109 static
110 DEFSETFUNC (backEdges)
112 edge *ep = item;
113 V_ARG (set **, bEdges);
115 /* if this is a back edge ; to determine this we check */
116 /* to see if the 'to' is in the dominator list of the */
117 /* 'from' if yes then this is a back edge */
118 if (bitVectBitValue (ep->from->domVect, ep->to->bbnum))
120 addSetHead (bEdges, ep);
121 return 1;
124 return 0;
127 /*-----------------------------------------------------------------*/
128 /* intersectLoopSucc - returns intersection of loop Successors */
129 /*-----------------------------------------------------------------*/
130 static bitVect *
131 intersectLoopSucc (set * lexits, eBBlock ** ebbs)
133 bitVect *succVect = NULL;
134 eBBlock *exit = setFirstItem (lexits);
136 if (!exit)
137 return NULL;
139 succVect = bitVectCopy (exit->succVect);
141 for (exit = setNextItem (lexits); exit; exit = setNextItem (lexits))
143 succVect = bitVectIntersect (succVect, exit->succVect);
146 return succVect;
149 /*-----------------------------------------------------------------*/
150 /* loopInsert will insert a block into the loop set */
151 /*-----------------------------------------------------------------*/
152 static void
153 loopInsert (set ** regionSet, eBBlock * block)
155 if (!isinSet (*regionSet, block))
157 addSetHead (regionSet, block);
158 STACK_PUSH (regionStack, block);
162 /*-----------------------------------------------------------------*/
163 /* insertIntoLoop - insert item into loop */
164 /*-----------------------------------------------------------------*/
165 static
166 DEFSETFUNC (insertIntoLoop)
168 eBBlock *ebp = item;
169 V_ARG (set **, regionSet);
171 loopInsert (regionSet, ebp);
172 return 0;
175 /*-----------------------------------------------------------------*/
176 /* isNotInBlocks - will return 1 if not is blocks */
177 /*-----------------------------------------------------------------*/
178 static
179 DEFSETFUNC (isNotInBlocks)
181 eBBlock *ebp = item;
182 V_ARG (set *, blocks);
184 if (!isinSet (blocks, ebp))
185 return 1;
187 return 0;
190 #if 0
191 /*-----------------------------------------------------------------*/
192 /* hasIncomingDefs - has definitions coming into the loop. i.e. */
193 /* check to see if the preheaders outDefs has any definitions */
194 /*-----------------------------------------------------------------*/
195 static int
196 hasIncomingDefs (region * lreg, operand * op)
198 eBBlock *preHdr = lreg->entry->preHeader;
200 if (preHdr && bitVectBitsInCommon (preHdr->outDefs, OP_DEFS (op)))
201 return 1;
202 return 0;
205 /*-----------------------------------------------------------------*/
206 /* findLoopEndSeq - will return the sequence number of the last */
207 /* iCode with the maximum dfNumber in the region */
208 /*-----------------------------------------------------------------*/
209 static int
210 findLoopEndSeq (region * lreg)
212 eBBlock *block;
213 eBBlock *lblock;
215 for (block = lblock = setFirstItem (lreg->regBlocks); block; block = setNextItem (lreg->regBlocks))
217 if (block != lblock && block->lSeq > lblock->lSeq)
218 lblock = block;
221 return lblock->lSeq;
223 #endif
225 /*-----------------------------------------------------------------*/
226 /* addToExitsMarkDepth - will add the the exitSet all blocks that */
227 /* have exits, will also update the depth field in the blocks */
228 /*-----------------------------------------------------------------*/
229 static
230 DEFSETFUNC (addToExitsMarkDepth)
232 eBBlock *ebp = item;
233 V_ARG (set *, loopBlocks);
234 V_ARG (set **, exits);
235 V_ARG (int, depth);
236 V_ARG (region *, lr);
238 /* mark the loop depth of this block */
239 //if (!ebp->depth)
240 if (ebp->depth < depth)
241 ebp->depth = depth;
243 /* NOTE: here we will update only the inner most loop
244 that it is a part of */
245 if (!ebp->partOfLoop)
246 ebp->partOfLoop = lr;
248 /* if any of the successors go out of the loop then */
249 /* we add this one to the exits */
250 if (applyToSet (ebp->succList, isNotInBlocks, loopBlocks))
252 addSetHead (exits, ebp);
253 return 1;
256 return 0;
259 /*-----------------------------------------------------------------*/
260 /* createLoop - will create a set of region */
261 /*-----------------------------------------------------------------*/
262 static
263 DEFSETFUNC (createLoop)
265 edge *ep = item;
266 V_ARG (set **, allRegion);
267 region *aloop = newRegion ();
268 eBBlock *block;
270 /* make sure regionStack is empty */
271 while (!STACK_EMPTY (regionStack))
272 STACK_POP (regionStack);
274 /* add the entryBlock */
275 addSet (&aloop->regBlocks, ep->to);
276 loopInsert (&aloop->regBlocks, ep->from);
278 while (!STACK_EMPTY (regionStack))
280 block = STACK_POP (regionStack);
281 /* if block != entry */
282 if (block != ep->to)
283 applyToSet (block->predList, insertIntoLoop, &aloop->regBlocks);
286 aloop->entry = ep->to;
288 /* now add it to the set */
289 addSetHead (allRegion, aloop);
290 return 0;
293 /*-----------------------------------------------------------------*/
294 /* dominatedBy - will return 1 if item is dominated by block */
295 /*-----------------------------------------------------------------*/
296 static
297 DEFSETFUNC (dominatedBy)
299 eBBlock *ebp = item;
300 V_ARG (eBBlock *, block);
302 return bitVectBitValue (ebp->domVect, block->bbnum);
305 /*-----------------------------------------------------------------*/
306 /* addDefInExprs - adds an expression into the inexpressions */
307 /*-----------------------------------------------------------------*/
308 static
309 DEFSETFUNC (addDefInExprs)
311 eBBlock *ebp = item;
312 V_ARG (cseDef *, cdp);
313 V_ARG (ebbIndex *, ebbi);
315 addSetHead (&ebp->inExprs, cdp);
316 cseBBlock (ebp, optimize.global_cse, ebbi);
317 return 0;
320 /*-----------------------------------------------------------------*/
321 /* assignmentsToSym - for a set of blocks determine # time assigned */
322 /*-----------------------------------------------------------------*/
323 static int
324 assignmentsToSym (set * sset, operand * sym)
326 eBBlock *ebp;
327 int assigns = 0;
328 set *blocks = setFromSet (sset);
330 for (ebp = setFirstItem (blocks); ebp; ebp = setNextItem (blocks))
332 /* get all the definitions for this symbol
333 in this block */
334 bitVect *defs = bitVectIntersect (ebp->ldefs, OP_DEFS (sym));
335 assigns += bitVectnBitsOn (defs);
336 setToNull ((void *) &defs);
339 return assigns;
343 /*-----------------------------------------------------------------*/
344 /* isOperandInvariant - determines if an operand is an invariant */
345 /*-----------------------------------------------------------------*/
346 static int
347 isOperandInvariant (operand * op, region * theLoop, set * lInvars)
349 int opin = 0;
350 /* operand is an invariant if it is a */
351 /* a. constants . */
352 /* b. that have definitions reaching loop entry */
353 /* c. that are already defined as invariant */
354 /* d. has no assignments in the loop */
355 if (op)
357 if (IS_OP_LITERAL (op))
358 opin = 1;
359 else if (IS_SYMOP (op) && OP_SYMBOL (op)->addrtaken)
360 opin = 0;
361 else if (ifDefSymIs (theLoop->entry->inExprs, op))
362 opin = 1;
363 else if (ifDefSymIs (lInvars, op))
364 opin = 1;
365 else if (IS_SYMOP (op) && !IS_OP_GLOBAL (op) && !IS_OP_VOLATILE (op) && assignmentsToSym (theLoop->regBlocks, op) == 0)
366 opin = 1;
368 else
369 opin++;
371 return opin;
374 /*-----------------------------------------------------------------*/
375 /* pointerAssigned - will return 1 if pointer set found */
376 /*-----------------------------------------------------------------*/
377 static
378 DEFSETFUNC (pointerAssigned)
380 eBBlock *ebp = item;
381 V_ARG (operand *, op);
383 if (ebp->hasFcall)
384 return 1;
386 if (bitVectBitValue (ebp->ptrsSet, op->key))
387 return 1;
389 /* Unfortunately, one of the other pointer set operations */
390 /* may be using an alias of this operand, and the above */
391 /* test would miss it. To be thorough, some aliasing */
392 /* analysis should be done here. In the meantime, be */
393 /* conservative and assume any other pointer set operation */
394 /* is dangerous */
395 if (!bitVectIsZero (ebp->ptrsSet))
396 return 1;
398 return 0;
401 /*-----------------------------------------------------------------*/
402 /* hasNonPtrUse - returns true if operand has non pointer usage */
403 /*-----------------------------------------------------------------*/
404 static
405 DEFSETFUNC (hasNonPtrUse)
407 eBBlock *ebp = item;
408 V_ARG (operand *, op);
409 iCode *ic = usedInRemaining (op, ebp->sch);
411 if (ic && !POINTER_SET (ic) && !POINTER_GET (ic))
412 return 1;
414 return 0;
418 /*-----------------------------------------------------------------*/
419 /* loopInvariants - takes loop invariants out of region */
420 /*-----------------------------------------------------------------*/
421 static int
422 loopInvariants (region *theLoop, ebbIndex *ebbi)
424 eBBlock **ebbs = ebbi->dfOrder;
425 int count = ebbi->count;
426 set *lInvars = NULL;
428 int change = 0;
429 int fCallsInBlock;
431 /* if the preHeader does not exist then do nothing */
432 /* or no exits then do nothing ( have to think about this situation */
433 if (theLoop->entry->preHeader == NULL || theLoop->exits == NULL)
434 return 0;
436 /* we will do the elimination for those blocks */
437 /* in the loop that dominate all exits from the loop */
438 for (eBBlock *lBlock = setFirstItem (theLoop->regBlocks); lBlock; lBlock = setNextItem (theLoop->regBlocks))
440 iCode *ic;
441 int domsAllExits;
443 /* mark the dominates all exits flag */
444 domsAllExits = (applyToSet (theLoop->exits, dominatedBy, lBlock) == elementsInSet (theLoop->exits));
446 /* find out if we have a function call in this block */
447 for (ic = lBlock->sch, fCallsInBlock = 0; ic; ic = ic->next)
449 if (SKIP_IC (ic))
451 fCallsInBlock++;
455 /* now we go thru the instructions of this block and */
456 /* collect those instructions with invariant operands */
457 for (ic = lBlock->sch; ic; ic = ic->next)
459 int lin, rin;
460 cseDef *ivar;
462 /* TODO this is only needed if the call is between
463 here and the definition, but I am too lazy to do that now */
465 /* if there are function calls in this block */
466 if (fCallsInBlock)
468 /* if this is a pointer get */
469 if (POINTER_GET (ic))
471 continue;
474 /* if this is an assignment from a global */
475 if (ic->op == '=' && isOperandGlobal (IC_RIGHT (ic)))
477 continue;
480 /* Bug 1717943,
481 * if this is an assignment to a global */
482 if (ic->op == '=' && isOperandGlobal (IC_RESULT (ic)))
484 continue;
488 if (SKIP_IC (ic) || POINTER_SET (ic) || ic->op == IFX)
489 continue;
491 /* iTemp assignment from a literal may be invariant, but it
492 will needlessly increase register pressure if the
493 iCode(s) that use this iTemp are not also invariant */
494 if (ic->op == '=' && IS_ITEMP (IC_RESULT (ic)) && IS_OP_LITERAL (IC_RIGHT (ic)))
495 continue;
497 /* if result is volatile then skip */
498 if (IC_RESULT (ic) && (isOperandVolatile (IC_RESULT (ic), TRUE) || IS_OP_PARM (IC_RESULT (ic))))
499 continue;
501 /* if result depends on a volatile then skip */
502 if ((IC_LEFT (ic) && isOperandVolatile (IC_LEFT (ic), TRUE)) ||
503 (IC_RIGHT (ic) && isOperandVolatile (IC_RIGHT (ic), TRUE)))
504 continue;
506 if (POINTER_GET (ic) && IS_VOLATILE (operandType (IC_LEFT (ic))->next))
507 continue;
509 lin = rin = 0;
511 /* special case */
512 /* if address of then it is an invariant */
513 if (ic->op == ADDRESS_OF && IS_SYMOP (IC_LEFT (ic)) && IS_AGGREGATE (operandType (IC_LEFT (ic))))
515 lin++;
517 else
519 /* check if left operand is an invariant */
520 if ((lin = isOperandInvariant (IC_LEFT (ic), theLoop, lInvars)) &&
521 /* if this is a pointer get then make sure
522 that the pointer set does not exist in
523 any of the blocks */
524 POINTER_GET (ic) && applyToSet (theLoop->regBlocks, pointerAssigned, IC_LEFT (ic)))
526 lin = 0;
530 /* do the same for right */
531 rin = isOperandInvariant (IC_RIGHT (ic), theLoop, lInvars);
533 /* if this is a POINTER_GET then special case, make sure all
534 usages within the loop are POINTER_GET any other usage
535 would mean that this is not an invariant , since the pointer
536 could then be passed as a parameter */
537 if (POINTER_GET (ic) && applyToSet (theLoop->regBlocks, hasNonPtrUse, IC_LEFT (ic)))
538 continue;
541 /* if both the left & right are invariants : then check that */
542 /* this definition exists in the out definition of all the */
543 /* blocks, this will ensure that this is not assigned any */
544 /* other value in the loop, and not used in this block */
545 /* prior to this definition which means only this definition */
546 /* is used in this loop */
547 if (lin && rin && IC_RESULT (ic))
549 eBBlock *sBlock;
550 set *lSet = setFromSet (theLoop->regBlocks);
552 /* if this block does not dominate all exits */
553 /* make sure this definition is not used anywhere else */
554 if (!domsAllExits)
556 if (isOperandGlobal (IC_RESULT (ic)))
557 continue;
558 /* for successors for all exits */
559 for (sBlock = setFirstItem (theLoop->exits); sBlock; sBlock = setNextItem (theLoop->exits))
561 for (int i = 0; i < count; ebbs[i++]->visited = 0);
562 lBlock->visited = 1;
563 if (applyToSet (sBlock->succList, isDefAlive, ic))
564 break;
567 /* we have found usage */
568 if (sBlock)
569 continue;
572 /* now make sure this is the only definition */
573 for (sBlock = setFirstItem (lSet); sBlock; sBlock = setNextItem (lSet))
575 iCode *ic2;
576 int used;
577 int defDominates;
579 /* if this is the block make sure the definition */
580 /* reaches the end of the block */
581 if (sBlock == lBlock)
583 if (!ifDiCodeIs (sBlock->outExprs, ic))
584 break;
586 else if (bitVectBitsInCommon (sBlock->defSet, OP_DEFS (IC_RESULT (ic))))
587 break;
589 /* Check that this definition is not assigned */
590 /* any other value in this block. Also check */
591 /* that any usage in the block is dominated by */
592 /* by this definition. */
593 defDominates = bitVectBitValue (sBlock->domVect, lBlock->bbnum);
594 used = 0;
595 for (ic2 = sBlock->sch; ic2; ic2 = ic2->next)
597 if (ic2->op == IFX)
599 if (isOperandEqual (IC_RESULT (ic), IC_COND (ic2)))
600 used = 1;
602 else if (ic2->op == JUMPTABLE)
604 if (isOperandEqual (IC_RESULT (ic), IC_JTCOND (ic2)))
605 used = 1;
607 else
609 if (IC_LEFT (ic2) && isOperandEqual (IC_RESULT (ic), IC_LEFT (ic2)))
610 used = 1;
611 if (IC_RIGHT (ic2) && isOperandEqual (IC_RESULT (ic), IC_RIGHT (ic2)))
612 used = 1;
613 if ((ic != ic2) && (isOperandEqual (IC_RESULT (ic), IC_RESULT (ic2))))
614 break;
615 /* If used before this definition, might not be invariant */
616 if ((ic == ic2) && used)
617 break;
619 if (used && !defDominates)
620 break;
622 if (ic2) /* found another definition or a usage before the definition */
623 break;
626 if (sBlock)
627 continue; /* another definition present in the block */
629 // Avoid bug #3560 - the address might have been passed elsewehere, so functions called in the loop could change the value (and rely on the changed value).
630 if (OP_SYMBOL (IC_RESULT (ic))->addrtaken && fCallsInBlock)
631 continue;
633 /* now check if it exists in the in of this block */
634 /* if not then it was killed before this instruction */
635 if (!bitVectBitValue (lBlock->inDefs, ic->key))
636 continue;
638 /* now we know it is a true invariant */
639 /* remove it from the insts chain & put */
640 /* in the invariant set */
642 OP_SYMBOL (IC_RESULT (ic))->isinvariant = 1;
643 SPIL_LOC (IC_RESULT (ic)) = NULL;
644 remiCodeFromeBBlock (lBlock, ic);
646 /* maintain the data flow */
647 /* this means removing from definition from the */
648 /* defset of this block and adding it to the */
649 /* inexpressions of all blocks within the loop */
650 bitVectUnSetBit (lBlock->defSet, ic->key);
651 bitVectUnSetBit (lBlock->ldefs, ic->key);
652 ivar = newCseDef (IC_RESULT (ic), ic);
653 applyToSet (theLoop->regBlocks, addDefInExprs, ivar, ebbi);
654 addSet (&lInvars, ivar);
657 } /* for all loop blocks */
659 /* if we have some invariants then */
660 if (lInvars)
662 eBBlock *preHdr = theLoop->entry->preHeader;
663 iCode *icFirst = NULL, *icLast = NULL;
665 /* create an iCode chain from it */
666 for (cseDef *cdp = setFirstItem (lInvars); cdp; cdp = setNextItem (lInvars))
668 /* maintain data flow .. add it to the */
669 /* ldefs defSet & outExprs of the preheader */
670 preHdr->defSet = bitVectSetBit (preHdr->defSet, cdp->diCode->key);
671 preHdr->ldefs = bitVectSetBit (preHdr->ldefs, cdp->diCode->key);
672 cdp->diCode->filename = preHdr->ech->filename;
673 cdp->diCode->lineno = preHdr->ech->lineno;
674 addSetHead (&preHdr->outExprs, cdp);
676 if (!icFirst)
677 icFirst = cdp->diCode;
678 if (icLast)
680 icLast->next = cdp->diCode;
681 cdp->diCode->prev = icLast;
682 icLast = cdp->diCode;
684 else
685 icLast = cdp->diCode;
686 change++;
689 /* add the instruction chain to the end of the
690 preheader for this loop, preheaders will always
691 have atleast a label */
692 preHdr->ech->next = icFirst;
693 icFirst->prev = preHdr->ech;
694 preHdr->ech = icLast;
695 icLast->next = NULL;
697 return change;
700 /*-----------------------------------------------------------------*/
701 /* addressTaken - returns true if the symbol is found in the addrof */
702 /*-----------------------------------------------------------------*/
703 static int
704 addressTaken (set * sset, operand * sym)
706 set *loop;
707 eBBlock *ebp;
708 set *loop2;
710 for (loop = sset; loop; loop = loop->next)
712 ebp = loop->item;
713 loop2 = ebp->addrOf;
714 while (loop2)
716 if (isOperandEqual ((operand *) loop2->item, sym))
717 return 1;
718 loop2 = loop2->next;
722 return 0;
726 /*-----------------------------------------------------------------*/
727 /* findInduction :- returns 1 & the item if the induction is found */
728 /*-----------------------------------------------------------------*/
729 static
730 DEFSETFUNC (findInduction)
732 induction *ip = item;
733 V_ARG (operand *, sym);
734 V_ARG (induction **, ipp);
736 if (isOperandEqual (ip->sym, sym))
738 *ipp = ip;
739 return 1;
742 return 0;
745 /*-----------------------------------------------------------------*/
746 /* findDefInRegion - finds the definition within the region */
747 /*-----------------------------------------------------------------*/
748 static iCode *
749 findDefInRegion (set * regBlocks, operand * defOp, eBBlock ** owner)
751 eBBlock *lBlock;
753 /* for all blocks in the region */
754 for (lBlock = setFirstItem (regBlocks); lBlock; lBlock = setNextItem (regBlocks))
757 /* if a definition for this exists */
758 if (bitVectBitsInCommon (lBlock->defSet, OP_DEFS (defOp)))
760 iCode *ic;
762 /* go thru the instruction chain to find it */
763 for (ic = lBlock->sch; ic; ic = ic->next)
764 if (bitVectBitValue (OP_DEFS (defOp), ic->key))
766 if (owner)
767 *owner = lBlock;
768 return ic;
773 return NULL;
776 /*-----------------------------------------------------------------*/
777 /* addPostLoopBlock - add a ebblock before the successors of the */
778 /* loop exits */
779 /*-----------------------------------------------------------------*/
780 static void
781 addPostLoopBlock (region * loopReg, ebbIndex * ebbi, iCode * ic)
783 bitVect *loopSuccs;
784 int i;
786 /* if the number of exits is greater than one then
787 we use another trick: we will create an intersection
788 of successors of the exits, then take those that are not
789 part of the loop and have dfNumber greater loop entry (eblock),
790 insert a new predecessor postLoopBlk before them and add
791 a copy of ic in the new block. The postLoopBlk in between
792 is necessary, because the old successors of the loop exits can
793 be successors of other blocks too: see bug-136564. */
795 /* loopSuccs now contains intersection
796 of all the loops successors */
797 loopSuccs = intersectLoopSucc (loopReg->exits, ebbi->dfOrder);
798 if (!loopSuccs)
799 return;
801 for (i = 0; i < loopSuccs->size; i++)
803 eBBlock *eblock = NULL;
804 eBBlock *postLoopBlk;
805 iCode *newic;
806 int j;
808 if (!bitVectBitValue (loopSuccs, i))
809 continue;
811 /* Need to search for bbnum == i since ebbi->dfOrder is
812 sorted by dfnum; a direct index won't do. */
813 for (j = 0; j < ebbi->count; j++)
814 if (ebbi->dfOrder[j]->bbnum == i)
816 eblock = ebbi->dfOrder[j];
817 break;
819 assert (eblock);
821 /* if the successor does not belong to the loop
822 and will be executed after the loop : then
823 add a definition to the block */
824 if (isinSet (loopReg->regBlocks, eblock))
825 continue;
826 if (eblock->dfnum <= loopReg->entry->dfnum)
827 continue;
829 /* look for an existing loopExitBlock */
830 if (strncmp (LOOPEXITLBL, eblock->entryLabel->name, sizeof (LOOPEXITLBL) - 1) == 0)
832 /* reuse the existing one */
833 postLoopBlk = eblock;
835 else
837 /* create and insert a new eBBlock.
838 Damn, that's messy ... */
839 int i;
840 set *oldPredList;
841 eBBlock *ebpi;
843 postLoopBlk = neweBBlock ();
845 /* create a loopExit-label */
846 postLoopBlk->entryLabel = newiTempLoopHeaderLabel (0);
848 /* increase bbnum for all blocks after
849 (and including) eblock */
850 for (i = 0; i < ebbi->count; i++)
852 if (ebbi->bbOrder[i]->bbnum >= eblock->bbnum)
853 ++ebbi->bbOrder[i]->bbnum;
855 /* insert postLoopBlk before bbnum */
856 postLoopBlk->bbnum = eblock->bbnum - 1;
858 ++ebbi->count;
860 /* these arrays need one more block, which ... */
861 ebbi->bbOrder = Safe_realloc (ebbi->bbOrder, (ebbi->count + 1) * sizeof (eBBlock *));
862 /* ... must be initialized with 0 */
863 ebbi->bbOrder[ebbi->count] = NULL;
864 /* move the blocks up ... */
865 memmove (&ebbi->bbOrder[postLoopBlk->bbnum + 1],
866 &ebbi->bbOrder[postLoopBlk->bbnum], (ebbi->count - postLoopBlk->bbnum - 1) * sizeof (eBBlock *));
867 /* ... and insert postLoopBlk */
868 ebbi->bbOrder[postLoopBlk->bbnum] = postLoopBlk;
870 /* just add postLoopBlk at the end of dfOrder,
871 computeControlFlow() will compute the new dfnum */
872 ebbi->dfOrder = Safe_realloc (ebbi->dfOrder, (ebbi->count + 1) * sizeof (eBBlock *));
873 ebbi->dfOrder[ebbi->count] = NULL;
874 ebbi->dfOrder[ebbi->count - 1] = postLoopBlk;
876 /* copy loop information from eblock to postLoopBlk */
877 if (eblock->partOfLoop)
879 postLoopBlk->partOfLoop = eblock->partOfLoop;
880 /* add postLoopBlk to loop region */
881 addSetHead (&postLoopBlk->partOfLoop->regBlocks, postLoopBlk);
884 /* go through loop exits and replace the old exit block eblock
885 with the new postLoopBlk */
886 for (ebpi = setFirstItem (loopReg->exits); ebpi; ebpi = setNextItem (loopReg->exits))
888 /* replace old label with new label */
889 replaceLabel (ebpi, eblock->entryLabel, postLoopBlk->entryLabel);
892 /* now eblock is replaced by postLoopBlk.
893 It's possible, that eblock has an immediate predecessor
894 (with ebpi->bbnum + 1 == eblock->bbnum), which isn't part of the
895 loop. This predecessor must stay predecessor of eblock, not of
896 postLoopBlk. But now postLoopBlk is in between, therefore a GOTO
897 from this predecessor to eblock must be appended.
898 Example: bug-136564.c */
900 /* take all predecessors and subtract the loop exits */
901 oldPredList = subtractFromSet (eblock->predList, loopReg->exits, THROW_NONE);
902 for (ebpi = setFirstItem (oldPredList); ebpi; ebpi = setNextItem (oldPredList))
904 /* Is it an immediate predecessor (without GOTO)?
905 All other predecessors end with a
906 GOTO, IF, IFX or JUMPTABLE: nothing to to do */
907 if (ebpi->bbnum + 1 == postLoopBlk->bbnum)
909 /* insert goto to old predecessor of eblock */
910 newic = newiCodeLabelGoto (GOTO, eblock->entryLabel);
911 addiCodeToeBBlock (ebpi, newic, NULL);
912 /* Make sure the GOTO has a target */
913 if (eblock->sch->op != LABEL)
915 newic = newiCodeLabelGoto (LABEL, eblock->entryLabel);
916 addiCodeToeBBlock (eblock, newic, eblock->sch);
918 break; /* got it, only one is possible */
922 /* set the label */
923 postLoopBlk->sch = postLoopBlk->ech = newiCodeLabelGoto (LABEL, postLoopBlk->entryLabel);
926 /* create the definition in postLoopBlk */
927 newic = newiCode ('=', NULL, operandFromOperand (IC_RIGHT (ic)));
928 IC_RESULT (newic) = operandFromOperand (IC_RESULT (ic));
929 /* maintain data flow */
930 OP_DEFS (IC_RESULT (newic)) = bitVectSetBit (OP_DEFS (IC_RESULT (newic)), newic->key);
931 OP_USES (IC_RIGHT (newic)) = bitVectSetBit (OP_USES (IC_RIGHT (newic)), newic->key);
933 /* and add it */
934 addiCodeToeBBlock (postLoopBlk, newic, NULL);
935 postLoopBlk->sch->filename = postLoopBlk->ech->filename = eblock->sch->filename;
936 postLoopBlk->sch->lineno = postLoopBlk->ech->lineno = eblock->sch->lineno;
938 /* outDefs is needed by computeControlFlow(), anything
939 else will be set up by computeControlFlow() */
940 postLoopBlk->outDefs = bitVectSetBit (postLoopBlk->outDefs, newic->key);
941 } /* for (i = 0; i < loopSuccs->size; i++) */
943 /* the postLoopBlk and the induction significantly
944 changed the control flow, recompute it */
945 computeControlFlow (ebbi);
948 /*-----------------------------------------------------------------*/
949 /* basicInduction - finds the basic induction variables in a loop */
950 /*-----------------------------------------------------------------*/
951 static set *
952 basicInduction (region * loopReg, ebbIndex * ebbi)
954 eBBlock *lBlock;
955 set *indVars = NULL;
957 /* i.e. all assignments of the form a := a +/- const */
958 /* for all blocks within the loop do */
959 for (lBlock = setFirstItem (loopReg->regBlocks); lBlock; lBlock = setNextItem (loopReg->regBlocks))
962 iCode *ic, *dic;
964 /* for all instructions in the blocks do */
965 for (ic = lBlock->sch; ic; ic = ic->next)
967 operand *aSym;
968 long litValue;
969 induction *ip;
970 iCode *indIc;
971 eBBlock *owner = NULL;
972 int nexits;
973 sym_link *optype;
975 /* To find an induction variable, we need to */
976 /* find within the loop three iCodes in this */
977 /* general form: */
978 /* */
979 /* ddic: iTempB := symbolVar */
980 /* dic: iTempA := iTempB + lit */
981 /* or iTempA := iTempB - lit */
982 /* or iTempA := lit + iTempB */
983 /* ic: symbolVar := iTempA */
984 /* */
985 /* (symbolVar may also be an iTemp if it is */
986 /* register equivalent) */
988 /* look for assignments of the form */
989 /* symbolVar := iTempNN */
990 if (ic->op != '=')
991 continue;
993 if (!IS_SYMOP (IC_RESULT (ic)))
994 continue;
996 if (!IS_TRUE_SYMOP (IC_RESULT (ic)) && !OP_SYMBOL (IC_RESULT (ic))->isreqv)
997 continue;
999 if (isOperandGlobal (IC_RESULT (ic)))
1000 continue;
1002 if (!IS_ITEMP (IC_RIGHT (ic)))
1003 continue;
1005 /* if it has multiple assignments within the loop then skip */
1006 if (assignmentsToSym (loopReg->regBlocks, IC_RESULT (ic)) > 1)
1007 continue;
1009 /* if the address of this was taken inside the loop then continue */
1010 if (addressTaken (loopReg->regBlocks, IC_RESULT (ic)))
1011 continue;
1013 /* Only consider variables with integral type. */
1014 /* (2004/12/06 - EEP - ds390 fails regression tests unless */
1015 /* pointers are also considered for induction (due to some */
1016 /* register allocation bugs). Remove !IS_PTR clause when */
1017 /* that gets fixed) */
1018 optype = operandType (IC_RIGHT (ic));
1019 if (!IS_INTEGRAL (optype) && !IS_PTR (optype))
1020 continue;
1022 /* find the definition for the result in the block */
1023 if (!(dic = findDefInRegion (setFromSet (loopReg->regBlocks), IC_RIGHT (ic), &owner)))
1024 continue;
1026 /* if not +/- continue */
1027 if (dic->op != '+' && dic->op != '-')
1028 continue;
1030 /* make sure definition is of the form a +/- c */
1031 if (!IS_OP_LITERAL (IC_LEFT (dic)) && !IS_OP_LITERAL (IC_RIGHT (dic)))
1032 continue;
1034 /* make sure the definition found is the only one */
1035 if (assignmentsToSym (loopReg->regBlocks, IC_RIGHT (ic)) > 1)
1036 continue;
1038 if (IS_OP_LITERAL (IC_RIGHT (dic)))
1040 aSym = IC_LEFT (dic);
1041 litValue = (long) operandLitValue (IC_RIGHT (dic));
1043 else
1045 /* For minus, the literal must not be on the left side. */
1046 /* (Actually, this case could be handled, but it's probably */
1047 /* not worth the extra code) */
1048 if (dic->op == '-')
1049 continue;
1050 aSym = IC_RIGHT (dic);
1051 litValue = (long) operandLitValue (IC_LEFT (dic));
1054 if (!isOperandEqual (IC_RESULT (ic), aSym) && !isOperandEqual (IC_RIGHT (ic), aSym))
1056 iCode *ddic;
1057 /* find the definition for this and check */
1058 if (!(ddic = findDefInRegion (setFromSet (loopReg->regBlocks), aSym, &owner)))
1059 continue;
1061 if (ddic->op != '=')
1062 continue;
1064 if (!isOperandEqual (IC_RESULT (ddic), aSym) || !isOperandEqual (IC_RIGHT (ddic), IC_RESULT (ic)))
1065 continue;
1068 /* if the right hand side has more than one usage then
1069 don't make it an induction (will have to think some more) */
1070 if (bitVectnBitsOn (OP_USES (IC_RIGHT (ic))) > 1)
1071 continue;
1073 /* if the definition is volatile then it cannot be
1074 an induction object */
1075 if (isOperandVolatile (IC_RIGHT (ic), FALSE) || isOperandVolatile (IC_RESULT (ic), FALSE))
1076 continue;
1078 /* whew !! that was a lot of work to find the definition */
1079 /* create an induction object */
1080 indIc = newiCode ('=', NULL, IC_RESULT (ic));
1081 indIc->filename = ic->filename;
1082 indIc->lineno = ic->lineno;
1083 IC_RESULT (indIc) = operandFromOperand (IC_RIGHT (ic));
1084 IC_RESULT (indIc)->isaddr = 0;
1085 OP_SYMBOL (IC_RESULT (indIc))->isind = 1;
1086 ip = newInduction (IC_RIGHT (ic), dic->op, litValue, indIc, NULL);
1088 /* replace the inducted variable by the iTemp */
1089 replaceSymBySym (loopReg->regBlocks, IC_RESULT (ic), IC_RIGHT (ic));
1090 /* ic should now be moved to the exit block(s) */
1092 nexits = elementsInSet (loopReg->exits);
1093 if (nexits == 0)
1095 /* this is a endless loop without exits: there's
1096 no need to restore the inducted variable */
1097 iCode *saveic = ic->prev;
1099 /* ic will be removed by killDeadCode() too,
1100 but it's a nice to see a clean dumploop now. */
1101 unsetDefsAndUses (ic);
1102 remiCodeFromeBBlock (lBlock, ic);
1103 /* clear the definition */
1104 bitVectUnSetBit (lBlock->defSet, ic->key);
1105 ic = saveic;
1107 else
1108 addPostLoopBlock (loopReg, ebbi, ic);
1109 addSet (&indVars, ip);
1110 } /* for ic */
1111 } /* end of all blocks for basic induction variables */
1113 return indVars;
1116 /*-----------------------------------------------------------------*/
1117 /* loopInduction - remove induction variables from a loop */
1118 /*-----------------------------------------------------------------*/
1119 static int
1120 loopInduction (region * loopReg, ebbIndex * ebbi)
1122 int change = 0;
1123 eBBlock *lBlock, *owner, *lastBlock = NULL;
1124 set *indVars = NULL;
1125 set *basicInd = NULL;
1127 if (loopReg->entry->preHeader == NULL)
1128 return 0;
1130 /* we first determine the basic Induction variables */
1131 basicInd = setFromSet (indVars = basicInduction (loopReg, ebbi));
1133 /* find other induction variables : by other we mean definitions of */
1134 /* the form x := y (* | / ) <constant> .. we will move this one to */
1135 /* beginning of the loop and reduce strength i.e. replace with +/- */
1136 /* these expensive expressions: OH! and y must be induction too */
1137 for (lBlock = setFirstItem (loopReg->regBlocks), lastBlock = lBlock;
1138 lBlock && indVars; lBlock = setNextItem (loopReg->regBlocks))
1140 iCode *ic, *indIc, *dic;
1141 induction *ip;
1143 /* last block is the one with the highest block
1144 number */
1145 if (lastBlock->bbnum < lBlock->bbnum)
1146 lastBlock = lBlock;
1148 for (ic = lBlock->sch; ic; ic = ic->next)
1150 operand *aSym;
1151 operand *litSym;
1152 long litVal;
1154 /* consider only * & / */
1155 if (ic->op != '*' && ic->op != '/')
1156 continue;
1158 /* only consider variables with integral type */
1159 if (!IS_INTEGRAL (operandType (IC_RESULT (ic))))
1160 continue;
1162 /* if the result has more definitions then */
1163 if (assignmentsToSym (loopReg->regBlocks, IC_RESULT (ic)) > 1)
1164 continue;
1166 /* check if the operands are what we want */
1167 /* i.e. one of them an symbol the other a literal */
1168 if (!((IS_SYMOP (IC_LEFT (ic)) && IS_OP_LITERAL (IC_RIGHT (ic))) ||
1169 (IS_OP_LITERAL (IC_LEFT (ic)) && IS_SYMOP (IC_RIGHT (ic)))))
1170 continue;
1172 if (IS_SYMOP (IC_LEFT (ic)))
1174 aSym = IC_LEFT (ic);
1175 litSym = IC_RIGHT (ic);
1177 else
1179 /* For division, the literal must not be on the left side */
1180 if (ic->op == '/')
1181 continue;
1182 aSym = IC_RIGHT (ic);
1183 litSym = IC_LEFT (ic);
1185 litVal = (long) operandLitValue (litSym);
1187 ip = NULL;
1188 /* check if this is an induction variable */
1189 if (!applyToSetFTrue (basicInd, findInduction, aSym, &ip))
1190 continue;
1192 /* ask port for size not worth if native instruction
1193 exist for multiply & divide */
1194 if (port->hasNativeMulFor (ic, operandType (IC_LEFT (ic)), operandType (IC_RIGHT (ic))))
1195 continue;
1197 /* if this is a division then the remainder should be zero
1198 for it to be inducted */
1199 if (ic->op == '/' && (ip->cval % litVal))
1200 continue;
1202 /* create the iCode to be placed in the loop header */
1203 /* and create the induction object */
1205 /* create an instruction */
1206 /* this will be put on the loop header */
1207 indIc = newiCode (ic->op, operandFromOperand (aSym), operandFromOperand (litSym));
1208 indIc->filename = ic->filename;
1209 indIc->lineno = ic->lineno;
1210 IC_RESULT (indIc) = operandFromOperand (IC_RESULT (ic));
1211 OP_SYMBOL (IC_RESULT (indIc))->isind = 1;
1213 /* keep track of the inductions */
1214 litVal = (ic->op == '*' ? (litVal * ip->cval) : (ip->cval / litVal));
1216 addSet (&indVars, newInduction (IC_RESULT (ic), ip->op, litVal, indIc, NULL));
1218 /* Replace mul/div with assignment to self; killDeadCode() will */
1219 /* clean this up since we can't use remiCodeFromeBBlock() here. */
1220 ic->op = '=';
1221 IC_LEFT (ic) = NULL;
1222 IC_RIGHT (ic) = IC_RESULT (ic);
1223 bitVectUnSetBit (OP_USES (aSym), ic->key);
1225 /* Insert an update of the induction variable just before */
1226 /* the update of the basic induction variable. */
1227 indIc = newiCode (ip->op, operandFromOperand (IC_RESULT (ic)), operandFromLit (litVal));
1228 IC_RESULT (indIc) = operandFromOperand (IC_RESULT (ic));
1229 owner = NULL;
1230 dic = findDefInRegion (setFromSet (loopReg->regBlocks), aSym, &owner);
1231 assert (dic);
1232 addiCodeToeBBlock (owner, indIc, dic);
1236 /* if we have some induction variables then */
1237 if (indVars)
1239 eBBlock *preHdr = loopReg->entry->preHeader;
1240 iCode *icFirst = NULL, *icLast = NULL;
1241 induction *ip;
1242 bitVect *indVect = NULL;
1244 /* create an iCode chain from it */
1245 for (ip = setFirstItem (indVars); ip; ip = setNextItem (indVars))
1247 indVect = bitVectSetBit (indVect, ip->ic->key);
1248 ip->ic->filename = preHdr->ech->filename;
1249 ip->ic->lineno = preHdr->ech->lineno;
1250 if (!icFirst)
1251 icFirst = ip->ic;
1252 if (icLast)
1254 icLast->next = ip->ic;
1255 ip->ic->prev = icLast;
1256 icLast = ip->ic;
1258 else
1259 icLast = ip->ic;
1260 change++;
1263 /* add the instruction chain to the end of the */
1264 /* preheader for this loop */
1265 preHdr->ech->next = icFirst;
1266 icFirst->prev = preHdr->ech;
1267 preHdr->ech = icLast;
1268 icLast->next = NULL;
1270 /* add the induction variable vector to the last
1271 block in the loop */
1272 lastBlock->isLastInLoop = 1;
1273 lastBlock->linds = bitVectUnion (lastBlock->linds, indVect);
1276 setToNull ((void *) &indVars);
1277 return change;
1280 /*-----------------------------------------------------------------*/
1281 /* mergeRegions - will merge region with same entry point */
1282 /*-----------------------------------------------------------------*/
1283 static
1284 DEFSETFUNC (mergeRegions)
1286 region *theLoop = item;
1287 V_ARG (set *, allRegion);
1288 region *lp;
1290 /* if this has already been merged then do nothing */
1291 if (theLoop->merged)
1292 return 0;
1294 /* go thru all the region and check if any of them have the */
1295 /* entryPoint as the Loop */
1296 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1299 if (lp == theLoop)
1300 continue;
1302 if (lp->entry == theLoop->entry)
1304 theLoop->regBlocks = unionSets (theLoop->regBlocks, lp->regBlocks, THROW_DEST);
1305 lp->merged = 1;
1309 return 1;
1312 /*-----------------------------------------------------------------*/
1313 /* ifMerged - return 1 if the merge flag is 1 */
1314 /*-----------------------------------------------------------------*/
1315 static
1316 DEFSETFUNC (ifMerged)
1318 region *lp = item;
1320 return lp->merged;
1323 /*-----------------------------------------------------------------*/
1324 /* mergeInnerLoops - will merge into body when entry is present */
1325 /*-----------------------------------------------------------------*/
1326 static
1327 DEFSETFUNC (mergeInnerLoops)
1329 region *theLoop = item;
1330 V_ARG (set *, allRegion);
1331 V_ARG (int *, maxDepth);
1332 region *lp;
1334 /* check if the entry point is present in the body of any */
1335 /* loop then put the body of this loop into the outer loop */
1336 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1339 if (lp == theLoop)
1340 continue;
1342 if (isinSet (lp->regBlocks, theLoop->entry))
1344 lp->containsLoops += theLoop->containsLoops + 1;
1345 if (lp->containsLoops > (*maxDepth))
1346 *maxDepth = lp->containsLoops;
1348 lp->regBlocks = unionSets (lp->regBlocks, theLoop->regBlocks, THROW_DEST);
1352 return 1;
1356 /*-----------------------------------------------------------------*/
1357 /* createLoopRegions - will detect and create a set of natural loops */
1358 /*-----------------------------------------------------------------*/
1359 hTab *
1360 createLoopRegions (ebbIndex * ebbi)
1362 set *allRegion = NULL; /* set of all loops */
1363 hTab *orderedLoops = NULL;
1364 set *bEdges = NULL;
1365 int maxDepth = 0;
1366 region *lp;
1368 /* get all the back edges in the graph */
1369 if (!applyToSet (graphEdges, backEdges, &bEdges))
1370 return 0; /* found no loops */
1372 /* for each of these back edges get the blocks that */
1373 /* constitute the loops */
1374 applyToSet (bEdges, createLoop, &allRegion);
1376 /* now we will create regions from these loops */
1377 /* loops with the same entry points are considered to be the */
1378 /* same loop & they are merged. If the entry point of a loop */
1379 /* is found in the body of another loop then , all the blocks */
1380 /* in that loop are added to the loops containing the header */
1381 applyToSet (allRegion, mergeRegions, allRegion);
1383 /* delete those already merged */
1384 deleteItemIf (&allRegion, ifMerged);
1386 applyToSet (allRegion, mergeInnerLoops, allRegion, &maxDepth);
1387 maxDepth++;
1389 /* now create all the exits .. also */
1390 /* create an ordered set of loops */
1391 /* i.e. we process loops in the inner to outer order */
1392 for (lp = setFirstItem (allRegion); lp; lp = setNextItem (allRegion))
1394 applyToSet (lp->regBlocks, addToExitsMarkDepth, lp->regBlocks, &lp->exits, (maxDepth - lp->containsLoops), lp);
1396 hTabAddItem (&orderedLoops, lp->containsLoops, lp);
1399 return orderedLoops;
1402 /*-----------------------------------------------------------------*/
1403 /* loopOptimizations - identify region & remove invariants & ind */
1404 /*-----------------------------------------------------------------*/
1406 loopOptimizations (hTab * orderedLoops, ebbIndex * ebbi)
1408 region *lp;
1409 int change = 0;
1410 int k;
1412 /* if no loop optimizations requested */
1413 if (!optimize.loopInvariant && !optimize.loopInduction)
1414 return 0;
1416 /* now we process the loops inner to outer order */
1417 /* this is essential to maintain data flow information */
1418 /* the other choice is an ugly iteration for the depth */
1419 /* of the loops would hate that */
1420 for (lp = hTabFirstItem (orderedLoops, &k); lp; lp = hTabNextItem (orderedLoops, &k))
1423 if (optimize.loopInvariant)
1424 change += loopInvariants (lp, ebbi);
1426 if (optimize.loopInduction)
1427 change += loopInduction (lp, ebbi);
1430 return change;