tournament.cc revision 13626
16691Stjones1@inf.ed.ac.uk/* 26691Stjones1@inf.ed.ac.uk * Copyright (c) 2011, 2014 ARM Limited 36691Stjones1@inf.ed.ac.uk * All rights reserved 46691Stjones1@inf.ed.ac.uk * 56691Stjones1@inf.ed.ac.uk * The license below extends only to copyright in the software and shall 66691Stjones1@inf.ed.ac.uk * not be construed as granting a license to any other intellectual 76691Stjones1@inf.ed.ac.uk * property including but not limited to intellectual property relating 86691Stjones1@inf.ed.ac.uk * to a hardware implementation of the functionality of the software 96691Stjones1@inf.ed.ac.uk * licensed hereunder. You may use the software subject to the license 106691Stjones1@inf.ed.ac.uk * terms below provided that you ensure that this notice is replicated 116691Stjones1@inf.ed.ac.uk * unmodified and in its entirety in all distributions of the software, 126691Stjones1@inf.ed.ac.uk * modified or unmodified, in source code or in binary form. 136691Stjones1@inf.ed.ac.uk * 146691Stjones1@inf.ed.ac.uk * Copyright (c) 2004-2006 The Regents of The University of Michigan 156691Stjones1@inf.ed.ac.uk * All rights reserved. 166691Stjones1@inf.ed.ac.uk * 176691Stjones1@inf.ed.ac.uk * Redistribution and use in source and binary forms, with or without 186691Stjones1@inf.ed.ac.uk * modification, are permitted provided that the following conditions are 196691Stjones1@inf.ed.ac.uk * met: redistributions of source code must retain the above copyright 206691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer; 216691Stjones1@inf.ed.ac.uk * redistributions in binary form must reproduce the above copyright 226691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer in the 236691Stjones1@inf.ed.ac.uk * documentation and/or other materials provided with the distribution; 246691Stjones1@inf.ed.ac.uk * neither the name of the copyright holders nor the names of its 256691Stjones1@inf.ed.ac.uk * contributors may be used to endorse or promote products derived from 266691Stjones1@inf.ed.ac.uk * this software without specific prior written permission. 276691Stjones1@inf.ed.ac.uk * 286691Stjones1@inf.ed.ac.uk * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 296691Stjones1@inf.ed.ac.uk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 306691Stjones1@inf.ed.ac.uk * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 316691Stjones1@inf.ed.ac.uk * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 326691Stjones1@inf.ed.ac.uk * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 336691Stjones1@inf.ed.ac.uk * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 346691Stjones1@inf.ed.ac.uk * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 356691Stjones1@inf.ed.ac.uk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 366691Stjones1@inf.ed.ac.uk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 376691Stjones1@inf.ed.ac.uk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 386691Stjones1@inf.ed.ac.uk * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 396691Stjones1@inf.ed.ac.uk * 406691Stjones1@inf.ed.ac.uk * Authors: Kevin Lim 416691Stjones1@inf.ed.ac.uk */ 426691Stjones1@inf.ed.ac.uk 436691Stjones1@inf.ed.ac.uk#include "cpu/pred/tournament.hh" 446691Stjones1@inf.ed.ac.uk 456691Stjones1@inf.ed.ac.uk#include "base/bitfield.hh" 466691Stjones1@inf.ed.ac.uk#include "base/intmath.hh" 476691Stjones1@inf.ed.ac.uk 486691Stjones1@inf.ed.ac.ukTournamentBP::TournamentBP(const TournamentBPParams *params) 496691Stjones1@inf.ed.ac.uk : BPredUnit(params), 506691Stjones1@inf.ed.ac.uk localPredictorSize(params->localPredictorSize), 516691Stjones1@inf.ed.ac.uk localCtrBits(params->localCtrBits), 526691Stjones1@inf.ed.ac.uk localHistoryTableSize(params->localHistoryTableSize), 536691Stjones1@inf.ed.ac.uk localHistoryBits(ceilLog2(params->localPredictorSize)), 546691Stjones1@inf.ed.ac.uk globalPredictorSize(params->globalPredictorSize), 556691Stjones1@inf.ed.ac.uk globalCtrBits(params->globalCtrBits), 566691Stjones1@inf.ed.ac.uk globalHistory(params->numThreads, 0), 576691Stjones1@inf.ed.ac.uk globalHistoryBits( 586691Stjones1@inf.ed.ac.uk ceilLog2(params->globalPredictorSize) > 596691Stjones1@inf.ed.ac.uk ceilLog2(params->choicePredictorSize) ? 606691Stjones1@inf.ed.ac.uk ceilLog2(params->globalPredictorSize) : 616691Stjones1@inf.ed.ac.uk ceilLog2(params->choicePredictorSize)), 626691Stjones1@inf.ed.ac.uk choicePredictorSize(params->choicePredictorSize), 636691Stjones1@inf.ed.ac.uk choiceCtrBits(params->choiceCtrBits) 646691Stjones1@inf.ed.ac.uk{ 656691Stjones1@inf.ed.ac.uk if (!isPowerOf2(localPredictorSize)) { 666691Stjones1@inf.ed.ac.uk fatal("Invalid local predictor size!\n"); 676691Stjones1@inf.ed.ac.uk } 686691Stjones1@inf.ed.ac.uk 696691Stjones1@inf.ed.ac.uk if (!isPowerOf2(globalPredictorSize)) { 706691Stjones1@inf.ed.ac.uk fatal("Invalid global predictor size!\n"); 716691Stjones1@inf.ed.ac.uk } 726691Stjones1@inf.ed.ac.uk 736691Stjones1@inf.ed.ac.uk //Set up the array of counters for the local predictor 746691Stjones1@inf.ed.ac.uk localCtrs.resize(localPredictorSize); 756691Stjones1@inf.ed.ac.uk 766691Stjones1@inf.ed.ac.uk for (int i = 0; i < localPredictorSize; ++i) 776691Stjones1@inf.ed.ac.uk localCtrs[i].setBits(localCtrBits); 786691Stjones1@inf.ed.ac.uk 796691Stjones1@inf.ed.ac.uk localPredictorMask = mask(localHistoryBits); 806691Stjones1@inf.ed.ac.uk 816691Stjones1@inf.ed.ac.uk if (!isPowerOf2(localHistoryTableSize)) { 826691Stjones1@inf.ed.ac.uk fatal("Invalid local history table size!\n"); 836691Stjones1@inf.ed.ac.uk } 846691Stjones1@inf.ed.ac.uk 856691Stjones1@inf.ed.ac.uk //Setup the history table for the local table 866691Stjones1@inf.ed.ac.uk localHistoryTable.resize(localHistoryTableSize); 876691Stjones1@inf.ed.ac.uk 886691Stjones1@inf.ed.ac.uk for (int i = 0; i < localHistoryTableSize; ++i) 896691Stjones1@inf.ed.ac.uk localHistoryTable[i] = 0; 906691Stjones1@inf.ed.ac.uk 916691Stjones1@inf.ed.ac.uk //Setup the array of counters for the global predictor 926691Stjones1@inf.ed.ac.uk globalCtrs.resize(globalPredictorSize); 936691Stjones1@inf.ed.ac.uk 946691Stjones1@inf.ed.ac.uk for (int i = 0; i < globalPredictorSize; ++i) 956691Stjones1@inf.ed.ac.uk globalCtrs[i].setBits(globalCtrBits); 96 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(ThreadID tid) 149{ 150 globalHistory[tid] = (globalHistory[tid] << 1) | 1; 151 globalHistory[tid] = globalHistory[tid] & historyRegisterMask; 152} 153 154inline 155void 156TournamentBP::updateGlobalHistNotTaken(ThreadID tid) 157{ 158 globalHistory[tid] = (globalHistory[tid] << 1); 159 globalHistory[tid] = globalHistory[tid] & 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(ThreadID tid, 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[tid] &= (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(ThreadID tid, 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 = globalThreshold < 208 globalCtrs[globalHistory[tid] & globalHistoryMask].read(); 209 210 //Lookup in the choice predictor to see which one to use 211 choice_prediction = choiceThreshold < 212 choiceCtrs[globalHistory[tid] & choiceHistoryMask].read(); 213 214 // Create BPHistory and pass it back to be recorded. 215 BPHistory *history = new BPHistory; 216 history->globalHistory = globalHistory[tid]; 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 // Speculative update of the global history and the 227 // selected local history. 228 if (choice_prediction) { 229 if (global_prediction) { 230 updateGlobalHistTaken(tid); 231 updateLocalHistTaken(local_history_idx); 232 return true; 233 } else { 234 updateGlobalHistNotTaken(tid); 235 updateLocalHistNotTaken(local_history_idx); 236 return false; 237 } 238 } else { 239 if (local_prediction) { 240 updateGlobalHistTaken(tid); 241 updateLocalHistTaken(local_history_idx); 242 return true; 243 } else { 244 updateGlobalHistNotTaken(tid); 245 updateLocalHistNotTaken(local_history_idx); 246 return false; 247 } 248 } 249} 250 251void 252TournamentBP::uncondBranch(ThreadID tid, 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[tid]; 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(tid); 265} 266 267void 268TournamentBP::update(ThreadID tid, Addr branch_addr, bool taken, 269 void *bp_history, bool squashed, 270 const StaticInstPtr & inst, Addr corrTarget) 271{ 272 assert(bp_history); 273 274 BPHistory *history = static_cast<BPHistory *>(bp_history); 275 276 unsigned local_history_idx = calcLocHistIdx(branch_addr); 277 278 assert(local_history_idx < localHistoryTableSize); 279 280 // Unconditional branches do not use local history. 281 bool old_local_pred_valid = history->localHistory != 282 invalidPredictorIndex; 283 284 // If this is a misprediction, restore the speculatively 285 // updated state (global history register and local history) 286 // and update again. 287 if (squashed) { 288 // Global history restore and update 289 globalHistory[tid] = (history->globalHistory << 1) | taken; 290 globalHistory[tid] &= historyRegisterMask; 291 292 // Local history restore and update. 293 if (old_local_pred_valid) { 294 localHistoryTable[local_history_idx] = 295 (history->localHistory << 1) | taken; 296 } 297 298 return; 299 } 300 301 unsigned old_local_pred_index = history->localHistory & 302 localPredictorMask; 303 304 assert(old_local_pred_index < localPredictorSize); 305 306 // Update the choice predictor to tell it which one was correct if 307 // there was a prediction. 308 if (history->localPredTaken != history->globalPredTaken && 309 old_local_pred_valid) 310 { 311 // If the local prediction matches the actual outcome, 312 // decrement the counter. Otherwise increment the 313 // counter. 314 unsigned choice_predictor_idx = 315 history->globalHistory & choiceHistoryMask; 316 if (history->localPredTaken == taken) { 317 choiceCtrs[choice_predictor_idx].decrement(); 318 } else if (history->globalPredTaken == taken) { 319 choiceCtrs[choice_predictor_idx].increment(); 320 } 321 } 322 323 // Update the counters with the proper 324 // resolution of the branch. Histories are updated 325 // speculatively, restored upon squash() calls, and 326 // recomputed upon update(squash = true) calls, 327 // so they do not need to be updated. 328 unsigned global_predictor_idx = 329 history->globalHistory & globalHistoryMask; 330 if (taken) { 331 globalCtrs[global_predictor_idx].increment(); 332 if (old_local_pred_valid) { 333 localCtrs[old_local_pred_index].increment(); 334 } 335 } else { 336 globalCtrs[global_predictor_idx].decrement(); 337 if (old_local_pred_valid) { 338 localCtrs[old_local_pred_index].decrement(); 339 } 340 } 341 342 // We're done with this history, now delete it. 343 delete history; 344} 345 346void 347TournamentBP::squash(ThreadID tid, void *bp_history) 348{ 349 BPHistory *history = static_cast<BPHistory *>(bp_history); 350 351 // Restore global history to state prior to this branch. 352 globalHistory[tid] = history->globalHistory; 353 354 // Restore local history 355 if (history->localHistoryIdx != invalidPredictorIndex) { 356 localHistoryTable[history->localHistoryIdx] = history->localHistory; 357 } 358 359 // Delete this BPHistory now that we're done with it. 360 delete history; 361} 362 363TournamentBP* 364TournamentBPParams::create() 365{ 366 return new TournamentBP(this); 367} 368 369unsigned 370TournamentBP::getGHR(ThreadID tid, void *bp_history) const 371{ 372 return static_cast<BPHistory *>(bp_history)->globalHistory; 373} 374 375#ifdef DEBUG 376int 377TournamentBP::BPHistory::newCount = 0; 378#endif 379