diff --git a/src/asahi/compiler/agx_optimizer.c b/src/asahi/compiler/agx_optimizer.c index bd74af7b2f2..88149ef9c4a 100644 --- a/src/asahi/compiler/agx_optimizer.c +++ b/src/asahi/compiler/agx_optimizer.c @@ -243,6 +243,38 @@ agx_optimizer_copyprop(agx_instr **defs, agx_instr *I) } } +/* + * Fuse conditions into if. Specifically, acts on if_icmp and fuses: + * + * if_icmp(cmp(x, y, *), 0, ne) -> if_cmp(x, y, *) + */ +static void +agx_optimizer_if_cmp(agx_instr **defs, agx_instr *I) +{ + /* Check for unfused if */ + if (!agx_is_equiv(I->src[1], agx_zero()) || I->icond != AGX_ICOND_UEQ || + !I->invert_cond || I->src[0].type != AGX_INDEX_NORMAL) + return; + + /* Check for condition */ + agx_instr *def = defs[I->src[0].value]; + if (def->op != AGX_OPCODE_ICMP && def->op != AGX_OPCODE_FCMP) + return; + + /* Fuse */ + I->src[0] = def->src[0]; + I->src[1] = def->src[1]; + I->invert_cond = def->invert_cond; + + if (def->op == AGX_OPCODE_ICMP) { + I->op = AGX_OPCODE_IF_ICMP; + I->icond = def->icond; + } else { + I->op = AGX_OPCODE_IF_FCMP; + I->fcond = def->fcond; + } +} + static void agx_optimizer_forward(agx_context *ctx) { @@ -269,6 +301,9 @@ agx_optimizer_forward(agx_context *ctx) I->op != AGX_OPCODE_UNIFORM_STORE && I->op != AGX_OPCODE_BLOCK_IMAGE_STORE) agx_optimizer_inline_imm(defs, I, info.nr_srcs, info.is_float); + + if (I->op == AGX_OPCODE_IF_ICMP) + agx_optimizer_if_cmp(defs, I); } free(defs);