Master the fundamental concepts of optimizations 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 worksFunction inlining replaces a call site with the callee's body, enabling further optimization across the old boundary. LLVM's inliner uses cost heuristics; C++ inline and LTO push this aggressively. Too much inlining blows code size.
Small callees, hot call sites, and functions marked always_inline are candidates. Copy parameters to locals, rename to avoid clashes, insert return handling at the call site.
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 function inline expansion on your IR. This exercise requires copying a callee's instructions to the call site, mapping arguments to parameters, and splicing the result back into the caller.
Implement inline expansion: replace a call with a renamed copy of the callee's body when the callee is small enough, and explain the decision for every call site.
cLoading…
Function bodies and main contain x = a, x = a OP b, print a, and calls x = call F(a1, a2, ...) (arguments are names or literals). Each function ends with exactly one return a. Indentation is ignored.
main, top to bottom)not inlined (unknown function)not inlined (recursive)return: is greater than B → not inlined (size S > budget B)inlined (size S)Only calls in main are candidates; calls inside an inlined body are copied as they are.
x = call F(args)F (parameters and locals) named v becomes v_k, where k counts inlined sites from 1.p_k = arg, in parameter order.x = r_k for return r (or x = N for return N).The rewritten main body, one instruction per line, then one line per call site:
cLoading…
and finally inlined=I kept=K.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover recursion, an unknown function, literal returns, a budget of 0, and callees that themselves call other functions.