tournament.cc revision 11098
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 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{ 63 if (!isPowerOf2(localPredictorSize)) { 64 fatal("Invalid local predictor size!\n"); 65 } 66 67 if (!isPowerOf2(globalPredictorSize)) { 68 fatal("Invalid global predictor size!\n"); 69 } 70 71 //Set up the array of counters for the local predictor 72 localCtrs.resize(localPredictorSize); 73 74 for (int i = 0; i < localPredictorSize; ++i) 75 localCtrs[i].setBits(localCtrBits); 76 77 localPredictorMask = mask(localHistoryBits); 78 79 if (!isPowerOf2(localHistoryTableSize)) { 80 fatal("Invalid local history table size!\n"); 81 } 82 83 //Setup the history table for the local table 84 localHistoryTable.resize(localHistoryTableSize); 85 86 for (int i = 0; i < localHistoryTableSize; ++i) 87 localHistoryTable[i] = 0; 88 89 //Setup the array of counters for the global predictor 90 globalCtrs.resize(globalPredictorSize); 91 92 for (int i = 0; i < globalPredictorSize; ++i) 93 globalCtrs[i].setBits(globalCtrBits); 94 95 //Clear the global history 96 globalHistory = 0; 97 // Set up the global history mask 98 // this is equivalent to mask(log2(globalPredictorSize) 99 globalHistoryMask = globalPredictorSize - 1; 100 101 if (!isPowerOf2(choicePredictorSize)) { 102 fatal("Invalid choice predictor size!\n"); 103 } 104 105 // Set up choiceHistoryMask 106 // this is equivalent to mask(log2(choicePredictorSize) 107 choiceHistoryMask = choicePredictorSize - 1; 108 109 //Setup the array of counters for the choice predictor 110 choiceCtrs.resize(choicePredictorSize); 111 112 for (int i = 0; i < choicePredictorSize; ++i) 113 choiceCtrs[i].setBits(choiceCtrBits); 114 115 //Set up historyRegisterMask 116 historyRegisterMask = mask(globalHistoryBits); 117 118 //Check that predictors don't use more bits than they have available 119 if (globalHistoryMask > historyRegisterMask) { 120 fatal("Global predictor too large for global history bits!\n"); 121 } 122 if (choiceHistoryMask > historyRegisterMask) { 123 fatal("Choice predictor too large for global history bits!\n"); 124 } 125 126 if (globalHistoryMask < historyRegisterMask && 127 choiceHistoryMask < historyRegisterMask) { 128 inform("More global history bits than required by predictors\n"); 129 } 130 131 // Set thresholds for the three predictors' counters 132 // This is equivalent to (2^(Ctr))/2 - 1 133 localThreshold = (ULL(1) << (localCtrBits - 1)) - 1; 134 globalThreshold = (ULL(1) << (globalCtrBits - 1)) - 1; 135 choiceThreshold = (ULL(1) << (choiceCtrBits - 1)) - 1; 136} 137 138inline 139unsigned 140TournamentBP::calcLocHistIdx(Addr &branch_addr) 141{ 142 // Get low order bits after removing instruction offset. 143 return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1); 144} 145 146inline 147void 148TournamentBP::updateGlobalHistTaken() 149{ 150 globalHistory = (globalHistory << 1) | 1; 151 globalHistory = globalHistory & historyRegisterMask; 152} 153 154inline 155void 156TournamentBP::updateGlobalHistNotTaken() 157{ 158 globalHistory = (globalHistory << 1); 159 globalHistory = globalHistory & historyRegisterMask; 160} 161 162inline 163void 164TournamentBP::updateLocalHistTaken(unsigned local_history_idx) 165{ 166 localHistoryTable[local_history_idx] = 167 (localHistoryTable[local_history_idx] << 1) | 1; 168} 169 170inline 171void 172TournamentBP::updateLocalHistNotTaken(unsigned local_history_idx) 173{ 174 localHistoryTable[local_history_idx] = 175 (localHistoryTable[local_history_idx] << 1); 176} 177 178 179void 180TournamentBP::btbUpdate(Addr branch_addr, void * &bp_history) 181{ 182 unsigned local_history_idx = calcLocHistIdx(branch_addr); 183 //Update Global History to Not Taken (clear LSB) 184 globalHistory &= (historyRegisterMask & ~ULL(1)); 185 //Update Local History to Not Taken 186 localHistoryTable[local_history_idx] = 187 localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1)); 188} 189 190bool 191TournamentBP::lookup(Addr branch_addr, void * &bp_history) 192{ 193 bool local_prediction; 194 unsigned local_history_idx; 195 unsigned local_predictor_idx; 196 197 bool global_prediction; 198 bool choice_prediction; 199 200 //Lookup in the local predictor to get its branch prediction 201 local_history_idx = calcLocHistIdx(branch_addr); 202 local_predictor_idx = localHistoryTable[local_history_idx] 203 & localPredictorMask; 204 local_prediction = localCtrs[local_predictor_idx].read() > localThreshold; 205 206 //Lookup in the global predictor to get its branch prediction 207 global_prediction = 208 globalCtrs[globalHistory & globalHistoryMask].read() > globalThreshold; 209 210 //Lookup in the choice predictor to see which one to use 211 choice_prediction = 212 choiceCtrs[globalHistory & choiceHistoryMask].read() > choiceThreshold; 213 214 // Create BPHistory and pass it back to be recorded. 215 BPHistory *history = new BPHistory; 216 history->globalHistory = globalHistory; 217 history->localPredTaken = local_prediction; 218 history->globalPredTaken = global_prediction; 219 history->globalUsed = choice_prediction; 220 history->localHistoryIdx = local_history_idx; 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(Addr pc, 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->localHistoryIdx = invalidPredictorIndex; 261 history->localHistory = invalidPredictorIndex; 262 bp_history = static_cast<void *>(history); 263 264 updateGlobalHistTaken(); 265} 266 267void 268TournamentBP::update(Addr branch_addr, bool taken, void *bp_history, 269 bool squashed) 270{ 271 unsigned local_history_idx; 272 unsigned local_predictor_idx M5_VAR_USED; 273 unsigned local_predictor_hist; 274 275 // Get the local predictor's current prediction 276 local_history_idx = calcLocHistIdx(branch_addr); 277 local_predictor_hist = localHistoryTable[local_history_idx]; 278 local_predictor_idx = local_predictor_hist & localPredictorMask; 279 280 if (bp_history) { 281 BPHistory *history = static_cast<BPHistory *>(bp_history); 282 // Update may also be called if the Branch target is incorrect even if 283 // the prediction is correct. In that case do not update the counters. 284 bool historyPred = false; 285 unsigned old_local_pred_index = history->localHistory & 286 localPredictorMask; 287 288 bool old_local_pred_valid = history->localHistory != 289 invalidPredictorIndex; 290 291 assert(old_local_pred_index < localPredictorSize); 292 293 if (history->globalUsed) { 294 historyPred = history->globalPredTaken; 295 } else { 296 historyPred = history->localPredTaken; 297 } 298 if (historyPred != taken || !squashed) { 299 // Update the choice predictor to tell it which one was correct if 300 // there was a prediction. 301 if (history->localPredTaken != history->globalPredTaken) { 302 // If the local prediction matches the actual outcome, 303 // decerement the counter. Otherwise increment the 304 // counter. 305 unsigned choice_predictor_idx = 306 history->globalHistory & choiceHistoryMask; 307 if (history->localPredTaken == taken) { 308 choiceCtrs[choice_predictor_idx].decrement(); 309 } else if (history->globalPredTaken == taken) { 310 choiceCtrs[choice_predictor_idx].increment(); 311 } 312 313 } 314 315 // Update the counters and local history with the proper 316 // resolution of the branch. Global history is updated 317 // speculatively and restored upon squash() calls, so it does not 318 // need to be updated. 319 unsigned global_predictor_idx = 320 history->globalHistory & globalHistoryMask; 321 if (taken) { 322 globalCtrs[global_predictor_idx].increment(); 323 if (old_local_pred_valid) { 324 localCtrs[old_local_pred_index].increment(); 325 } 326 } else { 327 globalCtrs[global_predictor_idx].decrement(); 328 if (old_local_pred_valid) { 329 localCtrs[old_local_pred_index].decrement(); 330 } 331 } 332 } 333 if (squashed) { 334 if (taken) { 335 globalHistory = (history->globalHistory << 1) | 1; 336 globalHistory = globalHistory & historyRegisterMask; 337 if (old_local_pred_valid) { 338 localHistoryTable[local_history_idx] = 339 (history->localHistory << 1) | 1; 340 } 341 } else { 342 globalHistory = (history->globalHistory << 1); 343 globalHistory = globalHistory & historyRegisterMask; 344 if (old_local_pred_valid) { 345 localHistoryTable[local_history_idx] = 346 history->localHistory << 1; 347 } 348 } 349 350 } else { 351 // We're done with this history, now delete it. 352 delete history; 353 } 354 } 355 356 assert(local_history_idx < localHistoryTableSize); 357 358 359} 360 361void 362TournamentBP::retireSquashed(void *bp_history) 363{ 364 BPHistory *history = static_cast<BPHistory *>(bp_history); 365 delete history; 366} 367 368void 369TournamentBP::squash(void *bp_history) 370{ 371 BPHistory *history = static_cast<BPHistory *>(bp_history); 372 373 // Restore global history to state prior to this branch. 374 globalHistory = history->globalHistory; 375 376 // Restore local history 377 if (history->localHistoryIdx != invalidPredictorIndex) { 378 localHistoryTable[history->localHistoryIdx] = history->localHistory; 379 } 380 381 // Delete this BPHistory now that we're done with it. 382 delete history; 383} 384 385TournamentBP* 386TournamentBPParams::create() 387{ 388 return new TournamentBP(this); 389} 390 391#ifdef DEBUG 392int 393TournamentBP::BPHistory::newCount = 0; 394#endif 395