Don't do a recursive call when there are CHA guards.
Otherwise we would continue execute the method with invalid inlining
optimizations.
Test: 832-cha-recursive
Bug: 19381779
Change-Id: I57d73828d2a9c30f429cf32906f94244346c1310
diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc
index 4830d65..2549546 100644
--- a/compiler/optimizing/code_generator_arm64.cc
+++ b/compiler/optimizing/code_generator_arm64.cc
@@ -4639,6 +4639,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 06bf8b5..22f7f17 100644
--- a/compiler/optimizing/code_generator_arm_vixl.cc
+++ b/compiler/optimizing/code_generator_arm_vixl.cc
@@ -9255,6 +9255,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 a37f809..2bea51f 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -5295,6 +5295,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 deabc87..0f76bd3 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1088,6 +1088,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 d6b3726..94eae4d 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -4939,7 +4939,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;
+ }
+}