comparison graal/GraalCompiler/src/com/sun/c1x/alloc/ControlFlowOptimizer.java @ 2761:d3398b21faf9

Re-enabled CFG optimization (now only on LIRBlock data structure).
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Sat, 21 May 2011 17:46:54 +0200
parents
children 2ac7b30b7290
comparison
equal deleted inserted replaced
2760:127db58b044e 2761:d3398b21faf9
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.graph.*;
29 import com.sun.c1x.ir.*;
30 import com.sun.c1x.lir.*;
31 import com.sun.c1x.util.*;
32 import com.sun.cri.ci.*;
33
34 /**
35 * This class performs basic optimizations on the control flow graph after LIR generation.
36 */
37 final class ControlFlowOptimizer {
38
39 /**
40 * Performs control flow optimizations on the given IR graph.
41 * @param ir the IR graph that should be optimized
42 */
43 public static void optimize(IR ir) {
44 ControlFlowOptimizer optimizer = new ControlFlowOptimizer(ir);
45 List<LIRBlock> code = ir.linearScanOrder();
46 optimizer.reorderShortLoops(code);
47 optimizer.deleteEmptyBlocks(code);
48 optimizer.deleteUnnecessaryJumps(code);
49 optimizer.deleteJumpsToReturn(code);
50 }
51
52 private final IR ir;
53
54 private ControlFlowOptimizer(IR ir) {
55 this.ir = ir;
56 }
57
58 private void reorderShortLoop(List<LIRBlock> code, LIRBlock headerBlock, int headerIdx) {
59 int i = headerIdx + 1;
60 int maxEnd = Math.min(headerIdx + C1XOptions.MaximumShortLoopSize, code.size());
61 while (i < maxEnd && code.get(i).loopDepth() >= headerBlock.loopDepth()) {
62 i++;
63 }
64
65 if (i == code.size() || code.get(i).loopDepth() < headerBlock.loopDepth()) {
66 int endIdx = i - 1;
67 LIRBlock endBlock = code.get(endIdx);
68
69 if (endBlock.numberOfSux() == 1 && endBlock.suxAt(0) == headerBlock) {
70 // short loop from headerIdx to endIdx found . reorder blocks such that
71 // the headerBlock is the last block instead of the first block of the loop
72
73 for (int j = headerIdx; j < endIdx; j++) {
74 code.set(j, code.get(j + 1));
75 }
76 code.set(endIdx, headerBlock);
77 }
78 }
79 }
80
81 private void reorderShortLoops(List<LIRBlock> code) {
82 for (int i = code.size() - 1; i >= 0; i--) {
83 LIRBlock block = code.get(i);
84
85 if (block.isLinearScanLoopHeader()) {
86 reorderShortLoop(code, block, i);
87 }
88 }
89
90 assert verify(code);
91 }
92
93 // only blocks with exactly one successor can be deleted. Such blocks
94 // must always end with an unconditional branch to this successor
95 private boolean canDeleteBlock(LIRBlock block) {
96 if (block.numberOfSux() != 1 ||
97 block == ir.startBlock ||
98 block.suxAt(0) == block) {
99 return false;
100 }
101
102 List<LIRInstruction> instructions = block.lir().instructionsList();
103
104 assert instructions.size() >= 2 : "block must have label and branch";
105 assert instructions.get(0).code == LIROpcode.Label : "first instruction must always be a label";
106 assert instructions.get(instructions.size() - 1) instanceof LIRBranch : "last instruction must always be a branch";
107 assert ((LIRBranch) instructions.get(instructions.size() - 1)).cond() == Condition.TRUE : "branch must be unconditional";
108 assert ((LIRBranch) instructions.get(instructions.size() - 1)).block() == block.suxAt(0) : "branch target must be the successor";
109
110 // block must have exactly one successor
111
112 return instructions.size() == 2 && instructions.get(instructions.size() - 1).info == null;
113 }
114
115 private void deleteEmptyBlocks(List<LIRBlock> code) {
116 int oldPos = 0;
117 int newPos = 0;
118 int numBlocks = code.size();
119
120 while (oldPos < numBlocks) {
121 LIRBlock block = code.get(oldPos);
122
123 if (canDeleteBlock(block)) {
124 LIRBlock newTarget = block.suxAt(0);
125
126 // update the block references in any branching LIR instructions
127 for (LIRBlock pred : block.blockPredecessors()) {
128 for (LIRInstruction instr : pred.lir().instructionsList()) {
129 if (instr instanceof LIRBranch) {
130 ((LIRBranch) instr).substitute(block, newTarget);
131 } else if (instr instanceof LIRTableSwitch) {
132 ((LIRTableSwitch) instr).substitute(block, newTarget);
133 }
134 }
135 }
136
137 // adjust successor and predecessor lists
138 block.replaceWith(newTarget);
139 C1XMetrics.BlocksDeleted++;
140 } else {
141 // adjust position of this block in the block list if blocks before
142 // have been deleted
143 if (newPos != oldPos) {
144 code.set(newPos, code.get(oldPos));
145 }
146 newPos++;
147 }
148 oldPos++;
149 }
150 assert verify(code);
151 Util.truncate(code, newPos);
152
153 assert verify(code);
154 }
155
156 private void deleteUnnecessaryJumps(List<LIRBlock> code) {
157 // skip the last block because there a branch is always necessary
158 for (int i = code.size() - 2; i >= 0; i--) {
159 LIRBlock block = code.get(i);
160 List<LIRInstruction> instructions = block.lir().instructionsList();
161
162 LIRInstruction lastOp = instructions.get(instructions.size() - 1);
163 if (lastOp.code == LIROpcode.Branch) {
164 assert lastOp instanceof LIRBranch : "branch must be of type LIRBranch";
165 LIRBranch lastBranch = (LIRBranch) lastOp;
166
167 assert lastBranch.block() != null : "last branch must always have a block as target";
168 assert lastBranch.label() == lastBranch.block().label() : "must be equal";
169
170 if (lastBranch.info == null) {
171 if (lastBranch.block() == code.get(i + 1)) {
172 // delete last branch instruction
173 Util.truncate(instructions, instructions.size() - 1);
174
175 } else {
176 LIRInstruction prevOp = instructions.get(instructions.size() - 2);
177 if (prevOp.code == LIROpcode.Branch || prevOp.code == LIROpcode.CondFloatBranch) {
178 assert prevOp instanceof LIRBranch : "branch must be of type LIRBranch";
179 LIRBranch prevBranch = (LIRBranch) prevOp;
180
181 if (prevBranch.block() == code.get(i + 1) && prevBranch.info == null) {
182 // eliminate a conditional branch to the immediate successor
183 prevBranch.changeBlock(lastBranch.block());
184 prevBranch.negateCondition();
185 Util.truncate(instructions, instructions.size() - 1);
186 }
187 }
188 }
189 }
190 }
191 }
192
193 assert verify(code);
194 }
195
196 private void deleteJumpsToReturn(List<LIRBlock> code) {
197 for (int i = code.size() - 1; i >= 0; i--) {
198 LIRBlock block = code.get(i);
199 List<LIRInstruction> curInstructions = block.lir().instructionsList();
200 LIRInstruction curLastOp = curInstructions.get(curInstructions.size() - 1);
201
202 assert curInstructions.get(0).code == LIROpcode.Label : "first instruction must always be a label";
203 if (curInstructions.size() == 2 && curLastOp.code == LIROpcode.Return) {
204 // the block contains only a label and a return
205 // if a predecessor ends with an unconditional jump to this block, then the jump
206 // can be replaced with a return instruction
207 //
208 // Note: the original block with only a return statement cannot be deleted completely
209 // because the predecessors might have other (conditional) jumps to this block.
210 // this may lead to unnecesary return instructions in the final code
211
212 assert curLastOp.info == null : "return instructions do not have debug information";
213
214 assert curLastOp instanceof LIROp1 : "return must be LIROp1";
215 CiValue returnOpr = ((LIROp1) curLastOp).operand();
216
217 for (int j = block.numberOfPreds() - 1; j >= 0; j--) {
218 LIRBlock pred = block.predAt(j);
219 List<LIRInstruction> predInstructions = pred.lir().instructionsList();
220 LIRInstruction predLastOp = predInstructions.get(predInstructions.size() - 1);
221
222 if (predLastOp.code == LIROpcode.Branch) {
223 assert predLastOp instanceof LIRBranch : "branch must be LIRBranch";
224 LIRBranch predLastBranch = (LIRBranch) predLastOp;
225
226 if (predLastBranch.block() == block && predLastBranch.cond() == Condition.TRUE && predLastBranch.info == null) {
227 // replace the jump to a return with a direct return
228 // Note: currently the edge between the blocks is not deleted
229 predInstructions.set(predInstructions.size() - 1, new LIROp1(LIROpcode.Return, returnOpr));
230 }
231 }
232 }
233 }
234 }
235 }
236
237 private boolean verify(List<LIRBlock> code) {
238 for (LIRBlock block : code) {
239 List<LIRInstruction> instructions = block.lir().instructionsList();
240
241 for (LIRInstruction instr : instructions) {
242 if (instr instanceof LIRBranch) {
243 LIRBranch opBranch = (LIRBranch) instr;
244 assert opBranch.block() == null || code.contains(opBranch.block()) : "missing successor branch from: " + block + " to: " + opBranch.block();
245 assert opBranch.unorderedBlock() == null || code.contains(opBranch.unorderedBlock()) : "missing successor branch from: " + block + " to: " + opBranch.unorderedBlock();
246 }
247 }
248
249 for (LIRBlock sux : block.blockSuccessors()) {
250 assert code.contains(sux) : "missing successor from: " + block + "to: " + sux;
251 }
252
253 for (LIRBlock pred : block.blockPredecessors()) {
254 assert code.contains(pred) : "missing predecessor from: " + block + "to: " + pred;
255 }
256 }
257
258 return true;
259 }
260 }