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