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
3111793Sbrandon.potter@amd.com#include "cpu/pred/2bit_local.hh"
3211793Sbrandon.potter@amd.com
336184SN/A#include "base/intmath.hh"
3412334Sgabeblack@google.com#include "base/logging.hh"
356184SN/A#include "base/trace.hh"
368232Snate@binkert.org#include "debug/Fetch.hh"
376184SN/A
3810785Sgope@wisc.eduLocalBP::LocalBP(const LocalBPParams *params)
399480Snilay@cs.wisc.edu    : BPredUnit(params),
409480Snilay@cs.wisc.edu      localPredictorSize(params->localPredictorSize),
4113959Sodanrc@yahoo.com.br      localCtrBits(params->localCtrBits),
4213959Sodanrc@yahoo.com.br      localPredictorSets(localPredictorSize / localCtrBits),
4313959Sodanrc@yahoo.com.br      localCtrs(localPredictorSets, SatCounter(localCtrBits)),
4413959Sodanrc@yahoo.com.br      indexMask(localPredictorSets - 1)
456184SN/A{
466184SN/A    if (!isPowerOf2(localPredictorSize)) {
476184SN/A        fatal("Invalid local predictor size!\n");
486184SN/A    }
496184SN/A
506184SN/A    if (!isPowerOf2(localPredictorSets)) {
516184SN/A        fatal("Invalid number of local predictor sets! Check localCtrBits.\n");
526184SN/A    }
536184SN/A
549480Snilay@cs.wisc.edu    DPRINTF(Fetch, "index mask: %#x\n", indexMask);
556184SN/A
569480Snilay@cs.wisc.edu    DPRINTF(Fetch, "local predictor size: %i\n",
576184SN/A            localPredictorSize);
586184SN/A
599480Snilay@cs.wisc.edu    DPRINTF(Fetch, "local counter bits: %i\n", localCtrBits);
606184SN/A
619480Snilay@cs.wisc.edu    DPRINTF(Fetch, "instruction shift amount: %i\n",
626184SN/A            instShiftAmt);
636184SN/A}
646184SN/A
656184SN/Avoid
6611434Smitch.hayenga@arm.comLocalBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
678842Smrinmoy.ghosh@arm.com{
688842Smrinmoy.ghosh@arm.com// Place holder for a function that is called to update predictor history when
698842Smrinmoy.ghosh@arm.com// a BTB entry is invalid or not found.
708842Smrinmoy.ghosh@arm.com}
718842Smrinmoy.ghosh@arm.com
728842Smrinmoy.ghosh@arm.com
736184SN/Abool
7411434Smitch.hayenga@arm.comLocalBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
756184SN/A{
766184SN/A    bool taken;
776184SN/A    unsigned local_predictor_idx = getLocalIndex(branch_addr);
786184SN/A
799480Snilay@cs.wisc.edu    DPRINTF(Fetch, "Looking up index %#x\n",
806184SN/A            local_predictor_idx);
816184SN/A
8213959Sodanrc@yahoo.com.br    uint8_t counter_val = localCtrs[local_predictor_idx];
836184SN/A
849480Snilay@cs.wisc.edu    DPRINTF(Fetch, "prediction is %i.\n",
856184SN/A            (int)counter_val);
866184SN/A
876184SN/A    taken = getPrediction(counter_val);
886184SN/A
896184SN/A    return taken;
906184SN/A}
916184SN/A
926184SN/Avoid
9311434Smitch.hayenga@arm.comLocalBP::update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
9413626Sjairo.balart@metempsy.com                bool squashed, const StaticInstPtr & inst, Addr corrTarget)
956184SN/A{
966184SN/A    assert(bp_history == NULL);
976184SN/A    unsigned local_predictor_idx;
986184SN/A
9911783Sarthur.perais@inria.fr    // No state to restore, and we do not update on the wrong
10011783Sarthur.perais@inria.fr    // path.
10111783Sarthur.perais@inria.fr    if (squashed) {
10211783Sarthur.perais@inria.fr        return;
10311783Sarthur.perais@inria.fr    }
10411783Sarthur.perais@inria.fr
1056184SN/A    // Update the local predictor.
1066184SN/A    local_predictor_idx = getLocalIndex(branch_addr);
1076184SN/A
1089480Snilay@cs.wisc.edu    DPRINTF(Fetch, "Looking up index %#x\n", local_predictor_idx);
1096184SN/A
1106184SN/A    if (taken) {
1119480Snilay@cs.wisc.edu        DPRINTF(Fetch, "Branch updated as taken.\n");
11213959Sodanrc@yahoo.com.br        localCtrs[local_predictor_idx]++;
1136184SN/A    } else {
1149480Snilay@cs.wisc.edu        DPRINTF(Fetch, "Branch updated as not taken.\n");
11513959Sodanrc@yahoo.com.br        localCtrs[local_predictor_idx]--;
1166184SN/A    }
1176184SN/A}
1186184SN/A
1196184SN/Ainline
1206184SN/Abool
1216184SN/ALocalBP::getPrediction(uint8_t &count)
1226184SN/A{
1236184SN/A    // Get the MSB of the count
1246184SN/A    return (count >> (localCtrBits - 1));
1256184SN/A}
1266184SN/A
1276184SN/Ainline
1286184SN/Aunsigned
1296184SN/ALocalBP::getLocalIndex(Addr &branch_addr)
1306184SN/A{
1316184SN/A    return (branch_addr >> instShiftAmt) & indexMask;
1326184SN/A}
1339480Snilay@cs.wisc.edu
1349480Snilay@cs.wisc.eduvoid
13511434Smitch.hayenga@arm.comLocalBP::uncondBranch(ThreadID tid, Addr pc, void *&bp_history)
1369480Snilay@cs.wisc.edu{
1379480Snilay@cs.wisc.edu}
13810785Sgope@wisc.edu
13910785Sgope@wisc.eduLocalBP*
14010785Sgope@wisc.eduLocalBPParams::create()
14110785Sgope@wisc.edu{
14210785Sgope@wisc.edu    return new LocalBP(this);
14310785Sgope@wisc.edu}
144