The following recursive example cannot generate “tail call”. It generate “call” only. Anyone knows how to make “tail call”?
// clang -c 1.c -emit-llvm -o 1.bc
// llvm-dis 1.bc -o -
// File 1.c
int factorial(int x)
{
x–;
if (x <= 0)
return 1;
return x*factorial(x-1);
}
// output
define i32 @factorial(i32 %x) #0 {
…
%call = call i32 @factorial(i32 %sub)
…
}
This function is not tail-recursive. A tail-recursive variant would
be something like:
int factorial(int x, int accum) {
if (x == 1)
return accum;
else
return factorial(x-1, accum*x);
}
Dmitri
I compile it with clang as follows. It didn’t generate “tail call”. Should I add some options in clang command?
// clang -target mips-unknown-linux-gnu -c 1.c -emit-llvm -o 1.bc
// llvm-dis 1.bc -o -
int factorial(int x, int accum) {
if (x == 1)
return accum;
else
return factorial(x-1, accum*x);
}
Jonathan
Dmitri Gribenko gribozavr@gmail.com 於 2014/1/1 (週三) 9:06 PM 寫道﹕
The following recursive example cannot generate “tail call”. It generate
“call” only. Anyone knows how to make “tail call”?
// clang -c 1.c -emit-llvm -o 1.bc
// llvm-dis 1.bc -o -
// File 1.c
int factorial(int x)
{
x–;
if (x <= 0)
return 1;
return x*factorial(x-1);
This function is not tail-recursive. A tail-recursive variant would
be something like:
int factorial(int x, int accum) {
if (x == 1)
return accum;
else
return factorial(x-1, accum*x);
}
Dmitri