1 /*
   2  * Copyright (c) 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.debug.instrumentation;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  27 
  28 import org.graalvm.compiler.core.common.type.StampFactory;
  29 import org.graalvm.compiler.graph.Node;
  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.FixedWithNextNode;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.spi.Lowerable;
  36 import org.graalvm.compiler.nodes.spi.LoweringTool;
  37 
  38 import jdk.vm.ci.meta.JavaKind;
  39 
  40 /**
  41  * The {@code IsMethodInlinedNode} represents a boolean value indicating whether it is inlined into
  42  * the current graph. There are two clues for the decision: first, the parsing depth, which is a
  43  * contextual information during bytecode parsing; second, the original graph upon cloning, which
  44  * can detect the inlining when comparing to the current graph.
  45  */
  46 @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  47 public final class IsMethodInlinedNode extends FixedWithNextNode implements Lowerable, InstrumentationInliningCallback {
  48 
  49     public static final NodeClass<IsMethodInlinedNode> TYPE = NodeClass.create(IsMethodInlinedNode.class);
  50 
  51     protected StructuredGraph originalGraph;
  52     protected int parsingDepth;
  53 
  54     public IsMethodInlinedNode(int depth) {
  55         super(TYPE, StampFactory.forKind(JavaKind.Boolean));
  56         this.parsingDepth = depth;
  57     }
  58 
  59     @Override
  60     protected void afterClone(Node other) {
  61         if (other instanceof IsMethodInlinedNode) {
  62             // keep track of the original graph
  63             IsMethodInlinedNode that = (IsMethodInlinedNode) other;
  64             this.originalGraph = that.originalGraph == null ? that.graph() : that.originalGraph;
  65         }
  66     }
  67 
  68     private void resolve(StructuredGraph target) {
  69         // if parsingDepth is greater than 0, then inlined
  70         // if the original graph is null, which means this node is never cloned, then not inlined
  71         // if the original graph does not match the current graph, then inlined
  72         replaceAtUsages(ConstantNode.forBoolean(parsingDepth > 0 || (originalGraph != null && originalGraph != target), graph()));
  73         graph().removeFixed(this);
  74     }
  75 
  76     @Override
  77     public void lower(LoweringTool tool) {
  78         resolve(graph());
  79     }
  80 
  81     @Override
  82     public void preInlineInstrumentation(InstrumentationNode instrumentation) {
  83         // This node will be further merged back to instrumentation.graph(). We care about if this
  84         // node originates from instrumentation.graph().
  85         resolve(instrumentation.graph());
  86     }
  87 
  88     @Override
  89     public void postInlineInstrumentation(InstrumentationNode instrumentation) {
  90     }
  91 
  92 }