<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>Il giorno 09/mag/2014, alle ore 01:00, Nick Lewycky <<a href="mailto:nlewycky@google.com">nlewycky@google.com</a>> ha scritto:</div><br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>At the moment TRE runs, the function reads:</div><div><br></div><div><div>; Function Attrs: nounwind readnone</div><div>define i32 @fib(i32 %n) #0 {</div><div>entry:</div><div> %cmp = icmp ult i32 %n, 2</div>
<div> br i1 %cmp, label %cond.end, label %cond.false</div><div><br></div><div>cond.false: ; preds = %entry</div><div> %sub = add i32 %n, -1</div><div> %call = tail call i32 @fib(i32 %sub)</div>
<div> %sub1 = add i32 %n, -2</div><div> %call2 = tail call i32 @fib(i32 %sub1)</div><div> %add = add i32 %call, %call2</div><div> br label %cond.end</div><div><br></div><div>cond.end: ; preds = %entry, %cond.false</div>
<div> %cond = phi i32 [ %add, %cond.false ], [ %n, %entry ]</div><div> ret i32 %cond</div><div>}</div></div><div><br></div><div>TRE starts at the 'ret' and looks upwards and checks all of that block's predecessors to find any that end in unconditional branches. Then it walks upwards in such blocks to find if there's a "TRE candidate" which is a call to itself (but aborts if there are instructions is can't handle between the call and the end of the block), and manages to find %call2 -- and calls EliminateRecursiveTailCall on it. Good job! At this stage, it transforms the "br label %cond.end" into a "ret i32 %add" and updates the phi in %cond.end. There are now *two* returns, the one in %cond.end is "ret i32 %n".</div>
<div><br></div><div>EliminateRecursiveTailCall detects that TRE'ing this requires performing accumulator recursion elimination. To do this, it checks that "the function containing the specified tail call consistently returns the same runtime-constant value" (at all exit points except for our ret at the end of %cond.false). Now that we have a ret i32 %n, this is false; the safety check finds that the argument for %n can change because we pass in %sub and %sub1 into ourselves instead of always passing %n to ourselves. That's what stops the transformation.</div>
<div><br></div><div>Your working example makes two modifications, you change "n < 2" to "n <= 2" and you change "? n" to "? 1". It's the second of these that will make the difference, because it changes %cond to:</div>
<div><br></div><div> %cond = phi i32 [ %add, %cond.false ], [ 1, %entry ]</div><div><br></div><div>Everything proceeds the same way as above, except that when the branch in %cond.false is replaced with a return, the return in %cond.end folds to "ret i32 1", a constant. Being the same value each time, this is simple enough for accumulator recursion elimination.</div>
<div><br></div></div></div></div></blockquote><div><br></div><div>Thank you very much for the explanation! It all makes sense now.</div><div>However, the point remains that gcc (compiling down from both C or fortran) can indeed optimize this code despite the non-constant</div><div>base case. Do you or someone know what reasoning does it apply?</div><div>Should I file a bug report?</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>PS. In case you aren't aware, I recently bemoaned the state of our TRE abilities here: <a href="http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140505/216092.html">http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140505/216092.html</a></div>
</div></div></div></blockquote></div><div><br></div>Very interesting!<div><br></div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Nick</div></div></div></blockquote><div><br></div>Thanks,</div><div>Nicola</div><div><div><br></div></div></body></html>