1 /* 2 * Copyright (c) 2009, 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.nodes.java; 24 25 import java.lang.ref.Reference; 26 import java.util.Collections; 27 28 import org.graalvm.compiler.core.common.type.StampFactory; 29 import org.graalvm.compiler.core.common.type.TypeReference; 30 import org.graalvm.compiler.graph.NodeClass; 31 import org.graalvm.compiler.nodeinfo.NodeInfo; 32 import org.graalvm.compiler.nodes.ConstantNode; 33 import org.graalvm.compiler.nodes.FrameState; 34 import org.graalvm.compiler.nodes.ValueNode; 35 import org.graalvm.compiler.nodes.spi.VirtualizableAllocation; 36 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 37 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode; 38 39 import jdk.vm.ci.meta.ResolvedJavaField; 40 import jdk.vm.ci.meta.ResolvedJavaType; 41 42 /** 43 * The {@code NewInstanceNode} represents the allocation of an instance class object. 44 */ 45 @NodeInfo(nameTemplate = "New {p#instanceClass/s}") 46 public class NewInstanceNode extends AbstractNewObjectNode implements VirtualizableAllocation { 47 48 public static final NodeClass<NewInstanceNode> TYPE = NodeClass.create(NewInstanceNode.class); 49 protected final ResolvedJavaType instanceClass; 50 51 public NewInstanceNode(ResolvedJavaType type, boolean fillContents) { 52 this(TYPE, type, fillContents, null); 53 } 54 55 public NewInstanceNode(ResolvedJavaType type, boolean fillContents, FrameState stateBefore) { 56 this(TYPE, type, fillContents, stateBefore); 57 } 58 59 protected NewInstanceNode(NodeClass<? extends NewInstanceNode> c, ResolvedJavaType type, boolean fillContents, FrameState stateBefore) { 60 super(c, StampFactory.objectNonNull(TypeReference.createExactTrusted(type)), fillContents, stateBefore); 61 assert !type.isArray() && !type.isInterface() && !type.isPrimitive(); 62 this.instanceClass = type; 63 } 64 65 /** 66 * Gets the instance class being allocated by this node. 67 * 68 * @return the instance class allocated 69 */ 70 public ResolvedJavaType instanceClass() { 71 return instanceClass; 72 } 73 74 @Override 75 public void virtualize(VirtualizerTool tool) { 76 /* 77 * Reference objects can escape into their ReferenceQueue at any safepoint, therefore 78 * they're excluded from escape analysis. 79 */ 80 if (!tool.getMetaAccessProvider().lookupJavaType(Reference.class).isAssignableFrom(instanceClass)) { 81 VirtualInstanceNode virtualObject = createVirtualInstanceNode(true); 82 ResolvedJavaField[] fields = virtualObject.getFields(); 83 ValueNode[] state = new ValueNode[fields.length]; 84 for (int i = 0; i < state.length; i++) { 85 state[i] = defaultFieldValue(fields[i]); 86 } 87 tool.createVirtualObject(virtualObject, state, Collections.<MonitorIdNode> emptyList(), false); 88 tool.replaceWithVirtual(virtualObject); 89 } 90 } 91 92 protected VirtualInstanceNode createVirtualInstanceNode(boolean hasIdentity) { 93 return new VirtualInstanceNode(instanceClass(), hasIdentity); 94 } 95 96 /* Factored out in a separate method so that subclasses can override it. */ 97 protected ConstantNode defaultFieldValue(ResolvedJavaField field) { 98 return ConstantNode.defaultForKind(field.getType().getJavaKind(), graph()); 99 } 100 }