bpred_unit.hh revision 5999
1768SN/A/*
21762SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
3768SN/A * All rights reserved.
4768SN/A *
5768SN/A * Redistribution and use in source and binary forms, with or without
6768SN/A * modification, are permitted provided that the following conditions are
7768SN/A * met: redistributions of source code must retain the above copyright
8768SN/A * notice, this list of conditions and the following disclaimer;
9768SN/A * redistributions in binary form must reproduce the above copyright
10768SN/A * notice, this list of conditions and the following disclaimer in the
11768SN/A * documentation and/or other materials provided with the distribution;
12768SN/A * neither the name of the copyright holders nor the names of its
13768SN/A * contributors may be used to endorse or promote products derived from
14768SN/A * this software without specific prior written permission.
15768SN/A *
16768SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17768SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18768SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19768SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20768SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21768SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22768SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23768SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24768SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25768SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26768SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Kevin Lim
292665SN/A */
302665SN/A
31768SN/A#ifndef __CPU_O3_BPRED_UNIT_HH__
32768SN/A#define __CPU_O3_BPRED_UNIT_HH__
331722SN/A
341722SN/A#include "base/statistics.hh"
35768SN/A#include "cpu/inst_seq.hh"
36768SN/A
371401SN/A#include "cpu/o3/2bit_local_pred.hh"
381401SN/A#include "cpu/o3/btb.hh"
39768SN/A#include "cpu/o3/ras.hh"
403540Sgblack@eecs.umich.edu#include "cpu/o3/tournament_pred.hh"
419338SAndreas.Sandberg@arm.com
425443Sgblack@eecs.umich.edu#include "sim/host.hh"
438229Snate@binkert.org
445392Sgblack@eecs.umich.edu#include <list>
454762Snate@binkert.org
46932SN/Aclass DerivO3CPUParams;
47768SN/A
481722SN/A/**
49885SN/A * Basically a wrapper class to hold both the branch predictor
50885SN/A * and the BTB.
51768SN/A */
522542SN/Atemplate<class Impl>
53768SN/Aclass BPredUnit
54809SN/A{
55773SN/A  private:
56773SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
57768SN/A
585392Sgblack@eecs.umich.edu    enum PredType {
595606Snate@binkert.org        Local,
601854SN/A        Tournament
615392Sgblack@eecs.umich.edu    };
625606Snate@binkert.org
635606Snate@binkert.org    PredType predictor;
645392Sgblack@eecs.umich.edu
655392Sgblack@eecs.umich.edu  public:
665392Sgblack@eecs.umich.edu
671854SN/A    /**
685392Sgblack@eecs.umich.edu     * @param params The params object, that has the size of the BP and BTB.
695392Sgblack@eecs.umich.edu     */
705392Sgblack@eecs.umich.edu    BPredUnit(DerivO3CPUParams *params);
711817SN/A
72771SN/A    /**
73885SN/A     * Registers statistics.
74803SN/A     */
75885SN/A    void regStats();
76885SN/A
77803SN/A    void switchOut();
78885SN/A
79885SN/A    void takeOverFrom();
80803SN/A
81885SN/A    /**
82885SN/A     * Predicts whether or not the instruction is a taken branch, and the
83803SN/A     * target of the branch if it is taken.
84769SN/A     * @param inst The branch instruction.
85885SN/A     * @param PC The predicted PC is passed back through this parameter.
86885SN/A     * @param tid The thread id.
87885SN/A     * @return Returns if the branch is taken or not.
88885SN/A     */
89777SN/A    bool predict(DynInstPtr &inst, Addr &PC, unsigned tid);
90777SN/A
91885SN/A    // @todo: Rename this function.
92775SN/A    void BPUncond(void * &bp_history);
93775SN/A
941817SN/A    /**
955443Sgblack@eecs.umich.edu     * Tells the branch predictor to commit any updates until the given
96773SN/A     * sequence number.
975606Snate@binkert.org     * @param done_sn The sequence number to commit any older updates up until.
985392Sgblack@eecs.umich.edu     * @param tid The thread id.
995392Sgblack@eecs.umich.edu     */
100773SN/A    void update(const InstSeqNum &done_sn, unsigned tid);
101885SN/A
102885SN/A    /**
103885SN/A     * Squashes all outstanding updates until a given sequence number.
104885SN/A     * @param squashed_sn The sequence number to squash any younger updates up
1051817SN/A     * until.
106771SN/A     * @param tid The thread id.
107768SN/A     */
108891SN/A    void squash(const InstSeqNum &squashed_sn, unsigned tid);
109891SN/A
110891SN/A    /**
111891SN/A     * Squashes all outstanding updates until a given sequence number, and
1121634SN/A     * corrects that sn's update with the proper address and taken/not taken.
113775SN/A     * @param squashed_sn The sequence number to squash any younger updates up
1142539SN/A     * until.
1154762Snate@binkert.org     * @param corr_target The correct branch target.
116885SN/A     * @param actually_taken The correct branch direction.
117885SN/A     * @param tid The thread id.
1182539SN/A     */
119885SN/A    void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
1204762Snate@binkert.org                bool actually_taken, unsigned tid);
1214762Snate@binkert.org
1224762Snate@binkert.org    /**
1234762Snate@binkert.org     * @param bp_history Pointer to the history object.  The predictor
1244762Snate@binkert.org     * will need to update any state and delete the object.
1254762Snate@binkert.org     */
1264762Snate@binkert.org    void BPSquash(void *bp_history);
127768SN/A
1283349SN/A    /**
1293349SN/A     * Looks up a given PC in the BP to see if it is taken or not taken.
130768SN/A     * @param inst_PC The PC to look up.
131885SN/A     * @param bp_history Pointer that will be set to an object that
132885SN/A     * has the branch predictor state associated with the lookup.
133885SN/A     * @return Whether the branch is taken or not taken.
134885SN/A     */
135777SN/A    bool BPLookup(Addr &inst_PC, void * &bp_history);
136885SN/A
137885SN/A    /**
138885SN/A     * Looks up a given PC in the BTB to see if a matching entry exists.
139885SN/A     * @param inst_PC The PC to look up.
140885SN/A     * @return Whether the BTB contains the given PC.
141777SN/A     */
142777SN/A    bool BTBValid(Addr &inst_PC)
143885SN/A    { return BTB.valid(inst_PC, 0); }
144885SN/A
145885SN/A    /**
146885SN/A     * Looks up a given PC in the BTB to get the predicted target.
147768SN/A     * @param inst_PC The PC to look up.
148885SN/A     * @return The address of the target of the branch.
149885SN/A     */
150885SN/A    Addr BTBLookup(Addr &inst_PC)
151885SN/A    { return BTB.lookup(inst_PC, 0); }
152885SN/A
153885SN/A    /**
154768SN/A     * Updates the BP with taken/not taken information.
155909SN/A     * @param inst_PC The branch's PC that will be updated.
156768SN/A     * @param taken Whether the branch was taken or not taken.
157768SN/A     * @param bp_history Pointer to the branch predictor state that is
1581401SN/A     * associated with the branch lookup that is being updated.
159     * @todo Make this update flexible enough to handle a global predictor.
160     */
161    void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
162
163    /**
164     * Updates the BTB with the target of a branch.
165     * @param inst_PC The branch's PC that will be updated.
166     * @param target_PC The branch's target that will be added to the BTB.
167     */
168    void BTBUpdate(Addr &inst_PC, Addr &target_PC)
169    { BTB.update(inst_PC, target_PC,0); }
170
171    void dump();
172
173  private:
174    struct PredictorHistory {
175        /**
176         * Makes a predictor history struct that contains any
177         * information needed to update the predictor, BTB, and RAS.
178         */
179        PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
180                         const bool pred_taken, void *bp_history,
181                         const unsigned _tid)
182            : seqNum(seq_num), PC(inst_PC), RASTarget(0),
183              RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
184              wasCall(0), bpHistory(bp_history)
185        { }
186
187        /** The sequence number for the predictor history entry. */
188        InstSeqNum seqNum;
189
190        /** The PC associated with the sequence number. */
191        Addr PC;
192
193        /** The RAS target (only valid if a return). */
194        Addr RASTarget;
195
196        /** The RAS index of the instruction (only valid if a call). */
197        unsigned RASIndex;
198
199        /** The thread id. */
200        unsigned tid;
201
202        /** Whether or not it was predicted taken. */
203        bool predTaken;
204
205        /** Whether or not the RAS was used. */
206        bool usedRAS;
207
208        /** Whether or not the instruction was a call. */
209        bool wasCall;
210
211        /** Pointer to the history object passed back from the branch
212         * predictor.  It is used to update or restore state of the
213         * branch predictor.
214         */
215        void *bpHistory;
216    };
217
218    typedef std::list<PredictorHistory> History;
219
220    /**
221     * The per-thread predictor history. This is used to update the predictor
222     * as instructions are committed, or restore it to the proper state after
223     * a squash.
224     */
225    History predHist[Impl::MaxThreads];
226
227    /** The local branch predictor. */
228    LocalBP *localBP;
229
230    /** The tournament branch predictor. */
231    TournamentBP *tournamentBP;
232
233    /** The BTB. */
234    DefaultBTB BTB;
235
236    /** The per-thread return address stack. */
237    ReturnAddrStack RAS[Impl::MaxThreads];
238
239    /** Stat for number of BP lookups. */
240    Stats::Scalar lookups;
241    /** Stat for number of conditional branches predicted. */
242    Stats::Scalar condPredicted;
243    /** Stat for number of conditional branches predicted incorrectly. */
244    Stats::Scalar condIncorrect;
245    /** Stat for number of BTB lookups. */
246    Stats::Scalar BTBLookups;
247    /** Stat for number of BTB hits. */
248    Stats::Scalar BTBHits;
249    /** Stat for number of times the BTB is correct. */
250    Stats::Scalar BTBCorrect;
251    /** Stat for number of times the RAS is used to get a target. */
252    Stats::Scalar usedRAS;
253    /** Stat for number of times the RAS is incorrect. */
254    Stats::Scalar RASIncorrect;
255};
256
257#endif // __CPU_O3_BPRED_UNIT_HH__
258