Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050521145433

Diff

Diff from 1.24 to:

Annotations

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

Annotated File View

ragge
1.24
1 /*      $Id: optim2.c,v 1.24 2005/05/21 14:54:33 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);
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;
ragge
1.23
270         static int inftn;
ragge
1.5
271
pj
1.4
272 #if 0
ragge
1.2
273         int regs;
pj
1.4
274 #endif
275         struct labelinfo labinfo;
pj
1.8
276         struct bblockinfo bbinfo;
ragge
1.1
277
ragge
1.23
278         if (ip->type == IP_PROLOG) {
ragge
1.11
279                 DLIST_INIT(&ipoleqelem);
ragge
1.23
280                 inftn = 1;
281         } else if (inftn == 0)
282                 comperr("saveip");
ragge
1.11
283
284         DLIST_INSERT_BEFORE(&ipoleipqelem);
ragge
1.1
285
286         if (ip->type != IP_EPILOG)
287                 return;
ragge
1.23
288         inftn = 0;
ragge
1.5
289         epp = (struct interpass_prolog *)ip;
ragge
1.1
290         saving = -1;
291
pj
1.4
292         //              deljumps();     /* Delete redundant jumps and dead code */
293         if (xssaflag) {
ragge
1.11
294                 DLIST_INIT(&bblocksbbelem);
pj
1.8
295                 if (bblocks_build(&labinfo, &bbinfo)) {
pj
1.4
296                         cfg_build(&labinfo);
pj
1.8
297                         dominators(&bbinfo);
ragge
1.11
298                         computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
pj
1.15
299                         remunreach();
pj
1.4
300 #if 0
pj
1.6
301                         if (xssaflag) {
302                                 dfg = dfg_build(cfg);
303                                 ssa = ssa_build(cfgdfg);
304                         }
pj
1.4
305 #endif
306                 }
307  
308         }
309 #if 0
ragge
1.2
310         regs = asgregs();       /* Assign non-temporary registers */
pj
1.4
311 #endif
ragge
1.2
312
313 #ifdef PCC_DEBUG
ragge
1.5
314         if (epp->ipp_regs != MAXRVAR)
ragge
1.2
315                 comperr("register error");
316 #endif
ragge
1.1
317
ragge
1.11
318         ipp = (struct interpass_prolog *)DLIST_NEXT(&ipoleqelem);
ragge
1.5
319         ipp->ipp_autos = epp->ipp_autos;
320         ipp->ipp_regs = epp->ipp_regs// = regs;
ragge
1.1
321
322 #ifdef MYOPTIM
pj
1.6
323         myoptim((struct interpass *)ipp);
ragge
1.1
324 #endif
325
ragge
1.19
326 if (xnewreg == 0) {
327         int tmpautooff;
328         NODE *p;
329
330         p2autooff = p2maxautooff = AUTOINIT;
331         /* Must verify stack usage first */
332         DLIST_FOREACH(ip, &ipoleqelem) {
333                 if (ip->type == IP_STKOFF) {
334                         p2autooff = ip->ip_off;
335                         if (p2autooff > p2maxautooff)
336                                 p2maxautooff = p2autooff;
337                 }
338         }
339         p2autooff = p2maxautooff/* don't have live range analysis yet */
340
341         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.23
342                 if (ip->type == IPSTK) {
ragge
1.19
343                         struct interpass *ip3;
344                         p2autooff = ip->ip_off;
345                         ip3 = ip;
346                         ip = DLIST_NEXT(ipqelem);
347                         DLIST_REMOVE(ip3qelem);
348                 }
349                         
350                 if (ip->type != IP_NODE)
351                         continue;
352
353                 p = ip->ip_node;
354                 walkf(pdeltemp);
355
356                 tmpautooff = p2autooff;
357
358                 geninsn(pFOREFF);
359                 if (sucomp(p) < 0) {
360                         /* Create STKOFF entry */
361                         struct interpass *ip3;
362                         DLIST_INSERT_BEFORE(ipstoresaveqelem);
363                         ip3 = ipnode(NULL);
ragge
1.23
364                         ip3->type = IPSTK;
ragge
1.19
365                         ip3->ip_off = tmpautooff;
366                         DLIST_INSERT_AFTER(ipip3qelem);
367                         ip = DLIST_PREV(storesaveqelem);
368                         continue;
369                 }
370
371                 p2autooff = tmpautooff;
372
373                 genregs(p);
374                 mygenregs(p);
375         }
376
377 else {
378         /*
379          * Loop over instruction assignment until the register assignment
380          * code is satisfied.
381          */
382         do {
383                 /* do instruction assignment */
384                 DLIST_FOREACH(ip, &ipoleqelem) {
385                         if (ip->type != IP_NODE)
386                                 continue;
387                         geninsn(ip->ip_nodeFOREFF);
388                         nsucomp(ip->ip_node);
389                 }
390         } while (ngenregs(DLIST_NEXT(&ipoleqelem),
391             DLIST_PREV(&ipoleqelem)));
392 }
393
394         DLIST_FOREACH(ip, &ipoleqelem) {
395                 emit(ip);
396         }
ragge
1.11
397         DLIST_INIT(&ipoleqelem);
ragge
1.1
398 }
399
400 void
401 deljumps()
402 {
403         struct interpass *ip, *n;
404         int gotone;
405
406 again:  gotone = 0;
407
ragge
1.11
408         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.1
409                 if (ip->type == IP_EPILOG)
410                         return;
411                 if (ip->type != IP_NODE)
412                         continue;
ragge
1.11
413                 n = DLIST_NEXT(ipqelem);
ragge
1.1
414                 /* Check for nodes without side effects */
415                 if (ip->ip_node->n_op != GOTO)
416                         continue;
417                 switch (n->type) {
418                 case IP_NODE:
419                         tfree(n->ip_node);
ragge
1.11
420                         DLIST_REMOVE(nqelem);
ragge
1.1
421                         break;
422                 case IP_DEFLAB:
423                         if (ip->ip_node->n_left->n_lval != n->ip_lbl)
424                                 continue;
425                         tfree(ip->ip_node);
426                         *ip = *n;
427                         break;
428                 default:
429                         continue;
430                 }
431                 gotone = 1;
432         }
433         if (gotone)
434                 goto again;
435 }
436
437 void
438 optdump(struct interpass *ip)
439 {
440         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
441                 "deflab""defnam""asm" };
442         printf("type %s\n"nm[ip->type-1]);
443         switch (ip->type) {
444         case IP_NODE:
445                 fwalk(ip->ip_nodee2print0);
446                 break;
447         case IP_DEFLAB:
448                 printf("label " LABFMT "\n"ip->ip_lbl);
449                 break;
450         case IP_ASM:
451                 printf(": %s\n"ip->ip_asm);
452                 break;
453         }
454 }
pj
1.4
455
456 /*
457  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
458  *
459  * Also fills the labelinfo struct with information about which bblocks
460  * that contain which label.
461  */
462
463 int
pj
1.8
464 bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo)
pj
1.4
465 {
466         struct interpass *ip;
467         struct basicblock *bb = NULL;
468         int leader = 1;
pj
1.12
469         unsigned int lowhigh = 0;
pj
1.8
470         int count = 0;
pj
1.4
471         int i;
472
pj
1.12
473         low = sizeof(int) == 2 ? 65000 : 1000000000;
pj
1.4
474         /* 
475          * First statement is a leader.
476          * Any statement that is target of a jump is a leader.
477          * Any statement that immediately follows a jump is a leader.
478          */
479
ragge
1.11
480         DLIST_FOREACH(ip, &ipoleqelem) {
pj
1.4
481                 /* Garbage, skip it */
482                 if (leader) {
483                         bb = tmpalloc(sizeof(struct basicblock));
484                         bb->first = bb->last = ip;
ragge
1.11
485                         SLIST_INIT(&bb->children);
486                         SLIST_INIT(&bb->parents);
pj
1.8
487                         bb->dfnum = 0;
488                         bb->dfparent = 0;
489                         bb->semi = 0;
490                         bb->ancestor = 0;
491                         bb->idom = 0;
492                         bb->samedom = 0;
493                         bb->bucket = NULL;
494                         bb->df = NULL;
pj
1.10
495                         bb->dfchildren = NULL;
pj
1.22
496                         bb->Aorig = NULL;
497                         bb->Aphi = NULL;
ragge
1.11
498                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.4
499                         leader = 0;
pj
1.8
500                         count++;
pj
1.4
501                 } 
502                 
503                 /* Prologue and epilogue in their own bblock */
504                 if ((ip->type == IP_PROLOG) || (ip->type == IP_EPILOG)) {
505                         bb->last = ip;
506                         if (ip->type == IP_EPILOG)
ragge
1.5
507                                 high = MAX(ip->ip_lblhigh);
pj
1.4
508                         leader = 1;
509                         continue;
510                 }
511                 
512                 /* Keep track of highest and lowest label number */
513                 if (ip->type == IP_DEFLAB) {
514                         low = MIN(ip->ip_lbllow);
515                         high = MAX(ip->ip_lblhigh);
516                 }
517
518                 /* Make sure each label is in a unique bblock */
519                 if (((ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) && 
520                     bb->first != ip) {
521                         bb = tmpalloc(sizeof(struct basicblock));
522                         bb->first = bb->last = ip;
ragge
1.11
523                         SLIST_INIT(&bb->children);
524                         SLIST_INIT(&bb->parents);
pj
1.8
525                         bb->dfnum = 0;
526                         bb->dfparent = 0;
527                         bb->semi = 0;
528                         bb->ancestor = 0;
529                         bb->idom = 0;
530                         bb->samedom = 0;
531                         bb->bucket = NULL;
532                         bb->df = NULL;
pj
1.10
533                         bb->dfchildren = NULL;
pj
1.22
534                         bb->Aorig = NULL;
535                         bb->Aphi = NULL;
ragge
1.11
536                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
pj
1.8
537                         count++;
pj
1.4
538                         continue;
539                 }
540
541                 if (ip->type == IP_ASM)
542                         return 0;
543
544                 if (ip->type == IP_NODE) {
545                         switch(ip->ip_node->n_op) {
546                         case CBRANCH:
547                         case GOTO:
548                         case RETURN:
549                                 /* Jumps, last in bblock. */
550                                 leader = 1;
551                                 break;
552
553                         case NAME:
554                         case ICON:
555                         case FCON:
556                         case REG:
557                         case OREG:
558                         case MOVE:
559                         case PLUS:
560                         case MINUS:
561                         case DIV:
562                         case MOD:
563                         case MUL:
564                         case AND:
565                         case OR:
566                         case ER:
567                         case LS:
568                         case COMPL:
569                         case INCR:
570                         case DECR:
571                         case UMUL:
572                         case UMINUS:
573                         case EQ:
574                         case NE:
575                         case LE:
576                         case GE:
577                         case GT:
578                         case ULE:
579                         case ULT:
580                         case UGE:
581                         case UGT:
582                         case ASSIGN:
583                         case FORCE:
584                         case FUNARG:
pj
1.7
585                         case CALL:
586                         case UCALL:
587                         case FORTCALL:
588                         case UFORTCALL:
589                         case STCALL:
590                         case USTCALL:
pj
1.4
591                                 /* Not jumps, continue with bblock. */
592                                 break;
593
594                         default:
595                                 comperr("optim2:bblocks_build() %d",ip->ip_node->n_op ); 
596                                 break;
597                         }
598                 }
599
600                 bb->last = ip;
601         }
pj
1.20
602 #if 0
pj
1.8
603         printf("Basic blocks in func: %d\n"count);
pj
1.10
604         printf("Label range in func: %d\n"high - low + 1);
pj
1.4
605 #endif
606
607         labinfo->low = low;
608         labinfo->size = high - low + 1;
609         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
pj
1.8
610         for (i = 0i <= labinfo->sizei++) {
pj
1.4
611                 labinfo->arr[i] = NULL;
612         }
pj
1.8
613         
614         bbinfo->size = count + 1;
615         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
616         for (i = 0i <= bbinfo->sizei++) {
617                 bbinfo->arr[i] = NULL;
618         }
pj
1.4
619
ragge
1.11
620         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.20
621                 /* Build the label table */
pj
1.4
622                 if (bb->first->type == IP_DEFLAB) {
623                         labinfo->arr[bb->first->ip_lbl - low] = bb;
624                 }
625                 if (bb->first->type == IP_EPILOG) {
ragge
1.5
626                         labinfo->arr[bb->first->ip_lbl - low] = bb;
pj
1.4
627                 }
628         }
629
630         return 1;
631 }
632
633 /*
634  * Build the control flow graph.
635  */
636
637 void
638 cfg_build(struct labelinfo *labinfo)
639 {
640         /* Child and parent nodes */
641         struct cfgnode *cnode
642         struct cfgnode *pnode;
643         struct basicblock *bb;
644         
ragge
1.11
645         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
646
647                 if (bb->first->type == IP_EPILOG) {
648                         break;
649                 }
650
651                 cnode = tmpalloc(sizeof(struct cfgnode));
652                 pnode = tmpalloc(sizeof(struct cfgnode));
653                 pnode->bblock = bb;
654
655                 if ((bb->last->type == IP_NODE) && 
656                     (bb->last->ip_node->n_op == GOTO)) {
657                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
658                             labinfo->size) {
659                                 comperr("Label out of range: %d, base %d"
660                                         bb->last->ip_node->n_left->n_lval
661                                         labinfo->low);
662                         }
663                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
664                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
665                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
666                         continue;
667                 }
668                 if ((bb->last->type == IP_NODE) && 
669                     (bb->last->ip_node->n_op == CBRANCH)) {
670                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
671                             labinfo->size
672                                 comperr("Label out of range: %d"
673                                         bb->last->ip_node->n_left->n_lval);
674                         
675                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
676                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
677                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
678                         cnode = tmpalloc(sizeof(struct cfgnode));
679                         pnode = tmpalloc(sizeof(struct cfgnode));
680                         pnode->bblock = bb;
681                 }
682
ragge
1.11
683                 cnode->bblock = DLIST_NEXT(bbbbelem);
684                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
685                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
686         }
687 }
pj
1.6
688
pj
1.8
689 void
690 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
691 {
692         struct cfgnode *cnode;
693         
694         if (bb->dfnum != 0)
695                 return;
696
697         bb->dfnum = ++dfsnum;
698         bb->dfparent = parent;
699         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
700         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
701                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
702         }
pj
1.13
703         /* Don't bring in unreachable nodes in the future */
pj
1.20
704         bbinfo->size = dfsnum + 1;
pj
1.8
705 }
706
ragge
1.11
707 static bittype *
708 setalloc(int nelem)
709 {
710         bittype *b;
711         int sz = (nelem+NUMBITS-1)/NUMBITS;
712
713         b = tmpalloc(sz * sizeof(bittype));
714         memset(b0sz * sizeof(bittype));
715         return b;
716 }
717
pj
1.8
718 /*
719  * Algorithm 19.9, pp 414 from Appel.
720  */
721
722 void
723 dominators(struct bblockinfo *bbinfo)
724 {
725         struct cfgnode *cnode;
726         struct basicblock *bb, *y, *v;
727         struct basicblock *s, *sprime, *p;
pj
1.10
728         int hi;
pj
1.8
729
ragge
1.11
730         DLIST_FOREACH(bb, &bblocksbbelem) {
731                 bb->bucket = setalloc(bbinfo->size);
732                 bb->df = setalloc(bbinfo->size);
733                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
734         }
735
736         dfsnum = 0;
ragge
1.11
737         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
738
pj
1.20
739 #if 0
740         { struct basicblock *bbbstruct cfgnode *ccnode;
741         DLIST_FOREACH(bbb, &bblocksbbelem) {
742                 printf("Basic block %d, parents: "bbb->dfnum);
743                 SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
744                         printf("%d, "ccnode->bblock->dfnum);
745                 }
746                 printf("\nChildren: ");
747                 SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
748                         printf("%d, "ccnode->bblock->dfnum);
749                 }
750                 printf("\n");
751         }
752         }
753 #endif
754
pj
1.10
755         for(h = bbinfo->size - 1h > 1h--) {
756                 bb = bbinfo->arr[h];
pj
1.8
757                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
758                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
759                         if (cnode->bblock->dfnum <= bb->dfnum
760                                 sprime = cnode->bblock;
pj
1.10
761                         else 
762                                 sprime = bbinfo->arr[ancestorwithlowestsemi
763                                               (cnode->bblockbbinfo)->semi];
pj
1.8
764                         if (sprime->dfnum < s->dfnum)
765                                 s = sprime;
766                 }
767                 bb->semi = s->dfnum;
pj
1.10
768                 BITSET(s->bucketbb->dfnum);
pj
1.8
769                 link(pbb);
770                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
771                         if(TESTBIT(p->bucketi)) {
pj
1.8
772                                 v = bbinfo->arr[i];
pj
1.10
773                                 y = ancestorwithlowestsemi(vbbinfo);
774                                 if (y->semi == v->semi
pj
1.8
775                                         v->idom = p->dfnum;
776                                 else
777                                         v->samedom = y->dfnum;
778                         }
779                 }
780                 memset(p->bucket0, (bbinfo->size + 7)/8);
781         }
pj
1.10
782 #if 0
783         printf("Num\tSemi\tAncest\tidom\n");
pj
1.8
784         CIRCLEQ_FOREACH(bb, &bblocksbbelem) {
pj
1.10
785                 printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semibb->ancestorbb->idom);
786         }
787 #endif
788         for(h = 2h < bbinfo->sizeh++) {
789                 bb = bbinfo->arr[h];
790                 if (bb->samedom != 0) {
pj
1.8
791                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
792                 }
793         }
ragge
1.11
794         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
795                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
796 #if 0
797
798                         printf("Setting child %d of %d\n"bb->dfnumbbinfo->arr[bb->idom]->dfnum);
799 #endif
800
801                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
802                 }
pj
1.8
803         }
804 }
805
806
807 struct basicblock *
808 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
809 {
810         struct basicblock *u = bblock;
811         struct basicblock *v = bblock;
812
813         while (v->ancestor != 0) {
814                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
815                     bbinfo->arr[u->semi]->dfnum
pj
1.8
816                         u = v;
817                 v = bbinfo->arr[v->ancestor];
818         }
819         return u;
820 }
821
822 void
823 link(struct basicblock *parentstruct basicblock *child)
824 {
825         child->ancestor = parent->dfnum;
826 }
827
828 void
829 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
830 {
831         struct cfgnode *cnode;
pj
1.10
832         int hi;
pj
1.8
833         
ragge
1.11
834         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
835                 if (cnode->bblock->idom != bblock->dfnum)
836                         BITSET(bblock->dfcnode->bblock->dfnum);
837         }
pj
1.10
838         for (h = 1h < bbinfo->sizeh++) {
839                 if (!TESTBIT(bblock->dfchildrenh))
840                         continue;
841                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
842                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
843                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
844                             (bbinfo->arr[h] == bblock ||
845                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
846                             BITSET(bblock->dfi);
847                 }
848         }
849 }
pj
1.15
850
pj
1.21
851 static struct basicblock *currbb;
852 static struct interpass *currip;
853
854 /* Helper function for findTemps, Find assignment nodes. */
855 static void
856 findasg(NODE *p)
857 {
858         struct pvarinfo *pv;
859
860         if (p->n_op != ASSIGN)
861                 return;
862
863         if (p->n_left->n_op != TEMP)
864                 return;
865
866         pv = tmpcalloc(sizeof(struct pvarinfo));
867         pv->next = defsites.arr[p->n_left->n_lval];
868         pv->bb = currbb;
869         pv->top = currip->ip_node;
870         pv->n = p->n_left;
pj
1.22
871         BITSET(currbb->Aorigp->n_left->n_lval);
pj
1.21
872
873         defsites.arr[p->n_left->n_lval] = pv;
874 }
875
876 /* Walk the interpass looking for assignment nodes. */
877 void findTemps(struct interpass *ip)
pj
1.20
878 {
879         if (ip->type != IP_NODE)
880                 return;
881
pj
1.21
882         currip = ip;
883
884         walkf(ip->ip_nodefindasg);
pj
1.20
885 }
886
887 /*
888  * Algorithm 19.6 from Appel.
889  */
890
891 void
892 placePhiFunctions(struct bblockinfo *bbinfo)
893 {
894         struct basicblock *bb;
895         struct interpass *ip;
896         int maxtmpijkl;
897         struct pvarinfo *n;
898         struct cfgnode *cnode;
899         TWORD ntype;
900         NODE *p;
pj
1.22
901         struct pvarinfo *pv;
pj
1.20
902
903         bb = DLIST_NEXT(&bblocksbbelem);
904         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
905         bb = DLIST_PREV(&bblocksbbelem);
906         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
907         defsites.size = maxtmp - defsites.low + 1;
908         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
909
pj
1.21
910         /* Find all defsites */
pj
1.20
911         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
912                 currbb = bb;
pj
1.20
913                 ip = bb->first;
pj
1.22
914                 bb->Aorig = setalloc(defsites.size);
915                 bb->Aphi = setalloc(defsites.size);
916                 
pj
1.20
917
918                 while (ip != bb->last) {
pj
1.21
919                         findTemps(ip);
pj
1.20
920                         ip = DLIST_NEXT(ipqelem);
921                 }
922                 /* Make sure we get the last statement in the bblock */
pj
1.21
923                 findTemps(ip);
pj
1.20
924         }
pj
1.21
925         /* For each variable */
pj
1.20
926         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
927                 /* While W not empty */
pj
1.20
928                 while (defsites.arr[i] != NULL) {
pj
1.21
929                         /* Remove some node n from W */
pj
1.20
930                         n = defsites.arr[i];
931                         defsites.arr[i] = n->next;
pj
1.21
932                         /* For each y in n->bb->df */
pj
1.20
933                         for (j = 0j < bbinfo->sizej++) {
pj
1.22
934                                 if (!TESTBIT(n->bb->dfj))
pj
1.20
935                                         continue;
pj
1.22
936                                 
937                                 if (TESTBIT(bbinfo->arr[j]->Aphii))
938                                         continue;
939
pj
1.20
940                                 ntype = n->n->n_type;
941                                 k = 0;
pj
1.21
942                                 /* Amount of predecessors for y */
pj
1.20
943                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
944                                         k++;
pj
1.21
945                                 /* Construct phi(...) */
pj
1.20
946                                 p = mklnode(TEMPi0ntype);
947                                 for (l = 0l < k-1l++)
948                                         p = mkbinode(PHIp,
949                                             mklnode(TEMPi0ntype), ntype);
950                                 ip = ipnode(mkbinode(ASSIGN,
951                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
952                                 /* Insert phi at top of basic block */
pj
1.20
953                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
954                                 n->bb->first = ip;
pj
1.22
955                                 BITSET(bbinfo->arr[j]->Aphii);
956                                 if (!TESTBIT(bbinfo->arr[j]->Aorigi)) {
957                                         pv = tmpalloc(sizeof(struct pvarinfo));
958                                         // XXXpj Ej fullständig information.
959                                         pv->bb = bbinfo->arr[j];
960                                         pv->next = defsites.arr[i]->next;
961                                         defsites.arr[i] = pv;
962                                 }
963                                         
pj
1.20
964
965                         }
966                 }
967         }
968 }
969
pj
1.15
970 /*
971  * Remove unreachable nodes in the CFG.
972  */ 
973
974 void
975 remunreach(void)
976 {
977         struct basicblock *bb, *nbb;
978         struct interpass *next, *ctree;
979
980         bb = DLIST_NEXT(&bblocksbbelem);
981         while (bb != &bblocks) {
982                 nbb = DLIST_NEXT(bbbbelem);
983
984                 /* Code with dfnum 0 is unreachable */
985                 if (bb->dfnum != 0) {
986                         bb = nbb;
987                         continue;
988                 }
989
990                 /* Need the epilogue node for other parts of the
991                    compiler, set its label to 0 and backend will
992                    handle it. */ 
993                 if (bb->first->type == IP_EPILOG) {
994                         bb->first->ip_lbl = 0;
995                         bb = nbb;
996                         continue;
997                 }
998
999                 next = bb->first;
1000                 do {
1001                         ctree = next;
1002                         next = DLIST_NEXT(ctreeqelem);
1003                         
1004                         if (ctree->type == IP_NODE)
pj
1.16
1005                                 tfree(ctree->ip_node);
pj
1.15
1006                         DLIST_REMOVE(ctreeqelem);
1007                 } while (ctree != bb->last);
1008                         
1009                 DLIST_REMOVE(bbbbelem);
1010                 bb = nbb;
1011         }
1012 }
ragge
1.19
1013
1014 void
1015 printip(struct interpass *pole)
1016 {
1017         static char *foo[] = {
1018            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
1019         struct interpass *ip;
1020
1021         DLIST_FOREACH(ippoleqelem) {
1022                 if (ip->type > MAXIP)
1023                         printf("IP(%d) (%p): "ip->typeip);
1024                 else
1025                         printf("%s (%p): "foo[ip->type], ip);
1026                 switch (ip->type) {
1027                 case IP_NODEprintf("\n");
1028                         fwalk(ip->ip_nodee2print0); break;
1029                 case IP_PROLOG:
1030                         printf("%s\n",
1031                             ((struct interpass_prolog *)ip)->ipp_name); break;
1032                 case IP_STKOFFprintf("%d\n"ip->ip_off); break;
1033                 case IP_EPILOGprintf("\n"); break;
1034                 case IP_DEFLABprintf(LABFMT "\n"ip->ip_lbl); break;
1035                 }