1 /* 2 * Copyright (c) 2013, 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.replacements; 26 27 import java.lang.reflect.Method; 28 29 import org.graalvm.compiler.core.common.type.AbstractPointerStamp; 30 import org.graalvm.compiler.core.common.type.Stamp; 31 import org.graalvm.compiler.core.common.type.StampPair; 32 import org.graalvm.compiler.debug.DebugContext; 33 import org.graalvm.compiler.graph.NodeClass; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; 36 import org.graalvm.compiler.nodes.NodeView; 37 import org.graalvm.compiler.nodes.ParameterNode; 38 import org.graalvm.compiler.nodes.ReturnNode; 39 import org.graalvm.compiler.nodes.StructuredGraph; 40 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 41 import org.graalvm.compiler.nodes.ValueNode; 42 import org.graalvm.compiler.nodes.java.LoadFieldNode; 43 import org.graalvm.compiler.nodes.java.NewInstanceNode; 44 import org.graalvm.compiler.nodes.java.StoreFieldNode; 45 import org.graalvm.compiler.nodes.spi.LoweringTool; 46 import org.graalvm.compiler.nodes.spi.Replacements; 47 import org.graalvm.compiler.nodes.type.StampTool; 48 import org.graalvm.compiler.replacements.nodes.BasicObjectCloneNode; 49 50 import jdk.vm.ci.meta.Assumptions; 51 import jdk.vm.ci.meta.ResolvedJavaField; 52 import jdk.vm.ci.meta.ResolvedJavaMethod; 53 import jdk.vm.ci.meta.ResolvedJavaType; 54 55 @NodeInfo 56 public final class ObjectCloneNode extends BasicObjectCloneNode { 57 58 public static final NodeClass<ObjectCloneNode> TYPE = NodeClass.create(ObjectCloneNode.class); 59 60 public ObjectCloneNode(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode receiver) { 61 super(TYPE, invokeKind, targetMethod, bci, returnStamp, receiver); 62 } 63 64 @Override 65 protected Stamp computeStamp(ValueNode object) { 66 if (getConcreteType(object.stamp(NodeView.DEFAULT)) != null) { 67 return AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT)); 68 } 69 /* 70 * If this call can't be intrinsified don't report a non-null stamp, otherwise the stamp 71 * would change when this is lowered back to an invoke and we might lose a null check. 72 */ 73 return AbstractPointerStamp.pointerMaybeNull(object.stamp(NodeView.DEFAULT)); 74 } 75 76 @Override 77 @SuppressWarnings("try") 78 protected StructuredGraph getLoweredSnippetGraph(LoweringTool tool) { 79 ResolvedJavaType type = StampTool.typeOrNull(getObject()); 80 if (type != null) { 81 if (type.isArray()) { 82 Method method = ObjectCloneSnippets.arrayCloneMethods.get(type.getComponentType().getJavaKind()); 83 if (method != null) { 84 final ResolvedJavaMethod snippetMethod = tool.getMetaAccess().lookupJavaMethod(method); 85 final Replacements replacements = tool.getReplacements(); 86 StructuredGraph snippetGraph = null; 87 DebugContext debug = getDebug(); 88 try (DebugContext.Scope s = debug.scope("ArrayCloneSnippet", snippetMethod)) { 89 snippetGraph = replacements.getSnippet(snippetMethod, null, graph().trackNodeSourcePosition(), this.getNodeSourcePosition()); 90 } catch (Throwable e) { 91 throw debug.handle(e); 92 } 93 94 assert snippetGraph != null : "ObjectCloneSnippets should be installed"; 95 assert getConcreteType(stamp(NodeView.DEFAULT)) != null; 96 return lowerReplacement((StructuredGraph) snippetGraph.copy(getDebug()), tool); 97 } 98 assert false : "unhandled array type " + type.getComponentType().getJavaKind(); 99 } else { 100 Assumptions assumptions = graph().getAssumptions(); 101 type = getConcreteType(getObject().stamp(NodeView.DEFAULT)); 102 if (type != null) { 103 StructuredGraph newGraph = new StructuredGraph.Builder(graph().getOptions(), graph().getDebug(), AllowAssumptions.ifNonNull(assumptions)).name("<clone>").build(); 104 ParameterNode param = newGraph.addWithoutUnique(new ParameterNode(0, StampPair.createSingle(getObject().stamp(NodeView.DEFAULT)))); 105 NewInstanceNode newInstance = newGraph.add(new NewInstanceNode(type, true)); 106 newGraph.addAfterFixed(newGraph.start(), newInstance); 107 ReturnNode returnNode = newGraph.add(new ReturnNode(newInstance)); 108 newGraph.addAfterFixed(newInstance, returnNode); 109 110 for (ResolvedJavaField field : type.getInstanceFields(true)) { 111 LoadFieldNode load = newGraph.add(LoadFieldNode.create(newGraph.getAssumptions(), param, field)); 112 newGraph.addBeforeFixed(returnNode, load); 113 newGraph.addBeforeFixed(returnNode, newGraph.add(new StoreFieldNode(newInstance, field, load))); 114 } 115 assert getConcreteType(stamp(NodeView.DEFAULT)) != null; 116 return lowerReplacement(newGraph, tool); 117 } 118 } 119 } 120 assert getConcreteType(stamp(NodeView.DEFAULT)) == null; 121 return null; 122 } 123 }