bpred_unit.hh revision 10330
11689SN/A/* 210273Sandreas.hansson@arm.com * Copyright (c) 2011-2012, 2014 ARM Limited 39480Snilay@cs.wisc.edu * Copyright (c) 2010 The University of Edinburgh 48843SN/A * All rights reserved 58843SN/A * 68843SN/A * The license below extends only to copyright in the software and shall 78843SN/A * not be construed as granting a license to any other intellectual 88843SN/A * property including but not limited to intellectual property relating 98843SN/A * to a hardware implementation of the functionality of the software 108843SN/A * licensed hereunder. You may use the software subject to the license 118843SN/A * terms below provided that you ensure that this notice is replicated 128843SN/A * unmodified and in its entirety in all distributions of the software, 138843SN/A * modified or unmodified, in source code or in binary form. 148843SN/A * 151689SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan 161689SN/A * All rights reserved. 171689SN/A * 181689SN/A * Redistribution and use in source and binary forms, with or without 191689SN/A * modification, are permitted provided that the following conditions are 201689SN/A * met: redistributions of source code must retain the above copyright 211689SN/A * notice, this list of conditions and the following disclaimer; 221689SN/A * redistributions in binary form must reproduce the above copyright 231689SN/A * notice, this list of conditions and the following disclaimer in the 241689SN/A * documentation and/or other materials provided with the distribution; 251689SN/A * neither the name of the copyright holders nor the names of its 261689SN/A * contributors may be used to endorse or promote products derived from 271689SN/A * this software without specific prior written permission. 281689SN/A * 291689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 301689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 311689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 321689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 331689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 341689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 351689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 361689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 371689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 381689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 391689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665SN/A * 412665SN/A * Authors: Kevin Lim 429480Snilay@cs.wisc.edu * Korey Sewell 439480Snilay@cs.wisc.edu * Timothy M. Jones 449480Snilay@cs.wisc.edu * Nilay Vaish 451689SN/A */ 461061SN/A 479480Snilay@cs.wisc.edu#ifndef __CPU_PRED_BPRED_UNIT_HH__ 489480Snilay@cs.wisc.edu#define __CPU_PRED_BPRED_UNIT_HH__ 491061SN/A 5010273Sandreas.hansson@arm.com#include <deque> 516216SN/A 521062SN/A#include "base/statistics.hh" 536216SN/A#include "base/types.hh" 546226SN/A#include "cpu/pred/btb.hh" 556226SN/A#include "cpu/pred/ras.hh" 568229SN/A#include "cpu/inst_seq.hh" 579480Snilay@cs.wisc.edu#include "cpu/static_inst.hh" 589480Snilay@cs.wisc.edu#include "params/BranchPredictor.hh" 599480Snilay@cs.wisc.edu#include "sim/sim_object.hh" 605529SN/A 611061SN/A/** 621061SN/A * Basically a wrapper class to hold both the branch predictor 632329SN/A * and the BTB. 641061SN/A */ 659480Snilay@cs.wisc.educlass BPredUnit : public SimObject 661061SN/A{ 672345SN/A public: 689480Snilay@cs.wisc.edu typedef BranchPredictorParams Params; 692292SN/A /** 702292SN/A * @param params The params object, that has the size of the BP and BTB. 712292SN/A */ 729480Snilay@cs.wisc.edu BPredUnit(const Params *p); 736005SN/A 742292SN/A /** 752292SN/A * Registers statistics. 762292SN/A */ 771062SN/A void regStats(); 781062SN/A 799444SN/A /** Perform sanity checks after a drain. */ 809444SN/A void drainSanityCheck() const; 811062SN/A 822292SN/A /** 832292SN/A * Predicts whether or not the instruction is a taken branch, and the 842292SN/A * target of the branch if it is taken. 852292SN/A * @param inst The branch instruction. 862292SN/A * @param PC The predicted PC is passed back through this parameter. 872292SN/A * @param tid The thread id. 882292SN/A * @return Returns if the branch is taken or not. 892292SN/A */ 909480Snilay@cs.wisc.edu bool predict(StaticInstPtr &inst, const InstSeqNum &seqNum, 919480Snilay@cs.wisc.edu TheISA::PCState &pc, ThreadID tid); 929480Snilay@cs.wisc.edu bool predictInOrder(StaticInstPtr &inst, const InstSeqNum &seqNum, 9310244Satgutier@umich.edu int asid, TheISA::PCState &instPC, 9410244Satgutier@umich.edu TheISA::PCState &predPC, ThreadID tid); 951684SN/A 962345SN/A // @todo: Rename this function. 979480Snilay@cs.wisc.edu virtual void uncondBranch(void * &bp_history) = 0; 982345SN/A 992292SN/A /** 1002292SN/A * Tells the branch predictor to commit any updates until the given 1012292SN/A * sequence number. 1022292SN/A * @param done_sn The sequence number to commit any older updates up until. 1032292SN/A * @param tid The thread id. 1042292SN/A */ 1056221SN/A void update(const InstSeqNum &done_sn, ThreadID tid); 1062165SN/A 1072292SN/A /** 1082292SN/A * Squashes all outstanding updates until a given sequence number. 1092292SN/A * @param squashed_sn The sequence number to squash any younger updates up 1102292SN/A * until. 1112292SN/A * @param tid The thread id. 1122292SN/A */ 1136221SN/A void squash(const InstSeqNum &squashed_sn, ThreadID tid); 1142165SN/A 1152292SN/A /** 1162292SN/A * Squashes all outstanding updates until a given sequence number, and 1172292SN/A * corrects that sn's update with the proper address and taken/not taken. 1182292SN/A * @param squashed_sn The sequence number to squash any younger updates up 1192292SN/A * until. 1202292SN/A * @param corr_target The correct branch target. 1212292SN/A * @param actually_taken The correct branch direction. 1222292SN/A * @param tid The thread id. 1232292SN/A */ 1247720SN/A void squash(const InstSeqNum &squashed_sn, 1257720SN/A const TheISA::PCState &corr_target, 1266221SN/A bool actually_taken, ThreadID tid); 1271062SN/A 1282292SN/A /** 1292345SN/A * @param bp_history Pointer to the history object. The predictor 1302345SN/A * will need to update any state and delete the object. 1312345SN/A */ 1329480Snilay@cs.wisc.edu virtual void squash(void *bp_history) = 0; 1332345SN/A 1342345SN/A /** 1352292SN/A * Looks up a given PC in the BP to see if it is taken or not taken. 1362292SN/A * @param inst_PC The PC to look up. 1372345SN/A * @param bp_history Pointer that will be set to an object that 1382345SN/A * has the branch predictor state associated with the lookup. 1392292SN/A * @return Whether the branch is taken or not taken. 1402292SN/A */ 1419480Snilay@cs.wisc.edu virtual bool lookup(Addr instPC, void * &bp_history) = 0; 1421061SN/A 1438842SN/A /** 1448842SN/A * If a branch is not taken, because the BTB address is invalid or missing, 1458842SN/A * this function sets the appropriate counter in the global and local 1468842SN/A * predictors to not taken. 1478842SN/A * @param inst_PC The PC to look up the local predictor. 1488842SN/A * @param bp_history Pointer that will be set to an object that 1498842SN/A * has the branch predictor state associated with the lookup. 1508842SN/A */ 1519480Snilay@cs.wisc.edu virtual void btbUpdate(Addr instPC, void * &bp_history) = 0; 1528842SN/A 1532292SN/A /** 1542292SN/A * Looks up a given PC in the BTB to see if a matching entry exists. 1552292SN/A * @param inst_PC The PC to look up. 1562292SN/A * @return Whether the BTB contains the given PC. 1572292SN/A */ 1587720SN/A bool BTBValid(Addr instPC) 1597720SN/A { return BTB.valid(instPC, 0); } 1601061SN/A 1612292SN/A /** 1622292SN/A * Looks up a given PC in the BTB to get the predicted target. 1632292SN/A * @param inst_PC The PC to look up. 1642292SN/A * @return The address of the target of the branch. 1652292SN/A */ 1667720SN/A TheISA::PCState BTBLookup(Addr instPC) 1677720SN/A { return BTB.lookup(instPC, 0); } 1681061SN/A 1692292SN/A /** 1702292SN/A * Updates the BP with taken/not taken information. 1712292SN/A * @param inst_PC The branch's PC that will be updated. 1722292SN/A * @param taken Whether the branch was taken or not taken. 1732345SN/A * @param bp_history Pointer to the branch predictor state that is 1742345SN/A * associated with the branch lookup that is being updated. 1758842SN/A * @param squashed Set to true when this function is called during a 1768842SN/A * squash operation. 1772292SN/A * @todo Make this update flexible enough to handle a global predictor. 1782292SN/A */ 1799480Snilay@cs.wisc.edu virtual void update(Addr instPC, bool taken, void *bp_history, 1809480Snilay@cs.wisc.edu bool squashed) = 0; 18110330Smitch.hayenga@arm.com /** 18210330Smitch.hayenga@arm.com * Deletes the associated history with a branch, performs no predictor 18310330Smitch.hayenga@arm.com * updates. Used for branches that mispredict and update tables but 18410330Smitch.hayenga@arm.com * are still speculative and later retire. 18510330Smitch.hayenga@arm.com * @param bp_history History to delete associated with this predictor 18610330Smitch.hayenga@arm.com */ 18710330Smitch.hayenga@arm.com virtual void retireSquashed(void *bp_history) = 0; 1881061SN/A 1892292SN/A /** 1902292SN/A * Updates the BTB with the target of a branch. 1912292SN/A * @param inst_PC The branch's PC that will be updated. 1922292SN/A * @param target_PC The branch's target that will be added to the BTB. 1932292SN/A */ 1947720SN/A void BTBUpdate(Addr instPC, const TheISA::PCState &target) 1957720SN/A { BTB.update(instPC, target, 0); } 1961061SN/A 1972345SN/A void dump(); 1982345SN/A 1991061SN/A private: 2001062SN/A struct PredictorHistory { 2012292SN/A /** 2022345SN/A * Makes a predictor history struct that contains any 2032345SN/A * information needed to update the predictor, BTB, and RAS. 2042292SN/A */ 2057720SN/A PredictorHistory(const InstSeqNum &seq_num, Addr instPC, 2066221SN/A bool pred_taken, void *bp_history, 2076221SN/A ThreadID _tid) 2089046SN/A : seqNum(seq_num), pc(instPC), bpHistory(bp_history), RASTarget(0), 2099078SN/A RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0), pushedRAS(0), 21010330Smitch.hayenga@arm.com wasCall(0), wasReturn(0), wasSquashed(0) 2117720SN/A {} 2121062SN/A 2136036SN/A bool operator==(const PredictorHistory &entry) const { 2146036SN/A return this->seqNum == entry.seqNum; 2156036SN/A } 2166036SN/A 2172292SN/A /** The sequence number for the predictor history entry. */ 2181062SN/A InstSeqNum seqNum; 2191062SN/A 2202292SN/A /** The PC associated with the sequence number. */ 2217720SN/A Addr pc; 2221062SN/A 2239046SN/A /** Pointer to the history object passed back from the branch 2249046SN/A * predictor. It is used to update or restore state of the 2259046SN/A * branch predictor. 2269046SN/A */ 2279046SN/A void *bpHistory; 2289046SN/A 2292292SN/A /** The RAS target (only valid if a return). */ 2307720SN/A TheISA::PCState RASTarget; 2312292SN/A 2322292SN/A /** The RAS index of the instruction (only valid if a call). */ 2332292SN/A unsigned RASIndex; 2342292SN/A 2352292SN/A /** The thread id. */ 2366221SN/A ThreadID tid; 2372292SN/A 2382292SN/A /** Whether or not it was predicted taken. */ 2391062SN/A bool predTaken; 2401062SN/A 2412292SN/A /** Whether or not the RAS was used. */ 2421062SN/A bool usedRAS; 2431062SN/A 24410330Smitch.hayenga@arm.com /* Whether or not the RAS was pushed */ 2459078SN/A bool pushedRAS; 2469078SN/A 2472292SN/A /** Whether or not the instruction was a call. */ 2481062SN/A bool wasCall; 2492345SN/A 2508843SN/A /** Whether or not the instruction was a return. */ 2518843SN/A bool wasReturn; 25210330Smitch.hayenga@arm.com 25310330Smitch.hayenga@arm.com /** Whether this instruction has already mispredicted/updated bp */ 25410330Smitch.hayenga@arm.com bool wasSquashed; 2551062SN/A }; 2561062SN/A 25710273Sandreas.hansson@arm.com typedef std::deque<PredictorHistory> History; 2589480Snilay@cs.wisc.edu 2599480Snilay@cs.wisc.edu /** Number of the threads for which the branch history is maintained. */ 2609480Snilay@cs.wisc.edu uint32_t numThreads; 2611061SN/A 2622292SN/A /** 2632292SN/A * The per-thread predictor history. This is used to update the predictor 2642292SN/A * as instructions are committed, or restore it to the proper state after 2652292SN/A * a squash. 2662292SN/A */ 26710273Sandreas.hansson@arm.com std::vector<History> predHist; 2681061SN/A 2692292SN/A /** The BTB. */ 2701061SN/A DefaultBTB BTB; 2711061SN/A 2722292SN/A /** The per-thread return address stack. */ 27310273Sandreas.hansson@arm.com std::vector<ReturnAddrStack> RAS; 2741062SN/A 2752292SN/A /** Stat for number of BP lookups. */ 2765999SN/A Stats::Scalar lookups; 2772292SN/A /** Stat for number of conditional branches predicted. */ 2785999SN/A Stats::Scalar condPredicted; 2792292SN/A /** Stat for number of conditional branches predicted incorrectly. */ 2805999SN/A Stats::Scalar condIncorrect; 2812292SN/A /** Stat for number of BTB lookups. */ 2825999SN/A Stats::Scalar BTBLookups; 2832292SN/A /** Stat for number of BTB hits. */ 2845999SN/A Stats::Scalar BTBHits; 2852292SN/A /** Stat for number of times the BTB is correct. */ 2865999SN/A Stats::Scalar BTBCorrect; 2879480Snilay@cs.wisc.edu /** Stat for percent times an entry in BTB found. */ 2889480Snilay@cs.wisc.edu Stats::Formula BTBHitPct; 2892292SN/A /** Stat for number of times the RAS is used to get a target. */ 2905999SN/A Stats::Scalar usedRAS; 2912292SN/A /** Stat for number of times the RAS is incorrect. */ 2925999SN/A Stats::Scalar RASIncorrect; 2931061SN/A}; 2941061SN/A 2959480Snilay@cs.wisc.edu#endif // __CPU_PRED_BPRED_UNIT_HH__ 296