comparison graal/GraalCompiler/src/com/sun/c1x/alloc/EdgeMoveOptimizer.java @ 2509:16b9a8b5ad39

Renamings Runtime=>GraalRuntime and Compiler=>GraalCompiler
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Apr 2011 11:50:44 +0200
parents graal/Compiler/src/com/sun/c1x/alloc/EdgeMoveOptimizer.java@9ec15d6914ca
children 6ab73784566a
comparison
equal deleted inserted replaced
2508:fea94949e0a2 2509:16b9a8b5ad39
1 /*
2 * Copyright (c) 2009, 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.alloc;
24
25 import java.util.*;
26
27 import com.sun.c1x.*;
28 import com.sun.c1x.ir.*;
29 import com.sun.c1x.lir.*;
30
31 /**
32 * This class optimizes moves, particularly those that result from eliminating SSA form.
33 *
34 * When a block has more than one predecessor, and all predecessors end with
35 * the {@linkplain #same(LIRInstruction, LIRInstruction) same} sequence of
36 * {@linkplain LIROpcode#Move move} instructions, then these sequences
37 * can be replaced with a single copy of the sequence at the beginning of the block.
38 *
39 * Similarly, when a block has more than one successor, then same sequences of
40 * moves at the beginning of the successors can be placed once at the end of
41 * the block. But because the moves must be inserted before all branch
42 * instructions, this works only when there is exactly one conditional branch
43 * at the end of the block (because the moves must be inserted before all
44 * branches, but after all compares).
45 *
46 * This optimization affects all kind of moves (reg->reg, reg->stack and
47 * stack->reg). Because this optimization works best when a block contains only
48 * a few moves, it has a huge impact on the number of blocks that are totally
49 * empty.
50 *
51 * @author Christian Wimmer (original HotSpot implementation)
52 * @author Thomas Wuerthinger
53 * @author Doug Simon
54 */
55 final class EdgeMoveOptimizer {
56
57 /**
58 * Optimizes moves on block edges.
59 *
60 * @param blockList a list of blocks whose moves should be optimized
61 */
62 public static void optimize(List<BlockBegin> blockList) {
63 EdgeMoveOptimizer optimizer = new EdgeMoveOptimizer();
64
65 // ignore the first block in the list (index 0 is not processed)
66 for (int i = blockList.size() - 1; i >= 1; i--) {
67 BlockBegin block = blockList.get(i);
68
69 if (block.numberOfPreds() > 1 && !block.checkBlockFlag(BlockBegin.BlockFlag.ExceptionEntry)) {
70 optimizer.optimizeMovesAtBlockEnd(block);
71 }
72 if (block.numberOfSux() == 2) {
73 optimizer.optimizeMovesAtBlockBegin(block);
74 }
75 }
76 }
77
78 private final List<List<LIRInstruction>> edgeInstructionSeqences;
79
80 private EdgeMoveOptimizer() {
81 edgeInstructionSeqences = new ArrayList<List<LIRInstruction>>(4);
82 }
83
84 /**
85 * Determines if two operations are both {@linkplain LIROpcode#Move moves}
86 * that have the same {@linkplain LIROp1#operand() source} and {@linkplain LIROp1#result() destination}
87 * operands and they have the same {@linkplain LIRInstruction#info debug info}.
88 *
89 * @param op1 the first instruction to compare
90 * @param op2 the second instruction to compare
91 * @return {@code true} if {@code op1} and {@code op2} are the same by the above algorithm
92 */
93 private boolean same(LIRInstruction op1, LIRInstruction op2) {
94 assert op1 != null;
95 assert op2 != null;
96
97 if (op1.code == LIROpcode.Move && op2.code == LIROpcode.Move) {
98 assert op1 instanceof LIROp1 : "move must be LIROp1";
99 assert op2 instanceof LIROp1 : "move must be LIROp1";
100 LIROp1 move1 = (LIROp1) op1;
101 LIROp1 move2 = (LIROp1) op2;
102 if (move1.info == move2.info && move1.operand().equals(move2.operand()) && move1.result().equals(move2.result())) {
103 // these moves are exactly equal and can be optimized
104 return true;
105 }
106 }
107 return false;
108 }
109
110 /**
111 * Moves the longest {@linkplain #same common} subsequence at the end all
112 * predecessors of {@code block} to the start of {@code block}.
113 */
114 private void optimizeMovesAtBlockEnd(BlockBegin block) {
115 if (block.isPredecessor(block)) {
116 // currently we can't handle this correctly.
117 return;
118 }
119
120 // clear all internal data structures
121 edgeInstructionSeqences.clear();
122
123 int numPreds = block.numberOfPreds();
124 assert numPreds > 1 : "do not call otherwise";
125 assert !block.checkBlockFlag(BlockBegin.BlockFlag.ExceptionEntry) : "exception handlers not allowed";
126
127 // setup a list with the LIR instructions of all predecessors
128 for (int i = 0; i < numPreds; i++) {
129 BlockBegin pred = block.predAt(i);
130 List<LIRInstruction> predInstructions = pred.lir().instructionsList();
131
132 if (pred.numberOfSux() != 1) {
133 // this can happen with switch-statements where multiple edges are between
134 // the same blocks.
135 return;
136 }
137
138 assert pred.suxAt(0) == block : "invalid control flow";
139 assert predInstructions.get(predInstructions.size() - 1).code == LIROpcode.Branch : "block with successor must end with branch";
140 assert predInstructions.get(predInstructions.size() - 1) instanceof LIRBranch : "branch must be LIROpBranch";
141 assert ((LIRBranch) predInstructions.get(predInstructions.size() - 1)).cond() == Condition.TRUE : "block must end with unconditional branch";
142
143 if (predInstructions.get(predInstructions.size() - 1).info != null) {
144 // can not optimize instructions that have debug info
145 return;
146 }
147
148 // ignore the unconditional branch at the end of the block
149 List<LIRInstruction> seq = predInstructions.subList(0, predInstructions.size() - 1);
150 edgeInstructionSeqences.add(seq);
151 }
152
153 // process lir-instructions while all predecessors end with the same instruction
154 while (true) {
155 List<LIRInstruction> seq = edgeInstructionSeqences.get(0);
156 if (seq.isEmpty()) {
157 return;
158 }
159
160 LIRInstruction op = last(seq);
161 for (int i = 1; i < numPreds; ++i) {
162 List<LIRInstruction> otherSeq = edgeInstructionSeqences.get(i);
163 if (otherSeq.isEmpty() || !same(op, last(otherSeq))) {
164 return;
165 }
166 }
167
168 // insert the instruction at the beginning of the current block
169 block.lir().insertBefore(1, op);
170
171 // delete the instruction at the end of all predecessors
172 for (int i = 0; i < numPreds; i++) {
173 seq = edgeInstructionSeqences.get(i);
174 removeLast(seq);
175 }
176 }
177 }
178
179 /**
180 * Moves the longest {@linkplain #same common} subsequence at the start of all
181 * successors of {@code block} to the end of {@code block} just prior to the
182 * branch instruction ending {@code block}.
183 */
184 private void optimizeMovesAtBlockBegin(BlockBegin block) {
185
186 edgeInstructionSeqences.clear();
187 int numSux = block.numberOfSux();
188
189 List<LIRInstruction> instructions = block.lir().instructionsList();
190
191 assert numSux == 2 : "method should not be called otherwise";
192 assert instructions.get(instructions.size() - 1).code == LIROpcode.Branch : "block with successor must end with branch";
193 assert instructions.get(instructions.size() - 1) instanceof LIRBranch : "branch must be LIROpBranch";
194 assert ((LIRBranch) instructions.get(instructions.size() - 1)).cond() == Condition.TRUE : "block must end with unconditional branch";
195
196 if (instructions.get(instructions.size() - 1).info != null) {
197 // cannot optimize instructions when debug info is needed
198 return;
199 }
200
201 LIRInstruction branch = instructions.get(instructions.size() - 2);
202 if (branch.info != null || (branch.code != LIROpcode.Branch && branch.code != LIROpcode.CondFloatBranch)) {
203 // not a valid case for optimization
204 // currently, only blocks that end with two branches (conditional branch followed
205 // by unconditional branch) are optimized
206 return;
207 }
208
209 // now it is guaranteed that the block ends with two branch instructions.
210 // the instructions are inserted at the end of the block before these two branches
211 int insertIdx = instructions.size() - 2;
212
213 if (C1XOptions.DetailedAsserts) {
214 for (int i = insertIdx - 1; i >= 0; i--) {
215 LIRInstruction op = instructions.get(i);
216 if ((op.code == LIROpcode.Branch || op.code == LIROpcode.CondFloatBranch) && ((LIRBranch) op).block() != null) {
217 throw new Error("block with two successors can have only two branch instructions");
218 }
219 }
220 }
221
222 // setup a list with the lir-instructions of all successors
223 for (int i = 0; i < numSux; i++) {
224 BlockBegin sux = block.suxAt(i);
225 List<LIRInstruction> suxInstructions = sux.lir().instructionsList();
226
227 assert suxInstructions.get(0).code == LIROpcode.Label : "block must start with label";
228
229 if (sux.numberOfPreds() != 1) {
230 // this can happen with switch-statements where multiple edges are between
231 // the same blocks.
232 return;
233 }
234 assert sux.predAt(0) == block : "invalid control flow";
235 assert !sux.checkBlockFlag(BlockBegin.BlockFlag.ExceptionEntry) : "exception handlers not allowed";
236
237 // ignore the label at the beginning of the block
238 List<LIRInstruction> seq = suxInstructions.subList(1, suxInstructions.size());
239 edgeInstructionSeqences.add(seq);
240 }
241
242 // process LIR instructions while all successors begin with the same instruction
243 while (true) {
244 List<LIRInstruction> seq = edgeInstructionSeqences.get(0);
245 if (seq.isEmpty()) {
246 return;
247 }
248
249 LIRInstruction op = first(seq);
250 for (int i = 1; i < numSux; i++) {
251 List<LIRInstruction> otherSeq = edgeInstructionSeqences.get(i);
252 if (otherSeq.isEmpty() || !same(op, first(otherSeq))) {
253 // these instructions are different and cannot be optimized .
254 // no further optimization possible
255 return;
256 }
257 }
258
259 // insert instruction at end of current block
260 block.lir().insertBefore(insertIdx, op);
261 insertIdx++;
262
263 // delete the instructions at the beginning of all successors
264 for (int i = 0; i < numSux; i++) {
265 seq = edgeInstructionSeqences.get(i);
266 removeFirst(seq);
267 }
268 }
269 }
270
271 /**
272 * Gets the first element from a LIR instruction sequence.
273 */
274 private static LIRInstruction first(List<LIRInstruction> seq) {
275 return seq.get(0);
276 }
277
278 /**
279 * Gets the last element from a LIR instruction sequence.
280 */
281 private static LIRInstruction last(List<LIRInstruction> seq) {
282 return seq.get(seq.size() - 1);
283 }
284
285 /**
286 * Removes the first element from a LIR instruction sequence.
287 */
288 private static void removeFirst(List<LIRInstruction> seq) {
289 seq.remove(0);
290 }
291
292 /**
293 * Removes the last element from a LIR instruction sequence.
294 */
295 private static void removeLast(List<LIRInstruction> seq) {
296 seq.remove(seq.size() - 1);
297 }
298 }