001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.debug;
024
025import com.oracle.graal.graph.*;
026import com.oracle.graal.graph.spi.*;
027import com.oracle.graal.nodeinfo.*;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.calc.*;
030import com.oracle.graal.nodes.spi.*;
031import com.oracle.graal.nodes.virtual.*;
032
033/**
034 * This is a special version of the dynamic counter node that removes itself as soon as it's the
035 * only usage of the associated node. This way it only increments the counter if the node is
036 * actually executed.
037 */
038@NodeInfo
039public final class WeakCounterNode extends DynamicCounterNode implements Simplifiable, Virtualizable {
040
041    public static final NodeClass<WeakCounterNode> TYPE = NodeClass.create(WeakCounterNode.class);
042    @Input ValueNode checkedValue;
043
044    public WeakCounterNode(String group, String name, ValueNode increment, boolean addContext, ValueNode checkedValue) {
045        super(TYPE, group, name, increment, addContext);
046        this.checkedValue = checkedValue;
047    }
048
049    @Override
050    public void simplify(SimplifierTool tool) {
051        if (checkedValue instanceof FloatingNode && checkedValue.getUsageCount() == 1) {
052            tool.addToWorkList(checkedValue);
053            graph().removeFixed(this);
054        }
055    }
056
057    @Override
058    public void virtualize(VirtualizerTool tool) {
059        ValueNode alias = tool.getAlias(checkedValue);
060        if (alias instanceof VirtualObjectNode) {
061            tool.delete();
062        }
063    }
064
065    public static void addCounterBefore(String group, String name, long increment, boolean addContext, ValueNode checkedValue, FixedNode position) {
066        StructuredGraph graph = position.graph();
067        WeakCounterNode counter = graph.add(new WeakCounterNode(name, group, ConstantNode.forLong(increment, graph), addContext, checkedValue));
068        graph.addBeforeFixed(position, counter);
069    }
070}