0

These two methods seem to be extensively used inside linux kernel code. I know the foundations of branch prediction, but I would like to know how these two functions affect the operation of if() statements. Also do they work at the level of CPU instruction pipeline? or is it at a much higher level of abstraction at kernel code level?

Being a starter to linux kernel I would be deeply interested in knowing the internal workings of these two methods. Thanks!

jww
  • 97,681
  • 90
  • 411
  • 885
Ace
  • 1,501
  • 4
  • 30
  • 49

1 Answers1

0

On some (micro)architectures there are branch hints the compiler can encode into the instruction to tell the CPU branch prediction unit whether the branch will likely be taken.

On other architectures, the static part of the branch prediction unit is documented well enough to use it to generate faster code. For example, on most modern x86's, forward branches are statically predicted as not taken, while backward branches are predicted as taken (to enable faster loops).

In the end, predictive cache fetching is probably more important for speed anyway. That's mostly up to the programmer, but for instruction caching it can help if the compiler knows which part of the program will likely be executed, and keep that part small enough to fit into the cache.

EOF
  • 6,273
  • 2
  • 26
  • 50
  • 1
    AFAIK, Intel hasn't used static branch-prediction hints since P4... – Oliver Charlesworth Jan 26 '14 at 22:55
  • 1
    @OliCharlesworth I think I was being careful enough to say that explicit branch hints are not common. However, when the CPU encounters a branch for the first time, it *has to* make a guess. Generally, forward branches are not predicted as taken the first time they are encountered, while backward branches are. This is still true for modern micro-architectures. – EOF Jan 27 '14 at 18:12