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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
#include "all.h"
#define NOBID (-1u)
/* ins can trap at runtime */
static int
istrapping(Fn *fn, Ins *i)
{
int64_t v;
if (KBASE(i->cls) == 0)
if (INRANGE(i->op, Odiv, Ourem))
if (!isconbits(fn, i->arg[1], &v) || v == 0)
return 1;
return 0;
}
/* fixed ins that can be eliminated if unused */
static int
canelim(Fn *fn, Ins *i)
{
return isload(i->op) || isalloc(i->op) || istrapping(fn, i);
}
/* ins must stay in def blk */
int
isfixed(Fn *fn, Ins *i)
{
return optab[i->op].ispinned || istrapping(fn, i);
}
static uint earlyins(Fn *, Blk *, Ins *);
/* return earlybid of ref def ins */
static uint
schedearly(Fn *fn, Ref r)
{
Tmp *t;
Blk *b;
if (rtype(r) != RTmp)
return 0 /* root/start */;
t = &fn->tmp[r.val];
if (t->gcmbid != NOBID)
return t->gcmbid; /* already visited/visiting */
b = fn->rpo[t->bid];
if (t->def) {
/* def is an ins */
assert(b->ins <= t->def && t->def < &b->ins[b->nins]);
t->gcmbid = 0; /* mark visiting root/start blk */
t->gcmbid = earlyins(fn, b, t->def); /* schedule ins input defs */
} else {
/* def is a phi - it stays in its def blk */
t->gcmbid = t->bid;
}
return t->gcmbid;
}
/* return deepest domdpth bid of arg defs */
static uint
earlyins(Fn *fn, Blk *b, Ins* i)
{
uint earlybid, arg1earlybid;
earlybid = schedearly(fn, i->arg[0]);
assert(earlybid != NOBID);
arg1earlybid = schedearly(fn, i->arg[1]);
assert(arg1earlybid != NOBID);
if (fn->rpo[earlybid]->domdpth < fn->rpo[arg1earlybid]->domdpth) {
assert(dom(fn->rpo[earlybid], fn->rpo[arg1earlybid]));
earlybid = arg1earlybid;
}
/* fixed ins remain in their defining blk */
return isfixed(fn, i) ? b->id : earlybid;
}
static void
earlyblk(Fn *fn, uint bid)
{
Blk *b;
Phi *p;
Ins *i;
uint n;
b = fn->rpo[bid];
for (p = b->phi; p; p = p->link)
for (n = 0; n < p->narg; n++)
schedearly(fn, p->arg[n]);
for (i = b->ins; i < &b->ins[b->nins]; i++)
if (isfixed(fn, i))
earlyins(fn, b, i);
schedearly(fn, b->jmp.arg);
}
/* least common ancestor in dom tree */
static uint
lcabid(Fn *fn, uint bid1, uint bid2)
{
Blk *b;
if (bid1 == NOBID)
return bid2;
if (bid2 == NOBID)
return bid1;
b = lca(fn->rpo[bid1], fn->rpo[bid2]);
assert(b);
return b->id;
}
static uint
bestbid(Fn *fn, uint earlybid, uint latebid)
{
Blk *curb, *earlyb, *bestb;
if (latebid == NOBID)
return NOBID; /* unused */
assert(earlybid != NOBID);
earlyb = fn->rpo[earlybid];
bestb = curb = fn->rpo[latebid];
assert(dom(earlyb, curb));
while (curb != earlyb) {
curb = curb->idom;
if (curb->loop < bestb->loop)
bestb = curb;
}
return bestb->id;
}
static uint lateins(Fn *, Blk *, Ins *, Ref r);
static uint latephi(Fn *, Phi *, Ref r);
static uint latejmp(Blk *, Ref r);
/* return lca bid of ref uses */
static uint
schedlate(Fn *fn, Ref r)
{
Tmp *t;
Blk *b;
Use *u;
uint earlybid;
uint latebid;
uint uselatebid;
if (rtype(r) != RTmp)
return NOBID;
t = &fn->tmp[r.val];
if (t->visit)
return t->gcmbid; /* already visited/visiting */
t->visit = 1; /* mark visiting/visited */
earlybid = t->gcmbid;
if (earlybid == NOBID)
return NOBID; /* not used */
t->gcmbid = t->bid; /* t->gcmbid is now latebid */
latebid = NOBID;
for (u = t->use; u < &t->use[t->nuse]; u++) {
assert(u->bid < fn->nblk);
b = fn->rpo[u->bid];
switch (u->type) {
case UXXX:
die("unreachable");
break;
case UPhi:
uselatebid = latephi(fn, u->u.phi, r);
break;
case UIns:
uselatebid = lateins(fn, b, u->u.ins, r);
break;
case UJmp:
uselatebid = latejmp(b, r);
break;
}
latebid = lcabid(fn, latebid, uselatebid);
}
/* phis stay in their def blk */
if (t->def) {
/* allow elimination of unused load/alloc/trapping ins */
if (latebid == NOBID && canelim(fn, t->def))
t->gcmbid = NOBID;
/* ... otherwise fixed ins stay in defining blk */
else if(!isfixed(fn, t->def))
t->gcmbid = bestbid(fn, earlybid, latebid);
}
return t->gcmbid;
}
/* return lca bid of uses, or NOBID if no active uses */
static uint
lateins(Fn *fn, Blk *b, Ins *i, Ref r)
{
uint latebid;
assert(i->op == Onop || req(i->arg[0], r) || req(i->arg[1], r));
if (i->op == Onop)
return NOBID; /* already eliminated */
assert(b->ins <= i && i < &b->ins[b->nins]);
latebid = schedlate(fn, i->to);
/* allow elimination of unused load/alloc/trapping ins */
if (latebid == NOBID)
if (canelim(fn, i))
return NOBID;
/* ... otherwise fixed ins stay in defining blk */
return isfixed(fn, i) ? b->id : latebid;
}
static uint
latephi(Fn *fn, Phi *p, Ref r)
{
uint n;
uint latebid;
if (!p->narg)
return NOBID; /* marked as unused */
latebid = NOBID;
for (n = 0; n < p->narg; n++)
if (req(p->arg[n], r))
latebid = lcabid(fn, latebid, p->blk[n]->id);
assert(latebid != NOBID);
return latebid;
}
static uint
latejmp(Blk *b, Ref r)
{
if (req(b->jmp.arg, R))
return NOBID;
else {
assert(req(b->jmp.arg, r));
return b->id;
}
}
static void
lateblk(Fn *fn, uint bid)
{
Blk *b;
Phi **pp;
Ins *i;
b = fn->rpo[bid];
for (pp = &b->phi; *(pp);)
if (schedlate(fn, (*pp)->to) == NOBID) {
/* unused */
(*pp)->narg = 0; /* mark unused */
*pp = (*pp)->link; /* remove phi */
} else
pp = &(*pp)->link;
for (i = b->ins; i < &b->ins[b->nins]; i++)
if (isfixed(fn, i))
lateins(fn, b, i, i->arg[0]);
}
static void
addgcmins(Fn *fn, Ins *vins, uint nins)
{
Ins *i;
Tmp *t;
Blk *b;
for (i = vins; i < &vins[nins]; i++) {
assert(rtype(i->to) == RTmp);
t = &fn->tmp[i->to.val];
b = fn->rpo[t->gcmbid];
addins(&b->ins, &b->nins, i);
}
}
/* remove unused instructions */
/* move instructions to (end of) target block */
/* use-before-def is fixed up afterwards */
static void
gcmmove(Fn *fn)
{
Tmp *t;
Ins *vins, *i;
uint nins;
nins = 0;
vins = vnew(nins, sizeof vins[0], PFn);
for (t=&fn->tmp[Tmp0]; t < &fn->tmp[fn->ntmp]; t++) {
if (t->def == 0)
continue;
if (t->bid == t->gcmbid)
continue;
i = t->def;
if (isfixed(fn, i) && !canelim(fn, i))
continue;
assert(rtype(i->to) == RTmp);
assert(t == &fn->tmp[i->to.val]);
if (t->gcmbid != NOBID)
addins(&vins, &nins, i);
*i = (Ins){.op = Onop};
}
addgcmins(fn, vins, nins);
}
static void
schedins(Fn *fn, Blk *b, Ins *i0, Ins **pvins, uint *pnins)
{
Ins *i, *i1;
Tmp *t;
uint na;
if (i0->op == Onop)
return;
/* arg...call have to stay together */
/* TODO - sel0...sel1 too */
for (i1 = i0; isarg(i1->op); i1++) {}
for (i = i0; i <= i1; i++)
for (na = 0; na < 2; na++) {
if (rtype(i->arg[na]) != RTmp)
continue;
t = &fn->tmp[i->arg[na].val];
/* def in different blk, or phi in this blk */
if (t->bid != b->id || t->def == 0)
continue;
/* already scheduled */
if (t->def->op == Onop) {
continue;
}
schedins(fn, b, t->def, pvins, pnins);
}
for (i = i0; i <= i1; i++) {
addins(pvins, pnins, i);
*i = (Ins){.op = Onop};
}
}
static void
fixbub4d(Fn *fn, Blk *b, Ins **pvins)
{
Ins *i;
uint nins;
nins = 0;
for (i = b->ins; i < &b->ins[b->nins]; i++)
schedins(fn, b, i, pvins, &nins);
idup(b, *pvins, nins);
}
static void
fixub4d(Fn *fn)
{
Blk *b;
Ins *vins; // TODO insb
vins = vnew(0, sizeof vins[0], PFn);
for (b = fn->start; b; b = b->link)
fixbub4d(fn, b, &vins);
}
static int
iskladdcon(Fn *fn, Ins *i, int64_t *v)
{
if (i->op == Oadd)
if (i->cls == Kl)
if (rtype(i->arg[0]) == RTmp)
if (isconbits(fn, i->arg[1], v))
return 1;
return 0;
}
static void
ireassoc(Fn *fn, Blk *b, Ins *i, Ins **pvins, uint *pnins)
{
Blk *b2;
Tmp *t, *t2;
Use *u;
Ref r2;
int64_t v;
int x;
assert(b->ins <= i && i < &b->ins[b->nins]);
if (!iscmp(i->op, &x, &x))
if (!iskladdcon(fn, i, &v))
return;
assert(rtype(i->to) == RTmp);
t = &fn->tmp[i->to.val];
for (u = t->use; u < &t->use[t->nuse]; u++) {
if (u->type == UPhi)
continue;
b2 = fn->rpo[u->bid];
addins(pvins, pnins, i);
/* careful, can move fn->tmp */
r2 = newtmp("rea", t->cls, fn);
t = &fn->tmp[i->to.val];
t2 = &fn->tmp[r2.val];
t2->gcmbid = u->bid;
(*pvins)[(*pnins)-1].to = r2;
if (u->type == UIns) {
assert(req(u->u.ins->arg[0], i->to)
|| req(u->u.ins->arg[1], i->to));
if (req(u->u.ins->arg[0], i->to))
u->u.ins->arg[0] = r2;
if (req(u->u.ins->arg[1], i->to))
u->u.ins->arg[1] = r2;
} else {
assert(u->type == UJmp);
assert(req(b2->jmp.arg, i->to));
b2->jmp.arg = r2;
}
}
}
/* Redistribute trivial ops to point of use. */
/* Reduces register pressure. */
/* needs rpo, use; breaks use */
void
reassoc(Fn *fn)
{
Blk *b;
Ins *vins, *i;
uint nins;
nins = 0;
vins = vnew(nins, sizeof vins[0], PFn);
/* identify trivial ins */
for (b = fn->start; b; b = b->link) {
for (i = b->ins; i < &b->ins[b->nins]; i++)
ireassoc(fn, b, i, &vins, &nins);
}
addgcmins(fn, vins, nins);
}
static void
cleartmps(Fn *fn)
{
Tmp *t;
for (t=&fn->tmp[Tmp0]; t < &fn->tmp[fn->ntmp]; t++) {
t->visit = 0;
t->gcmbid = NOBID;
t->gcminsn = -1u; // TODO - get rid
}
}
/* https://courses.cs.washington.edu/courses/cse501/06wi/reading/click-pldi95.pdf */
/* require use dom */
/* maintains rpo pred dom */
/* breaks use */
void
gcm(Fn *fn)
{
uint bid;
/* fprintf(stderr, "\n\nBefore gcm:\n\n"); */
/* printfn(fn, stderr); */
filldomdpth(fn);
fillloop(fn);
cleartmps(fn);
for (bid=0; bid<fn->nblk; bid++)
earlyblk(fn, bid);
for (bid=0; bid<fn->nblk; bid++)
lateblk(fn, bid);
/* fprintf(stderr, "\n\nBefore gcmmove/fixub4d:\n\n"); */
/* printfn(fn, stderr); */
gcmmove(fn);
cleartmps(fn);
/* filluse(fn); */
/* fixub4d(fn); */
/* fprintf(stderr, "\n\nAfter gcmmove/fixub4d:\n\n"); */
/* printfn(fn, stderr); */
//fillcfg(fn);
filluse(fn);
reassoc(fn);
/* cleartmps(fn); */
filluse(fn);
fixub4d(fn);
/* delete (now) unused ins - already done later??? */
filluse(fn);
nopunused(fn);
if (debug['G']) {
fprintf(stderr, "\n> After GCM:\n");
printfn(fn, stderr);
}
}
|