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/inst_seq.hh"
40 #include "cpu/inorder/inorder_dyn_inst.hh"
41 #include "cpu/inorder/pipeline_traits.hh"
42 #include "cpu/inorder/resource.hh"
43 #include "cpu/pred/2bit_local.hh"
44 #include "cpu/pred/btb.hh"
45 #include "cpu/pred/ras.hh"
46 #include "cpu/pred/tournament.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 pred_PC 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, Addr &pred_PC, ThreadID tid);
92 // @todo: Rename this function.
93 void BPUncond(void * &bp_history);
96 * Tells the branch predictor to commit any updates until the given
98 * @param done_sn The sequence number to commit any older updates up until.
99 * @param tid The thread id.
101 void update(const InstSeqNum &done_sn, ThreadID tid);
104 * Squashes all outstanding updates until a given sequence number.
105 * @param squashed_sn The sequence number to squash any younger updates up
107 * @param tid The thread id.
109 void squash(const InstSeqNum &squashed_sn, ThreadID tid,
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
117 * @param corr_target The correct branch target.
118 * @param actually_taken The correct branch direction.
119 * @param tid The thread id.
121 void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
122 bool actually_taken, ThreadID tid, ThreadID asid = 0);
125 * @param bp_history Pointer to the history object. The predictor
126 * will need to update any state and delete the object.
128 void BPSquash(void *bp_history);
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.
137 bool BPLookup(Addr &inst_PC, void * &bp_history);
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.
144 bool BTBValid(Addr &inst_PC)
145 { return BTB.valid(inst_PC, 0); }
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.
152 Addr BTBLookup(Addr &inst_PC)
153 { return BTB.lookup(inst_PC, 0); }
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.
163 void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
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.
170 void BTBUpdate(Addr &inst_PC, Addr &target_PC)
171 { BTB.update(inst_PC, target_PC,0); }
179 struct PredictorHistory {
181 * Makes a predictor history struct that contains any
182 * information needed to update the predictor, BTB, and RAS.
184 PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
185 bool pred_taken, void *bp_history,
187 : seqNum(seq_num), PC(inst_PC), RASTarget(0),
188 RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
189 wasCall(0), bpHistory(bp_history)
192 /** The sequence number for the predictor history entry. */
195 /** The PC associated with the sequence number. */
198 /** The RAS target (only valid if a return). */
201 /** The RAS index of the instruction (only valid if a call). */
204 /** The thread id. */
207 /** Whether or not it was predicted taken. */
210 /** Whether or not the RAS was used. */
213 /** Whether or not the instruction was a call. */
216 /** Pointer to the history object passed back from the branch
217 * predictor. It is used to update or restore state of the
223 typedef std::list<PredictorHistory> History;
224 typedef History::iterator HistoryIt;
227 * The per-thread predictor history. This is used to update the predictor
228 * as instructions are committed, or restore it to the proper state after
231 History predHist[ThePipeline::MaxThreads];
233 /** The local branch predictor. */
236 /** The tournament branch predictor. */
237 TournamentBP *tournamentBP;
242 /** The per-thread return address stack. */
243 ReturnAddrStack RAS[ThePipeline::MaxThreads];
245 /** Stat for number of BP lookups. */
246 Stats::Scalar lookups;
247 /** Stat for number of conditional branches predicted. */
248 Stats::Scalar condPredicted;
249 /** Stat for number of conditional branches predicted incorrectly. */
250 Stats::Scalar condIncorrect;
251 /** Stat for number of BTB lookups. */
252 Stats::Scalar BTBLookups;
253 /** Stat for number of BTB hits. */
254 Stats::Scalar BTBHits;
255 /** Stat for number of times the BTB is correct. */
256 Stats::Scalar BTBCorrect;
257 /** Stat for number of times the RAS is used to get a target. */
258 Stats::Scalar usedRAS;
259 /** Stat for number of times the RAS is incorrect. */
260 Stats::Scalar RASIncorrect;
261 Stats::Formula BTBHitPct;
264 #endif // __CPU_INORDER_BPRED_UNIT_HH__