comparison graal/com.oracle.graal.phases/src/com/oracle/graal/phases/schedule/SchedulePhase.java @ 12774:1729072a893a

NewMemoryAwareScheduling: hide data structure behind wrapper class
author Bernhard Urban <bernhard.urban@jku.at>
date Mon, 18 Nov 2013 22:07:38 +0100
parents b6e04d6fe3a7
children b334ca53f077
comparison
equal deleted inserted replaced
12773:b6e04d6fe3a7 12774:1729072a893a
173 } 173 }
174 return info.exitStates; 174 return info.exitStates;
175 } 175 }
176 } 176 }
177 177
178 private class NewMemoryScheduleClosure extends BlockIteratorClosure<Set<LocationIdentity>> { 178 private class KillSet implements Iterable<LocationIdentity> {
179 private final Set<LocationIdentity> set;
180
181 public KillSet() {
182 this.set = new HashSet<>();
183 }
184
185 public KillSet(KillSet other) {
186 this.set = new HashSet<>(other.set);
187 }
188
189 public void add(LocationIdentity locationIdentity) {
190 set.add(locationIdentity);
191 }
192
193 public void addAll(KillSet other) {
194 set.addAll(other.set);
195 }
196
197 public Iterator<LocationIdentity> iterator() {
198 return set.iterator();
199 }
200
201 public boolean isKilled(LocationIdentity locationIdentity) {
202 return set.contains(locationIdentity);
203 }
204
205 }
206
207 private class NewMemoryScheduleClosure extends BlockIteratorClosure<KillSet> {
179 @Override 208 @Override
180 protected Set<LocationIdentity> getInitialState() { 209 protected KillSet getInitialState() {
181 return cloneState(blockToKillSet.get(getCFG().getStartBlock())); 210 return cloneState(blockToKillSet.get(getCFG().getStartBlock()));
182 } 211 }
183 212
184 @Override 213 @Override
185 protected Set<LocationIdentity> processBlock(Block block, Set<LocationIdentity> currentState) { 214 protected KillSet processBlock(Block block, KillSet currentState) {
186 currentState.addAll(computeKillSet(block)); 215 currentState.addAll(computeKillSet(block));
187 return currentState; 216 return currentState;
188 } 217 }
189 218
190 @Override 219 @Override
191 protected Set<LocationIdentity> merge(Block merge, List<Set<LocationIdentity>> states) { 220 protected KillSet merge(Block merge, List<KillSet> states) {
192 assert merge.getBeginNode() instanceof MergeNode; 221 assert merge.getBeginNode() instanceof MergeNode;
193 222
194 Set<LocationIdentity> initKillSet = new HashSet<>(); 223 KillSet initKillSet = new KillSet();
195 for (Set<LocationIdentity> state : states) { 224 for (KillSet state : states) {
196 initKillSet.addAll(state); 225 initKillSet.addAll(state);
197 } 226 }
198 227
199 return initKillSet; 228 return initKillSet;
200 } 229 }
201 230
202 @Override 231 @Override
203 protected Set<LocationIdentity> cloneState(Set<LocationIdentity> state) { 232 protected KillSet cloneState(KillSet state) {
204 return new HashSet<>(state); 233 return new KillSet(state);
205 } 234 }
206 235
207 @Override 236 @Override
208 protected List<Set<LocationIdentity>> processLoop(Loop loop, Set<LocationIdentity> state) { 237 protected List<KillSet> processLoop(Loop loop, KillSet state) {
209 LoopInfo<Set<LocationIdentity>> info = ReentrantBlockIterator.processLoop(this, loop, cloneState(state)); 238 LoopInfo<KillSet> info = ReentrantBlockIterator.processLoop(this, loop, cloneState(state));
210 239
211 assert loop.header.getBeginNode() instanceof LoopBeginNode; 240 assert loop.header.getBeginNode() instanceof LoopBeginNode;
212 Set<LocationIdentity> headerState = merge(loop.header, info.endStates); 241 KillSet headerState = merge(loop.header, info.endStates);
213 242
214 // second iteration, for propagating information to loop exits 243 // second iteration, for propagating information to loop exits
215 info = ReentrantBlockIterator.processLoop(this, loop, cloneState(headerState)); 244 info = ReentrantBlockIterator.processLoop(this, loop, cloneState(headerState));
216 245
217 return info.exitStates; 246 return info.exitStates;
224 * assumptions: {@link MemoryCheckpoint MemoryCheckPoints} are {@link FixedNode FixedNodes}. 253 * assumptions: {@link MemoryCheckpoint MemoryCheckPoints} are {@link FixedNode FixedNodes}.
225 * 254 *
226 * @param block block to analyze 255 * @param block block to analyze
227 * @return all killed locations 256 * @return all killed locations
228 */ 257 */
229 private Set<LocationIdentity> computeKillSet(Block block) { 258 private KillSet computeKillSet(Block block) {
230 Set<LocationIdentity> cachedSet = blockToKillSet.get(block); 259 KillSet cachedSet = blockToKillSet.get(block);
231 if (cachedSet != null) { 260 if (cachedSet != null) {
232 return cachedSet; 261 return cachedSet;
233 } 262 }
234 HashSet<LocationIdentity> set = new HashSet<>(); 263 KillSet set = new KillSet();
235 blockToKillSet.put(block, set); 264 blockToKillSet.put(block, set);
236 265
237 if (block.getBeginNode() instanceof MergeNode) { 266 if (block.getBeginNode() instanceof MergeNode) {
238 MergeNode mergeNode = (MergeNode) block.getBeginNode(); 267 MergeNode mergeNode = (MergeNode) block.getBeginNode();
239 for (PhiNode phi : mergeNode.usages().filter(PhiNode.class)) { 268 for (PhiNode phi : mergeNode.usages().filter(PhiNode.class)) {
263 292
264 /** 293 /**
265 * Map from blocks to the nodes in each block. 294 * Map from blocks to the nodes in each block.
266 */ 295 */
267 private BlockMap<List<ScheduledNode>> blockToNodesMap; 296 private BlockMap<List<ScheduledNode>> blockToNodesMap;
268 private BlockMap<Set<LocationIdentity>> blockToKillSet; 297 private BlockMap<KillSet> blockToKillSet;
269 private final Map<FloatingNode, List<FixedNode>> phantomUsages = new IdentityHashMap<>(); 298 private final Map<FloatingNode, List<FixedNode>> phantomUsages = new IdentityHashMap<>();
270 private final Map<FixedNode, List<FloatingNode>> phantomInputs = new IdentityHashMap<>(); 299 private final Map<FixedNode, List<FloatingNode>> phantomInputs = new IdentityHashMap<>();
271 private final SchedulingStrategy selectedStrategy; 300 private final SchedulingStrategy selectedStrategy;
272 private final MemoryScheduling memsched; 301 private final MemoryScheduling memsched;
273 private NewMemoryScheduleClosure maschedClosure; 302 private NewMemoryScheduleClosure maschedClosure;
341 Debug.printf("==== b: %s (loopDepth: %s). ", b, b.getLoopDepth()); 370 Debug.printf("==== b: %s (loopDepth: %s). ", b, b.getLoopDepth());
342 Debug.printf("dom: %s. ", b.getDominator()); 371 Debug.printf("dom: %s. ", b.getDominator());
343 Debug.printf("post-dom: %s. ", b.getPostdominator()); 372 Debug.printf("post-dom: %s. ", b.getPostdominator());
344 Debug.printf("preds: %s. ", b.getPredecessors()); 373 Debug.printf("preds: %s. ", b.getPredecessors());
345 Debug.printf("succs: %s ====\n", b.getSuccessors()); 374 Debug.printf("succs: %s ====\n", b.getSuccessors());
346 BlockMap<Set<LocationIdentity>> killMaps = blockToKillSet; 375 BlockMap<KillSet> killSets = blockToKillSet;
347 if (killMaps != null) { 376 if (killSets != null) {
348 Debug.printf("X block kills: \n"); 377 Debug.printf("X block kills: \n");
349 if (killMaps.get(b) != null) { 378 if (killSets.get(b) != null) {
350 for (LocationIdentity locId : killMaps.get(b)) { 379 for (LocationIdentity locId : killSets.get(b)) {
351 Debug.printf("X %s killed by %s\n", locId, "dunno anymore"); 380 Debug.printf("X %s killed by %s\n", locId, "dunno anymore");
352 } 381 }
353 } 382 }
354 } 383 }
355 384
537 } 566 }
538 567
539 Stack<Block> path = computePathInDominatorTree(earliestBlock, latestBlock); 568 Stack<Block> path = computePathInDominatorTree(earliestBlock, latestBlock);
540 Debug.printf("|path| is %d: %s\n", path.size(), path); 569 Debug.printf("|path| is %d: %s\n", path.size(), path);
541 570
542 Set<LocationIdentity> killSet = new HashSet<>(); 571 KillSet killSet = new KillSet();
543 // follow path, start at earliest schedule 572 // follow path, start at earliest schedule
544 while (path.size() > 0) { 573 while (path.size() > 0) {
545 Block currentBlock = path.pop(); 574 Block currentBlock = path.pop();
546 Block dominatedBlock = path.size() == 0 ? null : path.peek(); 575 Block dominatedBlock = path.size() == 0 ? null : path.peek();
547 if (dominatedBlock != null && !currentBlock.getSuccessors().contains(dominatedBlock)) { 576 if (dominatedBlock != null && !currentBlock.getSuccessors().contains(dominatedBlock)) {
549 assert dominatedBlock.getBeginNode() instanceof MergeNode; 578 assert dominatedBlock.getBeginNode() instanceof MergeNode;
550 579
551 HashSet<Block> region = computeRegion(currentBlock, dominatedBlock); 580 HashSet<Block> region = computeRegion(currentBlock, dominatedBlock);
552 Debug.printf("%s: region for %s -> %s: %s\n", n, currentBlock, dominatedBlock, region); 581 Debug.printf("%s: region for %s -> %s: %s\n", n, currentBlock, dominatedBlock, region);
553 582
554 Map<FixedNode, Set<LocationIdentity>> states; 583 Map<FixedNode, KillSet> states;
555 states = ReentrantBlockIterator.apply(maschedClosure, currentBlock, new HashSet<>(killSet), region); 584 states = ReentrantBlockIterator.apply(maschedClosure, currentBlock, new KillSet(killSet), region);
556 585
557 Set<LocationIdentity> mergeState = states.get(dominatedBlock.getBeginNode()); 586 KillSet mergeState = states.get(dominatedBlock.getBeginNode());
558 if (mergeState.contains(locid)) { 587 if (mergeState.isKilled(locid)) {
559 // location got killed somewhere in the branches, 588 // location got killed somewhere in the branches,
560 // thus we've to move the read above it 589 // thus we've to move the read above it
561 return currentBlock; 590 return currentBlock;
562 } 591 }
563 killSet.addAll(mergeState); 592 killSet.addAll(mergeState);
564 } else { 593 } else {
565 // trivial case 594 // trivial case
566 if (dominatedBlock == null) { 595 if (dominatedBlock == null) {
567 return currentBlock; 596 return currentBlock;
568 } 597 }
569 Set<LocationIdentity> blockKills = computeKillSet(currentBlock); 598 KillSet blockKills = computeKillSet(currentBlock);
570 if (blockKills.contains(locid)) { 599 if (blockKills.isKilled(locid)) {
571 return currentBlock; 600 return currentBlock;
572 } 601 }
573 killSet.addAll(blockKills); 602 killSet.addAll(blockKills);
574 } 603 }
575 } 604 }