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