1/* 2 * Copyright (c) 2014 The University of Wisconsin 3 * 4 * Copyright (c) 2006 INRIA (Institut National de Recherche en 5 * Informatique et en Automatique / French National Research Institute 6 * for Computer Science and Applied Mathematics) 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are 12 * met: redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer; 14 * redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution; 17 * neither the name of the copyright holders nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Authors: Vignyan Reddy, Dibakar Gope and Arthur Perais, 34 * from André Seznec's code. 35 */ 36 37/* @file 38 * Implementation of a L-TAGE branch predictor 39 */ 40 41#include "cpu/pred/ltage.hh" 42 43#include "base/intmath.hh" 44#include "base/logging.hh" 45#include "base/random.hh" 46#include "base/trace.hh" 47#include "debug/Fetch.hh" 48#include "debug/LTage.hh" 49 50LTAGE::LTAGE(const LTAGEParams *params) 51 : BPredUnit(params), 52 logSizeBiMP(params->logSizeBiMP),
| 1/* 2 * Copyright (c) 2014 The University of Wisconsin 3 * 4 * Copyright (c) 2006 INRIA (Institut National de Recherche en 5 * Informatique et en Automatique / French National Research Institute 6 * for Computer Science and Applied Mathematics) 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are 12 * met: redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer; 14 * redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution; 17 * neither the name of the copyright holders nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Authors: Vignyan Reddy, Dibakar Gope and Arthur Perais, 34 * from André Seznec's code. 35 */ 36 37/* @file 38 * Implementation of a L-TAGE branch predictor 39 */ 40 41#include "cpu/pred/ltage.hh" 42 43#include "base/intmath.hh" 44#include "base/logging.hh" 45#include "base/random.hh" 46#include "base/trace.hh" 47#include "debug/Fetch.hh" 48#include "debug/LTage.hh" 49 50LTAGE::LTAGE(const LTAGEParams *params) 51 : BPredUnit(params), 52 logSizeBiMP(params->logSizeBiMP),
|
235} 236 237 238//loop prediction: only used if high confidence 239bool 240LTAGE::getLoop(Addr pc, BranchInfo* bi) const 241{ 242 bi->loopHit = -1; 243 bi->loopPredValid = false; 244 bi->loopIndex = lindex(pc); 245 bi->loopTag = ((pc) >> (instShiftAmt + logSizeLoopPred - 2)); 246 247 for (int i = 0; i < 4; i++) { 248 if (ltable[bi->loopIndex + i].tag == bi->loopTag) { 249 bi->loopHit = i; 250 bi->loopPredValid = (ltable[bi->loopIndex + i].confidence >= 3); 251 bi->currentIter = ltable[bi->loopIndex + i].currentIterSpec; 252 if (ltable[bi->loopIndex + i].currentIterSpec + 1 == 253 ltable[bi->loopIndex + i].numIter) { 254 return !(ltable[bi->loopIndex + i].dir); 255 }else { 256 return (ltable[bi->loopIndex + i].dir); 257 } 258 } 259 } 260 return false; 261} 262 263void 264LTAGE::specLoopUpdate(Addr pc, bool taken, BranchInfo* bi) 265{ 266 if (bi->loopHit>=0) { 267 int index = lindex(pc); 268 if (taken != ltable[index].dir) { 269 ltable[index].currentIterSpec = 0; 270 } else { 271 ltable[index].currentIterSpec++; 272 } 273 } 274} 275 276void 277LTAGE::loopUpdate(Addr pc, bool taken, BranchInfo* bi) 278{ 279 int idx = bi->loopIndex + bi->loopHit; 280 if (bi->loopHit >= 0) { 281 //already a hit 282 if (bi->loopPredValid) { 283 if (taken != bi->loopPred) { 284 // free the entry 285 ltable[idx].numIter = 0; 286 ltable[idx].age = 0; 287 ltable[idx].confidence = 0; 288 ltable[idx].currentIter = 0; 289 return; 290 } else if (bi->loopPred != bi->tagePred) { 291 DPRINTF(LTage, "Loop Prediction success:%lx\n",pc); 292 if (ltable[idx].age < 7) 293 ltable[idx].age++; 294 } 295 } 296 297 ltable[idx].currentIter++; 298 if (ltable[idx].currentIter > ltable[idx].numIter) { 299 ltable[idx].confidence = 0; 300 if (ltable[idx].numIter != 0) { 301 // free the entry 302 ltable[idx].numIter = 0; 303 ltable[idx].age = 0; 304 ltable[idx].confidence = 0; 305 } 306 } 307 308 if (taken != ltable[idx].dir) { 309 if (ltable[idx].currentIter == ltable[idx].numIter) { 310 DPRINTF(LTage, "Loop End predicted successfully:%lx\n", pc); 311 312 if (ltable[idx].confidence < 7) { 313 ltable[idx].confidence++; 314 } 315 //just do not predict when the loop count is 1 or 2 316 if (ltable[idx].numIter < 3) { 317 // free the entry 318 ltable[idx].dir = taken; 319 ltable[idx].numIter = 0; 320 ltable[idx].age = 0; 321 ltable[idx].confidence = 0; 322 } 323 } else { 324 DPRINTF(LTage, "Loop End predicted incorrectly:%lx\n", pc); 325 if (ltable[idx].numIter == 0) { 326 // first complete nest; 327 ltable[idx].confidence = 0; 328 ltable[idx].numIter = ltable[idx].currentIter; 329 } else { 330 //not the same number of iterations as last time: free the 331 //entry 332 ltable[idx].numIter = 0; 333 ltable[idx].age = 0; 334 ltable[idx].confidence = 0; 335 } 336 } 337 ltable[idx].currentIter = 0; 338 } 339 340 } else if (taken) { 341 //try to allocate an entry on taken branch 342 int nrand = random_mt.random<int>(); 343 for (int i = 0; i < 4; i++) { 344 int loop_hit = (nrand + i) & 3; 345 idx = bi->loopIndex + loop_hit; 346 if (ltable[idx].age == 0) { 347 DPRINTF(LTage, "Allocating loop pred entry for branch %lx\n", 348 pc); 349 ltable[idx].dir = !taken; 350 ltable[idx].tag = bi->loopTag; 351 ltable[idx].numIter = 0; 352 ltable[idx].age = 7; 353 ltable[idx].confidence = 0; 354 ltable[idx].currentIter = 1; 355 break; 356 357 } 358 else 359 ltable[idx].age--; 360 } 361 } 362 363} 364 365// shifting the global history: we manage the history in a big table in order 366// to reduce simulation time 367void 368LTAGE::updateGHist(uint8_t * &h, bool dir, uint8_t * tab, int &pt) 369{ 370 if (pt == 0) { 371 DPRINTF(LTage, "Rolling over the histories\n"); 372 // Copy beginning of globalHistoryBuffer to end, such that 373 // the last maxHist outcomes are still reachable 374 // through pt[0 .. maxHist - 1]. 375 for (int i = 0; i < maxHist; i++) 376 tab[histBufferSize - maxHist + i] = tab[i]; 377 pt = histBufferSize - maxHist; 378 h = &tab[pt]; 379 } 380 pt--; 381 h--; 382 h[0] = (dir) ? 1 : 0; 383} 384 385// Get GHR for hashing indirect predictor 386// Build history backwards from pointer in 387// bp_history. 388unsigned 389LTAGE::getGHR(ThreadID tid, void *bp_history) const 390{ 391 BranchInfo* bi = static_cast<BranchInfo*>(bp_history); 392 unsigned val = 0; 393 for (unsigned i = 0; i < 32; i++) { 394 // Make sure we don't go out of bounds 395 int gh_offset = bi->ptGhist + i; 396 assert(&(threadHistory[tid].globalHistory[gh_offset]) < 397 threadHistory[tid].globalHistory + histBufferSize); 398 val |= ((threadHistory[tid].globalHistory[gh_offset] & 0x1) << i); 399 } 400 401 return val; 402} 403 404//prediction 405bool 406LTAGE::predict(ThreadID tid, Addr branch_pc, bool cond_branch, void* &b) 407{ 408 BranchInfo *bi = new BranchInfo(nHistoryTables+1); 409 b = (void*)(bi); 410 Addr pc = branch_pc; 411 bool pred_taken = true; 412 bi->loopHit = -1; 413 414 if (cond_branch) { 415 // TAGE prediction 416 417 // computes the table addresses and the partial tags 418 for (int i = 1; i <= nHistoryTables; i++) { 419 tableIndices[i] = gindex(tid, pc, i); 420 bi->tableIndices[i] = tableIndices[i]; 421 tableTags[i] = gtag(tid, pc, i); 422 bi->tableTags[i] = tableTags[i]; 423 } 424 425 bi->bimodalIndex = bindex(pc); 426 427 bi->hitBank = 0; 428 bi->altBank = 0; 429 //Look for the bank with longest matching history 430 for (int i = nHistoryTables; i > 0; i--) { 431 if (gtable[i][tableIndices[i]].tag == tableTags[i]) { 432 bi->hitBank = i; 433 bi->hitBankIndex = tableIndices[bi->hitBank]; 434 break; 435 } 436 } 437 //Look for the alternate bank 438 for (int i = bi->hitBank - 1; i > 0; i--) { 439 if (gtable[i][tableIndices[i]].tag == tableTags[i]) { 440 bi->altBank = i; 441 bi->altBankIndex = tableIndices[bi->altBank]; 442 break; 443 } 444 } 445 //computes the prediction and the alternate prediction 446 if (bi->hitBank > 0) { 447 if (bi->altBank > 0) { 448 bi->altTaken = 449 gtable[bi->altBank][tableIndices[bi->altBank]].ctr >= 0; 450 }else { 451 bi->altTaken = getBimodePred(pc, bi); 452 } 453 454 bi->longestMatchPred = 455 gtable[bi->hitBank][tableIndices[bi->hitBank]].ctr >= 0; 456 bi->pseudoNewAlloc = 457 abs(2 * gtable[bi->hitBank][bi->hitBankIndex].ctr + 1) <= 1; 458 459 //if the entry is recognized as a newly allocated entry and 460 //useAltPredForNewlyAllocated is positive use the alternate 461 //prediction 462 if ((useAltPredForNewlyAllocated < 0) 463 || abs(2 * 464 gtable[bi->hitBank][tableIndices[bi->hitBank]].ctr + 1) > 1) 465 bi->tagePred = bi->longestMatchPred; 466 else 467 bi->tagePred = bi->altTaken; 468 } else { 469 bi->altTaken = getBimodePred(pc, bi); 470 bi->tagePred = bi->altTaken; 471 bi->longestMatchPred = bi->altTaken; 472 } 473 //end TAGE prediction 474 475 bi->loopPred = getLoop(pc, bi); // loop prediction 476 477 pred_taken = (((loopUseCounter >= 0) && bi->loopPredValid)) ? 478 (bi->loopPred): (bi->tagePred); 479 DPRINTF(LTage, "Predict for %lx: taken?:%d, loopTaken?:%d, " 480 "loopValid?:%d, loopUseCounter:%d, tagePred:%d, altPred:%d\n", 481 branch_pc, pred_taken, bi->loopPred, bi->loopPredValid, 482 loopUseCounter, bi->tagePred, bi->altTaken); 483 } 484 bi->branchPC = branch_pc; 485 bi->condBranch = cond_branch; 486 specLoopUpdate(branch_pc, pred_taken, bi); 487 return pred_taken; 488} 489 490// PREDICTOR UPDATE 491void 492LTAGE::update(ThreadID tid, Addr branch_pc, bool taken, void* bp_history, 493 bool squashed) 494{ 495 assert(bp_history); 496 497 BranchInfo *bi = static_cast<BranchInfo*>(bp_history); 498 499 if (squashed) { 500 // This restores the global history, then update it 501 // and recomputes the folded histories. 502 squash(tid, taken, bp_history); 503 return; 504 } 505 506 int nrand = random_mt.random<int>(0,3); 507 Addr pc = branch_pc; 508 if (bi->condBranch) { 509 DPRINTF(LTage, "Updating tables for branch:%lx; taken?:%d\n", 510 branch_pc, taken); 511 // first update the loop predictor 512 loopUpdate(pc, taken, bi); 513 514 if (bi->loopPredValid) { 515 if (bi->tagePred != bi->loopPred) { 516 ctrUpdate(loopUseCounter, (bi->loopPred== taken), 7); 517 } 518 } 519 520 // TAGE UPDATE 521 // try to allocate a new entries only if prediction was wrong 522 bool longest_match_pred = false; 523 bool alloc = (bi->tagePred != taken) && (bi->hitBank < nHistoryTables); 524 if (bi->hitBank > 0) { 525 // Manage the selection between longest matching and alternate 526 // matching for "pseudo"-newly allocated longest matching entry 527 longest_match_pred = bi->longestMatchPred; 528 bool PseudoNewAlloc = bi->pseudoNewAlloc; 529 // an entry is considered as newly allocated if its prediction 530 // counter is weak 531 if (PseudoNewAlloc) { 532 if (longest_match_pred == taken) { 533 alloc = false; 534 } 535 // if it was delivering the correct prediction, no need to 536 // allocate new entry even if the overall prediction was false 537 if (longest_match_pred != bi->altTaken) { 538 ctrUpdate(useAltPredForNewlyAllocated, 539 bi->altTaken == taken, 4); 540 } 541 } 542 } 543 544 if (alloc) { 545 // is there some "unuseful" entry to allocate 546 int8_t min = 1; 547 for (int i = nHistoryTables; i > bi->hitBank; i--) { 548 if (gtable[i][bi->tableIndices[i]].u < min) { 549 min = gtable[i][bi->tableIndices[i]].u; 550 } 551 } 552 553 // we allocate an entry with a longer history 554 // to avoid ping-pong, we do not choose systematically the next 555 // entry, but among the 3 next entries 556 int Y = nrand & 557 ((ULL(1) << (nHistoryTables - bi->hitBank - 1)) - 1); 558 int X = bi->hitBank + 1; 559 if (Y & 1) { 560 X++; 561 if (Y & 2) 562 X++; 563 } 564 // No entry available, forces one to be available 565 if (min > 0) { 566 gtable[X][bi->tableIndices[X]].u = 0; 567 } 568 569 570 //Allocate only one entry 571 for (int i = X; i <= nHistoryTables; i++) { 572 if ((gtable[i][bi->tableIndices[i]].u == 0)) { 573 gtable[i][bi->tableIndices[i]].tag = bi->tableTags[i]; 574 gtable[i][bi->tableIndices[i]].ctr = (taken) ? 0 : -1; 575 gtable[i][bi->tableIndices[i]].u = 0; //? 576 } 577 } 578 } 579 //periodic reset of u: reset is not complete but bit by bit 580 tCounter++; 581 if ((tCounter & ((ULL(1) << logTick) - 1)) == 0) { 582 // reset least significant bit 583 // most significant bit becomes least significant bit 584 for (int i = 1; i <= nHistoryTables; i++) { 585 for (int j = 0; j < (ULL(1) << tagTableSizes[i]); j++) { 586 gtable[i][j].u = gtable[i][j].u >> 1; 587 } 588 } 589 } 590 591 if (bi->hitBank > 0) { 592 DPRINTF(LTage, "Updating tag table entry (%d,%d) for branch %lx\n", 593 bi->hitBank, bi->hitBankIndex, branch_pc); 594 ctrUpdate(gtable[bi->hitBank][bi->hitBankIndex].ctr, taken, 595 tagTableCounterBits); 596 // if the provider entry is not certified to be useful also update 597 // the alternate prediction 598 if (gtable[bi->hitBank][bi->hitBankIndex].u == 0) { 599 if (bi->altBank > 0) { 600 ctrUpdate(gtable[bi->altBank][bi->altBankIndex].ctr, taken, 601 tagTableCounterBits); 602 DPRINTF(LTage, "Updating tag table entry (%d,%d) for" 603 " branch %lx\n", bi->hitBank, bi->hitBankIndex, 604 branch_pc); 605 } 606 if (bi->altBank == 0) { 607 baseUpdate(pc, taken, bi); 608 } 609 } 610 611 // update the u counter 612 if (longest_match_pred != bi->altTaken) { 613 if (longest_match_pred == taken) { 614 if (gtable[bi->hitBank][bi->hitBankIndex].u < 1) { 615 gtable[bi->hitBank][bi->hitBankIndex].u++; 616 } 617 } 618 } 619 } else { 620 baseUpdate(pc, taken, bi); 621 } 622 623 //END PREDICTOR UPDATE 624 } 625 if (!squashed) { 626 delete bi; 627 } 628} 629 630void 631LTAGE::updateHistories(ThreadID tid, Addr branch_pc, bool taken, void* b) 632{ 633 BranchInfo* bi = (BranchInfo*)(b); 634 ThreadHistory& tHist = threadHistory[tid]; 635 // UPDATE HISTORIES 636 bool pathbit = ((branch_pc >> instShiftAmt) & 1); 637 //on a squash, return pointers to this and recompute indices. 638 //update user history 639 updateGHist(tHist.gHist, taken, tHist.globalHistory, tHist.ptGhist); 640 tHist.pathHist = (tHist.pathHist << 1) + pathbit; 641 tHist.pathHist = (tHist.pathHist & ((ULL(1) << 16) - 1)); 642 643 bi->ptGhist = tHist.ptGhist; 644 bi->pathHist = tHist.pathHist; 645 //prepare next index and tag computations for user branchs 646 for (int i = 1; i <= nHistoryTables; i++) 647 { 648 bi->ci[i] = tHist.computeIndices[i].comp; 649 bi->ct0[i] = tHist.computeTags[0][i].comp; 650 bi->ct1[i] = tHist.computeTags[1][i].comp; 651 tHist.computeIndices[i].update(tHist.gHist); 652 tHist.computeTags[0][i].update(tHist.gHist); 653 tHist.computeTags[1][i].update(tHist.gHist); 654 } 655 DPRINTF(LTage, "Updating global histories with branch:%lx; taken?:%d, " 656 "path Hist: %x; pointer:%d\n", branch_pc, taken, tHist.pathHist, 657 tHist.ptGhist); 658} 659 660void 661LTAGE::squash(ThreadID tid, bool taken, void *bp_history) 662{ 663 BranchInfo* bi = (BranchInfo*)(bp_history); 664 ThreadHistory& tHist = threadHistory[tid]; 665 DPRINTF(LTage, "Restoring branch info: %lx; taken? %d; PathHistory:%x, " 666 "pointer:%d\n", bi->branchPC,taken, bi->pathHist, bi->ptGhist); 667 tHist.pathHist = bi->pathHist; 668 tHist.ptGhist = bi->ptGhist; 669 tHist.gHist = &(tHist.globalHistory[tHist.ptGhist]); 670 tHist.gHist[0] = (taken ? 1 : 0); 671 for (int i = 1; i <= nHistoryTables; i++) { 672 tHist.computeIndices[i].comp = bi->ci[i]; 673 tHist.computeTags[0][i].comp = bi->ct0[i]; 674 tHist.computeTags[1][i].comp = bi->ct1[i]; 675 tHist.computeIndices[i].update(tHist.gHist); 676 tHist.computeTags[0][i].update(tHist.gHist); 677 tHist.computeTags[1][i].update(tHist.gHist); 678 } 679 680 if (bi->condBranch) { 681 if (bi->loopHit >= 0) { 682 int idx = bi->loopIndex + bi->loopHit; 683 ltable[idx].currentIterSpec = bi->currentIter; 684 } 685 } 686 687} 688 689void 690LTAGE::squash(ThreadID tid, void *bp_history) 691{ 692 BranchInfo* bi = (BranchInfo*)(bp_history); 693 DPRINTF(LTage, "Deleting branch info: %lx\n", bi->branchPC); 694 if (bi->condBranch) { 695 if (bi->loopHit >= 0) { 696 int idx = bi->loopIndex + bi->loopHit; 697 ltable[idx].currentIterSpec = bi->currentIter; 698 } 699 } 700 701 delete bi; 702} 703 704bool 705LTAGE::lookup(ThreadID tid, Addr branch_pc, void* &bp_history) 706{ 707 bool retval = predict(tid, branch_pc, true, bp_history); 708 709 DPRINTF(LTage, "Lookup branch: %lx; predict:%d\n", branch_pc, retval); 710 updateHistories(tid, branch_pc, retval, bp_history); 711 assert(threadHistory[tid].gHist == 712 &threadHistory[tid].globalHistory[threadHistory[tid].ptGhist]); 713 714 return retval; 715} 716 717void 718LTAGE::btbUpdate(ThreadID tid, Addr branch_pc, void* &bp_history) 719{ 720 BranchInfo* bi = (BranchInfo*) bp_history; 721 ThreadHistory& tHist = threadHistory[tid]; 722 DPRINTF(LTage, "BTB miss resets prediction: %lx\n", branch_pc); 723 assert(tHist.gHist == &tHist.globalHistory[tHist.ptGhist]); 724 tHist.gHist[0] = 0; 725 for (int i = 1; i <= nHistoryTables; i++) { 726 tHist.computeIndices[i].comp = bi->ci[i]; 727 tHist.computeTags[0][i].comp = bi->ct0[i]; 728 tHist.computeTags[1][i].comp = bi->ct1[i]; 729 tHist.computeIndices[i].update(tHist.gHist); 730 tHist.computeTags[0][i].update(tHist.gHist); 731 tHist.computeTags[1][i].update(tHist.gHist); 732 } 733} 734 735void 736LTAGE::uncondBranch(ThreadID tid, Addr br_pc, void* &bp_history) 737{ 738 DPRINTF(LTage, "UnConditionalBranch: %lx\n", br_pc); 739 predict(tid, br_pc, false, bp_history); 740 updateHistories(tid, br_pc, true, bp_history); 741 assert(threadHistory[tid].gHist == 742 &threadHistory[tid].globalHistory[threadHistory[tid].ptGhist]); 743} 744 745LTAGE* 746LTAGEParams::create() 747{ 748 return new LTAGE(this); 749}
| 241} 242 243 244//loop prediction: only used if high confidence 245bool 246LTAGE::getLoop(Addr pc, BranchInfo* bi) const 247{ 248 bi->loopHit = -1; 249 bi->loopPredValid = false; 250 bi->loopIndex = lindex(pc); 251 bi->loopTag = ((pc) >> (instShiftAmt + logSizeLoopPred - 2)); 252 253 for (int i = 0; i < 4; i++) { 254 if (ltable[bi->loopIndex + i].tag == bi->loopTag) { 255 bi->loopHit = i; 256 bi->loopPredValid = (ltable[bi->loopIndex + i].confidence >= 3); 257 bi->currentIter = ltable[bi->loopIndex + i].currentIterSpec; 258 if (ltable[bi->loopIndex + i].currentIterSpec + 1 == 259 ltable[bi->loopIndex + i].numIter) { 260 return !(ltable[bi->loopIndex + i].dir); 261 }else { 262 return (ltable[bi->loopIndex + i].dir); 263 } 264 } 265 } 266 return false; 267} 268 269void 270LTAGE::specLoopUpdate(Addr pc, bool taken, BranchInfo* bi) 271{ 272 if (bi->loopHit>=0) { 273 int index = lindex(pc); 274 if (taken != ltable[index].dir) { 275 ltable[index].currentIterSpec = 0; 276 } else { 277 ltable[index].currentIterSpec++; 278 } 279 } 280} 281 282void 283LTAGE::loopUpdate(Addr pc, bool taken, BranchInfo* bi) 284{ 285 int idx = bi->loopIndex + bi->loopHit; 286 if (bi->loopHit >= 0) { 287 //already a hit 288 if (bi->loopPredValid) { 289 if (taken != bi->loopPred) { 290 // free the entry 291 ltable[idx].numIter = 0; 292 ltable[idx].age = 0; 293 ltable[idx].confidence = 0; 294 ltable[idx].currentIter = 0; 295 return; 296 } else if (bi->loopPred != bi->tagePred) { 297 DPRINTF(LTage, "Loop Prediction success:%lx\n",pc); 298 if (ltable[idx].age < 7) 299 ltable[idx].age++; 300 } 301 } 302 303 ltable[idx].currentIter++; 304 if (ltable[idx].currentIter > ltable[idx].numIter) { 305 ltable[idx].confidence = 0; 306 if (ltable[idx].numIter != 0) { 307 // free the entry 308 ltable[idx].numIter = 0; 309 ltable[idx].age = 0; 310 ltable[idx].confidence = 0; 311 } 312 } 313 314 if (taken != ltable[idx].dir) { 315 if (ltable[idx].currentIter == ltable[idx].numIter) { 316 DPRINTF(LTage, "Loop End predicted successfully:%lx\n", pc); 317 318 if (ltable[idx].confidence < 7) { 319 ltable[idx].confidence++; 320 } 321 //just do not predict when the loop count is 1 or 2 322 if (ltable[idx].numIter < 3) { 323 // free the entry 324 ltable[idx].dir = taken; 325 ltable[idx].numIter = 0; 326 ltable[idx].age = 0; 327 ltable[idx].confidence = 0; 328 } 329 } else { 330 DPRINTF(LTage, "Loop End predicted incorrectly:%lx\n", pc); 331 if (ltable[idx].numIter == 0) { 332 // first complete nest; 333 ltable[idx].confidence = 0; 334 ltable[idx].numIter = ltable[idx].currentIter; 335 } else { 336 //not the same number of iterations as last time: free the 337 //entry 338 ltable[idx].numIter = 0; 339 ltable[idx].age = 0; 340 ltable[idx].confidence = 0; 341 } 342 } 343 ltable[idx].currentIter = 0; 344 } 345 346 } else if (taken) { 347 //try to allocate an entry on taken branch 348 int nrand = random_mt.random<int>(); 349 for (int i = 0; i < 4; i++) { 350 int loop_hit = (nrand + i) & 3; 351 idx = bi->loopIndex + loop_hit; 352 if (ltable[idx].age == 0) { 353 DPRINTF(LTage, "Allocating loop pred entry for branch %lx\n", 354 pc); 355 ltable[idx].dir = !taken; 356 ltable[idx].tag = bi->loopTag; 357 ltable[idx].numIter = 0; 358 ltable[idx].age = 7; 359 ltable[idx].confidence = 0; 360 ltable[idx].currentIter = 1; 361 break; 362 363 } 364 else 365 ltable[idx].age--; 366 } 367 } 368 369} 370 371// shifting the global history: we manage the history in a big table in order 372// to reduce simulation time 373void 374LTAGE::updateGHist(uint8_t * &h, bool dir, uint8_t * tab, int &pt) 375{ 376 if (pt == 0) { 377 DPRINTF(LTage, "Rolling over the histories\n"); 378 // Copy beginning of globalHistoryBuffer to end, such that 379 // the last maxHist outcomes are still reachable 380 // through pt[0 .. maxHist - 1]. 381 for (int i = 0; i < maxHist; i++) 382 tab[histBufferSize - maxHist + i] = tab[i]; 383 pt = histBufferSize - maxHist; 384 h = &tab[pt]; 385 } 386 pt--; 387 h--; 388 h[0] = (dir) ? 1 : 0; 389} 390 391// Get GHR for hashing indirect predictor 392// Build history backwards from pointer in 393// bp_history. 394unsigned 395LTAGE::getGHR(ThreadID tid, void *bp_history) const 396{ 397 BranchInfo* bi = static_cast<BranchInfo*>(bp_history); 398 unsigned val = 0; 399 for (unsigned i = 0; i < 32; i++) { 400 // Make sure we don't go out of bounds 401 int gh_offset = bi->ptGhist + i; 402 assert(&(threadHistory[tid].globalHistory[gh_offset]) < 403 threadHistory[tid].globalHistory + histBufferSize); 404 val |= ((threadHistory[tid].globalHistory[gh_offset] & 0x1) << i); 405 } 406 407 return val; 408} 409 410//prediction 411bool 412LTAGE::predict(ThreadID tid, Addr branch_pc, bool cond_branch, void* &b) 413{ 414 BranchInfo *bi = new BranchInfo(nHistoryTables+1); 415 b = (void*)(bi); 416 Addr pc = branch_pc; 417 bool pred_taken = true; 418 bi->loopHit = -1; 419 420 if (cond_branch) { 421 // TAGE prediction 422 423 // computes the table addresses and the partial tags 424 for (int i = 1; i <= nHistoryTables; i++) { 425 tableIndices[i] = gindex(tid, pc, i); 426 bi->tableIndices[i] = tableIndices[i]; 427 tableTags[i] = gtag(tid, pc, i); 428 bi->tableTags[i] = tableTags[i]; 429 } 430 431 bi->bimodalIndex = bindex(pc); 432 433 bi->hitBank = 0; 434 bi->altBank = 0; 435 //Look for the bank with longest matching history 436 for (int i = nHistoryTables; i > 0; i--) { 437 if (gtable[i][tableIndices[i]].tag == tableTags[i]) { 438 bi->hitBank = i; 439 bi->hitBankIndex = tableIndices[bi->hitBank]; 440 break; 441 } 442 } 443 //Look for the alternate bank 444 for (int i = bi->hitBank - 1; i > 0; i--) { 445 if (gtable[i][tableIndices[i]].tag == tableTags[i]) { 446 bi->altBank = i; 447 bi->altBankIndex = tableIndices[bi->altBank]; 448 break; 449 } 450 } 451 //computes the prediction and the alternate prediction 452 if (bi->hitBank > 0) { 453 if (bi->altBank > 0) { 454 bi->altTaken = 455 gtable[bi->altBank][tableIndices[bi->altBank]].ctr >= 0; 456 }else { 457 bi->altTaken = getBimodePred(pc, bi); 458 } 459 460 bi->longestMatchPred = 461 gtable[bi->hitBank][tableIndices[bi->hitBank]].ctr >= 0; 462 bi->pseudoNewAlloc = 463 abs(2 * gtable[bi->hitBank][bi->hitBankIndex].ctr + 1) <= 1; 464 465 //if the entry is recognized as a newly allocated entry and 466 //useAltPredForNewlyAllocated is positive use the alternate 467 //prediction 468 if ((useAltPredForNewlyAllocated < 0) 469 || abs(2 * 470 gtable[bi->hitBank][tableIndices[bi->hitBank]].ctr + 1) > 1) 471 bi->tagePred = bi->longestMatchPred; 472 else 473 bi->tagePred = bi->altTaken; 474 } else { 475 bi->altTaken = getBimodePred(pc, bi); 476 bi->tagePred = bi->altTaken; 477 bi->longestMatchPred = bi->altTaken; 478 } 479 //end TAGE prediction 480 481 bi->loopPred = getLoop(pc, bi); // loop prediction 482 483 pred_taken = (((loopUseCounter >= 0) && bi->loopPredValid)) ? 484 (bi->loopPred): (bi->tagePred); 485 DPRINTF(LTage, "Predict for %lx: taken?:%d, loopTaken?:%d, " 486 "loopValid?:%d, loopUseCounter:%d, tagePred:%d, altPred:%d\n", 487 branch_pc, pred_taken, bi->loopPred, bi->loopPredValid, 488 loopUseCounter, bi->tagePred, bi->altTaken); 489 } 490 bi->branchPC = branch_pc; 491 bi->condBranch = cond_branch; 492 specLoopUpdate(branch_pc, pred_taken, bi); 493 return pred_taken; 494} 495 496// PREDICTOR UPDATE 497void 498LTAGE::update(ThreadID tid, Addr branch_pc, bool taken, void* bp_history, 499 bool squashed) 500{ 501 assert(bp_history); 502 503 BranchInfo *bi = static_cast<BranchInfo*>(bp_history); 504 505 if (squashed) { 506 // This restores the global history, then update it 507 // and recomputes the folded histories. 508 squash(tid, taken, bp_history); 509 return; 510 } 511 512 int nrand = random_mt.random<int>(0,3); 513 Addr pc = branch_pc; 514 if (bi->condBranch) { 515 DPRINTF(LTage, "Updating tables for branch:%lx; taken?:%d\n", 516 branch_pc, taken); 517 // first update the loop predictor 518 loopUpdate(pc, taken, bi); 519 520 if (bi->loopPredValid) { 521 if (bi->tagePred != bi->loopPred) { 522 ctrUpdate(loopUseCounter, (bi->loopPred== taken), 7); 523 } 524 } 525 526 // TAGE UPDATE 527 // try to allocate a new entries only if prediction was wrong 528 bool longest_match_pred = false; 529 bool alloc = (bi->tagePred != taken) && (bi->hitBank < nHistoryTables); 530 if (bi->hitBank > 0) { 531 // Manage the selection between longest matching and alternate 532 // matching for "pseudo"-newly allocated longest matching entry 533 longest_match_pred = bi->longestMatchPred; 534 bool PseudoNewAlloc = bi->pseudoNewAlloc; 535 // an entry is considered as newly allocated if its prediction 536 // counter is weak 537 if (PseudoNewAlloc) { 538 if (longest_match_pred == taken) { 539 alloc = false; 540 } 541 // if it was delivering the correct prediction, no need to 542 // allocate new entry even if the overall prediction was false 543 if (longest_match_pred != bi->altTaken) { 544 ctrUpdate(useAltPredForNewlyAllocated, 545 bi->altTaken == taken, 4); 546 } 547 } 548 } 549 550 if (alloc) { 551 // is there some "unuseful" entry to allocate 552 int8_t min = 1; 553 for (int i = nHistoryTables; i > bi->hitBank; i--) { 554 if (gtable[i][bi->tableIndices[i]].u < min) { 555 min = gtable[i][bi->tableIndices[i]].u; 556 } 557 } 558 559 // we allocate an entry with a longer history 560 // to avoid ping-pong, we do not choose systematically the next 561 // entry, but among the 3 next entries 562 int Y = nrand & 563 ((ULL(1) << (nHistoryTables - bi->hitBank - 1)) - 1); 564 int X = bi->hitBank + 1; 565 if (Y & 1) { 566 X++; 567 if (Y & 2) 568 X++; 569 } 570 // No entry available, forces one to be available 571 if (min > 0) { 572 gtable[X][bi->tableIndices[X]].u = 0; 573 } 574 575 576 //Allocate only one entry 577 for (int i = X; i <= nHistoryTables; i++) { 578 if ((gtable[i][bi->tableIndices[i]].u == 0)) { 579 gtable[i][bi->tableIndices[i]].tag = bi->tableTags[i]; 580 gtable[i][bi->tableIndices[i]].ctr = (taken) ? 0 : -1; 581 gtable[i][bi->tableIndices[i]].u = 0; //? 582 } 583 } 584 } 585 //periodic reset of u: reset is not complete but bit by bit 586 tCounter++; 587 if ((tCounter & ((ULL(1) << logTick) - 1)) == 0) { 588 // reset least significant bit 589 // most significant bit becomes least significant bit 590 for (int i = 1; i <= nHistoryTables; i++) { 591 for (int j = 0; j < (ULL(1) << tagTableSizes[i]); j++) { 592 gtable[i][j].u = gtable[i][j].u >> 1; 593 } 594 } 595 } 596 597 if (bi->hitBank > 0) { 598 DPRINTF(LTage, "Updating tag table entry (%d,%d) for branch %lx\n", 599 bi->hitBank, bi->hitBankIndex, branch_pc); 600 ctrUpdate(gtable[bi->hitBank][bi->hitBankIndex].ctr, taken, 601 tagTableCounterBits); 602 // if the provider entry is not certified to be useful also update 603 // the alternate prediction 604 if (gtable[bi->hitBank][bi->hitBankIndex].u == 0) { 605 if (bi->altBank > 0) { 606 ctrUpdate(gtable[bi->altBank][bi->altBankIndex].ctr, taken, 607 tagTableCounterBits); 608 DPRINTF(LTage, "Updating tag table entry (%d,%d) for" 609 " branch %lx\n", bi->hitBank, bi->hitBankIndex, 610 branch_pc); 611 } 612 if (bi->altBank == 0) { 613 baseUpdate(pc, taken, bi); 614 } 615 } 616 617 // update the u counter 618 if (longest_match_pred != bi->altTaken) { 619 if (longest_match_pred == taken) { 620 if (gtable[bi->hitBank][bi->hitBankIndex].u < 1) { 621 gtable[bi->hitBank][bi->hitBankIndex].u++; 622 } 623 } 624 } 625 } else { 626 baseUpdate(pc, taken, bi); 627 } 628 629 //END PREDICTOR UPDATE 630 } 631 if (!squashed) { 632 delete bi; 633 } 634} 635 636void 637LTAGE::updateHistories(ThreadID tid, Addr branch_pc, bool taken, void* b) 638{ 639 BranchInfo* bi = (BranchInfo*)(b); 640 ThreadHistory& tHist = threadHistory[tid]; 641 // UPDATE HISTORIES 642 bool pathbit = ((branch_pc >> instShiftAmt) & 1); 643 //on a squash, return pointers to this and recompute indices. 644 //update user history 645 updateGHist(tHist.gHist, taken, tHist.globalHistory, tHist.ptGhist); 646 tHist.pathHist = (tHist.pathHist << 1) + pathbit; 647 tHist.pathHist = (tHist.pathHist & ((ULL(1) << 16) - 1)); 648 649 bi->ptGhist = tHist.ptGhist; 650 bi->pathHist = tHist.pathHist; 651 //prepare next index and tag computations for user branchs 652 for (int i = 1; i <= nHistoryTables; i++) 653 { 654 bi->ci[i] = tHist.computeIndices[i].comp; 655 bi->ct0[i] = tHist.computeTags[0][i].comp; 656 bi->ct1[i] = tHist.computeTags[1][i].comp; 657 tHist.computeIndices[i].update(tHist.gHist); 658 tHist.computeTags[0][i].update(tHist.gHist); 659 tHist.computeTags[1][i].update(tHist.gHist); 660 } 661 DPRINTF(LTage, "Updating global histories with branch:%lx; taken?:%d, " 662 "path Hist: %x; pointer:%d\n", branch_pc, taken, tHist.pathHist, 663 tHist.ptGhist); 664} 665 666void 667LTAGE::squash(ThreadID tid, bool taken, void *bp_history) 668{ 669 BranchInfo* bi = (BranchInfo*)(bp_history); 670 ThreadHistory& tHist = threadHistory[tid]; 671 DPRINTF(LTage, "Restoring branch info: %lx; taken? %d; PathHistory:%x, " 672 "pointer:%d\n", bi->branchPC,taken, bi->pathHist, bi->ptGhist); 673 tHist.pathHist = bi->pathHist; 674 tHist.ptGhist = bi->ptGhist; 675 tHist.gHist = &(tHist.globalHistory[tHist.ptGhist]); 676 tHist.gHist[0] = (taken ? 1 : 0); 677 for (int i = 1; i <= nHistoryTables; i++) { 678 tHist.computeIndices[i].comp = bi->ci[i]; 679 tHist.computeTags[0][i].comp = bi->ct0[i]; 680 tHist.computeTags[1][i].comp = bi->ct1[i]; 681 tHist.computeIndices[i].update(tHist.gHist); 682 tHist.computeTags[0][i].update(tHist.gHist); 683 tHist.computeTags[1][i].update(tHist.gHist); 684 } 685 686 if (bi->condBranch) { 687 if (bi->loopHit >= 0) { 688 int idx = bi->loopIndex + bi->loopHit; 689 ltable[idx].currentIterSpec = bi->currentIter; 690 } 691 } 692 693} 694 695void 696LTAGE::squash(ThreadID tid, void *bp_history) 697{ 698 BranchInfo* bi = (BranchInfo*)(bp_history); 699 DPRINTF(LTage, "Deleting branch info: %lx\n", bi->branchPC); 700 if (bi->condBranch) { 701 if (bi->loopHit >= 0) { 702 int idx = bi->loopIndex + bi->loopHit; 703 ltable[idx].currentIterSpec = bi->currentIter; 704 } 705 } 706 707 delete bi; 708} 709 710bool 711LTAGE::lookup(ThreadID tid, Addr branch_pc, void* &bp_history) 712{ 713 bool retval = predict(tid, branch_pc, true, bp_history); 714 715 DPRINTF(LTage, "Lookup branch: %lx; predict:%d\n", branch_pc, retval); 716 updateHistories(tid, branch_pc, retval, bp_history); 717 assert(threadHistory[tid].gHist == 718 &threadHistory[tid].globalHistory[threadHistory[tid].ptGhist]); 719 720 return retval; 721} 722 723void 724LTAGE::btbUpdate(ThreadID tid, Addr branch_pc, void* &bp_history) 725{ 726 BranchInfo* bi = (BranchInfo*) bp_history; 727 ThreadHistory& tHist = threadHistory[tid]; 728 DPRINTF(LTage, "BTB miss resets prediction: %lx\n", branch_pc); 729 assert(tHist.gHist == &tHist.globalHistory[tHist.ptGhist]); 730 tHist.gHist[0] = 0; 731 for (int i = 1; i <= nHistoryTables; i++) { 732 tHist.computeIndices[i].comp = bi->ci[i]; 733 tHist.computeTags[0][i].comp = bi->ct0[i]; 734 tHist.computeTags[1][i].comp = bi->ct1[i]; 735 tHist.computeIndices[i].update(tHist.gHist); 736 tHist.computeTags[0][i].update(tHist.gHist); 737 tHist.computeTags[1][i].update(tHist.gHist); 738 } 739} 740 741void 742LTAGE::uncondBranch(ThreadID tid, Addr br_pc, void* &bp_history) 743{ 744 DPRINTF(LTage, "UnConditionalBranch: %lx\n", br_pc); 745 predict(tid, br_pc, false, bp_history); 746 updateHistories(tid, br_pc, true, bp_history); 747 assert(threadHistory[tid].gHist == 748 &threadHistory[tid].globalHistory[threadHistory[tid].ptGhist]); 749} 750 751LTAGE* 752LTAGEParams::create() 753{ 754 return new LTAGE(this); 755}
|