tournament.cc revision 10330
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 Params *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 globalHistoryBits( 56 ceilLog2(params->globalPredictorSize) > 57 ceilLog2(params->choicePredictorSize) ? 58 ceilLog2(params->globalPredictorSize) : 59 ceilLog2(params->choicePredictorSize)), 60 choicePredictorSize(params->choicePredictorSize), 61 choiceCtrBits(params->choiceCtrBits), 62 instShiftAmt(params->instShiftAmt) 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 //Clear the global history 97 globalHistory = 0; 98 // Set up the global history mask 99 // this is equivalent to mask(log2(globalPredictorSize) 100 globalHistoryMask = globalPredictorSize - 1; 101 102 if (!isPowerOf2(choicePredictorSize)) { 103 fatal("Invalid choice predictor size!\n"); 104 } 105 106 // Set up choiceHistoryMask 107 // this is equivalent to mask(log2(choicePredictorSize) 108 choiceHistoryMask = choicePredictorSize - 1; 109 110 //Setup the array of counters for the choice predictor 111 choiceCtrs.resize(choicePredictorSize); 112 113 for (int i = 0; i < choicePredictorSize; ++i) 114 choiceCtrs[i].setBits(choiceCtrBits); 115 116 //Set up historyRegisterMask 117 historyRegisterMask = mask(globalHistoryBits); 118 119 //Check that predictors don't use more bits than they have available 120 if (globalHistoryMask > historyRegisterMask) { 121 fatal("Global predictor too large for global history bits!\n"); 122 } 123 if (choiceHistoryMask > historyRegisterMask) { 124 fatal("Choice predictor too large for global history bits!\n"); 125 } 126 127 if (globalHistoryMask < historyRegisterMask && 128 choiceHistoryMask < historyRegisterMask) { 129 inform("More global history bits than required by predictors\n"); 130 } 131 132 // Set thresholds for the three predictors' counters 133 // This is equivalent to (2^(Ctr))/2 - 1 134 localThreshold = (ULL(1) << (localCtrBits - 1)) - 1; 135 globalThreshold = (ULL(1) << (globalCtrBits - 1)) - 1; 136 choiceThreshold = (ULL(1) << (choiceCtrBits - 1)) - 1; 137} 138 139inline 140unsigned 141TournamentBP::calcLocHistIdx(Addr &branch_addr) 142{ 143 // Get low order bits after removing instruction offset. 144 return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1); 145} 146 147inline 148void 149TournamentBP::updateGlobalHistTaken() 150{ 151 globalHistory = (globalHistory << 1) | 1; 152 globalHistory = globalHistory & historyRegisterMask; 153} 154 155inline 156void 157TournamentBP::updateGlobalHistNotTaken() 158{ 159 globalHistory = (globalHistory << 1); 160 globalHistory = globalHistory & historyRegisterMask; 161} 162 163inline 164void 165TournamentBP::updateLocalHistTaken(unsigned local_history_idx) 166{ 167 localHistoryTable[local_history_idx] = 168 (localHistoryTable[local_history_idx] << 1) | 1; 169} 170 171inline 172void 173TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx) 174{ 175 localHistoryTable[local_history_idx] = 176 (localHistoryTable[local_history_idx] << 1); 177} 178 179 180void 181TournamentBP::btbUpdate(Addr branch_addr, void * &bp_history) 182{ 183 unsigned local_history_idx = calcLocHistIdx(branch_addr); 184 //Update Global History to Not Taken (clear LSB) 185 globalHistory &= (historyRegisterMask & ~ULL(1)); 186 //Update Local History to Not Taken 187 localHistoryTable[local_history_idx] = 188 localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1)); 189} 190 191bool 192TournamentBP::lookup(Addr branch_addr, void * &bp_history) 193{ 194 bool local_prediction; 195 unsigned local_history_idx; 196 unsigned local_predictor_idx; 197 198 bool global_prediction; 199 bool choice_prediction; 200 201 //Lookup in the local predictor to get its branch prediction 202 local_history_idx = calcLocHistIdx(branch_addr); 203 local_predictor_idx = localHistoryTable[local_history_idx] 204 & localPredictorMask; 205 local_prediction = localCtrs[local_predictor_idx].read() > localThreshold; 206 207 //Lookup in the global predictor to get its branch prediction 208 global_prediction = 209 globalCtrs[globalHistory & globalHistoryMask].read() > globalThreshold; 210 211 //Lookup in the choice predictor to see which one to use 212 choice_prediction = 213 choiceCtrs[globalHistory & choiceHistoryMask].read() > choiceThreshold; 214 215 // Create BPHistory and pass it back to be recorded. 216 BPHistory *history = new BPHistory; 217 history->globalHistory = globalHistory; 218 history->localPredTaken = local_prediction; 219 history->globalPredTaken = global_prediction; 220 history->globalUsed = choice_prediction; 221 history->localHistory = local_predictor_idx; 222 bp_history = (void *)history; 223 224 assert(local_history_idx < localHistoryTableSize); 225 226 // Commented code is for doing speculative update of counters and 227 // all histories. 228 if (choice_prediction) { 229 if (global_prediction) { 230 updateGlobalHistTaken(); 231 updateLocalHistTaken(local_history_idx); 232 return true; 233 } else { 234 updateGlobalHistNotTaken(); 235 updateLocalHistNotTaken(local_history_idx); 236 return false; 237 } 238 } else { 239 if (local_prediction) { 240 updateGlobalHistTaken(); 241 updateLocalHistTaken(local_history_idx); 242 return true; 243 } else { 244 updateGlobalHistNotTaken(); 245 updateLocalHistNotTaken(local_history_idx); 246 return false; 247 } 248 } 249} 250 251void 252TournamentBP::uncondBranch(void * &bp_history) 253{ 254 // Create BPHistory and pass it back to be recorded. 255 BPHistory *history = new BPHistory; 256 history->globalHistory = globalHistory; 257 history->localPredTaken = true; 258 history->globalPredTaken = true; 259 history->globalUsed = true; 260 history->localHistory = invalidPredictorIndex; 261 bp_history = static_cast<void *>(history); 262 263 updateGlobalHistTaken(); 264} 265 266void 267TournamentBP::update(Addr branch_addr, bool taken, void *bp_history, 268 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 = (history->globalHistory << 1) | 1; 335 globalHistory = globalHistory & historyRegisterMask; 336 if (old_local_pred_valid) { 337 localHistoryTable[local_history_idx] = 338 (history->localHistory << 1) | 1; 339 } 340 } else { 341 globalHistory = (history->globalHistory << 1); 342 globalHistory = globalHistory & 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(void *bp_history) 362{ 363 BPHistory *history = static_cast<BPHistory *>(bp_history); 364 delete history; 365} 366 367void 368TournamentBP::squash(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 = history->globalHistory; 374 375 // Delete this BPHistory now that we're done with it. 376 delete history; 377} 378 379#ifdef DEBUG 380int 381TournamentBP::BPHistory::newCount = 0; 382#endif 383