bpred_unit.hh revision 6005
12889Sbinkertn@umich.edu/*
22889Sbinkertn@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
32889Sbinkertn@umich.edu * All rights reserved.
42889Sbinkertn@umich.edu *
52889Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
62889Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
72889Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
82889Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
92889Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
102889Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
112889Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
122889Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
132889Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
142889Sbinkertn@umich.edu * this software without specific prior written permission.
152889Sbinkertn@umich.edu *
162889Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172889Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182889Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192889Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202889Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212889Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222889Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232889Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242889Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252889Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262889Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272889Sbinkertn@umich.edu *
282889Sbinkertn@umich.edu * Authors: Kevin Lim
294850Snate@binkert.org */
304850Snate@binkert.org
314850Snate@binkert.org#ifndef __CPU_O3_BPRED_UNIT_HH__
324850Snate@binkert.org#define __CPU_O3_BPRED_UNIT_HH__
334850Snate@binkert.org
344850Snate@binkert.org#include "base/statistics.hh"
355467Snate@binkert.org#include "cpu/inst_seq.hh"
365471Snate@binkert.org
375470Snate@binkert.org#include "cpu/o3/2bit_local_pred.hh"
382889Sbinkertn@umich.edu#include "cpu/o3/btb.hh"
392889Sbinkertn@umich.edu#include "cpu/o3/ras.hh"
402889Sbinkertn@umich.edu#include "cpu/o3/tournament_pred.hh"
415470Snate@binkert.org
425470Snate@binkert.org#include "sim/host.hh"
435470Snate@binkert.org
445470Snate@binkert.org#include <list>
455470Snate@binkert.org
465470Snate@binkert.orgclass DerivO3CPUParams;
475470Snate@binkert.org
482889Sbinkertn@umich.edu/**
495470Snate@binkert.org * Basically a wrapper class to hold both the branch predictor
505470Snate@binkert.org * and the BTB.
515470Snate@binkert.org */
525470Snate@binkert.orgtemplate<class Impl>
535470Snate@binkert.orgclass BPredUnit
542889Sbinkertn@umich.edu{
552889Sbinkertn@umich.edu  private:
562889Sbinkertn@umich.edu    typedef typename Impl::DynInstPtr DynInstPtr;
572889Sbinkertn@umich.edu
584850Snate@binkert.org    enum PredType {
594850Snate@binkert.org        Local,
602889Sbinkertn@umich.edu        Tournament
612889Sbinkertn@umich.edu    };
622889Sbinkertn@umich.edu
632889Sbinkertn@umich.edu    PredType predictor;
642889Sbinkertn@umich.edu
652889Sbinkertn@umich.edu    const std::string _name;
662889Sbinkertn@umich.edu
672889Sbinkertn@umich.edu  public:
685773Snate@binkert.org
692889Sbinkertn@umich.edu    /**
705524Sstever@gmail.com     * @param params The params object, that has the size of the BP and BTB.
715524Sstever@gmail.com     */
725524Sstever@gmail.com    BPredUnit(DerivO3CPUParams *params);
735524Sstever@gmail.com
745524Sstever@gmail.com    const std::string &name() const { return _name; }
755524Sstever@gmail.com
765524Sstever@gmail.com    /**
775524Sstever@gmail.com     * Registers statistics.
782889Sbinkertn@umich.edu     */
792889Sbinkertn@umich.edu    void regStats();
802899Sbinkertn@umich.edu
812899Sbinkertn@umich.edu    void switchOut();
822889Sbinkertn@umich.edu
832889Sbinkertn@umich.edu    void takeOverFrom();
842889Sbinkertn@umich.edu
852889Sbinkertn@umich.edu    /**
862889Sbinkertn@umich.edu     * Predicts whether or not the instruction is a taken branch, and the
872889Sbinkertn@umich.edu     * target of the branch if it is taken.
882889Sbinkertn@umich.edu     * @param inst The branch instruction.
892889Sbinkertn@umich.edu     * @param PC The predicted PC is passed back through this parameter.
902889Sbinkertn@umich.edu     * @param tid The thread id.
915773Snate@binkert.org     * @return Returns if the branch is taken or not.
922889Sbinkertn@umich.edu     */
932889Sbinkertn@umich.edu    bool predict(DynInstPtr &inst, Addr &PC, unsigned tid);
945773Snate@binkert.org
955773Snate@binkert.org    // @todo: Rename this function.
965773Snate@binkert.org    void BPUncond(void * &bp_history);
975773Snate@binkert.org
985773Snate@binkert.org    /**
992889Sbinkertn@umich.edu     * Tells the branch predictor to commit any updates until the given
1002889Sbinkertn@umich.edu     * sequence number.
1012889Sbinkertn@umich.edu     * @param done_sn The sequence number to commit any older updates up until.
1022889Sbinkertn@umich.edu     * @param tid The thread id.
1035512SMichael.Adler@intel.com     */
1045512SMichael.Adler@intel.com    void update(const InstSeqNum &done_sn, unsigned tid);
1052889Sbinkertn@umich.edu
1062889Sbinkertn@umich.edu    /**
1072889Sbinkertn@umich.edu     * Squashes all outstanding updates until a given sequence number.
1084053Sbinkertn@umich.edu     * @param squashed_sn The sequence number to squash any younger updates up
1094053Sbinkertn@umich.edu     * until.
1102889Sbinkertn@umich.edu     * @param tid The thread id.
1114053Sbinkertn@umich.edu     */
1124044Sbinkertn@umich.edu    void squash(const InstSeqNum &squashed_sn, unsigned tid);
1134044Sbinkertn@umich.edu
1142889Sbinkertn@umich.edu    /**
1152889Sbinkertn@umich.edu     * Squashes all outstanding updates until a given sequence number, and
1162889Sbinkertn@umich.edu     * corrects that sn's update with the proper address and taken/not taken.
1172889Sbinkertn@umich.edu     * @param squashed_sn The sequence number to squash any younger updates up
1182889Sbinkertn@umich.edu     * until.
1195473Snate@binkert.org     * @param corr_target The correct branch target.
1205473Snate@binkert.org     * @param actually_taken The correct branch direction.
1215473Snate@binkert.org     * @param tid The thread id.
1225473Snate@binkert.org     */
1235473Snate@binkert.org    void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
1246171Snate@binkert.org                bool actually_taken, unsigned tid);
1256171Snate@binkert.org
1266171Snate@binkert.org    /**
1276171Snate@binkert.org     * @param bp_history Pointer to the history object.  The predictor
1286171Snate@binkert.org     * will need to update any state and delete the object.
1296171Snate@binkert.org     */
1306171Snate@binkert.org    void BPSquash(void *bp_history);
1316171Snate@binkert.org
1326171Snate@binkert.org    /**
1332889Sbinkertn@umich.edu     * Looks up a given PC in the BP to see if it is taken or not taken.
1345801Snate@binkert.org     * @param inst_PC The PC to look up.
1355801Snate@binkert.org     * @param bp_history Pointer that will be set to an object that
1365801Snate@binkert.org     * has the branch predictor state associated with the lookup.
1374167Sbinkertn@umich.edu     * @return Whether the branch is taken or not taken.
1384042Sbinkertn@umich.edu     */
1395801Snate@binkert.org    bool BPLookup(Addr &inst_PC, void * &bp_history);
1405799Snate@binkert.org
1415799Snate@binkert.org    /**
1425799Snate@binkert.org     * Looks up a given PC in the BTB to see if a matching entry exists.
1435799Snate@binkert.org     * @param inst_PC The PC to look up.
1445799Snate@binkert.org     * @return Whether the BTB contains the given PC.
1455799Snate@binkert.org     */
1465802Snate@binkert.org    bool BTBValid(Addr &inst_PC)
1472889Sbinkertn@umich.edu    { return BTB.valid(inst_PC, 0); }
1485524Sstever@gmail.com
1495524Sstever@gmail.com    /**
1505524Sstever@gmail.com     * Looks up a given PC in the BTB to get the predicted target.
1515524Sstever@gmail.com     * @param inst_PC The PC to look up.
1525524Sstever@gmail.com     * @return The address of the target of the branch.
1535524Sstever@gmail.com     */
1545524Sstever@gmail.com    Addr BTBLookup(Addr &inst_PC)
1555524Sstever@gmail.com    { return BTB.lookup(inst_PC, 0); }
1565524Sstever@gmail.com
1575524Sstever@gmail.com    /**
1585524Sstever@gmail.com     * Updates the BP with taken/not taken information.
1595524Sstever@gmail.com     * @param inst_PC The branch's PC that will be updated.
1605524Sstever@gmail.com     * @param taken Whether the branch was taken or not taken.
1615524Sstever@gmail.com     * @param bp_history Pointer to the branch predictor state that is
1625524Sstever@gmail.com     * associated with the branch lookup that is being updated.
1635524Sstever@gmail.com     * @todo Make this update flexible enough to handle a global predictor.
1645524Sstever@gmail.com     */
1655524Sstever@gmail.com    void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
1665524Sstever@gmail.com
1675524Sstever@gmail.com    /**
1685524Sstever@gmail.com     * Updates the BTB with the target of a branch.
1695524Sstever@gmail.com     * @param inst_PC The branch's PC that will be updated.
1705524Sstever@gmail.com     * @param target_PC The branch's target that will be added to the BTB.
1715524Sstever@gmail.com     */
1725524Sstever@gmail.com    void BTBUpdate(Addr &inst_PC, Addr &target_PC)
1735524Sstever@gmail.com    { BTB.update(inst_PC, target_PC,0); }
1745524Sstever@gmail.com
1752889Sbinkertn@umich.edu    void dump();
1764850Snate@binkert.org
1774850Snate@binkert.org  private:
1784850Snate@binkert.org    struct PredictorHistory {
1794850Snate@binkert.org        /**
1804850Snate@binkert.org         * Makes a predictor history struct that contains any
1815801Snate@binkert.org         * information needed to update the predictor, BTB, and RAS.
1825824Ssaidi@eecs.umich.edu         */
1834850Snate@binkert.org        PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
1845801Snate@binkert.org                         const bool pred_taken, void *bp_history,
1854850Snate@binkert.org                         const unsigned _tid)
1864850Snate@binkert.org            : seqNum(seq_num), PC(inst_PC), RASTarget(0),
1875801Snate@binkert.org              RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
1884850Snate@binkert.org              wasCall(0), bpHistory(bp_history)
1894850Snate@binkert.org        { }
1904850Snate@binkert.org
1912889Sbinkertn@umich.edu        /** The sequence number for the predictor history entry. */
1922889Sbinkertn@umich.edu        InstSeqNum seqNum;
1932889Sbinkertn@umich.edu
1942889Sbinkertn@umich.edu        /** The PC associated with the sequence number. */
1952889Sbinkertn@umich.edu        Addr PC;
1962889Sbinkertn@umich.edu
1972889Sbinkertn@umich.edu        /** The RAS target (only valid if a return). */
1982889Sbinkertn@umich.edu        Addr RASTarget;
1992889Sbinkertn@umich.edu
2002889Sbinkertn@umich.edu        /** The RAS index of the instruction (only valid if a call). */
2012889Sbinkertn@umich.edu        unsigned RASIndex;
2022889Sbinkertn@umich.edu
2032889Sbinkertn@umich.edu        /** The thread id. */
2042889Sbinkertn@umich.edu        unsigned tid;
2052889Sbinkertn@umich.edu
2062889Sbinkertn@umich.edu        /** Whether or not it was predicted taken. */
2072889Sbinkertn@umich.edu        bool predTaken;
2082889Sbinkertn@umich.edu
2092889Sbinkertn@umich.edu        /** Whether or not the RAS was used. */
2102889Sbinkertn@umich.edu        bool usedRAS;
2112889Sbinkertn@umich.edu
2122889Sbinkertn@umich.edu        /** Whether or not the instruction was a call. */
2132889Sbinkertn@umich.edu        bool wasCall;
2142889Sbinkertn@umich.edu
2152889Sbinkertn@umich.edu        /** Pointer to the history object passed back from the branch
2162889Sbinkertn@umich.edu         * predictor.  It is used to update or restore state of the
2174053Sbinkertn@umich.edu         * branch predictor.
2184053Sbinkertn@umich.edu         */
2195799Snate@binkert.org        void *bpHistory;
2205799Snate@binkert.org    };
2214053Sbinkertn@umich.edu
2225473Snate@binkert.org    typedef std::list<PredictorHistory> History;
2235473Snate@binkert.org
2245473Snate@binkert.org    /**
2255473Snate@binkert.org     * The per-thread predictor history. This is used to update the predictor
2265473Snate@binkert.org     * as instructions are committed, or restore it to the proper state after
2275473Snate@binkert.org     * a squash.
2285473Snate@binkert.org     */
2295473Snate@binkert.org    History predHist[Impl::MaxThreads];
2305473Snate@binkert.org
2315473Snate@binkert.org    /** The local branch predictor. */
2325473Snate@binkert.org    LocalBP *localBP;
2335473Snate@binkert.org
2345473Snate@binkert.org    /** The tournament branch predictor. */
2355473Snate@binkert.org    TournamentBP *tournamentBP;
2365473Snate@binkert.org
2375473Snate@binkert.org    /** The BTB. */
2385473Snate@binkert.org    DefaultBTB BTB;
2395473Snate@binkert.org
2405473Snate@binkert.org    /** The per-thread return address stack. */
2415473Snate@binkert.org    ReturnAddrStack RAS[Impl::MaxThreads];
2425473Snate@binkert.org
2432889Sbinkertn@umich.edu    /** Stat for number of BP lookups. */
2442889Sbinkertn@umich.edu    Stats::Scalar lookups;
2452889Sbinkertn@umich.edu    /** Stat for number of conditional branches predicted. */
2465470Snate@binkert.org    Stats::Scalar condPredicted;
2475470Snate@binkert.org    /** Stat for number of conditional branches predicted incorrectly. */
2485470Snate@binkert.org    Stats::Scalar condIncorrect;
2495470Snate@binkert.org    /** Stat for number of BTB lookups. */
2505470Snate@binkert.org    Stats::Scalar BTBLookups;
2512889Sbinkertn@umich.edu    /** Stat for number of BTB hits. */
2522889Sbinkertn@umich.edu    Stats::Scalar BTBHits;
2532889Sbinkertn@umich.edu    /** Stat for number of times the BTB is correct. */
2542889Sbinkertn@umich.edu    Stats::Scalar BTBCorrect;
2555801Snate@binkert.org    /** Stat for number of times the RAS is used to get a target. */
2565801Snate@binkert.org    Stats::Scalar usedRAS;
2575824Ssaidi@eecs.umich.edu    /** Stat for number of times the RAS is incorrect. */
2585456Ssaidi@eecs.umich.edu    Stats::Scalar RASIncorrect;
2595528Sstever@gmail.com};
2605528Sstever@gmail.com
2615528Sstever@gmail.com#endif // __CPU_O3_BPRED_UNIT_HH__
2622967Sktlim@umich.edu