Optimize some breaks (including breaks from switch clauses) in a

separate pass
This commit is contained in:
konsoletyper 2015-03-15 14:43:38 +03:00
parent 8be9e6c4ba
commit b6cf7c4cba
4 changed files with 162 additions and 43 deletions

View File

@ -0,0 +1,156 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
import java.util.*;
import org.teavm.javascript.ast.*;
/**
*
* @author Alexey Andreev
*/
class BreakEliminator implements StatementVisitor {
private Map<BlockStatement, List<Statement>> blockSuccessors = new HashMap<>();
private Set<IdentifiedStatement> outerStatements = new HashSet<>();
private List<Statement> currentSequence;
private boolean sequenceEscapes;
private int currentIndex;
private void processSequence(List<Statement> statements) {
List<Statement> oldSequence = currentSequence;
int oldIndex = currentIndex;
boolean oldEscapes = sequenceEscapes;
sequenceEscapes = escapes(statements);
currentSequence = statements;
for (currentIndex = 0; currentIndex < currentSequence.size(); ++currentIndex) {
statements.get(currentIndex).acceptVisitor(this);
}
sequenceEscapes = oldEscapes;
currentIndex = oldIndex;
currentSequence = oldSequence;
}
@Override
public void visit(AssignmentStatement statement) {
}
@Override
public void visit(SequentialStatement statement) {
if (currentSequence == null) {
processSequence(statement.getSequence());
return;
}
--currentIndex;
currentSequence.remove(currentIndex);
currentSequence.addAll(currentIndex, statement.getSequence());
}
@Override
public void visit(ConditionalStatement statement) {
processSequence(statement.getConsequent());
processSequence(statement.getAlternative());
}
@Override
public void visit(SwitchStatement statement) {
outerStatements.add(statement);
for (SwitchClause clause : statement.getClauses()) {
processSequence(clause.getBody());
}
processSequence(statement.getDefaultClause());
outerStatements.remove(statement);
}
@Override
public void visit(WhileStatement statement) {
outerStatements.add(statement);
processSequence(statement.getBody());
outerStatements.remove(statement);
}
@Override
public void visit(BlockStatement statement) {
outerStatements.add(statement);
if (!sequenceEscapes && !escapes(statement.getBody())) {
blockSuccessors.put(statement, currentSequence.subList(currentIndex + 1, currentSequence.size()));
}
processSequence(statement.getBody());
blockSuccessors.remove(statement);
outerStatements.remove(statement);
}
@Override
public void visit(BreakStatement statement) {
if (blockSuccessors.containsKey(statement.getTarget())) {
BlockCountVisitor usageCounter = new BlockCountVisitor(
(BlockStatement)statement.getTarget());
statement.getTarget().acceptVisitor(usageCounter);
if (usageCounter.getCount() == 1) {
currentSequence.subList(currentIndex, currentSequence.size()).clear();
List<Statement> successors = blockSuccessors.remove(statement.getTarget());
currentSequence.addAll(successors);
successors.clear();
--currentIndex;
sequenceEscapes = escapes(currentSequence);
return;
}
}
currentSequence.subList(currentIndex + 1, currentSequence.size()).clear();
}
@Override
public void visit(ContinueStatement statement) {
currentSequence.subList(currentIndex + 1, currentSequence.size()).clear();
}
@Override
public void visit(ReturnStatement statement) {
currentSequence.subList(currentIndex + 1, currentSequence.size()).clear();
}
@Override
public void visit(ThrowStatement statement) {
currentSequence.subList(currentIndex + 1, currentSequence.size()).clear();
}
@Override
public void visit(InitClassStatement statement) {
}
@Override
public void visit(TryCatchStatement statement) {
processSequence(statement.getProtectedBody());
processSequence(statement.getHandler());
}
@Override
public void visit(GotoPartStatement statement) {
}
@Override
public void visit(MonitorEnterStatement statement) {
}
@Override
public void visit(MonitorExitStatement statement) {
}
private boolean escapes(List<Statement> statements) {
return new EscapingStatementFinder(outerStatements).check(statements);
}
}

View File

