Branch Prediction
Branch Prediction
Branch Prediction
1 prediction bit
0
a31a30a11a2a1a0 branch instruction
1K-entry BHT
10-bit index
Instruction memory
Dynamic prediction:1-bit Predictor
Meaning of prediction bit
1 = branch was last taken
0 = branch was last not taken
Using the BHT
index into the BHT and use the prediction bit to predict branch
behavior
note the prediction bit may have been set by a different branch
instruction with the same lower address bits but that does not matter
the history bit is simply a hint
if prediction is wrong, invert prediction bit
Example: Consider a loop branch that is taken 9 times in a row and
then not taken once. What is the prediction accuracy of 1-bit predictor
for this branch assuming only this branch ever changes its
corresponding prediction bit?
Answer: 80%. Because there are two mispredictions one on the
first iteration and one on the last iteration. Why?
Dynamic prediction: 2-bit Predictor
2-bit prediction: for each index the BHT contains two prediction
bits that change as in the figure below
Key idea: the prediction must be wrong twice for it to be
changed
Example: What is the prediction accuracy of a 2-bit predictor on the
loop of the previous example?
2-bit Predictor Statistics
Prediction accuracy of 4K-entry 2-bit prediction buffer vs. infinite 2-bit buffer:
increasing buffer size from 4K does not significantly improve performance
n-bit Predictors
d= b1 b1 new b1 b2 b2 new b2
prediction action prediction prediction action prediction
2 NT T T NT T T
0 T NT NT T NT NT
2 NT T T NT T T
0 T NT NT T NT NT
d= b1 b1 new b1 b2 b2 new b2
prediction action prediction prediction action prediction
Behavior of 1-bit predictor with 1-bit of correlation, assuming initially NT/NT and d
alternating between 0 and 2: mispredictions only on first iteration.
Predictions used in red.
Correlating Predictors:
(m,n) Predictors
The correlating predictor as before 1 bit of prediction plus 1
correlating bit is called a (1,1) predictor
Generalization of the (1,1) predictor is the (m,n) predictor
(m,n) predictor : use the behavior of the last m branches to
choose from one of 2m branch predictors, each of which is an
n-bit predictor
The history of the most recent m branches is recorded in an
m-bit shift register called the m-bit global history register
shift in the behavior bit for the most recent branch, shift out the
the bit for the least recent branch
Index into the BHT by concatenating the lower bits of the
branch instruction address with the m-bit global history to
access an n-bit entry
(2, 2) Correlating Branch Predictors
Example of (2, 2) Correlating Predictor
Example of (2, 2) Correlating
Predictor
Accuracy of Correlating
Predictors
Accuracy of Correlating Predictors
Simple Example
Note : A 2-bit predictor with no global history is simply a (0,2)
predictor
nasa7 98%
matrix300 100%
tomcatv 94%
doduc 90%
spice 55%
fpppp 76%
gcc 72%
espresso 63%
eqntott 37%
li 69%
Accuracy of Branch Prediction
99%
tomcatv 99%
100%
95%
doduc 84%
97%
86%
fpppp 82%
98% Profile-based
2-bit counter
88% Tournament
li 77%
98%
86%
espresso 82%
96%
88%
gcc 70%
94%
9%
8%
5%
4%
Correlating - (2,2) scheme
3%
2% Tournament
1%
0%
0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128
Branch PC Predicted PC
FETCH
PC of instruction
=? Yes: instruction is
branch; use Prediction state
predicted PC as bits
No: branch not predicted;
proceed normally (PC+4) next PC (if
predict Taken)
Branch Target Cache
Branch Target cache - Only predicted taken branches
Cache - Content Addressable Memory (CAM) or Associative
Memory (see figure)
Use a big Branch History Table & a small Branch Target Cache
Branch PC Predicted PC
PC
=? Prediction state
Yes: predicted taken bits (optional)
No: not found branch found
Steps with Branch target Buffer
Send PC to memory for the 5-stage MIPS
and branch-target
buffer
IF
No Entry found Yes
in branch-
target
buffer?