tournament.cc revision 6226
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,
396184SN/A                           unsigned _globalCtrBits,
406184SN/A                           unsigned _globalHistoryBits,
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    threshold = threshold / 2;
1086184SN/A}
1096184SN/A
1106184SN/Ainline
1116184SN/Aunsigned
1126184SN/ATournamentBP::calcLocHistIdx(Addr &branch_addr)
1136184SN/A{
1146184SN/A    // Get low order bits after removing instruction offset.
1156184SN/A    return (branch_addr >> instShiftAmt) & (localHistoryTableSize - 1);
1166184SN/A}
1176184SN/A
1186184SN/Ainline
1196184SN/Avoid
1206184SN/ATournamentBP::updateGlobalHistTaken()
1216184SN/A{
1226184SN/A    globalHistory = (globalHistory << 1) | 1;
1236184SN/A    globalHistory = globalHistory & globalHistoryMask;
1246184SN/A}
1256184SN/A
1266184SN/Ainline
1276184SN/Avoid
1286184SN/ATournamentBP::updateGlobalHistNotTaken()
1296184SN/A{
1306184SN/A    globalHistory = (globalHistory << 1);
1316184SN/A    globalHistory = globalHistory & globalHistoryMask;
1326184SN/A}
1336184SN/A
1346184SN/Ainline
1356184SN/Avoid
1366184SN/ATournamentBP::updateLocalHistTaken(unsigned local_history_idx)
1376184SN/A{
1386184SN/A    localHistoryTable[local_history_idx] =
1396184SN/A        (localHistoryTable[local_history_idx] << 1) | 1;
1406184SN/A}
1416184SN/A
1426184SN/Ainline
1436184SN/Avoid
1446184SN/ATournamentBP::updateLocalHistNotTaken(unsigned local_history_idx)
1456184SN/A{
1466184SN/A    localHistoryTable[local_history_idx] =
1476184SN/A        (localHistoryTable[local_history_idx] << 1);
1486184SN/A}
1496184SN/A
1506184SN/Abool
1516184SN/ATournamentBP::lookup(Addr &branch_addr, void * &bp_history)
1526184SN/A{
1536184SN/A    bool local_prediction;
1546184SN/A    unsigned local_history_idx;
1556184SN/A    unsigned local_predictor_idx;
1566184SN/A
1576184SN/A    bool global_prediction;
1586184SN/A    bool choice_prediction;
1596184SN/A
1606184SN/A    //Lookup in the local predictor to get its branch prediction
1616184SN/A    local_history_idx = calcLocHistIdx(branch_addr);
1626184SN/A    local_predictor_idx = localHistoryTable[local_history_idx]
1636184SN/A        & localPredictorMask;
1646184SN/A    local_prediction = localCtrs[local_predictor_idx].read() > threshold;
1656184SN/A
1666184SN/A    //Lookup in the global predictor to get its branch prediction
1676184SN/A    global_prediction = globalCtrs[globalHistory].read() > threshold;
1686184SN/A
1696184SN/A    //Lookup in the choice predictor to see which one to use
1706184SN/A    choice_prediction = choiceCtrs[globalHistory].read() > threshold;
1716184SN/A
1726184SN/A    // Create BPHistory and pass it back to be recorded.
1736184SN/A    BPHistory *history = new BPHistory;
1746184SN/A    history->globalHistory = globalHistory;
1756184SN/A    history->localPredTaken = local_prediction;
1766184SN/A    history->globalPredTaken = global_prediction;
1776184SN/A    history->globalUsed = choice_prediction;
1786184SN/A    bp_history = (void *)history;
1796184SN/A
1806184SN/A    assert(globalHistory < globalPredictorSize &&
1816184SN/A           local_history_idx < localHistoryTableSize &&
1826184SN/A           local_predictor_idx < localPredictorSize);
1836184SN/A
1846184SN/A    // Commented code is for doing speculative update of counters and
1856184SN/A    // all histories.
1866184SN/A    if (choice_prediction) {
1876184SN/A        if (global_prediction) {
1886184SN/A//            updateHistoriesTaken(local_history_idx);
1896184SN/A//            globalCtrs[globalHistory].increment();
1906184SN/A//            localCtrs[local_history_idx].increment();
1916184SN/A            updateGlobalHistTaken();
1926184SN/A            return true;
1936184SN/A        } else {
1946184SN/A//            updateHistoriesNotTaken(local_history_idx);
1956184SN/A//            globalCtrs[globalHistory].decrement();
1966184SN/A//            localCtrs[local_history_idx].decrement();
1976184SN/A            updateGlobalHistNotTaken();
1986184SN/A            return false;
1996184SN/A        }
2006184SN/A    } else {
2016184SN/A        if (local_prediction) {
2026184SN/A//            updateHistoriesTaken(local_history_idx);
2036184SN/A//            globalCtrs[globalHistory].increment();
2046184SN/A//            localCtrs[local_history_idx].increment();
2056184SN/A            updateGlobalHistTaken();
2066184SN/A            return true;
2076184SN/A        } else {
2086184SN/A//            updateHistoriesNotTaken(local_history_idx);
2096184SN/A//            globalCtrs[globalHistory].decrement();
2106184SN/A//            localCtrs[local_history_idx].decrement();
2116184SN/A            updateGlobalHistNotTaken();
2126184SN/A            return false;
2136184SN/A        }
2146184SN/A    }
2156184SN/A}
2166184SN/A
2176184SN/Avoid
2186184SN/ATournamentBP::uncondBr(void * &bp_history)
2196184SN/A{
2206184SN/A    // Create BPHistory and pass it back to be recorded.
2216184SN/A    BPHistory *history = new BPHistory;
2226184SN/A    history->globalHistory = globalHistory;
2236184SN/A    history->localPredTaken = true;
2246184SN/A    history->globalPredTaken = true;
2256184SN/A    bp_history = static_cast<void *>(history);
2266184SN/A
2276184SN/A    updateGlobalHistTaken();
2286184SN/A}
2296184SN/A
2306184SN/Avoid
2316184SN/ATournamentBP::update(Addr &branch_addr, bool taken, void *bp_history)
2326184SN/A{
2336184SN/A    unsigned local_history_idx;
2346184SN/A    unsigned local_predictor_idx;
2356184SN/A    unsigned local_predictor_hist;
2366184SN/A
2376184SN/A    // Get the local predictor's current prediction
2386184SN/A    local_history_idx = calcLocHistIdx(branch_addr);
2396184SN/A    local_predictor_hist = localHistoryTable[local_history_idx];
2406184SN/A    local_predictor_idx = local_predictor_hist & localPredictorMask;
2416184SN/A
2426184SN/A    // Update the choice predictor to tell it which one was correct if
2436184SN/A    // there was a prediction.
2446184SN/A    if (bp_history) {
2456184SN/A        BPHistory *history = static_cast<BPHistory *>(bp_history);
2466184SN/A        if (history->localPredTaken != history->globalPredTaken) {
2476184SN/A            // If the local prediction matches the actual outcome,
2486184SN/A            // decerement the counter.  Otherwise increment the
2496184SN/A            // counter.
2506184SN/A            if (history->localPredTaken == taken) {
2516184SN/A                choiceCtrs[globalHistory].decrement();
2526184SN/A            } else if (history->globalPredTaken == taken){
2536184SN/A                choiceCtrs[globalHistory].increment();
2546184SN/A            }
2556184SN/A        }
2566184SN/A
2576184SN/A        // We're done with this history, now delete it.
2586184SN/A        delete history;
2596184SN/A    }
2606184SN/A
2616184SN/A    assert(globalHistory < globalPredictorSize &&
2626184SN/A           local_history_idx < localHistoryTableSize &&
2636184SN/A           local_predictor_idx < localPredictorSize);
2646184SN/A
2656184SN/A    // Update the counters and local history with the proper
2666184SN/A    // resolution of the branch.  Global history is updated
2676184SN/A    // speculatively and restored upon squash() calls, so it does not
2686184SN/A    // need to be updated.
2696184SN/A    if (taken) {
2706184SN/A        localCtrs[local_predictor_idx].increment();
2716184SN/A        globalCtrs[globalHistory].increment();
2726184SN/A
2736184SN/A        updateLocalHistTaken(local_history_idx);
2746184SN/A    } else {
2756184SN/A        localCtrs[local_predictor_idx].decrement();
2766184SN/A        globalCtrs[globalHistory].decrement();
2776184SN/A
2786184SN/A        updateLocalHistNotTaken(local_history_idx);
2796184SN/A    }
2806184SN/A}
2816184SN/A
2826184SN/Avoid
2836184SN/ATournamentBP::squash(void *bp_history)
2846184SN/A{
2856184SN/A    BPHistory *history = static_cast<BPHistory *>(bp_history);
2866184SN/A
2876184SN/A    // Restore global history to state prior to this branch.
2886184SN/A    globalHistory = history->globalHistory;
2896184SN/A
2906184SN/A    // Delete this BPHistory now that we're done with it.
2916184SN/A    delete history;
2926184SN/A}
2936184SN/A
2946184SN/A#ifdef DEBUG
2956184SN/Aint
2966184SN/ATournamentBP::BPHistory::newCount = 0;
2976184SN/A#endif
298