bpred_unit.hh revision 6226
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 <list>
35
36#include "base/statistics.hh"
37#include "base/types.hh"
38#include "cpu/inst_seq.hh"
39#include "cpu/pred/2bit_local.hh"
40#include "cpu/pred/btb.hh"
41#include "cpu/pred/ras.hh"
42#include "cpu/pred/tournament.hh"
43
44class DerivO3CPUParams;
45
46/**
47 * Basically a wrapper class to hold both the branch predictor
48 * and the BTB.
49 */
50template<class Impl>
51class BPredUnit
52{
53  private:
54    typedef typename Impl::DynInstPtr DynInstPtr;
55
56    enum PredType {
57        Local,
58        Tournament
59    };
60
61    PredType predictor;
62
63    const std::string _name;
64
65  public:
66
67    /**
68     * @param params The params object, that has the size of the BP and BTB.
69     */
70    BPredUnit(DerivO3CPUParams *params);
71
72    const std::string &name() const { return _name; }
73
74    /**
75     * Registers statistics.
76     */
77    void regStats();
78
79    void switchOut();
80
81    void takeOverFrom();
82
83    /**
84     * Predicts whether or not the instruction is a taken branch, and the
85     * target of the branch if it is taken.
86     * @param inst The branch instruction.
87     * @param PC The predicted PC is passed back through this parameter.
88     * @param tid The thread id.
89     * @return Returns if the branch is taken or not.
90     */
91    bool predict(DynInstPtr &inst, Addr &PC, ThreadID tid);
92
93    // @todo: Rename this function.
94    void BPUncond(void * &bp_history);
95
96    /**
97     * Tells the branch predictor to commit any updates until the given
98     * sequence number.
99     * @param done_sn The sequence number to commit any older updates up until.
100     * @param tid The thread id.
101     */
102    void update(const InstSeqNum &done_sn, ThreadID tid);
103
104    /**
105     * Squashes all outstanding updates until a given sequence number.
106     * @param squashed_sn The sequence number to squash any younger updates up
107     * until.
108     * @param tid The thread id.
109     */
110    void squash(const InstSeqNum &squashed_sn, ThreadID tid);
111
112    /**
113     * Squashes all outstanding updates until a given sequence number, and
114     * corrects that sn's update with the proper address and taken/not taken.
115     * @param squashed_sn The sequence number to squash any younger updates up
116     * until.
117     * @param corr_target The correct branch target.
118     * @param actually_taken The correct branch direction.
119     * @param tid The thread id.
120     */
121    void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
122                bool actually_taken, ThreadID tid);
123
124    /**
125     * @param bp_history Pointer to the history object.  The predictor
126     * will need to update any state and delete the object.
127     */
128    void BPSquash(void *bp_history);
129
130    /**
131     * Looks up a given PC in the BP to see if it is taken or not taken.
132     * @param inst_PC The PC to look up.
133     * @param bp_history Pointer that will be set to an object that
134     * has the branch predictor state associated with the lookup.
135     * @return Whether the branch is taken or not taken.
136     */
137    bool BPLookup(Addr &inst_PC, void * &bp_history);
138
139    /**
140     * Looks up a given PC in the BTB to see if a matching entry exists.
141     * @param inst_PC The PC to look up.
142     * @return Whether the BTB contains the given PC.
143     */
144    bool BTBValid(Addr &inst_PC)
145    { return BTB.valid(inst_PC, 0); }
146
147    /**
148     * Looks up a given PC in the BTB to get the predicted target.
149     * @param inst_PC The PC to look up.
150     * @return The address of the target of the branch.
151     */
152    Addr BTBLookup(Addr &inst_PC)
153    { return BTB.lookup(inst_PC, 0); }
154
155    /**
156     * Updates the BP with taken/not taken information.
157     * @param inst_PC The branch's PC that will be updated.
158     * @param taken Whether the branch was taken or not taken.
159     * @param bp_history Pointer to the branch predictor state that is
160     * associated with the branch lookup that is being updated.
161     * @todo Make this update flexible enough to handle a global predictor.
162     */
163    void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
164
165    /**
166     * Updates the BTB with the target of a branch.
167     * @param inst_PC The branch's PC that will be updated.
168     * @param target_PC The branch's target that will be added to the BTB.
169     */
170    void BTBUpdate(Addr &inst_PC, Addr &target_PC)
171    { BTB.update(inst_PC, target_PC,0); }
172
173    void dump();
174
175  private:
176    struct PredictorHistory {
177        /**
178         * Makes a predictor history struct that contains any
179         * information needed to update the predictor, BTB, and RAS.
180         */
181        PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
182                         bool pred_taken, void *bp_history,
183                         ThreadID _tid)
184            : seqNum(seq_num), PC(inst_PC), RASTarget(0),
185              RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
186              wasCall(0), bpHistory(bp_history)
187        { }
188
189        bool operator==(const PredictorHistory &entry) const {
190            return this->seqNum == entry.seqNum;
191        }
192
193        /** The sequence number for the predictor history entry. */
194        InstSeqNum seqNum;
195
196        /** The PC associated with the sequence number. */
197        Addr PC;
198
199        /** The RAS target (only valid if a return). */
200        Addr RASTarget;
201
202        /** The RAS index of the instruction (only valid if a call). */
203        unsigned RASIndex;
204
205        /** The thread id. */
206        ThreadID tid;
207
208        /** Whether or not it was predicted taken. */
209        bool predTaken;
210
211        /** Whether or not the RAS was used. */
212        bool usedRAS;
213
214        /** Whether or not the instruction was a call. */
215        bool wasCall;
216
217        /** Pointer to the history object passed back from the branch
218         * predictor.  It is used to update or restore state of the
219         * branch predictor.
220         */
221        void *bpHistory;
222    };
223
224    typedef std::list<PredictorHistory> History;
225    typedef typename History::iterator HistoryIt;
226
227    /**
228     * The per-thread predictor history. This is used to update the predictor
229     * as instructions are committed, or restore it to the proper state after
230     * a squash.
231     */
232    History predHist[Impl::MaxThreads];
233
234    /** The local branch predictor. */
235    LocalBP *localBP;
236
237    /** The tournament branch predictor. */
238    TournamentBP *tournamentBP;
239
240    /** The BTB. */
241    DefaultBTB BTB;
242
243    /** The per-thread return address stack. */
244    ReturnAddrStack RAS[Impl::MaxThreads];
245
246    /** Stat for number of BP lookups. */
247    Stats::Scalar lookups;
248    /** Stat for number of conditional branches predicted. */
249    Stats::Scalar condPredicted;
250    /** Stat for number of conditional branches predicted incorrectly. */
251    Stats::Scalar condIncorrect;
252    /** Stat for number of BTB lookups. */
253    Stats::Scalar BTBLookups;
254    /** Stat for number of BTB hits. */
255    Stats::Scalar BTBHits;
256    /** Stat for number of times the BTB is correct. */
257    Stats::Scalar BTBCorrect;
258    /** Stat for number of times the RAS is used to get a target. */
259    Stats::Scalar usedRAS;
260    /** Stat for number of times the RAS is incorrect. */
261    Stats::Scalar RASIncorrect;
262};
263
264#endif // __CPU_O3_BPRED_UNIT_HH__
265