Get rid of movins() infra.

This commit is contained in:
Roland Paterson-Jones 2024-11-20 16:05:51 +02:00 committed by Quentin Carbonneaux
parent c2ff93e75e
commit 1cb255cb04
6 changed files with 22 additions and 341 deletions

View File

@ -5,8 +5,7 @@ PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
COMMOBJ = main.o util.o parse.o abi.o cfg.o mem.o ssa.o alias.o load.o \
copy.o fold.o gvn.o gcm.o ins.o simpl.o live.o \
spill.o rega.o emit.o
copy.o fold.o gvn.o gcm.o simpl.o live.o spill.o rega.o emit.o
AMD64OBJ = amd64/targ.o amd64/sysv.o amd64/isel.o amd64/emit.o
ARM64OBJ = arm64/targ.o arm64/abi.o arm64/isel.o arm64/emit.o
RV64OBJ = rv64/targ.o rv64/abi.o rv64/isel.o rv64/emit.o

24
all.h
View File

@ -355,8 +355,6 @@ struct Tmp {
} width;
int visit;
uint gcmbid;
uint gcminsn; // TODO get rid
uint gcmdefinsn; // TODO get rid
};
struct Con {
@ -463,20 +461,6 @@ struct Dat {
char isstr;
};
typedef struct InsLoc InsLoc;
struct InsLoc {
uint bid;
uint insn;
};
typedef struct InsMov InsMov;
struct InsMov {
InsLoc from;
InsLoc to;
};
/* main.c */
extern Target T;
extern char debug['Z'+1];
@ -497,6 +481,8 @@ void freeall(void);
void *vnew(ulong, size_t, Pool);
void vfree(void *);
void vgrow(void *, ulong);
void addins(Ins **, uint *, Ins *);
void addbins(Blk *, Ins **, uint *);
void strf(char[NString], char *, ...);
uint32_t intern(char *);
char *str(uint32_t);
@ -616,12 +602,6 @@ void gvn(Fn *);
int isfixed(Fn *, Ins *);
void gcm(Fn *);
/* ins.c */
void addins(Ins **, uint *, Ins *);
void addbins(Blk *, Ins **, uint *);
void nopunused(Fn *);
void movins(Fn *, InsMov *, uint, int);
/* reassoc.c */
void reassoc(Fn *);

22
gcm.c
View File

@ -452,7 +452,6 @@ cleartmps(Fn *fn)
for (t=&fn->tmp[Tmp0]; t < &fn->tmp[fn->ntmp]; t++) {
t->visit = 0;
t->gcmbid = NOBID;
t->gcminsn = -1u; // TODO - get rid
}
}
@ -465,9 +464,6 @@ gcm(Fn *fn)
{
uint bid;
/* fprintf(stderr, "\n\nBefore gcm:\n\n"); */
/* printfn(fn, stderr); */
filldomdpth(fn);
fillloop(fn);
@ -477,29 +473,13 @@ gcm(Fn *fn)
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);
cleartmps(fn); /* filluse() uses visit */
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);

195
ins.c
View File

