title: LLVM tail call optimization
body: <p>Here is my understanding of things:</p>
  
  <p>A function "f" is tail recursive when calling itself is its last action.
  Tail-recursion can be significantly optimized by forming a loop instead of calling the function again; the function's parameters are updated in place, and the body is ran again.  This is called recursive tail call optimization.</p>
  
  <p>LLVM implements recursive tail call optimization when using fastcc, GHC, or the HiPE calling convention.
  <a href="http://llvm.org/docs/CodeGenerator.html#tail-call-optimization" rel="noreferrer">http://llvm.org/docs/CodeGenerator.html#tail-call-optimization</a></p>
  
  <p>I have some questions:
  Let's consider the silly example:</p>
  
  <pre><code>int h(int x){
    if (x &lt;= 0)
      return x;
    else
      h(x-1);
  }
  </code></pre>
  
  <p>1) In their example, the keyword "tail" preceeds call.  Elsewhere I read that this keyword is optional.  Suppose the function above is translated to LLVM appropriately, do the last few lines need to be</p>
  
  <pre><code>%x' = load *i32 %x
  %m = tail call fastcc i32 @h(i32 %x')
  ret %m
  </code></pre>
  
  <p>2) What is the meaning of the inreg option in their example?</p>
  
  <p>3) I would not want to perform tail call optimizations all over the place, only for recursive functions.  Is there a way I can get LLVM to only perform the optimization (when available) for recursive functions?</p>