decode_impl.hh revision 9514
19444SAndreas.Sandberg@ARM.com/*
29444SAndreas.Sandberg@ARM.com * Copyright (c) 2012 ARM Limited
39444SAndreas.Sandberg@ARM.com * All rights reserved
49444SAndreas.Sandberg@ARM.com *
59444SAndreas.Sandberg@ARM.com * The license below extends only to copyright in the software and shall
69444SAndreas.Sandberg@ARM.com * not be construed as granting a license to any other intellectual
79444SAndreas.Sandberg@ARM.com * property including but not limited to intellectual property relating
89444SAndreas.Sandberg@ARM.com * to a hardware implementation of the functionality of the software
99444SAndreas.Sandberg@ARM.com * licensed hereunder.  You may use the software subject to the license
109444SAndreas.Sandberg@ARM.com * terms below provided that you ensure that this notice is replicated
119444SAndreas.Sandberg@ARM.com * unmodified and in its entirety in all distributions of the software,
129444SAndreas.Sandberg@ARM.com * modified or unmodified, in source code or in binary form.
139444SAndreas.Sandberg@ARM.com *
142329SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
151689SN/A * All rights reserved.
161689SN/A *
171689SN/A * Redistribution and use in source and binary forms, with or without
181689SN/A * modification, are permitted provided that the following conditions are
191689SN/A * met: redistributions of source code must retain the above copyright
201689SN/A * notice, this list of conditions and the following disclaimer;
211689SN/A * redistributions in binary form must reproduce the above copyright
221689SN/A * notice, this list of conditions and the following disclaimer in the
231689SN/A * documentation and/or other materials provided with the distribution;
241689SN/A * neither the name of the copyright holders nor the names of its
251689SN/A * contributors may be used to endorse or promote products derived from
261689SN/A * this software without specific prior written permission.
271689SN/A *
281689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
371689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
381689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
411689SN/A */
421689SN/A
438230Snate@binkert.org#include "arch/types.hh"
448230Snate@binkert.org#include "base/trace.hh"
456658Snate@binkert.org#include "config/the_isa.hh"
461717SN/A#include "cpu/o3/decode.hh"
478230Snate@binkert.org#include "cpu/inst_seq.hh"
488232Snate@binkert.org#include "debug/Activity.hh"
498232Snate@binkert.org#include "debug/Decode.hh"
506221Snate@binkert.org#include "params/DerivO3CPU.hh"
518793Sgblack@eecs.umich.edu#include "sim/full_system.hh"
521060SN/A
538737Skoansin.tan@gmail.com// clang complains about std::set being overloaded with Packet::set if
548737Skoansin.tan@gmail.com// we open up the entire namespace std
558737Skoansin.tan@gmail.comusing std::list;
565529Snate@binkert.org
571060SN/Atemplate<class Impl>
585529Snate@binkert.orgDefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params)
594329Sktlim@umich.edu    : cpu(_cpu),
604329Sktlim@umich.edu      renameToDecodeDelay(params->renameToDecodeDelay),
612292SN/A      iewToDecodeDelay(params->iewToDecodeDelay),
622292SN/A      commitToDecodeDelay(params->commitToDecodeDelay),
632292SN/A      fetchToDecodeDelay(params->fetchToDecodeDelay),
642292SN/A      decodeWidth(params->decodeWidth),
655529Snate@binkert.org      numThreads(params->numThreads)
661060SN/A{
679444SAndreas.Sandberg@ARM.com    // @todo: Make into a parameter
689444SAndreas.Sandberg@ARM.com    skidBufferMax = (fetchToDecodeDelay + 1) *  params->fetchWidth;
699444SAndreas.Sandberg@ARM.com}
709444SAndreas.Sandberg@ARM.com
719444SAndreas.Sandberg@ARM.comtemplate<class Impl>
729444SAndreas.Sandberg@ARM.comvoid
739444SAndreas.Sandberg@ARM.comDefaultDecode<Impl>::startupStage()
749444SAndreas.Sandberg@ARM.com{
759444SAndreas.Sandberg@ARM.com    resetStage();
769444SAndreas.Sandberg@ARM.com}
779444SAndreas.Sandberg@ARM.com
789444SAndreas.Sandberg@ARM.comtemplate<class Impl>
799444SAndreas.Sandberg@ARM.comvoid
809444SAndreas.Sandberg@ARM.comDefaultDecode<Impl>::resetStage()
819444SAndreas.Sandberg@ARM.com{
822292SN/A    _status = Inactive;
832292SN/A
842348SN/A    // Setup status, make sure stall signals are clear.
856221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
866221Snate@binkert.org        decodeStatus[tid] = Idle;
872292SN/A
886221Snate@binkert.org        stalls[tid].rename = false;
896221Snate@binkert.org        stalls[tid].iew = false;
906221Snate@binkert.org        stalls[tid].commit = false;
912292SN/A    }
922292SN/A}
932292SN/A
942292SN/Atemplate <class Impl>
952292SN/Astd::string
962292SN/ADefaultDecode<Impl>::name() const
972292SN/A{
982292SN/A    return cpu->name() + ".decode";
991060SN/A}
1001060SN/A
1011062SN/Atemplate <class Impl>
1021062SN/Avoid
1032292SN/ADefaultDecode<Impl>::regStats()
1041062SN/A{
1051062SN/A    decodeIdleCycles
1068240Snate@binkert.org        .name(name() + ".IdleCycles")
1071062SN/A        .desc("Number of cycles decode is idle")
1081062SN/A        .prereq(decodeIdleCycles);
1091062SN/A    decodeBlockedCycles
1108240Snate@binkert.org        .name(name() + ".BlockedCycles")
1111062SN/A        .desc("Number of cycles decode is blocked")
1121062SN/A        .prereq(decodeBlockedCycles);
1132292SN/A    decodeRunCycles
1148240Snate@binkert.org        .name(name() + ".RunCycles")
1152292SN/A        .desc("Number of cycles decode is running")
1162292SN/A        .prereq(decodeRunCycles);
1171062SN/A    decodeUnblockCycles
1188240Snate@binkert.org        .name(name() + ".UnblockCycles")
1191062SN/A        .desc("Number of cycles decode is unblocking")
1201062SN/A        .prereq(decodeUnblockCycles);
1211062SN/A    decodeSquashCycles
1228240Snate@binkert.org        .name(name() + ".SquashCycles")
1231062SN/A        .desc("Number of cycles decode is squashing")
1241062SN/A        .prereq(decodeSquashCycles);
1252307SN/A    decodeBranchResolved
1268240Snate@binkert.org        .name(name() + ".BranchResolved")
1272307SN/A        .desc("Number of times decode resolved a branch")
1282307SN/A        .prereq(decodeBranchResolved);
1291062SN/A    decodeBranchMispred
1308240Snate@binkert.org        .name(name() + ".BranchMispred")
1311062SN/A        .desc("Number of times decode detected a branch misprediction")
1321062SN/A        .prereq(decodeBranchMispred);
1331062SN/A    decodeControlMispred
1348240Snate@binkert.org        .name(name() + ".ControlMispred")
1351062SN/A        .desc("Number of times decode detected an instruction incorrectly"
1361062SN/A              " predicted as a control")
1371062SN/A        .prereq(decodeControlMispred);
1381062SN/A    decodeDecodedInsts
1398240Snate@binkert.org        .name(name() + ".DecodedInsts")
1401062SN/A        .desc("Number of instructions handled by decode")
1411062SN/A        .prereq(decodeDecodedInsts);
1421062SN/A    decodeSquashedInsts
1438240Snate@binkert.org        .name(name() + ".SquashedInsts")
1441062SN/A        .desc("Number of squashed instructions handled by decode")
1451062SN/A        .prereq(decodeSquashedInsts);
1461062SN/A}
1471062SN/A
1481060SN/Atemplate<class Impl>
1491060SN/Avoid
1502292SN/ADefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
1511060SN/A{
1521060SN/A    timeBuffer = tb_ptr;
1531060SN/A
1541060SN/A    // Setup wire to write information back to fetch.
1551060SN/A    toFetch = timeBuffer->getWire(0);
1561060SN/A
1571060SN/A    // Create wires to get information from proper places in time buffer.
1581060SN/A    fromRename = timeBuffer->getWire(-renameToDecodeDelay);
1591060SN/A    fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
1601060SN/A    fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
1611060SN/A}
1621060SN/A
1631060SN/Atemplate<class Impl>
1641060SN/Avoid
1652292SN/ADefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
1661060SN/A{
1671060SN/A    decodeQueue = dq_ptr;
1681060SN/A
1691060SN/A    // Setup wire to write information to proper place in decode queue.
1701060SN/A    toRename = decodeQueue->getWire(0);
1711060SN/A}
1721060SN/A
1731060SN/Atemplate<class Impl>
1741060SN/Avoid
1752292SN/ADefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
1761060SN/A{
1771060SN/A    fetchQueue = fq_ptr;
1781060SN/A
1791060SN/A    // Setup wire to read information from fetch queue.
1801060SN/A    fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
1811060SN/A}
1821060SN/A
1831060SN/Atemplate<class Impl>
1842292SN/Avoid
1856221Snate@binkert.orgDefaultDecode<Impl>::setActiveThreads(std::list<ThreadID> *at_ptr)
1862292SN/A{
1872292SN/A    activeThreads = at_ptr;
1882292SN/A}
1892292SN/A
1902307SN/Atemplate <class Impl>
1919444SAndreas.Sandberg@ARM.comvoid
1929444SAndreas.Sandberg@ARM.comDefaultDecode<Impl>::drainSanityCheck() const
1932307SN/A{
1946221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
1959444SAndreas.Sandberg@ARM.com        assert(insts[tid].empty());
1969444SAndreas.Sandberg@ARM.com        assert(skidBuffer[tid].empty());
1972307SN/A    }
1982307SN/A}
1992307SN/A
2002292SN/Atemplate<class Impl>
2012292SN/Abool
2026221Snate@binkert.orgDefaultDecode<Impl>::checkStall(ThreadID tid) const
2032292SN/A{
2042292SN/A    bool ret_val = false;
2052292SN/A
2062292SN/A    if (stalls[tid].rename) {
2072292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
2082292SN/A        ret_val = true;
2092292SN/A    } else if (stalls[tid].iew) {
2102292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
2112292SN/A        ret_val = true;
2122292SN/A    } else if (stalls[tid].commit) {
2132292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
2142292SN/A        ret_val = true;
2152292SN/A    }
2162292SN/A
2172292SN/A    return ret_val;
2182292SN/A}
2192292SN/A
2202292SN/Atemplate<class Impl>
2211681SN/Ainline bool
2222292SN/ADefaultDecode<Impl>::fetchInstsValid()
2231681SN/A{
2241681SN/A    return fromFetch->size > 0;
2251681SN/A}
2261681SN/A
2271681SN/Atemplate<class Impl>
2282292SN/Abool
2296221Snate@binkert.orgDefaultDecode<Impl>::block(ThreadID tid)
2301060SN/A{
2312292SN/A    DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
2321060SN/A
2331060SN/A    // Add the current inputs to the skid buffer so they can be
2341060SN/A    // reprocessed when this stage unblocks.
2352292SN/A    skidInsert(tid);
2361060SN/A
2372348SN/A    // If the decode status is blocked or unblocking then decode has not yet
2382348SN/A    // signalled fetch to unblock. In that case, there is no need to tell
2392348SN/A    // fetch to block.
2402292SN/A    if (decodeStatus[tid] != Blocked) {
2412292SN/A        // Set the status to Blocked.
2422292SN/A        decodeStatus[tid] = Blocked;
2432348SN/A
2449514SAli.Saidi@ARM.com        if (toFetch->decodeUnblock[tid]) {
2459514SAli.Saidi@ARM.com            toFetch->decodeUnblock[tid] = false;
2469514SAli.Saidi@ARM.com        } else {
2472348SN/A            toFetch->decodeBlock[tid] = true;
2482348SN/A            wroteToTimeBuffer = true;
2492348SN/A        }
2502348SN/A
2512292SN/A        return true;
2522292SN/A    }
2532292SN/A
2542292SN/A    return false;
2551060SN/A}
2561060SN/A
2571060SN/Atemplate<class Impl>
2582292SN/Abool
2596221Snate@binkert.orgDefaultDecode<Impl>::unblock(ThreadID tid)
2601060SN/A{
2612292SN/A    // Decode is done unblocking only if the skid buffer is empty.
2622292SN/A    if (skidBuffer[tid].empty()) {
2632292SN/A        DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
2642292SN/A        toFetch->decodeUnblock[tid] = true;
2652292SN/A        wroteToTimeBuffer = true;
2661060SN/A
2672292SN/A        decodeStatus[tid] = Running;
2682292SN/A        return true;
2691060SN/A    }
2701681SN/A
2712329SN/A    DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
2722329SN/A
2732292SN/A    return false;
2741060SN/A}
2751060SN/A
2761060SN/Atemplate<class Impl>
2771060SN/Avoid
2786221Snate@binkert.orgDefaultDecode<Impl>::squash(DynInstPtr &inst, ThreadID tid)
2791060SN/A{
2807720Sgblack@eecs.umich.edu    DPRINTF(Decode, "[tid:%i]: [sn:%i] Squashing due to incorrect branch "
2817720Sgblack@eecs.umich.edu            "prediction detected at decode.\n", tid, inst->seqNum);
2822292SN/A
2832348SN/A    // Send back mispredict information.
2842292SN/A    toFetch->decodeInfo[tid].branchMispredict = true;
2852935Sksewell@umich.edu    toFetch->decodeInfo[tid].predIncorrect = true;
2868842Smrinmoy.ghosh@arm.com    toFetch->decodeInfo[tid].mispredictInst = inst;
2876036Sksewell@umich.edu    toFetch->decodeInfo[tid].squash = true;
2882292SN/A    toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
2896036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
2907720Sgblack@eecs.umich.edu    toFetch->decodeInfo[tid].branchTaken = inst->pcState().branching();
2918503Sgblack@eecs.umich.edu    toFetch->decodeInfo[tid].squashInst = inst;
2928842Smrinmoy.ghosh@arm.com    if (toFetch->decodeInfo[tid].mispredictInst->isUncondCtrl()) {
2938842Smrinmoy.ghosh@arm.com            toFetch->decodeInfo[tid].branchTaken = true;
2948842Smrinmoy.ghosh@arm.com    }
2956036Sksewell@umich.edu
2963093Sksewell@umich.edu    InstSeqNum squash_seq_num = inst->seqNum;
2972935Sksewell@umich.edu
2982348SN/A    // Might have to tell fetch to unblock.
2992292SN/A    if (decodeStatus[tid] == Blocked ||
3002292SN/A        decodeStatus[tid] == Unblocking) {
3012292SN/A        toFetch->decodeUnblock[tid] = 1;
3022292SN/A    }
3032292SN/A
3041060SN/A    // Set status to squashing.
3052292SN/A    decodeStatus[tid] = Squashing;
3061060SN/A
3072292SN/A    for (int i=0; i<fromFetch->size; i++) {
3082292SN/A        if (fromFetch->insts[i]->threadNumber == tid &&
3092935Sksewell@umich.edu            fromFetch->insts[i]->seqNum > squash_seq_num) {
3102731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3112292SN/A        }
3122292SN/A    }
3132292SN/A
3142348SN/A    // Clear the instruction list and skid buffer in case they have any
3152348SN/A    // insts in them.
3162292SN/A    while (!insts[tid].empty()) {
3172292SN/A        insts[tid].pop();
3182292SN/A    }
3191060SN/A
3202292SN/A    while (!skidBuffer[tid].empty()) {
3212292SN/A        skidBuffer[tid].pop();
3222292SN/A    }
3232292SN/A
3242292SN/A    // Squash instructions up until this one
3252935Sksewell@umich.edu    cpu->removeInstsUntil(squash_seq_num, tid);
3262292SN/A}
3272292SN/A
3282292SN/Atemplate<class Impl>
3292292SN/Aunsigned
3306221Snate@binkert.orgDefaultDecode<Impl>::squash(ThreadID tid)
3312292SN/A{
3322292SN/A    DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
3332292SN/A
3342292SN/A    if (decodeStatus[tid] == Blocked ||
3352292SN/A        decodeStatus[tid] == Unblocking) {
3368793Sgblack@eecs.umich.edu        if (FullSystem) {
3378793Sgblack@eecs.umich.edu            toFetch->decodeUnblock[tid] = 1;
3382292SN/A        } else {
3398793Sgblack@eecs.umich.edu            // In syscall emulation, we can have both a block and a squash due
3408793Sgblack@eecs.umich.edu            // to a syscall in the same cycle.  This would cause both signals
3418793Sgblack@eecs.umich.edu            // to be high.  This shouldn't happen in full system.
3428793Sgblack@eecs.umich.edu            // @todo: Determine if this still happens.
3438793Sgblack@eecs.umich.edu            if (toFetch->decodeBlock[tid])
3448793Sgblack@eecs.umich.edu                toFetch->decodeBlock[tid] = 0;
3458793Sgblack@eecs.umich.edu            else
3468793Sgblack@eecs.umich.edu                toFetch->decodeUnblock[tid] = 1;
3472292SN/A        }
3482292SN/A    }
3492292SN/A
3502292SN/A    // Set status to squashing.
3512292SN/A    decodeStatus[tid] = Squashing;
3522292SN/A
3532292SN/A    // Go through incoming instructions from fetch and squash them.
3542292SN/A    unsigned squash_count = 0;
3552292SN/A
3562292SN/A    for (int i=0; i<fromFetch->size; i++) {
3572292SN/A        if (fromFetch->insts[i]->threadNumber == tid) {
3582731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3592292SN/A            squash_count++;
3602292SN/A        }
3612292SN/A    }
3622292SN/A
3632348SN/A    // Clear the instruction list and skid buffer in case they have any
3642348SN/A    // insts in them.
3652292SN/A    while (!insts[tid].empty()) {
3662292SN/A        insts[tid].pop();
3672292SN/A    }
3682292SN/A
3692292SN/A    while (!skidBuffer[tid].empty()) {
3702292SN/A        skidBuffer[tid].pop();
3712292SN/A    }
3722292SN/A
3732292SN/A    return squash_count;
3742292SN/A}
3752292SN/A
3762292SN/Atemplate<class Impl>
3772292SN/Avoid
3786221Snate@binkert.orgDefaultDecode<Impl>::skidInsert(ThreadID tid)
3792292SN/A{
3802292SN/A    DynInstPtr inst = NULL;
3812292SN/A
3822292SN/A    while (!insts[tid].empty()) {
3832292SN/A        inst = insts[tid].front();
3842292SN/A
3852292SN/A        insts[tid].pop();
3862292SN/A
3872292SN/A        assert(tid == inst->threadNumber);
3882292SN/A
3897720Sgblack@eecs.umich.edu        DPRINTF(Decode,"Inserting [sn:%lli] PC: %s into decode skidBuffer %i\n",
3907720Sgblack@eecs.umich.edu                inst->seqNum, inst->pcState(), inst->threadNumber);
3912292SN/A
3922292SN/A        skidBuffer[tid].push(inst);
3932292SN/A    }
3942292SN/A
3952329SN/A    // @todo: Eventually need to enforce this by not letting a thread
3962292SN/A    // fetch past its skidbuffer
3972292SN/A    assert(skidBuffer[tid].size() <= skidBufferMax);
3982292SN/A}
3992292SN/A
4002292SN/Atemplate<class Impl>
4012292SN/Abool
4022292SN/ADefaultDecode<Impl>::skidsEmpty()
4032292SN/A{
4046221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4056221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4062292SN/A
4073867Sbinkertn@umich.edu    while (threads != end) {
4086221Snate@binkert.org        ThreadID tid = *threads++;
4093867Sbinkertn@umich.edu        if (!skidBuffer[tid].empty())
4102292SN/A            return false;
4112292SN/A    }
4122292SN/A
4132292SN/A    return true;
4142292SN/A}
4152292SN/A
4162292SN/Atemplate<class Impl>
4172292SN/Avoid
4182292SN/ADefaultDecode<Impl>::updateStatus()
4192292SN/A{
4202292SN/A    bool any_unblocking = false;
4212292SN/A
4226221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4236221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4242292SN/A
4253867Sbinkertn@umich.edu    while (threads != end) {
4266221Snate@binkert.org        ThreadID tid = *threads++;
4272292SN/A
4282292SN/A        if (decodeStatus[tid] == Unblocking) {
4292292SN/A            any_unblocking = true;
4302292SN/A            break;
4312292SN/A        }
4322292SN/A    }
4332292SN/A
4342292SN/A    // Decode will have activity if it's unblocking.
4352292SN/A    if (any_unblocking) {
4362292SN/A        if (_status == Inactive) {
4372292SN/A            _status = Active;
4382292SN/A
4392292SN/A            DPRINTF(Activity, "Activating stage.\n");
4402292SN/A
4412733Sktlim@umich.edu            cpu->activateStage(O3CPU::DecodeIdx);
4422292SN/A        }
4432292SN/A    } else {
4442292SN/A        // If it's not unblocking, then decode will not have any internal
4452292SN/A        // activity.  Switch it to inactive.
4462292SN/A        if (_status == Active) {
4472292SN/A            _status = Inactive;
4482292SN/A            DPRINTF(Activity, "Deactivating stage.\n");
4492292SN/A
4502733Sktlim@umich.edu            cpu->deactivateStage(O3CPU::DecodeIdx);
4512292SN/A        }
4522292SN/A    }
4532292SN/A}
4542292SN/A
4552292SN/Atemplate <class Impl>
4562292SN/Avoid
4572292SN/ADefaultDecode<Impl>::sortInsts()
4582292SN/A{
4592292SN/A    int insts_from_fetch = fromFetch->size;
4602292SN/A    for (int i = 0; i < insts_from_fetch; ++i) {
4612292SN/A        insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
4621060SN/A    }
4631060SN/A}
4641060SN/A
4651060SN/Atemplate<class Impl>
4661060SN/Avoid
4676221Snate@binkert.orgDefaultDecode<Impl>::readStallSignals(ThreadID tid)
4681060SN/A{
4692292SN/A    if (fromRename->renameBlock[tid]) {
4702292SN/A        stalls[tid].rename = true;
4712292SN/A    }
4721060SN/A
4732292SN/A    if (fromRename->renameUnblock[tid]) {
4742292SN/A        assert(stalls[tid].rename);
4752292SN/A        stalls[tid].rename = false;
4762292SN/A    }
4771060SN/A
4782292SN/A    if (fromIEW->iewBlock[tid]) {
4792292SN/A        stalls[tid].iew = true;
4802292SN/A    }
4811062SN/A
4822292SN/A    if (fromIEW->iewUnblock[tid]) {
4832292SN/A        assert(stalls[tid].iew);
4842292SN/A        stalls[tid].iew = false;
4852292SN/A    }
4861061SN/A
4872292SN/A    if (fromCommit->commitBlock[tid]) {
4882292SN/A        stalls[tid].commit = true;
4892292SN/A    }
4901062SN/A
4912292SN/A    if (fromCommit->commitUnblock[tid]) {
4922292SN/A        assert(stalls[tid].commit);
4932292SN/A        stalls[tid].commit = false;
4942292SN/A    }
4952292SN/A}
4961060SN/A
4972292SN/Atemplate <class Impl>
4982292SN/Abool
4996221Snate@binkert.orgDefaultDecode<Impl>::checkSignalsAndUpdate(ThreadID tid)
5002292SN/A{
5012292SN/A    // Check if there's a squash signal, squash if there is.
5022292SN/A    // Check stall signals, block if necessary.
5032292SN/A    // If status was blocked
5042292SN/A    //     Check if stall conditions have passed
5052292SN/A    //         if so then go to unblocking
5062292SN/A    // If status was Squashing
5072292SN/A    //     check if squashing is not high.  Switch to running this cycle.
5081060SN/A
5092292SN/A    // Update the per thread stall statuses.
5102292SN/A    readStallSignals(tid);
5111060SN/A
5122292SN/A    // Check squash signals from commit.
5132292SN/A    if (fromCommit->commitInfo[tid].squash) {
5141681SN/A
5152292SN/A        DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
5162292SN/A                "from commit.\n", tid);
5172292SN/A
5182292SN/A        squash(tid);
5192292SN/A
5202292SN/A        return true;
5212292SN/A    }
5222292SN/A
5232292SN/A    // Check ROB squash signals from commit.
5242292SN/A    if (fromCommit->commitInfo[tid].robSquashing) {
5252703Sktlim@umich.edu        DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
5262292SN/A
5272292SN/A        // Continue to squash.
5282292SN/A        decodeStatus[tid] = Squashing;
5292292SN/A
5302292SN/A        return true;
5312292SN/A    }
5322292SN/A
5332292SN/A    if (checkStall(tid)) {
5342292SN/A        return block(tid);
5352292SN/A    }
5362292SN/A
5372292SN/A    if (decodeStatus[tid] == Blocked) {
5382292SN/A        DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
5392292SN/A                tid);
5402292SN/A
5412292SN/A        decodeStatus[tid] = Unblocking;
5422292SN/A
5432292SN/A        unblock(tid);
5442292SN/A
5452292SN/A        return true;
5462292SN/A    }
5472292SN/A
5482292SN/A    if (decodeStatus[tid] == Squashing) {
5492292SN/A        // Switch status to running if decode isn't being told to block or
5502292SN/A        // squash this cycle.
5512292SN/A        DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
5522292SN/A                tid);
5532292SN/A
5542292SN/A        decodeStatus[tid] = Running;
5552292SN/A
5562292SN/A        return false;
5572292SN/A    }
5582292SN/A
5592292SN/A    // If we've reached this point, we have not gotten any signals that
5602292SN/A    // cause decode to change its status.  Decode remains the same as before.
5612292SN/A    return false;
5622292SN/A}
5632292SN/A
5642292SN/Atemplate<class Impl>
5652292SN/Avoid
5662292SN/ADefaultDecode<Impl>::tick()
5672292SN/A{
5682292SN/A    wroteToTimeBuffer = false;
5692292SN/A
5702292SN/A    bool status_change = false;
5712292SN/A
5722292SN/A    toRenameIndex = 0;
5732292SN/A
5746221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
5756221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
5762292SN/A
5772292SN/A    sortInsts();
5782292SN/A
5792292SN/A    //Check stall and squash signals.
5803867Sbinkertn@umich.edu    while (threads != end) {
5816221Snate@binkert.org        ThreadID tid = *threads++;
5822292SN/A
5832292SN/A        DPRINTF(Decode,"Processing [tid:%i]\n",tid);
5842292SN/A        status_change =  checkSignalsAndUpdate(tid) || status_change;
5852292SN/A
5862292SN/A        decode(status_change, tid);
5872292SN/A    }
5882292SN/A
5892292SN/A    if (status_change) {
5902292SN/A        updateStatus();
5912292SN/A    }
5922292SN/A
5932292SN/A    if (wroteToTimeBuffer) {
5942292SN/A        DPRINTF(Activity, "Activity this cycle.\n");
5952292SN/A
5962292SN/A        cpu->activityThisCycle();
5971060SN/A    }
5981060SN/A}
5991060SN/A
6001060SN/Atemplate<class Impl>
6011060SN/Avoid
6026221Snate@binkert.orgDefaultDecode<Impl>::decode(bool &status_change, ThreadID tid)
6031060SN/A{
6042292SN/A    // If status is Running or idle,
6052292SN/A    //     call decodeInsts()
6062292SN/A    // If status is Unblocking,
6072292SN/A    //     buffer any instructions coming from fetch
6082292SN/A    //     continue trying to empty skid buffer
6092292SN/A    //     check if stall conditions have passed
6102292SN/A
6112292SN/A    if (decodeStatus[tid] == Blocked) {
6122292SN/A        ++decodeBlockedCycles;
6132292SN/A    } else if (decodeStatus[tid] == Squashing) {
6142292SN/A        ++decodeSquashCycles;
6151060SN/A    }
6161060SN/A
6172292SN/A    // Decode should try to decode as many instructions as its bandwidth
6182292SN/A    // will allow, as long as it is not currently blocked.
6192292SN/A    if (decodeStatus[tid] == Running ||
6202292SN/A        decodeStatus[tid] == Idle) {
6212935Sksewell@umich.edu        DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
6222292SN/A                "stage.\n",tid);
6232292SN/A
6242292SN/A        decodeInsts(tid);
6252292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6262292SN/A        // Make sure that the skid buffer has something in it if the
6272292SN/A        // status is unblocking.
6282292SN/A        assert(!skidsEmpty());
6292292SN/A
6302292SN/A        // If the status was unblocking, then instructions from the skid
6312292SN/A        // buffer were used.  Remove those instructions and handle
6322292SN/A        // the rest of unblocking.
6332292SN/A        decodeInsts(tid);
6342292SN/A
6352292SN/A        if (fetchInstsValid()) {
6362292SN/A            // Add the current inputs to the skid buffer so they can be
6372292SN/A            // reprocessed when this stage unblocks.
6382292SN/A            skidInsert(tid);
6392292SN/A        }
6402292SN/A
6412292SN/A        status_change = unblock(tid) || status_change;
6421060SN/A    }
6432292SN/A}
6441060SN/A
6452292SN/Atemplate <class Impl>
6462292SN/Avoid
6476221Snate@binkert.orgDefaultDecode<Impl>::decodeInsts(ThreadID tid)
6482292SN/A{
6492292SN/A    // Instructions can come either from the skid buffer or the list of
6502292SN/A    // instructions coming from fetch, depending on decode's status.
6512292SN/A    int insts_available = decodeStatus[tid] == Unblocking ?
6522292SN/A        skidBuffer[tid].size() : insts[tid].size();
6532292SN/A
6542292SN/A    if (insts_available == 0) {
6552292SN/A        DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
6562292SN/A                " early.\n",tid);
6571060SN/A        // Should I change the status to idle?
6581062SN/A        ++decodeIdleCycles;
6591060SN/A        return;
6602292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6612292SN/A        DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
6622292SN/A                "buffer.\n",tid);
6632292SN/A        ++decodeUnblockCycles;
6642292SN/A    } else if (decodeStatus[tid] == Running) {
6652292SN/A        ++decodeRunCycles;
6661060SN/A    }
6671060SN/A
6681061SN/A    DynInstPtr inst;
6691061SN/A
6702292SN/A    std::queue<DynInstPtr>
6712292SN/A        &insts_to_decode = decodeStatus[tid] == Unblocking ?
6722292SN/A        skidBuffer[tid] : insts[tid];
6731061SN/A
6742292SN/A    DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
6751060SN/A
6762292SN/A    while (insts_available > 0 && toRenameIndex < decodeWidth) {
6772292SN/A        assert(!insts_to_decode.empty());
6781060SN/A
6792292SN/A        inst = insts_to_decode.front();
6801062SN/A
6812292SN/A        insts_to_decode.pop();
6821061SN/A
6832292SN/A        DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
6847720Sgblack@eecs.umich.edu                "PC %s\n", tid, inst->seqNum, inst->pcState());
6851061SN/A
6861061SN/A        if (inst->isSquashed()) {
6877720Sgblack@eecs.umich.edu            DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %s is "
6881061SN/A                    "squashed, skipping.\n",
6897720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6901061SN/A
6911062SN/A            ++decodeSquashedInsts;
6921062SN/A
6931061SN/A            --insts_available;
6941061SN/A
6951061SN/A            continue;
6961061SN/A        }
6971060SN/A
6981681SN/A        // Also check if instructions have no source registers.  Mark
6991681SN/A        // them as ready to issue at any time.  Not sure if this check
7001681SN/A        // should exist here or at a later stage; however it doesn't matter
7011681SN/A        // too much for function correctness.
7021681SN/A        if (inst->numSrcRegs() == 0) {
7031681SN/A            inst->setCanIssue();
7041681SN/A        }
7051681SN/A
7061060SN/A        // This current instruction is valid, so add it into the decode
7071060SN/A        // queue.  The next instruction may not be valid, so check to
7081060SN/A        // see if branches were predicted correctly.
7092292SN/A        toRename->insts[toRenameIndex] = inst;
7101061SN/A
7111061SN/A        ++(toRename->size);
7122292SN/A        ++toRenameIndex;
7132292SN/A        ++decodeDecodedInsts;
7142292SN/A        --insts_available;
7151060SN/A
7168471SGiacomo.Gabrielli@arm.com#if TRACING_ON
7179046SAli.Saidi@ARM.com        inst->decodeTick = curTick() - inst->fetchTick;
7188471SGiacomo.Gabrielli@arm.com#endif
7198471SGiacomo.Gabrielli@arm.com
7201060SN/A        // Ensure that if it was predicted as a branch, it really is a
7211061SN/A        // branch.
7223796Sgblack@eecs.umich.edu        if (inst->readPredTaken() && !inst->isControl()) {
7231060SN/A            panic("Instruction predicted as a branch!");
7241060SN/A
7251062SN/A            ++decodeControlMispred;
7262292SN/A
7271060SN/A            // Might want to set some sort of boolean and just do
7281060SN/A            // a check at the end
7292292SN/A            squash(inst, inst->threadNumber);
7302292SN/A
7311060SN/A            break;
7321060SN/A        }
7331060SN/A
7341062SN/A        // Go ahead and compute any PC-relative branches.
7351063SN/A        if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
7362307SN/A            ++decodeBranchResolved;
7371062SN/A
7387720Sgblack@eecs.umich.edu            if (!(inst->branchTarget() == inst->readPredTarg())) {
7391062SN/A                ++decodeBranchMispred;
7402292SN/A
7411060SN/A                // Might want to set some sort of boolean and just do
7421060SN/A                // a check at the end
7432292SN/A                squash(inst, inst->threadNumber);
7447720Sgblack@eecs.umich.edu                TheISA::PCState target = inst->branchTarget();
7456036Sksewell@umich.edu
7467720Sgblack@eecs.umich.edu                DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %s\n",
7477720Sgblack@eecs.umich.edu                        inst->seqNum, target);
7486036Sksewell@umich.edu                //The micro pc after an instruction level branch should be 0
7497720Sgblack@eecs.umich.edu                inst->setPredTarg(target);
7502935Sksewell@umich.edu                break;
7512935Sksewell@umich.edu            }
7522935Sksewell@umich.edu        }
7531060SN/A    }
7541061SN/A
7552292SN/A    // If we didn't process all instructions, then we will need to block
7562292SN/A    // and put all those instructions into the skid buffer.
7572292SN/A    if (!insts_to_decode.empty()) {
7582292SN/A        block(tid);
7592292SN/A    }
7602292SN/A
7612292SN/A    // Record that decode has written to the time buffer for activity
7622292SN/A    // tracking.
7632292SN/A    if (toRenameIndex) {
7642292SN/A        wroteToTimeBuffer = true;
7652292SN/A    }
7661060SN/A}
767