keith471 / COMP520_peephole

Other
0 stars 0 forks source link

replace pointless arithmatic #7

Open gjh33 opened 7 years ago

gjh33 commented 7 years ago

examples

; useless mul div (not vice versa due to how int div works)
ldc x ; or iconst_x
imul
ldc x ; or iconst_x
idiv
; useless add then sub (or vice versa)
ldc x ; or iconst_x
iadd
ldc x ; or iconst_x
isub
agpar commented 7 years ago

As I mentioned on facebook, I'm fairly certain this is not how the arithmetic operators work in java bytecode, so the examples you posted would never be a 'lone' operation that you could remove without knowing more context. An example of a single inefficient div would be:

ldc x
ldc x
idiv

In your examples, only one thing is put on the stack before the arithmetic instructions, which means we have no idea what the other thing on the stack that is being operated on is, or if there is even anything else on the stack.

agpar commented 7 years ago

Oh wait I see. You are assuming there is something on the stack. Your example code would add x to it, then remove x from it.

Need to drink my coffee :confused: