tournament.hh revision 13960
16892SBrad.Beckmann@amd.com/*
26892SBrad.Beckmann@amd.com * Copyright (c) 2011, 2014 ARM Limited
36892SBrad.Beckmann@amd.com * All rights reserved
46892SBrad.Beckmann@amd.com *
56892SBrad.Beckmann@amd.com * The license below extends only to copyright in the software and shall
66892SBrad.Beckmann@amd.com * not be construed as granting a license to any other intellectual
76892SBrad.Beckmann@amd.com * property including but not limited to intellectual property relating
86892SBrad.Beckmann@amd.com * to a hardware implementation of the functionality of the software
96892SBrad.Beckmann@amd.com * licensed hereunder.  You may use the software subject to the license
106892SBrad.Beckmann@amd.com * terms below provided that you ensure that this notice is replicated
116892SBrad.Beckmann@amd.com * unmodified and in its entirety in all distributions of the software,
126892SBrad.Beckmann@amd.com * modified or unmodified, in source code or in binary form.
136892SBrad.Beckmann@amd.com *
146892SBrad.Beckmann@amd.com * Copyright (c) 2004-2006 The Regents of The University of Michigan
156892SBrad.Beckmann@amd.com * All rights reserved.
166892SBrad.Beckmann@amd.com *
176892SBrad.Beckmann@amd.com * Redistribution and use in source and binary forms, with or without
186892SBrad.Beckmann@amd.com * modification, are permitted provided that the following conditions are
196892SBrad.Beckmann@amd.com * met: redistributions of source code must retain the above copyright
206892SBrad.Beckmann@amd.com * notice, this list of conditions and the following disclaimer;
216892SBrad.Beckmann@amd.com * redistributions in binary form must reproduce the above copyright
226892SBrad.Beckmann@amd.com * notice, this list of conditions and the following disclaimer in the
236892SBrad.Beckmann@amd.com * documentation and/or other materials provided with the distribution;
246892SBrad.Beckmann@amd.com * neither the name of the copyright holders nor the names of its
256892SBrad.Beckmann@amd.com * contributors may be used to endorse or promote products derived from
266892SBrad.Beckmann@amd.com * this software without specific prior written permission.
276892SBrad.Beckmann@amd.com *
286892SBrad.Beckmann@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296892SBrad.Beckmann@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
307563SBrad.Beckmann@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316892SBrad.Beckmann@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326892SBrad.Beckmann@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336892SBrad.Beckmann@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346892SBrad.Beckmann@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
357538SBrad.Beckmann@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
367538SBrad.Beckmann@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
377538SBrad.Beckmann@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
387538SBrad.Beckmann@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
397538SBrad.Beckmann@amd.com *
407538SBrad.Beckmann@amd.com * Authors: Kevin Lim
417661Snate@binkert.org *          Timothy M. Jones
427538SBrad.Beckmann@amd.com *          Nilay Vaish
438612Stushar@csail.mit.edu */
448612Stushar@csail.mit.edu
457538SBrad.Beckmann@amd.com#ifndef __CPU_PRED_TOURNAMENT_PRED_HH__
467538SBrad.Beckmann@amd.com#define __CPU_PRED_TOURNAMENT_PRED_HH__
477917SBrad.Beckmann@amd.com
487563SBrad.Beckmann@amd.com#include <vector>
497563SBrad.Beckmann@amd.com
507538SBrad.Beckmann@amd.com#include "base/sat_counter.hh"
517538SBrad.Beckmann@amd.com#include "base/types.hh"
527538SBrad.Beckmann@amd.com#include "cpu/pred/bpred_unit.hh"
537538SBrad.Beckmann@amd.com#include "params/TournamentBP.hh"
547538SBrad.Beckmann@amd.com
557566SBrad.Beckmann@amd.com/**
567566SBrad.Beckmann@amd.com * Implements a tournament branch predictor, hopefully identical to the one
577809Snilay@cs.wisc.edu * used in the 21264.  It has a local predictor, which uses a local history
587809Snilay@cs.wisc.edu * table to index into a table of counters, and a global predictor, which
597809Snilay@cs.wisc.edu * uses a global history to index into a table of counters.  A choice
607809Snilay@cs.wisc.edu * predictor chooses between the two.  Both the global history register
618638Sgloh * and the selected local history are speculatively updated.
628638Sgloh */
637538SBrad.Beckmann@amd.comclass TournamentBP : public BPredUnit
647538SBrad.Beckmann@amd.com{
657538SBrad.Beckmann@amd.com  public:
667538SBrad.Beckmann@amd.com    /**
677541SBrad.Beckmann@amd.com     * Default branch predictor constructor.
686892SBrad.Beckmann@amd.com     */
698638Sgloh    TournamentBP(const TournamentBPParams *params);
708638Sgloh
718436SBrad.Beckmann@amd.com    /**
728436SBrad.Beckmann@amd.com     * Looks up the given address in the branch predictor and returns
737032SBrad.Beckmann@amd.com     * a true/false value as to whether it is taken.  Also creates a
747032SBrad.Beckmann@amd.com     * BPHistory object to store any state it will need on squash/update.
756923SBrad.Beckmann@amd.com     * @param branch_addr The address of the branch to look up.
766893SBrad.Beckmann@amd.com     * @param bp_history Pointer that will be set to the BPHistory object.
778436SBrad.Beckmann@amd.com     * @return Whether or not the branch is taken.
788436SBrad.Beckmann@amd.com     */
797557SBrad.Beckmann@amd.com    bool lookup(ThreadID tid, Addr branch_addr, void * &bp_history);
806923SBrad.Beckmann@amd.com
816923SBrad.Beckmann@amd.com    /**
827557SBrad.Beckmann@amd.com     * Records that there was an unconditional branch, and modifies
838257SBrad.Beckmann@amd.com     * the bp history to point to an object that has the previous
848257SBrad.Beckmann@amd.com     * global history stored in it.
858257SBrad.Beckmann@amd.com     * @param bp_history Pointer that will be set to the BPHistory object.
868257SBrad.Beckmann@amd.com     */
878257SBrad.Beckmann@amd.com    void uncondBranch(ThreadID tid, Addr pc, void * &bp_history);
888257SBrad.Beckmann@amd.com    /**
898257SBrad.Beckmann@amd.com     * Updates the branch predictor to Not Taken if a BTB entry is
908257SBrad.Beckmann@amd.com     * invalid or not found.
918257SBrad.Beckmann@amd.com     * @param branch_addr The address of the branch to look up.
928257SBrad.Beckmann@amd.com     * @param bp_history Pointer to any bp history state.
938257SBrad.Beckmann@amd.com     * @return Whether or not the branch is taken.
948257SBrad.Beckmann@amd.com     */
958257SBrad.Beckmann@amd.com    void btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history);
968257SBrad.Beckmann@amd.com    /**
978257SBrad.Beckmann@amd.com     * Updates the branch predictor with the actual result of a branch.
988257SBrad.Beckmann@amd.com     * @param branch_addr The address of the branch to update.
998258SBrad.Beckmann@amd.com     * @param taken Whether or not the branch was taken.
1008258SBrad.Beckmann@amd.com     * @param bp_history Pointer to the BPHistory object that was created
1018257SBrad.Beckmann@amd.com     * when the branch was predicted.
1028257SBrad.Beckmann@amd.com     * @param squashed is set when this function is called during a squash
1036892SBrad.Beckmann@amd.com     * operation.
1047032SBrad.Beckmann@amd.com     * @param inst Static instruction information
1057032SBrad.Beckmann@amd.com     * @param corrTarget Resolved target of the branch (only needed if
1066892SBrad.Beckmann@amd.com     * squashed)
1077032SBrad.Beckmann@amd.com     */
1087032SBrad.Beckmann@amd.com    void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
1098257SBrad.Beckmann@amd.com                bool squashed, const StaticInstPtr & inst, Addr corrTarget);
1108257SBrad.Beckmann@amd.com
1118257SBrad.Beckmann@amd.com    /**
1127557SBrad.Beckmann@amd.com     * Restores the global branch history on a squash.
1137032SBrad.Beckmann@amd.com     * @param bp_history Pointer to the BPHistory object that has the
1147032SBrad.Beckmann@amd.com     * previous global branch history in it.
1157557SBrad.Beckmann@amd.com     */
1168257SBrad.Beckmann@amd.com    void squash(ThreadID tid, void *bp_history);
1178612Stushar@csail.mit.edu
1188612Stushar@csail.mit.edu  private:
1198612Stushar@csail.mit.edu    /**
1208612Stushar@csail.mit.edu     * Returns if the branch should be taken or not, given a counter
1218612Stushar@csail.mit.edu     * value.
1228612Stushar@csail.mit.edu     * @param count The counter value.
1238612Stushar@csail.mit.edu     */
1246892SBrad.Beckmann@amd.com    inline bool getPrediction(uint8_t &count);
1256903SBrad.Beckmann@amd.com
1267563SBrad.Beckmann@amd.com    /**
1277025SBrad.Beckmann@amd.com     * Returns the local history index, given a branch address.
1287025SBrad.Beckmann@amd.com     * @param branch_addr The branch's PC address.
1297025SBrad.Beckmann@amd.com     */
1307025SBrad.Beckmann@amd.com    inline unsigned calcLocHistIdx(Addr &branch_addr);
1317563SBrad.Beckmann@amd.com
1326903SBrad.Beckmann@amd.com    /** Updates global history as taken. */
1336903SBrad.Beckmann@amd.com    inline void updateGlobalHistTaken(ThreadID tid);
1347563SBrad.Beckmann@amd.com
1357563SBrad.Beckmann@amd.com    /** Updates global history as not taken. */
1367563SBrad.Beckmann@amd.com    inline void updateGlobalHistNotTaken(ThreadID tid);
1377563SBrad.Beckmann@amd.com
1387563SBrad.Beckmann@amd.com    /**
1397563SBrad.Beckmann@amd.com     * Updates local histories as taken.
1407563SBrad.Beckmann@amd.com     * @param local_history_idx The local history table entry that
1417663SBrad.Beckmann@amd.com     * will be updated.
1427663SBrad.Beckmann@amd.com     */
1437663SBrad.Beckmann@amd.com    inline void updateLocalHistTaken(unsigned local_history_idx);
1447663SBrad.Beckmann@amd.com
1457663SBrad.Beckmann@amd.com    /**
1467563SBrad.Beckmann@amd.com     * Updates local histories as not taken.
1476903SBrad.Beckmann@amd.com     * @param local_history_idx The local history table entry that
1486903SBrad.Beckmann@amd.com     * will be updated.
1497563SBrad.Beckmann@amd.com     */
1507563SBrad.Beckmann@amd.com    inline void updateLocalHistNotTaken(unsigned local_history_idx);
1517541SBrad.Beckmann@amd.com
1527541SBrad.Beckmann@amd.com    /**
1536905SBrad.Beckmann@amd.com     * The branch history information that is created upon predicting
1546892SBrad.Beckmann@amd.com     * a branch.  It will be passed back upon updating and squashing,
1558436SBrad.Beckmann@amd.com     * when the BP can use this information to update/restore its
1568436SBrad.Beckmann@amd.com     * state properly.
1578436SBrad.Beckmann@amd.com     */
1586892SBrad.Beckmann@amd.com    struct BPHistory {
1598436SBrad.Beckmann@amd.com#ifdef DEBUG
1608436SBrad.Beckmann@amd.com        BPHistory()
1618436SBrad.Beckmann@amd.com        { newCount++; }
1628436SBrad.Beckmann@amd.com        ~BPHistory()
1638322Ssteve.reinhardt@amd.com        { newCount--; }
1647809Snilay@cs.wisc.edu
165        static int newCount;
166#endif
167        unsigned globalHistory;
168        unsigned localHistoryIdx;
169        unsigned localHistory;
170        bool localPredTaken;
171        bool globalPredTaken;
172        bool globalUsed;
173    };
174
175    /** Flag for invalid predictor index */
176    static const int invalidPredictorIndex = -1;
177    /** Number of counters in the local predictor. */
178    unsigned localPredictorSize;
179
180    /** Mask to truncate values stored in the local history table. */
181    unsigned localPredictorMask;
182
183    /** Number of bits of the local predictor's counters. */
184    unsigned localCtrBits;
185
186    /** Local counters. */
187    std::vector<SatCounter> localCtrs;
188
189    /** Array of local history table entries. */
190    std::vector<unsigned> localHistoryTable;
191
192    /** Number of entries in the local history table. */
193    unsigned localHistoryTableSize;
194
195    /** Number of bits for each entry of the local history table. */
196    unsigned localHistoryBits;
197
198    /** Number of entries in the global predictor. */
199    unsigned globalPredictorSize;
200
201    /** Number of bits of the global predictor's counters. */
202    unsigned globalCtrBits;
203
204    /** Array of counters that make up the global predictor. */
205    std::vector<SatCounter> globalCtrs;
206
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;
211
212    /** Number of bits for the global history. Determines maximum number of
213        entries in global and choice predictor tables. */
214    unsigned globalHistoryBits;
215
216    /** Mask to apply to globalHistory to access global history table.
217     *  Based on globalPredictorSize.*/
218    unsigned globalHistoryMask;
219
220    /** Mask to apply to globalHistory to access choice history table.
221     *  Based on choicePredictorSize.*/
222    unsigned choiceHistoryMask;
223
224    /** Mask to control how much history is stored. All of it might not be
225     *  used. */
226    unsigned historyRegisterMask;
227
228    /** Number of entries in the choice predictor. */
229    unsigned choicePredictorSize;
230
231    /** Number of bits in the choice predictor's counters. */
232    unsigned choiceCtrBits;
233
234    /** Array of counters that make up the choice predictor. */
235    std::vector<SatCounter> choiceCtrs;
236
237    /** Thresholds for the counter value; above the threshold is taken,
238     *  equal to or below the threshold is not taken.
239     */
240    unsigned localThreshold;
241    unsigned globalThreshold;
242    unsigned choiceThreshold;
243};
244
245#endif // __CPU_PRED_TOURNAMENT_PRED_HH__
246