Mercurial > hg > graal-jvmci-8
comparison graal/Compiler/src/com/sun/c1x/alloc/ControlFlowOptimizer.java @ 2507:9ec15d6914ca
Pull over of compiler from maxine repository.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 27 Apr 2011 11:43:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2506:4a3bf8a5bf41 | 2507:9ec15d6914ca |
---|---|
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 * @author Thomas Wuerthinger | |
38 */ | |
39 final class ControlFlowOptimizer { | |
40 | |
41 /** | |
42 * Performs control flow optimizations on the given IR graph. | |
43 * @param ir the IR graph that should be optimized | |
44 */ | |
45 public static void optimize(IR ir) { | |
46 ControlFlowOptimizer optimizer = new ControlFlowOptimizer(ir); | |
47 List<BlockBegin> code = ir.linearScanOrder(); | |
48 | |
49 // push the OSR entry block to the end so that we're not jumping over it. | |
50 BlockBegin osrEntry = ((Base) code.get(0).end()).osrEntry(); | |
51 if (osrEntry != null) { | |
52 int index = osrEntry.linearScanNumber(); | |
53 assert code.get(index) == osrEntry : "wrong index"; | |
54 code.remove(index); | |
55 code.add(osrEntry); | |
56 } | |
57 | |
58 optimizer.reorderShortLoops(code); | |
59 optimizer.deleteEmptyBlocks(code); | |
60 optimizer.deleteUnnecessaryJumps(code); | |
61 optimizer.deleteJumpsToReturn(code); | |
62 } | |
63 | |
64 private final IR ir; | |
65 | |
66 private ControlFlowOptimizer(IR ir) { | |
67 this.ir = ir; | |
68 } | |
69 | |
70 private void reorderShortLoop(List<BlockBegin> code, BlockBegin headerBlock, int headerIdx) { | |
71 int i = headerIdx + 1; | |
72 int maxEnd = Math.min(headerIdx + C1XOptions.MaximumShortLoopSize, code.size()); | |
73 while (i < maxEnd && code.get(i).loopDepth() >= headerBlock.loopDepth()) { | |
74 i++; | |
75 } | |
76 | |
77 if (i == code.size() || code.get(i).loopDepth() < headerBlock.loopDepth()) { | |
78 int endIdx = i - 1; | |
79 BlockBegin endBlock = code.get(endIdx); | |
80 | |
81 if (endBlock.numberOfSux() == 1 && endBlock.suxAt(0) == headerBlock) { | |
82 // short loop from headerIdx to endIdx found . reorder blocks such that | |
83 // the headerBlock is the last block instead of the first block of the loop | |
84 | |
85 for (int j = headerIdx; j < endIdx; j++) { | |
86 code.set(j, code.get(j + 1)); | |
87 } | |
88 code.set(endIdx, headerBlock); | |
89 | |
90 // correct the flags so that any loop alignment occurs in the right place. | |
91 assert code.get(endIdx).checkBlockFlag(BlockBegin.BlockFlag.BackwardBranchTarget) : "must be backward branch target"; | |
92 code.get(endIdx).clearBlockFlag(BlockBegin.BlockFlag.BackwardBranchTarget); | |
93 code.get(headerIdx).setBlockFlag(BlockBegin.BlockFlag.BackwardBranchTarget); | |
94 } | |
95 } | |
96 } | |
97 | |
98 private void reorderShortLoops(List<BlockBegin> code) { | |
99 for (int i = code.size() - 1; i >= 0; i--) { | |
100 BlockBegin block = code.get(i); | |
101 | |
102 if (block.checkBlockFlag(BlockBegin.BlockFlag.LinearScanLoopHeader)) { | |
103 reorderShortLoop(code, block, i); | |
104 } | |
105 } | |
106 | |
107 assert verify(code); | |
108 } | |
109 | |
110 // only blocks with exactly one successor can be deleted. Such blocks | |
111 // must always end with an unconditional branch to this successor | |
112 private boolean canDeleteBlock(BlockBegin block) { | |
113 if (block.numberOfSux() != 1 || | |
114 block.numberOfExceptionHandlers() != 0 || | |
115 block == ir.startBlock || | |
116 block.isExceptionEntry() || | |
117 block.suxAt(0) == block) { | |
118 return false; | |
119 } | |
120 | |
121 List<LIRInstruction> instructions = block.lir().instructionsList(); | |
122 | |
123 assert instructions.size() >= 2 : "block must have label and branch"; | |
124 assert instructions.get(0).code == LIROpcode.Label : "first instruction must always be a label"; | |
125 assert instructions.get(instructions.size() - 1) instanceof LIRBranch : "last instruction must always be a branch"; | |
126 assert ((LIRBranch) instructions.get(instructions.size() - 1)).cond() == Condition.TRUE : "branch must be unconditional"; | |
127 assert ((LIRBranch) instructions.get(instructions.size() - 1)).block() == block.suxAt(0) : "branch target must be the successor"; | |
128 | |
129 // block must have exactly one successor | |
130 | |
131 return instructions.size() == 2 && instructions.get(instructions.size() - 1).info == null; | |
132 } | |
133 | |
134 private void deleteEmptyBlocks(List<BlockBegin> code) { | |
135 int oldPos = 0; | |
136 int newPos = 0; | |
137 int numBlocks = code.size(); | |
138 | |
139 while (oldPos < numBlocks) { | |
140 BlockBegin block = code.get(oldPos); | |
141 | |
142 if (canDeleteBlock(block)) { | |
143 BlockBegin newTarget = block.suxAt(0); | |
144 | |
145 // propagate backward branch target flag for correct code alignment | |
146 if (block.checkBlockFlag(BlockBegin.BlockFlag.BackwardBranchTarget)) { | |
147 newTarget.setBlockFlag(BlockBegin.BlockFlag.BackwardBranchTarget); | |
148 } | |
149 | |
150 // update the block references in any branching LIR instructions | |
151 for (BlockBegin pred : block.predecessors()) { | |
152 for (LIRInstruction instr : pred.lir().instructionsList()) { | |
153 if (instr instanceof LIRBranch) { | |
154 ((LIRBranch) instr).substitute(block, newTarget); | |
155 } else if (instr instanceof LIRTableSwitch) { | |
156 ((LIRTableSwitch) instr).substitute(block, newTarget); | |
157 } | |
158 } | |
159 } | |
160 | |
161 // adjust successor and predecessor lists | |
162 ir.replaceBlock(block, newTarget); | |
163 C1XMetrics.BlocksDeleted++; | |
164 } else { | |
165 // adjust position of this block in the block list if blocks before | |
166 // have been deleted | |
167 if (newPos != oldPos) { | |
168 code.set(newPos, code.get(oldPos)); | |
169 } | |
170 newPos++; | |
171 } | |
172 oldPos++; | |
173 } | |
174 Util.truncate(code, newPos); | |
175 | |
176 assert verify(code); | |
177 } | |
178 | |
179 private void deleteUnnecessaryJumps(List<BlockBegin> code) { | |
180 // skip the last block because there a branch is always necessary | |
181 for (int i = code.size() - 2; i >= 0; i--) { | |
182 BlockBegin block = code.get(i); | |
183 List<LIRInstruction> instructions = block.lir().instructionsList(); | |
184 | |
185 LIRInstruction lastOp = instructions.get(instructions.size() - 1); | |
186 if (lastOp.code == LIROpcode.Branch) { | |
187 assert lastOp instanceof LIRBranch : "branch must be of type LIRBranch"; | |
188 LIRBranch lastBranch = (LIRBranch) lastOp; | |
189 | |
190 assert lastBranch.block() != null : "last branch must always have a block as target"; | |
191 assert lastBranch.label() == lastBranch.block().label() : "must be equal"; | |
192 | |
193 if (lastBranch.info == null) { | |
194 if (lastBranch.block() == code.get(i + 1)) { | |
195 // delete last branch instruction | |
196 Util.truncate(instructions, instructions.size() - 1); | |
197 | |
198 } else { | |
199 LIRInstruction prevOp = instructions.get(instructions.size() - 2); | |
200 if (prevOp.code == LIROpcode.Branch || prevOp.code == LIROpcode.CondFloatBranch) { | |
201 assert prevOp instanceof LIRBranch : "branch must be of type LIRBranch"; | |
202 LIRBranch prevBranch = (LIRBranch) prevOp; | |
203 | |
204 if (prevBranch.block() == code.get(i + 1) && prevBranch.info == null) { | |
205 // eliminate a conditional branch to the immediate successor | |
206 prevBranch.changeBlock(lastBranch.block()); | |
207 prevBranch.negateCondition(); | |
208 Util.truncate(instructions, instructions.size() - 1); | |
209 } | |
210 } | |
211 } | |
212 } | |
213 } | |
214 } | |
215 | |
216 assert verify(code); | |
217 } | |
218 | |
219 private void deleteJumpsToReturn(List<BlockBegin> code) { | |
220 for (int i = code.size() - 1; i >= 0; i--) { | |
221 BlockBegin block = code.get(i); | |
222 List<LIRInstruction> curInstructions = block.lir().instructionsList(); | |
223 LIRInstruction curLastOp = curInstructions.get(curInstructions.size() - 1); | |
224 | |
225 assert curInstructions.get(0).code == LIROpcode.Label : "first instruction must always be a label"; | |
226 if (curInstructions.size() == 2 && curLastOp.code == LIROpcode.Return) { | |
227 // the block contains only a label and a return | |
228 // if a predecessor ends with an unconditional jump to this block, then the jump | |
229 // can be replaced with a return instruction | |
230 // | |
231 // Note: the original block with only a return statement cannot be deleted completely | |
232 // because the predecessors might have other (conditional) jumps to this block. | |
233 // this may lead to unnecesary return instructions in the final code | |
234 | |
235 assert curLastOp.info == null : "return instructions do not have debug information"; | |
236 | |
237 assert curLastOp instanceof LIROp1 : "return must be LIROp1"; | |
238 CiValue returnOpr = ((LIROp1) curLastOp).operand(); | |
239 | |
240 for (int j = block.numberOfPreds() - 1; j >= 0; j--) { | |
241 BlockBegin pred = block.predAt(j); | |
242 List<LIRInstruction> predInstructions = pred.lir().instructionsList(); | |
243 LIRInstruction predLastOp = predInstructions.get(predInstructions.size() - 1); | |
244 | |
245 if (predLastOp.code == LIROpcode.Branch) { | |
246 assert predLastOp instanceof LIRBranch : "branch must be LIRBranch"; | |
247 LIRBranch predLastBranch = (LIRBranch) predLastOp; | |
248 | |
249 if (predLastBranch.block() == block && predLastBranch.cond() == Condition.TRUE && predLastBranch.info == null) { | |
250 // replace the jump to a return with a direct return | |
251 // Note: currently the edge between the blocks is not deleted | |
252 predInstructions.set(predInstructions.size() - 1, new LIROp1(LIROpcode.Return, returnOpr)); | |
253 } | |
254 } | |
255 } | |
256 } | |
257 } | |
258 } | |
259 | |
260 private boolean verify(List<BlockBegin> code) { | |
261 for (BlockBegin block : code) { | |
262 List<LIRInstruction> instructions = block.lir().instructionsList(); | |
263 | |
264 for (LIRInstruction instr : instructions) { | |
265 if (instr instanceof LIRBranch) { | |
266 LIRBranch opBranch = (LIRBranch) instr; | |
267 assert opBranch.block() == null || code.contains(opBranch.block()) : "missing successor branch from: " + block + " to: " + opBranch.block(); | |
268 assert opBranch.unorderedBlock() == null || code.contains(opBranch.unorderedBlock()) : "missing successor branch from: " + block + " to: " + opBranch.unorderedBlock(); | |
269 } | |
270 } | |
271 | |
272 for (BlockBegin sux : block.end().successors()) { | |
273 assert code.contains(sux) : "missing successor from: " + block + "to: " + sux; | |
274 } | |
275 | |
276 for (BlockBegin pred : block.predecessors()) { | |
277 assert code.contains(pred) : "missing predecessor from: " + block + "to: " + pred; | |
278 } | |
279 } | |
280 | |
281 return true; | |
282 } | |
283 } |