EDIT: My confusion arises because surely by predicting which branch is taken, you are effectively doing the target prediction too??
This question is intrinsically linked to my first question on the topic:
branch prediction vs branch target prediction
Looking at the accepted answer:
Unconditional branch, fixed target
- Infinite loop
goto
statement
break
or continue
statement
- End of the 'then' clause of an
if/else
statement (to jump past the else
clause)
- Non-virtual function call
Unconditional branch, variable target
- Returning from a function
- Virtual function call
- Function pointer call
switch
statement (if compiled into a jump table)
Conditional branch, fixed target
if
statement
switch
statement (if compiled into a series of if/else
statements)
- Loop condition tests
- The
&&
and ||
operators
- The ternary
?:
operator
Conditional branch, variable target
- Less likely to show up under normal conditions, but the compiler may synthesize one as an optimization, combining two of the above cases.
For example, on x86, the compiler may optimize code like
if
(condition) { obj->VirtualFunctionCall(); }
into a conditional
indirect jump like jne *%eax
if it appears at the end of a function
due to tail call optimization.
If I have the following code:
if(something){
//a
}
else{
//b
}
(BP = "Branch Prediction" and BTP = "Branch Target Prediction")
Its pretty obvious BP is used to evaluate the conditional something
. However I am trying to understand whether BTP is also involved in determine what happens in branch a
. Does BTP also happen to determine the address of the code located at branch a
/b
, depending on the result of the BP?
I ask becase on this wikipedia page (http://en.wikipedia.org/wiki/Branch_target_predictor):
In computer architecture, a branch target predictor is the part of a
processor that predicts the target of a taken conditional branch or an
unconditional branch instruction before the target of the branch
instruction is computed by the execution unit of the processor.
it suggests BTP is used to predict the target after the conditional has been predicted.
1) Could somebody clarify the above please?
A second related question- how do BP and BTP differ in the way they interact with the fetch/decode/execute/write-back pipeline of the CPU? Does BP begin at the fetch or decode stage? After the execution stage of the conditional code we can check whether the prediction was correct and update the branch prediction cache.
2) How does BTP work with regards to the fetch/decode/execute/write-back CPU stages?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…