Skip to content

optimizer: add De Morgan's law transformations for bitwise and boolean ops#116

Merged
yevbar merged 1 commit intomasterfrom
happy/demorgan-bitwise-optimization
Mar 1, 2026
Merged

optimizer: add De Morgan's law transformations for bitwise and boolean ops#116
yevbar merged 1 commit intomasterfrom
happy/demorgan-bitwise-optimization

Conversation

@yevbar
Copy link
Copy Markdown
Contributor

@yevbar yevbar commented Mar 1, 2026

Summary

Add four new algebraic simplification rules based on De Morgan's laws to the optimizer.

Bitwise De Morgan's Laws

  • bnot(a) & bnot(b)bnot(a | b) — saves one i64.xor instruction
  • bnot(a) | bnot(b)bnot(a & b) — saves one i64.xor instruction

Boolean De Morgan's Laws

  • not(a) and not(b)not(a or b) — saves one i32.eqz instruction
  • not(a) or not(b)not(a and b) — saves one i32.eqz instruction

These transformations reduce two negation operations to one while preserving semantic equivalence, producing smaller and faster WASM output for bitwise-heavy code patterns.

Changes

  • lib/firebird/compiler/optimizer.ex — 4 new pattern-match clauses in algebraic_simplify/1, updated moduledoc
  • test/compiler/optimizer_algebraic_test.exs — 10 new tests (unit, complex sub-expressions, negative/no-op, and end-to-end pipeline)

Test Results

  • All 2471 compiler tests pass
  • All 9403 total tests pass (1 pre-existing xmerl-related failure unrelated to this change)

…n ops

Add four new algebraic simplification rules based on De Morgan's laws:

Bitwise:
- bnot(a) & bnot(b) → bnot(a | b)  (saves one i64.xor instruction)
- bnot(a) | bnot(b) → bnot(a & b)  (saves one i64.xor instruction)

Boolean:
- not(a) and not(b) → not(a or b)  (saves one i32.eqz instruction)
- not(a) or not(b) → not(a and b)  (saves one i32.eqz instruction)

These transformations reduce two negation operations to one while
preserving semantic equivalence, producing smaller and faster WASM
output for bitwise-heavy code patterns.

Includes 10 new tests covering unit, complex sub-expression,
negative (no-op), and end-to-end pipeline cases.
@yevbar yevbar merged commit 792baf3 into master Mar 1, 2026
4 checks passed
@yevbar yevbar deleted the happy/demorgan-bitwise-optimization branch March 1, 2026 08:12
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.

1 participant