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) { 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 } | 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 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 java.lang.reflect.Field; 32 33 import org.graalvm.compiler.core.common.CompilationIdentifier; 34 import org.graalvm.compiler.core.common.type.StampFactory; 35 import org.graalvm.compiler.core.common.type.StampPair; 36 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 37 import org.graalvm.compiler.hotspot.HotSpotCompilationIdentifier; 38 import org.graalvm.compiler.hotspot.nodes.CurrentJavaThreadNode; 39 import org.graalvm.compiler.hotspot.word.HotSpotWordTypes; 40 import org.graalvm.compiler.nodes.ConstantNode; 41 import org.graalvm.compiler.nodes.FixedGuardNode; 42 import org.graalvm.compiler.nodes.FixedWithNextNode; 43 import org.graalvm.compiler.nodes.LogicNode; 44 import org.graalvm.compiler.nodes.NamedLocationIdentity; 45 import org.graalvm.compiler.nodes.StructuredGraph; 46 import org.graalvm.compiler.nodes.ValueNode; 47 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; 48 import org.graalvm.compiler.nodes.extended.GuardingNode; 49 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 50 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool; 51 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin; 52 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin; 53 import org.graalvm.compiler.nodes.graphbuilderconf.TypePlugin; 54 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType; 55 import org.graalvm.compiler.nodes.memory.ReadNode; 56 import org.graalvm.compiler.nodes.memory.address.AddressNode; 57 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; 58 import org.graalvm.compiler.nodes.util.ConstantFoldUtil; 59 import org.graalvm.compiler.word.Word; 60 import org.graalvm.compiler.word.WordOperationPlugin; 61 import jdk.internal.vm.compiler.word.LocationIdentity; 62 63 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 64 import jdk.vm.ci.meta.JavaConstant; 65 import jdk.vm.ci.meta.JavaKind; 66 import jdk.vm.ci.meta.JavaType; 67 import jdk.vm.ci.meta.JavaTypeProfile; 68 import jdk.vm.ci.meta.ResolvedJavaField; 69 import jdk.vm.ci.meta.ResolvedJavaMethod; 70 import jdk.vm.ci.meta.ResolvedJavaType; 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) { 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 static final Unsafe UNSAFE = initUnsafe(); 247 248 static Unsafe initUnsafe() { 249 try { 250 // Fast path when we are trusted. 251 return Unsafe.getUnsafe(); 252 } catch (SecurityException se) { 253 // Slow path when we are not trusted. 254 try { 255 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); 256 theUnsafe.setAccessible(true); 257 return (Unsafe) theUnsafe.get(Unsafe.class); 258 } catch (Exception e) { 259 throw new RuntimeException("exception while trying to get Unsafe", e); 260 } 261 } 262 } 263 } |