bpred_unit.hh revision 13957
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"
5511433Smitch.hayenga@arm.com#include "cpu/pred/indirect.hh"
566226SN/A#include "cpu/pred/ras.hh"
578229SN/A#include "cpu/inst_seq.hh"
589480Snilay@cs.wisc.edu#include "cpu/static_inst.hh"
599480Snilay@cs.wisc.edu#include "params/BranchPredictor.hh"
6010462SAndreas.Sandberg@ARM.com#include "sim/probe/pmu.hh"
619480Snilay@cs.wisc.edu#include "sim/sim_object.hh"
625529SN/A
631061SN/A/**
641061SN/A * Basically a wrapper class to hold both the branch predictor
652329SN/A * and the BTB.
661061SN/A */
679480Snilay@cs.wisc.educlass BPredUnit : public SimObject
681061SN/A{
692345SN/A  public:
709480Snilay@cs.wisc.edu      typedef BranchPredictorParams Params;
712292SN/A    /**
722292SN/A     * @param params The params object, that has the size of the BP and BTB.
732292SN/A     */
749480Snilay@cs.wisc.edu    BPredUnit(const Params *p);
756005SN/A
762292SN/A    /**
772292SN/A     * Registers statistics.
782292SN/A     */
7911169Sandreas.hansson@arm.com    void regStats() override;
801062SN/A
8111168Sandreas.hansson@arm.com    void regProbePoints() override;
8210462SAndreas.Sandberg@ARM.com
839444SN/A    /** Perform sanity checks after a drain. */
849444SN/A    void drainSanityCheck() const;
851062SN/A
862292SN/A    /**
872292SN/A     * Predicts whether or not the instruction is a taken branch, and the
882292SN/A     * target of the branch if it is taken.
892292SN/A     * @param inst The branch instruction.
902292SN/A     * @param PC The predicted PC is passed back through this parameter.
912292SN/A     * @param tid The thread id.
922292SN/A     * @return Returns if the branch is taken or not.
932292SN/A     */
9410417Sandreas.hansson@arm.com    bool predict(const StaticInstPtr &inst, const InstSeqNum &seqNum,
959480Snilay@cs.wisc.edu                 TheISA::PCState &pc, ThreadID tid);
961684SN/A
972345SN/A    // @todo: Rename this function.
9811434Smitch.hayenga@arm.com    virtual void uncondBranch(ThreadID tid, Addr pc, void * &bp_history) = 0;
992345SN/A
1002292SN/A    /**
1012292SN/A     * Tells the branch predictor to commit any updates until the given
1022292SN/A     * sequence number.
1032292SN/A     * @param done_sn The sequence number to commit any older updates up until.
1042292SN/A     * @param tid The thread id.
1052292SN/A     */
1066221SN/A    void update(const InstSeqNum &done_sn, ThreadID tid);
1072165SN/A
1082292SN/A    /**
1092292SN/A     * Squashes all outstanding updates until a given sequence number.
1102292SN/A     * @param squashed_sn The sequence number to squash any younger updates up
1112292SN/A     * until.
1122292SN/A     * @param tid The thread id.
1132292SN/A     */
1146221SN/A    void squash(const InstSeqNum &squashed_sn, ThreadID tid);
1152165SN/A
1162292SN/A    /**
1172292SN/A     * Squashes all outstanding updates until a given sequence number, and
1182292SN/A     * corrects that sn's update with the proper address and taken/not taken.
1192292SN/A     * @param squashed_sn The sequence number to squash any younger updates up
1202292SN/A     * until.
1212292SN/A     * @param corr_target The correct branch target.
1222292SN/A     * @param actually_taken The correct branch direction.
1232292SN/A     * @param tid The thread id.
1242292SN/A     */
1257720SN/A    void squash(const InstSeqNum &squashed_sn,
1267720SN/A                const TheISA::PCState &corr_target,
1276221SN/A                bool actually_taken, ThreadID tid);
1281062SN/A
1292292SN/A    /**
1302345SN/A     * @param bp_history Pointer to the history object.  The predictor
1312345SN/A     * will need to update any state and delete the object.
1322345SN/A     */
13311434Smitch.hayenga@arm.com    virtual void squash(ThreadID tid, void *bp_history) = 0;
1342345SN/A
1352345SN/A    /**
1362292SN/A     * Looks up a given PC in the BP to see if it is taken or not taken.
1372292SN/A     * @param inst_PC The PC to look up.
1382345SN/A     * @param bp_history Pointer that will be set to an object that
1392345SN/A     * has the branch predictor state associated with the lookup.
1402292SN/A     * @return Whether the branch is taken or not taken.
1412292SN/A     */
14211434Smitch.hayenga@arm.com    virtual bool lookup(ThreadID tid, Addr instPC, void * &bp_history) = 0;
1431061SN/A
1448842SN/A     /**
1458842SN/A     * If a branch is not taken, because the BTB address is invalid or missing,
1468842SN/A     * this function sets the appropriate counter in the global and local
1478842SN/A     * predictors to not taken.
1488842SN/A     * @param inst_PC The PC to look up the local predictor.
1498842SN/A     * @param bp_history Pointer that will be set to an object that
1508842SN/A     * has the branch predictor state associated with the lookup.
1518842SN/A     */
15211434Smitch.hayenga@arm.com    virtual void btbUpdate(ThreadID tid, Addr instPC, void * &bp_history) = 0;
1538842SN/A
1542292SN/A    /**
1552292SN/A     * Looks up a given PC in the BTB to see if a matching entry exists.
1562292SN/A     * @param inst_PC The PC to look up.
1572292SN/A     * @return Whether the BTB contains the given PC.
1582292SN/A     */
1597720SN/A    bool BTBValid(Addr instPC)
1607720SN/A    { return BTB.valid(instPC, 0); }
1611061SN/A
1622292SN/A    /**
1632292SN/A     * Looks up a given PC in the BTB to get the predicted target.
1642292SN/A     * @param inst_PC The PC to look up.
1652292SN/A     * @return The address of the target of the branch.
1662292SN/A     */
1677720SN/A    TheISA::PCState BTBLookup(Addr instPC)
1687720SN/A    { return BTB.lookup(instPC, 0); }
1691061SN/A
1702292SN/A    /**
1712292SN/A     * Updates the BP with taken/not taken information.
1722292SN/A     * @param inst_PC The branch's PC that will be updated.
1732292SN/A     * @param taken Whether the branch was taken or not taken.
1742345SN/A     * @param bp_history Pointer to the branch predictor state that is
1752345SN/A     * associated with the branch lookup that is being updated.
1768842SN/A     * @param squashed Set to true when this function is called during a
1778842SN/A     * squash operation.
17813626Sjairo.balart@metempsy.com     * @param inst Static instruction information
17913626Sjairo.balart@metempsy.com     * @param corrTarget The resolved target of the branch (only needed
18013626Sjairo.balart@metempsy.com     * for squashed branches)
1812292SN/A     * @todo Make this update flexible enough to handle a global predictor.
1822292SN/A     */
18311434Smitch.hayenga@arm.com    virtual void update(ThreadID tid, Addr instPC, bool taken,
18413626Sjairo.balart@metempsy.com                   void *bp_history, bool squashed,
18513626Sjairo.balart@metempsy.com                   const StaticInstPtr & inst = StaticInst::nullStaticInstPtr,
18613626Sjairo.balart@metempsy.com                   Addr corrTarget = MaxAddr) = 0;
1872292SN/A    /**
1882292SN/A     * Updates the BTB with the target of a branch.
1892292SN/A     * @param inst_PC The branch's PC that will be updated.
1902292SN/A     * @param target_PC The branch's target that will be added to the BTB.
1912292SN/A     */
1927720SN/A    void BTBUpdate(Addr instPC, const TheISA::PCState &target)
1937720SN/A    { BTB.update(instPC, target, 0); }
1941061SN/A
19511433Smitch.hayenga@arm.com
1962345SN/A    void dump();
1972345SN/A
1981061SN/A  private:
1991062SN/A    struct PredictorHistory {
2002292SN/A        /**
2012345SN/A         * Makes a predictor history struct that contains any
2022345SN/A         * information needed to update the predictor, BTB, and RAS.
2032292SN/A         */
2047720SN/A        PredictorHistory(const InstSeqNum &seq_num, Addr instPC,
2056221SN/A                         bool pred_taken, void *bp_history,
20613654Sjairo.balart@metempsy.com                         void *indirect_history, ThreadID _tid,
20713654Sjairo.balart@metempsy.com                         const StaticInstPtr & inst)
20813654Sjairo.balart@metempsy.com            : seqNum(seq_num), pc(instPC), bpHistory(bp_history),
20913654Sjairo.balart@metempsy.com              indirectHistory(indirect_history), RASTarget(0), RASIndex(0),
21013654Sjairo.balart@metempsy.com              tid(_tid), predTaken(pred_taken), usedRAS(0), pushedRAS(0),
21113654Sjairo.balart@metempsy.com              wasCall(0), wasReturn(0), wasIndirect(0), target(MaxAddr),
21213654Sjairo.balart@metempsy.com              inst(inst)
2137720SN/A        {}
2141062SN/A
2156036SN/A        bool operator==(const PredictorHistory &entry) const {
2166036SN/A            return this->seqNum == entry.seqNum;
2176036SN/A        }
2186036SN/A
2192292SN/A        /** The sequence number for the predictor history entry. */
2201062SN/A        InstSeqNum seqNum;
2211062SN/A
2222292SN/A        /** The PC associated with the sequence number. */
2237720SN/A        Addr pc;
2241062SN/A
2259046SN/A        /** Pointer to the history object passed back from the branch
2269046SN/A         * predictor.  It is used to update or restore state of the
2279046SN/A         * branch predictor.
2289046SN/A         */
2299046SN/A        void *bpHistory;
2309046SN/A
23113654Sjairo.balart@metempsy.com        void *indirectHistory;
23213654Sjairo.balart@metempsy.com
2332292SN/A        /** The RAS target (only valid if a return). */
2347720SN/A        TheISA::PCState RASTarget;
2352292SN/A
2362292SN/A        /** The RAS index of the instruction (only valid if a call). */
2372292SN/A        unsigned RASIndex;
2382292SN/A
2392292SN/A        /** The thread id. */
2406221SN/A        ThreadID tid;
2412292SN/A
2422292SN/A        /** Whether or not it was predicted taken. */
2431062SN/A        bool predTaken;
2441062SN/A
2452292SN/A        /** Whether or not the RAS was used. */
2461062SN/A        bool usedRAS;
2471062SN/A
24810330Smitch.hayenga@arm.com        /* Whether or not the RAS was pushed */
2499078SN/A        bool pushedRAS;
2509078SN/A
2512292SN/A        /** Whether or not the instruction was a call. */
2521062SN/A        bool wasCall;
2532345SN/A
2548843SN/A        /** Whether or not the instruction was a return. */
2558843SN/A        bool wasReturn;
25610330Smitch.hayenga@arm.com
25711433Smitch.hayenga@arm.com        /** Wether this instruction was an indirect branch */
25811433Smitch.hayenga@arm.com        bool wasIndirect;
25913626Sjairo.balart@metempsy.com
26013626Sjairo.balart@metempsy.com        /** Target of the branch. First it is predicted, and fixed later
26113626Sjairo.balart@metempsy.com         *  if necessary
26213626Sjairo.balart@metempsy.com         */
26313626Sjairo.balart@metempsy.com        Addr target;
26413626Sjairo.balart@metempsy.com
26513626Sjairo.balart@metempsy.com        /** The branch instrction */
26613626Sjairo.balart@metempsy.com        const StaticInstPtr inst;
2671062SN/A    };
2681062SN/A
26910273Sandreas.hansson@arm.com    typedef std::deque<PredictorHistory> History;
2709480Snilay@cs.wisc.edu
2719480Snilay@cs.wisc.edu    /** Number of the threads for which the branch history is maintained. */
27210785Sgope@wisc.edu    const unsigned numThreads;
27310785Sgope@wisc.edu
2741061SN/A
2752292SN/A    /**
2762292SN/A     * The per-thread predictor history. This is used to update the predictor
2772292SN/A     * as instructions are committed, or restore it to the proper state after
2782292SN/A     * a squash.
2792292SN/A     */
28010273Sandreas.hansson@arm.com    std::vector<History> predHist;
2811061SN/A
2822292SN/A    /** The BTB. */
2831061SN/A    DefaultBTB BTB;
2841061SN/A
2852292SN/A    /** The per-thread return address stack. */
28610273Sandreas.hansson@arm.com    std::vector<ReturnAddrStack> RAS;
2871062SN/A
28811433Smitch.hayenga@arm.com    /** The indirect target predictor. */
28913957Sjairo.balart@metempsy.com    IndirectPredictor * iPred;
29011433Smitch.hayenga@arm.com
2912292SN/A    /** Stat for number of BP lookups. */
2925999SN/A    Stats::Scalar lookups;
2932292SN/A    /** Stat for number of conditional branches predicted. */
2945999SN/A    Stats::Scalar condPredicted;
2952292SN/A    /** Stat for number of conditional branches predicted incorrectly. */
2965999SN/A    Stats::Scalar condIncorrect;
2972292SN/A    /** Stat for number of BTB lookups. */
2985999SN/A    Stats::Scalar BTBLookups;
2992292SN/A    /** Stat for number of BTB hits. */
3005999SN/A    Stats::Scalar BTBHits;
3012292SN/A    /** Stat for number of times the BTB is correct. */
3025999SN/A    Stats::Scalar BTBCorrect;
3039480Snilay@cs.wisc.edu    /** Stat for percent times an entry in BTB found. */
3049480Snilay@cs.wisc.edu    Stats::Formula BTBHitPct;
3052292SN/A    /** Stat for number of times the RAS is used to get a target. */
3065999SN/A    Stats::Scalar usedRAS;
3072292SN/A    /** Stat for number of times the RAS is incorrect. */
3085999SN/A    Stats::Scalar RASIncorrect;
30910462SAndreas.Sandberg@ARM.com
31011433Smitch.hayenga@arm.com    /** Stat for the number of indirect target lookups.*/
31111433Smitch.hayenga@arm.com    Stats::Scalar indirectLookups;
31211433Smitch.hayenga@arm.com    /** Stat for the number of indirect target hits.*/
31311433Smitch.hayenga@arm.com    Stats::Scalar indirectHits;
31411433Smitch.hayenga@arm.com    /** Stat for the number of indirect target misses.*/
31511433Smitch.hayenga@arm.com    Stats::Scalar indirectMisses;
31611433Smitch.hayenga@arm.com    /** Stat for the number of indirect target mispredictions.*/
31711433Smitch.hayenga@arm.com    Stats::Scalar indirectMispredicted;
31811433Smitch.hayenga@arm.com
31910462SAndreas.Sandberg@ARM.com  protected:
32010785Sgope@wisc.edu    /** Number of bits to shift instructions by for predictor addresses. */
32110785Sgope@wisc.edu    const unsigned instShiftAmt;
32210785Sgope@wisc.edu
32310462SAndreas.Sandberg@ARM.com    /**
32410462SAndreas.Sandberg@ARM.com     * @{
32510462SAndreas.Sandberg@ARM.com     * @name PMU Probe points.
32610462SAndreas.Sandberg@ARM.com     */
32710462SAndreas.Sandberg@ARM.com
32810462SAndreas.Sandberg@ARM.com    /**
32910462SAndreas.Sandberg@ARM.com     * Helper method to instantiate probe points belonging to this
33010462SAndreas.Sandberg@ARM.com     * object.
33110462SAndreas.Sandberg@ARM.com     *
33210462SAndreas.Sandberg@ARM.com     * @param name Name of the probe point.
33310462SAndreas.Sandberg@ARM.com     * @return A unique_ptr to the new probe point.
33410462SAndreas.Sandberg@ARM.com     */
33510462SAndreas.Sandberg@ARM.com    ProbePoints::PMUUPtr pmuProbePoint(const char *name);
33610462SAndreas.Sandberg@ARM.com
33710462SAndreas.Sandberg@ARM.com
33810462SAndreas.Sandberg@ARM.com    /**
33910462SAndreas.Sandberg@ARM.com     * Branches seen by the branch predictor
34010462SAndreas.Sandberg@ARM.com     *
34110462SAndreas.Sandberg@ARM.com     * @note This counter includes speculative branches.
34210462SAndreas.Sandberg@ARM.com     */
34310462SAndreas.Sandberg@ARM.com    ProbePoints::PMUUPtr ppBranches;
34410462SAndreas.Sandberg@ARM.com
34510462SAndreas.Sandberg@ARM.com    /** Miss-predicted branches */
34610462SAndreas.Sandberg@ARM.com    ProbePoints::PMUUPtr ppMisses;
34710462SAndreas.Sandberg@ARM.com
34810462SAndreas.Sandberg@ARM.com    /** @} */
3491061SN/A};
3501061SN/A
3519480Snilay@cs.wisc.edu#endif // __CPU_PRED_BPRED_UNIT_HH__
352