Remove uses of definitionFor(DexMethod) in minifier

Change-Id: Ic557e6ef5565c0f68263f6fd5ab19cd09e990626
diff --git a/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java b/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java
index c12f3f4..670344c 100644
--- a/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java
+++ b/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java
@@ -250,6 +250,14 @@
     assert parameterAnnotationsList != null;
   }
 
+  public DexType getHolderType() {
+    return getReference().holder;
+  }
+
+  public DexString getName() {
+    return getReference().name;
+  }
+
   public DexMethod getReference() {
     return method;
   }
diff --git a/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java b/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java
index bbefa18..060f171 100644
--- a/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java
@@ -20,6 +20,7 @@
 import com.google.common.base.Equivalence;
 import com.google.common.base.Equivalence.Wrapper;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -110,16 +111,13 @@
       this.iface = iface;
     }
 
-    DexString getReservedName(DexMethod method) {
+    DexString getReservedName(DexEncodedMethod method) {
       // If an interface is kept and we are using applymapping, the renamed name for this method
       // is tracked on this level.
       if (appView.options().getProguardConfiguration().hasApplyMappingFile()) {
-        DexEncodedMethod encodedMethod = appView.definitionFor(method);
-        if (encodedMethod != null) {
-          DexString reservedName = minifierState.getReservedName(encodedMethod, iface);
-          if (reservedName != null) {
-            return reservedName;
-          }
+        DexString reservedName = minifierState.getReservedName(method, iface);
+        if (reservedName != null) {
+          return reservedName;
         }
       }
       // Otherwise, we just search the hierarchy for the first identity reservation since
@@ -131,18 +129,18 @@
                   Set<DexString> reservedNamesFor =
                       minifierState
                           .getReservationState(reservationType)
-                          .getReservedNamesFor(method);
+                          .getReservedNamesFor(method.getReference());
                   assert reservedNamesFor == null || !reservedNamesFor.isEmpty();
-                  if (reservedNamesFor != null && reservedNamesFor.contains(method.name)) {
+                  if (reservedNamesFor != null && reservedNamesFor.contains(method.getName())) {
                     return true;
                   }
                 }
                 return null;
               });
-      return isReserved == null ? null : method.name;
+      return isReserved == null ? null : method.getName();
     }
 
