Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050630135917

Diff

Diff from 1.30 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/mip/optim2.c

Annotated File View

ragge
1.30
1 /*      $Id: optim2.c,v 1.30 2005/06/30 13:59:17 ragge Exp $    */
ragge
1.1
2 /*
3  * Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "pass2.h"
30
ragge
1.3
31 #include <string.h>
pj
1.4
32 #include <stdlib.h>
33
34 #ifndef MIN
35 #define MIN(a,b) (((a)<(b))?(a):(b))
36 #endif
37
38 #ifndef MAX
39 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
40 #endif
ragge
1.3
41
ragge
1.30
42 #define BDEBUG(x)       if (b2debug) printf x
43
pj
1.8
44 static int dfsnum;
ragge
1.1
45
46 void saveip(struct interpass *ip);
47 void deljumps(void);
48 void deltemp(NODE *p);
49 void optdump(struct interpass *ip);
ragge
1.19
50 void printip(struct interpass *pole);
ragge
1.1
51
pj
1.21
52 static struct varinfo defsites;
ragge
1.11
53 static struct interpass ipole;
ragge
1.19
54 struct interpass *storesave;
ragge
1.30
55 static struct interpass_prolog *ipp, *epp/* prolog/epilog */
ragge
1.1
56
ragge
1.30
57 void bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo);
pj
1.4
58 void cfg_build(struct labelinfo *labinfo);
pj
1.8
59 void cfg_dfs(struct basicblock *bbunsigned int parent
60              struct bblockinfo *bbinfo);
61 void dominators(struct bblockinfo *bbinfo);
pj
1.9
62 struct basicblock *
63 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo);
64 void link(struct basicblock *parentstruct basicblock *child);
65 void computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo);
pj
1.21
66 void findTemps(struct interpass *ip);
pj
1.20
67 void placePhiFunctions(struct bblockinfo *bbinfo);
pj
1.15
68 void remunreach(void);
pj
1.4
69
ragge
1.11
70 static struct basicblock bblocks;
pj
1.4
71
ragge
1.1
72 void
73 saveip(struct interpass *ip)
74 {
ragge
1.23
75         static int inftn;
ragge
1.5
76
pj
1.4
77 #if 0
ragge
1.2
78         int regs;
pj
1.4
79 #endif
80         struct labelinfo labinfo;
pj
1.8
81         struct bblockinfo bbinfo;
ragge
1.1
82
ragge
1.23
83         if (ip->type == IP_PROLOG) {
ragge
1.11
84                 DLIST_INIT(&ipoleqelem);
ragge
1.23
85                 inftn = 1;
86         } else if (inftn == 0)
87                 comperr("saveip");
ragge
1.11
88
89         DLIST_INSERT_BEFORE(&ipoleipqelem);
ragge
1.1
90
91         if (ip->type != IP_EPILOG)
92                 return;
ragge
1.23
93         inftn = 0;
ragge
1.30
94         ipp = (struct interpass_prolog *)DLIST_NEXT(&ipoleqelem);
ragge
1.5
95         epp = (struct interpass_prolog *)ip;
ragge
1.1
96
ragge
1.30
97         if (b2debug) {
98                 printf("initial links\n");
99                 printip(&ipole);
100         }
101
ragge
1.26
102         if (xdeljumps)
103                 deljumps();     /* Delete redundant jumps and dead code */
ragge
1.28
104
ragge
1.30
105         if (b2debug) {
106                 printf("links after deljumps\n");
107                 printip(&ipole);
108         }
pj
1.4
109         if (xssaflag) {
ragge
1.11
110                 DLIST_INIT(&bblocksbbelem);
ragge
1.30
111                 bblocks_build(&labinfo, &bbinfo);
112                 cfg_build(&labinfo);
113                 dominators(&bbinfo);
114                 computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
115                 remunreach();
pj
1.4
116 #if 0
ragge
1.30
117                 dfg = dfg_build(cfg);
118                 ssa = ssa_build(cfgdfg);
pj
1.4
119 #endif
120         }
ragge
1.2
121
122 #ifdef PCC_DEBUG
ragge
1.5
123         if (epp->ipp_regs != MAXRVAR)
ragge
1.2
124                 comperr("register error");
125 #endif
ragge
1.1
126
ragge
1.5
127         ipp->ipp_autos = epp->ipp_autos;
128         ipp->ipp_regs = epp->ipp_regs// = regs;
ragge
1.1
129
130 #ifdef MYOPTIM
pj
1.6
131         myoptim((struct interpass *)ipp);
ragge
1.1
132 #endif
133
ragge
1.19
134 if (xnewreg == 0) {
135         int tmpautooff;
136         NODE *p;
137
138         p2autooff = p2maxautooff = AUTOINIT;
139         /* Must verify stack usage first */
140         DLIST_FOREACH(ip, &ipoleqelem) {
141                 if (ip->type == IP_STKOFF) {
142                         p2autooff = ip->ip_off;
143                         if (p2autooff > p2maxautooff)
144                                 p2maxautooff = p2autooff;
145                 }
146         }
147         p2autooff = p2maxautooff/* don't have live range analysis yet */
148
149         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.23
150                 if (ip->type == IPSTK) {
ragge
1.19
151                         struct interpass *ip3;
152                         p2autooff = ip->ip_off;
153                         ip3 = ip;
154                         ip = DLIST_NEXT(ipqelem);
155                         DLIST_REMOVE(ip3qelem);
156                 }
157                         
158                 if (ip->type != IP_NODE)
159                         continue;
160
161                 p = ip->ip_node;
162                 walkf(pdeltemp);
163
164                 tmpautooff = p2autooff;
165
166                 geninsn(pFOREFF);
167                 if (sucomp(p) < 0) {
168                         /* Create STKOFF entry */
169                         struct interpass *ip3;
170                         DLIST_INSERT_BEFORE(ipstoresaveqelem);
171                         ip3 = ipnode(NULL);
ragge
1.23
172                         ip3->type = IPSTK;
ragge
1.19
173                         ip3->ip_off = tmpautooff;
174                         DLIST_INSERT_AFTER(ipip3qelem);
175                         ip = DLIST_PREV(storesaveqelem);
176                         continue;
177                 }
178
179                 p2autooff = tmpautooff;
180
181                 genregs(p);
182                 mygenregs(p);
183         }
184
185 else {
186         /*
187          * Loop over instruction assignment until the register assignment
188          * code is satisfied.
189          */
ragge
1.25
190 #if 0
191         extern int tempmintempmax;
192
193         tempmin = ip->ip_tmpnum;
194         tempmin = ie->ip_tmpnum;
195 #endif
ragge
1.19
196         do {
197                 /* do instruction assignment */
198                 DLIST_FOREACH(ip, &ipoleqelem) {
199                         if (ip->type != IP_NODE)
200                                 continue;
201                         geninsn(ip->ip_nodeFOREFF);
202                         nsucomp(ip->ip_node);
203                 }
ragge
1.28
204         } while (ngenregs(&ipole));
ragge
1.19
205 }
206
207         DLIST_FOREACH(ip, &ipoleqelem) {
208                 emit(ip);
209         }
ragge
1.11
210         DLIST_INIT(&ipoleqelem);
ragge
1.1
211 }
212
ragge
1.26
213 /*
214  * Delete unused labels, excess of labels, gotos to gotos.
215  * This routine can be made much more efficient.
216  */
ragge
1.1
217 void
218 deljumps()
219 {
ragge
1.28
220         struct interpass *ip, *n, *ip2;
ragge
1.26
221         int gotone,lowhigh;
ragge
1.28
222         int *lblaryszoi;
ragge
1.26
223
224         low = ipp->ip_lblnum;
225         high = epp->ip_lblnum;
226
227 #ifdef notyet
228         mark = tmpmark(); /* temporary used memory */
229 #endif
ragge
1.28
230
ragge
1.26
231         sz = (high-low) * sizeof(int);
232         lblary = tmpalloc(sz);
233
234 again:  gotone = 0;
ragge
1.28
235         memset(lblary0sz);
ragge
1.26
236
ragge
1.28
237         /* refcount and coalesce  all labels */
ragge
1.27
238         DLIST_FOREACH(ip, &ipoleqelem) {
239                 if (ip->type == IP_DEFLAB) {
240                         n = DLIST_NEXT(ipqelem);
ragge
1.28
241                         while (n->type == IP_DEFLAB || n->type == IP_STKOFF) {
242                                 if (n->type == IP_DEFLAB &&
243                                     lblary[n->ip_lbl-low] >= 0)
244                                         lblary[n->ip_lbl-low] = -ip->ip_lbl;
ragge
1.27
245                                 n = DLIST_NEXT(nqelem);
246                         }
247                 }
ragge
1.28
248                 if (ip->type != IP_NODE)
249                         continue;
250                 o = ip->ip_node->n_op;
251                 if (o == GOTO)
252                         i = ip->ip_node->n_left->n_lval;
253                 else if (o == CBRANCH)
254                         i = ip->ip_node->n_right->n_lval;
255                 else
256                         continue;
257                 lblary[i-low] |= 1;
ragge
1.27
258         }
259
ragge
1.28
260         /* delete coalesced/unused labels and rename gotos */
261         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.27
262                 n = DLIST_NEXT(ipqelem);
ragge
1.28
263                 if (n->type == IP_DEFLAB) {
264                         if (lblary[n->ip_lbl-low] <= 0) {
265                                 DLIST_REMOVE(nqelem);
266                                 gotone = 1;
ragge
1.26
267                         }
268                         continue;
269                 }
270                 if (n->type != IP_NODE)
271                         continue;
272                 o = n->ip_node->n_op;
273                 if (o == GOTO)
274                         i = n->ip_node->n_left->n_lval;
275                 else if (o == CBRANCH)
276                         i = n->ip_node->n_right->n_lval;
277                 else
278                         continue;
279                 if (lblary[i-low] < 0) {
280                         if (o == GOTO)
281                                 n->ip_node->n_left->n_lval = -lblary[i-low];
282                         else
283                                 n->ip_node->n_right->n_lval = -lblary[i-low];
284                 }
285         }
ragge
1.27
286
ragge
1.28
287         /* Delete gotos to the next statement */
ragge
1.26
288         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.28
289                 n = DLIST_NEXT(ipqelem);
290                 if (n->type != IP_NODE)
ragge
1.26
291                         continue;
ragge
1.28
292                 o = n->ip_node->n_op;
ragge
1.26
293                 if (o == GOTO)
ragge
1.28
294                         i = n->ip_node->n_left->n_lval;
ragge
1.26
295                 else if (o == CBRANCH)
ragge
1.28
296                         i = n->ip_node->n_right->n_lval;
ragge
1.26
297                 else
298                         continue;
ragge
1.28
299                 ip2 = DLIST_NEXT(nqelem);
300                 if (ip2->type != IP_DEFLAB)
ragge
1.1
301                         continue;
ragge
1.28
302                 if (ip2->ip_lbl == i) {
ragge
1.1
303                         tfree(n->ip_node);
ragge
1.11
304                         DLIST_REMOVE(nqelem);
ragge
1.28
305                         gotone = 1;
ragge
1.1
306                 }
307         }
ragge
1.28
308
ragge
1.1
309         if (gotone)
310                 goto again;
ragge
1.28
311
312 #ifdef notyet
313         tmpfree(mark);
ragge
1.26
314 #endif
ragge
1.1
315 }
316
317 void
318 optdump(struct interpass *ip)
319 {
320         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
321                 "deflab""defnam""asm" };
322         printf("type %s\n"nm[ip->type-1]);
323         switch (ip->type) {
324         case IP_NODE:
325                 fwalk(ip->ip_nodee2print0);
326                 break;
327         case IP_DEFLAB:
328                 printf("label " LABFMT "\n"ip->ip_lbl);
329                 break;
330         case IP_ASM:
331                 printf(": %s\n"ip->ip_asm);
332                 break;
333         }
334 }
pj
1.4
335
336 /*
337  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
338  *
339  * Also fills the labelinfo struct with information about which bblocks
340  * that contain which label.
341  */
342
ragge
1.30
343 void
pj
1.8
344 bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo)
pj
1.4
345 {
346         struct interpass *ip;
347         struct basicblock *bb = NULL;
348         int leader = 1;
ragge
1.30
349         int lowhigh;
pj
1.8
350         int count = 0;
pj
1.4
351         int i;
352
ragge
1.30
353         BDEBUG(("bblocks_build (%p, %p)\n"labinfobbinfo));
354         low = ipp->ip_lblnum;
355         high = epp->ip_lblnum;
356
pj
1.4
357         /* 
358          * First statement is a leader.
359          * Any statement that is target of a jump is a leader.
360          * Any statement that immediately follows a jump is a leader.
361          */
362
ragge
1.11
363         DLIST_FOREACH(ip, &ipoleqelem) {
pj
1.4
364                 /* Garbage, skip it */
365                 if (leader) {
366                         bb = tmpalloc(sizeof(struct basicblock));
367                         bb->first = bb->last = ip;
ragge
1.11
368                         SLIST_INIT(&bb->children);
369                         SLIST_INIT(&bb->parents);
pj
1.8
370                         bb->dfnum = 0;
371                         bb->dfparent = 0;
372                         bb->semi = 0;
373                         bb->ancestor = 0;
374                         bb->idom = 0;
375                         bb->samedom = 0;
376                         bb->bucket = NULL;
377                         bb->df = NULL;
pj
1.10
378                         bb->dfchildren = NULL;
pj
1.22
379                         bb->Aorig = NULL;
380                         bb->Aphi = NULL;
ragge
1.11
381                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.4
382                         leader = 0;
pj
1.8
383                         count++;
pj
1.4
384                 } 
385                 
386                 /* Prologue and epilogue in their own bblock */
387                 if ((ip->type == IP_PROLOG) || (ip->type == IP_EPILOG)) {
388                         bb->last = ip;
389                         if (ip->type == IP_EPILOG)
ragge
1.5
390                                 high = MAX(ip->ip_lblhigh);
pj
1.4
391                         leader = 1;
392                         continue;
393                 }
394                 
395                 /* Make sure each label is in a unique bblock */
396                 if (((ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) && 
397                     bb->first != ip) {
398                         bb = tmpalloc(sizeof(struct basicblock));
399                         bb->first = bb->last = ip;
ragge
1.11
400                         SLIST_INIT(&bb->children);
401                         SLIST_INIT(&bb->parents);
pj
1.8
402                         bb->dfnum = 0;
403                         bb->dfparent = 0;
404                         bb->semi = 0;
405                         bb->ancestor = 0;
406                         bb->idom = 0;
407                         bb->samedom = 0;
408                         bb->bucket = NULL;
409                         bb->df = NULL;
pj
1.10
410                         bb->dfchildren = NULL;
pj
1.22
411                         bb->Aorig = NULL;
412                         bb->Aphi = NULL;
ragge
1.11
413                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.8
414                         count++;
pj
1.4
415                         continue;
416                 }
417
418                 if (ip->type == IP_NODE) {
419                         switch(ip->ip_node->n_op) {
420                         case CBRANCH:
421                         case GOTO:
422                         case RETURN:
423                                 /* Jumps, last in bblock. */
424                                 leader = 1;
425                                 break;
426
427                         case NAME:
428                         case ICON:
429                         case FCON:
430                         case REG:
431                         case OREG:
432                         case MOVE:
433                         case PLUS:
434                         case MINUS:
435                         case DIV:
436                         case MOD:
437                         case MUL:
438                         case AND:
439                         case OR:
440                         case ER:
441                         case LS:
442                         case COMPL:
443                         case INCR:
444                         case DECR:
445                         case UMUL:
446                         case UMINUS:
447                         case EQ:
448                         case NE:
449                         case LE:
450                         case GE:
451                         case GT:
452                         case ULE:
453                         case ULT:
454                         case UGE:
455                         case UGT:
456                         case ASSIGN:
457                         case FORCE:
458                         case FUNARG:
pj
1.7
459                         case CALL:
460                         case UCALL:
461                         case FORTCALL:
462                         case UFORTCALL:
463                         case STCALL:
464                         case USTCALL:
pj
1.4
465                                 /* Not jumps, continue with bblock. */
466                                 break;
467
468                         default:
469                                 comperr("optim2:bblocks_build() %d",ip->ip_node->n_op ); 
470                                 break;
471                         }
472                 }
473
474                 bb->last = ip;
475         }
ragge
1.30
476
477         BDEBUG(("Basic blocks in func: %d\n"count));
478         BDEBUG(("Label range in func: %d\n"high - low + 1));
479
pj
1.4
480
481         labinfo->low = low;
482         labinfo->size = high - low + 1;
483         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
pj
1.8
484         for (i = 0i <= labinfo->sizei++) {
pj
1.4
485                 labinfo->arr[i] = NULL;
486         }
pj
1.8
487         
488         bbinfo->size = count + 1;
489         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
490         for (i = 0i <= bbinfo->sizei++) {
491                 bbinfo->arr[i] = NULL;
492         }
pj
1.4
493
ragge
1.11
494         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.20
495                 /* Build the label table */
pj
1.4
496                 if (bb->first->type == IP_DEFLAB) {
497                         labinfo->arr[bb->first->ip_lbl - low] = bb;
498                 }
499                 if (bb->first->type == IP_EPILOG) {
ragge
1.5
500                         labinfo->arr[bb->first->ip_lbl - low] = bb;
pj
1.4
501                 }
502         }
503 }
504
505 /*
506  * Build the control flow graph.
507  */
508
509 void
510 cfg_build(struct labelinfo *labinfo)
511 {
512         /* Child and parent nodes */
513         struct cfgnode *cnode
514         struct cfgnode *pnode;
515         struct basicblock *bb;
516         
ragge
1.11
517         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
518
519                 if (bb->first->type == IP_EPILOG) {
520                         break;
521                 }
522
523                 cnode = tmpalloc(sizeof(struct cfgnode));
524                 pnode = tmpalloc(sizeof(struct cfgnode));
525                 pnode->bblock = bb;
526
527                 if ((bb->last->type == IP_NODE) && 
528                     (bb->last->ip_node->n_op == GOTO)) {
529                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
530                             labinfo->size) {
531                                 comperr("Label out of range: %d, base %d"
532                                         bb->last->ip_node->n_left->n_lval
533                                         labinfo->low);
534                         }
535                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
536                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
537                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
538                         continue;
539                 }
540                 if ((bb->last->type == IP_NODE) && 
541                     (bb->last->ip_node->n_op == CBRANCH)) {
542                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
543                             labinfo->size
544                                 comperr("Label out of range: %d"
545                                         bb->last->ip_node->n_left->n_lval);
546                         
547                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
548                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
549                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
550                         cnode = tmpalloc(sizeof(struct cfgnode));
551                         pnode = tmpalloc(sizeof(struct cfgnode));
552                         pnode->bblock = bb;
553                 }
554
ragge
1.11
555                 cnode->bblock = DLIST_NEXT(bbbbelem);
556                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
557                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
558         }
559 }
pj
1.6
560
pj
1.8
561 void
562 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
563 {
564         struct cfgnode *cnode;
565         
566         if (bb->dfnum != 0)
567                 return;
568
569         bb->dfnum = ++dfsnum;
570         bb->dfparent = parent;
571         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
572         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
573                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
574         }
pj
1.13
575         /* Don't bring in unreachable nodes in the future */
pj
1.20
576         bbinfo->size = dfsnum + 1;
pj
1.8
577 }
578
ragge
1.11
579 static bittype *
580 setalloc(int nelem)
581 {
582         bittype *b;
583         int sz = (nelem+NUMBITS-1)/NUMBITS;
584
585         b = tmpalloc(sz * sizeof(bittype));
586         memset(b0sz * sizeof(bittype));
587         return b;
588 }
589
pj
1.8
590 /*
591  * Algorithm 19.9, pp 414 from Appel.
592  */
593
594 void
595 dominators(struct bblockinfo *bbinfo)
596 {
597         struct cfgnode *cnode;
598         struct basicblock *bb, *y, *v;
599         struct basicblock *s, *sprime, *p;
pj
1.10
600         int hi;
pj
1.8
601
ragge
1.11
602         DLIST_FOREACH(bb, &bblocksbbelem) {
603                 bb->bucket = setalloc(bbinfo->size);
604                 bb->df = setalloc(bbinfo->size);
605                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
606         }
607
608         dfsnum = 0;
ragge
1.11
609         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
610
ragge
1.30
611         if (b2debug) {
612                 struct basicblock *bbb;
613                 struct cfgnode *ccnode;
614
615                 DLIST_FOREACH(bbb, &bblocksbbelem) {
616                         printf("Basic block %d, parents: "bbb->dfnum);
617                         SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
618                                 printf("%d, "ccnode->bblock->dfnum);
619                         }
620                         printf("\nChildren: ");
621                         SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
622                                 printf("%d, "ccnode->bblock->dfnum);
623                         }
624                         printf("\n");
pj
1.20
625                 }
626         }
627
pj
1.10
628         for(h = bbinfo->size - 1h > 1h--) {
629                 bb = bbinfo->arr[h];
pj
1.8
630                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
631                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
632                         if (cnode->bblock->dfnum <= bb->dfnum
633                                 sprime = cnode->bblock;
pj
1.10
634                         else 
635                                 sprime = bbinfo->arr[ancestorwithlowestsemi
636                                               (cnode->bblockbbinfo)->semi];
pj
1.8
637                         if (sprime->dfnum < s->dfnum)
638                                 s = sprime;
639                 }
640                 bb->semi = s->dfnum;
pj
1.10
641                 BITSET(s->bucketbb->dfnum);
pj
1.8
642                 link(pbb);
643                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
644                         if(TESTBIT(p->bucketi)) {
pj
1.8
645                                 v = bbinfo->arr[i];
pj
1.10
646                                 y = ancestorwithlowestsemi(vbbinfo);
647                                 if (y->semi == v->semi
pj
1.8
648                                         v->idom = p->dfnum;
649                                 else
650                                         v->samedom = y->dfnum;
651                         }
652                 }
653                 memset(p->bucket0, (bbinfo->size + 7)/8);
654         }
ragge
1.30
655
656         if (b2debug) {
657                 printf("Num\tSemi\tAncest\tidom\n");
658                 DLIST_FOREACH(bb, &bblocksbbelem) {
659                         printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semi,
660                             bb->ancestorbb->idom);
661                 }
pj
1.10
662         }
ragge
1.30
663
pj
1.10
664         for(h = 2h < bbinfo->sizeh++) {
665                 bb = bbinfo->arr[h];
666                 if (bb->samedom != 0) {
pj
1.8
667                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
668                 }
669         }
ragge
1.11
670         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
671                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
ragge
1.30
672                         BDEBUG(("Setting child %d of %d\n",
673                             bb->dfnumbbinfo->arr[bb->idom]->dfnum));
pj
1.10
674                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
675                 }
pj
1.8
676         }
677 }
678
679
680 struct basicblock *
681 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
682 {
683         struct basicblock *u = bblock;
684         struct basicblock *v = bblock;
685
686         while (v->ancestor != 0) {
687                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
688                     bbinfo->arr[u->semi]->dfnum
pj
1.8
689                         u = v;
690                 v = bbinfo->arr[v->ancestor];
691         }
692         return u;
693 }
694
695 void
696 link(struct basicblock *parentstruct basicblock *child)
697 {
698         child->ancestor = parent->dfnum;
699 }
700
701 void
702 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
703 {
704         struct cfgnode *cnode;
pj
1.10
705         int hi;
pj
1.8
706         
ragge
1.11
707         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
708                 if (cnode->bblock->idom != bblock->dfnum)
709                         BITSET(bblock->dfcnode->bblock->dfnum);
710         }
pj
1.10
711         for (h = 1h < bbinfo->sizeh++) {
712                 if (!TESTBIT(bblock->dfchildrenh))
713                         continue;
714                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
715                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
716                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
717                             (bbinfo->arr[h] == bblock ||
718                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
719                             BITSET(bblock->dfi);
720                 }
721         }
722 }
pj
1.15
723
pj
1.21
724 static struct basicblock *currbb;
725 static struct interpass *currip;
726
727 /* Helper function for findTemps, Find assignment nodes. */
728 static void
729 findasg(NODE *p)
730 {
731         struct pvarinfo *pv;
732
733         if (p->n_op != ASSIGN)
734                 return;
735
736         if (p->n_left->n_op != TEMP)
737                 return;
738
739         pv = tmpcalloc(sizeof(struct pvarinfo));
740         pv->next = defsites.arr[p->n_left->n_lval];
741         pv->bb = currbb;
742         pv->top = currip->ip_node;
743         pv->n = p->n_left;
pj
1.22
744         BITSET(currbb->Aorigp->n_left->n_lval);
pj
1.21
745
746         defsites.arr[p->n_left->n_lval] = pv;
747 }
748
749 /* Walk the interpass looking for assignment nodes. */
750 void findTemps(struct interpass *ip)
pj
1.20
751 {
752         if (ip->type != IP_NODE)
753                 return;
754
pj
1.21
755         currip = ip;
756
757         walkf(ip->ip_nodefindasg);
pj
1.20
758 }
759
760 /*
761  * Algorithm 19.6 from Appel.
762  */
763
764 void
765 placePhiFunctions(struct bblockinfo *bbinfo)
766 {
767         struct basicblock *bb;
768         struct interpass *ip;
769         int maxtmpijkl;
770         struct pvarinfo *n;
771         struct cfgnode *cnode;
772         TWORD ntype;
773         NODE *p;
pj
1.22
774         struct pvarinfo *pv;
pj
1.20
775
776         bb = DLIST_NEXT(&bblocksbbelem);
777         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
778         bb = DLIST_PREV(&bblocksbbelem);
779         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
780         defsites.size = maxtmp - defsites.low + 1;
781         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
782
pj
1.21
783         /* Find all defsites */
pj
1.20
784         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
785                 currbb = bb;
pj
1.20
786                 ip = bb->first;
pj
1.22
787                 bb->Aorig = setalloc(defsites.size);
788                 bb->Aphi = setalloc(defsites.size);
789                 
pj
1.20
790
791                 while (ip != bb->last) {
pj
1.21
792                         findTemps(ip);
pj
1.20
793                         ip = DLIST_NEXT(ipqelem);
794                 }
795                 /* Make sure we get the last statement in the bblock */
pj
1.21
796                 findTemps(ip);
pj
1.20
797         }
pj
1.21
798         /* For each variable */
pj
1.20
799         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
800                 /* While W not empty */
pj
1.20
801                 while (defsites.arr[i] != NULL) {
pj
1.21
802                         /* Remove some node n from W */
pj
1.20
803                         n = defsites.arr[i];
804                         defsites.arr[i] = n->next;
pj
1.21
805                         /* For each y in n->bb->df */
pj
1.20
806                         for (j = 0j < bbinfo->sizej++) {
pj
1.22
807                                 if (!TESTBIT(n->bb->dfj))
pj
1.20
808                                         continue;
pj
1.22
809                                 
810                                 if (TESTBIT(bbinfo->arr[j]->Aphii))
811                                         continue;
812
pj
1.20
813                                 ntype = n->n->n_type;
814                                 k = 0;
pj
1.21
815                                 /* Amount of predecessors for y */
pj
1.20
816                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
817                                         k++;
pj
1.21
818                                 /* Construct phi(...) */
pj
1.20
819                                 p = mklnode(TEMPi0ntype);
820                                 for (l = 0l < k-1l++)
821                                         p = mkbinode(PHIp,
822                                             mklnode(TEMPi0ntype), ntype);
823                                 ip = ipnode(mkbinode(ASSIGN,
824                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
825                                 /* Insert phi at top of basic block */
pj
1.20
826                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
827                                 n->bb->first = ip;
pj
1.22
828                                 BITSET(bbinfo->arr[j]->Aphii);
829                                 if (!TESTBIT(bbinfo->arr[j]->Aorigi)) {
830                                         pv = tmpalloc(sizeof(struct pvarinfo));
831                                         // XXXpj Ej fullständig information.
832                                         pv->bb = bbinfo->arr[j];
833                                         pv->next = defsites.arr[i]->next;
834                                         defsites.arr[i] = pv;
835                                 }
836                                         
pj
1.20
837
838                         }
839                 }
840         }
841 }
842
pj
1.15
843 /*
844  * Remove unreachable nodes in the CFG.
845  */ 
846
847 void
848 remunreach(void)
849 {
850         struct basicblock *bb, *nbb;
851         struct interpass *next, *ctree;
852
853         bb = DLIST_NEXT(&bblocksbbelem);
854         while (bb != &bblocks) {
855                 nbb = DLIST_NEXT(bbbbelem);
856
857                 /* Code with dfnum 0 is unreachable */
858                 if (bb->dfnum != 0) {
859                         bb = nbb;
860                         continue;
861                 }
862
863                 /* Need the epilogue node for other parts of the
864                    compiler, set its label to 0 and backend will
865                    handle it. */ 
866                 if (bb->first->type == IP_EPILOG) {
867                         bb->first->ip_lbl = 0;
868                         bb = nbb;
869                         continue;
870                 }
871
872                 next = bb->first;
873                 do {
874                         ctree = next;
875                         next = DLIST_NEXT(ctreeqelem);
876                         
877                         if (ctree->type == IP_NODE)
pj
1.16
878                                 tfree(ctree->ip_node);
pj
1.15
879                         DLIST_REMOVE(ctreeqelem);
880                 } while (ctree != bb->last);
881                         
882                 DLIST_REMOVE(bbbbelem);
883                 bb = nbb;
884         }
885 }
ragge
1.19
886
887 void
888 printip(struct interpass *pole)
889 {
890         static char *foo[] = {
891            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
892         struct interpass *ip;
893
894         DLIST_FOREACH(ippoleqelem) {
895                 if (ip->type > MAXIP)
896                         printf("IP(%d) (%p): "ip->typeip);
897                 else
898                         printf("%s (%p): "foo[ip->type], ip);
899                 switch (ip->type) {
900                 case IP_NODEprintf("\n");
901                         fwalk(ip->ip_nodee2print0); break;
902                 case IP_PROLOG:
903                         printf("%s\n",
904                             ((struct interpass_prolog *)ip)->ipp_name); break;
905                 case IP_STKOFFprintf("%d\n"ip->ip_off); break;
906                 case IP_EPILOGprintf("\n"); break;
907                 case IP_DEFLABprintf(LABFMT "\n"ip->ip_lbl); break;
908                 }
909         }
910 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-11-01 05:44 +0100