Skip to content

wasm: add algebraic identity and strength-reduction peepholes to MIR and core optimizers - #142

Merged
cataggar merged 1 commit into
wazigfrom
wasm-peepholes-113
Nov 4, 2026
Merged

cataggar merged 1 commit into
wazigfrom
wasm-peepholes-113

Conversation

@cataggar

@cataggar cataggar commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Summary

Implements 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 across both Wasm optimizers:

  • MIR Intrafunction Optimizer (src/codegen/wasm/Optimize.zig) and Core Wasm Module Optimizer (src/link/Wasm/component/Optimizer.zig):
    • Additive, bitwise, and shift identities: x + 0, x - 0, x | 0, x ^ 0, x & -1, shifts/rotations by masked 0 ((c & 31) == 0 / (c & 63) == 0), and commutative left-identities (0 + x, 0 | x, 0 ^ x, -1 & x) on single 0-pop pure instructions.
    • Zero comparison & boolean/branch normalization:
      • x == 0 $\rightarrow$ i32.eqz / i64.eqz (saving 2 bytes per zero test).
      • <cmp>; i32.eqz $\rightarrow$ inverted comparison for all 10 i32 comparisons, all 10 i64 comparisons, and f32/f64 eq <-> ne.
      • Redundant boolean normalizations after boolean producers (<cmp>; != 0, <cmp>; == 1, <cmp>; & 1, <cmp>; ^ 1, <cmp>; != 1, <cmp>; i32.eqz; i32.eqz).
      • Condition normalizations before br_if, select, and if (<x>; i32.const 0; i32.ne and <x>; i32.eqz; i32.eqz stripped).
    • Multiplicative identities & power-of-two strength reductions:
      • x * 1, x /_s 1, x /_u 1 $\rightarrow$ x (plus commutative 1 * x).
      • x * 2^k ( \ge 1$) $\rightarrow$ x << k (plus commutative 2^k * x).
      • x /_u 2^k ( \ge 1$) $\rightarrow$ x >>_u k.
      • x %_u 2^k ( \ge 0$) $\rightarrow$ x & (2^k - 1).
      • Non-trapping two-constant div_s, div_u, rem_s, rem_u constant folding.
    • Constant reassociation:
      • (x +- c1) +- c2 $\rightarrow$ x + (d1 +% d2) (or x when 0), allowing mixed-sign constant chains like (x + 16) - 4 to fold directly into MemArg.offset in the same pass.
      • Associative bitwise constant reassociation for &, |, and ^.

Closes #113

@cataggar
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>
@cataggar
cataggar merged commit 5d00393 into wazig Nov 4, 2026
2 checks passed
@cataggar
cataggar deleted the wasm-peepholes-113 branch November 4, 2026 18:43
cataggar added a commit that referenced this pull request Nov 4, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasm: add algebraic identity and strength-reduction peepholes to MIR and core optimizers

1 participant