Refactor inclusion of superclasses in startup profile

Change-Id: I1286e5e2bac0726c8bc4d9642d7aca9621067f9f
diff --git a/src/main/java/com/android/tools/r8/profile/AbstractProfile.java b/src/main/java/com/android/tools/r8/profile/AbstractProfile.java
index 43776fd..4ca69d8 100644
--- a/src/main/java/com/android/tools/r8/profile/AbstractProfile.java
+++ b/src/main/java/com/android/tools/r8/profile/AbstractProfile.java
@@ -4,7 +4,9 @@
 
 package com.android.tools.r8.profile;
 
+import com.android.tools.r8.graph.AppView;
 import com.android.tools.r8.graph.DexMethod;
+import com.android.tools.r8.graph.DexProgramClass;
 import com.android.tools.r8.graph.DexType;
 import com.android.tools.r8.profile.AbstractProfile.Builder;
 import com.android.tools.r8.utils.ThrowingConsumer;
@@ -31,6 +33,12 @@
 
   ProfileBuilder toEmptyBuilderWithCapacity();
 
+  default Profile toProfileWithSuperclasses(AppView<?> appView) {
+    return transform(
+        (classRule, builder) -> builder.addClassAndParentClasses(classRule.getReference(), appView),
+        (methodRule, builder) -> builder.addMethodRule(methodRule));
+  }
+
   default Profile transform(
       BiConsumer<ClassRule, ProfileBuilder> classRuleTransformer,
       BiConsumer<MethodRule, ProfileBuilder> methodRuleTransformer) {
@@ -51,8 +59,29 @@
 
     ProfileBuilder addClassRule(ClassRule classRule);
 
+    boolean addClassRule(DexType type);
+
+    default void addClassAndParentClasses(DexType type, AppView<?> appView) {
+      DexProgramClass definition = appView.app().programDefinitionFor(type);
+      if (definition != null) {
+        addClassAndParentClasses(definition, appView);
+      }
+    }
+
+    private void addClassAndParentClasses(DexProgramClass clazz, AppView<?> appView) {
+      if (addClassRule(clazz.getType())) {
+        addParentClasses(clazz, appView);
+      }
+    }
+
+    private void addParentClasses(DexProgramClass clazz, AppView<?> appView) {
+      clazz.forEachImmediateSupertype(supertype -> addClassAndParentClasses(supertype, appView));
+    }
+
     ProfileBuilder addMethodRule(MethodRule methodRule);
 
     Profile build();
+
+    int size();
   }
 }
diff --git a/src/main/java/com/android/tools/r8/profile/art/ArtProfile.java b/src/main/java/com/android/tools/r8/profile/art/ArtProfile.java
index 9ff43b1..3023520 100644
--- a/src/main/java/com/android/tools/r8/profile/art/ArtProfile.java
+++ b/src/main/java/com/android/tools/r8/profile/art/ArtProfile.java
@@ -287,6 +287,13 @@
     }
 
     @Override
+    public boolean addClassRule(DexType type) {
+      int oldSize = size();
+      addClassRule(ArtProfileClassRule.builder().setType(type).build());
+      return size() > oldSize;
+    }
+
+    @Override
     public Builder addMethodRule(ArtProfileMethodRule methodRule) {
       rules.compute(
           methodRule.getReference(),
@@ -346,5 +353,10 @@
     public ArtProfile build() {
       return new ArtProfile(rules);
     }
+
+    @Override
+    public int size() {
+      return rules.size();
+    }
   }
 }
diff --git a/src/main/java/com/android/tools/r8/profile/startup/profile/NonEmptyStartupProfile.java b/src/main/java/com/android/tools/r8/profile/startup/profile/NonEmptyStartupProfile.java
index eea1f3d..f675a24 100644
--- a/src/main/java/com/android/tools/r8/profile/startup/profile/NonEmptyStartupProfile.java
+++ b/src/main/java/com/android/tools/r8/profile/startup/profile/NonEmptyStartupProfile.java
@@ -8,7 +8,6 @@
 import com.android.tools.r8.graph.AppView;
 import com.android.tools.r8.graph.DexClass;
 import com.android.tools.r8.graph.DexMethod;
-import com.android.tools.r8.graph.DexProgramClass;
 import com.android.tools.r8.graph.DexReference;
 import com.android.tools.r8.graph.DexType;
 import com.android.tools.r8.graph.PrunedItems;
@@ -122,42 +121,7 @@
    */
   @Override
   public StartupProfile toStartupProfileForWriting(AppView<?> appView) {
-    return transform(
-        (classRule, builder) -> addStartupItem(classRule, builder, appView),
-        (methodRule, builder) -> addStartupItem(methodRule, builder, appView));
-  }
-
-  private static void addStartupItem(
-      StartupProfileRule startupItem, Builder builder, AppView<?> appView) {
-    startupItem.accept(
-        classRule -> addClassAndParentClasses(classRule.getReference(), builder, appView),
-        builder::addMethodRule);
-  }
-
-  private static boolean addClass(DexProgramClass clazz, Builder builder) {
-    int oldSize = builder.size();
-    builder.addClassRule(
-        StartupProfileClassRule.builder().setClassReference(clazz.getType()).build());
-    return builder.size() > oldSize;
-  }
-
-  private static void addClassAndParentClasses(DexType type, Builder builder, AppView<?> appView) {
-    DexProgramClass definition = appView.app().programDefinitionFor(type);
-    if (definition != null) {
-      addClassAndParentClasses(definition, builder, appView);
-    }
-  }
-
-  private static void addClassAndParentClasses(
-      DexProgramClass clazz, Builder builder, AppView<?> appView) {
-    if (addClass(clazz, builder)) {
-      addParentClasses(clazz, builder, appView);
-    }
-  }
-
-  private static void addParentClasses(DexProgramClass clazz, Builder builder, AppView<?> appView) {
-    clazz.forEachImmediateSupertype(
-        supertype -> addClassAndParentClasses(supertype, builder, appView));
+    return toProfileWithSuperclasses(appView);
   }
 
   @Override
diff --git a/src/main/java/com/android/tools/r8/profile/startup/profile/StartupProfile.java b/src/main/java/com/android/tools/r8/profile/startup/profile/StartupProfile.java
index b10578c..e6a4cb5 100644
--- a/src/main/java/com/android/tools/r8/profile/startup/profile/StartupProfile.java
+++ b/src/main/java/com/android/tools/r8/profile/startup/profile/StartupProfile.java
@@ -196,6 +196,13 @@
     }
 
     @Override
+    public boolean addClassRule(DexType type) {
+      int oldSize = size();
+      addClassRule(StartupProfileClassRule.builder().setClassReference(type).build());
+      return size() > oldSize;
+    }
+
+    @Override
     public Builder addMethodRule(StartupProfileMethodRule methodRule) {
       return addStartupItem(methodRule);
     }
@@ -259,6 +266,7 @@
       return this;
     }
 
+    @Override
     public int size() {
       return startupItems.size();
     }