Common subexpression elimination in Compiler Design - Time & Space Complexity
When a compiler removes repeated calculations, it saves time. We want to know how the time to optimize grows as the program size grows.
How does the work of finding repeated expressions change when the code gets bigger?
Analyze the time complexity of the following code snippet.
for each basic block in program:
for each expression in block:
if expression seen before in block:
replace with previous result
else:
record expression
This code finds repeated expressions inside each block and replaces duplicates with stored results.
Look for loops or repeated checks that take most time.
- Primary operation: Checking each expression against previously seen ones in the same block.
- How many times: For each expression in each block, this check happens once per previously seen expression.
As the number of expressions grows, the number of checks grows too.
| Input Size (n expressions) | Approx. Operations |
|---|---|
| 10 | About 55 checks |
| 100 | About 5,050 checks |
| 1000 | About 500,500 checks |
Pattern observation: The number of checks grows roughly like the square of the number of expressions.
Time Complexity: O(n²)
This means if you double the number of expressions, the work to find repeats roughly quadruples.
[X] Wrong: "Finding repeated expressions only takes time proportional to the number of expressions."
[OK] Correct: Because each new expression is compared to all previous ones, the total checks add up much faster than just counting expressions.
Understanding how optimization steps grow with program size helps you explain compiler efficiency clearly. It shows you can think about how tools work behind the scenes.
"What if we used a fast lookup structure like a hash table to track expressions? How would the time complexity change?"
Practice
Common Subexpression Elimination in compiler design?Solution
Step 1: Understand the concept of repeated calculations
Common subexpression elimination looks for expressions that appear multiple times in code.Step 2: Identify the optimization goal
It computes these expressions once and reuses the result to save time and work.Final Answer:
To find repeated calculations and compute them only once -> Option AQuick Check:
Repeated calculations = compute once [OK]
- Confusing with dead code elimination
- Thinking it changes instruction order
- Assuming it translates code
Solution
Step 1: Identify the optimization technique
Common subexpression elimination stores the result of repeated expressions in a temporary variable.Step 2: Understand reuse of stored value
Later uses of the expression reuse this stored value instead of recomputing.Final Answer:
Store the result of the expression in a temporary variable and reuse it -> Option CQuick Check:
Store once, reuse many [OK]
- Thinking expressions are removed
- Believing expressions are recomputed every time
- Replacing expressions with unrelated values
a = b + c
d = b + c + e
After applying common subexpression elimination, what is the optimized code?
Solution
Step 1: Identify common subexpression
The expressionb + cappears twice in the code.Step 2: Store and reuse the common expression
Computeb + conce, store intemp, then usetempfor bothaand part ofd.Final Answer:
temp = b + c a = temp d = temp + e -> Option AQuick Check:
Common part stored once =temp = b + c a = temp d = temp + e
[OK]
- Not storing the common part
- Storing the entire expression incorrectly
- Duplicating computations
temp = a * b
c = temp + d
temp = a * b + e
Solution
Step 1: Analyze temp variable usage
The variabletempis assigneda * bfirst, then reassigneda * b + e.Step 2: Understand impact on reuse
Reassigningtempoverwrites the original value, so reuse oftemplater may be incorrect.Final Answer:
Reassigning temp overwrites previous value causing incorrect reuse -> Option BQuick Check:
Overwriting temp breaks reuse [OK]
- Ignoring reassignment effects
- Assuming temp is constant automatically
- Thinking all expressions are common
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?
Solution
Step 1: Identify common subexpression
The expressiona + brepeats in all calculations.Step 2: Store common subexpression and reuse
Computetemp = a + bonce, then usetemp * candtemp * dforxandy. Finally,z = x + y.Final Answer:
temp = a + b x = temp * c y = temp * d z = x + y -> Option DQuick Check:
Extract common sum, reuse in products [OK]
- Not extracting (a + b) once
- Incorrectly combining terms
- Duplicating calculations
Ready to prove it?
Pass the test and this pattern moves unknown
