Compilers
Which describes applying common subexpression elimination to the code above?
x = (a + b) * c;
y = (a + b) * d;
- ACompute a + b once into a temporary and reuse it in both expressions
- BSince x and y are similar, delete the computation of one of them
- CReplace the multiplications with additions to make the computation cheaper
Show answer and explanation
Answer: A. Compute a + b once into a temporary and reuse it in both expressions
Common subexpression elimination computes a repeatedly evaluated expression once and reuses the result. Here, computing t = a + b and then x = t * c and y = t * d saves one addition. It must be confirmed that a and b are not modified between the two expressions. x and y are different values, so neither can be deleted. Replacing multiplication with cheaper operations is a different optimization, strength reduction.
- A. Correct. The shared expression is computed once and reused.
- B. x and y have different values, so both are needed.
- C. Making expensive operations cheaper is strength reduction.