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 #if THE_ISA == ALPHA_ISA
205 Addr ras_pc = PC + sizeof(MachInst); // Next PC
207 Addr ras_pc = PC + (2 * sizeof(MachInst)); // Next Next PC
209 RAS[tid].push(ras_pc);
211 // Record that it was a call so that the top RAS entry can
212 // be popped off if the speculation is incorrect.
213 predict_record.wasCall = true;
215 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x was a call"
216 ", adding %#x to the RAS.\n",
217 tid, inst->readPC(), ras_pc);
220 if (BTB.valid(PC, tid)) {
223 // If it's not a return, use the BTB to get the target addr.
224 target = BTB.lookup(PC, tid);
226 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x predicted"
228 tid, inst->readPC(), target);
231 DPRINTF(Fetch, "BranchPred: [tid:%i]: BTB doesn't have a "
232 "valid entry.\n",tid);
240 // Set the PC and the instruction's predicted target.
242 inst->setPredTarg(target);
244 PC = PC + sizeof(MachInst);
245 inst->setPredTarg(PC);
248 predHist[tid].push_front(predict_record);
250 DPRINTF(Fetch, "[tid:%i]: predHist.size(): %i\n", tid, predHist[tid].size());
255 template <class Impl>
257 BPredUnit<Impl>::update(const InstSeqNum &done_sn, unsigned tid)
259 DPRINTF(Fetch, "BranchPred: [tid:%i]: Commiting branches until "
260 "[sn:%lli].\n", tid, done_sn);
262 while (!predHist[tid].empty() &&
263 predHist[tid].back().seqNum <= done_sn) {
264 // Update the branch predictor with the correct results.
265 BPUpdate(predHist[tid].back().PC,
266 predHist[tid].back().predTaken,
267 predHist[tid].back().bpHistory);
269 predHist[tid].pop_back();
273 template <class Impl>
275 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn, unsigned tid)
277 History &pred_hist = predHist[tid];
279 while (!pred_hist.empty() &&
280 pred_hist.front().seqNum > squashed_sn) {
281 if (pred_hist.front().usedRAS) {
282 DPRINTF(Fetch, "BranchPred: [tid:%i]: Restoring top of RAS to: %i,"
285 pred_hist.front().RASIndex,
286 pred_hist.front().RASTarget);
288 RAS[tid].restore(pred_hist.front().RASIndex,
289 pred_hist.front().RASTarget);
291 } else if (pred_hist.front().wasCall) {
292 DPRINTF(Fetch, "BranchPred: [tid:%i]: Removing speculative entry "
293 "added to the RAS.\n",tid);
298 // This call should delete the bpHistory.
299 BPSquash(pred_hist.front().bpHistory);
301 pred_hist.pop_front();
306 template <class Impl>
308 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn,
309 const Addr &corr_target,
310 const bool actually_taken,
313 // Now that we know that a branch was mispredicted, we need to undo
314 // all the branches that have been seen up until this branch and
315 // fix up everything.
317 History &pred_hist = predHist[tid];
321 DPRINTF(Fetch, "BranchPred: [tid:%i]: Squashing from sequence number %i, "
322 "setting target to %#x.\n",
323 tid, squashed_sn, corr_target);
325 squash(squashed_sn, tid);
327 // If there's a squash due to a syscall, there may not be an entry
328 // corresponding to the squash. In that case, don't bother trying to
330 if (!pred_hist.empty()) {
331 assert(pred_hist.front().seqNum == squashed_sn);
332 if (pred_hist.front().usedRAS) {
336 BPUpdate(pred_hist.front().PC, actually_taken,
337 pred_hist.front().bpHistory);
339 BTB.update(pred_hist.front().PC, corr_target, tid);
340 pred_hist.pop_front();
344 template <class Impl>
346 BPredUnit<Impl>::BPUncond(void * &bp_history)
348 // Only the tournament predictor cares about unconditional branches.
349 if (predictor == Tournament) {
350 tournamentBP->uncondBr(bp_history);
354 template <class Impl>
356 BPredUnit<Impl>::BPSquash(void *bp_history)
358 if (predictor == Local) {
359 localBP->squash(bp_history);
360 } else if (predictor == Tournament) {
361 tournamentBP->squash(bp_history);
363 panic("Predictor type is unexpected value!");
367 template <class Impl>
369 BPredUnit<Impl>::BPLookup(Addr &inst_PC, void * &bp_history)
371 if (predictor == Local) {
372 return localBP->lookup(inst_PC, bp_history);
373 } else if (predictor == Tournament) {
374 return tournamentBP->lookup(inst_PC, bp_history);
376 panic("Predictor type is unexpected value!");
380 template <class Impl>
382 BPredUnit<Impl>::BPUpdate(Addr &inst_PC, bool taken, void *bp_history)
384 if (predictor == Local) {
385 localBP->update(inst_PC, taken, bp_history);
386 } else if (predictor == Tournament) {
387 tournamentBP->update(inst_PC, taken, bp_history);
389 panic("Predictor type is unexpected value!");
393 template <class Impl>
395 BPredUnit<Impl>::dump()
397 typename History::iterator pred_hist_it;
399 for (int i = 0; i < Impl::MaxThreads; ++i) {
400 if (!predHist[i].empty()) {
401 pred_hist_it = predHist[i].begin();
403 cprintf("predHist[%i].size(): %i\n", i, predHist[i].size());
405 while (pred_hist_it != predHist[i].end()) {
406 cprintf("[sn:%lli], PC:%#x, tid:%i, predTaken:%i, "
408 (*pred_hist_it).seqNum, (*pred_hist_it).PC,
409 (*pred_hist_it).tid, (*pred_hist_it).predTaken,
410 (*pred_hist_it).bpHistory);