@ -73,9 +73,7 @@ class EscapingStatementFinder implements StatementVisitor{
@Override @Override
public void visit(WhileStatement statement) { public void visit(WhileStatement statement) {
outerStatements.add(statement); escaping = true;
check(statement.getBody());
outerStatements.remove(statement);
} }
@Override @Override

View File

@ -35,6 +35,8 @@ public class Optimizer {
preservedVars[i] = true; preservedVars[i] = true;
} }
} }
BreakEliminator breakEliminator = new BreakEliminator();
method.getBody().acceptVisitor(breakEliminator);
OptimizingVisitor optimizer = new OptimizingVisitor(preservedVars, stats.reads); OptimizingVisitor optimizer = new OptimizingVisitor(preservedVars, stats.reads);
method.getBody().acceptVisitor(optimizer); method.getBody().acceptVisitor(optimizer);
method.setBody(optimizer.resultStmt); method.setBody(optimizer.resultStmt);
@ -64,6 +66,8 @@ public class Optimizer {
} }
for (int i = 0; i < splitter.size(); ++i) { for (int i = 0; i < splitter.size(); ++i) {
AsyncMethodPart part = method.getBody().get(i); AsyncMethodPart part = method.getBody().get(i);
BreakEliminator breakEliminator = new BreakEliminator();
part.getStatement().acceptVisitor(breakEliminator);
OptimizingVisitor optimizer = new OptimizingVisitor(preservedVars, readFrequencies[i]); OptimizingVisitor optimizer = new OptimizingVisitor(preservedVars, readFrequencies[i]);
part.getStatement().acceptVisitor(optimizer); part.getStatement().acceptVisitor(optimizer);
part.setStatement(optimizer.resultStmt); part.setStatement(optimizer.resultStmt);

View File

@ -28,8 +28,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
private boolean[] preservedVars; private boolean[] preservedVars;
private int[] readFrequencies; private int[] readFrequencies;
private List<Statement> resultSequence; private List<Statement> resultSequence;
private Map<BlockStatement, List<Statement>> blockSuccessors = new HashMap<>();
private Set<IdentifiedStatement> outerStatements = new HashSet<>();
public OptimizingVisitor(boolean[] preservedVars, int[] readFreqencies) { public OptimizingVisitor(boolean[] preservedVars, int[] readFreqencies) {
this.preservedVars = preservedVars; this.preservedVars = preservedVars;
@ -379,22 +377,11 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
if (exit == target) { if (exit == target) {
statements.remove(statements.size() - 1); statements.remove(statements.size() - 1);
} }
} else if (blockSuccessors.containsKey(breakStmt.getTarget())) {
BlockCountVisitor usageCounter = new BlockCountVisitor(
(BlockStatement)breakStmt.getTarget());
breakStmt.getTarget().acceptVisitor(usageCounter);
if (usageCounter.getCount() == 1) {
statements.remove(statements.size() - 1);
List<Statement> successors = blockSuccessors.remove(breakStmt.getTarget());
statements.addAll(successors);
successors.clear();
}
} }
} }
if (statements.isEmpty()) { if (statements.isEmpty()) {
return; return;
} }
boolean escapes = escapes(statements);
for (int i = 0; i < statements.size(); ++i) { for (int i = 0; i < statements.size(); ++i) {
Statement stmt = statements.get(i); Statement stmt = statements.get(i);
if (stmt instanceof ConditionalStatement) { if (stmt instanceof ConditionalStatement) {
@ -417,7 +404,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
if (last instanceof BreakStatement) { if (last instanceof BreakStatement) {
BreakStatement breakStmt = (BreakStatement)last; BreakStatement breakStmt = (BreakStatement)last;
if (exit != null && exit == breakStmt.getTarget()) { if (exit != null && exit == breakStmt.getTarget()) {
cond.getAlternative().remove(cond.getConsequent().size() - 1); cond.getAlternative().remove(cond.getAlternative().size() - 1);
List<Statement> remaining = statements.subList(i + 1, statements.size()); List<Statement> remaining = statements.subList(i + 1, statements.size());
cond.getConsequent().addAll(remaining); cond.getConsequent().addAll(remaining);
remaining.clear(); remaining.clear();
@ -452,40 +439,20 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
} }
} else if (stmt instanceof BlockStatement) { } else if (stmt instanceof BlockStatement) {
BlockStatement nestedBlock = (BlockStatement)stmt; BlockStatement nestedBlock = (BlockStatement)stmt;
outerStatements.add(nestedBlock);
if (!escapes && !escapes(nestedBlock.getBody())) {
blockSuccessors.put(nestedBlock, statements.subList(i + 1, statements.size()));
}
eliminateRedundantBreaks(nestedBlock.getBody(), nestedBlock); eliminateRedundantBreaks(nestedBlock.getBody(), nestedBlock);
blockSuccessors.remove(nestedBlock);
outerStatements.remove(nestedBlock);
} else if (stmt instanceof WhileStatement) { } else if (stmt instanceof WhileStatement) {
WhileStatement whileStmt = (WhileStatement)stmt; WhileStatement whileStmt = (WhileStatement)stmt;
outerStatements.add(whileStmt);
eliminateRedundantBreaks(whileStmt.getBody(), null); eliminateRedundantBreaks(whileStmt.getBody(), null);
outerStatements.remove(whileStmt);
} else if (stmt instanceof SwitchStatement) { } else if (stmt instanceof SwitchStatement) {
SwitchStatement switchStmt = (SwitchStatement)stmt; SwitchStatement switchStmt = (SwitchStatement)stmt;
outerStatements.add(switchStmt);
if (i == statements.size() - 1) {
for (SwitchClause clause : switchStmt.getClauses()) {
eliminateRedundantBreaks(clause.getBody(), exit);
}
eliminateRedundantBreaks(switchStmt.getDefaultClause(), exit);
}
for (SwitchClause clause : switchStmt.getClauses()) { for (SwitchClause clause : switchStmt.getClauses()) {
eliminateRedundantBreaks(clause.getBody(), null); eliminateRedundantBreaks(clause.getBody(), null);
} }
eliminateRedundantBreaks(switchStmt.getDefaultClause(), null); eliminateRedundantBreaks(switchStmt.getDefaultClause(), null);
outerStatements.remove(switchStmt);
} }
} }
} }
private boolean escapes(List<Statement> statements) {
return new EscapingStatementFinder(outerStatements).check(statements);
}
private void normalizeConditional(ConditionalStatement stmt) { private void normalizeConditional(ConditionalStatement stmt) {
if (stmt.getConsequent().isEmpty()) { if (stmt.getConsequent().isEmpty()) {
stmt.getConsequent().addAll(stmt.getAlternative()); stmt.getConsequent().addAll(stmt.getAlternative());
@ -530,7 +497,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
@Override @Override
public void visit(SwitchStatement statement) { public void visit(SwitchStatement statement) {
outerStatements.add(statement);
statement.getValue().acceptVisitor(this); statement.getValue().acceptVisitor(this);
statement.setValue(resultExpr); statement.setValue(resultExpr);
for (SwitchClause clause : statement.getClauses()) { for (SwitchClause clause : statement.getClauses()) {
@ -542,12 +508,10 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
statement.getDefaultClause().clear(); statement.getDefaultClause().clear();
statement.getDefaultClause().addAll(newDefault); statement.getDefaultClause().addAll(newDefault);
resultStmt = statement; resultStmt = statement;
outerStatements.remove(statement);
} }
@Override @Override
public void visit(WhileStatement statement) { public void visit(WhileStatement statement) {
outerStatements.add(statement);
if (statement.getBody().size() == 1 && statement.getBody().get(0) instanceof WhileStatement) { if (statement.getBody().size() == 1 && statement.getBody().get(0) instanceof WhileStatement) {
WhileStatement innerLoop = (WhileStatement)statement.getBody().get(0); WhileStatement innerLoop = (WhileStatement)statement.getBody().get(0);
BreakToContinueReplacer replacer = new BreakToContinueReplacer(innerLoop, statement); BreakToContinueReplacer replacer = new BreakToContinueReplacer(innerLoop, statement);
@ -596,12 +560,10 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
break; break;
} }
resultStmt = statement; resultStmt = statement;
outerStatements.remove(statements);
} }
@Override @Override
public void visit(BlockStatement statement) { public void visit(BlockStatement statement) {
outerStatements.add(statement);
List<Statement> statements = processSequence(statement.getBody()); List<Statement> statements = processSequence(statement.getBody());
eliminateRedundantBreaks(statements, statement); eliminateRedundantBreaks(statements, statement);
statements = processSequence(statements); statements = processSequence(statements);
@ -616,7 +578,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
statement.getBody().addAll(statements); statement.getBody().addAll(statements);
resultStmt = statement; resultStmt = statement;
} }
outerStatements.remove(statement);
} }
@Override @Override