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__
36 #include "arch/isa_traits.hh"
37 #include "base/statistics.hh"
38 #include "cpu/inst_seq.hh"
40 //#include "cpu/inorder/params.hh"
41 #include "cpu/o3/2bit_local_pred.hh"
42 #include "cpu/o3/btb.hh"
43 #include "cpu/o3/ras.hh"
44 #include "cpu/o3/tournament_pred.hh"
45 #include "params/InOrderCPU.hh"
46 #include "cpu/inorder/inorder_dyn_inst.hh"
47 #include "cpu/inorder/pipeline_traits.hh"
52 * Basically a wrapper class to hold both the branch predictor
69 * @param params The params object, that has the size of the BP and BTB.
71 BPredUnit(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 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 &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);
112 * Squashes all outstanding updates until a given sequence number, and
113 * corrects that sn's update with the proper address and taken/not taken.
114 * @param squashed_sn The sequence number to squash any younger updates up
116 * @param corr_target The correct branch target.
117 * @param actually_taken The correct branch direction.
118 * @param tid The thread id.
120 void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
121 bool actually_taken, ThreadID tid);
124 * @param bp_history Pointer to the history object. The predictor
125 * will need to update any state and delete the object.
127 void BPSquash(void *bp_history);
130 * Looks up a given PC in the BP to see if it is taken or not taken.
131 * @param inst_PC The PC to look up.
132 * @param bp_history Pointer that will be set to an object that
133 * has the branch predictor state associated with the lookup.
134 * @return Whether the branch is taken or not taken.
136 bool BPLookup(Addr &inst_PC, void * &bp_history);
139 * Looks up a given PC in the BTB to see if a matching entry exists.
140 * @param inst_PC The PC to look up.
141 * @return Whether the BTB contains the given PC.
143 bool BTBValid(Addr &inst_PC)
144 { return BTB.valid(inst_PC, 0); }
147 * Looks up a given PC in the BTB to get the predicted target.
148 * @param inst_PC The PC to look up.
149 * @return The address of the target of the branch.
151 Addr BTBLookup(Addr &inst_PC)
152 { return BTB.lookup(inst_PC, 0); }
155 * Updates the BP with taken/not taken information.
156 * @param inst_PC The branch's PC that will be updated.
157 * @param taken Whether the branch was taken or not taken.
158 * @param bp_history Pointer to the branch predictor state that is
159 * associated with the branch lookup that is being updated.
160 * @todo Make this update flexible enough to handle a global predictor.
162 void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
165 * Updates the BTB with the target of a branch.
166 * @param inst_PC The branch's PC that will be updated.
167 * @param target_PC The branch's target that will be added to the BTB.
169 void BTBUpdate(Addr &inst_PC, Addr &target_PC)
170 { BTB.update(inst_PC, target_PC,0); }
175 struct PredictorHistory {
177 * Makes a predictor history struct that contains any
178 * information needed to update the predictor, BTB, and RAS.
180 PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
181 bool pred_taken, void *bp_history,
183 : seqNum(seq_num), PC(inst_PC), RASTarget(0),
184 RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
185 wasCall(0), bpHistory(bp_history)
188 /** The sequence number for the predictor history entry. */
191 /** The PC associated with the sequence number. */
194 /** The RAS target (only valid if a return). */
197 /** The RAS index of the instruction (only valid if a call). */
200 /** The thread id. */
203 /** Whether or not it was predicted taken. */
206 /** Whether or not the RAS was used. */
209 /** Whether or not the instruction was a call. */
212 /** Pointer to the history object passed back from the branch
213 * predictor. It is used to update or restore state of the
219 typedef std::list<PredictorHistory> History;
222 * The per-thread predictor history. This is used to update the predictor
223 * as instructions are committed, or restore it to the proper state after
226 History predHist[ThePipeline::MaxThreads];
228 /** The local branch predictor. */
231 /** The tournament branch predictor. */
232 TournamentBP *tournamentBP;
237 /** The per-thread return address stack. */
238 ReturnAddrStack RAS[ThePipeline::MaxThreads];
240 /** Stat for number of BP lookups. */
241 Stats::Scalar lookups;
242 /** Stat for number of conditional branches predicted. */
243 Stats::Scalar condPredicted;
244 /** Stat for number of conditional branches predicted incorrectly. */
245 Stats::Scalar condIncorrect;
246 /** Stat for number of BTB lookups. */
247 Stats::Scalar BTBLookups;
248 /** Stat for number of BTB hits. */
249 Stats::Scalar BTBHits;
250 /** Stat for number of times the BTB is correct. */
251 Stats::Scalar BTBCorrect;
252 /** Stat for number of times the RAS is used to get a target. */
253 Stats::Scalar usedRAS;
254 /** Stat for number of times the RAS is incorrect. */
255 Stats::Scalar RASIncorrect;
258 #endif // __CPU_INORDER_BPRED_UNIT_HH__