blob: d39e6ed57be6cc08c9a91a1d86de1a29e7bc827a [file] [log] [blame]
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17interface Itf {
18 public Class sameInvokeInterface();
19}
20
21public class Main implements Itf {
22 public static void assertEquals(Object expected, Object actual) {
23 if (expected != actual) {
24 throw new Error("Expected " + expected + ", got " + actual);
25 }
26 }
27
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +010028 public static void assertEquals(int expected, int actual) {
29 if (expected != actual) {
30 throw new Error("Expected " + expected + ", got " + actual);
31 }
32 }
33
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000034 public static void main(String[] args) throws Exception {
35 System.loadLibrary(args[0]);
36 Main[] mains = new Main[3];
37 Itf[] itfs = new Itf[3];
38 itfs[0] = mains[0] = new Main();
39 itfs[1] = mains[1] = new Subclass();
40 itfs[2] = mains[2] = new OtherSubclass();
41
Nicolas Geoffray2fa11372016-06-16 14:09:03 +010042 // Create the profiling info eagerly to make sure they are filled.
43 ensureProfilingInfo566();
44
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000045 // Make testInvokeVirtual and testInvokeInterface hot to get them jitted.
46 // We pass Main and Subclass to get polymorphic inlining based on calling
47 // the same method.
48 for (int i = 0; i < 10000; ++i) {
49 testInvokeVirtual(mains[0]);
50 testInvokeVirtual(mains[1]);
51 testInvokeInterface(itfs[0]);
52 testInvokeInterface(itfs[1]);
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +010053 $noinline$testInlineToSameTarget(mains[0]);
54 $noinline$testInlineToSameTarget(mains[1]);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000055 }
56
Nicolas Geoffray2fa11372016-06-16 14:09:03 +010057 ensureJittedAndPolymorphicInline566();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000058
59 // At this point, the JIT should have compiled both methods, and inline
60 // sameInvokeVirtual and sameInvokeInterface.
61 assertEquals(Main.class, testInvokeVirtual(mains[0]));
62 assertEquals(Main.class, testInvokeVirtual(mains[1]));
63
64 assertEquals(Itf.class, testInvokeInterface(itfs[0]));
65 assertEquals(Itf.class, testInvokeInterface(itfs[1]));
66
67 // This will trigger a deoptimization of the compiled code.
68 assertEquals(OtherSubclass.class, testInvokeVirtual(mains[2]));
69 assertEquals(OtherSubclass.class, testInvokeInterface(itfs[2]));
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +010070
71 // Run this once to make sure we execute the JITted code.
72 $noinline$testInlineToSameTarget(mains[0]);
73 assertEquals(20001, counter);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000074 }
75
76 public Class sameInvokeVirtual() {
Nicolas Geoffray2fa11372016-06-16 14:09:03 +010077 field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo.
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000078 return Main.class;
79 }
80
81 public Class sameInvokeInterface() {
Nicolas Geoffray2fa11372016-06-16 14:09:03 +010082 field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo.
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000083 return Itf.class;
84 }
85
86 public static Class testInvokeInterface(Itf i) {
87 return i.sameInvokeInterface();
88 }
89
90 public static Class testInvokeVirtual(Main m) {
91 return m.sameInvokeVirtual();
92 }
93
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +010094 public static void $noinline$testInlineToSameTarget(Main m) {
95 if (doThrow) throw new Error("");
96 m.increment();
97 }
98
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000099 public Object field = new Object();
100
Nicolas Geoffray2fa11372016-06-16 14:09:03 +0100101 public static native void ensureJittedAndPolymorphicInline566();
102 public static native void ensureProfilingInfo566();
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +0100103
104 public void increment() {
Nicolas Geoffray130b7cf2016-05-06 10:08:36 +0100105 field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +0100106 counter++;
107 }
108 public static int counter = 0;
109 public static boolean doThrow = false;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000110}
111
112class Subclass extends Main {
113}
114
115class OtherSubclass extends Main {
116 public Class sameInvokeVirtual() {
117 return OtherSubclass.class;
118 }
119
120 public Class sameInvokeInterface() {
121 return OtherSubclass.class;
122 }
123}