Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050618113013

Diff

Diff from 1.25 to:

Annotations

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

Annotated File View

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