Fix wrong evaluation when optimizer sees array initialization that looks like array initializer expression, but has access to array variable on its elements, i.e. on a case like this:

Object[] array = new Object[1];
array[0] = array;

That can't be optimized to

Object[] array = new Object[] { array };
This commit is contained in:
Alexey Andreev 2020-06-16 19:24:45 +03:00
parent cf658cf57d
commit 9cf21825fc
3 changed files with 145 additions and 25 deletions

View File

@ -24,18 +24,30 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
protected void afterVisit(Expr expr) { protected void afterVisit(Expr expr) {
} }
protected boolean canceled;
protected final void cancel() {
canceled = true;
}
@Override @Override
public void visit(BinaryExpr expr) { public void visit(BinaryExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getFirstOperand().acceptVisitor(this); if (!canceled) {
expr.getSecondOperand().acceptVisitor(this); expr.getFirstOperand().acceptVisitor(this);
if (!canceled) {
expr.getSecondOperand().acceptVisitor(this);
}
}
afterVisit(expr); afterVisit(expr);
} }
@Override @Override
public void visit(UnaryExpr expr) { public void visit(UnaryExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getOperand().acceptVisitor(this); if (!canceled) {
expr.getOperand().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@ -43,6 +55,9 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
public void visit(AssignmentStatement statement) { public void visit(AssignmentStatement statement) {
if (statement.getLeftValue() != null) { if (statement.getLeftValue() != null) {
statement.getLeftValue().acceptVisitor(this); statement.getLeftValue().acceptVisitor(this);
if (canceled) {
return;
}
} }
statement.getRightValue().acceptVisitor(this); statement.getRightValue().acceptVisitor(this);
} }
@ -50,15 +65,24 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(ConditionalExpr expr) { public void visit(ConditionalExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getCondition().acceptVisitor(this); if (!canceled) {
expr.getConsequent().acceptVisitor(this); expr.getCondition().acceptVisitor(this);
expr.getAlternative().acceptVisitor(this); if (!canceled) {
expr.getConsequent().acceptVisitor(this);
if (!canceled) {
expr.getAlternative().acceptVisitor(this);
}
}
}
afterVisit(expr); afterVisit(expr);
} }
public void visit(List<Statement> statements) { public void visit(List<Statement> statements) {
for (Statement part : statements) { for (Statement part : statements) {
part.acceptVisitor(this); part.acceptVisitor(this);
if (canceled) {
break;
}
} }
} }
@ -77,7 +101,9 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
public void visit(ConditionalStatement statement) { public void visit(ConditionalStatement statement) {
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
visit(statement.getConsequent()); visit(statement.getConsequent());
visit(statement.getAlternative()); if (!canceled) {
visit(statement.getAlternative());
}
} }
@Override @Override
@ -90,23 +116,35 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
public void visit(SubscriptExpr expr) { public void visit(SubscriptExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getArray().acceptVisitor(this); expr.getArray().acceptVisitor(this);
expr.getIndex().acceptVisitor(this); if (!canceled) {
expr.getIndex().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@Override @Override
public void visit(SwitchStatement statement) { public void visit(SwitchStatement statement) {
statement.getValue().acceptVisitor(this); statement.getValue().acceptVisitor(this);
if (canceled) {
return;
}
for (SwitchClause clause : statement.getClauses()) { for (SwitchClause clause : statement.getClauses()) {
visit(clause.getBody()); visit(clause.getBody());
if (canceled) {
return;
}
}
if (!canceled) {
visit(statement.getDefaultClause());
} }
visit(statement.getDefaultClause());
} }
@Override @Override
public void visit(UnwrapArrayExpr expr) { public void visit(UnwrapArrayExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getArray().acceptVisitor(this); if (!canceled) {
expr.getArray().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@ -114,6 +152,9 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
public void visit(WhileStatement statement) { public void visit(WhileStatement statement) {
if (statement.getCondition() != null) { if (statement.getCondition() != null) {
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
if (canceled) {
return;
}
} }
visit(statement.getBody()); visit(statement.getBody());
} }
@ -121,8 +162,13 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(InvocationExpr expr) { public void visit(InvocationExpr expr) {
beforeVisit(expr); beforeVisit(expr);
for (Expr argument : expr.getArguments()) { if (!canceled) {
argument.acceptVisitor(this); for (Expr argument : expr.getArguments()) {
argument.acceptVisitor(this);
if (canceled) {
break;
}
}
} }
afterVisit(expr); afterVisit(expr);
} }
@ -135,8 +181,10 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(QualificationExpr expr) { public void visit(QualificationExpr expr) {
beforeVisit(expr); beforeVisit(expr);
if (expr.getQualified() != null) { if (!canceled) {
expr.getQualified().acceptVisitor(this); if (expr.getQualified() != null) {
expr.getQualified().acceptVisitor(this);
}
} }
afterVisit(expr); afterVisit(expr);
} }
@ -158,15 +206,22 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(NewArrayExpr expr) { public void visit(NewArrayExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getLength().acceptVisitor(this); if (!canceled) {
expr.getLength().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@Override @Override
public void visit(NewMultiArrayExpr expr) { public void visit(NewMultiArrayExpr expr) {
beforeVisit(expr); beforeVisit(expr);
for (Expr dimension : expr.getDimensions()) { if (!canceled) {
dimension.acceptVisitor(this); for (Expr dimension : expr.getDimensions()) {
dimension.acceptVisitor(this);
if (canceled) {
break;
}
}
} }
afterVisit(expr); afterVisit(expr);
} }
@ -174,8 +229,13 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(ArrayFromDataExpr expr) { public void visit(ArrayFromDataExpr expr) {
beforeVisit(expr); beforeVisit(expr);
for (Expr element : expr.getData()) { if (!canceled) {
element.acceptVisitor(this); for (Expr element : expr.getData()) {
element.acceptVisitor(this);
if (canceled) {
break;
}
}
} }
afterVisit(expr); afterVisit(expr);
} }
@ -190,7 +250,9 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(InstanceOfExpr expr) { public void visit(InstanceOfExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getExpr().acceptVisitor(this); if (!canceled) {
expr.getExpr().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@ -202,7 +264,9 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(CastExpr expr) { public void visit(CastExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getValue().acceptVisitor(this); if (!canceled) {
expr.getValue().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@ -213,7 +277,9 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(PrimitiveCastExpr expr) { public void visit(PrimitiveCastExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getValue().acceptVisitor(this); if (!canceled) {
expr.getValue().acceptVisitor(this);
}
afterVisit(expr); afterVisit(expr);
} }
@ -240,9 +306,11 @@ public class RecursiveVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(BoundCheckExpr expr) { public void visit(BoundCheckExpr expr) {
beforeVisit(expr); beforeVisit(expr);
expr.getIndex().acceptVisitor(this); if (!canceled) {
if (expr.getArray() != null) { expr.getIndex().acceptVisitor(this);
expr.getArray().acceptVisitor(this); if (!canceled && expr.getArray() != null) {
expr.getArray().acceptVisitor(this);
}
} }
afterVisit(expr); afterVisit(expr);
} }

View File

@ -762,6 +762,13 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
return false; return false;
} }
VariableAccessFinder isVariableAccessed = new VariableAccessFinder(v -> v == optimization.arrayVariable
|| v == optimization.unwrappedArrayVariable);
assign.getRightValue().acceptVisitor(isVariableAccessed);
if (isVariableAccessed.isFound()) {
return false;
}
optimization.elements.add(assign.getRightValue()); optimization.elements.add(assign.getRightValue());
if (++optimization.arrayElementIndex == optimization.arraySize) { if (++optimization.arrayElementIndex == optimization.arraySize) {
applyArrayOptimization(optimization); applyArrayOptimization(optimization);

View File

@ -0,0 +1,45 @@
/*
* Copyright 2020 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.ast.optimization;
import java.util.function.IntPredicate;
import org.teavm.ast.RecursiveVisitor;
import org.teavm.ast.VariableExpr;
public class VariableAccessFinder extends RecursiveVisitor {
private IntPredicate predicate;
private boolean found;
public VariableAccessFinder(IntPredicate predicate) {
this.predicate = predicate;
}
public boolean isFound() {
return found;
}
public void reset() {
found = false;
}
@Override
public void visit(VariableExpr expr) {
if (predicate.test(expr.getIndex())) {
found = true;
cancel();
}
}
}