2 * Copyright (c) 2011, 2014 ARM Limited
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.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
45 #ifndef __CPU_PRED_TOURNAMENT_PRED_HH__
46 #define __CPU_PRED_TOURNAMENT_PRED_HH__
50 #include "base/sat_counter.hh"
51 #include "base/types.hh"
52 #include "cpu/pred/bpred_unit.hh"
53 #include "params/TournamentBP.hh"
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.
63 class TournamentBP : public BPredUnit
67 * Default branch predictor constructor.
69 TournamentBP(const TournamentBPParams *params);
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.
79 bool lookup(ThreadID tid, Addr branch_addr, void * &bp_history);
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.
87 void uncondBranch(ThreadID tid, Addr pc, void * &bp_history);
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.
95 void btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history);
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
104 * @param inst Static instruction information
105 * @param corrTarget Resolved target of the branch (only needed if
108 void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
109 bool squashed, const StaticInstPtr & inst, Addr corrTarget);
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.
116 void squash(ThreadID tid, void *bp_history);
120 * Returns if the branch should be taken or not, given a counter
122 * @param count The counter value.
124 inline bool getPrediction(uint8_t &count);
127 * Returns the local history index, given a branch address.
128 * @param branch_addr The branch's PC address.
130 inline unsigned calcLocHistIdx(Addr &branch_addr);
132 /** Updates global history as taken. */
133 inline void updateGlobalHistTaken(ThreadID tid);
135 /** Updates global history as not taken. */
136 inline void updateGlobalHistNotTaken(ThreadID tid);
139 * Updates local histories as taken.
140 * @param local_history_idx The local history table entry that
143 inline void updateLocalHistTaken(unsigned local_history_idx);
146 * Updates local histories as not taken.
147 * @param local_history_idx The local history table entry that
150 inline void updateLocalHistNotTaken(unsigned local_history_idx);
153 * The branch history information that is created upon predicting
154 * a branch. It will be passed back upon updating and squashing,
155 * when the BP can use this information to update/restore its
167 unsigned globalHistory;
168 unsigned localHistoryIdx;
169 unsigned localHistory;
171 bool globalPredTaken;
175 /** Flag for invalid predictor index */
176 static const int invalidPredictorIndex = -1;
177 /** Number of counters in the local predictor. */
178 unsigned localPredictorSize;
180 /** Mask to truncate values stored in the local history table. */
181 unsigned localPredictorMask;
183 /** Number of bits of the local predictor's counters. */
184 unsigned localCtrBits;
186 /** Local counters. */
187 std::vector<SatCounter> localCtrs;
189 /** Array of local history table entries. */
190 std::vector<unsigned> localHistoryTable;
192 /** Number of entries in the local history table. */
193 unsigned localHistoryTableSize;
195 /** Number of bits for each entry of the local history table. */
196 unsigned localHistoryBits;
198 /** Number of entries in the global predictor. */
199 unsigned globalPredictorSize;
201 /** Number of bits of the global predictor's counters. */
202 unsigned globalCtrBits;
204 /** Array of counters that make up the global predictor. */
205 std::vector<SatCounter> globalCtrs;
207 /** Global history register. Contains as much history as specified by
208 * globalHistoryBits. Actual number of bits used is determined by
209 * globalHistoryMask and choiceHistoryMask. */
210 std::vector<unsigned> globalHistory;
212 /** Number of bits for the global history. Determines maximum number of
213 entries in global and choice predictor tables. */
214 unsigned globalHistoryBits;
216 /** Mask to apply to globalHistory to access global history table.
217 * Based on globalPredictorSize.*/
218 unsigned globalHistoryMask;
220 /** Mask to apply to globalHistory to access choice history table.
221 * Based on choicePredictorSize.*/
222 unsigned choiceHistoryMask;
224 /** Mask to control how much history is stored. All of it might not be
226 unsigned historyRegisterMask;
228 /** Number of entries in the choice predictor. */
229 unsigned choicePredictorSize;
231 /** Number of bits in the choice predictor's counters. */
232 unsigned choiceCtrBits;
234 /** Array of counters that make up the choice predictor. */
235 std::vector<SatCounter> choiceCtrs;
237 /** Thresholds for the counter value; above the threshold is taken,
238 * equal to or below the threshold is not taken.
240 unsigned localThreshold;
241 unsigned globalThreshold;
242 unsigned choiceThreshold;
245 #endif // __CPU_PRED_TOURNAMENT_PRED_HH__