qbe/cfg.c
Roland Paterson-Jones 9e36cbe4d8 Combine fillrpo() and fillpreds() into fillcfg().
Remove edgedel() calls from fillrpo().

Call new prunephis() from fillpreds().
[Curiously this never seems to do anything even tho edgedel()
is no longer called from fillrpo()]

One remaining fillpreds() call in parse.c typecheck - seems
like it will still work the same.

defensive; fillcfg() combining fillrpo() and fillpreds() - problem after simpljmp() - think it is cos fillrpo() is still doing edgedel() which should now be covered by fillpreds()

comment out edgedel() in fillrpo() - fillcfg() no longer asserts after simpljmp() but seems like prunephis() never triggers???

static fillrpo(); remove edgedel() from fillrpo()

replace fillrpo() and/or fillpreds() with fillcfg()
2025-03-14 09:47:05 +01:00

361 lines
5.6 KiB
C

#include "all.h"
Blk *
newblk()
{
static Blk z;
Blk *b;
b = alloc(sizeof *b);
*b = z;
return b;
}
/* TODO - this never seems to do anything??? */
static void
prunephis(Fn *f)
{
Blk *b;
Phi *p, **pp;
uint na, na0;
for (b = f->start; b; b = b->link) {
assert(b->id < f->nblk);
for (pp = &b->phi; (*pp);) {
p = *pp;
for (na = na0 = 0; na < p->narg; na++)
if (p->blk[na]->id != -1u) {
p->blk[na0] = p->blk[na];
p->arg[na0] = p->arg[na];
na0++;
}
if (na0 == 0) {
*pp = p->link;
} else {
p->narg = na0;
pp = &p->link;
}
}
}
}
void
edgedel(Blk *bs, Blk **pbd)
{
Blk *bd;
Phi *p;
uint a;
int mult;
bd = *pbd;
mult = 1 + (bs->s1 == bs->s2);
*pbd = 0;
if (!bd || mult > 1)
return;
for (p=bd->phi; p; p=p->link) {
for (a=0; p->blk[a]!=bs; a++)
assert(a+1<p->narg);
p->narg--;
memmove(&p->blk[a], &p->blk[a+1],
sizeof p->blk[0] * (p->narg-a));
memmove(&p->arg[a], &p->arg[a+1],
sizeof p->arg[0] * (p->narg-a));
}
if (bd->npred != 0) {
for (a=0; bd->pred[a]!=bs; a++)
assert(a+1<bd->npred);
bd->npred--;
memmove(&bd->pred[a], &bd->pred[a+1],
sizeof bd->pred[0] * (bd->npred-a));
}
}
static void
addpred(Blk *bp, Blk *bc)
{
++bc->npred;
if (bc->pred)
vgrow(&bc->pred, bc->npred);
else
bc->pred = vnew(bc->npred, sizeof bc->pred[0], PFn);
bc->pred[bc->npred-1] = bp;
}
/* fill predecessors information in blocks; prune dead phi refs */
void
fillpreds(Fn *f)
{
Blk *b;
for (b=f->start; b; b=b->link)
b->npred = 0;
for (b=f->start; b; b=b->link) {
if (b->s1)
addpred(b, b->s1);
if (b->s2 && b->s2 != b->s1)
addpred(b, b->s2);
}
/* TODO - this never seems to do anything??? */
prunephis(f);
}
static void
porec(Blk *b, uint *npo)
{
Blk *s1, *s2;
if (!b || b->id != -1u)
return;
b->id = 0; /* marker */
s1 = b->s1;
s2 = b->s2;
if (s1 && s2 && s1->loop > s2->loop) {
s1 = b->s2;
s2 = b->s1;
}
porec(s1, npo);
porec(s2, npo);
b->id = (*npo)++;
}
/* fill the rpo information; prune dead blks */
static void
fillrpo(Fn *f)
{
Blk *b, **p;
for (b=f->start; b; b=b->link)
b->id = -1u;
f->nblk = 0;
porec(f->start, &f->nblk);
if (f->rpo)
vgrow(&f->rpo, f->nblk);
else
f->rpo = vnew(f->nblk, sizeof f->rpo[0], PFn);
for (p=&f->start; (b=*p);) {
if (b->id == -1u) {
*p = b->link;
} else {
b->id = f->nblk-b->id-1; /* po -> rpo */
f->rpo[b->id] = b;
p = &b->link;
}
}
}
/* fill rpo, preds; prune dead blks, prune dead blk refs from phis */
void
fillcfg(Fn *f)
{
fillrpo(f);
fillpreds(f);
}
/* for dominators computation, read
* "A Simple, Fast Dominance Algorithm"
* by K. Cooper, T. Harvey, and K. Kennedy.
*/
static Blk *
inter(Blk *b1, Blk *b2)
{
Blk *bt;
if (b1 == 0)
return b2;
while (b1 != b2) {
if (b1->id < b2->id) {
bt = b1;
b1 = b2;
b2 = bt;
}
while (b1->id > b2->id) {
b1 = b1->idom;
assert(b1);
}
}
return b1;
}
void
filldom(Fn *fn)
{
Blk *b, *d;
int ch;
uint n, p;
for (b=fn->start; b; b=b->link) {
b->idom = 0;
b->dom = 0;
b->dlink = 0;
}
do {
ch = 0;
for (n=1; n<fn->nblk; n++) {
b = fn->rpo[n];
d = 0;
for (p=0; p<b->npred; p++)
if (b->pred[p]->idom
|| b->pred[p] == fn->start)
d = inter(d, b->pred[p]);
if (d != b->idom) {
ch++;
b->idom = d;
}
}
} while (ch);
for (b=fn->start; b; b=b->link)
if ((d=b->idom)) {
assert(d != b);
b->dlink = d->dom;
d->dom = b;
}
}
int
sdom(Blk *b1, Blk *b2)
{
assert(b1 && b2);
if (b1 == b2)
return 0;
while (b2->id > b1->id)
b2 = b2->idom;
return b1 == b2;
}
int
dom(Blk *b1, Blk *b2)
{
return b1 == b2 || sdom(b1, b2);
}
static void
addfron(Blk *a, Blk *b)
{
uint n;
for (n=0; n<a->nfron; n++)
if (a->fron[n] == b)
return;
if (!a->nfron)
a->fron = vnew(++a->nfron, sizeof a->fron[0], PFn);
else
vgrow(&a->fron, ++a->nfron);
a->fron[a->nfron-1] = b;
}
/* fill the dominance frontier */
void
fillfron(Fn *fn)
{
Blk *a, *b;
for (b=fn->start; b; b=b->link)
b->nfron = 0;
for (b=fn->start; b; b=b->link) {
if (b->s1)
for (a=b; !sdom(a, b->s1); a=a->idom)
addfron(a, b->s1);
if (b->s2)
for (a=b; !sdom(a, b->s2); a=a->idom)
addfron(a, b->s2);
}
}
static void
loopmark(Blk *hd, Blk *b, void f(Blk *, Blk *))
{
uint p;
if (b->id < hd->id || b->visit == hd->id)
return;
b->visit = hd->id;
f(hd, b);
for (p=0; p<b->npred; ++p)
loopmark(hd, b->pred[p], f);
}
void
loopiter(Fn *fn, void f(Blk *, Blk *))
{
uint n, p;
Blk *b;
for (b=fn->start; b; b=b->link)
b->visit = -1u;
for (n=0; n<fn->nblk; ++n) {
b = fn->rpo[n];
for (p=0; p<b->npred; ++p)
if (b->pred[p]->id >= n)
loopmark(b, b->pred[p], f);
}
}
void
multloop(Blk *hd, Blk *b)
{
(void)hd;
b->loop *= 10;
}
void
fillloop(Fn *fn)
{
Blk *b;
for (b=fn->start; b; b=b->link)
b->loop = 1;
loopiter(fn, multloop);
}
static void
uffind(Blk **pb, Blk **uf)
{
Blk **pb1;
pb1 = &uf[(*pb)->id];
if (*pb1) {
uffind(pb1, uf);
*pb = *pb1;
}
}
/* requires rpo and no phis, breaks cfg */
void
simpljmp(Fn *fn)
{
Blk **uf; /* union-find */
Blk **p, *b, *ret;
ret = newblk();
ret->id = fn->nblk++;
ret->jmp.type = Jret0;
uf = emalloc(fn->nblk * sizeof uf[0]);
for (b=fn->start; b; b=b->link) {
assert(!b->phi);
if (b->jmp.type == Jret0) {
b->jmp.type = Jjmp;
b->s1 = ret;
}
if (b->nins == 0)
if (b->jmp.type == Jjmp) {
uffind(&b->s1, uf);
if (b->s1 != b)
uf[b->id] = b->s1;
}
}
for (p=&fn->start; (b=*p); p=&b->link) {
if (b->s1)
uffind(&b->s1, uf);
if (b->s2)
uffind(&b->s2, uf);
if (b->s1 && b->s1 == b->s2) {
b->jmp.type = Jjmp;
b->s2 = 0;
}
}
*p = ret;
free(uf);
}