0
0
Compiler Designknowledge~10 mins

Local optimization (peephole) in Compiler Design - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to identify a simple peephole optimization that removes redundant load instructions.

Compiler Design
if instruction == 'LOAD A' and next_instruction == '[1]':
    optimize_remove(next_instruction)
Drag options to blanks, or click blank then click option'
ALOAD A
BADD B
CSTORE A
DLOAD B
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a different instruction that does not match the first load.
Confusing STORE with LOAD instructions.
2fill in blank
medium

Complete the code to replace a multiplication by 2 with an addition, a common peephole optimization.

Compiler Design
if instruction == 'MUL [1] 2':
    replace_with('ADD [1] [1]')
Drag options to blanks, or click blank then click option'
Az
By
Cx
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables in the multiplication and addition.
Not replacing multiplication by 2 specifically.
3fill in blank
hard

Fix the error in the peephole optimization that incorrectly replaces 'SUB x 0' with 'ADD x 0'.

Compiler Design
if instruction == 'SUB [1] 0':
    replace_with('[2]')
Drag options to blanks, or click blank then click option'
ASUB
BNOP
CMOV
DADD
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing subtraction by zero with addition by zero, which is unnecessary.
Not using a no-operation instruction.
4fill in blank
hard

Fill both blanks to optimize a sequence that loads a constant zero and then adds it to a variable.

Compiler Design
if instructions == ['LOAD 0', 'ADD [1]']:
    replace_with('[2]')
Drag options to blanks, or click blank then click option'
Ax
BNOP
CMOV
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to move or load the variable unnecessarily.
Not recognizing that adding zero is redundant.
5fill in blank
hard

Fill all three blanks to optimize a sequence that multiplies a variable by 1 and then stores it.

Compiler Design
if instructions == ['MUL [1] 1', 'STORE [2]']:
    replace_with('[3] [2]')
Drag options to blanks, or click blank then click option'
Ax
By
CMOV
DNOP
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing multiplication by 1 with a no-operation, which skips necessary data movement.
Using the wrong variable names inconsistently.