Introduce a lens rewrite utility on AppView.

Bug: 149167169
Change-Id: I507af3b81ad6ad4b11379c8c78fd92678761856a
diff --git a/src/main/java/com/android/tools/r8/R8.java b/src/main/java/com/android/tools/r8/R8.java
index 4c8919f..c006eb5 100644
--- a/src/main/java/com/android/tools/r8/R8.java
+++ b/src/main/java/com/android/tools/r8/R8.java
@@ -488,12 +488,7 @@
         timing.begin("NestBasedAccessDesugaring");
         R8NestBasedAccessDesugaring analyzer = new R8NestBasedAccessDesugaring(appViewWithLiveness);
         NestedPrivateMethodLens lens = analyzer.run(executorService);
-        if (lens != null) {
-          boolean changed = appView.setGraphLens(lens);
-          assert changed;
-          appViewWithLiveness.setAppInfo(
-              appViewWithLiveness.appInfo().rewrittenWithLens(getDirectApp(appView), lens));
-        }
+        appView.rewriteWithLens(lens);
         timing.end();
       } else {
         timing.begin("NestReduction");
@@ -515,12 +510,7 @@
           StaticClassMerger staticClassMerger =
               new StaticClassMerger(appViewWithLiveness, options, mainDexClasses);
           NestedGraphLens lens = staticClassMerger.run();
-          if (lens != null) {
-            boolean changed = appView.setGraphLens(lens);
-            assert changed;
-            appViewWithLiveness.setAppInfo(
-                appViewWithLiveness.appInfo().rewrittenWithLens(getDirectApp(appView), lens));
-          }
+          appView.rewriteWithLens(lens);
           timing.end();
         }
         if (options.enableVerticalClassMerging) {
@@ -534,12 +524,8 @@
                   mainDexClasses);
           VerticalClassMergerGraphLens lens = verticalClassMerger.run();
           if (lens != null) {
-            boolean changed = appView.setGraphLens(lens);
-            assert changed;
             appView.setVerticallyMergedClasses(verticalClassMerger.getMergedClasses());
-            DirectMappedDexApplication application = getDirectApp(appView).rewrittenWithLens(lens);
-            appViewWithLiveness.setAppInfo(
-                appViewWithLiveness.appInfo().rewrittenWithLens(application, lens));
+            appView.rewriteWithLens(lens);
           }
           timing.end();
         }
@@ -552,13 +538,8 @@
                         appViewWithLiveness,
                         new MethodPoolCollection(appViewWithLiveness, subtypingInfo))
                     .run(executorService, timing);
-            if (lens != null) {
-              boolean changed = appView.setGraphLens(lens);
-              assert changed;
-              assert getDirectApp(appView).verifyNothingToRewrite(appView, lens);
-              appViewWithLiveness.setAppInfo(
-                  appViewWithLiveness.appInfo().rewrittenWithLens(getDirectApp(appView), lens));
-            }
+            assert lens == null || getDirectApp(appView).verifyNothingToRewrite(appView, lens);
+            appView.rewriteWithLens(lens);
             timing.end();
           }
           if (options.enableUninstantiatedTypeOptimization) {
@@ -570,13 +551,8 @@
                         new MethodPoolCollection(appViewWithLiveness, subtypingInfo),
                         executorService,
                         timing);
-            if (lens != null) {
-              boolean changed = appView.setGraphLens(lens);
-              assert changed;
-              assert getDirectApp(appView).verifyNothingToRewrite(appView, lens);
-              appViewWithLiveness.setAppInfo(
-                  appViewWithLiveness.appInfo().rewrittenWithLens(getDirectApp(appView), lens));
-            }
+            assert lens == null || getDirectApp(appView).verifyNothingToRewrite(appView, lens);
+            appView.rewriteWithLens(lens);
             timing.end();
           }
         }
@@ -996,11 +972,7 @@
                 executorService,
                 timing));
     NestedGraphLens lens = enqueuer.buildGraphLens(appView);
