Complete the code to identify a simple peephole optimization that removes redundant load instructions.
if instruction == 'LOAD A' and next_instruction == '[1]': optimize_remove(next_instruction)
The peephole optimization removes a redundant 'LOAD A' instruction if it immediately follows another 'LOAD A'. This avoids loading the same value twice.
Complete the code to replace a multiplication by 2 with an addition, a common peephole optimization.
if instruction == 'MUL [1] 2': replace_with('ADD [1] [1]')
Multiplying a variable by 2 can be optimized by adding the variable to itself. Here, 'x' is used as the variable.
Fix the error in the peephole optimization that incorrectly replaces 'SUB x 0' with 'ADD x 0'.
if instruction == 'SUB [1] 0': replace_with('[2]')
Subtracting zero from a variable does nothing, so it can be replaced with a 'NOP' (no operation) instead of an addition.
Fill both blanks to optimize a sequence that loads a constant zero and then adds it to a variable.
if instructions == ['LOAD 0', 'ADD [1]']: replace_with('[2]')
Adding zero to a variable does nothing, so the entire sequence can be replaced with a no-operation (NOP).
Fill all three blanks to optimize a sequence that multiplies a variable by 1 and then stores it.
if instructions == ['MUL [1] 1', 'STORE [2]']: replace_with('[3] [2]')
Multiplying by 1 does not change the value, so the multiplication can be removed and replaced by a move (MOV) instruction before storing.