@ -1,195 +0,0 @@
#include "all.h"
void
addins(Ins **pvins, uint *pnins, Ins *i)
{
if (i->op == Onop)
return;
vgrow(pvins, ++(*pnins));
(*pvins)[(*pnins)-1] = *i;
}
void
addbins(Blk *b, Ins **pvins, uint *pnins)
{
Ins *i;
for (i = b->ins; i < &b->ins[b->nins]; i++)
addins(pvins, pnins, i);
}
static int
unusedins(Fn *fn, Ins *i);
static int
unusedtmp(Fn *fn, Tmp *t)
{
if (t->nuse == 0)
return 1;
if (t->nuse == 1)
if (t->use[0].type == UIns)
return unusedins(fn, t->use[0].u.ins);
return 0;
}
static int
unusedtmpref(Fn *fn, Ref r)
{
if (rtype(r) != RTmp)
return 0;
return unusedtmp(fn, &fn->tmp[r.val]);
}
static int
unusedins(Fn *fn, Ins *i)
{
if (!INRANGE(i->op, Opar, Ocall))
if (unusedtmpref(fn, i->to))
return 1;
return 0;
}
/* replace unused ins with nop */
/* needs use; breaks use */
void
nopunused(Fn *fn)
{
Blk *b;
Ins *i;
for (b = fn->start; b; b = b->link)
for (i = b->ins; i < &b->ins[b->nins]; i++)
if (unusedins(fn, i))
*i = (Ins){.op = Onop};
}
typedef struct FromLoc FromLoc;
struct FromLoc {
InsLoc from;
uint ton;
};
static int
loccmp(InsLoc *la, InsLoc *lb)
{
if (la->bid != lb->bid)
return (int)la->bid - (int)lb->bid;
return la->insn - lb->insn;
}
static int
tocmp(const void *a, const void *b)
{
InsMov *ma, *mb;
ma = (InsMov*)a;
mb = (InsMov*)b;
return loccmp(&ma->to, &mb->to);
}
static int
fromcmp(const void *a, const void *b)
{
FromLoc *fa, *fb;
fa = (FromLoc*)a;
fb = (FromLoc*)b;
return loccmp(&fa->from, &fb->from);
}
static int
loceq(InsLoc *a, InsLoc *b)
{
return a->bid == b->bid && a->insn == b->insn;
}
/* after, mov is sorted by to, and to.insn, from.insn are updated */
void
movins(Fn *fn, InsMov *mov, uint nmov, int del)
{
Blk *b, *b2;
uint bid, n, fromn, ton, ifromn, nins;
FromLoc *from;
uint *newbnins;
Ins **newbins, *vins;
qsort(mov, nmov, sizeof mov[0], tocmp);
from = emalloc(nmov * sizeof from[0]);
for (n = 0; n < nmov; n++) {
from[n].from = mov[n].from;
from[n].ton = n;
}
qsort(from, nmov, sizeof from[0], fromcmp);
nins = 0;
vins = vnew(nins, sizeof vins[0], PFn);
newbnins = emalloc(fn->nblk * sizeof newbnins[0]);
newbins = emalloc(fn->nblk * sizeof newbins[0]);
/* populate new ins buffers */
/* update mov to.insn, from insn */
fromn = ton = 0;
for (bid = 0; bid < fn->nblk; bid++) {
/* no moves to this block */
if (nmov <= ton || mov[ton].to.bid != bid) {
while (fromn < nmov && from[fromn].from.bid == bid)
fromn++;
continue;
}
b = fn->rpo[bid];
nins = 0;
for (ifromn = 0; ifromn < b->nins; ifromn++) {
/* insert new ins, update to */
while (ton < nmov && loceq(&mov[ton].to, &(InsLoc){.bid = bid, .insn = ifromn})) {
b2 = fn->rpo[mov[ton].from.bid];
assert(mov[ton].from.insn < b2->nins);
addins(&vins, &nins, &b2->ins[mov[ton].from.insn]);
mov[ton++].to.insn = nins-1;
}
/* update from */
while (fromn < nmov && loceq(&from[fromn].from, &(InsLoc){.bid = bid, .insn = ifromn}))
from[fromn++].from.insn = nins;
/* copy original ins */
addins(&vins, &nins, &b->ins[ifromn]);
}
/* append new ins, update to */
while (ton < nmov && mov[ton].to.bid == bid) {
assert(mov[ton].to.insn == b->nins);
b2 = fn->rpo[mov[ton].from.bid];
assert(mov[ton].from.insn < b2->nins);
addins(&vins, &nins, &b2->ins[mov[ton].from.insn]);
mov[ton++].to.insn = nins-1;
}
assert(ifromn == b->nins);
/* idup(&newbins[bid], vins, nins); */
newbins[bid] = vins;
vins = vnew(0, sizeof vins[0], PFn);
newbnins[bid] = nins;
}
assert(fromn == nmov);
assert(ton == nmov);
/* install new b->ins */
for (bid = 0; bid < fn->nblk; bid++) {
if (newbnins[bid] == 0)
continue;
b = fn->rpo[bid];
b->ins = newbins[bid];
b->nins = newbnins[bid];
}
/* remove from ins, update mov from insn */
for (fromn = 0; fromn < nmov; fromn++) {
b = fn->rpo[from[fromn].from.bid];
assert(from[fromn].from.insn < b->nins);
if (del)
b->ins[from[fromn].from.insn] = (Ins){.op = Onop};
mov[from[fromn].ton].from.insn = from[fromn].from.insn;
}
free(from);
free(newbins);
free(newbnins);
}

