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 ValueNode getArgument(int i) { 101 return arguments.get(i); 102 } 103 104 public int getArgumentCount() { 105 return arguments.size(); 106 } 107 108 public ValueNode[] toArgumentArray() { 109 return arguments.toArray(new ValueNode[0]); 110 } 111 112 public int getBci() { 113 return bci; 114 } 115 116 public ResolvedJavaMethod getTargetMethod() { 117 return targetMethod; 118 } 119 120 protected FrameState stateAfter() { 121 return null; 122 } 123 124 /** 125 * Gets a snippet to be used for lowering this macro node. The returned graph (if non-null) must 126 * have been {@linkplain #lowerReplacement(StructuredGraph, LoweringTool) lowered}. 127 */ 128 @SuppressWarnings("unused") 129 protected StructuredGraph getLoweredSnippetGraph(LoweringTool tool) { 130 return null; 131 } 132 133 /** 134 * Applies {@linkplain LoweringPhase lowering} to a replacement graph. 135 * 136 * @param replacementGraph a replacement (i.e., snippet or method substitution) graph 137 */ 138 @SuppressWarnings("try") 139 protected StructuredGraph lowerReplacement(final StructuredGraph replacementGraph, LoweringTool tool) { 140 final PhaseContext c = new PhaseContext(tool.getMetaAccess(), tool.getConstantReflection(), tool.getConstantFieldProvider(), tool.getLowerer(), tool.getReplacements(), 141 tool.getStampProvider()); 142 if (!graph().hasValueProxies()) { 143 new RemoveValueProxyPhase().apply(replacementGraph); 144 } 145 GuardsStage guardsStage = graph().getGuardsStage(); 146 if (!guardsStage.allowsFloatingGuards()) { 147 new GuardLoweringPhase().apply(replacementGraph, null); 148 if (guardsStage.areFrameStatesAtDeopts()) { 149 new FrameStateAssignmentPhase().apply(replacementGraph); 150 } 151 } 152 try (Scope s = Debug.scope("LoweringSnippetTemplate", replacementGraph)) { 153 new LoweringPhase(new CanonicalizerPhase(), tool.getLoweringStage()).apply(replacementGraph, c); 154 } catch (Throwable e) { 155 throw Debug.handle(e); 156 } 157 return replacementGraph; 158 } 159 160 @Override 161 public void lower(LoweringTool tool) { 162 StructuredGraph replacementGraph = getLoweredSnippetGraph(tool); 163 164 InvokeNode invoke = replaceWithInvoke(); 165 assert invoke.verify(); 166 167 if (replacementGraph != null) { 168 // Pull out the receiver null check so that a replaced 169 // receiver can be lowered if necessary 170 if (!targetMethod.isStatic()) { 171 ValueNode nonNullReceiver = InliningUtil.nonNullReceiver(invoke); 172 if (nonNullReceiver instanceof Lowerable) { 173 ((Lowerable) nonNullReceiver).lower(tool); 174 } 175 } 176 InliningUtil.inline(invoke, replacementGraph, false, targetMethod); 177 Debug.dump(Debug.DETAILED_LEVEL, graph(), "After inlining replacement %s", replacementGraph); 178 } else { 179 if (isPlaceholderBci(invoke.bci())) { 180 throw new GraalError("%s: cannot lower to invoke with placeholder BCI: %s", graph(), this); 181 } 182 183 if (invoke.stateAfter() == null) { 184 ResolvedJavaMethod method = graph().method(); 185 if (method.getAnnotation(MethodSubstitution.class) != null || method.getAnnotation(Snippet.class) != null) { 186 // One cause for this is that a MacroNode is created for a method that 187 // no longer needs a MacroNode. For example, Class.getComponentType() 188 // only needs a MacroNode prior to JDK9 as it was given a non-native 189 // implementation in JDK9. 190 throw new GraalError("%s macro created for call to %s in %s must be lowerable to a snippet or intrinsic graph. " + 191 "Maybe a macro node is not needed for this method in the current JDK?", getClass().getSimpleName(), targetMethod.format("%h.%n(%p)"), graph()); 192 } 193 throw new GraalError("%s: cannot lower to invoke without state: %s", graph(), this); 194 } 195 invoke.lower(tool); 196 } 197 } 198 199 public InvokeNode replaceWithInvoke() { 200 InvokeNode invoke = createInvoke(); 201 graph().replaceFixedWithFixed(this, invoke); 202 return invoke; 203 } 204 205 protected InvokeNode createInvoke() { 206 MethodCallTargetNode callTarget = graph().add(new MethodCallTargetNode(invokeKind, targetMethod, arguments.toArray(new ValueNode[arguments.size()]), returnStamp, null)); 207 InvokeNode invoke = graph().add(new InvokeNode(callTarget, bci)); 208 if (stateAfter() != null) { 209 invoke.setStateAfter(stateAfter().duplicate()); 210 if (getStackKind() != JavaKind.Void) { 211 invoke.stateAfter().replaceFirstInput(this, invoke); 212 } 213 } 214 return invoke; 215 } 216 }