- int loopInvariants( region *theLoop , eBBlock **ebbs, int count)
-{
- eBBlock *lBlock ;
- set *lInvars = NULL ;
-
- int change = 0 ;
-
- /* if the preHeader does not exist then do nothing */
- /* or no exits then do nothing ( have to think about this situation */
- if (theLoop->entry->preHeader == NULL ||
- theLoop->exits == NULL )
- return 0;
-
- /* we will do the elimination for those blocks */
- /* in the loop that dominates all exits from the loop */
- for (lBlock = setFirstItem(theLoop->regBlocks); lBlock;
- lBlock = setNextItem(theLoop->regBlocks)) {
-
- iCode *ic;
- int domsAllExits ;
- int i ;
-
- /* mark the dominates all exits flag */
- domsAllExits = ( applyToSet (theLoop->exits,dominatedBy,lBlock) ==
- elementsInSet (theLoop->exits));
-
-
- /* now we go thru the instructions of this block and */
- /* collect those instructions with invariant operands*/
- for ( ic = lBlock->sch ; ic ; ic = ic->next ) {
-
- int lin , rin ;
- cseDef *ivar ;
-
- if (SKIP_IC(ic) || POINTER_SET(ic) || ic->op == IFX)
- continue ;
-
- /* if result is volatile then skip */
- if (IC_RESULT(ic) &&
- ( isOperandVolatile(IC_RESULT(ic),TRUE) ||
- IS_OP_PARM(IC_RESULT(ic))))
- continue ;
-
- lin = rin = 0 ;
-
- /* special case */
- /* if address of then it is an invariant */
- if (ic->op == ADDRESS_OF &&
- IS_SYMOP(IC_LEFT(ic)) &&
- IS_AGGREGATE(operandType(IC_LEFT(ic))))
- lin++;
- else
- /* check if left operand is an invariant */
- if ( (lin = isOperandInvariant (IC_LEFT(ic),theLoop,lInvars)))
- /* if this is a pointer get then make sure
- that the pointer set does not exist in
- any of the blocks */
- if (POINTER_GET(ic) &&
- ( applyToSet (theLoop->regBlocks,pointerAssigned,IC_LEFT(ic)) ))
- lin = 0;
-
- /* do the same for right */
- rin = isOperandInvariant (IC_RIGHT(ic),theLoop, lInvars);
-
- /* if this is a POINTER_GET then special case, make sure all
- usages within the loop are POINTER_GET any other usage
- would mean that this is not an invariant , since the pointer
- could then be passed as a parameter */
- if (POINTER_GET(ic) &&
- applyToSet(theLoop->regBlocks,hasNonPtrUse,IC_LEFT(ic)))
- continue ;
-
- /* if both the left & right are invariants : then check that*/
- /* this definition exists in the out definition of all the */
- /* blocks, this will ensure that this is not assigned any */
- /* other value in the loop , and not used in this block */
- /* prior to this definition which means only this definition*/
- /* is used in this loop */
- if (lin && rin && IC_RESULT(ic)) {
- eBBlock *sBlock ;
- set *lSet = setFromSet(theLoop->regBlocks);
-
- /* if this block does not dominate all exists */
- /* make sure this defintion is not used anywhere else */
- if (!domsAllExits) {
-
- if (isOperandGlobal(IC_RESULT(ic)))
- continue;
- /* for successors for all exits */
- for ( sBlock = setFirstItem(theLoop->exits); sBlock;
- sBlock = setNextItem(theLoop->exits)) {
-
- for(i=0; i < count; ebbs[i++]->visited = 0);
- lBlock->visited = 1;
- if ( applyToSet (sBlock->succList,isDefAlive,ic))
- break ;
+int
+loopInvariants (region * theLoop, eBBlock ** ebbs, int count)
+{
+ eBBlock *lBlock;
+ set *lInvars = NULL;
+
+ int change = 0;
+ int fCallsInBlock;
+
+ /* if the preHeader does not exist then do nothing */
+ /* or no exits then do nothing ( have to think about this situation */
+ if (theLoop->entry->preHeader == NULL ||
+ theLoop->exits == NULL)
+ return 0;
+
+ /* we will do the elimination for those blocks */
+ /* in the loop that dominates all exits from the loop */
+ for (lBlock = setFirstItem (theLoop->regBlocks); lBlock;
+ lBlock = setNextItem (theLoop->regBlocks))
+ {
+
+ iCode *ic;
+ int domsAllExits;
+ int i;
+
+ /* mark the dominates all exits flag */
+ domsAllExits = (applyToSet (theLoop->exits, dominatedBy, lBlock) ==
+ elementsInSet (theLoop->exits));
+
+ /* find out if we have a function call in this block */
+ for (ic = lBlock->sch, fCallsInBlock=0; ic; ic = ic->next) {
+ if (SKIP_IC(ic)) {
+ fCallsInBlock++;
+ }
+ }
+
+ /* now we go thru the instructions of this block and */
+ /* collect those instructions with invariant operands */
+ for (ic = lBlock->sch; ic; ic = ic->next)
+ {
+
+ int lin, rin;
+ cseDef *ivar;
+
+ /* TODO this is only needed if the call is between
+ here and the definition, but I am too lazy to do that now */
+
+ /* if there are function calls in this block */
+ if (fCallsInBlock) {
+
+ /* if this is a pointer get */
+ if (POINTER_GET(ic)) {
+ continue;
+ }
+
+ /* if this is an assignment from a global */
+ if (ic->op=='=' && isOperandGlobal(IC_RIGHT(ic))) {
+ continue;
+ }
+ }
+
+ if (SKIP_IC (ic) || POINTER_SET (ic) || ic->op == IFX)
+ continue;
+
+ /* iTemp assignment from a literal may be invariant, but it
+ will needlessly increase register pressure if the
+ iCode(s) that use this iTemp are not also invariant */
+ if (ic->op=='=' && IS_ITEMP (IC_RESULT (ic))
+ && IS_OP_LITERAL (IC_RIGHT (ic)))
+ continue;
+
+ /* if result is volatile then skip */
+ if (IC_RESULT (ic) &&
+ (isOperandVolatile (IC_RESULT (ic), TRUE) ||
+ IS_OP_PARM (IC_RESULT (ic))))
+ continue;
+
+ /* if result depends on a volatile then skip */
+ if ((IC_LEFT(ic) && isOperandVolatile(IC_LEFT(ic), TRUE)) ||
+ (IC_RIGHT(ic) && isOperandVolatile(IC_RIGHT(ic), TRUE)))
+ continue;
+
+ lin = rin = 0;
+
+ /* special case */
+ /* if address of then it is an invariant */
+ if (ic->op == ADDRESS_OF &&
+ IS_SYMOP (IC_LEFT (ic)) &&
+ IS_AGGREGATE (operandType (IC_LEFT (ic))))
+ lin++;
+ else {
+ /* check if left operand is an invariant */
+ if ((lin = isOperandInvariant (IC_LEFT (ic), theLoop, lInvars)))
+ /* if this is a pointer get then make sure
+ that the pointer set does not exist in
+ any of the blocks */
+ if (POINTER_GET (ic) &&
+ (applyToSet (theLoop->regBlocks,
+ pointerAssigned, IC_LEFT (ic))))
+ lin = 0;
+ }
+
+ /* do the same for right */
+ rin = isOperandInvariant (IC_RIGHT (ic), theLoop, lInvars);
+
+ /* if this is a POINTER_GET then special case, make sure all
+ usages within the loop are POINTER_GET any other usage
+ would mean that this is not an invariant , since the pointer
+ could then be passed as a parameter */
+ if (POINTER_GET (ic) &&
+ applyToSet (theLoop->regBlocks, hasNonPtrUse, IC_LEFT (ic)))
+ continue;
+
+
+ /* if both the left & right are invariants : then check that */
+ /* this definition exists in the out definition of all the */
+ /* blocks, this will ensure that this is not assigned any */
+ /* other value in the loop , and not used in this block */
+ /* prior to this definition which means only this definition */
+ /* is used in this loop */
+ if (lin && rin && IC_RESULT (ic))
+ {
+ eBBlock *sBlock;
+ set *lSet = setFromSet (theLoop->regBlocks);
+
+ /* if this block does not dominate all exits */
+ /* make sure this defintion is not used anywhere else */
+ if (!domsAllExits)
+ {
+
+ if (isOperandGlobal (IC_RESULT (ic)))
+ continue;
+ /* for successors for all exits */
+ for (sBlock = setFirstItem (theLoop->exits); sBlock;
+ sBlock = setNextItem (theLoop->exits))
+ {
+
+ for (i = 0; i < count; ebbs[i++]->visited = 0);
+ lBlock->visited = 1;
+ if (applyToSet (sBlock->succList, isDefAlive, ic))
+ break;