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 worksStatic Single Assignment form gives each variable exactly one definition site, simplifying dataflow analysis. LLVM IR, GCC's SSA GIMPLE, and HotSpot's C1 IR all use SSA. The Cytron algorithm inserts phi-nodes at control-flow joins to merge values.
Rename each assignment to a fresh version: x becomes x1, x2. At join points after if/else, insert x3 = phi(x1, x2). Optimizations like constant propagation and dead code elimination become trivial on SSA.
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 convert your IR to static single assignment form. This exercise asks you to rename variables and insert phi-nodes at merge points so each SSA name is assigned exactly once.
Convert straight-line three-address code into Static Single Assignment form: every variable is assigned exactly once, so each definition gets a fresh versioned name and every use refers to the version that reaches it. (Merging values from different control-flow paths with φ-nodes comes later, in Add Phi-Nodes to Your SSA IR; here there are no jumps.)
Straight-line TAC, one instruction per line (blank lines ignored):
cLoading…
Operands are integer literals or variable names.
x increments it and writes x_N.x becomes x_N for the current N. A variable used before any definition is an incoming value, x_0.x = x + 1 the use is renamed before the definition: x_1 = x_0 + 1.The renamed code, one instruction per line, then:
cLoading…
listing every variable that was defined, in order of its first definition, with its final version.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover long chains of redefinitions, incoming values used several times, return, and variables whose names end in digits (t1 becomes t1_1).