keith471 / COMP520_peephole

Other
0 stars 0 forks source link

Extra pointless branching on for loops. #6

Closed agpar closed 7 years ago

agpar commented 7 years ago
  iload_0
  ldc 10
  if_icmplt true_2
  iconst_0
  goto stop_3
  true_2:
  iconst_1
  stop_3:
  ifeq stop_1
  iload_2
  iload_2
  imul
  istore_2
  iinc 1 1
  goto start_0
  stop_1:
  return

In the above, on line 4 a 0 is put on the stack then we branch to stop_3, stop 3 checks if there is a 0 on the stack, then branches to stop_1 if so. Thus, we should optimise as follows:

  iload_0
  ldc 10
  if_icmplt true_2
  goto stop_1
  true_2:
  iconst_1
  stop_3:
  ifeq stop_1
  iload_2
  iload_2
  imul
  istore_2
  iinc 1 1
  goto start_0
  stop_1:
  return
agpar commented 7 years ago

This thing does it: 889af767d75886419d9aa4424703dc63a183016a