From 7e4f2eaea8c3536247f70838d2c7a16b489e13cc Mon Sep 17 00:00:00 2001 From: lemon Date: Fri, 21 Nov 2025 10:39:38 +0100 Subject: ir/builder: peephole optimize branch with constant conditional --- ir/builder.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'ir') diff --git a/ir/builder.c b/ir/builder.c index 1d73314..02664c3 100644 --- a/ir/builder.c +++ b/ir/builder.c @@ -198,10 +198,20 @@ void putcondbranch(struct function *fn, union ref arg, struct block *t, struct block *f) { assert(fn->curblk && t && f); - adduse(fn->curblk, USERJUMP, arg); - addpred(t, fn->curblk); - addpred(f, fn->curblk); - putjump(fn, Jb, arg, NOREF, t, f); + if (iscon(arg)) { + bool truthy; + if (isintcon(arg)) truthy = intconval(arg) != 0; + else if (isfltcon(arg)) truthy = fltconval(arg) != 0.0; + else if (isaddrcon(arg,0)) truthy = 1; /* XXX ok to assume symbols have non null addresses? */ + else goto Cond; + putbranch(fn, truthy ? t : f); + } else { + Cond: + adduse(fn->curblk, USERJUMP, arg); + addpred(t, fn->curblk); + addpred(f, fn->curblk); + putjump(fn, Jb, arg, NOREF, t, f); + } } void -- cgit v1.2.3