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.
31 #include "arch/types.hh"
32 #include "arch/isa_traits.hh"
33 #include "base/trace.hh"
34 #include "base/traceflags.hh"
35 #include "cpu/o3/bpred_unit.hh"
38 BPredUnit<Impl>::BPredUnit(Params *params)
39 : BTB(params->BTBEntries,
43 // Setup the selected predictor.
44 if (params->predType == "local") {
45 localBP = new LocalBP(params->localPredictorSize,
47 params->instShiftAmt);
49 } else if (params->predType == "tournament") {
50 tournamentBP = new TournamentBP(params->localPredictorSize,
52 params->localHistoryTableSize,
53 params->localHistoryBits,
54 params->globalPredictorSize,
55 params->globalHistoryBits,
56 params->globalCtrBits,
57 params->choicePredictorSize,
58 params->choiceCtrBits,
59 params->instShiftAmt);
60 predictor = Tournament;
62 fatal("Invalid BP selected!");
65 for (int i=0; i < Impl::MaxThreads; i++)
66 RAS[i].init(params->RASSize);
71 BPredUnit<Impl>::regStats()
74 .name(name() + ".BPredUnit.lookups")
75 .desc("Number of BP lookups")
79 .name(name() + ".BPredUnit.condPredicted")
80 .desc("Number of conditional branches predicted")
84 .name(name() + ".BPredUnit.condIncorrect")
85 .desc("Number of conditional branches incorrect")
89 .name(name() + ".BPredUnit.BTBLookups")
90 .desc("Number of BTB lookups")
94 .name(name() + ".BPredUnit.BTBHits")
95 .desc("Number of BTB hits")
99 .name(name() + ".BPredUnit.BTBCorrect")
100 .desc("Number of correct BTB predictions (this stat may not "
105 .name(name() + ".BPredUnit.usedRAS")
106 .desc("Number of times the RAS was used to get a target.")
110 .name(name() + ".BPredUnit.RASInCorrect")
111 .desc("Number of incorrect RAS predictions.")
115 template <class Impl>
117 BPredUnit<Impl>::switchOut()
119 // Clear any state upon switch out.
120 for (int i = 0; i < Impl::MaxThreads; ++i) {
125 template <class Impl>
127 BPredUnit<Impl>::takeOverFrom()
129 // Can reset all predictor state, but it's not necessarily better
130 // than leaving it be.
132 for (int i = 0; i < Impl::MaxThreads; ++i)
140 template <class Impl>
142 BPredUnit<Impl>::predict(DynInstPtr &inst, Addr &PC, unsigned tid)
144 // See if branch predictor predicts taken.
145 // If so, get its target addr either from the BTB or the RAS.
146 // Save off record of branch stuff so the RAS can be fixed
147 // up once it's done.
149 using TheISA::MachInst;
151 bool pred_taken = false;
156 void *bp_history = NULL;
158 if (inst->isUncondCtrl()) {
159 DPRINTF(Fetch, "BranchPred: [tid:%i]: Unconditional control.\n", tid);
161 // Tell the BP there was an unconditional branch.
162 BPUncond(bp_history);
166 pred_taken = BPLookup(PC, bp_history);
168 DPRINTF(Fetch, "BranchPred: [tid:%i]: Branch predictor predicted %i "
170 tid, pred_taken, inst->readPC());
173 PredictorHistory predict_record(inst->seqNum, PC, pred_taken,
176 // Now lookup in the BTB or RAS.
178 if (inst->isReturn()) {
181 // If it's a function return call, then look up the address
183 target = RAS[tid].top();
185 // Record the top entry of the RAS, and its index.
186 predict_record.usedRAS = true;
187 predict_record.RASIndex = RAS[tid].topIdx();
188 predict_record.RASTarget = target;
190 assert(predict_record.RASIndex < 16);
194 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x is a return, "
195 "RAS predicted target: %#x, RAS index: %i.\n",
196 tid, inst->readPC(), target, predict_record.RASIndex);
200 if (inst->isCall()) {
201 #if ISA_HAS_DELAY_SLOT
202 Addr ras_pc = PC + (2 * sizeof(MachInst)); // Next Next PC
204 Addr ras_pc = PC + sizeof(MachInst); // Next PC
206 RAS[tid].push(ras_pc);
208 // Record that it was a call so that the top RAS entry can
209 // be popped off if the speculation is incorrect.
210 predict_record.wasCall = true;
212 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x was a call"
213 ", adding %#x to the RAS index: %i.\n",
214 tid, inst->readPC(), ras_pc, RAS[tid].topIdx());
217 if (BTB.valid(PC, tid)) {
220 // If it's not a return, use the BTB to get the target addr.
221 target = BTB.lookup(PC, tid);
223 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x predicted"
225 tid, inst->readPC(), target);
228 DPRINTF(Fetch, "BranchPred: [tid:%i]: BTB doesn't have a "
229 "valid entry.\n",tid);
238 predHist[tid].push_front(predict_record);
240 DPRINTF(Fetch, "[tid:%i]: predHist.size(): %i\n", tid, predHist[tid].size());
245 template <class Impl>
247 BPredUnit<Impl>::update(const InstSeqNum &done_sn, unsigned tid)
249 DPRINTF(Fetch, "BranchPred: [tid:%i]: Commiting branches until "
250 "[sn:%lli].\n", tid, done_sn);
252 while (!predHist[tid].empty() &&
253 predHist[tid].back().seqNum <= done_sn) {
254 // Update the branch predictor with the correct results.
255 BPUpdate(predHist[tid].back().PC,
256 predHist[tid].back().predTaken,
257 predHist[tid].back().bpHistory);
259 predHist[tid].pop_back();
263 template <class Impl>
265 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn, unsigned tid)
267 History &pred_hist = predHist[tid];
269 while (!pred_hist.empty() &&
270 pred_hist.front().seqNum > squashed_sn) {
271 if (pred_hist.front().usedRAS) {
272 DPRINTF(Fetch, "BranchPred: [tid:%i]: Restoring top of RAS to: %i,"
275 pred_hist.front().RASIndex,
276 pred_hist.front().RASTarget);
278 RAS[tid].restore(pred_hist.front().RASIndex,
279 pred_hist.front().RASTarget);
280 } else if (pred_hist.front().wasCall) {
281 DPRINTF(Fetch, "BranchPred: [tid:%i]: Removing speculative entry "
282 "added to the RAS.\n",tid);
287 // This call should delete the bpHistory.
288 BPSquash(pred_hist.front().bpHistory);
290 pred_hist.pop_front();
295 template <class Impl>
297 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn,
298 const Addr &corr_target,
299 const bool actually_taken,
302 // Now that we know that a branch was mispredicted, we need to undo
303 // all the branches that have been seen up until this branch and
304 // fix up everything.
306 History &pred_hist = predHist[tid];
310 DPRINTF(Fetch, "BranchPred: [tid:%i]: Squashing from sequence number %i, "
311 "setting target to %#x.\n",
312 tid, squashed_sn, corr_target);
314 squash(squashed_sn, tid);
316 // If there's a squash due to a syscall, there may not be an entry
317 // corresponding to the squash. In that case, don't bother trying to
319 if (!pred_hist.empty()) {
320 assert(pred_hist.front().seqNum == squashed_sn);
321 if (pred_hist.front().usedRAS) {
325 BPUpdate(pred_hist.front().PC, actually_taken,
326 pred_hist.front().bpHistory);
328 BTB.update(pred_hist.front().PC, corr_target, tid);
329 pred_hist.pop_front();
333 template <class Impl>
335 BPredUnit<Impl>::BPUncond(void * &bp_history)
337 // Only the tournament predictor cares about unconditional branches.
338 if (predictor == Tournament) {
339 tournamentBP->uncondBr(bp_history);
343 template <class Impl>
345 BPredUnit<Impl>::BPSquash(void *bp_history)
347 if (predictor == Local) {
348 localBP->squash(bp_history);
349 } else if (predictor == Tournament) {
350 tournamentBP->squash(bp_history);
352 panic("Predictor type is unexpected value!");
356 template <class Impl>
358 BPredUnit<Impl>::BPLookup(Addr &inst_PC, void * &bp_history)
360 if (predictor == Local) {
361 return localBP->lookup(inst_PC, bp_history);
362 } else if (predictor == Tournament) {
363 return tournamentBP->lookup(inst_PC, bp_history);
365 panic("Predictor type is unexpected value!");
369 template <class Impl>
371 BPredUnit<Impl>::BPUpdate(Addr &inst_PC, bool taken, void *bp_history)
373 if (predictor == Local) {
374 localBP->update(inst_PC, taken, bp_history);
375 } else if (predictor == Tournament) {
376 tournamentBP->update(inst_PC, taken, bp_history);
378 panic("Predictor type is unexpected value!");
382 template <class Impl>
384 BPredUnit<Impl>::dump()
386 typename History::iterator pred_hist_it;
388 for (int i = 0; i < Impl::MaxThreads; ++i) {
389 if (!predHist[i].empty()) {
390 pred_hist_it = predHist[i].begin();
392 cprintf("predHist[%i].size(): %i\n", i, predHist[i].size());
394 while (pred_hist_it != predHist[i].end()) {
395 cprintf("[sn:%lli], PC:%#x, tid:%i, predTaken:%i, "
397 (*pred_hist_it).seqNum, (*pred_hist_it).PC,
398 (*pred_hist_it).tid, (*pred_hist_it).predTaken,
399 (*pred_hist_it).bpHistory);