if (whilePredecessor == block) {
whilePredecessor = (Block) whileCond.getPreds().get(1);
}
// System.out.println("whilePredecessor = " + whilePredecessor);
Unit unit = whilePredecessor.getTail();
boolean found = false;
// walk backwards until we find a definition of the iterator.
while ((unit != whilePredecessor.getHead()) && !found) {
if (unit instanceof DefinitionStmt
&& ((DefinitionStmt) unit).getLeftOp().equals(
iteratorLocal)) {
found = true;
} else {
unit = whilePredecessor.getPredOf(unit);
}
}
// System.out.println("iterator def = " + unit);
DefinitionStmt iteratorDefinition = ((DefinitionStmt) unit);
if (!(iteratorDefinition.getRightOp() instanceof InterfaceInvokeExpr)
|| !((InterfaceInvokeExpr) iteratorDefinition
.getRightOp()).getMethod().getName().equals(
"iterator")) {
continue;
}
Local collectionLocal = (Local) ((InterfaceInvokeExpr) iteratorDefinition
.getRightOp()).getBase();
// System.out.println("collection Local = " + collectionLocal);
found = false;
// Walk backward again until we reach the definition
// of the collection.
while ((unit != whilePredecessor.getHead()) && !found) {
if (unit instanceof DefinitionStmt
&& ((DefinitionStmt) unit).getLeftOp().equals(
collectionLocal)) {
found = true;
} else {
unit = whilePredecessor.getPredOf(unit);
}
}
// System.out.println("collection def = " + unit);
// System.out.println("field = " + field);
DefinitionStmt collectionDefinition = ((DefinitionStmt) unit);
if (!(collectionDefinition.getRightOp() instanceof FieldRef)
|| (((FieldRef) collectionDefinition.getRightOp())
.getField() != field)) {
continue;
}
// FINALLY we know we've found something we can unroll... :)
// System.out.println("is unrollable...");
// There should be a jump from the predecessor to the
// condition. Redirect this jump to the body.
whileCond.getHead().redirectJumpsToThisTo(block.getHead());
Local thisLocal = body.getThisLocal();
Chain units = body.getUnits();
List blockStmtList = new LinkedList();
// pull the statements that we are inlining out of the block
// so that we can copy them. Note that this also removes
// them from the method body.
Unit insertPoint = (Unit) units.getSuccOf(block.getTail());
for (Iterator blockStmts = block.iterator(); blockStmts
.hasNext();) {
Stmt original = (Stmt) blockStmts.next();
blockStmtList.add(original);