Use machine desugared library conf when possible

Bug: 184026720
Change-Id: I8a995bdd6bcf0a90dd3ff0e919fece98524c7338
diff --git a/src/main/java/com/android/tools/r8/androidapi/AndroidApiReferenceLevelCache.java b/src/main/java/com/android/tools/r8/androidapi/AndroidApiReferenceLevelCache.java
index 2e2aa7a..cce0852 100644
--- a/src/main/java/com/android/tools/r8/androidapi/AndroidApiReferenceLevelCache.java
+++ b/src/main/java/com/android/tools/r8/androidapi/AndroidApiReferenceLevelCache.java
@@ -9,7 +9,6 @@
 import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexReference;
 import com.android.tools.r8.graph.DexType;
-import com.android.tools.r8.ir.desugar.desugaredlibrary.legacyspecification.LegacyDesugaredLibrarySpecification;
 import com.android.tools.r8.utils.AndroidApiLevel;
 import com.android.tools.r8.utils.ConsumerUtils;
 import com.google.common.collect.ImmutableList;
@@ -18,7 +17,6 @@
 
 public class AndroidApiReferenceLevelCache {
 
-  private final LegacyDesugaredLibrarySpecification desugaredLibrarySpecification;
   private final AndroidApiLevelCompute apiLevelCompute;
   private final AndroidApiLevelDatabase androidApiLevelDatabase;
   private final AppView<?> appView;
@@ -33,7 +31,6 @@
     factory = appView.dexItemFactory();
     androidApiLevelDatabase =
         new AndroidApiLevelHashingDatabaseImpl(predefinedApiTypeLookupForHashing);
-    desugaredLibrarySpecification = appView.options().desugaredLibrarySpecification;
   }
 
   public static AndroidApiReferenceLevelCache create(
@@ -75,7 +72,7 @@
     if (reference.getContextType() == factory.objectType) {
       return appView.computedMinApiLevel();
     }
-    if (desugaredLibrarySpecification.isSupported(reference, appView)) {
+    if (appView.options().machineDesugaredLibrarySpecification.isSupported(reference)) {
       // If we end up desugaring the reference, the library classes is bridged by j$ which is part
       // of the program.
       return appView.computedMinApiLevel();
diff --git a/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java b/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java
index f9b13a4..ef86f73 100644
--- a/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java
+++ b/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java
@@ -19,7 +19,6 @@
 import com.android.tools.r8.graph.ProgramMethod;
 import com.android.tools.r8.graph.ThrowExceptionCode;
 import com.android.tools.r8.graph.UseRegistry;
-import com.android.tools.r8.ir.desugar.desugaredlibrary.legacyspecification.LegacyDesugaredLibrarySpecification;
 import com.android.tools.r8.shaking.AppInfoWithLiveness;
 import com.android.tools.r8.synthesis.CommittedItems;
 import com.android.tools.r8.synthesis.SyntheticNaming.SyntheticKind;
@@ -112,12 +111,10 @@
       new ConcurrentHashMap<>();
   private final Set<DexType> seenTypes = Sets.newConcurrentHashSet();
   private final AndroidApiLevelCompute apiLevelCompute;
-  private final LegacyDesugaredLibrarySpecification desugaredLibraryConfiguration;
 
   public ApiReferenceStubber(AppView<? extends AppInfoWithClassHierarchy> appView) {
     this.appView = appView;
     apiLevelCompute = appView.apiLevelCompute();
-    desugaredLibraryConfiguration = appView.options().desugaredLibrarySpecification;
   }
 
   public void run(ExecutorService executorService) throws ExecutionException {
@@ -220,7 +217,10 @@
         || libraryClass.getType().toDescriptorString().startsWith("Ljava/")) {
       return;
     }
-    if (desugaredLibraryConfiguration.isSupported(libraryClass.getType(), appView)) {
+    if (appView
+        .options()
+        .machineDesugaredLibrarySpecification
+        .isSupported(libraryClass.getType())) {
       return;
     }
     appView
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
index 0606d2d..f581078 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
@@ -1404,7 +1404,7 @@
     }
 
     private void addProvider(MethodProvider generator) {
-      if (appView.options().desugaredLibrarySpecification.isSupported(generator.method, appView)) {
+      if (appView.options().machineDesugaredLibrarySpecification.isSupported(generator.method)) {
         // TODO(b/174453232): Remove this after the configuration file format has bee updated
         // with the "rewrite_method" section.
         if (generator.method.getHolderType() == appView.dexItemFactory().objectsType) {
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/PrefixRewritingMapper.java b/src/main/java/com/android/tools/r8/ir/desugar/PrefixRewritingMapper.java
index f8cf00e..90f2505 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/PrefixRewritingMapper.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/PrefixRewritingMapper.java
@@ -4,20 +4,11 @@
 
 package com.android.tools.r8.ir.desugar;
 
-import com.android.tools.r8.errors.CompilationError;
 import com.android.tools.r8.graph.AppView;
-import com.android.tools.r8.graph.DexClass;
-import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexProto;
-import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.graph.DexType;
-import com.android.tools.r8.ir.desugar.desugaredlibrary.legacyspecification.LegacyDesugaredLibrarySpecification;
 import com.android.tools.r8.ir.desugar.desugaredlibrary.machinespecification.MachineDesugaredLibrarySpecification;
-import com.android.tools.r8.utils.DescriptorUtils;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Sets;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Consumer;
 
@@ -31,7 +22,7 @@
 
   public abstract DexType rewrittenType(DexType type, AppView<?> appView);
 
-  public abstract DexType rewrittenContextType(DexType type, AppView<?> appView);
+  public abstract DexType rewrittenContextType(DexType type);
 
   public boolean hasRewrittenType(DexType type, AppView<?> appView) {
     return rewrittenType(type, appView) != null;
@@ -53,183 +44,12 @@
 
   public abstract void forAllRewrittenTypes(Consumer<DexType> consumer);
 
-  public static class DesugarPrefixRewritingMapper extends PrefixRewritingMapper {
-
-    private final Set<DexType> notRewritten = Sets.newConcurrentHashSet();
-    private final Map<DexType, DexType> rewritten = new ConcurrentHashMap<>();
-    private final Map<DexString, DexString> initialPrefixes;
-    private final DexItemFactory factory;
-    private final boolean l8Compilation;
-
-    public DesugarPrefixRewritingMapper(
-        Map<String, String> prefixes, DexItemFactory itemFactory, boolean libraryCompilation) {
-      assert itemFactory != null || prefixes.isEmpty();
-      this.factory = itemFactory;
-      this.l8Compilation = libraryCompilation;
-      ImmutableMap.Builder<DexString, DexString> builder = ImmutableMap.builder();
-      for (String key : prefixes.keySet()) {
-        builder.put(toDescriptorPrefix(key), toDescriptorPrefix(prefixes.get(key)));
-      }
-      this.initialPrefixes = builder.build();
-      validatePrefixes(prefixes);
-    }
-
-    private DexString toDescriptorPrefix(String prefix) {
-      return factory.createString("L" + DescriptorUtils.getBinaryNameFromJavaType(prefix));
-    }
-
-    @Override
-    public void forAllRewrittenTypes(Consumer<DexType> consumer) {
-      rewritten.keySet().forEach(consumer);
-    }
-
-    private void validatePrefixes(Map<String, String> initialPrefixes) {
-      String[] prefixes = initialPrefixes.keySet().toArray(new String[0]);
-      for (int i = 0; i < prefixes.length; i++) {
-        for (int j = i + 1; j < prefixes.length; j++) {
-          String small, large;
-          if (prefixes[i].length() < prefixes[j].length()) {
-            small = prefixes[i];
-            large = prefixes[j];
-          } else {
-            small = prefixes[j];
-            large = prefixes[i];
-          }
-          if (large.startsWith(small)) {
-            throw new CompilationError(
-                "Inconsistent prefix in desugared library:"
-                    + " Should a class starting with "
-                    + small
-                    + " be rewritten using "
-                    + small
-                    + " -> "
-                    + initialPrefixes.get(small)
-                    + " or using "
-                    + large
-                    + " - > "
-                    + initialPrefixes.get(large)
-                    + " ?");
-          }
-        }
-      }
-    }
-
-    @Override
-    public DexType rewrittenType(DexType type, AppView<?> appView) {
-      assert appView != null || l8Compilation;
-      if (notRewritten.contains(type)) {
-        return null;
-      }
-      if (rewritten.containsKey(type)) {
-        return rewritten.get(type);
-      }
-      return computePrefix(type, appView);
-    }
-
-    @Override
-    public DexType rewrittenContextType(DexType type, AppView<?> appView) {
-      DexType rewritten = rewrittenType(type, appView);
-      if (rewritten != null) {
-        return rewritten;
-      }
-      LegacyDesugaredLibrarySpecification desugaredLibrarySpecification =
-          appView.options().desugaredLibrarySpecification;
-      if (desugaredLibrarySpecification.getEmulateLibraryInterface().containsKey(type)) {
-        return desugaredLibrarySpecification.getEmulateLibraryInterface().get(type);
-      }
-      for (Map<DexType, DexType> value :
-          desugaredLibrarySpecification.getRetargetCoreLibMember().values()) {
-        if (value.containsKey(type)) {
-          // Hack until machine specification are ready.
-          String prefix =
-              DescriptorUtils.getJavaTypeFromBinaryName(
-                  desugaredLibrarySpecification.getSynthesizedLibraryClassesPackagePrefix());
-          String interfaceType = type.toString();
-          int firstPackage = interfaceType.indexOf('.');
-          return appView
-              .dexItemFactory()
-              .createType(
-                  DescriptorUtils.javaTypeToDescriptor(
-                      prefix + interfaceType.substring(firstPackage + 1)));
-        }
-      }
-      return null;
-    }
-
-    // Besides L8 compilation, program types should not be rewritten.
-    private void failIfRewritingProgramType(DexType type, AppView<?> appView) {
-      if (l8Compilation) {
-        return;
-      }
-
-      DexType dexType = type.isArrayType() ? type.toBaseType(appView.dexItemFactory()) : type;
-      DexClass dexClass = appView.definitionFor(dexType);
-      if (dexClass != null && dexClass.isProgramClass()) {
-        appView
-            .options()
-            .reporter
-            .error(
-                "Cannot compile program class "
-                    + dexType
-                    + " since it conflicts with a desugared library rewriting rule.");
-      }
-    }
-
-    @Override
-    public void rewriteType(DexType type, DexType rewrittenType) {
-      assert !notRewritten.contains(type)
-          : "New rewriting rule for "
-              + type
-              + " but the compiler has already made decisions based on the fact that this type was"
-              + " not rewritten";
-      assert !rewritten.containsKey(type) || rewritten.get(type) == rewrittenType
-          : "New rewriting rule for "
-              + type
-              + " but the compiler has already made decisions based on a different rewriting rule"
-              + " for this type";
-      rewritten.put(type, rewrittenType);
-    }
-
-    private DexType computePrefix(DexType type, AppView<?> appView) {
-      DexString prefixToMatch = type.descriptor.withoutArray(factory);
-      DexType result = lookup(type, prefixToMatch, initialPrefixes);
-      if (result != null) {
-        failIfRewritingProgramType(type, appView);
-        return result;
-      }
-      notRewritten.add(type);
-      return null;
-    }
-
-    private DexType lookup(DexType type, DexString prefixToMatch, Map<DexString, DexString> map) {
-      // TODO(b/154800164): We could use tries instead of looking-up everywhere.
-      for (DexString prefix : map.keySet()) {
-        if (prefixToMatch.startsWith(prefix)) {
-          DexString rewrittenTypeDescriptor =
-              type.descriptor.withNewPrefix(prefix, map.get(prefix), factory);
-          DexType rewrittenType = factory.createType(rewrittenTypeDescriptor);
-          rewriteType(type, rewrittenType);
-          return rewrittenType;
-        }
-      }
-      return null;
-    }
-
-    @Override
-    public boolean isRewriting() {
-      return true;
-    }
-  }
-
   public static class MachineDesugarPrefixRewritingMapper extends PrefixRewritingMapper {
 
-    private final PrefixRewritingMapper mapper;
     private final Map<DexType, DexType> rewriteType;
     private final Map<DexType, DexType> rewriteDerivedTypeOnly;
 
-    public MachineDesugarPrefixRewritingMapper(
-        PrefixRewritingMapper mapper, MachineDesugaredLibrarySpecification specification) {
-      this.mapper = mapper;
+    public MachineDesugarPrefixRewritingMapper(MachineDesugaredLibrarySpecification specification) {
       this.rewriteType = new ConcurrentHashMap<>(specification.getRewriteType());
       rewriteDerivedTypeOnly = specification.getRewriteDerivedTypeOnly();
     }
@@ -246,15 +66,11 @@
             .dexItemFactory()
             .createArrayType(type.getNumberOfLeadingSquareBrackets(), rewrittenBaseType);
       }
-      assert mapper.rewrittenType(type, appView) == rewriteType.get(type)
-          || appView.definitionFor(type) == null
-          || (appView.definitionFor(type).isProgramClass()
-              && !appView.options().isDesugaredLibraryCompilation());
       return rewriteType.get(type);
     }
 
     @Override
-    public DexType rewrittenContextType(DexType context, AppView<?> appView) {
+    public DexType rewrittenContextType(DexType context) {
       assert !context.isArrayType();
       if (rewriteType.containsKey(context)) {
         return rewriteType.get(context);
@@ -264,7 +80,6 @@
 
     @Override
     public void rewriteType(DexType type, DexType rewrittenType) {
-      mapper.rewriteType(type, rewrittenType);
       rewriteType.compute(
           type,
           (t, val) -> {
@@ -292,7 +107,7 @@
     }
 
     @Override
-    public DexType rewrittenContextType(DexType type, AppView<?> appView) {
+    public DexType rewrittenContextType(DexType type) {
       return null;
     }
 
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/apiconversion/DesugaredLibraryWrapperSynthesizer.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/apiconversion/DesugaredLibraryWrapperSynthesizer.java
index 1e62da6..3769787 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/apiconversion/DesugaredLibraryWrapperSynthesizer.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/apiconversion/DesugaredLibraryWrapperSynthesizer.java
@@ -20,6 +20,7 @@
 import com.android.tools.r8.graph.DexMethod;
 import com.android.tools.r8.graph.DexProgramClass;
 import com.android.tools.r8.graph.DexProto;
+import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.graph.DexType;
 import com.android.tools.r8.graph.FieldAccessFlags;
 import com.android.tools.r8.graph.MethodAccessFlags;
@@ -39,6 +40,7 @@
 import com.android.tools.r8.synthesis.SyntheticClassBuilder;
 import com.android.tools.r8.synthesis.SyntheticMethodBuilder;
 import com.android.tools.r8.synthesis.SyntheticNaming.SyntheticKind;
+import com.android.tools.r8.utils.Pair;
 import com.android.tools.r8.utils.StringDiagnostic;
 import com.google.common.collect.Iterables;
 import java.util.ArrayList;
@@ -175,11 +177,11 @@
   private DexMethod getCustomConversion(DexType type, DexType srcType, DexType destType) {
     // ConversionType holds the methods "rewrittenType convert(type)" and the other way around.
     // But everything is going to be rewritten, so we need to use vivifiedType and type".
-    DexType conversionHolder =
-        appView.options().desugaredLibrarySpecification.getCustomConversions().get(type);
-    if (conversionHolder != null) {
+    Pair<DexType, DexString> pair =
+        appView.options().machineDesugaredLibrarySpecification.getCustomConversions().get(type);
+    if (pair != null) {
       return factory.createMethod(
-          conversionHolder, factory.createProto(destType, srcType), factory.convertMethodName);
+          pair.getFirst(), factory.createProto(destType, srcType), pair.getSecond());
     }
     return null;
   }
@@ -217,7 +219,7 @@
   }
 
   private boolean canGenerateWrapper(DexType type) {
-    return appView.options().desugaredLibrarySpecification.getWrapperConversions().contains(type);
+    return appView.options().machineDesugaredLibrarySpecification.getWrappers().containsKey(type);
   }
 
   private DexClass getValidClassToWrap(DexType type) {
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecification.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecification.java
index 6a6a2d4..1e871b3 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecification.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecification.java
@@ -6,14 +6,9 @@
 import com.android.tools.r8.graph.AppView;
 import com.android.tools.r8.graph.DexClassAndMethod;
 import com.android.tools.r8.graph.DexEncodedMethod;
-import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexMethod;
-import com.android.tools.r8.graph.DexReference;
 import com.android.tools.r8.graph.DexType;
-import com.android.tools.r8.ir.desugar.PrefixRewritingMapper;
-import com.android.tools.r8.ir.desugar.PrefixRewritingMapper.DesugarPrefixRewritingMapper;
 import com.android.tools.r8.utils.AndroidApiLevel;
-import com.android.tools.r8.utils.InternalOptions;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -23,56 +18,20 @@
   private final boolean libraryCompilation;
   private final HumanTopLevelFlags topLevelFlags;
   private final HumanRewritingFlags rewritingFlags;
-  private final PrefixRewritingMapper prefixRewritingMapper;
-
-  public static HumanDesugaredLibrarySpecification withOnlyRewritePrefixForTesting(
-      Map<String, String> prefix, InternalOptions options) {
-    return new HumanDesugaredLibrarySpecification(
-        HumanTopLevelFlags.empty(),
-        HumanRewritingFlags.withOnlyRewritePrefixForTesting(prefix, options),
-        true,
-        options.itemFactory);
-  }
-
-  public static HumanDesugaredLibrarySpecification empty() {
-    return new HumanDesugaredLibrarySpecification(
-        HumanTopLevelFlags.empty(), HumanRewritingFlags.empty(), false, null) {
-
-      @Override
-      public boolean isSupported(DexReference reference, AppView<?> appView) {
-        return false;
-      }
-
-      @Override
-      public boolean isEmptyConfiguration() {
-        return true;
-      }
-    };
-  }
 
   public HumanDesugaredLibrarySpecification(
       HumanTopLevelFlags topLevelFlags,
       HumanRewritingFlags rewritingFlags,
-      boolean libraryCompilation,
-      DexItemFactory factory) {
+      boolean libraryCompilation) {
     this.libraryCompilation = libraryCompilation;
     this.topLevelFlags = topLevelFlags;
     this.rewritingFlags = rewritingFlags;
-    this.prefixRewritingMapper =
-        rewritingFlags.getRewritePrefix().isEmpty()
-            ? PrefixRewritingMapper.empty()
-            : new DesugarPrefixRewritingMapper(
-                rewritingFlags.getRewritePrefix(), factory, libraryCompilation);
   }
 
   public boolean supportAllCallbacksFromLibrary() {
     return topLevelFlags.supportAllCallbacksFromLibrary();
   }
 
-  public PrefixRewritingMapper getPrefixRewritingMapper() {
-    return prefixRewritingMapper;
-  }
-
   public AndroidApiLevel getRequiredCompilationApiLevel() {
     return topLevelFlags.getRequiredCompilationAPILevel();
   }
@@ -109,10 +68,6 @@
     return rewritingFlags.getEmulateLibraryInterface();
   }
 
-  public boolean isSupported(DexReference reference, AppView<?> appView) {
-    return prefixRewritingMapper.hasRewrittenType(reference.getContextType(), appView);
-  }
-
   // If the method is retargeted, answers the retargeted method, else null.
   public DexMethod retargetMethod(DexEncodedMethod method, AppView<?> appView) {
     Map<DexMethod, DexType> retargetCoreLibMember = rewritingFlags.getRetargetCoreLibMember();
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java
index a612177..e01def6 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java
@@ -103,7 +103,7 @@
 
     HumanDesugaredLibrarySpecification config =
         new HumanDesugaredLibrarySpecification(
-            topLevelFlags, legacyRewritingFlags, libraryCompilation, dexItemFactory);
+            topLevelFlags, legacyRewritingFlags, libraryCompilation);
     origin = null;
     return config;
   }
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecification.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecification.java
index 3da0b97..9ab05b9 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecification.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecification.java
@@ -6,13 +6,9 @@
 import com.android.tools.r8.graph.AppView;
 import com.android.tools.r8.graph.DexClassAndMethod;
 import com.android.tools.r8.graph.DexEncodedMethod;
-import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexMethod;
-import com.android.tools.r8.graph.DexReference;
 import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.graph.DexType;
-import com.android.tools.r8.ir.desugar.PrefixRewritingMapper;
-import com.android.tools.r8.ir.desugar.PrefixRewritingMapper.DesugarPrefixRewritingMapper;
 import com.android.tools.r8.utils.AndroidApiLevel;
 import com.android.tools.r8.utils.DescriptorUtils;
 import com.android.tools.r8.utils.InternalOptions;
@@ -26,25 +22,18 @@
   private final boolean libraryCompilation;
   private final LegacyTopLevelFlags topLevelFlags;
   private final LegacyRewritingFlags rewritingFlags;
-  private final PrefixRewritingMapper prefixRewritingMapper;
 
   public static LegacyDesugaredLibrarySpecification withOnlyRewritePrefixForTesting(
       Map<String, String> prefix, InternalOptions options) {
     return new LegacyDesugaredLibrarySpecification(
         LegacyTopLevelFlags.empty(),
         LegacyRewritingFlags.withOnlyRewritePrefixForTesting(prefix, options),
-        true,
-        options.itemFactory);
+        true);
   }
 
   public static LegacyDesugaredLibrarySpecification empty() {
     return new LegacyDesugaredLibrarySpecification(
-        LegacyTopLevelFlags.empty(), LegacyRewritingFlags.empty(), false, null) {
-
-      @Override
-      public boolean isSupported(DexReference reference, AppView<?> appView) {
-        return false;
-      }
+        LegacyTopLevelFlags.empty(), LegacyRewritingFlags.empty(), false) {
 
       @Override
       public boolean isEmptyConfiguration() {
@@ -56,16 +45,10 @@
   public LegacyDesugaredLibrarySpecification(
       LegacyTopLevelFlags topLevelFlags,
       LegacyRewritingFlags rewritingFlags,
-      boolean libraryCompilation,
-      DexItemFactory factory) {
+      boolean libraryCompilation) {
     this.libraryCompilation = libraryCompilation;
     this.topLevelFlags = topLevelFlags;
     this.rewritingFlags = rewritingFlags;
-    this.prefixRewritingMapper =
-        rewritingFlags.getRewritePrefix().isEmpty()
-            ? PrefixRewritingMapper.empty()
-            : new DesugarPrefixRewritingMapper(
-                rewritingFlags.getRewritePrefix(), factory, libraryCompilation);
   }
 
   public LegacyTopLevelFlags getTopLevelFlags() {
@@ -80,10 +63,6 @@
     return topLevelFlags.supportAllCallbacksFromLibrary();
   }
 
-  public PrefixRewritingMapper getPrefixRewritingMapper() {
-    return prefixRewritingMapper;
-  }
-
   public AndroidApiLevel getRequiredCompilationApiLevel() {
     return topLevelFlags.getRequiredCompilationAPILevel();
   }
@@ -123,10 +102,6 @@
     return rewritingFlags.getEmulateLibraryInterface();
   }
 
-  public boolean isSupported(DexReference reference, AppView<?> appView) {
-    return prefixRewritingMapper.hasRewrittenType(reference.getContextType(), appView);
-  }
-
   // If the method is retargeted, answers the retargeted method, else null.
   public DexMethod retargetMethod(DexEncodedMethod method, AppView<?> appView) {
     Map<DexString, Map<DexType, DexType>> retargetCoreLibMember =
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecificationParser.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecificationParser.java
index 7cece17..a3afcd9 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecificationParser.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/legacyspecification/LegacyDesugaredLibrarySpecificationParser.java
@@ -110,7 +110,7 @@
 
     LegacyDesugaredLibrarySpecification config =
         new LegacyDesugaredLibrarySpecification(
-            topLevelFlags, legacyRewritingFlags, libraryCompilation, dexItemFactory);
+            topLevelFlags, legacyRewritingFlags, libraryCompilation);
     origin = null;
     return config;
   }
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/machinespecification/MachineDesugaredLibrarySpecification.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/machinespecification/MachineDesugaredLibrarySpecification.java
index 7e48808..7cf54a8 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/machinespecification/MachineDesugaredLibrarySpecification.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/machinespecification/MachineDesugaredLibrarySpecification.java
@@ -5,6 +5,7 @@
 package com.android.tools.r8.ir.desugar.desugaredlibrary.machinespecification;
 
 import com.android.tools.r8.graph.DexMethod;
+import com.android.tools.r8.graph.DexReference;
 import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.graph.DexType;
 import com.android.tools.r8.utils.AndroidApiLevel;
@@ -22,7 +23,12 @@
 
   public static MachineDesugaredLibrarySpecification empty() {
     return new MachineDesugaredLibrarySpecification(
-        false, MachineTopLevelFlags.empty(), MachineRewritingFlags.builder().build());
+        false, MachineTopLevelFlags.empty(), MachineRewritingFlags.builder().build()) {
+      @Override
+      public boolean isSupported(DexReference reference) {
+        return false;
+      }
+    };
   }
 
   public static MachineDesugaredLibrarySpecification withOnlyRewriteTypeForTesting(
@@ -130,4 +136,28 @@
   public boolean hasEmulatedInterfaces() {
     return rewritingFlags.hasEmulatedInterfaces();
   }
+
+  public boolean isSupported(DexReference reference) {
+    // Support through type rewriting.
+    if (rewritingFlags.getRewriteType().containsKey(reference.getContextType())) {
+      return true;
+    }
+    if (!reference.isDexMethod()) {
+      return false;
+    }
+    // Support through retargeting.
+    DexMethod dexMethod = reference.asDexMethod();
+    if (getStaticRetarget().containsKey(dexMethod)
+        || getNonEmulatedVirtualRetarget().containsKey(dexMethod)
+        || getEmulatedVirtualRetarget().containsKey(dexMethod)) {
+      return true;
+    }
+    // Support through emulated interface.
+    for (EmulatedInterfaceDescriptor descriptor : getEmulatedInterfaces().values()) {
+      if (descriptor.getEmulatedMethods().containsKey(dexMethod)) {
+        return true;
+      }
+    }
+    return false;
+  }
 }
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterPostProcessor.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterPostProcessor.java
index 414c32d..d1e64ec 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterPostProcessor.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterPostProcessor.java
@@ -127,8 +127,8 @@
       }
       if (appView
           .options()
-          .desugaredLibrarySpecification
-          .getDontRetargetLibMember()
+          .machineDesugaredLibrarySpecification
+          .getDontRetarget()
           .contains(clazz.getType())) {
         continue;
       }
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
index 242777d..e5c9b42 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
@@ -117,10 +117,7 @@
     HumanRewritingFlags humanRewritingFlags =
         convertRewritingFlags(legacySpec.getRewritingFlags(), app, origin);
     return new HumanDesugaredLibrarySpecification(
-        humanTopLevelFlags,
-        humanRewritingFlags,
-        legacySpec.isLibraryCompilation(),
-        app.dexItemFactory());
+        humanTopLevelFlags, humanRewritingFlags, legacySpec.isLibraryCompilation());
   }
 
   private void legacyLibraryFlagHacks(
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceApplicationRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceApplicationRewriter.java
index 74d94e3..d06fee7 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceApplicationRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceApplicationRewriter.java
@@ -15,6 +15,7 @@
 import com.android.tools.r8.utils.IterableUtils;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.IdentityHashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -25,8 +26,15 @@
 
   public EmulatedInterfaceApplicationRewriter(AppView<?> appView) {
     this.appView = appView;
-    this.emulatedInterfaces =
-        appView.options().desugaredLibrarySpecification.getEmulateLibraryInterface();
+    emulatedInterfaces = new IdentityHashMap<>();
+    appView
+        .options()
+        .machineDesugaredLibrarySpecification
+        .getEmulatedInterfaces()
+        .forEach(
+            (ei, descriptor) -> {
+              emulatedInterfaces.put(ei, descriptor.getRewrittenType());
+            });
   }
 
   public void rewriteApplication(DexApplication.Builder<?> builder) {
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceDesugaringSyntheticHelper.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceDesugaringSyntheticHelper.java
index ff74b79..a77507a 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceDesugaringSyntheticHelper.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceDesugaringSyntheticHelper.java
@@ -241,8 +241,9 @@
     if (descriptor == null) {
       return null;
     }
-    DerivedMethod forwardingMethod = descriptor.getDispatchCases().get(method.getHolderType());
-    return forwardingMethod != null ? forwardingMethod : descriptor.getForwardingMethod();
+    return descriptor.getDispatchCases().containsKey(method.getHolderType())
+        ? descriptor.getDispatchCases().get(method.getHolderType())
+        : descriptor.getForwardingMethod();
   }
 
   DexMethod ensureEmulatedInterfaceForwardingMethod(DerivedMethod method) {
@@ -252,7 +253,6 @@
     assert method.getHolderKind() == SyntheticKind.COMPANION_CLASS;
     DexClassAndMethod resolvedMethod =
         appView.appInfoForDesugaring().resolveMethod(method.getMethod(), true).getResolutionPair();
-    assert resolvedMethod != null;
     return ensureDefaultAsMethodOfCompanionClassStub(resolvedMethod).getReference();
   }
 
@@ -265,7 +265,6 @@
             .appInfoForDesugaring()
             .resolveMethod(emulatedDispatchMethod.getMethod(), true)
             .getResolutionPair();
-    assert method != null;
     assert emulatedDispatchMethod.getHolderKind() == SyntheticKind.EMULATED_INTERFACE_CLASS;
     if (method.isProgramMethod()) {
       assert appView.options().isDesugaredLibraryCompilation();
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java
index 7ebf704..0227b1f 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java
@@ -467,7 +467,7 @@
     DerivedMethod emulatedDispatchMethod =
         helper.computeEmulatedInterfaceDispatchMethod(resolutionResult);
     if (emulatedDispatchMethod == null) {
-      return DesugarDescription.nothing();
+      return null;
     }
     return DesugarDescription.builder()
         .setDesugarRewrite(
diff --git a/src/main/java/com/android/tools/r8/synthesis/SynthesizingContext.java b/src/main/java/com/android/tools/r8/synthesis/SynthesizingContext.java
index e2b427d..604748a 100644
--- a/src/main/java/com/android/tools/r8/synthesis/SynthesizingContext.java
+++ b/src/main/java/com/android/tools/r8/synthesis/SynthesizingContext.java
@@ -129,8 +129,7 @@
       return;
     }
     assert hygienicType.toSourceString().startsWith(synthesizingContextType.toSourceString());
-    DexType rewrittenContext =
-        appView.rewritePrefix.rewrittenContextType(synthesizingContextType, appView);
+    DexType rewrittenContext = appView.rewritePrefix.rewrittenContextType(synthesizingContextType);
     if (rewrittenContext == null) {
       return;
     }
diff --git a/src/main/java/com/android/tools/r8/synthesis/SyntheticItems.java b/src/main/java/com/android/tools/r8/synthesis/SyntheticItems.java
index 0cb0137..6d45ee1 100644
--- a/src/main/java/com/android/tools/r8/synthesis/SyntheticItems.java
+++ b/src/main/java/com/android/tools/r8/synthesis/SyntheticItems.java
@@ -483,8 +483,7 @@
       AppView<?> appView,
       DexType type) {
     DexType rewrittenContextType =
-        appView.rewritePrefix.rewrittenContextType(
-            outerContext.getSynthesizingContextType(), appView);
+        appView.rewritePrefix.rewrittenContextType(outerContext.getSynthesizingContextType());
     if (rewrittenContextType == null) {
       return;
     }
diff --git a/src/main/java/com/android/tools/r8/utils/InternalOptions.java b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
index 0a2b3fc..3261b5b 100644
--- a/src/main/java/com/android/tools/r8/utils/InternalOptions.java
+++ b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
@@ -53,7 +53,6 @@
 import com.android.tools.r8.inspector.internal.InspectorImpl;
 import com.android.tools.r8.ir.code.IRCode;
 import com.android.tools.r8.ir.desugar.PrefixRewritingMapper;
-import com.android.tools.r8.ir.desugar.PrefixRewritingMapper.DesugarPrefixRewritingMapper;
 import com.android.tools.r8.ir.desugar.PrefixRewritingMapper.MachineDesugarPrefixRewritingMapper;
 import com.android.tools.r8.ir.desugar.desugaredlibrary.humanspecification.HumanDesugaredLibrarySpecification;
 import com.android.tools.r8.ir.desugar.desugaredlibrary.legacyspecification.LegacyDesugaredLibrarySpecification;
@@ -936,18 +935,9 @@
       MachineDesugaredLibrarySpecification.empty();
 
   public PrefixRewritingMapper getPrefixRewritingMapper() {
-    if (machineDesugaredLibrarySpecification != null) {
-      if (desugaredLibrarySpecification.getRewritePrefix().isEmpty()) {
-        assert machineDesugaredLibrarySpecification.getRewriteType().isEmpty();
-        return PrefixRewritingMapper.empty();
-      }
-      return new MachineDesugarPrefixRewritingMapper(
-          // This mapper is used for assertions only (prior behavior is identical to the new one).
-          new DesugarPrefixRewritingMapper(
-              desugaredLibrarySpecification.getRewritePrefix(), dexItemFactory(), true),
-          machineDesugaredLibrarySpecification);
-    }
-    return desugaredLibrarySpecification.getPrefixRewritingMapper();
+    return machineDesugaredLibrarySpecification.getRewriteType().isEmpty()
+        ? PrefixRewritingMapper.empty()
+        : new MachineDesugarPrefixRewritingMapper(machineDesugaredLibrarySpecification);
   }
 
   public boolean relocatorCompilation = false;
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/JavaTimeTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/JavaTimeTest.java
index 6598d6b..d78df31 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/JavaTimeTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/JavaTimeTest.java
@@ -144,16 +144,22 @@
             .map(TypeSubject::toString)
             .collect(Collectors.toSet());
     assertEquals(expectedCatchGuards, foundCatchGuards);
-
-    if (isR8) {
-      assertThat(
-          inspector.clazz(TemporalAccessorImpl.class).uniqueMethodWithFinalName("query"),
-          not(isPresent()));
-    } else {
+    if (!(parameters
+            .getApiLevel()
+            .isGreaterThanOrEqualTo(TestBase.apiLevelWithDefaultInterfaceMethodsSupport())
+        && isR8)) {
       assertThat(
           inspector.clazz(TemporalAccessorImplSub.class).uniqueMethodWithFinalName("query"),
           CodeMatchers.invokesMethod(
-              null, TemporalAccessorImpl.class.getTypeName(), "query", null));
+              null, inspector.clazz(TemporalAccessorImpl.class).getFinalName(), "query", null));
+    } else {
+      String holder =
+          requiresTimeDesugaring(parameters)
+              ? "j$.time.temporal.TemporalAccessor"
+              : "java.time.temporal.TemporalAccessor";
+      assertThat(
+          inspector.clazz(TemporalAccessorImplSub.class).uniqueMethodWithFinalName("query"),
+          CodeMatchers.invokesMethod(null, holder, "query", null));
     }
     if (parameters
         .getApiLevel()
@@ -163,9 +169,7 @@
           not(isPresent()));
     } else {
       assertThat(
-          inspector
-              .clazz(isR8 ? TemporalAccessorImplSub.class : TemporalAccessorImpl.class)
-              .uniqueMethodWithFinalName("query"),
+          inspector.clazz(TemporalAccessorImpl.class).uniqueMethodWithFinalName("query"),
           CodeMatchers.invokesMethod(
               null, "j$.time.temporal.TemporalAccessor$-CC", "$default$query", null));
     }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java
index 5367c8b..9b1804e 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java
@@ -60,7 +60,7 @@
     try {
       options.setDesugaredLibrarySpecificationForTesting(
           new LegacyDesugaredLibrarySpecification(
-              LegacyTopLevelFlags.testing(), rewritingFlags, true, options.itemFactory),
+              LegacyTopLevelFlags.testing(), rewritingFlags, true),
           ToolHelper.getDesugarJDKLibs(),
           ToolHelper.getAndroidJar(AndroidApiLevel.R));
     } catch (IOException e) {
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/AccessModeConversionTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/AccessModeConversionTest.java
index 21ad64e..a57140b 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/AccessModeConversionTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/AccessModeConversionTest.java
@@ -66,7 +66,7 @@
             .build();
     LegacyDesugaredLibrarySpecification specification =
         new LegacyDesugaredLibrarySpecification(
-            LegacyTopLevelFlags.testing(), rewritingFlags, l8Compilation, options.itemFactory);
+            LegacyTopLevelFlags.testing(), rewritingFlags, l8Compilation);
     setDesugaredLibrarySpecificationForTesting(options, specification);
   }