Skip to content

Commit

Permalink
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions core/src/main/java/org/jruby/ir/representations/CFG.java
Original file line number Diff line number Diff line change
@@ -415,22 +415,32 @@ private void deleteOrphanedBlocks(DirectedGraph<BasicBlock> graph) {
// System.out.println("\nGraph:\n" + toStringGraph());
// System.out.println("\nInstructions:\n" + toStringInstrs());

// FIXME: Quick and dirty implementation
while (true) {
BasicBlock bbToRemove = null;
for (BasicBlock b : graph.allData()) {
if (b == entryBB) continue; // Skip entry bb!

// Every other bb should have at least one incoming edge
if (graph.findVertexFor(b).getIncomingEdges().isEmpty()) {
bbToRemove = b;
break;
Queue<BasicBlock> worklist = new LinkedList();
Set<BasicBlock> living = new HashSet();
worklist.add(entryBB);
living.add(entryBB);

while (!worklist.isEmpty()) {
BasicBlock current = worklist.remove();

for (BasicBlock bb: graph.findVertexFor(current).getOutgoingDestinationsData()) {
if (!living.contains(bb)) {
worklist.add(bb);
living.add(bb);
}
}
if (bbToRemove == null) break;
}

// Seems like Java should have simpler way of doing this.
// We canot just remove in this loop or we get concmodexc.
Set<BasicBlock> dead = new HashSet();
for (BasicBlock bb: graph.allData()) {
if (!living.contains(bb)) dead.add(bb);
}

removeBB(bbToRemove);
removeNestedScopesFromBB(bbToRemove);
for (BasicBlock bb: dead) {
removeBB(bb);
removeNestedScopesFromBB(bb);
}
}

0 comments on commit 3b41c9c

Please sign in to comment.