bpred_unit.hh revision 6036
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Kevin Lim
29 */
30
31#ifndef __CPU_O3_BPRED_UNIT_HH__
32#define __CPU_O3_BPRED_UNIT_HH__
33
34#include "base/statistics.hh"
35#include "cpu/inst_seq.hh"
36
37#include "cpu/o3/2bit_local_pred.hh"
38#include "cpu/o3/btb.hh"
39#include "cpu/o3/ras.hh"
40#include "cpu/o3/tournament_pred.hh"
41
42#include "sim/host.hh"
43
44#include <list>
45
46class DerivO3CPUParams;
47
48/**
49 * Basically a wrapper class to hold both the branch predictor
50 * and the BTB.
51 */
52template<class Impl>
53class BPredUnit
54{
55  private:
56    typedef typename Impl::DynInstPtr DynInstPtr;
57
58    enum PredType {
59        Local,
60        Tournament
61    };
62
63    PredType predictor;
64
65    const std::string _name;
66
67  public:
68
69    /**
70     * @param params The params object, that has the size of the BP and BTB.
71     */
72    BPredUnit(DerivO3CPUParams *params);
73
74    const std::string &name() const { return _name; }
75
76    /**
77     * Registers statistics.
78     */
79    void regStats();
80
81    void switchOut();
82
83    void takeOverFrom();
84
85    /**
86     * Predicts whether or not the instruction is a taken branch, and the
87     * target of the branch if it is taken.
88     * @param inst The branch instruction.
89     * @param PC The predicted PC is passed back through this parameter.
90     * @param tid The thread id.
91     * @return Returns if the branch is taken or not.
92     */
93    bool predict(DynInstPtr &inst, Addr &PC, unsigned tid);
94
95    // @todo: Rename this function.
96    void BPUncond(void * &bp_history);
97
98    /**
99     * Tells the branch predictor to commit any updates until the given
100     * sequence number.
101     * @param done_sn The sequence number to commit any older updates up until.
102     * @param tid The thread id.
103     */
104    void update(const InstSeqNum &done_sn, unsigned tid);
105
106    /**
107     * Squashes all outstanding updates until a given sequence number.
108     * @param squashed_sn The sequence number to squash any younger updates up
109     * until.
110     * @param tid The thread id.
111     */
112    void squash(const InstSeqNum &squashed_sn, unsigned tid);
113
114    /**
115     * Squashes all outstanding updates until a given sequence number, and
116     * corrects that sn's update with the proper address and taken/not taken.
117     * @param squashed_sn The sequence number to squash any younger updates up
118     * until.
119     * @param corr_target The correct branch target.
120     * @param actually_taken The correct branch direction.
121     * @param tid The thread id.
122     */
123    void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
124                bool actually_taken, unsigned tid);
125
126    /**
127     * @param bp_history Pointer to the history object.  The predictor
128     * will need to update any state and delete the object.
129     */
130    void BPSquash(void *bp_history);
131
132    /**
133     * Looks up a given PC in the BP to see if it is taken or not taken.
134     * @param inst_PC The PC to look up.
135     * @param bp_history Pointer that will be set to an object that
136     * has the branch predictor state associated with the lookup.
137     * @return Whether the branch is taken or not taken.
138     */
139    bool BPLookup(Addr &inst_PC, void * &bp_history);
140
141    /**
142     * Looks up a given PC in the BTB to see if a matching entry exists.
143     * @param inst_PC The PC to look up.
144     * @return Whether the BTB contains the given PC.
145     */
146    bool BTBValid(Addr &inst_PC)
147    { return BTB.valid(inst_PC, 0); }
148
149    /**
150     * Looks up a given PC in the BTB to get the predicted target.
151     * @param inst_PC The PC to look up.
152     * @return The address of the target of the branch.
153     */
154    Addr BTBLookup(Addr &inst_PC)
155    { return BTB.lookup(inst_PC, 0); }
156
157    /**
158     * Updates the BP with taken/not taken information.
159     * @param inst_PC The branch's PC that will be updated.
160     * @param taken Whether the branch was taken or not taken.
161     * @param bp_history Pointer to the branch predictor state that is
162     * associated with the branch lookup that is being updated.
163     * @todo Make this update flexible enough to handle a global predictor.
164     */
165    void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
166
167    /**
168     * Updates the BTB with the target of a branch.
169     * @param inst_PC The branch's PC that will be updated.
170     * @param target_PC The branch's target that will be added to the BTB.
171     */
172    void BTBUpdate(Addr &inst_PC, Addr &target_PC)
173    { BTB.update(inst_PC, target_PC,0); }
174
175    void dump();
176
177  private:
178    struct PredictorHistory {
179        /**
180         * Makes a predictor history struct that contains any
181         * information needed to update the predictor, BTB, and RAS.
182         */
183        PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
184                         const bool pred_taken, void *bp_history,
185                         const unsigned _tid)
186            : seqNum(seq_num), PC(inst_PC), RASTarget(0),
187              RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
188              wasCall(0), bpHistory(bp_history)
189        { }
190
191        bool operator==(const PredictorHistory &entry) const {
192            return this->seqNum == entry.seqNum;
193        }
194
195        /** The sequence number for the predictor history entry. */
196        InstSeqNum seqNum;
197
198        /** The PC associated with the sequence number. */
199        Addr PC;
200
201        /** The RAS target (only valid if a return). */
202        Addr RASTarget;
203
204        /** The RAS index of the instruction (only valid if a call). */
205        unsigned RASIndex;
206
207        /** The thread id. */
208        unsigned tid;
209
210        /** Whether or not it was predicted taken. */
211        bool predTaken;
212
213        /** Whether or not the RAS was used. */
214        bool usedRAS;
215
216        /** Whether or not the instruction was a call. */
217        bool wasCall;
218
219        /** Pointer to the history object passed back from the branch
220         * predictor.  It is used to update or restore state of the
221         * branch predictor.
222         */
223        void *bpHistory;
224    };
225
226    typedef std::list<PredictorHistory> History;
227    typedef typename History::iterator HistoryIt;
228
229    /**
230     * The per-thread predictor history. This is used to update the predictor
231     * as instructions are committed, or restore it to the proper state after
232     * a squash.
233     */
234    History predHist[Impl::MaxThreads];
235
236    /** The local branch predictor. */
237    LocalBP *localBP;
238
239    /** The tournament branch predictor. */
240    TournamentBP *tournamentBP;
241
242    /** The BTB. */
243    DefaultBTB BTB;
244
245    /** The per-thread return address stack. */
246    ReturnAddrStack RAS[Impl::MaxThreads];
247
248    /** Stat for number of BP lookups. */
249    Stats::Scalar lookups;
250    /** Stat for number of conditional branches predicted. */
251    Stats::Scalar condPredicted;
252    /** Stat for number of conditional branches predicted incorrectly. */
253    Stats::Scalar condIncorrect;
254    /** Stat for number of BTB lookups. */
255    Stats::Scalar BTBLookups;
256    /** Stat for number of BTB hits. */
257    Stats::Scalar BTBHits;
258    /** Stat for number of times the BTB is correct. */
259    Stats::Scalar BTBCorrect;
260    /** Stat for number of times the RAS is used to get a target. */
261    Stats::Scalar usedRAS;
262    /** Stat for number of times the RAS is incorrect. */
263    Stats::Scalar RASIncorrect;
264};
265
266#endif // __CPU_O3_BPRED_UNIT_HH__
267