2bit_local.cc revision 9480
12SN/A/* 21762SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim 292665Ssaidi@eecs.umich.edu */ 302SN/A 312SN/A#include "base/intmath.hh" 322SN/A#include "base/misc.hh" 332SN/A#include "base/trace.hh" 342SN/A#include "cpu/pred/2bit_local.hh" 352SN/A#include "debug/Fetch.hh" 365882Snate@binkert.org 371492SN/ALocalBP::LocalBP(const Params *params) 381717SN/A : BPredUnit(params), 398229Snate@binkert.org localPredictorSize(params->localPredictorSize), 402680Sktlim@umich.edu localCtrBits(params->localCtrBits), 418232Snate@binkert.org instShiftAmt(params->instShiftAmt) 424167Sbinkertn@umich.edu{ 432190SN/A if (!isPowerOf2(localPredictorSize)) { 442SN/A fatal("Invalid local predictor size!\n"); 452SN/A } 462SN/A 472SN/A localPredictorSets = localPredictorSize / localCtrBits; 482SN/A 492SN/A if (!isPowerOf2(localPredictorSets)) { 502SN/A fatal("Invalid number of local predictor sets! Check localCtrBits.\n"); 512SN/A } 522SN/A 532SN/A // Setup the index mask. 542SN/A indexMask = localPredictorSets - 1; 552SN/A 562SN/A DPRINTF(Fetch, "index mask: %#x\n", indexMask); 572SN/A 588991SAli.Saidi@ARM.com // Setup the array of counters for the local predictor. 598991SAli.Saidi@ARM.com localCtrs.resize(localPredictorSets); 608991SAli.Saidi@ARM.com 612SN/A for (unsigned i = 0; i < localPredictorSets; ++i) 622SN/A localCtrs[i].setBits(localCtrBits); 632SN/A 648991SAli.Saidi@ARM.com DPRINTF(Fetch, "local predictor size: %i\n", 652SN/A localPredictorSize); 668991SAli.Saidi@ARM.com 678991SAli.Saidi@ARM.com DPRINTF(Fetch, "local counter bits: %i\n", localCtrBits); 682SN/A 698991SAli.Saidi@ARM.com DPRINTF(Fetch, "instruction shift amount: %i\n", 702SN/A instShiftAmt); 712SN/A} 722SN/A 732SN/Avoid 742SN/ALocalBP::reset() 752SN/A{ 762SN/A for (unsigned i = 0; i < localPredictorSets; ++i) { 772SN/A localCtrs[i].reset(); 782SN/A } 792SN/A} 802SN/A 812SN/Avoid 822SN/ALocalBP::btbUpdate(Addr branch_addr, void * &bp_history) 832SN/A{ 842SN/A// Place holder for a function that is called to update predictor history when 852SN/A// a BTB entry is invalid or not found. 862SN/A} 872SN/A 882680Sktlim@umich.edu 892SN/Abool 908670Ss052838@student.dtu.dkLocalBP::lookup(Addr branch_addr, void * &bp_history) 918670Ss052838@student.dtu.dk{ 928670Ss052838@student.dtu.dk bool taken; 932SN/A uint8_t counter_val; 942SN/A unsigned local_predictor_idx = getLocalIndex(branch_addr); 952SN/A 962SN/A DPRINTF(Fetch, "Looking up index %#x\n", 972SN/A local_predictor_idx); 982SN/A 992SN/A counter_val = localCtrs[local_predictor_idx].read(); 1008670Ss052838@student.dtu.dk 1012SN/A DPRINTF(Fetch, "prediction is %i.\n", 1022SN/A (int)counter_val); 1032SN/A 1042SN/A taken = getPrediction(counter_val); 1052SN/A 1062680Sktlim@umich.edu#if 0 1072SN/A // Speculative update. 1082SN/A if (taken) { 1092SN/A DPRINTF(Fetch, "Branch updated as taken.\n"); 1102SN/A localCtrs[local_predictor_idx].increment(); 1112SN/A } else { 1122SN/A DPRINTF(Fetch, "Branch updated as not taken.\n"); 1132SN/A localCtrs[local_predictor_idx].decrement(); 1142SN/A } 1152SN/A#endif 1162SN/A 1172SN/A return taken; 1182SN/A} 1192SN/A 1207823Ssteve.reinhardt@amd.comvoid 1212SN/ALocalBP::update(Addr branch_addr, bool taken, void *bp_history, bool squashed) 1222SN/A{ 1232SN/A assert(bp_history == NULL); 1242SN/A unsigned local_predictor_idx; 1252SN/A 1262SN/A // Update the local predictor. 1272SN/A local_predictor_idx = getLocalIndex(branch_addr); 1282SN/A 1292SN/A DPRINTF(Fetch, "Looking up index %#x\n", local_predictor_idx); 1301885SN/A 1311885SN/A if (taken) { 1321885SN/A DPRINTF(Fetch, "Branch updated as taken.\n"); 1332SN/A localCtrs[local_predictor_idx].increment(); 1342SN/A } else { 1352SN/A DPRINTF(Fetch, "Branch updated as not taken.\n"); 1362SN/A localCtrs[local_predictor_idx].decrement(); 1372680Sktlim@umich.edu } 1382SN/A} 1392680Sktlim@umich.edu 1401646SN/Ainline 1418231Snate@binkert.orgbool 1422SN/ALocalBP::getPrediction(uint8_t &count) 1432SN/A{ 1442SN/A // Get the MSB of the count 1452SN/A return (count >> (localCtrBits - 1)); 1462SN/A} 1472130SN/A 1482SN/Ainline 1491885SN/Aunsigned 1502SN/ALocalBP::getLocalIndex(Addr &branch_addr) 1512SN/A{ 1522SN/A return (branch_addr >> instShiftAmt) & indexMask; 1532130SN/A} 1542SN/A 1552SN/Avoid 1562SN/ALocalBP::uncondBranch(void *&bp_history) 1572SN/A{ 1582SN/A} 1592SN/A