keith471 / COMP520_peephole

Other
0 stars 0 forks source link

Don't store last use #1

Closed gjh33 closed 7 years ago

gjh33 commented 7 years ago

In the last use of something on the stack, instead of storing and loading we can just let it be removed from the stack for good. It is no longer needed.

EXAMPLE from bench01/backtracksolver.j

.method public getVal(II)I
  .limit locals 8
  .limit stack 2
  aload_0
  getfield SudokuSolver/grid Ljava/util/Vector;
  iload_1
  invokevirtual java/util/Vector/elementAt(I)Ljava/lang/Object;
  astore_3
  aload_3
  checkcast java/util/Vector
  astore 5
  aload 5
  iload_2
  invokevirtual java/util/Vector/elementAt(I)Ljava/lang/Object;
  astore 4
  aload 4
  checkcast java/lang/Integer
  astore 6
  aload 6
  invokevirtual java/lang/Integer/intValue()I
  ireturn
  nop
.end method

We can remove all of these astore/aload combos eliminating 8 lines!!!

Optimized:


.method public getVal(II)I
  .limit locals 8
  .limit stack 2
  aload_0
  getfield SudokuSolver/grid Ljava/util/Vector;
  iload_1
  invokevirtual java/util/Vector/elementAt(I)Ljava/lang/Object;
  checkcast java/util/Vector
  iload_2
  invokevirtual java/util/Vector/elementAt(I)Ljava/lang/Object;
  checkcast java/lang/Integer
  invokevirtual java/lang/Integer/intValue()I
  ireturn
  nop
.end method
gjh33 commented 7 years ago

Conditions

gjh33 commented 7 years ago

New condition: