• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

compiler optimization

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, ranchers!
Could you explain me the following compiler optimization?
Code:


Bytecode:


So, looking at JVM spec I found that means , but I think it should be .
In addtition, means , but it should be .

Why does it happen?

Thanks in advance,
Oleg.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the whole instruction

if_icmple #16

This means, if the first value is lower or equal to the second, then jump to (continue at) code 16. In other words, if first value greater than second, continue with the next instruction which is what you intended, isn't it? Same with the other instructions.
 
Oleg Tikhonov
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for really fast response!
I just tried to understand. If I transform bytecode into the code, I get:


And it is not what I wanted. I wish one 2 one transformation, but it's not...
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not what's being done. You miss the #16 etc after the comparisons. Those mean "jump to that instruction". So it will in fact be this:
As you can see, "A not eq B" will never be printed. But that's right - it will never be printed in your original code either. That else-if branch is only reached if a is not larger than b and b is not larger than a. However, a != b is (mathematically) equivalent to a < b or b > a.
 
Oleg Tikhonov
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, boy!
Thank you Rob. I got it !!!

Oleg.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another remark about your code:

Oleg Tikhonov wrote:


The if (a != b) will never be true, because you already checked the cases a > b and b > a. When the JVM gets at "... else if (a != b)", there is only one possibility, which is a == b.
 
Hang a left on main. Then read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic