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.nodes.graphbuilderconf; 26 27 import static jdk.vm.ci.meta.DeoptimizationAction.InvalidateReprofile; 28 import static jdk.vm.ci.meta.DeoptimizationReason.NullCheckException; 29 import static org.graalvm.compiler.core.common.type.StampFactory.objectNonNull; 30 31 import org.graalvm.compiler.bytecode.Bytecode; 32 import org.graalvm.compiler.bytecode.BytecodeProvider; 33 import org.graalvm.compiler.core.common.type.AbstractPointerStamp; 34 import org.graalvm.compiler.core.common.type.Stamp; 35 import org.graalvm.compiler.core.common.type.StampFactory; 36 import org.graalvm.compiler.core.common.type.StampPair; 37 import org.graalvm.compiler.debug.GraalError; 38 import org.graalvm.compiler.nodes.AbstractBeginNode; 39 import org.graalvm.compiler.nodes.AbstractMergeNode; 40 import org.graalvm.compiler.nodes.CallTargetNode; 41 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; 42 import org.graalvm.compiler.nodes.ConstantNode; 43 import org.graalvm.compiler.nodes.DynamicPiNode; 44 import org.graalvm.compiler.nodes.FixedGuardNode; 45 import org.graalvm.compiler.nodes.Invoke; 46 import org.graalvm.compiler.nodes.LogicNode; 47 import org.graalvm.compiler.nodes.NodeView; 48 import org.graalvm.compiler.nodes.PiNode; 49 import org.graalvm.compiler.nodes.StateSplit; 50 import org.graalvm.compiler.nodes.StructuredGraph; 51 import org.graalvm.compiler.nodes.ValueNode; 52 import org.graalvm.compiler.nodes.calc.IsNullNode; 53 import org.graalvm.compiler.nodes.calc.NarrowNode; 54 import org.graalvm.compiler.nodes.calc.SignExtendNode; 55 import org.graalvm.compiler.nodes.calc.ZeroExtendNode; 56 import org.graalvm.compiler.nodes.extended.BytecodeExceptionNode.BytecodeExceptionKind; 57 import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode; 58 import org.graalvm.compiler.nodes.type.StampTool; 59 60 import jdk.vm.ci.code.BailoutException; 61 import jdk.vm.ci.meta.Assumptions; 62 import jdk.vm.ci.meta.DeoptimizationAction; 63 import jdk.vm.ci.meta.DeoptimizationReason; 64 import jdk.vm.ci.meta.JavaKind; 65 import jdk.vm.ci.meta.JavaType; 66 import jdk.vm.ci.meta.ResolvedJavaMethod; 67 68 /** 69 * Used by a {@link GraphBuilderPlugin} to interface with an object that parses the bytecode of a 70 * single {@linkplain #getMethod() method} as part of building a {@linkplain #getGraph() graph} . 71 */ 72 public interface GraphBuilderContext extends GraphBuilderTool { 73 74 /** 75 * Pushes a given value to the frame state stack using an explicit kind. This should be used 76 * when {@code value.getJavaKind()} is different from the kind that the bytecode instruction 77 * currently being parsed pushes to the stack. 78 * 79 * @param kind the kind to use when type checking this operation 80 * @param value the value to push to the stack. The value must already have been 81 * {@linkplain #append(ValueNode) appended}. 82 */ 83 void push(JavaKind kind, ValueNode value); 84 85 /** 86 * Pops a value from the frame state stack using an explicit kind. 87 * 88 * @param slotKind the kind to use when type checking this operation 89 * @return the value on the top of the stack 90 */ 91 default ValueNode pop(JavaKind slotKind) { 92 throw GraalError.unimplemented(); 93 } 94 95 /** 96 * Adds a node and all its inputs to the graph. If the node is in the graph, returns 97 * immediately. If the node is a {@link StateSplit} with a null 98 * {@linkplain StateSplit#stateAfter() frame state} , the frame state is initialized. 99 * 100 * @param value the value to add to the graph and push to the stack. The 101 * {@code value.getJavaKind()} kind is used when type checking this operation. 102 * @return a node equivalent to {@code value} in the graph 103 */ 104 default <T extends ValueNode> T add(T value) { 105 if (value.graph() != null) { 106 assert !(value instanceof StateSplit) || ((StateSplit) value).stateAfter() != null; 107 return value; 108 } 109 return GraphBuilderContextUtil.setStateAfterIfNecessary(this, append(value)); 110 } 111 112 default ValueNode addNonNullCast(ValueNode value) { 113 AbstractPointerStamp valueStamp = (AbstractPointerStamp) value.stamp(NodeView.DEFAULT); 114 if (valueStamp.nonNull()) { 115 return value; 116 } else { 117 LogicNode isNull = add(IsNullNode.create(value)); 118 FixedGuardNode fixedGuard = add(new FixedGuardNode(isNull, DeoptimizationReason.NullCheckException, DeoptimizationAction.None, true)); 119 Stamp newStamp = valueStamp.improveWith(StampFactory.objectNonNull()); 120 return add(PiNode.create(value, newStamp, fixedGuard)); 121 } 122 } 123 124 /** 125 * Adds a node with a non-void kind to the graph, pushes it to the stack. If the returned node 126 * is a {@link StateSplit} with a null {@linkplain StateSplit#stateAfter() frame state}, the 127 * frame state is initialized. 128 * 129 * @param kind the kind to use when type checking this operation 130 * @param value the value to add to the graph and push to the stack 131 * @return a node equivalent to {@code value} in the graph 132 */ 133 default <T extends ValueNode> T addPush(JavaKind kind, T value) { 134 T equivalentValue = value.graph() != null ? value : append(value); 135 push(kind, equivalentValue); 136 return GraphBuilderContextUtil.setStateAfterIfNecessary(this, equivalentValue); 137 } 138 139 /** 140 * Handles an invocation that a plugin determines can replace the original invocation (i.e., the 141 * one for which the plugin was applied). This applies all standard graph builder processing to 142 * the replaced invocation including applying any relevant plugins. 143 * 144 * @param invokeKind the kind of the replacement invocation 145 * @param targetMethod the target of the replacement invocation 146 * @param args the arguments to the replacement invocation 147 * @param forceInlineEverything specifies if all invocations encountered in the scope of 148 * handling the replaced invoke are to be force inlined 149 */ 150 Invoke handleReplacedInvoke(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, boolean forceInlineEverything); 151 152 void handleReplacedInvoke(CallTargetNode callTarget, JavaKind resultType); 153 154 /** 155 * Intrinsifies an invocation of a given method by inlining the bytecodes of a given 156 * substitution method. 157 * 158 * @param bytecodeProvider used to get the bytecodes to parse for the substitution method 159 * @param targetMethod the method being intrinsified 160 * @param substitute the intrinsic implementation 161 * @param receiver the receiver, or null for static methods 162 * @param argsIncludingReceiver the arguments with which to inline the invocation 163 * 164 * @return whether the intrinsification was successful 165 */ 166 boolean intrinsify(BytecodeProvider bytecodeProvider, ResolvedJavaMethod targetMethod, ResolvedJavaMethod substitute, InvocationPlugin.Receiver receiver, ValueNode[] argsIncludingReceiver); 167 168 /** 169 * Intrinsifies an invocation of a given method by inlining the graph of a given substitution 170 * method. 171 * 172 * @param targetMethod the method being intrinsified 173 * @param substituteGraph the intrinsic implementation 174 * @param receiver the receiver, or null for static methods 175 * @param argsIncludingReceiver the arguments with which to inline the invocation 176 * 177 * @return whether the intrinsification was successful 178 */ 179 boolean intrinsify(ResolvedJavaMethod targetMethod, StructuredGraph substituteGraph, InvocationPlugin.Receiver receiver, ValueNode[] argsIncludingReceiver); 180 181 /** 182 * Creates a snap shot of the current frame state with the BCI of the instruction after the one 183 * currently being parsed and assigns it to a given {@linkplain StateSplit#hasSideEffect() side 184 * effect} node. 185 * 186 * @param sideEffect a side effect node just appended to the graph 187 */ 188 void setStateAfter(StateSplit sideEffect); 189 190 /** 191 * Gets the parsing context for the method that inlines the method being parsed by this context. 192 */ 193 GraphBuilderContext getParent(); 194 195 /** 196 * Gets the first ancestor parsing context that is not parsing a {@linkplain #parsingIntrinsic() 197 * intrinsic}. 198 */ 199 default GraphBuilderContext getNonIntrinsicAncestor() { 200 GraphBuilderContext ancestor = getParent(); 201 while (ancestor != null && ancestor.parsingIntrinsic()) { 202 ancestor = ancestor.getParent(); 203 } 204 return ancestor; 205 } 206 207 /** 208 * Gets the code being parsed. 209 */ 210 Bytecode getCode(); 211 212 /** 213 * Gets the method being parsed by this context. 214 */ 215 ResolvedJavaMethod getMethod(); 216 217 /** 218 * Gets the index of the bytecode instruction currently being parsed. 219 */ 220 int bci(); 221 222 /** 223 * Gets the kind of invocation currently being parsed. 224 */ 225 InvokeKind getInvokeKind(); 226 227 /** 228 * Gets the return type of the invocation currently being parsed. 229 */ 230 JavaType getInvokeReturnType(); 231 232 default StampPair getInvokeReturnStamp(Assumptions assumptions) { 233 JavaType returnType = getInvokeReturnType(); 234 return StampFactory.forDeclaredType(assumptions, returnType, false); 235 } 236 237 /** 238 * Gets the inline depth of this context. A return value of 0 implies that this is the context 239 * for the parse root. 240 */ 241 default int getDepth() { 242 GraphBuilderContext parent = getParent(); 243 int result = 0; 244 while (parent != null) { 245 result++; 246 parent = parent.getParent(); 247 } 248 return result; 249 } 250 251 /** 252 * Determines if this parsing context is within the bytecode of an intrinsic or a method inlined 253 * by an intrinsic. 254 */ 255 @Override 256 default boolean parsingIntrinsic() { 257 return getIntrinsic() != null; 258 } 259 260 /** 261 * Gets the intrinsic of the current parsing context or {@code null} if not 262 * {@link #parsingIntrinsic() parsing an intrinsic}. 263 */ 264 IntrinsicContext getIntrinsic(); 265 266 BailoutException bailout(String string); 267 268 default ValueNode nullCheckedValue(ValueNode value) { 269 return nullCheckedValue(value, InvalidateReprofile); 270 } 271 272 /** 273 * Gets a version of a given value that has a {@linkplain StampTool#isPointerNonNull(ValueNode) 274 * non-null} stamp. 275 */ 276 default ValueNode nullCheckedValue(ValueNode value, DeoptimizationAction action) { 277 if (!StampTool.isPointerNonNull(value)) { 278 LogicNode condition = getGraph().unique(IsNullNode.create(value)); 279 FixedGuardNode fixedGuard = append(new FixedGuardNode(condition, NullCheckException, action, true)); 280 ValueNode nonNullReceiver = getGraph().addOrUniqueWithInputs(PiNode.create(value, objectNonNull(), fixedGuard)); 281 // TODO: Propogating the non-null into the frame state would 282 // remove subsequent null-checks on the same value. However, 283 // it currently causes an assertion failure when merging states. 284 // 285 // frameState.replace(value, nonNullReceiver); 286 return nonNullReceiver; 287 } 288 return value; 289 } 290 291 default void genCheckcastDynamic(ValueNode object, ValueNode javaClass) { 292 LogicNode condition = InstanceOfDynamicNode.create(getAssumptions(), getConstantReflection(), javaClass, object, true); 293 if (condition.isTautology()) { 294 addPush(JavaKind.Object, object); 295 } else { 296 append(condition); 297 FixedGuardNode fixedGuard = add(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.InvalidateReprofile, false)); 298 addPush(JavaKind.Object, DynamicPiNode.create(getAssumptions(), getConstantReflection(), object, fixedGuard, javaClass)); 299 } 300 } 301 302 @SuppressWarnings("unused") 303 default void notifyReplacedCall(ResolvedJavaMethod targetMethod, ConstantNode node) { 304 305 } 306 307 /** 308 * Interface whose instances hold inlining information about the current context, in a wider 309 * sense. The wider sense in this case concerns graph building approaches that don't necessarily 310 * keep a chain of {@link GraphBuilderContext} instances normally available through 311 * {@linkplain #getParent()}. Examples of such approaches are partial evaluation and incremental 312 * inlining. 313 */ 314 interface ExternalInliningContext { 315 int getInlinedDepth(); 316 } 317 318 default ExternalInliningContext getExternalInliningContext() { 319 return null; 320 } 321 322 /** 323 * Adds masking to a given subword value according to a given {@Link JavaKind}, such that the 324 * masked value falls in the range of the given kind. In the cases where the given kind is not a 325 * subword kind, the input value is returned immediately. 326 * 327 * @param value the value to be masked 328 * @param kind the kind that specifies the range of the masked value 329 * @return the masked value 330 */ 331 default ValueNode maskSubWordValue(ValueNode value, JavaKind kind) { 332 if (kind == kind.getStackKind()) { 333 return value; 334 } 335 // Subword value 336 ValueNode narrow = append(NarrowNode.create(value, kind.getBitCount(), NodeView.DEFAULT)); 337 if (kind.isUnsigned()) { 338 return append(ZeroExtendNode.create(narrow, 32, NodeView.DEFAULT)); 339 } else { 340 return append(SignExtendNode.create(narrow, 32, NodeView.DEFAULT)); 341 } 342 } 343 344 /** 345 * @return true if an explicit exception check should be emitted. 346 */ 347 default boolean needsExplicitException() { 348 return false; 349 } 350 351 /** 352 * Generates an exception edge for the current bytecode. When {@link #needsExplicitException()} 353 * returns true, this method should return non-null begin nodes. 354 * 355 * @param exceptionKind the type of exception to be created. 356 * @return a begin node that precedes the actual exception instantiation code. 357 */ 358 default AbstractBeginNode genExplicitExceptionEdge(@SuppressWarnings("ununsed") BytecodeExceptionKind exceptionKind) { 359 return null; 360 } 361 } 362 363 class GraphBuilderContextUtil { 364 static <T extends ValueNode> T setStateAfterIfNecessary(GraphBuilderContext b, T value) { 365 if (value instanceof StateSplit) { 366 StateSplit stateSplit = (StateSplit) value; 367 if (stateSplit.stateAfter() == null && (stateSplit.hasSideEffect() || stateSplit instanceof AbstractMergeNode)) { 368 b.setStateAfter(stateSplit); 369 } 370 } 371 return value; 372 } 373 }