diff options
| author | 2025-12-16 07:17:09 +0100 | |
|---|---|---|
| committer | 2025-12-16 08:16:21 +0100 | |
| commit | a00214e6e569e87f82bb7bcce9df7b5365884ba1 (patch) | |
| tree | a3062ad65471215f2286c00a954342b56401a253 /ir/optmem.c | |
| parent | 0ab0918bc128b11a0e22cd0d69189280a9d103fd (diff) | |
mem2reg: fix obvious inefficiency
deltrivialphis() was iterating over every variably instead of just
looking at the variable being examined. And I'd been wondering why
mem2reg was such a bottleneck for a testcase like sqlite3 amalgamation..
it's easy to miss the forest for the trees.
Diffstat (limited to 'ir/optmem.c')
| -rw-r--r-- | ir/optmem.c | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/ir/optmem.c b/ir/optmem.c index 7bd0d1a..35544b2 100644 --- a/ir/optmem.c +++ b/ir/optmem.c @@ -33,16 +33,11 @@ struct ssabuilder { static union ref readvar(struct ssabuilder *, int var, enum irclass, struct block *); static union ref -deltrivialphis(struct ssabuilder *sb, struct block *blk, union ref phiref) +deltrivialphis(struct ssabuilder *sb, int var, struct block *blk, union ref phiref) { - union ref **pcurdefs; - int var; - struct use *use, *uend; + assert(instrtab[phiref.i].op == Ophi); union ref *args = phitab.p[instrtab[phiref.i].l.i]; union ref same = {0}; - - assert(instrtab[phiref.i].op == Ophi); - for (int i = 0; i < blk->npred; ++i) { if (args[i].bits == same.bits || args[i].bits == phiref.bits) { continue; /* unique value or self-reference */ @@ -57,12 +52,11 @@ deltrivialphis(struct ssabuilder *sb, struct block *blk, union ref phiref) /* replace uses */ replcuses(phiref, same); - imap_each(&sb->curdefs, var, pcurdefs) { - (void)var; - for (int i = blk->id; i < sb->nblk; ++i) { - if ((*pcurdefs)[i].bits == phiref.bits) - (*pcurdefs)[i] = same; - } + union ref **pcurdefs = imap_get(&sb->curdefs, var); + assert (pcurdefs); + for (int i = blk->id; i < sb->nblk; ++i) { + if ((*pcurdefs)[i].bits == phiref.bits) + (*pcurdefs)[i] = same; } /* stops infinite recursion and marks phi for removal */ @@ -70,10 +64,10 @@ deltrivialphis(struct ssabuilder *sb, struct block *blk, union ref phiref) /* recursively try to remove all phi users as they might have become trivial */ Redo: - for (use = instruse[phiref.i], uend = use + instrnuse[phiref.i]; use < uend; ++use) { + for (struct use *use = instruse[phiref.i], *uend = use + instrnuse[phiref.i]; use < uend; ++use) { if (use->u != USERJUMP && instrtab[use->u].op == Ophi && use->u != phiref.i) { union ref it = mkref(RTMP, use->u); - union ref vphi2 = deltrivialphis(sb, use->blk, it); + union ref vphi2 = deltrivialphis(sb, var, use->blk, it); if (vphi2.bits != it.bits) { same = vphi2; /* deletion happened so phiref use may have changed */ @@ -96,7 +90,7 @@ addphiargs(struct ssabuilder *sb, int var, enum irclass cls, struct block *blk, adduse(blk, phiref.i, args[i]); } instrtab[phiref.i].r.bits = 0; - return deltrivialphis(sb, blk, phiref); + return deltrivialphis(sb, var, blk, phiref); } static void |