Is there a compiler hint for GCC to force branch prediction to always go a certain way?

前端 未结 8 1672
日久生厌
日久生厌 2020-11-28 02:06

For the Intel architectures, is there a way to instruct the GCC compiler to generate code that always forces branch prediction a particular way in my code? Does the Intel h

8条回答
  •  暖寄归人
    2020-11-28 02:53

    No, there is not. (At least on modern x86 processors.)

    __builtin_expect mentioned in other answers influences the way gcc arranges the assembly code. It does not directly influence the CPU's branch predictor. Of course, there will be indirect effects on branch prediction caused by reordering the code. But on modern x86 processors there is no instruction that tells the CPU "assume this branch is/isn't taken".

    See this question for more detail: Intel x86 0x2E/0x3E Prefix Branch Prediction actually used?

    To be clear, __builtin_expect and/or the use of -fprofile-arcs can improve the performance of your code, both by giving hints to the branch predictor through code layout (see Performance optimisations of x86-64 assembly - Alignment and branch prediction), and also improving cache behaviour by keeping "unlikely" code away from "likely" code.

提交回复
热议问题