bpred_unit.hh revision 5999
12810SN/A/* 29614Srene.dejong@arm.com * Copyright (c) 2004-2005 The Regents of The University of Michigan 38856Sandreas.hansson@arm.com * All rights reserved. 48856Sandreas.hansson@arm.com * 58856Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without 68856Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are 78856Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright 88856Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer; 98856Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright 108856Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the 118856Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution; 128856Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its 138856Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from 142810SN/A * this software without specific prior written permission. 152810SN/A * 162810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272810SN/A * 282810SN/A * Authors: Kevin Lim 292810SN/A */ 302810SN/A 312810SN/A#ifndef __CPU_O3_BPRED_UNIT_HH__ 322810SN/A#define __CPU_O3_BPRED_UNIT_HH__ 332810SN/A 342810SN/A#include "base/statistics.hh" 352810SN/A#include "cpu/inst_seq.hh" 362810SN/A 372810SN/A#include "cpu/o3/2bit_local_pred.hh" 382810SN/A#include "cpu/o3/btb.hh" 392810SN/A#include "cpu/o3/ras.hh" 402810SN/A#include "cpu/o3/tournament_pred.hh" 412810SN/A 422810SN/A#include "sim/host.hh" 432810SN/A 442810SN/A#include <list> 452810SN/A 462810SN/Aclass DerivO3CPUParams; 472810SN/A 488232Snate@binkert.org/** 499152Satgutier@umich.edu * Basically a wrapper class to hold both the branch predictor 509795Sandreas.hansson@arm.com * and the BTB. 519795Sandreas.hansson@arm.com */ 5210263Satgutier@umich.edutemplate<class Impl> 535338Sstever@gmail.comclass BPredUnit 549795Sandreas.hansson@arm.com{ 555338Sstever@gmail.com private: 568786Sgblack@eecs.umich.edu typedef typename Impl::DynInstPtr DynInstPtr; 572810SN/A 582810SN/A enum PredType { 592810SN/A Local, 608856Sandreas.hansson@arm.com Tournament 618856Sandreas.hansson@arm.com }; 628856Sandreas.hansson@arm.com 638922Swilliam.wang@arm.com PredType predictor; 648914Sandreas.hansson@arm.com 658856Sandreas.hansson@arm.com public: 668856Sandreas.hansson@arm.com 674475SN/A /** 685034SN/A * @param params The params object, that has the size of the BP and BTB. 695034SN/A */ 7010360Sandreas.hansson@arm.com BPredUnit(DerivO3CPUParams *params); 7110622Smitch.hayenga@arm.com 7210622Smitch.hayenga@arm.com /** 734628SN/A * Registers statistics. 749814Sandreas.hansson@arm.com */ 7510693SMarco.Balboni@ARM.com void regStats(); 7610693SMarco.Balboni@ARM.com 7710693SMarco.Balboni@ARM.com void switchOut(); 789263Smrinmoy.ghosh@arm.com 795034SN/A void takeOverFrom(); 806122SSteve.Reinhardt@amd.com 818134SAli.Saidi@ARM.com /** 824626SN/A * Predicts whether or not the instruction is a taken branch, and the 8310360Sandreas.hansson@arm.com * target of the branch if it is taken. 844626SN/A * @param inst The branch instruction. 855034SN/A * @param PC The predicted PC is passed back through this parameter. 868883SAli.Saidi@ARM.com * @param tid The thread id. 878833Sdam.sunwoo@arm.com * @return Returns if the branch is taken or not. 884458SN/A */ 892810SN/A bool predict(DynInstPtr &inst, Addr &PC, unsigned tid); 902810SN/A 913013SN/A // @todo: Rename this function. 928856Sandreas.hansson@arm.com void BPUncond(void * &bp_history); 932810SN/A 943013SN/A /** 958856Sandreas.hansson@arm.com * Tells the branch predictor to commit any updates until the given 962810SN/A * sequence number. 979614Srene.dejong@arm.com * @param done_sn The sequence number to commit any older updates up until. 989614Srene.dejong@arm.com * @param tid The thread id. 999614Srene.dejong@arm.com */ 10010345SCurtis.Dunham@arm.com void update(const InstSeqNum &done_sn, unsigned tid); 10110345SCurtis.Dunham@arm.com 10210345SCurtis.Dunham@arm.com /** 1039614Srene.dejong@arm.com * Squashes all outstanding updates until a given sequence number. 1042810SN/A * @param squashed_sn The sequence number to squash any younger updates up 1052810SN/A * until. 1062810SN/A * @param tid The thread id. 1078856Sandreas.hansson@arm.com */ 1082810SN/A void squash(const InstSeqNum &squashed_sn, unsigned tid); 1093013SN/A 1108856Sandreas.hansson@arm.com /** 1113013SN/A * Squashes all outstanding updates until a given sequence number, and 1128856Sandreas.hansson@arm.com * corrects that sn's update with the proper address and taken/not taken. 1134666SN/A * @param squashed_sn The sequence number to squash any younger updates up 1148922Swilliam.wang@arm.com * until. 1152897SN/A * @param corr_target The correct branch target. 1162810SN/A * @param actually_taken The correct branch direction. 1172810SN/A * @param tid The thread id. 11810344Sandreas.hansson@arm.com */ 11910344Sandreas.hansson@arm.com void squash(const InstSeqNum &squashed_sn, const Addr &corr_target, 12010344Sandreas.hansson@arm.com bool actually_taken, unsigned tid); 12110344Sandreas.hansson@arm.com 12210344Sandreas.hansson@arm.com /** 12310344Sandreas.hansson@arm.com * @param bp_history Pointer to the history object. The predictor 12410344Sandreas.hansson@arm.com * will need to update any state and delete the object. 12510344Sandreas.hansson@arm.com */ 12610344Sandreas.hansson@arm.com void BPSquash(void *bp_history); 1272844SN/A 1282810SN/A /** 1292858SN/A * Looks up a given PC in the BP to see if it is taken or not taken. 1302858SN/A * @param inst_PC The PC to look up. 1318856Sandreas.hansson@arm.com * @param bp_history Pointer that will be set to an object that 1328922Swilliam.wang@arm.com * has the branch predictor state associated with the lookup. 1338711Sandreas.hansson@arm.com * @return Whether the branch is taken or not taken. 1342858SN/A */ 1352858SN/A bool BPLookup(Addr &inst_PC, void * &bp_history); 1369294Sandreas.hansson@arm.com 1379294Sandreas.hansson@arm.com /** 1388922Swilliam.wang@arm.com * Looks up a given PC in the BTB to see if a matching entry exists. 1398922Swilliam.wang@arm.com * @param inst_PC The PC to look up. 1408922Swilliam.wang@arm.com * @return Whether the BTB contains the given PC. 1418922Swilliam.wang@arm.com */ 1428922Swilliam.wang@arm.com bool BTBValid(Addr &inst_PC) 1438922Swilliam.wang@arm.com { return BTB.valid(inst_PC, 0); } 1448922Swilliam.wang@arm.com 1458922Swilliam.wang@arm.com /** 1469294Sandreas.hansson@arm.com * Looks up a given PC in the BTB to get the predicted target. 1479294Sandreas.hansson@arm.com * @param inst_PC The PC to look up. 1488922Swilliam.wang@arm.com * @return The address of the target of the branch. 1498922Swilliam.wang@arm.com */ 1508922Swilliam.wang@arm.com Addr BTBLookup(Addr &inst_PC) 1518922Swilliam.wang@arm.com { return BTB.lookup(inst_PC, 0); } 1528922Swilliam.wang@arm.com 1538922Swilliam.wang@arm.com /** 1548922Swilliam.wang@arm.com * Updates the BP with taken/not taken information. 1554628SN/A * @param inst_PC The branch's PC that will be updated. 1562858SN/A * @param taken Whether the branch was taken or not taken. 1572810SN/A * @param bp_history Pointer to the branch predictor state that is 1582810SN/A * associated with the branch lookup that is being updated. 1592810SN/A * @todo Make this update flexible enough to handle a global predictor. 1602810SN/A */ 1612810SN/A void BPUpdate(Addr &inst_PC, bool taken, void *bp_history); 1624022SN/A 1634022SN/A /** 1644022SN/A * Updates the BTB with the target of a branch. 1652810SN/A * @param inst_PC The branch's PC that will be updated. 1662810SN/A * @param target_PC The branch's target that will be added to the BTB. 1678833Sdam.sunwoo@arm.com */ 1682810SN/A void BTBUpdate(Addr &inst_PC, Addr &target_PC) 1692810SN/A { BTB.update(inst_PC, target_PC,0); } 1702810SN/A 1712810SN/A void dump(); 1728833Sdam.sunwoo@arm.com 1738833Sdam.sunwoo@arm.com private: 1748833Sdam.sunwoo@arm.com struct PredictorHistory { 1752810SN/A /** 1762810SN/A * Makes a predictor history struct that contains any 1774871SN/A * information needed to update the predictor, BTB, and RAS. 1784871SN/A */ 1794871SN/A PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC, 1804871SN/A const bool pred_taken, void *bp_history, 1814871SN/A const unsigned _tid) 1824871SN/A : seqNum(seq_num), PC(inst_PC), RASTarget(0), 1834871SN/A RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0), 1844871SN/A wasCall(0), bpHistory(bp_history) 1854871SN/A { } 1864871SN/A 1872810SN/A /** The sequence number for the predictor history entry. */ 1882810SN/A InstSeqNum seqNum; 1892810SN/A 1908833Sdam.sunwoo@arm.com /** The PC associated with the sequence number. */ 1912810SN/A Addr PC; 1924871SN/A 1938833Sdam.sunwoo@arm.com /** The RAS target (only valid if a return). */ 1948833Sdam.sunwoo@arm.com Addr RASTarget; 1958833Sdam.sunwoo@arm.com 1962810SN/A /** The RAS index of the instruction (only valid if a call). */ 1972810SN/A unsigned RASIndex; 1982810SN/A 1992810SN/A /** The thread id. */ 2008833Sdam.sunwoo@arm.com unsigned tid; 2012810SN/A 2024871SN/A /** Whether or not it was predicted taken. */ 2038833Sdam.sunwoo@arm.com bool predTaken; 2048833Sdam.sunwoo@arm.com 2058833Sdam.sunwoo@arm.com /** Whether or not the RAS was used. */ 2062810SN/A bool usedRAS; 2072810SN/A 2084022SN/A /** Whether or not the instruction was a call. */ 2094022SN/A bool wasCall; 2104022SN/A 2112810SN/A /** Pointer to the history object passed back from the branch 2122810SN/A * predictor. It is used to update or restore state of the 2138833Sdam.sunwoo@arm.com * branch predictor. 2142810SN/A */ 2152810SN/A void *bpHistory; 2162810SN/A }; 2172810SN/A 2188833Sdam.sunwoo@arm.com typedef std::list<PredictorHistory> History; 2198833Sdam.sunwoo@arm.com 2208833Sdam.sunwoo@arm.com /** 2212810SN/A * The per-thread predictor history. This is used to update the predictor 2222810SN/A * as instructions are committed, or restore it to the proper state after 2232810SN/A * a squash. 2242810SN/A */ 2252810SN/A History predHist[Impl::MaxThreads]; 2268833Sdam.sunwoo@arm.com 2272810SN/A /** The local branch predictor. */ 2284871SN/A LocalBP *localBP; 2298833Sdam.sunwoo@arm.com 2308833Sdam.sunwoo@arm.com /** The tournament branch predictor. */ 2318833Sdam.sunwoo@arm.com TournamentBP *tournamentBP; 2322810SN/A 2332810SN/A /** The BTB. */ 2342810SN/A DefaultBTB BTB; 2352810SN/A 2368833Sdam.sunwoo@arm.com /** The per-thread return address stack. */ 2372810SN/A ReturnAddrStack RAS[Impl::MaxThreads]; 2384871SN/A 2398833Sdam.sunwoo@arm.com /** Stat for number of BP lookups. */ 2408833Sdam.sunwoo@arm.com Stats::Scalar lookups; 2418833Sdam.sunwoo@arm.com /** Stat for number of conditional branches predicted. */ 2422810SN/A Stats::Scalar condPredicted; 2432810SN/A /** Stat for number of conditional branches predicted incorrectly. */ 2444022SN/A Stats::Scalar condIncorrect; 2454022SN/A /** Stat for number of BTB lookups. */ 2464022SN/A Stats::Scalar BTBLookups; 2472810SN/A /** Stat for number of BTB hits. */ 2482810SN/A Stats::Scalar BTBHits; 2498833Sdam.sunwoo@arm.com /** Stat for number of times the BTB is correct. */ 2502810SN/A Stats::Scalar BTBCorrect; 2512810SN/A /** Stat for number of times the RAS is used to get a target. */ 2522810SN/A Stats::Scalar usedRAS; 2532810SN/A /** Stat for number of times the RAS is incorrect. */ 2548833Sdam.sunwoo@arm.com Stats::Scalar RASIncorrect; 2558833Sdam.sunwoo@arm.com}; 2568833Sdam.sunwoo@arm.com 2572810SN/A#endif // __CPU_O3_BPRED_UNIT_HH__ 2582810SN/A