merge: mips fix to getArgument
[gem5.git] / src / cpu / o3 / 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 */
30
31 #ifndef __CPU_O3_BPRED_UNIT_HH__
32 #define __CPU_O3_BPRED_UNIT_HH__
33
34 #include "base/statistics.hh"
35 #include "cpu/inst_seq.hh"
36
37 #include "cpu/o3/2bit_local_pred.hh"
38 #include "cpu/o3/btb.hh"
39 #include "cpu/o3/ras.hh"
40 #include "cpu/o3/tournament_pred.hh"
41
42 #include "sim/host.hh"
43
44 #include <list>
45
46 /**
47 * Basically a wrapper class to hold both the branch predictor
48 * and the BTB.
49 */
50 template<class Impl>
51 class BPredUnit
52 {
53 private:
54 typedef typename Impl::Params Params;
55 typedef typename Impl::DynInstPtr DynInstPtr;
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(Params *params);
70
71 /**
72 * Registers statistics.
73 */
74 void regStats();
75
76 void switchOut();
77
78 void takeOverFrom();
79
80 /**
81 * Predicts whether or not the instruction is a taken branch, and the
82 * target of the branch if it is taken.
83 * @param inst The branch instruction.
84 * @param PC The predicted PC is passed back through this parameter.
85 * @param tid The thread id.
86 * @return Returns if the branch is taken or not.
87 */
88 bool predict(DynInstPtr &inst, Addr &PC, unsigned tid);
89
90 // @todo: Rename this function.
91 void BPUncond(void * &bp_history);
92
93 /**
94 * Tells the branch predictor to commit any updates until the given
95 * sequence number.
96 * @param done_sn The sequence number to commit any older updates up until.
97 * @param tid The thread id.
98 */
99 void update(const InstSeqNum &done_sn, unsigned tid);
100
101 /**
102 * Squashes all outstanding updates until a given sequence number.
103 * @param squashed_sn The sequence number to squash any younger updates up
104 * until.
105 * @param tid The thread id.
106 */
107 void squash(const InstSeqNum &squashed_sn, unsigned tid);
108
109 /**
110 * Squashes all outstanding updates until a given sequence number, and
111 * corrects that sn's update with the proper address and taken/not taken.
112 * @param squashed_sn The sequence number to squash any younger updates up
113 * until.
114 * @param corr_target The correct branch target.
115 * @param actually_taken The correct branch direction.
116 * @param tid The thread id.
117 */
118 void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
119 bool actually_taken, unsigned tid);
120
121 /**
122 * @param bp_history Pointer to the history object. The predictor
123 * will need to update any state and delete the object.
124 */
125 void BPSquash(void *bp_history);
126
127 /**
128 * Looks up a given PC in the BP to see if it is taken or not taken.
129 * @param inst_PC The PC to look up.
130 * @param bp_history Pointer that will be set to an object that
131 * has the branch predictor state associated with the lookup.
132 * @return Whether the branch is taken or not taken.
133 */
134 bool BPLookup(Addr &inst_PC, void * &bp_history);
135
136 /**
137 * Looks up a given PC in the BTB to see if a matching entry exists.
138 * @param inst_PC The PC to look up.
139 * @return Whether the BTB contains the given PC.
140 */
141 bool BTBValid(Addr &inst_PC)
142 { return BTB.valid(inst_PC, 0); }
143
144 /**
145 * Looks up a given PC in the BTB to get the predicted target.
146 * @param inst_PC The PC to look up.
147 * @return The address of the target of the branch.
148 */
149 Addr BTBLookup(Addr &inst_PC)
150 { return BTB.lookup(inst_PC, 0); }
151
152 /**
153 * Updates the BP with taken/not taken information.
154 * @param inst_PC The branch's PC that will be updated.
155 * @param taken Whether the branch was taken or not taken.
156 * @param bp_history Pointer to the branch predictor state that is
157 * associated with the branch lookup that is being updated.
158 * @todo Make this update flexible enough to handle a global predictor.
159 */
160 void BPUpdate(Addr &inst_PC, bool taken, void *bp_history);
161
162 /**
163 * Updates the BTB with the target of a branch.
164 * @param inst_PC The branch's PC that will be updated.
165 * @param target_PC The branch's target that will be added to the BTB.
166 */
167 void BTBUpdate(Addr &inst_PC, Addr &target_PC)
168 { BTB.update(inst_PC, target_PC,0); }
169
170 void dump();
171
172 private:
173 struct PredictorHistory {
174 /**
175 * Makes a predictor history struct that contains any
176 * information needed to update the predictor, BTB, and RAS.
177 */
178 PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
179 const bool pred_taken, void *bp_history,
180 const unsigned _tid)
181 : seqNum(seq_num), PC(inst_PC), RASTarget(0),
182 RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0),
183 wasCall(0), bpHistory(bp_history)
184 { }
185
186 /** The sequence number for the predictor history entry. */
187 InstSeqNum seqNum;
188
189 /** The PC associated with the sequence number. */
190 Addr PC;
191
192 /** The RAS target (only valid if a return). */
193 Addr RASTarget;
194
195 /** The RAS index of the instruction (only valid if a call). */
196 unsigned RASIndex;
197
198 /** The thread id. */
199 unsigned tid;
200
201 /** Whether or not it was predicted taken. */
202 bool predTaken;
203
204 /** Whether or not the RAS was used. */
205 bool usedRAS;
206
207 /** Whether or not the instruction was a call. */
208 bool wasCall;
209
210 /** Pointer to the history object passed back from the branch
211 * predictor. It is used to update or restore state of the
212 * branch predictor.
213 */
214 void *bpHistory;
215 };
216
217 typedef std::list<PredictorHistory> History;
218
219 /**
220 * The per-thread predictor history. This is used to update the predictor
221 * as instructions are committed, or restore it to the proper state after
222 * a squash.
223 */
224 History predHist[Impl::MaxThreads];
225
226 /** The local branch predictor. */
227 LocalBP *localBP;
228
229 /** The tournament branch predictor. */
230 TournamentBP *tournamentBP;
231
232 /** The BTB. */
233 DefaultBTB BTB;
234
235 /** The per-thread return address stack. */
236 ReturnAddrStack RAS[Impl::MaxThreads];
237
238 /** Stat for number of BP lookups. */
239 Stats::Scalar<> lookups;
240 /** Stat for number of conditional branches predicted. */
241 Stats::Scalar<> condPredicted;
242 /** Stat for number of conditional branches predicted incorrectly. */
243 Stats::Scalar<> condIncorrect;
244 /** Stat for number of BTB lookups. */
245 Stats::Scalar<> BTBLookups;
246 /** Stat for number of BTB hits. */
247 Stats::Scalar<> BTBHits;
248 /** Stat for number of times the BTB is correct. */
249 Stats::Scalar<> BTBCorrect;
250 /** Stat for number of times the RAS is used to get a target. */
251 Stats::Scalar<> usedRAS;
252 /** Stat for number of times the RAS is incorrect. */
253 Stats::Scalar<> RASIncorrect;
254 };
255
256 #endif // __CPU_O3_BPRED_UNIT_HH__