001/*
002 * Copyright (c) 2011, 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.phases.common;
024
025import static com.oracle.graal.phases.common.DeadCodeEliminationPhase.Options.*;
026
027import java.util.function.*;
028
029import com.oracle.graal.debug.*;
030import jdk.internal.jvmci.options.*;
031
032import com.oracle.graal.graph.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.phases.*;
035
036public class DeadCodeEliminationPhase extends Phase {
037
038    public static class Options {
039        // @formatter:off
040        @Option(help = "Disable optional dead code eliminations", type = OptionType.Debug)
041        public static final OptionValue<Boolean> ReduceDCE = new OptionValue<>(true);
042        // @formatter:on
043    }
044
045    // Metrics
046    private static final DebugMetric metricNodesRemoved = Debug.metric("NodesRemoved");
047
048    public enum Optionality {
049        Optional,
050        Required;
051    }
052
053    /**
054     * Creates a dead code elimination phase that will be run irrespective of
055     * {@link Options#ReduceDCE}.
056     */
057    public DeadCodeEliminationPhase() {
058        this(Optionality.Required);
059    }
060
061    /**
062     * Creates a dead code elimination phase that will be run only if it is
063     * {@linkplain Optionality#Required non-optional} or {@link Options#ReduceDCE} is false.
064     */
065    public DeadCodeEliminationPhase(Optionality optionality) {
066        this.optional = optionality == Optionality.Optional;
067    }
068
069    private final boolean optional;
070
071    @Override
072    public void run(StructuredGraph graph) {
073        if (optional && ReduceDCE.getValue()) {
074            return;
075        }
076
077        NodeFlood flood = graph.createNodeFlood();
078        int totalNodeCount = graph.getNodeCount();
079        flood.add(graph.start());
080        iterateSuccessorsAndInputs(flood);
081        int totalMarkedCount = flood.getTotalMarkedCount();
082        if (totalNodeCount == totalMarkedCount) {
083            // All nodes are live => nothing more to do.
084            return;
085        } else {
086            // Some nodes are not marked alive and therefore dead => proceed.
087            assert totalNodeCount > totalMarkedCount;
088        }
089
090        deleteNodes(flood, graph);
091    }
092
093    private static void iterateSuccessorsAndInputs(NodeFlood flood) {
094        BiConsumer<Node, Node> consumer = (n, succOrInput) -> {
095            assert succOrInput.isAlive() : "dead successor or input " + succOrInput + " in " + n;
096            flood.add(succOrInput);
097        };
098        for (Node current : flood) {
099            if (current instanceof AbstractEndNode) {
100                AbstractEndNode end = (AbstractEndNode) current;
101                flood.add(end.merge());
102            } else {
103                current.acceptSuccessors(consumer);
104                current.acceptInputs(consumer);
105            }
106        }
107    }
108
109    private static void deleteNodes(NodeFlood flood, StructuredGraph graph) {
110        BiConsumer<Node, Node> consumer = (n, input) -> {
111            if (input.isAlive() && flood.isMarked(input)) {
112                input.removeUsage(n);
113            }
114        };
115
116        for (Node node : graph.getNodes()) {
117            if (!flood.isMarked(node)) {
118                node.markDeleted();
119                node.acceptInputs(consumer);
120                metricNodesRemoved.increment();
121            }
122        }
123    }
124}