1 /*
   2  * Copyright (c) 2013, 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;
  24 
  25 import org.graalvm.compiler.graph.NodeClass;
  26 import org.graalvm.compiler.graph.spi.Simplifiable;
  27 import org.graalvm.compiler.graph.spi.SimplifierTool;
  28 import org.graalvm.compiler.nodeinfo.NodeInfo;
  29 import org.graalvm.compiler.nodes.ConstantNode;
  30 import org.graalvm.compiler.nodes.FixedNode;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.calc.FloatingNode;
  34 import org.graalvm.compiler.nodes.spi.Virtualizable;
  35 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  36 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  37 
  38 /**
  39  * This is a special version of the dynamic counter node that removes itself as soon as it's the
  40  * only usage of the associated node. This way it only increments the counter if the node is
  41  * actually executed.
  42  */
  43 @NodeInfo
  44 public final class WeakCounterNode extends DynamicCounterNode implements Simplifiable, Virtualizable {
  45 
  46     public static final NodeClass<WeakCounterNode> TYPE = NodeClass.create(WeakCounterNode.class);
  47     @Input ValueNode checkedValue;
  48 
  49     public WeakCounterNode(String group, String name, ValueNode increment, boolean addContext, ValueNode checkedValue) {
  50         super(TYPE, group, name, increment, addContext);
  51         this.checkedValue = checkedValue;
  52     }
  53 
  54     @Override
  55     public void simplify(SimplifierTool tool) {
  56         if (checkedValue instanceof FloatingNode && checkedValue.getUsageCount() == 1) {
  57             tool.addToWorkList(checkedValue);
  58             graph().removeFixed(this);
  59         }
  60     }
  61 
  62     @Override
  63     public void virtualize(VirtualizerTool tool) {
  64         ValueNode alias = tool.getAlias(checkedValue);
  65         if (alias instanceof VirtualObjectNode) {
  66             tool.delete();
  67         }
  68     }
  69 
  70     public static void addCounterBefore(String group, String name, long increment, boolean addContext, ValueNode checkedValue, FixedNode position) {
  71         StructuredGraph graph = position.graph();
  72         WeakCounterNode counter = graph.add(new WeakCounterNode(name, group, ConstantNode.forLong(increment, graph), addContext, checkedValue));
  73         graph.addBeforeFixed(position, counter);
  74     }
  75 }