0
0
Compiler Designknowledge~10 mins

Common subexpression elimination in Compiler Design - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common subexpression elimination
Start: Code with expressions
Identify repeated expressions
Check if operands unchanged
Yes No
Replace with temp
Use temp variable instead
Optimized code output
The process finds repeated expressions, checks if their inputs stay the same, then replaces duplicates with a temporary variable to avoid recomputing.
Execution Sample
Compiler Design
a = b + c
x = b + c
y = a * 2
z = b + c + y
This code has the expression 'b + c' repeated three times, which can be optimized by computing it once and reusing.
Analysis Table
StepActionExpression FoundOperands Unchanged?Replace with Temp?Resulting Code
1Scan first expressionb + cN/ANoa = b + c
2Scan second expressionb + cYesYesx = temp1
3Create temp1 = b + cb + cN/ANotemp1 = b + c
4Scan third expressiona * 2N/ANoy = a * 2
5Scan fourth expressionb + cYesYesz = temp1 + y
6Final optimized codeN/AN/AN/Atemp1 = b + c a = temp1 x = temp1 y = a * 2 z = temp1 + y
💡 All repeated expressions replaced with temp1 where operands unchanged, optimization complete.
State Tracker
VariableStartAfter Step 1After Step 3After Step 6
aundefinedb + cb + ctemp1
xundefinedundefinedundefinedtemp1
yundefinedundefinedundefineda * 2
zundefinedundefinedundefinedtemp1 + y
temp1undefinedundefinedb + cb + c
Key Insights - 2 Insights
Why do we need to check if operands are unchanged before replacing expressions?
Because if operands changed, the repeated expression would produce different results. The execution_table rows 2 and 5 show replacement only when operands are unchanged.
Why create a temporary variable for the common expression?
To store the computed value once and reuse it, avoiding repeated calculations. Step 3 in the execution_table shows creating temp1 for this purpose.
Visual Quiz - 3 Questions
Test your understanding
According to the execution_table, at which step is the temporary variable created?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'Resulting Code' columns in the execution_table for step 3.
In the variable_tracker, what is the value of 'x' after step 6?
Atemp1
Bundefined
Cb + c
Da * 2
💡 Hint
Check the 'After Step 6' column for variable 'x' in variable_tracker.
If operands changed between expressions, what would happen according to the concept_flow?
AReplace with temp variable anyway
BCreate multiple temp variables
CDo not replace repeated expression
DStop optimization
💡 Hint
Refer to the decision branch 'Operands Unchanged?' in concept_flow ASCII diagram.
Concept Snapshot
Common subexpression elimination:
- Find repeated expressions in code
- Check if operands are unchanged
- If yes, compute once and store in temp variable
- Replace repeated expressions with temp variable
- Saves computation time by avoiding repeats
Full Transcript
Common subexpression elimination is a compiler optimization that finds expressions repeated multiple times in code. It checks if the inputs to these expressions remain the same. If they do, the compiler computes the expression once, stores the result in a temporary variable, and replaces all repeated occurrences with this variable. This reduces redundant calculations and improves efficiency. The process involves scanning code, identifying repeated expressions, verifying operand stability, creating temporary variables, and rewriting code accordingly.