113454Spau.cabre@metempsy.com/*
213454Spau.cabre@metempsy.com * Copyright (c) 2014 The University of Wisconsin
313454Spau.cabre@metempsy.com *
413454Spau.cabre@metempsy.com * Copyright (c) 2006 INRIA (Institut National de Recherche en
513454Spau.cabre@metempsy.com * Informatique et en Automatique  / French National Research Institute
613454Spau.cabre@metempsy.com * for Computer Science and Applied Mathematics)
713454Spau.cabre@metempsy.com *
813454Spau.cabre@metempsy.com * All rights reserved.
913454Spau.cabre@metempsy.com *
1013454Spau.cabre@metempsy.com * Redistribution and use in source and binary forms, with or without
1113454Spau.cabre@metempsy.com * modification, are permitted provided that the following conditions are
1213454Spau.cabre@metempsy.com * met: redistributions of source code must retain the above copyright
1313454Spau.cabre@metempsy.com * notice, this list of conditions and the following disclaimer;
1413454Spau.cabre@metempsy.com * redistributions in binary form must reproduce the above copyright
1513454Spau.cabre@metempsy.com * notice, this list of conditions and the following disclaimer in the
1613454Spau.cabre@metempsy.com * documentation and/or other materials provided with the distribution;
1713454Spau.cabre@metempsy.com * neither the name of the copyright holders nor the names of its
1813454Spau.cabre@metempsy.com * contributors may be used to endorse or promote products derived from
1913454Spau.cabre@metempsy.com * this software without specific prior written permission.
2013454Spau.cabre@metempsy.com *
2113454Spau.cabre@metempsy.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2213454Spau.cabre@metempsy.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2313454Spau.cabre@metempsy.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2413454Spau.cabre@metempsy.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2513454Spau.cabre@metempsy.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2613454Spau.cabre@metempsy.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2713454Spau.cabre@metempsy.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2813454Spau.cabre@metempsy.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2913454Spau.cabre@metempsy.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3013454Spau.cabre@metempsy.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3113454Spau.cabre@metempsy.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3213454Spau.cabre@metempsy.com *
3313454Spau.cabre@metempsy.com * Authors: Vignyan Reddy, Dibakar Gope and Arthur Perais,
3413454Spau.cabre@metempsy.com * from André Seznec's code.
3513454Spau.cabre@metempsy.com */
3613454Spau.cabre@metempsy.com
3713454Spau.cabre@metempsy.com/* @file
3813454Spau.cabre@metempsy.com * Implementation of a TAGE branch predictor
3913454Spau.cabre@metempsy.com */
4013454Spau.cabre@metempsy.com
4113454Spau.cabre@metempsy.com#include "cpu/pred/tage.hh"
4213454Spau.cabre@metempsy.com
4313454Spau.cabre@metempsy.com#include "base/intmath.hh"
4413454Spau.cabre@metempsy.com#include "base/logging.hh"
4513454Spau.cabre@metempsy.com#include "base/random.hh"
4613454Spau.cabre@metempsy.com#include "base/trace.hh"
4713454Spau.cabre@metempsy.com#include "debug/Fetch.hh"
4813454Spau.cabre@metempsy.com#include "debug/Tage.hh"
4913454Spau.cabre@metempsy.com
5013626Sjairo.balart@metempsy.comTAGE::TAGE(const TAGEParams *params) : BPredUnit(params), tage(params->tage)
5113454Spau.cabre@metempsy.com{
5213454Spau.cabre@metempsy.com}
5313454Spau.cabre@metempsy.com
5413454Spau.cabre@metempsy.com// PREDICTOR UPDATE
5513454Spau.cabre@metempsy.comvoid
5613454Spau.cabre@metempsy.comTAGE::update(ThreadID tid, Addr branch_pc, bool taken, void* bp_history,
5713626Sjairo.balart@metempsy.com              bool squashed, const StaticInstPtr & inst, Addr corrTarget)
5813454Spau.cabre@metempsy.com{
5913454Spau.cabre@metempsy.com    assert(bp_history);
6013454Spau.cabre@metempsy.com
6113454Spau.cabre@metempsy.com    TageBranchInfo *bi = static_cast<TageBranchInfo*>(bp_history);
6213685Sjavier.bueno@metempsy.com    TAGEBase::BranchInfo *tage_bi = bi->tageBranchInfo;
6313454Spau.cabre@metempsy.com
6413626Sjairo.balart@metempsy.com    assert(corrTarget != MaxAddr);
6513626Sjairo.balart@metempsy.com
6613454Spau.cabre@metempsy.com    if (squashed) {
6713454Spau.cabre@metempsy.com        // This restores the global history, then update it
6813454Spau.cabre@metempsy.com        // and recomputes the folded histories.
6913685Sjavier.bueno@metempsy.com        tage->squash(tid, taken, tage_bi, corrTarget);
7013454Spau.cabre@metempsy.com        return;
7113454Spau.cabre@metempsy.com    }
7213454Spau.cabre@metempsy.com
7313685Sjavier.bueno@metempsy.com    int nrand = random_mt.random<int>() & 3;
7413626Sjairo.balart@metempsy.com    if (bi->tageBranchInfo->condBranch) {
7513454Spau.cabre@metempsy.com        DPRINTF(Tage, "Updating tables for branch:%lx; taken?:%d\n",
7613454Spau.cabre@metempsy.com                branch_pc, taken);
7713626Sjairo.balart@metempsy.com        tage->updateStats(taken, bi->tageBranchInfo);
7813685Sjavier.bueno@metempsy.com        tage->condBranchUpdate(tid, branch_pc, taken, tage_bi, nrand,
7913685Sjavier.bueno@metempsy.com                               corrTarget, bi->tageBranchInfo->tagePred);
8013454Spau.cabre@metempsy.com    }
8113454Spau.cabre@metempsy.com
8213685Sjavier.bueno@metempsy.com    // optional non speculative update of the histories
8313685Sjavier.bueno@metempsy.com    tage->updateHistories(tid, branch_pc, taken, tage_bi, false, inst,
8413685Sjavier.bueno@metempsy.com                          corrTarget);
8513626Sjairo.balart@metempsy.com    delete bi;
8613454Spau.cabre@metempsy.com}
8713454Spau.cabre@metempsy.com
8813454Spau.cabre@metempsy.comvoid
8913454Spau.cabre@metempsy.comTAGE::squash(ThreadID tid, void *bp_history)
9013454Spau.cabre@metempsy.com{
9113626Sjairo.balart@metempsy.com    TageBranchInfo *bi = static_cast<TageBranchInfo*>(bp_history);
9213626Sjairo.balart@metempsy.com    DPRINTF(Tage, "Deleting branch info: %lx\n", bi->tageBranchInfo->branchPC);
9313454Spau.cabre@metempsy.com    delete bi;
9413454Spau.cabre@metempsy.com}
9513454Spau.cabre@metempsy.com
9613454Spau.cabre@metempsy.combool
9713626Sjairo.balart@metempsy.comTAGE::predict(ThreadID tid, Addr branch_pc, bool cond_branch, void* &b)
9813626Sjairo.balart@metempsy.com{
9913685Sjavier.bueno@metempsy.com    TageBranchInfo *bi = new TageBranchInfo(*tage);//nHistoryTables+1);
10013626Sjairo.balart@metempsy.com    b = (void*)(bi);
10113626Sjairo.balart@metempsy.com    return tage->tagePredict(tid, branch_pc, cond_branch, bi->tageBranchInfo);
10213626Sjairo.balart@metempsy.com}
10313626Sjairo.balart@metempsy.com
10413626Sjairo.balart@metempsy.combool
10513454Spau.cabre@metempsy.comTAGE::lookup(ThreadID tid, Addr branch_pc, void* &bp_history)
10613454Spau.cabre@metempsy.com{
10713454Spau.cabre@metempsy.com    bool retval = predict(tid, branch_pc, true, bp_history);
10813454Spau.cabre@metempsy.com
10913626Sjairo.balart@metempsy.com    TageBranchInfo *bi = static_cast<TageBranchInfo*>(bp_history);
11013626Sjairo.balart@metempsy.com
11113454Spau.cabre@metempsy.com    DPRINTF(Tage, "Lookup branch: %lx; predict:%d\n", branch_pc, retval);
11213626Sjairo.balart@metempsy.com
11313626Sjairo.balart@metempsy.com    tage->updateHistories(tid, branch_pc, retval, bi->tageBranchInfo, true);
11413454Spau.cabre@metempsy.com
11513454Spau.cabre@metempsy.com    return retval;
11613454Spau.cabre@metempsy.com}
11713454Spau.cabre@metempsy.com
11813454Spau.cabre@metempsy.comvoid
11913454Spau.cabre@metempsy.comTAGE::btbUpdate(ThreadID tid, Addr branch_pc, void* &bp_history)
12013454Spau.cabre@metempsy.com{
12113626Sjairo.balart@metempsy.com    TageBranchInfo *bi = static_cast<TageBranchInfo*>(bp_history);
12213626Sjairo.balart@metempsy.com    tage->btbUpdate(tid, branch_pc, bi->tageBranchInfo);
12313454Spau.cabre@metempsy.com}
12413454Spau.cabre@metempsy.com
12513454Spau.cabre@metempsy.comvoid
12613454Spau.cabre@metempsy.comTAGE::uncondBranch(ThreadID tid, Addr br_pc, void* &bp_history)
12713454Spau.cabre@metempsy.com{
12813454Spau.cabre@metempsy.com    DPRINTF(Tage, "UnConditionalBranch: %lx\n", br_pc);
12913454Spau.cabre@metempsy.com    predict(tid, br_pc, false, bp_history);
13013626Sjairo.balart@metempsy.com    TageBranchInfo *bi = static_cast<TageBranchInfo*>(bp_history);
13113626Sjairo.balart@metempsy.com    tage->updateHistories(tid, br_pc, true, bi->tageBranchInfo, true);
13213455Spau.cabre@metempsy.com}
13313455Spau.cabre@metempsy.com
13413454Spau.cabre@metempsy.comTAGE*
13513454Spau.cabre@metempsy.comTAGEParams::create()
13613454Spau.cabre@metempsy.com{
13713454Spau.cabre@metempsy.com    return new TAGE(this);
13813454Spau.cabre@metempsy.com}
139