One of annoying problems in ThinLTO is that variable promotion can prevent optimiser from constant folding/propagation. Consider
following C program
main.c
int main() { int foo(); return foo(); }
foo.c
#include <stdlib.h> static int gFoo = 1; int foo() { return gFoo; } void bar() { gFoo = rand(); }
Note that bar() is dead, so variable gFoo is never really modified. However promotion of gFoo to hidden global makes it impossible for optimizer to mark it constant and fold it afterwards.
To overcome this problem I suggest to introduce concept of "constant reference" which implies all accesses to a given global from a given function are non-volatile loads. With this we can determine which variables will become read-only after DCE and convert them to constants with a special LLVM pass.
This patch implements first part of this approach. It doesn't have good test cases yet - for now I'm just interested of your opinion.
Maybe have this flag just on the GlobalVarSummary? Not needed on the function summary, and presumably the alias summary could just look through to the base object summary.