link-time optimization

I'm experimenting a bit with clang + llvm to get an idea of what it
can and can't do. It seems to do a very good job with compile-time
optimization and function inlining; I'm wondering how to enable
link-time optimization properly, and what its limitations are.

I have a set of simple files computing the Fibonacci function
iteratively (attached but with synopsis below; I had to rename a batch
file to get Google to allow the attachment):

Foo.cpp:

int Foo::fib5(int n) {
  Bar bar;
  if (n <= 0)
    return 0;
  int p1 = 0;
  int p2 = 1;
  for (int i = 0; i < n; ++i)
  {
    int p2new = bar.sum2(p1,p2);
    p1 = p2;
    p2 = p2new;
  }
  return p2;
}

Bar.cpp:

int Bar::sum2(int a, int b)
{
  return a+b;
}

Obviously there's no way for the compiler compiling each file
separately to inline the Bar::sum2() function in Foo::fib5(), but I
was hoping it would be smart enough to inline it at link time. (Alas,
no.) Is there a way to tell it to create inlined functions at link
time, even at the cost of code duplication? If so, how? If not, what's
the limitation?

I'm compiling with -O4 on a WinXP box and with the prebuilt binaries
for clang/llvm 2.9 (http://llvm.org/releases/download.html) running
with mingw-32

--Jason

test2.zip (1.72 KB)

I am not sure if -O4 enables LTO on windows, but you can try to
replace llvm-link with llvm-ld in your build script.

- xi

Just tried it. Wow -- it inlined the whole main() function except for
calls to printf.

thanks!