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