summary refs log tree commit diff
path: root/lisc/rega.c
blob: b839538f5ec68ff0fba7310210f2b6f461ca4ac1 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "lisc.h"


typedef struct RMap RMap;
struct RMap {
	int t[NReg];
	int r[NReg];
	Bits bt;
	Bits br;
	int n;
};

extern Ins insb[NIns], *curi;
static Tmp *tmp;       /* function temporaries */
static struct {
	Ref src, dst;
} *pm;                 /* parallel move constructed */
static int cpm, npm;   /* capacity and size of pm */

static int
rfind(RMap *m, int t)
{
	int i;

	for (i=0; i<m->n; i++)
		if (m->t[i] == t)
			return m->r[i];
	return -1;
}

static Ref
rref(RMap *m, int t)
{
	int r, s;

	r = rfind(m, t);
	if (r == -1) {
		s = tmp[t].spill;
		assert(s && "should have spilled");
		return SLOT(s);
	} else
		switch (tmp[t].type) {
		default:
			assert(!"invalid temporary");
		case TWord:
			return REG(LTW(r));
		case TLong:
			return REG(r);
		}
}

static void
radd(RMap *m, int t, int r)
{
	assert(RAX <= r && r < RAX + NReg && "invalid register");
	assert(!BGET(m->bt, t) && "temp has mapping");
	assert(!BGET(m->br, r) && "reg already allocated");
	assert(m->n <= NReg && "too many mappings");
	BSET(m->bt, t);
	BSET(m->br, r);
	m->t[m->n] = t;
	m->r[m->n] = r;
	m->n++;
}

static Ref
ralloc(RMap *m, int t)
{
	int r;

	if (BGET(m->bt, t)) {
		r = rfind(m, t);
		assert(r != -1);
	} else {
		r = tmp[t].hint;
		if (r == -1 || BGET(m->br, r))
			for (r=RAX; BGET(m->br, r); r++)
				;
		radd(m, t, r);
		if (tmp[t].hint == -1)
			tmp[t].hint = r;
	}
	return REG(r);
}

static int
rfree(RMap *m, int t)
{
	int i, r;

	if (!BGET(m->bt, t))
		return -1;
	for (i=0; m->t[i] != t; i++)
		assert(i+1 < m->n);
	r = m->r[i];
	BCLR(m->bt, t);
	BCLR(m->br, r);
	m->n--;
	memmove(&m->t[i], &m->t[i+1], (m->n-i) * m->t[0]);
	memmove(&m->r[i], &m->r[i+1], (m->n-i) * m->r[0]);
	return r;
}

static void
mdump(RMap *m)
{
	int i;

	for (i=0; i<m->n; i++)
		fprintf(stderr, " (%s, reg%d)",
			tmp[m->t[i]].name,
			m->r[i]);
	fprintf(stderr, "\n");
}

static void
pmadd(Ref src, Ref dst)
{
	if (npm == cpm) {
		cpm = cpm * 2 + 16;
		pm = realloc(pm, cpm * sizeof pm[0]);
		if (!pm)
			diag("pmadd: out of memory");
	}
	pm[npm].src = src;
	pm[npm].dst = dst;
	npm++;
}

enum PMStat { ToMove, Moving, Moved };

static Ref
pmrec(enum PMStat *status, int i)
{
	Ref swp, swp1;
	int j;

	if (req(pm[i].src, pm[i].dst))
		return R;
	status[i] = Moving;
	swp = R;
	for (j=0; j<npm; j++) {
		if (req(pm[j].src, pm[i].dst))
			switch (status[j]) {
			case ToMove:
				swp1 = pmrec(status, j);
				assert(req(swp, R) || req(swp1, R));
				swp = swp1;
				break;
			case Moving:
				assert(req(swp, R));
				swp = pm[i].dst;
				break;
			case Moved:
				break;
			}
	}
	status[i] = Moved;
	if (req(swp, R)) {
		*curi++ = (Ins){OCopy, pm[i].dst, {pm[i].src, R}};
		return R;
	} else if (!req(swp, pm[i].src)) {
		*curi++ = (Ins){OSwap, R, {pm[i].src, pm[i].dst}};
		return swp;
	} else
		return R;

}

static void
pmgen()
{
	int i;
	enum PMStat *status;

	status = alloc(npm * sizeof status[0]);
	assert(!npm || status[npm-1] == ToMove);
	curi = insb;
	for (i=0; i<npm; i++)
		if (status[i] == ToMove)
			pmrec(status, i);
	free(status);
}

