Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20070930193207

Diff

Diff from 1.45 to:

Annotations

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

Annotated File View

ragge
1.45
1 /*      $Id: optim2.c,v 1.45 2007/09/30 19:32:07 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.30
42 #define BDEBUG(x)       if (b2debug) printf x
43
pj
1.8
44 static int dfsnum;
ragge
1.1
45
46 void saveip(struct interpass *ip);
ragge
1.38
47 void deljumps(struct interpass *);
ragge
1.1
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.19
52 struct interpass *storesave;
ragge
1.30
53 static struct interpass_prolog *ipp, *epp/* prolog/epilog */
ragge
1.1
54
ragge
1.38
55 void bblocks_build(struct interpass *, struct labelinfo *, struct bblockinfo *);
pj
1.4
56 void cfg_build(struct labelinfo *labinfo);
pj
1.8
57 void cfg_dfs(struct basicblock *bbunsigned int parent
58              struct bblockinfo *bbinfo);
59 void dominators(struct bblockinfo *bbinfo);
pj
1.9
60 struct basicblock *
61 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo);
62 void link(struct basicblock *parentstruct basicblock *child);
63 void computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo);
pj
1.21
64 void findTemps(struct interpass *ip);
pj
1.20
65 void placePhiFunctions(struct bblockinfo *bbinfo);
pj
1.15
66 void remunreach(void);
pj
1.4
67
ragge
1.34
68 struct basicblock bblocks;
69 int nbblocks;
ragge
1.44
70 static struct interpass *cvpole;
pj
1.4
71
ragge
1.38
72 struct addrof {
73         struct addrof *next;
74         int tempnum;
75         int oregoff;
76 } *otlink;
77
78 static int
79 getoff(int num)
80 {
81         struct addrof *w;
82
83         for (w = otlinkww = w->next)
84                 if (w->tempnum == num)
85                         return w->oregoff;
86         return 0;
87 }
88
89 /*
ragge
1.44
90  * Use stack argument addresses instead of copying if & is used on a var.
91  */
92 static int
93 setargs(int tvalstruct addrof *w)
94 {
95         struct interpass *ip;
96         NODE *p;
97
98         ip = DLIST_NEXT(cvpoleqelem); /* PROLOG */
99         ip = DLIST_NEXT(ipqelem); /* first DEFLAB */
100         ip = DLIST_NEXT(ipqelem); /* first NODE */
101         for (; ip->type != IP_DEFLABip = DLIST_NEXT(ipqelem)) {
102                 p = ip->ip_node;
103 #ifdef PCC_DEBUG
104                 if (p->n_op != ASSIGN || p->n_left->n_op != TEMP)
105                         comperr("temparg");
106 #endif
107                 if (p->n_right->n_op != OREG)
108                         continue/* arg in register */
109                 if (tval != p->n_left->n_lval)
110                         continue/* wrong assign */
111                 w->oregoff = p->n_right->n_lval;
112                 tfree(p);
113                 DLIST_REMOVE(ipqelem);
114                 return 1;
115         }
116         return 0;
117 }
118
119 /*
ragge
1.38
120  * Search for ADDROF elements and, if found, record them.
121  */
122 static void
123 findaddrof(NODE *p)
124 {
125         struct addrof *w;
126
127         if (p->n_op != ADDROF)
128                 return;
129         if (getoff(p->n_left->n_lval))
130                 return;
131         w = tmpalloc(sizeof(struct addrof));
132         w->tempnum = p->n_left->n_lval;
ragge
1.44
133         if (setargs(p->n_left->n_lvalw) == 0)
134                 w->oregoff = BITOOR(freetemp(szty(p->n_left->n_type)));
ragge
1.38
135         w->next = otlink;
136         otlink = w;
137 }
138
ragge
1.44
139
ragge
1.38
140 /*
141  * Convert address-taken temps to OREGs.
142  */
143 static void
144 cvtaddrof(NODE *p)
145 {
146         NODE *l;
147         int n;
148
149         if (p->n_op != ADDROF && p->n_op != TEMP)
150                 return;
151         if (p->n_op == TEMP) {
152                 n = getoff(p->n_lval);
153                 if (n == 0)
154                         return;
155                 p->n_op = OREG;
156                 p->n_lval = n;
157                 p->n_rval = FPREG;
158         } else {
159                 l = p->n_left;
ragge
1.41
160                 l->n_type = p->n_type;
ragge
1.38
161                 p->n_right = mklnode(ICONl->n_lval0l->n_type);
162                 p->n_op = PLUS;
163                 l->n_op = REG;
164                 l->n_lval = 0;
165                 l->n_rval = FPREG;
166                 
167         }
168 }
169
170 void
171 optimize(struct interpass *ipole)
172 {
173         struct interpass *ip;
174         struct labelinfo labinfo;
175         struct bblockinfo bbinfo;
176
177         ipp = (struct interpass_prolog *)DLIST_NEXT(ipoleqelem);
178         epp = (struct interpass_prolog *)DLIST_PREV(ipoleqelem);
179
180         if (b2debug) {
181                 printf("initial links\n");
182                 printip(ipole);
183         }
184
185         /*
186          * Convert ADDROF TEMP to OREGs.
187          */
188         if (xtemps) {
189                 otlink = NULL;
ragge
1.44
190                 cvpole = ipole;
ragge
1.38
191                 DLIST_FOREACH(ipipoleqelem) {
192                         if (ip->type != IP_NODE)
193                                 continue;
194                         walkf(ip->ip_nodefindaddrof);
195                 }
196                 if (otlink) {
197                         DLIST_FOREACH(ipipoleqelem) {
198                                 if (ip->type != IP_NODE)
199                                         continue;
200                                 walkf(ip->ip_nodecvtaddrof);
201                         }
202                 }
203         }
204                 
205         if (xdeljumps)
206                 deljumps(ipole); /* Delete redundant jumps and dead code */
207
208 #ifdef PCC_DEBUG
209         if (b2debug) {
210                 printf("links after deljumps\n");
211                 printip(ipole);
212         }
213 #endif
214         if (xssaflag || xtemps) {
215                 DLIST_INIT(&bblocksbbelem);
216                 bblocks_build(ipole, &labinfo, &bbinfo);
217                 BDEBUG(("Calling cfg_build\n"));
218                 cfg_build(&labinfo);
219         }
220         if (xssaflag) {
221                 BDEBUG(("Calling dominators\n"));
222                 dominators(&bbinfo);
223                 BDEBUG(("Calling computeDF\n"));
224                 computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
225                 BDEBUG(("Calling remunreach\n"));
226                 remunreach();
227 #if 0
228                 dfg = dfg_build(cfg);
229                 ssa = ssa_build(cfgdfg);
230 #endif
231         }
232
233 #ifdef PCC_DEBUG
234         if (epp->ipp_regs != 0)
235                 comperr("register error");
236 #endif
237
238 #ifdef MYOPTIM
239         myoptim((struct interpass *)ipp);
240 #endif
241 }
ragge
1.1
242
ragge
1.26
243 /*
244  * Delete unused labels, excess of labels, gotos to gotos.
245  * This routine can be made much more efficient.
246  */
ragge
1.1
247 void
ragge
1.38
248 deljumps(struct interpass *ipole)
ragge
1.1
249 {
ragge
1.45
250         struct interpass *ip, *n, *ip2, *start;
ragge
1.26
251         int gotone,lowhigh;
ragge
1.45
252         int *lblary, *jmparyszoijlab1lab2;
253         int del;
ragge
1.26
254
255         low = ipp->ip_lblnum;
256         high = epp->ip_lblnum;
257
258 #ifdef notyet
259         mark = tmpmark(); /* temporary used memory */
260 #endif
ragge
1.28
261
ragge
1.26
262         sz = (high-low) * sizeof(int);
263         lblary = tmpalloc(sz);
ragge
1.45
264         jmpary = tmpalloc(sz);
265
266         /*
267          * XXX: Find the first two labels. They may not be deleted,
268          * because the register allocator expects them to be there.
269          * These will not be coalesced with any other label.
270          */
271         lab1 = lab2 = -1;
272         start = NULL;
273         DLIST_FOREACH(ipipoleqelem) {
274                 if (ip->type != IP_DEFLAB)
275                         continue;
276                 if (lab1 < 0)
277                         lab1 = ip->ip_lbl;
278                 else if (lab2 < 0) {
279                         lab2 = ip->ip_lbl;
280                         start = ip;
281                 } else  /* lab1 >= 0 && lab2 >= 0, we're done. */
282                         break;
283         }
284         if (lab1 < 0 || lab2 < 0)
285                 comperr("deljumps");
ragge
1.26
286
287 again:  gotone = 0;
ragge
1.28
288         memset(lblary0sz);
ragge
1.45
289         lblary[lab1 - low] = lblary[lab2 - low] = 1;
290         memset(jmpary0sz);
ragge
1.26
291
ragge
1.33
292         /* refcount and coalesce all labels */
ragge
1.38
293         DLIST_FOREACH(ipipoleqelem) {
ragge
1.45
294                 if (ip->type == IP_DEFLAB && ip->ip_lbl != lab1 &&
295                     ip->ip_lbl != lab2) {
ragge
1.27
296                         n = DLIST_NEXT(ipqelem);
ragge
1.45
297
298                         /*
299                          * Find unconditional jumps directly following a
300                          * label.
301                          */
302                         if (n->type == IP_NODE && n->ip_node->n_op == GOTO) {
303                                 i = n->ip_node->n_left->n_lval;
304                                 jmpary[ip->ip_lbl - low] = i;
305                         }
306
ragge
1.38
307                         while (n->type == IP_DEFLAB) {
ragge
1.45
308                                 if (n->ip_lbl != lab1 && n->ip_lbl != lab2 &&
309                                     lblary[n->ip_lbl-low] >= 0) {
310                                         /*
311                                          * If the label is used, mark the
312                                          * label to be coalesced with as
313                                          * used, too.
314                                          */
315                                         if (lblary[n->ip_lbl - low] > 0 &&
316                                             lblary[ip->ip_lbl - low] == 0)
317                                                 lblary[ip->ip_lbl - low] = 1;
318                                         lblary[n->ip_lbl - low] = -ip->ip_lbl;
319                                 }
ragge
1.27
320                                 n = DLIST_NEXT(nqelem);
321                         }
322                 }
ragge
1.28
323                 if (ip->type != IP_NODE)
324                         continue;
325                 o = ip->ip_node->n_op;
326                 if (o == GOTO)
327                         i = ip->ip_node->n_left->n_lval;
328                 else if (o == CBRANCH)
329                         i = ip->ip_node->n_right->n_lval;
330                 else
331                         continue;
ragge
1.45
332
333                 /*
334                  * Mark destination label i as used, if it is not already.
335                  * If i is to be merged with label j, mark j as used, too.
336                  */
337                 if (lblary[i - low] == 0)
338                         lblary[i-low] = 1;
339                 else if ((j = lblary[i - low]) < 0 && lblary[-j - low] == 0)
340                         lblary[-j - low] = 1;
ragge
1.27
341         }
342
ragge
1.28
343         /* delete coalesced/unused labels and rename gotos */
ragge
1.38
344         DLIST_FOREACH(ipipoleqelem) {
ragge
1.27
345                 n = DLIST_NEXT(ipqelem);
ragge
1.28
346                 if (n->type == IP_DEFLAB) {
347                         if (lblary[n->ip_lbl-low] <= 0) {
348                                 DLIST_REMOVE(nqelem);
349                                 gotone = 1;
ragge
1.26
350                         }
351                 }
ragge
1.45
352                 if (ip->type != IP_NODE)
ragge
1.26
353                         continue;
ragge
1.45
354                 o = ip->ip_node->n_op;
ragge
1.26
355                 if (o == GOTO)
ragge
1.45
356                         i = ip->ip_node->n_left->n_lval;
ragge
1.26
357                 else if (o == CBRANCH)
ragge
1.45
358                         i = ip->ip_node->n_right->n_lval;
ragge
1.26
359                 else
360                         continue;
ragge
1.45
361
362                 /* Simplify (un-)conditional jumps to unconditional jumps. */
363                 if (jmpary[i - low] > 0) {
364                         gotone = 1;
365                         i = jmpary[i - low];
366                         if (o == GOTO)
367                                 ip->ip_node->n_left->n_lval = i;
368                         else
369                                 ip->ip_node->n_right->n_lval = i;
370                 }
371
372                 /* Fixup for coalesced labels. */
ragge
1.26
373                 if (lblary[i-low] < 0) {
374                         if (o == GOTO)
ragge
1.45
375                                 ip->ip_node->n_left->n_lval = -lblary[i-low];
ragge
1.26
376                         else
ragge
1.45
377                                 ip->ip_node->n_right->n_lval = -lblary[i-low];
ragge
1.26
378                 }
379         }
ragge
1.27
380
ragge
1.28
381         /* Delete gotos to the next statement */
ragge
1.38
382         DLIST_FOREACH(ipipoleqelem) {
ragge
1.28
383                 n = DLIST_NEXT(ipqelem);
384                 if (n->type != IP_NODE)
ragge
1.26
385                         continue;
ragge
1.28
386                 o = n->ip_node->n_op;
ragge
1.26
387                 if (o == GOTO)
ragge
1.28
388                         i = n->ip_node->n_left->n_lval;
ragge
1.26
389                 else if (o == CBRANCH)
ragge
1.28
390                         i = n->ip_node->n_right->n_lval;
ragge
1.26
391                 else
392                         continue;
ragge
1.33
393
394                 ip2 = n;
ragge
1.38
395                 ip2 = DLIST_NEXT(ip2qelem);
ragge
1.33
396
ragge
1.28
397                 if (ip2->type != IP_DEFLAB)
ragge
1.1
398                         continue;
ragge
1.45
399                 if (ip2->ip_lbl == i && i != lab1 && i != lab2) {
ragge
1.1
400                         tfree(n->ip_node);
ragge
1.11
401                         DLIST_REMOVE(nqelem);
ragge
1.28
402                         gotone = 1;
ragge
1.1
403                 }
404         }
ragge
1.28
405
ragge
1.45
406         /*
407          * Transform cbranch cond, 1; goto 2; 1: ... into
408          * cbranch !cond, 2; 1: ...
409          */
410         DLIST_FOREACH(ipipoleqelem) {
411                 n = DLIST_NEXT(ipqelem);
412                 ip2 = DLIST_NEXT(nqelem);
413                 if (ip->type != IP_NODE || ip->ip_node->n_op != CBRANCH)
414                         continue;
415                 if (n->type != IP_NODE || n->ip_node->n_op != GOTO)
416                         continue;
417                 if (ip2->type != IP_DEFLAB)
418                         continue;
419                 i = ip->ip_node->n_right->n_lval;
420                 j = n->ip_node->n_left->n_lval;
421                 if (j == lab1 || j == lab2)
422                         continue;
423                 if (i != ip2->ip_lbl || i == lab1 || i == lab2)
424                         continue;
425                 ip->ip_node->n_right->n_lval = j;
426                 switch (ip->ip_node->n_left->n_op) {
427                 case EQ:
428                         j = NE;
429                         break;
430                 case NE:
431                         j = EQ;
432                         break;
433                 case LE:
434                         j = GT;
435                         break;
436                 case LT:
437                         j = GE;
438                         break;
439                 case GE:
440                         j = LT;
441                         break;
442                 case GT:
443                         j = LE;
444                         break;
445                 case ULE:
446                         j = UGT;
447                         break;
448                 case ULT:
449                         j = UGE;
450                         break;
451                 case UGE:
452                         j = ULT;
453                         break;
454                 case UGT:
455                         j = ULE;
456                         break;
457                 default:
458                         comperr("deljumps: unexpected op");
459                 }
460                 ip->ip_node->n_left->n_op = j;
461                 tfree(n->ip_node);
462                 DLIST_REMOVE(nqelem);
463                 gotone = 1;
464         }
465
466         /* Delete everything after a goto up to the next label. */
467         for (ip = startdel = 0ip != DLIST_ENDMARK(ipole);
468              ip = DLIST_NEXT(ipqelem)) {
469 loop:
470                 if ((n = DLIST_NEXT(ipqelem)) == DLIST_ENDMARK(ipole))
471                         break;
472                 if (n->type != IP_NODE) {
473                         del = 0;
474                         continue;
475                 }
476                 if (del) {
477                         tfree(n->ip_node);
478                         DLIST_REMOVE(nqelem);
479                         gotone = 1;
480                         goto loop;
481                 } else if (n->ip_node->n_op == GOTO)
482                         del = 1;
483         }
484
ragge
1.1
485         if (gotone)
486                 goto again;
ragge
1.28
487
488 #ifdef notyet
489         tmpfree(mark);
ragge
1.26
490 #endif
ragge
1.1
491 }
492
493 void
494 optdump(struct interpass *ip)
495 {
496         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
497                 "deflab""defnam""asm" };
498         printf("type %s\n"nm[ip->type-1]);
499         switch (ip->type) {
500         case IP_NODE:
501                 fwalk(ip->ip_nodee2print0);
502                 break;
503         case IP_DEFLAB:
504                 printf("label " LABFMT "\n"ip->ip_lbl);
505                 break;
506         case IP_ASM:
507                 printf(": %s\n"ip->ip_asm);
508                 break;
509         }
510 }
pj
1.4
511
512 /*
513  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
514  *
515  * Also fills the labelinfo struct with information about which bblocks
516  * that contain which label.
517  */
518
ragge
1.30
519 void
ragge
1.38
520 bblocks_build(struct interpass *ipolestruct labelinfo *labinfo,
521     struct bblockinfo *bbinfo)
pj
1.4
522 {
523         struct interpass *ip;
524         struct basicblock *bb = NULL;
ragge
1.30
525         int lowhigh;
pj
1.8
526         int count = 0;
pj
1.4
527         int i;
528
ragge
1.30
529         BDEBUG(("bblocks_build (%p, %p)\n"labinfobbinfo));
530         low = ipp->ip_lblnum;
531         high = epp->ip_lblnum;
532
pj
1.4
533         /* 
534          * First statement is a leader.
535          * Any statement that is target of a jump is a leader.
536          * Any statement that immediately follows a jump is a leader.
537          */
ragge
1.38
538         DLIST_FOREACH(ipipoleqelem) {
ragge
1.31
539                 if (bb == NULL || (ip->type == IP_EPILOG) ||
540                     (ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) {
541                         bb = tmpalloc(sizeof(struct basicblock));
542                         bb->first = ip;
543                         SLIST_INIT(&bb->children);
544                         SLIST_INIT(&bb->parents);
545                         bb->dfnum = 0;
546                         bb->dfparent = 0;
547                         bb->semi = 0;
548                         bb->ancestor = 0;
549                         bb->idom = 0;
550                         bb->samedom = 0;
551                         bb->bucket = NULL;
552                         bb->df = NULL;
553                         bb->dfchildren = NULL;
554                         bb->Aorig = NULL;
555                         bb->Aphi = NULL;
ragge
1.34
556                         bb->bbnum = count;
ragge
1.31
557                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
558                         count++;
559                 }
560                 bb->last = ip;
561                 if ((ip->type == IP_NODE) && (ip->ip_node->n_op == GOTO || 
562                     ip->ip_node->n_op == CBRANCH))
563                         bb = NULL;
564                 if (ip->type == IP_PROLOG)
565                         bb = NULL;
566         }
ragge
1.34
567         nbblocks = count;
ragge
1.30
568
ragge
1.31
569         if (b2debug) {
ragge
1.32
570                 printf("Basic blocks in func: %d, low %d, high %d\n",
571                     countlowhigh);
ragge
1.31
572                 DLIST_FOREACH(bb, &bblocksbbelem) {
573                         printf("bb %p: first %p last %p\n"bb,
574                             bb->firstbb->last);
575                 }
576         }
pj
1.4
577
578         labinfo->low = low;
579         labinfo->size = high - low + 1;
580         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
ragge
1.42
581         for (i = 0i < labinfo->sizei++) {
pj
1.4
582                 labinfo->arr[i] = NULL;
583         }
pj
1.8
584         
585         bbinfo->size = count + 1;
586         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
ragge
1.42
587         for (i = 0i < bbinfo->sizei++) {
pj
1.8
588                 bbinfo->arr[i] = NULL;
589         }
pj
1.4
590
ragge
1.32
591         /* Build the label table */
ragge
1.11
592         DLIST_FOREACH(bb, &bblocksbbelem) {
ragge
1.32
593                 if (bb->first->type == IP_DEFLAB)
pj
1.4
594                         labinfo->arr[bb->first->ip_lbl - low] = bb;
ragge
1.32
595         }
596
597         if (b2debug) {
598                 printf("Label table:\n");
599                 for (i = 0i < labinfo->sizei++)
600                         if (labinfo->arr[i])
601                                 printf("Label %d bblock %p\n"i+low,
602                                     labinfo->arr[i]);
pj
1.4
603         }
604 }
605
606 /*
607  * Build the control flow graph.
608  */
609
610 void
611 cfg_build(struct labelinfo *labinfo)
612 {
613         /* Child and parent nodes */
614         struct cfgnode *cnode
615         struct cfgnode *pnode;
616         struct basicblock *bb;
617         
ragge
1.11
618         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
619
620                 if (bb->first->type == IP_EPILOG) {
621                         break;
622                 }
623
624                 cnode = tmpalloc(sizeof(struct cfgnode));
625                 pnode = tmpalloc(sizeof(struct cfgnode));
626                 pnode->bblock = bb;
627
628                 if ((bb->last->type == IP_NODE) && 
629                     (bb->last->ip_node->n_op == GOTO)) {
630                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
631                             labinfo->size) {
632                                 comperr("Label out of range: %d, base %d"
633                                         bb->last->ip_node->n_left->n_lval
634                                         labinfo->low);
635                         }
636                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
637                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
638                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
639                         continue;
640                 }
641                 if ((bb->last->type == IP_NODE) && 
642                     (bb->last->ip_node->n_op == CBRANCH)) {
643                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
644                             labinfo->size
645                                 comperr("Label out of range: %d"
646                                         bb->last->ip_node->n_left->n_lval);
647                         
648                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
649                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
650                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
651                         cnode = tmpalloc(sizeof(struct cfgnode));
652                         pnode = tmpalloc(sizeof(struct cfgnode));
653                         pnode->bblock = bb;
654                 }
655
ragge
1.11
656                 cnode->bblock = DLIST_NEXT(bbbbelem);
657                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
658                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
659         }
660 }
pj
1.6
661
pj
1.8
662 void
663 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
664 {
665         struct cfgnode *cnode;
666         
667         if (bb->dfnum != 0)
668                 return;
669
670         bb->dfnum = ++dfsnum;
671         bb->dfparent = parent;
672         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
673         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
674                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
675         }
pj
1.13
676         /* Don't bring in unreachable nodes in the future */
pj
1.20
677         bbinfo->size = dfsnum + 1;
pj
1.8
678 }
679
ragge
1.11
680 static bittype *
681 setalloc(int nelem)
682 {
683         bittype *b;
684         int sz = (nelem+NUMBITS-1)/NUMBITS;
685
686         b = tmpalloc(sz * sizeof(bittype));
687         memset(b0sz * sizeof(bittype));
688         return b;
689 }
690
pj
1.8
691 /*
692  * Algorithm 19.9, pp 414 from Appel.
693  */
694
695 void
696 dominators(struct bblockinfo *bbinfo)
697 {
698         struct cfgnode *cnode;
699         struct basicblock *bb, *y, *v;
700         struct basicblock *s, *sprime, *p;
pj
1.10
701         int hi;
pj
1.8
702
ragge
1.11
703         DLIST_FOREACH(bb, &bblocksbbelem) {
704                 bb->bucket = setalloc(bbinfo->size);
705                 bb->df = setalloc(bbinfo->size);
706                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
707         }
708
709         dfsnum = 0;
ragge
1.11
710         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
711
ragge
1.30
712         if (b2debug) {
713                 struct basicblock *bbb;
714                 struct cfgnode *ccnode;
715
716                 DLIST_FOREACH(bbb, &bblocksbbelem) {
717                         printf("Basic block %d, parents: "bbb->dfnum);
718                         SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
719                                 printf("%d, "ccnode->bblock->dfnum);
720                         }
721                         printf("\nChildren: ");
722                         SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
723                                 printf("%d, "ccnode->bblock->dfnum);
724                         }
725                         printf("\n");
pj
1.20
726                 }
727         }
728
pj
1.10
729         for(h = bbinfo->size - 1h > 1h--) {
730                 bb = bbinfo->arr[h];
pj
1.8
731                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
732                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
733                         if (cnode->bblock->dfnum <= bb->dfnum
734                                 sprime = cnode->bblock;
pj
1.10
735                         else 
736                                 sprime = bbinfo->arr[ancestorwithlowestsemi
737                                               (cnode->bblockbbinfo)->semi];
pj
1.8
738                         if (sprime->dfnum < s->dfnum)
739                                 s = sprime;
740                 }
741                 bb->semi = s->dfnum;
pj
1.10
742                 BITSET(s->bucketbb->dfnum);
pj
1.8
743                 link(pbb);
744                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
745                         if(TESTBIT(p->bucketi)) {
pj
1.8
746                                 v = bbinfo->arr[i];
pj
1.10
747                                 y = ancestorwithlowestsemi(vbbinfo);
748                                 if (y->semi == v->semi
pj
1.8
749                                         v->idom = p->dfnum;
750                                 else
751                                         v->samedom = y->dfnum;
752                         }
753                 }
754                 memset(p->bucket0, (bbinfo->size + 7)/8);
755         }
ragge
1.30
756
757         if (b2debug) {
758                 printf("Num\tSemi\tAncest\tidom\n");
759                 DLIST_FOREACH(bb, &bblocksbbelem) {
760                         printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semi,
761                             bb->ancestorbb->idom);
762                 }
pj
1.10
763         }
ragge
1.30
764
pj
1.10
765         for(h = 2h < bbinfo->sizeh++) {
766                 bb = bbinfo->arr[h];
767                 if (bb->samedom != 0) {
pj
1.8
768                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
769                 }
770         }
ragge
1.11
771         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
772                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
ragge
1.30
773                         BDEBUG(("Setting child %d of %d\n",
774                             bb->dfnumbbinfo->arr[bb->idom]->dfnum));
pj
1.10
775                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
776                 }
pj
1.8
777         }
778 }
779
780
781 struct basicblock *
782 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
783 {
784         struct basicblock *u = bblock;
785         struct basicblock *v = bblock;
786
787         while (v->ancestor != 0) {
788                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
789                     bbinfo->arr[u->semi]->dfnum
pj
1.8
790                         u = v;
791                 v = bbinfo->arr[v->ancestor];
792         }
793         return u;
794 }
795
796 void
797 link(struct basicblock *parentstruct basicblock *child)
798 {
799         child->ancestor = parent->dfnum;
800 }
801
802 void
803 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
804 {
805         struct cfgnode *cnode;
pj
1.10
806         int hi;
pj
1.8
807         
ragge
1.11
808         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
809                 if (cnode->bblock->idom != bblock->dfnum)
810                         BITSET(bblock->dfcnode->bblock->dfnum);
811         }
pj
1.10
812         for (h = 1h < bbinfo->sizeh++) {
813                 if (!TESTBIT(bblock->dfchildrenh))
814                         continue;
815                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
816                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
817                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
818                             (bbinfo->arr[h] == bblock ||
819                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
820                             BITSET(bblock->dfi);
821                 }
822         }
823 }
pj
1.15
824
pj
1.21
825 static struct basicblock *currbb;
826 static struct interpass *currip;
827
828 /* Helper function for findTemps, Find assignment nodes. */
829 static void
ragge
1.39
830 searchasg(NODE *p)
pj
1.21
831 {
832         struct pvarinfo *pv;
833
834         if (p->n_op != ASSIGN)
835                 return;
836
837         if (p->n_left->n_op != TEMP)
838                 return;
839
840         pv = tmpcalloc(sizeof(struct pvarinfo));
841         pv->next = defsites.arr[p->n_left->n_lval];
842         pv->bb = currbb;
843         pv->top = currip->ip_node;
844         pv->n = p->n_left;
pj
1.22
845         BITSET(currbb->Aorigp->n_left->n_lval);
pj
1.21
846
847         defsites.arr[p->n_left->n_lval] = pv;
848 }
849
850 /* Walk the interpass looking for assignment nodes. */
851 void findTemps(struct interpass *ip)
pj
1.20
852 {
853         if (ip->type != IP_NODE)
854                 return;
855
pj
1.21
856         currip = ip;
857
ragge
1.39
858         walkf(ip->ip_nodesearchasg);
pj
1.20
859 }
860
861 /*
862  * Algorithm 19.6 from Appel.
863  */
864
865 void
866 placePhiFunctions(struct bblockinfo *bbinfo)
867 {
868         struct basicblock *bb;
869         struct interpass *ip;
870         int maxtmpijkl;
871         struct pvarinfo *n;
872         struct cfgnode *cnode;
873         TWORD ntype;
874         NODE *p;
pj
1.22
875         struct pvarinfo *pv;
pj
1.20
876
877         bb = DLIST_NEXT(&bblocksbbelem);
878         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
879         bb = DLIST_PREV(&bblocksbbelem);
880         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
881         defsites.size = maxtmp - defsites.low + 1;
882         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
883
pj
1.21
884         /* Find all defsites */
pj
1.20
885         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
886                 currbb = bb;
pj
1.20
887                 ip = bb->first;
pj
1.22
888                 bb->Aorig = setalloc(defsites.size);
889                 bb->Aphi = setalloc(defsites.size);
890                 
pj
1.20
891
892                 while (ip != bb->last) {
pj
1.21
893                         findTemps(ip);
pj
1.20
894                         ip = DLIST_NEXT(ipqelem);
895                 }
896                 /* Make sure we get the last statement in the bblock */
pj
1.21
897                 findTemps(ip);
pj
1.20
898         }
pj
1.21
899         /* For each variable */
pj
1.20
900         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
901                 /* While W not empty */
pj
1.20
902                 while (defsites.arr[i] != NULL) {
pj
1.21
903                         /* Remove some node n from W */
pj
1.20
904                         n = defsites.arr[i];
905                         defsites.arr[i] = n->next;
pj
1.21
906                         /* For each y in n->bb->df */
pj
1.20
907                         for (j = 0j < bbinfo->sizej++) {
pj
1.22
908                                 if (!TESTBIT(n->bb->dfj))
pj
1.20
909                                         continue;
pj
1.22
910                                 
911                                 if (TESTBIT(bbinfo->arr[j]->Aphii))
912                                         continue;
913
pj
1.20
914                                 ntype = n->n->n_type;
915                                 k = 0;
pj
1.21
916                                 /* Amount of predecessors for y */
pj
1.20
917                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
918                                         k++;
pj
1.21
919                                 /* Construct phi(...) */
pj
1.20
920                                 p = mklnode(TEMPi0ntype);
921                                 for (l = 0l < k-1l++)
922                                         p = mkbinode(PHIp,
923                                             mklnode(TEMPi0ntype), ntype);
924                                 ip = ipnode(mkbinode(ASSIGN,
925                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
926                                 /* Insert phi at top of basic block */
pj
1.20
927                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
928                                 n->bb->first = ip;
pj
1.22
929                                 BITSET(bbinfo->arr[j]->Aphii);
930                                 if (!TESTBIT(bbinfo->arr[j]->Aorigi)) {
931                                         pv = tmpalloc(sizeof(struct pvarinfo));
932                                         // XXXpj Ej fullständig information.
933                                         pv->bb = bbinfo->arr[j];
934                                         pv->next = defsites.arr[i]->next;
935                                         defsites.arr[i] = pv;
936                                 }
937                                         
pj
1.20
938
939                         }
940                 }
941         }
942 }
943
pj
1.15
944 /*
945  * Remove unreachable nodes in the CFG.
946  */ 
947
948 void
949 remunreach(void)
950 {
951         struct basicblock *bb, *nbb;
952         struct interpass *next, *ctree;
953
954         bb = DLIST_NEXT(&bblocksbbelem);
955         while (bb != &bblocks) {
956                 nbb = DLIST_NEXT(bbbbelem);
957
958                 /* Code with dfnum 0 is unreachable */
959                 if (bb->dfnum != 0) {
960                         bb = nbb;
961                         continue;
962                 }
963
964                 /* Need the epilogue node for other parts of the
965                    compiler, set its label to 0 and backend will
966                    handle it. */ 
967                 if (bb->first->type == IP_EPILOG) {
968                         bb->first->ip_lbl = 0;
969                         bb = nbb;
970                         continue;
971                 }
972
973                 next = bb->first;
974                 do {
975                         ctree = next;
976                         next = DLIST_NEXT(ctreeqelem);
977                         
978                         if (ctree->type == IP_NODE)
pj
1.16
979                                 tfree(ctree->ip_node);
pj
1.15
980                         DLIST_REMOVE(ctreeqelem);
981                 } while (ctree != bb->last);
982                         
983                 DLIST_REMOVE(bbbbelem);
984                 bb = nbb;
985         }
986 }
ragge
1.19
987
988 void
989 printip(struct interpass *pole)
990 {
991         static char *foo[] = {
992            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
993         struct interpass *ip;
ragge
1.38
994         struct interpass_prolog *ipp, *epp;
ragge
1.19
995
996         DLIST_FOREACH(ippoleqelem) {
ragge
1.43
997                 if (ip->type > MAXIP)
ragge
1.19
998                         printf("IP(%d) (%p): "ip->typeip);
999                 else
1000                         printf("%s (%p): "foo[ip->type], ip);
1001                 switch (ip->type) {
1002                 case IP_NODEprintf("\n");
1003                         fwalk(ip->ip_nodee2print0); break;
1004                 case IP_PROLOG:
ragge
1.38
1005                         ipp = (struct interpass_prolog *)ip;
1006                         printf("%s %s regs %x autos %d mintemp %d minlbl %d\n",
1007                             ipp->ipp_nameipp->ipp_vis ? "(local)" : "",
1008                             ipp->ipp_regsipp->ipp_autosipp->ip_tmpnum,
1009                             ipp->ip_lblnum);
1010                         break;
1011                 case IP_EPILOG:
1012                         epp = (struct interpass_prolog *)ip;
1013                         printf("%s %s regs %x autos %d mintemp %d minlbl %d\n",
1014                             epp->ipp_nameepp->ipp_vis ? "(local)" : "",
1015                             epp->