Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20050501100451

Diff

Diff from 1.21 to:

Annotations

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

Annotated File View

pj
1.21
1 /*      $Id: optim2.c,v 1.21 2005/05/01 10:04:51 pj 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);
ragge
1.19
49 void printip(struct interpass *pole);
ragge
1.1
50
pj
1.21
51 static struct varinfo defsites;
ragge
1.11
52 static struct interpass ipole;
ragge
1.19
53 struct interpass *storesave;
ragge
1.1
54
55 static struct rsv {
56         struct rsv *next;
57         int fpoff;
58         TWORD type;
59         int use;
60 } *rsv;
61
pj
1.8
62 int bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo);
pj
1.4
63 void cfg_build(struct labelinfo *labinfo);
pj
1.8
64 void cfg_dfs(struct basicblock *bbunsigned int parent
65              struct bblockinfo *bbinfo);
66 void dominators(struct bblockinfo *bbinfo);
pj
1.9
67 struct basicblock *
68 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo);
69 void link(struct basicblock *parentstruct basicblock *child);
70 void computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo);
pj
1.21
71 void findTemps(struct interpass *ip);
pj
1.20
72 void placePhiFunctions(struct bblockinfo *bbinfo);
pj
1.15
73 void remunreach(void);
pj
1.4
74
ragge
1.11
75 static struct basicblock bblocks;
pj
1.4
76
ragge
1.1
77 static void
78 addcand(TWORD typeint offint avoid)
79 {
80         struct rsv *w = rsv;
81
82         while (w != NULL) {
83                 if (w->type == type && w->fpoff == off) {
84                         if (avoid)
85                                 w->use = -1;
86                         else if (w->use > 0)
87                                 w->use++;
88                         return;
89                 }
90                 w = w->next;
91         }
92         w = tmpalloc(sizeof(*w));
93         w->type = type;
94         w->fpoff = off;
95         w->use = avoid ? -1 : 1;
96         w->next = rsv;
97         rsv = w;
98 }
99
100 /*
101  * walk through the tree and count the number of (possible)
102  * temporary nodes.
103  */
104 static void
105 cntuse(NODE *p)
106 {
107         NODE *l = p->n_left;
108         NODE *r = p->n_right;
109
110         if (p->n_op == UMUL && l->n_op == REG && l->n_rval == FPREG) {
111                 /* found a candidate for register */
112                 addcand(p->n_type0ISVOL(p->n_qual << TSHIFT));
113         } else if (p->n_op == UMUL && l->n_op == PLUS &&
114             l->n_right->n_op == ICON && 
115              (l->n_left->n_op == REG && l->n_left->n_rval == FPREG)) {
116                 /* The same as above */
117                 addcand(p->n_typel->n_right->n_lval,
118                     ISVOL(p->n_qual << TSHIFT));
119         } else if (p->n_op == PLUS && l->n_op == REG && l->n_rval == FPREG &&
120             p->n_right->n_op == ICON) {
121                 /* Address taken of temporary, avoid register */
122                 addcand(DECREF(p->n_type), r->n_lval1);
123         } else {
124                 if (optype(p->n_op) == BITYPE)
125                         cntuse(r);
126                 if (optype(p->n_op) != LTYPE)
127                         cntuse(l);
128         }
129 }
130
131 /*
ragge
1.2
132  * Insert a node into the register stack.
133  */
134 static void
135 insert(struct rsv *wstruct rsv **savedint maxregs)
136 {
137         int ijsize;
138
139         size = szty(w->type);
140
141         /* Find reg move position */
142         for (i = 0i < maxregsi++) {
143                 if (saved[i] == NULL)
144                         continue;
145                 if (saved[i]->use > w->use)
146                         break;
147         }
148         /* Move down other regs */
149         for (j = sizej < ij++)
150                 saved[j-size] = saved[j];
151
152         /* Insert new reg pointer */
153         if (i-size >= 0) {
154                 saved[i-size] = w;
155                 for (j = i-size+1j < ij++)
156                         saved[j] = NULL;
157         }
158 }
159
160 /* Help routine to rconvert() */
161 static int
162 matches(TWORD typeint offstruct rsv **rsvint maxregs)
163 {
164         int i;
165
166         for (i = 0i < maxregsi++)
167                 if (rsv[i] && rsv[i]->type == type && rsv[i]->fpoff == off)
168                         return i;
169         return -1;
170 }
171
172 /* Help routine to rconvert() */
173 static void
174 modify(NODE *pint reg)
175 {
176         tfree(p->n_left);
177         p->n_op = REG;
178         p->n_rval = p->n_rall = reg + MINRVAR;
179         p->n_lval = 0;
180 }
181
182 /*
183  * walk through the tree and convert nodes to registers
184  */
185 static void
186 rconvert(NODE *pstruct rsv **rsvint maxregs)
187 {
188         NODE *l = p->n_left;
189         NODE *r = p->n_right;
190         int i;
191
192         if (p->n_op == UMUL && l->n_op == REG && l->n_rval == FPREG) {
193                 /* found a candidate for register */
194                 if ((i = matches(p->n_type0rsvmaxregs)) >= 0)
195                         modify(pi);
196         } else if (p->n_op == UMUL && l->n_op == PLUS &&
197             l->n_right->n_op == ICON && 
198              (l->n_left->n_op == REG && l->n_left->n_rval == FPREG)) {
199                 /* The same as above */
200                 if ((i = matches(p->n_type,
201                     l->n_right->n_lvalrsvmaxregs)) >= 0)
202                         modify(pi);
203 #if 0
204         } else if (p->n_op == PLUS && l->n_op == REG && l->n_rval == FPREG &&
205             p->n_right->n_op == ICON) {
206                 /* Address taken of temporary, avoid register */
207                 addcand(DECREF(p->n_type), r->n_lval1);
208 #endif
209         } else {
210                 if (optype(p->n_op) == BITYPE)
211                         rconvert(rrsvmaxregs);
212                 if (optype(p->n_op) != LTYPE)
213                         rconvert(lrsvmaxregs);
214         }
215 }
216
217 /*
ragge
1.1
218  * Assign non-temporary registers to variables.
219  * Cannot do it if:
220  * - address is taken of the temporary
221  * - variable is declared "volatile".
222  */
pj
1.4
223 int asgregs(void);
224 int
ragge
1.1
225 asgregs(void)
226 {
227         struct interpass *ip;
ragge
1.2
228         struct rsv *w, **saved;
229         int imaxregs = MAXRVAR - MINRVAR + 1;
ragge
1.1
230
ragge
1.2
231         if (maxregs == 0)
232                 return MAXRVAR/* No register usage */
ragge
1.1
233         rsv = NULL;
234
235         /* Loop over the function to do a usage count */
ragge
1.11
236         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
237                 if (ip->type != IP_NODE)
238                         continue;
239                 cntuse(ip->ip_node);
240         }
241         /* Check which nodes that shall be converted to registers */
ragge
1.2
242         saved = tmpalloc(sizeof(struct rsv *) * maxregs);
243         memset(saved0sizeof(struct rsv *) * maxregs);
ragge
1.1
244         w = rsv;
245         for (w = rsvww = w->next) {
246                 if (w->use < 0)
ragge
1.2
247                         continue/* Not allowed to be in register */
248
249                 /* XXX check here if type is allowed to be in register */
250
251                 insert(wsavedmaxregs);
ragge
1.1
252         }
253
254         /* Convert found nodes to registers */
ragge
1.11
255         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.2
256                 if (ip->type != IP_NODE)
257                         continue;
258                 rconvert(ip->ip_nodesavedmaxregs);
259         }
260         for (i = 0i < maxregsi++)
261                 if (saved[i] != NULL)
262                         break;
263         return MINRVAR+i-1;
ragge
1.1
264 }
265
266 void
267 saveip(struct interpass *ip)
268 {
ragge
1.5
269         struct interpass_prolog *ipp, *epp;
270
pj
1.4
271 #if 0
ragge
1.2
272         int regs;
pj
1.4
273 #endif
274         struct labelinfo labinfo;
pj
1.8
275         struct bblockinfo bbinfo;
ragge
1.1
276
ragge
1.11
277         if (ip->type == IP_PROLOG)
278                 DLIST_INIT(&ipoleqelem);
279
280         DLIST_INSERT_BEFORE(&ipoleipqelem);
ragge
1.1
281
282         if (ip->type != IP_EPILOG)
283                 return;
ragge
1.5
284         epp = (struct interpass_prolog *)ip;
ragge
1.1
285         saving = -1;
286
pj
1.4
287         //              deljumps();     /* Delete redundant jumps and dead code */
288         if (xssaflag) {
ragge
1.11
289                 DLIST_INIT(&bblocksbbelem);
pj
1.8
290                 if (bblocks_build(&labinfo, &bbinfo)) {
pj
1.4
291                         cfg_build(&labinfo);
pj
1.8
292                         dominators(&bbinfo);
ragge
1.11
293                         computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
pj
1.15
294                         remunreach();
pj
1.4
295 #if 0
pj
1.6
296                         if (xssaflag) {
297                                 dfg = dfg_build(cfg);
298                                 ssa = ssa_build(cfgdfg);
299                         }
pj
1.4
300 #endif
301                 }
302  
303         }
304 #if 0
ragge
1.2
305         regs = asgregs();       /* Assign non-temporary registers */
pj
1.4
306 #endif
ragge
1.2
307
308 #ifdef PCC_DEBUG
ragge
1.5
309         if (epp->ipp_regs != MAXRVAR)
ragge
1.2
310                 comperr("register error");
311 #endif
ragge
1.1
312
ragge
1.11
313         ipp = (struct interpass_prolog *)DLIST_NEXT(&ipoleqelem);
ragge
1.5
314         ipp->ipp_autos = epp->ipp_autos;
315         ipp->ipp_regs = epp->ipp_regs// = regs;
ragge
1.1
316
317 #ifdef MYOPTIM
pj
1.6
318         myoptim((struct interpass *)ipp);
ragge
1.1
319 #endif
320
ragge
1.18
321 #ifdef NEW_READER
ragge
1.19
322 if (xnewreg == 0) {
323         int tmpautooff;
324         NODE *p;
325
326         p2autooff = p2maxautooff = AUTOINIT;
327         /* Must verify stack usage first */
328         DLIST_FOREACH(ip, &ipoleqelem) {
329                 if (ip->type == IP_STKOFF) {
330                         p2autooff = ip->ip_off;
331                         if (p2autooff > p2maxautooff)
332                                 p2maxautooff = p2autooff;
333                 }
334         }
335         p2autooff = p2maxautooff/* don't have live range analysis yet */
336
337         DLIST_FOREACH(ip, &ipoleqelem) {
338                 if (ip->type == (MAXIP+1)) {
339                         struct interpass *ip3;
340                         p2autooff = ip->ip_off;
341                         ip3 = ip;
342                         ip = DLIST_NEXT(ipqelem);
343                         DLIST_REMOVE(ip3qelem);
344                 }
345                         
346                 if (ip->type != IP_NODE)
347                         continue;
348
349                 p = ip->ip_node;
350                 walkf(pdeltemp);
351
352                 tmpautooff = p2autooff;
353
354                 geninsn(pFOREFF);
355                 if (sucomp(p) < 0) {
356                         /* Create STKOFF entry */
357                         struct interpass *ip3;
358                         DLIST_INSERT_BEFORE(ipstoresaveqelem);
359                         ip3 = ipnode(NULL);
360                         ip3->type = (MAXIP+1);
361                         ip3->ip_off = tmpautooff;
362                         DLIST_INSERT_AFTER(ipip3qelem);
363                         ip = DLIST_PREV(storesaveqelem);
364                         continue;
365                 }
366
367                 p2autooff = tmpautooff;
368
369                 genregs(p);
370                 mygenregs(p);
371         }
372
373 else {
374         /*
375          * Loop over instruction assignment until the register assignment
376          * code is satisfied.
377          */
378         do {
379                 /* do instruction assignment */
380                 DLIST_FOREACH(ip, &ipoleqelem) {
381                         if (ip->type != IP_NODE)
382                                 continue;
383                         geninsn(ip->ip_nodeFOREFF);
384                         nsucomp(ip->ip_node);
385                 }
386         } while (ngenregs(DLIST_NEXT(&ipoleqelem),
387             DLIST_PREV(&ipoleqelem)));
388 }
389
390         DLIST_FOREACH(ip, &ipoleqelem) {
391                 emit(ip);
392         }
ragge
1.18
393 #else
ragge
1.11
394         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
395                 pass2_compile(ip);
396         }
ragge
1.18
397 #endif
ragge
1.11
398         DLIST_INIT(&ipoleqelem);
ragge
1.1
399 }
400
401 void
402 deljumps()
403 {
404         struct interpass *ip, *n;
405         int gotone;
406
407 again:  gotone = 0;
408
ragge
1.11
409         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
410                 if (ip->type == IP_EPILOG)
411                         return;
412                 if (ip->type != IP_NODE)
413                         continue;
ragge
1.11
414                 n = DLIST_NEXT(ipqelem);
ragge
1.1
415                 /* Check for nodes without side effects */
416                 if (ip->ip_node->n_op != GOTO)
417                         continue;
418                 switch (n->type) {
419                 case IP_NODE:
420                         tfree(n->ip_node);
ragge
1.11
421                         DLIST_REMOVE(nqelem);
ragge
1.1
422                         break;
423                 case IP_DEFLAB:
424                         if (ip->ip_node->n_left->n_lval != n->ip_lbl)
425                                 continue;
426                         tfree(ip->ip_node);
427                         *ip = *n;
428                         break;
429                 default:
430                         continue;
431                 }
432                 gotone = 1;
433         }
434         if (gotone)
435                 goto again;
436 }
437
438 void
439 optdump(struct interpass *ip)
440 {
441         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
442                 "deflab""defnam""asm" };
443         printf("type %s\n"nm[ip->type-1]);
444         switch (ip->type) {
445         case IP_NODE:
446                 fwalk(ip->ip_nodee2print0);
447                 break;
448         case IP_DEFLAB:
449                 printf("label " LABFMT "\n"ip->ip_lbl);
450                 break;
451         case IP_ASM:
452                 printf(": %s\n"ip->ip_asm);
453                 break;
454         }
455 }
pj
1.4
456
457 /*
458  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
459  *
460  * Also fills the labelinfo struct with information about which bblocks
461  * that contain which label.
462  */
463
464 int
pj
1.8
465 bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo)
pj
1.4
466 {
467         struct interpass *ip;
468         struct basicblock *bb = NULL;
469         int leader = 1;
pj
1.12
470         unsigned int lowhigh = 0;
pj
1.8
471         int count = 0;
pj
1.4
472         int i;
473
pj
1.12
474         low = sizeof(int) == 2 ? 65000 : 1000000000;
pj
1.4
475         /* 
476          * First statement is a leader.
477          * Any statement that is target of a jump is a leader.
478          * Any statement that immediately follows a jump is a leader.
479          */
480
ragge
1.11
481         DLIST_FOREACH(ip, &ipoleqelem) {
pj
1.4
482                 /* Garbage, skip it */
483                 if (leader) {
484                         bb = tmpalloc(sizeof(struct basicblock));
485                         bb->first = bb->last = ip;
ragge
1.11
486                         SLIST_INIT(&bb->children);
487                         SLIST_INIT(&bb->parents);
pj
1.8
488                         bb->dfnum = 0;
489                         bb->dfparent = 0;
490                         bb->semi = 0;
491                         bb->ancestor = 0;
492                         bb->idom = 0;
493                         bb->samedom = 0;
494                         bb->bucket = NULL;
495                         bb->df = NULL;
pj
1.10
496                         bb->dfchildren = NULL;
ragge
1.11
497                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.4
498                         leader = 0;
pj
1.8
499                         count++;
pj
1.4
500                 } 
501                 
502                 /* Prologue and epilogue in their own bblock */
503                 if ((ip->type == IP_PROLOG) || (ip->type == IP_EPILOG)) {
504                         bb->last = ip;
505                         if (ip->type == IP_EPILOG)
ragge
1.5
506                                 high = MAX(ip->ip_lblhigh);
pj
1.4
507                         leader = 1;
508                         continue;
509                 }
510                 
511                 /* Keep track of highest and lowest label number */
512                 if (ip->type == IP_DEFLAB) {
513                         low = MIN(ip->ip_lbllow);
514                         high = MAX(ip->ip_lblhigh);
515                 }
516
517                 /* Make sure each label is in a unique bblock */
518                 if (((ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) && 
519                     bb->first != ip) {
520                         bb = tmpalloc(sizeof(struct basicblock));
521                         bb->first = bb->last = ip;
ragge
1.11
522                         SLIST_INIT(&bb->children);
523                         SLIST_INIT(&bb->parents);
pj
1.8
524                         bb->dfnum = 0;
525                         bb->dfparent = 0;
526                         bb->semi = 0;
527                         bb->ancestor = 0;
528                         bb->idom = 0;
529                         bb->samedom = 0;
530                         bb->bucket = NULL;
531                         bb->df = NULL;
pj
1.10
532                         bb->dfchildren = NULL;
ragge
1.11
533                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.8
534                         count++;
pj
1.4
535                         continue;
536                 }
537
538                 if (ip->type == IP_ASM)
539                         return 0;
540
541                 if (ip->type == IP_NODE) {
542                         switch(ip->ip_node->n_op) {
543                         case CBRANCH:
544                         case GOTO:
545                         case RETURN:
546                                 /* Jumps, last in bblock. */
547                                 leader = 1;
548                                 break;
549
550                         case NAME:
551                         case ICON:
552                         case FCON:
553                         case REG:
554                         case OREG:
555                         case MOVE:
556                         case PLUS:
557                         case MINUS:
558                         case DIV:
559                         case MOD:
560                         case MUL:
561                         case AND:
562                         case OR:
563                         case ER:
564                         case LS:
565                         case COMPL:
566                         case INCR:
567                         case DECR:
568                         case UMUL:
569                         case UMINUS:
570                         case EQ:
571                         case NE:
572                         case LE:
573                         case GE:
574                         case GT:
575                         case ULE:
576                         case ULT:
577                         case UGE:
578                         case UGT:
579                         case ASSIGN:
580                         case FORCE:
581                         case FUNARG:
pj
1.7
582                         case CALL:
583                         case UCALL:
584                         case FORTCALL:
585                         case UFORTCALL:
586                         case STCALL:
587                         case USTCALL:
pj
1.4
588                                 /* Not jumps, continue with bblock. */
589                                 break;
590
591                         default:
592                                 comperr("optim2:bblocks_build() %d",ip->ip_node->n_op ); 
593                                 break;
594                         }
595                 }
596
597                 bb->last = ip;
598         }
pj
1.20
599 #if 0
pj
1.8
600         printf("Basic blocks in func: %d\n"count);
pj
1.10
601         printf("Label range in func: %d\n"high - low + 1);
pj
1.4
602 #endif
603
604         labinfo->low = low;
605         labinfo->size = high - low + 1;
606         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
pj
1.8
607         for (i = 0i <= labinfo->sizei++) {
pj
1.4
608                 labinfo->arr[i] = NULL;
609         }
pj
1.8
610         
611         bbinfo->size = count + 1;
612         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
613         for (i = 0i <= bbinfo->sizei++) {
614                 bbinfo->arr[i] = NULL;
615         }
pj
1.4
616
ragge
1.11
617         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.20
618                 /* Build the label table */
pj
1.4
619                 if (bb->first->type == IP_DEFLAB) {
620                         labinfo->arr[bb->first->ip_lbl - low] = bb;
621                 }
622                 if (bb->first->type == IP_EPILOG) {
ragge
1.5
623                         labinfo->arr[bb->first->ip_lbl - low] = bb;
pj
1.4
624                 }
625         }
626
627         return 1;
628 }
629
630 /*
631  * Build the control flow graph.
632  */
633
634 void
635 cfg_build(struct labelinfo *labinfo)
636 {
637         /* Child and parent nodes */
638         struct cfgnode *cnode
639         struct cfgnode *pnode;
640         struct basicblock *bb;
641         
ragge
1.11
642         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
643
644                 if (bb->first->type == IP_EPILOG) {
645                         break;
646                 }
647
648                 cnode = tmpalloc(sizeof(struct cfgnode));
649                 pnode = tmpalloc(sizeof(struct cfgnode));
650                 pnode->bblock = bb;
651
652                 if ((bb->last->type == IP_NODE) && 
653                     (bb->last->ip_node->n_op == GOTO)) {
654                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
655                             labinfo->size) {
656                                 comperr("Label out of range: %d, base %d"
657                                         bb->last->ip_node->n_left->n_lval
658                                         labinfo->low);
659                         }
660                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
661                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
662                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
663                         continue;
664                 }
665                 if ((bb->last->type == IP_NODE) && 
666                     (bb->last->ip_node->n_op == CBRANCH)) {
667                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
668                             labinfo->size
669                                 comperr("Label out of range: %d"
670                                         bb->last->ip_node->n_left->n_lval);
671                         
672                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
673                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
674                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
675                         cnode = tmpalloc(sizeof(struct cfgnode));
676                         pnode = tmpalloc(sizeof(struct cfgnode));
677                         pnode->bblock = bb;
678                 }
679
ragge
1.11
680                 cnode->bblock = DLIST_NEXT(bbbbelem);
681                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
682                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
683         }
684 }
pj
1.6
685
pj
1.8
686 void
687 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
688 {
689         struct cfgnode *cnode;
690         
691         if (bb->dfnum != 0)
692                 return;
693
694         bb->dfnum = ++dfsnum;
695         bb->dfparent = parent;
696         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
697         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
698                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
699         }
pj
1.13
700         /* Don't bring in unreachable nodes in the future */
pj
1.20
701         bbinfo->size = dfsnum + 1;
pj
1.8
702 }
703
ragge
1.11
704 static bittype *
705 setalloc(int nelem)
706 {
707         bittype *b;
708         int sz = (nelem+NUMBITS-1)/NUMBITS;
709
710         b = tmpalloc(sz * sizeof(bittype));
711         memset(b0sz * sizeof(bittype));
712         return b;
713 }
714
pj
1.8
715 /*
716  * Algorithm 19.9, pp 414 from Appel.
717  */
718
719 void
720 dominators(struct bblockinfo *bbinfo)
721 {
722         struct cfgnode *cnode;
723         struct basicblock *bb, *y, *v;
724         struct basicblock *s, *sprime, *p;
pj
1.10
725         int hi;
pj
1.8
726
ragge
1.11
727         DLIST_FOREACH(bb, &bblocksbbelem) {
728                 bb->bucket = setalloc(bbinfo->size);
729                 bb->df = setalloc(bbinfo->size);
730                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
731         }
732
733         dfsnum = 0;
ragge
1.11
734         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
735
pj
1.20
736 #if 0
737         { struct basicblock *bbbstruct cfgnode *ccnode;
738         DLIST_FOREACH(bbb, &bblocksbbelem) {
739                 printf("Basic block %d, parents: "bbb->dfnum);
740                 SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
741                         printf("%d, "ccnode->bblock->dfnum);
742                 }
743                 printf("\nChildren: ");
744                 SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
745                         printf("%d, "ccnode->bblock->dfnum);
746                 }
747                 printf("\n");
748         }
749         }
750 #endif
751
pj
1.10
752         for(h = bbinfo->size - 1h > 1h--) {
753                 bb = bbinfo->arr[h];
pj
1.8
754                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
755                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
756                         if (cnode->bblock->dfnum <= bb->dfnum
757                                 sprime = cnode->bblock;
pj
1.10
758                         else 
759                                 sprime = bbinfo->arr[ancestorwithlowestsemi
760                                               (cnode->bblockbbinfo)->semi];
pj
1.8
761                         if (sprime->dfnum < s->dfnum)
762                                 s = sprime;
763                 }
764                 bb->semi = s->dfnum;
pj
1.10
765                 BITSET(s->bucketbb->dfnum);
pj
1.8
766                 link(pbb);
767                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
768                         if(TESTBIT(p->bucketi)) {
pj
1.8
769                                 v = bbinfo->arr[i];
pj
1.10
770                                 y = ancestorwithlowestsemi(vbbinfo);
771                                 if (y->semi == v->semi
pj
1.8
772                                         v->idom = p->dfnum;
773                                 else
774                                         v->samedom = y->dfnum;
775                         }
776                 }
777                 memset(p->bucket0, (bbinfo->size + 7)/8);
778         }
pj
1.10
779 #if 0
780         printf("Num\tSemi\tAncest\tidom\n");
pj
1.8
781         CIRCLEQ_FOREACH(bb, &bblocksbbelem) {
pj
1.10
782                 printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semibb->ancestorbb->idom);
783         }
784 #endif
785         for(h = 2h < bbinfo->sizeh++) {
786                 bb = bbinfo->arr[h];
787                 if (bb->samedom != 0) {
pj
1.8
788                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
789                 }
790         }
ragge
1.11
791         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
792                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
793 #if 0
794
795                         printf("Setting child %d of %d\n"bb->dfnumbbinfo->arr[bb->idom]->dfnum);
796 #endif
797
798                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
799                 }
pj
1.8
800         }
801 }
802
803
804 struct basicblock *
805 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
806 {
807         struct basicblock *u = bblock;
808         struct basicblock *v = bblock;
809
810         while (v->ancestor != 0) {
811                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
812                     bbinfo->arr[u->semi]->dfnum
pj
1.8
813                         u = v;
814                 v = bbinfo->arr[v->ancestor];
815         }
816         return u;
817 }
818
819 void
820 link(struct basicblock *parentstruct basicblock *child)
821 {
822         child->ancestor = parent->dfnum;
823 }
824
825 void
826 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
827 {
828         struct cfgnode *cnode;
pj
1.10
829         int hi;
pj
1.8
830         
ragge
1.11
831         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
832                 if (cnode->bblock->idom != bblock->dfnum)
833                         BITSET(bblock->dfcnode->bblock->dfnum);
834         }
pj
1.10
835         for (h = 1h < bbinfo->sizeh++) {
836                 if (!TESTBIT(bblock->dfchildrenh))
837                         continue;
838                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
839                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
840                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
841                             (bbinfo->arr[h] == bblock ||
842                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
843                             BITSET(bblock->dfi);
844                 }
845         }
846 }
pj
1.15
847
pj
1.21
848 static struct basicblock *currbb;
849 static struct interpass *currip;
850
851 /* Helper function for findTemps, Find assignment nodes. */
852 static void
853 findasg(NODE *p)
854 {
855         struct pvarinfo *pv;
856
857         if (p->n_op != ASSIGN)
858                 return;
859
860         if (p->n_left->n_op != TEMP)
861                 return;
862
863         pv = tmpcalloc(sizeof(struct pvarinfo));
864         pv->next = defsites.arr[p->n_left->n_lval];
865         pv->bb = currbb;
866         pv->top = currip->ip_node;
867         pv->n = p->n_left;
868
869         defsites.arr[p->n_left->n_lval] = pv;
870 }
871
872 /* Walk the interpass looking for assignment nodes. */
873 void findTemps(struct interpass *ip)
pj
1.20
874 {
875         if (ip->type != IP_NODE)
876                 return;
877
pj
1.21
878         currip = ip;
879
880         walkf(ip->ip_nodefindasg);
pj
1.20
881 }
882
883 /*
884  * Algorithm 19.6 from Appel.
885  */
886
887 void
888 placePhiFunctions(struct bblockinfo *bbinfo)
889 {
890         struct basicblock *bb;
891         struct interpass *ip;
892         int maxtmpijkl;
893         struct pvarinfo *n;
894         struct cfgnode *cnode;
895         TWORD ntype;
896         NODE *p;
897
898         bb = DLIST_NEXT(&bblocksbbelem);
899         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
900         bb = DLIST_PREV(&bblocksbbelem);
901         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
902         defsites.size = maxtmp - defsites.low + 1;
903         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
904
pj
1.21
905         /* Find all defsites */
pj
1.20
906         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
907                 currbb = bb;
pj
1.20
908                 ip = bb->first;
909
910                 while (ip != bb->last) {
pj
1.21
911                         findTemps(ip);
pj
1.20
912                         ip = DLIST_NEXT(ipqelem);
913                 }
914                 /* Make sure we get the last statement in the bblock */
pj
1.21
915                 findTemps(ip);
pj
1.20
916         }
pj
1.21
917         /* For each variable */
pj
1.20
918         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
919                 /* While W not empty */
pj
1.20
920                 while (defsites.arr[i] != NULL) {
pj
1.21
921                         /* Remove some node n from W */
pj
1.20
922                         n = defsites.arr[i];
923                         defsites.arr[i] = n->next;
pj
1.21
924                         /* For each y in n->bb->df */
pj
1.20
925                         for (j = 0j < bbinfo->sizej++) {
926                                 if(!TESTBIT(n->bb->dfj))
927                                         continue;
928                                 ntype = n->n->n_type;
929                                 k = 0;
pj
1.21
930                                 /* Amount of predecessors for y */
pj
1.20
931                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
932                                         k++;
pj
1.21
933                                 /* Construct phi(...) */
pj
1.20
934                                 p = mklnode(TEMPi0ntype);
935                                 for (l = 0l < k-1l++)
936                                         p = mkbinode(PHIp,
937                                             mklnode(TEMPi0ntype), ntype);
938                                 ip = ipnode(mkbinode(ASSIGN,
939                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
940                                 /* Insert phi at top of basic block */
pj
1.20
941                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
942                                 n->bb->first = ip;
943
944                         }
945                 }
946         }
947 }
948
pj
1.15
949 /*
950  * Remove unreachable nodes in the CFG.
951  */ 
952
953 void
954 remunreach(void)
955 {
956         struct basicblock *bb, *nbb;
957         struct interpass *next, *ctree;
958
959         bb = DLIST_NEXT(&bblocksbbelem);
960         while (bb != &bblocks) {
961                 nbb = DLIST_NEXT(bbbbelem);
962
963                 /* Code with dfnum 0 is unreachable */
964                 if (bb->dfnum != 0) {
965                         bb = nbb;
966                         continue;
967                 }
968
969                 /* Need the epilogue node for other parts of the
970                    compiler, set its label to 0 and backend will
971                    handle it. */ 
972                 if (bb->first->type == IP_EPILOG) {
973                         bb->first->ip_lbl = 0;
974                         bb = nbb;
975                         continue;
976                 }
977
978                 next = bb->first;
979                 do {
980                         ctree = next;
981                         next = DLIST_NEXT(ctreeqelem);
982                         
983                         if (ctree->type == IP_NODE)
pj
1.16
984                                 tfree(ctree->ip_node);
pj
1.15
985                         DLIST_REMOVE(ctreeqelem);
986                 } while (ctree != bb->last);
987                         
988                 DLIST_REMOVE(bbbbelem);
989                 bb = nbb;
990         }
991 }
ragge
1.19
992
993 void
994 printip(struct interpass *pole)
995 {
996         static char *foo[] = {
997            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
998         struct interpass *ip;
999
1000         DLIST_FOREACH(ippoleqelem) {
1001                 if (ip->type > MAXIP)
1002                         printf("IP(%d) (%p): "ip->typeip);
1003                 else
1004                         printf("%s (%p): "foo[ip->type], ip);
1005                 switch (ip->type) {
1006                 case IP_NODEprintf("\n");
1007                         fwalk(ip->ip_nodee2print0); break;
1008                 case IP_PROLOG:
1009                         printf("%s\n",
1010                             ((struct interpass_prolog *)ip)->ipp_name); break;
1011                 case IP_STKOFFprintf("%d\n"ip->ip_off); break;
1012                 case IP_EPILOGprintf("\n"); break;
1013                 case IP_DEFLABprintf(LABFMT "\n"ip->ip_lbl); break;
1014                 }
1015         }
1016 }
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-10-31 18:57 +0100