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