DeadCodeElimination was not optimizing some expressions fully.

DeadCodeElimination was missing optimizaiton opportunities because
it did not account correctly for the methods it had to be run on.

In particular when a constructor body gets optimized away there are new
optimization opportunities in places where it is invoked.

Now DeadCodeElimination is run not only on modified methods but also
on their callers.

Bug: #9387
Bug-Link: https://github.com/gwtproject/gwt/issues/9387
Change-Id: I1abbee03ce420ae5a29f2bb6fa829c8eb94dff87
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/DeadCodeElimination.java b/dev/core/src/com/google/gwt/dev/jjs/impl/DeadCodeElimination.java
index 68504bb..9ee0239 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/DeadCodeElimination.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/DeadCodeElimination.java
@@ -2032,10 +2032,7 @@
    * context).
    */
   public static OptimizerStats exec(JProgram program, OptimizerContext optimizerCtx) {
-    Set<JMethod> affectedMethods =
-        optimizerCtx.getModifiedMethodsSince(optimizerCtx.getLastStepFor(NAME));
-    affectedMethods.addAll(optimizerCtx.getMethodsByReferencedFields(
-        optimizerCtx.getModifiedFieldsSince(optimizerCtx.getLastStepFor(NAME))));
+    Set<JMethod> affectedMethods = affectedMethods(optimizerCtx);
     OptimizerStats stats = new DeadCodeElimination(program).execImpl(affectedMethods, optimizerCtx);
     optimizerCtx.setLastStepFor(NAME, optimizerCtx.getOptimizationStep());
     optimizerCtx.incOptimizationStep();
@@ -2043,6 +2040,21 @@
     return stats;
   }
 
+  /**
+   * Return the set of methods affected (because they are or callers of) by the modifications to the
+   * given set functions.
+   */
+  private static Set<JMethod> affectedMethods(OptimizerContext optimizerCtx) {
+    Set<JMethod> modifiedMethods =
+        optimizerCtx.getModifiedMethodsSince(optimizerCtx.getLastStepFor(NAME));
+    Set<JMethod> affectedMethods = Sets.newLinkedHashSet();
+    affectedMethods.addAll(modifiedMethods);
+    affectedMethods.addAll(optimizerCtx.getCallers(modifiedMethods));
+    affectedMethods.addAll(optimizerCtx.getMethodsByReferencedFields(
+        optimizerCtx.getModifiedFieldsSince(optimizerCtx.getLastStepFor(NAME))));
+    return affectedMethods;
+  }
+
   private final JProgram program;
 
   private final Map<JType, Class<?>> classObjectsByType;
diff --git a/user/test/com/google/gwt/dev/jjs/OptimizedOnlyCompilerSuite.java b/user/test/com/google/gwt/dev/jjs/OptimizedOnlyCompilerSuite.java
index 0a69664..b95797c 100644
--- a/user/test/com/google/gwt/dev/jjs/OptimizedOnlyCompilerSuite.java
+++ b/user/test/com/google/gwt/dev/jjs/OptimizedOnlyCompilerSuite.java
@@ -20,6 +20,7 @@
 import com.google.gwt.dev.jjs.optimized.CastOptimizationTest;
 import com.google.gwt.dev.jjs.optimized.JsOverlayMethodOptimizationTest;
 import com.google.gwt.dev.jjs.optimized.SpecializationTest;
+import com.google.gwt.dev.jjs.optimized.StringOptimizationTest;
 import com.google.gwt.dev.jjs.test.HasNoSideEffectsTest;
 import com.google.gwt.dev.jjs.test.RunAsyncContentTest;
 import com.google.gwt.junit.tools.GWTTestSuite;
@@ -37,6 +38,7 @@
     // $JUnit-BEGIN$
     suite.addTestSuite(ArrayListOptimizationTest.class);
     suite.addTestSuite(ArrayStoreOptimizationTest.class);
+    suite.addTestSuite(StringOptimizationTest.class);
     suite.addTestSuite(CastOptimizationTest.class);
     suite.addTestSuite(JsOverlayMethodOptimizationTest.class);
     suite.addTestSuite(SpecializationTest.class);
diff --git a/user/test/com/google/gwt/dev/jjs/optimized/StringOptimizationTest.java b/user/test/com/google/gwt/dev/jjs/optimized/StringOptimizationTest.java
new file mode 100644
index 0000000..bc11f17
--- /dev/null
+++ b/user/test/com/google/gwt/dev/jjs/optimized/StringOptimizationTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * 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 com.google.gwt.dev.jjs.optimized;
+
+import com.google.gwt.junit.DoNotRunWith;
+import com.google.gwt.junit.Platform;
+
+/**
+ * Tests that the String class gets optimized properly.
+ */
+@DoNotRunWith(Platform.Devel)
+public class StringOptimizationTest extends OptimizationTestBase {
+
+  private static String createString() {
+    return new String();
+  }
+  private static native String getGeneratedFunctionDefintionThatTriggersStringClinit() /*-{
+    return function() {
+      tmp = @StringOptimizationTest::createString()();
+     }.toString();
+  }-*/;
+
+  public void testStringClinitIsRemoved() throws Exception {
+    String functionDef = getGeneratedFunctionDefintionThatTriggersStringClinit();
+    assertFunctionMatches(functionDef, "tmp=''");
+  }
+
+}