Add tests around deoptimization.

Test: 837-deopt
Bug: 233021156
Change-Id: I59982aee5c4c3e03ab1ba90448bfd0857aacd388
diff --git a/test/837-deopt/expected-stderr.txt b/test/837-deopt/expected-stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/837-deopt/expected-stderr.txt
diff --git a/test/837-deopt/expected-stdout.txt b/test/837-deopt/expected-stdout.txt
new file mode 100644
index 0000000..6a5618e
--- /dev/null
+++ b/test/837-deopt/expected-stdout.txt
@@ -0,0 +1 @@
+JNI_OnLoad called
diff --git a/test/837-deopt/info.txt b/test/837-deopt/info.txt
new file mode 100644
index 0000000..5c95277
--- /dev/null
+++ b/test/837-deopt/info.txt
@@ -0,0 +1 @@
+Tests around deoptimization.
diff --git a/test/837-deopt/src/Main.java b/test/837-deopt/src/Main.java
new file mode 100644
index 0000000..93efff9
--- /dev/null
+++ b/test/837-deopt/src/Main.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+class Main {
+  int field = 42;
+
+  // Test that deoptimization preserves objects that are singletons.
+  public static int $noinline$foo(Main arg) {
+    Main m = new Main();
+    arg.returnValue();
+    return m.field;
+  }
+
+  // Test that doing OSR after deoptimization works.
+  public static int $noinline$foo2(Main arg, boolean osr) {
+    Main m = new Main();
+    arg.returnValue();
+    if (osr) {
+      for (int i = 0; i < Integer.MAX_VALUE; i++) {}
+    }
+    return m.field;
+  }
+
+  public static void main(String[] args) throws Throwable {
+    System.loadLibrary(args[0]);
+    test1();
+    test2();
+  }
+
+  public static void assertEquals(int expected, int actual) {
+    if (expected != actual) {
+      throw new Error("Expected " + expected + ", got " + actual);
+    }
+  }
+
+  public static void test1() {
+    ensureJitBaselineCompiled(Main.class, "$noinline$foo");
+    // Surround the call with GCs to increase chances we execute $noinline$foo
+    // while the GC isn't marking. This makes sure the inline cache is populated.
+    Runtime.getRuntime().gc();
+    assertEquals(42, $noinline$foo(new Main()));
+    Runtime.getRuntime().gc();
+
+    ensureJitCompiled(Main.class, "$noinline$foo");
+    assertEquals(42, $noinline$foo(new SubMain()));
+  }
+
+  public static void test2() {
+    ensureJitBaselineCompiled(Main.class, "$noinline$foo2");
+    // Surround the call with GCs to increase chances we execute $noinline$foo
+    // while the GC isn't marking. This makes sure the inline cache is populated.
+    Runtime.getRuntime().gc();
+    assertEquals(42, $noinline$foo2(new Main(), false));
+    Runtime.getRuntime().gc();
+
+    ensureJitCompiled(Main.class, "$noinline$foo2");
+    assertEquals(42, $noinline$foo2(new SubMain(), true));
+  }
+
+  public String returnValue() {
+    return "Main";
+  }
+
+  public static native void ensureJitCompiled(Class<?> cls, String methodName);
+  public static native void ensureJitBaselineCompiled(Class<?> cls, String methodName);
+}
+
+// Define a subclass with another implementation of returnValue to deoptimize $noinline$foo and
+// $noinline$foo2.
+class SubMain extends Main {
+  public String returnValue() {
+    return "SubMain";
+  }
+}
diff --git a/test/knownfailures.json b/test/knownfailures.json
index 013272f..e38ea86 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -1507,5 +1507,10 @@
         "tests": ["2043-reference-pauses"],
         "env_vars": {"ART_TEST_DEBUG_GC": "true"},
         "description": ["Test timing out on debug gc."]
+    },
+    {
+        "tests": ["837-deopt"],
+        "description": ["Tests deoptimization and OSR, which never happens when tracing."],
+        "variant": "trace | stream | interpreter | interp-ac"
     }
 ]