2

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

BenJacob
  • 957
  • 10
  • 31

1 Answers1

7

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.

  • 2
    This being the case does it mean that when performing data flow analysis and optimisation we ignore global variables as we might arrays? – BenJacob May 04 '17 at 21:16
  • 1
    @BenJacob Yes, at least for analyses based solely on SSA variables. You can still used non-SSA algorithms that reason about mutable storage locations (e.g. for two loads from the same address can be considered equivalent if there are no intervening writes between the two locations). As I said, there are also extensions of SSA to memory locations, but LLVM at least doesn't implement them (at the IR level; it has a ["memory SSA"](http://llvm.org/docs/MemorySSA.html) utility nowadays). –  May 09 '17 at 15:32