Master the fundamental concepts of intermediate representation through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksA control flow graph has basic blocks as nodes and jumps as edges. LLVM's BasicBlock, GCC's edge lists, and every dataflow analysis start here. Building the CFG from IR means partitioning instructions at leaders and wiring fall-through and branch targets.
Leaders: first instruction, targets of jumps, instructions after branches. Each leader starts a basic block. Successors: fall-through to next block, branch targets. Predecessors are the inverse edges.
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 build a control flow graph from your IR. This exercise requires identifying basic block boundaries, creating block nodes, and linking predecessors and successors for each branch and fall-through.
Partition three-address code into basic blocks and connect them into a control-flow graph: the structure every optimisation in the next subtracks works on.
TAC in the format of Design a Simple 3-Address Code IR (copies, binary operations, NAME: labels, goto L, if a goto L, ifFalse a goto L, print a), plus return a. Instructions are numbered from 0 in input order, labels included; blank lines are ignored.
Leaders: instruction 0; every label; every instruction immediately after a goto, if, ifFalse or return.
A basic block runs from a leader up to (not including) the next leader. Number blocks B0, B1, … in program order.
Successors of a block, from its last instruction:
goto L → the block starting with label Lif / ifFalse ... goto L → the block of L (taken) then the next block (fall-through)return → EXIT"The next block" after the last block is EXIT. If both successors are the same block, list it once.
Predecessors are the reverse edges (EXIT has none listed).
A block is unreachable if no path leads to it from B0.
For each block:
cLoading…
Then unreachable: BX BY (or unreachable: none) and edges=E, counting every successor entry including those to EXIT.
Input:
cLoading…
Output:
cLoading…
BasicBlock struct (first/last instruction, successor and predecessor lists) built from a leader bitmap.Hidden tests cover code after an unconditional goto (unreachable), a conditional jump to the very next block, several returns, and a loop whose exit falls off the end of the program.