Debian Codegraph @

mip/regs.c

raw symbols SCIP
1 /* $Id: regs.c,v 1.254 2021/10/09 12:46:09 ragge Exp $ */
2 /*
3 * Copyright (c) 2005 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 #include <string.h>
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h>
36 #endif
37 #include <stdlib.h>
38
39 #define MAXLOOP 20 /* Max number of allocation loops XXX 3 should be enough */
40
41 #ifndef MAX
42 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
43 #endif
44
45 /*
46 * New-style register allocator using graph coloring.
47 * The design is based on the George and Appel paper
48 * "Iterated Register Coalescing", ACM Transactions, No 3, May 1996.
49 */
50
51 #define BITALLOC(ptr,all,sz) { \
52 int sz__s = BIT2BYTE(sz); ptr = all(sz__s); memset(ptr, 0, sz__s); }
53
54 #undef COMPERR_PERM_MOVE
55 #ifdef PCC_DEBUG
56 #define RDEBUG(x) if (r2debug) printf x
57 #define RRDEBUG(x) if (r2debug > 1) printf x
58 #define RPRINTIP(x) if (r2debug) printip(x)
59 #define RDEBUGX(x) x
60 #define UDEBUG(x) if (u2debug) printf x
61 #define BDEBUG(x) if (b2debug) printf x
62 #define BBDEBUG(x) if (b2debug > 1) printf x
63 #else
64 #define RDEBUG(x)
65 #define RRDEBUG(x)
66 #define RPRINTIP(x)
67 #define RDEBUGX(x)
68 #define UDEBUG(x)
69 #define BDEBUG(x)
70 #define BBDEBUG(x)
71 #endif
72
73 #define VALIDREG(p) (p->n_op == REG && TESTBIT(validregs, regno(p)))
74
75 /*
76 * Data structure overview for this implementation of graph coloring:
77 *
78 * Each temporary (called "node") is described by the type REGW.
79 * Space for all nodes is allocated initially as an array, so
80 * the nodes can be can be referenced both by the node number and
81 * by pointer.
82 *
83 * All moves are represented by the type REGM, allocated when needed.
84 *
85 * The "live" set used during graph building is represented by a bitset.
86 *
87 * Interference edges are represented by struct AdjSet, hashed and linked
88 * from index into the edgehash array.
89 *
90 * A mapping from each node to the moves it is assiciated with is
91 * maintained by an array moveList which for each node number has a linked
92 * list of MOVL types, each pointing to a REGM.
93 *
94 * Adjacency list is maintained by the adjList array, indexed by the
95 * node number. Each adjList entry points to an ADJL type, and is a
96 * single-linked list for all adjacent nodes.
97 *
98 * degree, alias and color are integer arrays indexed by node number.
99 */
100
101 /*
102 * linked list of adjacent nodes.
103 */
104 typedef struct regw3 {
105 struct regw3 *r_next;
106 struct regw *a_temp;
107 } ADJL;
108
109 /*
110 * Structure describing a move.
111 */
112 typedef struct regm {
113 DLIST_ENTRY(regm) link;
114 struct regw *src, *dst;
115 int queue;
116 } REGM;
117
118 typedef struct movlink {
119 struct movlink *next;
120 REGM *regm;
121 } MOVL;
122
123 /*
124 * Structure describing a temporary.
125 */
126 typedef struct regw {
127 DLIST_ENTRY(regw) link;
128 ADJL *r_adjList; /* linked list of adjacent nodes */
129 int r_class; /* this nodes class */
130 int r_nclass[NUMCLASS+1]; /* count of adjacent classes */
131 struct regw *r_alias; /* aliased temporary */
132 int r_color; /* final node color */
133 struct regw *r_onlist; /* which work list this node belongs to */
134 MOVL *r_moveList; /* moves associated with this node */
135 int nodnum; /* Human-readable node number */
136 } REGW;
137
138 /*
139 * Worklists, a node is always on exactly one of these lists.
140 */
141 static REGW precolored, simplifyWorklist, freezeWorklist, spillWorklist,
142 spilledNodes, coalescedNodes, coloredNodes, selectStack;
143 static REGW initial, *nblock;
144 static void insnwalk(NODE *p);
145 #ifdef PCC_DEBUG
146 int use_regw;
147 #endif
148 int nodnum = 100;
149 int ntsz, stktemp;
150 #define SETNUM(x) (x)->nodnum = nodnum++
151 #define ASGNUM(x) (x)->nodnum
152
153 #define ALLNEEDS (NACOUNT|NBCOUNT|NCCOUNT|NDCOUNT|NECOUNT|NFCOUNT|NGCOUNT)
154
155 /* XXX */
156 REGW *ablock;
157
158 static int tempmin, tempmax, basetemp, xbits;
159 /*
160 * nsavregs is an array that matches the permregs array.
161 * Each entry in the array may have the values:
162 * 0 : register coalesced, just ignore.
163 * 1 : save register on stack
164 * If the entry is 0 but the resulting color differs from the
165 * corresponding permregs index, add moves.
166 * XXX - should be a bitfield!
167 */
168 static int *nsavregs, *ndontregs;
169
170 /*
171 * Return the REGW struct for a temporary.
172 * If first time touched, enter into list for existing vars.
173 * Only called from sucomp().
174 */
175 static REGW *
176 newblock(NODE *p)
177 {
178 REGW *nb;
179
180 #ifdef PCC_DEBUG
181 if (regno(p) < tempmin || regno(p) >= tempmax)
182 comperr("temp %p(%d) outside limits (%d-%d)",
183 p, regno(p), tempmin, tempmax);
184 #endif
185 nb = &nblock[regno(p)];
186 if (nb->link.q_forw == 0) {
187 DLIST_INSERT_AFTER(&initial, nb, link);
188 #ifdef PCC_DEBUG
189 ASGNUM(nb) = regno(p);
190 RDEBUG(("Adding longtime %d for tmp %d\n",
191 nb->nodnum, regno(p)));
192 #endif
193 }
194 if (nb->r_class == 0)
195 nb->r_class = gclass(p->n_type);
196 #ifdef PCC_DEBUG
197 RDEBUG(("newblock: p %p, node %d class %d\n",
198 p, nb->nodnum, nb->r_class));
199 #endif
200 return nb;
201 }
202
203 /*
204 * Count the number of registers needed to evaluate a tree.
205 * This is only done to find the evaluation order of the tree.
206 * While here, assign temp numbers to the registers that will
207 * be needed when the tree is evaluated.
208 *
209 * While traversing the tree, assign REGW nodes to the registers
210 * used by all instructions:
211 * - n_regw[0] is always set to the outgoing node. If the
212 * instruction is 2-op (addl r0,r1) then an implicit move
213 * is inserted just before the left (clobbered) operand.
214 * - if the instruction has needs then REGW nodes are
215 * allocated as n_regw[1] etc.
216 */
217 int
218 nsucomp(NODE *p)
219 {
220 struct optab *q;
221 int left, right;
222 int nreg, need, i, nxreg, o;
223 #ifdef NEWNEED
224 int cnregs[8], j;
225 char *w2;
226 #else
227 int nareg, nbreg, ncreg, ndreg, nereg, nfreg, ngreg;
228 #endif
229 REGW *w;
230
231 o = optype(p->n_op);
232
233 UDEBUG(("entering nsucomp, node %p\n", p));
234
235 if (TBLIDX(p->n_su) == 0) {
236 int a = 0, b;
237
238 p->n_regw = NULL;
239 if (o == LTYPE ) {
240 if (p->n_op == TEMP) {
241 p->n_regw = newblock(p);
242 a = 1;
243 } else if (p->n_op == REG)
244 p->n_regw = &ablock[regno(p)];
245 } else
246 a = nsucomp(p->n_left);
247 if (o == BITYPE) {
248 b = nsucomp(p->n_right);
249 if (b > a)
250 p->n_su |= DORIGHT;
251 a = MAX(a, b);
252 }
253 return a;
254 }
255
256 q = &table[TBLIDX(p->n_su)];
257
258 #ifdef NEWNEED
259 nxreg = 0;
260 for (i = 1; i < 8; i++) {
261 if ((w2 = hasneed2(q->needs, cNREG, i)))
262 cnregs[i] = w2[2];
263 else
264 cnregs[i] = 0;
265 nxreg += cnregs[i];
266 }
267 if ((w2 = hasneed(q->needs, cNTEMP))) {
268 if (ntsz < w2[1] * szty(p->n_type))
269 ntsz = w2[1] * szty(p->n_type);
270 }
271 #else
272 #define NNEEDS(a,b) ((q->needs & a)/b)
273 for (i = (q->needs & NACOUNT), nareg = 0; i; i -= NAREG)
274 nareg++;
275 for (i = (q->needs & NBCOUNT), nbreg = 0; i; i -= NBREG)
276 nbreg++;
277 for (i = (q->needs & NCCOUNT), ncreg = 0; i; i -= NCREG)
278 ncreg++;
279 for (i = (q->needs & NDCOUNT), ndreg = 0; i; i -= NDREG)
280 ndreg++;
281 for (i = (q->needs & NECOUNT), nereg = 0; i; i -= NEREG)
282 nereg++;
283 for (i = (q->needs & NFCOUNT), nfreg = 0; i; i -= NFREG)
284 nfreg++;
285 for (i = (q->needs & NGCOUNT), ngreg = 0; i; i -= NGREG)
286 ngreg++;
287
288 if (ntsz < NNEEDS(NTMASK, NTEMP) * szty(p->n_type))
289 ntsz = NNEEDS(NTMASK, NTEMP) * szty(p->n_type);
290 nxreg = nareg + nbreg + ncreg + ndreg + nereg + nfreg + ngreg;
291 #endif
292
293 nreg = nxreg;
294 if (callop(p->n_op))
295 nreg = MAX(fregs, nreg);
296
297 if (o == BITYPE) {
298 right = nsucomp(p->n_right);
299 } else
300 right = 0;
301
302 if (o != LTYPE)
303 left = nsucomp(p->n_left);
304 else
305 left = 0;
306
307 UDEBUG(("node %p left %d right %d\n", p, left, right));
308
309 if (o == BITYPE) {
310 /* Two children */
311 if (right == left) {
312 need = left + MAX(nreg, 1);
313 } else {
314 need = MAX(right, left);
315 need = MAX(need, nreg);
316 }
317 if (setorder(p) == 0) {
318 /* XXX - should take care of overlapping needs */
319 if (right > left) {
320 p->n_su |= DORIGHT;
321 } else if (right == left) {
322 #if 0
323 /* XXX - need something more clever when large left trees */
324 /* A favor to 2-operand architectures */
325 if ((q->rewrite & RRIGHT) == 0)
326 p->n_su |= DORIGHT;
327 #endif
328 }
329 }
330 } else if (o != LTYPE) {
331 /* One child */
332 need = MAX(right, left) + nreg;
333 } else
334 need = nreg;
335
336 if (p->n_op == TEMP)
337 (void)newblock(p);
338
339 if (TCLASS(p->n_su) == 0 && nxreg == 0) {
340 UDEBUG(("node %p no class\n", p));
341 p->n_regw = NULL; /* may be set earlier */
342 return need;
343 }
344
345 #ifdef PCC_DEBUG
346 #define ADCL(n, cl) \
347 for (i = 0; i < n; i++, w++) { w->r_class = cl; \
348 DLIST_INSERT_BEFORE(&initial, w, link); SETNUM(w); \
349 UDEBUG(("Adding " #n " %d\n", w->nodnum)); \
350 }
351 #else
352 #define ADCL(n, cl) \
353 for (i = 0; i < n; i++, w++) { w->r_class = cl; \
354 DLIST_INSERT_BEFORE(&initial, w, link); SETNUM(w); \
355 }
356 #endif
357
358 UDEBUG(("node %p numregs %d\n", p, nxreg+1));
359 w = p->n_regw = tmpalloc(sizeof(REGW) * (nxreg+1));
360 memset(w, 0, sizeof(REGW) * (nxreg+1));
361
362 w->r_class = TCLASS(p->n_su);
363 if (w->r_class == 0)
364 w->r_class = gclass(p->n_type);
365 w->r_nclass[0] = o == LTYPE; /* XXX store leaf info here */
366 SETNUM(w);
367 if (w->r_class)
368 DLIST_INSERT_BEFORE(&initial, w, link);
369 #ifdef PCC_DEBUG
370 UDEBUG(("Adding short %d class %d\n", w->nodnum, w->r_class));
371 #endif
372 w++;
373 #ifdef NEWNEED
374 for (j = 1; j < 8; j++) {
375 ADCL(cnregs[j], j);
376 }
377 #else
378 ADCL(nareg, CLASSA);
379 ADCL(nbreg, CLASSB);
380 ADCL(ncreg, CLASSC);
381 ADCL(ndreg, CLASSD);
382 ADCL(nereg, CLASSE);
383 ADCL(nfreg, CLASSF);
384 ADCL(ngreg, CLASSG);
385 #endif
386
387 if (q->rewrite & RESC1) {
388 w = p->n_regw + 1;
389 w->r_class = -1;
390 DLIST_REMOVE(w,link);
391 } else if (q->rewrite & RESC2) {
392 w = p->n_regw + 2;
393 w->r_class = -1;
394 DLIST_REMOVE(w,link);
395 } else if (q->rewrite & RESC3) {
396 w = p->n_regw + 3;
397 w->r_class = -1;
398 DLIST_REMOVE(w,link);
399 }
400
401 UDEBUG(("node %p return regs %d\n", p, need));
402
403 return need;
404 }
405
406 #define CLASS(x) (x)->r_class
407 #define NCLASS(x,c) (x)->r_nclass[c]
408 #define ADJLIST(x) (x)->r_adjList
409 #define ALIAS(x) (x)->r_alias
410 #define ONLIST(x) (x)->r_onlist
411 #define MOVELIST(x) (x)->r_moveList
412 #define COLOR(x) (x)->r_color
413
414 static bittype *live;
415
416 #define PUSHWLIST(w, l) DLIST_INSERT_AFTER(&l, w, link); w->r_onlist = &l
417 #define POPWLIST(l) popwlist(&l);
418 #define DELWLIST(w) DLIST_REMOVE(w, link)
419 #define WLISTEMPTY(h) DLIST_ISEMPTY(&h,link)
420 #define PUSHMLIST(w, l, q) DLIST_INSERT_AFTER(&l, w, link); w->queue = q
421 #define POPMLIST(l) popmlist(&l);
422
423 #define trivially_colorable(x) \
424 trivially_colorable_p((x)->r_class, (x)->r_nclass)
425 /*
426 * Determine if a node is trivially colorable ("degree < K").
427 * This implementation is a dumb one, without considering speed.
428 */
429 static int
430 trivially_colorable_p(int c, int *n)
431 {
432 int r[NUMCLASS+1];
433 int i;
434
435 for (i = 1; i < NUMCLASS+1; i++)
436 r[i] = n[i] < regK[i] ? n[i] : regK[i];
437
438 #if 0
439 /* add the exclusion nodes. */
440 /* XXX can we do someything smart here? */
441 /* worst-case for exclusion nodes are better than the `worst-case' */
442 for (; excl; excl >>= 1)
443 if (excl & 1)
444 r[c]++;
445 #endif
446
447 i = COLORMAP(c, r);
448 if (i < 0 || i > 1)
449 comperr("trivially_colorable_p");
450 RRDEBUG(("trivially_colorable_p: n[1] %d n[2] %d n[3] %d n[4] "
451 "%d for class %d, triv %d\n", n[1], n[2], n[3], n[4], c, i));
452 return i;
453 }
454
455 #ifdef NEWNEED
456 int
457 ncnt(char *w)
458 {
459 int i = 0;
460 if (w)
461 while ((w = hasneed(w, cNREG)))
462 i += w[2], w += 3;
463 return i;
464 }
465 #else
466 int
467 ncnt(int needs)
468 {
469 int i = 0;
470
471 while (needs & NACOUNT)
472 needs -= NAREG, i++;
473 while (needs & NBCOUNT)
474 needs -= NBREG, i++;
475 while (needs & NCCOUNT)
476 needs -= NCREG, i++;
477 while (needs & NDCOUNT)
478 needs -= NDREG, i++;
479 while (needs & NECOUNT)
480 needs -= NEREG, i++;
481 while (needs & NFCOUNT)
482 needs -= NFREG, i++;
483 while (needs & NGCOUNT)
484 needs -= NGREG, i++;
485 return i;
486 }
487 #endif
488
489 static REGW *
490 popwlist(REGW *l)
491 {
492 REGW *w = DLIST_NEXT(l, link);
493
494 DLIST_REMOVE(w, link);
495 w->r_onlist = NULL;
496 return w;
497 }
498
499 /*
500 * Move lists, a move node is always on only one list.
501 */
502 static REGM coalescedMoves, constrainedMoves, frozenMoves,
503 worklistMoves, activeMoves;
504 enum { COAL, CONSTR, FROZEN, WLIST, ACTIVE };
505
506 static REGM *
507 popmlist(REGM *l)
508 {
509 REGM *w = DLIST_NEXT(l, link);
510
511 DLIST_REMOVE(w, link);
512 return w;
513 }
514
515 /*
516 * About data structures used in liveness analysis:
517 *
518 * The temporaries generated in pass1 are numbered between tempmin and
519 * tempmax. Temporaries generated in pass2 are numbered above tempmax,
520 * so they are sequentially numbered.
521 *
522 * Bitfields are used for liveness. Bit arrays are allocated on the
523 * heap for the "live" variable and on the stack for the in, out, gen
524 * and killed variables. Therefore, for a temp number, the bit number must
525 * be biased with tempmin.
526 *
527 * There may be an idea to use a different data structure to store
528 * pass2 allocated temporaries, because they are very sparse.
529 */
530
531 #ifdef PCC_DEBUG
532 static void
533 LIVEADD(int x)
534 {
535 RDEBUG(("Liveadd: %d\n", x));
536 if (x >= MAXREGS && (x < tempmin || x >= tempmax))
537 comperr("LIVEADD: out of range");
538 if (x < MAXREGS) {
539 BITSET(live, x);
540 } else
541 BITSET(live, (x-tempmin+MAXREGS));
542 }
543
544 static void
545 LIVEDEL(int x)
546 {
547 RDEBUG(("Livedel: %d\n", x));
548
549 if (x >= MAXREGS && (x < tempmin || x >= tempmax))
550 comperr("LIVEDEL: out of range");
551 if (x < MAXREGS) {
552 BITCLEAR(live, x);
553 } else
554 BITCLEAR(live, (x-tempmin+MAXREGS));
555 }
556 #else
557 #define LIVEADD(x) \
558 (x >= MAXREGS ? BITSET(live, (x-tempmin+MAXREGS)) : BITSET(live, x))
559 #define LIVEDEL(x) \
560 (x >= MAXREGS ? BITCLEAR(live, (x-tempmin+MAXREGS)) : BITCLEAR(live, x))
561 #endif
562
563 static struct lives {
564 DLIST_ENTRY(lives) link;
565 REGW *var;
566 } lused, lunused;
567
568 static void
569 LIVEADDR(REGW *x)
570 {
571 struct lives *l;
572
573 #ifdef PCC_DEBUG
574 RDEBUG(("LIVEADDR: %d\n", x->nodnum));
575 DLIST_FOREACH(l, &lused, link)
576 if (l->var == x)
577 return;
578 #if 0
579 comperr("LIVEADDR: multiple %d", ASGNUM(x));
580 #endif
581 #endif
582 if (!DLIST_ISEMPTY(&lunused, link)) {
583 l = DLIST_NEXT(&lunused, link);
584 DLIST_REMOVE(l, link);
585 } else
586 l = tmpalloc(sizeof(struct lives));
587
588 l->var = x;
589 DLIST_INSERT_AFTER(&lused, l, link);
590 }
591
592 static void
593 LIVEDELR(REGW *x)
594 {
595 struct lives *l;
596
597 #ifdef PCC_DEBUG
598 RDEBUG(("LIVEDELR: %d\n", x->nodnum));
599 #endif
600 DLIST_FOREACH(l, &lused, link) {
601 if (l->var != x)
602 continue;
603 DLIST_REMOVE(l, link);
604 DLIST_INSERT_AFTER(&lunused, l, link);
605 return;
606 }
607 #if 0
608 comperr("LIVEDELR: %p not found", x);
609 #endif
610 }
611
612 #define MOVELISTADD(t, p) movelistadd(t, p)
613 #define WORKLISTMOVEADD(s,d) worklistmoveadd(s,d)
614
615 static void
616 movelistadd(REGW *t, REGM *p)
617 {
618 MOVL *w = tmpalloc(sizeof(MOVL));
619
620 w->regm = p;
621 w->next = t->r_moveList;
622 t->r_moveList = w;
623 }
624
625 static REGM *
626 worklistmoveadd(REGW *src, REGW *dst)
627 {
628 REGM *w = tmpalloc(sizeof(REGM));
629
630 DLIST_INSERT_AFTER(&worklistMoves, w, link);
631 w->src = src;
632 w->dst = dst;
633 w->queue = WLIST;
634 return w;
635 }
636
637 #define HASHSZ 16384
638 struct AdjSet {
639 struct AdjSet *next;
640 REGW *u, *v;
641 } *edgehash[HASHSZ];
642
643 /* Check if a node pair is adjacent */
644 static int
645 adjSet(REGW *u, REGW *v)
646 {
647 struct AdjSet *w;
648 REGW *t;
649
650 if (ONLIST(u) == &precolored) {
651 ADJL *a = ADJLIST(v);
652 /*
653 * Check if any of the registers that have edges against v
654 * alias to u.
655 */
656 for (; a; a = a->r_next) {
657 if (ONLIST(a->a_temp) != &precolored)
658 continue;
659 t = a->a_temp;
660 if (interferes(t - ablock, u - ablock))
661 return 1;
662 }
663 }
664
665 w = edgehash[(u->nodnum+v->nodnum)& (HASHSZ-1)];
666
667 for (; w; w = w->next) {
668 if ((u == w->u && v == w->v) || (u == w->v && v == w->u))
669 return 1;
670 }
671 return 0;
672 }
673
674 /* Add a pair to adjset. No check for dups */
675 static int
676 adjSetadd(REGW *u, REGW *v)
677 {
678 struct AdjSet *w;
679 int x;
680
681 x = (u->nodnum+v->nodnum)& (HASHSZ-1);
682 for (w = edgehash[x]; w; w = w->next)
683 if ((u == w->u && v == w->v) || (u == w->v && v == w->u))
684 return 1;
685
686 w = tmpalloc(sizeof(struct AdjSet));
687 w->u = u, w->v = v;
688 w->next = edgehash[x];
689 edgehash[x] = w;
690 return 0;
691 }
692
693 /*
694 * Add an interference edge between two nodes.
695 */
696 static void
697 AddEdge(REGW *u, REGW *v)
698 {
699 ADJL *x;
700
701 #ifdef PCC_DEBUG
702 RRDEBUG(("AddEdge: u %d v %d\n", ASGNUM(u), ASGNUM(v)));
703
704 #if 0
705 if (ASGNUM(u) == 0)
706 comperr("AddEdge 0");
707 #endif
708 if (CLASS(u) == 0 || CLASS(v) == 0)
709 comperr("AddEdge class == 0 (%d=%d, %d=%d)",
710 CLASS(u), ASGNUM(u), CLASS(v), ASGNUM(v));
711 #endif
712
713 if (u == v)
714 return;
715 if (adjSetadd(u, v))
716 return;
717
718 #if 0
719 if (ONLIST(u) == &precolored || ONLIST(v) == &precolored)
720 comperr("precolored node in AddEdge");
721 #endif
722
723 if (ONLIST(u) != &precolored) {
724 x = tmpalloc(sizeof(ADJL));
725 x->a_temp = v;
726 x->r_next = u->r_adjList;
727 u->r_adjList = x;
728 NCLASS(u, CLASS(v))++;
729 }
730
731 if (ONLIST(v) != &precolored) {
732 x = tmpalloc(sizeof(ADJL));
733 x->a_temp = u;
734 x->r_next = v->r_adjList;
735 v->r_adjList = x;
736 NCLASS(v, CLASS(u))++;
737 }
738
739 #if 0
740 RDEBUG(("AddEdge: u %d(d %d) v %d(d %d)\n", u, DEGREE(u), v, DEGREE(v)));
741 #endif
742 }
743
744 static int
745 MoveRelated(REGW *n)
746 {
747 MOVL *l;
748 REGM *w;
749
750 for (l = MOVELIST(n); l; l = l->next) {
751 w = l->regm;
752 if (w->queue == ACTIVE || w->queue == WLIST)
753 return 1;
754 }
755 return 0;
756 }
757
758 static void
759 MkWorklist(void)
760 {
761 REGW *w;
762
763 RDEBUGX(int s=0);
764 RDEBUGX(int f=0);
765 RDEBUGX(int d=0);
766
767 DLIST_INIT(&precolored, link);
768 DLIST_INIT(&simplifyWorklist, link);
769 DLIST_INIT(&freezeWorklist, link);
770 DLIST_INIT(&spillWorklist, link);
771 DLIST_INIT(&spilledNodes, link);
772 DLIST_INIT(&coalescedNodes, link);
773 DLIST_INIT(&coloredNodes, link);
774 DLIST_INIT(&selectStack, link);
775
776 /*
777 * Remove all nodes from the initial list and put them on
778 * one of the worklists.
779 */
780 while (!DLIST_ISEMPTY(&initial, link)) {
781 w = DLIST_NEXT(&initial, link);
782 DLIST_REMOVE(w, link);
783 if (!trivially_colorable(w)) {
784 PUSHWLIST(w, spillWorklist);
785 RDEBUGX(s++);
786 } else if (MoveRelated(w)) {
787 PUSHWLIST(w, freezeWorklist);
788 RDEBUGX(f++);
789 } else {
790 PUSHWLIST(w, simplifyWorklist);
791 RDEBUGX(d++);
792 }
793 }
794 RDEBUG(("MkWorklist: spill %d freeze %d simplify %d\n", s,f,d));
795 }
796
797 static void
798 addalledges(REGW *e)
799 {
800 int i, j, k;
801 struct lives *l;
802
803 #ifdef PCC_DEBUG
804 RDEBUG(("addalledges for %d\n", e->nodnum));
805 #endif
806
807 if (e->r_class == -1)
808 return; /* unused */
809
810 if (ONLIST(e) != &precolored) {
811 for (i = 0; ndontregs[i] >= 0; i++)
812 AddEdge(e, &ablock[ndontregs[i]]);
813 }
814
815 /* First add to long-lived temps and hard regs */
816 RDEBUG(("addalledges longlived "));
817 for (i = 0; i < xbits; i += NUMBITS) {
818 if ((k = (int)live[i/NUMBITS])) {
819 while (k) {
820 j = ffs(k)-1;
821 if (i+j < MAXREGS)
822 AddEdge(&ablock[i+j], e);
823 else
824 AddEdge(&nblock[i+j+tempmin-MAXREGS],e);
825 RRDEBUG(("%d ", i+j+tempmin));
826 k &= ~(1 << j);
827 }
828 }
829 #if NUMBITS > 32 /* XXX hack for LP64 */
830 k = (int)(live[i/NUMBITS] >> 32);
831 while (k) {
832 j = ffs(k)-1;
833 if (i+j+32 < MAXREGS)
834 AddEdge(&ablock[i+j+32], e);
835 else
836 AddEdge(&nblock[i+j+tempmin-MAXREGS+32], e);
837 RRDEBUG(("%d ", i+j+tempmin+32));
838 k &= ~(1 << j);
839 }
840 #endif
841 }
842 RDEBUG(("done\n"));
843 /* short-lived temps */
844 RDEBUG(("addalledges shortlived "));
845 DLIST_FOREACH(l, &lused, link) {
846 #ifdef PCC_DEBUG
847 RRDEBUG(("%d ", ASGNUM(l->var)));
848 #endif
849 AddEdge(l->var, e);
850 }
851 RDEBUG(("done\n"));
852 }
853
854 /*
855 * Add a move edge between def and use.
856 */
857 static void
858 moveadd(REGW *def, REGW *use)
859 {
860 REGM *r;
861 MOVL *w;
862
863 if (def == use)
864 return; /* no move to itself XXX - ``shouldn't happen'' */
865 #ifdef PCC_DEBUG
866 RDEBUG(("moveadd: def %d use %d\n", ASGNUM(def), ASGNUM(use)));
867 #endif
868
869 /*
870 * Check if we are already on move list.
871 * XXX How can that happen ???
872 */
873 for (w = MOVELIST(def); w; w = w->next) {
874 if ((w->regm->src == def && w->regm->dst == use) ||
875 (w->regm->src == use && w->regm->dst == def))
876 return; /* already there XXX reverse? */
877 }
878
879 r = WORKLISTMOVEADD(use, def);
880 MOVELISTADD(def, r);
881 MOVELISTADD(use, r);
882 }
883
884 /*
885 * Traverse arguments backwards.
886 * XXX - can this be tricked in some other way?
887 */
888 static void
889 argswalk(NODE *p)
890 {
891
892 if (p->n_op == CM) {
893 argswalk(p->n_left);
894 insnwalk(p->n_right);
895 } else
896 insnwalk(p);
897 }
898
899 /*
900 * Add to (or remove from) live set variables that must not
901 * be clobbered when traversing down on the other leg for
902 * a BITYPE node.
903 */
904 static void
905 setlive(NODE *p, int set, REGW *rv)
906 {
907 if (rv != NULL) {
908 if (rv->nodnum < MAXREGS &&
909 TESTBIT(validregs, rv->nodnum) == 0)
910 return;
911 set ? LIVEADDR(rv) : LIVEDELR(rv);
912 return;
913 }
914
915 if (p->n_regw != NULL) {
916 if (p->n_regw->nodnum < MAXREGS &&
917 TESTBIT(validregs, p->n_regw->nodnum) == 0)
918 return;
919 set ? LIVEADDR(p->n_regw) : LIVEDELR(p->n_regw);
920 return;
921 }
922
923 switch (optype(p->n_op)) {
924 case LTYPE:
925 if (p->n_op == TEMP || VALIDREG(p))
926 set ? LIVEADD(regno(p)) : LIVEDEL(regno(p));
927 break;
928 case BITYPE:
929 setlive(p->n_right, set, rv);
930 /* FALLTHROUGH */
931 case UTYPE:
932 setlive(p->n_left, set, rv);
933 break;
934 }
935 }
936
937 /*
938 * Add edges for temporary w against all temporaries that may be
939 * used simultaneously (like index registers).
940 */
941 static void
942 addedge_r(NODE *p, REGW *w)
943 {
944 RRDEBUG(("addedge_r: node %p regw %p\n", p, w));
945
946 if (p->n_regw != NULL) {
947 if (p->n_regw->nodnum < MAXREGS &&
948 TESTBIT(validregs, p->n_regw->nodnum) == 0)
949 return;
950 AddEdge(p->n_regw, w);
951 return;
952 }
953
954 if (optype(p->n_op) == BITYPE)
955 addedge_r(p->n_right, w);
956 if (optype(p->n_op) != LTYPE)
957 addedge_r(p->n_left, w);
958 }
959
960 /*
961 * delete early clobber liveness. Only interesting on regs.
962 */
963 static void
964 delcl(NODE *p)
965 {
966 int cw;
967
968 if (p->n_op == ICON && p->n_type == STRTY)
969 return;
970 cw = xasmcode(p->n_name);
971 if ((cw & XASMCONSTR) == 0 || !XASMISOUT(cw))
972 return;
973 if (XASMVAL(cw) != 'r')
974 return;
975 LIVEDEL(regno(p->n_left));
976 }
977
978 /*
979 * add/del parameter from live set.
980 */
981 static void
982 setxarg(NODE *p)
983 {
984 int i, ut = 0, in = 0;
985 REGW *rw;
986 int c, cw;
987
988 if (p->n_op == ICON && p->n_type == STRTY)
989 return;
990
991 RDEBUG(("setxarg %p %s\n", p, p->n_name));
992 cw = xasmcode(p->n_name);
993 if (XASMISINP(cw))
994 in = 1;
995 if (XASMISOUT(cw) && !(cw & XASMCONSTR))
996 ut = 1;
997
998 c = XASMVAL(cw);
999
1000 #ifdef MYSETXARG
1001 MYSETXARG;
1002 #endif
1003
1004 switch (c) {
1005 case 'm':
1006 case 'g':
1007 /* must find all TEMPs/REGs and set them live */
1008 if (p->n_left->n_op != REG && p->n_left->n_op != TEMP) {
1009 insnwalk(p->n_left);
1010 break;
1011 }
1012 /* FALLTHROUGH */
1013 case 'r':
1014 i = regno(p->n_left);
1015 rw = p->n_left->n_op == REG ? ablock : nblock;
1016 if (ut) {
1017 LIVEDEL(i);
1018 }
1019 if (in) {
1020 LIVEADD(i);
1021 }
1022 addalledges(&rw[i]);
1023 break;
1024
1025 case 'i':
1026 case 'n':
1027 break;
1028 default:
1029 comperr("bad ixarg %s", p->n_name);
1030 }
1031 #ifdef MYSETXARG
1032 MYSETXARG;
1033 #endif
1034 }
1035
1036 /*
1037 * Do the in-tree part of liveness analysis. (the difficult part)
1038 *
1039 * Walk down the tree in reversed-evaluation order (backwards).
1040 * The moves and edges inserted and evaluation order for
1041 * instructions when code is emitted is described here, hence
1042 * this code runs the same but backwards.
1043 *
1044 * 2-op reclaim LEFT: eval L, move to DEST, eval R.
1045 * moveadd L,DEST; addedge DEST,R
1046 * 2-op reclaim LEFT DORIGHT: eval R, eval L, move to DEST.
1047 * moveadd L,DEST; addedge DEST,R; addedge L,R
1048 * 2-op reclaim RIGHT; eval L, eval R, move to DEST.
1049 * moveadd R,DEST; addedge DEST,L; addedge L,R
1050 * 2-op reclaim RIGHT DORIGHT: eval R, move to DEST, eval L.
1051 * moveadd R,DEST; addedge DEST,L
1052 * 3-op: eval L, eval R
1053 * addedge L,R
1054 * 3-op DORIGHT: eval R, eval L
1055 * addedge L,R
1056 *
1057 * Instructions with special needs are handled just like these variants,
1058 * with the exception of extra added moves and edges.
1059 * Moves to special regs are scheduled after the evaluation of both legs.
1060 */
1061
1062 static void
1063 insnwalk(NODE *p)
1064 {
1065 int o = p->n_op;
1066 struct optab *q = &table[TBLIDX(p->n_su)];
1067 REGW *lr, *rr, *rv, *r, *rrv, *lrv;
1068 NODE *lp, *rp;
1069 int i, n;
1070 #ifdef NEWNEED
1071 char *w;
1072 #endif
1073
1074 RDEBUG(("insnwalk %p\n", p));
1075
1076 rv = p->n_regw;
1077
1078 rrv = lrv = NULL;
1079 if (p->n_op == ASSIGN &&
1080 (p->n_left->n_op == TEMP || VALIDREG(p->n_left))) {
1081 lr = p->n_left->n_op == TEMP ? nblock : ablock;
1082 i = regno(p->n_left);
1083 LIVEDEL(i); /* remove assigned temp from live set */
1084 addalledges(&lr[i]);
1085 }
1086
1087 /* Add edges for the result of this node */
1088 if (rv && (q->visit & INREGS || o == TEMP || VALIDREG(p)))
1089 addalledges(rv);
1090
1091 /* special handling of CALL operators */
1092 if (callop(o)) {
1093 if (rv)
1094 moveadd(rv, &ablock[RETREG(p->n_type)]);
1095 for (i = 0; tempregs[i] >= 0; i++)
1096 addalledges(&ablock[tempregs[i]]);
1097 }
1098
1099 /* for special return value registers add moves */
1100 #ifdef NEWNEED
1101 if ((w = hasneed(q->needs, cNRES)) && p->n_regw != NULL) {
1102 n = w[1];
1103 #else
1104 if ((q->needs & NSPECIAL) && (n = rspecial(q, NRES)) >= 0 &&
1105 p->n_regw != NULL) {
1106 #endif
1107 rv = &ablock[n];
1108 moveadd(p->n_regw, rv);
1109 }
1110
1111 /* Check leaves for results in registers */
1112 lr = optype(o) != LTYPE ? p->n_left->n_regw : NULL;
1113 lp = optype(o) != LTYPE ? p->n_left : NULL;
1114 rr = optype(o) == BITYPE ? p->n_right->n_regw : NULL;
1115 rp = optype(o) == BITYPE ? p->n_right : NULL;
1116
1117 #ifdef NEWNEED
1118 /* simple needs */
1119 n = ncnt(q->needs);
1120 for (i = 0; i < n; i++) {
1121 int j;
1122
1123 /* edges are already added */
1124 if ((r = &p->n_regw[1+i])->r_class == -1) {
1125 r = p->n_regw;
1126 } else {
1127 AddEdge(r, p->n_regw);
1128 addalledges(r);
1129 w = q->needs;
1130 while ((w = hasneed(w, cNEVER))) {
1131 AddEdge(r, &ablock[(int)w[1]]);
1132 w += 2;
1133 }
1134 }
1135
1136 if (optype(o) != LTYPE && hasneed2(q->needs, cNSL, CLASS(r)) == 0)
1137 addedge_r(p->n_left, r);
1138
1139 if (optype(o) == BITYPE && hasneed2(q->needs, cNSR, CLASS(r)) == 0)
1140 addedge_r(p->n_right, r);
1141
1142 for (j = i + 1; j < n; j++) {
1143 if (p->n_regw[j+1].r_class == -1)
1144 continue;
1145 AddEdge(r, &p->n_regw[j+1]);
1146 }
1147 }
1148 #else
1149 /* simple needs */
1150 n = ncnt(q->needs);
1151 for (i = 0; i < n; i++) {
1152 #if 1
1153 static int ncl[] =
1154 { 0, NASL, NBSL, NCSL, NDSL, NESL, NFSL, NGSL };
1155 static int ncr[] =
1156 { 0, NASR, NBSR, NCSR, NDSR, NESR, NFSR, NGSR };
1157 int j;
1158
1159 /* edges are already added */
1160 if ((r = &p->n_regw[1+i])->r_class == -1) {
1161 r = p->n_regw;
1162 } else {
1163 AddEdge(r, p->n_regw);
1164 addalledges(r);
1165 if (q->needs & NSPECIAL) {
1166 struct rspecial *rc;
1167 for (rc = nspecial(q); rc->op; rc++) {
1168 if (rc->op != NEVER)
1169 continue;
1170 AddEdge(r, &ablock[rc->num]);
1171 }
1172 }
1173 }
1174 if (optype(o) != LTYPE && (q->needs & ncl[CLASS(r)]) == 0)
1175 addedge_r(p->n_left, r);
1176 if (optype(o) == BITYPE && (q->needs & ncr[CLASS(r)]) == 0)
1177 addedge_r(p->n_right, r);
1178 for (j = i + 1; j < n; j++) {
1179 if (p->n_regw[j+1].r_class == -1)
1180 continue;
1181 AddEdge(r, &p->n_regw[j+1]);
1182 }
1183 #else
1184 if ((r = &p->n_regw[1+i])->r_class == -1)
1185 continue;
1186 addalledges(r);
1187 if (optype(o) != LTYPE && (q->needs & NASL) == 0)
1188 addedge_r(p->n_left, r);
1189 if (optype(o) == BITYPE && (q->needs & NASR) == 0)
1190 addedge_r(p->n_right, r);
1191 #endif
1192 }
1193 #endif
1194
1195 #ifdef NEWNEED
1196 /* special needs */
1197 for (w = q->needs; w && *w; w += NEEDADD(w[0])) {
1198 switch ((int)w[0]) {
1199 #define ONLY(c,s) if (c) s(c, &ablock[(int)w[1]])
1200 case cNL:
1201 addalledges(&ablock[(int)w[1]]);
1202 ONLY(lr, moveadd);
1203 if (optype(o) != BITYPE)
1204 break;
1205 /* FALLTHROUGH */
1206 case cNOR:
1207 addedge_r(p->n_right, &ablock[(int)w[1]]);
1208 break;
1209 case cNR:
1210 addalledges(&ablock[(int)w[1]]);
1211 ONLY(rr, moveadd);
1212 /* FALLTHROUGH */
1213 case cNOL:
1214 addedge_r(p->n_left, &ablock[(int)w[1]]);
1215 break;
1216 case cNEVER:
1217 addalledges(&ablock[(int)w[1]]);
1218 break;
1219 #undef ONLY
1220 }
1221 }
1222 #else
1223 /* special needs */
1224 if (q->needs & NSPECIAL) {
1225 struct rspecial *rc;
1226 for (rc = nspecial(q); rc->op; rc++) {
1227 switch (rc->op) {
1228 #define ONLY(c,s) if (c) s(c, &ablock[rc->num])
1229 case NLEFT:
1230 addalledges(&ablock[rc->num]);
1231 ONLY(lr, moveadd);
1232 if (optype(o) != BITYPE)
1233 break;
1234 /* FALLTHROUGH */
1235 case NORIGHT:
1236 addedge_r(p->n_right, &ablock[rc->num]);
1237 break;
1238 case NRIGHT:
1239 addalledges(&ablock[rc->num]);
1240 ONLY(rr, moveadd);
1241 /* FALLTHROUGH */
1242 case NOLEFT:
1243 addedge_r(p->n_left, &ablock[rc->num]);
1244 break;
1245 case NEVER:
1246 addalledges(&ablock[rc->num]);
1247 break;
1248 #undef ONLY
1249 }
1250 }
1251 }
1252 #endif
1253
1254 if (o == ASSIGN) {
1255 /* avoid use of unhandled registers */
1256 if (p->n_left->n_op == REG &&
1257 !TESTBIT(validregs, regno(p->n_left)))
1258 lr = NULL;
1259 if (p->n_right->n_op == REG &&
1260 !TESTBIT(validregs, regno(p->n_right)))
1261 rr = NULL;
1262 /* needs special treatment */
1263 if (lr && rr)
1264 moveadd(lr, rr);
1265 if (lr && rv)
1266 moveadd(lr, rv);
1267 if (rr && rv)
1268 moveadd(rr, rv);
1269 } else if (callop(o)) {
1270 int *c;
1271
1272 for (c = livecall(p); *c != -1; c++) {
1273 addalledges(ablock + *c);
1274 LIVEADD(*c);
1275 }
1276 } else if (q->rewrite & (RESC1|RESC2|RESC3)) {
1277 if (lr && rr)
1278 AddEdge(lr, rr);
1279 } else if (q->rewrite & RLEFT) {
1280 if (lr && rv)
1281 moveadd(rv, lr), lrv = rv;
1282 if (rv && rp)
1283 addedge_r(rp, rv);
1284 } else if (q->rewrite & RRIGHT) {
1285 if (rr && rv)
1286 moveadd(rv, rr), rrv = rv;
1287 if (rv && lp)
1288 addedge_r(lp, rv);
1289 }
1290
1291 switch (optype(o)) {
1292 case BITYPE:
1293 if (p->n_op == ASSIGN &&
1294 (p->n_left->n_op == TEMP || p->n_left->n_op == REG)) {
1295 /* only go down right node */
1296 insnwalk(p->n_right);
1297 } else if (callop(o)) {
1298 insnwalk(p->n_left);
1299 /* Do liveness analysis on arguments (backwards) */
1300 argswalk(p->n_right);
1301 } else if ((p->n_su & DORIGHT) == 0) {
1302 setlive(p->n_left, 1, lrv);
1303 insnwalk(p->n_right);
1304 setlive(p->n_left, 0, lrv);
1305 insnwalk(p->n_left);
1306 } else {
1307 setlive(p->n_right, 1, rrv);
1308 insnwalk(p->n_left);
1309 setlive(p->n_right, 0, rrv);
1310 insnwalk(p->n_right);
1311 }
1312 break;
1313
1314 case UTYPE:
1315 insnwalk(p->n_left);
1316 break;
1317
1318 case LTYPE:
1319 switch (o) {
1320 case REG:
1321 if (!TESTBIT(validregs, regno(p)))
1322 break; /* never add moves */
1323 /* FALLTHROUGH */
1324 case TEMP:
1325 i = regno(p);
1326 rr = (o == TEMP ? &nblock[i] : &ablock[i]);
1327 if (rv != rr) {
1328 addalledges(rr);
1329 moveadd(rv, rr);
1330 }
1331 LIVEADD(i);
1332 break;
1333
1334 case OREG: /* XXX - not yet */
1335 break;
1336
1337 default:
1338 break;
1339 }
1340 break;
1341 }
1342 }
1343
1344 static bittype **gen, **killed, **in, **out;
1345
1346 struct notspill {
1347 SLIST_ENTRY(notspill) link;
1348 int spnum;
1349 };
1350 SLIST_HEAD(, notspill) nothead;
1351
1352 static int
1353 innotspill(int n)
1354 {
1355 struct notspill *nsp;
1356
1357 SLIST_FOREACH(nsp, &nothead, link)
1358 if (nsp->spnum == n)
1359 return 1;
1360 return 0;
1361 }
1362
1363 static void
1364 addnotspill(int n)
1365 {
1366 struct notspill *nsp;
1367
1368 if (innotspill(n))
1369 return;
1370 nsp = tmpalloc(sizeof(struct notspill));
1371 nsp->spnum = n;
1372 SLIST_INSERT_LAST(&nothead, nsp, link);
1373 }
1374
1375 /*
1376 * Found an extended assembler node, so growel out gen/killed nodes.
1377 */
1378 static void
1379 xasmionize(NODE *p, void *arg)
1380 {
1381 int bb = *(int *)arg;
1382 int cw, b;
1383
1384 if (p->n_op == ICON && p->n_type == STRTY)
1385 return; /* dummy end marker */
1386
1387 cw = xasmcode(p->n_name);
1388 if (XASMVAL(cw) == 'n' /* || XASMVAL(cw) == 'm' */)
1389 return; /* no flow analysis */
1390 p = p->n_left;
1391
1392 if (XASMVAL(cw) == 'g' && p->n_op != TEMP && p->n_op != REG)
1393 return; /* no flow analysis */
1394
1395 b = regno(p);
1396 if (XASMVAL(cw) == 'r' && p->n_op == TEMP)
1397 addnotspill(b);
1398 if (XASMVAL(cw) == 'm') {
1399 if (p->n_op == UMUL && p->n_left->n_op == TEMP) {
1400 p = p->n_left;
1401 b = regno(p);
1402 addnotspill(b);
1403 cw &= ~(XASMASG|XASMINOUT);
1404 } else
1405 return;
1406 }
1407 #define MKTOFF(r) ((r) - tempmin + MAXREGS)
1408 if (XASMISOUT(cw)) {
1409 if (p->n_op == TEMP) {
1410 BITCLEAR(gen[bb], MKTOFF(b));
1411 BITSET(killed[bb], MKTOFF(b));
1412 } else if (p->n_op == REG) {
1413 BITCLEAR(gen[bb], b);
1414 BITSET(killed[bb], b);
1415 } else
1416 uerror("bad xasm node type %d", p->n_op);
1417 }
1418 if (XASMISINP(cw)) {
1419 if (p->n_op == TEMP) {
1420 BITSET(gen[bb], MKTOFF(b));
1421 } else if (p->n_op == REG) {
1422 BITSET(gen[bb], b);
1423 } else if (optype(p->n_op) != LTYPE) {
1424 if (XASMVAL(cw) == 'r')
1425 uerror("couldn't find available register");
1426 else
1427 uerror("bad xasm node type2");
1428 }
1429 }
1430 }
1431
1432 #ifndef XASMCONSTREGS
1433 #define XASMCONSTREGS(x) (-1)
1434 #endif
1435
1436 /*
1437 * Check that given constraints are valid.
1438 */
1439 static void
1440 xasmconstr(NODE *p, void *arg)
1441 {
1442 int i;
1443
1444 if (p->n_op == ICON && p->n_type == STRTY)
1445 return; /* no constraints */
1446
1447 if (strcmp(p->n_name, "cc") == 0 || strcmp(p->n_name, "memory") == 0)
1448 return;
1449
1450 for (i = 0; i < MAXREGS; i++)
1451 if (rnames[i] && strcmp(rnames[i], p->n_name) == 0) {
1452 addalledges(&ablock[i]);
1453 return;
1454 }
1455 if ((i = XASMCONSTREGS(p->n_name)) < 0)
1456 comperr("unsupported xasm constraint %s", p->n_name);
1457 addalledges(&ablock[i]);
1458 }
1459
1460 #define RUP(x) (((x)+NUMBITS-1)/NUMBITS)
1461 #define SETCOPY(t,f,i,n) for (i = 0; i < RUP(n); i++) t[i] = f[i]
1462 #define SETSET(t,f,i,n) for (i = 0; i < RUP(n); i++) t[i] |= f[i]
1463 #define SETCLEAR(t,f,i,n) for (i = 0; i < RUP(n); i++) t[i] &= ~f[i]
1464 #define SETCMP(v,t,f,i,n) for (i = 0; i < RUP(n); i++) \
1465 if (t[i] != f[i]) v = 1
1466 #define SETEMPTY(t,sz) memset(t, 0, BIT2BYTE(sz))
1467
1468 static int
1469 deldead(NODE *p, bittype *lvar)
1470 {
1471 NODE *q;
1472 int ty, rv = 0;
1473
1474 #define BNO(p) (regno(p) - tempmin+MAXREGS)
1475 if (p->n_op == TEMP)
1476 BITSET(lvar, BNO(p));
1477 if (asgop(p->n_op) && p->n_left->n_op == TEMP &&
1478 TESTBIT(lvar, BNO(p->n_left)) == 0) {
1479 /*
1480 * Not live, must delete the right tree at least
1481 * down to next statement with side effects.
1482 */
1483 BDEBUG(("DCE deleting temp %d\n", regno(p->n_left)));
1484 nfree(p->n_left);
1485 q = p->n_right;
1486 *p = *q;
1487 nfree(q);
1488 deldead(p, lvar);
1489 return 1;
1490 }
1491 ty = optype(p->n_op);
1492 if (ty != LTYPE)
1493 rv |= deldead(p->n_left, lvar);
1494 if (ty == BITYPE)
1495 rv |= deldead(p->n_right, lvar);
1496 return rv;
1497 }
1498
1499 /*
1500 * Ensure that the su field is empty before generating instructions.
1501 */
1502 static void
1503 clrsu(NODE *p)
1504 {
1505 int o = optype(p->n_op);
1506
1507 p->n_su = 0;
1508 if (o != LTYPE)
1509 clrsu(p->n_left);
1510 if (o == BITYPE)
1511 clrsu(p->n_right);
1512 }
1513
1514 /*
1515 * Do dead code elimination.
1516 */
1517 static int
1518 dce(struct p2env *p2e)
1519 {
1520 extern struct interpass prepole;
1521 struct basicblock *bb;
1522 struct interpass *ip;
1523 NODE *p;
1524 bittype *lvar;
1525 int i, bbnum, fix = 0;
1526
1527 #ifdef mach_vax
1528 return 0; /* XXX may need to recalc tree structure */
1529 /* eliminating assignments may create more OREGs */
1530 /* Fix by or/either break out ASSIGN or do this earlier */
1531 #endif
1532
1533 BDEBUG(("Entering DCE\n"));
1534 /*
1535 * Traverse over the basic blocks.
1536 * if an assignment is found that writes to a temporary
1537 * that is not live out, remove that assignment and its legs.
1538 */
1539 DLIST_INIT(&prepole, qelem);
1540 BITALLOC(lvar, tmpalloc, xbits);
1541 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1542 bbnum = bb->bbnum;
1543 BBDEBUG(("DCE bblock %d, start %p last %p\n",
1544 bbnum, bb->first, bb->last));
1545 SETCOPY(lvar, out[bbnum], i, xbits);
1546 for (ip = bb->last; ; ip = DLIST_PREV(ip, qelem)) {
1547 if (ip->type == IP_NODE && deldead(ip->ip_node, lvar)) {
1548 if ((p = deluseless(ip->ip_node)) == NULL) {
1549 struct interpass *previp;
1550 struct basicblock *prevbb;
1551
1552 if (ip == bb->first && ip == bb->last) {
1553 /* Remove basic block */
1554 previp = DLIST_PREV(ip, qelem);
1555 DLIST_REMOVE(ip, qelem);
1556 prevbb = DLIST_PREV(bb, bbelem);
1557 DLIST_REMOVE(bb, bbelem);
1558 bb = prevbb;
1559 } else if (ip == bb->first) {
1560 bb->first =
1561 DLIST_NEXT(ip, qelem);
1562 DLIST_REMOVE(ip, qelem);
1563 } else if (ip == bb->last) {
1564 previp = DLIST_PREV(ip, qelem);
1565 DLIST_REMOVE(ip, qelem);
1566 bb->last = previp;
1567 bb = DLIST_PREV(bb, bbelem);
1568 } else {
1569 previp = DLIST_NEXT(ip, qelem);
1570 DLIST_REMOVE(ip, qelem);
1571 ip = previp;
1572 fix++;
1573 continue;
1574 }
1575 fix++;
1576 BDEBUG(("bb %d: DCE ip %p deleted\n",
1577 bbnum, ip));
1578 break;
1579 } else while (!DLIST_ISEMPTY(&prepole, qelem)) {
1580 struct interpass *tipp;
1581
1582 BDEBUG(("bb %d: DCE doing ip prepend\n", bbnum));
1583 tipp = DLIST_NEXT(&prepole, qelem);
1584 DLIST_REMOVE(tipp, qelem);
1585 DLIST_INSERT_BEFORE(ip, tipp, qelem);
1586 if (ip == bb->first)
1587 bb->first = tipp;
1588 fix++;
1589 BDEBUG(("DCE ip prepended\n"));
1590 }
1591 if (ip->type == IP_NODE) {
1592 clrsu(p);
1593 geninsn(p, FOREFF);
1594 nsucomp(p);
1595 ip->ip_node = p;
1596 }
1597 }
1598 if (ip == bb->first)
1599 break;
1600 }
1601 }
1602 BDEBUG(("DCE fix %d\n", fix));
1603 return fix;
1604 }
1605
1606 /*
1607 * Set/clear long term liveness for regs and temps.
1608 */
1609 static void
1610 unionize(NODE *p, int bb)
1611 {
1612 int i, o, ty;
1613
1614 if ((o = p->n_op) == TEMP) {
1615 #ifdef notyet
1616 for (i = 0; i < szty(p->n_type); i++) {
1617 BITSET(gen[bb], (regno(p) - tempmin+i+MAXREGS));
1618 }
1619 #else
1620 i = 0;
1621 BITSET(gen[bb], (regno(p) - tempmin+i+MAXREGS));
1622 #endif
1623 } else if (VALIDREG(p)) {
1624 BITSET(gen[bb], regno(p));
1625 }
1626 if (asgop(o)) {
1627 if (p->n_left->n_op == TEMP) {
1628 int b = regno(p->n_left) - tempmin+MAXREGS;
1629 #ifdef notyet
1630 for (i = 0; i < szty(p->n_type); i++) {
1631 BITCLEAR(gen[bb], (b+i));
1632 BITSET(killed[bb], (b+i));
1633 }
1634 #else
1635 i = 0;
1636 BITCLEAR(gen[bb], (b+i));
1637 BITSET(killed[bb], (b+i));
1638 #endif
1639 unionize(p->n_right, bb);
1640 return;
1641 } else if (VALIDREG(p->n_left)) {
1642 int b = regno(p->n_left);
1643 BITCLEAR(gen[bb], b);
1644 BITSET(killed[bb], b);
1645 unionize(p->n_right, bb);
1646 return;
1647 }
1648 }
1649 ty = optype(o);
1650 if (ty != LTYPE)
1651 unionize(p->n_left, bb);
1652 if (ty == BITYPE)
1653 unionize(p->n_right, bb);
1654 }
1655
1656 /*
1657 * Do variable liveness analysis. Only analyze the long-lived
1658 * variables, and save the live-on-exit temporaries in a bit-field
1659 * at the end of each basic block. This bit-field is later used
1660 * when doing short-range liveness analysis in Build().
1661 */
1662 static void
1663 LivenessAnalysis(struct p2env *p2e)
1664 {
1665 struct basicblock *bb;
1666 struct interpass *ip;
1667 int bbnum;
1668
1669 /*
1670 * generate the gen-killed sets for all basic blocks.
1671 */
1672 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1673 bbnum = bb->bbnum;
1674 for (ip = bb->last; ; ip = DLIST_PREV(ip, qelem)) {
1675 /* gen/killed is 'p', this node is 'n' */
1676 if (ip->type == IP_NODE) {
1677 if (ip->ip_node->n_op == XASM)
1678 flist(ip->ip_node->n_left,
1679 xasmionize, &bbnum);
1680 else
1681 unionize(ip->ip_node, bbnum);
1682 }
1683 if (ip == bb->first)
1684 break;
1685 }
1686 memcpy(in[bbnum], gen[bbnum], BIT2BYTE(xbits));
1687 #ifdef PCC_DEBUG
1688 #define PRTRG(x) printf("%d ", x < MAXREGS ? x : x + tempmin-MAXREGS)
1689 if (r2debug) {
1690 int i;
1691
1692 printf("basic block %d\ngen: ", bbnum);
1693 for (i = 0; i < xbits; i++)
1694 if (TESTBIT(gen[bbnum], i))
1695 PRTRG(i);
1696 printf("\nkilled: ");
1697 for (i = 0; i < xbits; i++)
1698 if (TESTBIT(killed[bbnum], i))
1699 PRTRG(i);
1700 printf("\n");
1701 }
1702 #endif
1703 }
1704 }
1705
1706
1707 /*
1708 * Build the set of interference edges and adjacency list.
1709 */
1710 static void
1711 Build(struct p2env *p2e)
1712 {
1713 struct interpass *ipole = &p2e->ipole;
1714 struct basicblock bbfake;
1715 struct interpass *ip;
1716 struct basicblock *bb;
1717 bittype *saved;
1718 int i, j, again;
1719
1720 if (xtemps == 0) {
1721 /*
1722 * No basic block splitup is done if not optimizing,
1723 * so fake one basic block to keep the liveness analysis
1724 * happy.
1725 */
1726 p2e->nbblocks = 1;
1727 bbfake.bbnum = 0;
1728 bbfake.last = DLIST_PREV(ipole, qelem);
1729 bbfake.first = DLIST_NEXT(ipole, qelem);
1730 DLIST_INIT(&p2e->bblocks, bbelem);
1731 DLIST_INSERT_AFTER(&p2e->bblocks, &bbfake, bbelem);
1732 SLIST_INIT(&bbfake.child);
1733 }
1734
1735 /* Just fetch space for the temporaries from stack */
1736 gen = tmpalloc(p2e->nbblocks*sizeof(bittype*));
1737 killed = tmpalloc(p2e->nbblocks*sizeof(bittype*));
1738 in = tmpalloc(p2e->nbblocks*sizeof(bittype*));
1739 out = tmpalloc(p2e->nbblocks*sizeof(bittype*));
1740 for (i = 0; i < p2e->nbblocks; i++) {
1741 BITALLOC(gen[i],tmpalloc,xbits);
1742 BITALLOC(killed[i],tmpalloc,xbits);
1743 BITALLOC(in[i],tmpalloc,xbits);
1744 BITALLOC(out[i],tmpalloc,xbits);
1745 }
1746 BITALLOC(saved,tmpalloc,xbits);
1747
1748 SLIST_INIT(&nothead);
1749 livagain:
1750 LivenessAnalysis(p2e);
1751
1752 /* register variable temporaries are live */
1753 for (i = 0; i < NPERMREG-1; i++) {
1754 if (nsavregs[i])
1755 continue;
1756 BITSET(out[p2e->nbblocks-1], (i+MAXREGS));
1757 for (j = i+1; j < NPERMREG-1; j++) {
1758 if (nsavregs[j])
1759 continue;
1760 AddEdge(&nblock[i+tempmin], &nblock[j+tempmin]);
1761 }
1762 }
1763
1764 /* do liveness analysis on basic block level */
1765 do {
1766 struct cfgnode *cn;
1767 again = 0;
1768 /* XXX - loop should be in reversed execution-order */
1769 DLIST_FOREACH_REVERSE(bb, &p2e->bblocks, bbelem) {
1770 i = bb->bbnum;
1771 SETCOPY(saved, out[i], j, xbits);
1772 SLIST_FOREACH(cn, &bb->child, chld) {
1773 SETSET(out[i], in[cn->bblock->bbnum], j, xbits);
1774 }
1775 SETCMP(again, saved, out[i], j, xbits);
1776 SETCOPY(saved, in[i], j, xbits);
1777 SETCOPY(in[i], out[i], j, xbits);
1778 SETCLEAR(in[i], killed[i], j, xbits);
1779 SETSET(in[i], gen[i], j, xbits);
1780 SETCMP(again, saved, in[i], j, xbits);
1781 }
1782 } while (again);
1783
1784 #ifdef PCC_DEBUG
1785 if (r2debug) {
1786 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1787 printf("basic block %d\nin: ", bb->bbnum);
1788 for (i = 0; i < xbits; i++)
1789 if (TESTBIT(in[bb->bbnum], i))
1790 PRTRG(i);
1791 printf("\nout: ");
1792 for (i = 0; i < xbits; i++)
1793 if (TESTBIT(out[bb->bbnum], i))
1794 PRTRG(i);
1795 printf("\n");
1796 }
1797 }
1798 #endif
1799 if (xtemps && xdce) {
1800 /*
1801 * Do dead code elimination by using live out.
1802 * Ignores if any variable read from is marked volatile,
1803 * but what it should do is unspecified anyway.
1804 * Liveness Analysis should be done in optim2 instead.
1805 *
1806 * This should recalculate the basic block structure.
1807 */
1808 if (dce(p2e)) {
1809 /* Clear bitfields */
1810 for (i = 0; i < p2e->nbblocks; i++) {
1811 SETEMPTY(gen[i],xbits);
1812 SETEMPTY(killed[i],xbits);
1813 SETEMPTY(in[i],xbits);
1814 SETEMPTY(out[i],xbits);
1815 }
1816 SETEMPTY(saved,xbits);
1817 goto livagain;
1818 }
1819 }
1820
1821 DLIST_FOREACH(bb, &p2e->bblocks, bbelem) {
1822 RDEBUG(("liveadd bb %d\n", bb->bbnum));
1823 i = bb->bbnum;
1824 for (j = 0; j < xbits; j += NUMBITS)
1825 live[j/NUMBITS] = 0;
1826 SETCOPY(live, out[i], j, xbits);
1827 for (ip = bb->last; ; ip = DLIST_PREV(ip, qelem)) {
1828 if (ip->type == IP_NODE) {
1829 if (ip->ip_node->n_op == XASM) {
1830 flist(ip->ip_node->n_right,
1831 xasmconstr, 0);
1832 listf(ip->ip_node->n_left, setxarg);
1833 listf(ip->ip_node->n_left, delcl);
1834 } else
1835 insnwalk(ip->ip_node);
1836 }
1837 if (ip == bb->first)
1838 break;
1839 }
1840 }
1841
1842 #ifdef PCC_DEBUG
1843 if (r2debug) {
1844 struct AdjSet *w;
1845 ADJL *x;
1846 REGW *y;
1847 MOVL *m;
1848
1849 printf("Interference edges\n");
1850 for (i = 0; i < HASHSZ; i++) {
1851 if ((w = edgehash[i]) == NULL)
1852 continue;
1853 for (; w; w = w->next)
1854 printf("%d <-> %d\n", ASGNUM(w->u), ASGNUM(w->v));
1855 }
1856 printf("Degrees\n");
1857 DLIST_FOREACH(y, &initial, link) {
1858 printf("%d (%c): trivial [%d] ", ASGNUM(y),
1859 CLASS(y)+'@', trivially_colorable(y));
1860 i = 0;
1861 for (x = ADJLIST(y); x; x = x->r_next) {
1862 if (ONLIST(x->a_temp) != &selectStack &&
1863 ONLIST(x->a_temp) != &coalescedNodes)
1864 printf("%d ", ASGNUM(x->a_temp));
1865 else
1866 printf("(%d) ", ASGNUM(x->a_temp));
1867 i++;
1868 }
1869 printf(": n=%d\n", i);
1870 }
1871 printf("Move nodes\n");
1872 DLIST_FOREACH(y, &initial, link) {
1873 if (MOVELIST(y) == NULL)
1874 continue;
1875 printf("%d: ", ASGNUM(y));
1876 for (m = MOVELIST(y); m; m = m->next) {
1877 REGW *yy = m->regm->src == y ?
1878 m->regm->dst : m->regm->src;
1879 printf("%d ", ASGNUM(yy));
1880 }
1881 printf("\n");
1882 }
1883 }
1884 #endif
1885
1886 }
1887
1888 static void
1889 EnableMoves(REGW *n)
1890 {
1891 MOVL *l;
1892 REGM *m;
1893
1894 for (l = MOVELIST(n); l; l = l->next) {
1895 m = l->regm;
1896 if (m->queue != ACTIVE)
1897 continue;
1898 DLIST_REMOVE(m, link);
1899 PUSHMLIST(m, worklistMoves, WLIST);
1900 }
1901 }
1902
1903 static void
1904 EnableAdjMoves(REGW *nodes)
1905 {
1906 ADJL *w;
1907 REGW *n;
1908
1909 EnableMoves(nodes);
1910 for (w = ADJLIST(nodes); w; w = w->r_next) {
1911 n = w->a_temp;
1912 if (ONLIST(n) == &selectStack || ONLIST(n) == &coalescedNodes)
1913 continue;
1914 EnableMoves(w->a_temp);
1915 }
1916 }
1917
1918 /*
1919 * Decrement the degree of node w for class c.
1920 */
1921 static void
1922 DecrementDegree(REGW *w, int c)
1923 {
1924 int wast;
1925
1926 #ifdef PCC_DEBUG
1927 RRDEBUG(("DecrementDegree: w %d, c %d\n", ASGNUM(w), c));
1928 #endif
1929
1930 wast = trivially_colorable(w);
1931 if (NCLASS(w, c) > 0)
1932 NCLASS(w, c)--;
1933 if (wast == trivially_colorable(w))
1934 return;
1935
1936 EnableAdjMoves(w);
1937 DELWLIST(w);
1938 ONLIST(w) = 0;
1939 if (MoveRelated(w)) {
1940 PUSHWLIST(w, freezeWorklist);
1941 } else {
1942 PUSHWLIST(w, simplifyWorklist);
1943 }
1944 }
1945
1946 static void
1947 Simplify(void)
1948 {
1949 REGW *w;
1950 ADJL *l;
1951
1952 w = POPWLIST(simplifyWorklist);
1953 PUSHWLIST(w, selectStack);
1954 #ifdef PCC_DEBUG
1955 RDEBUG(("Simplify: node %d class %d\n", ASGNUM(w), w->r_class));
1956 #endif
1957
1958 l = w->r_adjList;
1959 for (; l; l = l->r_next) {
1960 if (ONLIST(l->a_temp) == &selectStack ||
1961 ONLIST(l->a_temp) == &coalescedNodes)
1962 continue;
1963 DecrementDegree(l->a_temp, w->r_class);
1964 }
1965 }
1966
1967 static REGW *
1968 GetAlias(REGW *n)
1969 {
1970 if (ONLIST(n) == &coalescedNodes)
1971 return GetAlias(ALIAS(n));
1972 return n;
1973 }
1974
1975 static int
1976 OK(REGW *t, REGW *r)
1977 {
1978 #ifdef PCC_DEBUG
1979 RDEBUG(("OK: t %d CLASS(t) %d adjSet(%d,%d)=%d\n",
1980 ASGNUM(t), CLASS(t), ASGNUM(t), ASGNUM(r), adjSet(t, r)));
1981
1982 if (r2debug > 1) {
1983 ADJL *w;
1984 int ndeg = 0;
1985 printf("OK degree: ");
1986 for (w = ADJLIST(t); w; w = w->r_next) {
1987 if (ONLIST(w->a_temp) != &selectStack &&
1988 ONLIST(w->a_temp) != &coalescedNodes)
1989 printf("%c%d ", CLASS(w->a_temp)+'@',
1990 ASGNUM(w->a_temp)), ndeg++;
1991 else
1992 printf("(%d) ", ASGNUM(w->a_temp));
1993 }
1994 printf("\n");
1995 #if 0
1996 if (ndeg != DEGREE(t) && DEGREE(t) >= 0)
1997 printf("!!!ndeg %d != DEGREE(t) %d\n", ndeg, DEGREE(t));
1998 #endif
1999 }
2000 #endif
2001
2002 if (trivially_colorable(t) || ONLIST(t) == &precolored ||
2003 (adjSet(t, r) || !aliasmap(CLASS(t), COLOR(r))))/* XXX - check aliasmap */
2004 return 1;
2005 return 0;
2006 }
2007
2008 static int
2009 adjok(REGW *v, REGW *u)
2010 {
2011 ADJL *w;
2012 REGW *t;
2013
2014 RDEBUG(("adjok\n"));
2015 for (w = ADJLIST(v); w; w = w->r_next) {
2016 t = w->a_temp;
2017 if (ONLIST(t) == &selectStack || ONLIST(t) == &coalescedNodes)
2018 continue;
2019 if (OK(t, u) == 0)
2020 return 0;
2021 }
2022 RDEBUG(("adjok returns OK\n"));
2023 return 1;
2024 }
2025
2026 /*
2027 * Do a conservative estimation of whether two temporaries can
2028 * be coalesced. This is "Briggs-style" check.
2029 * Neither u nor v is precolored when called.
2030 */
2031 static int
2032 Conservative(REGW *u, REGW *v)
2033 {
2034 ADJL *w, *ww;
2035 REGW *n;
2036 int xncl[NUMCLASS+1], mcl = 0, j;
2037
2038 for (j = 0; j < NUMCLASS+1; j++)
2039 xncl[j] = 0;
2040 /*
2041 * Increment xncl[class] up to K for each class.
2042 * If all classes has reached K then check colorability and return.
2043 */
2044 for (w = ADJLIST(u); w; w = w->r_next) {
2045 n = w->a_temp;
2046 if (ONLIST(n) == &selectStack || ONLIST(n) == &coalescedNodes)
2047 continue;
2048 if (xncl[CLASS(n)] == regK[CLASS(n)])
2049 continue;
2050 if (!trivially_colorable(n) || ONLIST(n) == &precolored)
2051 xncl[CLASS(n)]++;
2052 if (xncl[CLASS(n)] < regK[CLASS(n)])
2053 continue;
2054 if (++mcl == NUMCLASS)
2055 goto out; /* cannot get more out of it */
2056 }
2057 for (w = ADJLIST(v); w; w = w->r_next) {
2058 n = w->a_temp;
2059 if (ONLIST(n) == &selectStack || ONLIST(n) == &coalescedNodes)
2060 continue;
2061 if (xncl[CLASS(n)] == regK[CLASS(n)])
2062 continue;
2063 /* ugly: have we been here already? */
2064 for (ww = ADJLIST(u); ww; ww = ww->r_next)
2065 if (ww->a_temp == n)
2066 break;
2067 if (ww)
2068 continue;
2069 if (!trivially_colorable(n) || ONLIST(n) == &precolored)
2070 xncl[CLASS(n)]++;
2071 if (xncl[CLASS(n)] < regK[CLASS(n)])
2072 continue;
2073 if (++mcl == NUMCLASS)
2074 break;
2075 }
2076 out: j = trivially_colorable_p(CLASS(u), xncl);
2077 return j;
2078 }
2079
2080 static void
2081 AddWorkList(REGW *w)
2082 {
2083
2084 if (ONLIST(w) != &precolored && !MoveRelated(w) &&
2085 trivially_colorable(w)) {
2086 DELWLIST(w);
2087 PUSHWLIST(w, simplifyWorklist);
2088 }
2089 }
2090
2091 static void
2092 Combine(REGW *u, REGW *v)
2093 {
2094 MOVL *m;
2095 ADJL *l;
2096 REGW *t;
2097
2098 #ifdef PCC_DEBUG
2099 RDEBUG(("Combine (%d,%d)\n", ASGNUM(u), ASGNUM(v)));
2100 #endif
2101
2102 if (ONLIST(v) == &freezeWorklist) {
2103 DELWLIST(v);
2104 } else {
2105 DELWLIST(v);
2106 }
2107 PUSHWLIST(v, coalescedNodes);
2108 ALIAS(v) = u;
2109 #ifdef PCC_DEBUG
2110 if (r2debug) {
2111 printf("adjlist(%d): ", ASGNUM(v));
2112 for (l = ADJLIST(v); l; l = l->r_next)
2113 printf("%d ", l->a_temp->nodnum);
2114 printf("\n");
2115 }
2116 #endif
2117 #if 1
2118 {
2119 MOVL *m0 = MOVELIST(v);
2120
2121 for (m0 = MOVELIST(v); m0; m0 = m0->next) {
2122 for (m = MOVELIST(u); m; m = m->next)
2123 if (m->regm == m0->regm)
2124 break; /* Already on list */
2125 if (m)
2126 continue; /* already on list */
2127 MOVELISTADD(u, m0->regm);
2128 }
2129 }
2130 #else
2131
2132 if ((m = MOVELIST(u))) {
2133 while (m->next)
2134 m = m->next;
2135 m->next = MOVELIST(v);
2136 } else
2137 MOVELIST(u) = MOVELIST(v);
2138 #endif
2139 EnableMoves(v);
2140 for (l = ADJLIST(v); l; l = l->r_next) {
2141 t = l->a_temp;
2142 if (ONLIST(t) == &selectStack || ONLIST(t) == &coalescedNodes)
2143 continue;
2144 /* Do not add edge if u cannot affect the colorability of t */
2145 /* XXX - check aliasmap */
2146 if (ONLIST(u) != &precolored || aliasmap(CLASS(t), COLOR(u)))
2147 AddEdge(t, u);
2148 DecrementDegree(t, CLASS(v));
2149 }
2150 if (!trivially_colorable(u) && ONLIST(u) == &freezeWorklist) {
2151 DELWLIST(u);
2152 PUSHWLIST(u, spillWorklist);
2153 }
2154 #ifdef PCC_DEBUG
2155 if (r2debug) {
2156 ADJL *w;
2157 printf("Combine %d class (%d): ", ASGNUM(u), CLASS(u));
2158 for (w = ADJLIST(u); w; w = w->r_next) {
2159 if (ONLIST(w->a_temp) != &selectStack &&
2160 ONLIST(w->a_temp) != &coalescedNodes)
2161 printf("%d ", ASGNUM(w->a_temp));
2162 else
2163 printf("(%d) ", ASGNUM(w->a_temp));
2164 }
2165 printf("\n");
2166 }
2167 #endif
2168 }
2169
2170 static void
2171 Coalesce(void)
2172 {
2173 REGM *m;
2174 REGW *x, *y, *u, *v;
2175
2176 m = POPMLIST(worklistMoves);
2177 x = GetAlias(m->src);
2178 y = GetAlias(m->dst);
2179
2180 if (ONLIST(y) == &precolored)
2181 u = y, v = x;
2182 else
2183 u = x, v = y;
2184
2185 #ifdef PCC_DEBUG
2186 RDEBUG(("Coalesce: src %d dst %d u %d v %d x %d y %d\n",
2187 ASGNUM(m->src), ASGNUM(m->dst), ASGNUM(u), ASGNUM(v),
2188 ASGNUM(x), ASGNUM(y)));
2189 #endif
2190
2191 if (CLASS(m->src) != CLASS(m->dst))
2192 comperr("Coalesce: src class %d, dst class %d",
2193 CLASS(m->src), CLASS(m->dst));
2194
2195 if (u == v) {
2196 RDEBUG(("Coalesce: u == v\n"));
2197 PUSHMLIST(m, coalescedMoves, COAL);
2198 AddWorkList(u);
2199 } else if (ONLIST(v) == &precolored || adjSet(u, v)) {
2200 RDEBUG(("Coalesce: constrainedMoves\n"));
2201 PUSHMLIST(m, constrainedMoves, CONSTR);
2202 AddWorkList(u);
2203 AddWorkList(v);
2204 } else if ((ONLIST(u) == &precolored && adjok(v, u)) ||
2205 (ONLIST(u) != &precolored && Conservative(u, v))) {
2206 RDEBUG(("Coalesce: Conservative\n"));
2207 PUSHMLIST(m, coalescedMoves, COAL);
2208 Combine(u, v);
2209 AddWorkList(u);
2210 } else {
2211 RDEBUG(("Coalesce: activeMoves\n"));
2212 PUSHMLIST(m, activeMoves, ACTIVE);
2213 }
2214 }
2215
2216 static void
2217 coalasg(NODE *p, void *arg)
2218 {
2219 NODE *l;
2220 REGW *u;
2221
2222 if (p->n_op != ASSIGN || p->n_regw == NULL)
2223 return;
2224 l = p->n_left;
2225 if (l->n_op == TEMP)
2226 u = &nblock[regno(l)];
2227 else if (l->n_op == REG)
2228 u = &ablock[regno(l)];
2229 else
2230 return;
2231
2232 Combine(u, p->n_regw);
2233 AddWorkList(u);
2234 }
2235
2236 /*
2237 * Coalesce assign to a left reg with the assign temp node itself.
2238 * This has to be done before anything else.
2239 */
2240 static void
2241 Coalassign(struct p2env *p2e)
2242 {
2243 struct interpass *ip;
2244
2245 DLIST_FOREACH(ip, &p2env.ipole, qelem) {
2246 if (ip->type == IP_NODE)
2247 walkf(ip->ip_node, coalasg, 0);
2248 }
2249 }
2250
2251 static void
2252 FreezeMoves(REGW *u)
2253 {
2254 MOVL *w, *o;
2255 REGM *m;
2256 REGW *z;
2257 REGW *x, *y, *v;
2258
2259 for (w = MOVELIST(u); w; w = w->next) {
2260 m = w->regm;
2261 if (m->queue != WLIST && m->queue != ACTIVE)
2262 continue;
2263 x = m->src;
2264 y = m->dst;
2265 if (GetAlias(y) == GetAlias(u))
2266 v = GetAlias(x);
2267 else
2268 v = GetAlias(y);
2269 #ifdef PCC_DEBUG
2270 RDEBUG(("FreezeMoves: u %d (%d,%d) v %d\n",
2271 ASGNUM(u),ASGNUM(x),ASGNUM(y),ASGNUM(v)));
2272 #endif
2273 DLIST_REMOVE(m, link);
2274 PUSHMLIST(m, frozenMoves, FROZEN);
2275 if (ONLIST(v) != &freezeWorklist)
2276 continue;
2277 for (o = MOVELIST(v); o; o = o->next)
2278 if (o->regm->queue == WLIST || o->regm->queue == ACTIVE)
2279 break;
2280 if (o == NULL) {
2281 z = v;
2282 DELWLIST(z);
2283 PUSHWLIST(z, simplifyWorklist);
2284 }
2285 }
2286 }
2287
2288 static void
2289 Freeze(void)
2290 {
2291 REGW *u;
2292
2293 /*
2294 * To find out:
2295 * Check if the moves to freeze have exactly the same
2296 * interference edges. If they do, coalesce them instead, it
2297 * may free up other nodes that they interfere with.
2298 */
2299
2300 /*
2301 * Select nodes to freeze first by using following criteria:
2302 * - Trivially colorable
2303 * - Single or few moves to less trivial nodes.
2304 */
2305 DLIST_FOREACH(u, &freezeWorklist, link) {
2306 if (u >= &nblock[tempmax] || u < &nblock[tempmin])
2307 continue; /* No short range temps */
2308 if (!trivially_colorable(u))
2309 continue; /* Prefer colorable nodes */
2310 /* Check for at most two move-related nodes */
2311 if (u->r_moveList->next && u->r_moveList->next->next)
2312 continue;
2313 /* Ok, remove node */
2314 DLIST_REMOVE(u, link);
2315 u->r_onlist = 0;
2316 break;
2317 }
2318 if (u == &freezeWorklist) /* Nothing matched criteria, just take one */
2319 u = POPWLIST(freezeWorklist);
2320 PUSHWLIST(u, simplifyWorklist);
2321 #ifdef PCC_DEBUG
2322 RDEBUG(("Freeze %d\n", ASGNUM(u)));
2323 #endif
2324 FreezeMoves(u);
2325 }
2326
2327 static void
2328 SelectSpill(void)
2329 {
2330 REGW *w;
2331
2332 RDEBUG(("SelectSpill\n"));
2333 #ifdef PCC_DEBUG
2334 if (r2debug)
2335 DLIST_FOREACH(w, &spillWorklist, link)
2336 printf("SelectSpill: %d\n", ASGNUM(w));
2337 #endif
2338
2339 /* First check if we can spill register variables */
2340 DLIST_FOREACH(w, &spillWorklist, link) {
2341 if (w >= &nblock[tempmin] && w < &nblock[basetemp])
2342 break;
2343 }
2344
2345 RRDEBUG(("SelectSpill: trying longrange\n"));
2346 if (w == &spillWorklist) {
2347 /* try to find another long-range variable */
2348 DLIST_FOREACH(w, &spillWorklist, link) {
2349 if (innotspill(w - nblock))
2350 continue;
2351 if (w >= &nblock[tempmin] && w < &nblock[tempmax])
2352 break;
2353 }
2354 }
2355
2356 if (w == &spillWorklist) {
2357 RRDEBUG(("SelectSpill: trying not leaf\n"));
2358 /* no heuristics, just fetch first element */
2359 /* but not if leaf */
2360 DLIST_FOREACH(w, &spillWorklist, link) {
2361 if (w->r_nclass[0] == 0)
2362 break;
2363 }
2364 }
2365
2366 if (w == &spillWorklist) {
2367 /* Eh, only leaves :-/ Try anyway */
2368 /* May not be useable */
2369 w = DLIST_NEXT(&spillWorklist, link);
2370 RRDEBUG(("SelectSpill: need leaf\n"));
2371 }
2372
2373 DLIST_REMOVE(w, link);
2374
2375 PUSHWLIST(w, simplifyWorklist);
2376 #ifdef PCC_DEBUG
2377 RDEBUG(("Freezing node %d\n", ASGNUM(w)));
2378 #endif
2379 FreezeMoves(w);
2380 }
2381
2382 /*
2383 * Set class on long-lived temporaries based on its type.
2384 */
2385 static void
2386 traclass(NODE *p, void *arg)
2387 {
2388 REGW *nb;
2389
2390 if (p->n_op != TEMP)
2391 return;
2392
2393 nb = &nblock[regno(p)];
2394 if (CLASS(nb) == 0)
2395 CLASS(nb) = gclass(p->n_type);
2396 }
2397
2398 static void
2399 paint(NODE *p, void *arg)
2400 {
2401 struct optab *q;
2402 REGW *w, *ww;
2403 int i;
2404
2405 #ifdef notyet
2406 /* XXX - trashes rewrite of trees (short) */
2407 if (!DLIST_ISEMPTY(&spilledNodes, link)) {
2408 p->n_reg = 0;
2409 return;
2410 }
2411 #endif
2412 if (p->n_regw != NULL) {
2413 /* Must color all allocated regs also */
2414 ww = w = p->n_regw;
2415 q = &table[TBLIDX(p->n_su)];
2416 p->n_reg = COLOR(w);
2417 w++;
2418 #ifdef NEWNEED
2419 if (q->needs)
2420 #else
2421 if (q->needs & ALLNEEDS)
2422 #endif
2423 for (i = 0; i < ncnt(q->needs); i++) {
2424 if (w->r_class == -1)
2425 p->n_reg |= ENCRA(COLOR(ww), i);
2426 else
2427 p->n_reg |= ENCRA(COLOR(w), i);
2428 w++;
2429 }
2430 #ifdef notdef
2431 if (p->n_op == ASSIGN && p->n_left->n_op == REG &&
2432 DECRA(p->n_reg, 0) != regno(p->n_left))
2433 comperr("paint: %p clashing ASSIGN moves; %d != %d", p,
2434 DECRA(p->n_reg, 0), regno(p->n_left));
2435 #endif
2436 } else
2437 p->n_reg = -1;
2438 if (p->n_op == TEMP) {
2439 REGW *nb = &nblock[regno(p)];
2440 regno(p) = COLOR(nb);
2441 if (TCLASS(p->n_su) == 0)
2442 SCLASS(p->n_su, CLASS(nb));
2443 p->n_op = REG;
2444 setlval(p, 0);
2445 }
2446 }
2447
2448 /*
2449 * See if this node have a move that has been removed in Freeze
2450 * but as we can make use of anyway.
2451 */
2452 static int
2453 colfind(int okColors, REGW *r)
2454 {
2455 REGW *w;
2456 MOVL *m;
2457 int c;
2458
2459 for (m = MOVELIST(r); m; m = m->next) {
2460 if ((w = m->regm->src) == r)
2461 w = m->regm->dst;
2462 w = GetAlias(w);
2463 if (ONLIST(w) != &coloredNodes && ONLIST(w) != &precolored)
2464 continue; /* Not yet colored */
2465 if (CLASS(w) != CLASS(r))
2466 comperr("colfind: move between classes");
2467
2468 for (c = 0; c < regK[CLASS(w)]; c++)
2469 if (color2reg(c, CLASS(w)) == COLOR(w))
2470 break;
2471 if (c == regK[CLASS(w)])
2472 comperr("colfind: out of reg number");
2473
2474 if (((1 << c) & okColors) == 0) {
2475 RDEBUG(("colfind: Failed coloring as %d\n", ASGNUM(w)));
2476 continue;
2477 }
2478 RDEBUG(("colfind: Recommend color from %d\n", ASGNUM(w)));
2479 return COLOR(w);
2480 }
2481 return color2reg(ffs(okColors)-1, CLASS(r));
2482 }
2483
2484 static void
2485 AssignColors(struct interpass *ip)
2486 {
2487 struct interpass *ip2;
2488 int okColors, c;
2489 REGW *o, *w;
2490 ADJL *x;
2491
2492 RDEBUG(("AssignColors\n"));
2493 while (!WLISTEMPTY(selectStack)) {
2494 w = POPWLIST(selectStack);
2495 okColors = classmask(CLASS(w));
2496 #ifdef PCC_DEBUG
2497 RDEBUG(("classmask av %d, class %d: %x\n",
2498 w->nodnum, CLASS(w), okColors));
2499 #endif
2500
2501 for (x = ADJLIST(w); x; x = x->r_next) {
2502 o = GetAlias(x->a_temp);
2503 #ifdef PCC_DEBUG
2504 RRDEBUG(("Adj(%d): %d (%d)\n",
2505 ASGNUM(w), ASGNUM(o), ASGNUM(x->a_temp)));
2506 #endif
2507
2508 if (ONLIST(o) == &coloredNodes ||
2509 ONLIST(o) == &precolored) {
2510 c = aliasmap(CLASS(w), COLOR(o));
2511 RRDEBUG(("aliasmap in class %d by color %d: "
2512 "%x, okColors %x\n",
2513 CLASS(w), COLOR(o), c, okColors));
2514
2515 okColors &= ~c;
2516 }
2517 }
2518 if (okColors == 0) {
2519 PUSHWLIST(w, spilledNodes);
2520 #ifdef PCC_DEBUG
2521 RDEBUG(("Spilling node %d\n", ASGNUM(w)));
2522 #endif
2523 } else {
2524 COLOR(w) = colfind(okColors, w);
2525 PUSHWLIST(w, coloredNodes);
2526 #ifdef PCC_DEBUG
2527 RDEBUG(("Coloring %d with %s, free %x\n",
2528 ASGNUM(w), rnames[COLOR(w)], okColors));
2529 #endif
2530 }
2531 }
2532 DLIST_FOREACH(w, &coalescedNodes, link) {
2533 REGW *ww = GetAlias(w);
2534 COLOR(w) = COLOR(ww);
2535 if (ONLIST(ww) == &spilledNodes) {
2536 #ifdef PCC_DEBUG
2537 RDEBUG(("coalesced node %d spilled\n", w->nodnum));
2538 #endif
2539 ww = DLIST_PREV(w, link);
2540 DLIST_REMOVE(w, link);
2541 PUSHWLIST(w, spilledNodes);
2542 w = ww;
2543 } else {
2544 #ifdef PCC_DEBUG
2545 RDEBUG(("Giving coalesced node %d color %s\n",
2546 w->nodnum, rnames[COLOR(w)]));
2547 #endif
2548 }
2549 }
2550
2551 #ifdef PCC_DEBUG
2552 if (r2debug)
2553 DLIST_FOREACH(w, &coloredNodes, link)
2554 printf("%d: color %s\n", ASGNUM(w), rnames[COLOR(w)]);
2555 #endif
2556 if (DLIST_ISEMPTY(&spilledNodes, link)) {
2557 DLIST_FOREACH(ip2, ip, qelem)
2558 if (ip2->type == IP_NODE)
2559 walkf(ip2->ip_node, paint, 0);
2560 }
2561 }
2562
2563 static REGW *spole, *longsp;
2564 /*
2565 * Store all spilled nodes in memory by fetching a temporary on the stack.
2566 * Will never end up here if not optimizing.
2567 */
2568 static void
2569 longtemp(NODE *p, void *arg)
2570 {
2571 REGW *w;
2572
2573 if (p->n_op != TEMP)
2574 return;
2575 /* XXX - should have a bitmask to find temps to convert */
2576 DLIST_FOREACH(w, spole, link) {
2577 if (w != &nblock[regno(p)])
2578 continue;
2579 #ifdef MYLONGTEMP
2580 MYLONGTEMP(p, w);
2581 #endif
2582 if (w->r_class == 0) {
2583 w->r_color = freetemp(szty(p->n_type));
2584 w->r_class = FPREG; /* XXX - assumption? */
2585 }
2586 storemod(p, w->r_color, w->r_class);
2587 break;
2588 }
2589 }
2590
2591 /*
2592 * Check if this node is just something directly addressable.
2593 * XXX - should use target canaddr() when we knows that it is OK
2594 * for all targets. Can currently be a little too optimistic.
2595 */
2596 static int
2597 regcanaddr(NODE *p)
2598 {
2599 int o = p->n_op;
2600
2601 if (o==NAME || o==ICON || o==OREG )
2602 return 1;
2603 if (o == UMUL) {
2604 if (p->n_left->n_op == REG || p->n_left->n_op == TEMP)
2605 return 1;
2606 if ((p->n_left->n_op == PLUS || p->n_left->n_op == MINUS) &&
2607 (p->n_left->n_left->n_op == REG ||
2608 p->n_left->n_left->n_op == TEMP) &&
2609 p->n_left->n_right->n_op == ICON)
2610 return 1;
2611 }
2612 return 0;
2613 }
2614
2615 static struct interpass *cip;
2616
2617 static NODE *
2618 shstore(NODE *p, struct interpass *ipp, REGW *w)
2619 {
2620 struct interpass *ip;
2621 int off;
2622 NODE *l;
2623
2624 off = freetemp(szty(p->n_type));
2625 l = storenode(p->n_type, off);
2626
2627 ip = ipnode(mkbinode(ASSIGN, storenode(p->n_type, off), p, p->n_type));
2628 DLIST_INSERT_BEFORE(ipp, ip, qelem);
2629 DLIST_REMOVE(w, link);
2630 return l;
2631 }
2632
2633 /*
2634 * Rewrite a tree by storing a variable in memory.
2635 * This code would work better if the SU computations were smarter.
2636 * XXX - must check if basic block structure is destroyed!
2637 *
2638 * 1) Ensure that both left & right are directly addressable.
2639 * 2) Save interfering long-term temporaries.
2640 * 3) If node itself not addressable, store the node itself.
2641 * 4) Store the parent.
2642 * 5) ...HELP!!??!!
2643 *
2644 * It might be a better idea to start with 3) or 4) first, but that will
2645 * make the code more complicated and I'm not sure it's worth it.
2646 */
2647 static int
2648 shorttemp(NODE *p, NODE *parent, REGW *w)
2649 {
2650 struct interpass *nip;
2651 ADJL *ll;
2652 NODE *l, *r;
2653 int off, i, nc;
2654
2655 if (p->n_regw == NULL)
2656 goto down;
2657
2658 /* Check if this the correct node. */
2659 nc = p->n_su == -1 ? 0 : ncnt(table[TBLIDX(p->n_su)].needs);
2660 for (i = 0; i < nc + 1; i++)
2661 if ((p->n_regw + i) == w)
2662 break;
2663
2664 if (i == nc + 1) {
2665 down: switch (optype(p->n_op)) {
2666 case BITYPE:
2667 if (shorttemp(p->n_left, p, w) == 0)
2668 return shorttemp(p->n_right, p, w);
2669 return 1;
2670 case UTYPE:
2671 return shorttemp(p->n_left, p, w);
2672 case LTYPE:
2673 return 0;
2674 }
2675 }
2676
2677 RDEBUG(("Node %d (%p) to store\n", ASGNUM(w), p));
2678 /* ensure that both left and right are addressable */
2679 if (!regcanaddr(p) && !callop(p->n_op)) {
2680 /* this is neither leaf nor addressable */
2681 if (p->n_op != ASSIGN && !regcanaddr(p->n_left)) {
2682 /* store left */
2683 p->n_left = shstore(p->n_left, cip, w);
2684 RDEBUG(("Node %d stored left\n", ASGNUM(w)));
2685 return 1;
2686 } else if (optype(p->n_op) == BITYPE &&
2687 !regcanaddr(p->n_right)) {
2688 /* store right */
2689 p->n_right = shstore(p->n_right, cip, w);
2690 RDEBUG(("Node %d stored right\n", ASGNUM(w)));
2691 return 1;
2692 }
2693 } else if (callop(p->n_op) && parent && parent->n_op != ASSIGN) {
2694 /* can spill this node */
2695 goto dospill;
2696 }
2697
2698 /* Store long-term temps that interferes */
2699 ll = w->r_adjList;
2700 for (; ll; ll = ll->r_next) {
2701 if (ll->a_temp < &nblock[tempmax] &&
2702 ll->a_temp >= &nblock[tempmin]) {
2703 longsp = ll->a_temp;
2704 RDEBUG(("Stored long %d\n", ASGNUM(longsp)));
2705 return 1; /* try again */
2706 }
2707 }
2708
2709 if (regcanaddr(p)) {
2710 /* The node to spill is addressable, spill parent */
2711 if (parent == NULL)
2712 comperr("cannot spill TOP node!");
2713 p = parent;
2714 }
2715
2716 dospill:
2717 off = freetemp(szty(p->n_type));
2718 l = storenode(p->n_type, off);
2719 r = talloc();
2720 *r = *p;
2721 nip = ipnode(mkbinode(ASSIGN, l, r, p->n_type));
2722 storemod(p, off, FPREG); /* XXX */
2723 DLIST_INSERT_BEFORE(cip, nip, qelem);
2724 DLIST_REMOVE(w, link);
2725 RDEBUG(("Stored parent node %d (%p)\n", ASGNUM(w), p));
2726 return 1;
2727 }
2728
2729 static void
2730 dlr(REGW *w)
2731 {
2732 if (w == 0)
2733 return;
2734 DLIST_REMOVE(w, link);
2735 RDEBUG(("Removing %d\n", ASGNUM(w)));
2736 }
2737
2738 /*
2739 * Return the number of the topmost temporary that should be spilled.
2740 * If temps below found, remove them from the spill list.
2741 * If two temps at the same level are found, remove one.
2742 * If both left & right have temps, store right and leave left.
2743 */
2744 static REGW *
2745 toptemp(NODE *p, REGW *rw)
2746 {
2747 REGW *r, *l, *rv, *w;
2748 int nc, i;
2749
2750 l = r = rv = NULL;
2751 if (optype(p->n_op) != LTYPE)
2752 l = toptemp(p->n_left, rw);
2753 if (optype(p->n_op) == BITYPE)
2754 r = toptemp(p->n_right, rw);
2755 DLIST_FOREACH(w, rw, link) {
2756 nc = p->n_su == -1 ? 0 : ncnt(table[TBLIDX(p->n_su)].needs);
2757 for (i = 0; i < nc + 1; i++) {
2758 if ((p->n_regw + i) != w)
2759 continue;
2760 RDEBUG(("Found %d \n", ASGNUM(w)));
2761 dlr(rv);
2762 rv = w;
2763 }
2764 }
2765 if (rv != NULL) {
2766 dlr(l);
2767 dlr(r);
2768 } else if (r != NULL) {
2769 /* pick right, not left */
2770 rv = r;
2771 dlr(l);
2772 } else {
2773 rv = l;
2774 }
2775 return rv;
2776 }
2777
2778 static void leafrewrite(struct interpass *ipole, REGW *rpole);
2779 /*
2780 * Change the TEMPs in the ipole list to stack variables.
2781 */
2782 static void
2783 treerewrite(struct interpass *ipole, REGW *rpole)
2784 {
2785 struct interpass *ip;
2786 REGW *w, longregs;
2787
2788 spole = rpole;
2789
2790 DLIST_FOREACH(ip, ipole, qelem) {
2791 if (ip->type != IP_NODE)
2792 continue;
2793 if ((w = toptemp(ip->ip_node, rpole)) == 0)
2794 continue;
2795 cip = ip;
2796 shorttemp(ip->ip_node, NULL, w); /* convert temps to oregs */
2797 }
2798 if (longsp) {
2799 #ifdef PCC_DEBUG
2800 RDEBUG(("Storing node %d to save short\n", ASGNUM(longsp)));
2801 #endif
2802 if (longsp >= &nblock[tempmin] && longsp < &nblock[basetemp]) {
2803 int num = (int)(longsp - nblock) - tempmin;
2804 nsavregs[num] = 1;
2805 } else {
2806 DLIST_INIT(&longregs, link);
2807 DLIST_INSERT_AFTER(&longregs, longsp, link);
2808 leafrewrite(ipole, &longregs);
2809 }
2810 } else if (!DLIST_ISEMPTY(spole, link))
2811 comperr("treerewrite not empty");
2812 }
2813
2814 /*
2815 * Change the TEMPs in the ipole list to stack variables.
2816 */
2817 static void
2818 leafrewrite(struct interpass *ipole, REGW *rpole)
2819 {
2820 extern NODE *nodepole;
2821 extern int thisline;
2822 struct interpass *ip;
2823
2824 spole = rpole;
2825 DLIST_FOREACH(ip, ipole, qelem) {
2826 if (ip->type != IP_NODE)
2827 continue;
2828 nodepole = ip->ip_node;
2829 thisline = ip->lineno;
2830 walkf(ip->ip_node, longtemp, 0); /* convert temps to oregs */
2831 }
2832 nodepole = NIL;
2833 }
2834
2835 /*
2836 * Avoid copying spilled argument to new position on stack.
2837 */
2838 static int
2839 temparg(struct interpass *ipole, REGW *w)
2840 {
2841 struct interpass *ip;
2842 NODE *p;
2843 int reg;
2844
2845 ip = DLIST_NEXT(ipole, qelem); /* PROLOG */
2846 while (ip->type != IP_DEFLAB)
2847 ip = DLIST_NEXT(ip, qelem);
2848 ip = DLIST_NEXT(ip, qelem); /* first NODE */
2849 for (; ip->type != IP_DEFLAB; ip = DLIST_NEXT(ip, qelem)) {
2850 if (ip->type == IP_ASM)
2851 continue;
2852 p = ip->ip_node;
2853 #ifdef notdef
2854 /* register args may already have been put on stack */
2855 if (p->n_op != ASSIGN || p->n_left->n_op != TEMP)
2856 comperr("temparg");
2857 #endif
2858 if (p->n_op != ASSIGN || p->n_left->n_op != TEMP)
2859 continue; /* unknown tree */
2860
2861 if (p->n_right->n_op != OREG)
2862 continue; /* arg in register */
2863 if (w != &nblock[regno(p->n_left)])
2864 continue;
2865 w->r_color = (int)getlval(p->n_right);
2866 reg = regno(p->n_right);
2867 tfree(p);
2868 /* Cannot DLIST_REMOVE here, would break basic blocks */
2869 /* Make it a nothing instead */
2870 ip->type = IP_ASM;
2871 ip->ip_asm = "";
2872 return reg;
2873 }
2874 return 0;
2875 }
2876
2877 #define ONLYPERM 1
2878 #define LEAVES 2
2879 #define SMALL 3
2880
2881 /*
2882 * Scan the whole function and search for temporaries to be stored
2883 * on-stack.
2884 *
2885 * Be careful to not destroy the basic block structure in the first scan.
2886 */
2887 static int
2888 RewriteProgram(struct interpass *ip)
2889 {
2890 REGW shortregs, longregs, saveregs, *q;
2891 REGW *w;
2892 int rwtyp;
2893
2894 RDEBUG(("RewriteProgram\n"));
2895 DLIST_INIT(&shortregs, link);
2896 DLIST_INIT(&longregs, link);
2897 DLIST_INIT(&saveregs, link);
2898
2899 /* sort the temporaries in three queues, short, long and perm */
2900 while (!DLIST_ISEMPTY(&spilledNodes, link)) {
2901 w = DLIST_NEXT(&spilledNodes, link);
2902 DLIST_REMOVE(w, link);
2903
2904 if (w >= &nblock[tempmin] && w < &nblock[basetemp]) {
2905 q = &saveregs;
2906 } else if (w >= &nblock[basetemp] && w < &nblock[tempmax]) {
2907 q = &longregs;
2908 } else
2909 q = &shortregs;
2910 DLIST_INSERT_AFTER(q, w, link);
2911 }
2912 #ifdef PCC_DEBUG
2913 if (r2debug) {
2914 printf("permanent: ");
2915 DLIST_FOREACH(w, &saveregs, link)
2916 printf("%d ", ASGNUM(w));
2917 printf("\nlong-lived: ");
2918 DLIST_FOREACH(w, &longregs, link)
2919 printf("%d ", ASGNUM(w));
2920 printf("\nshort-lived: ");
2921 DLIST_FOREACH(w, &shortregs, link)
2922 printf("%d ", ASGNUM(w));
2923 printf("\n");
2924 }
2925 #endif
2926 rwtyp = 0;
2927
2928 if (!DLIST_ISEMPTY(&saveregs, link)) {
2929 rwtyp = ONLYPERM;
2930 DLIST_FOREACH(w, &saveregs, link) {
2931 int num = (int)(w - nblock) - tempmin;
2932 nsavregs[num] = 1;
2933 }
2934 }
2935 if (!DLIST_ISEMPTY(&longregs, link)) {
2936 rwtyp = LEAVES;
2937 DLIST_FOREACH(w, &longregs, link) {
2938 w->r_class = xtemps ? temparg(ip, w) : 0;
2939 }
2940 }
2941
2942 if (rwtyp == LEAVES) {
2943 leafrewrite(ip, &longregs);
2944 rwtyp = ONLYPERM;
2945 }
2946
2947 if (rwtyp == 0 && !DLIST_ISEMPTY(&shortregs, link)) {
2948 /* Must rewrite the trees */
2949 treerewrite(ip, &shortregs);
2950 #if 0
2951 if (xtemps)
2952 comperr("treerewrite");
2953 #endif
2954 rwtyp = SMALL;
2955 }
2956
2957 RDEBUG(("savregs %x rwtyp %d\n", 0, rwtyp));
2958
2959 return rwtyp;
2960 }
2961
2962 #ifdef PCC_DEBUG
2963 /*
2964 * Print TEMP/REG contents in a node.
2965 */
2966 void
2967 prtreg(NODE *p)
2968 {
2969 int i, n = p->n_su == -1 ? 0 : ncnt(table[TBLIDX(p->n_su)].needs);
2970 if (p->n_reg == -1) goto foo;
2971 if (use_regw || p->n_reg > 0x40000000 || p->n_reg < 0) {
2972 printf("TEMP ");
2973 if (p->n_regw != NULL) {
2974 for (i = 0; i < n+1; i++)
2975 printf("%d ", p->n_regw[i].nodnum);
2976 } else
2977 printf("<undef>");
2978 } else {
2979 foo: printf("REG ");
2980 if (p->n_reg != -1) {
2981 for (i = 0; i < n+1; i++) {
2982 int r = DECRA(p->n_reg, i);
2983 if (r >= MAXREGS)
2984 printf("<badreg> ");
2985 else
2986 printf("%s ", rnames[r]);
2987 }
2988 } else
2989 printf("<undef>");
2990 }
2991 }
2992 #endif
2993
2994 #ifdef notyet
2995 /*
2996 * Assign instructions, calculate evaluation order and
2997 * set temporary register numbers.
2998 */
2999 static void
3000 insgen()
3001 {
3002 clrsu(p);
3003 geninsn(); /* instruction assignment */
3004 sucomp(); /* set evaluation order */
3005 slong(); /* set long temp types */
3006 sshort(); /* set short temp numbers */
3007 }
3008 #endif
3009
3010 /*
3011 * Do register allocation for trees by graph-coloring.
3012 */
3013 void
3014 ngenregs(struct p2env *p2e)
3015 {
3016 struct interpass *ipole = &p2e->ipole;
3017 extern NODE *nodepole;
3018 struct interpass *ip;
3019 int i, j, tbits;
3020 int uu[NPERMREG] = { -1 };
3021 int xnsavregs[NPERMREG];
3022 int beenhere = 0;
3023 TWORD type;
3024
3025 DLIST_INIT(&lunused, link);
3026 DLIST_INIT(&lused, link);
3027
3028 /*
3029 * Do some setup before doing the real thing.
3030 */
3031 tempmin = p2e->ipp->ip_tmpnum;
3032
3033 /*
3034 * Allocate space for the permanent registers in the
3035 * same block as the long-lived temporaries.
3036 * These temporaries will be handled the same way as
3037 * all other variables.
3038 */
3039 basetemp = tempmin;
3040 nsavregs = xnsavregs;
3041 for (i = 0; i < NPERMREG; i++)
3042 xnsavregs[i] = 0;
3043 ndontregs = uu; /* currently never avoid any regs */
3044
3045 tempmin -= (NPERMREG-1);
3046 #ifdef notyet
3047 if (xavoidfp)
3048 dontregs |= REGBIT(FPREG);
3049 #endif
3050
3051 /* Block for precolored nodes */
3052 ablock = tmpalloc(sizeof(REGW)*MAXREGS);
3053 memset(ablock, 0, sizeof(REGW)*MAXREGS);
3054 for (i = 0; i < MAXREGS; i++) {
3055 ablock[i].r_onlist = &precolored;
3056 ablock[i].r_class = GCLASS(i); /* XXX */
3057 ablock[i].r_color = i;
3058 #ifdef PCC_DEBUG
3059 ablock[i].nodnum = i;
3060 #endif
3061 }
3062
3063 ssagain:
3064 tempmax = p2e->epp->ip_tmpnum;
3065 #ifdef PCC_DEBUG
3066 nodnum = tempmax;
3067 #endif
3068 tbits = tempmax - tempmin; /* # of temporaries */
3069 xbits = tbits + MAXREGS; /* total size of live array */
3070 if (tbits) {
3071 nblock = tmpalloc(tbits * sizeof(REGW));
3072
3073 nblock -= tempmin;
3074 #ifdef HAVE_C99_FORMAT
3075 RDEBUG(("nblock %p num %d size %zu\n",
3076 nblock, tbits, (size_t)(tbits * sizeof(REGW))));
3077 #endif
3078 }
3079 live = tmpalloc(BIT2BYTE(xbits));
3080
3081 #ifdef notyet
3082 TMPMARK();
3083 #endif
3084
3085
3086 recalc:
3087 onlyperm: /* XXX - should not have to redo all */
3088 memset(edgehash, 0, sizeof(edgehash));
3089
3090 /* clear adjacent node list */
3091 for (i = 0; i < MAXREGS; i++)
3092 for (j = 0; j < NUMCLASS+1; j++)
3093 NCLASS(&ablock[i], j) = 0;
3094
3095 if (tbits) {
3096 memset(nblock+tempmin, 0, tbits * sizeof(REGW));
3097 #ifdef PCC_DEBUG
3098 for (i = tempmin; i < tempmax; i++)
3099 nblock[i].nodnum = i;
3100 #endif
3101 }
3102 memset(live, 0, BIT2BYTE(xbits));
3103 RPRINTIP(ipole);
3104 DLIST_INIT(&initial, link);
3105 ntsz = 0;
3106 DLIST_FOREACH(ip, ipole, qelem) {
3107 extern int thisline;
3108 if (ip->type != IP_NODE)
3109 continue;
3110 nodepole = ip->ip_node;
3111 thisline = ip->lineno;
3112 if (ip->ip_node->n_op != XASM) {
3113 clrsu(ip->ip_node);
3114 geninsn(ip->ip_node, FOREFF);
3115 }
3116 nsucomp(ip->ip_node);
3117 walkf(ip->ip_node, traclass, 0);
3118 }
3119 nodepole = NIL;
3120 RDEBUG(("nsucomp allocated %d temps (%d,%d)\n",
3121 tempmax-tempmin, tempmin, tempmax));
3122
3123 #ifdef PCC_DEBUG
3124 use_regw = 1;
3125 RPRINTIP(ipole);
3126 use_regw = 0;
3127 #endif
3128 RDEBUG(("ngenregs: numtemps %d (%d, %d)\n", tempmax-tempmin,
3129 tempmin, tempmax));
3130
3131 DLIST_INIT(&coalescedMoves, link);
3132 DLIST_INIT(&constrainedMoves, link);
3133 DLIST_INIT(&frozenMoves, link);
3134 DLIST_INIT(&worklistMoves, link);
3135 DLIST_INIT(&activeMoves, link);
3136
3137 /* Set class and move-related for perm regs */
3138 for (i = 0; i < (NPERMREG-1); i++) {
3139 if (nsavregs[i])
3140 continue;
3141 nblock[i+tempmin].r_class = GCLASS(permregs[i]);
3142 DLIST_INSERT_AFTER(&initial, &nblock[i+tempmin], link);
3143 moveadd(&nblock[i+tempmin], &ablock[permregs[i]]);
3144 addalledges(&nblock[i+tempmin]);
3145 }
3146
3147 Build(p2e);
3148 RDEBUG(("Build done\n"));
3149 MkWorklist();
3150 RDEBUG(("MkWorklist done\n"));
3151 Coalassign(p2e);
3152 do {
3153 if (!WLISTEMPTY(simplifyWorklist))
3154 Simplify();
3155 else if (!WLISTEMPTY(worklistMoves))
3156 Coalesce();
3157 else if (!WLISTEMPTY(freezeWorklist))
3158 Freeze();
3159 else if (!WLISTEMPTY(spillWorklist))
3160 SelectSpill();
3161 } while (!WLISTEMPTY(simplifyWorklist) || !WLISTEMPTY(worklistMoves) ||
3162 !WLISTEMPTY(freezeWorklist) || !WLISTEMPTY(spillWorklist));
3163 AssignColors(ipole);
3164
3165 RDEBUG(("After AssignColors\n"));
3166 RPRINTIP(ipole);
3167
3168 if (!WLISTEMPTY(spilledNodes)) {
3169 switch (RewriteProgram(ipole)) {
3170 case ONLYPERM:
3171 goto onlyperm;
3172 case SMALL:
3173 optimize(p2e);
3174 if (beenhere++ == MAXLOOP)
3175 comperr("cannot color graph - COLORMAP() bug?");
3176 if (xssa)
3177 goto ssagain;
3178 goto recalc;
3179 }
3180 }
3181
3182 /* fill in regs to save */
3183 memset(p2e->p_regs, 0, sizeof(p2e->p_regs));
3184 for (i = 0; i < NPERMREG-1; i++) {
3185 NODE *p;
3186
3187 if (nsavregs[i]) {
3188 BITSET(p2e->p_regs, permregs[i]);
3189 continue; /* Spilled */
3190 }
3191 if (nblock[i+tempmin].r_color == permregs[i])
3192 continue; /* Coalesced */
3193 /*
3194 * If the original color of this permreg is used for
3195 * coloring another register, swap them to avoid
3196 * unnecessary moves.
3197 */
3198 for (j = i+1; j < NPERMREG-1; j++) {
3199 if (nblock[j+tempmin].r_color != permregs[i])
3200 continue;
3201 nblock[j+tempmin].r_color = nblock[i+tempmin].r_color;
3202 break;
3203 }
3204 if (j != NPERMREG-1)
3205 continue;
3206
3207 /* Generate reg-reg move nodes for save */
3208 type = PERMTYPE(permregs[i]);
3209 #ifdef PCC_DEBUG
3210 if (PERMTYPE(nblock[i+tempmin].r_color) != type)
3211 comperr("permreg botch");
3212 #endif
3213 p = mkbinode(ASSIGN,
3214 mklnode(REG, 0, nblock[i+tempmin].r_color, type),
3215 mklnode(REG, 0, permregs[i], type), type);
3216 p->n_reg = p->n_left->n_reg = p->n_right->n_reg = -1;
3217 clrsu(p);
3218 geninsn(p, FOREFF);
3219 ip = ipnode(p);
3220 DLIST_INSERT_AFTER(ipole->qelem.q_forw, ip, qelem);
3221 p = mkbinode(ASSIGN, mklnode(REG, 0, permregs[i], type),
3222 mklnode(REG, 0, nblock[i+tempmin].r_color, type), type);
3223 p->n_reg = p->n_left->n_reg = p->n_right->n_reg = -1;
3224 clrsu(p);
3225 geninsn(p, FOREFF);
3226 ip = ipnode(p);
3227 DLIST_INSERT_BEFORE(ipole->qelem.q_back, ip, qelem);
3228 }
3229 if (ntsz)
3230 stktemp = freetemp(ntsz);
3231 /* Done! */
3232 }