mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Some code size optimizations
1. Eliminate empty switch statements 2. Eliminate unnecessary == 0 and != 0 conditions in 'if'
This commit is contained in:
parent
168bf1fa9c
commit
6b063bc567
|
@ -246,7 +246,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
pushLocation(expr.getLocation());
|
pushLocation(expr.getLocation());
|
||||||
try {
|
try {
|
||||||
expr.getCondition().acceptVisitor(this);
|
expr.getCondition().acceptVisitor(this);
|
||||||
Expr cond = resultExpr;
|
Expr cond = optimizeCondition(resultExpr);
|
||||||
|
|
||||||
Statement barrier = addBarrier();
|
Statement barrier = addBarrier();
|
||||||
expr.getConsequent().acceptVisitor(this);
|
expr.getConsequent().acceptVisitor(this);
|
||||||
|
@ -786,7 +786,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
@Override
|
@Override
|
||||||
public void visit(ConditionalStatement statement) {
|
public void visit(ConditionalStatement statement) {
|
||||||
statement.getCondition().acceptVisitor(this);
|
statement.getCondition().acceptVisitor(this);
|
||||||
statement.setCondition(resultExpr);
|
statement.setCondition(optimizeCondition(resultExpr));
|
||||||
List<Statement> consequent = processSequence(statement.getConsequent());
|
List<Statement> consequent = processSequence(statement.getConsequent());
|
||||||
List<Statement> alternative = processSequence(statement.getAlternative());
|
List<Statement> alternative = processSequence(statement.getAlternative());
|
||||||
if (consequent.isEmpty()) {
|
if (consequent.isEmpty()) {
|
||||||
|
@ -871,8 +871,15 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
List<Statement> newDefault = processSequence(statement.getDefaultClause());
|
List<Statement> newDefault = processSequence(statement.getDefaultClause());
|
||||||
statement.getDefaultClause().clear();
|
statement.getDefaultClause().clear();
|
||||||
statement.getDefaultClause().addAll(newDefault);
|
statement.getDefaultClause().addAll(newDefault);
|
||||||
|
|
||||||
|
if (statement.getClauses().isEmpty()) {
|
||||||
|
SequentialStatement seq = new SequentialStatement();
|
||||||
|
seq.getSequence().addAll(statement.getDefaultClause());
|
||||||
|
resultStmt = seq;
|
||||||
|
} else {
|
||||||
resultStmt = statement;
|
resultStmt = statement;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(WhileStatement statement) {
|
public void visit(WhileStatement statement) {
|
||||||
|
@ -1074,4 +1081,19 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Expr optimizeCondition(Expr expr) {
|
||||||
|
if (expr instanceof BinaryExpr) {
|
||||||
|
BinaryExpr binary = (BinaryExpr) expr;
|
||||||
|
if (isZero(((BinaryExpr) expr).getSecondOperand())) {
|
||||||
|
switch (binary.getOperation()) {
|
||||||
|
case EQUALS:
|
||||||
|
return ExprOptimizer.invert(binary.getFirstOperand());
|
||||||
|
case NOT_EQUALS:
|
||||||
|
return binary.getFirstOperand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1354,6 +1354,9 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
||||||
precedence = Precedence.CONDITIONAL.next();
|
precedence = Precedence.CONDITIONAL.next();
|
||||||
expr.getExpr().acceptVisitor(this);
|
expr.getExpr().acceptVisitor(this);
|
||||||
writer.append(" instanceof ").appendClass(clsName);
|
writer.append(" instanceof ").appendClass(clsName);
|
||||||
|
if (needsParentheses) {
|
||||||
|
writer.append(')');
|
||||||
|
}
|
||||||
if (expr.getLocation() != null) {
|
if (expr.getLocation() != null) {
|
||||||
popLocation();
|
popLocation();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user