lkml.org 
[lkml]   [2009]   [Jan]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: if (unlikely(...)) == unnecessary?
Davide Libenzi wrote:
> On Wed, 28 Jan 2009, Chris Snook wrote:
>
>> Davide Libenzi wrote:
>>> I noticed that GCC >= 3.3 (not tried the ones before) automatically branches
>>> out the "if" code (and follow-through the "else" code, if there). Is that a
>>> coincidence or a rule we can rely on going forward?
>> That's the default behavior, but there are lots of things that can cause it to
>> behave differently.
>
> Please don't keep me hanging. What are they (just a few of the "lots"
> that makes GCC follow-through "if" code)?
>
>
> - Davide
>
>

When you turn on optimizations, gcc will try to avoid branching just to execute
a few instructions, since the cache miss and page fault penalties greatly exceed
the cost of a branch mispredict. The thresholds and heuristics vary, but in
general, if you stick something like this:

if (condition) foo++;
else if (complex condition) {do lots of stuff}

In the middle of a long function body and compile with optimizations enabled,
gcc will try to put the foo++ right after the evaluation. Some ISAs support
conditional instructions to let the compiler help fill pipeline bubbles, and
some superscalar processors will speculatively execute it in parallel with their
evaluation of the second condition, and proceed with whichever execution path is
chosen when they retire the instruction evaluating the first conditional.

-- Chris


\
 
 \ /
  Last update: 2009-01-28 22:01    [W:0.036 / U:0.176 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site