Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions dev/core/src/com/google/gwt/dev/jjs/impl/Simplifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,13 @@ public static JExpression simplifyAnd(JBinaryOperation expression) {
if (rhs instanceof JBooleanLiteral) {
if (((JBooleanLiteral) rhs).getValue()) {
return lhs;
} else if (!lhs.hasSideEffects()) {
// Do not remove lhs if it had side effects
} else if (lhs.hasSideEffects()) {
// if side effect, rewrite a() && false to (a(), false)
// so that parent node can be simplified further
// e.g. (a() && false) && b() -> (a(), false) && b()
// -> (a(), false && b()) -> (a(), false)
return new JMultiExpression(info, lhs, rhs);
} else {
return rhs;
}
}
Expand Down Expand Up @@ -482,7 +487,9 @@ public static JExpression simplifyOr(JBinaryOperation expression) {
if (rhs instanceof JBooleanLiteral) {
if (!((JBooleanLiteral) rhs).getValue()) {
return lhs;
} else if (!lhs.hasSideEffects()) {
} else if (lhs.hasSideEffects()) {
return new JMultiExpression(info, lhs, rhs);
} else {
return rhs;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ public void testCommuteMultiExpression() throws Exception {
+ "static int f1;"
+ "static A createA() { A.f1 = 1; return new A(); } "
+ "static boolean booleanWithSideEffects() { createA(); return true;}"
+ "@javaemul.internal.annotations.DoNotInline "
+ "static boolean notInlinedBool1() { return true;}"
+ "@javaemul.internal.annotations.DoNotInline "
+ "static boolean notInlinedBool2() { return true;}"
+ "static boolean booleanWithoutSideEffects() { return true;}"
+ "static int arithmeticWithSideEffects() { createA(); return 4;}"
+ "}");
Expand All @@ -311,6 +315,28 @@ public void testCommuteMultiExpression() throws Exception {
optimizeExpressions(false, "boolean", "false && A.booleanWithSideEffects()")
.intoString("return false;");

optimizeExpressions(false, "boolean", "A.notInlinedBool1() && false")
.intoString("return (EntryPoint$A.notInlinedBool1(), false);");

optimizeExpressions(false, "int",
"A.notInlinedBool1() && (false && A.notInlinedBool2()) ? 1 : 2")
.intoString("return (EntryPoint$A.notInlinedBool1(), 2);");

optimizeExpressions(false, "int",
"(A.notInlinedBool1() && false) && A.notInlinedBool2() ? 1 : 2")
.intoString("return (EntryPoint$A.notInlinedBool1(), 2);");

optimizeExpressions(false, "boolean", "A.notInlinedBool1() || true")
.intoString("return (EntryPoint$A.notInlinedBool1(), true);");

optimizeExpressions(false, "int",
"A.notInlinedBool1() || (true || A.notInlinedBool2()) ? 1 : 2")
.intoString("return (EntryPoint$A.notInlinedBool1(), 1);");

optimizeExpressions(false, "int",
"(A.notInlinedBool1() || true) || A.notInlinedBool2() ? 1 : 2")
.intoString("return (EntryPoint$A.notInlinedBool1(), 1);");

optimizeExpressions(false, "int", "3 + A.arithmeticWithSideEffects()")
.intoString("return (EntryPoint$A.f1 = 1, new EntryPoint$A(), 7);");
}
Expand Down