summary refs log tree commit diff
path: root/lisc/live.c
blob: 0e59d784cb27f335fffb9b36a2e6229337570659 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "lisc.h"

Bits
liveon(Blk *b, Blk *s)
{
	Bits v;
	Phi *p;
	uint a;

	v = s->in;
	for (p=s->phi; p; p=p->link) {
		BCLR(v, p->to.val);
		for (a=0; a<p->narg; a++)
			if (p->blk[a] == b)
			if (rtype(p->arg[a]) == RTmp)
				BSET(v, p->arg[a].val);
	}
	return v;
}

static void
phifix(int t1, short *phi, Tmp *tmp)
{
	int t, t2;

	/* detect temporaries in the same
	 * phi-class that interfere and
	 * separate them
	 */
	t = phirepr(tmp, t1);
	t2 = phi[t];
	if (t2 && t2 != t1) {
		if (tmp[t1].phi != t1) {
			tmp[t1].phi = t1;
			t = t1;
		} else {
			tmp[t2].phi = t2;
			phi[t2] = t2;
		}
	}
	phi[t] = t1;
}

static void
bset(Ref r, Blk *b, int *nlv, short *phi, Tmp *tmp)
{

	if (rtype(r) != RTmp)
		return;
	BSET(b->gen, r.val);
	phifix(r.val, phi, tmp);
	if (!BGET(b->in, r.val)) {
		++*nlv;
		BSET(b->in, r.val);
	}
}

/* liveness analysis
 * requires rpo computation
 */
void
filllive(Fn *f)
{
	Blk *b;
	Ins *i;
	int t, z, m, n, chg, nlv;
	short *phi;
	Bits u, v;
	Mem *ma;

	assert(f->ntmp <= NBit*BITS);
	phi = emalloc(f->ntmp * sizeof phi[0]);
	for (b=f->start; b; b=b->link) {
		b->in = (Bits){{0}};
		b->out = (Bits){{0}};
		b->gen = (Bits){{0}};
	}
	chg = 1;
Again:
	for (n=f->nblk-1; n>=0; n--) {
		b = f->rpo[n];

		u = b->out;
		if (b->s1) {
			v = liveon(b, b->s1);
			for (z=0; z<BITS; z++)
				b->out.t[z] |= v.t[z];
		}
		if (b->s2) {
			v = liveon(b, b->s2);
			for (z=0; z<BITS; z++)
				b->out.t[z] |= v.t[z];
		}
		chg |= memcmp(&b->out, &u, sizeof(Bits));

		memset(phi, 0, f->ntmp * sizeof phi[0]);
		b->in = b->out;
		nlv = bcnt(&b->in);
		for (t=0; t<f->ntmp; t++)
			if (BGET(b->in, t))
				phifix(t, phi, f->tmp);
		bset(b->jmp.arg, b, &nlv, phi, f->tmp);
		b->nlive = nlv;
		for (i=&b->ins[b->nins]; i!=b->ins;) {
			if ((--i)->op == OCall) {
				b->in.t[0] &= ~calldef(*i, &m);
				nlv -= m;
				if (nlv + NRSave > b->nlive)
					b->nlive = nlv + NRSave;
				b->in.t[0] |= calluse(*i, &m);
				nlv += m;
			}
			if (!req(i->to, R)) {
				assert(rtype(i->to) == RTmp);
				nlv -= BGET(b->in, i->to.val);
				BSET(b->gen, i->to.val);
				BCLR(b->in, i->to.val);
				t = phirepr(f->tmp, i->to.val);
				phi[t] = 0;
			}
			for (m=0; m<2; m++)
				switch (rtype(i->arg[m])) {
				case RAMem:
					ma = &f->mem[i->arg[m].val & AMask];
					bset(ma->base, b, &nlv, phi, f->tmp);
					bset(ma->index, b, &nlv, phi, f->tmp);
					break;
				default:
					bset(i->arg[m], b, &nlv, phi, f->tmp);
					break;
				}
			if (nlv > b->nlive)
				b->nlive = nlv;
		}
	}
	if (chg) {
		chg = 0;
		goto Again;
	}
	free(phi);

	if (debug['L']) {
		fprintf(stderr, "\n> Liveness analysis:\n");
		for (b=f->start; b; b=b->link) {
			fprintf(stderr, "\t%-10sin:   ", b->name);
			dumpts(&b->in, f->tmp, stderr);
			fprintf(stderr, "\t          out:  ");
			dumpts(&b->out, f->tmp, stderr);
			fprintf(stderr, "\t          gen:  ");
			dumpts(&b->gen, f->tmp, stderr);
			fprintf(stderr, "\t          live: %d\n", b->nlive);
		}
	}
}