[LLVMdev] stack usage and scoping

Scott Shumaker sshumaker at gmail.com
Fri Dec 11 11:32:09 PST 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);

void bar()
     char tmp[1000];

    char tmp[1000];

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 llvm-dev mailing list