Merge zizzer:/bk/newmem
[gem5.git] / src / cpu / o3 / bpred_unit_impl.hh
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Kevin Lim
29 */
30
31 #include <list>
32 #include <vector>
33
34 #include "base/trace.hh"
35 #include "base/traceflags.hh"
36 #include "cpu/o3/bpred_unit.hh"
37
38 using namespace std;
39
40 template<class Impl>
41 BPredUnit<Impl>::BPredUnit(Params *params)
42 : BTB(params->BTBEntries,
43 params->BTBTagSize,
44 params->instShiftAmt)
45 {
46 // Setup the selected predictor.
47 if (params->predType == "local") {
48 localBP = new LocalBP(params->localPredictorSize,
49 params->localCtrBits,
50 params->instShiftAmt);
51 predictor = Local;
52 } else if (params->predType == "tournament") {
53 tournamentBP = new TournamentBP(params->localPredictorSize,
54 params->localCtrBits,
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;
64 } else {
65 fatal("Invalid BP selected!");
66 }
67
68 for (int i=0; i < Impl::MaxThreads; i++)
69 RAS[i].init(params->RASSize);
70 }
71
72 template <class Impl>
73 void
74 BPredUnit<Impl>::regStats()
75 {
76 lookups
77 .name(name() + ".BPredUnit.lookups")
78 .desc("Number of BP lookups")
79 ;
80
81 condPredicted
82 .name(name() + ".BPredUnit.condPredicted")
83 .desc("Number of conditional branches predicted")
84 ;
85
86 condIncorrect
87 .name(name() + ".BPredUnit.condIncorrect")
88 .desc("Number of conditional branches incorrect")
89 ;
90
91 BTBLookups
92 .name(name() + ".BPredUnit.BTBLookups")
93 .desc("Number of BTB lookups")
94 ;
95
96 BTBHits
97 .name(name() + ".BPredUnit.BTBHits")
98 .desc("Number of BTB hits")
99 ;
100
101 BTBCorrect
102 .name(name() + ".BPredUnit.BTBCorrect")
103 .desc("Number of correct BTB predictions (this stat may not "
104 "work properly.")
105 ;
106
107 usedRAS
108 .name(name() + ".BPredUnit.usedRAS")
109 .desc("Number of times the RAS was used to get a target.")
110 ;
111
112 RASIncorrect
113 .name(name() + ".BPredUnit.RASInCorrect")
114 .desc("Number of incorrect RAS predictions.")
115 ;
116 }
117
118 template <class Impl>
119 void
120 BPredUnit<Impl>::switchOut()
121 {
122 // Clear any state upon switch out.
123 for (int i = 0; i < Impl::MaxThreads; ++i) {
124 squash(0, i);
125 }
126 }
127
128 template <class Impl>
129 void
130 BPredUnit<Impl>::takeOverFrom()
131 {
132 // Can reset all predictor state, but it's not necessarily better
133 // than leaving it be.
134 /*
135 for (int i = 0; i < Impl::MaxThreads; ++i)
136 RAS[i].reset();
137
138 BP.reset();
139 BTB.reset();
140 */
141 }
142
143 template <class Impl>
144 bool
145 BPredUnit<Impl>::predict(DynInstPtr &inst, Addr &PC, unsigned tid)
146 {
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.
151
152 using TheISA::MachInst;
153
154 bool pred_taken = false;
155 Addr target;
156
157 ++lookups;
158
159 void *bp_history = NULL;
160
161 if (inst->isUncondCtrl()) {
162 DPRINTF(Fetch, "BranchPred: [tid:%i] Unconditional control.\n", tid);
163 pred_taken = true;
164 // Tell the BP there was an unconditional branch.
165 BPUncond(bp_history);
166 } else {
167 ++condPredicted;
168
169 pred_taken = BPLookup(PC, bp_history);
170
171 DPRINTF(Fetch, "BranchPred: [tid:%i]: Branch predictor predicted %i "
172 "for PC %#x\n",
173 tid, pred_taken, inst->readPC());
174 }
175
176 PredictorHistory predict_record(inst->seqNum, PC, pred_taken,
177 bp_history, tid);
178
179 // Now lookup in the BTB or RAS.
180 if (pred_taken) {
181 if (inst->isReturn()) {
182 ++usedRAS;
183
184 // If it's a function return call, then look up the address
185 // in the RAS.
186 target = RAS[tid].top();
187
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;
192
193 assert(predict_record.RASIndex < 16);
194
195 RAS[tid].pop();
196
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);
200 } else {
201 ++BTBLookups;
202
203 if (inst->isCall()) {
204 RAS[tid].push(PC + sizeof(MachInst));
205
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;
209
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));
213 }
214
215 if (BTB.valid(PC, tid)) {
216 ++BTBHits;
217
218 // If it's not a return, use the BTB to get the target addr.
219 target = BTB.lookup(PC, tid);
220
221 DPRINTF(Fetch, "BranchPred: [tid:%i]: Instruction %#x predicted"
222 " target is %#x.\n",
223 tid, inst->readPC(), target);
224
225 } else {
226 DPRINTF(Fetch, "BranchPred: [tid:%i]: BTB doesn't have a "
227 "valid entry.\n",tid);
228 pred_taken = false;
229 }
230
231 }
232 }
233
234 if (pred_taken) {
235 // Set the PC and the instruction's predicted target.
236 PC = target;
237 inst->setPredTarg(target);
238 } else {
239 PC = PC + sizeof(MachInst);
240 inst->setPredTarg(PC);
241 }
242
243 predHist[tid].push_front(predict_record);
244
245 DPRINTF(Fetch, "[tid:%i] predHist.size(): %i\n", tid, predHist[tid].size());
246
247 return pred_taken;
248 }
249
250 template <class Impl>
251 void
252 BPredUnit<Impl>::update(const InstSeqNum &done_sn, unsigned tid)
253 {
254 DPRINTF(Fetch, "BranchPred: [tid:%i]: Commiting branches until sequence"
255 "number %lli.\n", tid, done_sn);
256
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);
263
264 predHist[tid].pop_back();
265 }
266 }
267
268 template <class Impl>
269 void
270 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn, unsigned tid)
271 {
272 History &pred_hist = predHist[tid];
273
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,"
278 " target: %#x.\n",
279 tid,
280 pred_hist.front().RASIndex,
281 pred_hist.front().RASTarget);
282
283 RAS[tid].restore(pred_hist.front().RASIndex,
284 pred_hist.front().RASTarget);
285
286 } else if (pred_hist.front().wasCall) {
287 DPRINTF(Fetch, "BranchPred: [tid:%i]: Removing speculative entry "
288 "added to the RAS.\n",tid);
289
290 RAS[tid].pop();
291 }
292
293 // This call should delete the bpHistory.
294 BPSquash(pred_hist.front().bpHistory);
295
296 pred_hist.pop_front();
297 }
298
299 }
300
301 template <class Impl>
302 void
303 BPredUnit<Impl>::squash(const InstSeqNum &squashed_sn,
304 const Addr &corr_target,
305 const bool actually_taken,
306 unsigned tid)
307 {
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.
311
312 History &pred_hist = predHist[tid];
313
314 ++condIncorrect;
315
316 DPRINTF(Fetch, "BranchPred: [tid:%i]: Squashing from sequence number %i, "
317 "setting target to %#x.\n",
318 tid, squashed_sn, corr_target);
319
320 squash(squashed_sn, tid);
321
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
324 // fix up the entry.
325 if (!pred_hist.empty()) {
326 assert(pred_hist.front().seqNum == squashed_sn);
327 if (pred_hist.front().usedRAS) {
328 ++RASIncorrect;
329 }
330
331 BPUpdate(pred_hist.front().PC, actually_taken,
332 pred_hist.front().bpHistory);
333
334 BTB.update(pred_hist.front().PC, corr_target, tid);
335 pred_hist.pop_front();
336 }
337 }
338
339 template <class Impl>
340 void
341 BPredUnit<Impl>::BPUncond(void * &bp_history)
342 {
343 // Only the tournament predictor cares about unconditional branches.
344 if (predictor == Tournament) {
345 tournamentBP->uncondBr(bp_history);
346 }
347 }
348
349 template <class Impl>
350 void
351 BPredUnit<Impl>::BPSquash(void *bp_history)
352 {
353 if (predictor == Local) {
354 localBP->squash(bp_history);
355 } else if (predictor == Tournament) {
356 tournamentBP->squash(bp_history);
357 } else {
358 panic("Predictor type is unexpected value!");
359 }
360 }
361
362 template <class Impl>
363 bool
364 BPredUnit<Impl>::BPLookup(Addr &inst_PC, void * &bp_history)
365 {
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);
370 } else {
371 panic("Predictor type is unexpected value!");
372 }
373 }
374
375 template <class Impl>
376 void
377 BPredUnit<Impl>::BPUpdate(Addr &inst_PC, bool taken, void *bp_history)
378 {
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);
383 } else {
384 panic("Predictor type is unexpected value!");
385 }
386 }
387
388 template <class Impl>
389 void
390 BPredUnit<Impl>::dump()
391 {
392 typename History::iterator pred_hist_it;
393
394 for (int i = 0; i < Impl::MaxThreads; ++i) {
395 if (!predHist[i].empty()) {
396 pred_hist_it = predHist[i].begin();
397
398 cprintf("predHist[%i].size(): %i\n", i, predHist[i].size());
399
400 while (pred_hist_it != predHist[i].end()) {
401 cprintf("[sn:%lli], PC:%#x, tid:%i, predTaken:%i, "
402 "bpHistory:%#x\n",
403 (*pred_hist_it).seqNum, (*pred_hist_it).PC,
404 (*pred_hist_it).tid, (*pred_hist_it).predTaken,
405 (*pred_hist_it).bpHistory);
406 pred_hist_it++;
407 }
408
409 cprintf("\n");
410 }
411 }
412 }