Merge "Use lookupMethodInAllContexts in rewriteMixedItems"
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
index 4b57209..d73b826 100644
--- a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
@@ -11,6 +11,7 @@
 import com.android.tools.r8.graph.DexEncodedField;
 import com.android.tools.r8.graph.DexEncodedMethod;
 import com.android.tools.r8.graph.DexField;
+import com.android.tools.r8.graph.DexItem;
 import com.android.tools.r8.graph.DexMethod;
 import com.android.tools.r8.graph.DexProgramClass;
 import com.android.tools.r8.graph.DexProto;
@@ -87,28 +88,20 @@
   // Returns a set of types that must not be merged into other types.
   private Set<DexType> getPinnedTypes(Iterable<DexProgramClass> classes) {
     Set<DexType> pinnedTypes = new HashSet<>();
+
+    // For all pinned fields, also pin the type of the field (because changing the type of the field
+    // implicitly changes the signature of the field). Similarly, for all pinned methods, also pin
+    // the return type and the parameter types of the method.
+    extractPinnedItems(appInfo.pinnedItems, pinnedTypes);
+
+    // TODO(christofferqa): Remove the invariant that the graph lense should not modify any
+    // methods from the sets alwaysInline and noSideEffects (see use of assertNotModifiedBy-
+    // Lense).
+    extractPinnedItems(appInfo.alwaysInline, pinnedTypes);
+    extractPinnedItems(appInfo.noSideEffects.keySet(), pinnedTypes);
+
     for (DexProgramClass clazz : classes) {
       for (DexEncodedMethod method : clazz.methods()) {
-        // TODO(christofferqa): Remove the invariant that the graph lense should not modify any
-        // methods from the sets alwaysInline and noSideEffects (see use of assertNotModifiedBy-
-        // Lense).
-        if (appInfo.alwaysInline.contains(method) || appInfo.noSideEffects.containsKey(method)) {
-          DexClass other = appInfo.definitionFor(method.method.proto.returnType);
-          if (other != null && other.isProgramClass()) {
-            // If we were to merge [other] into its sub class, then we would implicitly change the
-            // signature of this method, and therefore break the invariant.
-            pinnedTypes.add(other.type);
-          }
-          for (DexType parameterType : method.method.proto.parameters.values) {
-            other = appInfo.definitionFor(parameterType);
-            if (other != null && other.isProgramClass()) {
-              // If we were to merge [other] into its sub class, then we would implicitly change the
-              // signature of this method, and therefore break the invariant.
-              pinnedTypes.add(other.type);
-            }
-          }
-        }
-
         // Avoid merging two types if this could remove a NoSuchMethodError, as illustrated by the
         // following example. (Alternatively, it would be possible to merge A and B and rewrite the
         // "invoke-super A.m" instruction into "invoke-super Object.m" to preserve the error. This
@@ -143,6 +136,38 @@
     return pinnedTypes;
   }
 
+  private void extractPinnedItems(Iterable<DexItem> items, Set<DexType> pinnedTypes) {
+    for (DexItem item : items) {
+      // Note: Nothing to do for the case where item is a DexType, since we check for this case
+      // using appInfo.isPinned.
+      if (item instanceof DexField) {
+        // Pin the type of the field.
+        DexField field = (DexField) item;
+        DexClass clazz = appInfo.definitionFor(field.type);
+        if (clazz != null && clazz.isProgramClass()) {
+          pinnedTypes.add(clazz.type);
+        }
+      } else if (item instanceof DexMethod) {
+        // Pin the return type and the parameter types of the method.
+        DexMethod method = (DexMethod) item;
+        DexClass clazz = appInfo.definitionFor(method.proto.returnType);
+        if (clazz != null && clazz.isProgramClass()) {
+          // If we were to merge [other] into its sub class, then we would implicitly change the
+          // signature of this method, and therefore break the invariant.
+          pinnedTypes.add(clazz.type);
+        }
+        for (DexType parameterType : method.proto.parameters.values) {
+          clazz = appInfo.definitionFor(parameterType);
+          if (clazz != null && clazz.isProgramClass()) {
+            // If we were to merge [other] into its sub class, then we would implicitly change the
+            // signature of this method, and therefore break the invariant.
+            pinnedTypes.add(clazz.type);
+          }
+        }
+      }
+    }
+  }
+
   private boolean isMergeCandidate(DexProgramClass clazz, Set<DexType> pinnedTypes) {
     if (appInfo.instantiatedTypes.contains(clazz.type)
         || appInfo.isPinned(clazz.type)
diff --git a/third_party/r8.tar.gz.sha1 b/third_party/r8.tar.gz.sha1
index fbb2aa8..06f1922 100644
--- a/third_party/r8.tar.gz.sha1
+++ b/third_party/r8.tar.gz.sha1
@@ -1 +1 @@
-38b85dcea75f12c37332a5425c87733e78754ba6
\ No newline at end of file
+ad3858bd8a8597f674d1af6bb762c16672dc6436
\ No newline at end of file
diff --git a/tools/minify_tool.py b/tools/minify_tool.py
index 8f440dd..8b086e5 100755
--- a/tools/minify_tool.py
+++ b/tools/minify_tool.py
@@ -19,9 +19,10 @@
 import re
 import sys
 import time
+import zipfile
+
 import toolhelper
 import utils
-import zipfile
 
 KEEP = '-keep public class %s { public static void main(...); }\n'
 MANIFEST_PATH = 'META-INF/MANIFEST.MF'
@@ -85,7 +86,8 @@
     return mo.group(1)
 
 def minify_tool(mainclass=None, input_jar=utils.R8_JAR, output_jar=None,
-                lib=utils.RT_JAR, debug=True, build=True, benchmark_name=None):
+                lib=utils.RT_JAR, debug=True, build=True, benchmark_name=None,
+                track_memory_file=None):
   if output_jar is None:
     output_jar = generate_output_name(input_jar, mainclass)
   with utils.TempDir() as path:
@@ -105,10 +107,15 @@
             '--release',
             tmp_input_path)
     start_time = time.time()
-    return_code = toolhelper.run('r8', args, debug=debug, build=build)
+    return_code = toolhelper.run('r8', args, debug=debug, build=build,
+                                 track_memory_file=track_memory_file)
     if benchmark_name:
       elapsed_ms = 1000 * (time.time() - start_time)
       print('%s(RunTimeRaw): %s ms' % (benchmark_name, elapsed_ms))
+      if track_memory_file:
+        print('%s(MemoryUse): %s' %
+              (benchmark_name, utils.grep_memoryuse(track_memory_file)))
+
     return return_code
 
 if __name__ == '__main__':
diff --git a/tools/run_bootstrap_benchmark.py b/tools/run_bootstrap_benchmark.py
index 2ae97b2..8230c71 100755
--- a/tools/run_bootstrap_benchmark.py
+++ b/tools/run_bootstrap_benchmark.py
@@ -3,21 +3,53 @@
 # for details. All rights reserved. Use of this source code is governed by a
 # BSD-style license that can be found in the LICENSE file.
 
-import argparse
-import minify_tool
 import os
 import sys
+
+import minify_tool
+import toolhelper
 import utils
 
-
 PINNED_R8_JAR = os.path.join(utils.REPO_ROOT, 'third_party/r8/r8.jar')
+PINNED_PGR8_JAR = os.path.join(utils.REPO_ROOT, 'third_party/r8/r8-pg6.0.1.jar')
 
-parser = argparse.ArgumentParser()
-parser.add_argument(
-    '--name', metavar='NAME', dest='benchmark_name',
-    help='Print "<NAME>(RunTimeRaw): <elapsed> ms" at the end')
-
+def dex(input, output):
+  return_code = toolhelper.run(
+      'd8', [
+        input,
+        '--output', output,
+        '--lib', utils.RT_JAR,
+        '--min-api', '10000',
+        '--no-desugaring',
+      ],
+      debug=False,
+      build=False)
+  if return_code != 0:
+    sys.exit(return_code)
 
 if __name__ == '__main__':
-  sys.exit(minify_tool.minify_tool(input_jar=PINNED_R8_JAR, debug=False,
-                                   build=False, **vars(parser.parse_args())))
+  with utils.TempDir() as temp:
+    memory_file = os.path.join(temp, 'memory.dump')
+    r8_output = os.path.join(temp, 'r8.zip')
+    d8_r8_output = os.path.join(temp, 'd8r8.zip')
+    d8_pg_output = os.path.join(temp, 'd8pg.zip')
+
+    return_code = minify_tool.minify_tool(
+      input_jar=PINNED_R8_JAR,
+      output_jar=r8_output,
+      debug=False,
+      build=False,
+      track_memory_file=memory_file,
+      benchmark_name="BootstrapR8")
+    if return_code != 0:
+      sys.exit(return_code)
+
+    dex(r8_output, d8_r8_output)
+    print "BootstrapR8(CodeSize):", os.path.getsize(r8_output)
+    print "BootstrapR8Dex(CodeSize):", os.path.getsize(d8_r8_output)
+
+    dex(PINNED_PGR8_JAR, d8_pg_output)
+    print "BootstrapPG(CodeSize):", os.path.getsize(PINNED_PGR8_JAR)
+    print "BootstrapPGDex(CodeSize):", os.path.getsize(d8_pg_output)
+
+  sys.exit(0)
\ No newline at end of file