Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +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 | public class Main { |
| 18 | public static void main(String[] args) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame^] | 19 | new SubMain(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 20 | System.loadLibrary(args[0]); |
| 21 | if ($noinline$returnInt() != 53) { |
| 22 | throw new Error("Unexpected return value"); |
| 23 | } |
| 24 | if ($noinline$returnFloat() != 42.2f) { |
| 25 | throw new Error("Unexpected return value"); |
| 26 | } |
| 27 | if ($noinline$returnDouble() != Double.longBitsToDouble(0xF000000000001111L)) { |
| 28 | throw new Error("Unexpected return value "); |
| 29 | } |
| 30 | if ($noinline$returnLong() != 0xFFFF000000001111L) { |
| 31 | throw new Error("Unexpected return value"); |
| 32 | } |
| 33 | |
| 34 | try { |
| 35 | $noinline$deopt(); |
| 36 | } catch (Exception e) {} |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame^] | 37 | DeoptimizationController.stopDeoptimization(); |
| 38 | |
| 39 | $noinline$inlineCache(new Main(), 0); |
| 40 | if ($noinline$inlineCache(new SubMain(), 1) != SubMain.class) { |
| 41 | throw new Error("Unexpected return value"); |
| 42 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public static int $noinline$returnInt() { |
| 46 | if (doThrow) throw new Error(""); |
| 47 | int i = 0; |
| 48 | for (; i < 100000000; ++i) { |
| 49 | } |
| 50 | while (!ensureInOsrCode()) {} |
| 51 | System.out.println(i); |
| 52 | return 53; |
| 53 | } |
| 54 | |
| 55 | public static float $noinline$returnFloat() { |
| 56 | if (doThrow) throw new Error(""); |
| 57 | int i = 0; |
| 58 | for (; i < 200000000; ++i) { |
| 59 | } |
| 60 | while (!ensureInOsrCode()) {} |
| 61 | System.out.println(i); |
| 62 | return 42.2f; |
| 63 | } |
| 64 | |
| 65 | public static double $noinline$returnDouble() { |
| 66 | if (doThrow) throw new Error(""); |
| 67 | int i = 0; |
| 68 | for (; i < 300000000; ++i) { |
| 69 | } |
| 70 | while (!ensureInOsrCode()) {} |
| 71 | System.out.println(i); |
| 72 | return Double.longBitsToDouble(0xF000000000001111L); |
| 73 | } |
| 74 | |
| 75 | public static long $noinline$returnLong() { |
| 76 | if (doThrow) throw new Error(""); |
| 77 | int i = 1000000; |
| 78 | for (; i < 400000000; ++i) { |
| 79 | } |
| 80 | while (!ensureInOsrCode()) {} |
| 81 | System.out.println(i); |
| 82 | return 0xFFFF000000001111L; |
| 83 | } |
| 84 | |
| 85 | public static void $noinline$deopt() { |
| 86 | if (doThrow) throw new Error(""); |
| 87 | int i = 0; |
| 88 | for (; i < 100000000; ++i) { |
| 89 | } |
| 90 | while (!ensureInOsrCode()) {} |
| 91 | DeoptimizationController.startDeoptimization(); |
| 92 | } |
| 93 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame^] | 94 | public static Class $noinline$inlineCache(Main m, int count) { |
| 95 | for (int i = 0; i < 500; ++i) { |
| 96 | // Warm me up. |
| 97 | } |
| 98 | if (count == 1) { |
| 99 | // Lots of back edges to trigger OSR compilation. |
| 100 | for (int i = 0; i < 1000; ++i) { |
| 101 | } |
| 102 | // Wait for OSR compilation. |
| 103 | try { |
| 104 | Thread.sleep(10); |
| 105 | } catch (Exception e) {} |
| 106 | } |
| 107 | |
| 108 | // This call will be optimized in the OSR compiled code |
| 109 | // to check and deoptimize if m is not of type 'Main'. |
| 110 | Main other = m.inlineCache(); |
| 111 | |
| 112 | if (count == 1) { |
| 113 | // Jump to OSR compiled code. The second run |
| 114 | // of this method will have 'm' as a SubMain, and the compiled |
| 115 | // code we are jumping to will have wrongly optimize other as being a |
| 116 | // 'Main'. |
| 117 | while (!ensureInOsrCode()) {} |
| 118 | } |
| 119 | |
| 120 | // We used to wrongly optimize this call and assume 'other' was a 'Main'. |
| 121 | return other.returnClass(); |
| 122 | } |
| 123 | |
| 124 | public Main inlineCache() { |
| 125 | return new Main(); |
| 126 | } |
| 127 | |
| 128 | public Class returnClass() { |
| 129 | return Main.class; |
| 130 | } |
| 131 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 132 | public static int[] array = new int[4]; |
| 133 | |
| 134 | public static native boolean ensureInOsrCode(); |
| 135 | |
| 136 | public static boolean doThrow = false; |
| 137 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame^] | 138 | |
| 139 | class SubMain extends Main { |
| 140 | public Class returnClass() { |
| 141 | return SubMain.class; |
| 142 | } |
| 143 | |
| 144 | public Main inlineCache() { |
| 145 | return new SubMain(); |
| 146 | } |
| 147 | } |