Repository navigation
wasm: add algebraic identity and strength-reduction peepholes to MIR and core optimizers - #142
Merged
Merged
Conversation
cataggar
enabled auto-merge (squash)
November 4, 2026 18:37
…and core optimizers Implement single-constant algebraic identities, zero-comparison eqz lowering, comparison inversion, boolean/branch condition normalizations, multiplicative identities, power-of-two strength reductions, and additive/bitwise constant reassociation in both the MIR intrafunction optimizer (src/codegen/wasm/Optimize.zig) and the post-link core Wasm module optimizer (src/link/Wasm/component/Optimizer.zig): - Additive, bitwise, and shift identities (+0, -0, |0, ^0, &-1, shifts and rotations by 0, plus commutative left-identities on pure 0-pop operands). - Zero comparison lowering (x == 0 -> i32.eqz / i64.eqz) and comparison inversion (<cmp>; i32.eqz -> inverted comparison for i32, i64, and f32/f64 eq/ne). - Double-boolean and branch condition normalizations (<cmp>; != 0, <cmp>; == 1, <cmp>; & 1, <cmp>; ^ 1, <cmp>; != 1, <cmp>; eqz; eqz, and redundant != 0 / eqz; eqz before br_if, if, and select). - Multiplicative identities (* 1, /_s 1, /_u 1) and power-of-two strength reductions (* 2^k -> << k, /_u 2^k -> >>_u k, %_u 2^k -> & (2^k - 1)), plus non-trapping two-constant div/rem folding. - Additive ((x +- c1) +- c2 -> x + (d1 +% d2)) and bitwise (&, |, ^) constant reassociation, feeding folded positive offsets into MemArg.offset in the same pass. Closes #113 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Implements single-constant algebraic identities, zero-comparison
eqzlowering, comparison inversion, boolean/branch condition normalizations, multiplicative identities, power-of-two strength reductions, and additive/bitwise constant reassociation across both Wasm optimizers:src/codegen/wasm/Optimize.zig) and Core Wasm Module Optimizer (src/link/Wasm/component/Optimizer.zig):x + 0,x - 0,x | 0,x ^ 0,x & -1, shifts/rotations by masked0((c & 31) == 0/(c & 63) == 0), and commutative left-identities (0 + x,0 | x,0 ^ x,-1 & x) on single 0-pop pure instructions.x == 0i32.eqz/i64.eqz(saving 2 bytes per zero test).<cmp>; i32.eqzi32comparisons, all 10i64comparisons, andf32/f64eq <-> ne.<cmp>; != 0,<cmp>; == 1,<cmp>; & 1,<cmp>; ^ 1,<cmp>; != 1,<cmp>; i32.eqz; i32.eqz).br_if,select, andif(<x>; i32.const 0; i32.neand<x>; i32.eqz; i32.eqzstripped).x * 1,x /_s 1,x /_u 1x(plus commutative1 * x).x * 2^k( \ge 1$)x << k(plus commutative2^k * x).x /_u 2^k( \ge 1$)x >>_u k.x %_u 2^k( \ge 0$)x & (2^k - 1).div_s,div_u,rem_s,rem_uconstant folding.(x +- c1) +- c2x + (d1 +% d2)(orxwhen0), allowing mixed-sign constant chains like(x + 16) - 4to fold directly intoMemArg.offsetin the same pass.&,|, and^.Closes #113