Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080415095232

Diff

Diff from 1.52 to:

Annotations

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

Annotated File View

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