bpred_unit.hh revision 6216
14120Sgblack@eecs.umich.edu/* 24120Sgblack@eecs.umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan 34120Sgblack@eecs.umich.edu * All rights reserved. 44120Sgblack@eecs.umich.edu * 57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without 67087Snate@binkert.org * modification, are permitted provided that the following conditions are 77087Snate@binkert.org * met: redistributions of source code must retain the above copyright 87087Snate@binkert.org * notice, this list of conditions and the following disclaimer; 97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright 107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 117087Snate@binkert.org * documentation and/or other materials provided with the distribution; 127087Snate@binkert.org * neither the name of the copyright holders nor the names of its 134120Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 147087Snate@binkert.org * this software without specific prior written permission. 157087Snate@binkert.org * 167087Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 177087Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 187087Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 197087Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 207087Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 217087Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224120Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237087Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244120Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254120Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 264120Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274120Sgblack@eecs.umich.edu * 284120Sgblack@eecs.umich.edu * Authors: Kevin Lim 294120Sgblack@eecs.umich.edu */ 304120Sgblack@eecs.umich.edu 314120Sgblack@eecs.umich.edu#ifndef __CPU_O3_BPRED_UNIT_HH__ 324120Sgblack@eecs.umich.edu#define __CPU_O3_BPRED_UNIT_HH__ 334120Sgblack@eecs.umich.edu 344120Sgblack@eecs.umich.edu#include <list> 354120Sgblack@eecs.umich.edu 364120Sgblack@eecs.umich.edu#include "base/statistics.hh" 374120Sgblack@eecs.umich.edu#include "base/types.hh" 384120Sgblack@eecs.umich.edu#include "cpu/inst_seq.hh" 394120Sgblack@eecs.umich.edu#include "cpu/o3/2bit_local_pred.hh" 404120Sgblack@eecs.umich.edu#include "cpu/o3/btb.hh" 414120Sgblack@eecs.umich.edu#include "cpu/o3/ras.hh" 424120Sgblack@eecs.umich.edu#include "cpu/o3/tournament_pred.hh" 438229Snate@binkert.org 448229Snate@binkert.orgclass DerivO3CPUParams; 455857Sgblack@eecs.umich.edu 464139Sgblack@eecs.umich.edu/** 474135Sgblack@eecs.umich.edu * Basically a wrapper class to hold both the branch predictor 486023Snate@binkert.org * and the BTB. 494120Sgblack@eecs.umich.edu */ 504120Sgblack@eecs.umich.edutemplate<class Impl> 514120Sgblack@eecs.umich.educlass BPredUnit 525114Sgblack@eecs.umich.edu{ 535114Sgblack@eecs.umich.edu private: 544135Sgblack@eecs.umich.edu typedef typename Impl::DynInstPtr DynInstPtr; 554365Sgblack@eecs.umich.edu 565114Sgblack@eecs.umich.edu enum PredType { 575114Sgblack@eecs.umich.edu Local, 585851Sgblack@eecs.umich.edu Tournament 595139Sgblack@eecs.umich.edu }; 605114Sgblack@eecs.umich.edu 615139Sgblack@eecs.umich.edu PredType predictor; 626009Snate@binkert.org 636009Snate@binkert.org const std::string _name; 646009Snate@binkert.org 655114Sgblack@eecs.umich.edu public: 665114Sgblack@eecs.umich.edu 675114Sgblack@eecs.umich.edu /** 684729Sgblack@eecs.umich.edu * @param params The params object, that has the size of the BP and BTB. 694365Sgblack@eecs.umich.edu */ 705114Sgblack@eecs.umich.edu BPredUnit(DerivO3CPUParams *params); 714365Sgblack@eecs.umich.edu 724365Sgblack@eecs.umich.edu const std::string &name() const { return _name; } 735114Sgblack@eecs.umich.edu 745114Sgblack@eecs.umich.edu /** 755114Sgblack@eecs.umich.edu * Registers statistics. 765114Sgblack@eecs.umich.edu */ 775114Sgblack@eecs.umich.edu void regStats(); 785114Sgblack@eecs.umich.edu 795114Sgblack@eecs.umich.edu void switchOut(); 805114Sgblack@eecs.umich.edu 815114Sgblack@eecs.umich.edu void takeOverFrom(); 825684Sgblack@eecs.umich.edu 835858Sgblack@eecs.umich.edu /** 845684Sgblack@eecs.umich.edu * Predicts whether or not the instruction is a taken branch, and the 855858Sgblack@eecs.umich.edu * target of the branch if it is taken. 865684Sgblack@eecs.umich.edu * @param inst The branch instruction. 875858Sgblack@eecs.umich.edu * @param PC The predicted PC is passed back through this parameter. 885858Sgblack@eecs.umich.edu * @param tid The thread id. 897678Sgblack@eecs.umich.edu * @return Returns if the branch is taken or not. 907678Sgblack@eecs.umich.edu */ 915909Sgblack@eecs.umich.edu bool predict(DynInstPtr &inst, Addr &PC, unsigned tid); 925909Sgblack@eecs.umich.edu 935858Sgblack@eecs.umich.edu // @todo: Rename this function. 945114Sgblack@eecs.umich.edu void BPUncond(void * &bp_history); 955114Sgblack@eecs.umich.edu 965114Sgblack@eecs.umich.edu /** 975114Sgblack@eecs.umich.edu * Tells the branch predictor to commit any updates until the given 985114Sgblack@eecs.umich.edu * sequence number. 995114Sgblack@eecs.umich.edu * @param done_sn The sequence number to commit any older updates up until. 1005114Sgblack@eecs.umich.edu * @param tid The thread id. 1015139Sgblack@eecs.umich.edu */ 1026009Snate@binkert.org void update(const InstSeqNum &done_sn, unsigned tid); 1036009Snate@binkert.org 1045114Sgblack@eecs.umich.edu /** 1055114Sgblack@eecs.umich.edu * Squashes all outstanding updates until a given sequence number. 1065114Sgblack@eecs.umich.edu * @param squashed_sn The sequence number to squash any younger updates up 1075114Sgblack@eecs.umich.edu * until. 1085114Sgblack@eecs.umich.edu * @param tid The thread id. 1095114Sgblack@eecs.umich.edu */ 1105114Sgblack@eecs.umich.edu void squash(const InstSeqNum &squashed_sn, unsigned tid); 1115114Sgblack@eecs.umich.edu 1125139Sgblack@eecs.umich.edu /** 1136009Snate@binkert.org * Squashes all outstanding updates until a given sequence number, and 1146009Snate@binkert.org * corrects that sn's update with the proper address and taken/not taken. 1155114Sgblack@eecs.umich.edu * @param squashed_sn The sequence number to squash any younger updates up 1165114Sgblack@eecs.umich.edu * until. 1175114Sgblack@eecs.umich.edu * @param corr_target The correct branch target. 1187678Sgblack@eecs.umich.edu * @param actually_taken The correct branch direction. 1197678Sgblack@eecs.umich.edu * @param tid The thread id. 1205114Sgblack@eecs.umich.edu */ 1215114Sgblack@eecs.umich.edu void squash(const InstSeqNum &squashed_sn, const Addr &corr_target, 1225114Sgblack@eecs.umich.edu bool actually_taken, unsigned tid); 1235114Sgblack@eecs.umich.edu 1245114Sgblack@eecs.umich.edu /** 1255114Sgblack@eecs.umich.edu * @param bp_history Pointer to the history object. The predictor 1265114Sgblack@eecs.umich.edu * will need to update any state and delete the object. 1275139Sgblack@eecs.umich.edu */ 1286009Snate@binkert.org void BPSquash(void *bp_history); 1296009Snate@binkert.org 1305114Sgblack@eecs.umich.edu /** 1315114Sgblack@eecs.umich.edu * Looks up a given PC in the BP to see if it is taken or not taken. 1325114Sgblack@eecs.umich.edu * @param inst_PC The PC to look up. 1337678Sgblack@eecs.umich.edu * @param bp_history Pointer that will be set to an object that 1347678Sgblack@eecs.umich.edu * has the branch predictor state associated with the lookup. 1355114Sgblack@eecs.umich.edu * @return Whether the branch is taken or not taken. 1365114Sgblack@eecs.umich.edu */ 1375114Sgblack@eecs.umich.edu bool BPLookup(Addr &inst_PC, void * &bp_history); 1385114Sgblack@eecs.umich.edu 1395114Sgblack@eecs.umich.edu /** 1405114Sgblack@eecs.umich.edu * Looks up a given PC in the BTB to see if a matching entry exists. 1415114Sgblack@eecs.umich.edu * @param inst_PC The PC to look up. 1425851Sgblack@eecs.umich.edu * @return Whether the BTB contains the given PC. 1436009Snate@binkert.org */ 1446009Snate@binkert.org bool BTBValid(Addr &inst_PC) 1455114Sgblack@eecs.umich.edu { return BTB.valid(inst_PC, 0); } 1464135Sgblack@eecs.umich.edu 1474150Sgblack@eecs.umich.edu /** 1484365Sgblack@eecs.umich.edu * Looks up a given PC in the BTB to get the predicted target. 1494365Sgblack@eecs.umich.edu * @param inst_PC The PC to look up. 1504365Sgblack@eecs.umich.edu * @return The address of the target of the branch. 1514729Sgblack@eecs.umich.edu */ 1524365Sgblack@eecs.umich.edu Addr BTBLookup(Addr &inst_PC) 1534365Sgblack@eecs.umich.edu { return BTB.lookup(inst_PC, 0); } 1544365Sgblack@eecs.umich.edu 1554365Sgblack@eecs.umich.edu /** 1567678Sgblack@eecs.umich.edu * Updates the BP with taken/not taken information. 1577678Sgblack@eecs.umich.edu * @param inst_PC The branch's PC that will be updated. 1584365Sgblack@eecs.umich.edu * @param taken Whether the branch was taken or not taken. 1594365Sgblack@eecs.umich.edu * @param bp_history Pointer to the branch predictor state that is 1604365Sgblack@eecs.umich.edu * associated with the branch lookup that is being updated. 1614365Sgblack@eecs.umich.edu * @todo Make this update flexible enough to handle a global predictor. 1624365Sgblack@eecs.umich.edu */ 1635114Sgblack@eecs.umich.edu void BPUpdate(Addr &inst_PC, bool taken, void *bp_history); 1645114Sgblack@eecs.umich.edu 1655114Sgblack@eecs.umich.edu /** 1665114Sgblack@eecs.umich.edu * Updates the BTB with the target of a branch. 1675114Sgblack@eecs.umich.edu * @param inst_PC The branch's PC that will be updated. 1685114Sgblack@eecs.umich.edu * @param target_PC The branch's target that will be added to the BTB. 1695114Sgblack@eecs.umich.edu */ 1705114Sgblack@eecs.umich.edu void BTBUpdate(Addr &inst_PC, Addr &target_PC) 1715114Sgblack@eecs.umich.edu { BTB.update(inst_PC, target_PC,0); } 1725114Sgblack@eecs.umich.edu 1735114Sgblack@eecs.umich.edu void dump(); 1745114Sgblack@eecs.umich.edu 1755114Sgblack@eecs.umich.edu private: 1765114Sgblack@eecs.umich.edu struct PredictorHistory { 1775114Sgblack@eecs.umich.edu /** 1785114Sgblack@eecs.umich.edu * Makes a predictor history struct that contains any 1795114Sgblack@eecs.umich.edu * information needed to update the predictor, BTB, and RAS. 1805114Sgblack@eecs.umich.edu */ 1815114Sgblack@eecs.umich.edu PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC, 1825114Sgblack@eecs.umich.edu const bool pred_taken, void *bp_history, 1835114Sgblack@eecs.umich.edu const unsigned _tid) 1845114Sgblack@eecs.umich.edu : seqNum(seq_num), PC(inst_PC), RASTarget(0), 1855114Sgblack@eecs.umich.edu RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0), 1865114Sgblack@eecs.umich.edu wasCall(0), bpHistory(bp_history) 1875114Sgblack@eecs.umich.edu { } 1885114Sgblack@eecs.umich.edu 1895114Sgblack@eecs.umich.edu bool operator==(const PredictorHistory &entry) const { 1905114Sgblack@eecs.umich.edu return this->seqNum == entry.seqNum; 1915114Sgblack@eecs.umich.edu } 1925114Sgblack@eecs.umich.edu 1935114Sgblack@eecs.umich.edu /** The sequence number for the predictor history entry. */ 1945114Sgblack@eecs.umich.edu InstSeqNum seqNum; 1955114Sgblack@eecs.umich.edu 1965114Sgblack@eecs.umich.edu /** The PC associated with the sequence number. */ 1975114Sgblack@eecs.umich.edu Addr PC; 1985851Sgblack@eecs.umich.edu 1995114Sgblack@eecs.umich.edu /** The RAS target (only valid if a return). */ 2005114Sgblack@eecs.umich.edu Addr RASTarget; 2015114Sgblack@eecs.umich.edu 2025114Sgblack@eecs.umich.edu /** The RAS index of the instruction (only valid if a call). */ 2035114Sgblack@eecs.umich.edu unsigned RASIndex; 2045114Sgblack@eecs.umich.edu 2055114Sgblack@eecs.umich.edu /** The thread id. */ 2065851Sgblack@eecs.umich.edu unsigned tid; 2075114Sgblack@eecs.umich.edu 2085114Sgblack@eecs.umich.edu /** Whether or not it was predicted taken. */ 2095114Sgblack@eecs.umich.edu bool predTaken; 2105114Sgblack@eecs.umich.edu 2115114Sgblack@eecs.umich.edu /** Whether or not the RAS was used. */ 2125114Sgblack@eecs.umich.edu bool usedRAS; 2135655Sgblack@eecs.umich.edu 2145851Sgblack@eecs.umich.edu /** Whether or not the instruction was a call. */ 2155114Sgblack@eecs.umich.edu bool wasCall; 2165114Sgblack@eecs.umich.edu 2175114Sgblack@eecs.umich.edu /** Pointer to the history object passed back from the branch 2185114Sgblack@eecs.umich.edu * predictor. It is used to update or restore state of the 2195114Sgblack@eecs.umich.edu * branch predictor. 2205114Sgblack@eecs.umich.edu */ 2215114Sgblack@eecs.umich.edu void *bpHistory; 2225851Sgblack@eecs.umich.edu }; 2235114Sgblack@eecs.umich.edu 2245114Sgblack@eecs.umich.edu typedef std::list<PredictorHistory> History; 2255114Sgblack@eecs.umich.edu typedef typename History::iterator HistoryIt; 2265114Sgblack@eecs.umich.edu 2275114Sgblack@eecs.umich.edu /** 2285114Sgblack@eecs.umich.edu * The per-thread predictor history. This is used to update the predictor 2295114Sgblack@eecs.umich.edu * as instructions are committed, or restore it to the proper state after 2305851Sgblack@eecs.umich.edu * a squash. 2315114Sgblack@eecs.umich.edu */ 2325114Sgblack@eecs.umich.edu History predHist[Impl::MaxThreads]; 2335114Sgblack@eecs.umich.edu 2345114Sgblack@eecs.umich.edu /** The local branch predictor. */ 2355114Sgblack@eecs.umich.edu LocalBP *localBP; 2365114Sgblack@eecs.umich.edu 2375114Sgblack@eecs.umich.edu /** The tournament branch predictor. */ 2385851Sgblack@eecs.umich.edu TournamentBP *tournamentBP; 2395114Sgblack@eecs.umich.edu 2405114Sgblack@eecs.umich.edu /** The BTB. */ 2415114Sgblack@eecs.umich.edu DefaultBTB BTB; 2425114Sgblack@eecs.umich.edu 2435114Sgblack@eecs.umich.edu /** The per-thread return address stack. */ 2445114Sgblack@eecs.umich.edu ReturnAddrStack RAS[Impl::MaxThreads]; 2455114Sgblack@eecs.umich.edu 2465851Sgblack@eecs.umich.edu /** Stat for number of BP lookups. */ 2475114Sgblack@eecs.umich.edu Stats::Scalar lookups; 2487681Sgblack@eecs.umich.edu /** Stat for number of conditional branches predicted. */ 2497681Sgblack@eecs.umich.edu Stats::Scalar condPredicted; 2507681Sgblack@eecs.umich.edu /** Stat for number of conditional branches predicted incorrectly. */ 2517681Sgblack@eecs.umich.edu Stats::Scalar condIncorrect; 2527681Sgblack@eecs.umich.edu /** Stat for number of BTB lookups. */ 2535114Sgblack@eecs.umich.edu Stats::Scalar BTBLookups; 2545114Sgblack@eecs.umich.edu /** Stat for number of BTB hits. */ 2555114Sgblack@eecs.umich.edu Stats::Scalar BTBHits; 2565114Sgblack@eecs.umich.edu /** Stat for number of times the BTB is correct. */ 2575114Sgblack@eecs.umich.edu Stats::Scalar BTBCorrect; 2585114Sgblack@eecs.umich.edu /** Stat for number of times the RAS is used to get a target. */ 2595851Sgblack@eecs.umich.edu Stats::Scalar usedRAS; 2605114Sgblack@eecs.umich.edu /** Stat for number of times the RAS is incorrect. */ 2615114Sgblack@eecs.umich.edu Stats::Scalar RASIncorrect; 2625114Sgblack@eecs.umich.edu}; 2635114Sgblack@eecs.umich.edu 2645114Sgblack@eecs.umich.edu#endif // __CPU_O3_BPRED_UNIT_HH__ 2655114Sgblack@eecs.umich.edu