2 * Copyright (c) 2004-2005 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.
34 #include "base/trace.hh"
35 #include "base/traceflags.hh"
36 #include "cpu/o3/bpred_unit.hh"
41 BPredUnit<Impl>::BPredUnit(Params *params)
42 : BTB(params->BTBEntries,
46 // Setup the selected predictor.
47 if (params->predType == "local") {
48 localBP = new LocalBP(params->localPredictorSize,
50 params->instShiftAmt);
52 } else if (params->predType == "tournament") {
53 tournamentBP = new TournamentBP(params->localPredictorSize,
55 params->localHistoryTableSize,
56 params->localHistoryBits,
57 params->globalPredictorSize,
58 params->globalHistoryBits,
59 params->globalCtrBits,
60 params->choicePredictorSize,
61 params->choiceCtrBits,
62 params->instShiftAmt);
63 predictor = Tournament;
65 fatal("Invalid BP selected!");
68 for (int i=0; i < Impl::MaxThreads; i++)
69 RAS[i].init(params->RASSize);
74 BPredUnit<Impl>::regStats()
77 .name(name() + ".BPredUnit.lookups")
78 .desc("Number of BP lookups")
82 .name(name() + ".BPredUnit.condPredicted")
83 .desc("Number of conditional branches predicted")
87 .name(name() + ".BPredUnit.condIncorrect")
88 .desc("Number of conditional branches incorrect")
92 .name(name() + ".BPredUnit.BTBLookups")
93 .desc("Number of BTB lookups")
97 .name(name() + ".BPredUnit.BTBHits")
98 .desc("Number of BTB hits")
102 .name(name() + ".BPredUnit.BTBCorrect")
103 .desc("Number of correct BTB predictions (this stat may not "
108 .name(name() + ".BPredUnit.usedRAS")
109 .desc("Number of times the RAS was used to get a target.")
113 .name(name() + ".BPredUnit.RASInCorrect")
114 .desc("Number of incorrect RAS predictions.")
118 template <class Impl>
120 BPredUnit<Impl>::switchOut()
122 // Clear any state upon switch out.
123 for (int i = 0; i < Impl::MaxThreads; ++i) {
128 template <class Impl>
130 BPredUnit<Impl>::takeOverFrom()
132 // Can reset all predictor state, but it's not necessarily better
133 // than leaving it be.
135 for (int i = 0; i < Impl::MaxThreads; ++i)
143 template <class Impl>
145 BPredUnit<Impl>::predict(DynInstPtr &inst, Addr &PC, unsigned tid)
147 // See if branch predictor predicts taken.
148 // If so, get its target addr either from the BTB or the RAS.
149 // Save off record of branch stuff so the RAS can be fixed
150 // up once it's done.
152 using TheISA::MachInst;
154 bool pred_taken = false;
159 void *bp_history = NULL;
161 if (inst->isUncondCtrl()) {
162 DPRINTF(Fetch, "BranchPred: [tid:%i] Unconditional control.\n", tid);
164 // Tell the BP there was an unconditional branch.
165 BPUncond(bp_history);
169 pred_taken = BPLookup(PC, bp_history);
171 DPRINTF(Fetch, "BranchPred: [tid:%i]: Branch predictor predicted %i "
173 tid, pred_taken, inst->readPC());
176 PredictorHistory predict_record(inst->seqNum, PC, pred_taken,
179 // Now lookup in the BTB or RAS.
181 if (inst->isReturn()) {
184 // If it's a function return call, then look up the address
186 target = RAS[tid].top();
188 // Record the top entry of the RAS, and its index.
189 predict_record.usedRAS = true;
190 predict_record.RASIndex = RAS[tid].topIdx();
191 predict_record.RASTarget = target;
193 assert(predict_record.RASIndex < 16);
197 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x is a return, "
198 "RAS predicted target: %#x, RAS index: %i.\n",
199 tid, inst->readPC(), target, predict_record.RASIndex);
203 if (inst->isCall()) {
204 RAS[tid].push(PC + sizeof(MachInst));
206 // Record that it was a call so that the top RAS entry can
207 // be popped off if the speculation is incorrect.
208 predict_record.wasCall = true;
210 DPRINTF(Fetch, "BranchPred: [tid:%i] Instruction %#x was a call"
211 ", adding %#x to the RAS.\n",
212 tid, inst->readPC(), PC + sizeof(MachInst));
215 if (BTB.valid(PC, tid)) {
218 // If it's not a return, use the BTB to get the target addr.
219 target = BTB.lookup(PC, tid);
221 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x predicted"
223 tid, inst->readPC(), target);
226 DPRINTF(Fetch, "BranchPred: [tid:%i]: BTB doesn't have a "
227 "valid entry.\n",tid);
235 // Set the PC and the instruction's predicted target.
237 inst->setPredTarg(target);
239 PC = PC + sizeof(MachInst);
240 inst->setPredTarg(PC);
243 predHist[tid].push_front(predict_record);
245 DPRINTF(Fetch, "[tid:%i] predHist.size(): %i\n", tid, predHist[tid].size());
250 template <class Impl>
252 BPredUnit<Impl>::update(const InstSeqNum &done_sn, unsigned tid)
254 DPRINTF(Fetch, "BranchPred: [tid:%i]: Commiting branches until sequence"
255 "number %lli.\n", tid, done_sn);
257 while (!predHist[tid].empty() &&
258 predHist[tid].back().seqNum <= done_sn) {
259 // Update the branch predictor with the correct results.
260 BPUpdate(predHist[tid].back().PC,
261 predHist[tid].back().predTaken,
262 predHist[tid].back().bpHistory);
264 predHist[tid].pop_back();
268 template <class Impl>
270 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn, unsigned tid)
272 History &pred_hist = predHist[tid];
274 while (!pred_hist.empty() &&
275 pred_hist.front().seqNum > squashed_sn) {
276 if (pred_hist.front().usedRAS) {
277 DPRINTF(Fetch, "BranchPred: [tid:%i]: Restoring top of RAS to: %i,"
280 pred_hist.front().RASIndex,
281 pred_hist.front().RASTarget);
283 RAS[tid].restore(pred_hist.front().RASIndex,
284 pred_hist.front().RASTarget);
286 } else if (pred_hist.front().wasCall) {
287 DPRINTF(Fetch, "BranchPred: [tid:%i]: Removing speculative entry "
288 "added to the RAS.\n",tid);
293 // This call should delete the bpHistory.
294 BPSquash(pred_hist.front().bpHistory);
296 pred_hist.pop_front();
301 template <class Impl>
303 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn,
304 const Addr &corr_target,
305 const bool actually_taken,
308 // Now that we know that a branch was mispredicted, we need to undo
309 // all the branches that have been seen up until this branch and
310 // fix up everything.
312 History &pred_hist = predHist[tid];
316 DPRINTF(Fetch, "BranchPred: [tid:%i]: Squashing from sequence number %i, "
317 "setting target to %#x.\n",
318 tid, squashed_sn, corr_target);
320 squash(squashed_sn, tid);
322 // If there's a squash due to a syscall, there may not be an entry
323 // corresponding to the squash. In that case, don't bother trying to
325 if (!pred_hist.empty()) {
326 assert(pred_hist.front().seqNum == squashed_sn);
327 if (pred_hist.front().usedRAS) {
331 BPUpdate(pred_hist.front().PC, actually_taken,
332 pred_hist.front().bpHistory);
334 BTB.update(pred_hist.front().PC, corr_target, tid);
335 pred_hist.pop_front();
339 template <class Impl>
341 BPredUnit<Impl>::BPUncond(void * &bp_history)
343 // Only the tournament predictor cares about unconditional branches.
344 if (predictor == Tournament) {
345 tournamentBP->uncondBr(bp_history);
349 template <class Impl>
351 BPredUnit<Impl>::BPSquash(void *bp_history)
353 if (predictor == Local) {
354 localBP->squash(bp_history);
355 } else if (predictor == Tournament) {
356 tournamentBP->squash(bp_history);
358 panic("Predictor type is unexpected value!");
362 template <class Impl>
364 BPredUnit<Impl>::BPLookup(Addr &inst_PC, void * &bp_history)
366 if (predictor == Local) {
367 return localBP->lookup(inst_PC, bp_history);
368 } else if (predictor == Tournament) {
369 return tournamentBP->lookup(inst_PC, bp_history);
371 panic("Predictor type is unexpected value!");
375 template <class Impl>
377 BPredUnit<Impl>::BPUpdate(Addr &inst_PC, bool taken, void *bp_history)
379 if (predictor == Local) {
380 localBP->update(inst_PC, taken, bp_history);
381 } else if (predictor == Tournament) {
382 tournamentBP->update(inst_PC, taken, bp_history);
384 panic("Predictor type is unexpected value!");
388 template <class Impl>
390 BPredUnit<Impl>::dump()
392 typename History::iterator pred_hist_it;
394 for (int i = 0; i < Impl::MaxThreads; ++i) {
395 if (!predHist[i].empty()) {
396 pred_hist_it = predHist[i].begin();
398 cprintf("predHist[%i].size(): %i\n", i, predHist[i].size());
400 while (pred_hist_it != predHist[i].end()) {
401 cprintf("[sn:%lli], PC:%#x, tid:%i, predTaken:%i, "
403 (*pred_hist_it).seqNum, (*pred_hist_it).PC,
404 (*pred_hist_it).tid, (*pred_hist_it).predTaken,
405 (*pred_hist_it).bpHistory);