keith471 / COMP520_peephole

Other
0 stars 0 forks source link

Remove superfluous relays #4

Closed gjh33 closed 7 years ago

gjh33 commented 7 years ago

if jumping to a another stop we can remove this. Let me show you an example, the semantics are hard to phrase. This is from bench01/backtracksolver.j

.method public checkRow(II)Z
  .limit locals 4
  .limit stack 3
  iconst_0
  dup
  istore_3
  pop
  start_0:
  iload_3
  ldc 9
  if_icmplt true_2
  iconst_0
  goto stop_3
  true_2:
  iconst_1
  stop_3:
  ifeq stop_1
  aload_0
  iload_1
  iload_3
  invokevirtual BacktrackSolver/getVal(II)I
  iload_2
  if_icmpeq true_5
  iconst_0
  goto stop_6
  true_5:
  iconst_1
  stop_6:
  ifeq stop_4
  iconst_0
  ireturn
  stop_4:
  iload_3
  iconst_1
  iadd
  dup
  istore_3
  pop
  goto start_0
  stop_1:
  iconst_1
  ireturn
  nop
.end method

Note the first goto stop_3. What we can change:

replacing all such patterns in the above code results in

.method public checkRow(II)Z
  .limit locals 4
  .limit stack 3
  iconst_0
  dup
  istore_3
  pop
  start_0:
  iload_3
  ldc 9
  if_icmplt true_2
  goto stop_1
  true_2:
  aload_0
  iload_1
  iload_3
  invokevirtual BacktrackSolver/getVal(II)I
  iload_2
  if_icmpeq true_5
  goto stop_4
  true_5:
  iconst_0
  ireturn
  stop_4:
  iload_3
  iconst_1
  iadd
  dup
  istore_3
  pop
  goto start_0
  stop_1:
  iconst_1
  ireturn
  nop
.end method

Removed 8 lines

Conditions:

Postconditions:

Why this is ok (cause this is a hard once to convince). Because with iconst_1 directly above labelA any jump or code flow will pass over it EXCEPT the jumps directly going to labelA (condition 1). All such jumps going to labelA will pass through to labelB (condition 2). So we know this will have no adverse affects.

gjh33 commented 7 years ago

Similar to an already closed issue