2bit_local.cc revision 13626
15627Sgblack@eecs.umich.edu/*
25627Sgblack@eecs.umich.edu * Copyright (c) 2004-2006 The Regents of The University of Michigan
35627Sgblack@eecs.umich.edu * All rights reserved.
45627Sgblack@eecs.umich.edu *
57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67087Snate@binkert.org * modification, are permitted provided that the following conditions are
77087Snate@binkert.org * met: redistributions of source code must retain the above copyright
87087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117087Snate@binkert.org * documentation and/or other materials provided with the distribution;
127087Snate@binkert.org * neither the name of the copyright holders nor the names of its
135627Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
147087Snate@binkert.org * this software without specific prior written permission.
157087Snate@binkert.org *
167087Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177087Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187087Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197087Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207087Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217087Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225627Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237087Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245627Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255627Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265627Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275627Sgblack@eecs.umich.edu *
285627Sgblack@eecs.umich.edu * Authors: Kevin Lim
295627Sgblack@eecs.umich.edu */
305627Sgblack@eecs.umich.edu
315627Sgblack@eecs.umich.edu#include "cpu/pred/2bit_local.hh"
325627Sgblack@eecs.umich.edu
335627Sgblack@eecs.umich.edu#include "base/intmath.hh"
345627Sgblack@eecs.umich.edu#include "base/logging.hh"
355627Sgblack@eecs.umich.edu#include "base/trace.hh"
365627Sgblack@eecs.umich.edu#include "debug/Fetch.hh"
375627Sgblack@eecs.umich.edu
385627Sgblack@eecs.umich.eduLocalBP::LocalBP(const LocalBPParams *params)
395627Sgblack@eecs.umich.edu    : BPredUnit(params),
405627Sgblack@eecs.umich.edu      localPredictorSize(params->localPredictorSize),
4111793Sbrandon.potter@amd.com      localCtrBits(params->localCtrBits)
425627Sgblack@eecs.umich.edu{
438229Snate@binkert.org    if (!isPowerOf2(localPredictorSize)) {
448229Snate@binkert.org        fatal("Invalid local predictor size!\n");
458229Snate@binkert.org    }
468229Snate@binkert.org
475627Sgblack@eecs.umich.edu    localPredictorSets = localPredictorSize / localCtrBits;
485627Sgblack@eecs.umich.edu
495627Sgblack@eecs.umich.edu    if (!isPowerOf2(localPredictorSets)) {
505627Sgblack@eecs.umich.edu        fatal("Invalid number of local predictor sets! Check localCtrBits.\n");
515627Sgblack@eecs.umich.edu    }
525627Sgblack@eecs.umich.edu
535627Sgblack@eecs.umich.edu    // Setup the index mask.
545627Sgblack@eecs.umich.edu    indexMask = localPredictorSets - 1;
555627Sgblack@eecs.umich.edu
565627Sgblack@eecs.umich.edu    DPRINTF(Fetch, "index mask: %#x\n", indexMask);
575627Sgblack@eecs.umich.edu
585627Sgblack@eecs.umich.edu    // Setup the array of counters for the local predictor.
595627Sgblack@eecs.umich.edu    localCtrs.resize(localPredictorSets);
605627Sgblack@eecs.umich.edu
615627Sgblack@eecs.umich.edu    for (unsigned i = 0; i < localPredictorSets; ++i)
625627Sgblack@eecs.umich.edu        localCtrs[i].setBits(localCtrBits);
635627Sgblack@eecs.umich.edu
645627Sgblack@eecs.umich.edu    DPRINTF(Fetch, "local predictor size: %i\n",
655627Sgblack@eecs.umich.edu            localPredictorSize);
665627Sgblack@eecs.umich.edu
675627Sgblack@eecs.umich.edu    DPRINTF(Fetch, "local counter bits: %i\n", localCtrBits);
685627Sgblack@eecs.umich.edu
695627Sgblack@eecs.umich.edu    DPRINTF(Fetch, "instruction shift amount: %i\n",
705627Sgblack@eecs.umich.edu            instShiftAmt);
715627Sgblack@eecs.umich.edu}
725627Sgblack@eecs.umich.edu
735627Sgblack@eecs.umich.eduvoid
745627Sgblack@eecs.umich.eduLocalBP::reset()
755627Sgblack@eecs.umich.edu{
765627Sgblack@eecs.umich.edu    for (unsigned i = 0; i < localPredictorSets; ++i) {
775627Sgblack@eecs.umich.edu        localCtrs[i].reset();
785627Sgblack@eecs.umich.edu    }
795627Sgblack@eecs.umich.edu}
805627Sgblack@eecs.umich.edu
815627Sgblack@eecs.umich.eduvoid
825627Sgblack@eecs.umich.eduLocalBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
835627Sgblack@eecs.umich.edu{
845627Sgblack@eecs.umich.edu// Place holder for a function that is called to update predictor history when
855627Sgblack@eecs.umich.edu// a BTB entry is invalid or not found.
865627Sgblack@eecs.umich.edu}
875627Sgblack@eecs.umich.edu
885627Sgblack@eecs.umich.edu
895627Sgblack@eecs.umich.edubool
905627Sgblack@eecs.umich.eduLocalBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
91{
92    bool taken;
93    uint8_t counter_val;
94    unsigned local_predictor_idx = getLocalIndex(branch_addr);
95
96    DPRINTF(Fetch, "Looking up index %#x\n",
97            local_predictor_idx);
98
99    counter_val = localCtrs[local_predictor_idx].read();
100
101    DPRINTF(Fetch, "prediction is %i.\n",
102            (int)counter_val);
103
104    taken = getPrediction(counter_val);
105
106#if 0
107    // Speculative update.
108    if (taken) {
109        DPRINTF(Fetch, "Branch updated as taken.\n");
110        localCtrs[local_predictor_idx].increment();
111    } else {
112        DPRINTF(Fetch, "Branch updated as not taken.\n");
113        localCtrs[local_predictor_idx].decrement();
114    }
115#endif
116
117    return taken;
118}
119
120void
121LocalBP::update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
122                bool squashed, const StaticInstPtr & inst, Addr corrTarget)
123{
124    assert(bp_history == NULL);
125    unsigned local_predictor_idx;
126
127    // No state to restore, and we do not update on the wrong
128    // path.
129    if (squashed) {
130        return;
131    }
132
133    // Update the local predictor.
134    local_predictor_idx = getLocalIndex(branch_addr);
135
136    DPRINTF(Fetch, "Looking up index %#x\n", local_predictor_idx);
137
138    if (taken) {
139        DPRINTF(Fetch, "Branch updated as taken.\n");
140        localCtrs[local_predictor_idx].increment();
141    } else {
142        DPRINTF(Fetch, "Branch updated as not taken.\n");
143        localCtrs[local_predictor_idx].decrement();
144    }
145}
146
147inline
148bool
149LocalBP::getPrediction(uint8_t &count)
150{
151    // Get the MSB of the count
152    return (count >> (localCtrBits - 1));
153}
154
155inline
156unsigned
157LocalBP::getLocalIndex(Addr &branch_addr)
158{
159    return (branch_addr >> instShiftAmt) & indexMask;
160}
161
162void
163LocalBP::uncondBranch(ThreadID tid, Addr pc, void *&bp_history)
164{
165}
166
167LocalBP*
168LocalBPParams::create()
169{
170    return new LocalBP(this);
171}
172