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.
29 #include "base/trace.hh"
30 #include "base/traceflags.hh"
31 #include "cpu/o3/bpred_unit.hh"
34 TwobitBPredUnit<Impl>::TwobitBPredUnit(Params ¶ms)
35 : BP(params.local_predictor_size,
36 params.local_ctr_bits,
38 BTB(params.BTBEntries,
47 TwobitBPredUnit<Impl>::regStats()
50 .name(name() + ".BPredUnit.lookups")
51 .desc("Number of BP lookups")
55 .name(name() + ".BPredUnit.condPredicted")
56 .desc("Number of conditional branches predicted")
60 .name(name() + ".BPredUnit.condIncorrect")
61 .desc("Number of conditional branches incorrect")
65 .name(name() + ".BPredUnit.BTBLookups")
66 .desc("Number of BTB lookups")
70 .name(name() + ".BPredUnit.BTBHits")
71 .desc("Number of BTB hits")
75 .name(name() + ".BPredUnit.BTBCorrect")
76 .desc("Number of correct BTB predictions (this stat may not "
81 .name(name() + ".BPredUnit.usedRAS")
82 .desc("Number of times the RAS was used.")
86 .name(name() + ".BPredUnit.RASInCorrect")
87 .desc("Number of incorrect RAS predictions.")
93 TwobitBPredUnit<Impl>::predict(DynInstPtr &inst, Addr &PC)
95 // See if branch predictor predicts taken.
96 // If so, get its target addr either from the BTB or the RAS.
97 // Once that's done, speculatively update the predictor?
98 // Save off record of branch stuff so the RAS can be fixed
101 using TheISA::MachInst;
103 bool pred_taken = false;
108 if (inst->isUncondCtrl()) {
109 DPRINTF(Fetch, "BranchPred: Unconditional control.\n");
114 pred_taken = BPLookup(PC);
116 DPRINTF(Fetch, "BranchPred: Branch predictor predicted %i for PC %#x"
117 "\n", pred_taken, inst->readPC());
120 PredictorHistory predict_record(inst->seqNum, PC, pred_taken);
122 // Now lookup in the BTB or RAS.
124 if (inst->isReturn()) {
127 // If it's a function return call, then look up the address
131 // Record the top entry of the RAS, and its index.
132 predict_record.usedRAS = true;
133 predict_record.RASIndex = RAS.topIdx();
134 predict_record.RASTarget = target;
138 DPRINTF(Fetch, "BranchPred: Instruction %#x is a return, RAS "
139 "predicted target: %#x, RAS index: %i.\n",
140 inst->readPC(), target, predict_record.RASIndex);
144 if (inst->isCall()) {
145 RAS.push(PC+sizeof(MachInst));
147 // Record that it was a call so that the top RAS entry can
148 // be popped off if the speculation is incorrect.
149 predict_record.wasCall = true;
151 DPRINTF(Fetch, "BranchPred: Instruction %#x was a call, "
152 "adding %#x to the RAS.\n",
153 inst->readPC(), PC+sizeof(MachInst));
159 //If it's anything else, use the BTB to get the target addr.
160 target = BTB.lookup(PC);
162 DPRINTF(Fetch, "BranchPred: Instruction %#x predicted target "
163 "is %#x.\n", inst->readPC(), target);
166 DPRINTF(Fetch, "BranchPred: BTB doesn't have a valid entry."
175 // Set the PC and the instruction's predicted target.
177 inst->setPredTarg(target);
179 PC = PC + sizeof(MachInst);
180 inst->setPredTarg(PC);
183 predHist.push_front(predict_record);
185 assert(!predHist.empty());
190 template <class Impl>
192 TwobitBPredUnit<Impl>::update(const InstSeqNum &done_sn)
194 DPRINTF(Fetch, "BranchPred: Commiting branches until sequence number "
197 while (!predHist.empty() && predHist.back().seqNum <= done_sn) {
198 assert(!predHist.empty());
200 // Update the branch predictor with the correct results of branches.
201 BP.update(predHist.back().PC, predHist.back().predTaken);
207 template <class Impl>
209 TwobitBPredUnit<Impl>::squash(const InstSeqNum &squashed_sn)
211 while (!predHist.empty() && predHist.front().seqNum > squashed_sn) {
212 if (predHist.front().usedRAS) {
213 DPRINTF(Fetch, "BranchPred: Restoring top of RAS to: %i, "
215 predHist.front().RASIndex,
216 predHist.front().RASTarget);
218 RAS.restore(predHist.front().RASIndex,
219 predHist.front().RASTarget);
220 } else if (predHist.front().wasCall) {
221 DPRINTF(Fetch, "BranchPred: Removing speculative entry added "
227 predHist.pop_front();
231 template <class Impl>
233 TwobitBPredUnit<Impl>::squash(const InstSeqNum &squashed_sn,
234 const Addr &corr_target,
235 const bool actually_taken)
237 // Now that we know that a branch was mispredicted, we need to undo
238 // all the branches that have been seen up until this branch and
239 // fix up everything.
243 DPRINTF(Fetch, "BranchPred: Squashing from sequence number %i, "
244 "setting target to %#x.\n",
245 squashed_sn, corr_target);
247 while (!predHist.empty() && predHist.front().seqNum > squashed_sn) {
249 if (predHist.front().usedRAS) {
250 DPRINTF(Fetch, "BranchPred: Restoring top of RAS to: %i, "
252 predHist.front().RASIndex,
253 predHist.front().RASTarget);
255 RAS.restore(predHist.front().RASIndex,
256 predHist.front().RASTarget);
257 } else if (predHist.front().wasCall) {
258 DPRINTF(Fetch, "BranchPred: Removing speculative entry added "
264 predHist.pop_front();
267 predHist.front().predTaken = actually_taken;
269 if (predHist.front().usedRAS) {
273 BP.update(predHist.front().PC, actually_taken);
275 BTB.update(predHist.front().PC, corr_target);