Skip to content

Commit

Permalink
Modify minimize exit points to only visit changed functions.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125255755
  • Loading branch information
concavelenz authored and blickly committed Jun 20, 2016
1 parent 329111b commit 3d64f6a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
35 changes: 20 additions & 15 deletions src/com/google/javascript/jscomp/MinimizeExitPoints.java
Expand Up @@ -17,7 +17,8 @@
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; 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;
Expand All @@ -31,10 +32,7 @@
* *
* @author johnlenz@google.com (John Lenz) * @author johnlenz@google.com (John Lenz)
*/ */
class MinimizeExitPoints class MinimizeExitPoints extends AbstractShallowCallback implements CompilerPass {
extends AbstractPostOrderCallback
implements CompilerPass {

AbstractCompiler compiler; AbstractCompiler compiler;


MinimizeExitPoints(AbstractCompiler compiler) { MinimizeExitPoints(AbstractCompiler compiler) {
Expand All @@ -43,8 +41,15 @@ class MinimizeExitPoints


@Override @Override
public void process(Node externs, Node root) { public void process(Node externs, Node root) {
// TODO(johnlenz): only inspect changed functions NodeTraversal.traverseChangedFunctions(compiler, new FunctionCallback() {
NodeTraversal.traverseEs6(compiler, root, this); @Override
public void enterFunction(AbstractCompiler compiler, Node root) {
if (root.isFunction()) {
root = root.getLastChild();
}
NodeTraversal.traverseEs6(compiler, root, MinimizeExitPoints.this);
}
});
} }


@Override @Override
Expand Down Expand Up @@ -72,11 +77,12 @@ public void visit(NodeTraversal t, Node n, Node parent) {
} }
break; break;


case FUNCTION: case BLOCK:
// FYI: the function will be analyzed w/out a call to // The traversal starts at the function block so `parent` will be null, so we use
// NodeTraversal/pushScope. Bypassing pushScope could cause a bug if // getParent here.
// there is code that relies on NodeTraversal knowing the correct scope. if (n.getParent() != null && n.getParent().isFunction()) {
tryMinimizeExits(n.getLastChild(), Token.RETURN, null); tryMinimizeExits(n, Token.RETURN, null);
}
break; break;


case SWITCH: case SWITCH:
Expand Down Expand Up @@ -165,16 +171,15 @@ void tryMinimizeExits(Node n, Token exitType, @Nullable String labelName) {
} }


// The rest assumes a block with at least one child, bail on anything else. // The rest assumes a block with at least one child, bail on anything else.
if (!n.isBlock() || n.getLastChild() == null) { if (!n.isBlock() || !n.hasChildren()) {
return; return;
} }


// Multiple if-exits can be converted in a single pass. // Multiple if-exits can be converted in a single pass.
// Convert "if (blah) break; if (blah2) break; other_stmt;" to // Convert "if (blah) break; if (blah2) break; other_stmt;" to
// become "if (blah); else { if (blah2); else { other_stmt; } }" // become "if (blah); else { if (blah2); else { other_stmt; } }"
// which will get converted to "if (!blah && !blah2) { other_stmt; }". // which will get converted to "if (!blah && !blah2) { other_stmt; }".
for (Node c : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {

// An 'if' block to process below. // An 'if' block to process below.
if (c.isIf()) { if (c.isIf()) {
Node ifTree = c; Node ifTree = c;
Expand Down
Expand Up @@ -43,15 +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);
NodeTraversal.traverseEs6(compiler, js, new MinimizeExitPoints(compiler)); new MinimizeExitPoints(compiler).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).process(externs, js);

new Denormalize(compiler).process(externs, js); new Denormalize(compiler).process(externs, js);
} }
}; };
Expand Down
16 changes: 7 additions & 9 deletions test/com/google/javascript/jscomp/MinimizeExitPointsTest.java
Expand Up @@ -16,20 +16,13 @@


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


import com.google.javascript.rhino.Node;

/** /**
* @author johnlenz@google.com (John Lenz) * @author johnlenz@google.com (John Lenz)
*/ */
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 CompilerPass() { return new MinimizeExitPoints(compiler);
@Override
public void process(Node externs, Node js) {
NodeTraversal.traverseEs6(compiler, js, new MinimizeExitPoints(compiler));
}
};
} }


@Override @Override
Expand Down Expand Up @@ -78,7 +71,12 @@ public void testBreakOptimization() throws Exception {
"f:g:{if(a()){}else{}}"); "f:g:{if(a()){}else{}}");
} }


public void testFunctionReturnOptimization() throws Exception { public void testFunctionReturnOptimization1() throws Exception {
fold("function f(){return}",
"function f(){}");
}

public void testFunctionReturnOptimization2() throws Exception {
fold("function f(){if(a()){b();if(c())return;}}", fold("function f(){if(a()){b();if(c())return;}}",
"function f(){if(a()){b();if(c());}}"); "function f(){if(a()){b();if(c());}}");
fold("function f(){if(x)return; x=3; return; }", fold("function f(){if(x)return; x=3; return; }",
Expand Down

0 comments on commit 3d64f6a

Please sign in to comment.