ggreif opened PR #13458 from ggreif:gabor/ireduce-ctz-clz-peephole to bytecodealliance:main:
Extends #13343's
simplify_skeletonfold so the existing
brif (ctz X)/brif (clz X)rules also fire when anireduce
(i.e.i32.wrap_i64at the wasm level) sits between the count and
the branch.Why this matters: non-Rust frontends targeting wasm-EOP often emit
i64.ctz; i32.wrap_i64; br_iffor boolean-context LSB tests. The
wrap is harmless (the count is in [0, 64]) but it currently blocks
the fold, so cranelift falls back to materializing the full count
before the branch. With this PR, the i64-wrap form lowers to the
sametestq \$1, %rdx; jeshape as the i32 case.Test coverage mirrors the existing patterns:
cranelift/filetests/.../brif-cnt-cond.clifgets two new
brif (ireduce (ctz/clz X))functions.
tests/disas/ctz-clz-bool-condition.watgets a new
\`\$if_clz_bare_i64\` and the existing \`\$if_ctz_bare_i64\`
is reblessed - both now lower to the optimal shape.Source pattern in the wild: motoko/moc's EOP peephole
(motoko PR #6117)
emits exactly \`i64.ctz; i32.wrap_i64; br_if\` for
\`(value & 1) == 0\`-as-branch-condition.
ggreif requested cfallin for a review on PR #13458.
ggreif requested wasmtime-compiler-reviewers for a review on PR #13458.
ggreif requested wasmtime-core-reviewers for a review on PR #13458.
Last updated: Jun 01 2026 at 09:49 UTC