Skip to content

feat: add branch condition folding pass#35

Merged
arnoox merged 2 commits intomainfrom
pr-f1/branch-fold
Mar 27, 2026
Merged

feat: add branch condition folding pass#35
arnoox merged 2 commits intomainfrom
pr-f1/branch-fold

Conversation

@arnoox
Copy link
Copy Markdown
Owner

@arnoox arnoox commented Mar 27, 2026

Summary

  • Adds branch_fold as a new post-lowering optimizer pass
  • Simplifies BranchIf terminators when the condition is defined by Eqz, Ne(x,0), or Eq(x,0) — replaces the condition variable with its operand and swaps targets where needed
  • Dead defining instructions are cleaned up by the subsequent dead_instrs pass

Pipeline position

... → copy_prop → dead_instrs → branch_fold → dead_instrs → ...

Test plan

  • cargo test -p herkos-core --lib — 197 tests pass (5 new branch_fold tests)
  • cargo clippy — clean
  • cargo fmt --check

Stack

This is PR F1 of 4 in the optimizer split. Stacked on main.

PR Pass Status
F1 (this) branch_fold
F2 local_cse pending
F3 gvn pending
F4 licm pending

🤖 Generated with Claude Code

Adds `branch_fold` as a new post-lowering optimizer pass. Simplifies
`BranchIf` terminators when the condition variable is defined by a known
comparison (Eqz, Ne(x,0), Eq(x,0)), replacing the condition with its
operand directly and swapping targets where needed. Dead defining
instructions are cleaned up by the subsequent `dead_instrs` pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Remove local `build_global_const_map` from branch_fold — utils already
  provides an identical implementation; import it instead
- Move `is_zero` helper to utils so it can be shared by other passes
- Compute both `global_uses` and `global_consts` in `eliminate` and pass
  both into `fold_one`, making the two maps symmetric at the call site
- Drop the per-block `local_consts` clone — `build_global_const_map`
  already captures all Const-defined variables across the function in SSA
  form, so the block-level augmentation was redundant

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@arnoox arnoox merged commit 4427973 into main Mar 27, 2026
1 check passed
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