Deleted Added
sdiff udiff text old ( 11783:f94c14fd6561 ) new ( 11793:ef606668d247 )
full compact
1/*
2 * Copyright (c) 2011, 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Kevin Lim
41 */
42
43#include "base/bitfield.hh"
44#include "base/intmath.hh"
45#include "cpu/pred/tournament.hh"
46
47TournamentBP::TournamentBP(const TournamentBPParams *params)
48 : BPredUnit(params),
49 localPredictorSize(params->localPredictorSize),
50 localCtrBits(params->localCtrBits),
51 localHistoryTableSize(params->localHistoryTableSize),
52 localHistoryBits(ceilLog2(params->localPredictorSize)),
53 globalPredictorSize(params->globalPredictorSize),
54 globalCtrBits(params->globalCtrBits),
55 globalHistory(params->numThreads, 0),
56 globalHistoryBits(
57 ceilLog2(params->globalPredictorSize) >
58 ceilLog2(params->choicePredictorSize) ?
59 ceilLog2(params->globalPredictorSize) :
60 ceilLog2(params->choicePredictorSize)),
61 choicePredictorSize(params->choicePredictorSize),
62 choiceCtrBits(params->choiceCtrBits)
63{
64 if (!isPowerOf2(localPredictorSize)) {
65 fatal("Invalid local predictor size!\n");
66 }
67
68 if (!isPowerOf2(globalPredictorSize)) {
69 fatal("Invalid global predictor size!\n");
70 }
71
72 //Set up the array of counters for the local predictor
73 localCtrs.resize(localPredictorSize);
74
75 for (int i = 0; i < localPredictorSize; ++i)
76 localCtrs[i].setBits(localCtrBits);
77
78 localPredictorMask = mask(localHistoryBits);
79
80 if (!isPowerOf2(localHistoryTableSize)) {
81 fatal("Invalid local history table size!\n");
82 }
83
84 //Setup the history table for the local table
85 localHistoryTable.resize(localHistoryTableSize);
86
87 for (int i = 0; i < localHistoryTableSize; ++i)
88 localHistoryTable[i] = 0;
89
90 //Setup the array of counters for the global predictor
91 globalCtrs.resize(globalPredictorSize);
92
93 for (int i = 0; i < globalPredictorSize; ++i)
94 globalCtrs[i].setBits(globalCtrBits);
95
96 // Set up the global history mask
97 // this is equivalent to mask(log2(globalPredictorSize)
98 globalHistoryMask = globalPredictorSize - 1;
99
100 if (!isPowerOf2(choicePredictorSize)) {
101 fatal("Invalid choice predictor size!\n");
102 }
103
104 // Set up choiceHistoryMask
105 // this is equivalent to mask(log2(choicePredictorSize)
106 choiceHistoryMask = choicePredictorSize - 1;
107
108 //Setup the array of counters for the choice predictor
109 choiceCtrs.resize(choicePredictorSize);
110
111 for (int i = 0; i < choicePredictorSize; ++i)
112 choiceCtrs[i].setBits(choiceCtrBits);
113
114 //Set up historyRegisterMask
115 historyRegisterMask = mask(globalHistoryBits);
116
117 //Check that predictors don't use more bits than they have available
118 if (globalHistoryMask > historyRegisterMask) {
119 fatal("Global predictor too large for global history bits!\n");
120 }
121 if (choiceHistoryMask > historyRegisterMask) {
122 fatal("Choice predictor too large for global history bits!\n");
123 }
124
125 if (globalHistoryMask < historyRegisterMask &&
126 choiceHistoryMask < historyRegisterMask) {
127 inform("More global history bits than required by predictors\n");
128 }
129
130 // Set thresholds for the three predictors' counters
131 // This is equivalent to (2^(Ctr))/2 - 1
132 localThreshold = (ULL(1) << (localCtrBits - 1)) - 1;
133 globalThreshold = (ULL(1) << (globalCtrBits - 1)) - 1;
134 choiceThreshold = (ULL(1) << (choiceCtrBits - 1)) - 1;
135}
136
137inline
138unsigned
139TournamentBP::calcLocHistIdx(Addr &branch_addr)
140{
141 // Get low order bits after removing instruction offset.
142 return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
143}
144
145inline
146void
147TournamentBP::updateGlobalHistTaken(ThreadID tid)
148{
149 globalHistory[tid] = (globalHistory[tid] << 1) | 1;
150 globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
151}
152
153inline
154void
155TournamentBP::updateGlobalHistNotTaken(ThreadID tid)
156{
157 globalHistory[tid] = (globalHistory[tid] << 1);
158 globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
159}
160
161inline
162void
163TournamentBP::updateLocalHistTaken(unsigned local_history_idx)
164{
165 localHistoryTable[local_history_idx] =
166 (localHistoryTable[local_history_idx] << 1) | 1;
167}
168
169inline
170void
171TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
172{
173 localHistoryTable[local_history_idx] =
174 (localHistoryTable[local_history_idx] << 1);
175}
176
177
178void
179TournamentBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
180{
181 unsigned local_history_idx = calcLocHistIdx(branch_addr);
182 //Update Global History to Not Taken (clear LSB)
183 globalHistory[tid] &= (historyRegisterMask & ~ULL(1));
184 //Update Local History to Not Taken
185 localHistoryTable[local_history_idx] =
186 localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1));
187}
188
189bool
190TournamentBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
191{
192 bool local_prediction;
193 unsigned local_history_idx;
194 unsigned local_predictor_idx;
195
196 bool global_prediction;
197 bool choice_prediction;
198
199 //Lookup in the local predictor to get its branch prediction
200 local_history_idx = calcLocHistIdx(branch_addr);
201 local_predictor_idx = localHistoryTable[local_history_idx]
202 & localPredictorMask;
203 local_prediction = localCtrs[local_predictor_idx].read() > localThreshold;
204
205 //Lookup in the global predictor to get its branch prediction
206 global_prediction = globalThreshold <
207 globalCtrs[globalHistory[tid] & globalHistoryMask].read();
208
209 //Lookup in the choice predictor to see which one to use
210 choice_prediction = choiceThreshold <
211 choiceCtrs[globalHistory[tid] & choiceHistoryMask].read();
212
213 // Create BPHistory and pass it back to be recorded.
214 BPHistory *history = new BPHistory;
215 history->globalHistory = globalHistory[tid];
216 history->localPredTaken = local_prediction;
217 history->globalPredTaken = global_prediction;
218 history->globalUsed = choice_prediction;
219 history->localHistoryIdx = local_history_idx;
220 history->localHistory = local_predictor_idx;
221 bp_history = (void *)history;
222
223 assert(local_history_idx < localHistoryTableSize);
224
225 // Speculative update of the global history and the
226 // selected local history.
227 if (choice_prediction) {
228 if (global_prediction) {
229 updateGlobalHistTaken(tid);
230 updateLocalHistTaken(local_history_idx);
231 return true;
232 } else {
233 updateGlobalHistNotTaken(tid);
234 updateLocalHistNotTaken(local_history_idx);
235 return false;
236 }
237 } else {
238 if (local_prediction) {
239 updateGlobalHistTaken(tid);
240 updateLocalHistTaken(local_history_idx);
241 return true;
242 } else {
243 updateGlobalHistNotTaken(tid);
244 updateLocalHistNotTaken(local_history_idx);
245 return false;
246 }
247 }
248}
249
250void
251TournamentBP::uncondBranch(ThreadID tid, Addr pc, void * &bp_history)
252{
253 // Create BPHistory and pass it back to be recorded.
254 BPHistory *history = new BPHistory;
255 history->globalHistory = globalHistory[tid];
256 history->localPredTaken = true;
257 history->globalPredTaken = true;
258 history->globalUsed = true;
259 history->localHistoryIdx = invalidPredictorIndex;
260 history->localHistory = invalidPredictorIndex;
261 bp_history = static_cast<void *>(history);
262
263 updateGlobalHistTaken(tid);
264}
265
266void
267TournamentBP::update(ThreadID tid, Addr branch_addr, bool taken,
268 void *bp_history, bool squashed)
269{
270 assert(bp_history);
271
272 BPHistory *history = static_cast<BPHistory *>(bp_history);
273
274 unsigned local_history_idx = calcLocHistIdx(branch_addr);
275
276 assert(local_history_idx < localHistoryTableSize);
277
278 // Unconditional branches do not use local history.
279 bool old_local_pred_valid = history->localHistory !=
280 invalidPredictorIndex;
281
282 // If this is a misprediction, restore the speculatively
283 // updated state (global history register and local history)
284 // and update again.
285 if (squashed) {
286 // Global history restore and update
287 globalHistory[tid] = (history->globalHistory << 1) | taken;
288 globalHistory[tid] &= historyRegisterMask;
289
290 // Local history restore and update.
291 if (old_local_pred_valid) {
292 localHistoryTable[local_history_idx] =
293 (history->localHistory << 1) | taken;
294 }
295
296 return;
297 }
298
299 unsigned old_local_pred_index = history->localHistory &
300 localPredictorMask;
301
302 assert(old_local_pred_index < localPredictorSize);
303
304 // Update the choice predictor to tell it which one was correct if
305 // there was a prediction.
306 if (history->localPredTaken != history->globalPredTaken &&
307 old_local_pred_valid)
308 {
309 // If the local prediction matches the actual outcome,
310 // decrement the counter. Otherwise increment the
311 // counter.
312 unsigned choice_predictor_idx =
313 history->globalHistory & choiceHistoryMask;
314 if (history->localPredTaken == taken) {
315 choiceCtrs[choice_predictor_idx].decrement();
316 } else if (history->globalPredTaken == taken) {
317 choiceCtrs[choice_predictor_idx].increment();
318 }
319 }
320
321 // Update the counters with the proper
322 // resolution of the branch. Histories are updated
323 // speculatively, restored upon squash() calls, and
324 // recomputed upon update(squash = true) calls,
325 // so they do not need to be updated.
326 unsigned global_predictor_idx =
327 history->globalHistory & globalHistoryMask;
328 if (taken) {
329 globalCtrs[global_predictor_idx].increment();
330 if (old_local_pred_valid) {
331 localCtrs[old_local_pred_index].increment();
332 }
333 } else {
334 globalCtrs[global_predictor_idx].decrement();
335 if (old_local_pred_valid) {
336 localCtrs[old_local_pred_index].decrement();
337 }
338 }
339
340 // We're done with this history, now delete it.
341 delete history;
342}
343
344void
345TournamentBP::squash(ThreadID tid, void *bp_history)
346{
347 BPHistory *history = static_cast<BPHistory *>(bp_history);
348
349 // Restore global history to state prior to this branch.
350 globalHistory[tid] = history->globalHistory;
351
352 // Restore local history
353 if (history->localHistoryIdx != invalidPredictorIndex) {
354 localHistoryTable[history->localHistoryIdx] = history->localHistory;
355 }
356
357 // Delete this BPHistory now that we're done with it.
358 delete history;
359}
360
361TournamentBP*
362TournamentBPParams::create()
363{
364 return new TournamentBP(this);
365}
366
367unsigned
368TournamentBP::getGHR(ThreadID tid, void *bp_history) const
369{
370 return static_cast<BPHistory *>(bp_history)->globalHistory;
371}
372
373#ifdef DEBUG
374int
375TournamentBP::BPHistory::newCount = 0;
376#endif