Snap for 7922329 from a3fdf25f7d2800c7196422559ee1ea09cb74cc18 to mainline-resolv-release

Change-Id: I22ffbfed9bdf20c71c281ec97fb359005d0bb0c1
diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc
index e1a4718..92b43fb 100644
--- a/compiler/optimizing/code_generator_arm64.cc
+++ b/compiler/optimizing/code_generator_arm64.cc
@@ -4636,6 +4636,7 @@
   switch (invoke->GetCodePtrLocation()) {
     case CodePtrLocation::kCallSelf:
       {
+        DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
         // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
         ExactAssemblyScope eas(GetVIXLAssembler(),
                                kInstructionSize,
diff --git a/compiler/optimizing/code_generator_arm_vixl.cc b/compiler/optimizing/code_generator_arm_vixl.cc
index bca0936..b24dca9 100644
--- a/compiler/optimizing/code_generator_arm_vixl.cc
+++ b/compiler/optimizing/code_generator_arm_vixl.cc
@@ -9253,6 +9253,7 @@
   switch (invoke->GetCodePtrLocation()) {
     case CodePtrLocation::kCallSelf:
       {
+        DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
         // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
         ExactAssemblyScope aas(GetVIXLAssembler(),
                                vixl32::k32BitT32InstructionSizeInBytes,
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 4fc29fc..099d84d 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -5280,6 +5280,7 @@
 
   switch (invoke->GetCodePtrLocation()) {
     case CodePtrLocation::kCallSelf:
+      DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
       __ call(GetFrameEntryLabel());
       RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
       break;
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index d54484c..9541933 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1077,6 +1077,7 @@
 
   switch (invoke->GetCodePtrLocation()) {
     case CodePtrLocation::kCallSelf:
+      DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
       __ call(&frame_entry_label_);
       RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
       break;
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index 939c49f..5a62580 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -4932,7 +4932,18 @@
   }
 
   MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
-  CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
+  CodePtrLocation GetCodePtrLocation() const {
+    // We do CHA analysis after sharpening. When a method has CHA inlining, it
+    // cannot call itself, as if the CHA optmization is invalid we want to make
+    // sure the method is never executed again. So, while sharpening can return
+    // kCallSelf, we bypass it here if there is a CHA optimization.
+    if (dispatch_info_.code_ptr_location == CodePtrLocation::kCallSelf &&
+        GetBlock()->GetGraph()->HasShouldDeoptimizeFlag()) {
+      return CodePtrLocation::kCallArtMethod;
+    } else {
+      return dispatch_info_.code_ptr_location;
+    }
+  }
   bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
   bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
   bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
diff --git a/test/832-cha-recursive/expected-stderr.txt b/test/832-cha-recursive/expected-stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/832-cha-recursive/expected-stderr.txt
diff --git a/test/832-cha-recursive/expected-stdout.txt b/test/832-cha-recursive/expected-stdout.txt
new file mode 100644
index 0000000..6a5618e
--- /dev/null
+++ b/test/832-cha-recursive/expected-stdout.txt
@@ -0,0 +1 @@
+JNI_OnLoad called
diff --git a/test/832-cha-recursive/info.txt b/test/832-cha-recursive/info.txt
new file mode 100644
index 0000000..4913993
--- /dev/null
+++ b/test/832-cha-recursive/info.txt
@@ -0,0 +1,2 @@
+Regression test for the optimization compiler which used to have a bug when a
+recursive method was doing CHA inlining.
diff --git a/test/832-cha-recursive/src/Main.java b/test/832-cha-recursive/src/Main.java
new file mode 100644
index 0000000..ed865a7
--- /dev/null
+++ b/test/832-cha-recursive/src/Main.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Main {
+  int mainField = 42;
+
+  public static void main(String[] args) throws Exception {
+    System.loadLibrary(args[0]);
+    ensureJitCompiled(Main.class, "$noinline$callRecursiveMethod");
+    $noinline$callRecursiveMethod(true);
+  }
+
+  public static void expectEquals(int expected, int actual) {
+    if (expected != actual) {
+      throw new Error("Expected " + expected + ", got " + actual);
+    }
+  }
+
+  public static void $noinline$callRecursiveMethod(boolean firstEntry) throws Exception {
+    Class<?> cls = Class.forName("LoadedLater");
+    Main m = (Main)cls.newInstance();
+    if (firstEntry) {
+      $noinline$callRecursiveMethod(false);
+    } else {
+      expectEquals(0, m.getField());
+    }
+  }
+
+  public int getField() {
+    return mainField;
+  }
+
+  public static native void ensureJitCompiled(Class<?> cls, String methodName);
+}
+
+class LoadedLater extends Main {
+  public int getField() {
+    return 0;
+  }
+}