global question

Hi John,

Below is some C code and its LLVM translation (using a recent rev). The
compiler goes ahead and loads from x in main, but it seems clear that a
constant propagation pass could have inferred that x is 0. In fact that
is what happens if x is static, but in this case I cannot see how
external linkage of x would invalidate that optimization.

int x;

int main (void)
{
  return x;
}

Please correct me if I'm wrong, but how can the compiler know, that x is not initialized in another file which defines x as extern? It can only be sure, when x is declared static.

So, for me the LLVM-code is right.

(I even remember someone, that it is platform specific whether x is initialized to 0 or not... or was it a question of C89 or C99?)

regards,
Patrick.

Patrick Boettcher wrote:

Below is some C code and its LLVM translation (using a recent rev). The
compiler goes ahead and loads from x in main, but it seems clear that a
constant propagation pass could have inferred that x is 0.

I'm not sure I believe that, unless you're suggesting we treat main()
as special.

In fact that
is what happens if x is static, but in this case I cannot see how
external linkage of x would invalidate that optimization.

int x;

int main (void)
{
  return x;
}

Please correct me if I'm wrong, but how can the compiler know, that x is not
initialized in another file which defines x as extern? It can only be sure,
when x is declared static.

So, for me the LLVM-code is right.

(I even remember someone, that it is platform specific whether x is initialized
to 0 or not... or was it a question of C89 or C99?)

It's initialized to zero. Always, for all versions of C.

Andrew.