Re: knowing when it is tail recursion or not?
- From: torbenm@xxxxxxxxxxxxxx (Torben Ægidius Mogensen)
- Date: Mon, 19 Jan 2009 13:12:39 +0100
George Neuner <gneuner2@xxxxxxxxxxx> writes:
On Thu, 15 Jan 2009 16:06:25 -0800 (PST), raould <raould@xxxxxxxxx>
wrote:
why don't compilers tell us when we have recursion that is not going
to be tail-call-optimized-away into a loop?
Because then you'd get warnings on any use of recursion.
Local tail recursion is a special case that is easy to identify, but,
under the right circumstances, a mutual recursion that spans multiple
functions could potentially be turned into a loop. However the
analysis to identify such things is complex and if the loop path is
conditional it might require much code duplication to take advantage
of it.
That is not entirely true. You can locally identify tail calls and
non-tail calls, so you could conceivably give warnings with every
non-tail call, but you wouldn't know if it is part of a recursion.
However, mutually recursive functions are bound to be in the
compilation unit (unless you allow mutualy recursive compilation
units, which few languages do), so the analysis need not span more
than one compilation unit. Basically, make a local call graph and
mark all calls as tail or non-tail. If there is a loop in the graph
that contains a non-tail call, emit a warning.
However, any nontrivial program will contain non-tail recursion, so ou
should be able to annotate a call to mark that you know this is
non-tail recursion, and please don't warn me about this. This is,
however, rather tedious.
As for optimisation, tail self-recursion is obviously the easiest, but
any tail call can be optimised (even if it is not part of a
recursion), so even with only local detection of tail calls, you gain
a significant benefit.
However, the ideal is to find strongly connected components of tail
calls and turn each of these into a single function such that the tail
calls inside the component are all local jumps. If the component has
multiple entry points, this might be a bit complicated, but not too
bad. Tail calls into or out of the component would just get "normal"
tail-call optimisation.
Torben
.
- Follow-Ups:
- Re: knowing when it is tail recursion or not?
- From: George Neuner
- Re: knowing when it is tail recursion or not?
- From: rossberg
- Re: knowing when it is tail recursion or not?
- References:
- knowing when it is tail recursion or not?
- From: raould
- Re: knowing when it is tail recursion or not?
- From: George Neuner
- knowing when it is tail recursion or not?
- Prev by Date: a minimal definition of classic lambda-calculus?
- Next by Date: Re: sml operators and/or
- Previous by thread: Re: knowing when it is tail recursion or not?
- Next by thread: Re: knowing when it is tail recursion or not?
- Index(es):
Relevant Pages
|