public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Chris Snook <csnook@redhat.com>
To: Davide Libenzi <davidel@xmailserver.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: if (unlikely(...)) == unnecessary?
Date: Wed, 28 Jan 2009 15:39:20 -0500	[thread overview]
Message-ID: <4980C278.8030204@redhat.com> (raw)
In-Reply-To: <alpine.DEB.1.10.0901281130020.21401@alien.or.mcafeemobile.com>

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

  reply	other threads:[~2009-01-28 20:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-28 16:54 if (unlikely(...)) == unnecessary? Davide Libenzi
2009-01-28 17:55 ` Chris Snook
2009-01-28 19:41   ` Davide Libenzi
2009-01-28 20:39     ` Chris Snook [this message]
2009-01-28 21:10       ` Davide Libenzi
2009-01-28 19:53 ` Mikael Pettersson
2009-01-28 19:59   ` Davide Libenzi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4980C278.8030204@redhat.com \
    --to=csnook@redhat.com \
    --cc=davidel@xmailserver.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox