aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2026-04-11 22:49:19 +0200
committerlemon <lsof@mailbox.org>2026-04-11 22:49:19 +0200
commitd40371b615b560d8726fd4fdaf7d35abc959e0e9 (patch)
tree6e302f213c3e44fecccc032e79ea9f2d13c5c177
parent757b5735f44e9a9454f12612ec00c38d81f5f32c (diff)
backend: fix memory leak from not calling deluses() after replcuses in some places
-rw-r--r--src/ir.c11
-rw-r--r--src/ir_simpl.c4
-rw-r--r--src/ir_ssa.c7
-rw-r--r--src/ir_stack.c1
4 files changed, 11 insertions, 12 deletions
diff --git a/src/ir.c b/src/ir.c
index f34cfaf..c32ff4d 100644
--- a/src/ir.c
+++ b/src/ir.c
@@ -541,9 +541,9 @@ void
replcuses(Ref from, Ref to)
{
assert(from.t == RTMP);
- for (IRUse *use = instruse[from.i], *next; use; use = next) {
+ for (IRUse *use, *next = instruse[from.i]; (use = next);) {
Ref *u;
- int n, j;
+ int n;
next = use->next;
if (use->u == from.i) continue;
if (use->u == USERJUMP) {
@@ -556,15 +556,14 @@ replcuses(Ref from, Ref to)
n = use->blk->npred;
if (use->blk->phi.n == 0) continue; /* shouldn't happen */
} else {
- u = &instrtab[use->u].l;
- n = 2;
+ u = instrtab[use->u].oper;
+ n = opnoper[instrtab[use->u].op];
}
- for (j = 0; j < n; ++j) {
+ for (int j = 0; j < n; ++j) {
if (u[j].bits == from.bits) {
u[j].bits = to.bits;
adduse(use->blk, use->u, to);
- next = use;
break;
}
}
diff --git a/src/ir_simpl.c b/src/ir_simpl.c
index 1a3eb76..4395c7d 100644
--- a/src/ir_simpl.c
+++ b/src/ir_simpl.c
@@ -91,6 +91,7 @@ doins(Instr *ins, Block *blk, int *curi)
if (r.bits) {
*ins = mkinstr0(Onop,0);
replcuses(mkref(RTMP, ins - instrtab), r);
+ deluses(ins - instrtab);
return 1;
}
}
@@ -103,6 +104,7 @@ doins(Instr *ins, Block *blk, int *curi)
Ref it = ins->l;
*ins = mkinstr0(Onop,0);
replcuses(mkref(RTMP, ins - instrtab), it);
+ deluses(ins - instrtab);
return 1;
}
break;
@@ -243,7 +245,7 @@ simpl(Function *fn)
int phi = blk->phi.p[i];
/* delete trivial phis */
Ref *args = phitab.p[instrtab[phi].l.i],
- same = *args;
+ same = *args;
if (same.t == RTMP && instrtab[same.i].op == Ophi) goto Next;
if (blk->npred > 1) for (int j = 1; j < blk->npred; ++j) {
if (args[j].bits != same.bits) goto Next;
diff --git a/src/ir_ssa.c b/src/ir_ssa.c
index a1d960f..77474b5 100644
--- a/src/ir_ssa.c
+++ b/src/ir_ssa.c
@@ -14,11 +14,8 @@ copyopt(Function *fn)
for (int j = 1; j < blk->npred; ++j) {
if (arg[j].bits != arg->bits) goto Next;
}
- /* being conservative here because phis could have circular dependencies? */
- if (arg->t != RTMP || instrtab[arg->i].op != Ophi) {
- replcuses(mkref(RTMP, phi), *arg);
- delphi(blk, i--);
- }
+ replcuses(mkref(RTMP, phi), *arg);
+ delphi(blk, i--);
Next:;
}
for (int i = 0; i < blk->ins.n; ++i) {
diff --git a/src/ir_stack.c b/src/ir_stack.c
index a9acc61..e77e114 100644
--- a/src/ir_stack.c
+++ b/src/ir_stack.c
@@ -20,6 +20,7 @@ lowerstack(Function *fn)
if (fn->stksiz > (1<<20)-1) error(NULL, "'%s' stack frame too big", fn->name);
*ins = mkinstr0(Onop,0);
replcuses(mkref(RTMP, t), mkref(RSTACK, fn->stksiz-siz));
+ deluses(t);
}
}
} while ((blk = blk->lnext) != fn->entry);