compiler: emit i64.eqz for zero-equality comparisons in WAT codegen#113
Merged
compiler: emit i64.eqz for zero-equality comparisons in WAT codegen#113
Conversation
…egen
Add a peephole optimization to the WAT code generator that emits the
i64.eqz instruction when comparing an i64 expression against literal 0
for equality. This saves one instruction (i64.const 0) in generated WASM.
The optimization applies in three code paths:
- General binop expressions: {:binop, :eq, expr, {:literal, 0}}
- Case clause pattern matching: {:literal_pat, 0}
- TCO case clause pattern matching: {:literal_pat, 0} in tail position
Before (3 stack operations):
local.get $x
i64.const 0
i64.eq
After (2 stack operations):
local.get $x
i64.eqz
This pattern is extremely common in:
- Boolean/zero checks: if x == 0
- Case dispatch with literal 0 patterns
- Guard conditions and parity tests (rem(n,2) == 0 after strength reduction)
Also extracts generate_binop_expr/7 helper for peephole fallback paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a peephole optimization to the WAT code generator that emits the
i64.eqzinstruction when comparing an i64 expression against literal 0 for equality. This saves one instruction (i64.const 0) per zero-comparison in the generated WASM binary.What changed
WAT code generator (
lib/firebird/compiler/wat_gen.ex):{:binop, :eq, expr, {:literal, 0}}and{:binop, :eq, {:literal, 0}, expr}that emiti64.eqzinstead ofi64.const 0; i64.eqgenerate_binop_expr/7helper for peephole fallbackBefore / After
Impact
This pattern appears in:
if x == 0case x do 0 -> ... endrem(n,2) == 0after strength reduction becomesband(n,1) == 0)Tests
test/compiler/eqz_peephole_test.exscovering WAT output verification, commutative patterns, case expressions, optimized mode, and WASM binary validitywat_gen_edge_cases_test.exsto expecti64.eqzfor pattern-0 cases