我正在研究一個令人難以置信的性能關鍵項目,其中每個時鐘週期都計入我最重要的內部循環中。我在考慮重構代碼以隱藏指令延遲,但是我想知道現代CPU的無序執行硬件在多大程度上已經爲我做了這些。考慮下面的(簡單假設)例如:無序硬件:它有多「聰明」?
// Increment three counters. These instructions should all execute in
// parallel with latency of one cycle. Assume the previous register values
// have been computed a long time ago and are ready to use by the time
// these are decoded.
add RAX, 1;
add RBX, 2;
add RCX, 3;
// Multiply takes at least three cycles. Again, assume both inputs are
// ready by the time we get here.
imul RDX, RDI;
// Use the result of the imul immediately in a long dependency chain.
mov RDX, [RDX];
cmp RDX, 1;
jae LBlahBlahBlah;
我的問題是下列哪項適用:
現代主流亂序硬件將重新排列三個
add
的指令前,imul
即使add
指令以編程方式出現在imul
之前,並且在解碼時可以獲得所有輸入相關性。imul
比add
指令的延遲時間更長,並立即用於依賴鏈中,因此這是最佳選擇。亂序執行只發生在編程式先前的指令不能執行由於缺少輸入相關性而被解碼時執行。硬件不能指望「前瞻」來優化這種動態。
我不能回答你的問題,但它變化的基礎涉及的實際CPU和特別說明,我猜。不過,編譯器肯定會爲你做這種優化。 –
您會發現現代x86硬件*非常*能夠重新排序長距離和跨循環迭代的事物,前提是沒有分支預測失誤。 – Mysticial