2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "base/intmath.hh"
32 #include "cpu/o3/tournament_pred.hh"
34 TournamentBP::TournamentBP(unsigned _localPredictorSize
,
35 unsigned _localCtrBits
,
36 unsigned _localHistoryTableSize
,
37 unsigned _localHistoryBits
,
38 unsigned _globalPredictorSize
,
39 unsigned _globalCtrBits
,
40 unsigned _globalHistoryBits
,
41 unsigned _choicePredictorSize
,
42 unsigned _choiceCtrBits
,
43 unsigned _instShiftAmt
)
44 : localPredictorSize(_localPredictorSize
),
45 localCtrBits(_localCtrBits
),
46 localHistoryTableSize(_localHistoryTableSize
),
47 localHistoryBits(_localHistoryBits
),
48 globalPredictorSize(_globalPredictorSize
),
49 globalCtrBits(_globalCtrBits
),
50 globalHistoryBits(_globalHistoryBits
),
51 choicePredictorSize(_globalPredictorSize
),
52 choiceCtrBits(_choiceCtrBits
),
53 instShiftAmt(_instShiftAmt
)
55 if (!isPowerOf2(localPredictorSize
)) {
56 fatal("Invalid local predictor size!\n");
59 //Setup the array of counters for the local predictor
60 localCtrs
.resize(localPredictorSize
);
62 for (int i
= 0; i
< localPredictorSize
; ++i
)
63 localCtrs
[i
].setBits(localCtrBits
);
65 if (!isPowerOf2(localHistoryTableSize
)) {
66 fatal("Invalid local history table size!\n");
69 //Setup the history table for the local table
70 localHistoryTable
.resize(localHistoryTableSize
);
72 for (int i
= 0; i
< localHistoryTableSize
; ++i
)
73 localHistoryTable
[i
] = 0;
75 // Setup the local history mask
76 localHistoryMask
= (1 << localHistoryBits
) - 1;
78 if (!isPowerOf2(globalPredictorSize
)) {
79 fatal("Invalid global predictor size!\n");
82 //Setup the array of counters for the global predictor
83 globalCtrs
.resize(globalPredictorSize
);
85 for (int i
= 0; i
< globalPredictorSize
; ++i
)
86 globalCtrs
[i
].setBits(globalCtrBits
);
88 //Clear the global history
90 // Setup the global history mask
91 globalHistoryMask
= (1 << globalHistoryBits
) - 1;
93 if (!isPowerOf2(choicePredictorSize
)) {
94 fatal("Invalid choice predictor size!\n");
97 //Setup the array of counters for the choice predictor
98 choiceCtrs
.resize(choicePredictorSize
);
100 for (int i
= 0; i
< choicePredictorSize
; ++i
)
101 choiceCtrs
[i
].setBits(choiceCtrBits
);
103 // @todo: Allow for different thresholds between the predictors.
104 threshold
= (1 << (localCtrBits
- 1)) - 1;
105 threshold
= threshold
/ 2;
110 TournamentBP::calcLocHistIdx(Addr
&branch_addr
)
112 // Get low order bits after removing instruction offset.
113 return (branch_addr
>> instShiftAmt
) & (localHistoryTableSize
- 1);
118 TournamentBP::updateGlobalHistTaken()
120 globalHistory
= (globalHistory
<< 1) | 1;
121 globalHistory
= globalHistory
& globalHistoryMask
;
126 TournamentBP::updateGlobalHistNotTaken()
128 globalHistory
= (globalHistory
<< 1);
129 globalHistory
= globalHistory
& globalHistoryMask
;
134 TournamentBP::updateLocalHistTaken(unsigned local_history_idx
)
136 localHistoryTable
[local_history_idx
] =
137 (localHistoryTable
[local_history_idx
] << 1) | 1;
142 TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx
)
144 localHistoryTable
[local_history_idx
] =
145 (localHistoryTable
[local_history_idx
] << 1);
149 TournamentBP::lookup(Addr
&branch_addr
, void * &bp_history
)
151 bool local_prediction
;
152 unsigned local_history_idx
;
153 unsigned local_predictor_idx
;
155 bool global_prediction
;
156 bool choice_prediction
;
158 //Lookup in the local predictor to get its branch prediction
159 local_history_idx
= calcLocHistIdx(branch_addr
);
160 local_predictor_idx
= localHistoryTable
[local_history_idx
]
162 local_prediction
= localCtrs
[local_predictor_idx
].read() > threshold
;
164 //Lookup in the global predictor to get its branch prediction
165 global_prediction
= globalCtrs
[globalHistory
].read() > threshold
;
167 //Lookup in the choice predictor to see which one to use
168 choice_prediction
= choiceCtrs
[globalHistory
].read() > threshold
;
170 // Create BPHistory and pass it back to be recorded.
171 BPHistory
*history
= new BPHistory
;
172 history
->globalHistory
= globalHistory
;
173 history
->localPredTaken
= local_prediction
;
174 history
->globalPredTaken
= global_prediction
;
175 history
->globalUsed
= choice_prediction
;
176 bp_history
= (void *)history
;
178 assert(globalHistory
< globalPredictorSize
&&
179 local_history_idx
< localPredictorSize
);
181 // Commented code is for doing speculative update of counters and
183 if (choice_prediction
) {
184 if (global_prediction
) {
185 // updateHistoriesTaken(local_history_idx);
186 // globalCtrs[globalHistory].increment();
187 // localCtrs[local_history_idx].increment();
188 updateGlobalHistTaken();
191 // updateHistoriesNotTaken(local_history_idx);
192 // globalCtrs[globalHistory].decrement();
193 // localCtrs[local_history_idx].decrement();
194 updateGlobalHistNotTaken();
198 if (local_prediction
) {
199 // updateHistoriesTaken(local_history_idx);
200 // globalCtrs[globalHistory].increment();
201 // localCtrs[local_history_idx].increment();
202 updateGlobalHistTaken();
205 // updateHistoriesNotTaken(local_history_idx);
206 // globalCtrs[globalHistory].decrement();
207 // localCtrs[local_history_idx].decrement();
208 updateGlobalHistNotTaken();
215 TournamentBP::uncondBr(void * &bp_history
)
217 // Create BPHistory and pass it back to be recorded.
218 BPHistory
*history
= new BPHistory
;
219 history
->globalHistory
= globalHistory
;
220 history
->localPredTaken
= true;
221 history
->globalPredTaken
= true;
222 bp_history
= static_cast<void *>(history
);
224 updateGlobalHistTaken();
228 TournamentBP::update(Addr
&branch_addr
, bool taken
, void *bp_history
)
230 unsigned local_history_idx
;
231 unsigned local_predictor_idx
;
232 unsigned local_predictor_hist
;
234 // Get the local predictor's current prediction
235 local_history_idx
= calcLocHistIdx(branch_addr
);
236 local_predictor_hist
= localHistoryTable
[local_history_idx
];
237 local_predictor_idx
= local_predictor_hist
& localHistoryMask
;
239 // Update the choice predictor to tell it which one was correct if
240 // there was a prediction.
242 BPHistory
*history
= static_cast<BPHistory
*>(bp_history
);
243 if (history
->localPredTaken
!= history
->globalPredTaken
) {
244 // If the local prediction matches the actual outcome,
245 // decerement the counter. Otherwise increment the
247 if (history
->localPredTaken
== taken
) {
248 choiceCtrs
[globalHistory
].decrement();
249 } else if (history
->globalPredTaken
== taken
){
250 choiceCtrs
[globalHistory
].increment();
254 // We're done with this history, now delete it.
258 assert(globalHistory
< globalPredictorSize
&&
259 local_predictor_idx
< localPredictorSize
);
261 // Update the counters and local history with the proper
262 // resolution of the branch. Global history is updated
263 // speculatively and restored upon squash() calls, so it does not
264 // need to be updated.
266 localCtrs
[local_predictor_idx
].increment();
267 globalCtrs
[globalHistory
].increment();
269 updateLocalHistTaken(local_history_idx
);
271 localCtrs
[local_predictor_idx
].decrement();
272 globalCtrs
[globalHistory
].decrement();
274 updateLocalHistNotTaken(local_history_idx
);
279 TournamentBP::squash(void *bp_history
)
281 BPHistory
*history
= static_cast<BPHistory
*>(bp_history
);
283 // Restore global history to state prior to this branch.
284 globalHistory
= history
->globalHistory
;
286 // Delete this BPHistory now that we're done with it.
292 TournamentBP::BPHistory::newCount
= 0;