Module: optimize
Source:
optimize.rs
QFL bytecode optimizer — 11-pass pipeline over compiled QfrPrograms.
Pipeline (each pass feeds the next):
constant_fold— evaluate constant expressions within basic blockscfg_simplify— merge blocks, remove unreachable code, simplify jumpssccp— sparse conditional constant propagation (cross-block)cse— common subexpression elimination (per-block)local_shadowing— PersistGet/Set forwarding within blockslicm— loop-invariant code motionloop_unroll— unroll small constant-iteration loopsfused_lowering— peephole patterns (Mov chains, zero-based idioms)persist_coalesce— merge adjacent persist operationsdead_code_eliminate— remove unreachable or unused instructionsglobal_value_numbering— redundant computation elimination
Entry point: [optimize()].
Functions
pub fn optimize
#![allow(unused)]
fn main() {
pub fn optimize(...) { ... }
}
Run the full optimization pipeline on a compiled program. Pipeline order (each pass feeds the next):
- constant_fold — evaluate constant expressions within blocks
- cfg_simplify — merge blocks, remove unreachable code, simplify jumps
- sccp — sparse conditional constant propagation (cross-block)
- cse — common subexpression elimination (per-block)
- local_shadowing — PersistGet/Set forwarding within blocks
- licm — loop-invariant code motion
- loop_unroll — unroll small constant-iteration loops
- fused_lowering — peephole patterns (Mov chains, zero-based idioms)
- gvn — global value numbering (cross-block CSE via dominators)
- dce — dead code elimination (instruction-level reachability)
- persist_coalesce — redundant PersistGet/Set removal (slot-shadowing)
pub fn dead_code_eliminate
#![allow(unused)]
fn main() {
pub fn dead_code_eliminate(...) { ... }
}
Dead Code Elimination pass. Removes instructions unreachable from any entry point. Uses instruction-level reachability tracing (unlike CFG-based which traces blocks). Correctly adjusts jump offsets for remaining instructions.
pub fn common_subexpr_elim
#![allow(unused)]
fn main() {
pub fn common_subexpr_elim(...) { ... }
}
Common Subexpression Elimination pass. Within a basic block, replaces repeated identical computations with Mov from the first result register. Uses a hashmap keyed on (opcode, rs1, operand2) to detect duplicates within the block.
pub fn constant_fold
#![allow(unused)]
fn main() {
pub fn constant_fold(...) { ... }
}
Constant-folding pass. Folds arithmetic on known-constant registers within each basic block.
pub fn cfg_simplify
#![allow(unused)]
fn main() {
pub fn cfg_simplify(...) { ... }
}
CFG Simplification pass. Builds a control flow graph, merges consecutive basic blocks, removes unreachable blocks, and simplifies jump chains.
pub fn sccp
#![allow(unused)]
fn main() {
pub fn sccp(...) { ... }
}
Sparse Conditional Constant Propagation. Uses a lattice (Top в†’ Constant в†’ Bottom) per register, propagating across the CFG. Conditional branches with constant predicates are folded: the unreachable successor is marked non-executable. After convergence, known-constant expressions are replaced with Ldi/Ldi64/Ldc, and blocks gated by a folded branch are removed.
pub fn persist_coalesce
#![allow(unused)]
fn main() {
pub fn persist_coalesce(...) { ... }
}
PersistGet/Set coalescing optimization. Removes redundant PersistGet when the same slot is already cached in a register, and removes redundant PersistSet when the register value hasn’t changed since the last PersistGet of the same slot.