comparison graal/GraalCompiler/src/com/sun/c1x/graph/DeadCodeElimination.java @ 2867:5c545fef2c81

merge
author Lukas Stadler <lukas.stadler@jku.at>
date Tue, 07 Jun 2011 16:33:04 +0200
parents
children 6d24c27902a2
comparison
equal deleted inserted replaced
2866:7f14e6b48a9c 2867:5c545fef2c81
1 /*
2 * Copyright (c) 2011, 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 com.sun.c1x.graph;
24
25 import java.util.*;
26
27 import com.oracle.graal.graph.*;
28 import com.oracle.max.graal.schedule.*;
29 import com.sun.c1x.*;
30 import com.sun.c1x.gen.*;
31 import com.sun.c1x.ir.*;
32
33
34 public class DeadCodeElimination extends Phase {
35
36 private NodeBitMap alive;
37 private Queue<Node> worklist;
38 private Graph graph;
39
40 public int deletedNodeCount;
41
42 @Override
43 protected void run(Graph graph) {
44 this.graph = graph;
45 this.alive = graph.createNodeBitMap();
46 this.worklist = new ArrayDeque<Node>();
47
48 addToWorklist(graph.start());
49
50 iterateSuccessors();
51 disconnectCFGNodes();
52
53 iterateInputs();
54 disconnectNonCFGNodes();
55
56 deleteCFGNodes();
57 deleteNonCFGNodes();
58
59 new PhiSimplifier(graph);
60
61 if (C1XOptions.TraceDeadCodeElimination) {
62 System.out.printf("dead code elimination: deleted %d nodes\n", deletedNodeCount);
63 }
64 }
65
66 private void iterateSuccessors() {
67 Node current;
68 while ((current = nextNode()) != null) {
69 for (Node successor : current.successors()) {
70 addToWorklist(successor);
71 }
72 }
73 }
74
75 private void disconnectCFGNodes() {
76 for (Node node : graph.getNodes()) {
77 if (node != Node.Null && !alive.isMarked(node) && Schedule.isCFG(node)) {
78 // iterate backwards so that the predecessor indexes in removePhiPredecessor are correct
79 for (int i = node.successors().size() - 1; i >= 0; i--) {
80 Node successor = node.successors().get(i);
81 if (successor != Node.Null && alive.isMarked(successor)) {
82 if (successor instanceof Merge) {
83 ((Merge) successor).removePhiPredecessor(node);
84 }
85 }
86 }
87 node.successors().clearAll();
88 node.inputs().clearAll();
89 }
90 }
91 }
92
93 private void deleteCFGNodes() {
94 for (Node node : graph.getNodes()) {
95 if (node != Node.Null && !alive.isMarked(node) && Schedule.isCFG(node)) {
96 node.delete();
97 deletedNodeCount++;
98 }
99 }
100 }
101
102 private void iterateInputs() {
103 for (Node node : graph.getNodes()) {
104 if (node != Node.Null && alive.isMarked(node)) {
105 for (Node input : node.inputs()) {
106 addToWorklist(input);
107 }
108 }
109 }
110 Node current;
111 while ((current = nextNode()) != null) {
112 for (Node input : current.inputs()) {
113 addToWorklist(input);
114 }
115 }
116 }
117
118 private void disconnectNonCFGNodes() {
119 for (Node node : graph.getNodes()) {
120 if (node != Node.Null && !alive.isMarked(node) && !Schedule.isCFG(node)) {
121 node.inputs().clearAll();
122 }
123 }
124 }
125
126 private void deleteNonCFGNodes() {
127 for (Node node : graph.getNodes()) {
128 if (node != Node.Null && !alive.isMarked(node) && !Schedule.isCFG(node)) {
129 node.delete();
130 deletedNodeCount++;
131 }
132 }
133 }
134
135 private Node nextNode() {
136 return worklist.poll();
137 }
138
139 private void addToWorklist(Node node) {
140 if (node != null && !alive.isMarked(node)) {
141 alive.mark(node);
142 worklist.add(node);
143 }
144 }
145 }