bpred_unit.hh revision 11169
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"
5910462SAndreas.Sandberg@ARM.com#include "sim/probe/pmu.hh"
609480Snilay@cs.wisc.edu#include "sim/sim_object.hh"
615529SN/A
621061SN/A/**
631061SN/A * Basically a wrapper class to hold both the branch predictor
642329SN/A * and the BTB.
651061SN/A */
669480Snilay@cs.wisc.educlass BPredUnit : public SimObject
671061SN/A{
682345SN/A  public:
699480Snilay@cs.wisc.edu      typedef BranchPredictorParams Params;
702292SN/A    /**
712292SN/A     * @param params The params object, that has the size of the BP and BTB.
722292SN/A     */
739480Snilay@cs.wisc.edu    BPredUnit(const Params *p);
746005SN/A
752292SN/A    /**
762292SN/A     * Registers statistics.
772292SN/A     */
7811169Sandreas.hansson@arm.com    void regStats() override;
791062SN/A
8011168Sandreas.hansson@arm.com    void regProbePoints() override;
8110462SAndreas.Sandberg@ARM.com
829444SN/A    /** Perform sanity checks after a drain. */
839444SN/A    void drainSanityCheck() const;
841062SN/A
852292SN/A    /**
862292SN/A     * Predicts whether or not the instruction is a taken branch, and the
872292SN/A     * target of the branch if it is taken.
882292SN/A     * @param inst The branch instruction.
892292SN/A     * @param PC The predicted PC is passed back through this parameter.
902292SN/A     * @param tid The thread id.
912292SN/A     * @return Returns if the branch is taken or not.
922292SN/A     */
9310417Sandreas.hansson@arm.com    bool predict(const StaticInstPtr &inst, const InstSeqNum &seqNum,
949480Snilay@cs.wisc.edu                 TheISA::PCState &pc, ThreadID tid);
9510417Sandreas.hansson@arm.com    bool predictInOrder(const StaticInstPtr &inst, const InstSeqNum &seqNum,
9610244Satgutier@umich.edu                        int asid, TheISA::PCState &instPC,
9710244Satgutier@umich.edu                        TheISA::PCState &predPC, ThreadID tid);
981684SN/A
992345SN/A    // @todo: Rename this function.
10010785Sgope@wisc.edu    virtual void uncondBranch(Addr pc, void * &bp_history) = 0;
1012345SN/A
1022292SN/A    /**
1032292SN/A     * Tells the branch predictor to commit any updates until the given
1042292SN/A     * sequence number.
1052292SN/A     * @param done_sn The sequence number to commit any older updates up until.
1062292SN/A     * @param tid The thread id.
1072292SN/A     */
1086221SN/A    void update(const InstSeqNum &done_sn, ThreadID tid);
1092165SN/A
1102292SN/A    /**
1112292SN/A     * Squashes all outstanding updates until a given sequence number.
1122292SN/A     * @param squashed_sn The sequence number to squash any younger updates up
1132292SN/A     * until.
1142292SN/A     * @param tid The thread id.
1152292SN/A     */
1166221SN/A    void squash(const InstSeqNum &squashed_sn, ThreadID tid);
1172165SN/A
1182292SN/A    /**
1192292SN/A     * Squashes all outstanding updates until a given sequence number, and
1202292SN/A     * corrects that sn's update with the proper address and taken/not taken.
1212292SN/A     * @param squashed_sn The sequence number to squash any younger updates up
1222292SN/A     * until.
1232292SN/A     * @param corr_target The correct branch target.
1242292SN/A     * @param actually_taken The correct branch direction.
1252292SN/A     * @param tid The thread id.
1262292SN/A     */
1277720SN/A    void squash(const InstSeqNum &squashed_sn,
1287720SN/A                const TheISA::PCState &corr_target,
1296221SN/A                bool actually_taken, ThreadID tid);
1301062SN/A
1312292SN/A    /**
1322345SN/A     * @param bp_history Pointer to the history object.  The predictor
1332345SN/A     * will need to update any state and delete the object.
1342345SN/A     */
1359480Snilay@cs.wisc.edu    virtual void squash(void *bp_history) = 0;
1362345SN/A
1372345SN/A    /**
1382292SN/A     * Looks up a given PC in the BP to see if it is taken or not taken.
1392292SN/A     * @param inst_PC The PC to look up.
1402345SN/A     * @param bp_history Pointer that will be set to an object that
1412345SN/A     * has the branch predictor state associated with the lookup.
1422292SN/A     * @return Whether the branch is taken or not taken.
1432292SN/A     */
1449480Snilay@cs.wisc.edu    virtual bool lookup(Addr instPC, void * &bp_history) = 0;
1451061SN/A
1468842SN/A     /**
1478842SN/A     * If a branch is not taken, because the BTB address is invalid or missing,
1488842SN/A     * this function sets the appropriate counter in the global and local
1498842SN/A     * predictors to not taken.
1508842SN/A     * @param inst_PC The PC to look up the local predictor.
1518842SN/A     * @param bp_history Pointer that will be set to an object that
1528842SN/A     * has the branch predictor state associated with the lookup.
1538842SN/A     */
1549480Snilay@cs.wisc.edu    virtual void btbUpdate(Addr instPC, void * &bp_history) = 0;
1558842SN/A
1562292SN/A    /**
1572292SN/A     * Looks up a given PC in the BTB to see if a matching entry exists.
1582292SN/A     * @param inst_PC The PC to look up.
1592292SN/A     * @return Whether the BTB contains the given PC.
1602292SN/A     */
1617720SN/A    bool BTBValid(Addr instPC)
1627720SN/A    { return BTB.valid(instPC, 0); }
1631061SN/A
1642292SN/A    /**
1652292SN/A     * Looks up a given PC in the BTB to get the predicted target.
1662292SN/A     * @param inst_PC The PC to look up.
1672292SN/A     * @return The address of the target of the branch.
1682292SN/A     */
1697720SN/A    TheISA::PCState BTBLookup(Addr instPC)
1707720SN/A    { return BTB.lookup(instPC, 0); }
1711061SN/A
1722292SN/A    /**
1732292SN/A     * Updates the BP with taken/not taken information.
1742292SN/A     * @param inst_PC The branch's PC that will be updated.
1752292SN/A     * @param taken Whether the branch was taken or not taken.
1762345SN/A     * @param bp_history Pointer to the branch predictor state that is
1772345SN/A     * associated with the branch lookup that is being updated.
1788842SN/A     * @param squashed Set to true when this function is called during a
1798842SN/A     * squash operation.
1802292SN/A     * @todo Make this update flexible enough to handle a global predictor.
1812292SN/A     */
1829480Snilay@cs.wisc.edu    virtual void update(Addr instPC, bool taken, void *bp_history,
1839480Snilay@cs.wisc.edu                        bool squashed) = 0;
18410330Smitch.hayenga@arm.com     /**
18510330Smitch.hayenga@arm.com     * Deletes the associated history with a branch, performs no predictor
18610330Smitch.hayenga@arm.com     * updates.  Used for branches that mispredict and update tables but
18710330Smitch.hayenga@arm.com     * are still speculative and later retire.
18810330Smitch.hayenga@arm.com     * @param bp_history History to delete associated with this predictor
18910330Smitch.hayenga@arm.com     */
19010330Smitch.hayenga@arm.com    virtual void retireSquashed(void *bp_history) = 0;
1911061SN/A
1922292SN/A    /**
1932292SN/A     * Updates the BTB with the target of a branch.
1942292SN/A     * @param inst_PC The branch's PC that will be updated.
1952292SN/A     * @param target_PC The branch's target that will be added to the BTB.
1962292SN/A     */
1977720SN/A    void BTBUpdate(Addr instPC, const TheISA::PCState &target)
1987720SN/A    { BTB.update(instPC, target, 0); }
1991061SN/A
2002345SN/A    void dump();
2012345SN/A
2021061SN/A  private:
2031062SN/A    struct PredictorHistory {
2042292SN/A        /**
2052345SN/A         * Makes a predictor history struct that contains any
2062345SN/A         * information needed to update the predictor, BTB, and RAS.
2072292SN/A         */
2087720SN/A        PredictorHistory(const InstSeqNum &seq_num, Addr instPC,
2096221SN/A                         bool pred_taken, void *bp_history,
2106221SN/A                         ThreadID _tid)
2119046SN/A            : seqNum(seq_num), pc(instPC), bpHistory(bp_history), RASTarget(0),
2129078SN/A              RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0), pushedRAS(0),
21310330Smitch.hayenga@arm.com              wasCall(0), wasReturn(0), wasSquashed(0)
2147720SN/A        {}
2151062SN/A
2166036SN/A        bool operator==(const PredictorHistory &entry) const {
2176036SN/A            return this->seqNum == entry.seqNum;
2186036SN/A        }
2196036SN/A
2202292SN/A        /** The sequence number for the predictor history entry. */
2211062SN/A        InstSeqNum seqNum;
2221062SN/A
2232292SN/A        /** The PC associated with the sequence number. */
2247720SN/A        Addr pc;
2251062SN/A
2269046SN/A        /** Pointer to the history object passed back from the branch
2279046SN/A         * predictor.  It is used to update or restore state of the
2289046SN/A         * branch predictor.
2299046SN/A         */
2309046SN/A        void *bpHistory;
2319046SN/A
2322292SN/A        /** The RAS target (only valid if a return). */
2337720SN/A        TheISA::PCState RASTarget;
2342292SN/A
2352292SN/A        /** The RAS index of the instruction (only valid if a call). */
2362292SN/A        unsigned RASIndex;
2372292SN/A
2382292SN/A        /** The thread id. */
2396221SN/A        ThreadID tid;
2402292SN/A
2412292SN/A        /** Whether or not it was predicted taken. */
2421062SN/A        bool predTaken;
2431062SN/A
2442292SN/A        /** Whether or not the RAS was used. */
2451062SN/A        bool usedRAS;
2461062SN/A
24710330Smitch.hayenga@arm.com        /* Whether or not the RAS was pushed */
2489078SN/A        bool pushedRAS;
2499078SN/A
2502292SN/A        /** Whether or not the instruction was a call. */
2511062SN/A        bool wasCall;
2522345SN/A
2538843SN/A        /** Whether or not the instruction was a return. */
2548843SN/A        bool wasReturn;
25510330Smitch.hayenga@arm.com
25610330Smitch.hayenga@arm.com        /** Whether this instruction has already mispredicted/updated bp */
25710330Smitch.hayenga@arm.com        bool wasSquashed;
2581062SN/A    };
2591062SN/A
26010273Sandreas.hansson@arm.com    typedef std::deque<PredictorHistory> History;
2619480Snilay@cs.wisc.edu
2629480Snilay@cs.wisc.edu    /** Number of the threads for which the branch history is maintained. */
26310785Sgope@wisc.edu    const unsigned numThreads;
26410785Sgope@wisc.edu
2651061SN/A
2662292SN/A    /**
2672292SN/A     * The per-thread predictor history. This is used to update the predictor
2682292SN/A     * as instructions are committed, or restore it to the proper state after
2692292SN/A     * a squash.
2702292SN/A     */
27110273Sandreas.hansson@arm.com    std::vector<History> predHist;
2721061SN/A
2732292SN/A    /** The BTB. */
2741061SN/A    DefaultBTB BTB;
2751061SN/A
2762292SN/A    /** The per-thread return address stack. */
27710273Sandreas.hansson@arm.com    std::vector<ReturnAddrStack> RAS;
2781062SN/A
2792292SN/A    /** Stat for number of BP lookups. */
2805999SN/A    Stats::Scalar lookups;
2812292SN/A    /** Stat for number of conditional branches predicted. */
2825999SN/A    Stats::Scalar condPredicted;
2832292SN/A    /** Stat for number of conditional branches predicted incorrectly. */
2845999SN/A    Stats::Scalar condIncorrect;
2852292SN/A    /** Stat for number of BTB lookups. */
2865999SN/A    Stats::Scalar BTBLookups;
2872292SN/A    /** Stat for number of BTB hits. */
2885999SN/A    Stats::Scalar BTBHits;
2892292SN/A    /** Stat for number of times the BTB is correct. */
2905999SN/A    Stats::Scalar BTBCorrect;
2919480Snilay@cs.wisc.edu    /** Stat for percent times an entry in BTB found. */
2929480Snilay@cs.wisc.edu    Stats::Formula BTBHitPct;
2932292SN/A    /** Stat for number of times the RAS is used to get a target. */
2945999SN/A    Stats::Scalar usedRAS;
2952292SN/A    /** Stat for number of times the RAS is incorrect. */
2965999SN/A    Stats::Scalar RASIncorrect;
29710462SAndreas.Sandberg@ARM.com
29810462SAndreas.Sandberg@ARM.com  protected:
29910785Sgope@wisc.edu    /** Number of bits to shift instructions by for predictor addresses. */
30010785Sgope@wisc.edu    const unsigned instShiftAmt;
30110785Sgope@wisc.edu
30210462SAndreas.Sandberg@ARM.com    /**
30310462SAndreas.Sandberg@ARM.com     * @{
30410462SAndreas.Sandberg@ARM.com     * @name PMU Probe points.
30510462SAndreas.Sandberg@ARM.com     */
30610462SAndreas.Sandberg@ARM.com
30710462SAndreas.Sandberg@ARM.com    /**
30810462SAndreas.Sandberg@ARM.com     * Helper method to instantiate probe points belonging to this
30910462SAndreas.Sandberg@ARM.com     * object.
31010462SAndreas.Sandberg@ARM.com     *
31110462SAndreas.Sandberg@ARM.com     * @param name Name of the probe point.
31210462SAndreas.Sandberg@ARM.com     * @return A unique_ptr to the new probe point.
31310462SAndreas.Sandberg@ARM.com     */
31410462SAndreas.Sandberg@ARM.com    ProbePoints::PMUUPtr pmuProbePoint(const char *name);
31510462SAndreas.Sandberg@ARM.com
31610462SAndreas.Sandberg@ARM.com
31710462SAndreas.Sandberg@ARM.com    /**
31810462SAndreas.Sandberg@ARM.com     * Branches seen by the branch predictor
31910462SAndreas.Sandberg@ARM.com     *
32010462SAndreas.Sandberg@ARM.com     * @note This counter includes speculative branches.
32110462SAndreas.Sandberg@ARM.com     */
32210462SAndreas.Sandberg@ARM.com    ProbePoints::PMUUPtr ppBranches;
32310462SAndreas.Sandberg@ARM.com
32410462SAndreas.Sandberg@ARM.com    /** Miss-predicted branches */
32510462SAndreas.Sandberg@ARM.com    ProbePoints::PMUUPtr ppMisses;
32610462SAndreas.Sandberg@ARM.com
32710462SAndreas.Sandberg@ARM.com    /** @} */
3281061SN/A};
3291061SN/A
3309480Snilay@cs.wisc.edu#endif // __CPU_PRED_BPRED_UNIT_HH__
331