Compilers

Converting the code above to SSA form inserts an expression like "x3 = φ(x1, x2)" after the if statement. What does the φ function do?

if (c) x = 1;
else   x = 2;
y = x + 3;
  1. AIt returns the larger of x1 and x2
  2. BIt computes and returns the sum of x1 and x2
  3. CIt picks x1 or x2 by the incoming path
Show answer and explanation

Answer: C. It picks x1 or x2 by the incoming path

In SSA form the then-branch assignment becomes x1 and the else-branch assignment x2, so after the join the computation of y must specify which x to use. A φ function denotes selecting a value at a join point: x1 if control arrived from the then side, x2 if from the else side. Real machine code has no φ instruction; it is eventually replaced by register copies or similar. It has nothing to do with comparing or adding values.

  • A. It does not choose by magnitude.
  • B. It does not add the values.
  • C. Correct. It selects a value according to the incoming path.
Practice this topic Back to topic

More Compilers questions

  1. Which is an example of constant folding by a compiler?
  2. After repeatedly applying constant propagation and constant folding to the code above, what does it finally become?
  3. Which describes applying common subexpression elimination to the code above?
  4. What does dead-code elimination remove?
  5. Which pair gives the advantage and drawback of function inlining?