Class FindTailCalls
- java.lang.Object
-
- gnu.expr.ExpVisitor<Expression,D>
-
- gnu.expr.ExpExpVisitor<Expression>
-
- gnu.expr.FindTailCalls
-
- All Implemented Interfaces:
SourceLocator,SourceLocator,Locator
public class FindTailCalls extends ExpExpVisitor<Expression>
A visitor that checks for tails-calls; also notes read/write/call accesses.The main goal of this pass is to figure of which functions can and should be inlined. We inline a function where possible to avoid closure allocation and related overheads, and also (more importantly) to enable tail-call-optiomization: A tail-call can be implemented cheaply and correctly using a
gotobytecode instruction - but only for functions inlined in the same JVM method.We currently restrict inlining to cases when we can do so without code duplication: When the function is only called once, not counting tail-calls. Because passing a "return link" is difficult, we require that all calls to the function have the same "return continuation".
The extra visitor parameter is the
returnContinuation- the expression we "return to" - i.e. when done evaluating an expression, we're also done with thereturnContinuation. Normally it is is sameExpressionas we are visiting, but (for example) when visiting the last expression of aBeginExpthereturnContinuationis the same as that of the containingBeginExp. We're in a tail-context (in the sense of the Scheme reports) iff the currentreturnContinuationis thebodyof the currentLambdaExp.For each non-inlined function
fwe defineinlineSet(f)as the set of functionsgsuch thatg.inlinedIn(f). There are various requirements for a functiongto be inlined; for example we require that it have a fixed number of arguments. Because of the no-duplication policy, all calls toghave to be known, and all calls have to be from other inlined functions: Ifhcallsg, thenh==f || h.inlinedIn(f). In addition all calls must have the samereturnContinuation.This analysis is done in two parts: First the main expression-walker, and at the end (in
visitModuleExp) we check each procedure using the data from the main pass.When this vistor is done, it has set the
returnContinuation,tailCallers, andinlineHomefields of aLambdaExp. If functionftail-callsg, thenfis added to the set ofg'stailCallers.If there is a non-tail-call to
gthen we try to setg'sreturnContinuationto the current (context)returnContinuation, if the former hasn't been set yet; otherwise we set it to the specialunknownContinuationvalue. We also constructtailCallersas the list of functionshthat make a tail-call tog. At the end of this pass, if a function'sreturnContinuationis is non-null and notunknownthen it has a unique continuation, and the function can potentially be inlined at the location of the continuation. However, that depends on if any tail-calls also have same return-continuation; that analysis happens later incheckInlineable.(In addition a
validatemethod (executed during the earlierInlinedCallspass may pre-initialized thereturnContinuationinlineHomefields, but only for aLambdaExpthat will be inlined during code generation in a customcompilemethod.)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface gnu.text.SourceLocator
SourceLocator.Simple
-
-
Field Summary
Fields Modifier and Type Field Description HashMap<Expression,Expression>savedReturnContinuations-
Fields inherited from class gnu.expr.ExpVisitor
currentLambda, exitValue, messages
-
-
Constructor Summary
Constructors Constructor Description FindTailCalls()
-
Method Summary
-
Methods inherited from class gnu.expr.ExpExpVisitor
defaultValue, error, error, update
-
Methods inherited from class gnu.expr.ExpVisitor
error, getColumnNumber, getCompilation, getCurrentLambda, getEndColumn, getEndLine, getExitValue, getFileName, getLanguage, getLineNumber, getMessages, getPublicId, getStartColumn, getStartLine, getSystemId, isStableSourceLocation, noteError, setColumn, setContext, setFile, setLine, setLine, visit, visit, visitAndUpdate, visitDeclarationType, visitDeclarationTypes, visitExps, visitExps, visitLangExp, visitObjectExp, visitQuoteExp, visitReferenceExp, visitScopeExp, visitThisExp
-
-
-
-
Field Detail
-
savedReturnContinuations
public HashMap<Expression,Expression> savedReturnContinuations
-
-
Method Detail
-
findTailCalls
public static void findTailCalls(Expression exp, Compilation comp)
-
visitExpression
protected Expression visitExpression(Expression exp, Expression returnContinuation)
- Overrides:
visitExpressionin classExpVisitor<Expression,Expression>
-
visitExps
public Expression[] visitExps(Expression[] exps)
-
visitApplyExp
protected Expression visitApplyExp(ApplyExp exp, Expression returnContinuation)
- Overrides:
visitApplyExpin classExpVisitor<Expression,Expression>
-
visitBlockExp
protected Expression visitBlockExp(BlockExp exp, Expression returnContinuation)
- Overrides:
visitBlockExpin classExpVisitor<Expression,Expression>
-
visitExitExp
protected Expression visitExitExp(ExitExp exp, Expression returnContinuation)
- Overrides:
visitExitExpin classExpVisitor<Expression,Expression>
-
visitBeginExp
protected Expression visitBeginExp(BeginExp exp, Expression returnContinuation)
- Overrides:
visitBeginExpin classExpVisitor<Expression,Expression>
-
visitFluidLetExp
protected Expression visitFluidLetExp(FluidLetExp exp, Expression returnContinuation)
- Overrides:
visitFluidLetExpin classExpVisitor<Expression,Expression>
-
visitLetExp
protected Expression visitLetExp(LetExp exp, Expression returnContinuation)
- Overrides:
visitLetExpin classExpVisitor<Expression,Expression>
-
postVisitDecls
public void postVisitDecls(ScopeExp exp)
-
visitIfExp
protected Expression visitIfExp(IfExp exp, Expression returnContinuation)
- Overrides:
visitIfExpin classExpVisitor<Expression,Expression>
-
visitCaseExp
protected Expression visitCaseExp(CaseExp exp, Expression returnContinuation)
- Overrides:
visitCaseExpin classExpVisitor<Expression,Expression>
-
visitLambdaExp
protected Expression visitLambdaExp(LambdaExp exp, Expression returnContinuation)
- Overrides:
visitLambdaExpin classExpVisitor<Expression,Expression>
-
visitDefaultArgs
public void visitDefaultArgs(LambdaExp exp, Expression d)
- Overrides:
visitDefaultArgsin classExpVisitor<Expression,Expression>
-
visitClassExp
protected Expression visitClassExp(ClassExp exp, Expression returnContinuation)
- Overrides:
visitClassExpin classExpVisitor<Expression,Expression>
-
visitSetExp
protected Expression visitSetExp(SetExp exp, Expression returnContinuation)
- Overrides:
visitSetExpin classExpVisitor<Expression,Expression>
-
visitTryExp
protected Expression visitTryExp(TryExp exp, Expression returnContinuation)
- Overrides:
visitTryExpin classExpVisitor<Expression,Expression>
-
visitSynchronizedExp
protected Expression visitSynchronizedExp(SynchronizedExp exp, Expression returnContinuation)
- Overrides:
visitSynchronizedExpin classExpVisitor<Expression,Expression>
-
visitModuleExp
protected Expression visitModuleExp(ModuleExp exp, Expression returnContinuation)
- Overrides:
visitModuleExpin classExpVisitor<Expression,Expression>
-
-