[LLVMdev] RegAllocFast uses too much stack

Nick Lewycky nlewycky at google.com
Mon Jul 11 13:48:46 PDT 2011


I discovered recently that RegAllocFast spills all the registers before
every function call. This is the root cause of one of our recursive
functions that takes about 150 bytes of stack when built with gcc (same at
-O0 and -O2, or 120 bytes at llc -O2) taking 960 bytes of stack when built
by llc -O0. That's pretty bad for situations where you have small stacks,
which is not uncommon for threaded software on a 32-bit architecture, or a
signal handler.

I realize that some of this is intentional and we don't want to do
optimization at -O0, but I'm really hoping there's something we could
sensibly do to improve this. Here's an example:

  extern "C" void foo(int);

  void test() {
    foo(0);
    foo(1);
    foo(2);
  }

This doesn't just spill out all the registers to the stack before each call,
we also set up 0, 1 and 2 into regs first, then spill them and don't even
get a chance to reuse stack slots. That's just bad:

        pushq   %rax
        movl    $2, %edi
        movl    $1, %eax
        movl    $0, %ecx
        movl    %edi, 4(%rsp)           # 4-byte Spill
        movl    %ecx, %edi
        movl    %eax, (%rsp)            # 4-byte Spill
        callq   foo
        movl    (%rsp), %edi            # 4-byte Reload
        callq   foo
        movl    4(%rsp), %edi           # 4-byte Reload
        callq   foo
        popq    %rax
        ret

Does anyone have any ideas what we could do that doesn't add to the compile
time?

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110711/c59efb24/attachment.html>


More information about the llvm-dev mailing list