Question:

Consider the code:
$X = a * 2$
$Y = a * 2$
After optimization, what is the most efficient transformation?

Show Hint

Replacing multiplication with addition ($a + a$) is called "Strength Reduction." While also an optimization, it doesn't solve the redundancy problem as effectively as CSE in this specific code snippet.
Updated On: Jul 4, 2026
  • Keep both statements unchanged
  • Replace multiplication with addition
  • Compute once and reuse result
  • Remove both statements
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Concept: Compilers perform "Code Optimization" to improve performance without changing the program's output.
Common Subexpression Elimination (CSE): Identifying identical expressions that compute the same value.
Redundancy: Avoiding the re-calculation of a value that has already been computed.

Step 1:
Identifying the redundancy.
In the given code, both $X$ and $Y$ are assigned the result of $(a * 2)$. Assuming 'a' does not change between these lines, the calculation is performed twice.

Step 2:
Applying Common Subexpression Elimination.
The compiler can store the result of the first calculation in a temporary register or variable. For the second line, it simply assigns the stored value to $Y$.

Step 3:
Efficiency Comparison.
Multiplication is a relatively expensive operation for a CPU. A simple "copy" or "assignment" is much faster. Thus, computing once and reusing is the most efficient transformation.
Was this answer helpful?
0
0