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
, long constVal
, iCode
* ic
, operand
* asym
)
41 ip
= Safe_alloc (sizeof (induction
));
48 //updateSpillLocation(ic,1);
52 /*-----------------------------------------------------------------*/
53 /* newRegion - allocate & returns a loop structure */
54 /*-----------------------------------------------------------------*/
60 lp
= Safe_alloc (sizeof (region
));
67 /*-----------------------------------------------------------------*/
68 /* pinduction - prints induction */
69 /*-----------------------------------------------------------------*/
71 DEFSETFUNC (pinduction
)
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
);
84 /*-----------------------------------------------------------------*/
85 /* pregion - prints loop information */
86 /*-----------------------------------------------------------------*/
92 printf ("================\n");
93 printf (" loop with entry -- > ");
94 printEntryLabel (lp
->entry
, ap
);
96 printf (" loop body --> ");
97 applyToSet (lp
->regBlocks
, printEntryLabel
);
99 printf (" loop exits --> ");
100 applyToSet (lp
->exits
, printEntryLabel
);
106 /*-----------------------------------------------------------------*/
107 /* backEdges - returns a list of back edges */
108 /*-----------------------------------------------------------------*/
110 DEFSETFUNC (backEdges
)
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
);
127 /*-----------------------------------------------------------------*/
128 /* intersectLoopSucc - returns intersection of loop Successors */
129 /*-----------------------------------------------------------------*/
131 intersectLoopSucc (set
* lexits
, eBBlock
** ebbs
)
133 bitVect
*succVect
= NULL
;
134 eBBlock
*exit
= setFirstItem (lexits
);
139 succVect
= bitVectCopy (exit
->succVect
);
141 for (exit
= setNextItem (lexits
); exit
; exit
= setNextItem (lexits
))
143 succVect
= bitVectIntersect (succVect
, exit
->succVect
);
149 /*-----------------------------------------------------------------*/
150 /* loopInsert will insert a block into the loop set */
151 /*-----------------------------------------------------------------*/
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 /*-----------------------------------------------------------------*/
166 DEFSETFUNC (insertIntoLoop
)
169 V_ARG (set
**, regionSet
);
171 loopInsert (regionSet
, ebp
);
175 /*-----------------------------------------------------------------*/
176 /* isNotInBlocks - will return 1 if not is blocks */
177 /*-----------------------------------------------------------------*/
179 DEFSETFUNC (isNotInBlocks
)
182 V_ARG (set
*, blocks
);
184 if (!isinSet (blocks
, ebp
))
191 /*-----------------------------------------------------------------*/
192 /* hasIncomingDefs - has definitions coming into the loop. i.e. */
193 /* check to see if the preheaders outDefs has any definitions */
194 /*-----------------------------------------------------------------*/
196 hasIncomingDefs (region
* lreg
, operand
* op
)
198 eBBlock
*preHdr
= lreg
->entry
->preHeader
;
200 if (preHdr
&& bitVectBitsInCommon (preHdr
->outDefs
, OP_DEFS (op
)))
205 /*-----------------------------------------------------------------*/
206 /* findLoopEndSeq - will return the sequence number of the last */
207 /* iCode with the maximum dfNumber in the region */
208 /*-----------------------------------------------------------------*/
210 findLoopEndSeq (region
* lreg
)
215 for (block
= lblock
= setFirstItem (lreg
->regBlocks
); block
; block
= setNextItem (lreg
->regBlocks
))
217 if (block
!= lblock
&& block
->lSeq
> lblock
->lSeq
)
225 /*-----------------------------------------------------------------*/
226 /* addToExitsMarkDepth - will add the the exitSet all blocks that */
227 /* have exits, will also update the depth field in the blocks */
228 /*-----------------------------------------------------------------*/
230 DEFSETFUNC (addToExitsMarkDepth
)
233 V_ARG (set
*, loopBlocks
);
234 V_ARG (set
**, exits
);
236 V_ARG (region
*, lr
);
238 /* mark the loop depth of this block */
240 if (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
);
259 /*-----------------------------------------------------------------*/
260 /* createLoop - will create a set of region */
261 /*-----------------------------------------------------------------*/
263 DEFSETFUNC (createLoop
)
266 V_ARG (set
**, allRegion
);
267 region
*aloop
= newRegion ();
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 */
283 applyToSet (block
->predList
, insertIntoLoop
, &aloop
->regBlocks
);
286 aloop
->entry
= ep
->to
;
288 /* now add it to the set */
289 addSetHead (allRegion
, aloop
);
293 /*-----------------------------------------------------------------*/
294 /* dominatedBy - will return 1 if item is dominated by block */
295 /*-----------------------------------------------------------------*/
297 DEFSETFUNC (dominatedBy
)
300 V_ARG (eBBlock
*, block
);
302 return bitVectBitValue (ebp
->domVect
, block
->bbnum
);
305 /*-----------------------------------------------------------------*/
306 /* addDefInExprs - adds an expression into the inexpressions */
307 /*-----------------------------------------------------------------*/
309 DEFSETFUNC (addDefInExprs
)
312 V_ARG (cseDef
*, cdp
);
313 V_ARG (ebbIndex
*, ebbi
);
315 addSetHead (&ebp
->inExprs
, cdp
);
316 cseBBlock (ebp
, optimize
.global_cse
, ebbi
);
320 /*-----------------------------------------------------------------*/
321 /* assignmentsToSym - for a set of blocks determine # time assigned */
322 /*-----------------------------------------------------------------*/
324 assignmentsToSym (set
* sset
, operand
* sym
)
328 set
*blocks
= setFromSet (sset
);
330 for (ebp
= setFirstItem (blocks
); ebp
; ebp
= setNextItem (blocks
))
332 /* get all the definitions for this symbol
334 bitVect
*defs
= bitVectIntersect (ebp
->ldefs
, OP_DEFS (sym
));
335 assigns
+= bitVectnBitsOn (defs
);
336 setToNull ((void *) &defs
);
343 /*-----------------------------------------------------------------*/
344 /* isOperandInvariant - determines if an operand is an invariant */
345 /*-----------------------------------------------------------------*/
347 isOperandInvariant (operand
* op
, region
* theLoop
, set
* lInvars
)
350 /* operand is an invariant if it is a */
352 /* b. that have definitions reaching loop entry */
353 /* c. that are already defined as invariant */
354 /* d. has no assignments in the loop */
357 if (IS_OP_LITERAL (op
))
359 else if (IS_SYMOP (op
) && OP_SYMBOL (op
)->addrtaken
)
361 else if (ifDefSymIs (theLoop
->entry
->inExprs
, op
))
363 else if (ifDefSymIs (lInvars
, op
))
365 else if (IS_SYMOP (op
) && !IS_OP_GLOBAL (op
) && !IS_OP_VOLATILE (op
) && assignmentsToSym (theLoop
->regBlocks
, op
) == 0)
374 /*-----------------------------------------------------------------*/
375 /* pointerAssigned - will return 1 if pointer set found */
376 /*-----------------------------------------------------------------*/
378 DEFSETFUNC (pointerAssigned
)
381 V_ARG (operand
*, op
);
386 if (bitVectBitValue (ebp
->ptrsSet
, op
->key
))
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 */
395 if (!bitVectIsZero (ebp
->ptrsSet
))
401 /*-----------------------------------------------------------------*/
402 /* hasNonPtrUse - returns true if operand has non pointer usage */
403 /*-----------------------------------------------------------------*/
405 DEFSETFUNC (hasNonPtrUse
)
408 V_ARG (operand
*, op
);
409 iCode
*ic
= usedInRemaining (op
, ebp
->sch
);
411 if (ic
&& !POINTER_SET (ic
) && !POINTER_GET (ic
))
418 /*-----------------------------------------------------------------*/
419 /* loopInvariants - takes loop invariants out of region */
420 /*-----------------------------------------------------------------*/
422 loopInvariants (region
*theLoop
, ebbIndex
*ebbi
)
424 eBBlock
**ebbs
= ebbi
->dfOrder
;
425 int count
= ebbi
->count
;
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
)
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
))
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
)
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
)
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 */
468 /* if this is a pointer get */
469 if (POINTER_GET (ic
))
474 /* if this is an assignment from a global */
475 if (ic
->op
== '=' && isOperandGlobal (IC_RIGHT (ic
)))
481 * if this is an assignment to a global */
482 if (ic
->op
== '=' && isOperandGlobal (IC_RESULT (ic
)))
488 if (SKIP_IC (ic
) || POINTER_SET (ic
) || ic
->op
== IFX
)
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
)))
497 /* if result is volatile then skip */
498 if (IC_RESULT (ic
) && (isOperandVolatile (IC_RESULT (ic
), TRUE
) || IS_OP_PARM (IC_RESULT (ic
))))
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
)))
506 if (POINTER_GET (ic
) && IS_VOLATILE (operandType (IC_LEFT (ic
))->next
))
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
))))
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
524 POINTER_GET (ic
) && applyToSet (theLoop
->regBlocks
, pointerAssigned
, IC_LEFT (ic
)))
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
)))
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
))
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 */
556 if (isOperandGlobal (IC_RESULT (ic
)))
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);
563 if (applyToSet (sBlock
->succList
, isDefAlive
, ic
))
567 /* we have found usage */
572 /* now make sure this is the only definition */
573 for (sBlock
= setFirstItem (lSet
); sBlock
; sBlock
= setNextItem (lSet
))
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
))
586 else if (bitVectBitsInCommon (sBlock
->defSet
, OP_DEFS (IC_RESULT (ic
))))
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
);
595 for (ic2
= sBlock
->sch
; ic2
; ic2
= ic2
->next
)
599 if (isOperandEqual (IC_RESULT (ic
), IC_COND (ic2
)))
602 else if (ic2
->op
== JUMPTABLE
)
604 if (isOperandEqual (IC_RESULT (ic
), IC_JTCOND (ic2
)))
609 if (IC_LEFT (ic2
) && isOperandEqual (IC_RESULT (ic
), IC_LEFT (ic2
)))
611 if (IC_RIGHT (ic2
) && isOperandEqual (IC_RESULT (ic
), IC_RIGHT (ic2
)))
613 if ((ic
!= ic2
) && (isOperandEqual (IC_RESULT (ic
), IC_RESULT (ic2
))))
615 /* If used before this definition, might not be invariant */
616 if ((ic
== ic2
) && used
)
619 if (used
&& !defDominates
)
622 if (ic2
) /* found another definition or a usage before the definition */
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
)
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
))
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 */
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
);
677 icFirst
= cdp
->diCode
;
680 icLast
->next
= cdp
->diCode
;
681 cdp
->diCode
->prev
= icLast
;
682 icLast
= cdp
->diCode
;
685 icLast
= cdp
->diCode
;
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
;
700 /*-----------------------------------------------------------------*/
701 /* addressTaken - returns true if the symbol is found in the addrof */
702 /*-----------------------------------------------------------------*/
704 addressTaken (set
* sset
, operand
* sym
)
710 for (loop
= sset
; loop
; loop
= loop
->next
)
716 if (isOperandEqual ((operand
*) loop2
->item
, sym
))
726 /*-----------------------------------------------------------------*/
727 /* findInduction :- returns 1 & the item if the induction is found */
728 /*-----------------------------------------------------------------*/
730 DEFSETFUNC (findInduction
)
732 induction
*ip
= item
;
733 V_ARG (operand
*, sym
);
734 V_ARG (induction
**, ipp
);
736 if (isOperandEqual (ip
->sym
, sym
))
745 /*-----------------------------------------------------------------*/
746 /* findDefInRegion - finds the definition within the region */
747 /*-----------------------------------------------------------------*/
749 findDefInRegion (set
* regBlocks
, operand
* defOp
, eBBlock
** owner
)
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
)))
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
))
776 /*-----------------------------------------------------------------*/
777 /* addPostLoopBlock - add a ebblock before the successors of the */
779 /*-----------------------------------------------------------------*/
781 addPostLoopBlock (region
* loopReg
, ebbIndex
* ebbi
, iCode
* ic
)
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
);
801 for (i
= 0; i
< loopSuccs
->size
; i
++)
803 eBBlock
*eblock
= NULL
;
804 eBBlock
*postLoopBlk
;
808 if (!bitVectBitValue (loopSuccs
, i
))
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
];
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
))
826 if (eblock
->dfnum
<= loopReg
->entry
->dfnum
)
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
;
837 /* create and insert a new eBBlock.
838 Damn, that's messy ... */
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;
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 */
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
);
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 /*-----------------------------------------------------------------*/
952 basicInduction (region
* loopReg
, ebbIndex
* ebbi
)
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
))
964 /* for all instructions in the blocks do */
965 for (ic
= lBlock
->sch
; ic
; ic
= ic
->next
)
971 eBBlock
*owner
= NULL
;
975 /* To find an induction variable, we need to */
976 /* find within the loop three iCodes in this */
979 /* ddic: iTempB := symbolVar */
980 /* dic: iTempA := iTempB + lit */
981 /* or iTempA := iTempB - lit */
982 /* or iTempA := lit + iTempB */
983 /* ic: symbolVar := iTempA */
985 /* (symbolVar may also be an iTemp if it is */
986 /* register equivalent) */
988 /* look for assignments of the form */
989 /* symbolVar := iTempNN */
993 if (!IS_SYMOP (IC_RESULT (ic
)))
996 if (!IS_TRUE_SYMOP (IC_RESULT (ic
)) && !OP_SYMBOL (IC_RESULT (ic
))->isreqv
)
999 if (isOperandGlobal (IC_RESULT (ic
)))
1002 if (!IS_ITEMP (IC_RIGHT (ic
)))
1005 /* if it has multiple assignments within the loop then skip */
1006 if (assignmentsToSym (loopReg
->regBlocks
, IC_RESULT (ic
)) > 1)
1009 /* if the address of this was taken inside the loop then continue */
1010 if (addressTaken (loopReg
->regBlocks
, IC_RESULT (ic
)))
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
))
1022 /* find the definition for the result in the block */
1023 if (!(dic
= findDefInRegion (setFromSet (loopReg
->regBlocks
), IC_RIGHT (ic
), &owner
)))
1026 /* if not +/- continue */
1027 if (dic
->op
!= '+' && dic
->op
!= '-')
1030 /* make sure definition is of the form a +/- c */
1031 if (!IS_OP_LITERAL (IC_LEFT (dic
)) && !IS_OP_LITERAL (IC_RIGHT (dic
)))
1034 /* make sure the definition found is the only one */
1035 if (assignmentsToSym (loopReg
->regBlocks
, IC_RIGHT (ic
)) > 1)
1038 if (IS_OP_LITERAL (IC_RIGHT (dic
)))
1040 aSym
= IC_LEFT (dic
);
1041 litValue
= (long) operandLitValue (IC_RIGHT (dic
));
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) */
1050 aSym
= IC_RIGHT (dic
);
1051 litValue
= (long) operandLitValue (IC_LEFT (dic
));
1054 if (!isOperandEqual (IC_RESULT (ic
), aSym
) && !isOperandEqual (IC_RIGHT (ic
), aSym
))
1057 /* find the definition for this and check */
1058 if (!(ddic
= findDefInRegion (setFromSet (loopReg
->regBlocks
), aSym
, &owner
)))
1061 if (ddic
->op
!= '=')
1064 if (!isOperandEqual (IC_RESULT (ddic
), aSym
) || !isOperandEqual (IC_RIGHT (ddic
), IC_RESULT (ic
)))
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)
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
))
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
);
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
);
1108 addPostLoopBlock (loopReg
, ebbi
, ic
);
1109 addSet (&indVars
, ip
);
1111 } /* end of all blocks for basic induction variables */
1116 /*-----------------------------------------------------------------*/
1117 /* loopInduction - remove induction variables from a loop */
1118 /*-----------------------------------------------------------------*/
1120 loopInduction (region
* loopReg
, ebbIndex
* ebbi
)
1123 eBBlock
*lBlock
, *owner
, *lastBlock
= NULL
;
1124 set
*indVars
= NULL
;
1125 set
*basicInd
= NULL
;
1127 if (loopReg
->entry
->preHeader
== NULL
)
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
;
1143 /* last block is the one with the highest block
1145 if (lastBlock
->bbnum
< lBlock
->bbnum
)
1148 for (ic
= lBlock
->sch
; ic
; ic
= ic
->next
)
1154 /* consider only * & / */
1155 if (ic
->op
!= '*' && ic
->op
!= '/')
1158 /* only consider variables with integral type */
1159 if (!IS_INTEGRAL (operandType (IC_RESULT (ic
))))
1162 /* if the result has more definitions then */
1163 if (assignmentsToSym (loopReg
->regBlocks
, IC_RESULT (ic
)) > 1)
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
)))))
1172 if (IS_SYMOP (IC_LEFT (ic
)))
1174 aSym
= IC_LEFT (ic
);
1175 litSym
= IC_RIGHT (ic
);
1179 /* For division, the literal must not be on the left side */
1182 aSym
= IC_RIGHT (ic
);
1183 litSym
= IC_LEFT (ic
);
1185 litVal
= (long) operandLitValue (litSym
);
1188 /* check if this is an induction variable */
1189 if (!applyToSetFTrue (basicInd
, findInduction
, aSym
, &ip
))
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
))))
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
))
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. */
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
));
1230 dic
= findDefInRegion (setFromSet (loopReg
->regBlocks
), aSym
, &owner
);
1232 addiCodeToeBBlock (owner
, indIc
, dic
);
1236 /* if we have some induction variables then */
1239 eBBlock
*preHdr
= loopReg
->entry
->preHeader
;
1240 iCode
*icFirst
= NULL
, *icLast
= NULL
;
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
;
1254 icLast
->next
= ip
->ic
;
1255 ip
->ic
->prev
= icLast
;
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
);
1280 /*-----------------------------------------------------------------*/
1281 /* mergeRegions - will merge region with same entry point */
1282 /*-----------------------------------------------------------------*/
1284 DEFSETFUNC (mergeRegions
)
1286 region
*theLoop
= item
;
1287 V_ARG (set
*, allRegion
);
1290 /* if this has already been merged then do nothing */
1291 if (theLoop
->merged
)
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
))
1302 if (lp
->entry
== theLoop
->entry
)
1304 theLoop
->regBlocks
= unionSets (theLoop
->regBlocks
, lp
->regBlocks
, THROW_DEST
);
1312 /*-----------------------------------------------------------------*/
1313 /* ifMerged - return 1 if the merge flag is 1 */
1314 /*-----------------------------------------------------------------*/
1316 DEFSETFUNC (ifMerged
)
1323 /*-----------------------------------------------------------------*/
1324 /* mergeInnerLoops - will merge into body when entry is present */
1325 /*-----------------------------------------------------------------*/
1327 DEFSETFUNC (mergeInnerLoops
)
1329 region
*theLoop
= item
;
1330 V_ARG (set
*, allRegion
);
1331 V_ARG (int *, maxDepth
);
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
))
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
);
1356 /*-----------------------------------------------------------------*/
1357 /* createLoopRegions - will detect and create a set of natural loops */
1358 /*-----------------------------------------------------------------*/
1360 createLoopRegions (ebbIndex
* ebbi
)
1362 set
*allRegion
= NULL
; /* set of all loops */
1363 hTab
*orderedLoops
= NULL
;
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
);
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
)
1412 /* if no loop optimizations requested */
1413 if (!optimize
.loopInvariant
&& !optimize
.loopInduction
)
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
);