2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
32 #ifndef __CPU_INORDER_BPRED_UNIT_HH__
33 #define __CPU_INORDER_BPRED_UNIT_HH__
37 #include "arch/isa_traits.hh"
38 #include "base/statistics.hh"
39 #include "cpu/inorder/inorder_dyn_inst.hh"
40 #include "cpu/inorder/pipeline_traits.hh"
41 #include "cpu/inorder/resource.hh"
42 #include "cpu/pred/2bit_local.hh"
43 #include "cpu/pred/btb.hh"
44 #include "cpu/pred/ras.hh"
45 #include "cpu/pred/tournament.hh"
46 #include "cpu/inst_seq.hh"
47 #include "params/InOrderCPU.hh"
50 * Basically a wrapper class to hold both the branch predictor
67 * @param params The params object, that has the size of the BP and BTB.
69 BPredUnit(Resource *_res, ThePipeline::Params *params);
74 * Registers statistics.
83 * Predicts whether or not the instruction is a taken branch, and the
84 * target of the branch if it is taken.
85 * @param inst The branch instruction.
86 * @param predPC The predicted PC is passed back through this parameter.
87 * @param tid The thread id.
88 * @return Returns if the branch is taken or not.
90 bool predict(ThePipeline::DynInstPtr &inst,
91 TheISA::PCState &predPC, ThreadID tid);
93 // @todo: Rename this function.
94 void BPUncond(void * &bp_history);
97 * Tells the branch predictor to commit any updates until the given
99 * @param done_sn The sequence number to commit any older updates up until.
100 * @param tid The thread id.
102 void update(const InstSeqNum &done_sn, ThreadID tid);
105 * Squashes all outstanding updates until a given sequence number.
106 * @param squashed_sn The sequence number to squash any younger updates up
108 * @param tid The thread id.
110 void squash(const InstSeqNum &squashed_sn, ThreadID tid,
114 * Squashes all outstanding updates until a given sequence number, and
115 * corrects that sn's update with the proper address and taken/not taken.
116 * @param squashed_sn The sequence number to squash any younger updates up
118 * @param corrTarget The correct branch target.
119 * @param actually_taken The correct branch direction.
120 * @param tid The thread id.
122 void squash(const InstSeqNum &squashed_sn,
123 const TheISA::PCState &corrTarget, bool actually_taken,
124 ThreadID tid, ThreadID asid = 0);
127 * @param bp_history Pointer to the history object. The predictor
128 * will need to update any state and delete the object.
130 void BPSquash(void *bp_history);
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.
139 bool BPLookup(Addr instPC, void * &bp_history);
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.
146 bool BTBValid(Addr &inst_PC)
147 { return BTB.valid(inst_PC, 0); }
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.
154 TheISA::PCState BTBLookup(Addr instPC)
155 { return BTB.lookup(instPC, 0); }
158 * Updates the BP with taken/not taken information.
159 * @param instPC 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.
165 void BPUpdate(Addr instPC, bool taken, void *bp_history);
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.
172 void BTBUpdate(Addr instPC, const TheISA::PCState &targetPC)
173 { BTB.update(instPC, targetPC, 0); }
181 struct PredictorHistory {
183 * Makes a predictor history struct that contains any
184 * information needed to update the predictor, BTB, and RAS.
186 PredictorHistory(const InstSeqNum &seq_num,
187 const TheISA::PCState &instPC, bool pred_taken,
188 void *bp_history, ThreadID _tid)
189 : seqNum(seq_num), pc(instPC), rasTarget(0), RASIndex(0),
190 tid(_tid), predTaken(pred_taken), usedRAS(0), wasCall(0),
191 bpHistory(bp_history)
194 /** The sequence number for the predictor history entry. */
197 /** The PC associated with the sequence number. */
200 /** The RAS target (only valid if a return). */
201 TheISA::PCState rasTarget;
203 /** The RAS index of the instruction (only valid if a call). */
206 /** The thread id. */
209 /** Whether or not it was predicted taken. */
212 /** Whether or not the RAS was used. */
215 /** Whether or not the instruction was a call. */
218 /** Pointer to the history object passed back from the branch
219 * predictor. It is used to update or restore state of the
225 typedef std::list<PredictorHistory> History;
226 typedef History::iterator HistoryIt;
229 * The per-thread predictor history. This is used to update the predictor
230 * as instructions are committed, or restore it to the proper state after
233 History predHist[ThePipeline::MaxThreads];
235 /** The local branch predictor. */
238 /** The tournament branch predictor. */
239 TournamentBP *tournamentBP;
244 /** The per-thread return address stack. */
245 ReturnAddrStack RAS[ThePipeline::MaxThreads];
247 /** Stat for number of BP lookups. */
248 Stats::Scalar lookups;
249 /** Stat for number of conditional branches predicted. */
250 Stats::Scalar condPredicted;
251 /** Stat for number of conditional branches predicted incorrectly. */
252 Stats::Scalar condIncorrect;
253 /** Stat for number of BTB lookups. */
254 Stats::Scalar BTBLookups;
255 /** Stat for number of BTB hits. */
256 Stats::Scalar BTBHits;
257 /** Stat for number of times the BTB is correct. */
258 Stats::Scalar BTBCorrect;
259 /** Stat for number of times the RAS is used to get a target. */
260 Stats::Scalar usedRAS;
261 /** Stat for number of times the RAS is incorrect. */
262 Stats::Scalar RASIncorrect;
263 Stats::Formula BTBHitPct;
266 #endif // __CPU_INORDER_BPRED_UNIT_HH__