decode_impl.hh revision 8230
11689SN/A/*
22329SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
291689SN/A */
301689SN/A
318230Snate@binkert.org#include "arch/types.hh"
328230Snate@binkert.org#include "base/trace.hh"
338230Snate@binkert.org#include "config/full_system.hh"
346658Snate@binkert.org#include "config/the_isa.hh"
351717SN/A#include "cpu/o3/decode.hh"
368230Snate@binkert.org#include "cpu/inst_seq.hh"
376221Snate@binkert.org#include "params/DerivO3CPU.hh"
381060SN/A
396221Snate@binkert.orgusing namespace std;
405529Snate@binkert.org
411060SN/Atemplate<class Impl>
425529Snate@binkert.orgDefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params)
434329Sktlim@umich.edu    : cpu(_cpu),
444329Sktlim@umich.edu      renameToDecodeDelay(params->renameToDecodeDelay),
452292SN/A      iewToDecodeDelay(params->iewToDecodeDelay),
462292SN/A      commitToDecodeDelay(params->commitToDecodeDelay),
472292SN/A      fetchToDecodeDelay(params->fetchToDecodeDelay),
482292SN/A      decodeWidth(params->decodeWidth),
495529Snate@binkert.org      numThreads(params->numThreads)
501060SN/A{
512292SN/A    _status = Inactive;
522292SN/A
532348SN/A    // Setup status, make sure stall signals are clear.
546221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
556221Snate@binkert.org        decodeStatus[tid] = Idle;
562292SN/A
576221Snate@binkert.org        stalls[tid].rename = false;
586221Snate@binkert.org        stalls[tid].iew = false;
596221Snate@binkert.org        stalls[tid].commit = false;
602292SN/A    }
612292SN/A
622292SN/A    // @todo: Make into a parameter
632292SN/A    skidBufferMax = (fetchToDecodeDelay * params->fetchWidth) + decodeWidth;
642292SN/A}
652292SN/A
662292SN/Atemplate <class Impl>
672292SN/Astd::string
682292SN/ADefaultDecode<Impl>::name() const
692292SN/A{
702292SN/A    return cpu->name() + ".decode";
711060SN/A}
721060SN/A
731062SN/Atemplate <class Impl>
741062SN/Avoid
752292SN/ADefaultDecode<Impl>::regStats()
761062SN/A{
771062SN/A    decodeIdleCycles
782307SN/A        .name(name() + ".DECODE:IdleCycles")
791062SN/A        .desc("Number of cycles decode is idle")
801062SN/A        .prereq(decodeIdleCycles);
811062SN/A    decodeBlockedCycles
822307SN/A        .name(name() + ".DECODE:BlockedCycles")
831062SN/A        .desc("Number of cycles decode is blocked")
841062SN/A        .prereq(decodeBlockedCycles);
852292SN/A    decodeRunCycles
862307SN/A        .name(name() + ".DECODE:RunCycles")
872292SN/A        .desc("Number of cycles decode is running")
882292SN/A        .prereq(decodeRunCycles);
891062SN/A    decodeUnblockCycles
902307SN/A        .name(name() + ".DECODE:UnblockCycles")
911062SN/A        .desc("Number of cycles decode is unblocking")
921062SN/A        .prereq(decodeUnblockCycles);
931062SN/A    decodeSquashCycles
942307SN/A        .name(name() + ".DECODE:SquashCycles")
951062SN/A        .desc("Number of cycles decode is squashing")
961062SN/A        .prereq(decodeSquashCycles);
972307SN/A    decodeBranchResolved
982307SN/A        .name(name() + ".DECODE:BranchResolved")
992307SN/A        .desc("Number of times decode resolved a branch")
1002307SN/A        .prereq(decodeBranchResolved);
1011062SN/A    decodeBranchMispred
1022307SN/A        .name(name() + ".DECODE:BranchMispred")
1031062SN/A        .desc("Number of times decode detected a branch misprediction")
1041062SN/A        .prereq(decodeBranchMispred);
1051062SN/A    decodeControlMispred
1062307SN/A        .name(name() + ".DECODE:ControlMispred")
1071062SN/A        .desc("Number of times decode detected an instruction incorrectly"
1081062SN/A              " predicted as a control")
1091062SN/A        .prereq(decodeControlMispred);
1101062SN/A    decodeDecodedInsts
1112307SN/A        .name(name() + ".DECODE:DecodedInsts")
1121062SN/A        .desc("Number of instructions handled by decode")
1131062SN/A        .prereq(decodeDecodedInsts);
1141062SN/A    decodeSquashedInsts
1152307SN/A        .name(name() + ".DECODE:SquashedInsts")
1161062SN/A        .desc("Number of squashed instructions handled by decode")
1171062SN/A        .prereq(decodeSquashedInsts);
1181062SN/A}
1191062SN/A
1201060SN/Atemplate<class Impl>
1211060SN/Avoid
1222292SN/ADefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
1231060SN/A{
1241060SN/A    timeBuffer = tb_ptr;
1251060SN/A
1261060SN/A    // Setup wire to write information back to fetch.
1271060SN/A    toFetch = timeBuffer->getWire(0);
1281060SN/A
1291060SN/A    // Create wires to get information from proper places in time buffer.
1301060SN/A    fromRename = timeBuffer->getWire(-renameToDecodeDelay);
1311060SN/A    fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
1321060SN/A    fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
1331060SN/A}
1341060SN/A
1351060SN/Atemplate<class Impl>
1361060SN/Avoid
1372292SN/ADefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
1381060SN/A{
1391060SN/A    decodeQueue = dq_ptr;
1401060SN/A
1411060SN/A    // Setup wire to write information to proper place in decode queue.
1421060SN/A    toRename = decodeQueue->getWire(0);
1431060SN/A}
1441060SN/A
1451060SN/Atemplate<class Impl>
1461060SN/Avoid
1472292SN/ADefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
1481060SN/A{
1491060SN/A    fetchQueue = fq_ptr;
1501060SN/A
1511060SN/A    // Setup wire to read information from fetch queue.
1521060SN/A    fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
1531060SN/A}
1541060SN/A
1551060SN/Atemplate<class Impl>
1562292SN/Avoid
1576221Snate@binkert.orgDefaultDecode<Impl>::setActiveThreads(std::list<ThreadID> *at_ptr)
1582292SN/A{
1592292SN/A    activeThreads = at_ptr;
1602292SN/A}
1612292SN/A
1622307SN/Atemplate <class Impl>
1632863Sktlim@umich.edubool
1642843Sktlim@umich.eduDefaultDecode<Impl>::drain()
1652307SN/A{
1662843Sktlim@umich.edu    // Decode is done draining at any time.
1672843Sktlim@umich.edu    cpu->signalDrained();
1682863Sktlim@umich.edu    return true;
1692307SN/A}
1702307SN/A
1712307SN/Atemplate <class Impl>
1722307SN/Avoid
1732307SN/ADefaultDecode<Impl>::takeOverFrom()
1742307SN/A{
1752307SN/A    _status = Inactive;
1762307SN/A
1772348SN/A    // Be sure to reset state and clear out any old instructions.
1786221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
1796221Snate@binkert.org        decodeStatus[tid] = Idle;
1802307SN/A
1816221Snate@binkert.org        stalls[tid].rename = false;
1826221Snate@binkert.org        stalls[tid].iew = false;
1836221Snate@binkert.org        stalls[tid].commit = false;
1846221Snate@binkert.org        while (!insts[tid].empty())
1856221Snate@binkert.org            insts[tid].pop();
1866221Snate@binkert.org        while (!skidBuffer[tid].empty())
1876221Snate@binkert.org            skidBuffer[tid].pop();
1886221Snate@binkert.org        branchCount[tid] = 0;
1892307SN/A    }
1902307SN/A    wroteToTimeBuffer = false;
1912307SN/A}
1922307SN/A
1932292SN/Atemplate<class Impl>
1942292SN/Abool
1956221Snate@binkert.orgDefaultDecode<Impl>::checkStall(ThreadID tid) const
1962292SN/A{
1972292SN/A    bool ret_val = false;
1982292SN/A
1992292SN/A    if (stalls[tid].rename) {
2002292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
2012292SN/A        ret_val = true;
2022292SN/A    } else if (stalls[tid].iew) {
2032292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
2042292SN/A        ret_val = true;
2052292SN/A    } else if (stalls[tid].commit) {
2062292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
2072292SN/A        ret_val = true;
2082292SN/A    }
2092292SN/A
2102292SN/A    return ret_val;
2112292SN/A}
2122292SN/A
2132292SN/Atemplate<class Impl>
2141681SN/Ainline bool
2152292SN/ADefaultDecode<Impl>::fetchInstsValid()
2161681SN/A{
2171681SN/A    return fromFetch->size > 0;
2181681SN/A}
2191681SN/A
2201681SN/Atemplate<class Impl>
2212292SN/Abool
2226221Snate@binkert.orgDefaultDecode<Impl>::block(ThreadID tid)
2231060SN/A{
2242292SN/A    DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
2251060SN/A
2261060SN/A    // Add the current inputs to the skid buffer so they can be
2271060SN/A    // reprocessed when this stage unblocks.
2282292SN/A    skidInsert(tid);
2291060SN/A
2302348SN/A    // If the decode status is blocked or unblocking then decode has not yet
2312348SN/A    // signalled fetch to unblock. In that case, there is no need to tell
2322348SN/A    // fetch to block.
2332292SN/A    if (decodeStatus[tid] != Blocked) {
2342292SN/A        // Set the status to Blocked.
2352292SN/A        decodeStatus[tid] = Blocked;
2362348SN/A
2372348SN/A        if (decodeStatus[tid] != Unblocking) {
2382348SN/A            toFetch->decodeBlock[tid] = true;
2392348SN/A            wroteToTimeBuffer = true;
2402348SN/A        }
2412348SN/A
2422292SN/A        return true;
2432292SN/A    }
2442292SN/A
2452292SN/A    return false;
2461060SN/A}
2471060SN/A
2481060SN/Atemplate<class Impl>
2492292SN/Abool
2506221Snate@binkert.orgDefaultDecode<Impl>::unblock(ThreadID tid)
2511060SN/A{
2522292SN/A    // Decode is done unblocking only if the skid buffer is empty.
2532292SN/A    if (skidBuffer[tid].empty()) {
2542292SN/A        DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
2552292SN/A        toFetch->decodeUnblock[tid] = true;
2562292SN/A        wroteToTimeBuffer = true;
2571060SN/A
2582292SN/A        decodeStatus[tid] = Running;
2592292SN/A        return true;
2601060SN/A    }
2611681SN/A
2622329SN/A    DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
2632329SN/A
2642292SN/A    return false;
2651060SN/A}
2661060SN/A
2671060SN/Atemplate<class Impl>
2681060SN/Avoid
2696221Snate@binkert.orgDefaultDecode<Impl>::squash(DynInstPtr &inst, ThreadID tid)
2701060SN/A{
2717720Sgblack@eecs.umich.edu    DPRINTF(Decode, "[tid:%i]: [sn:%i] Squashing due to incorrect branch "
2727720Sgblack@eecs.umich.edu            "prediction detected at decode.\n", tid, inst->seqNum);
2732292SN/A
2742348SN/A    // Send back mispredict information.
2752292SN/A    toFetch->decodeInfo[tid].branchMispredict = true;
2762935Sksewell@umich.edu    toFetch->decodeInfo[tid].predIncorrect = true;
2776036Sksewell@umich.edu    toFetch->decodeInfo[tid].squash = true;
2782292SN/A    toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
2796036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
2807720Sgblack@eecs.umich.edu    toFetch->decodeInfo[tid].branchTaken = inst->pcState().branching();
2816036Sksewell@umich.edu
2823093Sksewell@umich.edu    InstSeqNum squash_seq_num = inst->seqNum;
2832935Sksewell@umich.edu
2842348SN/A    // Might have to tell fetch to unblock.
2852292SN/A    if (decodeStatus[tid] == Blocked ||
2862292SN/A        decodeStatus[tid] == Unblocking) {
2872292SN/A        toFetch->decodeUnblock[tid] = 1;
2882292SN/A    }
2892292SN/A
2901060SN/A    // Set status to squashing.
2912292SN/A    decodeStatus[tid] = Squashing;
2921060SN/A
2932292SN/A    for (int i=0; i<fromFetch->size; i++) {
2942292SN/A        if (fromFetch->insts[i]->threadNumber == tid &&
2952935Sksewell@umich.edu            fromFetch->insts[i]->seqNum > squash_seq_num) {
2962731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
2972292SN/A        }
2982292SN/A    }
2992292SN/A
3002348SN/A    // Clear the instruction list and skid buffer in case they have any
3012348SN/A    // insts in them.
3022292SN/A    while (!insts[tid].empty()) {
3032292SN/A        insts[tid].pop();
3042292SN/A    }
3051060SN/A
3062292SN/A    while (!skidBuffer[tid].empty()) {
3072292SN/A        skidBuffer[tid].pop();
3082292SN/A    }
3092292SN/A
3102292SN/A    // Squash instructions up until this one
3112935Sksewell@umich.edu    cpu->removeInstsUntil(squash_seq_num, tid);
3122292SN/A}
3132292SN/A
3142292SN/Atemplate<class Impl>
3152292SN/Aunsigned
3166221Snate@binkert.orgDefaultDecode<Impl>::squash(ThreadID tid)
3172292SN/A{
3182292SN/A    DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
3192292SN/A
3202292SN/A    if (decodeStatus[tid] == Blocked ||
3212292SN/A        decodeStatus[tid] == Unblocking) {
3222292SN/A#if !FULL_SYSTEM
3232292SN/A        // In syscall emulation, we can have both a block and a squash due
3242292SN/A        // to a syscall in the same cycle.  This would cause both signals to
3252292SN/A        // be high.  This shouldn't happen in full system.
3262329SN/A        // @todo: Determine if this still happens.
3272292SN/A        if (toFetch->decodeBlock[tid]) {
3282292SN/A            toFetch->decodeBlock[tid] = 0;
3292292SN/A        } else {
3302292SN/A            toFetch->decodeUnblock[tid] = 1;
3312292SN/A        }
3322292SN/A#else
3332292SN/A        toFetch->decodeUnblock[tid] = 1;
3342292SN/A#endif
3352292SN/A    }
3362292SN/A
3372292SN/A    // Set status to squashing.
3382292SN/A    decodeStatus[tid] = Squashing;
3392292SN/A
3402292SN/A    // Go through incoming instructions from fetch and squash them.
3412292SN/A    unsigned squash_count = 0;
3422292SN/A
3432292SN/A    for (int i=0; i<fromFetch->size; i++) {
3442292SN/A        if (fromFetch->insts[i]->threadNumber == tid) {
3452731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3462292SN/A            squash_count++;
3472292SN/A        }
3482292SN/A    }
3492292SN/A
3502348SN/A    // Clear the instruction list and skid buffer in case they have any
3512348SN/A    // insts in them.
3522292SN/A    while (!insts[tid].empty()) {
3532292SN/A        insts[tid].pop();
3542292SN/A    }
3552292SN/A
3562292SN/A    while (!skidBuffer[tid].empty()) {
3572292SN/A        skidBuffer[tid].pop();
3582292SN/A    }
3592292SN/A
3602292SN/A    return squash_count;
3612292SN/A}
3622292SN/A
3632292SN/Atemplate<class Impl>
3642292SN/Avoid
3656221Snate@binkert.orgDefaultDecode<Impl>::skidInsert(ThreadID tid)
3662292SN/A{
3672292SN/A    DynInstPtr inst = NULL;
3682292SN/A
3692292SN/A    while (!insts[tid].empty()) {
3702292SN/A        inst = insts[tid].front();
3712292SN/A
3722292SN/A        insts[tid].pop();
3732292SN/A
3742292SN/A        assert(tid == inst->threadNumber);
3752292SN/A
3767720Sgblack@eecs.umich.edu        DPRINTF(Decode,"Inserting [sn:%lli] PC: %s into decode skidBuffer %i\n",
3777720Sgblack@eecs.umich.edu                inst->seqNum, inst->pcState(), inst->threadNumber);
3782292SN/A
3792292SN/A        skidBuffer[tid].push(inst);
3802292SN/A    }
3812292SN/A
3822329SN/A    // @todo: Eventually need to enforce this by not letting a thread
3832292SN/A    // fetch past its skidbuffer
3842292SN/A    assert(skidBuffer[tid].size() <= skidBufferMax);
3852292SN/A}
3862292SN/A
3872292SN/Atemplate<class Impl>
3882292SN/Abool
3892292SN/ADefaultDecode<Impl>::skidsEmpty()
3902292SN/A{
3916221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
3926221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
3932292SN/A
3943867Sbinkertn@umich.edu    while (threads != end) {
3956221Snate@binkert.org        ThreadID tid = *threads++;
3963867Sbinkertn@umich.edu        if (!skidBuffer[tid].empty())
3972292SN/A            return false;
3982292SN/A    }
3992292SN/A
4002292SN/A    return true;
4012292SN/A}
4022292SN/A
4032292SN/Atemplate<class Impl>
4042292SN/Avoid
4052292SN/ADefaultDecode<Impl>::updateStatus()
4062292SN/A{
4072292SN/A    bool any_unblocking = false;
4082292SN/A
4096221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4106221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4112292SN/A
4123867Sbinkertn@umich.edu    while (threads != end) {
4136221Snate@binkert.org        ThreadID tid = *threads++;
4142292SN/A
4152292SN/A        if (decodeStatus[tid] == Unblocking) {
4162292SN/A            any_unblocking = true;
4172292SN/A            break;
4182292SN/A        }
4192292SN/A    }
4202292SN/A
4212292SN/A    // Decode will have activity if it's unblocking.
4222292SN/A    if (any_unblocking) {
4232292SN/A        if (_status == Inactive) {
4242292SN/A            _status = Active;
4252292SN/A
4262292SN/A            DPRINTF(Activity, "Activating stage.\n");
4272292SN/A
4282733Sktlim@umich.edu            cpu->activateStage(O3CPU::DecodeIdx);
4292292SN/A        }
4302292SN/A    } else {
4312292SN/A        // If it's not unblocking, then decode will not have any internal
4322292SN/A        // activity.  Switch it to inactive.
4332292SN/A        if (_status == Active) {
4342292SN/A            _status = Inactive;
4352292SN/A            DPRINTF(Activity, "Deactivating stage.\n");
4362292SN/A
4372733Sktlim@umich.edu            cpu->deactivateStage(O3CPU::DecodeIdx);
4382292SN/A        }
4392292SN/A    }
4402292SN/A}
4412292SN/A
4422292SN/Atemplate <class Impl>
4432292SN/Avoid
4442292SN/ADefaultDecode<Impl>::sortInsts()
4452292SN/A{
4462292SN/A    int insts_from_fetch = fromFetch->size;
4472329SN/A#ifdef DEBUG
4486221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++)
4496221Snate@binkert.org        assert(insts[tid].empty());
4502329SN/A#endif
4512292SN/A    for (int i = 0; i < insts_from_fetch; ++i) {
4522292SN/A        insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
4531060SN/A    }
4541060SN/A}
4551060SN/A
4561060SN/Atemplate<class Impl>
4571060SN/Avoid
4586221Snate@binkert.orgDefaultDecode<Impl>::readStallSignals(ThreadID tid)
4591060SN/A{
4602292SN/A    if (fromRename->renameBlock[tid]) {
4612292SN/A        stalls[tid].rename = true;
4622292SN/A    }
4631060SN/A
4642292SN/A    if (fromRename->renameUnblock[tid]) {
4652292SN/A        assert(stalls[tid].rename);
4662292SN/A        stalls[tid].rename = false;
4672292SN/A    }
4681060SN/A
4692292SN/A    if (fromIEW->iewBlock[tid]) {
4702292SN/A        stalls[tid].iew = true;
4712292SN/A    }
4721062SN/A
4732292SN/A    if (fromIEW->iewUnblock[tid]) {
4742292SN/A        assert(stalls[tid].iew);
4752292SN/A        stalls[tid].iew = false;
4762292SN/A    }
4771061SN/A
4782292SN/A    if (fromCommit->commitBlock[tid]) {
4792292SN/A        stalls[tid].commit = true;
4802292SN/A    }
4811062SN/A
4822292SN/A    if (fromCommit->commitUnblock[tid]) {
4832292SN/A        assert(stalls[tid].commit);
4842292SN/A        stalls[tid].commit = false;
4852292SN/A    }
4862292SN/A}
4871060SN/A
4882292SN/Atemplate <class Impl>
4892292SN/Abool
4906221Snate@binkert.orgDefaultDecode<Impl>::checkSignalsAndUpdate(ThreadID tid)
4912292SN/A{
4922292SN/A    // Check if there's a squash signal, squash if there is.
4932292SN/A    // Check stall signals, block if necessary.
4942292SN/A    // If status was blocked
4952292SN/A    //     Check if stall conditions have passed
4962292SN/A    //         if so then go to unblocking
4972292SN/A    // If status was Squashing
4982292SN/A    //     check if squashing is not high.  Switch to running this cycle.
4991060SN/A
5002292SN/A    // Update the per thread stall statuses.
5012292SN/A    readStallSignals(tid);
5021060SN/A
5032292SN/A    // Check squash signals from commit.
5042292SN/A    if (fromCommit->commitInfo[tid].squash) {
5051681SN/A
5062292SN/A        DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
5072292SN/A                "from commit.\n", tid);
5082292SN/A
5092292SN/A        squash(tid);
5102292SN/A
5112292SN/A        return true;
5122292SN/A    }
5132292SN/A
5142292SN/A    // Check ROB squash signals from commit.
5152292SN/A    if (fromCommit->commitInfo[tid].robSquashing) {
5162703Sktlim@umich.edu        DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
5172292SN/A
5182292SN/A        // Continue to squash.
5192292SN/A        decodeStatus[tid] = Squashing;
5202292SN/A
5212292SN/A        return true;
5222292SN/A    }
5232292SN/A
5242292SN/A    if (checkStall(tid)) {
5252292SN/A        return block(tid);
5262292SN/A    }
5272292SN/A
5282292SN/A    if (decodeStatus[tid] == Blocked) {
5292292SN/A        DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
5302292SN/A                tid);
5312292SN/A
5322292SN/A        decodeStatus[tid] = Unblocking;
5332292SN/A
5342292SN/A        unblock(tid);
5352292SN/A
5362292SN/A        return true;
5372292SN/A    }
5382292SN/A
5392292SN/A    if (decodeStatus[tid] == Squashing) {
5402292SN/A        // Switch status to running if decode isn't being told to block or
5412292SN/A        // squash this cycle.
5422292SN/A        DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
5432292SN/A                tid);
5442292SN/A
5452292SN/A        decodeStatus[tid] = Running;
5462292SN/A
5472292SN/A        return false;
5482292SN/A    }
5492292SN/A
5502292SN/A    // If we've reached this point, we have not gotten any signals that
5512292SN/A    // cause decode to change its status.  Decode remains the same as before.
5522292SN/A    return false;
5532292SN/A}
5542292SN/A
5552292SN/Atemplate<class Impl>
5562292SN/Avoid
5572292SN/ADefaultDecode<Impl>::tick()
5582292SN/A{
5592292SN/A    wroteToTimeBuffer = false;
5602292SN/A
5612292SN/A    bool status_change = false;
5622292SN/A
5632292SN/A    toRenameIndex = 0;
5642292SN/A
5656221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
5666221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
5672292SN/A
5682292SN/A    sortInsts();
5692292SN/A
5702292SN/A    //Check stall and squash signals.
5713867Sbinkertn@umich.edu    while (threads != end) {
5726221Snate@binkert.org        ThreadID tid = *threads++;
5732292SN/A
5742292SN/A        DPRINTF(Decode,"Processing [tid:%i]\n",tid);
5752292SN/A        status_change =  checkSignalsAndUpdate(tid) || status_change;
5762292SN/A
5772292SN/A        decode(status_change, tid);
5782292SN/A    }
5792292SN/A
5802292SN/A    if (status_change) {
5812292SN/A        updateStatus();
5822292SN/A    }
5832292SN/A
5842292SN/A    if (wroteToTimeBuffer) {
5852292SN/A        DPRINTF(Activity, "Activity this cycle.\n");
5862292SN/A
5872292SN/A        cpu->activityThisCycle();
5881060SN/A    }
5891060SN/A}
5901060SN/A
5911060SN/Atemplate<class Impl>
5921060SN/Avoid
5936221Snate@binkert.orgDefaultDecode<Impl>::decode(bool &status_change, ThreadID tid)
5941060SN/A{
5952292SN/A    // If status is Running or idle,
5962292SN/A    //     call decodeInsts()
5972292SN/A    // If status is Unblocking,
5982292SN/A    //     buffer any instructions coming from fetch
5992292SN/A    //     continue trying to empty skid buffer
6002292SN/A    //     check if stall conditions have passed
6012292SN/A
6022292SN/A    if (decodeStatus[tid] == Blocked) {
6032292SN/A        ++decodeBlockedCycles;
6042292SN/A    } else if (decodeStatus[tid] == Squashing) {
6052292SN/A        ++decodeSquashCycles;
6061060SN/A    }
6071060SN/A
6082292SN/A    // Decode should try to decode as many instructions as its bandwidth
6092292SN/A    // will allow, as long as it is not currently blocked.
6102292SN/A    if (decodeStatus[tid] == Running ||
6112292SN/A        decodeStatus[tid] == Idle) {
6122935Sksewell@umich.edu        DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
6132292SN/A                "stage.\n",tid);
6142292SN/A
6152292SN/A        decodeInsts(tid);
6162292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6172292SN/A        // Make sure that the skid buffer has something in it if the
6182292SN/A        // status is unblocking.
6192292SN/A        assert(!skidsEmpty());
6202292SN/A
6212292SN/A        // If the status was unblocking, then instructions from the skid
6222292SN/A        // buffer were used.  Remove those instructions and handle
6232292SN/A        // the rest of unblocking.
6242292SN/A        decodeInsts(tid);
6252292SN/A
6262292SN/A        if (fetchInstsValid()) {
6272292SN/A            // Add the current inputs to the skid buffer so they can be
6282292SN/A            // reprocessed when this stage unblocks.
6292292SN/A            skidInsert(tid);
6302292SN/A        }
6312292SN/A
6322292SN/A        status_change = unblock(tid) || status_change;
6331060SN/A    }
6342292SN/A}
6351060SN/A
6362292SN/Atemplate <class Impl>
6372292SN/Avoid
6386221Snate@binkert.orgDefaultDecode<Impl>::decodeInsts(ThreadID tid)
6392292SN/A{
6402292SN/A    // Instructions can come either from the skid buffer or the list of
6412292SN/A    // instructions coming from fetch, depending on decode's status.
6422292SN/A    int insts_available = decodeStatus[tid] == Unblocking ?
6432292SN/A        skidBuffer[tid].size() : insts[tid].size();
6442292SN/A
6452292SN/A    if (insts_available == 0) {
6462292SN/A        DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
6472292SN/A                " early.\n",tid);
6481060SN/A        // Should I change the status to idle?
6491062SN/A        ++decodeIdleCycles;
6501060SN/A        return;
6512292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6522292SN/A        DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
6532292SN/A                "buffer.\n",tid);
6542292SN/A        ++decodeUnblockCycles;
6552292SN/A    } else if (decodeStatus[tid] == Running) {
6562292SN/A        ++decodeRunCycles;
6571060SN/A    }
6581060SN/A
6591061SN/A    DynInstPtr inst;
6601061SN/A
6612292SN/A    std::queue<DynInstPtr>
6622292SN/A        &insts_to_decode = decodeStatus[tid] == Unblocking ?
6632292SN/A        skidBuffer[tid] : insts[tid];
6641061SN/A
6652292SN/A    DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
6661060SN/A
6672292SN/A    while (insts_available > 0 && toRenameIndex < decodeWidth) {
6682292SN/A        assert(!insts_to_decode.empty());
6691060SN/A
6702292SN/A        inst = insts_to_decode.front();
6711062SN/A
6722292SN/A        insts_to_decode.pop();
6731061SN/A
6742292SN/A        DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
6757720Sgblack@eecs.umich.edu                "PC %s\n", tid, inst->seqNum, inst->pcState());
6761061SN/A
6771061SN/A        if (inst->isSquashed()) {
6787720Sgblack@eecs.umich.edu            DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %s is "
6791061SN/A                    "squashed, skipping.\n",
6807720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6811061SN/A
6821062SN/A            ++decodeSquashedInsts;
6831062SN/A
6841061SN/A            --insts_available;
6851061SN/A
6861061SN/A            continue;
6871061SN/A        }
6881060SN/A
6891681SN/A        // Also check if instructions have no source registers.  Mark
6901681SN/A        // them as ready to issue at any time.  Not sure if this check
6911681SN/A        // should exist here or at a later stage; however it doesn't matter
6921681SN/A        // too much for function correctness.
6931681SN/A        if (inst->numSrcRegs() == 0) {
6941681SN/A            inst->setCanIssue();
6951681SN/A        }
6961681SN/A
6971060SN/A        // This current instruction is valid, so add it into the decode
6981060SN/A        // queue.  The next instruction may not be valid, so check to
6991060SN/A        // see if branches were predicted correctly.
7002292SN/A        toRename->insts[toRenameIndex] = inst;
7011061SN/A
7021061SN/A        ++(toRename->size);
7032292SN/A        ++toRenameIndex;
7042292SN/A        ++decodeDecodedInsts;
7052292SN/A        --insts_available;
7061060SN/A
7071060SN/A        // Ensure that if it was predicted as a branch, it really is a
7081061SN/A        // branch.
7093796Sgblack@eecs.umich.edu        if (inst->readPredTaken() && !inst->isControl()) {
7101060SN/A            panic("Instruction predicted as a branch!");
7111060SN/A
7121062SN/A            ++decodeControlMispred;
7132292SN/A
7141060SN/A            // Might want to set some sort of boolean and just do
7151060SN/A            // a check at the end
7162292SN/A            squash(inst, inst->threadNumber);
7172292SN/A
7181060SN/A            break;
7191060SN/A        }
7201060SN/A
7211062SN/A        // Go ahead and compute any PC-relative branches.
7221063SN/A        if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
7232307SN/A            ++decodeBranchResolved;
7241062SN/A
7257720Sgblack@eecs.umich.edu            if (!(inst->branchTarget() == inst->readPredTarg())) {
7261062SN/A                ++decodeBranchMispred;
7272292SN/A
7281060SN/A                // Might want to set some sort of boolean and just do
7291060SN/A                // a check at the end
7302292SN/A                squash(inst, inst->threadNumber);
7317720Sgblack@eecs.umich.edu                TheISA::PCState target = inst->branchTarget();
7326036Sksewell@umich.edu
7337720Sgblack@eecs.umich.edu                DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %s\n",
7347720Sgblack@eecs.umich.edu                        inst->seqNum, target);
7356036Sksewell@umich.edu                //The micro pc after an instruction level branch should be 0
7367720Sgblack@eecs.umich.edu                inst->setPredTarg(target);
7372935Sksewell@umich.edu                break;
7382935Sksewell@umich.edu            }
7392935Sksewell@umich.edu        }
7401060SN/A    }
7411061SN/A
7422292SN/A    // If we didn't process all instructions, then we will need to block
7432292SN/A    // and put all those instructions into the skid buffer.
7442292SN/A    if (!insts_to_decode.empty()) {
7452292SN/A        block(tid);
7462292SN/A    }
7472292SN/A
7482292SN/A    // Record that decode has written to the time buffer for activity
7492292SN/A    // tracking.
7502292SN/A    if (toRenameIndex) {
7512292SN/A        wroteToTimeBuffer = true;
7522292SN/A    }
7531060SN/A}
754