[LLVMdev] stack usage and scoping
sshumaker at gmail.com
Fri Dec 11 13:32:09 CST 2009
I've just started using LLVM for a project I'm working on, and the
docs seem to encourage the use of alloca, with the expectation that
various optimization passes will optimize away unnecessary stack
pressure. However, I can't seem to figure out how LLVM can properly
re-use stack space, since it doesn't know the extend of a stack
variable. In this simple C example:
extern void foo(char* c);
Notice that I have two arrays of 1000 bytes declared, and pass them to
an extern function. If I compile this code with gcc, gcc notices that
the tmp doesn't extend outside of the inner scope that it's defined,
so it can re-use the stack space from the first declaration. If you
look at the assembly generated, gcc only uses something like 1024
bytes of stack space.
However, if I build this code with llvm-gcc, I end up using twice as
much stack space. There doesn't appear to be a way to tell LLVM that
a variable has finite scope. I thought maybe stacksave and
stackrestore would help, but they didn't seem to make any difference.
Note that I'm not planning on using llvm-gcc with my project anyway,
I'm just trying to figure out how LLVM can possibly 'know' about the
extent of a stack variable.
More information about the LLVMdev