tournament.cc revision 8463
16184SN/A/*
26184SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
36184SN/A * All rights reserved.
46184SN/A *
56184SN/A * Redistribution and use in source and binary forms, with or without
66184SN/A * modification, are permitted provided that the following conditions are
76184SN/A * met: redistributions of source code must retain the above copyright
86184SN/A * notice, this list of conditions and the following disclaimer;
96184SN/A * redistributions in binary form must reproduce the above copyright
106184SN/A * notice, this list of conditions and the following disclaimer in the
116184SN/A * documentation and/or other materials provided with the distribution;
126184SN/A * neither the name of the copyright holders nor the names of its
136184SN/A * contributors may be used to endorse or promote products derived from
146184SN/A * this software without specific prior written permission.
156184SN/A *
166184SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176184SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186184SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196184SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206184SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216184SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226184SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236184SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246184SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256184SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266184SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276184SN/A *
286184SN/A * Authors: Kevin Lim
296184SN/A */
306184SN/A
316184SN/A#include "base/intmath.hh"
326226Snate@binkert.org#include "cpu/pred/tournament.hh"
336184SN/A
346184SN/ATournamentBP::TournamentBP(unsigned _localPredictorSize,
356184SN/A                           unsigned _localCtrBits,
366184SN/A                           unsigned _localHistoryTableSize,
376184SN/A                           unsigned _localHistoryBits,
386184SN/A                           unsigned _globalPredictorSize,
398463SMrinmoy.Ghosh@arm.com                           unsigned _globalHistoryBits,
406184SN/A                           unsigned _globalCtrBits,
416184SN/A                           unsigned _choicePredictorSize,
426184SN/A                           unsigned _choiceCtrBits,
436184SN/A                           unsigned _instShiftAmt)
446184SN/A    : localPredictorSize(_localPredictorSize),
456184SN/A      localCtrBits(_localCtrBits),
466184SN/A      localHistoryTableSize(_localHistoryTableSize),
476184SN/A      localHistoryBits(_localHistoryBits),
486184SN/A      globalPredictorSize(_globalPredictorSize),
496184SN/A      globalCtrBits(_globalCtrBits),
506184SN/A      globalHistoryBits(_globalHistoryBits),
516184SN/A      choicePredictorSize(_globalPredictorSize),
526184SN/A      choiceCtrBits(_choiceCtrBits),
536184SN/A      instShiftAmt(_instShiftAmt)
546184SN/A{
556184SN/A    if (!isPowerOf2(localPredictorSize)) {
566184SN/A        fatal("Invalid local predictor size!\n");
576184SN/A    }
586184SN/A
596184SN/A    //Setup the array of counters for the local predictor
606184SN/A    localCtrs.resize(localPredictorSize);
616184SN/A
626184SN/A    for (int i = 0; i < localPredictorSize; ++i)
636184SN/A        localCtrs[i].setBits(localCtrBits);
646184SN/A
656184SN/A    localPredictorMask = floorPow2(localPredictorSize) - 1;
666184SN/A
676184SN/A    if (!isPowerOf2(localHistoryTableSize)) {
686184SN/A        fatal("Invalid local history table size!\n");
696184SN/A    }
706184SN/A
716184SN/A    //Setup the history table for the local table
726184SN/A    localHistoryTable.resize(localHistoryTableSize);
736184SN/A
746184SN/A    for (int i = 0; i < localHistoryTableSize; ++i)
756184SN/A        localHistoryTable[i] = 0;
766184SN/A
776184SN/A    // Setup the local history mask
786184SN/A    localHistoryMask = (1 << localHistoryBits) - 1;
796184SN/A
806184SN/A    if (!isPowerOf2(globalPredictorSize)) {
816184SN/A        fatal("Invalid global predictor size!\n");
826184SN/A    }
836184SN/A
846184SN/A    //Setup the array of counters for the global predictor
856184SN/A    globalCtrs.resize(globalPredictorSize);
866184SN/A
876184SN/A    for (int i = 0; i < globalPredictorSize; ++i)
886184SN/A        globalCtrs[i].setBits(globalCtrBits);
896184SN/A
906184SN/A    //Clear the global history
916184SN/A    globalHistory = 0;
926184SN/A    // Setup the global history mask
936184SN/A    globalHistoryMask = (1 << globalHistoryBits) - 1;
946184SN/A
956184SN/A    if (!isPowerOf2(choicePredictorSize)) {
966184SN/A        fatal("Invalid choice predictor size!\n");
976184SN/A    }
986184SN/A
996184SN/A    //Setup the array of counters for the choice predictor
1006184SN/A    choiceCtrs.resize(choicePredictorSize);
1016184SN/A
1026184SN/A    for (int i = 0; i < choicePredictorSize; ++i)
1036184SN/A        choiceCtrs[i].setBits(choiceCtrBits);
1046184SN/A
1056184SN/A    // @todo: Allow for different thresholds between the predictors.
1066184SN/A    threshold = (1 << (localCtrBits - 1)) - 1;
1076184SN/A}
1086184SN/A
1096184SN/Ainline
1106184SN/Aunsigned
1116184SN/ATournamentBP::calcLocHistIdx(Addr &branch_addr)
1126184SN/A{
1136184SN/A    // Get low order bits after removing instruction offset.
1146184SN/A    return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
1156184SN/A}
1166184SN/A
1176184SN/Ainline
1186184SN/Avoid
1196184SN/ATournamentBP::updateGlobalHistTaken()
1206184SN/A{
1216184SN/A    globalHistory = (globalHistory << 1) | 1;
1226184SN/A    globalHistory = globalHistory & globalHistoryMask;
1236184SN/A}
1246184SN/A
1256184SN/Ainline
1266184SN/Avoid
1276184SN/ATournamentBP::updateGlobalHistNotTaken()
1286184SN/A{
1296184SN/A    globalHistory = (globalHistory << 1);
1306184SN/A    globalHistory = globalHistory & globalHistoryMask;
1316184SN/A}
1326184SN/A
1336184SN/Ainline
1346184SN/Avoid
1356184SN/ATournamentBP::updateLocalHistTaken(unsigned local_history_idx)
1366184SN/A{
1376184SN/A    localHistoryTable[local_history_idx] =
1386184SN/A        (localHistoryTable[local_history_idx] << 1) | 1;
1396184SN/A}
1406184SN/A
1416184SN/Ainline
1426184SN/Avoid
1436184SN/ATournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
1446184SN/A{
1456184SN/A    localHistoryTable[local_history_idx] =
1466184SN/A        (localHistoryTable[local_history_idx] << 1);
1476184SN/A}
1486184SN/A
1496184SN/Abool
1506184SN/ATournamentBP::lookup(Addr &branch_addr, void * &bp_history)
1516184SN/A{
1526184SN/A    bool local_prediction;
1536184SN/A    unsigned local_history_idx;
1546184SN/A    unsigned local_predictor_idx;
1556184SN/A
1566184SN/A    bool global_prediction;
1576184SN/A    bool choice_prediction;
1586184SN/A
1596184SN/A    //Lookup in the local predictor to get its branch prediction
1606184SN/A    local_history_idx = calcLocHistIdx(branch_addr);
1616184SN/A    local_predictor_idx = localHistoryTable[local_history_idx]
1626184SN/A        & localPredictorMask;
1636184SN/A    local_prediction = localCtrs[local_predictor_idx].read() > threshold;
1646184SN/A
1656184SN/A    //Lookup in the global predictor to get its branch prediction
1666184SN/A    global_prediction = globalCtrs[globalHistory].read() > threshold;
1676184SN/A
1686184SN/A    //Lookup in the choice predictor to see which one to use
1696184SN/A    choice_prediction = choiceCtrs[globalHistory].read() > threshold;
1706184SN/A
1716184SN/A    // Create BPHistory and pass it back to be recorded.
1726184SN/A    BPHistory *history = new BPHistory;
1736184SN/A    history->globalHistory = globalHistory;
1746184SN/A    history->localPredTaken = local_prediction;
1756184SN/A    history->globalPredTaken = global_prediction;
1766184SN/A    history->globalUsed = choice_prediction;
1776184SN/A    bp_history = (void *)history;
1786184SN/A
1796184SN/A    assert(globalHistory < globalPredictorSize &&
1806184SN/A           local_history_idx < localHistoryTableSize &&
1816184SN/A           local_predictor_idx < localPredictorSize);
1826184SN/A
1836184SN/A    // Commented code is for doing speculative update of counters and
1846184SN/A    // all histories.
1856184SN/A    if (choice_prediction) {
1866184SN/A        if (global_prediction) {
1876184SN/A//            updateHistoriesTaken(local_history_idx);
1886184SN/A//            globalCtrs[globalHistory].increment();
1896184SN/A//            localCtrs[local_history_idx].increment();
1906184SN/A            updateGlobalHistTaken();
1916184SN/A            return true;
1926184SN/A        } else {
1936184SN/A//            updateHistoriesNotTaken(local_history_idx);
1946184SN/A//            globalCtrs[globalHistory].decrement();
1956184SN/A//            localCtrs[local_history_idx].decrement();
1966184SN/A            updateGlobalHistNotTaken();
1976184SN/A            return false;
1986184SN/A        }
1996184SN/A    } else {
2006184SN/A        if (local_prediction) {
2016184SN/A//            updateHistoriesTaken(local_history_idx);
2026184SN/A//            globalCtrs[globalHistory].increment();
2036184SN/A//            localCtrs[local_history_idx].increment();
2046184SN/A            updateGlobalHistTaken();
2056184SN/A            return true;
2066184SN/A        } else {
2076184SN/A//            updateHistoriesNotTaken(local_history_idx);
2086184SN/A//            globalCtrs[globalHistory].decrement();
2096184SN/A//            localCtrs[local_history_idx].decrement();
2106184SN/A            updateGlobalHistNotTaken();
2116184SN/A            return false;
2126184SN/A        }
2136184SN/A    }
2146184SN/A}
2156184SN/A
2166184SN/Avoid
2176184SN/ATournamentBP::uncondBr(void * &bp_history)
2186184SN/A{
2196184SN/A    // Create BPHistory and pass it back to be recorded.
2206184SN/A    BPHistory *history = new BPHistory;
2216184SN/A    history->globalHistory = globalHistory;
2226184SN/A    history->localPredTaken = true;
2236184SN/A    history->globalPredTaken = true;
2246184SN/A    bp_history = static_cast<void *>(history);
2256184SN/A
2266184SN/A    updateGlobalHistTaken();
2276184SN/A}
2286184SN/A
2296184SN/Avoid
2306184SN/ATournamentBP::update(Addr &branch_addr, bool taken, void *bp_history)
2316184SN/A{
2326184SN/A    unsigned local_history_idx;
2336184SN/A    unsigned local_predictor_idx;
2346184SN/A    unsigned local_predictor_hist;
2356184SN/A
2366184SN/A    // Get the local predictor's current prediction
2376184SN/A    local_history_idx = calcLocHistIdx(branch_addr);
2386184SN/A    local_predictor_hist = localHistoryTable[local_history_idx];
2396184SN/A    local_predictor_idx = local_predictor_hist & localPredictorMask;
2406184SN/A
2416184SN/A    // Update the choice predictor to tell it which one was correct if
2426184SN/A    // there was a prediction.
2436184SN/A    if (bp_history) {
2446184SN/A        BPHistory *history = static_cast<BPHistory *>(bp_history);
2456184SN/A        if (history->localPredTaken != history->globalPredTaken) {
2466184SN/A            // If the local prediction matches the actual outcome,
2476184SN/A            // decerement the counter.  Otherwise increment the
2486184SN/A            // counter.
2496184SN/A            if (history->localPredTaken == taken) {
2508463SMrinmoy.Ghosh@arm.com                choiceCtrs[history->globalHistory].decrement();
2516184SN/A            } else if (history->globalPredTaken == taken){
2528463SMrinmoy.Ghosh@arm.com                choiceCtrs[history->globalHistory].increment();
2536184SN/A            }
2548463SMrinmoy.Ghosh@arm.com
2556184SN/A        }
2566184SN/A
2578463SMrinmoy.Ghosh@arm.com        // Update the counters and local history with the proper
2588463SMrinmoy.Ghosh@arm.com        // resolution of the branch.  Global history is updated
2598463SMrinmoy.Ghosh@arm.com        // speculatively and restored upon squash() calls, so it does not
2608463SMrinmoy.Ghosh@arm.com        // need to be updated.
2618463SMrinmoy.Ghosh@arm.com        if (taken) {
2628463SMrinmoy.Ghosh@arm.com               localCtrs[local_predictor_idx].increment();
2638463SMrinmoy.Ghosh@arm.com               globalCtrs[history->globalHistory].increment();
2648463SMrinmoy.Ghosh@arm.com
2658463SMrinmoy.Ghosh@arm.com               updateLocalHistTaken(local_history_idx);
2668463SMrinmoy.Ghosh@arm.com        } else {
2678463SMrinmoy.Ghosh@arm.com               localCtrs[local_predictor_idx].decrement();
2688463SMrinmoy.Ghosh@arm.com               globalCtrs[history->globalHistory].decrement();
2698463SMrinmoy.Ghosh@arm.com
2708463SMrinmoy.Ghosh@arm.com               updateLocalHistNotTaken(local_history_idx);
2718463SMrinmoy.Ghosh@arm.com       }
2728463SMrinmoy.Ghosh@arm.com
2738463SMrinmoy.Ghosh@arm.com       bool mispredict = false;
2748463SMrinmoy.Ghosh@arm.com
2758463SMrinmoy.Ghosh@arm.com       //global predictor used and mispredicted
2768463SMrinmoy.Ghosh@arm.com       if (history->globalUsed && history->globalPredTaken != taken)
2778463SMrinmoy.Ghosh@arm.com           mispredict = true;
2788463SMrinmoy.Ghosh@arm.com       //local predictor used and mispredicted
2798463SMrinmoy.Ghosh@arm.com       else if (!history->globalUsed && history->localPredTaken != taken)
2808463SMrinmoy.Ghosh@arm.com           mispredict = true;
2818463SMrinmoy.Ghosh@arm.com
2828463SMrinmoy.Ghosh@arm.com       if (mispredict) {
2838463SMrinmoy.Ghosh@arm.com           if (taken) {
2848463SMrinmoy.Ghosh@arm.com              globalHistory = globalHistory | 1;
2858463SMrinmoy.Ghosh@arm.com           } else {
2868463SMrinmoy.Ghosh@arm.com              unsigned mask = globalHistoryMask - 1;
2878463SMrinmoy.Ghosh@arm.com              globalHistory = globalHistory & mask;
2888463SMrinmoy.Ghosh@arm.com           }
2898463SMrinmoy.Ghosh@arm.com
2908463SMrinmoy.Ghosh@arm.com        }
2916184SN/A        // We're done with this history, now delete it.
2926184SN/A        delete history;
2936184SN/A    }
2946184SN/A
2956184SN/A    assert(globalHistory < globalPredictorSize &&
2966184SN/A           local_history_idx < localHistoryTableSize &&
2976184SN/A           local_predictor_idx < localPredictorSize);
2986184SN/A
2996184SN/A
3006184SN/A}
3016184SN/A
3026184SN/Avoid
3036184SN/ATournamentBP::squash(void *bp_history)
3046184SN/A{
3056184SN/A    BPHistory *history = static_cast<BPHistory *>(bp_history);
3066184SN/A
3076184SN/A    // Restore global history to state prior to this branch.
3086184SN/A    globalHistory = history->globalHistory;
3096184SN/A
3106184SN/A    // Delete this BPHistory now that we're done with it.
3116184SN/A    delete history;
3126184SN/A}
3136184SN/A
3146184SN/A#ifdef DEBUG
3156184SN/Aint
3166184SN/ATournamentBP::BPHistory::newCount = 0;
3176184SN/A#endif
318