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