1 /*-------------------------------------------------------------------------
3 SDCCcflow.c - source file for control flow analysis
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 -------------------------------------------------------------------------*/
27 #include "SDCCglobl.h"
31 #include "SDCChasht.h"
34 #include "SDCCicode.h"
35 #include "SDCClabel.h"
36 #include "SDCCBBlock.h"
38 #include "SDCCcflow.h"
40 /*-----------------------------------------------------------------*/
41 /* domSetFromVect - creates a domset from the vector */
42 /*-----------------------------------------------------------------*/
43 set *domSetFromVect (eBBlock **ebbs, bitVect *domVect)
51 for (i = 0 ; i < domVect->size ;i++ )
52 if (bitVectBitValue(domVect,i))
53 addSet(&domSet,ebbs[i]);
58 /*-----------------------------------------------------------------*/
59 /* addSuccessor - will add bb to succ also add it to the pred of */
61 /*-----------------------------------------------------------------*/
62 void addSuccessor (eBBlock *thisBlock, eBBlock *succ )
64 /* check for boundary conditions */
65 if ( ! thisBlock || ! succ )
68 /* add it to the succ of thisBlock */
69 addSetIfnotP (&thisBlock->succList,succ);
72 bitVectSetBit(thisBlock->succVect,succ->bbnum);
73 /* add this edge to the list of edges */
74 addSet(&graphEdges,newEdge(thisBlock,succ));
78 /*-----------------------------------------------------------------*/
79 /* eBBPredecessors - find the predecessors for each block */
80 /*-----------------------------------------------------------------*/
81 void eBBPredecessors ( eBBlock **ebbs, int count)
85 /* for each block do */
86 for ( i = 0 ; i < count ; i++ ) {
88 /* if there is no path to this then continue */
92 /* for each successor of this block if */
93 /* it has depth first number > this block */
94 /* then this block precedes the successor */
95 for (j = 0; j < ebbs[i]->succVect->size ; j++)
97 if ( bitVectBitValue(ebbs[i]->succVect,j) &&
98 ebbs[j]->dfnum > ebbs[i]->dfnum )
100 addSet(&ebbs[j]->predList,ebbs[i]);
104 /*-----------------------------------------------------------------*/
105 /* eBBSuccessors- find out the successors of all the nodes */
106 /*-----------------------------------------------------------------*/
107 void eBBSuccessors ( eBBlock **ebbs , int count )
111 /* for all the blocks do */
112 for (; i < count; i++ ) {
118 ebbs[i]->succVect = newBitVect(count);
120 /* if the next on exists & this one does not */
121 /* end in a GOTO or RETURN then the next is */
122 /* a natural successor of this. Note we have */
123 /* consider eBBlocks with no instructions */
128 if (ebbs[i]->ech->op != GOTO &&
129 ebbs[i]->ech->op != RETURN &&
130 ebbs[i]->ech->op != JUMPTABLE) {
133 while (ebbs[j] && ebbs[j]->noPath) j++;
135 addSuccessor (ebbs[i],ebbs[j]); /* add it */
137 } /* no instructions in the block */
138 /* could happen for dummy blocks*/
140 addSuccessor (ebbs[i],ebbs[i+1]);
143 /* go thru all the instructions: if we find a */
144 /* goto or ifx or a return then we have a succ*/
145 if ((ic = ebbs[i]->ech)) {
148 /* special case for jumptable */
149 if (ic->op == JUMPTABLE ) {
151 for (lbl = setFirstItem(IC_JTLABELS(ic)); lbl;
152 lbl = setNextItem(IC_JTLABELS(ic)))
153 addSuccessor(ebbs[i],
154 eBBWithEntryLabel(ebbs,lbl,count));
158 /* depending on the instruction operator */
160 case GOTO : /* goto has edge to label */
161 succ = eBBWithEntryLabel (ebbs,ic->argLabel.label,count);
164 case IFX : /* conditional jump */
165 /* if true label is present */
167 succ = eBBWithEntryLabel (ebbs,IC_TRUE(ic),count);
169 succ = eBBWithEntryLabel (ebbs,IC_FALSE(ic),count);
172 case RETURN : /* block with return */
173 succ = eBBWithEntryLabel (ebbs,returnLabel,count);
177 /* if there is a successor add to the list */
178 /* if it is not already present in the list*/
180 addSuccessor(ebbs[i],succ);
186 /*-----------------------------------------------------------------*/
187 /* computeDominance - computes the dominance graph */
188 /* for algorithm look at Dragon book section 10.10, algo 10.16 */
189 /*-----------------------------------------------------------------*/
190 void computeDominance ( eBBlock **ebbs, int count )
194 /* now do the initialisation */
195 /* D(n0) := { n0 } */
197 bitVectSetBit(ebbs[0]->domVect = newBitVect(count),ebbs[0]->bbnum);
200 /* for n in N - { n0 } do D(n) = N */
201 for (i = 1 ; i < count ; i++ ) {
202 ebbs[i]->domVect = newBitVect(count);
203 for (j = 0 ; j < count ; j++ ) {
205 bitVectSetBit(ebbs[i]->domVect,ebbs[j]->bbnum);
209 /* end of initialisation */
211 /* while changes to any D(n) occur do */
212 /* for n in N - { n0 } do */
213 /* D(n) := { n } U (intersection of D( all predecessors of n)) */
218 for ( i = 1 ; i < count ; i++ ) {
224 /* get the intersection of the dominance of all predecessors */
225 for (pred = setFirstItem(ebbs[i]->predList) ,
226 cDomVect = (pred ? bitVectCopy(pred->domVect) : NULL) ;
228 pred = setNextItem(ebbs[i]->predList)) {
229 cDomVect = bitVectIntersect(cDomVect,pred->domVect);
232 cDomVect = newBitVect(count);
233 /* this node to the list */
234 cDomVect = bitVectSetBit(cDomVect,ebbs[i]->bbnum);
237 if (!bitVectEqual(cDomVect,ebbs[i]->domVect)) {
238 ebbs[i]->domVect = cDomVect;
243 /* if no change then exit */
249 /*-----------------------------------------------------------------*/
250 /* immedDom - returns the immediate dominator of a block */
251 /*-----------------------------------------------------------------*/
252 eBBlock *immedDom (eBBlock **ebbs,eBBlock *ebp)
254 /* first delete self from the list */
255 set *iset = domSetFromVect(ebbs,ebp->domVect);
257 eBBlock *idom = NULL;
259 deleteSetItem(&iset,ebp);
260 /* then just return the one with the greatest */
261 /* depthfirst number, this will be the immed dominator */
262 if ((loop = setFirstItem(iset)))
264 for (; loop; loop = setNextItem(iset))
265 if (loop->dfnum > idom->dfnum)
268 setToNull ((void **)&iset);
273 /*-----------------------------------------------------------------*/
274 /* DFOrdering - is visited then nothing else call DFOrdering this */
275 /*-----------------------------------------------------------------*/
276 DEFSETFUNC(DFOrdering)
278 eBBlock *ebbp = item ;
284 computeDFOrdering (ebbp,count); /* depthfirst */
289 /*-----------------------------------------------------------------*/
290 /* computeDFOrdering - computes the depth first ordering of the */
292 /*-----------------------------------------------------------------*/
293 void computeDFOrdering ( eBBlock *ebbp , int *count)
297 /* for each successor that is not visited */
298 applyToSet(ebbp->succList,DFOrdering,count);
300 /* set the depth first number */
301 ebbp->dfnum = *count ;
305 /*-----------------------------------------------------------------*/
306 /* disconBBlock - removes all control flow links for a block */
307 /*-----------------------------------------------------------------*/
308 void disconBBlock (eBBlock *ebp, eBBlock **ebbs, int count)
310 /* mark this block as noPath & recompute control flow */
312 computeControlFlow (ebbs,count,TRUE);
315 /*-----------------------------------------------------------------*/
316 /* markNoPath - marks those blocks which cannot be reached from top*/
317 /*-----------------------------------------------------------------*/
318 void markNoPath ( eBBlock **ebbs, int count )
323 /* for all blocks if the visited flag is not set : then there */
324 /* is no path from _entry to this block push them down in the */
325 /* depth first order */
326 for ( i = 0 ; i < count ; i++ )
327 if (!ebbs[i]->visited)
330 /*-----------------------------------------------------------------*/
331 /* dfNumCompare - used by qsort to sort by dfNumber */
332 /*-----------------------------------------------------------------*/
333 int dfNumCompare (const void *a, const void *b)
335 const eBBlock * const *i = a;
336 const eBBlock * const *j = b;
338 if ( (*i)->dfnum > (*j)->dfnum )
341 if ( (*i)->dfnum < (*j)->dfnum )
347 /*-----------------------------------------------------------------*/
348 /* bbNumCompare - used by qsort to sort by bbNumber */
349 /*-----------------------------------------------------------------*/
350 int bbNumCompare (const void *a, const void *b)
352 const eBBlock * const *i = a;
353 const eBBlock * const *j = b;
355 if ( (*i)->bbnum > (*j)->bbnum )
358 if ( (*i)->bbnum < (*j)->bbnum )
365 /*-----------------------------------------------------------------*/
366 /* computeControlFlow - does the control flow computation */
367 /*-----------------------------------------------------------------*/
368 void computeControlFlow (eBBlock **ebbs,int count, int reSort)
370 int saveCount = count;
373 /* initialise some things */
375 for ( i = 0 ; i < count ; i++ ) {
376 setToNull((void **)&ebbs[i]->predList);
377 setToNull((void **)&ebbs[i]->domVect);
378 setToNull((void **)&ebbs[i]->succList);
379 setToNull((void **)&ebbs[i]->succVect);
380 ebbs[i]->visited = 0;
385 /* sort it back by block number */
386 qsort (ebbs,saveCount,sizeof(eBBlock *),bbNumCompare);
388 setToNull ((void **)&graphEdges);
389 /* this will put in the */
390 /* successor information for each blk */
391 eBBSuccessors (ebbs,count);
393 /* compute the depth first ordering */
394 computeDFOrdering (ebbs[0],&count);
396 /* mark blocks with no paths to them */
397 markNoPath (ebbs,saveCount);
399 /* with the depth first info in place */
400 /* add the predecessors for the blocks*/
401 eBBPredecessors (ebbs,saveCount);
403 /* compute the dominance graph */
404 computeDominance (ebbs,saveCount);
406 /* sort it by dfnumber */
407 qsort (ebbs,saveCount,sizeof(eBBlock *),dfNumCompare);