comparison graal/GraalCompiler/src/com/sun/c1x/graph/GraphBuilder.java @ 2799:e1dad0edd57a

first part of loop reworking
author Lukas Stadler <lukas.stadler@jku.at>
date Fri, 27 May 2011 17:48:28 +0200
parents d6bf240963fb
children c3f64b66fc78
comparison
equal deleted inserted replaced
2798:58e65eb6bb5d 2799:e1dad0edd57a
285 appendGoto(target); 285 appendGoto(target);
286 } 286 }
287 287
288 public void mergeOrClone(Block target, FrameStateAccess newState) { 288 public void mergeOrClone(Block target, FrameStateAccess newState) {
289 Instruction first = target.firstInstruction; 289 Instruction first = target.firstInstruction;
290 if (target.isLoopHeader && isVisited(target)) {
291 first = ((LoopBegin) first).loopEnd();
292 }
290 assert first instanceof StateSplit; 293 assert first instanceof StateSplit;
291 294
292 int bci = target.startBci; 295 int bci = target.startBci;
293 296
294 FrameState existingState = ((StateSplit) first).stateBefore(); 297 FrameState existingState = ((StateSplit) first).stateBefore();
295 298
296 if (existingState == null) { 299 if (existingState == null) {
297 assert first instanceof Merge ^ !target.isLoopHeader : "isLoopHeader: " + target.isLoopHeader; 300 // assert first instanceof Merge ^ !target.isLoopHeader : "isLoopHeader: " + target.isLoopHeader;
298 301
299 // copy state because it is modified 302 // copy state because it is modified
300 FrameState duplicate = newState.duplicate(bci); 303 FrameState duplicate = newState.duplicate(bci);
301 304
302 // if the block is a loop header, insert all necessary phis 305 // if the block is a loop header, insert all necessary phis
303 if (target.isLoopHeader) { 306 if (first instanceof LoopBegin && target.isLoopHeader) {
304 assert first instanceof Merge; 307 assert first instanceof Merge;
305 insertLoopPhis((Merge) first, duplicate); 308 insertLoopPhis((Merge) first, duplicate);
306 ((Merge) first).setStateBefore(duplicate); 309 ((Merge) first).setStateBefore(duplicate);
307 } else { 310 } else {
308 ((StateSplit) first).setStateBefore(duplicate); 311 ((StateSplit) first).setStateBefore(duplicate);
316 } 319 }
317 assert existingState.localsSize() == newState.localsSize(); 320 assert existingState.localsSize() == newState.localsSize();
318 assert existingState.stackSize() == newState.stackSize(); 321 assert existingState.stackSize() == newState.stackSize();
319 322
320 if (first instanceof Placeholder) { 323 if (first instanceof Placeholder) {
321 Merge merge = new Merge(existingState.bci, target.isLoopHeader, graph); 324 assert !target.isLoopHeader;
325 Merge merge = new Merge(graph);
322 326
323 Placeholder p = (Placeholder) first; 327 Placeholder p = (Placeholder) first;
324 assert p.next() == null; 328 assert p.next() == null;
325 p.replace(merge); 329 p.replace(merge);
326 target.firstInstruction = merge; 330 target.firstInstruction = merge;
327 merge.setStateBefore(existingState); 331 merge.setStateBefore(existingState);
328 } 332 first = merge;
329 333 }
330 existingState.merge((Merge) target.firstInstruction, newState); 334
335 existingState.merge((Merge) first, newState);
331 } 336 }
332 337
333 for (int j = 0; j < frameState.localsSize() + frameState.stackSize(); ++j) { 338 for (int j = 0; j < frameState.localsSize() + frameState.stackSize(); ++j) {
334 if (frameState.valueAt(j) != null) { 339 if (frameState.valueAt(j) != null) {
335 assert !frameState.valueAt(j).isDeleted(); 340 assert !frameState.valueAt(j).isDeleted();
1054 private Instruction createTargetAt(int bci, FrameStateAccess stateAfter) { 1059 private Instruction createTargetAt(int bci, FrameStateAccess stateAfter) {
1055 return createTarget(blockFromBci[bci], stateAfter); 1060 return createTarget(blockFromBci[bci], stateAfter);
1056 } 1061 }
1057 1062
1058 private Instruction createTarget(Block block, FrameStateAccess stateAfter) { 1063 private Instruction createTarget(Block block, FrameStateAccess stateAfter) {
1059
1060 assert block != null && stateAfter != null; 1064 assert block != null && stateAfter != null;
1061 assert block.isLoopHeader || block.firstInstruction == null || block.firstInstruction.next() == null : "non-loop block must be iterated after all its predecessors"; 1065 assert block.isLoopHeader || block.firstInstruction == null || block.firstInstruction.next() == null : "non-loop block must be iterated after all its predecessors";
1062 1066
1063 if (block.isExceptionEntry) { 1067 if (block.isExceptionEntry) {
1064 assert stateAfter.stackSize() == 1; 1068 assert stateAfter.stackSize() == 1;
1065 } 1069 }
1066 1070
1067 if (block.firstInstruction == null) { 1071 if (block.firstInstruction == null) {
1068 if (block.isLoopHeader) { 1072 if (block.isLoopHeader) {
1069 block.firstInstruction = new Merge(block.startBci, block.isLoopHeader, graph); 1073 // block.firstInstruction = new Merge(block.startBci, graph);
1074
1075 LoopBegin loopBegin = new LoopBegin(graph);
1076 LoopEnd loopEnd = new LoopEnd(graph);
1077 loopEnd.setLoopBegin(loopBegin);
1078 block.firstInstruction = loopBegin;
1070 } else { 1079 } else {
1071 block.firstInstruction = new Placeholder(graph); 1080 block.firstInstruction = new Placeholder(graph);
1072 } 1081 }
1073 } 1082 }
1074 mergeOrClone(block, stateAfter); 1083 mergeOrClone(block, stateAfter);
1075 addToWorkList(block); 1084 addToWorkList(block);
1076 return block.firstInstruction; 1085
1086 if (block.firstInstruction instanceof LoopBegin && isVisited(block)) {
1087 return ((LoopBegin) block.firstInstruction).loopEnd();
1088 } else {
1089 return block.firstInstruction;
1090 }
1077 } 1091 }
1078 1092
1079 private Value synchronizedObject(FrameStateAccess state, RiMethod target) { 1093 private Value synchronizedObject(FrameStateAccess state, RiMethod target) {
1080 if (isStatic(target.accessFlags())) { 1094 if (isStatic(target.accessFlags())) {
1081 Constant classConstant = new Constant(target.holder().getEncoding(Representation.JavaClass), graph); 1095 Constant classConstant = new Constant(target.holder().getEncoding(Representation.JavaClass), graph);
1130 1144
1131 if (block instanceof ExceptionBlock) { 1145 if (block instanceof ExceptionBlock) {
1132 createExceptionDispatch((ExceptionBlock) block); 1146 createExceptionDispatch((ExceptionBlock) block);
1133 } else { 1147 } else {
1134 iterateBytecodesForBlock(block); 1148 iterateBytecodesForBlock(block);
1149 }
1150 }
1151 }
1152 for (Block b : blocksVisited) {
1153 if (b.isLoopHeader) {
1154 LoopBegin begin = (LoopBegin) b.firstInstruction;
1155 LoopEnd end = begin.loopEnd();
1156
1157 // This can happen with degenerated loops like this one:
1158 // for (;;) {
1159 // try {
1160 // break;
1161 // } catch (UnresolvedException iioe) {
1162 // }
1163 // }
1164 if (end.stateBefore() != null) {
1165 begin.stateBefore().merge(begin, end.stateBefore());
1166 } else {
1167 end.delete();
1168 Merge merge = new Merge(graph);
1169 merge.successors().setAndClear(merge.nextIndex(), begin, begin.nextIndex());
1170 begin.replace(merge);
1135 } 1171 }
1136 } 1172 }
1137 } 1173 }
1138 } 1174 }
1139 1175