bpred_unit.hh revision 9480
11689SN/A/*
29444SN/A * Copyright (c) 2011-2012 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
506216SN/A#include <list>
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,
939480Snilay@cs.wisc.edu                        int asid, TheISA::PCState &instPC, TheISA::PCState &predPC,
949480Snilay@cs.wisc.edu                        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;
1811061SN/A
1822292SN/A    /**
1832292SN/A     * Updates the BTB with the target of a branch.
1842292SN/A     * @param inst_PC The branch's PC that will be updated.
1852292SN/A     * @param target_PC The branch's target that will be added to the BTB.
1862292SN/A     */
1877720SN/A    void BTBUpdate(Addr instPC, const TheISA::PCState &target)
1887720SN/A    { BTB.update(instPC, target, 0); }
1891061SN/A
1902345SN/A    void dump();
1912345SN/A
1921061SN/A  private:
1931062SN/A    struct PredictorHistory {
1942292SN/A        /**
1952345SN/A         * Makes a predictor history struct that contains any
1962345SN/A         * information needed to update the predictor, BTB, and RAS.
1972292SN/A         */
1987720SN/A        PredictorHistory(const InstSeqNum &seq_num, Addr instPC,
1996221SN/A                         bool pred_taken, void *bp_history,
2006221SN/A                         ThreadID _tid)
2019046SN/A            : seqNum(seq_num), pc(instPC), bpHistory(bp_history), RASTarget(0),
2029078SN/A              RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0), pushedRAS(0),
2039480Snilay@cs.wisc.edu              wasCall(0), wasReturn(0)
2047720SN/A        {}
2051062SN/A
2066036SN/A        bool operator==(const PredictorHistory &entry) const {
2076036SN/A            return this->seqNum == entry.seqNum;
2086036SN/A        }
2096036SN/A
2102292SN/A        /** The sequence number for the predictor history entry. */
2111062SN/A        InstSeqNum seqNum;
2121062SN/A
2132292SN/A        /** The PC associated with the sequence number. */
2147720SN/A        Addr pc;
2151062SN/A
2169046SN/A        /** Pointer to the history object passed back from the branch
2179046SN/A         * predictor.  It is used to update or restore state of the
2189046SN/A         * branch predictor.
2199046SN/A         */
2209046SN/A        void *bpHistory;
2219046SN/A
2222292SN/A        /** The RAS target (only valid if a return). */
2237720SN/A        TheISA::PCState RASTarget;
2242292SN/A
2252292SN/A        /** The RAS index of the instruction (only valid if a call). */
2262292SN/A        unsigned RASIndex;
2272292SN/A
2282292SN/A        /** The thread id. */
2296221SN/A        ThreadID tid;
2302292SN/A
2312292SN/A        /** Whether or not it was predicted taken. */
2321062SN/A        bool predTaken;
2331062SN/A
2342292SN/A        /** Whether or not the RAS was used. */
2351062SN/A        bool usedRAS;
2361062SN/A
2379078SN/A        /* Wether or not the RAS was pushed */
2389078SN/A        bool pushedRAS;
2399078SN/A
2402292SN/A        /** Whether or not the instruction was a call. */
2411062SN/A        bool wasCall;
2422345SN/A
2438843SN/A        /** Whether or not the instruction was a return. */
2448843SN/A        bool wasReturn;
2451062SN/A    };
2461062SN/A
2472292SN/A    typedef std::list<PredictorHistory> History;
2489480Snilay@cs.wisc.edu    typedef History::iterator HistoryIt;
2499480Snilay@cs.wisc.edu
2509480Snilay@cs.wisc.edu    /** Number of the threads for which the branch history is maintained. */
2519480Snilay@cs.wisc.edu    uint32_t numThreads;
2521061SN/A
2532292SN/A    /**
2542292SN/A     * The per-thread predictor history. This is used to update the predictor
2552292SN/A     * as instructions are committed, or restore it to the proper state after
2562292SN/A     * a squash.
2572292SN/A     */
2589480Snilay@cs.wisc.edu    History *predHist;
2591061SN/A
2602292SN/A    /** The BTB. */
2611061SN/A    DefaultBTB BTB;
2621061SN/A
2632292SN/A    /** The per-thread return address stack. */
2649480Snilay@cs.wisc.edu    ReturnAddrStack *RAS;
2651062SN/A
2662292SN/A    /** Stat for number of BP lookups. */
2675999SN/A    Stats::Scalar lookups;
2682292SN/A    /** Stat for number of conditional branches predicted. */
2695999SN/A    Stats::Scalar condPredicted;
2702292SN/A    /** Stat for number of conditional branches predicted incorrectly. */
2715999SN/A    Stats::Scalar condIncorrect;
2722292SN/A    /** Stat for number of BTB lookups. */
2735999SN/A    Stats::Scalar BTBLookups;
2742292SN/A    /** Stat for number of BTB hits. */
2755999SN/A    Stats::Scalar BTBHits;
2762292SN/A    /** Stat for number of times the BTB is correct. */
2775999SN/A    Stats::Scalar BTBCorrect;
2789480Snilay@cs.wisc.edu    /** Stat for percent times an entry in BTB found. */
2799480Snilay@cs.wisc.edu    Stats::Formula BTBHitPct;
2802292SN/A    /** Stat for number of times the RAS is used to get a target. */
2815999SN/A    Stats::Scalar usedRAS;
2822292SN/A    /** Stat for number of times the RAS is incorrect. */
2835999SN/A    Stats::Scalar RASIncorrect;
2841061SN/A};
2851061SN/A
2869480Snilay@cs.wisc.edu#endif // __CPU_PRED_BPRED_UNIT_HH__
287