Master the fundamental concepts of code generation through this focused micro-challenge.
Instruction selection maps IR trees to machine instruction trees. BURG and LLVM's SelectionDAG use dynamic programming over tree patterns. A simple matcher associates IR opcodes with assembly templates and picks the cheapest covering.
Break the IR expression tree into tiles each coverable by one or two machine instructions. a + b where both are in registers tiles to a single add. a + constant may tile to lea on x86.
cLoading…
[base + index*scale + offset] on x86Production 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 instruction selection using tree pattern matching. This exercise asks you to match IR operation trees to assembly instruction sequences, choosing tiles that minimize instruction count.
Implement instruction selection using tree pattern matching.
Requirements:
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