tournament.hh (11783:f94c14fd6561) tournament.hh (13626:d6a6358aa6db)
1/*
2 * Copyright (c) 2011, 2014 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 * Timothy M. Jones
42 * Nilay Vaish
43 */
44
45#ifndef __CPU_PRED_TOURNAMENT_PRED_HH__
46#define __CPU_PRED_TOURNAMENT_PRED_HH__
47
48#include <vector>
49
50#include "base/types.hh"
51#include "cpu/pred/bpred_unit.hh"
52#include "cpu/pred/sat_counter.hh"
53#include "params/TournamentBP.hh"
54
55/**
56 * Implements a tournament branch predictor, hopefully identical to the one
57 * used in the 21264. It has a local predictor, which uses a local history
58 * table to index into a table of counters, and a global predictor, which
59 * uses a global history to index into a table of counters. A choice
60 * predictor chooses between the two. Both the global history register
61 * and the selected local history are speculatively updated.
62 */
63class TournamentBP : public BPredUnit
64{
65 public:
66 /**
67 * Default branch predictor constructor.
68 */
69 TournamentBP(const TournamentBPParams *params);
70
71 /**
72 * Looks up the given address in the branch predictor and returns
73 * a true/false value as to whether it is taken. Also creates a
74 * BPHistory object to store any state it will need on squash/update.
75 * @param branch_addr The address of the branch to look up.
76 * @param bp_history Pointer that will be set to the BPHistory object.
77 * @return Whether or not the branch is taken.
78 */
79 bool lookup(ThreadID tid, Addr branch_addr, void * &bp_history);
80
81 /**
82 * Records that there was an unconditional branch, and modifies
83 * the bp history to point to an object that has the previous
84 * global history stored in it.
85 * @param bp_history Pointer that will be set to the BPHistory object.
86 */
87 void uncondBranch(ThreadID tid, Addr pc, void * &bp_history);
88 /**
89 * Updates the branch predictor to Not Taken if a BTB entry is
90 * invalid or not found.
91 * @param branch_addr The address of the branch to look up.
92 * @param bp_history Pointer to any bp history state.
93 * @return Whether or not the branch is taken.
94 */
95 void btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history);
96 /**
97 * Updates the branch predictor with the actual result of a branch.
98 * @param branch_addr The address of the branch to update.
99 * @param taken Whether or not the branch was taken.
100 * @param bp_history Pointer to the BPHistory object that was created
101 * when the branch was predicted.
102 * @param squashed is set when this function is called during a squash
103 * operation.
1/*
2 * Copyright (c) 2011, 2014 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 * Timothy M. Jones
42 * Nilay Vaish
43 */
44
45#ifndef __CPU_PRED_TOURNAMENT_PRED_HH__
46#define __CPU_PRED_TOURNAMENT_PRED_HH__
47
48#include <vector>
49
50#include "base/types.hh"
51#include "cpu/pred/bpred_unit.hh"
52#include "cpu/pred/sat_counter.hh"
53#include "params/TournamentBP.hh"
54
55/**
56 * Implements a tournament branch predictor, hopefully identical to the one
57 * used in the 21264. It has a local predictor, which uses a local history
58 * table to index into a table of counters, and a global predictor, which
59 * uses a global history to index into a table of counters. A choice
60 * predictor chooses between the two. Both the global history register
61 * and the selected local history are speculatively updated.
62 */
63class TournamentBP : public BPredUnit
64{
65 public:
66 /**
67 * Default branch predictor constructor.
68 */
69 TournamentBP(const TournamentBPParams *params);
70
71 /**
72 * Looks up the given address in the branch predictor and returns
73 * a true/false value as to whether it is taken. Also creates a
74 * BPHistory object to store any state it will need on squash/update.
75 * @param branch_addr The address of the branch to look up.
76 * @param bp_history Pointer that will be set to the BPHistory object.
77 * @return Whether or not the branch is taken.
78 */
79 bool lookup(ThreadID tid, Addr branch_addr, void * &bp_history);
80
81 /**
82 * Records that there was an unconditional branch, and modifies
83 * the bp history to point to an object that has the previous
84 * global history stored in it.
85 * @param bp_history Pointer that will be set to the BPHistory object.
86 */
87 void uncondBranch(ThreadID tid, Addr pc, void * &bp_history);
88 /**
89 * Updates the branch predictor to Not Taken if a BTB entry is
90 * invalid or not found.
91 * @param branch_addr The address of the branch to look up.
92 * @param bp_history Pointer to any bp history state.
93 * @return Whether or not the branch is taken.
94 */
95 void btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history);
96 /**
97 * Updates the branch predictor with the actual result of a branch.
98 * @param branch_addr The address of the branch to update.
99 * @param taken Whether or not the branch was taken.
100 * @param bp_history Pointer to the BPHistory object that was created
101 * when the branch was predicted.
102 * @param squashed is set when this function is called during a squash
103 * operation.
104 * @param inst Static instruction information
105 * @param corrTarget Resolved target of the branch (only needed if
106 * squashed)
104 */
105 void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
107 */
108 void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
106 bool squashed);
109 bool squashed, const StaticInstPtr & inst, Addr corrTarget);
107
108 /**
109 * Restores the global branch history on a squash.
110 * @param bp_history Pointer to the BPHistory object that has the
111 * previous global branch history in it.
112 */
113 void squash(ThreadID tid, void *bp_history);
114
115 unsigned getGHR(ThreadID tid, void *bp_history) const;
116
117 private:
118 /**
119 * Returns if the branch should be taken or not, given a counter
120 * value.
121 * @param count The counter value.
122 */
123 inline bool getPrediction(uint8_t &count);
124
125 /**
126 * Returns the local history index, given a branch address.
127 * @param branch_addr The branch's PC address.
128 */
129 inline unsigned calcLocHistIdx(Addr &branch_addr);
130
131 /** Updates global history as taken. */
132 inline void updateGlobalHistTaken(ThreadID tid);
133
134 /** Updates global history as not taken. */
135 inline void updateGlobalHistNotTaken(ThreadID tid);
136
137 /**
138 * Updates local histories as taken.
139 * @param local_history_idx The local history table entry that
140 * will be updated.
141 */
142 inline void updateLocalHistTaken(unsigned local_history_idx);
143
144 /**
145 * Updates local histories as not taken.
146 * @param local_history_idx The local history table entry that
147 * will be updated.
148 */
149 inline void updateLocalHistNotTaken(unsigned local_history_idx);
150
151 /**
152 * The branch history information that is created upon predicting
153 * a branch. It will be passed back upon updating and squashing,
154 * when the BP can use this information to update/restore its
155 * state properly.
156 */
157 struct BPHistory {
158#ifdef DEBUG
159 BPHistory()
160 { newCount++; }
161 ~BPHistory()
162 { newCount--; }
163
164 static int newCount;
165#endif
166 unsigned globalHistory;
167 unsigned localHistoryIdx;
168 unsigned localHistory;
169 bool localPredTaken;
170 bool globalPredTaken;
171 bool globalUsed;
172 };
173
174 /** Flag for invalid predictor index */
175 static const int invalidPredictorIndex = -1;
176 /** Local counters. */
177 std::vector<SatCounter> localCtrs;
178
179 /** Number of counters in the local predictor. */
180 unsigned localPredictorSize;
181
182 /** Mask to truncate values stored in the local history table. */
183 unsigned localPredictorMask;
184
185 /** Number of bits of the local predictor's counters. */
186 unsigned localCtrBits;
187
188 /** Array of local history table entries. */
189 std::vector<unsigned> localHistoryTable;
190
191 /** Number of entries in the local history table. */
192 unsigned localHistoryTableSize;
193
194 /** Number of bits for each entry of the local history table. */
195 unsigned localHistoryBits;
196
197 /** Array of counters that make up the global predictor. */
198 std::vector<SatCounter> globalCtrs;
199
200 /** Number of entries in the global predictor. */
201 unsigned globalPredictorSize;
202
203 /** Number of bits of the global predictor's counters. */
204 unsigned globalCtrBits;
205
206 /** Global history register. Contains as much history as specified by
207 * globalHistoryBits. Actual number of bits used is determined by
208 * globalHistoryMask and choiceHistoryMask. */
209 std::vector<unsigned> globalHistory;
210
211 /** Number of bits for the global history. Determines maximum number of
212 entries in global and choice predictor tables. */
213 unsigned globalHistoryBits;
214
215 /** Mask to apply to globalHistory to access global history table.
216 * Based on globalPredictorSize.*/
217 unsigned globalHistoryMask;
218
219 /** Mask to apply to globalHistory to access choice history table.
220 * Based on choicePredictorSize.*/
221 unsigned choiceHistoryMask;
222
223 /** Mask to control how much history is stored. All of it might not be
224 * used. */
225 unsigned historyRegisterMask;
226
227 /** Array of counters that make up the choice predictor. */
228 std::vector<SatCounter> choiceCtrs;
229
230 /** Number of entries in the choice predictor. */
231 unsigned choicePredictorSize;
232
233 /** Number of bits in the choice predictor's counters. */
234 unsigned choiceCtrBits;
235
236 /** Thresholds for the counter value; above the threshold is taken,
237 * equal to or below the threshold is not taken.
238 */
239 unsigned localThreshold;
240 unsigned globalThreshold;
241 unsigned choiceThreshold;
242};
243
244#endif // __CPU_PRED_TOURNAMENT_PRED_HH__
110
111 /**
112 * Restores the global branch history on a squash.
113 * @param bp_history Pointer to the BPHistory object that has the
114 * previous global branch history in it.
115 */
116 void squash(ThreadID tid, void *bp_history);
117
118 unsigned getGHR(ThreadID tid, void *bp_history) const;
119
120 private:
121 /**
122 * Returns if the branch should be taken or not, given a counter
123 * value.
124 * @param count The counter value.
125 */
126 inline bool getPrediction(uint8_t &count);
127
128 /**
129 * Returns the local history index, given a branch address.
130 * @param branch_addr The branch's PC address.
131 */
132 inline unsigned calcLocHistIdx(Addr &branch_addr);
133
134 /** Updates global history as taken. */
135 inline void updateGlobalHistTaken(ThreadID tid);
136
137 /** Updates global history as not taken. */
138 inline void updateGlobalHistNotTaken(ThreadID tid);
139
140 /**
141 * Updates local histories as taken.
142 * @param local_history_idx The local history table entry that
143 * will be updated.
144 */
145 inline void updateLocalHistTaken(unsigned local_history_idx);
146
147 /**
148 * Updates local histories as not taken.
149 * @param local_history_idx The local history table entry that
150 * will be updated.
151 */
152 inline void updateLocalHistNotTaken(unsigned local_history_idx);
153
154 /**
155 * The branch history information that is created upon predicting
156 * a branch. It will be passed back upon updating and squashing,
157 * when the BP can use this information to update/restore its
158 * state properly.
159 */
160 struct BPHistory {
161#ifdef DEBUG
162 BPHistory()
163 { newCount++; }
164 ~BPHistory()
165 { newCount--; }
166
167 static int newCount;
168#endif
169 unsigned globalHistory;
170 unsigned localHistoryIdx;
171 unsigned localHistory;
172 bool localPredTaken;
173 bool globalPredTaken;
174 bool globalUsed;
175 };
176
177 /** Flag for invalid predictor index */
178 static const int invalidPredictorIndex = -1;
179 /** Local counters. */
180 std::vector<SatCounter> localCtrs;
181
182 /** Number of counters in the local predictor. */
183 unsigned localPredictorSize;
184
185 /** Mask to truncate values stored in the local history table. */
186 unsigned localPredictorMask;
187
188 /** Number of bits of the local predictor's counters. */
189 unsigned localCtrBits;
190
191 /** Array of local history table entries. */
192 std::vector<unsigned> localHistoryTable;
193
194 /** Number of entries in the local history table. */
195 unsigned localHistoryTableSize;
196
197 /** Number of bits for each entry of the local history table. */
198 unsigned localHistoryBits;
199
200 /** Array of counters that make up the global predictor. */
201 std::vector<SatCounter> globalCtrs;
202
203 /** Number of entries in the global predictor. */
204 unsigned globalPredictorSize;
205
206 /** Number of bits of the global predictor's counters. */
207 unsigned globalCtrBits;
208
209 /** Global history register. Contains as much history as specified by
210 * globalHistoryBits. Actual number of bits used is determined by
211 * globalHistoryMask and choiceHistoryMask. */
212 std::vector<unsigned> globalHistory;
213
214 /** Number of bits for the global history. Determines maximum number of
215 entries in global and choice predictor tables. */
216 unsigned globalHistoryBits;
217
218 /** Mask to apply to globalHistory to access global history table.
219 * Based on globalPredictorSize.*/
220 unsigned globalHistoryMask;
221
222 /** Mask to apply to globalHistory to access choice history table.
223 * Based on choicePredictorSize.*/
224 unsigned choiceHistoryMask;
225
226 /** Mask to control how much history is stored. All of it might not be
227 * used. */
228 unsigned historyRegisterMask;
229
230 /** Array of counters that make up the choice predictor. */
231 std::vector<SatCounter> choiceCtrs;
232
233 /** Number of entries in the choice predictor. */
234 unsigned choicePredictorSize;
235
236 /** Number of bits in the choice predictor's counters. */
237 unsigned choiceCtrBits;
238
239 /** Thresholds for the counter value; above the threshold is taken,
240 * equal to or below the threshold is not taken.
241 */
242 unsigned localThreshold;
243 unsigned globalThreshold;
244 unsigned choiceThreshold;
245};
246
247#endif // __CPU_PRED_TOURNAMENT_PRED_HH__