1 /* 2 * Copyright (c) 2013, 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.replacements.nodes; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN; 27 import static jdk.vm.ci.code.BytecodeFrame.isPlaceholderBci; 28 29 import org.graalvm.compiler.api.replacements.MethodSubstitution; 30 import org.graalvm.compiler.api.replacements.Snippet; 31 import org.graalvm.compiler.core.common.type.StampPair; 32 import org.graalvm.compiler.debug.Debug; 33 import org.graalvm.compiler.debug.Debug.Scope; 34 import org.graalvm.compiler.debug.GraalError; 35 import org.graalvm.compiler.graph.NodeClass; 36 import org.graalvm.compiler.graph.NodeInputList; 37 import org.graalvm.compiler.nodeinfo.NodeInfo; 38 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; 39 import org.graalvm.compiler.nodes.FixedWithNextNode; 40 import org.graalvm.compiler.nodes.FrameState; 41 import org.graalvm.compiler.nodes.InvokeNode; 42 import org.graalvm.compiler.nodes.StructuredGraph; 43 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage; 44 import org.graalvm.compiler.nodes.ValueNode; 45 import org.graalvm.compiler.nodes.java.MethodCallTargetNode; 46 import org.graalvm.compiler.nodes.spi.Lowerable; 47 import org.graalvm.compiler.nodes.spi.LoweringTool; 48 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 49 import org.graalvm.compiler.phases.common.FrameStateAssignmentPhase; 50 import org.graalvm.compiler.phases.common.GuardLoweringPhase; 51 import org.graalvm.compiler.phases.common.LoweringPhase; 52 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase; 53 import org.graalvm.compiler.phases.common.inlining.InliningUtil; 54 import org.graalvm.compiler.phases.tiers.PhaseContext; 55 56 import jdk.vm.ci.meta.JavaKind; 57 import jdk.vm.ci.meta.ResolvedJavaMethod; 58 59 /** 60 * Macro nodes can be used to temporarily replace an invoke. They can, for example, be used to 61 * implement constant folding for known JDK functions like {@link Class#isInterface()}.<br/> 62 * <br/> 63 * During lowering, multiple sources are queried in order to look for a replacement: 64 * <ul> 65 * <li>If {@link #getLoweredSnippetGraph(LoweringTool)} returns a non-null result, this graph is 66 * used as a replacement.</li> 67 * <li>If a {@link MethodSubstitution} for the target method is found, this substitution is used as 68 * a replacement.</li> 69 * <li>Otherwise, the macro node is replaced with an {@link InvokeNode}. Note that this is only 70 * possible if the macro node is a {@link MacroStateSplitNode}.</li> 71 * </ul> 72 */ 73 //@formatter:off 74 @NodeInfo(cycles = CYCLES_UNKNOWN, 75 cyclesRationale = "If this node is not optimized away it will be lowered to a call, which we cannot estimate", 76 size = SIZE_UNKNOWN, 77 sizeRationale = "If this node is not optimized away it will be lowered to a call, which we cannot estimate") 78 //@formatter:on 79 public abstract class MacroNode extends FixedWithNextNode implements Lowerable { 80 81 public static final NodeClass<MacroNode> TYPE = NodeClass.create(MacroNode.class); 82 @Input protected NodeInputList<ValueNode> arguments; 83 84 protected final int bci; 85 protected final ResolvedJavaMethod targetMethod; 86 protected final StampPair returnStamp; 87 protected final InvokeKind invokeKind; 88 89 protected MacroNode(NodeClass<? extends MacroNode> c, InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode... arguments) { 90 super(c, returnStamp.getTrustedStamp()); 91 assert targetMethod.getSignature().getParameterCount(!targetMethod.isStatic()) == arguments.length; 92 this.arguments = new NodeInputList<>(this, arguments); 93 this.bci = bci; 94 this.targetMethod = targetMethod; 95 this.returnStamp = returnStamp; 96 this.invokeKind = invokeKind; 97 assert !isPlaceholderBci(bci); 98 } 99 100 public int getBci() { 101 return bci; 102 } 103 104 public ResolvedJavaMethod getTargetMethod() { 105 return targetMethod; 106 } 107 108 protected FrameState stateAfter() { 109 return null; 110 } 111 112 /** 113 * Gets a snippet to be used for lowering this macro node. The returned graph (if non-null) must 114 * have been {@linkplain #lowerReplacement(StructuredGraph, LoweringTool) lowered}. 115 */ 116 @SuppressWarnings("unused") 117 protected StructuredGraph getLoweredSnippetGraph(LoweringTool tool) { 118 return null; 119 } 120 121 /** 122 * Applies {@linkplain LoweringPhase lowering} to a replacement graph. 123 * 124 * @param replacementGraph a replacement (i.e., snippet or method substitution) graph 125 */ 126 @SuppressWarnings("try") 127 protected StructuredGraph lowerReplacement(final StructuredGraph replacementGraph, LoweringTool tool) { 128 final PhaseContext c = new PhaseContext(tool.getMetaAccess(), tool.getConstantReflection(), tool.getConstantFieldProvider(), tool.getLowerer(), tool.getReplacements(), 129 tool.getStampProvider(), tool.getNodeCostProvider()); 130 if (!graph().hasValueProxies()) { 131 new RemoveValueProxyPhase().apply(replacementGraph); 132 } 133 GuardsStage guardsStage = graph().getGuardsStage(); 134 if (!guardsStage.allowsFloatingGuards()) { 135 new GuardLoweringPhase().apply(replacementGraph, null); 136 if (guardsStage.areFrameStatesAtDeopts()) { 137 new FrameStateAssignmentPhase().apply(replacementGraph); 138 } 139 } 140 try (Scope s = Debug.scope("LoweringSnippetTemplate", replacementGraph)) { 141 new LoweringPhase(new CanonicalizerPhase(), tool.getLoweringStage()).apply(replacementGraph, c); 142 } catch (Throwable e) { 143 throw Debug.handle(e); 144 } 145 return replacementGraph; 146 } 147 148 @Override 149 public void lower(LoweringTool tool) { 150 StructuredGraph replacementGraph = getLoweredSnippetGraph(tool); 151 152 InvokeNode invoke = replaceWithInvoke(); 153 assert invoke.verify(); 154 155 if (replacementGraph != null) { 156 // Pull out the receiver null check so that a replaced 157 // receiver can be lowered if necessary 158 if (!targetMethod.isStatic()) { 159 ValueNode nonNullReceiver = InliningUtil.nonNullReceiver(invoke); 160 if (nonNullReceiver instanceof Lowerable) { 161 ((Lowerable) nonNullReceiver).lower(tool); 162 } 163 } 164 InliningUtil.inline(invoke, replacementGraph, false, null, targetMethod); 165 Debug.dump(Debug.INFO_LOG_LEVEL, graph(), "After inlining replacement %s", replacementGraph); 166 } else { 167 if (isPlaceholderBci(invoke.bci())) { 168 throw new GraalError("%s: cannot lower to invoke with placeholder BCI: %s", graph(), this); 169 } 170 171 if (invoke.stateAfter() == null) { 172 ResolvedJavaMethod method = graph().method(); 173 if (method.getAnnotation(MethodSubstitution.class) != null || method.getAnnotation(Snippet.class) != null) { 174 // One cause for this is that a MacroNode is created for a method that 175 // no longer needs a MacroNode. For example, Class.getComponentType() 176 // only needs a MacroNode prior to JDK9 as it was given a non-native 177 // implementation in JDK9. 178 throw new GraalError("%s macro created for call to %s in %s must be lowerable to a snippet or intrinsic graph. " + 179 "Maybe a macro node is not needed for this method in the current JDK?", getClass().getSimpleName(), targetMethod.format("%h.%n(%p)"), graph()); 180 } 181 throw new GraalError("%s: cannot lower to invoke without state: %s", graph(), this); 182 } 183 invoke.lower(tool); 184 } 185 } 186 187 protected InvokeNode replaceWithInvoke() { 188 InvokeNode invoke = createInvoke(); 189 graph().replaceFixedWithFixed(this, invoke); 190 return invoke; 191 } 192 193 protected InvokeNode createInvoke() { 194 MethodCallTargetNode callTarget = graph().add(new MethodCallTargetNode(invokeKind, targetMethod, arguments.toArray(new ValueNode[arguments.size()]), returnStamp, null)); 195 InvokeNode invoke = graph().add(new InvokeNode(callTarget, bci)); 196 if (stateAfter() != null) { 197 invoke.setStateAfter(stateAfter().duplicate()); 198 if (getStackKind() != JavaKind.Void) { 199 invoke.stateAfter().replaceFirstInput(this, invoke); 200 } 201 } 202 return invoke; 203 } 204 }