}
Stack<Node> stack = new Stack<Node>();
Set<Node> visited = new HashSet<Node>();
stack.add(root);
int blockCount = 0;
InputBlock rootBlock = null;
while (!stack.isEmpty()) {
Node proj = stack.pop();
Node parent = proj;
if (proj.isBlockProjection && proj.preds.size() > 0) {
parent = proj.preds.get(0);
}
if (!visited.contains(parent)) {
visited.add(parent);
InputBlock block = new InputBlock(graph, "" + blockCount);
blocks.add(block);
if (parent == root) {
rootBlock = block;
}
blockCount++;
parent.block = block;
if (proj != parent && proj.succs.size() == 1 && proj.succs.contains(root)) {
// Special treatment of Halt-nodes
proj.block = block;
}
Node p = proj;
do {
if (p.preds.size() == 0 || p.preds.get(0) == null) {
p = parent;
break;
}
p = p.preds.get(0);
if (p.block == null) {
p.block = block;
}
} while (!p.isBlockProjection && !p.isBlockStart);
if (block != rootBlock) {
for (Node n : p.preds) {
if (n != null && n != p) {
if (n.isBlockProjection) {
n = n.preds.get(0);
}
if (n.block != null) {
n.block.addSuccessor(block);
}
}
}
}
for (Node n : parent.succs) {
if (n != root && n.isBlockProjection) {
for (Node n2 : n.succs) {
if (n2 != parent && n2.block != null && n2.block != rootBlock) {
block.addSuccessor(n2.block);
}
}
} else {
if (n != parent && n.block != null && n.block != rootBlock) {
block.addSuccessor(n.block);
}
}
}
int num_preds = p.preds.size();
int bottom = -1;
if (isRegion(p) || isPhi(p)) {
bottom = 0;
}
int pushed = 0;
for (int i = num_preds - 1; i > bottom; i--) {
if (p.preds.get(i) != null && p.preds.get(i) != p) {
stack.push(p.preds.get(i));
pushed++;
}
}
if (pushed == 0 && p == root) {
// TODO: special handling when root backedges are not built yet
}
}
}
for (Node n : nodes) {
InputBlock block = n.block;
if (block != null) {
block.addNode(n.inputNode.getId());
}
}
int z = 0;
blockIndex = new HashMap<InputBlock, Integer>();