Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:mickey:20081101082937

Diff

Diff from 1.55 to:

Annotations

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

Annotated File View

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