1 /* 2 * Copyright (c) 2015, 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.hotspot.meta; 26 27 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; 28 29 import org.graalvm.compiler.core.common.type.StampPair; 30 import org.graalvm.compiler.nodes.ConstantNode; 31 import org.graalvm.compiler.nodes.ValueNode; 32 import org.graalvm.compiler.nodes.extended.GuardingNode; 33 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 34 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool; 35 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin; 36 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin; 37 import org.graalvm.compiler.nodes.graphbuilderconf.TypePlugin; 38 import org.graalvm.compiler.nodes.util.ConstantFoldUtil; 39 import org.graalvm.compiler.word.Word; 40 import org.graalvm.compiler.word.WordOperationPlugin; 41 42 import jdk.vm.ci.meta.JavaConstant; 43 import jdk.vm.ci.meta.JavaKind; 44 import jdk.vm.ci.meta.JavaType; 45 import jdk.vm.ci.meta.JavaTypeProfile; 46 import jdk.vm.ci.meta.ResolvedJavaField; 47 import jdk.vm.ci.meta.ResolvedJavaMethod; 48 import jdk.vm.ci.meta.ResolvedJavaType; 49 50 /** 51 * This plugin handles the HotSpot-specific customizations of bytecode parsing: 52 * <p> 53 * {@link Word}-type rewriting for {@link GraphBuilderContext#parsingIntrinsic intrinsic} functions 54 * (snippets and method substitutions), by forwarding to the {@link WordOperationPlugin}. Note that 55 * we forward the {@link NodePlugin} and {@link TypePlugin} methods, but not the 56 * {@link InlineInvokePlugin} methods implemented by {@link WordOperationPlugin}. The latter is not 57 * necessary because HotSpot only uses the {@link Word} type in methods that are force-inlined, 58 * i.e., there are never non-inlined invokes that involve the {@link Word} type. 59 * <p> 60 * Constant folding of field loads. 61 */ 62 public final class HotSpotNodePlugin implements NodePlugin, TypePlugin { 63 protected final WordOperationPlugin wordOperationPlugin; 64 65 public HotSpotNodePlugin(WordOperationPlugin wordOperationPlugin) { 66 this.wordOperationPlugin = wordOperationPlugin; 67 } 68 69 @Override 70 public boolean canChangeStackKind(GraphBuilderContext b) { 71 if (b.parsingIntrinsic()) { 72 return wordOperationPlugin.canChangeStackKind(b); 73 } 74 return false; 75 } 76 77 @Override 78 public StampPair interceptType(GraphBuilderTool b, JavaType declaredType, boolean nonNull) { 79 if (b.parsingIntrinsic()) { 80 return wordOperationPlugin.interceptType(b, declaredType, nonNull); 81 } 82 return null; 83 } 84 85 @Override 86 public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) { 163 return true; 164 } 165 return false; 166 } 167 168 @Override 169 public boolean handleCheckCast(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) { 170 if (b.parsingIntrinsic() && wordOperationPlugin.handleCheckCast(b, object, type, profile)) { 171 return true; 172 } 173 return false; 174 } 175 176 @Override 177 public boolean handleInstanceOf(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) { 178 if (b.parsingIntrinsic() && wordOperationPlugin.handleInstanceOf(b, object, type, profile)) { 179 return true; 180 } 181 return false; 182 } 183 } | 1 /* 2 * Copyright (c) 2015, 2019, 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.hotspot.meta; 26 27 import static jdk.vm.ci.meta.DeoptimizationAction.None; 28 import static jdk.vm.ci.meta.DeoptimizationReason.TransferToInterpreter; 29 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; 30 31 import org.graalvm.compiler.core.common.CompilationIdentifier; 32 import org.graalvm.compiler.core.common.type.StampFactory; 33 import org.graalvm.compiler.core.common.type.StampPair; 34 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 35 import org.graalvm.compiler.hotspot.HotSpotCompilationIdentifier; 36 import org.graalvm.compiler.hotspot.nodes.CurrentJavaThreadNode; 37 import org.graalvm.compiler.hotspot.word.HotSpotWordTypes; 38 import org.graalvm.compiler.nodes.ConstantNode; 39 import org.graalvm.compiler.nodes.FixedGuardNode; 40 import org.graalvm.compiler.nodes.FixedWithNextNode; 41 import org.graalvm.compiler.nodes.LogicNode; 42 import org.graalvm.compiler.nodes.NamedLocationIdentity; 43 import org.graalvm.compiler.nodes.StructuredGraph; 44 import org.graalvm.compiler.nodes.ValueNode; 45 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; 46 import org.graalvm.compiler.nodes.extended.GuardingNode; 47 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 48 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool; 49 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin; 50 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin; 51 import org.graalvm.compiler.nodes.graphbuilderconf.TypePlugin; 52 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType; 53 import org.graalvm.compiler.nodes.memory.ReadNode; 54 import org.graalvm.compiler.nodes.memory.address.AddressNode; 55 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; 56 import org.graalvm.compiler.nodes.util.ConstantFoldUtil; 57 import org.graalvm.compiler.word.Word; 58 import org.graalvm.compiler.word.WordOperationPlugin; 59 import jdk.internal.vm.compiler.word.LocationIdentity; 60 61 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 62 import jdk.vm.ci.meta.JavaConstant; 63 import jdk.vm.ci.meta.JavaKind; 64 import jdk.vm.ci.meta.JavaType; 65 import jdk.vm.ci.meta.JavaTypeProfile; 66 import jdk.vm.ci.meta.ResolvedJavaField; 67 import jdk.vm.ci.meta.ResolvedJavaMethod; 68 import jdk.vm.ci.meta.ResolvedJavaType; 69 70 import java.lang.reflect.Field; 71 import sun.misc.Unsafe; 72 73 /** 74 * This plugin does HotSpot-specific customization of bytecode parsing: 75 * <ul> 76 * <li>{@link Word}-type rewriting for {@link GraphBuilderContext#parsingIntrinsic intrinsic} 77 * functions (snippets and method substitutions), by forwarding to the {@link WordOperationPlugin}. 78 * Note that we forward the {@link NodePlugin} and {@link TypePlugin} methods, but not the 79 * {@link InlineInvokePlugin} methods implemented by {@link WordOperationPlugin}. The latter is not 80 * necessary because HotSpot only uses the {@link Word} type in methods that are force-inlined, 81 * i.e., there are never non-inlined invokes that involve the {@link Word} type.</li> 82 * <li>Constant folding of field loads.</li> 83 * </ul> 84 */ 85 public final class HotSpotNodePlugin implements NodePlugin, TypePlugin { 86 protected final WordOperationPlugin wordOperationPlugin; 87 private final GraalHotSpotVMConfig config; 88 private final HotSpotWordTypes wordTypes; 89 90 public HotSpotNodePlugin(WordOperationPlugin wordOperationPlugin, GraalHotSpotVMConfig config, HotSpotWordTypes wordTypes) { 91 this.wordOperationPlugin = wordOperationPlugin; 92 this.config = config; 93 this.wordTypes = wordTypes; 94 } 95 96 @Override 97 public boolean canChangeStackKind(GraphBuilderContext b) { 98 if (b.parsingIntrinsic()) { 99 return wordOperationPlugin.canChangeStackKind(b); 100 } 101 return false; 102 } 103 104 @Override 105 public StampPair interceptType(GraphBuilderTool b, JavaType declaredType, boolean nonNull) { 106 if (b.parsingIntrinsic()) { 107 return wordOperationPlugin.interceptType(b, declaredType, nonNull); 108 } 109 return null; 110 } 111 112 @Override 113 public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) { 190 return true; 191 } 192 return false; 193 } 194 195 @Override 196 public boolean handleCheckCast(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) { 197 if (b.parsingIntrinsic() && wordOperationPlugin.handleCheckCast(b, object, type, profile)) { 198 return true; 199 } 200 return false; 201 } 202 203 @Override 204 public boolean handleInstanceOf(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) { 205 if (b.parsingIntrinsic() && wordOperationPlugin.handleInstanceOf(b, object, type, profile)) { 206 return true; 207 } 208 return false; 209 } 210 211 @Override 212 public FixedWithNextNode instrumentExceptionDispatch(StructuredGraph graph, FixedWithNextNode afterExceptionLoaded) { 213 CompilationIdentifier id = graph.compilationId(); 214 if (id instanceof HotSpotCompilationIdentifier) { 215 HotSpotCompilationRequest request = ((HotSpotCompilationIdentifier) id).getRequest(); 216 if (request != null) { 217 long compileState = request.getJvmciEnv(); 218 if (compileState != 0 && 219 config.jvmciCompileStateCanPostOnExceptionsOffset != Integer.MIN_VALUE && 220 config.javaThreadShouldPostOnExceptionsFlagOffset != Integer.MIN_VALUE) { 221 long canPostOnExceptionsOffset = compileState + config.jvmciCompileStateCanPostOnExceptionsOffset; 222 boolean canPostOnExceptions = UNSAFE.getByte(canPostOnExceptionsOffset) != 0; 223 if (canPostOnExceptions) { 224 // If the exception capability is set, then generate code 225 // to check the JavaThread.should_post_on_exceptions flag to see 226 // if we actually need to report exception events for the current 227 // thread. If not, take the fast path otherwise deoptimize. 228 CurrentJavaThreadNode thread = graph.unique(new CurrentJavaThreadNode(wordTypes.getWordKind())); 229 ValueNode offset = graph.unique(ConstantNode.forLong(config.javaThreadShouldPostOnExceptionsFlagOffset)); 230 AddressNode address = graph.unique(new OffsetAddressNode(thread, offset)); 231 ReadNode shouldPostException = graph.add(new ReadNode(address, JAVA_THREAD_SHOULD_POST_ON_EXCEPTIONS_FLAG_LOCATION, StampFactory.intValue(), BarrierType.NONE)); 232 afterExceptionLoaded.setNext(shouldPostException); 233 ValueNode zero = graph.unique(ConstantNode.forInt(0)); 234 LogicNode cond = graph.unique(new IntegerEqualsNode(shouldPostException, zero)); 235 FixedGuardNode check = graph.add(new FixedGuardNode(cond, TransferToInterpreter, None, false)); 236 shouldPostException.setNext(check); 237 return check; 238 } 239 } 240 } 241 } 242 return afterExceptionLoaded; 243 } 244 245 private static final LocationIdentity JAVA_THREAD_SHOULD_POST_ON_EXCEPTIONS_FLAG_LOCATION = NamedLocationIdentity.mutable("JavaThread::_should_post_on_exceptions_flag"); 246 247 private static final Unsafe UNSAFE = initUnsafe(); 248 249 private static Unsafe initUnsafe() { 250 try { 251 // Fast path when we are trusted. 252 return Unsafe.getUnsafe(); 253 } catch (SecurityException se) { 254 // Slow path when we are not trusted. 255 try { 256 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); 257 theUnsafe.setAccessible(true); 258 return (Unsafe) theUnsafe.get(Unsafe.class); 259 } catch (Exception e) { 260 throw new RuntimeException("exception while trying to get Unsafe", e); 261 } 262 } 263 } 264 } |