1 /*
   2  * Copyright (c) 2013, 2014, 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.hotspot.replacements;
  24 
  25 import org.graalvm.compiler.core.common.type.StampPair;
  26 import org.graalvm.compiler.graph.Node;
  27 import org.graalvm.compiler.graph.NodeClass;
  28 import org.graalvm.compiler.graph.spi.Canonicalizable;
  29 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  30 import org.graalvm.compiler.nodeinfo.NodeInfo;
  31 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.InvokeNode;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.spi.Lowerable;
  36 import org.graalvm.compiler.nodes.spi.LoweringTool;
  37 import org.graalvm.compiler.replacements.nodes.MacroStateSplitNode;
  38 
  39 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  40 import jdk.vm.ci.meta.Assumptions;
  41 import jdk.vm.ci.meta.JavaConstant;
  42 import jdk.vm.ci.meta.MetaAccessProvider;
  43 import jdk.vm.ci.meta.ResolvedJavaMethod;
  44 
  45 @NodeInfo
  46 public final class CallSiteTargetNode extends MacroStateSplitNode implements Canonicalizable, Lowerable {
  47 
  48     public static final NodeClass<CallSiteTargetNode> TYPE = NodeClass.create(CallSiteTargetNode.class);
  49 
  50     public CallSiteTargetNode(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode receiver) {
  51         super(TYPE, invokeKind, targetMethod, bci, returnStamp, receiver);
  52     }
  53 
  54     private ValueNode getCallSite() {
  55         return arguments.get(0);
  56     }
  57 
  58     public static ConstantNode tryFold(ValueNode callSite, MetaAccessProvider metaAccess, Assumptions assumptions) {
  59         if (callSite != null && callSite.isConstant() && !callSite.isNullConstant()) {
  60             HotSpotObjectConstant c = (HotSpotObjectConstant) callSite.asConstant();
  61             JavaConstant target = c.getCallSiteTarget(assumptions);
  62             if (target != null) {
  63                 return ConstantNode.forConstant(target, metaAccess);
  64             }
  65         }
  66         return null;
  67     }
  68 
  69     @Override
  70     public Node canonical(CanonicalizerTool tool) {
  71         ConstantNode target = tryFold(getCallSite(), tool.getMetaAccess(), graph().getAssumptions());
  72         if (target != null) {
  73             return target;
  74         }
  75 
  76         return this;
  77     }
  78 
  79     @Override
  80     public void lower(LoweringTool tool) {
  81         ConstantNode target = tryFold(getCallSite(), tool.getMetaAccess(), graph().getAssumptions());
  82 
  83         if (target != null) {
  84             graph().replaceFixedWithFloating(this, target);
  85         } else {
  86             InvokeNode invoke = createInvoke();
  87             graph().replaceFixedWithFixed(this, invoke);
  88             invoke.lower(tool);
  89         }
  90     }
  91 }