1

In SSA form, a variable assigned from a phi ends up with the appropriate value depending on which path it was reached by. But what happens if, perhaps via some unusual path, both inputs to the phi have been assigned? e.g.

a = 1
...
b = 2
...
c = phi(a, b)

Is it defined to behave like a union such that c ends up with the value 2 because that was the last assignment?

rwallace
  • 31,405
  • 40
  • 123
  • 242

1 Answers1

5

For SSA (Single Static Assignment), the two operand of a phi node should be two definition of a specific variable.

For your example, that means b=2, and a=1 are two definition for a single variable. And since b=2 will always run after a=1. Therefore, The definition b=2 will kill the definition of a=1. So, your phi(a,b) is in fact illegal.

phi's operands usually are definitions from two different execution pass of the program.

Kun Ling
  • 2,211
  • 14
  • 22
  • Fair enough. Do I understand correctly that this means there is no simple polynomial-time algorithm to check whether a chunk of code is valid SSA? – rwallace May 21 '15 at 13:35
  • To me, it seems polynomial-time algorithm for valid SSA checking exists. For SSA, we only need to make sure that, 1) every assignment or definition create a new version of a variable. 2) if the code has different path, like if-then-else, for-loop. `PHI` node is inserted to merge them. But it is a bit challenge to handle alias well. – Kun Ling May 21 '15 at 13:40
  • And 3) conversely there is no possible path on which two operands to a phi could be assigned - this seems about as hard as calculating SSA in the first place, so it can be done in polynomial time, just not very simply. Of course, there are optimizations that would require similar checking in any case. – rwallace May 21 '15 at 13:45