Why is this a hard problem?

To understand why mutable variables cause complexities in SSA construction, consider this extremely simple C example:

  1. int G, H;
  2. int test(_Bool Condition) {
  3. int X;
  4. if (Condition)
  5. X = G;
  6. else
  7. X = H;
  8. return X;
  9. }

In this case, we have the variable “X”, whose value depends on the path executed in the program. Because there are two different possible values for X before the return instruction, a Phi node is inserted to merge the two values. The LLVM IR that we want for this example looks like this:

  1. @G = weak global i32 0 ; type of @G is i32*
  2. @H = weak global i32 0 ; type of @H is i32*
  3. define i32 @test(i1 %Condition) {
  4. entry:
  5. br i1 %Condition, label %cond_true, label %cond_false
  6. cond_true:
  7. %X.0 = load i32* @G
  8. br label %cond_next
  9. cond_false:
  10. %X.1 = load i32* @H
  11. br label %cond_next
  12. cond_next:
  13. %X.2 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
  14. ret i32 %X.2
  15. }

The control flow graph for the above IR:

Why is this a hard problem? - 图1

In this example, the loads from the G and H global variables are explicit in the LLVM IR, and they live in the then/else branches of the if statement (cond_true/cond_false). In order to merge the incoming values, the X.2 phi node in the cond_next block selects the right value to use based on where control flow is coming from: if control flow comes from the cond_false block, X.2 gets the value of X.1. Alternatively, if control flow comes from cond_true, it gets the value of X.0. The intent of this chapter is not to explain the details of SSA form. For more information, see one of the many online references.

The question for this article is “who places the phi nodes when lowering assignments to mutable variables?”. The issue here is that LLVM requires that its IR be in SSA form: there is no “non-SSA” mode for it. However, SSA construction requires non-trivial algorithms and data structures, so it is inconvenient and wasteful for every front-end to have to reproduce this logic.