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