commit 94a15adbf27f18e030686e70efaa88605dc9d9bd
parent 73e128a2a4859f2f82f39f3cf956ba61bb8a2917
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Sat, 13 Jun 2026 06:00:46 -0700
perf(aa64): fuse `cmp <reg>,#0; b.eq/b.ne` into cbz/cbnz at -O0
aa_cmp_branch now emits a single cbz/cbnz when the compare is an integer
CMP_EQ/CMP_NE against an immediate zero (the cg layer's null/zero-test
shape), dropping the separate `cmp <reg>,#0`. FP and non-zero compares
keep cmp+b.cond. The boolean-materialization path (aa_cmp) already used
cset, so no change there.
sqlite3.c -c -O0 (arm64-macOS): 539,549 -> 526,681 insns (-12,868, -2.4%).
b.eq 11,383->2,441, b.ne 5,969->2,043, cmp 21,052->8,184;
new cbz 8,942 + cbnz 3,926 = 12,868 fused branches.
Green: test-toy 1392/0, test-parse-ok 3920/0, test-parse-err 129/0,
test-smoke-x64 3/0, test-smoke-rv64 3/0; landmine prints 3; sqlite
end-to-end prints 84|2.
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/src/arch/aa64/native.c b/src/arch/aa64/native.c
@@ -1866,6 +1866,21 @@ static void aa_jump(NativeTarget* t, MCLabel label) {
static void aa_cmp_branch(NativeTarget* t, CmpOp op, NativeLoc lhs,
NativeLoc rhs, MCLabel label) {
+ /* Fuse `<reg> == 0` / `!= 0` tests into a single cbz/cbnz, dropping the
+ * separate `cmp <reg>,#0`. Only the GPR integer-equality case qualifies: the
+ * rhs must be an immediate zero (the cg layer passes IMM 0 for null/zero
+ * tests) and lhs a register (always true on the -O0 path). FP compares route
+ * through FCMP and keep cmp+b.cond. */
+ if ((op == CMP_EQ || op == CMP_NE) && !native_loc_is_fp(lhs) &&
+ lhs.kind == NATIVE_LOC_REG && rhs.kind == NATIVE_LOC_IMM &&
+ rhs.v.imm == 0) {
+ u32 sf = loc_is_64(t, lhs) ? 1u : 0u;
+ aa_emit32(t->mc,
+ op == CMP_EQ ? aa64_cbz(sf, loc_reg(lhs), 0)
+ : aa64_cbnz_imm(sf, loc_reg(lhs), 0));
+ mc_emit_label_ref(t->mc, label, R_AARCH64_CONDBR19, 4, 0);
+ return;
+ }
aa_emit_cmp_to_flags(t, lhs, rhs);
/* CMP_ONE_F / CMP_UEQ_F have no single FP condition: take the branch from a
* pair of conditional branches to the same label (no scratch register). */