/**
* Peephole optimization.
*/
private void peepHoleOptimization(MethodGenerator methodGen) {
InstructionList il = methodGen.getInstructionList();
InstructionFinder find = new InstructionFinder(il);
InstructionHandle ih;
String pattern;
// LoadInstruction, POP => (removed)
pattern = "LoadInstruction POP";
for (Iterator iter = find.search(pattern); iter.hasNext();) {
InstructionHandle[] match = (InstructionHandle[]) iter.next();
try {
if (!match[0].hasTargeters() && !match[1].hasTargeters()) {
il.delete(match[0], match[1]);
}
}
catch (TargetLostException e) {
// TODO: move target down into the list
}
}
// ILOAD_N, ILOAD_N, SWAP, ISTORE_N => ILOAD_N
pattern = "ILOAD ILOAD SWAP ISTORE";
for (Iterator iter = find.search(pattern); iter.hasNext();) {
InstructionHandle[] match = (InstructionHandle[]) iter.next();
try {
org.apache.bcel.generic.ILOAD iload1 =
(org.apache.bcel.generic.ILOAD) match[0].getInstruction();
org.apache.bcel.generic.ILOAD iload2 =
(org.apache.bcel.generic.ILOAD) match[1].getInstruction();
org.apache.bcel.generic.ISTORE istore =
(org.apache.bcel.generic.ISTORE) match[3].getInstruction();
if (!match[1].hasTargeters() &&
!match[2].hasTargeters() &&
!match[3].hasTargeters() &&
iload1.getIndex() == iload2.getIndex() &&
iload2.getIndex() == istore.getIndex())
{
il.delete(match[1], match[3]);
}
}
catch (TargetLostException e) {
// TODO: move target down into the list
}
}
// LoadInstruction_N, LoadInstruction_M, SWAP => LoadInstruction_M, LoadInstruction_N
pattern = "LoadInstruction LoadInstruction SWAP";
for (Iterator iter = find.search(pattern); iter.hasNext();) {
InstructionHandle[] match = (InstructionHandle[])iter.next();
try {
if (!match[0].hasTargeters() &&
!match[1].hasTargeters() &&
!match[2].hasTargeters())
{
Instruction load_m = match[1].getInstruction();
il.insert(match[0], load_m);
il.delete(match[1], match[2]);
}
}
catch (TargetLostException e) {
// TODO: move target down into the list
}
}
// ALOAD_N ALOAD_N => ALOAD_N DUP
pattern = "ALOAD ALOAD";
for (Iterator iter = find.search(pattern); iter.hasNext();) {
InstructionHandle[] match = (InstructionHandle[])iter.next();
try {
if (!match[1].hasTargeters()) {
org.apache.bcel.generic.ALOAD aload1 =
(org.apache.bcel.generic.ALOAD) match[0].getInstruction();