2bit_local.cc revision 6184
12447SN/A/* 25254Sksewell@umich.edu * Copyright (c) 2004-2006 The Regents of The University of Michigan 35254Sksewell@umich.edu * All rights reserved. 45254Sksewell@umich.edu * 52447SN/A * Redistribution and use in source and binary forms, with or without 65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are 75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 145254Sksewell@umich.edu * this software without specific prior written permission. 155254Sksewell@umich.edu * 162447SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275254Sksewell@umich.edu * 282632Sstever@eecs.umich.edu * Authors: Kevin Lim 295254Sksewell@umich.edu */ 305254Sksewell@umich.edu 315254Sksewell@umich.edu#include "base/intmath.hh" 322447SN/A#include "base/misc.hh" 332447SN/A#include "base/trace.hh" 342447SN/A#include "cpu/o3/2bit_local_pred.hh" 352447SN/A 368229Snate@binkert.orgLocalBP::LocalBP(unsigned _localPredictorSize, 372597SN/A unsigned _localCtrBits, 382597SN/A unsigned _instShiftAmt) 396216Snate@binkert.org : localPredictorSize(_localPredictorSize), 402980Sgblack@eecs.umich.edu localCtrBits(_localCtrBits), 417720Sgblack@eecs.umich.edu instShiftAmt(_instShiftAmt) 424661Sksewell@umich.edu{ 434661Sksewell@umich.edu if (!isPowerOf2(localPredictorSize)) { 442980Sgblack@eecs.umich.edu fatal("Invalid local predictor size!\n"); 452980Sgblack@eecs.umich.edu } 462597SN/A 472597SN/A localPredictorSets = localPredictorSize / localCtrBits; 487720Sgblack@eecs.umich.edu 497720Sgblack@eecs.umich.edu if (!isPowerOf2(localPredictorSets)) { 507720Sgblack@eecs.umich.edu fatal("Invalid number of local predictor sets! Check localCtrBits.\n"); 517720Sgblack@eecs.umich.edu } 527720Sgblack@eecs.umich.edu 537720Sgblack@eecs.umich.edu // Setup the index mask. 547720Sgblack@eecs.umich.edu indexMask = localPredictorSets - 1; 557720Sgblack@eecs.umich.edu 567720Sgblack@eecs.umich.edu DPRINTF(Fetch, "Branch predictor: index mask: %#x\n", indexMask); 577707Sgblack@eecs.umich.edu 584826Ssaidi@eecs.umich.edu // Setup the array of counters for the local predictor. 596378Sgblack@eecs.umich.edu localCtrs.resize(localPredictorSets); 606378Sgblack@eecs.umich.edu 616378Sgblack@eecs.umich.edu for (int i = 0; i < localPredictorSets; ++i) 626378Sgblack@eecs.umich.edu localCtrs[i].setBits(_localCtrBits); 636378Sgblack@eecs.umich.edu 646378Sgblack@eecs.umich.edu DPRINTF(Fetch, "Branch predictor: local predictor size: %i\n", 656378Sgblack@eecs.umich.edu localPredictorSize); 662686Sksewell@umich.edu 676378Sgblack@eecs.umich.edu DPRINTF(Fetch, "Branch predictor: local counter bits: %i\n", localCtrBits); 686378Sgblack@eecs.umich.edu 696378Sgblack@eecs.umich.edu DPRINTF(Fetch, "Branch predictor: instruction shift amount: %i\n", 702686Sksewell@umich.edu instShiftAmt); 716378Sgblack@eecs.umich.edu} 726378Sgblack@eecs.umich.edu 736378Sgblack@eecs.umich.eduvoid 742972Sgblack@eecs.umich.eduLocalBP::reset() 756378Sgblack@eecs.umich.edu{ 766378Sgblack@eecs.umich.edu for (int i = 0; i < localPredictorSets; ++i) { 776378Sgblack@eecs.umich.edu localCtrs[i].reset(); 786383Sgblack@eecs.umich.edu } 796383Sgblack@eecs.umich.edu} 805222Sksewell@umich.edu 816378Sgblack@eecs.umich.edubool 826378Sgblack@eecs.umich.eduLocalBP::lookup(Addr &branch_addr, void * &bp_history) 836378Sgblack@eecs.umich.edu{ 846378Sgblack@eecs.umich.edu bool taken; 856378Sgblack@eecs.umich.edu uint8_t counter_val; 866378Sgblack@eecs.umich.edu unsigned local_predictor_idx = getLocalIndex(branch_addr); 876378Sgblack@eecs.umich.edu 885222Sksewell@umich.edu DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n", 896378Sgblack@eecs.umich.edu local_predictor_idx); 904661Sksewell@umich.edu 916378Sgblack@eecs.umich.edu counter_val = localCtrs[local_predictor_idx].read(); 926378Sgblack@eecs.umich.edu 935222Sksewell@umich.edu DPRINTF(Fetch, "Branch predictor: prediction is %i.\n", 946378Sgblack@eecs.umich.edu (int)counter_val); 956378Sgblack@eecs.umich.edu 966378Sgblack@eecs.umich.edu taken = getPrediction(counter_val); 976378Sgblack@eecs.umich.edu 986378Sgblack@eecs.umich.edu#if 0 996378Sgblack@eecs.umich.edu // Speculative update. 1006378Sgblack@eecs.umich.edu if (taken) { 1015222Sksewell@umich.edu DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n"); 1026378Sgblack@eecs.umich.edu localCtrs[local_predictor_idx].increment(); 1036378Sgblack@eecs.umich.edu } else { 1046378Sgblack@eecs.umich.edu DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n"); 1055222Sksewell@umich.edu localCtrs[local_predictor_idx].decrement(); 1066378Sgblack@eecs.umich.edu } 1076378Sgblack@eecs.umich.edu#endif 1086378Sgblack@eecs.umich.edu 1096378Sgblack@eecs.umich.edu return taken; 1106378Sgblack@eecs.umich.edu} 1116329Sgblack@eecs.umich.edu 1126378Sgblack@eecs.umich.eduvoid 1136378Sgblack@eecs.umich.eduLocalBP::update(Addr &branch_addr, bool taken, void *bp_history) 1146378Sgblack@eecs.umich.edu{ 1157693SAli.Saidi@ARM.com assert(bp_history == NULL); 1167693SAli.Saidi@ARM.com unsigned local_predictor_idx; 1177720Sgblack@eecs.umich.edu 1187720Sgblack@eecs.umich.edu // Update the local predictor. 1197720Sgblack@eecs.umich.edu local_predictor_idx = getLocalIndex(branch_addr); 1207720Sgblack@eecs.umich.edu 1217720Sgblack@eecs.umich.edu DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n", 1227720Sgblack@eecs.umich.edu local_predictor_idx); 1238300Schander.sudanthi@arm.com 1248300Schander.sudanthi@arm.com if (taken) { 1258300Schander.sudanthi@arm.com DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n"); 1268300Schander.sudanthi@arm.com localCtrs[local_predictor_idx].increment(); 1278300Schander.sudanthi@arm.com } else { 1288300Schander.sudanthi@arm.com DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n"); 1292597SN/A localCtrs[local_predictor_idx].decrement(); 1302447SN/A } 1312686Sksewell@umich.edu} 1322447SN/A 133inline 134bool 135LocalBP::getPrediction(uint8_t &count) 136{ 137 // Get the MSB of the count 138 return (count >> (localCtrBits - 1)); 139} 140 141inline 142unsigned 143LocalBP::getLocalIndex(Addr &branch_addr) 144{ 145 return (branch_addr >> instShiftAmt) & indexMask; 146} 147