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 "base/trace.hh"
33 #include "base/traceflags.hh"
34 #include "cpu/o3/bpred_unit.hh"
37 BPredUnit<Impl>::BPredUnit(Params *params)
38 : BTB(params->BTBEntries,
42 // Setup the selected predictor.
43 if (params->predType == "local") {
44 localBP = new LocalBP(params->localPredictorSize,
46 params->instShiftAmt);
48 } else if (params->predType == "tournament") {
49 tournamentBP = new TournamentBP(params->localPredictorSize,
51 params->localHistoryTableSize,
52 params->localHistoryBits,
53 params->globalPredictorSize,
54 params->globalHistoryBits,
55 params->globalCtrBits,
56 params->choicePredictorSize,
57 params->choiceCtrBits,
58 params->instShiftAmt);
59 predictor = Tournament;
61 fatal("Invalid BP selected!");
64 for (int i=0; i < Impl::MaxThreads; i++)
65 RAS[i].init(params->RASSize);
70 BPredUnit<Impl>::regStats()
73 .name(name() + ".BPredUnit.lookups")
74 .desc("Number of BP lookups")
78 .name(name() + ".BPredUnit.condPredicted")
79 .desc("Number of conditional branches predicted")
83 .name(name() + ".BPredUnit.condIncorrect")
84 .desc("Number of conditional branches incorrect")
88 .name(name() + ".BPredUnit.BTBLookups")
89 .desc("Number of BTB lookups")
93 .name(name() + ".BPredUnit.BTBHits")
94 .desc("Number of BTB hits")
98 .name(name() + ".BPredUnit.BTBCorrect")
99 .desc("Number of correct BTB predictions (this stat may not "
104 .name(name() + ".BPredUnit.usedRAS")
105 .desc("Number of times the RAS was used to get a target.")
109 .name(name() + ".BPredUnit.RASInCorrect")
110 .desc("Number of incorrect RAS predictions.")
114 template <class Impl>
116 BPredUnit<Impl>::switchOut()
118 // Clear any state upon switch out.
119 for (int i = 0; i < Impl::MaxThreads; ++i) {
124 template <class Impl>
126 BPredUnit<Impl>::takeOverFrom()
128 // Can reset all predictor state, but it's not necessarily better
129 // than leaving it be.
131 for (int i = 0; i < Impl::MaxThreads; ++i)
139 template <class Impl>
141 BPredUnit<Impl>::predict(DynInstPtr &inst, Addr &PC, unsigned tid)
143 // See if branch predictor predicts taken.
144 // If so, get its target addr either from the BTB or the RAS.
145 // Save off record of branch stuff so the RAS can be fixed
146 // up once it's done.
148 using TheISA::MachInst;
150 bool pred_taken = false;
155 void *bp_history = NULL;
157 if (inst->isUncondCtrl()) {
158 DPRINTF(Fetch, "BranchPred: [tid:%i]: Unconditional control.\n", tid);
160 // Tell the BP there was an unconditional branch.
161 BPUncond(bp_history);
165 pred_taken = BPLookup(PC, bp_history);
167 DPRINTF(Fetch, "BranchPred: [tid:%i]: Branch predictor predicted %i "
169 tid, pred_taken, inst->readPC());
172 PredictorHistory predict_record(inst->seqNum, PC, pred_taken,
175 // Now lookup in the BTB or RAS.
177 if (inst->isReturn()) {
180 // If it's a function return call, then look up the address
182 target = RAS[tid].top();
184 // Record the top entry of the RAS, and its index.
185 predict_record.usedRAS = true;
186 predict_record.RASIndex = RAS[tid].topIdx();
187 predict_record.RASTarget = target;
189 assert(predict_record.RASIndex < 16);
193 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x is a return, "
194 "RAS predicted target: %#x, RAS index: %i.\n",
195 tid, inst->readPC(), target, predict_record.RASIndex);
199 if (inst->isCall()) {
200 #if THE_ISA == ALPHA_ISA
201 Addr ras_pc = PC + sizeof(MachInst); // Next PC
203 Addr ras_pc = PC + (2 * sizeof(MachInst)); // Next Next PC
205 RAS[tid].push(ras_pc);
207 // Record that it was a call so that the top RAS entry can
208 // be popped off if the speculation is incorrect.
209 predict_record.wasCall = true;
211 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x was a call"
212 ", adding %#x to the RAS.\n",
213 tid, inst->readPC(), ras_pc);
216 if (BTB.valid(PC, tid)) {
219 // If it's not a return, use the BTB to get the target addr.
220 target = BTB.lookup(PC, tid);
222 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x predicted"
224 tid, inst->readPC(), target);
227 DPRINTF(Fetch, "BranchPred: [tid:%i]: BTB doesn't have a "
228 "valid entry.\n",tid);
236 // Set the PC and the instruction's predicted target.
238 inst->setPredTarg(target);
240 PC = PC + sizeof(MachInst);
241 inst->setPredTarg(PC);
244 predHist[tid].push_front(predict_record);
246 DPRINTF(Fetch, "[tid:%i]: predHist.size(): %i\n", tid, predHist[tid].size());
251 template <class Impl>
253 BPredUnit<Impl>::update(const InstSeqNum &done_sn, unsigned tid)
255 DPRINTF(Fetch, "BranchPred: [tid:%i]: Commiting branches until "
256 "[sn:%lli].\n", tid, done_sn);
258 while (!predHist[tid].empty() &&
259 predHist[tid].back().seqNum <= done_sn) {
260 // Update the branch predictor with the correct results.
261 BPUpdate(predHist[tid].back().PC,
262 predHist[tid].back().predTaken,
263 predHist[tid].back().bpHistory);
265 predHist[tid].pop_back();
269 template <class Impl>
271 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn, unsigned tid)
273 History &pred_hist = predHist[tid];
275 while (!pred_hist.empty() &&
276 pred_hist.front().seqNum > squashed_sn) {
277 if (pred_hist.front().usedRAS) {
278 DPRINTF(Fetch, "BranchPred: [tid:%i]: Restoring top of RAS to: %i,"
281 pred_hist.front().RASIndex,
282 pred_hist.front().RASTarget);
284 RAS[tid].restore(pred_hist.front().RASIndex,
285 pred_hist.front().RASTarget);
287 } else if (pred_hist.front().wasCall) {
288 DPRINTF(Fetch, "BranchPred: [tid:%i]: Removing speculative entry "
289 "added to the RAS.\n",tid);
294 // This call should delete the bpHistory.
295 BPSquash(pred_hist.front().bpHistory);
297 pred_hist.pop_front();
302 template <class Impl>
304 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn,
305 const Addr &corr_target,
306 const bool actually_taken,
309 // Now that we know that a branch was mispredicted, we need to undo
310 // all the branches that have been seen up until this branch and
311 // fix up everything.
313 History &pred_hist = predHist[tid];
317 DPRINTF(Fetch, "BranchPred: [tid:%i]: Squashing from sequence number %i, "
318 "setting target to %#x.\n",
319 tid, squashed_sn, corr_target);
321 squash(squashed_sn, tid);
323 // If there's a squash due to a syscall, there may not be an entry
324 // corresponding to the squash. In that case, don't bother trying to
326 if (!pred_hist.empty()) {
327 assert(pred_hist.front().seqNum == squashed_sn);
328 if (pred_hist.front().usedRAS) {
332 BPUpdate(pred_hist.front().PC, actually_taken,
333 pred_hist.front().bpHistory);
335 BTB.update(pred_hist.front().PC, corr_target, tid);
336 pred_hist.pop_front();
340 template <class Impl>
342 BPredUnit<Impl>::BPUncond(void * &bp_history)
344 // Only the tournament predictor cares about unconditional branches.
345 if (predictor == Tournament) {
346 tournamentBP->uncondBr(bp_history);
350 template <class Impl>
352 BPredUnit<Impl>::BPSquash(void *bp_history)
354 if (predictor == Local) {
355 localBP->squash(bp_history);
356 } else if (predictor == Tournament) {
357 tournamentBP->squash(bp_history);
359 panic("Predictor type is unexpected value!");
363 template <class Impl>
365 BPredUnit<Impl>::BPLookup(Addr &inst_PC, void * &bp_history)
367 if (predictor == Local) {
368 return localBP->lookup(inst_PC, bp_history);
369 } else if (predictor == Tournament) {
370 return tournamentBP->lookup(inst_PC, bp_history);
372 panic("Predictor type is unexpected value!");
376 template <class Impl>
378 BPredUnit<Impl>::BPUpdate(Addr &inst_PC, bool taken, void *bp_history)
380 if (predictor == Local) {
381 localBP->update(inst_PC, taken, bp_history);
382 } else if (predictor == Tournament) {
383 tournamentBP->update(inst_PC, taken, bp_history);
385 panic("Predictor type is unexpected value!");
389 template <class Impl>
391 BPredUnit<Impl>::dump()
393 typename History::iterator pred_hist_it;
395 for (int i = 0; i < Impl::MaxThreads; ++i) {
396 if (!predHist[i].empty()) {
397 pred_hist_it = predHist[i].begin();
399 cprintf("predHist[%i].size(): %i\n", i, predHist[i].size());
401 while (pred_hist_it != predHist[i].end()) {
402 cprintf("[sn:%lli], PC:%#x, tid:%i, predTaken:%i, "
404 (*pred_hist_it).seqNum, (*pred_hist_it).PC,
405 (*pred_hist_it).tid, (*pred_hist_it).predTaken,
406 (*pred_hist_it).bpHistory);