1 /*
   2  * Copyright (c) 2011, 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.extended;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  27 
  28 import org.graalvm.compiler.core.common.type.ObjectStamp;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.graph.spi.Canonicalizable;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.ConstantNode;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.calc.FloatingNode;
  37 import org.graalvm.compiler.nodes.spi.Lowerable;
  38 import org.graalvm.compiler.nodes.spi.LoweringTool;
  39 import org.graalvm.compiler.nodes.spi.Virtualizable;
  40 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  41 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  42 
  43 import jdk.vm.ci.meta.Constant;
  44 import jdk.vm.ci.meta.ConstantReflectionProvider;
  45 import jdk.vm.ci.meta.MetaAccessProvider;
  46 
  47 /**
  48  * Loads an object's class (i.e., this node can be created for {@code object.getClass()}).
  49  */
  50 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  51 public final class GetClassNode extends FloatingNode implements Lowerable, Canonicalizable, Virtualizable {
  52 
  53     public static final NodeClass<GetClassNode> TYPE = NodeClass.create(GetClassNode.class);
  54     @Input ValueNode object;
  55 
  56     public ValueNode getObject() {
  57         return object;
  58     }
  59 
  60     public GetClassNode(Stamp stamp, ValueNode object) {
  61         super(TYPE, stamp);
  62         this.object = object;
  63         assert ((ObjectStamp) object.stamp()).nonNull();
  64     }
  65 
  66     @Override
  67     public void lower(LoweringTool tool) {
  68         tool.getLowerer().lower(this, tool);
  69     }
  70 
  71     public static ValueNode tryFold(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ValueNode object) {
  72         if (metaAccess != null && object != null && object.stamp() instanceof ObjectStamp) {
  73             ObjectStamp objectStamp = (ObjectStamp) object.stamp();
  74             if (objectStamp.isExactType()) {
  75                 return ConstantNode.forConstant(constantReflection.asJavaClass(objectStamp.type()), metaAccess);
  76             }
  77         }
  78         return null;
  79     }
  80 
  81     @Override
  82     public ValueNode canonical(CanonicalizerTool tool) {
  83         ValueNode folded = tryFold(tool.getMetaAccess(), tool.getConstantReflection(), getObject());
  84         return folded == null ? this : folded;
  85     }
  86 
  87     @Override
  88     public void virtualize(VirtualizerTool tool) {
  89         ValueNode alias = tool.getAlias(getObject());
  90         if (alias instanceof VirtualObjectNode) {
  91             VirtualObjectNode virtual = (VirtualObjectNode) alias;
  92             Constant javaClass = tool.getConstantReflectionProvider().asJavaClass(virtual.type());
  93             tool.replaceWithValue(ConstantNode.forConstant(stamp(), javaClass, tool.getMetaAccessProvider(), graph()));
  94         }
  95     }
  96 }