[LLVMdev] inlining of recursive functions

Christophe Raffalli raffalli at univ-savoie.fr
Tue Oct 4 16:36:09 PDT 2011


Hello,

In lib/Analysis/InlineCost.cpp, inlining of recursive functions is
disabled because it is
like loop unrolling. But

- I could not find a way to have loop unrolling do the job
- In the context of functionnal languages (I am implementing one), inlining
small recursive functions is often a great gain

My question is what is the cleanest and simplest way to inline small
recursive functions ?
 Just commenting out this test (which is probably not correct)
gives on fibonacci (C code attached) on my intel dual core old mac :

clang -O3 with inlining 0.16s
gcc -O3 0.50s
clang -O3 without inlining 1.20s

fibonacci is really useless but if you look at OCaml or GHC libraries,
you will find plenty of very small recursive functions on lists for
instance (map, fold_left, ...).
And the figure here should apply there to ... with probably a less
dramatic effect in general.

Best regards,
Christophe

---- C code ----
#include<stdio.h>

long long fib(long long n) {
  if (n == 0 || n == 1) return 1;
  else return (fib (n - 2) + fib (n - 1));
}

void test(long long f (long long), long long n, long long m) {
  long long r = f(n);
  printf("%lld ", r);
  if (n < m) test (f,n+1, m);
}

int main() {
  test(&fib,0,37);
  printf("\n");
  return 0;
}





More information about the llvm-dev mailing list