A Tail Rec Transformer.
What it does:
Finds method calls in tail-position and replaces them with jumps.
A call is in a tail-position if it is the last instruction to be
executed in the body of a method. This includes being in
tail-position of a return
from a Labeled
block which is itself
in tail-position (which is critical for tail-recursive calls in the
cases of a match
). To identify tail positions, we recurse over
the trees that may contain calls in tail-position (trees that can't
contain such calls are not transformed).
When a method contains at least one tail-recursive call, its rhs is wrapped in the following structure:
var localForParam1: T1 = param1
...
while (<empty>) {
tailResult[ResultType]: {
return {
// original rhs with tail recursive calls transformed (see below)
}
}
}
Self-recursive calls in tail-position are then replaced by (a)
reassigning the local var
s substituting formal parameters and
(b) a return
from the tailResult
labeled block, which has the
net effect of looping back to the beginning of the method.
If the receiver is modifed in a recursive call, an additional var
is used to replace this
.
As a complete example of the transformation, the classical fact
function, defined as:
def fact(n: Int, acc: Int): Int =
if (n == 0) acc
else fact(n - 1, acc * n)
is rewritten as:
def fact(n: Int, acc: Int): Int = {
var acc$tailLocal1: Int = acc
var n$tailLocal1: Int = n
while (<empty>) {
tailLabel1[Unit]: {
return {
if (n$tailLocal1 == 0)
acc$tailLocal1
else {
val n$tailLocal1$tmp1: Int = n$tailLocal1 - 1
val acc$tailLocal1$tmp1: Int = acc$tailLocal1 * n$tailLocal1
n$tailLocal1 = n$tailLocal1$tmp1
acc$tailLocal1 = acc$tailLocal1$tmp1
(return[tailLabel1] ()): Int
}
}
}
}
}
As the JVM provides no way to jump from a method to another one, non-recursive calls in tail-position are not optimized.
A method call is self-recursive if it calls the current method and the method is final (otherwise, it could be a call to an overridden method in a subclass). Recursive calls on a different instance are optimized.
This phase has been moved after erasure to allow the use of vars
for the parameters combined with a WhileDo
. This is also
beneficial to support polymorphic tail-recursive calls.
In scalac, if the method had type parameters, the call must contain the same parameters as type arguments. This is no longer the case in dotc thanks to being located after erasure. In scalac, this is named tailCall but it does only provide optimization for self recursive functions, that's why it's renamed to tailrec
Attributes
- Authors:
Erik Stenman, Iulian Dragos, ported and heavily modified for dotty by Dmitry Petrashko moved after erasure and adapted to emit
Labeled
blocks by Sébastien Doeraene- Companion:
- object
- Graph
- Supertypes