-    if (lens != null) {
-      appView.setGraphLens(lens);
-      appViewWithLiveness.setAppInfo(
-          appViewWithLiveness.appInfo().rewrittenWithLens(getDirectApp(appView), lens));
-    }
+    appView.rewriteWithLens(lens);
     if (InternalOptions.assertionsEnabled()) {
       // Register the dead proto types. These are needed to verify that no new missing types are
       // reported and that no dead proto types are referenced in the generated application.
diff --git a/src/main/java/com/android/tools/r8/graph/AppView.java b/src/main/java/com/android/tools/r8/graph/AppView.java
index 0cac318..3bacf89 100644
--- a/src/main/java/com/android/tools/r8/graph/AppView.java
+++ b/src/main/java/com/android/tools/r8/graph/AppView.java
@@ -5,6 +5,7 @@
 package com.android.tools.r8.graph;
 
 import com.android.tools.r8.graph.DexValue.DexValueString;
+import com.android.tools.r8.graph.GraphLens.NestedGraphLens;
 import com.android.tools.r8.graph.analysis.InitializedClassesInInstanceMethodsAnalysis.InitializedClassesInInstanceMethods;
 import com.android.tools.r8.graph.classmerging.HorizontallyMergedLambdaClasses;
 import com.android.tools.r8.graph.classmerging.MergedClassesCollection;
@@ -467,4 +468,19 @@
   public boolean hasCfByteCodePassThroughMethods() {
     return !cfByteCodePassThrough.isEmpty();
   }
+
+  public void rewriteWithLens(NestedGraphLens lens) {
+    rewriteWithLens(lens, withLiveness());
+  }
+
+  private static void rewriteWithLens(NestedGraphLens lens, AppView<AppInfoWithLiveness> appView) {
+    if (lens == null) {
+      return;
+    }
+    boolean changed = appView.setGraphLens(lens);
+    assert changed;
+    DirectMappedDexApplication application = appView.appInfo().app().asDirect();
+    assert application.verifyWithLens(lens);
+    appView.setAppInfo(appView.appInfo().rewrittenWithLens(application, lens));
+  }
 }
diff --git a/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java b/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
index f0f7d32..612329c 100644
--- a/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
+++ b/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
@@ -115,12 +115,10 @@
     return "DexApplication (direct)";
   }
 
-  public DirectMappedDexApplication rewrittenWithLens(GraphLens lens) {
-    // As a side effect, this will rebuild the program classes and library classes maps.
-    DirectMappedDexApplication rewrittenApplication = builder().build().asDirect();
-    assert rewrittenApplication.mappingIsValid(lens, allClasses.keySet());
-    assert rewrittenApplication.verifyCodeObjectsOwners();
-    return rewrittenApplication;
+  public boolean verifyWithLens(GraphLens lens) {
+    assert mappingIsValid(lens, allClasses.keySet());
+    assert verifyCodeObjectsOwners();
+    return true;
   }
 
   public boolean verifyNothingToRewrite(AppView<?> appView, GraphLens lens) {
@@ -139,6 +137,9 @@
     // original type will point to a definition that was renamed.
     for (DexType type : types) {
       DexType renamed = graphLens.lookupType(type);
+      if (renamed.isIntType()) {
+        continue;
+      }
       if (renamed != type) {
         if (definitionFor(type) == null && definitionFor(renamed) != null) {
           continue;
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumUnboxer.java b/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumUnboxer.java
index 0a82859..591394e 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumUnboxer.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumUnboxer.java
@@ -340,9 +340,7 @@
     NestedGraphLens enumUnboxingLens = new TreeFixer(enumsToUnbox).fixupTypeReferences();
     appView.setUnboxedEnums(enumUnboxerRewriter.getEnumsToUnbox());
     GraphLens previousLens = appView.graphLens();
-    appView.setGraphLens(enumUnboxingLens);
-    appView.setAppInfo(
-        appView.appInfo().rewrittenWithLens(appView.appInfo().app().asDirect(), enumUnboxingLens));
+    appView.rewriteWithLens(enumUnboxingLens);
     // Update optimization info.
     feedback.fixupOptimizationInfos(
         appView,
diff --git a/src/main/java/com/android/tools/r8/optimize/BridgeHoisting.java b/src/main/java/com/android/tools/r8/optimize/BridgeHoisting.java
index 6e0168f..c19bcd2 100644
--- a/src/main/java/com/android/tools/r8/optimize/BridgeHoisting.java
+++ b/src/main/java/com/android/tools/r8/optimize/BridgeHoisting.java
@@ -87,10 +87,7 @@
         .visit(appView.appInfo().classes(), clazz -> processClass(clazz, subtypingInfo));
     if (!lensBuilder.isEmpty()) {
       BridgeHoistingLens lens = lensBuilder.build(appView);
-      boolean changed = appView.setGraphLens(lens);
-      assert changed;
-      appView.setAppInfo(
-          appView.appInfo().rewrittenWithLens(appView.appInfo().app().asDirect(), lens));
+      appView.rewriteWithLens(lens);
     }
   }