-    void reserveName(DexString reservedName, DexMethod method) {
+    void reserveName(DexString reservedName, DexEncodedMethod method) {
       forAll(
           s -> {
             s.reservationTypes.forEach(
@@ -153,13 +151,13 @@
           });
     }
 
-    boolean isAvailable(DexString candidate, DexMethod method) {
+    boolean isAvailable(DexString candidate, DexEncodedMethod method) {
       Boolean result =
           forAny(
               s -> {
                 for (DexType resType : s.reservationTypes) {
                   MethodNamingState<?> state = minifierState.getNamingState(resType);
-                  if (!state.isAvailable(candidate, method)) {
+                  if (!state.isAvailable(candidate, method.getReference())) {
                     return false;
                   }
                 }
@@ -168,15 +166,11 @@
       return result == null ? true : result;
     }
 
-    void addRenaming(DexString newName, DexMethod method) {
+    void addRenaming(DexString newName, DexEncodedMethod method) {
       forAll(
-          s -> {
-            s.reservationTypes.forEach(
-                resType -> {
-                  MethodNamingState<?> state = minifierState.getNamingState(resType);
-                  state.addRenaming(newName, method);
-                });
-          });
+          s ->
+              s.reservationTypes.forEach(
+                  resType -> minifierState.getNamingState(resType).addRenaming(newName, method)));
     }
 
     <T> void forAll(Consumer<InterfaceReservationState> action) {
@@ -241,17 +235,18 @@
   class InterfaceMethodGroupState implements Comparable<InterfaceMethodGroupState> {
 
     private final Set<DexCallSite> callSites = new HashSet<>();
-    private final Map<DexMethod, Set<InterfaceReservationState>> methodStates = new HashMap<>();
-    private final List<DexMethod> callSiteCollidingMethods = new ArrayList<>();
+    private final Map<DexEncodedMethod, Set<InterfaceReservationState>> methodStates =
+        new HashMap<>();
+    private final List<DexEncodedMethod> callSiteCollidingMethods = new ArrayList<>();
 
-    void addState(DexMethod method, InterfaceReservationState interfaceState) {
+    void addState(DexEncodedMethod method, InterfaceReservationState interfaceState) {
       methodStates.computeIfAbsent(method, m -> new HashSet<>()).add(interfaceState);
     }
 
     void appendMethodGroupState(InterfaceMethodGroupState state) {
       callSites.addAll(state.callSites);
       callSiteCollidingMethods.addAll(state.callSiteCollidingMethods);
-      for (DexMethod key : state.methodStates.keySet()) {
+      for (DexEncodedMethod key : state.methodStates.keySet()) {
         methodStates.computeIfAbsent(key, k -> new HashSet<>()).addAll(state.methodStates.get(key));
       }
     }
@@ -269,14 +264,14 @@
       // It is perfectly fine to have multiple reserved names inside a group. If we have an identity
       // reservation, we have to prioritize that over the others, otherwise we just propose the
       // first ordered reserved name since we do not allow overwriting the name.
-      List<DexMethod> sortedMethods = Lists.newArrayList(methodStates.keySet());
-      sortedMethods.sort(DexMethod::slowCompareTo);
+      List<DexEncodedMethod> sortedMethods = Lists.newArrayList(methodStates.keySet());
+      sortedMethods.sort((x, y) -> x.getReference().slowCompareTo(y.getReference()));
       DexString reservedName = null;
-      for (DexMethod method : sortedMethods) {
+      for (DexEncodedMethod method : sortedMethods) {
         for (InterfaceReservationState state : methodStates.get(method)) {
           DexString stateReserved = state.getReservedName(method);
-          if (stateReserved == method.name) {
-            return method.name;
+          if (stateReserved == method.getName()) {
+            return method.getName();
           } else if (stateReserved != null) {
             reservedName = stateReserved;
           }
@@ -321,7 +316,7 @@
           });
     }
 
-    void forEachState(BiConsumer<DexMethod, InterfaceReservationState> action) {
+    void forEachState(BiConsumer<DexEncodedMethod, InterfaceReservationState> action) {
       forAnyState(
           (s, i) -> {
             action.accept(s, i);
@@ -329,9 +324,10 @@
           });
     }
 
-    <T> T forAnyState(BiFunction<DexMethod, InterfaceReservationState, T> callback) {
+    <T> T forAnyState(BiFunction<DexEncodedMethod, InterfaceReservationState, T> callback) {
       T returnValue;
-      for (Map.Entry<DexMethod, Set<InterfaceReservationState>> entry : methodStates.entrySet()) {
+      for (Map.Entry<DexEncodedMethod, Set<InterfaceReservationState>> entry :
+          methodStates.entrySet()) {
         for (InterfaceReservationState state : entry.getValue()) {
           returnValue = callback.apply(entry.getKey(), state);
           if (returnValue != null) {
@@ -342,7 +338,7 @@
       return null;
     }
 
-    boolean containsReservation(DexMethod method, DexType reservationType) {
+    boolean containsReservation(DexEncodedMethod method, DexType reservationType) {
       Set<InterfaceReservationState> states = methodStates.get(method);
       if (states != null) {
         for (InterfaceReservationState state : states) {
@@ -363,12 +359,14 @@
 
   private final AppView<AppInfoWithLiveness> appView;
   private final Equivalence<DexMethod> equivalence;
+  private final Equivalence<DexEncodedMethod> definitionEquivalence;
   private final MethodNameMinifier.State minifierState;
 
   private final Map<DexCallSite, DexString> callSiteRenamings = new IdentityHashMap<>();
 
   /** A map from DexMethods to all the states linked to interfaces they appear in. */
-  private final Map<Wrapper<DexMethod>, InterfaceMethodGroupState> globalStateMap = new HashMap<>();
+  private final Map<Wrapper<DexEncodedMethod>, InterfaceMethodGroupState> globalStateMap =
+      new HashMap<>();
 
   /** A map for caching all interface states. */
   private final Map<DexType, InterfaceReservationState> interfaceStateMap = new HashMap<>();
@@ -380,9 +378,21 @@
         appView.options().getProguardConfiguration().isOverloadAggressively()
             ? MethodSignatureEquivalence.get()
             : MethodJavaSignatureEquivalence.get();
+    this.definitionEquivalence =
+        new Equivalence<DexEncodedMethod>() {
+          @Override
+          protected boolean doEquivalent(DexEncodedMethod method, DexEncodedMethod other) {
+            return equivalence.equivalent(method.getReference(), other.getReference());
+          }
+
+          @Override
+          protected int doHash(DexEncodedMethod method) {
+            return equivalence.hash(method.getReference());
+          }
+        };
   }
 
-  private Comparator<Wrapper<DexMethod>> getDefaultInterfaceMethodOrdering() {
+  private Comparator<Wrapper<DexEncodedMethod>> getDefaultInterfaceMethodOrdering() {
     return Comparator.comparing(globalStateMap::get);
   }
 
@@ -419,10 +429,10 @@
       InterfaceReservationState inheritanceState = interfaceStateMap.get(iface.type);
       assert inheritanceState != null;
       for (DexEncodedMethod method : iface.methods()) {
-        Wrapper<DexMethod> key = equivalence.wrap(method.method);
+        Wrapper<DexEncodedMethod> key = definitionEquivalence.wrap(method);
         globalStateMap
             .computeIfAbsent(key, k -> new InterfaceMethodGroupState())
-            .addState(method.method, inheritanceState);
+            .addState(method, inheritanceState);
       }
     }
     timing.end();
@@ -439,11 +449,11 @@
     // Note that if the input does not use multi-interface lambdas unificationParent will remain
     // empty.
     timing.begin("Union-find");
-    DisjointSets<Wrapper<DexMethod>> unification = new DisjointSets<>();
+    DisjointSets<Wrapper<DexEncodedMethod>> unification = new DisjointSets<>();
 
     liveCallSites.forEach(
         callSite -> {
-          Set<Wrapper<DexMethod>> callSiteMethods = new HashSet<>();
+          Set<Wrapper<DexEncodedMethod>> callSiteMethods = new HashSet<>();
           // Don't report errors, as the set of call sites is a conservative estimate, and can
           // refer to interfaces which has been removed.
           Set<DexEncodedMethod> implementedMethods =
@@ -452,7 +462,7 @@
             return;
           }
           for (DexEncodedMethod method : implementedMethods) {
-            Wrapper<DexMethod> wrapped = equivalence.wrap(method.method);
+            Wrapper<DexEncodedMethod> wrapped = definitionEquivalence.wrap(method);
             InterfaceMethodGroupState groupState = globalStateMap.get(wrapped);
             assert groupState != null : wrapped;
             groupState.addCallSite(callSite);
@@ -474,16 +484,15 @@
               assert iface.isInterface();
               for (DexEncodedMethod implementedMethod : implementedMethods) {
                 for (DexEncodedMethod virtualMethod : iface.virtualMethods()) {
-                  boolean differentName =
-                      !implementedMethod.method.name.equals(virtualMethod.method.name);
+                  boolean differentName = implementedMethod.getName() != virtualMethod.getName();
                   if (differentName
                       && MethodJavaSignatureEquivalence.getEquivalenceIgnoreName()
                           .equivalent(implementedMethod.method, virtualMethod.method)) {
                     InterfaceMethodGroupState interfaceMethodGroupState =
                         globalStateMap.computeIfAbsent(
-                            equivalence.wrap(implementedMethod.method),
+                            definitionEquivalence.wrap(implementedMethod),
                             k -> new InterfaceMethodGroupState());
-                    interfaceMethodGroupState.callSiteCollidingMethods.add(virtualMethod.method);
+                    interfaceMethodGroupState.callSiteCollidingMethods.add(virtualMethod);
                   }
                 }
               }
@@ -491,9 +500,9 @@
           }
           if (callSiteMethods.size() > 1) {
             // Implemented interfaces have different protos. Unify them.
-            Wrapper<DexMethod> mainKey = callSiteMethods.iterator().next();
-            Wrapper<DexMethod> representative = unification.findOrMakeSet(mainKey);
-            for (Wrapper<DexMethod> key : callSiteMethods) {
+            Wrapper<DexEncodedMethod> mainKey = callSiteMethods.iterator().next();
+            Wrapper<DexEncodedMethod> representative = unification.findOrMakeSet(mainKey);
+            for (Wrapper<DexEncodedMethod> key : callSiteMethods) {
               unification.unionWithMakeSet(representative, key);
             }
           }
@@ -504,14 +513,15 @@
     // We now have roots for all unions. Add all of the states for the groups to the method state
     // for the unions to allow consistent naming across different protos.
     timing.begin("States for union");
-    Map<Wrapper<DexMethod>, Set<Wrapper<DexMethod>>> unions = unification.collectSets();
+    Map<Wrapper<DexEncodedMethod>, Set<Wrapper<DexEncodedMethod>>> unions =
+        unification.collectSets();
 
-    for (Wrapper<DexMethod> wrapped : unions.keySet()) {
+    for (Wrapper<DexEncodedMethod> wrapped : unions.keySet()) {
       InterfaceMethodGroupState groupState = globalStateMap.get(wrapped);
       assert groupState != null;
 
-      for (Wrapper<DexMethod> groupedMethod : unions.get(wrapped)) {
-        DexMethod method = groupedMethod.get();
+      for (Wrapper<DexEncodedMethod> groupedMethod : unions.get(wrapped)) {
+        DexEncodedMethod method = groupedMethod.get();
         assert method != null;
         groupState.appendMethodGroupState(globalStateMap.get(groupedMethod));
       }
@@ -522,7 +532,7 @@
     // Filter out the groups that is included both in the unification and in the map. We sort the
     // methods by the number of dependent states, so that we use short names for method that are
     // referenced in many places.
-    List<Wrapper<DexMethod>> interfaceMethodGroups =
+    List<Wrapper<DexEncodedMethod>> interfaceMethodGroups =
         globalStateMap.keySet().stream()
             .filter(unification::isRepresentativeOrNotPresent)
             .sorted(
@@ -540,8 +550,8 @@
     timing.begin("Reserve in groups");
     // It is important that this entire phase is run before given new names, to ensure all
     // reservations are propagated to all naming states.
-    List<Wrapper<DexMethod>> nonReservedMethodGroups = new ArrayList<>();
-    for (Wrapper<DexMethod> interfaceMethodGroup : interfaceMethodGroups) {
+    List<Wrapper<DexEncodedMethod>> nonReservedMethodGroups = new ArrayList<>();
+    for (Wrapper<DexEncodedMethod> interfaceMethodGroup : interfaceMethodGroups) {
       InterfaceMethodGroupState groupState = globalStateMap.get(interfaceMethodGroup);
       assert groupState != null;
       DexString reservedName = groupState.getReservedName();
@@ -559,7 +569,7 @@
     timing.end();
 
     timing.begin("Rename in groups");
-    for (Wrapper<DexMethod> interfaceMethodGroup : nonReservedMethodGroups) {
+    for (Wrapper<DexEncodedMethod> interfaceMethodGroup : nonReservedMethodGroups) {
       InterfaceMethodGroupState groupState = globalStateMap.get(interfaceMethodGroup);
       assert groupState != null;
       assert groupState.getReservedName() == null;
@@ -567,11 +577,11 @@
       assert newName != null;
       Set<String> loggingFilter = appView.options().extensiveInterfaceMethodMinifierLoggingFilter;
       if (!loggingFilter.isEmpty()) {
-        Set<DexMethod> sourceMethods = groupState.methodStates.keySet();
+        Set<DexEncodedMethod> sourceMethods = groupState.methodStates.keySet();
         if (sourceMethods.stream()
-            .map(DexMethod::toSourceString)
+            .map(DexEncodedMethod::toSourceString)
             .anyMatch(loggingFilter::contains)) {
-          print(interfaceMethodGroup.get(), sourceMethods, System.out);
+          print(interfaceMethodGroup.get().getReference(), sourceMethods, System.out);
         }
       }
       for (DexCallSite callSite : groupState.callSites) {
@@ -582,20 +592,20 @@
 
     // After all naming is completed for callsites, we must ensure to rename all interface methods
     // that can collide with the callsite method name.
-    for (Wrapper<DexMethod> interfaceMethodGroup : nonReservedMethodGroups) {
+    for (Wrapper<DexEncodedMethod> interfaceMethodGroup : nonReservedMethodGroups) {
       InterfaceMethodGroupState groupState = globalStateMap.get(interfaceMethodGroup);
       if (groupState.callSiteCollidingMethods.isEmpty()) {
         continue;
       }
-      DexMethod key = interfaceMethodGroup.get();
-      MethodNamingState<?> keyNamingState = minifierState.getNamingState(key.holder);
+      DexEncodedMethod key = interfaceMethodGroup.get();
+      MethodNamingState<?> keyNamingState = minifierState.getNamingState(key.getHolderType());
       DexString existingRenaming = keyNamingState.newOrReservedNameFor(key);
       assert existingRenaming != null;
-      for (DexMethod collidingMethod : groupState.callSiteCollidingMethods) {
+      for (DexEncodedMethod collidingMethod : groupState.callSiteCollidingMethods) {
         DexString newNameInGroup = newNameInGroup(collidingMethod, keyNamingState, groupState);
         minifierState.putRenaming(collidingMethod, newNameInGroup);
         MethodNamingState<?> methodNamingState =
-            minifierState.getNamingState(collidingMethod.holder);
+            minifierState.getNamingState(collidingMethod.getReference().holder);
         methodNamingState.addRenaming(newNameInGroup, collidingMethod);
         keyNamingState.addRenaming(newNameInGroup, collidingMethod);
       }
@@ -605,11 +615,11 @@
     timing.end(); // end compute timing
   }
 
-  private DexString assignNewName(DexMethod method, InterfaceMethodGroupState groupState) {
+  private DexString assignNewName(DexEncodedMethod method, InterfaceMethodGroupState groupState) {
     assert groupState.getReservedName() == null;
     assert groupState.methodStates.containsKey(method);
-    assert groupState.containsReservation(method, method.holder);
-    MethodNamingState<?> namingState = minifierState.getNamingState(method.holder);
+    assert groupState.containsReservation(method, method.getHolderType());
+    MethodNamingState<?> namingState = minifierState.getNamingState(method.getHolderType());
     // Check if the name is available in all states.
     DexString newName =
         namingState.newOrReservedNameFor(
@@ -619,7 +629,9 @@
   }
 
   private DexString newNameInGroup(
-      DexMethod method, MethodNamingState<?> namingState, InterfaceMethodGroupState groupState) {
+      DexEncodedMethod method,
+      MethodNamingState<?> namingState,
+      InterfaceMethodGroupState groupState) {
     // Check if the name is available in all states.
     return namingState.nextName(method, (candidate, ignore) -> groupState.isAvailable(candidate));
   }
@@ -655,11 +667,11 @@
             });
   }
 
-  private boolean verifyAllCallSitesAreRepresentedIn(List<Wrapper<DexMethod>> groups) {
-    Set<Wrapper<DexMethod>> unifiedMethods = new HashSet<>(groups);
+  private boolean verifyAllCallSitesAreRepresentedIn(List<Wrapper<DexEncodedMethod>> groups) {
+    Set<Wrapper<DexEncodedMethod>> unifiedMethods = new HashSet<>(groups);
     Set<DexCallSite> unifiedSeen = new HashSet<>();
     Set<DexCallSite> seen = new HashSet<>();
-    for (Map.Entry<Wrapper<DexMethod>, InterfaceMethodGroupState> state :
+    for (Map.Entry<Wrapper<DexEncodedMethod>, InterfaceMethodGroupState> state :
         globalStateMap.entrySet()) {
       for (DexCallSite callSite : state.getValue().callSites) {
         seen.add(callSite);
@@ -674,13 +686,13 @@
     return true;
   }
 
-  private boolean verifyAllMethodsAreRepresentedIn(List<Wrapper<DexMethod>> groups) {
-    Set<Wrapper<DexMethod>> unifiedMethods = new HashSet<>(groups);
-    Set<DexMethod> unifiedSeen = new HashSet<>();
-    Set<DexMethod> seen = new HashSet<>();
-    for (Map.Entry<Wrapper<DexMethod>, InterfaceMethodGroupState> state :
+  private boolean verifyAllMethodsAreRepresentedIn(List<Wrapper<DexEncodedMethod>> groups) {
+    Set<Wrapper<DexEncodedMethod>> unifiedMethods = new HashSet<>(groups);
+    Set<DexEncodedMethod> unifiedSeen = Sets.newIdentityHashSet();
+    Set<DexEncodedMethod> seen = Sets.newIdentityHashSet();
+    for (Map.Entry<Wrapper<DexEncodedMethod>, InterfaceMethodGroupState> state :
         globalStateMap.entrySet()) {
-      for (DexMethod method : state.getValue().methodStates.keySet()) {
+      for (DexEncodedMethod method : state.getValue().methodStates.keySet()) {
         seen.add(method);
         if (unifiedMethods.contains(state.getKey())) {
           boolean added = unifiedSeen.add(method);
@@ -693,12 +705,12 @@
     return true;
   }
 
-  private void print(DexMethod method, Set<DexMethod> sourceMethods, PrintStream out) {
+  private void print(DexMethod method, Set<DexEncodedMethod> sourceMethods, PrintStream out) {
     out.println("-----------------------------------------------------------------------");
     out.println("assignNameToInterfaceMethod(`" + method.toSourceString() + "`)");
     out.println("-----------------------------------------------------------------------");
     out.println("Source methods:");
-    for (DexMethod sourceMethod : sourceMethods) {
+    for (DexEncodedMethod sourceMethod : sourceMethods) {
       out.println("  " + sourceMethod.toSourceString());
     }
     out.println("States:");
diff --git a/src/main/java/com/android/tools/r8/naming/MemberNamingStrategy.java b/src/main/java/com/android/tools/r8/naming/MemberNamingStrategy.java
index cddbea8..58a4dc6 100644
--- a/src/main/java/com/android/tools/r8/naming/MemberNamingStrategy.java
+++ b/src/main/java/com/android/tools/r8/naming/MemberNamingStrategy.java
@@ -15,7 +15,7 @@
 public interface MemberNamingStrategy {
 
   DexString next(
-      DexMethod method,
+      DexEncodedMethod method,
       InternalNamingState internalState,
       BiPredicate<DexString, DexMethod> isAvailable);
 
diff --git a/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java b/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
index cdb9166..7b3e763 100644
--- a/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
@@ -89,8 +89,8 @@
   // from the method name minifier to the interface method name minifier.
   class State {
 
-    void putRenaming(DexMethod key, DexString value) {
-      renaming.put(key, value);
+    void putRenaming(DexEncodedMethod key, DexString value) {
+      renaming.put(key.getReference(), value);
     }
 
     MethodReservationState<?> getReservationState(DexType type) {
@@ -221,21 +221,21 @@
   }
 
   private void assignNameToMethod(
-      DexClass holder, DexEncodedMethod encodedMethod, MethodNamingState<?> state) {
-    if (encodedMethod.accessFlags.isConstructor()) {
+      DexClass holder, DexEncodedMethod method, MethodNamingState<?> state) {
+    if (method.isInitializer()) {
       return;
     }
     // The strategy may have an explicit naming for this member which we query first. It may be that
     // the strategy will return the identity name, for which we have to look into a previous
     // renaming tracked by the state.
-    DexString newName = strategy.getReservedName(encodedMethod, holder);
-    if (newName == null || newName == encodedMethod.method.name) {
-      newName = state.newOrReservedNameFor(encodedMethod.method);
+    DexString newName = strategy.getReservedName(method, holder);
+    if (newName == null || newName == method.getName()) {
+      newName = state.newOrReservedNameFor(method);
     }
-    if (encodedMethod.method.name != newName) {
-      renaming.put(encodedMethod.method, newName);
+    if (method.getName() != newName) {
+      renaming.put(method.getReference(), newName);
     }
-    state.addRenaming(newName, encodedMethod.method);
+    state.addRenaming(newName, method);
   }
 
   private void reserveNamesInClasses() {
@@ -280,7 +280,7 @@
       for (DexEncodedMethod method : shuffleMethods(holder.methods(), appView.options())) {
         DexString reservedName = strategy.getReservedName(method, holder);
         if (reservedName != null) {
-          state.reserveName(reservedName, method.method);
+          state.reserveName(reservedName, method);
         }
       }
     }
diff --git a/src/main/java/com/android/tools/r8/naming/MethodNamingState.java b/src/main/java/com/android/tools/r8/naming/MethodNamingState.java
index 85c2f0f..5aa368b 100644
--- a/src/main/java/com/android/tools/r8/naming/MethodNamingState.java
+++ b/src/main/java/com/android/tools/r8/naming/MethodNamingState.java
@@ -3,6 +3,7 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8.naming;
 
+import com.android.tools.r8.graph.DexEncodedMethod;
 import com.android.tools.r8.graph.DexMethod;
 import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.naming.MethodNamingState.InternalNewNameState;
@@ -44,37 +45,38 @@
         this, this.keyTransform, this.namingStrategy, frontierReservationState);
   }
 
-  DexString newOrReservedNameFor(DexMethod method) {
+  DexString newOrReservedNameFor(DexEncodedMethod method) {
     return newOrReservedNameFor(method, this::isAvailable);
   }
 
-  DexString newOrReservedNameFor(DexMethod method, BiPredicate<DexString, DexMethod> isAvailable) {
-    DexString newName = getAssignedName(method);
+  DexString newOrReservedNameFor(
+      DexEncodedMethod method, BiPredicate<DexString, DexMethod> isAvailable) {
+    DexString newName = getAssignedName(method.getReference());
     if (newName != null) {
       return newName;
     }
-    Set<DexString> reservedNamesFor = reservationState.getReservedNamesFor(method);
+    Set<DexString> reservedNamesFor = reservationState.getReservedNamesFor(method.getReference());
     // Reservations with applymapping can cause multiple reserved names added to the frontier. In
     // that case, the strategy will return the correct one.
     if (reservedNamesFor != null && reservedNamesFor.size() == 1) {
       DexString candidate = reservedNamesFor.iterator().next();
-      if (isAvailable(candidate, method)) {
+      if (isAvailable(candidate, method.getReference())) {
         return candidate;
       }
     }
     return nextName(method, isAvailable);
   }
 
-  DexString nextName(DexMethod method, BiPredicate<DexString, DexMethod> isAvailable) {
-    InternalNewNameState internalState = getOrCreateInternalState(method);
+  DexString nextName(DexEncodedMethod method, BiPredicate<DexString, DexMethod> isAvailable) {
+    InternalNewNameState internalState = getOrCreateInternalState(method.getReference());
     DexString newName = namingStrategy.next(method, internalState, isAvailable);
     assert newName != null;
     return newName;
   }
 
-  void addRenaming(DexString newName, DexMethod method) {
-    InternalNewNameState internalState = getOrCreateInternalState(method);
-    internalState.addRenaming(newName, method);
+  void addRenaming(DexString newName, DexEncodedMethod method) {
+    InternalNewNameState internalState = getOrCreateInternalState(method.getReference());
+    internalState.addRenaming(newName, method.getReference());
   }
 
   boolean isAvailable(DexString candidate, DexMethod method) {
diff --git a/src/main/java/com/android/tools/r8/naming/MethodReservationState.java b/src/main/java/com/android/tools/r8/naming/MethodReservationState.java
index 2ce35f9..b963b3f 100644
--- a/src/main/java/com/android/tools/r8/naming/MethodReservationState.java
+++ b/src/main/java/com/android/tools/r8/naming/MethodReservationState.java
@@ -4,6 +4,7 @@
 
 package com.android.tools.r8.naming;
 
+import com.android.tools.r8.graph.DexEncodedMethod;
 import com.android.tools.r8.graph.DexMethod;
 import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.naming.MethodReservationState.InternalReservationState;
@@ -36,9 +37,9 @@
     return new MethodReservationState<>(this, this.keyTransform);
   }
 
-  void reserveName(DexString reservedName, DexMethod method) {
+  void reserveName(DexString reservedName, DexEncodedMethod method) {
     try {
-      getOrCreateInternalState(method).reserveName(method, reservedName);
+      getOrCreateInternalState(method.getReference()).reserveName(method, reservedName);
     } catch (AssertionError err) {
       throw new RuntimeException(
           String.format(
@@ -91,13 +92,14 @@
       return originalToReservedNames.get(MethodSignatureEquivalence.get().wrap(method));
     }
 
-    void reserveName(DexMethod method, DexString name) {
+    void reserveName(DexEncodedMethod method, DexString name) {
       if (reservedNames == null) {
         assert originalToReservedNames == null;
         originalToReservedNames = new HashMap<>();
         reservedNames = new HashSet<>();
       }
-      final Wrapper<DexMethod> wrapped = MethodSignatureEquivalence.get().wrap(method);
+      final Wrapper<DexMethod> wrapped =
+          MethodSignatureEquivalence.get().wrap(method.getReference());
       originalToReservedNames.computeIfAbsent(wrapped, ignore -> new HashSet<>()).add(name);
       reservedNames.add(name);
     }
diff --git a/src/main/java/com/android/tools/r8/naming/Minifier.java b/src/main/java/com/android/tools/r8/naming/Minifier.java
index d0f0381..182de8d 100644
--- a/src/main/java/com/android/tools/r8/naming/Minifier.java
+++ b/src/main/java/com/android/tools/r8/naming/Minifier.java
@@ -225,16 +225,15 @@
 
     @Override
     public DexString next(
-        DexMethod method,
+        DexEncodedMethod method,
         InternalNamingState internalState,
         BiPredicate<DexString, DexMethod> isAvailable) {
-      assert checkAllowMemberRenaming(method.holder);
-      DexEncodedMethod encodedMethod = appView.definitionFor(method);
-      boolean isDirectOrStatic = encodedMethod.isDirectMethod() || encodedMethod.isStatic();
+      assert checkAllowMemberRenaming(method.getHolderType());
+      boolean isDirectOrStatic = method.isDirectMethod() || method.isStatic();
       DexString candidate;
       do {
         candidate = getNextName(internalState, isDirectOrStatic);
-      } while (!isAvailable.test(candidate, method));
+      } while (!isAvailable.test(candidate, method.getReference()));
       return candidate;
     }
 
diff --git a/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java b/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java
index e720b30..515d21f 100644
--- a/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java
@@ -188,11 +188,11 @@
       Set<DexReference> notMappedReferences,
       SubtypingInfo subtypingInfo) {
     ClassNamingForMapApplier classNaming = seedMapper.getClassNaming(type);
-    DexClass dexClass = appView.definitionFor(type);
+    DexClass clazz = appView.definitionFor(type);
 
     // Keep track of classes that needs to get renamed.
-    if (dexClass != null && (classNaming != null || dexClass.isProgramClass())) {
-      mappedClasses.add(dexClass);
+    if (clazz != null && (classNaming != null || clazz.isProgramClass())) {
+      mappedClasses.add(clazz);
     }
 
     Map<DexReference, MemberNaming> nonPrivateMembers = new IdentityHashMap<>();
@@ -205,7 +205,7 @@
           memberNaming -> addMemberNamings(type, memberNaming, nonPrivateMembers, false));
     } else {
       // We have to ensure we do not rename to an existing member, that cannot be renamed.
-      if (dexClass == null || !appView.options().isMinifying()) {
+      if (clazz == null || !appView.options().isMinifying()) {
         notMappedReferences.add(type);
       } else if (appView.options().isMinifying()
           && appView.rootSet().mayNotBeMinified(type, appView)) {
@@ -222,10 +222,10 @@
           if (!memberNames.containsKey(parentReferenceOnCurrentType)) {
             addMemberNaming(
                 parentReferenceOnCurrentType, parentMembers.get(key), additionalMethodNamings);
-          } else {
-            DexEncodedMethod encodedMethod = appView.definitionFor(parentReferenceOnCurrentType);
-            assert encodedMethod == null
-                || encodedMethod.accessFlags.isStatic()
+          } else if (clazz != null) {
+            DexEncodedMethod method = clazz.lookupMethod(parentReferenceOnCurrentType);
+            assert method == null
+                || method.isStatic()
                 || memberNames
                     .get(parentReferenceOnCurrentType)
                     .getRenamedName()
@@ -245,12 +245,12 @@
       }
     }
 
-    if (dexClass != null) {
+    if (clazz != null) {
       // If a class is marked as abstract it is allowed to not implement methods from interfaces
       // thus the map will not contain a mapping. Also, if an interface is defined in the library
       // and the class is in the program, we have to build up the correct names to reserve them.
-      if (dexClass.isProgramClass() || dexClass.isAbstract()) {
-        addNonPrivateInterfaceMappings(type, nonPrivateMembers, dexClass.interfaces.values);
+      if (clazz.isProgramClass() || clazz.isAbstract()) {
+        addNonPrivateInterfaceMappings(type, nonPrivateMembers, clazz.interfaces.values);
       }
     }
 
@@ -293,8 +293,9 @@
       DexMethod originalMethod = ((MethodSignature) signature).toDexMethod(factory, type);
       addMemberNaming(
           originalMethod, memberNaming, addToAdditionalMaps ? additionalMethodNamings : null);
-      DexEncodedMethod encodedMethod = appView.definitionFor(originalMethod);
-      if (encodedMethod == null || !encodedMethod.accessFlags.isPrivate()) {
+      DexClass holder = appView.definitionForHolder(originalMethod);
+      DexEncodedMethod definition = originalMethod.lookupOnClass(holder);
+      if (definition == null || !definition.accessFlags.isPrivate()) {
         nonPrivateMembers.put(originalMethod, memberNaming);
       }
     } else {
@@ -466,12 +467,12 @@
 
     @Override
     public DexString next(
-        DexMethod reference,
+        DexEncodedMethod method,
         InternalNamingState internalState,
         BiPredicate<DexString, DexMethod> isAvailable) {
+      DexMethod reference = method.getReference();
       DexClass holder = appView.definitionForHolder(reference);
       assert holder != null;
-      DexEncodedMethod method = holder.lookupMethod(reference);
       DexString reservedName = getReservedName(method, reference.name, holder);
       DexString nextName;
       if (reservedName != null) {
@@ -482,7 +483,7 @@
       } else {
         assert !mappedNames.containsKey(reference);
         assert appView.rootSet().mayBeMinified(reference, appView);
-        nextName = super.next(reference, internalState, isAvailable);
+        nextName = super.next(method, internalState, isAvailable);
       }
       assert nextName == reference.name || !method.isInitializer();
       assert nextName == reference.name || !holder.isAnnotation();
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 00a2136..b42920e 100644
--- a/src/main/java/com/android/tools/r8/utils/InternalOptions.java
+++ b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
@@ -1210,10 +1210,11 @@
 
       public Comparator<DexMethod> interfaceMethodOrdering = null;
 
-      public Comparator<Wrapper<DexMethod>> getInterfaceMethodOrderingOrDefault(
-          Comparator<Wrapper<DexMethod>> comparator) {
+      public Comparator<Wrapper<DexEncodedMethod>> getInterfaceMethodOrderingOrDefault(
+          Comparator<Wrapper<DexEncodedMethod>> comparator) {
         if (interfaceMethodOrdering != null) {
-          return (a, b) -> interfaceMethodOrdering.compare(a.get(), b.get());
+          return (a, b) ->
+              interfaceMethodOrdering.compare(a.get().getReference(), b.get().getReference());
         }
         return comparator;
       }