TournamentBP: Fix some bugs with table sizes and counters
[gem5.git] / src / cpu / pred / tournament.hh
1 /*
2 * Copyright (c) 2011 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Kevin Lim
41 */
42
43 #ifndef __CPU_O3_TOURNAMENT_PRED_HH__
44 #define __CPU_O3_TOURNAMENT_PRED_HH__
45
46 #include <vector>
47
48 #include "base/types.hh"
49 #include "cpu/o3/sat_counter.hh"
50
51 /**
52 * Implements a tournament branch predictor, hopefully identical to the one
53 * used in the 21264. It has a local predictor, which uses a local history
54 * table to index into a table of counters, and a global predictor, which
55 * uses a global history to index into a table of counters. A choice
56 * predictor chooses between the two. Only the global history register
57 * is speculatively updated, the rest are updated upon branches committing
58 * or misspeculating.
59 */
60 class TournamentBP
61 {
62 public:
63 /**
64 * Default branch predictor constructor.
65 */
66 TournamentBP(unsigned localCtrBits,
67 unsigned localHistoryTableSize,
68 unsigned localHistoryBits,
69 unsigned globalPredictorSize,
70 unsigned globalHistoryBits,
71 unsigned globalCtrBits,
72 unsigned choicePredictorSize,
73 unsigned choiceCtrBits,
74 unsigned instShiftAmt);
75
76 /**
77 * Looks up the given address in the branch predictor and returns
78 * a true/false value as to whether it is taken. Also creates a
79 * BPHistory object to store any state it will need on squash/update.
80 * @param branch_addr The address of the branch to look up.
81 * @param bp_history Pointer that will be set to the BPHistory object.
82 * @return Whether or not the branch is taken.
83 */
84 bool lookup(Addr &branch_addr, void * &bp_history);
85
86 /**
87 * Records that there was an unconditional branch, and modifies
88 * the bp history to point to an object that has the previous
89 * global history stored in it.
90 * @param bp_history Pointer that will be set to the BPHistory object.
91 */
92 void uncondBr(void * &bp_history);
93 /**
94 * Updates the branch predictor to Not Taken if a BTB entry is
95 * invalid or not found.
96 * @param branch_addr The address of the branch to look up.
97 * @param bp_history Pointer to any bp history state.
98 * @return Whether or not the branch is taken.
99 */
100 void BTBUpdate(Addr &branch_addr, void * &bp_history);
101 /**
102 * Updates the branch predictor with the actual result of a branch.
103 * @param branch_addr The address of the branch to update.
104 * @param taken Whether or not the branch was taken.
105 * @param bp_history Pointer to the BPHistory object that was created
106 * when the branch was predicted.
107 * @param squashed is set when this function is called during a squash
108 * operation.
109 */
110 void update(Addr &branch_addr, bool taken, void *bp_history, bool squashed);
111
112 /**
113 * Restores the global branch history on a squash.
114 * @param bp_history Pointer to the BPHistory object that has the
115 * previous global branch history in it.
116 */
117 void squash(void *bp_history);
118
119 /** Returns the global history. */
120 inline unsigned readGlobalHist() { return globalHistory; }
121
122 private:
123 /**
124 * Returns if the branch should be taken or not, given a counter
125 * value.
126 * @param count The counter value.
127 */
128 inline bool getPrediction(uint8_t &count);
129
130 /**
131 * Returns the local history index, given a branch address.
132 * @param branch_addr The branch's PC address.
133 */
134 inline unsigned calcLocHistIdx(Addr &branch_addr);
135
136 /** Updates global history as taken. */
137 inline void updateGlobalHistTaken();
138
139 /** Updates global history as not taken. */
140 inline void updateGlobalHistNotTaken();
141
142 /**
143 * Updates local histories as taken.
144 * @param local_history_idx The local history table entry that
145 * will be updated.
146 */
147 inline void updateLocalHistTaken(unsigned local_history_idx);
148
149 /**
150 * Updates local histories as not taken.
151 * @param local_history_idx The local history table entry that
152 * will be updated.
153 */
154 inline void updateLocalHistNotTaken(unsigned local_history_idx);
155
156 /**
157 * The branch history information that is created upon predicting
158 * a branch. It will be passed back upon updating and squashing,
159 * when the BP can use this information to update/restore its
160 * state properly.
161 */
162 struct BPHistory {
163 #ifdef DEBUG
164 BPHistory()
165 { newCount++; }
166 ~BPHistory()
167 { newCount--; }
168
169 static int newCount;
170 #endif
171 unsigned globalHistory;
172 unsigned localHistory;
173 bool localPredTaken;
174 bool globalPredTaken;
175 bool globalUsed;
176 };
177
178 /** Flag for invalid predictor index */
179 static const int invalidPredictorIndex = -1;
180 /** Local counters. */
181 std::vector<SatCounter> localCtrs;
182
183 /** Number of counters in the local predictor. */
184 unsigned localPredictorSize;
185
186 /** Mask to truncate values stored in the local history table. */
187 unsigned localPredictorMask;
188
189 /** Number of bits of the local predictor's counters. */
190 unsigned localCtrBits;
191
192 /** Array of local history table entries. */
193 std::vector<unsigned> localHistoryTable;
194
195 /** Number of entries in the local history table. */
196 unsigned localHistoryTableSize;
197
198 /** Number of bits for each entry of the local history table. */
199 unsigned localHistoryBits;
200
201 /** Array of counters that make up the global predictor. */
202 std::vector<SatCounter> globalCtrs;
203
204 /** Number of entries in the global predictor. */
205 unsigned globalPredictorSize;
206
207 /** Number of bits of the global predictor's counters. */
208 unsigned globalCtrBits;
209
210 /** Global history register. Contains as much history as specified by
211 * globalHistoryBits. Actual number of bits used is determined by
212 * globalHistoryMask and choiceHistoryMask. */
213 unsigned globalHistory;
214
215 /** Number of bits for the global history. Determines maximum number of
216 entries in global and choice predictor tables. */
217 unsigned globalHistoryBits;
218
219 /** Mask to apply to globalHistory to access global history table.
220 * Based on globalPredictorSize.*/
221 unsigned globalHistoryMask;
222
223 /** Mask to apply to globalHistory to access choice history table.
224 * Based on choicePredictorSize.*/
225 unsigned choiceHistoryMask;
226
227 /** Mask to control how much history is stored. All of it might not be
228 * used. */
229 unsigned historyRegisterMask;
230
231 /** Array of counters that make up the choice predictor. */
232 std::vector<SatCounter> choiceCtrs;
233
234 /** Number of entries in the choice predictor. */
235 unsigned choicePredictorSize;
236
237 /** Number of bits in the choice predictor's counters. */
238 unsigned choiceCtrBits;
239
240 /** Number of bits to shift the instruction over to get rid of the word
241 * offset.
242 */
243 unsigned instShiftAmt;
244
245 /** Thresholds for the counter value; above the threshold is taken,
246 * equal to or below the threshold is not taken.
247 */
248 unsigned localThreshold;
249 unsigned globalThreshold;
250 unsigned choiceThreshold;
251 };
252
253 #endif // __CPU_O3_TOURNAMENT_PRED_HH__