1 /*-------------------------------------------------------------------------
3 SDCCdflow.c - source file for data flow analysis and other utility
4 routines related to data flow.
6 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding!
25 -------------------------------------------------------------------------*/
29 /*-----------------------------------------------------------------*/
30 /* ifKilledInBlock - will return 1 if the symbol is redefined in B */
31 /*-----------------------------------------------------------------*/
32 DEFSETFUNC (ifKilledInBlock)
35 V_ARG (eBBlock *, src);
38 /* if this is a global variable and this block
39 has a function call then delete it */
40 if (isOperandGlobal (cdp->sym) && src->hasFcall)
43 /* if this is pointer get then it will be killed
44 if there is a pointer set for the same pointer
46 if (POINTER_GET (cdp->diCode) &&
47 bitVectBitValue (src->ptrsSet,
48 IC_LEFT (cdp->diCode)->key))
51 /* if assignment to iTmep then if right is defined
52 elsewhere kill this one */
53 if (ASSIGNMENT (cdp->diCode) &&
54 !POINTER_SET (cdp->diCode) &&
55 IS_ITEMP (IC_RESULT (cdp->diCode)) &&
56 IS_SYMOP (IC_RIGHT (cdp->diCode)) &&
57 bitVectBitsInCommon (src->outDefs, OP_DEFS (IC_RIGHT (cdp->diCode))))
60 /* if we find it in the defSet of this block */
61 if (bitVectBitsInCommon (src->defSet, OP_DEFS (cdp->sym)))
64 /* if in the outdef we find a definition other than this one */
65 /* to do this we make a copy of the out definitions and turn */
66 /* this one off then check if there are other definitions */
67 bitVectUnSetBit (outs = bitVectCopy (src->outDefs),
69 if (bitVectBitsInCommon (outs, OP_DEFS (cdp->sym)))
71 setToNull ((void **) &outs);
75 setToNull ((void **) &outs);
77 /* if the operands of this one was changed in the block */
80 ((IS_SYMOP (IC_LEFT (cdp->diCode)) &&
81 bitVectBitsInCommon (src->defSet, OP_DEFS (IC_LEFT (cdp->diCode)))) ||
82 (IS_SYMOP (IC_RIGHT (cdp->diCode)) &&
83 bitVectBitsInCommon (src->defSet, OP_DEFS (IC_RIGHT (cdp->diCode))))))
89 /*-----------------------------------------------------------------*/
90 /* mergeInExprs - copy the in expression if it dominates */
91 /*-----------------------------------------------------------------*/
92 DEFSETFUNC (mergeInExprs)
95 V_ARG (eBBlock *, dest);
96 V_ARG (int *, firstTime);
98 /* if in the dominator list then */
99 if (bitVectBitValue (dest->domVect, ebp->bbnum) && dest != ebp)
101 /* if already present then intersect */
102 if (!dest->inExprs && *firstTime)
104 dest->inExprs = setFromSet (ebp->outExprs);
105 /* copy the pointer set from the dominator */
106 dest->inPtrsSet = bitVectCopy (ebp->ptrsSet);
107 dest->ndompset = bitVectCopy (ebp->ndompset);
111 dest->inExprs = intersectSets (dest->inExprs,
114 dest->inPtrsSet = bitVectUnion (dest->inPtrsSet, ebp->ptrsSet);
115 dest->ndompset = bitVectUnion (dest->ndompset, ebp->ndompset);
120 /* delete only if killed in this block */
121 deleteItemIf (&dest->inExprs, ifKilledInBlock, ebp);
122 /* union the ndompset with pointers set in this block */
123 dest->ndompset = bitVectUnion (dest->ndompset, ebp->ptrsSet);
131 /*-----------------------------------------------------------------*/
132 /* mergeInDefs - merge in incoming definitions */
133 /*-----------------------------------------------------------------*/
134 DEFSETFUNC (mergeInDefs)
137 V_ARG (eBBlock *, dest);
138 V_ARG (int *, firstTime);
140 /* the in definition is the union of the out */
141 /* of all blocks that come to this block */
142 if (!dest->inDefs && *firstTime)
143 dest->inDefs = bitVectCopy (ebp->outDefs);
145 dest->inDefs = bitVectUnion (dest->inDefs,
154 /*-----------------------------------------------------------------*/
155 /* computeDataFlow - does computations for data flow accross blocks */
156 /*-----------------------------------------------------------------*/
158 computeDataFlow (eBBlock ** ebbs, int count)
169 for (i = 0; i < count; i++)
177 /* if this is the entry block then continue */
178 /* since entry block can never have any inExprs */
182 /* get blocks that can come to this block */
183 pred = edgesTo (ebbs[i]);
185 /* make a copy of the outExpressions : to be */
186 /* used for iteration */
187 oldOut = setFromSet (ebbs[i]->outExprs);
188 setToNull ((void **) &ebbs[i]->inDefs);
190 /* indefitions are easy just merge them by union */
191 /* these are the definitions that can possibly */
192 /* reach this block */
194 applyToSet (pred, mergeInDefs, ebbs[i], &firstTime);
196 /* if none of the edges coming to this block */
197 /* dominate this block then add the immediate dominator */
198 /* of this block to the list of predecessors */
199 for (pBlock = setFirstItem (pred); pBlock;
200 pBlock = setNextItem (pred))
202 if (bitVectBitValue (ebbs[i]->domVect, pBlock->bbnum))
206 /* get the immediate dominator and put it there */
209 eBBlock *idom = immedDom (ebbs, ebbs[i]);
211 addSetHead (&pred, idom);
214 /* figure out the incoming expressions */
215 /* this is a little more complex */
216 setToNull ((void **) &ebbs[i]->inExprs);
217 if (optimize.global_cse)
220 applyToSet (pred, mergeInExprs, ebbs[i], &firstTime);
222 setToNull ((void **) &pred);
224 /* do cse with computeOnly flag set to TRUE */
225 /* this by far the quickest way of computing */
226 cseBBlock (ebbs[i], TRUE, ebbs, count);
228 /* if it change we will need to iterate */
229 change += !isSetsEqualWith (ebbs[i]->outExprs, oldOut, isCseDefEqual);
232 if (!change) /* iterate till no change */
239 /*-----------------------------------------------------------------*/
240 /* usedBetweenPoints - used between start & end */
241 /*-----------------------------------------------------------------*/
243 usedBetweenPoints (operand * op, iCode * start, iCode * end)
247 for (; lic != end; lic = lic->next)
250 /* if the operand is a parameter */
251 /* then check for calls and return */
252 /* true if there is a call */
257 if (lic->op == CALL) {
258 args=FUNC_ARGS(OP_SYMBOL(IC_LEFT(lic))->type);
260 args=FUNC_ARGS(OP_SYMBOL(IC_LEFT(lic))->type->next);
262 if (isParameterToCall (args, op))
269 /* if ifx then check the condition */
270 if (lic->op == IFX &&
271 IC_COND (lic)->key == op->key)
274 if (lic->op == JUMPTABLE &&
275 IC_JTCOND (lic)->key == op->key)
278 if (IC_RIGHT (lic) &&
279 IC_RIGHT (lic)->key == op->key)
283 IC_LEFT (lic)->key == op->key)
286 /* for a pointer assignment usage */
287 if (POINTER_SET (lic) &&
288 op->key == IC_RESULT (lic)->key)
290 else if (IC_RESULT (lic) && op->key == IC_RESULT (lic)->key)
299 /*-----------------------------------------------------------------*/
300 /* usedInRemaining - returns point of usage for an operand if found */
301 /*-----------------------------------------------------------------*/
303 usedInRemaining (operand * op, iCode * ic)
310 for (; lic; lic = lic->next)
313 /* if the operand is a parameter */
314 /* then check for calls and return */
315 /* true if there is a call */
316 /* if this is a global variable then
318 if (lic->op == CALL || lic->op == PCALL)
321 if (lic->op == CALL) {
322 args=FUNC_ARGS(OP_SYMBOL(IC_LEFT(lic))->type);
324 args=FUNC_ARGS(operandType(IC_LEFT(lic))->next);
327 isParameterToCall (args, op)) ||
328 isOperandGlobal (op))
332 if (ic->op == SEND &&
333 isOperandEqual (IC_LEFT (lic), op))
339 /* if ifx then check the condition */
340 if (lic->op == IFX &&
341 isOperandEqual (IC_COND (lic), op))
344 if (lic->op == JUMPTABLE &&
345 isOperandEqual (IC_JTCOND (lic), op))
348 if (IC_RIGHT (lic) &&
349 isOperandEqual (IC_RIGHT (lic), op))
353 isOperandEqual (IC_LEFT (lic), op))
356 /* for a pointer assignment usage */
357 if (POINTER_SET (lic) &&
358 isOperandEqual (op, IC_RESULT (lic)))
360 else if (IC_RESULT (lic) && isOperandEqual (IC_RESULT (lic), op))
369 /*-----------------------------------------------------------------*/
370 /* isDefAlive - will return true if definiton reaches a block & used */
371 /*-----------------------------------------------------------------*/
372 DEFSETFUNC (isDefAlive)
375 V_ARG (iCode *, diCode);
382 /* if this definition is used in the block */
383 if (bitVectBitValue (ebp->usesDefs, diCode->key))
386 return applyToSet (ebp->succList, isDefAlive, diCode);