分支预测 | Branch Prediction
定义 | Definition
分支预测是一种处理器技术,用于猜测程序中的分支(如条件跳转)是否会被执行,从而提高指令流水线的效率。
Branch prediction is a processor technique used to guess whether a branch (e.g., a conditional jump) will be taken or not, thereby improving the efficiency of the instruction pipeline.
分类 | Types
静态预测 | Static Prediction
静态预测在编译时进行,不依赖于运行时的历史信息。
Static prediction is done at compile time and does not rely on runtime history.
示例 | Example
- 始终预测不跳转:假设所有条件跳转都不会执行。 Always predict not taken: Assumes all conditional jumps will not be executed.
- 后向跳转预测跳转:假设所有向后的跳转(通常是循环)都会执行。 Predict backward branches as taken: Assumes all backward jumps (usually loops) will be executed.
动态预测 | Dynamic Prediction
动态预测在运行时进行,依赖于过去的执行历史。
Dynamic prediction is done at runtime and relies on the execution history.
单比特预测 | One-Bit Predictor
每个分支指令对应一个比特,记录上一次是否跳转。
Each branch instruction is associated with a single bit indicating whether it was taken the last time.
两比特预测 | Two-Bit Predictor
使用两比特状态机减少错误预测的概率。
Uses a two-bit state machine to reduce the likelihood of misprediction.
状态转换 | State Transition:
全局历史预测 | Global History Predictor
使用全局分支历史寄存器记录最近的分支结果。
Uses a global branch history register to record recent branch outcomes.
重要性 | Importance
提高流水线效率 | Improve Pipeline Efficiency
分支预测可以减少流水线停顿,使处理器更有效率。
Branch prediction reduces pipeline stalls, making the processor more efficient.
增加吞吐量 | Increase Throughput
通过预测分支,提高指令执行的吞吐量。
By predicting branches, it increases the instruction execution throughput.
误预测处理 | Handling Mispredictions
回滚 | Rollback
在误预测发生时,处理器需要回滚到正确的分支路径。
When a misprediction occurs, the processor needs to roll back to the correct branch path.
性能影响 | Performance Impact
误预测会导致流水线停顿,降低性能。
Mispredictions cause pipeline stalls, reducing performance.