decode_impl.hh revision 8737
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"
378232Snate@binkert.org#include "debug/Activity.hh"
388232Snate@binkert.org#include "debug/Decode.hh"
396221Snate@binkert.org#include "params/DerivO3CPU.hh"
401060SN/A
418737Skoansin.tan@gmail.com// clang complains about std::set being overloaded with Packet::set if
428737Skoansin.tan@gmail.com// we open up the entire namespace std
438737Skoansin.tan@gmail.comusing std::list;
445529Snate@binkert.org
451060SN/Atemplate<class Impl>
465529Snate@binkert.orgDefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params)
474329Sktlim@umich.edu    : cpu(_cpu),
484329Sktlim@umich.edu      renameToDecodeDelay(params->renameToDecodeDelay),
492292SN/A      iewToDecodeDelay(params->iewToDecodeDelay),
502292SN/A      commitToDecodeDelay(params->commitToDecodeDelay),
512292SN/A      fetchToDecodeDelay(params->fetchToDecodeDelay),
522292SN/A      decodeWidth(params->decodeWidth),
535529Snate@binkert.org      numThreads(params->numThreads)
541060SN/A{
552292SN/A    _status = Inactive;
562292SN/A
572348SN/A    // Setup status, make sure stall signals are clear.
586221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
596221Snate@binkert.org        decodeStatus[tid] = Idle;
602292SN/A
616221Snate@binkert.org        stalls[tid].rename = false;
626221Snate@binkert.org        stalls[tid].iew = false;
636221Snate@binkert.org        stalls[tid].commit = false;
642292SN/A    }
652292SN/A
662292SN/A    // @todo: Make into a parameter
672292SN/A    skidBufferMax = (fetchToDecodeDelay * params->fetchWidth) + decodeWidth;
682292SN/A}
692292SN/A
702292SN/Atemplate <class Impl>
712292SN/Astd::string
722292SN/ADefaultDecode<Impl>::name() const
732292SN/A{
742292SN/A    return cpu->name() + ".decode";
751060SN/A}
761060SN/A
771062SN/Atemplate <class Impl>
781062SN/Avoid
792292SN/ADefaultDecode<Impl>::regStats()
801062SN/A{
811062SN/A    decodeIdleCycles
828240Snate@binkert.org        .name(name() + ".IdleCycles")
831062SN/A        .desc("Number of cycles decode is idle")
841062SN/A        .prereq(decodeIdleCycles);
851062SN/A    decodeBlockedCycles
868240Snate@binkert.org        .name(name() + ".BlockedCycles")
871062SN/A        .desc("Number of cycles decode is blocked")
881062SN/A        .prereq(decodeBlockedCycles);
892292SN/A    decodeRunCycles
908240Snate@binkert.org        .name(name() + ".RunCycles")
912292SN/A        .desc("Number of cycles decode is running")
922292SN/A        .prereq(decodeRunCycles);
931062SN/A    decodeUnblockCycles
948240Snate@binkert.org        .name(name() + ".UnblockCycles")
951062SN/A        .desc("Number of cycles decode is unblocking")
961062SN/A        .prereq(decodeUnblockCycles);
971062SN/A    decodeSquashCycles
988240Snate@binkert.org        .name(name() + ".SquashCycles")
991062SN/A        .desc("Number of cycles decode is squashing")
1001062SN/A        .prereq(decodeSquashCycles);
1012307SN/A    decodeBranchResolved
1028240Snate@binkert.org        .name(name() + ".BranchResolved")
1032307SN/A        .desc("Number of times decode resolved a branch")
1042307SN/A        .prereq(decodeBranchResolved);
1051062SN/A    decodeBranchMispred
1068240Snate@binkert.org        .name(name() + ".BranchMispred")
1071062SN/A        .desc("Number of times decode detected a branch misprediction")
1081062SN/A        .prereq(decodeBranchMispred);
1091062SN/A    decodeControlMispred
1108240Snate@binkert.org        .name(name() + ".ControlMispred")
1111062SN/A        .desc("Number of times decode detected an instruction incorrectly"
1121062SN/A              " predicted as a control")
1131062SN/A        .prereq(decodeControlMispred);
1141062SN/A    decodeDecodedInsts
1158240Snate@binkert.org        .name(name() + ".DecodedInsts")
1161062SN/A        .desc("Number of instructions handled by decode")
1171062SN/A        .prereq(decodeDecodedInsts);
1181062SN/A    decodeSquashedInsts
1198240Snate@binkert.org        .name(name() + ".SquashedInsts")
1201062SN/A        .desc("Number of squashed instructions handled by decode")
1211062SN/A        .prereq(decodeSquashedInsts);
1221062SN/A}
1231062SN/A
1241060SN/Atemplate<class Impl>
1251060SN/Avoid
1262292SN/ADefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
1271060SN/A{
1281060SN/A    timeBuffer = tb_ptr;
1291060SN/A
1301060SN/A    // Setup wire to write information back to fetch.
1311060SN/A    toFetch = timeBuffer->getWire(0);
1321060SN/A
1331060SN/A    // Create wires to get information from proper places in time buffer.
1341060SN/A    fromRename = timeBuffer->getWire(-renameToDecodeDelay);
1351060SN/A    fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
1361060SN/A    fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
1371060SN/A}
1381060SN/A
1391060SN/Atemplate<class Impl>
1401060SN/Avoid
1412292SN/ADefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
1421060SN/A{
1431060SN/A    decodeQueue = dq_ptr;
1441060SN/A
1451060SN/A    // Setup wire to write information to proper place in decode queue.
1461060SN/A    toRename = decodeQueue->getWire(0);
1471060SN/A}
1481060SN/A
1491060SN/Atemplate<class Impl>
1501060SN/Avoid
1512292SN/ADefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
1521060SN/A{
1531060SN/A    fetchQueue = fq_ptr;
1541060SN/A
1551060SN/A    // Setup wire to read information from fetch queue.
1561060SN/A    fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
1571060SN/A}
1581060SN/A
1591060SN/Atemplate<class Impl>
1602292SN/Avoid
1616221Snate@binkert.orgDefaultDecode<Impl>::setActiveThreads(std::list<ThreadID> *at_ptr)
1622292SN/A{
1632292SN/A    activeThreads = at_ptr;
1642292SN/A}
1652292SN/A
1662307SN/Atemplate <class Impl>
1672863Sktlim@umich.edubool
1682843Sktlim@umich.eduDefaultDecode<Impl>::drain()
1692307SN/A{
1702843Sktlim@umich.edu    // Decode is done draining at any time.
1712843Sktlim@umich.edu    cpu->signalDrained();
1722863Sktlim@umich.edu    return true;
1732307SN/A}
1742307SN/A
1752307SN/Atemplate <class Impl>
1762307SN/Avoid
1772307SN/ADefaultDecode<Impl>::takeOverFrom()
1782307SN/A{
1792307SN/A    _status = Inactive;
1802307SN/A
1812348SN/A    // Be sure to reset state and clear out any old instructions.
1826221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid) {
1836221Snate@binkert.org        decodeStatus[tid] = Idle;
1842307SN/A
1856221Snate@binkert.org        stalls[tid].rename = false;
1866221Snate@binkert.org        stalls[tid].iew = false;
1876221Snate@binkert.org        stalls[tid].commit = false;
1886221Snate@binkert.org        while (!insts[tid].empty())
1896221Snate@binkert.org            insts[tid].pop();
1906221Snate@binkert.org        while (!skidBuffer[tid].empty())
1916221Snate@binkert.org            skidBuffer[tid].pop();
1926221Snate@binkert.org        branchCount[tid] = 0;
1932307SN/A    }
1942307SN/A    wroteToTimeBuffer = false;
1952307SN/A}
1962307SN/A
1972292SN/Atemplate<class Impl>
1982292SN/Abool
1996221Snate@binkert.orgDefaultDecode<Impl>::checkStall(ThreadID tid) const
2002292SN/A{
2012292SN/A    bool ret_val = false;
2022292SN/A
2032292SN/A    if (stalls[tid].rename) {
2042292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
2052292SN/A        ret_val = true;
2062292SN/A    } else if (stalls[tid].iew) {
2072292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
2082292SN/A        ret_val = true;
2092292SN/A    } else if (stalls[tid].commit) {
2102292SN/A        DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
2112292SN/A        ret_val = true;
2122292SN/A    }
2132292SN/A
2142292SN/A    return ret_val;
2152292SN/A}
2162292SN/A
2172292SN/Atemplate<class Impl>
2181681SN/Ainline bool
2192292SN/ADefaultDecode<Impl>::fetchInstsValid()
2201681SN/A{
2211681SN/A    return fromFetch->size > 0;
2221681SN/A}
2231681SN/A
2241681SN/Atemplate<class Impl>
2252292SN/Abool
2266221Snate@binkert.orgDefaultDecode<Impl>::block(ThreadID tid)
2271060SN/A{
2282292SN/A    DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
2291060SN/A
2301060SN/A    // Add the current inputs to the skid buffer so they can be
2311060SN/A    // reprocessed when this stage unblocks.
2322292SN/A    skidInsert(tid);
2331060SN/A
2342348SN/A    // If the decode status is blocked or unblocking then decode has not yet
2352348SN/A    // signalled fetch to unblock. In that case, there is no need to tell
2362348SN/A    // fetch to block.
2372292SN/A    if (decodeStatus[tid] != Blocked) {
2382292SN/A        // Set the status to Blocked.
2392292SN/A        decodeStatus[tid] = Blocked;
2402348SN/A
2412348SN/A        if (decodeStatus[tid] != Unblocking) {
2422348SN/A            toFetch->decodeBlock[tid] = true;
2432348SN/A            wroteToTimeBuffer = true;
2442348SN/A        }
2452348SN/A
2462292SN/A        return true;
2472292SN/A    }
2482292SN/A
2492292SN/A    return false;
2501060SN/A}
2511060SN/A
2521060SN/Atemplate<class Impl>
2532292SN/Abool
2546221Snate@binkert.orgDefaultDecode<Impl>::unblock(ThreadID tid)
2551060SN/A{
2562292SN/A    // Decode is done unblocking only if the skid buffer is empty.
2572292SN/A    if (skidBuffer[tid].empty()) {
2582292SN/A        DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
2592292SN/A        toFetch->decodeUnblock[tid] = true;
2602292SN/A        wroteToTimeBuffer = true;
2611060SN/A
2622292SN/A        decodeStatus[tid] = Running;
2632292SN/A        return true;
2641060SN/A    }
2651681SN/A
2662329SN/A    DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
2672329SN/A
2682292SN/A    return false;
2691060SN/A}
2701060SN/A
2711060SN/Atemplate<class Impl>
2721060SN/Avoid
2736221Snate@binkert.orgDefaultDecode<Impl>::squash(DynInstPtr &inst, ThreadID tid)
2741060SN/A{
2757720Sgblack@eecs.umich.edu    DPRINTF(Decode, "[tid:%i]: [sn:%i] Squashing due to incorrect branch "
2767720Sgblack@eecs.umich.edu            "prediction detected at decode.\n", tid, inst->seqNum);
2772292SN/A
2782348SN/A    // Send back mispredict information.
2792292SN/A    toFetch->decodeInfo[tid].branchMispredict = true;
2802935Sksewell@umich.edu    toFetch->decodeInfo[tid].predIncorrect = true;
2816036Sksewell@umich.edu    toFetch->decodeInfo[tid].squash = true;
2822292SN/A    toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
2836036Sksewell@umich.edu    toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
2847720Sgblack@eecs.umich.edu    toFetch->decodeInfo[tid].branchTaken = inst->pcState().branching();
2858503Sgblack@eecs.umich.edu    toFetch->decodeInfo[tid].squashInst = inst;
2866036Sksewell@umich.edu
2873093Sksewell@umich.edu    InstSeqNum squash_seq_num = inst->seqNum;
2882935Sksewell@umich.edu
2892348SN/A    // Might have to tell fetch to unblock.
2902292SN/A    if (decodeStatus[tid] == Blocked ||
2912292SN/A        decodeStatus[tid] == Unblocking) {
2922292SN/A        toFetch->decodeUnblock[tid] = 1;
2932292SN/A    }
2942292SN/A
2951060SN/A    // Set status to squashing.
2962292SN/A    decodeStatus[tid] = Squashing;
2971060SN/A
2982292SN/A    for (int i=0; i<fromFetch->size; i++) {
2992292SN/A        if (fromFetch->insts[i]->threadNumber == tid &&
3002935Sksewell@umich.edu            fromFetch->insts[i]->seqNum > squash_seq_num) {
3012731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3022292SN/A        }
3032292SN/A    }
3042292SN/A
3052348SN/A    // Clear the instruction list and skid buffer in case they have any
3062348SN/A    // insts in them.
3072292SN/A    while (!insts[tid].empty()) {
3082292SN/A        insts[tid].pop();
3092292SN/A    }
3101060SN/A
3112292SN/A    while (!skidBuffer[tid].empty()) {
3122292SN/A        skidBuffer[tid].pop();
3132292SN/A    }
3142292SN/A
3152292SN/A    // Squash instructions up until this one
3162935Sksewell@umich.edu    cpu->removeInstsUntil(squash_seq_num, tid);
3172292SN/A}
3182292SN/A
3192292SN/Atemplate<class Impl>
3202292SN/Aunsigned
3216221Snate@binkert.orgDefaultDecode<Impl>::squash(ThreadID tid)
3222292SN/A{
3232292SN/A    DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
3242292SN/A
3252292SN/A    if (decodeStatus[tid] == Blocked ||
3262292SN/A        decodeStatus[tid] == Unblocking) {
3272292SN/A#if !FULL_SYSTEM
3282292SN/A        // In syscall emulation, we can have both a block and a squash due
3292292SN/A        // to a syscall in the same cycle.  This would cause both signals to
3302292SN/A        // be high.  This shouldn't happen in full system.
3312329SN/A        // @todo: Determine if this still happens.
3322292SN/A        if (toFetch->decodeBlock[tid]) {
3332292SN/A            toFetch->decodeBlock[tid] = 0;
3342292SN/A        } else {
3352292SN/A            toFetch->decodeUnblock[tid] = 1;
3362292SN/A        }
3372292SN/A#else
3382292SN/A        toFetch->decodeUnblock[tid] = 1;
3392292SN/A#endif
3402292SN/A    }
3412292SN/A
3422292SN/A    // Set status to squashing.
3432292SN/A    decodeStatus[tid] = Squashing;
3442292SN/A
3452292SN/A    // Go through incoming instructions from fetch and squash them.
3462292SN/A    unsigned squash_count = 0;
3472292SN/A
3482292SN/A    for (int i=0; i<fromFetch->size; i++) {
3492292SN/A        if (fromFetch->insts[i]->threadNumber == tid) {
3502731Sktlim@umich.edu            fromFetch->insts[i]->setSquashed();
3512292SN/A            squash_count++;
3522292SN/A        }
3532292SN/A    }
3542292SN/A
3552348SN/A    // Clear the instruction list and skid buffer in case they have any
3562348SN/A    // insts in them.
3572292SN/A    while (!insts[tid].empty()) {
3582292SN/A        insts[tid].pop();
3592292SN/A    }
3602292SN/A
3612292SN/A    while (!skidBuffer[tid].empty()) {
3622292SN/A        skidBuffer[tid].pop();
3632292SN/A    }
3642292SN/A
3652292SN/A    return squash_count;
3662292SN/A}
3672292SN/A
3682292SN/Atemplate<class Impl>
3692292SN/Avoid
3706221Snate@binkert.orgDefaultDecode<Impl>::skidInsert(ThreadID tid)
3712292SN/A{
3722292SN/A    DynInstPtr inst = NULL;
3732292SN/A
3742292SN/A    while (!insts[tid].empty()) {
3752292SN/A        inst = insts[tid].front();
3762292SN/A
3772292SN/A        insts[tid].pop();
3782292SN/A
3792292SN/A        assert(tid == inst->threadNumber);
3802292SN/A
3817720Sgblack@eecs.umich.edu        DPRINTF(Decode,"Inserting [sn:%lli] PC: %s into decode skidBuffer %i\n",
3827720Sgblack@eecs.umich.edu                inst->seqNum, inst->pcState(), inst->threadNumber);
3832292SN/A
3842292SN/A        skidBuffer[tid].push(inst);
3852292SN/A    }
3862292SN/A
3872329SN/A    // @todo: Eventually need to enforce this by not letting a thread
3882292SN/A    // fetch past its skidbuffer
3892292SN/A    assert(skidBuffer[tid].size() <= skidBufferMax);
3902292SN/A}
3912292SN/A
3922292SN/Atemplate<class Impl>
3932292SN/Abool
3942292SN/ADefaultDecode<Impl>::skidsEmpty()
3952292SN/A{
3966221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
3976221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
3982292SN/A
3993867Sbinkertn@umich.edu    while (threads != end) {
4006221Snate@binkert.org        ThreadID tid = *threads++;
4013867Sbinkertn@umich.edu        if (!skidBuffer[tid].empty())
4022292SN/A            return false;
4032292SN/A    }
4042292SN/A
4052292SN/A    return true;
4062292SN/A}
4072292SN/A
4082292SN/Atemplate<class Impl>
4092292SN/Avoid
4102292SN/ADefaultDecode<Impl>::updateStatus()
4112292SN/A{
4122292SN/A    bool any_unblocking = false;
4132292SN/A
4146221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4156221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4162292SN/A
4173867Sbinkertn@umich.edu    while (threads != end) {
4186221Snate@binkert.org        ThreadID tid = *threads++;
4192292SN/A
4202292SN/A        if (decodeStatus[tid] == Unblocking) {
4212292SN/A            any_unblocking = true;
4222292SN/A            break;
4232292SN/A        }
4242292SN/A    }
4252292SN/A
4262292SN/A    // Decode will have activity if it's unblocking.
4272292SN/A    if (any_unblocking) {
4282292SN/A        if (_status == Inactive) {
4292292SN/A            _status = Active;
4302292SN/A
4312292SN/A            DPRINTF(Activity, "Activating stage.\n");
4322292SN/A
4332733Sktlim@umich.edu            cpu->activateStage(O3CPU::DecodeIdx);
4342292SN/A        }
4352292SN/A    } else {
4362292SN/A        // If it's not unblocking, then decode will not have any internal
4372292SN/A        // activity.  Switch it to inactive.
4382292SN/A        if (_status == Active) {
4392292SN/A            _status = Inactive;
4402292SN/A            DPRINTF(Activity, "Deactivating stage.\n");
4412292SN/A
4422733Sktlim@umich.edu            cpu->deactivateStage(O3CPU::DecodeIdx);
4432292SN/A        }
4442292SN/A    }
4452292SN/A}
4462292SN/A
4472292SN/Atemplate <class Impl>
4482292SN/Avoid
4492292SN/ADefaultDecode<Impl>::sortInsts()
4502292SN/A{
4512292SN/A    int insts_from_fetch = fromFetch->size;
4522292SN/A    for (int i = 0; i < insts_from_fetch; ++i) {
4532292SN/A        insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
4541060SN/A    }
4551060SN/A}
4561060SN/A
4571060SN/Atemplate<class Impl>
4581060SN/Avoid
4596221Snate@binkert.orgDefaultDecode<Impl>::readStallSignals(ThreadID tid)
4601060SN/A{
4612292SN/A    if (fromRename->renameBlock[tid]) {
4622292SN/A        stalls[tid].rename = true;
4632292SN/A    }
4641060SN/A
4652292SN/A    if (fromRename->renameUnblock[tid]) {
4662292SN/A        assert(stalls[tid].rename);
4672292SN/A        stalls[tid].rename = false;
4682292SN/A    }
4691060SN/A
4702292SN/A    if (fromIEW->iewBlock[tid]) {
4712292SN/A        stalls[tid].iew = true;
4722292SN/A    }
4731062SN/A
4742292SN/A    if (fromIEW->iewUnblock[tid]) {
4752292SN/A        assert(stalls[tid].iew);
4762292SN/A        stalls[tid].iew = false;
4772292SN/A    }
4781061SN/A
4792292SN/A    if (fromCommit->commitBlock[tid]) {
4802292SN/A        stalls[tid].commit = true;
4812292SN/A    }
4821062SN/A
4832292SN/A    if (fromCommit->commitUnblock[tid]) {
4842292SN/A        assert(stalls[tid].commit);
4852292SN/A        stalls[tid].commit = false;
4862292SN/A    }
4872292SN/A}
4881060SN/A
4892292SN/Atemplate <class Impl>
4902292SN/Abool
4916221Snate@binkert.orgDefaultDecode<Impl>::checkSignalsAndUpdate(ThreadID tid)
4922292SN/A{
4932292SN/A    // Check if there's a squash signal, squash if there is.
4942292SN/A    // Check stall signals, block if necessary.
4952292SN/A    // If status was blocked
4962292SN/A    //     Check if stall conditions have passed
4972292SN/A    //         if so then go to unblocking
4982292SN/A    // If status was Squashing
4992292SN/A    //     check if squashing is not high.  Switch to running this cycle.
5001060SN/A
5012292SN/A    // Update the per thread stall statuses.
5022292SN/A    readStallSignals(tid);
5031060SN/A
5042292SN/A    // Check squash signals from commit.
5052292SN/A    if (fromCommit->commitInfo[tid].squash) {
5061681SN/A
5072292SN/A        DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
5082292SN/A                "from commit.\n", tid);
5092292SN/A
5102292SN/A        squash(tid);
5112292SN/A
5122292SN/A        return true;
5132292SN/A    }
5142292SN/A
5152292SN/A    // Check ROB squash signals from commit.
5162292SN/A    if (fromCommit->commitInfo[tid].robSquashing) {
5172703Sktlim@umich.edu        DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
5182292SN/A
5192292SN/A        // Continue to squash.
5202292SN/A        decodeStatus[tid] = Squashing;
5212292SN/A
5222292SN/A        return true;
5232292SN/A    }
5242292SN/A
5252292SN/A    if (checkStall(tid)) {
5262292SN/A        return block(tid);
5272292SN/A    }
5282292SN/A
5292292SN/A    if (decodeStatus[tid] == Blocked) {
5302292SN/A        DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
5312292SN/A                tid);
5322292SN/A
5332292SN/A        decodeStatus[tid] = Unblocking;
5342292SN/A
5352292SN/A        unblock(tid);
5362292SN/A
5372292SN/A        return true;
5382292SN/A    }
5392292SN/A
5402292SN/A    if (decodeStatus[tid] == Squashing) {
5412292SN/A        // Switch status to running if decode isn't being told to block or
5422292SN/A        // squash this cycle.
5432292SN/A        DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
5442292SN/A                tid);
5452292SN/A
5462292SN/A        decodeStatus[tid] = Running;
5472292SN/A
5482292SN/A        return false;
5492292SN/A    }
5502292SN/A
5512292SN/A    // If we've reached this point, we have not gotten any signals that
5522292SN/A    // cause decode to change its status.  Decode remains the same as before.
5532292SN/A    return false;
5542292SN/A}
5552292SN/A
5562292SN/Atemplate<class Impl>
5572292SN/Avoid
5582292SN/ADefaultDecode<Impl>::tick()
5592292SN/A{
5602292SN/A    wroteToTimeBuffer = false;
5612292SN/A
5622292SN/A    bool status_change = false;
5632292SN/A
5642292SN/A    toRenameIndex = 0;
5652292SN/A
5666221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
5676221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
5682292SN/A
5692292SN/A    sortInsts();
5702292SN/A
5712292SN/A    //Check stall and squash signals.
5723867Sbinkertn@umich.edu    while (threads != end) {
5736221Snate@binkert.org        ThreadID tid = *threads++;
5742292SN/A
5752292SN/A        DPRINTF(Decode,"Processing [tid:%i]\n",tid);
5762292SN/A        status_change =  checkSignalsAndUpdate(tid) || status_change;
5772292SN/A
5782292SN/A        decode(status_change, tid);
5792292SN/A    }
5802292SN/A
5812292SN/A    if (status_change) {
5822292SN/A        updateStatus();
5832292SN/A    }
5842292SN/A
5852292SN/A    if (wroteToTimeBuffer) {
5862292SN/A        DPRINTF(Activity, "Activity this cycle.\n");
5872292SN/A
5882292SN/A        cpu->activityThisCycle();
5891060SN/A    }
5901060SN/A}
5911060SN/A
5921060SN/Atemplate<class Impl>
5931060SN/Avoid
5946221Snate@binkert.orgDefaultDecode<Impl>::decode(bool &status_change, ThreadID tid)
5951060SN/A{
5962292SN/A    // If status is Running or idle,
5972292SN/A    //     call decodeInsts()
5982292SN/A    // If status is Unblocking,
5992292SN/A    //     buffer any instructions coming from fetch
6002292SN/A    //     continue trying to empty skid buffer
6012292SN/A    //     check if stall conditions have passed
6022292SN/A
6032292SN/A    if (decodeStatus[tid] == Blocked) {
6042292SN/A        ++decodeBlockedCycles;
6052292SN/A    } else if (decodeStatus[tid] == Squashing) {
6062292SN/A        ++decodeSquashCycles;
6071060SN/A    }
6081060SN/A
6092292SN/A    // Decode should try to decode as many instructions as its bandwidth
6102292SN/A    // will allow, as long as it is not currently blocked.
6112292SN/A    if (decodeStatus[tid] == Running ||
6122292SN/A        decodeStatus[tid] == Idle) {
6132935Sksewell@umich.edu        DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
6142292SN/A                "stage.\n",tid);
6152292SN/A
6162292SN/A        decodeInsts(tid);
6172292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6182292SN/A        // Make sure that the skid buffer has something in it if the
6192292SN/A        // status is unblocking.
6202292SN/A        assert(!skidsEmpty());
6212292SN/A
6222292SN/A        // If the status was unblocking, then instructions from the skid
6232292SN/A        // buffer were used.  Remove those instructions and handle
6242292SN/A        // the rest of unblocking.
6252292SN/A        decodeInsts(tid);
6262292SN/A
6272292SN/A        if (fetchInstsValid()) {
6282292SN/A            // Add the current inputs to the skid buffer so they can be
6292292SN/A            // reprocessed when this stage unblocks.
6302292SN/A            skidInsert(tid);
6312292SN/A        }
6322292SN/A
6332292SN/A        status_change = unblock(tid) || status_change;
6341060SN/A    }
6352292SN/A}
6361060SN/A
6372292SN/Atemplate <class Impl>
6382292SN/Avoid
6396221Snate@binkert.orgDefaultDecode<Impl>::decodeInsts(ThreadID tid)
6402292SN/A{
6412292SN/A    // Instructions can come either from the skid buffer or the list of
6422292SN/A    // instructions coming from fetch, depending on decode's status.
6432292SN/A    int insts_available = decodeStatus[tid] == Unblocking ?
6442292SN/A        skidBuffer[tid].size() : insts[tid].size();
6452292SN/A
6462292SN/A    if (insts_available == 0) {
6472292SN/A        DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
6482292SN/A                " early.\n",tid);
6491060SN/A        // Should I change the status to idle?
6501062SN/A        ++decodeIdleCycles;
6511060SN/A        return;
6522292SN/A    } else if (decodeStatus[tid] == Unblocking) {
6532292SN/A        DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
6542292SN/A                "buffer.\n",tid);
6552292SN/A        ++decodeUnblockCycles;
6562292SN/A    } else if (decodeStatus[tid] == Running) {
6572292SN/A        ++decodeRunCycles;
6581060SN/A    }
6591060SN/A
6601061SN/A    DynInstPtr inst;
6611061SN/A
6622292SN/A    std::queue<DynInstPtr>
6632292SN/A        &insts_to_decode = decodeStatus[tid] == Unblocking ?
6642292SN/A        skidBuffer[tid] : insts[tid];
6651061SN/A
6662292SN/A    DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
6671060SN/A
6682292SN/A    while (insts_available > 0 && toRenameIndex < decodeWidth) {
6692292SN/A        assert(!insts_to_decode.empty());
6701060SN/A
6712292SN/A        inst = insts_to_decode.front();
6721062SN/A
6732292SN/A        insts_to_decode.pop();
6741061SN/A
6752292SN/A        DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
6767720Sgblack@eecs.umich.edu                "PC %s\n", tid, inst->seqNum, inst->pcState());
6771061SN/A
6781061SN/A        if (inst->isSquashed()) {
6797720Sgblack@eecs.umich.edu            DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %s is "
6801061SN/A                    "squashed, skipping.\n",
6817720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6821061SN/A
6831062SN/A            ++decodeSquashedInsts;
6841062SN/A
6851061SN/A            --insts_available;
6861061SN/A
6871061SN/A            continue;
6881061SN/A        }
6891060SN/A
6901681SN/A        // Also check if instructions have no source registers.  Mark
6911681SN/A        // them as ready to issue at any time.  Not sure if this check
6921681SN/A        // should exist here or at a later stage; however it doesn't matter
6931681SN/A        // too much for function correctness.
6941681SN/A        if (inst->numSrcRegs() == 0) {
6951681SN/A            inst->setCanIssue();
6961681SN/A        }
6971681SN/A
6981060SN/A        // This current instruction is valid, so add it into the decode
6991060SN/A        // queue.  The next instruction may not be valid, so check to
7001060SN/A        // see if branches were predicted correctly.
7012292SN/A        toRename->insts[toRenameIndex] = inst;
7021061SN/A
7031061SN/A        ++(toRename->size);
7042292SN/A        ++toRenameIndex;
7052292SN/A        ++decodeDecodedInsts;
7062292SN/A        --insts_available;
7071060SN/A
7088471SGiacomo.Gabrielli@arm.com#if TRACING_ON
7098471SGiacomo.Gabrielli@arm.com        inst->decodeTick = curTick();
7108471SGiacomo.Gabrielli@arm.com#endif
7118471SGiacomo.Gabrielli@arm.com
7121060SN/A        // Ensure that if it was predicted as a branch, it really is a
7131061SN/A        // branch.
7143796Sgblack@eecs.umich.edu        if (inst->readPredTaken() && !inst->isControl()) {
7151060SN/A            panic("Instruction predicted as a branch!");
7161060SN/A
7171062SN/A            ++decodeControlMispred;
7182292SN/A
7191060SN/A            // Might want to set some sort of boolean and just do
7201060SN/A            // a check at the end
7212292SN/A            squash(inst, inst->threadNumber);
7222292SN/A
7231060SN/A            break;
7241060SN/A        }
7251060SN/A
7261062SN/A        // Go ahead and compute any PC-relative branches.
7271063SN/A        if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
7282307SN/A            ++decodeBranchResolved;
7291062SN/A
7307720Sgblack@eecs.umich.edu            if (!(inst->branchTarget() == inst->readPredTarg())) {
7311062SN/A                ++decodeBranchMispred;
7322292SN/A
7331060SN/A                // Might want to set some sort of boolean and just do
7341060SN/A                // a check at the end
7352292SN/A                squash(inst, inst->threadNumber);
7367720Sgblack@eecs.umich.edu                TheISA::PCState target = inst->branchTarget();
7376036Sksewell@umich.edu
7387720Sgblack@eecs.umich.edu                DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %s\n",
7397720Sgblack@eecs.umich.edu                        inst->seqNum, target);
7406036Sksewell@umich.edu                //The micro pc after an instruction level branch should be 0
7417720Sgblack@eecs.umich.edu                inst->setPredTarg(target);
7422935Sksewell@umich.edu                break;
7432935Sksewell@umich.edu            }
7442935Sksewell@umich.edu        }
7451060SN/A    }
7461061SN/A
7472292SN/A    // If we didn't process all instructions, then we will need to block
7482292SN/A    // and put all those instructions into the skid buffer.
7492292SN/A    if (!insts_to_decode.empty()) {
7502292SN/A        block(tid);
7512292SN/A    }
7522292SN/A
7532292SN/A    // Record that decode has written to the time buffer for activity
7542292SN/A    // tracking.
7552292SN/A    if (toRenameIndex) {
7562292SN/A        wroteToTimeBuffer = true;
7572292SN/A    }
7581060SN/A}
759