Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050402075617

Diff

Diff from 1.17 to:

Annotations

Annotate by Age | Author | Mixed | None

Annotated File View

ragge
1.17
1 /*      $Id: optim2.c,v 1.17 2005/04/02 07:56: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.1
42 extern int saving;
pj
1.8
43 static int dfsnum;
ragge
1.1
44
45 void saveip(struct interpass *ip);
46 void deljumps(void);
47 void deltemp(NODE *p);
48 void optdump(struct interpass *ip);
49
ragge
1.11
50 static struct interpass ipole;
ragge
1.1
51
52 static struct rsv {
53         struct rsv *next;
54         int fpoff;
55         TWORD type;
56         int use;
57 } *rsv;
58
pj
1.8
59 int bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo);
pj
1.4
60 void cfg_build(struct labelinfo *labinfo);
pj
1.8
61 void cfg_dfs(struct basicblock *bbunsigned int parent
62              struct bblockinfo *bbinfo);
63 void dominators(struct bblockinfo *bbinfo);
pj
1.9
64 struct basicblock *
65 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo);
66 void link(struct basicblock *parentstruct basicblock *child);
67 void computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo);
pj
1.15
68 void remunreach(void);
pj
1.4
69
70
ragge
1.11
71 static struct basicblock bblocks;
pj
1.4
72
ragge
1.1
73 static void
74 addcand(TWORD typeint offint avoid)
75 {
76         struct rsv *w = rsv;
77
78         while (w != NULL) {
79                 if (w->type == type && w->fpoff == off) {
80                         if (avoid)
81                                 w->use = -1;
82                         else if (w->use > 0)
83                                 w->use++;
84                         return;
85                 }
86                 w = w->next;
87         }
88         w = tmpalloc(sizeof(*w));
89         w->type = type;
90         w->fpoff = off;
91         w->use = avoid ? -1 : 1;
92         w->next = rsv;
93         rsv = w;
94 }
95
96 /*
97  * walk through the tree and count the number of (possible)
98  * temporary nodes.
99  */
100 static void
101 cntuse(NODE *p)
102 {
103         NODE *l = p->n_left;
104         NODE *r = p->n_right;
105
106         if (p->n_op == UMUL && l->n_op == REG && l->n_rval == FPREG) {
107                 /* found a candidate for register */
108                 addcand(p->n_type0ISVOL(p->n_qual << TSHIFT));
109         } else if (p->n_op == UMUL && l->n_op == PLUS &&
110             l->n_right->n_op == ICON && 
111              (l->n_left->n_op == REG && l->n_left->n_rval == FPREG)) {
112                 /* The same as above */
113                 addcand(p->n_typel->n_right->n_lval,
114                     ISVOL(p->n_qual << TSHIFT));
115         } else if (p->n_op == PLUS && l->n_op == REG && l->n_rval == FPREG &&
116             p->n_right->n_op == ICON) {
117                 /* Address taken of temporary, avoid register */
118                 addcand(DECREF(p->n_type), r->n_lval1);
119         } else {
120                 if (optype(p->n_op) == BITYPE)
121                         cntuse(r);
122                 if (optype(p->n_op) != LTYPE)
123                         cntuse(l);
124         }
125 }
126
127 /*
ragge
1.2
128  * Insert a node into the register stack.
129  */
130 static void
131 insert(struct rsv *wstruct rsv **savedint maxregs)
132 {
133         int ijsize;
134
135         size = szty(w->type);
136
137         /* Find reg move position */
138         for (i = 0i < maxregsi++) {
139                 if (saved[i] == NULL)
140                         continue;
141                 if (saved[i]->use > w->use)
142                         break;
143         }
144         /* Move down other regs */
145         for (j = sizej < ij++)
146                 saved[j-size] = saved[j];
147
148         /* Insert new reg pointer */
149         if (i-size >= 0) {
150                 saved[i-size] = w;
151                 for (j = i-size+1j < ij++)
152                         saved[j] = NULL;
153         }
154 }
155
156 /* Help routine to rconvert() */
157 static int
158 matches(TWORD typeint offstruct rsv **rsvint maxregs)
159 {
160         int i;
161
162         for (i = 0i < maxregsi++)
163                 if (rsv[i] && rsv[i]->type == type && rsv[i]->fpoff == off)
164                         return i;
165         return -1;
166 }
167
168 /* Help routine to rconvert() */
169 static void
170 modify(NODE *pint reg)
171 {
172         tfree(p->n_left);
173         p->n_op = REG;
174         p->n_rval = p->n_rall = reg + MINRVAR;
175         p->n_lval = 0;
176 }
177
178 /*
179  * walk through the tree and convert nodes to registers
180  */
181 static void
182 rconvert(NODE *pstruct rsv **rsvint maxregs)
183 {
184         NODE *l = p->n_left;
185         NODE *r = p->n_right;
186         int i;
187
188         if (p->n_op == UMUL && l->n_op == REG && l->n_rval == FPREG) {
189                 /* found a candidate for register */
190                 if ((i = matches(p->n_type0rsvmaxregs)) >= 0)
191                         modify(pi);
192         } else if (p->n_op == UMUL && l->n_op == PLUS &&
193             l->n_right->n_op == ICON && 
194              (l->n_left->n_op == REG && l->n_left->n_rval == FPREG)) {
195                 /* The same as above */
196                 if ((i = matches(p->n_type,
197                     l->n_right->n_lvalrsvmaxregs)) >= 0)
198                         modify(pi);
199 #if 0
200         } else if (p->n_op == PLUS && l->n_op == REG && l->n_rval == FPREG &&
201             p->n_right->n_op == ICON) {
202                 /* Address taken of temporary, avoid register */
203                 addcand(DECREF(p->n_type), r->n_lval1);
204 #endif
205         } else {
206                 if (optype(p->n_op) == BITYPE)
207                         rconvert(rrsvmaxregs);
208                 if (optype(p->n_op) != LTYPE)
209                         rconvert(lrsvmaxregs);
210         }
211 }
212
213 /*
ragge
1.1
214  * Assign non-temporary registers to variables.
215  * Cannot do it if:
216  * - address is taken of the temporary
217  * - variable is declared "volatile".
218  */
pj
1.4
219 int asgregs(void);
220 int
ragge
1.1
221 asgregs(void)
222 {
223         struct interpass *ip;
ragge
1.2
224         struct rsv *w, **saved;
225         int imaxregs = MAXRVAR - MINRVAR + 1;
ragge
1.1
226
ragge
1.2
227         if (maxregs == 0)
228                 return MAXRVAR/* No register usage */
ragge
1.1
229         rsv = NULL;
230
231         /* Loop over the function to do a usage count */
ragge
1.11
232         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
233                 if (ip->type != IP_NODE)
234                         continue;
235                 cntuse(ip->ip_node);
236         }
237         /* Check which nodes that shall be converted to registers */
ragge
1.2
238         saved = tmpalloc(sizeof(struct rsv *) * maxregs);
239         memset(saved0sizeof(struct rsv *) * maxregs);
ragge
1.1
240         w = rsv;
241         for (w = rsvww = w->next) {
242                 if (w->use < 0)
ragge
1.2
243                         continue/* Not allowed to be in register */
244
245                 /* XXX check here if type is allowed to be in register */
246
247                 insert(wsavedmaxregs);
ragge
1.1
248         }
249
250         /* Convert found nodes to registers */
ragge
1.11
251         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.2
252                 if (ip->type != IP_NODE)
253                         continue;
254                 rconvert(ip->ip_nodesavedmaxregs);
255         }
256         for (i = 0i < maxregsi++)
257                 if (saved[i] != NULL)
258                         break;
259         return MINRVAR+i-1;
ragge
1.1
260 }
261
262 void
263 saveip(struct interpass *ip)
264 {
ragge
1.5
265         struct interpass_prolog *ipp, *epp;
266
pj
1.4
267 #if 0
ragge
1.2
268         int regs;
pj
1.4
269 #endif
270         struct labelinfo labinfo;
pj
1.8
271         struct bblockinfo bbinfo;
ragge
1.1
272
ragge
1.11
273         if (ip->type == IP_PROLOG)
274                 DLIST_INIT(&ipoleqelem);
275
276         DLIST_INSERT_BEFORE(&ipoleipqelem);
ragge
1.1
277
278         if (ip->type != IP_EPILOG)
279                 return;
ragge
1.5
280         epp = (struct interpass_prolog *)ip;
ragge
1.1
281         saving = -1;
282
pj
1.4
283         //              deljumps();     /* Delete redundant jumps and dead code */
284         if (xssaflag) {
ragge
1.11
285                 DLIST_INIT(&bblocksbbelem);
pj
1.8
286                 if (bblocks_build(&labinfo, &bbinfo)) {
pj
1.4
287                         cfg_build(&labinfo);
pj
1.8
288                         dominators(&bbinfo);
ragge
1.11
289                         computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
pj
1.15
290                         remunreach();
pj
1.4
291 #if 0
pj
1.6
292                         if (xssaflag) {
293                                 dfg = dfg_build(cfg);
294                                 ssa = ssa_build(cfgdfg);
295                         }
pj
1.4
296 #endif
297                 }
298  
299         }
300 #if 0
ragge
1.2
301         regs = asgregs();       /* Assign non-temporary registers */
pj
1.4
302 #endif
ragge
1.2
303
304 #ifdef PCC_DEBUG
ragge
1.5
305         if (epp->ipp_regs != MAXRVAR)
ragge
1.2
306                 comperr("register error");
307 #endif
ragge
1.1
308
ragge
1.11
309         ipp = (struct interpass_prolog *)DLIST_NEXT(&ipoleqelem);
ragge
1.5
310         ipp->ipp_autos = epp->ipp_autos;
311         ipp->ipp_regs = epp->ipp_regs// = regs;
ragge
1.1
312
313 #ifdef MYOPTIM
pj
1.6
314         myoptim((struct interpass *)ipp);
ragge
1.1
315 #endif
316
ragge
1.11
317         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
318                 pass2_compile(ip);
319         }
ragge
1.11
320         DLIST_INIT(&ipoleqelem);
ragge
1.1
321 }
322
323 void
324 deljumps()
325 {
326         struct interpass *ip, *n;
327         int gotone;
328
329 again:  gotone = 0;
330
ragge
1.11
331         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
332                 if (ip->type == IP_EPILOG)
333                         return;
334                 if (ip->type != IP_NODE)
335                         continue;
ragge
1.11
336                 n = DLIST_NEXT(ipqelem);
ragge
1.1
337                 /* Check for nodes without side effects */
338                 if (ip->ip_node->n_op != GOTO)
339                         continue;
340                 switch (n->type) {
341                 case IP_NODE:
342                         tfree(n->ip_node);
ragge
1.11
343                         DLIST_REMOVE(nqelem);
ragge
1.1
344                         break;
345                 case IP_DEFLAB:
346                         if (ip->ip_node->n_left->n_lval != n->ip_lbl)
347                                 continue;
348                         tfree(ip->ip_node);
349                         *ip = *n;
350                         break;
351                 default:
352                         continue;
353                 }
354                 gotone = 1;
355         }
356         if (gotone)
357                 goto again;
358 }
359
360 void
361 optdump(struct interpass *ip)
362 {
363         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
364                 "deflab""defnam""asm" };
365         printf("type %s\n"nm[ip->type-1]);
366         switch (ip->type) {
367         case IP_NODE:
368                 fwalk(ip->ip_nodee2print0);
369                 break;
370         case IP_DEFLAB:
371                 printf("label " LABFMT "\n"ip->ip_lbl);
372                 break;
373         case IP_ASM:
374                 printf(": %s\n"ip->ip_asm);
375                 break;
376         }
377 }
pj
1.4
378
379 /*
380  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
381  *
382  * Also fills the labelinfo struct with information about which bblocks
383  * that contain which label.
384  */
385
386 int
pj
1.8
387 bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo)
pj
1.4
388 {
389         struct interpass *ip;
390         struct basicblock *bb = NULL;
391         int leader = 1;
pj
1.12
392         unsigned int lowhigh = 0;
pj
1.8
393         int count = 0;
pj
1.4
394         int i;
395
pj
1.12
396         low = sizeof(int) == 2 ? 65000 : 1000000000;
pj
1.4
397         /* 
398          * First statement is a leader.
399          * Any statement that is target of a jump is a leader.
400          * Any statement that immediately follows a jump is a leader.
401          */
402
ragge
1.11
403         DLIST_FOREACH(ip, &ipoleqelem) {
pj
1.4
404                 /* Garbage, skip it */
405                 if (leader) {
406                         bb = tmpalloc(sizeof(struct basicblock));
407                         bb->first = bb->last = ip;
ragge
1.11
408                         SLIST_INIT(&bb->children);
409                         SLIST_INIT(&bb->parents);
pj
1.8
410                         bb->dfnum = 0;
411                         bb->dfparent = 0;
412                         bb->semi = 0;
413                         bb->ancestor = 0;
414                         bb->idom = 0;
415                         bb->samedom = 0;
416                         bb->bucket = NULL;
417                         bb->df = NULL;
pj
1.10
418                         bb->dfchildren = NULL;
ragge
1.11
419                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.4
420                         leader = 0;
pj
1.8
421                         count++;
pj
1.4
422                 } 
423                 
424                 /* Prologue and epilogue in their own bblock */
425                 if ((ip->type == IP_PROLOG) || (ip->type == IP_EPILOG)) {
426                         bb->last = ip;
427                         if (ip->type == IP_EPILOG)
ragge
1.5
428                                 high = MAX(ip->ip_lblhigh);
pj
1.4
429                         leader = 1;
430                         continue;
431                 }
432                 
433                 /* Keep track of highest and lowest label number */
434                 if (ip->type == IP_DEFLAB) {
435                         low = MIN(ip->ip_lbllow);
436                         high = MAX(ip->ip_lblhigh);
437                 }
438
439                 /* Make sure each label is in a unique bblock */
440                 if (((ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) && 
441                     bb->first != ip) {
442                         bb = tmpalloc(sizeof(struct basicblock));
443                         bb->first = bb->last = ip;
ragge
1.11
444                         SLIST_INIT(&bb->children);
445                         SLIST_INIT(&bb->parents);
pj
1.8
446                         bb->dfnum = 0;
447                         bb->dfparent = 0;
448                         bb->semi = 0;
449                         bb->ancestor = 0;
450                         bb->idom = 0;
451                         bb->samedom = 0;
452                         bb->bucket = NULL;
453                         bb->df = NULL;
pj
1.10
454                         bb->dfchildren = NULL;
ragge
1.11
455                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.8
456                         count++;
pj
1.4
457                         continue;
458                 }
459
460                 if (ip->type == IP_ASM)
461                         return 0;
462
463                 if (ip->type == IP_NODE) {
464                         switch(ip->ip_node->n_op) {
465                         case CBRANCH:
466                         case GOTO:
467                         case RETURN:
468                                 /* Jumps, last in bblock. */
469                                 leader = 1;
470                                 break;
471
472                         case NAME:
473                         case ICON:
474                         case FCON:
475                         case REG:
476                         case OREG:
477                         case MOVE:
478                         case PLUS:
479                         case MINUS:
480                         case DIV:
481                         case MOD:
482                         case MUL:
483                         case AND:
484                         case OR:
485                         case ER:
486                         case LS:
487                         case COMPL:
488                         case INCR:
489                         case DECR:
490                         case UMUL:
491                         case UMINUS:
492                         case EQ:
493                         case NE:
494                         case LE:
495                         case GE:
496                         case GT:
497                         case ULE:
498                         case ULT:
499                         case UGE:
500                         case UGT:
501                         case ASSIGN:
502                         case FORCE:
503                         case FUNARG:
pj
1.7
504                         case CALL:
505                         case UCALL:
506                         case FORTCALL:
507                         case UFORTCALL:
508                         case STCALL:
509                         case USTCALL:
pj
1.4
510                                 /* Not jumps, continue with bblock. */
511                                 break;
512
513                         default:
514                                 comperr("optim2:bblocks_build() %d",ip->ip_node->n_op ); 
515                                 break;
516                         }
517                 }
518
519                 bb->last = ip;
520         }
521 #ifdef PCC_DEBUG
pj
1.8
522         printf("Basic blocks in func: %d\n"count);
pj
1.10
523         printf("Label range in func: %d\n"high - low + 1);
pj
1.4
524 #endif
525
526         labinfo->low = low;
527         labinfo->size = high - low + 1;
528         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
pj
1.8
529         for (i = 0i <= labinfo->sizei++) {
pj
1.4
530                 labinfo->arr[i] = NULL;
531         }
pj
1.8
532         
533         bbinfo->size = count + 1;
534         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
535         for (i = 0i <= bbinfo->sizei++) {
536                 bbinfo->arr[i] = NULL;
537         }
pj
1.4
538
539         /* Build the label table */
ragge
1.11
540         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
541                 if (bb->first->type == IP_DEFLAB) {
542                         labinfo->arr[bb->first->ip_lbl - low] = bb;
543                 }
544                 if (bb->first->type == IP_EPILOG) {
ragge
1.5
545                         labinfo->arr[bb->first->ip_lbl - low] = bb;
pj
1.4
546                 }
547         }
548
549         return 1;
550 }
551
552 /*
553  * Build the control flow graph.
554  */
555
556 void
557 cfg_build(struct labelinfo *labinfo)
558 {
559         /* Child and parent nodes */
560         struct cfgnode *cnode
561         struct cfgnode *pnode;
562         struct basicblock *bb;
563         
ragge
1.11
564         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
565
566                 if (bb->first->type == IP_EPILOG) {
567                         break;
568                 }
569
570                 cnode = tmpalloc(sizeof(struct cfgnode));
571                 pnode = tmpalloc(sizeof(struct cfgnode));
572                 pnode->bblock = bb;
573
574                 if ((bb->last->type == IP_NODE) && 
575                     (bb->last->ip_node->n_op == GOTO)) {
576                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
577                             labinfo->size) {
578                                 comperr("Label out of range: %d, base %d"
579                                         bb->last->ip_node->n_left->n_lval
580                                         labinfo->low);
581                         }
582                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
583                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
584                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
585                         continue;
586                 }
587                 if ((bb->last->type == IP_NODE) && 
588                     (bb->last->ip_node->n_op == CBRANCH)) {
589                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
590                             labinfo->size
591                                 comperr("Label out of range: %d"
592                                         bb->last->ip_node->n_left->n_lval);
593                         
594                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
595                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
596                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
597                         cnode = tmpalloc(sizeof(struct cfgnode));
598                         pnode = tmpalloc(sizeof(struct cfgnode));
599                         pnode->bblock = bb;
600                 }
601
ragge
1.11
602                 cnode->bblock = DLIST_NEXT(bbbbelem);
603                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
604                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
605         }
606 }
pj
1.6
607
pj
1.8
608 void
609 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
610 {
611         struct cfgnode *cnode;
612         
613         if (bb->dfnum != 0)
614                 return;
615
616         bb->dfnum = ++dfsnum;
617         bb->dfparent = parent;
618         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
619         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
620                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
621         }
pj
1.13
622         /* Don't bring in unreachable nodes in the future */
pj
1.14
623         bbinfo->size = dfsnum + 1
pj
1.8
624 }
625
ragge
1.11
626 static bittype *
627 setalloc(int nelem)
628 {
629         bittype *b;
630         int sz = (nelem+NUMBITS-1)/NUMBITS;
631
632         b = tmpalloc(sz * sizeof(bittype));
633         memset(b0sz * sizeof(bittype));
634         return b;
635 }
636
pj
1.8
637 /*
638  * Algorithm 19.9, pp 414 from Appel.
639  */
640
641 void
642 dominators(struct bblockinfo *bbinfo)
643 {
644         struct cfgnode *cnode;
645         struct basicblock *bb, *y, *v;
646         struct basicblock *s, *sprime, *p;
pj
1.10
647         int hi;
pj
1.8
648
ragge
1.11
649         DLIST_FOREACH(bb, &bblocksbbelem) {
650                 bb->bucket = setalloc(bbinfo->size);
651                 bb->df = setalloc(bbinfo->size);
652                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
653         }
654
655         dfsnum = 0;
ragge
1.11
656         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
657
pj
1.10
658         for(h = bbinfo->size - 1h > 1h--) {
659                 bb = bbinfo->arr[h];
pj
1.8
660                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
661                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
662                         if (cnode->bblock->dfnum <= bb->dfnum
663                                 sprime = cnode->bblock;
pj
1.10
664                         else 
665                                 sprime = bbinfo->arr[ancestorwithlowestsemi
666                                               (cnode->bblockbbinfo)->semi];
pj
1.8
667                         if (sprime->dfnum < s->dfnum)
668                                 s = sprime;
669                 }
670                 bb->semi = s->dfnum;
pj
1.10
671                 BITSET(s->bucketbb->dfnum);
pj
1.8
672                 link(pbb);
673                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
674                         if(TESTBIT(p->bucketi)) {
pj
1.8
675                                 v = bbinfo->arr[i];
pj
1.10
676                                 y = ancestorwithlowestsemi(vbbinfo);
677                                 if (y->semi == v->semi
pj
1.8
678                                         v->idom = p->dfnum;
679                                 else
680                                         v->samedom = y->dfnum;
681                         }
682                 }
683                 memset(p->bucket0, (bbinfo->size + 7)/8);
684         }
pj
1.10
685 #if 0
686         printf("Num\tSemi\tAncest\tidom\n");
pj
1.8
687         CIRCLEQ_FOREACH(bb, &bblocksbbelem) {
pj
1.10
688                 printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semibb->ancestorbb->idom);
689         }
690 #endif
691         for(h = 2h < bbinfo->sizeh++) {
692                 bb = bbinfo->arr[h];
693                 if (bb->samedom != 0) {
pj
1.8
694                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
695                 }
696         }
ragge
1.11
697         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
698                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
699 #if 0
700
701                         printf("Setting child %d of %d\n"bb->dfnumbbinfo->arr[bb->idom]->dfnum);
702 #endif
703
704                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
705                 }
pj
1.8
706         }
707 }
708
709
710 struct basicblock *
711 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
712 {
713         struct basicblock *u = bblock;
714         struct basicblock *v = bblock;
715
716         while (v->ancestor != 0) {
717                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
718                     bbinfo->arr[u->semi]->dfnum
pj
1.8
719                         u = v;
720                 v = bbinfo->arr[v->ancestor];
721         }
722         return u;
723 }
724
725 void
726 link(struct basicblock *parentstruct basicblock *child)
727 {
728         child->ancestor = parent->dfnum;
729 }
730
731 void
732 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
733 {
734         struct cfgnode *cnode;
pj
1.10
735         int hi;
pj
1.8
736         
ragge
1.11
737         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
738                 if (cnode->bblock->idom != bblock->dfnum)
739                         BITSET(bblock->dfcnode->bblock->dfnum);
740         }
pj
1.10
741         for (h = 1h < bbinfo->sizeh++) {
742                 if (!TESTBIT(bblock->dfchildrenh))
743                         continue;
744                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
745                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
746                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
747                             (bbinfo->arr[h] == bblock ||
748                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
749                             BITSET(bblock->dfi);
750                 }
751         }
752 }
pj
1.15
753
754 /*
755  * Remove unreachable nodes in the CFG.
756  */ 
757
758 void
759 remunreach(void)
760 {
761         struct basicblock *bb, *nbb;
762         struct interpass *next, *ctree;
763
764         bb = DLIST_NEXT(&bblocksbbelem);
765         while (bb != &bblocks) {
766                 nbb = DLIST_NEXT(bbbbelem);
767
768                 /* Code with dfnum 0 is unreachable */
769                 if (bb->dfnum != 0) {
770                         bb = nbb;
771                         continue;
772                 }
773
774                 /* Need the epilogue node for other parts of the
775                    compiler, set its label to 0 and backend will
776                    handle it. */ 
777                 if (bb->first->type == IP_EPILOG) {
778                         bb->first->ip_lbl = 0;
779                         bb = nbb;
780                         continue;
781                 }
782
783                 next = bb->first;
784                 do {
785                         ctree = next;
786                         next = DLIST_NEXT(ctreeqelem);
787                         
788                         if (ctree->type == IP_NODE)
pj
1.16
789                                 tfree(ctree->ip_node);
pj
1.15
790                         DLIST_REMOVE(ctreeqelem);
791                 } while (ctree != bb->last);
792                         
793                 DLIST_REMOVE(bbbbelem);
794                 bb = nbb;
795         }
796 }
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-21 10:55 +0100