diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/Simplifier.java b/dev/core/src/com/google/gwt/dev/jjs/impl/Simplifier.java index d7dd069acc..e054081b93 100644 --- a/dev/core/src/com/google/gwt/dev/jjs/impl/Simplifier.java +++ b/dev/core/src/com/google/gwt/dev/jjs/impl/Simplifier.java @@ -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; } } @@ -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; } } diff --git a/dev/core/test/com/google/gwt/dev/jjs/impl/DeadCodeEliminationTest.java b/dev/core/test/com/google/gwt/dev/jjs/impl/DeadCodeEliminationTest.java index 5bc051d803..d641d08ef0 100644 --- a/dev/core/test/com/google/gwt/dev/jjs/impl/DeadCodeEliminationTest.java +++ b/dev/core/test/com/google/gwt/dev/jjs/impl/DeadCodeEliminationTest.java @@ -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;}" + "}"); @@ -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);"); }