Debian Codegraph @

mip/optim2.c

raw symbols SCIP
1 /* $Id: optim2.c,v 1.99 2018/09/15 15:21:45 ragge Exp $ */
2 /*
3 * Copyright (c) 2004 Anders Magnusson ([email protected]).
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
31 #include <string.h>
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
41
42 #define BDEBUG(x) if (b2debug) printf x
43
44 #define mktemp(n, t) mklnode(TEMP, 0, n, t)
45
46 #define CHADD(bb,c) { if (bb->ch[0] == 0) bb->ch[0] = c; \
47 else if (bb->ch[1] == 0) bb->ch[1] = c; \
48 else comperr("triple cfnodes"); }
49 #define FORCH(cn, chp) \
50 for (cn = &chp[0]; cn < &chp[2] && cn[0]; cn++)
51
52 /* main switch for new things not yet ready for all-day use */
53 /* #define ENABLE_NEW */
54
55
56 static int dfsnum;
57
58 void saveip(struct interpass *ip);
59 void deljumps(struct p2env *);
60 void optdump(struct interpass *ip);
61 void printip(struct interpass *pole);
62
63 static struct varinfo defsites;
64
65 void bblocks_build(struct p2env *);
66 void cfg_build(struct p2env *);
67 void cfg_dfs(struct basicblock *bb, unsigned int parent,
68 struct bblockinfo *bbinfo);
69 void dominators(struct p2env *);
70 struct basicblock *
71 ancestorwithlowestsemi(struct basicblock *bblock, struct bblockinfo *bbinfo);
72 void computeDF(struct p2env *, struct basicblock *bblock);
73 void printDF(struct p2env *p2e);
74 void findTemps(struct interpass *ip);
75 void placePhiFunctions(struct p2env *);
76 void renamevar(struct p2env *p2e,struct basicblock *bblock);
77 void removephi(struct p2env *p2e);
78 void simple_cp(struct p2env *p2e);
79 void remunreach(struct p2env *);
80 static void liveanal(struct p2env *p2e);
81 static void printip2(struct interpass *);
82
83 /* create "proper" basic blocks, add labels where needed (so bblocks have labels) */
84 /* run before bb generate */
85 static void add_labels(struct p2env*) ;
86
87 /* Perform trace scheduling, try to get rid of gotos as much as possible */
88 void TraceSchedule(struct p2env*) ;
89
90 #ifdef ENABLE_NEW
91 static void do_cse(struct p2env* p2e) ;
92 #endif
93
94 /* Walk the complete set, performing a function on each node.
95 * if type is given, apply function on only that type */
96 void WalkAll(struct p2env* p2e, void (*f) (NODE*, void*), void* arg, int type) ;
97
98 void BBLiveDead(struct basicblock* bblock, int what, unsigned int variable) ;
99
100 /* Fill the live/dead code */
101 void LiveDead(struct p2env* p2e, int what, unsigned int variable) ;
102
103 #ifdef PCC_DEBUG
104 void printflowdiagram(struct p2env *, char *);
105 #endif
106
107 void
108 optimize(struct p2env *p2e)
109 {
110 struct interpass *ipole = &p2e->ipole;
111
112 if (b2debug) {
113 printf("initial links\n");
114 printip(ipole);
115 }
116
117 if (xdeljumps)
118 deljumps(p2e); /* Delete redundant jumps and dead code */
119
120 if (xssa)
121 add_labels(p2e) ;
122 #ifdef ENABLE_NEW
123 do_cse(p2e);
124 #endif
125
126 #ifdef PCC_DEBUG
127 if (b2debug) {
128 printf("links after deljumps\n");
129 printip(ipole);
130 }
131 #endif
132 if (xssa || xtemps) {
133 bblocks_build(p2e);
134 BDEBUG(("Calling cfg_build\n"));
135 cfg_build(p2e);
136
137 #ifdef PCC_DEBUG
138 printflowdiagram(p2e, "first");
139 #endif
140 }
141 if (xssa) {
142 BDEBUG(("Calling liveanal\n"));
143 liveanal(p2e);
144 BDEBUG(("Calling dominators\n"));
145 dominators(p2e);
146 BDEBUG(("Calling computeDF\n"));
147 computeDF(p2e, DLIST_NEXT(&p2e->bblocks, bbelem));
148
149 if (b2debug) {
150 printDF(p2e);
151 }
152
153 BDEBUG(("Calling placePhiFunctions\n"));
154
155 placePhiFunctions(p2e);
156
157 BDEBUG(("Calling renamevar\n"));
158
159 renamevar(p2e,DLIST_NEXT(&p2e->bblocks, bbelem));
160
161 BDEBUG(("Calling removephi\n"));
162
163 #ifdef PCC_DEBUG
164 printflowdiagram(p2e, "ssa");
165 #endif
166
167 removephi(p2e);
168
169 /* Simple constant propagation */
170 if (xscp)
171 simple_cp(p2e);
172
173 BDEBUG(("Calling remunreach\n"));
174 /* remunreach(p2e); */
175
176 /*
177 * Recalculate basic blocks and cfg that was destroyed
178 * by removephi
179 */
180 /* first, clean up all what deljumps should have done, and more */
181
182 /* TODO: add the basic blocks done by the ssa code by hand.
183 * The trace scheduler should not change the order in
184 * which blocks are executed or what data is calculated.
185 * Thus, the BBlock order should remain correct.
186 */
187
188 #ifdef ENABLE_NEW
189 bblocks_build(p2e);
190 BDEBUG(("Calling cfg_build\n"));
191 cfg_build(p2e);
192
193 TraceSchedule(p2e);
194 #ifdef PCC_DEBUG
195 printflowdiagram(p2e, "sched_trace");
196
197 if (b2debug) {
198 printf("after tracesched\n");
199 printip(ipole);
200 fflush(stdout) ;
201 }
202 #endif
203 #endif
204
205 /* Now, clean up the gotos we do not need any longer */
206 if (xdeljumps)
207 deljumps(p2e); /* Delete redundant jumps and dead code */
208
209 bblocks_build(p2e);
210 BDEBUG(("Calling cfg_build\n"));
211 cfg_build(p2e);
212
213 #ifdef PCC_DEBUG
214 printflowdiagram(p2e, "no_phi");
215
216 if (b2debug) {
217 printf("new tree\n");
218 printip(ipole);
219 }
220 #endif
221 }
222 myoptim(ipole);
223 }
224
225 /*
226 * Delete unused labels, excess of labels, gotos to gotos.
227 * This routine can be made much more efficient.
228 *
229 * Layout of the statement list here (_must_ look this way!):
230 * PROLOG
231 * LABEL - states beginning of function argument moves
232 * ...code to save/move arguments
233 * LABEL - states beginning of execution code
234 * ...code + labels in function in function
235 * EPILOG
236 *
237 * This version of deljumps is based on the c2 implementation
238 * that were included in 2BSD.
239 */
240 #define LABEL 1
241 #define JBR 2
242 #define CBR 3
243 #define STMT 4
244 #define EROU 5
245 struct dlnod {
246 int op;
247 struct interpass *dlip;
248 struct dlnod *forw;
249 struct dlnod *back;
250 struct dlnod *ref;
251 int labno;
252 int refc;
253 };
254
255 #ifdef DLJDEBUG
256 static void
257 dumplink(struct dlnod *dl)
258 {
259 printf("dumplink %p\n", dl);
260 for (; dl; dl = dl->forw) {
261 if (dl->op == STMT) {
262 printf("STMT(%p)\n", dl);
263 fwalk(dl->dlip->ip_node, e2print, 0);
264 } else if (dl->op == EROU) {
265 printf("EROU(%p)\n", dl);
266 } else {
267 static char *str[] = { 0, "LABEL", "JBR", "CBR" };
268 printf("%s(%p) %d refc %d ref %p\n", str[dl->op],
269 dl, dl->labno, dl->refc, dl->ref);
270 }
271 }
272 printf("end dumplink\n");
273 }
274 #endif
275
276 /*
277 * Create the linked list that we can work on.
278 */
279 static void
280 listsetup(struct interpass *ipole, struct dlnod *dl)
281 {
282 struct interpass *ip = DLIST_NEXT(ipole, qelem);
283 struct interpass *nip;
284 struct dlnod *p, *lastp;
285 NODE *q;
286
287 lastp = dl;
288 while (ip->type != IP_DEFLAB)
289 ip = DLIST_NEXT(ip,qelem);
290 ip = DLIST_NEXT(ip,qelem);
291 while (ip->type != IP_DEFLAB)
292 ip = DLIST_NEXT(ip,qelem);
293 /* Now ip is at the beginning */
294 for (;;) {
295 ip = DLIST_NEXT(ip,qelem);
296 if (ip == ipole)
297 break;
298 p = tmpalloc(sizeof(struct dlnod));
299 p->labno = 0;
300 p->dlip = ip;
301 switch (ip->type) {
302 case IP_DEFLAB:
303 p->op = LABEL;
304 p->labno = ip->ip_lbl;
305 break;
306
307 case IP_NODE:
308 q = ip->ip_node;
309 switch (q->n_op) {
310 case GOTO:
311 if (q->n_left->n_op == ICON) {
312 p->op = JBR;
313 p->labno = (int)getlval(q->n_left);
314 } else
315 p->op = STMT;
316 break;
317 case CBRANCH:
318 p->op = CBR;
319 p->labno = (int)getlval(q->n_right);
320 break;
321 case ASSIGN:
322 /* remove ASSIGN to self for regs */
323 if (q->n_left->n_op == REG &&
324 q->n_right->n_op == REG &&
325 regno(q->n_left) == regno(q->n_right)) {
326 nip = DLIST_PREV(ip, qelem);
327 tfree(q);
328 DLIST_REMOVE(ip, qelem);
329 ip = nip;
330 continue;
331 }
332 /* FALLTHROUGH */
333 default:
334 p->op = STMT;
335 break;
336 }
337 break;
338
339 case IP_ASM:
340 p->op = STMT;
341 break;
342
343 case IP_EPILOG:
344 p->op = EROU;
345 break;
346
347 default:
348 comperr("listsetup: bad ip node %d", ip->type);
349 }
350 p->forw = 0;
351 p->back = lastp;
352 lastp->forw = p;
353 lastp = p;
354 p->ref = 0;
355 }
356 }
357
358 /*
359 * Traverse to the first statement behind a label.
360 */
361 static struct dlnod *
362 nonlab(struct dlnod *p)
363 {
364 while (p && p->op==LABEL)
365 p = p->forw;
366 return(p);
367 }
368
369 static void
370 iprem(struct dlnod *p)
371 {
372 if (p->dlip->type == IP_NODE)
373 tfree(p->dlip->ip_node);
374 DLIST_REMOVE(p->dlip, qelem);
375 }
376
377 static void
378 decref(struct dlnod *p)
379 {
380 if (--p->refc <= 0) {
381 iprem(p);
382 p->back->forw = p->forw;
383 p->forw->back = p->back;
384 }
385 }
386
387 /*
388 * Change a label number for jump/branch instruction to labno.
389 */
390 static void
391 setlab(struct dlnod *p, int labno)
392 {
393 p->labno = labno;
394 if (p->op == JBR)
395 setlval(p->dlip->ip_node->n_left, labno);
396 else if (p->op == CBR) {
397 setlval(p->dlip->ip_node->n_right, labno);
398 p->dlip->ip_node->n_left->n_label = labno;
399 } else
400 comperr("setlab bad op %d", p->op);
401 }
402
403 /*
404 * See if a label is used outside of us.
405 */
406 static int
407 inuse(struct p2env *p2e, int lbl)
408 {
409 int *l;
410
411 for (l = p2e->epp->ip_labels; *l; l++)
412 if (*l == lbl)
413 return 1;
414 return 0;
415 }
416
417 /*
418 * Label reference counting and removal of unused labels.
419 */
420 #define LABHS 127
421 static void
422 refcount(struct p2env *p2e, struct dlnod *dl)
423 {
424 struct dlnod *p, *lp;
425 struct dlnod *labhash[LABHS];
426 struct dlnod **hp, *tp;
427
428 /* Clear label hash */
429 for (hp = labhash; hp < &labhash[LABHS];)
430 *hp++ = 0;
431 /* Enter labels into hash. Later overwrites earlier */
432 for (p = dl->forw; p!=0; p = p->forw) {
433 if (p->op==LABEL) {
434 labhash[p->labno % LABHS] = p;
435 p->refc = 0;
436 if (inuse(p2e, p->labno))
437 p->refc = 1000; /* never remove */
438 }
439 }
440 /* search for jumps to labels and fill in reference */
441 for (p = dl->forw; p!=0; p = p->forw) {
442 if (p->op==JBR || p->op==CBR) {
443 p->ref = 0;
444 lp = labhash[p->labno % LABHS];
445 if (lp==0 || p->labno!=lp->labno)
446 for (lp = dl->forw; lp!=0; lp = lp->forw) {
447 if (lp->op==LABEL && p->labno==lp->labno)
448 break;
449 }
450 if (lp) {
451 tp = nonlab(lp)->back;
452 if (tp!=lp) {
453 setlab(p, tp->labno);
454 lp = tp;
455 }
456 p->ref = lp;
457 lp->refc++;
458 }
459 }
460 }
461 for (p = dl->forw; p!=0; p = p->forw)
462 if (p->op==LABEL && p->refc==0 && (lp = nonlab(p))->op)
463 decref(p);
464 }
465
466 static int nchange;
467
468 static struct dlnod *
469 codemove(struct dlnod *p)
470 {
471 struct dlnod *p1, *p2, *p3;
472 #ifdef notyet
473 struct dlnod *t, *tl;
474 int n;
475 #endif
476
477 p1 = p;
478 if (p1->op!=JBR || (p2 = p1->ref)==0)
479 return(p1);
480 while (p2->op == LABEL)
481 if ((p2 = p2->back) == 0)
482 return(p1);
483 if (p2->op!=JBR)
484 goto ivloop;
485 if (p1==p2)
486 return(p1);
487 p2 = p2->forw;
488 p3 = p1->ref;
489 while (p3) {
490 if (p3->op==JBR) {
491 if (p1==p3 || p1->forw==p3 || p1->back==p3)
492 return(p1);
493 nchange++;
494 p1->back->forw = p2;
495 p1->dlip->qelem.q_back->qelem.q_forw = p2->dlip;
496
497 p1->forw->back = p3;
498 p1->dlip->qelem.q_forw->qelem.q_back = p3->dlip;
499
500
501 p2->back->forw = p3->forw;
502 p2->dlip->qelem.q_back->qelem.q_forw = p3->forw->dlip;
503
504 p3->forw->back = p2->back;
505 p3->dlip->qelem.q_forw->qelem.q_back = p2->back->dlip;
506
507 p2->back = p1->back;
508 p2->dlip->qelem.q_back = p1->dlip->qelem.q_back;
509
510 p3->forw = p1->forw;
511 p3->dlip->qelem.q_forw = p1->forw->dlip;
512
513 decref(p1->ref);
514 if (p1->dlip->type == IP_NODE)
515 tfree(p1->dlip->ip_node);
516
517 return(p2);
518 } else
519 p3 = p3->forw;
520 }
521 return(p1);
522
523 ivloop:
524 if (p1->forw->op!=LABEL)
525 return(p1);
526 return(p1);
527
528 #ifdef notyet
529 p3 = p2 = p2->forw;
530 n = 16;
531 do {
532 if ((p3 = p3->forw) == 0 || p3==p1 || --n==0)
533 return(p1);
534 } while (p3->op!=CBR || p3->labno!=p1->forw->labno);
535 do
536 if ((p1 = p1->back) == 0)
537 return(p);
538 while (p1!=p3);
539 p1 = p;
540 tl = insertl(p1);
541 p3->subop = revbr[p3->subop];
542 decref(p3->ref);
543 p2->back->forw = p1;
544 p3->forw->back = p1;
545 p1->back->forw = p2;
546 p1->forw->back = p3;
547 t = p1->back;
548 p1->back = p2->back;
549 p2->back = t;
550 t = p1->forw;
551 p1->forw = p3->forw;
552 p3->forw = t;
553 p2 = insertl(p1->forw);
554 p3->labno = p2->labno;
555 p3->ref = p2;
556 decref(tl);
557 if (tl->refc<=0)
558 nrlab--;
559 nchange++;
560 return(p3);
561 #endif
562 }
563
564 static void
565 iterate(struct p2env *p2e, struct dlnod *dl)
566 {
567 struct dlnod *p, *rp, *p1;
568 extern int negrel[];
569 extern size_t negrelsize;
570 int i;
571
572 nchange = 0;
573 for (p = dl->forw; p!=0; p = p->forw) {
574 if ((p->op==JBR||p->op==CBR) && p->ref) {
575 /* Resolves:
576 * jbr L7
577 * ...
578 * L7: jbr L8
579 */
580 rp = nonlab(p->ref);
581 if (rp->op==JBR && rp->labno && p->labno!=rp->labno) {
582 setlab(p, rp->labno);
583 decref(p->ref);
584 rp->ref->refc++;
585 p->ref = rp->ref;
586 nchange++;
587 }
588 }
589 if (p->op==CBR && (p1 = p->forw)->op==JBR) {
590 /* Resolves:
591 * cbr L7
592 * jbr L8
593 * L7:
594 */
595 rp = p->ref;
596 do
597 rp = rp->back;
598 while (rp->op==LABEL);
599 if (rp==p1) {
600 decref(p->ref);
601 p->ref = p1->ref;
602 setlab(p, p1->labno);
603
604 iprem(p1);
605
606 p1->forw->back = p;
607 p->forw = p1->forw;
608
609 i = p->dlip->ip_node->n_left->n_op;
610 if (i < EQ || i - EQ >= (int)negrelsize)
611 comperr("deljumps: unexpected op");
612 p->dlip->ip_node->n_left->n_op = negrel[i - EQ];
613 nchange++;
614 }
615 }
616 if (p->op == JBR) {
617 /* Removes dead code */
618 while (p->forw && p->forw->op!=LABEL &&
619 p->forw->op!=EROU) {
620 nchange++;
621 if (p->forw->ref)
622 decref(p->forw->ref);
623
624 iprem(p->forw);
625
626 p->forw = p->forw->forw;
627 p->forw->back = p;
628 }
629 rp = p->forw;
630 while (rp && rp->op==LABEL) {
631 if (p->ref == rp) {
632 p->back->forw = p->forw;
633 p->forw->back = p->back;
634 iprem(p);
635 p = p->back;
636 decref(rp);
637 nchange++;
638 break;
639 }
640 rp = rp->forw;
641 }
642 }
643 if (p->op == JBR) {
644 /* xjump(p); * needs tree rewrite; not yet */
645 p = codemove(p);
646 }
647 }
648 }
649
650 void
651 deljumps(struct p2env *p2e)
652 {
653 struct interpass *ipole = &p2e->ipole;
654 struct dlnod dln;
655 MARK mark;
656
657 markset(&mark);
658
659 memset(&dln, 0, sizeof(dln));
660 listsetup(ipole, &dln);
661 do {
662 refcount(p2e, &dln);
663 do {
664 iterate(p2e, &dln);
665 } while (nchange);
666 #ifdef notyet
667 comjump();
668 #endif
669 } while (nchange);
670
671 markfree(&mark);
672 }
673
674 void
675 optdump(struct interpass *ip)
676 {
677 static char *nm[] = { "node", "prolog", "newblk", "epilog", "locctr",
678 "deflab", "defnam", "asm" };
679 printf("type %s\n", nm[ip->type-1]);
680 switch (ip->type) {
681 case IP_NODE:
682 #ifdef PCC_DEBUG
683 fwalk(ip->ip_node, e2print, 0);
684 #endif
685 break;
686 case IP_DEFLAB:
687 printf("label " LABFMT "\n", ip->ip_lbl);
688 break;
689 case IP_ASM:
690 printf("%s", ip->ip_asm);
691 break;
692 }
693 }
694
695 /*
696 * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
697 *
698 * Also fills the labelinfo struct with information about which bblocks
699 * that contain which label.
700 */
701
702 void
703 bblocks_build(struct p2env *p2e)
704 {
705 struct interpass *ipole = &p2e->ipole;
706 struct interpass *ip;
707 struct basicblock *bb = NULL;
708 int low, high;
709 int count = 0;
710 int i;
711
712 BDEBUG(("bblocks_build (%p, %p)\n", &p2e->labinfo, &p2e->bbinfo));
713 low = p2e->ipp->ip_lblnum;
714 high = p2e->epp->ip_lblnum;
715
716 /*
717 * First statement is a leader.
718 * Any statement that is target of a jump is a leader.
719 * Any statement that immediately follows a jump is a leader.
720 */
721 DLIST_INIT(&p2e->bblocks, bbelem);
722 DLIST_FOREACH(ip, ipole, qelem) {
723 if (bb == NULL || (ip->type == IP_EPILOG) ||
724 (ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) {
725 bb = tmpalloc(sizeof(struct basicblock));
726 bb->first = ip;
727 SLIST_INIT(&bb->parents);
728 SLIST_INIT(&bb->child);
729 bb->dfnum = 0;
730 bb->dfparent = 0;
731 bb->semi = 0;
732 bb->ancestor = 0;
733 bb->idom = 0;
734 bb->samedom = 0;
735 bb->bucket = NULL;
736 bb->df = NULL;
737 bb->dfchildren = NULL;
738 bb->Aorig = NULL;
739 bb->Aphi = NULL;
740 SLIST_INIT(&bb->phi);
741 bb->bbnum = count;
742 DLIST_INSERT_BEFORE(&p2e->bblocks, bb, bbelem);
743 count++;
744 }
745 bb->last = ip;
746 if ((ip->type == IP_NODE) && (ip->ip_node->n_op == GOTO ||
747 ip->ip_node->n_op == CBRANCH))
748 bb = NULL;
749 if (ip->type == IP_PROLOG)
750 bb = NULL;
751 }
752 p2e->nbblocks = count;
753
754 if (b2debug) {
755 printf("Basic blocks in func: %d, low %d, high %d\n",
756 count, low, high);
757 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
758 printf("bb(%d) %p: first %p last %p\n", bb->bbnum, bb,
759 bb->first, bb->last);
760 }
761 }
762
763 p2e->labinfo.low = low;
764 p2e->labinfo.size = high - low + 1;
765 p2e->labinfo.arr = tmpalloc(p2e->labinfo.size * sizeof(struct basicblock *));
766 for (i = 0; i < p2e->labinfo.size; i++) {
767 p2e->labinfo.arr[i] = NULL;
768 }
769
770 p2e->bbinfo.size = count + 1;
771 p2e->bbinfo.arr = tmpalloc(p2e->bbinfo.size * sizeof(struct basicblock *));
772 for (i = 0; i < p2e->bbinfo.size; i++) {
773 p2e->bbinfo.arr[i] = NULL;
774 }
775
776 /* Build the label table */
777 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
778 if (bb->first->type == IP_DEFLAB)
779 p2e->labinfo.arr[bb->first->ip_lbl - low] = bb;
780 }
781
782 if (b2debug) {
783 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
784 printf("bblock %d\n", bb->bbnum);
785 for (ip = bb->first; ip != bb->last;
786 ip = DLIST_NEXT(ip, qelem)) {
787 printip2(ip);
788 }
789 printip2(ip);
790 }
791
792 printf("Label table:\n");
793 for (i = 0; i < p2e->labinfo.size; i++)
794 if (p2e->labinfo.arr[i])
795 printf("Label %d bblock %p\n", i+low,
796 p2e->labinfo.arr[i]);
797 }
798 }
799
800 /*
801 * Build the control flow graph.
802 */
803
804 void
805 cfg_build(struct p2env *p2e)
806 {
807 /* Child and parent nodes */
808 struct cfgnode *cnode;
809 struct cfgnode *pnode;
810 struct basicblock *bb;
811 NODE *p;
812
813 #ifdef notyet
814 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
815 if (bb->first->type == IP_EPILOG)
816 break;
817
818 if (bb->last->type == IP_NODE) {
819 p = bb->last->ip_node;
820 switch (p->n_op) {
821 case GOTO:
822 if (p->n_left->n_op == ICON) {
823 lbchk(p2e, p->n_left->n_lval);
824 fillcnode(p2e, p->n_left->n_lval, bb);
825 } else {
826 }
827 break;
828
829 case CBRANCH:
830 lbchk(p2e, p->n_right->n_lval);
831 fillcnode(p2e, p->n_right->n_lval, bb);
832 ...
833
834 default:
835 ...
836 }
837 } else {
838 ...
839 }
840 }
841 #endif
842
843 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
844 if (bb->first->type == IP_EPILOG)
845 break;
846
847 cnode = tmpalloc(sizeof(struct cfgnode));
848 pnode = tmpalloc(sizeof(struct cfgnode));
849 pnode->bblock = bb;
850
851 p = bb->last->ip_node;
852 if (bb->last->type == IP_NODE && p->n_op == GOTO) {
853 if (p->n_left->n_op == ICON) {
854 if (getlval(p->n_left) - p2e->labinfo.low > p2e->labinfo.size)
855 comperr("Label out of range: %d, base %d",
856 getlval(p->n_left), p2e->labinfo.low);
857 cnode->bblock = p2e->labinfo.arr[getlval(p->n_left) - p2e->labinfo.low];
858 SLIST_INSERT_LAST(&cnode->bblock->parents, pnode, cfgelem);
859 SLIST_INSERT_LAST(&bb->child, cnode, chld);
860 } else {
861 int *l;
862 /* XXX assume all labels are valid as dest */
863 for (l = p2e->epp->ip_labels; *l; l++) {
864 cnode->bblock = p2e->labinfo.arr[*l - p2e->labinfo.low];
865 SLIST_INSERT_LAST(&cnode->bblock->parents, pnode, cfgelem);
866 SLIST_INSERT_LAST(&bb->child, cnode, chld);
867 cnode = tmpalloc(sizeof(struct cfgnode));
868 pnode = tmpalloc(sizeof(struct cfgnode));
869 pnode->bblock = bb;
870 }
871 }
872 continue;
873 }
874 if ((bb->last->type == IP_NODE) && p->n_op == CBRANCH) {
875 if (getlval(p->n_right) - p2e->labinfo.low > p2e->labinfo.size)
876 comperr("Label out of range: %d", getlval(p->n_left));
877
878 cnode->bblock = p2e->labinfo.arr[getlval(p->n_right) - p2e->labinfo.low];
879 SLIST_INSERT_LAST(&cnode->bblock->parents, pnode, cfgelem);
880 SLIST_INSERT_LAST(&bb->child, cnode, chld);
881 cnode = tmpalloc(sizeof(struct cfgnode));
882 pnode = tmpalloc(sizeof(struct cfgnode));
883 pnode->bblock = bb;
884 }
885
886 cnode->bblock = DLIST_NEXT(bb, bbelem);
887 SLIST_INSERT_LAST(&cnode->bblock->parents, pnode, cfgelem);
888 SLIST_INSERT_LAST(&bb->child, cnode, chld);
889 }
890 }
891
892 void
893 cfg_dfs(struct basicblock *bb, unsigned int parent, struct bblockinfo *bbinfo)
894 {
895 struct cfgnode *cn;
896
897 if (bb->dfnum != 0)
898 return;
899
900 bb->dfnum = ++dfsnum;
901 bb->dfparent = parent;
902 bbinfo->arr[bb->dfnum] = bb;
903 SLIST_FOREACH(cn, &bb->child, chld)
904 cfg_dfs(cn->bblock, bb->dfnum, bbinfo);
905 /* Don't bring in unreachable nodes in the future */
906 bbinfo->size = dfsnum + 1;
907 }
908
909 static bittype *
910 setalloc(int nelem)
911 {
912 bittype *b;
913 int sz = (nelem+NUMBITS-1)/NUMBITS;
914
915 b = tmpalloc(sz * sizeof(bittype));
916 memset(b, 0, sz * sizeof(bittype));
917 return b;
918 }
919
920 /*
921 * Algorithm 19.9, pp 414 from Appel.
922 */
923
924 void
925 dominators(struct p2env *p2e)
926 {
927 struct cfgnode *cnode;
928 struct basicblock *bb, *y, *v;
929 struct basicblock *s, *sprime, *p;
930 int h, i;
931
932 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
933 bb->bucket = setalloc(p2e->bbinfo.size);
934 bb->df = setalloc(p2e->bbinfo.size);
935 bb->dfchildren = setalloc(p2e->bbinfo.size);
936 }
937
938 dfsnum = 0;
939 cfg_dfs(DLIST_NEXT(&p2e->bblocks, bbelem), 0, &p2e->bbinfo);
940
941 if (b2debug) {
942 struct basicblock *bbb;
943 struct cfgnode *ccnode, *cn;
944
945 DLIST_FOREACH(bbb, &p2e->bblocks, bbelem) {
946 printf("Basic block %d, parents: ", bbb->dfnum);
947 SLIST_FOREACH(ccnode, &bbb->parents, cfgelem) {
948 printf("%d, ", ccnode->bblock->dfnum);
949 }
950 printf("\nChildren: ");
951 SLIST_FOREACH(cn, &bbb->child, chld)
952 printf("%d, ", cn->bblock->dfnum);
953 printf("\n");
954 }
955 }
956
957 for(h = p2e->bbinfo.size - 1; h > 1; h--) {
958 bb = p2e->bbinfo.arr[h];
959 p = s = p2e->bbinfo.arr[bb->dfparent];
960 SLIST_FOREACH(cnode, &bb->parents, cfgelem) {
961 if (cnode->bblock->dfnum ==0)
962 continue; /* Ignore unreachable code */
963
964 if (cnode->bblock->dfnum <= bb->dfnum)
965 sprime = cnode->bblock;
966 else
967 sprime = p2e->bbinfo.arr[ancestorwithlowestsemi
968 (cnode->bblock, &p2e->bbinfo)->semi];
969 if (sprime->dfnum < s->dfnum)
970 s = sprime;
971 }
972 bb->semi = s->dfnum;
973 BITSET(s->bucket, bb->dfnum);
974 bb->ancestor = p->dfnum;
975 for (i = 1; i < p2e->bbinfo.size; i++) {
976 if(TESTBIT(p->bucket, i)) {
977 v = p2e->bbinfo.arr[i];
978 y = ancestorwithlowestsemi(v, &p2e->bbinfo);
979 if (y->semi == v->semi)
980 v->idom = p->dfnum;
981 else
982 v->samedom = y->dfnum;
983 }
984 }
985 memset(p->bucket, 0, (p2e->bbinfo.size + 7)/8);
986 }
987
988 if (b2debug) {
989 printf("Num\tSemi\tAncest\tidom\n");
990 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
991 printf("%d\t%d\t%d\t%d\n", bb->dfnum, bb->semi,
992 bb->ancestor, bb->idom);
993 }
994 }
995
996 for(h = 2; h < p2e->bbinfo.size; h++) {
997 bb = p2e->bbinfo.arr[h];
998 if (bb->samedom != 0) {
999 bb->idom = p2e->bbinfo.arr[bb->samedom]->idom;
1000 }
1001 }
1002 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1003 if (bb->idom != 0 && bb->idom != bb->dfnum) {
1004 BDEBUG(("Setting child %d of %d\n",
1005 bb->dfnum, p2e->bbinfo.arr[bb->idom]->dfnum));
1006 BITSET(p2e->bbinfo.arr[bb->idom]->dfchildren, bb->dfnum);
1007 }
1008 }
1009 }
1010
1011
1012 struct basicblock *
1013 ancestorwithlowestsemi(struct basicblock *bblock, struct bblockinfo *bbinfo)
1014 {
1015 struct basicblock *u = bblock;
1016 struct basicblock *v = bblock;
1017
1018 while (v->ancestor != 0) {
1019 if (bbinfo->arr[v->semi]->dfnum <
1020 bbinfo->arr[u->semi]->dfnum)
1021 u = v;
1022 v = bbinfo->arr[v->ancestor];
1023 }
1024 return u;
1025 }
1026
1027 void
1028 computeDF(struct p2env *p2e, struct basicblock *bblock)
1029 {
1030 struct cfgnode *cn;
1031 int h, i;
1032
1033 SLIST_FOREACH(cn, &bblock->child, chld) {
1034 if (cn->bblock->idom != bblock->dfnum)
1035 BITSET(bblock->df, cn->bblock->dfnum);
1036 }
1037 for (h = 1; h < p2e->bbinfo.size; h++) {
1038 if (!TESTBIT(bblock->dfchildren, h))
1039 continue;
1040 computeDF(p2e, p2e->bbinfo.arr[h]);
1041 for (i = 1; i < p2e->bbinfo.size; i++) {
1042 if (TESTBIT(p2e->bbinfo.arr[h]->df, i) &&
1043 (p2e->bbinfo.arr[i] == bblock ||
1044 (bblock->dfnum != p2e->bbinfo.arr[i]->idom)))
1045 BITSET(bblock->df, i);
1046 }
1047 }
1048 }
1049
1050 void printDF(struct p2env *p2e)
1051 {
1052 struct basicblock *bb;
1053 int i;
1054
1055 printf("Dominance frontiers:\n");
1056
1057 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1058 printf("bb %d : ", bb->dfnum);
1059
1060 for (i=1; i < p2e->bbinfo.size;i++) {
1061 if (TESTBIT(bb->df,i)) {
1062 printf("%d ",i);
1063 }
1064 }
1065
1066 printf("\n");
1067 }
1068
1069 }
1070
1071
1072
1073 static struct basicblock *currbb;
1074
1075 /* Helper function for findTemps, Find assignment nodes. */
1076 static void
1077 searchasg(NODE *p, void *arg)
1078 {
1079 struct pvarinfo *pv;
1080 int tempnr;
1081 struct varstack *stacke;
1082
1083 if (p->n_op != ASSIGN)
1084 return;
1085
1086 if (p->n_left->n_op != TEMP)
1087 return;
1088
1089 tempnr=regno(p->n_left)-defsites.low;
1090
1091 BITSET(currbb->Aorig, tempnr);
1092
1093 pv = tmpcalloc(sizeof(struct pvarinfo));
1094 pv->next = defsites.arr[tempnr];
1095 pv->bb = currbb;
1096 pv->n_type = p->n_left->n_type;
1097
1098 defsites.arr[tempnr] = pv;
1099
1100
1101 if (SLIST_FIRST(&defsites.stack[tempnr])==NULL) {
1102 stacke=tmpcalloc(sizeof (struct varstack));
1103 stacke->tmpregno=0;
1104 SLIST_INSERT_FIRST(&defsites.stack[tempnr],stacke,varstackelem);
1105 }
1106 }
1107
1108 /* Walk the interpass looking for assignment nodes. */
1109 void findTemps(struct interpass *ip)
1110 {
1111 if (ip->type != IP_NODE)
1112 return;
1113
1114 walkf(ip->ip_node, searchasg, 0);
1115 }
1116
1117 /*
1118 * Algorithm 19.6 from Appel.
1119 */
1120
1121 void
1122 placePhiFunctions(struct p2env *p2e)
1123 {
1124 struct basicblock *bb;
1125 struct basicblock *y;
1126 struct interpass *ip;
1127 int maxtmp, i, j, k;
1128 struct pvarinfo *n;
1129 struct cfgnode *cnode;
1130 TWORD ntype;
1131 struct pvarinfo *pv;
1132 struct phiinfo *phi;
1133 int phifound;
1134
1135 bb = DLIST_NEXT(&p2e->bblocks, bbelem);
1136 defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
1137 bb = DLIST_PREV(&p2e->bblocks, bbelem);
1138 maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
1139 defsites.size = maxtmp - defsites.low + 1;
1140 defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
1141 defsites.stack = tmpcalloc(defsites.size*sizeof(SLIST_HEAD(, varstack)));
1142
1143 for (i=0;i<defsites.size;i++)
1144 SLIST_INIT(&defsites.stack[i]);
1145
1146 /* Find all defsites */
1147 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1148 currbb = bb;
1149 ip = bb->first;
1150 bb->Aorig = setalloc(defsites.size);
1151 bb->Aphi = setalloc(defsites.size);
1152
1153 while (ip != bb->last) {
1154 findTemps(ip);
1155 ip = DLIST_NEXT(ip, qelem);
1156 }
1157 /* Make sure we get the last statement in the bblock */
1158 findTemps(ip);
1159 }
1160
1161 /* For each variable */
1162 for (i = 0; i < defsites.size; i++) {
1163 /* While W not empty */
1164 while (defsites.arr[i] != NULL) {
1165 /* Remove some node n from W */
1166 n = defsites.arr[i];
1167 defsites.arr[i] = n->next;
1168 /* For each y in n->bb->df */
1169 for (j = 0; j < p2e->bbinfo.size; j++) {
1170 if (!TESTBIT(n->bb->df, j))
1171 continue;
1172
1173 if (TESTBIT(p2e->bbinfo.arr[j]->Aphi, i))
1174 continue;
1175
1176 y=p2e->bbinfo.arr[j];
1177 ntype = n->n_type;
1178 k = 0;
1179 /* Amount of predecessors for y */
1180 SLIST_FOREACH(cnode, &y->parents, cfgelem)
1181 k++;
1182 /* Construct phi(...)
1183 */
1184
1185 phifound=0;
1186
1187 SLIST_FOREACH(phi, &y->phi, phielem) {
1188 if (phi->tmpregno==i+defsites.low)
1189 phifound++;
1190 }
1191
1192 if (phifound==0) {
1193 if (b2debug)
1194 printf("Phi in %d(%d) (%p) for %d\n",
1195 y->dfnum,y->bbnum,y,i+defsites.low);
1196
1197 /* If no live in, no phi node needed */
1198 if (!TESTBIT(y->in,
1199 (i+defsites.low-p2e->ipp->ip_tmpnum+MAXREGS))) {
1200 if (b2debug)
1201 printf("tmp %d bb %d unused, no phi\n",
1202 i+defsites.low, y->bbnum);
1203 /* No live in */
1204 BITSET(p2e->bbinfo.arr[j]->Aphi, i);
1205 continue;
1206 }
1207
1208 phi = tmpcalloc(sizeof(struct phiinfo));
1209
1210 phi->tmpregno=i+defsites.low;
1211 phi->size=k;
1212 phi->n_type=ntype;
1213 phi->intmpregno=tmpcalloc(k*sizeof(int));
1214
1215 SLIST_INSERT_LAST(&y->phi,phi,phielem);
1216 } else {
1217 if (b2debug)
1218 printf("Phi already in %d for %d\n",y->dfnum,i+defsites.low);
1219 }
1220
1221 BITSET(p2e->bbinfo.arr[j]->Aphi, i);
1222 if (!TESTBIT(p2e->bbinfo.arr[j]->Aorig, i)) {
1223 pv = tmpalloc(sizeof(struct pvarinfo));
1224 pv->bb = y;
1225 pv->n_type=ntype;
1226 pv->next = defsites.arr[i];
1227 defsites.arr[i] = pv;
1228 }
1229 }
1230 }
1231 }
1232 }
1233
1234 /* Helper function for renamevar. */
1235 static void
1236 renamevarhelper(struct p2env *p2e,NODE *t,void *poplistarg)
1237 {
1238 SLIST_HEAD(, varstack) *poplist=poplistarg;
1239 int opty;
1240 int tempnr;
1241 int newtempnr;
1242 int x;
1243 struct varstack *stacke;
1244
1245 if (t->n_op == ASSIGN && t->n_left->n_op == TEMP) {
1246 renamevarhelper(p2e,t->n_right,poplist);
1247
1248 tempnr=regno(t->n_left)-defsites.low;
1249
1250 newtempnr=p2e->epp->ip_tmpnum++;
1251 regno(t->n_left)=newtempnr;
1252
1253 stacke=tmpcalloc(sizeof (struct varstack));
1254 stacke->tmpregno=newtempnr;
1255 SLIST_INSERT_FIRST(&defsites.stack[tempnr],stacke,varstackelem);
1256
1257 stacke=tmpcalloc(sizeof (struct varstack));
1258 stacke->tmpregno=tempnr;
1259 SLIST_INSERT_FIRST(poplist,stacke,varstackelem);
1260 } else {
1261 if (t->n_op == TEMP) {
1262 tempnr=regno(t)-defsites.low;
1263
1264 if (SLIST_FIRST(&defsites.stack[tempnr])!=NULL) {
1265 x=SLIST_FIRST(&defsites.stack[tempnr])->tmpregno;
1266 if (x == 0) {
1267 warner(Wuninitialized);
1268 x = defsites.low; /* can be anything */
1269 }
1270 regno(t)=x;
1271 }
1272 }
1273
1274 opty = optype(t->n_op);
1275
1276 if (opty != LTYPE)
1277 renamevarhelper(p2e, t->n_left,poplist);
1278 if (opty == BITYPE)
1279 renamevarhelper(p2e, t->n_right,poplist);
1280 }
1281 }
1282
1283
1284 void
1285 renamevar(struct p2env *p2e,struct basicblock *bb)
1286 {
1287 struct interpass *ip;
1288 int h,j;
1289 SLIST_HEAD(, varstack) poplist;
1290 struct varstack *stacke;
1291 struct cfgnode *cfgn2, *cn;
1292 int tmpregno,newtmpregno;
1293 struct phiinfo *phi;
1294
1295 SLIST_INIT(&poplist);
1296
1297 SLIST_FOREACH(phi,&bb->phi,phielem) {
1298 tmpregno=phi->tmpregno-defsites.low;
1299
1300 newtmpregno=p2e->epp->ip_tmpnum++;
1301 phi->newtmpregno=newtmpregno;
1302
1303 stacke=tmpcalloc(sizeof (struct varstack));
1304 stacke->tmpregno=newtmpregno;
1305 SLIST_INSERT_FIRST(&defsites.stack[tmpregno],stacke,varstackelem);
1306
1307 stacke=tmpcalloc(sizeof (struct varstack));
1308 stacke->tmpregno=tmpregno;
1309 SLIST_INSERT_FIRST(&poplist,stacke,varstackelem);
1310 }
1311
1312
1313 ip=bb->first;
1314
1315 while (1) {
1316 if ( ip->type == IP_NODE) {
1317 renamevarhelper(p2e,ip->ip_node,&poplist);
1318 }
1319
1320 if (ip==bb->last)
1321 break;
1322
1323 ip = DLIST_NEXT(ip, qelem);
1324 }
1325
1326 SLIST_FOREACH(cn, &bb->child, chld) {
1327 j=0;
1328
1329 SLIST_FOREACH(cfgn2, &cn->bblock->parents, cfgelem) {
1330 if (cfgn2->bblock->dfnum==bb->dfnum)
1331 break;
1332
1333 j++;
1334 }
1335
1336 SLIST_FOREACH(phi,&cn->bblock->phi,phielem) {
1337 phi->intmpregno[j]=SLIST_FIRST(&defsites.stack[phi->tmpregno-defsites.low])->tmpregno;
1338 }
1339 }
1340
1341 for (h = 1; h < p2e->bbinfo.size; h++) {
1342 if (!TESTBIT(bb->dfchildren, h))
1343 continue;
1344
1345 renamevar(p2e,p2e->bbinfo.arr[h]);
1346 }
1347
1348 SLIST_FOREACH(stacke,&poplist,varstackelem) {
1349 tmpregno=stacke->tmpregno;
1350
1351 defsites.stack[tmpregno].q_forw=defsites.stack[tmpregno].q_forw->varstackelem.q_forw;
1352 }
1353 }
1354
1355 static struct cpinfo {
1356 int ipb, asz;
1357 CONSZ *conp;
1358 char **namep;
1359 int *valp;
1360 } cpi;
1361
1362 static void
1363 getconst(NODE *p)
1364 {
1365 int ix;
1366
1367 if (p->n_op == ASSIGN && p->n_left->n_op == TEMP &&
1368 p->n_right->n_op == ICON) {
1369 ix = regno(p->n_left) - cpi.ipb;
1370 //printf("Found %d val " CONFMT " name \n", regno(p->n_left), getlval(p->n_right));
1371 if (cpi.valp[ix])
1372 comperr("TEMP %d found", regno(p->n_left));
1373 cpi.valp[ix] = 1;
1374 cpi.conp[ix] = getlval(p->n_right);
1375 cpi.namep[ix] = p->n_right->n_name;
1376 }
1377 if (optype(p->n_op) == BITYPE)
1378 getconst(p->n_right);
1379 if (optype(p->n_op) != LTYPE)
1380 getconst(p->n_left);
1381 }
1382
1383 static int replaced;
1384
1385 static void
1386 replconst(NODE *p)
1387 {
1388
1389 if (p->n_op == ASSIGN && p->n_left->n_op == TEMP) {
1390 replconst(p->n_right);
1391 } else if (p->n_op == TEMP) {
1392 if (cpi.valp[regno(p) - cpi.ipb]) {
1393 //printf("replacing %d %p\n", regno(p), p);
1394 p->n_op = ICON;
1395 setlval(p, cpi.conp[regno(p) - cpi.ipb]);
1396 p->n_name = cpi.namep[regno(p) - cpi.ipb];
1397 replaced = 1;
1398 regno(p) = 0;
1399 }
1400 } else {
1401 if (optype(p->n_op) == BITYPE)
1402 replconst(p->n_right);
1403 if (optype(p->n_op) != LTYPE)
1404 replconst(p->n_left);
1405 }
1406 }
1407
1408 /*
1409 * Simple version of constant propagation.
1410 * Should be replaced with conditional constant propagation someday.
1411 */
1412 void
1413 simple_cp(struct p2env *p2e)
1414 {
1415 struct interpass *ip;
1416
1417 cpi.ipb = p2e->ipp->ip_tmpnum;
1418 cpi.asz = p2e->epp->ip_tmpnum - cpi.ipb;
1419 cpi.conp = tmpcalloc(cpi.asz * sizeof(CONSZ));
1420 cpi.valp = tmpcalloc(cpi.asz * sizeof(int));
1421 cpi.namep = tmpcalloc(cpi.asz * sizeof(char *));
1422
1423 /* First loop over and get all constants */
1424 DLIST_FOREACH(ip, &p2e->ipole, qelem) {
1425 if (ip->type == IP_NODE)
1426 getconst(ip->ip_node);
1427 }
1428
1429 do {
1430 replaced = 0;
1431 /* Second loop replaces all uses of the constants */
1432 DLIST_FOREACH(ip, &p2e->ipole, qelem) {
1433 if (ip->type == IP_NODE)
1434 replconst(ip->ip_node);
1435 }
1436 } while (replaced);
1437 }
1438
1439 enum pred_type {
1440 pred_unknown = 0,
1441 pred_goto = 1,
1442 pred_cond = 2,
1443 pred_falltrough = 3,
1444 } ;
1445
1446 void
1447 removephi(struct p2env *p2e)
1448 {
1449 struct basicblock *bb,*bbparent;
1450 struct cfgnode *cfgn;
1451 struct phiinfo *phi;
1452 int i;
1453 struct interpass *ip;
1454 struct interpass *pip;
1455 TWORD n_type;
1456
1457 enum pred_type complex = pred_unknown ;
1458
1459 int label=0;
1460 int newlabel;
1461
1462 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1463 SLIST_FOREACH(phi,&bb->phi,phielem) {
1464 /* Look at only one, notice break at end */
1465 i=0;
1466
1467 SLIST_FOREACH(cfgn, &bb->parents, cfgelem) {
1468
1469 bbparent = cfgn->bblock; /* cfg parent basic block */
1470 pip = bbparent->last; /* last stmt in parent bb (goto, ...) */
1471
1472 complex = pred_unknown ;
1473 BDEBUG(("removephi: %p in %d",pip,bb->dfnum));
1474
1475 if (pip->type == IP_NODE && pip->ip_node->n_op == GOTO) {
1476 BDEBUG((" GOTO "));
1477 label = (int)getlval(pip->ip_node->n_left);
1478 complex = pred_goto ;
1479 } else if (pip->type == IP_NODE && pip->ip_node->n_op == CBRANCH) {
1480 BDEBUG((" CBRANCH "));
1481 label = (int)getlval(pip->ip_node->n_right);
1482
1483 /* Check if parent got us here via branch */
1484 if (bb == p2e->labinfo.arr[label - p2e->ipp->ip_lblnum])
1485 complex = pred_cond ;
1486 else
1487 complex = pred_falltrough ;
1488
1489 } else if (DLIST_PREV(bb, bbelem) == bbparent) {
1490 complex = pred_falltrough ;
1491 } else {
1492 /* PANIC */
1493 comperr("Assumption blown in rem-phi") ;
1494 }
1495
1496 BDEBUG((" Complex: %d ",complex)) ;
1497
1498 switch (complex) {
1499 case pred_goto:
1500 /* gotos can only go to this place. No bounce tab needed */
1501 SLIST_FOREACH(phi,&bb->phi,phielem) {
1502 if (phi->intmpregno[i]>0) {
1503 n_type=phi->n_type;
1504 ip = ipnode(mkbinode(ASSIGN,
1505 mktemp(phi->newtmpregno, n_type),
1506 mktemp(phi->intmpregno[i],n_type),
1507 n_type));
1508 BDEBUG(("(%p, %d -> %d) ", ip, phi->intmpregno[i], phi->newtmpregno));
1509
1510 DLIST_INSERT_BEFORE((bbparent->last), ip, qelem);
1511 }
1512 }
1513 break ;
1514 case pred_cond:
1515 /* Here, we need a jump pad */
1516 newlabel=getlab2();
1517
1518 ip = tmpalloc(sizeof(struct interpass));
1519 ip->type = IP_DEFLAB;
1520 /* Line number?? ip->lineno; */
1521 ip->ip_lbl = newlabel;
1522 DLIST_INSERT_BEFORE((bb->first), ip, qelem);
1523
1524 SLIST_FOREACH(phi,&bb->phi,phielem) {
1525 if (phi->intmpregno[i]>0) {
1526 n_type=phi->n_type;
1527 ip = ipnode(mkbinode(ASSIGN,
1528 mktemp(phi->newtmpregno, n_type),
1529 mktemp(phi->intmpregno[i],n_type),
1530 n_type));
1531
1532 BDEBUG(("(%p, %d -> %d) ", ip, phi->intmpregno[i], phi->newtmpregno));
1533 DLIST_INSERT_BEFORE((bb->first), ip, qelem);
1534 }
1535 }
1536 /* add a jump to us */
1537 ip = ipnode(mkunode(GOTO, mklnode(ICON, label, 0, INT), 0, INT));
1538 DLIST_INSERT_BEFORE((bb->first), ip, qelem);
1539 setlval(pip->ip_node->n_right,newlabel);
1540 if (!logop(pip->ip_node->n_left->n_op))
1541 comperr("SSA not logop");
1542 pip->ip_node->n_left->n_label=newlabel;
1543 break ;
1544 case pred_falltrough:
1545 if (bb->first->type == IP_DEFLAB) {
1546 label = bb->first->ip_lbl;
1547 BDEBUG(("falltrough label %d\n", label));
1548 } else {
1549 comperr("BBlock has no label?") ;
1550 }
1551
1552 /*
1553 * add a jump to us. We _will_ be, or already have, added code in between.
1554 * The code is created in the wrong order and switched at the insert, thus
1555 * comming out correctly
1556 */
1557
1558 ip = ipnode(mkunode(GOTO, mklnode(ICON, label, 0, INT), 0, INT));
1559 DLIST_INSERT_AFTER((bbparent->last), ip, qelem);
1560
1561 /* Add the code to the end, add a jump to us. */
1562 SLIST_FOREACH(phi,&bb->phi,phielem) {
1563 if (phi->intmpregno[i]>0) {
1564 n_type=phi->n_type;
1565 ip = ipnode(mkbinode(ASSIGN,
1566 mktemp(phi->newtmpregno, n_type),
1567 mktemp(phi->intmpregno[i],n_type),
1568 n_type));
1569
1570 BDEBUG(("(%p, %d -> %d) ", ip, phi->intmpregno[i], phi->newtmpregno));
1571 DLIST_INSERT_AFTER((bbparent->last), ip, qelem);
1572 }
1573 }
1574 break ;
1575 default:
1576 comperr("assumption blown, complex is %d\n", complex) ;
1577 }
1578 BDEBUG(("\n"));
1579 i++;
1580 }
1581 break;
1582 }
1583 }
1584 }
1585
1586
1587 /*
1588 * Remove unreachable nodes in the CFG.
1589 */
1590
1591 void
1592 remunreach(struct p2env *p2e)
1593 {
1594 struct basicblock *bb, *nbb;
1595 struct interpass *next, *ctree;
1596
1597 bb = DLIST_NEXT(&p2e->bblocks, bbelem);
1598 while (bb != &p2e->bblocks) {
1599 nbb = DLIST_NEXT(bb, bbelem);
1600
1601 /* Code with dfnum 0 is unreachable */
1602 if (bb->dfnum != 0) {
1603 bb = nbb;
1604 continue;
1605 }
1606
1607 /* Need the epilogue node for other parts of the
1608 compiler, set its label to 0 and backend will
1609 handle it. */
1610 if (bb->first->type == IP_EPILOG) {
1611 bb->first->ip_lbl = 0;
1612 bb = nbb;
1613 continue;
1614 }
1615
1616 next = bb->first;
1617 do {
1618 ctree = next;
1619 next = DLIST_NEXT(ctree, qelem);
1620
1621 if (ctree->type == IP_NODE)
1622 tfree(ctree->ip_node);
1623 DLIST_REMOVE(ctree, qelem);
1624 } while (ctree != bb->last);
1625
1626 DLIST_REMOVE(bb, bbelem);
1627 bb = nbb;
1628 }
1629 }
1630
1631 static void
1632 printip2(struct interpass *ip)
1633 {
1634 static char *foo[] = {
1635 0, "NODE", "PROLOG", "STKOFF", "EPILOG", "DEFLAB", "DEFNAM", "ASM" };
1636 struct interpass_prolog *ipplg, *epplg;
1637 int *l;
1638
1639 if (ip->type > MAXIP)
1640 printf("IP(%d) (%p): ", ip->type, ip);
1641 else
1642 printf("%s (%p): ", foo[ip->type], ip);
1643 switch (ip->type) {
1644 case IP_NODE: printf("\n");
1645 #ifdef PCC_DEBUG
1646 fwalk(ip->ip_node, e2print, 0); break;
1647 #endif
1648 case IP_PROLOG:
1649 ipplg = (struct interpass_prolog *)ip;
1650 printf("%s %s regs",
1651 ipplg->ipp_name, ipplg->ipp_vis ? "(local)" : "");
1652 printf(" autos %d mintemp %d minlbl %d\n",
1653 ipplg->ipp_autos, ipplg->ip_tmpnum, ipplg->ip_lblnum);
1654 break;
1655 case IP_EPILOG:
1656 epplg = (struct interpass_prolog *)ip;
1657 printf("%s %s regs",
1658 epplg->ipp_name, epplg->ipp_vis ? "(local)" : "");
1659 printf(" autos %d mintemp %d minlbl %d\ncgoto labels: ",
1660 epplg->ipp_autos, epplg->ip_tmpnum, epplg->ip_lblnum);
1661 for (l = epplg->ip_labels; *l; l++)
1662 printf("%d ", *l);
1663 printf("\n");
1664 break;
1665 case IP_DEFLAB: printf(LABFMT "\n", ip->ip_lbl); break;
1666 case IP_DEFNAM: printf("\n"); break;
1667 case IP_ASM: printf("%s\n", ip->ip_asm); break;
1668 default:
1669 break;
1670 }
1671 }
1672
1673 void
1674 printip(struct interpass *pole)
1675 {
1676 struct interpass *ip;
1677
1678 DLIST_FOREACH(ip, pole, qelem)
1679 printip2(ip);
1680 }
1681
1682 #ifdef PCC_DEBUG
1683 void flownodeprint(NODE *p,FILE *flowdiagramfile);
1684
1685 void
1686 flownodeprint(NODE *p,FILE *flowdiagramfile)
1687 {
1688 struct attr *ap;
1689 int opty;
1690 char *o;
1691
1692 fprintf(flowdiagramfile,"{");
1693
1694 o = opst[p->n_op];
1695
1696 while (*o != 0) {
1697 if (*o == '<' || *o == '>')
1698 fputc('\\', flowdiagramfile);
1699
1700 fputc(*o, flowdiagramfile);
1701 o++;
1702 }
1703
1704 switch( p->n_op ) {
1705 case REG:
1706 fprintf(flowdiagramfile, " %s", rnames[p->n_rval]);
1707 break;
1708
1709 case TEMP:
1710 fprintf(flowdiagramfile, " %d", regno(p));
1711 break;
1712
1713 case XASM:
1714 case XARG:
1715 fprintf(flowdiagramfile, " '%s'", p->n_name);
1716 break;
1717
1718 case ICON:
1719 case NAME:
1720 case OREG:
1721 fprintf(flowdiagramfile, " " );
1722 adrput(flowdiagramfile, p );
1723 break;
1724
1725 case STCALL:
1726 case USTCALL:
1727 case STARG:
1728 case STASG:
1729 ap = attr_find(p->n_ap, ATTR_P2STRUCT);
1730 fprintf(flowdiagramfile, " size=%d", ap->iarg(0));
1731 fprintf(flowdiagramfile, " align=%d", ap->iarg(1));
1732 break;
1733 }
1734
1735 opty = optype(p->n_op);
1736
1737 if (opty != LTYPE) {
1738 fprintf(flowdiagramfile, "| {");
1739
1740 flownodeprint(p->n_left, flowdiagramfile);
1741
1742 if (opty == BITYPE) {
1743 fprintf(flowdiagramfile, "|");
1744 flownodeprint(p->n_right, flowdiagramfile);
1745 }
1746 fprintf(flowdiagramfile, "}");
1747 }
1748
1749 fprintf(flowdiagramfile, "}");
1750 }
1751
1752 void
1753 printflowdiagram(struct p2env *p2e, char *type) {
1754 struct basicblock *bbb;
1755 struct cfgnode *cn;
1756 struct interpass *ip;
1757 struct interpass_prolog *plg;
1758 struct phiinfo *phi;
1759 char *name;
1760 char *filename;
1761 int filenamesize;
1762 char *ext = ".dot";
1763 FILE *flowdiagramfile;
1764
1765 if (!g2debug)
1766 return;
1767
1768 bbb = DLIST_NEXT(&p2e->bblocks, bbelem);
1769 ip = bbb->first;
1770
1771 if (ip->type != IP_PROLOG)
1772 return;
1773 plg = (struct interpass_prolog *)ip;
1774
1775 name = plg->ipp_name;
1776
1777 filenamesize = (int)(strlen(name)+1+strlen(type)+strlen(ext)+1);
1778 filename = tmpalloc(filenamesize);
1779 snprintf(filename, filenamesize,"%s-%s%s",name,type,ext);
1780
1781 flowdiagramfile = fopen(filename,"w");
1782
1783 fprintf(flowdiagramfile, "digraph {\n");
1784 fprintf(flowdiagramfile, "rankdir=LR\n");
1785
1786 DLIST_FOREACH(bbb, &p2e->bblocks, bbelem) {
1787 ip = bbb->first;
1788
1789 fprintf(flowdiagramfile, "bb%p [shape=record ",bbb);
1790
1791 if (ip->type == IP_PROLOG)
1792 fprintf(flowdiagramfile, "root ");
1793
1794 fprintf(flowdiagramfile, "label=\"");
1795
1796 SLIST_FOREACH(phi, &bbb->phi, phielem) {
1797 fprintf(flowdiagramfile, "Phi %d|", phi->tmpregno);
1798 }
1799
1800
1801 while (1) {
1802 switch (ip->type) {
1803 case IP_NODE:
1804 flownodeprint(ip->ip_node, flowdiagramfile);
1805 break;
1806
1807 case IP_DEFLAB:
1808 fprintf(flowdiagramfile, "Label: %d", ip->ip_lbl);
1809 break;
1810
1811 case IP_PROLOG:
1812 plg = (struct interpass_prolog *)ip;
1813 fprintf(flowdiagramfile, "%s %s", plg->ipp_name, type);
1814 break;
1815 }
1816
1817 fprintf(flowdiagramfile, "|");
1818 fprintf(flowdiagramfile, "|");
1819
1820 if (ip == bbb->last)
1821 break;
1822
1823 ip = DLIST_NEXT(ip, qelem);
1824 }
1825 fprintf(flowdiagramfile, "\"]\n");
1826
1827 SLIST_FOREACH(cn, &bbb->child, chld) {
1828 char *color = "black";
1829 struct interpass *pip = bbb->last;
1830
1831 if (pip->type == IP_NODE &&
1832 pip->ip_node->n_op == CBRANCH) {
1833 int label = (int)getlval(pip->ip_node->n_right);
1834
1835 if (cn->bblock ==
1836 p2e->labinfo.arr[label - p2e->ipp->ip_lblnum])
1837 color = "red";
1838 }
1839
1840 fprintf(flowdiagramfile, "bb%p -> bb%p [color=%s]\n",
1841 bbb, cn->bblock, color);
1842 }
1843 }
1844
1845 fprintf(flowdiagramfile, "}\n");
1846 fclose(flowdiagramfile);
1847 }
1848
1849 #endif
1850
1851 /* walk all the programm */
1852 void WalkAll(struct p2env* p2e, void (*f) (NODE*, void*), void* arg, int type)
1853 {
1854 struct interpass *ipole = &p2e->ipole;
1855 struct interpass *ip ;
1856 if (0 != type) {
1857 DLIST_FOREACH(ip, ipole, qelem) {
1858 if (ip->type == IP_NODE && ip->ip_node->n_op == type)
1859 walkf(ip->ip_node, f, arg) ;
1860 }
1861 } else {
1862 DLIST_FOREACH(ip, ipole, qelem) {
1863 if (ip->type == IP_NODE)
1864 walkf(ip->ip_node, f, arg) ;
1865 }
1866 }
1867 }
1868 #if 0
1869 static int is_goto_label(struct interpass*g, struct interpass* l)
1870 {
1871 if (!g || !l)
1872 return 0 ;
1873 if (g->type != IP_NODE) return 0 ;
1874 if (l->type != IP_DEFLAB) return 0 ;
1875 if (g->ip_node->n_op != GOTO) return 0 ;
1876 if (g->ip_node->n_left->n_lval != l->ip_lbl) return 0 ;
1877 return 1 ;
1878 }
1879 #endif
1880
1881 /*
1882 * iterate over the basic blocks.
1883 * In case a block has only one successor and that one has only one pred, and the link is a goto:
1884 * place the second one immediately behind the first one (in terms of nodes, means cut&resplice).
1885 * This should take care of a lot of jumps.
1886 * one problem: Cannot move the first or last basic block (prolog/epilog). This is taken care of by
1887 * moving block #1 to #2, not #2 to #1
1888 * More complex (on the back cooker;) : first coalesc the inner loops (L1 cache), then go from inside out.
1889 */
1890
1891 static unsigned long count_blocks(struct p2env* p2e)
1892 {
1893 struct basicblock* bb ;
1894 unsigned long count = 0 ;
1895 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1896 ++count ;
1897 }
1898 return count ;
1899 }
1900
1901 struct block_map {
1902 struct basicblock* block ;
1903 unsigned long index ;
1904 unsigned long thread ;
1905 } ;
1906
1907 static unsigned long map_blocks(struct p2env* p2e, struct block_map* map, unsigned long count)
1908 {
1909 struct basicblock* bb ;
1910 unsigned long indx = 0 ;
1911 int ignore = 2 ;
1912 unsigned long thread ;
1913 unsigned long changes ;
1914
1915 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1916 map[indx].block = bb ;
1917 map[indx].index = indx ;
1918
1919 /* ignore the first 2 labels, maybe up to 3 BBs */
1920 if (ignore) {
1921 if (bb->first->type == IP_DEFLAB)
1922 --ignore;
1923
1924 map[indx].thread = 1 ; /* these are "fixed" */
1925 } else
1926 map[indx].thread = 0 ;
1927
1928 indx++ ;
1929 }
1930
1931 thread = 1 ;
1932 do {
1933 changes = 0 ;
1934
1935 for (indx=0; indx < count; indx++) {
1936 /* find block without trace */
1937 if (map[indx].thread == 0) {
1938 /* do new thread */
1939 struct cfgnode *cn ;
1940 struct basicblock *block2 = 0;
1941 unsigned long i ;
1942 unsigned long added ;
1943
1944 BDEBUG (("new thread %ld at block %ld\n", thread, indx)) ;
1945
1946 bb = map[indx].block ;
1947 do {
1948 added = 0 ;
1949
1950 for (i=0; i < count; i++) {
1951 if (map[i].block == bb && map[i].thread == 0) {
1952 map[i].thread = thread ;
1953
1954 BDEBUG(("adding block %ld to trace %ld\n", i, thread)) ;
1955
1956 changes ++ ;
1957 added++ ;
1958
1959 /*
1960 * pick one from followers. For now (simple), pick the
1961 * one who is directly following us. If none of that exists,
1962 * this code picks the last one.
1963 */
1964
1965 SLIST_FOREACH(cn, &bb->child, chld) {
1966 block2=cn->bblock ;
1967 #if 1
1968 if (i+1 < count && map[i+1].block == block2)
1969 break ; /* pick that one */
1970 #else
1971 if (block2) break ;
1972 #endif
1973 }
1974
1975 if (block2)
1976 bb = block2 ;
1977 }
1978 }
1979 } while (added) ;
1980 thread++ ;
1981 }
1982 }
1983 } while (changes);
1984
1985 /* Last block is also a thread on it's own, and the highest one. */
1986 /*
1987 thread++ ;
1988 map[count-1].thread = thread ;
1989 */
1990 if (b2debug) {
1991 printf("Threads\n");
1992 for (indx=0; indx < count; indx++) {
1993 printf("Block #%ld (lbl %d) Thread %ld\n", indx, map[indx].block->first->ip_lbl, map[indx].thread);
1994 }
1995 }
1996 return thread ;
1997 }
1998
1999
2000 void TraceSchedule(struct p2env* p2e)
2001 {
2002 struct block_map* map ;
2003 unsigned long block_count = count_blocks(p2e);
2004 unsigned long i ;
2005 struct interpass *front, *back ;
2006
2007 map = tmpalloc(block_count * sizeof(struct block_map));
2008
2009 (void)map_blocks(p2e, map, block_count);
2010
2011 back = map[0].block->last ;
2012 for (i=1; i < block_count; i++) {
2013 /* collect the trace */
2014 unsigned long j ;
2015 unsigned long thread = map[i].thread ;
2016 if (thread) {
2017 BDEBUG(("Thread %ld\n", thread)) ;
2018 for (j=i; j < block_count; j++) {
2019 if (map[j].thread == thread) {
2020 front = map[j].block->first ;
2021
2022 BDEBUG(("Trace %ld, old BB %ld, next BB %ld\t",
2023 thread, i, j)) ;
2024 BDEBUG(("Label %d\n", front->ip_lbl)) ;
2025 DLIST_NEXT(back, qelem) = front ;
2026 DLIST_PREV(front, qelem) = back ;
2027 map[j].thread = 0 ; /* done */
2028 back = map[j].block->last ;
2029 DLIST_NEXT(back, qelem) = 0 ;
2030 }
2031 }
2032 }
2033 }
2034 DLIST_NEXT(back, qelem) = &(p2e->ipole) ;
2035 DLIST_PREV(&p2e->ipole, qelem) = back ;
2036 }
2037
2038 static void add_labels(struct p2env* p2e)
2039 {
2040 struct interpass *ipole = &p2e->ipole ;
2041 struct interpass *ip ;
2042
2043 DLIST_FOREACH(ip, ipole, qelem) {
2044 if (ip->type == IP_NODE && ip->ip_node->n_op == CBRANCH) {
2045 struct interpass *n = DLIST_NEXT(ip, qelem);
2046 if (n && n->type != IP_DEFLAB) {
2047 struct interpass* lab;
2048 int newlabel=getlab2() ;
2049
2050 BDEBUG(("add_label L%d\n", newlabel));
2051
2052 lab = tmpalloc(sizeof(struct interpass));
2053 lab->type = IP_DEFLAB;
2054 /* Line number?? ip->lineno; */
2055 lab->ip_lbl = newlabel;
2056 DLIST_INSERT_AFTER(ip, lab, qelem);
2057 }
2058 }
2059 }
2060 }
2061
2062 /* node map */
2063 #ifdef ENABLE_NEW
2064 struct node_map {
2065 NODE* node ; /* the node */
2066 unsigned int node_num ; /* node is equal to that one */
2067 unsigned int var_num ; /* node computes this variable */
2068 } ;
2069
2070 static unsigned long nodes_counter ;
2071 static void node_map_count_walker(NODE* n, void* x)
2072 {
2073 nodes_counter ++ ;
2074 }
2075
2076 static void do_cse(struct p2env* p2e)
2077 {
2078 nodes_counter = 0 ;
2079 WalkAll(p2e, node_map_count_walker, 0, 0) ;
2080 BDEBUG(("Found %ld nodes\n", nodes_counter)) ;
2081 }
2082 #endif
2083
2084 #define BITALLOC(ptr,all,sz) { \
2085 int sz__s = BIT2BYTE(sz); ptr = all(sz__s); memset(ptr, 0, sz__s); }
2086 #define VALIDREG(p) (p->n_op == REG && TESTBIT(validregs, regno(p)))
2087 #define XCHECK(x) if (x < 0 || x >= xbits) printf("x out of range %d\n", x)
2088 #define RUP(x) (((x)+NUMBITS-1)/NUMBITS)
2089 #define SETCOPY(t,f,i,n) for (i = 0; i < RUP(n); i++) t[i] = f[i]
2090 #define SETSET(t,f,i,n) for (i = 0; i < RUP(n); i++) t[i] |= f[i]
2091 #define SETCLEAR(t,f,i,n) for (i = 0; i < RUP(n); i++) t[i] &= ~f[i]
2092 #define SETCMP(v,t,f,i,n) for (i = 0; i < RUP(n); i++) \
2093 if (t[i] != f[i]) v = 1
2094
2095 static int xxx, xbits;
2096 /*
2097 * Set/clear long term liveness for regs and temps.
2098 */
2099 static void
2100 unionize(NODE *p, struct basicblock *bb, int suboff)
2101 {
2102 int o, ty;
2103
2104 if ((o = p->n_op) == TEMP || VALIDREG(p)) {
2105 int b = regno(p);
2106 if (o == TEMP)
2107 b = b - suboff + MAXREGS;
2108 XCHECK(b);
2109 BITSET(bb->gen, b);
2110 }
2111 if (asgop(o)) {
2112 if (p->n_left->n_op == TEMP || VALIDREG(p)) {
2113 int b = regno(p->n_left);
2114 if (p->n_left->n_op == TEMP)
2115 b = b - suboff + MAXREGS;
2116 XCHECK(b);
2117 BITCLEAR(bb->gen, b);
2118 BITSET(bb->killed, b);
2119 unionize(p->n_right, bb, suboff);
2120 return;
2121 }
2122 }
2123 ty = optype(o);
2124 if (ty != LTYPE)
2125 unionize(p->n_left, bb, suboff);
2126 if (ty == BITYPE)
2127 unionize(p->n_right, bb, suboff);
2128 }
2129
2130 /*
2131 * Found an extended assembler node, so growel out gen/killed nodes.
2132 */
2133 static void
2134 xasmionize(NODE *p, void *arg)
2135 {
2136 struct basicblock *bb = arg;
2137 int cw, b;
2138
2139 if (p->n_op == ICON && p->n_type == STRTY)
2140 return; /* dummy end marker */
2141
2142 cw = xasmcode(p->n_name);
2143 if (XASMVAL(cw) == 'n' || XASMVAL(cw) == 'm')
2144 return; /* no flow analysis */
2145 p = p->n_left;
2146
2147 if (XASMVAL(cw) == 'g' && p->n_op != TEMP && p->n_op != REG)
2148 return; /* no flow analysis */
2149
2150 b = regno(p);
2151 #if 0
2152 if (XASMVAL(cw) == 'r' && p->n_op == TEMP)
2153 addnotspill(b);
2154 #endif
2155 #define MKTOFF(r) ((r) - xxx)
2156 if (XASMISOUT(cw)) {
2157 if (p->n_op == TEMP) {
2158 BITCLEAR(bb->gen, MKTOFF(b));
2159 BITSET(bb->killed, MKTOFF(b));
2160 } else if (p->n_op == REG) {
2161 BITCLEAR(bb->gen, b);
2162 BITSET(bb->killed, b);
2163 } else
2164 uerror("bad xasm node type %d", p->n_op);
2165 }
2166 if (XASMISINP(cw)) {
2167 if (p->n_op == TEMP) {
2168 BITSET(bb->gen, MKTOFF(b));
2169 } else if (p->n_op == REG) {
2170 BITSET(bb->gen, b);
2171 } else if (optype(p->n_op) != LTYPE) {
2172 if (XASMVAL(cw) == 'r')
2173 uerror("couldn't find available register");
2174 else
2175 uerror("bad xasm node type2");
2176 }
2177 }
2178 }
2179
2180 /*
2181 * Do variable liveness analysis. Only analyze the long-lived
2182 * variables, and save the live-on-exit temporaries in a bit-field
2183 * at the end of each basic block. This bit-field is later used
2184 * when doing short-range liveness analysis in Build().
2185 */
2186 void
2187 liveanal(struct p2env *p2e)
2188 {
2189 struct basicblock *bb;
2190 struct interpass *ip;
2191 bittype *saved;
2192 int mintemp, again;
2193
2194 xbits = p2e->epp->ip_tmpnum - p2e->ipp->ip_tmpnum + MAXREGS;
2195 mintemp = p2e->ipp->ip_tmpnum;
2196
2197 /* Just fetch space for the temporaries from heap */
2198 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
2199 BITALLOC(bb->gen,tmpalloc,xbits);
2200 BITALLOC(bb->killed,tmpalloc,xbits);
2201 BITALLOC(bb->in,tmpalloc,xbits);
2202 BITALLOC(bb->out,tmpalloc,xbits);
2203 }
2204 BITALLOC(saved,tmpalloc,xbits);
2205
2206 xxx = mintemp;
2207 /*
2208 * generate the gen-killed sets for all basic blocks.
2209 */
2210 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
2211 for (ip = bb->last; ; ip = DLIST_PREV(ip, qelem)) {
2212 /* gen/killed is 'p', this node is 'n' */
2213 if (ip->type == IP_NODE) {
2214 if (ip->ip_node->n_op == XASM)
2215 flist(ip->ip_node->n_left,
2216 xasmionize, bb);
2217 else
2218 unionize(ip->ip_node, bb, mintemp);
2219 }
2220 if (ip == bb->first)
2221 break;
2222 }
2223 memcpy(bb->in, bb->gen, BIT2BYTE(xbits));
2224 #ifdef PCC_DEBUG
2225 #define PRTRG(x) printf("%d ", x < MAXREGS ? x : x + p2e->ipp->ip_tmpnum-MAXREGS)
2226 if (b2debug > 1) {
2227 int i;
2228
2229 printf("basic block %d\ngen: ", bb->bbnum);
2230 for (i = 0; i < xbits; i++)
2231 if (TESTBIT(bb->gen, i))
2232 PRTRG(i);
2233 printf("\nkilled: ");
2234 for (i = 0; i < xbits; i++)
2235 if (TESTBIT(bb->killed, i))
2236 PRTRG(i);
2237 printf("\n");
2238 }
2239 #endif
2240 }
2241 /* do liveness analysis on basic block level */
2242 do {
2243 struct cfgnode *cn;
2244 int j;
2245
2246 again = 0;
2247 /* XXX - loop should be in reversed execution-order */
2248 DLIST_FOREACH_REVERSE(bb, &p2e->bblocks, bbelem) {
2249 SETCOPY(saved, bb->out, j, xbits);
2250 SLIST_FOREACH(cn, &bb->child, chld) {
2251 SETSET(bb->out, cn->bblock->in, j, xbits);
2252 }
2253 SETCMP(again, saved, bb->out, j, xbits);
2254 SETCOPY(saved, bb->in, j, xbits);
2255 SETCOPY(bb->in, bb->out, j, xbits);
2256 SETCLEAR(bb->in, bb->killed, j, xbits);
2257 SETSET(bb->in, bb->gen, j, xbits);
2258 SETCMP(again, saved, bb->in, j, xbits);
2259 }
2260 } while (again);
2261
2262 #ifdef PCC_DEBUG
2263 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
2264 if (b2debug) {
2265 int i;
2266
2267 printf("all basic block %d\nin: ", bb->bbnum);
2268 for (i = 0; i < xbits; i++)
2269 if (TESTBIT(bb->in, i))
2270 PRTRG(i);
2271 printf("\nout: ");
2272 for (i = 0; i < xbits; i++)
2273 if (TESTBIT(bb->out, i))
2274 PRTRG(i);
2275 printf("\n");
2276 }
2277 }
2278 #endif
2279 }