Static school reading · Terminal implementation
Conditional constants and dataflow
Read here, then implement your own optimizer in the learner project. This page does not execute code, save answers or award results. Use the terminal tools to preserve first attempts, source versions, help and revisions.
The named U08/U09 reduction defect has passed an independent focused public-interface recheck. The owner package’s broader pending-review statements describe its original scope; the complete documented terminal route has since been exercised in a source-aware owner run. Browser and human learning are not claimed.
Get the learner project on GitHubSetup, nine lessons and contracts
Conditional constants and dataflow
A constant condition can make an edge unreachable, which can make a phi constant. Implement constants(program, contract) as a convergent sequence of justified value and control-flow simplifications.
Prerequisite: Project A. Your earlier scalar pass can be factored into a helper, but its edits must stay in one continuous evidence chain.
Values and control flow inform each other
Suppose entry branches on the known false value. Only its false successor executes. A downstream phi no longer needs the removed edge's value. If its remaining input is a constant, the phi can be replaced with that constant, exposing more scalar folds.
Do not confuse a currently unseen edge with an impossible edge. In a general constant-propagation analysis, value facts distinguish not-yet-known, one known constant, and varying/unknown values. Joining different possible constants loses the constant claim. Executable edges determine which incoming values contribute.
Our checked-edit core is deliberately conservative: first fold provable literal computations, simplify a branch whose condition is now a literal, repair the CFG, and fold phis whose remaining inputs agree. Repeat to a fixed point. This teaches the coupling without making an unverified sparse solver part of the trusted base.
Commit item 04a.
Make iteration explicit
Keep a worklist or repeat a full sweep. Either is acceptable at this scale. The loop must stop when there are no new justified edits. Recompute or invalidate dependent facts after changing the program; the old digest identifies the old graph.
A header phi with initial value zero and a backedge increment is not the constant zero merely because you visited entry first. Test at least two loop iterations. A single first-trip test cannot expose a frozen loop-carried fact.
The editor's branch operation removes newly unreachable blocks and obsolete phi inputs as one verified transaction. Do not manually omit that cleanup or claim stale analysis applies to the new program.
Build and transfer
node cli.mjs question 04b
node cli.mjs answer 04b work YOUR_LETTER --help-used none
node cli.mjs check 04 work --help-used none
node cli.mjs example branch branch.json
node cli.mjs optimize work branch.json constants --help-used noneKeep an unknown-input diamond unchanged unless your facts justify a transformation. Test a loop with values that change after the first backedge. Correct refusal on such a case matters as much as removing a constant branch.
This implementation path is a conservative conditional simplifier, not a claim to support every optimization available in LLVM SCCP.