How does llvm compute the live ranges (intervals) of its temporaries? Here is an example C file:
$ cat main.c
int main()
{
int i = 200;
int j = 300;
while (j)
{
i=i+1;
j=j-1;
}
return 0;
}
I execute the following commands, and then examine both main.ll and main.mem2reg.ll. I'm probably wrong, but it looks like computing live ranges is much easier on main.ll(?) is mem2reg an essential pass when computing the live range? or is it a nice-to-have for certain optimizations?
$ clang -c -emit-llvm -O0 main.c -o main.bc
$ opt -instnamer main.bc -o main.bc
$ opt -mem2reg main.bc -o main.mem2reg.bc
$ llvm-dis main.bc
$ llvm-dis main.mem2reg.bc
To perform live range analysis, you may have to know some knowledge about liveness analysis or you can checkout cranelift's annotation. Pass
mem2regjust transform the IR into SSA form, but for live range analysis, I think this is not necessary.I have an simple implementation of live variable analysis with llvm, please checkout my GitHub repo: https://github.com/lijiansong/clang-llvm-tutorial/tree/master/live-variable-analysis