Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050402144817

Diff

Diff from 1.18 to:

Annotations

Annotate by Age | Author | Mixed | None

Annotated File View

ragge
1.18
1 /*      $Id: optim2.c,v 1.18 2005/04/02 14:48: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.18
317 #ifdef NEW_READER
318         Ocompile(&ipole);
319 #else
ragge
1.11
320         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
321                 pass2_compile(ip);
322         }
ragge
1.18
323 #endif
ragge
1.11
324         DLIST_INIT(&ipoleqelem);
ragge
1.1
325 }
326
327 void
328 deljumps()
329 {
330         struct interpass *ip, *n;
331         int gotone;
332
333 again:  gotone = 0;
334
ragge
1.11
335         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
336                 if (ip->type == IP_EPILOG)
337                         return;
338                 if (ip->type != IP_NODE)
339                         continue;
ragge
1.11
340                 n = DLIST_NEXT(ipqelem);
ragge
1.1
341                 /* Check for nodes without side effects */
342                 if (ip->ip_node->n_op != GOTO)
343                         continue;
344                 switch (n->type) {
345                 case IP_NODE:
346                         tfree(n->ip_node);
ragge
1.11
347                         DLIST_REMOVE(nqelem);
ragge
1.1
348                         break;
349                 case IP_DEFLAB:
350                         if (ip->ip_node->n_left->n_lval != n->ip_lbl)
351                                 continue;
352                         tfree(ip->ip_node);
353                         *ip = *n;
354                         break;
355                 default:
356                         continue;
357                 }
358                 gotone = 1;
359         }
360         if (gotone)
361                 goto again;
362 }
363
364 void
365 optdump(struct interpass *ip)
366 {
367         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
368                 "deflab""defnam""asm" };
369         printf("type %s\n"nm[ip->type-1]);
370         switch (ip->type) {
371         case IP_NODE:
372                 fwalk(ip->ip_nodee2print0);
373                 break;
374         case IP_DEFLAB:
375                 printf("label " LABFMT "\n"ip->ip_lbl);
376                 break;
377         case IP_ASM:
378                 printf(": %s\n"ip->ip_asm);
379                 break;
380         }
381 }
pj
1.4
382
383 /*
384  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
385  *
386  * Also fills the labelinfo struct with information about which bblocks
387  * that contain which label.
388  */
389
390 int
pj
1.8
391 bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo)
pj
1.4
392 {
393         struct interpass *ip;
394         struct basicblock *bb = NULL;
395         int leader = 1;
pj
1.12
396         unsigned int lowhigh = 0;
pj
1.8
397         int count = 0;
pj
1.4
398         int i;
399
pj
1.12
400         low = sizeof(int) == 2 ? 65000 : 1000000000;
pj
1.4
401         /* 
402          * First statement is a leader.
403          * Any statement that is target of a jump is a leader.
404          * Any statement that immediately follows a jump is a leader.
405          */
406
ragge
1.11
407         DLIST_FOREACH(ip, &ipoleqelem) {
pj
1.4
408                 /* Garbage, skip it */
409                 if (leader) {
410                         bb = tmpalloc(sizeof(struct basicblock));
411                         bb->first = bb->last = ip;
ragge
1.11
412                         SLIST_INIT(&bb->children);
413                         SLIST_INIT(&bb->parents);
pj
1.8
414                         bb->dfnum = 0;
415                         bb->dfparent = 0;
416                         bb->semi = 0;
417                         bb->ancestor = 0;
418                         bb->idom = 0;
419                         bb->samedom = 0;
420                         bb->bucket = NULL;
421                         bb->df = NULL;
pj
1.10
422                         bb->dfchildren = NULL;
ragge
1.11
423                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.4
424                         leader = 0;
pj
1.8
425                         count++;
pj
1.4
426                 } 
427                 
428                 /* Prologue and epilogue in their own bblock */
429                 if ((ip->type == IP_PROLOG) || (ip->type == IP_EPILOG)) {
430                         bb->last = ip;
431                         if (ip->type == IP_EPILOG)
ragge
1.5
432                                 high = MAX(ip->ip_lblhigh);
pj
1.4
433                         leader = 1;
434                         continue;
435                 }
436                 
437                 /* Keep track of highest and lowest label number */
438                 if (ip->type == IP_DEFLAB) {
439                         low = MIN(ip->ip_lbllow);
440                         high = MAX(ip->ip_lblhigh);
441                 }
442
443                 /* Make sure each label is in a unique bblock */
444                 if (((ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) && 
445                     bb->first != ip) {
446                         bb = tmpalloc(sizeof(struct basicblock));
447                         bb->first = bb->last = ip;
ragge
1.11
448                         SLIST_INIT(&bb->children);
449                         SLIST_INIT(&bb->parents);
pj
1.8
450                         bb->dfnum = 0;
451                         bb->dfparent = 0;
452                         bb->semi = 0;
453                         bb->ancestor = 0;
454                         bb->idom = 0;
455                         bb->samedom = 0;
456                         bb->bucket = NULL;
457                         bb->df = NULL;
pj
1.10
458                         bb->dfchildren = NULL;
ragge
1.11
459                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.8
460                         count++;
pj
1.4
461                         continue;
462                 }
463
464                 if (ip->type == IP_ASM)
465                         return 0;
466
467                 if (ip->type == IP_NODE) {
468                         switch(ip->ip_node->n_op) {
469                         case CBRANCH:
470                         case GOTO:
471                         case RETURN:
472                                 /* Jumps, last in bblock. */
473                                 leader = 1;
474                                 break;
475
476                         case NAME:
477                         case ICON:
478                         case FCON:
479                         case REG:
480                         case OREG:
481                         case MOVE:
482                         case PLUS:
483                         case MINUS:
484                         case DIV:
485                         case MOD:
486                         case MUL:
487                         case AND:
488                         case OR:
489                         case ER:
490                         case LS:
491                         case COMPL:
492                         case INCR:
493                         case DECR:
494                         case UMUL:
495                         case UMINUS:
496                         case EQ:
497                         case NE:
498                         case LE:
499                         case GE:
500                         case GT:
501                         case ULE:
502                         case ULT:
503                         case UGE:
504                         case UGT:
505                         case ASSIGN:
506                         case FORCE:
507                         case FUNARG:
pj
1.7
508                         case CALL:
509                         case UCALL:
510                         case FORTCALL:
511                         case UFORTCALL:
512                         case STCALL:
513                         case USTCALL:
pj
1.4
514                                 /* Not jumps, continue with bblock. */
515                                 break;
516
517                         default:
518                                 comperr("optim2:bblocks_build() %d",ip->ip_node->n_op ); 
519                                 break;
520                         }
521                 }
522
523                 bb->last = ip;
524         }
525 #ifdef PCC_DEBUG
pj
1.8
526         printf("Basic blocks in func: %d\n"count);
pj
1.10
527         printf("Label range in func: %d\n"high - low + 1);
pj
1.4
528 #endif
529
530         labinfo->low = low;
531         labinfo->size = high - low + 1;
532         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
pj
1.8
533         for (i = 0i <= labinfo->sizei++) {
pj
1.4
534                 labinfo->arr[i] = NULL;
535         }
pj
1.8
536         
537         bbinfo->size = count + 1;
538         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
539         for (i = 0i <= bbinfo->sizei++) {
540                 bbinfo->arr[i] = NULL;
541         }
pj
1.4
542
543         /* Build the label table */
ragge
1.11
544         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
545                 if (bb->first->type == IP_DEFLAB) {
546                         labinfo->arr[bb->first->ip_lbl - low] = bb;
547                 }
548                 if (bb->first->type == IP_EPILOG) {
ragge
1.5
549                         labinfo->arr[bb->first->ip_lbl - low] = bb;
pj
1.4
550                 }
551         }
552
553         return 1;
554 }
555
556 /*
557  * Build the control flow graph.
558  */
559
560 void
561 cfg_build(struct labelinfo *labinfo)
562 {
563         /* Child and parent nodes */
564         struct cfgnode *cnode
565         struct cfgnode *pnode;
566         struct basicblock *bb;
567         
ragge
1.11
568         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
569
570                 if (bb->first->type == IP_EPILOG) {
571                         break;
572                 }
573
574                 cnode = tmpalloc(sizeof(struct cfgnode));
575                 pnode = tmpalloc(sizeof(struct cfgnode));
576                 pnode->bblock = bb;
577
578                 if ((bb->last->type == IP_NODE) && 
579                     (bb->last->ip_node->n_op == GOTO)) {
580                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
581                             labinfo->size) {
582                                 comperr("Label out of range: %d, base %d"
583                                         bb->last->ip_node->n_left->n_lval
584                                         labinfo->low);
585                         }
586                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
587                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
588                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
589                         continue;
590                 }
591                 if ((bb->last->type == IP_NODE) && 
592                     (bb->last->ip_node->n_op == CBRANCH)) {
593                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
594                             labinfo->size
595                                 comperr("Label out of range: %d"
596                                         bb->last->ip_node->n_left->n_lval);
597                         
598                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
599                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
600                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
601                         cnode = tmpalloc(sizeof(struct cfgnode));
602                         pnode = tmpalloc(sizeof(struct cfgnode));
603                         pnode->bblock = bb;
604                 }
605
ragge
1.11
606                 cnode->bblock = DLIST_NEXT(bbbbelem);
607                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
608                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
609         }
610 }
pj
1.6
611
pj
1.8
612 void
613 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
614 {
615         struct cfgnode *cnode;
616         
617         if (bb->dfnum != 0)
618                 return;
619
620         bb->dfnum = ++dfsnum;
621         bb->dfparent = parent;
622         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
623         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
624                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
625         }
pj
1.13
626         /* Don't bring in unreachable nodes in the future */
pj
1.14
627         bbinfo->size = dfsnum + 1
pj
1.8
628 }
629
ragge
1.11
630 static bittype *
631 setalloc(int nelem)
632 {
633         bittype *b;
634         int sz = (nelem+NUMBITS-1)/NUMBITS;
635
636         b = tmpalloc(sz * sizeof(bittype));
637         memset(b0sz * sizeof(bittype));
638         return b;
639 }
640
pj
1.8
641 /*
642  * Algorithm 19.9, pp 414 from Appel.
643  */
644
645 void
646 dominators(struct bblockinfo *bbinfo)
647 {
648         struct cfgnode *cnode;
649         struct basicblock *bb, *y, *v;
650         struct basicblock *s, *sprime, *p;
pj
1.10
651         int hi;
pj
1.8
652
ragge
1.11
653         DLIST_FOREACH(bb, &bblocksbbelem) {
654                 bb->bucket = setalloc(bbinfo->size);
655                 bb->df = setalloc(bbinfo->size);
656                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
657         }
658
659         dfsnum = 0;
ragge
1.11
660         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
661
pj
1.10
662         for(h = bbinfo->size - 1h > 1h--) {
663                 bb = bbinfo->arr[h];
pj
1.8
664                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
665                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
666                         if (cnode->bblock->dfnum <= bb->dfnum
667                                 sprime = cnode->bblock;
pj
1.10
668                         else 
669                                 sprime = bbinfo->arr[ancestorwithlowestsemi
670                                               (cnode->bblockbbinfo)->semi];
pj
1.8
671                         if (sprime->dfnum < s->dfnum)
672                                 s = sprime;
673                 }
674                 bb->semi = s->dfnum;
pj
1.10
675                 BITSET(s->bucketbb->dfnum);
pj
1.8
676                 link(pbb);
677                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
678                         if(TESTBIT(p->bucketi)) {
pj
1.8
679                                 v = bbinfo->arr[i];
pj
1.10
680                                 y = ancestorwithlowestsemi(vbbinfo);
681                                 if (y->semi == v->semi
pj
1.8
682                                         v->idom = p->dfnum;
683                                 else
684                                         v->samedom = y->dfnum;
685                         }
686                 }
687                 memset(p->bucket0, (bbinfo->size + 7)/8);
688         }
pj
1.10
689 #if 0
690         printf("Num\tSemi\tAncest\tidom\n");
pj
1.8
691         CIRCLEQ_FOREACH(bb, &bblocksbbelem) {
pj
1.10
692                 printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semibb->ancestorbb->idom);
693         }
694 #endif
695         for(h = 2h < bbinfo->sizeh++) {
696                 bb = bbinfo->arr[h];
697                 if (bb->samedom != 0) {
pj
1.8
698                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
699                 }
700         }
ragge
1.11
701         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
702                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
703 #if 0
704
705                         printf("Setting child %d of %d\n"bb->dfnumbbinfo->arr[bb->idom]->dfnum);
706 #endif
707
708                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
709                 }
pj
1.8
710         }
711 }
712
713
714 struct basicblock *
715 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
716 {
717         struct basicblock *u = bblock;
718         struct basicblock *v = bblock;
719
720         while (v->ancestor != 0) {
721                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
722                     bbinfo->arr[u->semi]->dfnum
pj
1.8
723                         u = v;
724                 v = bbinfo->arr[v->ancestor];
725         }
726         return u;
727 }
728
729 void
730 link(struct basicblock *parentstruct basicblock *child)
731 {
732         child->ancestor = parent->dfnum;
733 }
734
735 void
736 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
737 {
738         struct cfgnode *cnode;
pj
1.10
739         int hi;
pj
1.8
740         
ragge
1.11
741         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
742                 if (cnode->bblock->idom != bblock->dfnum)
743                         BITSET(bblock->dfcnode->bblock->dfnum);
744         }
pj
1.10
745         for (h = 1h < bbinfo->sizeh++) {
746                 if (!TESTBIT(bblock->dfchildrenh))
747                         continue;
748                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
749                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
750                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
751                             (bbinfo->arr[h] == bblock ||
752                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
753                             BITSET(bblock->dfi);
754                 }
755         }
756 }
pj
1.15
757
758 /*
759  * Remove unreachable nodes in the CFG.
760  */ 
761
762 void
763 remunreach(void)
764 {
765         struct basicblock *bb, *nbb;
766         struct interpass *next, *ctree;
767
768         bb = DLIST_NEXT(&bblocksbbelem);
769         while (bb != &bblocks) {
770                 nbb = DLIST_NEXT(bbbbelem);
771
772                 /* Code with dfnum 0 is unreachable */
773                 if (bb->dfnum != 0) {
774                         bb = nbb;
775                         continue;
776                 }
777
778                 /* Need the epilogue node for other parts of the
779                    compiler, set its label to 0 and backend will
780                    handle it. */ 
781                 if (bb->first->type == IP_EPILOG) {
782                         bb->first->ip_lbl = 0;
783                         bb = nbb;
784                         continue;
785                 }
786
787                 next = bb->first;
788                 do {
789                         ctree = next;
790                         next = DLIST_NEXT(ctreeqelem);
791                         
792                         if (ctree->type == IP_NODE)
pj
1.16
793                                 tfree(ctree->ip_node);
pj
1.15
794                         DLIST_REMOVE(ctreeqelem);
795                 } while (ctree != bb->last);
796                         
797                 DLIST_REMOVE(bbbbelem);
798                 bb = nbb;
799         }
800 }
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-22 23:03 +0100