Purpose of cmove instruction in x86 assembly? | cmove 指令如何避免错误的分支预测带来的开销?
cmove 指令是用來做什么的?(Purpose of cmove instruction in x86 assembly?)
The purpose of cmov is to allow software (in some cases) to avoid a branch.
For example, if you have this code:
cmp eax,ebxjne .l1mov eax,edx .l1:…then when a modern CPU sees the jne branch it will take a guess about whether the branch will be taken or not taken, and then start speculatively executing instructions based on the guess. If the guess is wrong there’s a performance penalty, because the CPU has to discard any speculatively executed work and then start fetching and executing the correct path.
For a conditional move (e.g. cmove eax,edx) the CPU doesn’t need to guess which code will be executed and the cost of a mispredicted branch is avoided. However, the CPU can’t know if the value in eax will change or not, which means that later instructions that depend on the results of the conditional move have to wait until the conditional move completes (instead of being speculatively executed with an assumed value and not stalling). 我的理解是,它只是通過不去預測這種方式,來避免預測錯了重來。就像你只要不寫代碼,就不會有bug。
This means that if the branch can be easily predicted a branch can be faster; and if the branch can’t be easily predicted the condition move can be faster.
Note that a conditional move is never strictly needed (it can always be done with a branch instead) - it’s more like an optional optimization.
參考:條件數據傳送(Conditional Data Transfer)
Reference: CSAPP Section 5.12 - Understanding Memory Performance
需要指出的是,不是所有的條件行為都能用條件數據傳送來實現,所以無可避免地在某些情況中,程序員會寫出導致條件分支的代碼,而對于這些條件分支,處理器用分支預測可能會處理得很糟糕。
總結
以上是生活随笔為你收集整理的Purpose of cmove instruction in x86 assembly? | cmove 指令如何避免错误的分支预测带来的开销?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 802. Find E
- 下一篇: Flink Checkpoint 机制: