tournament.cc revision 9480
16184SN/A/*
28842Smrinmoy.ghosh@arm.com * Copyright (c) 2011 ARM Limited
38842Smrinmoy.ghosh@arm.com * All rights reserved
48842Smrinmoy.ghosh@arm.com *
58842Smrinmoy.ghosh@arm.com * The license below extends only to copyright in the software and shall
68842Smrinmoy.ghosh@arm.com * not be construed as granting a license to any other intellectual
78842Smrinmoy.ghosh@arm.com * property including but not limited to intellectual property relating
88842Smrinmoy.ghosh@arm.com * to a hardware implementation of the functionality of the software
98842Smrinmoy.ghosh@arm.com * licensed hereunder.  You may use the software subject to the license
108842Smrinmoy.ghosh@arm.com * terms below provided that you ensure that this notice is replicated
118842Smrinmoy.ghosh@arm.com * unmodified and in its entirety in all distributions of the software,
128842Smrinmoy.ghosh@arm.com * modified or unmodified, in source code or in binary form.
138842Smrinmoy.ghosh@arm.com *
146184SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
156184SN/A * All rights reserved.
166184SN/A *
176184SN/A * Redistribution and use in source and binary forms, with or without
186184SN/A * modification, are permitted provided that the following conditions are
196184SN/A * met: redistributions of source code must retain the above copyright
206184SN/A * notice, this list of conditions and the following disclaimer;
216184SN/A * redistributions in binary form must reproduce the above copyright
226184SN/A * notice, this list of conditions and the following disclaimer in the
236184SN/A * documentation and/or other materials provided with the distribution;
246184SN/A * neither the name of the copyright holders nor the names of its
256184SN/A * contributors may be used to endorse or promote products derived from
266184SN/A * this software without specific prior written permission.
276184SN/A *
286184SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296184SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306184SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316184SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326184SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336184SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346184SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356184SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366184SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376184SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386184SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396184SN/A *
406184SN/A * Authors: Kevin Lim
416184SN/A */
426184SN/A
439360SE.Tomusk@sms.ed.ac.uk#include "base/bitfield.hh"
446184SN/A#include "base/intmath.hh"
456226Snate@binkert.org#include "cpu/pred/tournament.hh"
466184SN/A
479480Snilay@cs.wisc.eduTournamentBP::TournamentBP(const Params *params)
489480Snilay@cs.wisc.edu    : BPredUnit(params),
499480Snilay@cs.wisc.edu      localCtrBits(params->localCtrBits),
509480Snilay@cs.wisc.edu      localHistoryTableSize(params->localHistoryTableSize),
519480Snilay@cs.wisc.edu      localHistoryBits(params->localHistoryBits),
529480Snilay@cs.wisc.edu      globalPredictorSize(params->globalPredictorSize),
539480Snilay@cs.wisc.edu      globalCtrBits(params->globalCtrBits),
549480Snilay@cs.wisc.edu      globalHistoryBits(params->globalHistoryBits),
559480Snilay@cs.wisc.edu      choicePredictorSize(params->choicePredictorSize),
569480Snilay@cs.wisc.edu      choiceCtrBits(params->choiceCtrBits),
579480Snilay@cs.wisc.edu      instShiftAmt(params->instShiftAmt)
586184SN/A{
599360SE.Tomusk@sms.ed.ac.uk    localPredictorSize = ULL(1) << localHistoryBits;
606184SN/A
619360SE.Tomusk@sms.ed.ac.uk    //Set up the array of counters for the local predictor
626184SN/A    localCtrs.resize(localPredictorSize);
636184SN/A
646184SN/A    for (int i = 0; i < localPredictorSize; ++i)
656184SN/A        localCtrs[i].setBits(localCtrBits);
666184SN/A
679360SE.Tomusk@sms.ed.ac.uk    localPredictorMask = mask(localHistoryBits);
686184SN/A
696184SN/A    if (!isPowerOf2(localHistoryTableSize)) {
706184SN/A        fatal("Invalid local history table size!\n");
716184SN/A    }
726184SN/A
736184SN/A    //Setup the history table for the local table
746184SN/A    localHistoryTable.resize(localHistoryTableSize);
756184SN/A
766184SN/A    for (int i = 0; i < localHistoryTableSize; ++i)
776184SN/A        localHistoryTable[i] = 0;
786184SN/A
796184SN/A    if (!isPowerOf2(globalPredictorSize)) {
806184SN/A        fatal("Invalid global predictor size!\n");
816184SN/A    }
826184SN/A
836184SN/A    //Setup the array of counters for the global predictor
846184SN/A    globalCtrs.resize(globalPredictorSize);
856184SN/A
866184SN/A    for (int i = 0; i < globalPredictorSize; ++i)
876184SN/A        globalCtrs[i].setBits(globalCtrBits);
886184SN/A
896184SN/A    //Clear the global history
906184SN/A    globalHistory = 0;
919360SE.Tomusk@sms.ed.ac.uk    // Set up the global history mask
929360SE.Tomusk@sms.ed.ac.uk    // this is equivalent to mask(log2(globalPredictorSize)
939360SE.Tomusk@sms.ed.ac.uk    globalHistoryMask = globalPredictorSize - 1;
946184SN/A
956184SN/A    if (!isPowerOf2(choicePredictorSize)) {
966184SN/A        fatal("Invalid choice predictor size!\n");
976184SN/A    }
986184SN/A
999360SE.Tomusk@sms.ed.ac.uk    // Set up choiceHistoryMask
1009360SE.Tomusk@sms.ed.ac.uk    // this is equivalent to mask(log2(choicePredictorSize)
1019360SE.Tomusk@sms.ed.ac.uk    choiceHistoryMask = choicePredictorSize - 1;
1029360SE.Tomusk@sms.ed.ac.uk
1036184SN/A    //Setup the array of counters for the choice predictor
1046184SN/A    choiceCtrs.resize(choicePredictorSize);
1056184SN/A
1066184SN/A    for (int i = 0; i < choicePredictorSize; ++i)
1076184SN/A        choiceCtrs[i].setBits(choiceCtrBits);
1086184SN/A
1099360SE.Tomusk@sms.ed.ac.uk    //Set up historyRegisterMask
1109360SE.Tomusk@sms.ed.ac.uk    historyRegisterMask = mask(globalHistoryBits);
1119360SE.Tomusk@sms.ed.ac.uk
1129360SE.Tomusk@sms.ed.ac.uk    //Check that predictors don't use more bits than they have available
1139360SE.Tomusk@sms.ed.ac.uk    if (globalHistoryMask > historyRegisterMask) {
1149360SE.Tomusk@sms.ed.ac.uk        fatal("Global predictor too large for global history bits!\n");
1159360SE.Tomusk@sms.ed.ac.uk    }
1169360SE.Tomusk@sms.ed.ac.uk    if (choiceHistoryMask > historyRegisterMask) {
1179360SE.Tomusk@sms.ed.ac.uk        fatal("Choice predictor too large for global history bits!\n");
1189360SE.Tomusk@sms.ed.ac.uk    }
1199360SE.Tomusk@sms.ed.ac.uk
1209360SE.Tomusk@sms.ed.ac.uk    if (globalHistoryMask < historyRegisterMask &&
1219360SE.Tomusk@sms.ed.ac.uk        choiceHistoryMask < historyRegisterMask) {
1229360SE.Tomusk@sms.ed.ac.uk        inform("More global history bits than required by predictors\n");
1239360SE.Tomusk@sms.ed.ac.uk    }
1249360SE.Tomusk@sms.ed.ac.uk
1259360SE.Tomusk@sms.ed.ac.uk    // Set thresholds for the three predictors' counters
1269360SE.Tomusk@sms.ed.ac.uk    // This is equivalent to (2^(Ctr))/2 - 1
1279360SE.Tomusk@sms.ed.ac.uk    localThreshold  = (ULL(1) << (localCtrBits  - 1)) - 1;
1289360SE.Tomusk@sms.ed.ac.uk    globalThreshold = (ULL(1) << (globalCtrBits - 1)) - 1;
1299360SE.Tomusk@sms.ed.ac.uk    choiceThreshold = (ULL(1) << (choiceCtrBits - 1)) - 1;
1306184SN/A}
1316184SN/A
1326184SN/Ainline
1336184SN/Aunsigned
1346184SN/ATournamentBP::calcLocHistIdx(Addr &branch_addr)
1356184SN/A{
1366184SN/A    // Get low order bits after removing instruction offset.
1376184SN/A    return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
1386184SN/A}
1396184SN/A
1406184SN/Ainline
1416184SN/Avoid
1426184SN/ATournamentBP::updateGlobalHistTaken()
1436184SN/A{
1446184SN/A    globalHistory = (globalHistory << 1) | 1;
1459360SE.Tomusk@sms.ed.ac.uk    globalHistory = globalHistory & historyRegisterMask;
1466184SN/A}
1476184SN/A
1486184SN/Ainline
1496184SN/Avoid
1506184SN/ATournamentBP::updateGlobalHistNotTaken()
1516184SN/A{
1526184SN/A    globalHistory = (globalHistory << 1);
1539360SE.Tomusk@sms.ed.ac.uk    globalHistory = globalHistory & historyRegisterMask;
1546184SN/A}
1556184SN/A
1566184SN/Ainline
1576184SN/Avoid
1586184SN/ATournamentBP::updateLocalHistTaken(unsigned local_history_idx)
1596184SN/A{
1606184SN/A    localHistoryTable[local_history_idx] =
1616184SN/A        (localHistoryTable[local_history_idx] << 1) | 1;
1626184SN/A}
1636184SN/A
1646184SN/Ainline
1656184SN/Avoid
1666184SN/ATournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
1676184SN/A{
1686184SN/A    localHistoryTable[local_history_idx] =
1696184SN/A        (localHistoryTable[local_history_idx] << 1);
1706184SN/A}
1716184SN/A
1728842Smrinmoy.ghosh@arm.com
1738842Smrinmoy.ghosh@arm.comvoid
1749480Snilay@cs.wisc.eduTournamentBP::btbUpdate(Addr branch_addr, void * &bp_history)
1758842Smrinmoy.ghosh@arm.com{
1768842Smrinmoy.ghosh@arm.com    unsigned local_history_idx = calcLocHistIdx(branch_addr);
1779360SE.Tomusk@sms.ed.ac.uk    //Update Global History to Not Taken (clear LSB)
1789360SE.Tomusk@sms.ed.ac.uk    globalHistory &= (historyRegisterMask & ~ULL(1));
1798842Smrinmoy.ghosh@arm.com    //Update Local History to Not Taken
1808842Smrinmoy.ghosh@arm.com    localHistoryTable[local_history_idx] =
1819327Smrinmoy.ghosh@arm.com       localHistoryTable[local_history_idx] & (localPredictorMask & ~ULL(1));
1828842Smrinmoy.ghosh@arm.com}
1838842Smrinmoy.ghosh@arm.com
1846184SN/Abool
1859480Snilay@cs.wisc.eduTournamentBP::lookup(Addr branch_addr, void * &bp_history)
1866184SN/A{
1876184SN/A    bool local_prediction;
1886184SN/A    unsigned local_history_idx;
1896184SN/A    unsigned local_predictor_idx;
1906184SN/A
1916184SN/A    bool global_prediction;
1926184SN/A    bool choice_prediction;
1936184SN/A
1946184SN/A    //Lookup in the local predictor to get its branch prediction
1956184SN/A    local_history_idx = calcLocHistIdx(branch_addr);
1966184SN/A    local_predictor_idx = localHistoryTable[local_history_idx]
1976184SN/A        & localPredictorMask;
1989360SE.Tomusk@sms.ed.ac.uk    local_prediction = localCtrs[local_predictor_idx].read() > localThreshold;
1996184SN/A
2006184SN/A    //Lookup in the global predictor to get its branch prediction
2019360SE.Tomusk@sms.ed.ac.uk    global_prediction =
2029360SE.Tomusk@sms.ed.ac.uk      globalCtrs[globalHistory & globalHistoryMask].read() > globalThreshold;
2036184SN/A
2046184SN/A    //Lookup in the choice predictor to see which one to use
2059360SE.Tomusk@sms.ed.ac.uk    choice_prediction =
2069360SE.Tomusk@sms.ed.ac.uk      choiceCtrs[globalHistory & choiceHistoryMask].read() > choiceThreshold;
2076184SN/A
2086184SN/A    // Create BPHistory and pass it back to be recorded.
2096184SN/A    BPHistory *history = new BPHistory;
2106184SN/A    history->globalHistory = globalHistory;
2116184SN/A    history->localPredTaken = local_prediction;
2126184SN/A    history->globalPredTaken = global_prediction;
2136184SN/A    history->globalUsed = choice_prediction;
2148842Smrinmoy.ghosh@arm.com    history->localHistory = local_predictor_idx;
2156184SN/A    bp_history = (void *)history;
2166184SN/A
2179360SE.Tomusk@sms.ed.ac.uk    assert(local_history_idx < localHistoryTableSize);
2186184SN/A
2196184SN/A    // Commented code is for doing speculative update of counters and
2206184SN/A    // all histories.
2216184SN/A    if (choice_prediction) {
2226184SN/A        if (global_prediction) {
2236184SN/A            updateGlobalHistTaken();
2248842Smrinmoy.ghosh@arm.com            updateLocalHistTaken(local_history_idx);
2256184SN/A            return true;
2266184SN/A        } else {
2276184SN/A            updateGlobalHistNotTaken();
2288842Smrinmoy.ghosh@arm.com            updateLocalHistNotTaken(local_history_idx);
2296184SN/A            return false;
2306184SN/A        }
2316184SN/A    } else {
2326184SN/A        if (local_prediction) {
2336184SN/A            updateGlobalHistTaken();
2348842Smrinmoy.ghosh@arm.com            updateLocalHistTaken(local_history_idx);
2356184SN/A            return true;
2366184SN/A        } else {
2376184SN/A            updateGlobalHistNotTaken();
2388842Smrinmoy.ghosh@arm.com            updateLocalHistNotTaken(local_history_idx);
2396184SN/A            return false;
2406184SN/A        }
2416184SN/A    }
2426184SN/A}
2436184SN/A
2446184SN/Avoid
2459480Snilay@cs.wisc.eduTournamentBP::uncondBranch(void * &bp_history)
2466184SN/A{
2476184SN/A    // Create BPHistory and pass it back to be recorded.
2486184SN/A    BPHistory *history = new BPHistory;
2496184SN/A    history->globalHistory = globalHistory;
2506184SN/A    history->localPredTaken = true;
2516184SN/A    history->globalPredTaken = true;
2528487SAli.Saidi@ARM.com    history->globalUsed = true;
2538842Smrinmoy.ghosh@arm.com    history->localHistory = invalidPredictorIndex;
2546184SN/A    bp_history = static_cast<void *>(history);
2556184SN/A
2566184SN/A    updateGlobalHistTaken();
2576184SN/A}
2586184SN/A
2596184SN/Avoid
2609480Snilay@cs.wisc.eduTournamentBP::update(Addr branch_addr, bool taken, void *bp_history,
2618842Smrinmoy.ghosh@arm.com                     bool squashed)
2626184SN/A{
2636184SN/A    unsigned local_history_idx;
2648842Smrinmoy.ghosh@arm.com    unsigned local_predictor_idx M5_VAR_USED;
2656184SN/A    unsigned local_predictor_hist;
2666184SN/A
2676184SN/A    // Get the local predictor's current prediction
2686184SN/A    local_history_idx = calcLocHistIdx(branch_addr);
2696184SN/A    local_predictor_hist = localHistoryTable[local_history_idx];
2706184SN/A    local_predictor_idx = local_predictor_hist & localPredictorMask;
2716184SN/A
2726184SN/A    if (bp_history) {
2736184SN/A        BPHistory *history = static_cast<BPHistory *>(bp_history);
2748843Smrinmoy.ghosh@arm.com        // Update may also be called if the Branch target is incorrect even if
2758843Smrinmoy.ghosh@arm.com        // the prediction is correct. In that case do not update the counters.
2768843Smrinmoy.ghosh@arm.com        bool historyPred = false;
2779327Smrinmoy.ghosh@arm.com        unsigned old_local_pred_index = history->localHistory &
2789327Smrinmoy.ghosh@arm.com                localPredictorMask;
2799327Smrinmoy.ghosh@arm.com
2809327Smrinmoy.ghosh@arm.com        bool old_local_pred_valid = history->localHistory !=
2819327Smrinmoy.ghosh@arm.com            invalidPredictorIndex;
2829327Smrinmoy.ghosh@arm.com
2839327Smrinmoy.ghosh@arm.com        assert(old_local_pred_index < localPredictorSize);
2849327Smrinmoy.ghosh@arm.com
2858843Smrinmoy.ghosh@arm.com        if (history->globalUsed) {
2868843Smrinmoy.ghosh@arm.com           historyPred = history->globalPredTaken;
2878843Smrinmoy.ghosh@arm.com        } else {
2888843Smrinmoy.ghosh@arm.com           historyPred = history->localPredTaken;
2898843Smrinmoy.ghosh@arm.com        }
2908843Smrinmoy.ghosh@arm.com        if (historyPred != taken || !squashed) {
2918843Smrinmoy.ghosh@arm.com            // Update the choice predictor to tell it which one was correct if
2928843Smrinmoy.ghosh@arm.com            // there was a prediction.
2938843Smrinmoy.ghosh@arm.com            if (history->localPredTaken != history->globalPredTaken) {
2948843Smrinmoy.ghosh@arm.com                 // If the local prediction matches the actual outcome,
2958843Smrinmoy.ghosh@arm.com                 // decerement the counter.  Otherwise increment the
2968843Smrinmoy.ghosh@arm.com                 // counter.
2979360SE.Tomusk@sms.ed.ac.uk                 unsigned choice_predictor_idx =
2989360SE.Tomusk@sms.ed.ac.uk                   history->globalHistory & choiceHistoryMask;
2998843Smrinmoy.ghosh@arm.com                 if (history->localPredTaken == taken) {
3009360SE.Tomusk@sms.ed.ac.uk                     choiceCtrs[choice_predictor_idx].decrement();
3018843Smrinmoy.ghosh@arm.com                 } else if (history->globalPredTaken == taken) {
3029360SE.Tomusk@sms.ed.ac.uk                     choiceCtrs[choice_predictor_idx].increment();
3038843Smrinmoy.ghosh@arm.com                 }
3048463SMrinmoy.Ghosh@arm.com
3058843Smrinmoy.ghosh@arm.com             }
3068843Smrinmoy.ghosh@arm.com
3078843Smrinmoy.ghosh@arm.com             // Update the counters and local history with the proper
3088843Smrinmoy.ghosh@arm.com             // resolution of the branch.  Global history is updated
3098843Smrinmoy.ghosh@arm.com             // speculatively and restored upon squash() calls, so it does not
3108843Smrinmoy.ghosh@arm.com             // need to be updated.
3119360SE.Tomusk@sms.ed.ac.uk             unsigned global_predictor_idx =
3129360SE.Tomusk@sms.ed.ac.uk               history->globalHistory & globalHistoryMask;
3138843Smrinmoy.ghosh@arm.com             if (taken) {
3149360SE.Tomusk@sms.ed.ac.uk                  globalCtrs[global_predictor_idx].increment();
3159327Smrinmoy.ghosh@arm.com                  if (old_local_pred_valid) {
3168843Smrinmoy.ghosh@arm.com                          localCtrs[old_local_pred_index].increment();
3178843Smrinmoy.ghosh@arm.com                  }
3188843Smrinmoy.ghosh@arm.com             } else {
3199360SE.Tomusk@sms.ed.ac.uk                  globalCtrs[global_predictor_idx].decrement();
3209327Smrinmoy.ghosh@arm.com                  if (old_local_pred_valid) {
3218843Smrinmoy.ghosh@arm.com                          localCtrs[old_local_pred_index].decrement();
3228843Smrinmoy.ghosh@arm.com                  }
3238843Smrinmoy.ghosh@arm.com             }
3246184SN/A        }
3258843Smrinmoy.ghosh@arm.com        if (squashed) {
3268843Smrinmoy.ghosh@arm.com             if (taken) {
3278843Smrinmoy.ghosh@arm.com                globalHistory = (history->globalHistory << 1) | 1;
3289360SE.Tomusk@sms.ed.ac.uk                globalHistory = globalHistory & historyRegisterMask;
3299327Smrinmoy.ghosh@arm.com                if (old_local_pred_valid) {
3309327Smrinmoy.ghosh@arm.com                    localHistoryTable[local_history_idx] =
3318842Smrinmoy.ghosh@arm.com                     (history->localHistory << 1) | 1;
3328843Smrinmoy.ghosh@arm.com                }
3338843Smrinmoy.ghosh@arm.com             } else {
3348843Smrinmoy.ghosh@arm.com                globalHistory = (history->globalHistory << 1);
3359360SE.Tomusk@sms.ed.ac.uk                globalHistory = globalHistory & historyRegisterMask;
3369327Smrinmoy.ghosh@arm.com                if (old_local_pred_valid) {
3379327Smrinmoy.ghosh@arm.com                     localHistoryTable[local_history_idx] =
3388842Smrinmoy.ghosh@arm.com                     history->localHistory << 1;
3398843Smrinmoy.ghosh@arm.com                }
3408843Smrinmoy.ghosh@arm.com             }
3418463SMrinmoy.Ghosh@arm.com
3428463SMrinmoy.Ghosh@arm.com        }
3436184SN/A        // We're done with this history, now delete it.
3446184SN/A        delete history;
3458843Smrinmoy.ghosh@arm.com
3466184SN/A    }
3476184SN/A
3489360SE.Tomusk@sms.ed.ac.uk    assert(local_history_idx < localHistoryTableSize);
3496184SN/A
3506184SN/A
3516184SN/A}
3526184SN/A
3536184SN/Avoid
3546184SN/ATournamentBP::squash(void *bp_history)
3556184SN/A{
3566184SN/A    BPHistory *history = static_cast<BPHistory *>(bp_history);
3576184SN/A
3586184SN/A    // Restore global history to state prior to this branch.
3596184SN/A    globalHistory = history->globalHistory;
3606184SN/A
3616184SN/A    // Delete this BPHistory now that we're done with it.
3626184SN/A    delete history;
3636184SN/A}
3646184SN/A
3656184SN/A#ifdef DEBUG
3666184SN/Aint
3676184SN/ATournamentBP::BPHistory::newCount = 0;
3686184SN/A#endif
369