tournament.hh revision 9480:d059f8a95a42
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 *          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
54/**
55 * Implements a tournament branch predictor, hopefully identical to the one
56 * used in the 21264.  It has a local predictor, which uses a local history
57 * table to index into a table of counters, and a global predictor, which
58 * uses a global history to index into a table of counters.  A choice
59 * predictor chooses between the two.  Only the global history register
60 * is speculatively updated, the rest are updated upon branches committing
61 * or misspeculating.
62 */
63class TournamentBP : public BPredUnit
64{
65  public:
66    /**
67     * Default branch predictor constructor.
68     */
69    TournamentBP(const Params *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(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(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(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     */
105    void update(Addr branch_addr, bool taken, void *bp_history, bool squashed);
106
107    /**
108     * Restores the global branch history on a squash.
109     * @param bp_history Pointer to the BPHistory object that has the
110     * previous global branch history in it.
111     */
112    void squash(void *bp_history);
113
114    /** Returns the global history. */
115    inline unsigned readGlobalHist() { return globalHistory; }
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();
133
134    /** Updates global history as not taken. */
135    inline void updateGlobalHistNotTaken();
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 localHistory;
168        bool localPredTaken;
169        bool globalPredTaken;
170        bool globalUsed;
171    };
172
173    /** Flag for invalid predictor index */
174    static const int invalidPredictorIndex = -1;
175    /** Local counters. */
176    std::vector<SatCounter> localCtrs;
177
178    /** Number of counters in the local predictor. */
179    unsigned localPredictorSize;
180
181    /** Mask to truncate values stored in the local history table. */
182    unsigned localPredictorMask;
183
184    /** Number of bits of the local predictor's counters. */
185    unsigned localCtrBits;
186
187    /** Array of local history table entries. */
188    std::vector<unsigned> localHistoryTable;
189
190    /** Number of entries in the local history table. */
191    unsigned localHistoryTableSize;
192
193    /** Number of bits for each entry of the local history table. */
194    unsigned localHistoryBits;
195
196    /** Array of counters that make up the global predictor. */
197    std::vector<SatCounter> globalCtrs;
198
199    /** Number of entries in the global predictor. */
200    unsigned globalPredictorSize;
201
202    /** Number of bits of the global predictor's counters. */
203    unsigned globalCtrBits;
204
205    /** Global history register. Contains as much history as specified by
206     *  globalHistoryBits. Actual number of bits used is determined by
207     *  globalHistoryMask and choiceHistoryMask. */
208    unsigned globalHistory;
209
210    /** Number of bits for the global history. Determines maximum number of
211        entries in global and choice predictor tables. */
212    unsigned globalHistoryBits;
213
214    /** Mask to apply to globalHistory to access global history table.
215     *  Based on globalPredictorSize.*/
216    unsigned globalHistoryMask;
217
218    /** Mask to apply to globalHistory to access choice history table.
219     *  Based on choicePredictorSize.*/
220    unsigned choiceHistoryMask;
221
222    /** Mask to control how much history is stored. All of it might not be
223     *  used. */
224    unsigned historyRegisterMask;
225
226    /** Array of counters that make up the choice predictor. */
227    std::vector<SatCounter> choiceCtrs;
228
229    /** Number of entries in the choice predictor. */
230    unsigned choicePredictorSize;
231
232    /** Number of bits in the choice predictor's counters. */
233    unsigned choiceCtrBits;
234
235    /** Number of bits to shift the instruction over to get rid of the word
236     *  offset.
237     */
238    unsigned instShiftAmt;
239
240    /** Thresholds for the counter value; above the threshold is taken,
241     *  equal to or below the threshold is not taken.
242     */
243    unsigned localThreshold;
244    unsigned globalThreshold;
245    unsigned choiceThreshold;
246};
247
248#endif // __CPU_PRED_TOURNAMENT_PRED_HH__
249