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 | |
| 28 | public static void main(String[] args) throws Exception { |
| 29 | System.loadLibrary(args[0]); |
| 30 | Main[] mains = new Main[3]; |
| 31 | Itf[] itfs = new Itf[3]; |
| 32 | itfs[0] = mains[0] = new Main(); |
| 33 | itfs[1] = mains[1] = new Subclass(); |
| 34 | itfs[2] = mains[2] = new OtherSubclass(); |
| 35 | |
| 36 | // Make testInvokeVirtual and testInvokeInterface hot to get them jitted. |
| 37 | // We pass Main and Subclass to get polymorphic inlining based on calling |
| 38 | // the same method. |
| 39 | for (int i = 0; i < 10000; ++i) { |
| 40 | testInvokeVirtual(mains[0]); |
| 41 | testInvokeVirtual(mains[1]); |
| 42 | testInvokeInterface(itfs[0]); |
| 43 | testInvokeInterface(itfs[1]); |
| 44 | } |
| 45 | |
| 46 | ensureJittedAndPolymorphicInline(); |
| 47 | |
| 48 | // At this point, the JIT should have compiled both methods, and inline |
| 49 | // sameInvokeVirtual and sameInvokeInterface. |
| 50 | assertEquals(Main.class, testInvokeVirtual(mains[0])); |
| 51 | assertEquals(Main.class, testInvokeVirtual(mains[1])); |
| 52 | |
| 53 | assertEquals(Itf.class, testInvokeInterface(itfs[0])); |
| 54 | assertEquals(Itf.class, testInvokeInterface(itfs[1])); |
| 55 | |
| 56 | // This will trigger a deoptimization of the compiled code. |
| 57 | assertEquals(OtherSubclass.class, testInvokeVirtual(mains[2])); |
| 58 | assertEquals(OtherSubclass.class, testInvokeInterface(itfs[2])); |
| 59 | } |
| 60 | |
| 61 | public Class sameInvokeVirtual() { |
| 62 | field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo |
| 63 | return Main.class; |
| 64 | } |
| 65 | |
| 66 | public Class sameInvokeInterface() { |
| 67 | field.getClass(); // null check to ensure we get an inlined frame in the CodeInfo |
| 68 | return Itf.class; |
| 69 | } |
| 70 | |
| 71 | public static Class testInvokeInterface(Itf i) { |
| 72 | return i.sameInvokeInterface(); |
| 73 | } |
| 74 | |
| 75 | public static Class testInvokeVirtual(Main m) { |
| 76 | return m.sameInvokeVirtual(); |
| 77 | } |
| 78 | |
| 79 | public Object field = new Object(); |
| 80 | |
| 81 | public static native void ensureJittedAndPolymorphicInline(); |
| 82 | } |
| 83 | |
| 84 | class Subclass extends Main { |
| 85 | } |
| 86 | |
| 87 | class OtherSubclass extends Main { |
| 88 | public Class sameInvokeVirtual() { |
| 89 | return OtherSubclass.class; |
| 90 | } |
| 91 | |
| 92 | public Class sameInvokeInterface() { |
| 93 | return OtherSubclass.class; |
| 94 | } |
| 95 | } |