On 8/30/05, Momchil Velikov <velco@xxxxxxxxx> wrote:
Thanks to you and Thomas also for giving really good examples ...... It clears it to me.
regards,
-Gaurav
Gaurav Dhiman wrote:
> Well there have been number of mail on usage of __builtin_expect, but can
> any one tell how exactly the compiler use this info to optimize the mahchine
> code it writes. AFAIK, if .. else statement is converted into some type jmp
> statements, depending upon the condition is true or false. By telling the
> compiler that we have more probabilty to have this condition true (in case
> of likely) or more probabilty to having this condition false (in case of
> unlikely), how we are helping the compiler to arrange the corresponding jump
> instruction to optimize the execution of code.
>
> It would be great if somebody can tell how compiler internally benifits from
> it to arrange the code in optimized manner.
One possibility is to move the body of an if-statement away -- at the end of the
function or outside a loop -- so it rarely goes into the L1 cache):
if (x != 0)
do_foo;
do_bar;
may be translated to:
cmp x, 0
je L1
do_foo
L1:
do_bar
whereas
if (unlikely (x != 0))
do_foo;
do_bar;
may be translated to
cmp x, 0
jne L1
L2:
do_bar
...
ret
L1: do_foo
j L2
Thanks to you and Thomas also for giving really good examples ...... It clears it to me.
regards,
-Gaurav
And a more complex example here: http://www.gnu.org/software/gcc/news/reorder.html
~velco