[PATCH] Remove "localize global" optimization

Shuxin Yang shuxin.llvm at gmail.com
Thu Oct 3 15:42:43 PDT 2013

I realized llvm has such transformation few weeks ago when someone 
reportd a bug
saying compiler dose a lousy job in disambiguate "a" and *p:

  static in a;
  void foo(int *p) {
        for (int i = 0; i < a; i++)
          *p++ = a + ....

  bar() ... {}

    My immediate reaction was alias analysis should bear the blame. That 
is because LLVM dose not
have handle "addr-taken", it only detect addr-capture. A var without 
addr-capture dosen't
mean it cannot be indirectly access; however, a var dose not have it 
addr taken will not
be indirectly accessed.

   I was curious if compiler is able to demote the global var into local 
var. After some code navigation,
I found this piece of code.  I didn't read it carefully, but I had a 
feeling it's not quite correct -- the code
dosen't verify if the global variable has upward-exposed use when it try 
to demote the global var,
which is bit foolhardy. I guess the right way to demote global variable 
is to demote it one scope
at a time:
   - first, demote global var to function-scoped static var by a 
module-pass, then
   - then a function-pass demote static variable to auto-var. It need to 
check if the
      var has exposed-use (which need cfg) or not to make sure it is safe.

   I think this is bit hard for LLVM as it dose not differentiate 
func-scoped var and global var. But
notion of addr-taken might alleviate the problem a bit, as it eliminate 
the alias problem albeit
it dose not physically delete the global variable (and hence dose not 
help to reduce the code size).

More information about the llvm-commits mailing list