tool options to generate spill code

Hello,

For the following test case, reg.c

#include <stdio.h>

int getinput()
{
static int u=10;
return u++;
}

int main()
{

int a,b,c,d,e,f,g;

a=getinput();
b=getinput();
c=getinput();
d=getinput();
e=getinput();
f=getinput();
g=getinput();

printf("%d %d %d %d %d %d %d\n",a,b,c,d,e,f,g);
a=b=c=d=e=f=g=0;
return 0;
}

1. $clang reg.c -Xclang -disable-O0-optnone -S -emit-llvm -o reg.ll && opt -mem2reg -S reg.ll -o reg.ll && llc --regalloc=greedy reg.ll -o reg.s

2. $clang reg.c -Xclang -disable-llvm-passes -S -emit-llvm -o reg.ll && opt -mem2reg -S reg.ll -o reg.ll && llc --regalloc=greedy reg.ll -o reg.s

3. $clang reg.c -S -emit-llvm -o reg.ll && opt -mem2reg -S reg.ll -o reg.ll && llc --regalloc=greedy reg.ll -o reg.s

Only 1. gives the spill code but it deletes the dead stores., 2 and 3 give only allocas. Why so??

How -mem2reg works and what it depends on?

How to generate the spill code with dead stores?

Thank you.

Regards,
Priyanka

If you use -O0 to generate LLVM IR you must pass -disable-O0-optnone or it cannot be optimized. If you look at the LLVM IR for #2 and #3 it will say “optnone” [1] in the attributes which will prevent any optimization like mem2reg [2] from running.

[1] https://clang.llvm.org/docs/AttributeReference.html#optnone
[2] http://llvm.org/docs/Passes.html#mem2reg-promote-memory-to-register

Thanks for the clarification. Why does mem2reg remove the dead stores?

Regards,
Priyanka