tournament.cc revision 11434
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 // Commented code is for doing speculative update of counters and 226 // all histories. 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 unsigned local_history_idx; 271 unsigned local_predictor_idx M5_VAR_USED; 272 unsigned local_predictor_hist; 273 274 // Get the local predictor's current prediction 275 local_history_idx = calcLocHistIdx(branch_addr); 276 local_predictor_hist = localHistoryTable[local_history_idx]; 277 local_predictor_idx = local_predictor_hist & localPredictorMask; 278 279 if (bp_history) { 280 BPHistory *history = static_cast<BPHistory *>(bp_history); 281 // Update may also be called if the Branch target is incorrect even if 282 // the prediction is correct. In that case do not update the counters. 283 bool historyPred = false; 284 unsigned old_local_pred_index = history->localHistory & 285 localPredictorMask; 286 287 bool old_local_pred_valid = history->localHistory != 288 invalidPredictorIndex; 289 290 assert(old_local_pred_index < localPredictorSize); 291 292 if (history->globalUsed) { 293 historyPred = history->globalPredTaken; 294 } else { 295 historyPred = history->localPredTaken; 296 } 297 if (historyPred != taken || !squashed) { 298 // Update the choice predictor to tell it which one was correct if 299 // there was a prediction. 300 if (history->localPredTaken != history->globalPredTaken) { 301 // If the local prediction matches the actual outcome, 302 // decerement the counter. Otherwise increment the 303 // counter. 304 unsigned choice_predictor_idx = 305 history->globalHistory & choiceHistoryMask; 306 if (history->localPredTaken == taken) { 307 choiceCtrs[choice_predictor_idx].decrement(); 308 } else if (history->globalPredTaken == taken) { 309 choiceCtrs[choice_predictor_idx].increment(); 310 } 311 312 } 313 314 // Update the counters and local history with the proper 315 // resolution of the branch. Global history is updated 316 // speculatively and restored upon squash() calls, so it does not 317 // need to be updated. 318 unsigned global_predictor_idx = 319 history->globalHistory & globalHistoryMask; 320 if (taken) { 321 globalCtrs[global_predictor_idx].increment(); 322 if (old_local_pred_valid) { 323 localCtrs[old_local_pred_index].increment(); 324 } 325 } else { 326 globalCtrs[global_predictor_idx].decrement(); 327 if (old_local_pred_valid) { 328 localCtrs[old_local_pred_index].decrement(); 329 } 330 } 331 } 332 if (squashed) { 333 if (taken) { 334 globalHistory[tid] = (history->globalHistory << 1) | 1; 335 globalHistory[tid] = globalHistory[tid] & historyRegisterMask; 336 if (old_local_pred_valid) { 337 localHistoryTable[local_history_idx] = 338 (history->localHistory << 1) | 1; 339 } 340 } else { 341 globalHistory[tid] = (history->globalHistory << 1); 342 globalHistory[tid] = globalHistory[tid] & historyRegisterMask; 343 if (old_local_pred_valid) { 344 localHistoryTable[local_history_idx] = 345 history->localHistory << 1; 346 } 347 } 348 349 } else { 350 // We're done with this history, now delete it. 351 delete history; 352 } 353 } 354 355 assert(local_history_idx < localHistoryTableSize); 356 357 358} 359 360void 361TournamentBP::retireSquashed(ThreadID tid, void *bp_history) 362{ 363 BPHistory *history = static_cast<BPHistory *>(bp_history); 364 delete history; 365} 366 367void 368TournamentBP::squash(ThreadID tid, void *bp_history) 369{ 370 BPHistory *history = static_cast<BPHistory *>(bp_history); 371 372 // Restore global history to state prior to this branch. 373 globalHistory[tid] = history->globalHistory; 374 375 // Restore local history 376 if (history->localHistoryIdx != invalidPredictorIndex) { 377 localHistoryTable[history->localHistoryIdx] = history->localHistory; 378 } 379 380 // Delete this BPHistory now that we're done with it. 381 delete history; 382} 383 384TournamentBP* 385TournamentBPParams::create() 386{ 387 return new TournamentBP(this); 388} 389 390unsigned 391TournamentBP::getGHR(ThreadID tid, void *bp_history) const 392{ 393 return static_cast<BPHistory *>(bp_history)->globalHistory; 394} 395 396#ifdef DEBUG 397int 398TournamentBP::BPHistory::newCount = 0; 399#endif 400