Reland "Improve invokeinterface for nterp."

This reverts commit f1d06474baa2f7c00761db39099b89ddab71bbe4.

Bug: 177554973
Bug: 112676029

Test: test.py
Test: 815-invokeinterface-default
Test: enable text-to-speech on device, no crash

Reason for revert: Fixed issue with recursive default methods

Change-Id: I2fb9336adb6c4fc920f39aa19bfe7f0a92ce059a
diff --git a/compiler/optimizing/instruction_builder.cc b/compiler/optimizing/instruction_builder.cc
index 3b1086e..068d3a9 100644
--- a/compiler/optimizing/instruction_builder.cc
+++ b/compiler/optimizing/instruction_builder.cc
@@ -1111,6 +1111,7 @@
     HInvokeStaticOrDirect::DispatchInfo dispatch_info =
         HSharpening::SharpenLoadMethod(resolved_method,
                                        has_method_id,
+                                       /* for_interface_call= */ false,
                                        code_generator_);
     if (dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
       graph_->SetHasDirectCriticalNativeCall(true);
@@ -1147,8 +1148,11 @@
       ScopedObjectAccess soa(Thread::Current());
       DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
     }
-    MethodLoadKind load_kind =
-        HSharpening::SharpenLoadMethod(resolved_method, /* has_method_id= */ true, code_generator_)
+    MethodLoadKind load_kind = HSharpening::SharpenLoadMethod(
+        resolved_method,
+        /* has_method_id= */ true,
+        /* for_interface_call= */ true,
+        code_generator_)
             .method_load_kind;
     invoke = new (allocator_) HInvokeInterface(allocator_,
                                                number_of_arguments,
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index 96f82f6..8886f14 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -2310,8 +2310,11 @@
       // the invoke, as we would need to look it up in the current dex file, and it
       // is unlikely that it exists. The most usual situation for such typed
       // arraycopy methods is a direct pointer to the boot image.
-      invoke->SetDispatchInfo(
-          HSharpening::SharpenLoadMethod(method, /* has_method_id= */ true, codegen_));
+      invoke->SetDispatchInfo(HSharpening::SharpenLoadMethod(
+          method,
+          /* has_method_id= */ true,
+          /* for_interface_call= */ false,
+          codegen_));
     }
   }
 }
diff --git a/compiler/optimizing/sharpening.cc b/compiler/optimizing/sharpening.cc
index 3ffb24b..1fd76f7 100644
--- a/compiler/optimizing/sharpening.cc
+++ b/compiler/optimizing/sharpening.cc
@@ -58,7 +58,10 @@
 }
 
 HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenLoadMethod(
-    ArtMethod* callee, bool has_method_id, CodeGenerator* codegen) {
+    ArtMethod* callee,
+    bool has_method_id,
+    bool for_interface_call,
+    CodeGenerator* codegen) {
   if (kIsDebugBuild) {
     ScopedObjectAccess soa(Thread::Current());  // Required for GetDeclaringClass below.
     DCHECK(callee != nullptr);
@@ -83,8 +86,14 @@
   // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
   // situations.
   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
-  if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
-    // Recursive call.
+  if (callee == codegen->GetGraph()->GetArtMethod() &&
+      !codegen->GetGraph()->IsDebuggable() &&
+      // The runtime expects the canonical interface method being passed as
+      // hidden argument when doing an invokeinterface. Because default methods
+      // can be called through invokevirtual, we may get a copied method if we
+      // load 'recursively'.
+      (!for_interface_call || !callee->IsDefault())) {
+    // Recursive load.
     method_load_kind = MethodLoadKind::kRecursive;
     code_ptr_location = CodePtrLocation::kCallSelf;
   } else if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
diff --git a/compiler/optimizing/sharpening.h b/compiler/optimizing/sharpening.h
index f71d9b5..9753669 100644
--- a/compiler/optimizing/sharpening.h
+++ b/compiler/optimizing/sharpening.h
@@ -31,7 +31,10 @@
  public:
   // Used by the builder and InstructionSimplifier.
   static HInvokeStaticOrDirect::DispatchInfo SharpenLoadMethod(
-      ArtMethod* callee, bool has_method_id, CodeGenerator* codegen);
+      ArtMethod* callee,
+      bool has_method_id,
+      bool for_interface_call,
+      CodeGenerator* codegen);
 
   // Used by the builder and the inliner.
   static HLoadClass::LoadKind ComputeLoadClassKind(HLoadClass* load_class,