[webkit-dev] Time to remove LIKELY and UNLIKELY macros?

Maciej Stachowiak mjs at apple.com
Mon Oct 1 23:29:57 PDT 2012

On Oct 1, 2012, at 10:55 PM, Adam Barth <abarth at webkit.org> wrote:

> According to <http://stackoverflow.com/questions/1851299/is-it-possible-to-tell-the-branch-predictor-how-likely-it-is-to-follow-the-branc>,
> __builtin_expect (what our LIKELY and UNLIKELY macros expand to [1])
> doesn't do anything on modern CPUs.  Apparently, these used to be
> important for PowerPC, but I don't think many folks use WebKit on
> PowerPC anymore.
> Should we remove these macros?  I wasted some time today experimenting
> with them without realizing that they compile to no-ops in clang.

In general, removing code with no effect is good. More details on this case:

I'd guess the most popular target architectures for WebKit are x86, x86_64, and ARMv7. Is it a no-op for all of those in clang? How did you determine that it is a no-op?

And is the no-opness intentional or an llvm bug? Seems like not to me. At least some LLVM docs imply that it has an effect on codegen: <http://llvm.org/docs/BranchWeightMetadata.html>. This LLVM bug implies that it was implemented, but at least at one point, mistakenly had no effect on the optimizer: <http://llvm.org/bugs/show_bug.cgi?id=2577>. This LLVM bug implies that it should affect basic block reordering, but that was not turned on until somewhat recently: <http://llvm.org/bugs/show_bug.cgi?id=11096>.

Note, despite the stackoverflow thread cited, I would be highly surprised if static branch predicition had no effect ever, even on modern architectures. While recent intel CPUs have very good dynamic branch prediction, the basic block reordering should still have a significant impact in some cases due to cache effects.


More information about the webkit-dev mailing list