Bird
Raised Fist0
Compiler Designknowledge~10 mins

Common subexpression elimination in Compiler Design - Interactive Code Practice

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

Complete the code to identify a common subexpression in the given expressions.

Compiler Design
if (a + b) == [1]:
Drag options to blanks, or click blank then click option'
Aa * b
Ba - b
Cb + c
Da + b
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a different expression that is not repeated.
Confusing addition with subtraction or multiplication.
2fill in blank
medium

Complete the code to replace a repeated expression with a temporary variable.

Compiler Design
temp = [1]
result = temp * 2
Drag options to blanks, or click blank then click option'
Ac * d
Ba - b
Ca + b
Db + c
Attempts:
3 left
💡 Hint
Common Mistakes
Using an expression that is not repeated.
Not assigning the expression to a temporary variable.
3fill in blank
hard

Fix the error in the code to correctly eliminate the common subexpression.

Compiler Design
result = (x + y) * 2
value = (x + [1]) - 3
Drag options to blanks, or click blank then click option'
Ay
Bz
Cx
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable that breaks the common subexpression.
Replacing with a constant instead of the variable.
4fill in blank
hard

Fill both blanks to create code that eliminates the common subexpression 'a * b'.

Compiler Design
[1] = a * b;
result1 = [2] + c;
result2 = [2] * d;
Drag options to blanks, or click blank then click option'
Atemp
Ba * b
Ca + b
Db * a
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the temporary variable.
Putting the expression in the reuse spots instead of the temp name.
5fill in blank
hard

Fill all three blanks to eliminate common subexpressions in a chained computation.

Compiler Design
[1] = x + y;
[2] = [1] * z;
final = [3] + w;
Drag options to blanks, or click blank then click option'
At1
Bt2
Dx + y
Attempts:
3 left
💡 Hint
Common Mistakes
Reusing the wrong temporary.
Not chaining the dependencies correctly.
Using the original expressions instead of temps.

Practice

(1/5)
1. What is the main purpose of Common Subexpression Elimination in compiler design?
easy
A. To find repeated calculations and compute them only once
B. To remove unused variables from the code
C. To reorder instructions for parallel execution
D. To translate code into machine language

Solution

  1. Step 1: Understand the concept of repeated calculations

    Common subexpression elimination looks for expressions that appear multiple times in code.
  2. Step 2: Identify the optimization goal

    It computes these expressions once and reuses the result to save time and work.
  3. Final Answer:

    To find repeated calculations and compute them only once -> Option A
  4. Quick Check:

    Repeated calculations = compute once [OK]
Hint: Look for repeated expressions to compute once [OK]
Common Mistakes:
  • Confusing with dead code elimination
  • Thinking it changes instruction order
  • Assuming it translates code
2. Which of the following is the correct way to represent a common subexpression elimination in code optimization?
easy
A. Recompute the same expression every time it appears
B. Remove the expression completely from the code
C. Store the result of the expression in a temporary variable and reuse it
D. Replace the expression with a random value

Solution

  1. Step 1: Identify the optimization technique

    Common subexpression elimination stores the result of repeated expressions in a temporary variable.
  2. Step 2: Understand reuse of stored value

    Later uses of the expression reuse this stored value instead of recomputing.
  3. Final Answer:

    Store the result of the expression in a temporary variable and reuse it -> Option C
  4. Quick Check:

    Store once, reuse many [OK]
Hint: Store repeated results once to reuse [OK]
Common Mistakes:
  • Thinking expressions are removed
  • Believing expressions are recomputed every time
  • Replacing expressions with unrelated values
3. Consider the code snippet:
a = b + c
d = b + c + e

After applying common subexpression elimination, what is the optimized code?
medium
A.
temp = b + c
a = temp
d = temp + e
B.
a = b + c
d = b + c + e
C.
a = b + c + e
d = b + c + e
D.
temp = b + c + e
a = temp
d = temp

Solution

  1. Step 1: Identify common subexpression

    The expression b + c appears twice in the code.
  2. Step 2: Store and reuse the common expression

    Compute b + c once, store in temp, then use temp for both a and part of d.
  3. Final Answer:

    temp = b + c a = temp d = temp + e -> Option A
  4. Quick Check:

    Common part stored once =
    temp = b + c
    a = temp
    d = temp + e
    [OK]
Hint: Extract repeated parts into temp variable [OK]
Common Mistakes:
  • Not storing the common part
  • Storing the entire expression incorrectly
  • Duplicating computations
4. Identify the error in the following code after applying common subexpression elimination:
temp = a * b
c = temp + d
temp = a * b + e
medium
A. No error, code is optimized correctly
B. Reassigning temp overwrites previous value causing incorrect reuse
C. The expression a * b is not a common subexpression
D. temp should be declared as a constant

Solution

  1. Step 1: Analyze temp variable usage

    The variable temp is assigned a * b first, then reassigned a * b + e.
  2. Step 2: Understand impact on reuse

    Reassigning temp overwrites the original value, so reuse of temp later may be incorrect.
  3. Final Answer:

    Reassigning temp overwrites previous value causing incorrect reuse -> Option B
  4. Quick Check:

    Overwriting temp breaks reuse [OK]
Hint: Avoid overwriting temp holding common subexpression [OK]
Common Mistakes:
  • Ignoring reassignment effects
  • Assuming temp is constant automatically
  • Thinking all expressions are common
5. Given the code:
x = (a + b) * c
y = (a + b) * d
z = (a + b) * c + (a + b) * d

How can common subexpression elimination be applied to optimize this code?
hard
A.
temp = (a + b) * (c + d)
x = temp
y = temp
z = temp + temp
B.
x = (a + b) * c
y = (a + b) * d
z = x + y
C.
temp1 = a + b
z = temp1 * (c + d)
x = z - temp1 * d
y = z - temp1 * c
D.
temp = a + b
x = temp * c
y = temp * d
z = x + y

Solution

  1. Step 1: Identify common subexpression

    The expression a + b repeats in all calculations.
  2. Step 2: Store common subexpression and reuse

    Compute temp = a + b once, then use temp * c and temp * d for x and y. Finally, z = x + y.
  3. Final Answer:

    temp = a + b x = temp * c y = temp * d z = x + y -> Option D
  4. Quick Check:

    Extract common sum, reuse in products [OK]
Hint: Extract repeated sums first, then multiply [OK]
Common Mistakes:
  • Not extracting (a + b) once
  • Incorrectly combining terms
  • Duplicating calculations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard