Simplify fillrpo()

Essentially use post-order as id, then reverse to rpo.
Avoids needing f->nblk initially; slightly simpler logic.
This commit is contained in:
Roland Paterson-Jones 2024-11-16 15:09:19 +02:00 committed by Quentin Carbonneaux
parent 0ce9966c23
commit 1c769584ac

25
cfg.c
View File

@ -77,38 +77,35 @@ fillpreds(Fn *f)
} }
} }
static int static void
rporec(Blk *b, uint x) porec(Blk *b, uint *npo)
{ {
Blk *s1, *s2; Blk *s1, *s2;
if (!b || b->id != -1u) if (!b || b->id != -1u)
return x; return;
b->id = 1; b->id = 0; /* marker */
s1 = b->s1; s1 = b->s1;
s2 = b->s2; s2 = b->s2;
if (s1 && s2 && s1->loop > s2->loop) { if (s1 && s2 && s1->loop > s2->loop) {
s1 = b->s2; s1 = b->s2;
s2 = b->s1; s2 = b->s1;
} }
x = rporec(s1, x); porec(s1, npo);
x = rporec(s2, x); porec(s2, npo);
b->id = x; b->id = (*npo)++;
assert(x != -1u);
return x - 1;
} }
/* fill the rpo information */ /* fill the rpo information; prune dead blks */
void void
fillrpo(Fn *f) fillrpo(Fn *f)
{ {
uint n;
Blk *b, **p; Blk *b, **p;
for (b=f->start; b; b=b->link) for (b=f->start; b; b=b->link)
b->id = -1u; b->id = -1u;
n = 1 + rporec(f->start, f->nblk-1); f->nblk = 0;
f->nblk -= n; porec(f->start, &f->nblk);
if (f->rpo) if (f->rpo)
vgrow(&f->rpo, f->nblk); vgrow(&f->rpo, f->nblk);
else else
@ -119,7 +116,7 @@ fillrpo(Fn *f)
edgedel(b, &b->s2); edgedel(b, &b->s2);
*p = b->link; *p = b->link;
} else { } else {
b->id -= n; b->id = f->nblk-b->id-1; /* po -> rpo */
f->rpo[b->id] = b; f->rpo[b->id] = b;
p = &b->link; p = &b->link;
} }