1 /* 2 * Copyright (c) 2012, 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.hotspot.stubs; 26 27 import org.graalvm.compiler.api.replacements.Snippet; 28 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; 29 import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter; 30 import org.graalvm.compiler.core.common.CompilationIdentifier; 31 import org.graalvm.compiler.core.common.type.StampFactory; 32 import org.graalvm.compiler.debug.DebugContext; 33 import org.graalvm.compiler.debug.GraalError; 34 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 35 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 36 import org.graalvm.compiler.nodes.NodeView; 37 import org.graalvm.compiler.nodes.ParameterNode; 38 import org.graalvm.compiler.nodes.StructuredGraph; 39 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage; 40 import org.graalvm.compiler.nodes.spi.LoweringTool; 41 import org.graalvm.compiler.options.OptionValues; 42 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 43 import org.graalvm.compiler.phases.common.LoweringPhase; 44 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase; 45 import org.graalvm.compiler.phases.tiers.PhaseContext; 46 import org.graalvm.compiler.replacements.SnippetTemplate; 47 import org.graalvm.compiler.replacements.Snippets; 48 49 import jdk.vm.ci.meta.Local; 50 import jdk.vm.ci.meta.LocalVariableTable; 51 import jdk.vm.ci.meta.ResolvedJavaMethod; 52 53 /** 54 * Base class for a stub defined by a snippet. 55 */ 56 public abstract class SnippetStub extends Stub implements Snippets { 57 58 protected final ResolvedJavaMethod method; 59 60 /** 61 * Creates a new snippet stub. 62 * 63 * @param snippetMethodName name of the single {@link Snippet} annotated method in the class of 64 * this object 65 * @param linkage linkage details for a call to the stub 66 */ 67 public SnippetStub(String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 68 this(null, snippetMethodName, options, providers, linkage); 69 } 70 71 /** 72 * Creates a new snippet stub. 73 * 74 * @param snippetDeclaringClass this class in which the {@link Snippet} annotated method is 75 * declared. If {@code null}, this the class of this object is used. 76 * @param snippetMethodName name of the single {@link Snippet} annotated method in 77 * {@code snippetDeclaringClass} 78 * @param linkage linkage details for a call to the stub 79 */ 80 public SnippetStub(Class<? extends Snippets> snippetDeclaringClass, String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 81 super(options, providers, linkage); 82 this.method = SnippetTemplate.AbstractTemplates.findMethod(providers.getMetaAccess(), snippetDeclaringClass == null ? getClass() : snippetDeclaringClass, snippetMethodName); 83 registerSnippet(); 84 } 85 86 protected void registerSnippet() { 87 providers.getReplacements().registerSnippet(method, null, null, false); 88 } 89 90 @Override 91 @SuppressWarnings("try") 92 protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) { 93 // Stubs cannot have optimistic assumptions since they have 94 // to be valid for the entire run of the VM. 95 final StructuredGraph graph = buildInitialGraph(debug, compilationId, makeConstArgs()); 96 try (DebugContext.Scope outer = debug.scope("SnippetStub", graph)) { 97 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) { 98 int index = param.index(); 99 if (method.getParameterAnnotation(NonNullParameter.class, index) != null) { 100 param.setStamp(param.stamp(NodeView.DEFAULT).join(StampFactory.objectNonNull())); 101 } 102 } 103 104 new RemoveValueProxyPhase().apply(graph); 105 graph.setGuardsStage(GuardsStage.FLOATING_GUARDS); 106 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 107 PhaseContext context = new PhaseContext(providers); 108 canonicalizer.apply(graph, context); 109 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 110 } catch (Throwable e) { 111 throw debug.handle(e); 112 } 113 114 return graph; 115 } 116 117 protected StructuredGraph buildInitialGraph(DebugContext debug, CompilationIdentifier compilationId, Object[] args) { 118 return providers.getReplacements().getSnippet(method, args, false, null).copyWithIdentifier(compilationId, debug); 119 } 120 121 protected boolean checkConstArg(int index, String expectedName) { 122 assert method.getParameterAnnotation(ConstantParameter.class, index) != null : String.format("parameter %d of %s is expected to be constant", index, method.format("%H.%n(%p)")); 123 LocalVariableTable lvt = method.getLocalVariableTable(); 124 if (lvt != null) { 125 Local local = lvt.getLocal(index, 0); 126 assert local != null; 127 String actualName = local.getName(); 128 assert actualName.equals(expectedName) : String.format("parameter %d of %s is expected to be named %s, not %s", index, method.format("%H.%n(%p)"), expectedName, actualName); 129 } 130 return true; 131 } 132 133 protected Object[] makeConstArgs() { 134 int count = method.getSignature().getParameterCount(false); 135 Object[] args = new Object[count]; 136 for (int i = 0; i < args.length; i++) { 137 if (method.getParameterAnnotation(ConstantParameter.class, i) != null) { 138 args[i] = getConstantParameterValue(i, null); 139 } 140 } 141 return args; 142 } 143 144 protected Object getConstantParameterValue(int index, String name) { 145 throw new GraalError("%s must override getConstantParameterValue() to provide a value for parameter %d%s", getClass().getName(), index, name == null ? "" : " (" + name + ")"); 146 } 147 148 @Override 149 protected Object debugScopeContext() { 150 return getInstalledCodeOwner(); 151 } 152 153 @Override 154 public ResolvedJavaMethod getInstalledCodeOwner() { 155 return method; 156 } 157 158 @Override 159 public String toString() { 160 return "Stub<" + getInstalledCodeOwner().format("%h.%n") + ">"; 161 } 162 163 public ResolvedJavaMethod getMethod() { 164 return method; 165 } 166 }