types: add a type for thread IDs and try to use it everywhere
[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 // For Addr type.
36 #include "arch/isa_traits.hh"
37 #include "base/statistics.hh"
38 #include "cpu/inst_seq.hh"
39
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"
48
49 #include <list>
50
51 /**
52 * Basically a wrapper class to hold both the branch predictor
53 * and the BTB.
54 */
55 class BPredUnit
56 {
57 private:
58
59 enum PredType {
60 Local,
61 Tournament
62 };
63
64 PredType predictor;
65
66 public:
67
68 /**
69 * @param params The params object, that has the size of the BP and BTB.
70 */
71 BPredUnit(ThePipeline::Params *params);
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 struct PredictorHistory {
176 /**
177 * Makes a predictor history struct that contains any
178 * information needed to update the predictor, BTB, and RAS.
179 */
180 PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
181 bool pred_taken, void *bp_history,
182 ThreadID _tid)
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)
186 { }
187
188 /** The sequence number for the predictor history entry. */
189 InstSeqNum seqNum;
190
191 /** The PC associated with the sequence number. */
192 Addr PC;
193
194 /** The RAS target (only valid if a return). */
195 Addr RASTarget;
196
197 /** The RAS index of the instruction (only valid if a call). */
198 unsigned RASIndex;
199
200 /** The thread id. */
201 ThreadID tid;
202
203 /** Whether or not it was predicted taken. */
204 bool predTaken;
205
206 /** Whether or not the RAS was used. */
207 bool usedRAS;
208
209 /** Whether or not the instruction was a call. */
210 bool wasCall;
211
212 /** Pointer to the history object passed back from the branch
213 * predictor. It is used to update or restore state of the
214 * branch predictor.
215 */
216 void *bpHistory;
217 };
218
219 typedef std::list<PredictorHistory> History;
220
221 /**
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
224 * a squash.
225 */
226 History predHist[ThePipeline::MaxThreads];
227
228 /** The local branch predictor. */
229 LocalBP *localBP;
230
231 /** The tournament branch predictor. */
232 TournamentBP *tournamentBP;
233
234 /** The BTB. */
235 DefaultBTB BTB;
236
237 /** The per-thread return address stack. */
238 ReturnAddrStack RAS[ThePipeline::MaxThreads];
239
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;
256 };
257
258 #endif // __CPU_INORDER_BPRED_UNIT_HH__