inorder: import name for addtl. bpred stats
[gem5.git] / src / cpu / inorder / resources / bpred_unit.hh
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 * Korey Sewell
30 */
31
32 #ifndef __CPU_INORDER_BPRED_UNIT_HH__
33 #define __CPU_INORDER_BPRED_UNIT_HH__
34
35 #include <list>
36
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"
48
49 /**
50 * Basically a wrapper class to hold both the branch predictor
51 * and the BTB.
52 */
53 class BPredUnit
54 {
55 private:
56
57 enum PredType {
58 Local,
59 Tournament
60 };
61
62 PredType predictor;
63
64 public:
65
66 /**
67 * @param params The params object, that has the size of the BP and BTB.
68 */
69 BPredUnit(Resource *_res, ThePipeline::Params *params);
70
71 std::string name();
72
73 /**
74 * Registers statistics.
75 */
76 void regStats();
77
78 void switchOut();
79
80 void takeOverFrom();
81
82 /**
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.
89 */
90 bool predict(ThePipeline::DynInstPtr &inst, Addr &PC, ThreadID tid);
91
92 // @todo: Rename this function.
93 void BPUncond(void * &bp_history);
94
95 /**
96 * Tells the branch predictor to commit any updates until the given
97 * sequence number.
98 * @param done_sn The sequence number to commit any older updates up until.
99 * @param tid The thread id.
100 */
101 void update(const InstSeqNum &done_sn, ThreadID tid);
102
103 /**
104 * Squashes all outstanding updates until a given sequence number.
105 * @param squashed_sn The sequence number to squash any younger updates up
106 * until.
107 * @param tid The thread id.
108 */
109 void squash(const InstSeqNum &squashed_sn, ThreadID tid);
110
111 /**
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
115 * until.
116 * @param corr_target The correct branch target.
117 * @param actually_taken The correct branch direction.
118 * @param tid The thread id.
119 */
120 void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
121 bool actually_taken, ThreadID tid);
122
123 /**
124 * @param bp_history Pointer to the history object. The predictor
125 * will need to update any state and delete the object.
126 */
127 void BPSquash(void *bp_history);
128
129 /**
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.
135 */
136 bool BPLookup(Addr &inst_PC, void * &bp_history);
137
138 /**
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.
142 */
143 bool BTBValid(Addr &inst_PC)
144 { return BTB.valid(inst_PC, 0); }
145
146 /**
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.
150 */
151 Addr BTBLookup(Addr &inst_PC)
152 { return BTB.lookup(inst_PC, 0); }
153
154 /**
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.
161 */
162 void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
163
164 /**
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.
168 */
169 void BTBUpdate(Addr &inst_PC, Addr &target_PC)
170 { BTB.update(inst_PC, target_PC,0); }
171
172 void dump();
173
174 private:
175 Resource *res;
176
177 struct PredictorHistory {
178 /**
179 * Makes a predictor history struct that contains any
180 * information needed to update the predictor, BTB, and RAS.
181 */
182 PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
183 bool pred_taken, void *bp_history,
184 ThreadID _tid)
185 : seqNum(seq_num), PC(inst_PC), RASTarget(0),
186 RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
187 wasCall(0), bpHistory(bp_history)
188 { }
189
190 /** The sequence number for the predictor history entry. */
191 InstSeqNum seqNum;
192
193 /** The PC associated with the sequence number. */
194 Addr PC;
195
196 /** The RAS target (only valid if a return). */
197 Addr RASTarget;
198
199 /** The RAS index of the instruction (only valid if a call). */
200 unsigned RASIndex;
201
202 /** The thread id. */
203 ThreadID tid;
204
205 /** Whether or not it was predicted taken. */
206 bool predTaken;
207
208 /** Whether or not the RAS was used. */
209 bool usedRAS;
210
211 /** Whether or not the instruction was a call. */
212 bool wasCall;
213
214 /** Pointer to the history object passed back from the branch
215 * predictor. It is used to update or restore state of the
216 * branch predictor.
217 */
218 void *bpHistory;
219 };
220
221 typedef std::list<PredictorHistory> History;
222
223 /**
224 * The per-thread predictor history. This is used to update the predictor
225 * as instructions are committed, or restore it to the proper state after
226 * a squash.
227 */
228 History predHist[ThePipeline::MaxThreads];
229
230 /** The local branch predictor. */
231 LocalBP *localBP;
232
233 /** The tournament branch predictor. */
234 TournamentBP *tournamentBP;
235
236 /** The BTB. */
237 DefaultBTB BTB;
238
239 /** The per-thread return address stack. */
240 ReturnAddrStack RAS[ThePipeline::MaxThreads];
241
242 /** Stat for number of BP lookups. */
243 Stats::Scalar lookups;
244 /** Stat for number of conditional branches predicted. */
245 Stats::Scalar condPredicted;
246 /** Stat for number of conditional branches predicted incorrectly. */
247 Stats::Scalar condIncorrect;
248 /** Stat for number of BTB lookups. */
249 Stats::Scalar BTBLookups;
250 /** Stat for number of BTB hits. */
251 Stats::Scalar BTBHits;
252 /** Stat for number of times the BTB is correct. */
253 Stats::Scalar BTBCorrect;
254 /** Stat for number of times the RAS is used to get a target. */
255 Stats::Scalar usedRAS;
256 /** Stat for number of times the RAS is incorrect. */
257 Stats::Scalar RASIncorrect;
258 };
259
260 #endif // __CPU_INORDER_BPRED_UNIT_HH__