[cfe-users] What option can make clang generate tail call IR?

gamma_chen gamma_chen at yahoo.com.tw
Thu Jan 2 01:42:21 PST 2014


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 at gmail.com> 於 2014/1/1 (週三) 9:06 PM 寫道﹕
 
On Fri, Dec 20, 2013 at 8:04 AM, gamma_chen <gamma_chen at yahoo.com.tw> wrote:
> 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

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20140102/069150ca/attachment.html>


More information about the cfe-users mailing list