static Ins *
dopm(Blk *b, Ins *i, RMap *m)
{
	int n, r, t, nins;
	Ref rt;
	Ins *i1, *ib, *ip, *ir;

	npm = 0;
	i1 = i+1;
	if (rtype(i->to) == RReg)
		for (;; i--) {
			r = i->to.val;
			rt = i->arg[0];
			assert(rtype(rt) == RTmp);
			assert(BGET(m->br, r));
			/* todo, assert that r is not mapped */
			BCLR(m->br, r);
			rt = ralloc(m, rt.val);
			pmadd(rt, i->to);
			if (i==b->ins
			|| (i-1)->op!=OCopy
			|| rtype((i-1)->to) != RReg)
				break;
		}
	else if (rtype(i->arg[0]) == RReg)
		for (;; i--) {
			r = i->arg[0].val;
			assert(req(i->to, R) || i->to.type == RTmp);
			if (req(i->to, R)) {
				if (BGET(m->br, r)) {
					for (n=0; m->r[n] != r; n++)
						assert(n+1 < m->n);
					t = m->t[n];
					rfree(m, t);
					BSET(m->br, r);
					rt = ralloc(m, t);
					pmadd(rt, i->arg[0]);
				}
			} else if (BGET(m->bt, i->to.val)) {
				rt = TMP(rfree(m, i->to.val));
				pmadd(i->arg[0], rt);
			}
			BSET(m->br, r);
			if (i==b->ins
			|| (i-1)->op!=OCopy
			|| rtype((i-1)->arg[0]) != RReg)
				break;
		}
	else
		assert(!"no parallel move starting here");
	pmgen();
	nins = curi-insb;
	ib = alloc((b->nins + nins - (i1-i)) * sizeof(Ins));
	memcpy(ip = ib, b->ins, (i - b->ins) * sizeof(Ins));
	ip += i - b->ins;
	memcpy(ir = ip, insb, nins * sizeof(Ins));
	ip += nins;
	memcpy(ip, i1, (&b->ins[b->nins] - i1) * sizeof(Ins));
	b->nins += nins - (i1-i);
	free(b->ins);
	b->ins = ib;
	return ir;
}

/* register allocation
 * depends on rpo, cost, (and obviously spill)
 */
