compiler-constructionglobal-variablesssa

Global variables in Single Static Assignment Form


I am working on a compiler that uses SSA for a language which contains global variables. I am wondering how i should implement uses and definitions of global variables, for example how should I convert the below code?

Non SSA form:

x;
y;

main () {
  x = 0;
  foo();
  y = x;
  x = 2;
  foo();
}

foo () {
  print x;
  x = 1;
}

In SSA form there are some places where I am unsure of the subscripts to use:

main () {
  x.0 = 0;
  foo()
  y.0 = x.?
  x.1 = 2;
  foo();
}

foo () {
  print x.?;
  x.? = 1;
}

I have thought about adding in phi-functions but this doesn't seem to solve the problem of what subscripts those phi functions are referring to.

Many thanks, Ben


Solution

  • Classical SSA doesn't really cover global variables, or any other memory location that may be read and written by code you don't see. There are extensions that try to cover heap memory, but it doesn't appear that you're pursuing one of those.

    LLVM, as a point of comparison, doesn't try to bring globals into SSA form. A global variable is a memory location, and its "name" is a pointer to that memory location, so accessing global variables is an ordinary load/store operation.