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) { |
| 19 | System.loadLibrary(args[0]); |
Vladimir Marko | 1939a88 | 2016-04-07 10:12:31 +0100 | [diff] [blame] | 20 | Thread testThread = new Thread() { |
| 21 | public void run() { |
| 22 | performTest(); |
| 23 | } |
| 24 | }; |
| 25 | testThread.start(); |
| 26 | try { |
Vladimir Marko | 732350b | 2016-04-20 09:06:08 +0000 | [diff] [blame] | 27 | testThread.join(20 * 1000); // 20s timeout. |
Vladimir Marko | 1939a88 | 2016-04-07 10:12:31 +0100 | [diff] [blame] | 28 | } catch (InterruptedException ie) { |
| 29 | System.out.println("Interrupted."); |
| 30 | System.exit(1); |
| 31 | } |
| 32 | Thread.State state = testThread.getState(); |
| 33 | if (state != Thread.State.TERMINATED) { |
| 34 | System.out.println("Test timed out, current state: " + state); |
| 35 | System.exit(1); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public static void performTest() { |
| 40 | new SubMain(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 41 | if ($noinline$returnInt() != 53) { |
| 42 | throw new Error("Unexpected return value"); |
| 43 | } |
| 44 | if ($noinline$returnFloat() != 42.2f) { |
| 45 | throw new Error("Unexpected return value"); |
| 46 | } |
| 47 | if ($noinline$returnDouble() != Double.longBitsToDouble(0xF000000000001111L)) { |
| 48 | throw new Error("Unexpected return value "); |
| 49 | } |
| 50 | if ($noinline$returnLong() != 0xFFFF000000001111L) { |
| 51 | throw new Error("Unexpected return value"); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | $noinline$deopt(); |
| 56 | } catch (Exception e) {} |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 57 | DeoptimizationController.stopDeoptimization(); |
| 58 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 59 | $noinline$inlineCache(new Main(), /* isSecondInvocation */ false); |
| 60 | if ($noinline$inlineCache(new SubMain(), /* isSecondInvocation */ true) != SubMain.class) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 61 | throw new Error("Unexpected return value"); |
| 62 | } |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 63 | |
| 64 | $noinline$stackOverflow(new Main(), /* isSecondInvocation */ false); |
| 65 | $noinline$stackOverflow(new SubMain(), /* isSecondInvocation */ true); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 66 | |
| 67 | $opt$noinline$testOsrInlineLoop(null); |
| 68 | System.out.println("b28210356 passed."); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public static int $noinline$returnInt() { |
| 72 | if (doThrow) throw new Error(""); |
| 73 | int i = 0; |
Nicolas Geoffray | 17fccfb | 2016-02-12 16:52:16 +0000 | [diff] [blame] | 74 | for (; i < 100000; ++i) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 75 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 76 | while (!isInOsrCode("$noinline$returnInt")) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 77 | System.out.println(i); |
| 78 | return 53; |
| 79 | } |
| 80 | |
| 81 | public static float $noinline$returnFloat() { |
| 82 | if (doThrow) throw new Error(""); |
| 83 | int i = 0; |
Nicolas Geoffray | 17fccfb | 2016-02-12 16:52:16 +0000 | [diff] [blame] | 84 | for (; i < 200000; ++i) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 85 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 86 | while (!isInOsrCode("$noinline$returnFloat")) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 87 | System.out.println(i); |
| 88 | return 42.2f; |
| 89 | } |
| 90 | |
| 91 | public static double $noinline$returnDouble() { |
| 92 | if (doThrow) throw new Error(""); |
| 93 | int i = 0; |
Nicolas Geoffray | 17fccfb | 2016-02-12 16:52:16 +0000 | [diff] [blame] | 94 | for (; i < 300000; ++i) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 95 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 96 | while (!isInOsrCode("$noinline$returnDouble")) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 97 | System.out.println(i); |
| 98 | return Double.longBitsToDouble(0xF000000000001111L); |
| 99 | } |
| 100 | |
| 101 | public static long $noinline$returnLong() { |
| 102 | if (doThrow) throw new Error(""); |
Nicolas Geoffray | 17fccfb | 2016-02-12 16:52:16 +0000 | [diff] [blame] | 103 | int i = 0; |
| 104 | for (; i < 400000; ++i) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 105 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 106 | while (!isInOsrCode("$noinline$returnLong")) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 107 | System.out.println(i); |
| 108 | return 0xFFFF000000001111L; |
| 109 | } |
| 110 | |
| 111 | public static void $noinline$deopt() { |
| 112 | if (doThrow) throw new Error(""); |
| 113 | int i = 0; |
Nicolas Geoffray | 17fccfb | 2016-02-12 16:52:16 +0000 | [diff] [blame] | 114 | for (; i < 100000; ++i) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 115 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 116 | while (!isInOsrCode("$noinline$deopt")) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 117 | DeoptimizationController.startDeoptimization(); |
| 118 | } |
| 119 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 120 | public static Class $noinline$inlineCache(Main m, boolean isSecondInvocation) { |
| 121 | // If we are running in non-JIT mode, or were unlucky enough to get this method |
| 122 | // already JITted, just return the expected value. |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 123 | if (!isInInterpreter("$noinline$inlineCache")) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 124 | return SubMain.class; |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 125 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 126 | |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 127 | ensureHasProfilingInfo("$noinline$inlineCache"); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 128 | |
| 129 | // Ensure that we have OSR code to jump to. |
| 130 | if (isSecondInvocation) { |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 131 | ensureHasOsrCode("$noinline$inlineCache"); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // This call will be optimized in the OSR compiled code |
| 135 | // to check and deoptimize if m is not of type 'Main'. |
| 136 | Main other = m.inlineCache(); |
| 137 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 138 | // Jump to OSR compiled code. The second run |
| 139 | // of this method will have 'm' as a SubMain, and the compiled |
| 140 | // code we are jumping to will have wrongly optimize other as being a |
| 141 | // 'Main'. |
| 142 | if (isSecondInvocation) { |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 143 | while (!isInOsrCode("$noinline$inlineCache")) {} |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // We used to wrongly optimize this call and assume 'other' was a 'Main'. |
| 147 | return other.returnClass(); |
| 148 | } |
| 149 | |
| 150 | public Main inlineCache() { |
| 151 | return new Main(); |
| 152 | } |
| 153 | |
| 154 | public Class returnClass() { |
| 155 | return Main.class; |
| 156 | } |
| 157 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 158 | public void otherInlineCache() { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | public static void $noinline$stackOverflow(Main m, boolean isSecondInvocation) { |
| 163 | // If we are running in non-JIT mode, or were unlucky enough to get this method |
| 164 | // already JITted, just return the expected value. |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 165 | if (!isInInterpreter("$noinline$stackOverflow")) { |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
| 169 | // We need a ProfilingInfo object to populate the 'otherInlineCache' call. |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 170 | ensureHasProfilingInfo("$noinline$stackOverflow"); |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 171 | |
| 172 | if (isSecondInvocation) { |
| 173 | // Ensure we have an OSR code and we jump to it. |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 174 | while (!isInOsrCode("$noinline$stackOverflow")) {} |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | for (int i = 0; i < (isSecondInvocation ? 10000000 : 1); ++i) { |
| 178 | // The first invocation of $noinline$stackOverflow will populate the inline |
| 179 | // cache with Main. The second invocation of the method, will see a SubMain |
| 180 | // and will therefore trigger deoptimization. |
| 181 | m.otherInlineCache(); |
| 182 | } |
| 183 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 184 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 185 | public static void $opt$noinline$testOsrInlineLoop(String[] args) { |
| 186 | // Regression test for inlining a method with a loop to a method without a loop in OSR mode. |
| 187 | if (doThrow) throw new Error(); |
| 188 | assertIntEquals(12, $opt$inline$testRemoveSuspendCheck(12, 5)); |
| 189 | // Since we cannot have a loop directly in this method, we need to force the OSR |
| 190 | // compilation from native code. |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 191 | ensureHasProfilingInfo("$opt$noinline$testOsrInlineLoop"); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 192 | ensureHasOsrCode("$opt$noinline$testOsrInlineLoop"); |
| 193 | } |
| 194 | |
| 195 | public static int $opt$inline$testRemoveSuspendCheck(int x, int y) { |
| 196 | // For this test we need an inlined loop and have DCE re-run loop analysis |
| 197 | // after inlining. |
| 198 | while (y > 0) { |
| 199 | while ($opt$inline$inlineFalse() || !$opt$inline$inlineTrue()) { |
| 200 | x++; |
| 201 | } |
| 202 | y--; |
| 203 | } |
| 204 | return x; |
| 205 | } |
| 206 | |
| 207 | public static boolean $opt$inline$inlineTrue() { |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | public static boolean $opt$inline$inlineFalse() { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | public static void assertIntEquals(int expected, int result) { |
| 216 | if (expected != result) { |
| 217 | throw new Error("Expected: " + expected + ", found: " + result); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | public static native boolean isInOsrCode(String methodName); |
| 222 | public static native boolean isInInterpreter(String methodName); |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 223 | public static native void ensureHasProfilingInfo(String methodName); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 224 | public static native void ensureHasOsrCode(String methodName); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 225 | |
| 226 | public static boolean doThrow = false; |
| 227 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 228 | |
| 229 | class SubMain extends Main { |
| 230 | public Class returnClass() { |
| 231 | return SubMain.class; |
| 232 | } |
| 233 | |
| 234 | public Main inlineCache() { |
| 235 | return new SubMain(); |
| 236 | } |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 237 | |
| 238 | public void otherInlineCache() { |
| 239 | return; |
| 240 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 241 | } |