Apply graph lense when computing inlining constraints

Change-Id: I730a0d26c872d96a8d0d84650f09c0dcf5a07397
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/InliningConstraints.java b/src/main/java/com/android/tools/r8/ir/optimize/InliningConstraints.java
index 78d70a0..1e9566a 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/InliningConstraints.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/InliningConstraints.java
@@ -17,8 +17,6 @@
 import java.util.Collection;
 
 // Computes the inlining constraint for a given instruction.
-//
-// TODO(christofferqa): This class is incomplete.
 public class InliningConstraints {
 
   private AppInfoWithLiveness appInfo;
@@ -40,6 +38,7 @@
   }
 
   public InliningConstraints(AppInfoWithLiveness appInfo, GraphLense graphLense) {
+    assert graphLense.isContextFreeForMethods();
     this.appInfo = appInfo;
     this.graphLense = graphLense;
   }
@@ -93,8 +92,9 @@
   }
 
   public Constraint forInstanceGet(DexField field, DexType invocationContext) {
+    DexField lookup = graphLense.lookupField(field);
     return forFieldInstruction(
-        field, appInfo.lookupInstanceTarget(field.clazz, field), invocationContext);
+        lookup, appInfo.lookupInstanceTarget(lookup.clazz, lookup), invocationContext);
   }
 
   public Constraint forInstanceOf(DexType type, DexType invocationContext) {
@@ -106,16 +106,19 @@
   }
 
   public Constraint forInstancePut(DexField field, DexType invocationContext) {
+    DexField lookup = graphLense.lookupField(field);
     return forFieldInstruction(
-        field, appInfo.lookupInstanceTarget(field.clazz, field), invocationContext);
+        lookup, appInfo.lookupInstanceTarget(lookup.clazz, lookup), invocationContext);
   }
 
   public Constraint forInvokeDirect(DexMethod method, DexType invocationContext) {
-    return forSingleTargetInvoke(method, appInfo.lookupDirectTarget(method), invocationContext);
+    DexMethod lookup = graphLense.lookupMethod(method);
+    return forSingleTargetInvoke(lookup, appInfo.lookupDirectTarget(lookup), invocationContext);
   }
 
   public Constraint forInvokeInterface(DexMethod method, DexType invocationContext) {
-    return forVirtualInvoke(method, appInfo.lookupInterfaceTargets(method), invocationContext);
+    DexMethod lookup = graphLense.lookupMethod(method);
+    return forVirtualInvoke(lookup, appInfo.lookupInterfaceTargets(lookup), invocationContext);
   }
 
   public Constraint forInvokeMultiNewArray(DexType type, DexType invocationContext) {
@@ -131,7 +134,8 @@
   }
 
   public Constraint forInvokeStatic(DexMethod method, DexType invocationContext) {
-    return forSingleTargetInvoke(method, appInfo.lookupStaticTarget(method), invocationContext);
+    DexMethod lookup = graphLense.lookupMethod(method);
+    return forSingleTargetInvoke(lookup, appInfo.lookupStaticTarget(lookup), invocationContext);
   }
 
   public Constraint forInvokeSuper(DexMethod method, DexType invocationContext) {
@@ -140,7 +144,8 @@
   }
 
   public Constraint forInvokeVirtual(DexMethod method, DexType invocationContext) {
-    return forVirtualInvoke(method, appInfo.lookupVirtualTargets(method), invocationContext);
+    DexMethod lookup = graphLense.lookupMethod(method);
+    return forVirtualInvoke(lookup, appInfo.lookupVirtualTargets(lookup), invocationContext);
   }
 
   public Constraint forJumpInstruction() {
@@ -190,13 +195,15 @@
   }
 
   public Constraint forStaticGet(DexField field, DexType invocationContext) {
+    DexField lookup = graphLense.lookupField(field);
     return forFieldInstruction(
-        field, appInfo.lookupStaticTarget(field.clazz, field), invocationContext);
+        lookup, appInfo.lookupStaticTarget(lookup.clazz, lookup), invocationContext);
   }
 
   public Constraint forStaticPut(DexField field, DexType invocationContext) {
+    DexField lookup = graphLense.lookupField(field);
     return forFieldInstruction(
-        field, appInfo.lookupStaticTarget(field.clazz, field), invocationContext);
+        lookup, appInfo.lookupStaticTarget(lookup.clazz, lookup), invocationContext);
   }
 
   public Constraint forStore() {
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
index 4a36895..f38626e 100644
--- a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
@@ -1383,7 +1383,7 @@
     return true;
   }
 
