Skip to content

Commit

Permalink
Make MinimizeExitPoints a peephole optimization.
Browse files Browse the repository at this point in the history
This allows us to add it to the PeepholeOptimziationsPass, and
remove it as an independent compiler pass.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132586900
  • Loading branch information
blickly committed Sep 9, 2016
1 parent 59e7c63 commit 168485e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 34 deletions.
14 changes: 1 addition & 13 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -941,8 +941,6 @@ private List<PassFactory> getCodeRemovingPasses() {
} }


if (options.foldConstants) { if (options.foldConstants) {
// These used to be one pass.
passes.add(minimizeExitPoints);
passes.add(peepholeOptimizations); passes.add(peepholeOptimizations);
} }


Expand Down Expand Up @@ -1493,6 +1491,7 @@ private static CompilerPass createPeepholeOptimizationsPass(AbstractCompiler com
final boolean late = false; final boolean late = false;
final boolean useTypesForOptimization = compiler.getOptions().useTypesForOptimization; final boolean useTypesForOptimization = compiler.getOptions().useTypesForOptimization;
return new PeepholeOptimizationsPass(compiler, return new PeepholeOptimizationsPass(compiler,
new MinimizeExitPoints(compiler),
new PeepholeMinimizeConditions(late, useTypesForOptimization), new PeepholeMinimizeConditions(late, useTypesForOptimization),
new PeepholeSubstituteAlternateSyntax(late), new PeepholeSubstituteAlternateSyntax(late),
new PeepholeReplaceKnownMethods(late), new PeepholeReplaceKnownMethods(late),
Expand Down Expand Up @@ -2156,17 +2155,6 @@ protected CompilerPass create(AbstractCompiler compiler) {
} }
}; };


/**
* Perform local control flow optimizations.
*/
private final PassFactory minimizeExitPoints =
new PassFactory("minimizeExitPoints", false) {
@Override
protected CompilerPass create(AbstractCompiler compiler) {
return new MinimizeExitPoints(compiler);
}
};

/** /**
* Use data flow analysis to remove dead branches. * Use data flow analysis to remove dead branches.
*/ */
Expand Down
25 changes: 7 additions & 18 deletions src/com/google/javascript/jscomp/MinimizeExitPoints.java
Expand Up @@ -16,14 +16,12 @@


package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback;
import com.google.javascript.jscomp.NodeTraversal.FunctionCallback;
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.jstype.TernaryValue; import com.google.javascript.rhino.jstype.TernaryValue;

import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
Expand All @@ -32,28 +30,20 @@
* *
* @author johnlenz@google.com (John Lenz) * @author johnlenz@google.com (John Lenz)
*/ */
class MinimizeExitPoints extends AbstractShallowCallback implements CompilerPass { class MinimizeExitPoints extends AbstractPeepholeOptimization {
AbstractCompiler compiler; AbstractCompiler compiler;


MinimizeExitPoints(AbstractCompiler compiler) { MinimizeExitPoints(AbstractCompiler compiler) {
this.compiler = compiler; this.compiler = compiler;
} }


@Override @VisibleForTesting
public void process(Node externs, Node root) { final CompilerPass asCompilerPass() {
NodeTraversal.traverseChangedFunctions(compiler, new FunctionCallback() { return new PeepholeOptimizationsPass(compiler, this);
@Override
public void enterFunction(AbstractCompiler compiler, Node root) {
if (root.isFunction()) {
root = root.getLastChild();
}
NodeTraversal.traverseEs6(compiler, root, MinimizeExitPoints.this);
}
});
} }


@Override @Override
public void visit(NodeTraversal t, Node n, Node parent) { Node optimizeSubtree(Node n) {
switch (n.getToken()) { switch (n.getToken()) {
case LABEL: case LABEL:
tryMinimizeExits( tryMinimizeExits(
Expand All @@ -78,8 +68,6 @@ public void visit(NodeTraversal t, Node n, Node parent) {
break; break;


case BLOCK: case BLOCK:
// The traversal starts at the function block so `parent` will be null, so we use
// getParent here.
if (n.getParent() != null && n.getParent().isFunction()) { if (n.getParent() != null && n.getParent().isFunction()) {
tryMinimizeExits(n, Token.RETURN, null); tryMinimizeExits(n, Token.RETURN, null);
} }
Expand All @@ -94,6 +82,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
default: default:
break; break;
} }
return n;
} }


/** /**
Expand Down
Expand Up @@ -43,13 +43,13 @@ protected CompilerPass getProcessor(final Compiler compiler) {
public void process(Node externs, Node js) { public void process(Node externs, Node js) {
new CreateSyntheticBlocks(compiler, START_MARKER, END_MARKER).process( new CreateSyntheticBlocks(compiler, START_MARKER, END_MARKER).process(
externs, js); externs, js);
new MinimizeExitPoints(compiler).process(externs, js); new MinimizeExitPoints(compiler).asCompilerPass().process(externs, js);
new PeepholeOptimizationsPass(compiler, new PeepholeOptimizationsPass(compiler,
new PeepholeRemoveDeadCode(), new PeepholeRemoveDeadCode(),
new PeepholeMinimizeConditions(true /* late */, false /* useTypes */), new PeepholeMinimizeConditions(true /* late */, false /* useTypes */),
new PeepholeFoldConstants(true, false)) new PeepholeFoldConstants(true, false))
.process(externs, js); .process(externs, js);
new MinimizeExitPoints(compiler).process(externs, js); new MinimizeExitPoints(compiler).asCompilerPass().process(externs, js);
new Denormalize(compiler).process(externs, js); new Denormalize(compiler).process(externs, js);
} }
}; };
Expand Down
Expand Up @@ -22,7 +22,7 @@
public final class MinimizeExitPointsTest extends CompilerTestCase { public final class MinimizeExitPointsTest extends CompilerTestCase {
@Override @Override
protected CompilerPass getProcessor(final Compiler compiler) { protected CompilerPass getProcessor(final Compiler compiler) {
return new MinimizeExitPoints(compiler); return new MinimizeExitPoints(compiler).asCompilerPass();
} }


@Override @Override
Expand Down

0 comments on commit 168485e

Please sign in to comment.