1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.replacements.test; 26 27 import jdk.vm.ci.meta.SpeculationLog; 28 import org.graalvm.compiler.code.CompilationResult; 29 import org.graalvm.compiler.core.test.GraalCompilerTest; 30 import org.graalvm.compiler.debug.DebugContext; 31 import org.junit.Test; 32 33 import jdk.vm.ci.code.InstalledCode; 34 import jdk.vm.ci.code.InvalidInstalledCodeException; 35 import jdk.vm.ci.meta.ResolvedJavaMethod; 36 37 public class DeoptimizeOnIntegerExactTest extends GraalCompilerTest { 38 39 private final SpeculationLog speculationLog; 40 41 static boolean highlyLikely = true; 42 static boolean highlyUnlikely = false; 43 44 public DeoptimizeOnIntegerExactTest() { 45 speculationLog = getCodeCache().createSpeculationLog(); 46 } 47 48 public static int testAddExactSnippet(int x, int y) { 49 if (highlyLikely) { 50 return highlyUnlikely ? Math.addExact(x, y) : x; 51 } else { 52 return highlyUnlikely ? y : Math.addExact(x, y); 53 } 54 } 55 56 public static int testSubtractExactSnippet(int x, int y) { 57 if (highlyLikely) { 58 return highlyUnlikely ? Math.subtractExact(x, y) : x; 59 } else { 60 return highlyUnlikely ? y : Math.subtractExact(x, y); 61 } 62 } 63 64 public static int testMultiplyExactSnippet(int x, int y) { 65 if (highlyLikely) { 66 return highlyUnlikely ? Math.multiplyExact(x, y) : x; 67 } else { 68 return highlyUnlikely ? y : Math.multiplyExact(x, y); 69 } 70 } 71 72 public static int testIncrementExactSnippet(int x, int y) { 73 if (highlyLikely) { 74 return highlyUnlikely ? Math.incrementExact(x) : x; 75 } else { 76 return highlyUnlikely ? y : Math.incrementExact(x); 77 } 78 } 79 80 public static int testDecrementExactSnippet(int x, int y) { 81 if (highlyLikely) { 82 return highlyUnlikely ? Math.decrementExact(x) : x; 83 } else { 84 return highlyUnlikely ? y : Math.decrementExact(x); 85 } 86 } 87 88 public void testAgainIfDeopt(String methodName, int x, int y) throws InvalidInstalledCodeException { 89 ResolvedJavaMethod method = getResolvedJavaMethod(methodName); 90 // We speculate on the first compilation. The global value numbering will merge the two 91 // floating integer exact operation nodes. 92 InstalledCode code = getCode(method); 93 code.executeVarargs(x, y); 94 if (!code.isValid()) { 95 // At the recompilation, we anchor the floating integer exact operation nodes at their 96 // corresponding branches. 97 code = getCode(method); 98 code.executeVarargs(x, y); 99 // The recompiled code should not get deoptimized. 100 assertTrue(code.isValid()); 101 } 102 } 103 104 @Test 105 public void testAddExact() throws InvalidInstalledCodeException { 106 testAgainIfDeopt("testAddExactSnippet", Integer.MAX_VALUE, 1); 107 } 108 109 @Test 110 public void testSubtractExact() throws InvalidInstalledCodeException { 111 testAgainIfDeopt("testSubtractExactSnippet", 0, Integer.MIN_VALUE); 112 } 113 114 @Test 115 public void testMultiplyExact() throws InvalidInstalledCodeException { 116 testAgainIfDeopt("testMultiplyExactSnippet", Integer.MAX_VALUE, 2); 117 } 118 119 @Test 120 public void testIncrementExact() throws InvalidInstalledCodeException { 121 testAgainIfDeopt("testIncrementExactSnippet", Integer.MAX_VALUE, 1); 122 } 123 124 @Test 125 public void testDecrementExact() throws InvalidInstalledCodeException { 126 testAgainIfDeopt("testDecrementExactSnippet", Integer.MIN_VALUE, 1); 127 } 128 129 @Override 130 protected SpeculationLog getSpeculationLog() { 131 speculationLog.collectFailedSpeculations(); 132 return speculationLog; 133 } 134 135 @Override 136 protected InstalledCode addMethod(DebugContext debug, final ResolvedJavaMethod method, final CompilationResult compilationResult) { 137 return getBackend().createInstalledCode(debug, method, compilationResult, speculationLog, null, false); 138 } 139 }