101
reassoc.c
View File

@ -1,101 +0,0 @@
#include "all.h"
static void
ireassoc(Fn *fn, Blk *b, Ins *i, uint *pnim, InsMov **pim)
{
Blk *b2;
Tmp *t;
Use *u;
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;
if (u->type == UIns)
if (INRANGE(u->u.ins->op, Oarg, Ocall))
continue;
b2 = fn->rpo[u->bid];
vgrow(pim, ++(*pnim));
(*pim)[(*pnim)-1] = (InsMov){
.from = {.bid = b->id, .insn = i-b->ins},
.to = {
.bid = u->bid,
.insn = u->type == UJmp ? b2->nins : u->u.ins-b2->ins
}};
}
}
static void
trealloc(Fn *fn, Blk *b, Ins *i)
{
Ins *i2;
Ref r;
assert(b->ins <= i && i < &b->ins[b->nins]);
r = newtmp("rea", i->cls, fn);
if (i < &b->ins[b->nins-1]) {
i2 = i+1;
/* special case of both args of target instruction reassociated */
if (!req(i->to, i2->arg[0]) && !req(i->to, i2->arg[1])) {
assert(i < &b->ins[b->nins-2]);
i2 = i+2;
}
assert(req(i->to, i2->arg[0]) || req(i->to, i2->arg[1]));
if (req(i->to, i2->arg[0]))
i2->arg[0] = r;
if (req(i->to, i2->arg[1]))
i2->arg[1] = r;
} else {
assert(req(i->to, b->jmp.arg));
b->jmp.arg = r;
}
i->to = r;
}
/* Redistribute trivial ops to point of use. */
/* Reduces register pressure. */
/* needs rpo, use; breaks use */
void
reassoc(Fn *fn)
{
Blk *b;
Ins *i;
InsMov *im;
uint n, nim;
nim = 0;
im = vnew(nim, sizeof im[0], PHeap);
/* 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, &nim, &im);
}
/* duplicate trivial ins */
movins(fn, im, nim, 0/*!del*/);
/* create new tmps for dups */
for (n = 0; n < nim; n++) {
b = fn->rpo[im[n].to.bid];
i = &b->ins[im[n].to.insn];
trealloc(fn, b, i);
}
/* delete (now) unused ins */
filluse(fn);
nopunused(fn);
if (debug['G']) {
fprintf(stderr, "\n> After Reassociation:\n");
printfn(fn, stderr);
}
}

18
util.c
View File

@ -154,6 +154,24 @@ vgrow(void *vp, ulong len)
*(Vec **)vp = v1;
}
void
addins(Ins **pvins, uint *pnins, Ins *i)
{
if (i->op == Onop)
return;
vgrow(pvins, ++(*pnins));
(*pvins)[(*pnins)-1] = *i;
}
void
addbins(Blk *b, Ins **pvins, uint *pnins)
{
Ins *i;
for (i = b->ins; i < &b->ins[b->nins]; i++)
addins(pvins, pnins, i);
}
void
strf(char str[NString], char *s, ...)
{