decode_impl.hh revision 6221
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
311717SN/A#include "cpu/o3/decode.hh"
326221Snate@binkert.org#include "params/DerivO3CPU.hh"
331060SN/A
346221Snate@binkert.orgusing namespace std;
355529Snate@binkert.org
361060SN/Atemplate<class Impl>
375529Snate@binkert.orgDefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params)
384329Sktlim@umich.edu    : cpu(_cpu),
394329Sktlim@umich.edu      renameToDecodeDelay(params->renameToDecodeDelay),
402292SN/A      iewToDecodeDelay(params->iewToDecodeDelay),
412292SN/A      commitToDecodeDelay(params->commitToDecodeDelay),
422292SN/A      fetchToDecodeDelay(params->fetchToDecodeDelay),
432292SN/A      decodeWidth(params->decodeWidth),
445529Snate@binkert.org      numThreads(params->numThreads)
451060SN/A{
462292SN/A    _status = Inactive;
472292SN/A
482348SN/A    // Setup status, make sure stall signals are clear.
496221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
506221Snate@binkert.org        decodeStatus[tid] = Idle;
512292SN/A
526221Snate@binkert.org        stalls[tid].rename = false;
536221Snate@binkert.org        stalls[tid].iew = false;
546221Snate@binkert.org        stalls[tid].commit = false;
552292SN/A    }
562292SN/A
572292SN/A    // @todo: Make into a parameter
582292SN/A    skidBufferMax = (fetchToDecodeDelay * params->fetchWidth) + decodeWidth;
592292SN/A}
602292SN/A
612292SN/Atemplate <class Impl>
622292SN/Astd::string
632292SN/ADefaultDecode<Impl>::name() const
642292SN/A{
652292SN/A    return cpu->name() + ".decode";
661060SN/A}
671060SN/A
681062SN/Atemplate <class Impl>
691062SN/Avoid
702292SN/ADefaultDecode<Impl>::regStats()
711062SN/A{
721062SN/A    decodeIdleCycles
732307SN/A        .name(name() + ".DECODE:IdleCycles")
741062SN/A        .desc("Number of cycles decode is idle")
751062SN/A        .prereq(decodeIdleCycles);
761062SN/A    decodeBlockedCycles
772307SN/A        .name(name() + ".DECODE:BlockedCycles")
781062SN/A        .desc("Number of cycles decode is blocked")
791062SN/A        .prereq(decodeBlockedCycles);
802292SN/A    decodeRunCycles
812307SN/A        .name(name() + ".DECODE:RunCycles")
822292SN/A        .desc("Number of cycles decode is running")
832292SN/A        .prereq(decodeRunCycles);
841062SN/A    decodeUnblockCycles
852307SN/A        .name(name() + ".DECODE:UnblockCycles")
861062SN/A        .desc("Number of cycles decode is unblocking")
871062SN/A        .prereq(decodeUnblockCycles);
881062SN/A    decodeSquashCycles
892307SN/A        .name(name() + ".DECODE:SquashCycles")
901062SN/A        .desc("Number of cycles decode is squashing")
911062SN/A        .prereq(decodeSquashCycles);
922307SN/A    decodeBranchResolved
932307SN/A        .name(name() + ".DECODE:BranchResolved")
942307SN/A        .desc("Number of times decode resolved a branch")
952307SN/A        .prereq(decodeBranchResolved);
961062SN/A    decodeBranchMispred
972307SN/A        .name(name() + ".DECODE:BranchMispred")
981062SN/A        .desc("Number of times decode detected a branch misprediction")
991062SN/A        .prereq(decodeBranchMispred);
1001062SN/A    decodeControlMispred
1012307SN/A        .name(name() + ".DECODE:ControlMispred")
1021062SN/A        .desc("Number of times decode detected an instruction incorrectly"
1031062SN/A              " predicted as a control")
1041062SN/A        .prereq(decodeControlMispred);
1051062SN/A    decodeDecodedInsts
1062307SN/A        .name(name() + ".DECODE:DecodedInsts")
1071062SN/A        .desc("Number of instructions handled by decode")
1081062SN/A        .prereq(decodeDecodedInsts);
1091062SN/A    decodeSquashedInsts
1102307SN/A        .name(name() + ".DECODE:SquashedInsts")
1111062SN/A        .desc("Number of squashed instructions handled by decode")
1121062SN/A        .prereq(decodeSquashedInsts);
1131062SN/A}
1141062SN/A
1151060SN/Atemplate<class Impl>
1161060SN/Avoid
1172292SN/ADefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
1181060SN/A{
1191060SN/A    timeBuffer = tb_ptr;
1201060SN/A
1211060SN/A    // Setup wire to write information back to fetch.
1221060SN/A    toFetch = timeBuffer->getWire(0);
1231060SN/A
1241060SN/A    // Create wires to get information from proper places in time buffer.
1251060SN/A    fromRename = timeBuffer->getWire(-renameToDecodeDelay);
1261060SN/A    fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
1271060SN/A    fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
1281060SN/A}
1291060SN/A
1301060SN/Atemplate<class Impl>
1311060SN/Avoid
1322292SN/ADefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
1331060SN/A{
1341060SN/A    decodeQueue = dq_ptr;
1351060SN/A
1361060SN/A    // Setup wire to write information to proper place in decode queue.
1371060SN/A    toRename = decodeQueue->getWire(0);
1381060SN/A}
1391060SN/A
1401060SN/Atemplate<class Impl>
1411060SN/Avoid
1422292SN/ADefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
1431060SN/A{
1441060SN/A    fetchQueue = fq_ptr;
1451060SN/A
1461060SN/A    // Setup wire to read information from fetch queue.
1471060SN/A    fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
1481060SN/A}
1491060SN/A
1501060SN/Atemplate<class Impl>
1512292SN/Avoid
1526221Snate@binkert.orgDefaultDecode<Impl>::setActiveThreads(std::list<ThreadID> *at_ptr)
1532292SN/A{
1542292SN/A    activeThreads = at_ptr;
1552292SN/A}
1562292SN/A
1572307SN/Atemplate <class Impl>
1582863Sktlim@umich.edubool
1592843Sktlim@umich.eduDefaultDecode<Impl>::drain()
1602307SN/A{
1612843Sktlim@umich.edu    // Decode is done draining at any time.
1622843Sktlim@umich.edu    cpu->signalDrained();
1632863Sktlim@umich.edu    return true;
1642307SN/A}
1652307SN/A
1662307SN/Atemplate <class Impl>
1672307SN/Avoid
1682307SN/ADefaultDecode<Impl>::takeOverFrom()
1692307SN/A{
1702307SN/A    _status = Inactive;
1712307SN/A
1722348SN/A    // Be sure to reset state and clear out any old instructions.
1736221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
1746221Snate@binkert.org        decodeStatus[tid] = Idle;
1752307SN/A
1766221Snate@binkert.org        stalls[tid].rename = false;
1776221Snate@binkert.org        stalls[tid].iew = false;
1786221Snate@binkert.org        stalls[tid].commit = false;
1796221Snate@binkert.org        while (!insts[tid].empty())
1806221Snate@binkert.org            insts[tid].pop();
1816221Snate@binkert.org        while (!skidBuffer[tid].empty())
1826221Snate@binkert.org            skidBuffer[tid].pop();
1836221Snate@binkert.org        branchCount[tid] = 0;
1842307SN/A    }
1852307SN/A    wroteToTimeBuffer = false;
1862307SN/A}
1872307SN/A
1882292SN/Atemplate<class Impl>
1892292SN/Abool
1906221Snate@binkert.orgDefaultDecode<Impl>::checkStall(ThreadID tid) const
1912292SN/A{
1922292SN/A    bool ret_val = false;
1932292SN/A
1942292SN/A    if (stalls[tid].rename) {
1952292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
1962292SN/A        ret_val = true;
1972292SN/A    } else if (stalls[tid].iew) {
1982292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
1992292SN/A        ret_val = true;
2002292SN/A    } else if (stalls[tid].commit) {
2012292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
2022292SN/A        ret_val = true;
2032292SN/A    }
2042292SN/A
2052292SN/A    return ret_val;
2062292SN/A}
2072292SN/A
2082292SN/Atemplate<class Impl>
2091681SN/Ainline bool
2102292SN/ADefaultDecode<Impl>::fetchInstsValid()
2111681SN/A{
2121681SN/A    return fromFetch->size > 0;
2131681SN/A}
2141681SN/A
2151681SN/Atemplate<class Impl>
2162292SN/Abool
2176221Snate@binkert.orgDefaultDecode<Impl>::block(ThreadID tid)
2181060SN/A{
2192292SN/A    DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
2201060SN/A
2211060SN/A    // Add the current inputs to the skid buffer so they can be
2221060SN/A    // reprocessed when this stage unblocks.
2232292SN/A    skidInsert(tid);
2241060SN/A
2252348SN/A    // If the decode status is blocked or unblocking then decode has not yet
2262348SN/A    // signalled fetch to unblock. In that case, there is no need to tell
2272348SN/A    // fetch to block.
2282292SN/A    if (decodeStatus[tid] != Blocked) {
2292292SN/A        // Set the status to Blocked.
2302292SN/A        decodeStatus[tid] = Blocked;
2312348SN/A
2322348SN/A        if (decodeStatus[tid] != Unblocking) {
2332348SN/A            toFetch->decodeBlock[tid] = true;
2342348SN/A            wroteToTimeBuffer = true;
2352348SN/A        }
2362348SN/A
2372292SN/A        return true;
2382292SN/A    }
2392292SN/A
2402292SN/A    return false;
2411060SN/A}
2421060SN/A
2431060SN/Atemplate<class Impl>
2442292SN/Abool
2456221Snate@binkert.orgDefaultDecode<Impl>::unblock(ThreadID tid)
2461060SN/A{
2472292SN/A    // Decode is done unblocking only if the skid buffer is empty.
2482292SN/A    if (skidBuffer[tid].empty()) {
2492292SN/A        DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
2502292SN/A        toFetch->decodeUnblock[tid] = true;
2512292SN/A        wroteToTimeBuffer = true;
2521060SN/A
2532292SN/A        decodeStatus[tid] = Running;
2542292SN/A        return true;
2551060SN/A    }
2561681SN/A
2572329SN/A    DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
2582329SN/A
2592292SN/A    return false;
2601060SN/A}
2611060SN/A
2621060SN/Atemplate<class Impl>
2631060SN/Avoid
2646221Snate@binkert.orgDefaultDecode<Impl>::squash(DynInstPtr &inst, ThreadID tid)
2651060SN/A{
2666036Sksewell@umich.edu    DPRINTF(Decode, "[tid:%i]: [sn:%i] Squashing due to incorrect branch prediction "
2676036Sksewell@umich.edu            "detected at decode.\n", tid, inst->seqNum);
2682292SN/A
2692348SN/A    // Send back mispredict information.
2702292SN/A    toFetch->decodeInfo[tid].branchMispredict = true;
2712935Sksewell@umich.edu    toFetch->decodeInfo[tid].predIncorrect = true;
2726036Sksewell@umich.edu    toFetch->decodeInfo[tid].squash = true;
2732292SN/A    toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
2744636Sgblack@eecs.umich.edu    toFetch->decodeInfo[tid].nextMicroPC = inst->readMicroPC();
2756036Sksewell@umich.edu
2763093Sksewell@umich.edu#if ISA_HAS_DELAY_SLOT
2776036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextPC = inst->readPC() + sizeof(TheISA::MachInst);
2786036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextNPC = inst->branchTarget();
2792935Sksewell@umich.edu    toFetch->decodeInfo[tid].branchTaken = inst->readNextNPC() !=
2802935Sksewell@umich.edu        (inst->readNextPC() + sizeof(TheISA::MachInst));
2813093Sksewell@umich.edu#else
2826036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
2836036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextNPC =
2846036Sksewell@umich.edu        inst->branchTarget() + sizeof(TheISA::MachInst);
2853093Sksewell@umich.edu    toFetch->decodeInfo[tid].branchTaken =
2863093Sksewell@umich.edu        inst->readNextPC() != (inst->readPC() + sizeof(TheISA::MachInst));
2874632Sgblack@eecs.umich.edu#endif
2883093Sksewell@umich.edu
2896036Sksewell@umich.edu
2903093Sksewell@umich.edu    InstSeqNum squash_seq_num = inst->seqNum;
2912935Sksewell@umich.edu
2922348SN/A    // Might have to tell fetch to unblock.
2932292SN/A    if (decodeStatus[tid] == Blocked ||
2942292SN/A        decodeStatus[tid] == Unblocking) {
2952292SN/A        toFetch->decodeUnblock[tid] = 1;
2962292SN/A    }
2972292SN/A
2981060SN/A    // Set status to squashing.
2992292SN/A    decodeStatus[tid] = Squashing;
3001060SN/A
3012292SN/A    for (int i=0; i<fromFetch->size; i++) {
3022292SN/A        if (fromFetch->insts[i]->threadNumber == tid &&
3032935Sksewell@umich.edu            fromFetch->insts[i]->seqNum > squash_seq_num) {
3042731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3052292SN/A        }
3062292SN/A    }
3072292SN/A
3082348SN/A    // Clear the instruction list and skid buffer in case they have any
3092348SN/A    // insts in them.
3102292SN/A    while (!insts[tid].empty()) {
3112292SN/A        insts[tid].pop();
3122292SN/A    }
3131060SN/A
3142292SN/A    while (!skidBuffer[tid].empty()) {
3152292SN/A        skidBuffer[tid].pop();
3162292SN/A    }
3172292SN/A
3182292SN/A    // Squash instructions up until this one
3192935Sksewell@umich.edu    cpu->removeInstsUntil(squash_seq_num, tid);
3202292SN/A}
3212292SN/A
3222292SN/Atemplate<class Impl>
3232292SN/Aunsigned
3246221Snate@binkert.orgDefaultDecode<Impl>::squash(ThreadID tid)
3252292SN/A{
3262292SN/A    DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
3272292SN/A
3282292SN/A    if (decodeStatus[tid] == Blocked ||
3292292SN/A        decodeStatus[tid] == Unblocking) {
3302292SN/A#if !FULL_SYSTEM
3312292SN/A        // In syscall emulation, we can have both a block and a squash due
3322292SN/A        // to a syscall in the same cycle.  This would cause both signals to
3332292SN/A        // be high.  This shouldn't happen in full system.
3342329SN/A        // @todo: Determine if this still happens.
3352292SN/A        if (toFetch->decodeBlock[tid]) {
3362292SN/A            toFetch->decodeBlock[tid] = 0;
3372292SN/A        } else {
3382292SN/A            toFetch->decodeUnblock[tid] = 1;
3392292SN/A        }
3402292SN/A#else
3412292SN/A        toFetch->decodeUnblock[tid] = 1;
3422292SN/A#endif
3432292SN/A    }
3442292SN/A
3452292SN/A    // Set status to squashing.
3462292SN/A    decodeStatus[tid] = Squashing;
3472292SN/A
3482292SN/A    // Go through incoming instructions from fetch and squash them.
3492292SN/A    unsigned squash_count = 0;
3502292SN/A
3512292SN/A    for (int i=0; i<fromFetch->size; i++) {
3522292SN/A        if (fromFetch->insts[i]->threadNumber == tid) {
3532731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3542292SN/A            squash_count++;
3552292SN/A        }
3562292SN/A    }
3572292SN/A
3582348SN/A    // Clear the instruction list and skid buffer in case they have any
3592348SN/A    // insts in them.
3602292SN/A    while (!insts[tid].empty()) {
3612292SN/A        insts[tid].pop();
3622292SN/A    }
3632292SN/A
3642292SN/A    while (!skidBuffer[tid].empty()) {
3652292SN/A        skidBuffer[tid].pop();
3662292SN/A    }
3672292SN/A
3682292SN/A    return squash_count;
3692292SN/A}
3702292SN/A
3712292SN/Atemplate<class Impl>
3722292SN/Avoid
3736221Snate@binkert.orgDefaultDecode<Impl>::skidInsert(ThreadID tid)
3742292SN/A{
3752292SN/A    DynInstPtr inst = NULL;
3762292SN/A
3772292SN/A    while (!insts[tid].empty()) {
3782292SN/A        inst = insts[tid].front();
3792292SN/A
3802292SN/A        insts[tid].pop();
3812292SN/A
3822292SN/A        assert(tid == inst->threadNumber);
3832292SN/A
3842292SN/A        DPRINTF(Decode,"Inserting [sn:%lli] PC:%#x into decode skidBuffer %i\n",
3852292SN/A                inst->seqNum, inst->readPC(), inst->threadNumber);
3862292SN/A
3872292SN/A        skidBuffer[tid].push(inst);
3882292SN/A    }
3892292SN/A
3902329SN/A    // @todo: Eventually need to enforce this by not letting a thread
3912292SN/A    // fetch past its skidbuffer
3922292SN/A    assert(skidBuffer[tid].size() <= skidBufferMax);
3932292SN/A}
3942292SN/A
3952292SN/Atemplate<class Impl>
3962292SN/Abool
3972292SN/ADefaultDecode<Impl>::skidsEmpty()
3982292SN/A{
3996221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4006221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4012292SN/A
4023867Sbinkertn@umich.edu    while (threads != end) {
4036221Snate@binkert.org        ThreadID tid = *threads++;
4043867Sbinkertn@umich.edu        if (!skidBuffer[tid].empty())
4052292SN/A            return false;
4062292SN/A    }
4072292SN/A
4082292SN/A    return true;
4092292SN/A}
4102292SN/A
4112292SN/Atemplate<class Impl>
4122292SN/Avoid
4132292SN/ADefaultDecode<Impl>::updateStatus()
4142292SN/A{
4152292SN/A    bool any_unblocking = false;
4162292SN/A
4176221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4186221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4192292SN/A
4203867Sbinkertn@umich.edu    while (threads != end) {
4216221Snate@binkert.org        ThreadID tid = *threads++;
4222292SN/A
4232292SN/A        if (decodeStatus[tid] == Unblocking) {
4242292SN/A            any_unblocking = true;
4252292SN/A            break;
4262292SN/A        }
4272292SN/A    }
4282292SN/A
4292292SN/A    // Decode will have activity if it's unblocking.
4302292SN/A    if (any_unblocking) {
4312292SN/A        if (_status == Inactive) {
4322292SN/A            _status = Active;
4332292SN/A
4342292SN/A            DPRINTF(Activity, "Activating stage.\n");
4352292SN/A
4362733Sktlim@umich.edu            cpu->activateStage(O3CPU::DecodeIdx);
4372292SN/A        }
4382292SN/A    } else {
4392292SN/A        // If it's not unblocking, then decode will not have any internal
4402292SN/A        // activity.  Switch it to inactive.
4412292SN/A        if (_status == Active) {
4422292SN/A            _status = Inactive;
4432292SN/A            DPRINTF(Activity, "Deactivating stage.\n");
4442292SN/A
4452733Sktlim@umich.edu            cpu->deactivateStage(O3CPU::DecodeIdx);
4462292SN/A        }
4472292SN/A    }
4482292SN/A}
4492292SN/A
4502292SN/Atemplate <class Impl>
4512292SN/Avoid
4522292SN/ADefaultDecode<Impl>::sortInsts()
4532292SN/A{
4542292SN/A    int insts_from_fetch = fromFetch->size;
4552329SN/A#ifdef DEBUG
4566221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++)
4576221Snate@binkert.org        assert(insts[tid].empty());
4582329SN/A#endif
4592292SN/A    for (int i = 0; i < insts_from_fetch; ++i) {
4602292SN/A        insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
4611060SN/A    }
4621060SN/A}
4631060SN/A
4641060SN/Atemplate<class Impl>
4651060SN/Avoid
4666221Snate@binkert.orgDefaultDecode<Impl>::readStallSignals(ThreadID tid)
4671060SN/A{
4682292SN/A    if (fromRename->renameBlock[tid]) {
4692292SN/A        stalls[tid].rename = true;
4702292SN/A    }
4711060SN/A
4722292SN/A    if (fromRename->renameUnblock[tid]) {
4732292SN/A        assert(stalls[tid].rename);
4742292SN/A        stalls[tid].rename = false;
4752292SN/A    }
4761060SN/A
4772292SN/A    if (fromIEW->iewBlock[tid]) {
4782292SN/A        stalls[tid].iew = true;
4792292SN/A    }
4801062SN/A
4812292SN/A    if (fromIEW->iewUnblock[tid]) {
4822292SN/A        assert(stalls[tid].iew);
4832292SN/A        stalls[tid].iew = false;
4842292SN/A    }
4851061SN/A
4862292SN/A    if (fromCommit->commitBlock[tid]) {
4872292SN/A        stalls[tid].commit = true;
4882292SN/A    }
4891062SN/A
4902292SN/A    if (fromCommit->commitUnblock[tid]) {
4912292SN/A        assert(stalls[tid].commit);
4922292SN/A        stalls[tid].commit = false;
4932292SN/A    }
4942292SN/A}
4951060SN/A
4962292SN/Atemplate <class Impl>
4972292SN/Abool
4986221Snate@binkert.orgDefaultDecode<Impl>::checkSignalsAndUpdate(ThreadID tid)
4992292SN/A{
5002292SN/A    // Check if there's a squash signal, squash if there is.
5012292SN/A    // Check stall signals, block if necessary.
5022292SN/A    // If status was blocked
5032292SN/A    //     Check if stall conditions have passed
5042292SN/A    //         if so then go to unblocking
5052292SN/A    // If status was Squashing
5062292SN/A    //     check if squashing is not high.  Switch to running this cycle.
5071060SN/A
5082292SN/A    // Update the per thread stall statuses.
5092292SN/A    readStallSignals(tid);
5101060SN/A
5112292SN/A    // Check squash signals from commit.
5122292SN/A    if (fromCommit->commitInfo[tid].squash) {
5131681SN/A
5142292SN/A        DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
5152292SN/A                "from commit.\n", tid);
5162292SN/A
5172292SN/A        squash(tid);
5182292SN/A
5192292SN/A        return true;
5202292SN/A    }
5212292SN/A
5222292SN/A    // Check ROB squash signals from commit.
5232292SN/A    if (fromCommit->commitInfo[tid].robSquashing) {
5242703Sktlim@umich.edu        DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
5252292SN/A
5262292SN/A        // Continue to squash.
5272292SN/A        decodeStatus[tid] = Squashing;
5282292SN/A
5292292SN/A        return true;
5302292SN/A    }
5312292SN/A
5322292SN/A    if (checkStall(tid)) {
5332292SN/A        return block(tid);
5342292SN/A    }
5352292SN/A
5362292SN/A    if (decodeStatus[tid] == Blocked) {
5372292SN/A        DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
5382292SN/A                tid);
5392292SN/A
5402292SN/A        decodeStatus[tid] = Unblocking;
5412292SN/A
5422292SN/A        unblock(tid);
5432292SN/A
5442292SN/A        return true;
5452292SN/A    }
5462292SN/A
5472292SN/A    if (decodeStatus[tid] == Squashing) {
5482292SN/A        // Switch status to running if decode isn't being told to block or
5492292SN/A        // squash this cycle.
5502292SN/A        DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
5512292SN/A                tid);
5522292SN/A
5532292SN/A        decodeStatus[tid] = Running;
5542292SN/A
5552292SN/A        return false;
5562292SN/A    }
5572292SN/A
5582292SN/A    // If we've reached this point, we have not gotten any signals that
5592292SN/A    // cause decode to change its status.  Decode remains the same as before.
5602292SN/A    return false;
5612292SN/A}
5622292SN/A
5632292SN/Atemplate<class Impl>
5642292SN/Avoid
5652292SN/ADefaultDecode<Impl>::tick()
5662292SN/A{
5672292SN/A    wroteToTimeBuffer = false;
5682292SN/A
5692292SN/A    bool status_change = false;
5702292SN/A
5712292SN/A    toRenameIndex = 0;
5722292SN/A
5736221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
5746221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
5752292SN/A
5762292SN/A    sortInsts();
5772292SN/A
5782292SN/A    //Check stall and squash signals.
5793867Sbinkertn@umich.edu    while (threads != end) {
5806221Snate@binkert.org        ThreadID tid = *threads++;
5812292SN/A
5822292SN/A        DPRINTF(Decode,"Processing [tid:%i]\n",tid);
5832292SN/A        status_change =  checkSignalsAndUpdate(tid) || status_change;
5842292SN/A
5852292SN/A        decode(status_change, tid);
5862292SN/A    }
5872292SN/A
5882292SN/A    if (status_change) {
5892292SN/A        updateStatus();
5902292SN/A    }
5912292SN/A
5922292SN/A    if (wroteToTimeBuffer) {
5932292SN/A        DPRINTF(Activity, "Activity this cycle.\n");
5942292SN/A
5952292SN/A        cpu->activityThisCycle();
5961060SN/A    }
5971060SN/A}
5981060SN/A
5991060SN/Atemplate<class Impl>
6001060SN/Avoid
6016221Snate@binkert.orgDefaultDecode<Impl>::decode(bool &status_change, ThreadID tid)
6021060SN/A{
6032292SN/A    // If status is Running or idle,
6042292SN/A    //     call decodeInsts()
6052292SN/A    // If status is Unblocking,
6062292SN/A    //     buffer any instructions coming from fetch
6072292SN/A    //     continue trying to empty skid buffer
6082292SN/A    //     check if stall conditions have passed
6092292SN/A
6102292SN/A    if (decodeStatus[tid] == Blocked) {
6112292SN/A        ++decodeBlockedCycles;
6122292SN/A    } else if (decodeStatus[tid] == Squashing) {
6132292SN/A        ++decodeSquashCycles;
6141060SN/A    }
6151060SN/A
6162292SN/A    // Decode should try to decode as many instructions as its bandwidth
6172292SN/A    // will allow, as long as it is not currently blocked.
6182292SN/A    if (decodeStatus[tid] == Running ||
6192292SN/A        decodeStatus[tid] == Idle) {
6202935Sksewell@umich.edu        DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
6212292SN/A                "stage.\n",tid);
6222292SN/A
6232292SN/A        decodeInsts(tid);
6242292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6252292SN/A        // Make sure that the skid buffer has something in it if the
6262292SN/A        // status is unblocking.
6272292SN/A        assert(!skidsEmpty());
6282292SN/A
6292292SN/A        // If the status was unblocking, then instructions from the skid
6302292SN/A        // buffer were used.  Remove those instructions and handle
6312292SN/A        // the rest of unblocking.
6322292SN/A        decodeInsts(tid);
6332292SN/A
6342292SN/A        if (fetchInstsValid()) {
6352292SN/A            // Add the current inputs to the skid buffer so they can be
6362292SN/A            // reprocessed when this stage unblocks.
6372292SN/A            skidInsert(tid);
6382292SN/A        }
6392292SN/A
6402292SN/A        status_change = unblock(tid) || status_change;
6411060SN/A    }
6422292SN/A}
6431060SN/A
6442292SN/Atemplate <class Impl>
6452292SN/Avoid
6466221Snate@binkert.orgDefaultDecode<Impl>::decodeInsts(ThreadID tid)
6472292SN/A{
6482292SN/A    // Instructions can come either from the skid buffer or the list of
6492292SN/A    // instructions coming from fetch, depending on decode's status.
6502292SN/A    int insts_available = decodeStatus[tid] == Unblocking ?
6512292SN/A        skidBuffer[tid].size() : insts[tid].size();
6522292SN/A
6532292SN/A    if (insts_available == 0) {
6542292SN/A        DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
6552292SN/A                " early.\n",tid);
6561060SN/A        // Should I change the status to idle?
6571062SN/A        ++decodeIdleCycles;
6581060SN/A        return;
6592292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6602292SN/A        DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
6612292SN/A                "buffer.\n",tid);
6622292SN/A        ++decodeUnblockCycles;
6632292SN/A    } else if (decodeStatus[tid] == Running) {
6642292SN/A        ++decodeRunCycles;
6651060SN/A    }
6661060SN/A
6671061SN/A    DynInstPtr inst;
6681061SN/A
6692292SN/A    std::queue<DynInstPtr>
6702292SN/A        &insts_to_decode = decodeStatus[tid] == Unblocking ?
6712292SN/A        skidBuffer[tid] : insts[tid];
6721061SN/A
6732292SN/A    DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
6741060SN/A
6752292SN/A    while (insts_available > 0 && toRenameIndex < decodeWidth) {
6762292SN/A        assert(!insts_to_decode.empty());
6771060SN/A
6782292SN/A        inst = insts_to_decode.front();
6791062SN/A
6802292SN/A        insts_to_decode.pop();
6811061SN/A
6822292SN/A        DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
6832292SN/A                "PC %#x\n",
6842292SN/A                tid, inst->seqNum, inst->readPC());
6851061SN/A
6861061SN/A        if (inst->isSquashed()) {
6872292SN/A            DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %#x is "
6881061SN/A                    "squashed, skipping.\n",
6892292SN/A                    tid, inst->seqNum, inst->readPC());
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
7161060SN/A        // Ensure that if it was predicted as a branch, it really is a
7171061SN/A        // branch.
7183796Sgblack@eecs.umich.edu        if (inst->readPredTaken() && !inst->isControl()) {
7193967Sgblack@eecs.umich.edu            DPRINTF(Decode, "PredPC : %#x != NextPC: %#x\n",
7203967Sgblack@eecs.umich.edu                    inst->readPredPC(), inst->readNextPC() + 4);
7212935Sksewell@umich.edu
7221060SN/A            panic("Instruction predicted as a branch!");
7231060SN/A
7241062SN/A            ++decodeControlMispred;
7252292SN/A
7261060SN/A            // Might want to set some sort of boolean and just do
7271060SN/A            // a check at the end
7282292SN/A            squash(inst, inst->threadNumber);
7292292SN/A
7301060SN/A            break;
7311060SN/A        }
7321060SN/A
7331062SN/A        // Go ahead and compute any PC-relative branches.
7341063SN/A        if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
7352307SN/A            ++decodeBranchResolved;
7361062SN/A
7373796Sgblack@eecs.umich.edu            if (inst->branchTarget() != inst->readPredPC()) {
7381062SN/A                ++decodeBranchMispred;
7392292SN/A
7401060SN/A                // Might want to set some sort of boolean and just do
7411060SN/A                // a check at the end
7422292SN/A                squash(inst, inst->threadNumber);
7433796Sgblack@eecs.umich.edu                Addr target = inst->branchTarget();
7446036Sksewell@umich.edu
7456036Sksewell@umich.edu#if ISA_HAS_DELAY_SLOT
7466036Sksewell@umich.edu                DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %#x  PredNextPC: %#x\n",
7476036Sksewell@umich.edu                        inst->seqNum, inst->readPC() + sizeof(TheISA::MachInst), target);
7486036Sksewell@umich.edu
7496036Sksewell@umich.edu                //The micro pc after an instruction level branch should be 0
7506036Sksewell@umich.edu                inst->setPredTarg(inst->readPC() + sizeof(TheISA::MachInst), target, 0);
7516036Sksewell@umich.edu#else
7526036Sksewell@umich.edu                DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %#x  PredNextPC: %#x\n",
7536036Sksewell@umich.edu                        inst->seqNum, target, target + sizeof(TheISA::MachInst));
7544636Sgblack@eecs.umich.edu                //The micro pc after an instruction level branch should be 0
7554636Sgblack@eecs.umich.edu                inst->setPredTarg(target, target + sizeof(TheISA::MachInst), 0);
7566036Sksewell@umich.edu#endif
7572935Sksewell@umich.edu                break;
7582935Sksewell@umich.edu            }
7592935Sksewell@umich.edu        }
7601060SN/A    }
7611061SN/A
7622292SN/A    // If we didn't process all instructions, then we will need to block
7632292SN/A    // and put all those instructions into the skid buffer.
7642292SN/A    if (!insts_to_decode.empty()) {
7652292SN/A        block(tid);
7662292SN/A    }
7672292SN/A
7682292SN/A    // Record that decode has written to the time buffer for activity
7692292SN/A    // tracking.
7702292SN/A    if (toRenameIndex) {
7712292SN/A        wroteToTimeBuffer = true;
7722292SN/A    }
7731060SN/A}
774