void
rega(Fn *fn)
{
	int n, t, r, x;
	Blk *b, *b1, *s, ***ps, *blist;
	Ins *i;
	RMap *end, *beg, cur;
	Phi *p;
	uint a;
	Ref src, dst;

	tmp = fn->tmp;
	end = alloc(fn->ntmp * sizeof end[0]);
	beg = alloc(fn->ntmp * sizeof beg[0]);

	/* 1. gross hinting setup */
	for (t=0; t<fn->ntmp; t++)
		tmp[t].hint = -1;
	for (b=fn->start; b; b=b->link)
		for (i=b->ins; i-b->ins < b->nins; i++)
			if (i->op == OCopy) {
				if (rtype(i->arg[0]) == RTmp
				&& rtype(i->to) == RReg)
					tmp[i->arg[0].val].hint = i->to.val;
				if (rtype(i->to) == RTmp
				&& rtype(i->arg[0]) == RReg)
					tmp[i->to.val].hint = i->arg[0].val;
			}

	/* 2. assign registers backwards */
	if (debug['R'])
		fprintf(stderr, "> Register mappings:\n");
	for (n=fn->nblk-1; n>=0; n--) {
		b = fn->rpo[n];
		cur.n = 0;
		cur.bt = (Bits){{0}};
		cur.br = (Bits){{0}};
		b1 = b->s1;
		if (b1 && b->s2 && b->s2->loop > b1->loop)
			b1 = b->s2;
		if (b1 && b->loop > b1->loop)
			b1 = 0;
		/* try to reuse the register
		 * assignment of the most frequent
		 * successor
		 */
		if (b1)
			for (t=0; t<fn->ntmp; t++)
				if (BGET(b->out, t))
				if ((r = rfind(&beg[b1->id], t)) != -1)
					radd(&cur, t, r);
		for (x=0; x<2; x++)
			for (t=0; t<fn->ntmp; t++)
				if (x==1 || tmp[t].hint!=-1)
				if (BGET(b->out, t))
				if (!BGET(cur.bt, t))
					ralloc(&cur, t);
		/* process instructions */
		end[n] = cur;
		assert(rtype(b->jmp.arg) != RReg);
		if (rtype(b->jmp.arg) == RTmp)
			b->jmp.arg = ralloc(&cur, b->jmp.arg.val);
		for (i=&b->ins[b->nins]; i!=b->ins;) {
			i--;
			if (i->op == OCopy /* par. moves are special */
			&& (rtype(i->arg[0]) == RReg || rtype(i->to) == RReg)) {
				i = dopm(b, i, &cur);
				continue;
			}
			switch (rtype(i->to)) {
			default:
				assert(!"unhandled destination");
			case RTmp:
				r = rfree(&cur, i->to.val);
				if (r == -1) {
					*i = (Ins){ONop, R, {R, R}};
					continue;
				}
				i->to = REG(r);
				break;
			case RReg:
				r = i->to.val;
				assert(BGET(cur.br, r));
				BCLR(cur.br, r);
				break;
			case -1:;
			}
			switch (rtype(i->arg[0])) {
			case RTmp:
				/* <arch>
				 *   on Intel, we attempt to
				 *   use the same register
				 *   for the return and the
				 *   first argument
				 */
				t = i->arg[0].val;
				if (tmp[t].hint == -1 && r)
					tmp[t].hint = r;
				i->arg[0] = ralloc(&cur, t);
				break;
			case RReg:
				BSET(cur.br, i->arg[0].val);
				break;
			}
			switch (rtype(i->arg[1])) {
			case RTmp:
				/* <arch>
				 *   on Intel, we have to
				 *   make sure we avoid the
				 *   situation
				 *   eax = sub ebx, eax
				 */
				if (opdesc[i->op].comm == F && r)
					BSET(cur.br, r);
				t = i->arg[1].val;
				i->arg[1] = ralloc(&cur, t);
				if (opdesc[i->op].comm == F && r)
					BCLR(cur.br, r);
				break;
			case RReg:
				BSET(cur.br, i->arg[1].val);
				break;
			}
		}
		b->in = cur.bt;
		for (p=b->phi; p; p=p->link)
			if (rtype(p->to) == RTmp)
				BCLR(b->in, p->to.val);
		beg[n] = cur;
		if (debug['R']) {
			fprintf(stderr, "\t%-10s beg", b->name);
			mdump(&beg[n]);
			fprintf(stderr, "\t           end");
			mdump(&end[n]);
		}
	}
	if (debug['R'])
		fprintf(stderr, "\n");

	/* 3. compose glue code */
	blist = 0;
	for (b=fn->start;; b=b->link) {
		ps = (Blk**[3]){&b->s1, &b->s2, (Blk*[1]){0}};
		for (; (s=**ps); ps++) {
			npm = 0;
			for (p=s->phi; p; p=p->link) {
				dst = p->to;
				assert(rtype(dst)==RSlot|| rtype(dst)==RTmp);
				if (rtype(dst) == RTmp) {
					r = rfind(&beg[s->id], dst.val);
					if (r == -1)
						continue;
					dst = REG(r);
				}
				for (a=0; p->blk[a]!=b; a++)
					assert(a+1 < p->narg);
				src = p->arg[a];
				if (rtype(src) == RTmp)
					src = rref(&end[b->id], src.val);
				pmadd(src, dst);
			}
			for (t=0; t<fn->ntmp; t++)
				if (BGET(s->in, t)) {
					src = rref(&end[b->id], t);
					dst = rref(&beg[s->id], t);
					pmadd(src, dst);
				}
			pmgen();
			/* todo, moving this out of
			 * here would make sense */
			n = curi-insb;
			if (!n)
				continue;
			b1 = blocka();
			b1->link = blist;
			blist = b1;
			fn->nblk++;
			sprintf(b1->name, "%s_%s", b->name, s->name);
			i = alloc(n * sizeof(Ins));
			memcpy(i, insb, n * sizeof(Ins));
			b1->ins = i;
			b1->nins = n;
			b1->jmp.type = JJmp;
			b1->s1 = s;
			**ps = b1;
		}
		if (!b->link) {
			b->link = blist;
			break;
		}
	}
	for (b=fn->start; b; b=b->link)
		while ((p=b->phi)) {
			b->phi = p->link;
			free(p);
		}

	free(end);
	free(beg);
}