Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080619074112

Diff

Diff from 1.53 to:

Annotations

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

Annotated File View

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