Skip to content

Commit fcf9088

Browse files
authored
DeadCodeEliminationTest cleanup, optimize String.hashCode (#10183)
This test had a few tests unnecessarily disabled checks as well as some typos or errors in comments. Added a simple test for optimizing out for loops, as this was missing. Enabled String.hashCode() constant optimization, our emul has been consistent with JRE semantics since d094087. Also fixes #10067 by specifying locale when printing floats.
1 parent 43562c4 commit fcf9088

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

dev/core/src/com/google/gwt/dev/jjs/impl/DeadCodeElimination.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,13 +1847,6 @@ private void tryOptimizeStringCall(JMethodCall x, Context ctx, JMethod method) {
18471847
return;
18481848
}
18491849

1850-
if (method.getName().endsWith("hashCode")) {
1851-
// This cannot be computed at compile time because our implementation
1852-
// differs from the JRE.
1853-
// TODO: actually, I think it DOES match now, so we can remove this?
1854-
return;
1855-
}
1856-
18571850
boolean isStaticImplMethod = program.isStaticImpl(method);
18581851
JExpression instance = isStaticImplMethod ? x.getArgs().get(0) : x.getInstance();
18591852
// drop the instance argument if the call was devirtualized as the compile time evaluation

dev/core/test/com/google/gwt/dev/jjs/impl/DeadCodeEliminationTest.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.gwt.dev.jjs.ast.JMethod;
2020
import com.google.gwt.dev.jjs.ast.JProgram;
2121

22+
import java.util.Locale;
23+
2224
/**
2325
* Tests {@link DeadCodeElimination}.
2426
*/
@@ -255,8 +257,22 @@ public void testIfStatementToBoolean_ThenOptimization() throws Exception {
255257
"EntryPoint.b && (EntryPoint.i = 1);");
256258
}
257259

260+
public void testForOptimizations() {
261+
// We need a helper to inline the false from, so JDT doesn't alert us to unreachable code and fail
262+
runMethodInliner = true;
263+
addSnippetClassDecl(
264+
"static class B { "
265+
+ "static boolean isFalse() { return false; } "
266+
+ "}");
267+
268+
optimize("void", "for (i = 1; B.isFalse(); i++) { i = 2; }")
269+
.intoString("EntryPoint.i = 1;");
270+
optimize("void", "for (i = 1; i < 5; i++) { i = 2; }")
271+
.intoString("for (EntryPoint.i = 1; EntryPoint.i < 5; EntryPoint.i++) {\n EntryPoint.i = 2;\n}");
272+
}
273+
258274
/**
259-
* BUG: JInstance was marked as not having side effects whereas it all depends on the
275+
* BUG: JInstanceOf was marked as not having side effects whereas it all depends on
260276
* whether the expression on the left has side effects.
261277
*
262278
* Reproduces Issue:7818.
@@ -309,7 +325,7 @@ public void testStringOptimizations() throws Exception {
309325
" final static String s4 = null;",
310326
"}");
311327

312-
// TODO(rluble): This test is not 100% meaninful as the JDT performs some optimizations for us.
328+
// TODO(rluble): This test is not 100% meaningful as the JDT performs some optimizations for us.
313329
optimizeExpressions(false, "boolean", "\"a\".equals(\"a\")")
314330
.into("return true;");
315331
optimizeExpressions(false, "boolean", "\"a\" == \"a\"")
@@ -334,11 +350,10 @@ public void testStringOptimizations() throws Exception {
334350
.into("return true;");
335351
optimizeExpressions(false, "boolean", "A.s1.equals(A.s2)")
336352
.into("return true;");
337-
// Next two are not directly optimizable because of inserted clinits.
338-
// optimizeExpressions(false, "boolean", "\"a\" != A.s4")
339-
// .into("return true;");
340-
// optimizeExpressions(false, "boolean", "A.s4 == null")
341-
// .into("return true;");
353+
optimizeExpressions(false, "boolean", "\"a\" != A.s4")
354+
.intoString("return (EntryPoint$A.$clinit(), true);");
355+
optimizeExpressions(false, "boolean", "A.s4 == null")
356+
.intoString("return (EntryPoint$A.$clinit(), true);");
342357
}
343358

344359
public void testStringOptimizations_withSpecializer() throws Exception {
@@ -419,7 +434,7 @@ public void testMultiExpressionOptimization() throws Exception {
419434

420435
public void testOptimizeStringCalls() throws Exception {
421436
// Note: we're limited here by the methods declared in the mock String in
422-
// JJSTestBase#addBuiltinClasses
437+
// JavaResourceBase#getStandardResources().
423438

424439
// String.length
425440
optimize("int", "return \"abc\".length();").intoString("return 3;");
@@ -434,8 +449,8 @@ public void testOptimizeStringCalls() throws Exception {
434449
optimize("String", "return s.toString();").intoString("return EntryPoint.s.toString();");
435450
optimize("String", "return o.toString();").intoString("return EntryPoint.o.toString();");
436451

437-
// String.hashCode: never optimized
438-
optimize("int", "return \"abc\".hashCode();").intoString("return \"abc\".hashCode();");
452+
// String.hashCode
453+
optimize("int", "return \"abc\".hashCode();").intoString("return 96354;");
439454
optimize("int", "return s.hashCode();").intoString("return EntryPoint.s.hashCode();");
440455

441456
// String.equals
@@ -464,9 +479,9 @@ public void testSubtractFromZero() throws Exception {
464479

465480
public void testFloatingPoint() throws Exception {
466481
// Internally we represent float literals as double, so here we make sure that 1.1f is
467-
// is printed as a double with the right precision.
468-
optimize("float", "return 1.1f;").intoString("return " + String.format("%.16g", (double) 1.1f) +
469-
";");
482+
// printed as a double with the right precision.
483+
optimize("float", "return 1.1f;").intoString("return "
484+
+ String.format(Locale.ROOT, "%.16g", (double) 1.1f) + ";");
470485
optimize("boolean", "return 2d > 1;").intoString("return true;");
471486
optimize("boolean", "return 1 < 2d;").intoString("return true;");
472487
}

0 commit comments

Comments
 (0)