CA_L15b_BranchPrediction_DynamicPredictors
CA_L15b_BranchPrediction_DynamicPredictors
• Advantages
+ Prediction based on history of the execution of branches
+ It can adapt to dynamic changes in branch behavior
• Disadvantages
-- More complex than static predictors (requires additional hardware)
2
Our first example of dynamic branch predictor
3 3
Last Time Predictor
• It uses one bit per branch
• This bit records which direction branch went last time
it executed
• The same direction is output as the prediction next
time
State machine:
actually
predict taken actually
actually predict
not taken
not taken taken
taken
actually
not taken
4
Solved Example 1
• (a) Consider this sequence: T, NT, NT, NT, T
• What is the accuracy of always-taken and always-not-taken predictors for
this sequence of branch outcomes?
• Always-Taken Predictor:
• Actual T NT NT NT T
• Predict T T T T T
• Accuracy = 2/5 = 0.4
• Always-not-taken Predictor:
• Actual T NT NT NT T
• Predict NT NT NT NT NT
5 • Accuracy = 3/5 = 0.6
Solved Example 1 (continued)
Actual T NT NT NT T
Predict NT T NT NT NT
Accuracy = 2/5 = 0.4
6
Example 2
• For(int i=0; i<N; i++) {
• if(i>N/2) doSomething;
• }
• This code is executed once. The last-time predictor for this “if”
branch is initialized to “not-taken”. Find accuracy. N=6
10
Example 4 (continued)
Initially, i=0; we enter into second for loop for the first time.
This “NT” comes from NewState value stored from first time.
j Prediction Outcome NewState
0 NT T T
1 T T T
2 T T T
3 T T T
4 T T T
5 T T T
6 T NT NT
13
• Our second example of dynamic branch predictor
Two-bit predictor
14
Improving the Last Time Predictor
• Problem: A last-time predictor changes its prediction from
TNT or NTT too quickly
• even though the branch may be mostly taken or mostly not taken
actually
pred !taken pred Strongly not-taken
Weakly !taken !taken
not-taken
01 actually 00 actually
16 taken !taken
Example 1a: initialized with Weak NT
• For(int i=0; i<N; i++) {
if(i%2==0) doSomething;
}
• This code is executed once. The two-bit predictor for this “if” branch
is initialized to “weak NT”. Find accuracy. N=6
22
Example : Nested for loop
• For (int i=0; i<M; i++) {
• For (int j=0; j<N; j++) {
• doSomething;
• }
• }
• In second for loop, we check j<N condition, which is a branch instruction.
• We use 2-bit predictor for this branch, which is initialized to sT.
• Assume: Going inside loop= taken. Otherwise =not-taken.
• N=6
• Compute accuracy of this branch predictor in steady-state.
23
Example (continued)
Initially, i=0; we enter into second for loop for the first time.
This “T” comes from fact that the predictor is initialized to sT.
This “T” comes from NewState value of wT stored from first time.
j State Prediction Outcome NewState
0 wT T T sT
1 sT T T sT
2 sT T T sT
3 sT T T sT
4 sT T T sT
5 sT T T sT
6 sT T NT wT