From b58e2e695b4567083b383779dfca5e153a31fa57 Mon Sep 17 00:00:00 2001 From: Quentin Carbonneaux Date: Tue, 21 Apr 2026 13:24:52 +0200 Subject: [PATCH] fix exponential complexity in usewidthle() --- copy.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/copy.c b/copy.c index 43b9490..e7d7e72 100644 --- a/copy.c +++ b/copy.c @@ -41,9 +41,8 @@ bitwidth(uint64_t v) return n+v; } -/* no more than w bits are used */ static int -usewidthle(Fn *fn, Ref r, int w) +uwl(Fn *fn, Ref r, int w) { Ext e; Tmp *t; @@ -52,7 +51,6 @@ usewidthle(Fn *fn, Ref r, int w) Ins *i; Ref rc; int64_t v; - int b; assert(rtype(r) == RTmp); t = &fn->tmp[r.val]; @@ -69,21 +67,19 @@ usewidthle(Fn *fn, Ref r, int w) if (p->visit || req(p->to, R)) continue; p->visit = 1; - b = usewidthle(fn, p->to, w); - p->visit = 0; - if (b) + if (uwl(fn, p->to, w)) continue; break; case UIns: i = u->u.ins; assert(i != 0); if (i->op == Ocopy) - if (usewidthle(fn, i->to, w)) + if (uwl(fn, i->to, w)) continue; if (ext(i, &e)) { if (e.usew <= w) continue; - if (usewidthle(fn, i->to, w)) + if (uwl(fn, i->to, w)) continue; } if (i->op == Oand) { @@ -107,6 +103,21 @@ usewidthle(Fn *fn, Ref r, int w) return 1; } +/* no more than w bits are used */ +static int +usewidthle(Fn *fn, Ref r, int w) +{ + Blk *b; + Phi *p; + int ret; + + ret = uwl(fn, r, w); + for (b=fn->start; b; b=b->link) + for (p=b->phi; p; p=p->link) + p->visit = 0; + return ret; +} + static int min(int v1, int v2) {