2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
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.
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.
31 #ifndef __CPU_O3_TOURNAMENT_PRED_HH__
32 #define __CPU_O3_TOURNAMENT_PRED_HH__
34 #include "cpu/o3/sat_counter.hh"
35 #include "sim/host.hh"
39 * Implements a tournament branch predictor, hopefully identical to the one
40 * used in the 21264. It has a local predictor, which uses a local history
41 * table to index into a table of counters, and a global predictor, which
42 * uses a global history to index into a table of counters. A choice
43 * predictor chooses between the two. Only the global history register
44 * is speculatively updated, the rest are updated upon branches committing
51 * Default branch predictor constructor.
53 TournamentBP(unsigned localPredictorSize,
54 unsigned localCtrBits,
55 unsigned localHistoryTableSize,
56 unsigned localHistoryBits,
57 unsigned globalPredictorSize,
58 unsigned globalHistoryBits,
59 unsigned globalCtrBits,
60 unsigned choicePredictorSize,
61 unsigned choiceCtrBits,
62 unsigned instShiftAmt);
65 * Looks up the given address in the branch predictor and returns
66 * a true/false value as to whether it is taken. Also creates a
67 * BPHistory object to store any state it will need on squash/update.
68 * @param branch_addr The address of the branch to look up.
69 * @param bp_history Pointer that will be set to the BPHistory object.
70 * @return Whether or not the branch is taken.
72 bool lookup(Addr &branch_addr, void * &bp_history);
75 * Records that there was an unconditional branch, and modifies
76 * the bp history to point to an object that has the previous
77 * global history stored in it.
78 * @param bp_history Pointer that will be set to the BPHistory object.
80 void uncondBr(void * &bp_history);
83 * Updates the branch predictor with the actual result of a branch.
84 * @param branch_addr The address of the branch to update.
85 * @param taken Whether or not the branch was taken.
86 * @param bp_history Pointer to the BPHistory object that was created
87 * when the branch was predicted.
89 void update(Addr &branch_addr, bool taken, void *bp_history);
92 * Restores the global branch history on a squash.
93 * @param bp_history Pointer to the BPHistory object that has the
94 * previous global branch history in it.
96 void squash(void *bp_history);
98 /** Returns the global history. */
99 inline unsigned readGlobalHist() { return globalHistory; }
103 * Returns if the branch should be taken or not, given a counter
105 * @param count The counter value.
107 inline bool getPrediction(uint8_t &count);
110 * Returns the local history index, given a branch address.
111 * @param branch_addr The branch's PC address.
113 inline unsigned calcLocHistIdx(Addr &branch_addr);
115 /** Updates global history as taken. */
116 inline void updateGlobalHistTaken();
118 /** Updates global history as not taken. */
119 inline void updateGlobalHistNotTaken();
122 * Updates local histories as taken.
123 * @param local_history_idx The local history table entry that
126 inline void updateLocalHistTaken(unsigned local_history_idx);
129 * Updates local histories as not taken.
130 * @param local_history_idx The local history table entry that
133 inline void updateLocalHistNotTaken(unsigned local_history_idx);
136 * The branch history information that is created upon predicting
137 * a branch. It will be passed back upon updating and squashing,
138 * when the BP can use this information to update/restore its
150 unsigned globalHistory;
152 bool globalPredTaken;
156 /** Local counters. */
157 std::vector<SatCounter> localCtrs;
159 /** Size of the local predictor. */
160 unsigned localPredictorSize;
162 /** Number of bits of the local predictor's counters. */
163 unsigned localCtrBits;
165 /** Array of local history table entries. */
166 std::vector<unsigned> localHistoryTable;
168 /** Size of the local history table. */
169 unsigned localHistoryTableSize;
171 /** Number of bits for each entry of the local history table.
172 * @todo Doesn't this come from the size of the local predictor?
174 unsigned localHistoryBits;
176 /** Mask to get the proper local history. */
177 unsigned localHistoryMask;
179 /** Array of counters that make up the global predictor. */
180 std::vector<SatCounter> globalCtrs;
182 /** Size of the global predictor. */
183 unsigned globalPredictorSize;
185 /** Number of bits of the global predictor's counters. */
186 unsigned globalCtrBits;
188 /** Global history register. */
189 unsigned globalHistory;
191 /** Number of bits for the global history. */
192 unsigned globalHistoryBits;
194 /** Mask to get the proper global history. */
195 unsigned globalHistoryMask;
197 /** Array of counters that make up the choice predictor. */
198 std::vector<SatCounter> choiceCtrs;
200 /** Size of the choice predictor (identical to the global predictor). */
201 unsigned choicePredictorSize;
203 /** Number of bits of the choice predictor's counters. */
204 unsigned choiceCtrBits;
206 /** Number of bits to shift the instruction over to get rid of the word
209 unsigned instShiftAmt;
211 /** Threshold for the counter value; above the threshold is taken,
212 * equal to or below the threshold is not taken.
217 #endif // __CPU_O3_TOURNAMENT_PRED_HH__