Mercurial > hg > truffle
annotate graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/phases/DeadCodeEliminationPhase.java @ 2953:445233cd91df
added GraalOptions.TestGraphDuplication, fixed graph duplication
author | Lukas Stadler <lukas.stadler@jku.at> |
---|---|
date | Wed, 15 Jun 2011 11:21:53 +0200 |
parents | 0c0e407faa39 |
children | cbece91420af |
rev | line source |
---|---|
2867 | 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 */ | |
2879
7f584bf507ed
Renamed and moved phase subclasses.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2874
diff
changeset
|
23 package com.oracle.max.graal.compiler.phases; |
2867 | 24 |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
25 import java.util.*; |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
26 |
2874
d90bf514d647
Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2872
diff
changeset
|
27 import com.oracle.max.graal.compiler.*; |
d90bf514d647
Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2872
diff
changeset
|
28 import com.oracle.max.graal.compiler.gen.*; |
d90bf514d647
Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2872
diff
changeset
|
29 import com.oracle.max.graal.compiler.ir.*; |
d90bf514d647
Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2872
diff
changeset
|
30 import com.oracle.max.graal.graph.*; |
2867 | 31 |
32 | |
2879
7f584bf507ed
Renamed and moved phase subclasses.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2874
diff
changeset
|
33 public class DeadCodeEliminationPhase extends Phase { |
2867 | 34 |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
35 private NodeFlood flood; |
2867 | 36 private Graph graph; |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
37 private ArrayList<LoopBegin> brokenLoops; |
2867 | 38 |
39 @Override | |
40 protected void run(Graph graph) { | |
41 this.graph = graph; | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
42 this.flood = graph.createNodeFlood(); |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
43 this.brokenLoops = new ArrayList<LoopBegin>(); |
2867 | 44 |
2938
c7783b6773ea
fixed graph start frame state
Lukas Stadler <lukas.stadler@jku.at>
parents:
2913
diff
changeset
|
45 // remove chained Merges |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
46 for (Merge merge : graph.getNodes(Merge.class)) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
47 if (merge.predecessors().size() == 1 && merge.usages().size() == 0) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
48 if (merge.successors().get(0) instanceof Merge) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
49 Node pred = merge.predecessors().get(0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
50 int predIndex = merge.predecessorsIndex().get(0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
51 pred.successors().setAndClear(predIndex, merge, 0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
52 merge.delete(); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
53 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
54 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
55 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
56 Node startSuccessor = graph.start().successors().get(0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
57 if (startSuccessor instanceof Merge) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
58 Merge startMerge = (Merge) startSuccessor; |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
59 if (startMerge.predecessors().size() == 1 && startMerge.usages().size() == 0) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
60 int predIndex = startMerge.predecessorsIndex().get(0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
61 graph.start().successors().setAndClear(predIndex, startMerge, 0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
62 startMerge.delete(); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
63 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
64 } |
2938
c7783b6773ea
fixed graph start frame state
Lukas Stadler <lukas.stadler@jku.at>
parents:
2913
diff
changeset
|
65 |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
66 flood.add(graph.start()); |
2867 | 67 |
68 iterateSuccessors(); | |
69 disconnectCFGNodes(); | |
70 | |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
71 deleteBrokenLoops(); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
72 |
2867 | 73 iterateInputs(); |
74 disconnectNonCFGNodes(); | |
75 | |
76 deleteCFGNodes(); | |
77 deleteNonCFGNodes(); | |
78 | |
79 new PhiSimplifier(graph); | |
80 | |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
81 |
2888
224412c24426
Changed C1X=>Graal and c1x=>graal in Java code.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2886
diff
changeset
|
82 if (GraalOptions.TraceDeadCodeElimination) { |
2886
c3b8968233fa
Removed counting of deleted nodes for each phase.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
2879
diff
changeset
|
83 System.out.printf("dead code elimination finished\n"); |
2867 | 84 } |
85 } | |
86 | |
2868
6d24c27902a2
turned inlining into a phase, some node cloning fixes, added NodeWorklist
Lukas Stadler <lukas.stadler@jku.at>
parents:
2867
diff
changeset
|
87 private static boolean isCFG(Node n) { |
6d24c27902a2
turned inlining into a phase, some node cloning fixes, added NodeWorklist
Lukas Stadler <lukas.stadler@jku.at>
parents:
2867
diff
changeset
|
88 return n != null && ((n instanceof Instruction) || n == n.graph().start()); |
6d24c27902a2
turned inlining into a phase, some node cloning fixes, added NodeWorklist
Lukas Stadler <lukas.stadler@jku.at>
parents:
2867
diff
changeset
|
89 } |
6d24c27902a2
turned inlining into a phase, some node cloning fixes, added NodeWorklist
Lukas Stadler <lukas.stadler@jku.at>
parents:
2867
diff
changeset
|
90 |
2867 | 91 private void iterateSuccessors() { |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
92 for (Node current : flood) { |
2867 | 93 for (Node successor : current.successors()) { |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
94 flood.add(successor); |
2867 | 95 } |
96 } | |
97 } | |
98 | |
99 private void disconnectCFGNodes() { | |
100 for (Node node : graph.getNodes()) { | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
101 if (node != Node.Null && !flood.isMarked(node) && isCFG(node)) { |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
102 if (node instanceof LoopEnd) { |
2953
445233cd91df
added GraalOptions.TestGraphDuplication, fixed graph duplication
Lukas Stadler <lukas.stadler@jku.at>
parents:
2951
diff
changeset
|
103 assert ((LoopEnd) node).loopBegin() != null : "node " + node; |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
104 brokenLoops.add(((LoopEnd) node).loopBegin()); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
105 } |
2867 | 106 // iterate backwards so that the predecessor indexes in removePhiPredecessor are correct |
107 for (int i = node.successors().size() - 1; i >= 0; i--) { | |
108 Node successor = node.successors().get(i); | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
109 if (successor != Node.Null && flood.isMarked(successor)) { |
2867 | 110 if (successor instanceof Merge) { |
111 ((Merge) successor).removePhiPredecessor(node); | |
112 } | |
113 } | |
114 } | |
115 node.successors().clearAll(); | |
116 node.inputs().clearAll(); | |
117 } | |
118 } | |
119 } | |
120 | |
2951
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
121 private void deleteBrokenLoops() { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
122 for (LoopBegin loop : brokenLoops) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
123 assert loop.predecessors().size() == 1; |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
124 for (Node usage : new ArrayList<Node>(loop.usages())) { |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
125 assert usage instanceof Phi; |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
126 usage.replace(((Phi) usage).valueAt(0)); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
127 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
128 |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
129 Node pred = loop.predecessors().get(0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
130 int predIndex = loop.predecessorsIndex().get(0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
131 pred.successors().setAndClear(predIndex, loop, 0); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
132 loop.delete(); |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
133 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
134 } |
0c0e407faa39
another fix to debug info (on-stack parameters), DCE removes unnecessary merges and LoopBegins whose LoopEnd went away
Lukas Stadler <lukas.stadler@jku.at>
parents:
2938
diff
changeset
|
135 |
2867 | 136 private void deleteCFGNodes() { |
137 for (Node node : graph.getNodes()) { | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
138 if (node != Node.Null && !flood.isMarked(node) && isCFG(node)) { |
2867 | 139 node.delete(); |
140 } | |
141 } | |
142 } | |
143 | |
144 private void iterateInputs() { | |
145 for (Node node : graph.getNodes()) { | |
2938
c7783b6773ea
fixed graph start frame state
Lukas Stadler <lukas.stadler@jku.at>
parents:
2913
diff
changeset
|
146 if (node instanceof Local) { |
c7783b6773ea
fixed graph start frame state
Lukas Stadler <lukas.stadler@jku.at>
parents:
2913
diff
changeset
|
147 flood.add(node); |
c7783b6773ea
fixed graph start frame state
Lukas Stadler <lukas.stadler@jku.at>
parents:
2913
diff
changeset
|
148 } |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
149 if (node != Node.Null && flood.isMarked(node)) { |
2867 | 150 for (Node input : node.inputs()) { |
2913
81dab15b45e5
fixes to Phi.removeInput and DCE
Lukas Stadler <lukas.stadler@jku.at>
parents:
2911
diff
changeset
|
151 if (!isCFG(input)) { |
81dab15b45e5
fixes to Phi.removeInput and DCE
Lukas Stadler <lukas.stadler@jku.at>
parents:
2911
diff
changeset
|
152 flood.add(input); |
81dab15b45e5
fixes to Phi.removeInput and DCE
Lukas Stadler <lukas.stadler@jku.at>
parents:
2911
diff
changeset
|
153 } |
2867 | 154 } |
155 } | |
156 } | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
157 for (Node current : flood) { |
2867 | 158 for (Node input : current.inputs()) { |
2913
81dab15b45e5
fixes to Phi.removeInput and DCE
Lukas Stadler <lukas.stadler@jku.at>
parents:
2911
diff
changeset
|
159 if (!isCFG(input)) { |
81dab15b45e5
fixes to Phi.removeInput and DCE
Lukas Stadler <lukas.stadler@jku.at>
parents:
2911
diff
changeset
|
160 flood.add(input); |
81dab15b45e5
fixes to Phi.removeInput and DCE
Lukas Stadler <lukas.stadler@jku.at>
parents:
2911
diff
changeset
|
161 } |
2867 | 162 } |
163 } | |
164 } | |
165 | |
166 private void disconnectNonCFGNodes() { | |
167 for (Node node : graph.getNodes()) { | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
168 if (node != Node.Null && !flood.isMarked(node) && !isCFG(node)) { |
2867 | 169 node.inputs().clearAll(); |
170 } | |
171 } | |
172 } | |
173 | |
174 private void deleteNonCFGNodes() { | |
175 for (Node node : graph.getNodes()) { | |
2910
7eec76f3e39e
adjust monitor index while inlining, renamed NodeWorklist to NodeFlood
Lukas Stadler <lukas.stadler@jku.at>
parents:
2879
diff
changeset
|
176 if (node != Node.Null && !flood.isMarked(node) && !isCFG(node)) { |
2867 | 177 node.delete(); |
178 } | |
179 } | |
180 } | |
181 } |