1 /*
   2  * Copyright (c) 2013, 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 java.util.Collections;
  26 
  27 import org.graalvm.compiler.core.common.type.ObjectStamp;
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.core.common.type.StampPair;
  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.ValueNode;
  35 import org.graalvm.compiler.nodes.java.LoadFieldNode;
  36 import org.graalvm.compiler.nodes.java.MonitorIdNode;
  37 import org.graalvm.compiler.nodes.spi.ArrayLengthProvider;
  38 import org.graalvm.compiler.nodes.spi.VirtualizableAllocation;
  39 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  40 import org.graalvm.compiler.nodes.util.GraphUtil;
  41 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
  42 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  43 
  44 import jdk.vm.ci.meta.Assumptions;
  45 import jdk.vm.ci.meta.ResolvedJavaField;
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 import jdk.vm.ci.meta.ResolvedJavaType;
  48 
  49 @NodeInfo
  50 public abstract class BasicObjectCloneNode extends MacroStateSplitNode implements VirtualizableAllocation, ArrayLengthProvider {
  51 
  52     public static final NodeClass<BasicObjectCloneNode> TYPE = NodeClass.create(BasicObjectCloneNode.class);
  53 
  54     public BasicObjectCloneNode(NodeClass<? extends MacroNode> c, InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode... arguments) {
  55         super(c, invokeKind, targetMethod, bci, returnStamp, arguments);
  56         updateStamp(computeStamp(getObject()));
  57     }
  58 
  59     @Override
  60     public boolean inferStamp() {
  61         return updateStamp(computeStamp(getObject()));
  62     }
  63 
  64     private static Stamp computeStamp(ValueNode object) {
  65         Stamp objectStamp = object.stamp();
  66         if (objectStamp instanceof ObjectStamp) {
  67             objectStamp = objectStamp.join(StampFactory.objectNonNull());
  68         }
  69         return objectStamp;
  70     }
  71 
  72     public ValueNode getObject() {
  73         return arguments.get(0);
  74     }
  75 
  76     /*
  77      * Looks at the given stamp and determines if it is an exact type (or can be assumed to be an
  78      * exact type) and if it is a cloneable type.
  79      *
  80      * If yes, then the exact type is returned, otherwise it returns null.
  81      */
  82     protected static ResolvedJavaType getConcreteType(Stamp stamp) {
  83         if (!(stamp instanceof ObjectStamp)) {
  84             return null;
  85         }
  86         ObjectStamp objectStamp = (ObjectStamp) stamp;
  87         if (objectStamp.type() == null) {
  88             return null;
  89         } else if (objectStamp.isExactType()) {
  90             return objectStamp.type().isCloneableWithAllocation() ? objectStamp.type() : null;
  91         }
  92         return null;
  93     }
  94 
  95     protected LoadFieldNode genLoadFieldNode(Assumptions assumptions, ValueNode originalAlias, ResolvedJavaField field) {
  96         return LoadFieldNode.create(assumptions, originalAlias, field);
  97     }
  98 
  99     @Override
 100     public void virtualize(VirtualizerTool tool) {
 101         ValueNode originalAlias = tool.getAlias(getObject());
 102         if (originalAlias instanceof VirtualObjectNode) {
 103             VirtualObjectNode originalVirtual = (VirtualObjectNode) originalAlias;
 104             if (originalVirtual.type().isCloneableWithAllocation()) {
 105                 ValueNode[] newEntryState = new ValueNode[originalVirtual.entryCount()];
 106                 for (int i = 0; i < newEntryState.length; i++) {
 107                     newEntryState[i] = tool.getEntry(originalVirtual, i);
 108                 }
 109                 VirtualObjectNode newVirtual = originalVirtual.duplicate();
 110                 tool.createVirtualObject(newVirtual, newEntryState, Collections.<MonitorIdNode> emptyList(), false);
 111                 tool.replaceWithVirtual(newVirtual);
 112             }
 113         } else {
 114             ResolvedJavaType type = getConcreteType(originalAlias.stamp());
 115             if (type != null && !type.isArray()) {
 116                 VirtualInstanceNode newVirtual = createVirtualInstanceNode(type, true);
 117                 ResolvedJavaField[] fields = newVirtual.getFields();
 118 
 119                 ValueNode[] state = new ValueNode[fields.length];
 120                 final LoadFieldNode[] loads = new LoadFieldNode[fields.length];
 121                 for (int i = 0; i < fields.length; i++) {
 122                     state[i] = loads[i] = genLoadFieldNode(graph().getAssumptions(), originalAlias, fields[i]);
 123                     tool.addNode(loads[i]);
 124                 }
 125                 tool.createVirtualObject(newVirtual, state, Collections.<MonitorIdNode> emptyList(), false);
 126                 tool.replaceWithVirtual(newVirtual);
 127             }
 128         }
 129     }
 130 
 131     protected VirtualInstanceNode createVirtualInstanceNode(ResolvedJavaType type, boolean hasIdentity) {
 132         return new VirtualInstanceNode(type, hasIdentity);
 133     }
 134 
 135     @Override
 136     public ValueNode length() {
 137         return GraphUtil.arrayLength(getObject());
 138     }
 139 }