Questions: Peephole Optimization

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A compiler's code generator produces the instruction sequence: STORE R1, [addr] followed immediately by LOAD R2, [addr]. What does a peephole optimizer do with this, and why?

AIt removes both instructions because the value in R1 must still be needed later and the load is forward-looking
BIt replaces the pair with MOV R2, R1, because the value just stored to [addr] is already in R1, eliminating the redundant memory access
CIt reorders the instructions so the LOAD precedes the STORE to improve cache performance
DIt does nothing because peephole optimization only handles jump instructions, not memory operations
Question 2 Multiple Choice

Why do compilers often run peephole optimization iteratively (multiple passes) rather than just once?

AEach pass reduces instruction count, which shrinks the code and requires re-running the pass to handle the now-smaller windows correctly
BApplying one peephole rule can bring two previously non-adjacent instructions into adjacency, exposing new pattern-match opportunities that the first pass could not have seen
CIterative passes compensate for missed patterns caused by the fixed window size being too small on the first pass
DPeephole optimization is non-deterministic; multiple passes increase the probability of finding the globally optimal sequence
Question 3 True / False

Peephole optimization is a purely local transformation: it can improve instruction sequences within a small window without needing to analyze the program's data flow, control flow, or overall structure.

TTrue
FFalse
Question 4 True / False

Peephole optimization is designed to run early in the compilation pipeline, before register allocation, because it needs to work on high-level intermediate representations.

TTrue
FFalse
Question 5 Short Answer

What properties of peephole optimization make it well-suited as a 'final polish' pass in a compiler, as opposed to an earlier, more global optimization phase?

Think about your answer, then reveal below.