Fix inadequate force inlining

Fixes: b/317339053
Change-Id: Icfcad1abeb752c065528e16d1b92523d3e75e210
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java b/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java
index 3117467..dd5d7a1 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java
@@ -61,7 +61,7 @@
 import java.util.Optional;
 import java.util.Set;
 
-public final class DefaultInliningOracle implements InliningOracle, InliningStrategy {
+public final class DefaultInliningOracle implements InliningOracle {
 
   private final AppView<AppInfoWithLiveness> appView;
   private final InternalOptions options;
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/ForcedInliningOracle.java b/src/main/java/com/android/tools/r8/ir/optimize/ForcedInliningOracle.java
index 9cb641e..5dc2341 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/ForcedInliningOracle.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/ForcedInliningOracle.java
@@ -20,7 +20,7 @@
 import com.android.tools.r8.shaking.AppInfoWithLiveness;
 import java.util.Map;
 
-final class ForcedInliningOracle implements InliningOracle, InliningStrategy {
+final class ForcedInliningOracle implements InliningOracle {
 
   private final AppView<AppInfoWithLiveness> appView;
   private final Map<? extends InvokeMethod, Inliner.InliningInfo> invokesToInline;
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java b/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
index f9dc4e2..e31d511 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
@@ -902,7 +902,7 @@
       Timing timing) {
     ForcedInliningOracle oracle = new ForcedInliningOracle(appView, invokesToInline);
     performInliningImpl(
-        oracle, oracle, method, code, feedback, inliningIRProvider, methodProcessor, timing);
+        oracle, method, code, feedback, inliningIRProvider, methodProcessor, timing);
   }
 
   public void performInlining(
@@ -938,7 +938,7 @@
         new InliningIRProvider(appView, method, code, lensCodeRewriter, methodProcessor);
     assert inliningIRProvider.verifyIRCacheIsEmpty();
     performInliningImpl(
-        oracle, oracle, method, code, feedback, inliningIRProvider, methodProcessor, timing);
+        oracle, method, code, feedback, inliningIRProvider, methodProcessor, timing);
   }
 
   public InliningReasonStrategy createDefaultInliningReasonStrategy(
@@ -975,7 +975,6 @@
   }
 
   private void performInliningImpl(
-      InliningStrategy strategy,
       InliningOracle oracle,
       ProgramMethod context,
       IRCode code,
@@ -1059,13 +1058,13 @@
             continue;
           }
 
-          if (!strategy.stillHasBudget(action, whyAreYouNotInliningReporter)) {
+          if (!singleTargetOracle.stillHasBudget(action, whyAreYouNotInliningReporter)) {
             assert whyAreYouNotInliningReporter.unsetReasonHasBeenReportedFlag();
             continue;
           }
 
           IRCode inlinee = action.buildInliningIR(appView, invoke, context, inliningIRProvider);
-          if (strategy.willExceedBudget(
+          if (singleTargetOracle.willExceedBudget(
               action, code, inlinee, invoke, block, whyAreYouNotInliningReporter)) {
             assert whyAreYouNotInliningReporter.unsetReasonHasBeenReportedFlag();
             continue;
@@ -1079,7 +1078,11 @@
           // Inline the inlinee code in place of the invoke instruction
           // Back up before the invoke instruction.
           iterator.previous();
-          strategy.markInlined(inlinee);
+
+          // Intentionally not using singleTargetOracle here, so that we decrease the inlining
+          // instruction allowance on the default inlining oracle when force inlining methods.
+          oracle.markInlined(inlinee);
+
           iterator.inlineInvoke(
               appView, code, inlinee, blockIterator, blocksToRemove, action.getDowncastClass());
 
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/InliningOracle.java b/src/main/java/com/android/tools/r8/ir/optimize/InliningOracle.java
index b7f06c8..69d5ecf 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/InliningOracle.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/InliningOracle.java
@@ -4,28 +4,36 @@
 
 package com.android.tools.r8.ir.optimize;
 
+import com.android.tools.r8.graph.AccessControl;
+import com.android.tools.r8.graph.AppView;
+import com.android.tools.r8.graph.DexProgramClass;
 import com.android.tools.r8.graph.MethodResolutionResult.SingleResolutionResult;
 import com.android.tools.r8.graph.ProgramMethod;
 import com.android.tools.r8.ir.analysis.ClassInitializationAnalysis;
+import com.android.tools.r8.ir.analysis.type.ClassTypeElement;
+import com.android.tools.r8.ir.code.BasicBlock;
 import com.android.tools.r8.ir.code.IRCode;
+import com.android.tools.r8.ir.code.InvokeDirect;
 import com.android.tools.r8.ir.code.InvokeMethod;
+import com.android.tools.r8.ir.code.Value;
+import com.android.tools.r8.ir.optimize.Inliner.InlineAction;
 import com.android.tools.r8.ir.optimize.Inliner.InlineResult;
 import com.android.tools.r8.ir.optimize.inliner.InliningIRProvider;
 import com.android.tools.r8.ir.optimize.inliner.WhyAreYouNotInliningReporter;
+import com.android.tools.r8.shaking.AppInfoWithLiveness;
 
 /**
  * The InliningOracle contains information needed for when inlining other methods into @method.
  */
 public interface InliningOracle {
 
-  boolean isForcedInliningOracle();
+  AppView<AppInfoWithLiveness> appView();
 
-  // TODO(b/142116551): This should be equivalent to invoke.lookupSingleTarget(appView, context)!
-  ProgramMethod lookupSingleTarget(InvokeMethod invoke, ProgramMethod context);
-
-  boolean passesInliningConstraints(
-      SingleResolutionResult<?> resolutionResult,
-      ProgramMethod candidate,
+  boolean canInlineInstanceInitializer(
+      IRCode code,
+      InvokeDirect invoke,
+      ProgramMethod singleTarget,
+      InliningIRProvider inliningIRProvider,
       WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
 
   InlineResult computeInlining(
@@ -37,4 +45,72 @@
       ClassInitializationAnalysis classInitializationAnalysis,
       InliningIRProvider inliningIRProvider,
       WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
+
+  default DexProgramClass getDowncastTypeIfNeeded(InvokeMethod invoke, ProgramMethod target) {
+    if (invoke.isInvokeMethodWithReceiver()) {
+      // If the invoke has a receiver but the actual type of the receiver is different from the
+      // computed target holder, inlining requires a downcast of the receiver. In case we don't know
+      // the exact type of the receiver we use the static type of the receiver.
+      Value receiver = invoke.asInvokeMethodWithReceiver().getReceiver();
+      if (!receiver.getType().isClassType()) {
+        return target.getHolder();
+      }
+
+      ClassTypeElement receiverType =
+          getReceiverTypeOrDefault(invoke, receiver.getType().asClassType());
+      ClassTypeElement targetType = target.getHolderType().toTypeElement(appView()).asClassType();
+      if (!receiverType.lessThanOrEqualUpToNullability(targetType, appView())) {
+        return target.getHolder();
+      }
+    }
+    return null;
+  }
+
+  ClassTypeElement getReceiverTypeOrDefault(InvokeMethod invoke, ClassTypeElement defaultValue);
+
+  boolean isForcedInliningOracle();
+
+  // TODO(b/142116551): This should be equivalent to invoke.lookupSingleTarget(appView, context)!
+  ProgramMethod lookupSingleTarget(InvokeMethod invoke, ProgramMethod context);
+
+  /** Inform the strategy that the inlinee has been inlined. */
+  void markInlined(IRCode inlinee);
+
+  boolean passesInliningConstraints(
+      SingleResolutionResult<?> resolutionResult,
+      ProgramMethod candidate,
+      WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
+
+  default boolean setDowncastTypeIfNeeded(
+      AppView<AppInfoWithLiveness> appView,
+      InlineAction.Builder actionBuilder,
+      InvokeMethod invoke,
+      ProgramMethod singleTarget,
+      ProgramMethod context) {
+    DexProgramClass downcastClass = getDowncastTypeIfNeeded(invoke, singleTarget);
+    if (downcastClass != null) {
+      if (AccessControl.isClassAccessible(downcastClass, context, appView).isPossiblyFalse()) {
+        return false;
+      }
+      actionBuilder.setDowncastClass(downcastClass);
+    }
+    return true;
+  }
+
+  /** Return true if there is still budget for inlining into this method. */
+  boolean stillHasBudget(
+      InlineAction action, WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
+
+  /**
+   * Check if the inlinee will exceed the the budget for inlining size into current method.
+   *
+   * <p>Return true if the strategy will *not* allow inlining.
+   */
+  boolean willExceedBudget(
+      InlineAction action,
+      IRCode code,
+      IRCode inlinee,
+      InvokeMethod invoke,
+      BasicBlock block,
+      WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
 }
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/InliningStrategy.java b/src/main/java/com/android/tools/r8/ir/optimize/InliningStrategy.java
deleted file mode 100644
index a7bb682..0000000
--- a/src/main/java/com/android/tools/r8/ir/optimize/InliningStrategy.java
+++ /dev/null
@@ -1,90 +0,0 @@
-// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-package com.android.tools.r8.ir.optimize;
-
-import com.android.tools.r8.graph.AccessControl;
-import com.android.tools.r8.graph.AppView;
-import com.android.tools.r8.graph.DexProgramClass;
-import com.android.tools.r8.graph.ProgramMethod;
-import com.android.tools.r8.ir.analysis.type.ClassTypeElement;
-import com.android.tools.r8.ir.code.BasicBlock;
-import com.android.tools.r8.ir.code.IRCode;
-import com.android.tools.r8.ir.code.InvokeDirect;
-import com.android.tools.r8.ir.code.InvokeMethod;
-import com.android.tools.r8.ir.code.Value;
-import com.android.tools.r8.ir.optimize.Inliner.InlineAction;
-import com.android.tools.r8.ir.optimize.inliner.InliningIRProvider;
-import com.android.tools.r8.ir.optimize.inliner.WhyAreYouNotInliningReporter;
-import com.android.tools.r8.shaking.AppInfoWithLiveness;
-
-interface InliningStrategy {
-
-  AppView<AppInfoWithLiveness> appView();
-
-  boolean canInlineInstanceInitializer(
-      IRCode code,
-      InvokeDirect invoke,
-      ProgramMethod singleTarget,
-      InliningIRProvider inliningIRProvider,
-      WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
-
-  /** Return true if there is still budget for inlining into this method. */
-  boolean stillHasBudget(
-      InlineAction action, WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
-
-  /**
-   * Check if the inlinee will exceed the the budget for inlining size into current method.
-   *
-   * <p>Return true if the strategy will *not* allow inlining.
-   */
-  boolean willExceedBudget(
-      InlineAction action,
-      IRCode code,
-      IRCode inlinee,
-      InvokeMethod invoke,
-      BasicBlock block,
-      WhyAreYouNotInliningReporter whyAreYouNotInliningReporter);
-
-  /** Inform the strategy that the inlinee has been inlined. */
-  void markInlined(IRCode inlinee);
-
-  default boolean setDowncastTypeIfNeeded(
-      AppView<AppInfoWithLiveness> appView,
-      InlineAction.Builder actionBuilder,
-      InvokeMethod invoke,
-      ProgramMethod singleTarget,
-      ProgramMethod context) {
-    DexProgramClass downcastClass = getDowncastTypeIfNeeded(invoke, singleTarget);
-    if (downcastClass != null) {
-      if (AccessControl.isClassAccessible(downcastClass, context, appView).isPossiblyFalse()) {
-        return false;
-      }
-      actionBuilder.setDowncastClass(downcastClass);
-    }
-    return true;
-  }
-
-  default DexProgramClass getDowncastTypeIfNeeded(InvokeMethod invoke, ProgramMethod target) {
-    if (invoke.isInvokeMethodWithReceiver()) {
-      // If the invoke has a receiver but the actual type of the receiver is different from the
-      // computed target holder, inlining requires a downcast of the receiver. In case we don't know
-      // the exact type of the receiver we use the static type of the receiver.
-      Value receiver = invoke.asInvokeMethodWithReceiver().getReceiver();
-      if (!receiver.getType().isClassType()) {
-        return target.getHolder();
-      }
-
-      ClassTypeElement receiverType =
-          getReceiverTypeOrDefault(invoke, receiver.getType().asClassType());
-      ClassTypeElement targetType = target.getHolderType().toTypeElement(appView()).asClassType();
-      if (!receiverType.lessThanOrEqualUpToNullability(targetType, appView())) {
-        return target.getHolder();
-      }
-    }
-    return null;
-  }
-
-  ClassTypeElement getReceiverTypeOrDefault(InvokeMethod invoke, ClassTypeElement defaultValue);
-}