From 42c7551e508e45cd4346e67898c374b910ee1407 Mon Sep 17 00:00:00 2001 From: lemon Date: Tue, 20 Jun 2023 19:52:35 +0200 Subject: improve codegen for div a little (bikeshedding) --- amd64/isel.c | 5 +++++ regalloc.c | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/amd64/isel.c b/amd64/isel.c index 2273c99..b0cacea 100644 --- a/amd64/isel.c +++ b/amd64/isel.c @@ -224,6 +224,7 @@ static void sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) { uint siz, alignlog2; + int t; struct instr temp = {0}; enum op op = ins->op; @@ -290,6 +291,10 @@ sel(struct function *fn, struct instr *ins, struct block *blk, int *curi) *ins = mkinstr(Ocopy, ins->cls, mkref(RREG, op < Orem ? RAX : RDX)); /* get output */ temp = mkinstr(Ocopy, ins->cls, mkref(RREG, op < Orem ? RDX : RAX)); /* clobber other reg*/ insertinstr(blk, ++(*curi), temp); + /* swap instrs so that clobber goes first */ + t = blk->ins.p[*curi - 1]; + blk->ins.p[*curi - 1] = blk->ins.p[*curi - 0]; + blk->ins.p[*curi - 0] = t; break; case Osub: if (ins->r.bits == mkref(RICON, 1).bits) { diff --git a/regalloc.c b/regalloc.c index 8f31b05..a30ebe5 100644 --- a/regalloc.c +++ b/regalloc.c @@ -238,7 +238,8 @@ use(struct rega *ra, struct block *blk, int curi, enum op op, int hint, union re assert(ins->op != Ocall); if (ins->r.t == RREG && ins->inplace) excl |= 1ull<r.i; - if (hint == -1 && ins->op == Ocopy && ins->l.t == RREG) /* for '%x = copy Rx', hint %x to use Rx */ + if ((hint == -1 || ra->regs[hint].t) && ins->op == Ocopy && ins->l.t == RREG) + /* for '%x = copy Rx', hint %x to use Rx */ hint = ins->l.i; if (hint != -1 && !(excl >> hint & 1) && !ra->regs[hint].t) { take(ra, hint, *ref); -- cgit v1.2.3