Choose the best description of what common subexpression elimination (CSE) aims to achieve in compiler optimization.
Think about how repeated calculations can be optimized.
CSE identifies expressions that are computed multiple times and replaces later occurrences with the stored result, reducing redundant work.
Given the code:a = b + c
d = b + c + e
Which expression is a common subexpression?
Look for repeated calculations with the same operands.
The expression b + c appears twice: once assigned to a and again as part of d.
Consider this code snippet:x = (a + b) * c
y = (a + b) * d
What will be the optimized code after applying common subexpression elimination?
Identify repeated expressions and store them once.
The expression a + b is computed twice. CSE stores it in temp and reuses it, reducing repeated addition.
Which reason explains why applying common subexpression elimination could sometimes degrade performance?
Think about the trade-offs of storing extra values.
Storing intermediate results requires extra memory and registers, which can cause cache misses or register spills, sometimes slowing down the program.
Which snippet benefits more from common subexpression elimination?
Snippet 1:for i in range(1000):
x = a + b
y = x * i
Snippet 2:for i in range(1000):
x = a + b * i
y = a + b * i + 1
Look for expressions that do not change inside the loop.
In Snippet 1, 'a + b' is loop-invariant and can be computed once outside the loop. In Snippet 2, expressions depend on 'i' and cannot be reused.