Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | interface Itf { |
| 18 | public Class sameInvokeInterface(); |
| 19 | } |
| 20 | |
| 21 | public 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 Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 28 | public static void assertEquals(int expected, int actual) { |
| 29 | if (expected != actual) { |
| 30 | throw new Error("Expected " + expected + ", got " + actual); |
| 31 | } |
| 32 | } |
| 33 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 34 | 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 Geoffray | 2fa1137 | 2016-06-16 14:09:03 +0100 | [diff] [blame^] | 42 | // Create the profiling info eagerly to make sure they are filled. |
| 43 | ensureProfilingInfo566(); |
| 44 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 45 | // 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 Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 53 | $noinline$testInlineToSameTarget(mains[0]); |
| 54 | $noinline$testInlineToSameTarget(mains[1]); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Nicolas Geoffray | 2fa1137 | 2016-06-16 14:09:03 +0100 | [diff] [blame^] | 57 | ensureJittedAndPolymorphicInline566(); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 58 | |
| 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 Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 70 | |
| 71 | // Run this once to make sure we execute the JITted code. |
| 72 | $noinline$testInlineToSameTarget(mains[0]); |
| 73 | assertEquals(20001, counter); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public Class sameInvokeVirtual() { |
Nicolas Geoffray | 2fa1137 | 2016-06-16 14:09:03 +0100 | [diff] [blame^] | 77 | field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo. |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 78 | return Main.class; |
| 79 | } |
| 80 | |
| 81 | public Class sameInvokeInterface() { |
Nicolas Geoffray | 2fa1137 | 2016-06-16 14:09:03 +0100 | [diff] [blame^] | 82 | field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo. |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 83 | 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 Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 94 | public static void $noinline$testInlineToSameTarget(Main m) { |
| 95 | if (doThrow) throw new Error(""); |
| 96 | m.increment(); |
| 97 | } |
| 98 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 99 | public Object field = new Object(); |
| 100 | |
Nicolas Geoffray | 2fa1137 | 2016-06-16 14:09:03 +0100 | [diff] [blame^] | 101 | public static native void ensureJittedAndPolymorphicInline566(); |
| 102 | public static native void ensureProfilingInfo566(); |
Nicolas Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 103 | |
| 104 | public void increment() { |
Nicolas Geoffray | 130b7cf | 2016-05-06 10:08:36 +0100 | [diff] [blame] | 105 | field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo |
Nicolas Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 106 | counter++; |
| 107 | } |
| 108 | public static int counter = 0; |
| 109 | public static boolean doThrow = false; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | class Subclass extends Main { |
| 113 | } |
| 114 | |
| 115 | class OtherSubclass extends Main { |
| 116 | public Class sameInvokeVirtual() { |
| 117 | return OtherSubclass.class; |
| 118 | } |
| 119 | |
| 120 | public Class sameInvokeInterface() { |
| 121 | return OtherSubclass.class; |
| 122 | } |
| 123 | } |