-  private static class SingleTypeMapperGraphLense extends GraphLense {
+  private class SingleTypeMapperGraphLense extends GraphLense {
 
     private final DexType source;
     private final DexType target;
@@ -1401,17 +1401,18 @@
     @Override
     public GraphLenseLookupResult lookupMethod(
         DexMethod method, DexEncodedMethod context, Type type) {
-      throw new Unreachable();
+      return new GraphLenseLookupResult(
+          renamedMembersLense.methodMap.getOrDefault(method, method), type);
     }
 
     @Override
     public DexField lookupField(DexField field) {
-      throw new Unreachable();
+      return renamedMembersLense.fieldMap.getOrDefault(field, field);
     }
 
     @Override
     public boolean isContextFreeForMethods() {
-      throw new Unreachable();
+      return true;
     }
   }
 
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
index 4f614b4..fa10215 100644
--- a/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
@@ -136,9 +136,8 @@
   public static class Builder {
     private final AppInfo appInfo;
 
-    private final ImmutableMap.Builder<DexField, DexField> fieldMapBuilder = ImmutableMap.builder();
-    private final ImmutableMap.Builder<DexMethod, DexMethod> methodMapBuilder =
-        ImmutableMap.builder();
+    protected final Map<DexField, DexField> fieldMap = new HashMap<>();
+    protected final Map<DexMethod, DexMethod> methodMap = new HashMap<>();
     private final ImmutableSet.Builder<DexMethod> mergedMethodsBuilder = ImmutableSet.builder();
     private final Map<DexType, Map<DexMethod, DexMethod>> contextualVirtualToDirectMethodMaps =
         new HashMap<>();
@@ -151,8 +150,6 @@
         GraphLense previousLense,
         Map<DexType, DexType> mergedClasses,
         DexItemFactory dexItemFactory) {
-      Map<DexField, DexField> fieldMap = fieldMapBuilder.build();
-      Map<DexMethod, DexMethod> methodMap = methodMapBuilder.build();
       if (fieldMap.isEmpty()
           && methodMap.isEmpty()
           && contextualVirtualToDirectMethodMaps.isEmpty()) {
@@ -205,11 +202,11 @@
     }
 
     public void map(DexField from, DexField to) {
-      fieldMapBuilder.put(from, to);
+      fieldMap.put(from, to);
     }
 
     public void map(DexMethod from, DexMethod to) {
-      methodMapBuilder.put(from, to);
+      methodMap.put(from, to);
     }
 
     public void mapVirtualMethodToDirectInType(DexMethod from, DexMethod to, DexType type) {
@@ -219,8 +216,8 @@
     }
 
     public void merge(VerticalClassMergerGraphLense.Builder builder) {
-      fieldMapBuilder.putAll(builder.fieldMapBuilder.build());
-      methodMapBuilder.putAll(builder.methodMapBuilder.build());
+      fieldMap.putAll(builder.fieldMap);
+      methodMap.putAll(builder.methodMap);
       mergedMethodsBuilder.addAll(builder.mergedMethodsBuilder.build());
       for (DexType context : builder.contextualVirtualToDirectMethodMaps.keySet()) {
         Map<DexMethod, DexMethod> current = contextualVirtualToDirectMethodMaps.get(context);