mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-23 00:24:11 -08:00
First attempt to make AST optimizer faster
This commit is contained in:
parent
f17433aeab
commit
8be9e6c4ba
|
@ -22,11 +22,11 @@ import org.teavm.javascript.ast.*;
|
||||||
*
|
*
|
||||||
* @author Alexey Andreev
|
* @author Alexey Andreev
|
||||||
*/
|
*/
|
||||||
class CertainBlockCountVisitor implements StatementVisitor {
|
class BlockCountVisitor implements StatementVisitor {
|
||||||
private BlockStatement blockToCount;
|
private BlockStatement blockToCount;
|
||||||
private int count;
|
private int count;
|
||||||
|
|
||||||
public CertainBlockCountVisitor(BlockStatement blockToCount) {
|
public BlockCountVisitor(BlockStatement blockToCount) {
|
||||||
this.blockToCount = blockToCount;
|
this.blockToCount = blockToCount;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
/*
|
||||||
|
* 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.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.teavm.javascript.ast.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
class EscapingStatementFinder implements StatementVisitor{
|
||||||
|
public boolean escaping;
|
||||||
|
private Set<IdentifiedStatement> outerStatements;
|
||||||
|
|
||||||
|
public EscapingStatementFinder(Set<IdentifiedStatement> nestingStatements) {
|
||||||
|
this.outerStatements = nestingStatements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean check(List<Statement> statements) {
|
||||||
|
if (!escaping) {
|
||||||
|
if (statements.isEmpty()) {
|
||||||
|
escaping = true;
|
||||||
|
} else {
|
||||||
|
statements.get(statements.size() - 1).acceptVisitor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return escaping;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(AssignmentStatement statement) {
|
||||||
|
escaping = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(SequentialStatement statement) {
|
||||||
|
check(statement.getSequence());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ConditionalStatement statement) {
|
||||||
|
if (!check(statement.getConsequent())) {
|
||||||
|
check(statement.getAlternative());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(SwitchStatement statement) {
|
||||||
|
outerStatements.add(statement);
|
||||||
|
for (SwitchClause clause : statement.getClauses()) {
|
||||||
|
if (check(clause.getBody())) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check(statement.getDefaultClause());
|
||||||
|
outerStatements.remove(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(WhileStatement statement) {
|
||||||
|
outerStatements.add(statement);
|
||||||
|
check(statement.getBody());
|
||||||
|
outerStatements.remove(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(BlockStatement statement) {
|
||||||
|
outerStatements.add(statement);
|
||||||
|
check(statement.getBody());
|
||||||
|
outerStatements.remove(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(BreakStatement statement) {
|
||||||
|
escaping = !outerStatements.contains(statement.getTarget());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ContinueStatement statement) {
|
||||||
|
escaping = !outerStatements.contains(statement.getTarget());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ReturnStatement statement) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ThrowStatement statement) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(InitClassStatement statement) {
|
||||||
|
escaping = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(TryCatchStatement statement) {
|
||||||
|
check(statement.getProtectedBody());
|
||||||
|
check(statement.getHandler());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(GotoPartStatement statement) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(MonitorEnterStatement statement) {
|
||||||
|
escaping = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(MonitorExitStatement statement) {
|
||||||
|
escaping = true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -28,6 +28,8 @@ 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;
|
||||||
|
@ -370,15 +372,29 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Statement last = statements.get(statements.size() - 1);
|
Statement last = statements.get(statements.size() - 1);
|
||||||
if (last instanceof BreakStatement && exit != null) {
|
if (last instanceof BreakStatement) {
|
||||||
IdentifiedStatement target = ((BreakStatement)last).getTarget();
|
BreakStatement breakStmt = (BreakStatement)last;
|
||||||
|
if (exit != null) {
|
||||||
|
IdentifiedStatement target = breakStmt.getTarget();
|
||||||
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) {
|
||||||
|
@ -436,20 +452,39 @@ 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()) {
|
||||||
|
@ -495,6 +530,7 @@ 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()) {
|
||||||
|
@ -506,10 +542,12 @@ 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);
|
||||||
|
@ -558,13 +596,16 @@ 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);
|
||||||
CertainBlockCountVisitor usageCounter = new CertainBlockCountVisitor(statement);
|
statements = processSequence(statements);
|
||||||
|
BlockCountVisitor usageCounter = new BlockCountVisitor(statement);
|
||||||
usageCounter.visit(statements);
|
usageCounter.visit(statements);
|
||||||
if (usageCounter.getCount() == 0) {
|
if (usageCounter.getCount() == 0) {
|
||||||
SequentialStatement result = new SequentialStatement();
|
SequentialStatement result = new SequentialStatement();
|
||||||
|
@ -575,6 +616,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
statement.getBody().addAll(statements);
|
statement.getBody().addAll(statements);
|
||||||
resultStmt = statement;
|
resultStmt = statement;
|
||||||
}
|
}
|
||||||
|
outerStatements.remove(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue
Block a user