btb.cc revision 8232
16184SN/A/*
26184SN/A * Copyright (c) 2004-2005 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"
326184SN/A#include "base/trace.hh"
336226Snate@binkert.org#include "cpu/pred/btb.hh"
348232Snate@binkert.org#include "debug/Fetch.hh"
356184SN/A
366184SN/ADefaultBTB::DefaultBTB(unsigned _numEntries,
376184SN/A                       unsigned _tagBits,
386184SN/A                       unsigned _instShiftAmt)
396184SN/A    : numEntries(_numEntries),
406184SN/A      tagBits(_tagBits),
416184SN/A      instShiftAmt(_instShiftAmt)
426184SN/A{
436184SN/A    DPRINTF(Fetch, "BTB: Creating BTB object.\n");
446184SN/A
456184SN/A    if (!isPowerOf2(numEntries)) {
466184SN/A        fatal("BTB entries is not a power of 2!");
476184SN/A    }
486184SN/A
496184SN/A    btb.resize(numEntries);
506184SN/A
516227Snate@binkert.org    for (unsigned i = 0; i < numEntries; ++i) {
526184SN/A        btb[i].valid = false;
536184SN/A    }
546184SN/A
556184SN/A    idxMask = numEntries - 1;
566184SN/A
576184SN/A    tagMask = (1 << tagBits) - 1;
586184SN/A
596184SN/A    tagShiftAmt = instShiftAmt + floorLog2(numEntries);
606184SN/A}
616184SN/A
626184SN/Avoid
636184SN/ADefaultBTB::reset()
646184SN/A{
656227Snate@binkert.org    for (unsigned i = 0; i < numEntries; ++i) {
666184SN/A        btb[i].valid = false;
676184SN/A    }
686184SN/A}
696184SN/A
706184SN/Ainline
716184SN/Aunsigned
727720Sgblack@eecs.umich.eduDefaultBTB::getIndex(Addr instPC)
736184SN/A{
746184SN/A    // Need to shift PC over by the word offset.
757720Sgblack@eecs.umich.edu    return (instPC >> instShiftAmt) & idxMask;
766184SN/A}
776184SN/A
786184SN/Ainline
796184SN/AAddr
807720Sgblack@eecs.umich.eduDefaultBTB::getTag(Addr instPC)
816184SN/A{
827720Sgblack@eecs.umich.edu    return (instPC >> tagShiftAmt) & tagMask;
836184SN/A}
846184SN/A
856184SN/Abool
867720Sgblack@eecs.umich.eduDefaultBTB::valid(Addr instPC, ThreadID tid)
876184SN/A{
887720Sgblack@eecs.umich.edu    unsigned btb_idx = getIndex(instPC);
896184SN/A
907720Sgblack@eecs.umich.edu    Addr inst_tag = getTag(instPC);
916184SN/A
926184SN/A    assert(btb_idx < numEntries);
936184SN/A
946184SN/A    if (btb[btb_idx].valid
956184SN/A        && inst_tag == btb[btb_idx].tag
966184SN/A        && btb[btb_idx].tid == tid) {
976184SN/A        return true;
986184SN/A    } else {
996184SN/A        return false;
1006184SN/A    }
1016184SN/A}
1026184SN/A
1036184SN/A// @todo Create some sort of return struct that has both whether or not the
1046184SN/A// address is valid, and also the address.  For now will just use addr = 0 to
1056184SN/A// represent invalid entry.
1067720Sgblack@eecs.umich.eduTheISA::PCState
1077720Sgblack@eecs.umich.eduDefaultBTB::lookup(Addr instPC, ThreadID tid)
1086184SN/A{
1097720Sgblack@eecs.umich.edu    unsigned btb_idx = getIndex(instPC);
1106184SN/A
1117720Sgblack@eecs.umich.edu    Addr inst_tag = getTag(instPC);
1126184SN/A
1136184SN/A    assert(btb_idx < numEntries);
1146184SN/A
1156184SN/A    if (btb[btb_idx].valid
1166184SN/A        && inst_tag == btb[btb_idx].tag
1176184SN/A        && btb[btb_idx].tid == tid) {
1186184SN/A        return btb[btb_idx].target;
1196184SN/A    } else {
1206184SN/A        return 0;
1216184SN/A    }
1226184SN/A}
1236184SN/A
1246184SN/Avoid
1257720Sgblack@eecs.umich.eduDefaultBTB::update(Addr instPC, const TheISA::PCState &target, ThreadID tid)
1266184SN/A{
1277720Sgblack@eecs.umich.edu    unsigned btb_idx = getIndex(instPC);
1286184SN/A
1296184SN/A    assert(btb_idx < numEntries);
1306184SN/A
1316184SN/A    btb[btb_idx].tid = tid;
1326184SN/A    btb[btb_idx].valid = true;
1336184SN/A    btb[btb_idx].target = target;
1347720Sgblack@eecs.umich.edu    btb[btb_idx].tag = getTag(instPC);
1356184SN/A}
136