I recently thought about branch prediction in modern processors. As far as I understand, branch prediction is required, because when executing instructions in the pipeline, we don’t know the result of the conditional operation just before we take the branch.
Since I know that modern processors out of order can execute instructions in any order, if the dependencies between them are satisfied, my question is whether processors can reorder instructions in such a way that the branch goal is already known by the time the CPU should take the branch, in this way, the branching direction can be “foreseen,” so why not guess at all?
So the processor can do this:
do_some_work(); if(condition()) //evaluating here requires the cpu to guess the direction or stall do_this(); else do_that();
For this:
bool result = condition(); do_some_work(); //bunch of instructions that take longer than the pipeline length if(result) //value of result is known, thus decision is always 100% correct do_this(); else do_that();
, ( ).
, , , , , ?
, , , , , . , -, 100% ( / ), , , - .
, , , , HW (, ). , , .
, , , - HW - - , ( , , ), , ( ), , .
. , - ( do_some_work() ), . , . , , , , , ( , ). , , .
do_some_work()
, , , .
5- ( ):
3 , ALU.
.