native x86 code for "3-way if" (one or two comparisons?)

Dmytro Sheyko dmytro_sheyko at hotmail.com
Thu May 13 00:31:36 PDT 2010


Hi,

What native x86 code is to be generated for following java code?

    if (midVal < key) {
        // ...
    } else if (midVal > key) {
        // ...
    } else { // midVal == key
        // ...
    }

Especially I am interested how many times "midVal" and "key" are compared.
It seems to me that one time is enough. And code is to be generated like this:

    cmp midVal, key
    jb @less
    ja @greater
@equal:
    ...
    jmp @end
@less:
    ...
    jmp @end
@greater:
    ...
@end:


However, below code works a little bit faster than above assuming that always (midVal >= key).

    if (false) {
        // ...
    } else if (midVal > key) {
        // ...
    } else { // midVal == key
        // ...
    }

This leads me to conclusion that java code transformed to native literally and "midVal" and "key" are compared twice.

    cmp midVal, key
    jb @less
    cmp midVal, key
    ja @greater
@equal:
    ...
    jmp @end
@less:
    ...
    jmp @end
@greater:
    ...
@end:

Well, do we compare values twice in such cases? If so, is this necessary to do the second one?

Thank you,
Dmytro Sheyko 		 	   		  
_________________________________________________________________
Hotmail: Trusted email with Microsoft’s powerful SPAM protection.
https://signup.live.com/signup.aspx?id=60969
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20100513/631c0cbb/attachment.html 


More information about the hotspot-compiler-dev mailing list