Master the fundamental concepts of optimizations through this focused micro-challenge.
Dead code elimination (DCE) deletes instructions whose results are never used and branches that can never execute. LLVM's DCE and SCCP passes run constantly. Without DCE, folded if (0) blocks and temp chains clutter the output.
Mark instructions needed if they have side effects (store, call, return) or feed a needed instruction. Iterate backward: unmarked instructions are dead and deletable.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will implement dead code elimination on your IR. This exercise asks you to compute liveness and delete instructions and basic blocks that do not contribute to observable program behavior.
Implement dead code elimination on the AST.
Detect and remove unreachable code:
Detect and remove dead stores:
Remove empty statements:
{}Handle control flow:
if (false) A else B → Bif (true) A else B → Awhile (false) {...} → remove entirelyPreserve code with side effects:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github