iew_impl.hh revision 8471:18e560ba1539
112838Sgabeblack@google.com/*
212838Sgabeblack@google.com * Copyright (c) 2010 ARM Limited
312838Sgabeblack@google.com * All rights reserved.
412838Sgabeblack@google.com *
512838Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612838Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712838Sgabeblack@google.com * property including but not limited to intellectual property relating
812838Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912838Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012838Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112838Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212838Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312838Sgabeblack@google.com *
1412838Sgabeblack@google.com * Copyright (c) 2004-2006 The Regents of The University of Michigan
1512838Sgabeblack@google.com * All rights reserved.
1612838Sgabeblack@google.com *
1712838Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1812838Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1912838Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
2012838Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
2112838Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
2212838Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2312838Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2412838Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2512838Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2612838Sgabeblack@google.com * this software without specific prior written permission.
2712838Sgabeblack@google.com *
2812838Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2912838Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3012838Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3112838Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3212838Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3312838Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3412838Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3512838Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3612838Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3712838Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3812838Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3912838Sgabeblack@google.com *
4012838Sgabeblack@google.com * Authors: Kevin Lim
4112838Sgabeblack@google.com */
4212838Sgabeblack@google.com
4312838Sgabeblack@google.com// @todo: Fix the instantaneous communication among all the stages within
4412838Sgabeblack@google.com// iew.  There's a clear delay between issue and execute, yet backwards
4512838Sgabeblack@google.com// communication happens simultaneously.
4612838Sgabeblack@google.com
4712838Sgabeblack@google.com#include <queue>
4812838Sgabeblack@google.com
4912838Sgabeblack@google.com#include "arch/utility.hh"
5012838Sgabeblack@google.com#include "config/the_isa.hh"
5112838Sgabeblack@google.com#include "cpu/o3/fu_pool.hh"
5212838Sgabeblack@google.com#include "cpu/o3/iew.hh"
5312838Sgabeblack@google.com#include "cpu/timebuf.hh"
5412838Sgabeblack@google.com#include "debug/Activity.hh"
5512838Sgabeblack@google.com#include "debug/Decode.hh"
5612838Sgabeblack@google.com#include "debug/IEW.hh"
5712838Sgabeblack@google.com#include "params/DerivO3CPU.hh"
5812838Sgabeblack@google.com
5912838Sgabeblack@google.comusing namespace std;
6012838Sgabeblack@google.com
6112838Sgabeblack@google.comtemplate<class Impl>
6212838Sgabeblack@google.comDefaultIEW<Impl>::DefaultIEW(O3CPU *_cpu, DerivO3CPUParams *params)
6312838Sgabeblack@google.com    : issueToExecQueue(params->backComSize, params->forwardComSize),
6412838Sgabeblack@google.com      cpu(_cpu),
6512838Sgabeblack@google.com      instQueue(_cpu, this, params),
6612838Sgabeblack@google.com      ldstQueue(_cpu, this, params),
6712838Sgabeblack@google.com      fuPool(params->fuPool),
6812838Sgabeblack@google.com      commitToIEWDelay(params->commitToIEWDelay),
6912838Sgabeblack@google.com      renameToIEWDelay(params->renameToIEWDelay),
7012838Sgabeblack@google.com      issueToExecuteDelay(params->issueToExecuteDelay),
7112838Sgabeblack@google.com      dispatchWidth(params->dispatchWidth),
7212838Sgabeblack@google.com      issueWidth(params->issueWidth),
7312838Sgabeblack@google.com      wbOutstanding(0),
7412838Sgabeblack@google.com      wbWidth(params->wbWidth),
7512838Sgabeblack@google.com      numThreads(params->numThreads),
7612838Sgabeblack@google.com      switchedOut(false)
7712838Sgabeblack@google.com{
7812838Sgabeblack@google.com    _status = Active;
7912838Sgabeblack@google.com    exeStatus = Running;
8012838Sgabeblack@google.com    wbStatus = Idle;
8112838Sgabeblack@google.com
8212838Sgabeblack@google.com    // Setup wire to read instructions coming from issue.
8312838Sgabeblack@google.com    fromIssue = issueToExecQueue.getWire(-issueToExecuteDelay);
8412838Sgabeblack@google.com
8512838Sgabeblack@google.com    // Instruction queue needs the queue between issue and execute.
8612838Sgabeblack@google.com    instQueue.setIssueToExecuteQueue(&issueToExecQueue);
8712838Sgabeblack@google.com
8812838Sgabeblack@google.com    for (ThreadID tid = 0; tid < numThreads; tid++) {
8912838Sgabeblack@google.com        dispatchStatus[tid] = Running;
9012838Sgabeblack@google.com        stalls[tid].commit = false;
9112838Sgabeblack@google.com        fetchRedirect[tid] = false;
9212838Sgabeblack@google.com    }
9312838Sgabeblack@google.com
9412838Sgabeblack@google.com    wbMax = wbWidth * params->wbDepth;
9512838Sgabeblack@google.com
9612838Sgabeblack@google.com    updateLSQNextCycle = false;
9712838Sgabeblack@google.com
9812838Sgabeblack@google.com    ableToIssue = true;
9912838Sgabeblack@google.com
10012838Sgabeblack@google.com    skidBufferMax = (3 * (renameToIEWDelay * params->renameWidth)) + issueWidth;
10112838Sgabeblack@google.com}
10212838Sgabeblack@google.com
10312838Sgabeblack@google.comtemplate <class Impl>
10412838Sgabeblack@google.comstd::string
10512838Sgabeblack@google.comDefaultIEW<Impl>::name() const
10612838Sgabeblack@google.com{
10712838Sgabeblack@google.com    return cpu->name() + ".iew";
10812838Sgabeblack@google.com}
10912838Sgabeblack@google.com
11012838Sgabeblack@google.comtemplate <class Impl>
11112838Sgabeblack@google.comvoid
11212838Sgabeblack@google.comDefaultIEW<Impl>::regStats()
11312838Sgabeblack@google.com{
11412838Sgabeblack@google.com    using namespace Stats;
11512838Sgabeblack@google.com
11612838Sgabeblack@google.com    instQueue.regStats();
11712838Sgabeblack@google.com    ldstQueue.regStats();
11812838Sgabeblack@google.com
11912838Sgabeblack@google.com    iewIdleCycles
12012838Sgabeblack@google.com        .name(name() + ".iewIdleCycles")
12112838Sgabeblack@google.com        .desc("Number of cycles IEW is idle");
12212838Sgabeblack@google.com
12312838Sgabeblack@google.com    iewSquashCycles
12412838Sgabeblack@google.com        .name(name() + ".iewSquashCycles")
12512838Sgabeblack@google.com        .desc("Number of cycles IEW is squashing");
12612838Sgabeblack@google.com
12712838Sgabeblack@google.com    iewBlockCycles
12812838Sgabeblack@google.com        .name(name() + ".iewBlockCycles")
12912838Sgabeblack@google.com        .desc("Number of cycles IEW is blocking");
13012838Sgabeblack@google.com
13112838Sgabeblack@google.com    iewUnblockCycles
13212838Sgabeblack@google.com        .name(name() + ".iewUnblockCycles")
13312838Sgabeblack@google.com        .desc("Number of cycles IEW is unblocking");
13412838Sgabeblack@google.com
13512838Sgabeblack@google.com    iewDispatchedInsts
13612838Sgabeblack@google.com        .name(name() + ".iewDispatchedInsts")
13712838Sgabeblack@google.com        .desc("Number of instructions dispatched to IQ");
13812838Sgabeblack@google.com
13912838Sgabeblack@google.com    iewDispSquashedInsts
14012838Sgabeblack@google.com        .name(name() + ".iewDispSquashedInsts")
14112838Sgabeblack@google.com        .desc("Number of squashed instructions skipped by dispatch");
14212838Sgabeblack@google.com
14312838Sgabeblack@google.com    iewDispLoadInsts
14412838Sgabeblack@google.com        .name(name() + ".iewDispLoadInsts")
14512838Sgabeblack@google.com        .desc("Number of dispatched load instructions");
14612838Sgabeblack@google.com
14712838Sgabeblack@google.com    iewDispStoreInsts
14812838Sgabeblack@google.com        .name(name() + ".iewDispStoreInsts")
14912838Sgabeblack@google.com        .desc("Number of dispatched store instructions");
15012838Sgabeblack@google.com
15112838Sgabeblack@google.com    iewDispNonSpecInsts
15212838Sgabeblack@google.com        .name(name() + ".iewDispNonSpecInsts")
15312838Sgabeblack@google.com        .desc("Number of dispatched non-speculative instructions");
15412838Sgabeblack@google.com
15512838Sgabeblack@google.com    iewIQFullEvents
15612838Sgabeblack@google.com        .name(name() + ".iewIQFullEvents")
15712838Sgabeblack@google.com        .desc("Number of times the IQ has become full, causing a stall");
15812838Sgabeblack@google.com
15912838Sgabeblack@google.com    iewLSQFullEvents
16012838Sgabeblack@google.com        .name(name() + ".iewLSQFullEvents")
16112838Sgabeblack@google.com        .desc("Number of times the LSQ has become full, causing a stall");
16212838Sgabeblack@google.com
16312838Sgabeblack@google.com    memOrderViolationEvents
16412838Sgabeblack@google.com        .name(name() + ".memOrderViolationEvents")
16512838Sgabeblack@google.com        .desc("Number of memory order violations");
16612838Sgabeblack@google.com
16712838Sgabeblack@google.com    predictedTakenIncorrect
16812838Sgabeblack@google.com        .name(name() + ".predictedTakenIncorrect")
16912838Sgabeblack@google.com        .desc("Number of branches that were predicted taken incorrectly");
17012838Sgabeblack@google.com
17112838Sgabeblack@google.com    predictedNotTakenIncorrect
17212838Sgabeblack@google.com        .name(name() + ".predictedNotTakenIncorrect")
17312838Sgabeblack@google.com        .desc("Number of branches that were predicted not taken incorrectly");
17412838Sgabeblack@google.com
17512838Sgabeblack@google.com    branchMispredicts
17612838Sgabeblack@google.com        .name(name() + ".branchMispredicts")
17712838Sgabeblack@google.com        .desc("Number of branch mispredicts detected at execute");
17812838Sgabeblack@google.com
17912838Sgabeblack@google.com    branchMispredicts = predictedTakenIncorrect + predictedNotTakenIncorrect;
18012838Sgabeblack@google.com
18112838Sgabeblack@google.com    iewExecutedInsts
18212838Sgabeblack@google.com        .name(name() + ".iewExecutedInsts")
18312838Sgabeblack@google.com        .desc("Number of executed instructions");
18412838Sgabeblack@google.com
18512838Sgabeblack@google.com    iewExecLoadInsts
18612838Sgabeblack@google.com        .init(cpu->numThreads)
18712838Sgabeblack@google.com        .name(name() + ".iewExecLoadInsts")
18812838Sgabeblack@google.com        .desc("Number of load instructions executed")
18912838Sgabeblack@google.com        .flags(total);
19012838Sgabeblack@google.com
19112838Sgabeblack@google.com    iewExecSquashedInsts
19212838Sgabeblack@google.com        .name(name() + ".iewExecSquashedInsts")
19312838Sgabeblack@google.com        .desc("Number of squashed instructions skipped in execute");
19412838Sgabeblack@google.com
19512838Sgabeblack@google.com    iewExecutedSwp
19612838Sgabeblack@google.com        .init(cpu->numThreads)
19712838Sgabeblack@google.com        .name(name() + ".exec_swp")
19812838Sgabeblack@google.com        .desc("number of swp insts executed")
19912838Sgabeblack@google.com        .flags(total);
20012838Sgabeblack@google.com
20112838Sgabeblack@google.com    iewExecutedNop
20212838Sgabeblack@google.com        .init(cpu->numThreads)
20312838Sgabeblack@google.com        .name(name() + ".exec_nop")
20412838Sgabeblack@google.com        .desc("number of nop insts executed")
20512838Sgabeblack@google.com        .flags(total);
20612838Sgabeblack@google.com
20712838Sgabeblack@google.com    iewExecutedRefs
20812838Sgabeblack@google.com        .init(cpu->numThreads)
20912838Sgabeblack@google.com        .name(name() + ".exec_refs")
21012838Sgabeblack@google.com        .desc("number of memory reference insts executed")
21112838Sgabeblack@google.com        .flags(total);
21212838Sgabeblack@google.com
21312838Sgabeblack@google.com    iewExecutedBranches
21412838Sgabeblack@google.com        .init(cpu->numThreads)
21512838Sgabeblack@google.com        .name(name() + ".exec_branches")
21612838Sgabeblack@google.com        .desc("Number of branches executed")
21712838Sgabeblack@google.com        .flags(total);
21812838Sgabeblack@google.com
21912838Sgabeblack@google.com    iewExecStoreInsts
22012838Sgabeblack@google.com        .name(name() + ".exec_stores")
22112838Sgabeblack@google.com        .desc("Number of stores executed")
22212838Sgabeblack@google.com        .flags(total);
22312838Sgabeblack@google.com    iewExecStoreInsts = iewExecutedRefs - iewExecLoadInsts;
22412838Sgabeblack@google.com
22512838Sgabeblack@google.com    iewExecRate
22612838Sgabeblack@google.com        .name(name() + ".exec_rate")
22712838Sgabeblack@google.com        .desc("Inst execution rate")
22812838Sgabeblack@google.com        .flags(total);
22912838Sgabeblack@google.com
23012838Sgabeblack@google.com    iewExecRate = iewExecutedInsts / cpu->numCycles;
23112838Sgabeblack@google.com
23212838Sgabeblack@google.com    iewInstsToCommit
23312838Sgabeblack@google.com        .init(cpu->numThreads)
23412838Sgabeblack@google.com        .name(name() + ".wb_sent")
23512838Sgabeblack@google.com        .desc("cumulative count of insts sent to commit")
23612838Sgabeblack@google.com        .flags(total);
23712838Sgabeblack@google.com
23812838Sgabeblack@google.com    writebackCount
23912838Sgabeblack@google.com        .init(cpu->numThreads)
24012838Sgabeblack@google.com        .name(name() + ".wb_count")
24112838Sgabeblack@google.com        .desc("cumulative count of insts written-back")
24212838Sgabeblack@google.com        .flags(total);
24312838Sgabeblack@google.com
24412838Sgabeblack@google.com    producerInst
24512838Sgabeblack@google.com        .init(cpu->numThreads)
24612838Sgabeblack@google.com        .name(name() + ".wb_producers")
24712838Sgabeblack@google.com        .desc("num instructions producing a value")
24812838Sgabeblack@google.com        .flags(total);
24912838Sgabeblack@google.com
25012838Sgabeblack@google.com    consumerInst
25112838Sgabeblack@google.com        .init(cpu->numThreads)
25212838Sgabeblack@google.com        .name(name() + ".wb_consumers")
25312838Sgabeblack@google.com        .desc("num instructions consuming a value")
25412838Sgabeblack@google.com        .flags(total);
25512838Sgabeblack@google.com
25612838Sgabeblack@google.com    wbPenalized
25712838Sgabeblack@google.com        .init(cpu->numThreads)
25812838Sgabeblack@google.com        .name(name() + ".wb_penalized")
25912838Sgabeblack@google.com        .desc("number of instrctions required to write to 'other' IQ")
26012838Sgabeblack@google.com        .flags(total);
26112838Sgabeblack@google.com
26212838Sgabeblack@google.com    wbPenalizedRate
26312838Sgabeblack@google.com        .name(name() + ".wb_penalized_rate")
26412838Sgabeblack@google.com        .desc ("fraction of instructions written-back that wrote to 'other' IQ")
26512838Sgabeblack@google.com        .flags(total);
26612838Sgabeblack@google.com
26712838Sgabeblack@google.com    wbPenalizedRate = wbPenalized / writebackCount;
26812838Sgabeblack@google.com
26912838Sgabeblack@google.com    wbFanout
27012838Sgabeblack@google.com        .name(name() + ".wb_fanout")
27112838Sgabeblack@google.com        .desc("average fanout of values written-back")
27212838Sgabeblack@google.com        .flags(total);
27312838Sgabeblack@google.com
27412838Sgabeblack@google.com    wbFanout = producerInst / consumerInst;
27512838Sgabeblack@google.com
27612838Sgabeblack@google.com    wbRate
27712838Sgabeblack@google.com        .name(name() + ".wb_rate")
27812838Sgabeblack@google.com        .desc("insts written-back per cycle")
27912838Sgabeblack@google.com        .flags(total);
28012838Sgabeblack@google.com    wbRate = writebackCount / cpu->numCycles;
28112838Sgabeblack@google.com}
282
283template<class Impl>
284void
285DefaultIEW<Impl>::initStage()
286{
287    for (ThreadID tid = 0; tid < numThreads; tid++) {
288        toRename->iewInfo[tid].usedIQ = true;
289        toRename->iewInfo[tid].freeIQEntries =
290            instQueue.numFreeEntries(tid);
291
292        toRename->iewInfo[tid].usedLSQ = true;
293        toRename->iewInfo[tid].freeLSQEntries =
294            ldstQueue.numFreeEntries(tid);
295    }
296
297    cpu->activateStage(O3CPU::IEWIdx);
298}
299
300template<class Impl>
301void
302DefaultIEW<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
303{
304    timeBuffer = tb_ptr;
305
306    // Setup wire to read information from time buffer, from commit.
307    fromCommit = timeBuffer->getWire(-commitToIEWDelay);
308
309    // Setup wire to write information back to previous stages.
310    toRename = timeBuffer->getWire(0);
311
312    toFetch = timeBuffer->getWire(0);
313
314    // Instruction queue also needs main time buffer.
315    instQueue.setTimeBuffer(tb_ptr);
316}
317
318template<class Impl>
319void
320DefaultIEW<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
321{
322    renameQueue = rq_ptr;
323
324    // Setup wire to read information from rename queue.
325    fromRename = renameQueue->getWire(-renameToIEWDelay);
326}
327
328template<class Impl>
329void
330DefaultIEW<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
331{
332    iewQueue = iq_ptr;
333
334    // Setup wire to write instructions to commit.
335    toCommit = iewQueue->getWire(0);
336}
337
338template<class Impl>
339void
340DefaultIEW<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
341{
342    activeThreads = at_ptr;
343
344    ldstQueue.setActiveThreads(at_ptr);
345    instQueue.setActiveThreads(at_ptr);
346}
347
348template<class Impl>
349void
350DefaultIEW<Impl>::setScoreboard(Scoreboard *sb_ptr)
351{
352    scoreboard = sb_ptr;
353}
354
355template <class Impl>
356bool
357DefaultIEW<Impl>::drain()
358{
359    // IEW is ready to drain at any time.
360    cpu->signalDrained();
361    return true;
362}
363
364template <class Impl>
365void
366DefaultIEW<Impl>::resume()
367{
368}
369
370template <class Impl>
371void
372DefaultIEW<Impl>::switchOut()
373{
374    // Clear any state.
375    switchedOut = true;
376    assert(insts[0].empty());
377    assert(skidBuffer[0].empty());
378
379    instQueue.switchOut();
380    ldstQueue.switchOut();
381    fuPool->switchOut();
382
383    for (ThreadID tid = 0; tid < numThreads; tid++) {
384        while (!insts[tid].empty())
385            insts[tid].pop();
386        while (!skidBuffer[tid].empty())
387            skidBuffer[tid].pop();
388    }
389}
390
391template <class Impl>
392void
393DefaultIEW<Impl>::takeOverFrom()
394{
395    // Reset all state.
396    _status = Active;
397    exeStatus = Running;
398    wbStatus = Idle;
399    switchedOut = false;
400
401    instQueue.takeOverFrom();
402    ldstQueue.takeOverFrom();
403    fuPool->takeOverFrom();
404
405    initStage();
406    cpu->activityThisCycle();
407
408    for (ThreadID tid = 0; tid < numThreads; tid++) {
409        dispatchStatus[tid] = Running;
410        stalls[tid].commit = false;
411        fetchRedirect[tid] = false;
412    }
413
414    updateLSQNextCycle = false;
415
416    for (int i = 0; i < issueToExecQueue.getSize(); ++i) {
417        issueToExecQueue.advance();
418    }
419}
420
421template<class Impl>
422void
423DefaultIEW<Impl>::squash(ThreadID tid)
424{
425    DPRINTF(IEW, "[tid:%i]: Squashing all instructions.\n", tid);
426
427    // Tell the IQ to start squashing.
428    instQueue.squash(tid);
429
430    // Tell the LDSTQ to start squashing.
431    ldstQueue.squash(fromCommit->commitInfo[tid].doneSeqNum, tid);
432    updatedQueues = true;
433
434    // Clear the skid buffer in case it has any data in it.
435    DPRINTF(IEW, "[tid:%i]: Removing skidbuffer instructions until [sn:%i].\n",
436            tid, fromCommit->commitInfo[tid].doneSeqNum);
437
438    while (!skidBuffer[tid].empty()) {
439        if (skidBuffer[tid].front()->isLoad() ||
440            skidBuffer[tid].front()->isStore() ) {
441            toRename->iewInfo[tid].dispatchedToLSQ++;
442        }
443
444        toRename->iewInfo[tid].dispatched++;
445
446        skidBuffer[tid].pop();
447    }
448
449    emptyRenameInsts(tid);
450}
451
452template<class Impl>
453void
454DefaultIEW<Impl>::squashDueToBranch(DynInstPtr &inst, ThreadID tid)
455{
456    DPRINTF(IEW, "[tid:%i]: Squashing from a specific instruction, PC: %s "
457            "[sn:%i].\n", tid, inst->pcState(), inst->seqNum);
458
459    if (toCommit->squash[tid] == false ||
460            inst->seqNum < toCommit->squashedSeqNum[tid]) {
461        toCommit->squash[tid] = true;
462        toCommit->squashedSeqNum[tid] = inst->seqNum;
463        toCommit->branchTaken[tid] = inst->pcState().branching();
464
465        TheISA::PCState pc = inst->pcState();
466        TheISA::advancePC(pc, inst->staticInst);
467
468        toCommit->pc[tid] = pc;
469        toCommit->mispredictInst[tid] = inst;
470        toCommit->includeSquashInst[tid] = false;
471
472        wroteToTimeBuffer = true;
473    }
474
475}
476
477template<class Impl>
478void
479DefaultIEW<Impl>::squashDueToMemOrder(DynInstPtr &inst, ThreadID tid)
480{
481    DPRINTF(IEW, "[tid:%i]: Squashing from a specific instruction, "
482            "PC: %s [sn:%i].\n", tid, inst->pcState(), inst->seqNum);
483
484    if (toCommit->squash[tid] == false ||
485            inst->seqNum < toCommit->squashedSeqNum[tid]) {
486        toCommit->squash[tid] = true;
487        toCommit->squashedSeqNum[tid] = inst->seqNum;
488        TheISA::PCState pc;
489        if (inst->isMemRef() && inst->isIndirectCtrl()) {
490            // If an operation is a control operation as well as a memory
491            // reference we need to use the predicted PC, not the PC+N
492            // This instruction will verify misprediction based on predPC
493            pc = inst->readPredTarg();
494        } else {
495            pc = inst->pcState();
496            TheISA::advancePC(pc, inst->staticInst);
497        }
498        toCommit->pc[tid] = pc;
499        toCommit->mispredictInst[tid] = NULL;
500
501        toCommit->includeSquashInst[tid] = false;
502
503        wroteToTimeBuffer = true;
504    }
505}
506
507template<class Impl>
508void
509DefaultIEW<Impl>::squashDueToMemBlocked(DynInstPtr &inst, ThreadID tid)
510{
511    DPRINTF(IEW, "[tid:%i]: Memory blocked, squashing load and younger insts, "
512            "PC: %s [sn:%i].\n", tid, inst->pcState(), inst->seqNum);
513    if (toCommit->squash[tid] == false ||
514            inst->seqNum < toCommit->squashedSeqNum[tid]) {
515        toCommit->squash[tid] = true;
516
517        toCommit->squashedSeqNum[tid] = inst->seqNum;
518        toCommit->pc[tid] = inst->pcState();
519        toCommit->mispredictInst[tid] = NULL;
520
521        // Must include the broadcasted SN in the squash.
522        toCommit->includeSquashInst[tid] = true;
523
524        ldstQueue.setLoadBlockedHandled(tid);
525
526        wroteToTimeBuffer = true;
527    }
528}
529
530template<class Impl>
531void
532DefaultIEW<Impl>::block(ThreadID tid)
533{
534    DPRINTF(IEW, "[tid:%u]: Blocking.\n", tid);
535
536    if (dispatchStatus[tid] != Blocked &&
537        dispatchStatus[tid] != Unblocking) {
538        toRename->iewBlock[tid] = true;
539        wroteToTimeBuffer = true;
540    }
541
542    // Add the current inputs to the skid buffer so they can be
543    // reprocessed when this stage unblocks.
544    skidInsert(tid);
545
546    dispatchStatus[tid] = Blocked;
547}
548
549template<class Impl>
550void
551DefaultIEW<Impl>::unblock(ThreadID tid)
552{
553    DPRINTF(IEW, "[tid:%i]: Reading instructions out of the skid "
554            "buffer %u.\n",tid, tid);
555
556    // If the skid bufffer is empty, signal back to previous stages to unblock.
557    // Also switch status to running.
558    if (skidBuffer[tid].empty()) {
559        toRename->iewUnblock[tid] = true;
560        wroteToTimeBuffer = true;
561        DPRINTF(IEW, "[tid:%i]: Done unblocking.\n",tid);
562        dispatchStatus[tid] = Running;
563    }
564}
565
566template<class Impl>
567void
568DefaultIEW<Impl>::wakeDependents(DynInstPtr &inst)
569{
570    instQueue.wakeDependents(inst);
571}
572
573template<class Impl>
574void
575DefaultIEW<Impl>::rescheduleMemInst(DynInstPtr &inst)
576{
577    instQueue.rescheduleMemInst(inst);
578}
579
580template<class Impl>
581void
582DefaultIEW<Impl>::replayMemInst(DynInstPtr &inst)
583{
584    instQueue.replayMemInst(inst);
585}
586
587template<class Impl>
588void
589DefaultIEW<Impl>::instToCommit(DynInstPtr &inst)
590{
591    // This function should not be called after writebackInsts in a
592    // single cycle.  That will cause problems with an instruction
593    // being added to the queue to commit without being processed by
594    // writebackInsts prior to being sent to commit.
595
596    // First check the time slot that this instruction will write
597    // to.  If there are free write ports at the time, then go ahead
598    // and write the instruction to that time.  If there are not,
599    // keep looking back to see where's the first time there's a
600    // free slot.
601    while ((*iewQueue)[wbCycle].insts[wbNumInst]) {
602        ++wbNumInst;
603        if (wbNumInst == wbWidth) {
604            ++wbCycle;
605            wbNumInst = 0;
606        }
607
608        assert((wbCycle * wbWidth + wbNumInst) <= wbMax);
609    }
610
611    DPRINTF(IEW, "Current wb cycle: %i, width: %i, numInst: %i\nwbActual:%i\n",
612            wbCycle, wbWidth, wbNumInst, wbCycle * wbWidth + wbNumInst);
613    // Add finished instruction to queue to commit.
614    (*iewQueue)[wbCycle].insts[wbNumInst] = inst;
615    (*iewQueue)[wbCycle].size++;
616}
617
618template <class Impl>
619unsigned
620DefaultIEW<Impl>::validInstsFromRename()
621{
622    unsigned inst_count = 0;
623
624    for (int i=0; i<fromRename->size; i++) {
625        if (!fromRename->insts[i]->isSquashed())
626            inst_count++;
627    }
628
629    return inst_count;
630}
631
632template<class Impl>
633void
634DefaultIEW<Impl>::skidInsert(ThreadID tid)
635{
636    DynInstPtr inst = NULL;
637
638    while (!insts[tid].empty()) {
639        inst = insts[tid].front();
640
641        insts[tid].pop();
642
643        DPRINTF(Decode,"[tid:%i]: Inserting [sn:%lli] PC:%s into "
644                "dispatch skidBuffer %i\n",tid, inst->seqNum,
645                inst->pcState(),tid);
646
647        skidBuffer[tid].push(inst);
648    }
649
650    assert(skidBuffer[tid].size() <= skidBufferMax &&
651           "Skidbuffer Exceeded Max Size");
652}
653
654template<class Impl>
655int
656DefaultIEW<Impl>::skidCount()
657{
658    int max=0;
659
660    list<ThreadID>::iterator threads = activeThreads->begin();
661    list<ThreadID>::iterator end = activeThreads->end();
662
663    while (threads != end) {
664        ThreadID tid = *threads++;
665        unsigned thread_count = skidBuffer[tid].size();
666        if (max < thread_count)
667            max = thread_count;
668    }
669
670    return max;
671}
672
673template<class Impl>
674bool
675DefaultIEW<Impl>::skidsEmpty()
676{
677    list<ThreadID>::iterator threads = activeThreads->begin();
678    list<ThreadID>::iterator end = activeThreads->end();
679
680    while (threads != end) {
681        ThreadID tid = *threads++;
682
683        if (!skidBuffer[tid].empty())
684            return false;
685    }
686
687    return true;
688}
689
690template <class Impl>
691void
692DefaultIEW<Impl>::updateStatus()
693{
694    bool any_unblocking = false;
695
696    list<ThreadID>::iterator threads = activeThreads->begin();
697    list<ThreadID>::iterator end = activeThreads->end();
698
699    while (threads != end) {
700        ThreadID tid = *threads++;
701
702        if (dispatchStatus[tid] == Unblocking) {
703            any_unblocking = true;
704            break;
705        }
706    }
707
708    // If there are no ready instructions waiting to be scheduled by the IQ,
709    // and there's no stores waiting to write back, and dispatch is not
710    // unblocking, then there is no internal activity for the IEW stage.
711    instQueue.intInstQueueReads++;
712    if (_status == Active && !instQueue.hasReadyInsts() &&
713        !ldstQueue.willWB() && !any_unblocking) {
714        DPRINTF(IEW, "IEW switching to idle\n");
715
716        deactivateStage();
717
718        _status = Inactive;
719    } else if (_status == Inactive && (instQueue.hasReadyInsts() ||
720                                       ldstQueue.willWB() ||
721                                       any_unblocking)) {
722        // Otherwise there is internal activity.  Set to active.
723        DPRINTF(IEW, "IEW switching to active\n");
724
725        activateStage();
726
727        _status = Active;
728    }
729}
730
731template <class Impl>
732void
733DefaultIEW<Impl>::resetEntries()
734{
735    instQueue.resetEntries();
736    ldstQueue.resetEntries();
737}
738
739template <class Impl>
740void
741DefaultIEW<Impl>::readStallSignals(ThreadID tid)
742{
743    if (fromCommit->commitBlock[tid]) {
744        stalls[tid].commit = true;
745    }
746
747    if (fromCommit->commitUnblock[tid]) {
748        assert(stalls[tid].commit);
749        stalls[tid].commit = false;
750    }
751}
752
753template <class Impl>
754bool
755DefaultIEW<Impl>::checkStall(ThreadID tid)
756{
757    bool ret_val(false);
758
759    if (stalls[tid].commit) {
760        DPRINTF(IEW,"[tid:%i]: Stall from Commit stage detected.\n",tid);
761        ret_val = true;
762    } else if (instQueue.isFull(tid)) {
763        DPRINTF(IEW,"[tid:%i]: Stall: IQ  is full.\n",tid);
764        ret_val = true;
765    } else if (ldstQueue.isFull(tid)) {
766        DPRINTF(IEW,"[tid:%i]: Stall: LSQ is full\n",tid);
767
768        if (ldstQueue.numLoads(tid) > 0 ) {
769
770            DPRINTF(IEW,"[tid:%i]: LSQ oldest load: [sn:%i] \n",
771                    tid,ldstQueue.getLoadHeadSeqNum(tid));
772        }
773
774        if (ldstQueue.numStores(tid) > 0) {
775
776            DPRINTF(IEW,"[tid:%i]: LSQ oldest store: [sn:%i] \n",
777                    tid,ldstQueue.getStoreHeadSeqNum(tid));
778        }
779
780        ret_val = true;
781    } else if (ldstQueue.isStalled(tid)) {
782        DPRINTF(IEW,"[tid:%i]: Stall: LSQ stall detected.\n",tid);
783        ret_val = true;
784    }
785
786    return ret_val;
787}
788
789template <class Impl>
790void
791DefaultIEW<Impl>::checkSignalsAndUpdate(ThreadID tid)
792{
793    // Check if there's a squash signal, squash if there is
794    // Check stall signals, block if there is.
795    // If status was Blocked
796    //     if so then go to unblocking
797    // If status was Squashing
798    //     check if squashing is not high.  Switch to running this cycle.
799
800    readStallSignals(tid);
801
802    if (fromCommit->commitInfo[tid].squash) {
803        squash(tid);
804
805        if (dispatchStatus[tid] == Blocked ||
806            dispatchStatus[tid] == Unblocking) {
807            toRename->iewUnblock[tid] = true;
808            wroteToTimeBuffer = true;
809        }
810
811        dispatchStatus[tid] = Squashing;
812        fetchRedirect[tid] = false;
813        return;
814    }
815
816    if (fromCommit->commitInfo[tid].robSquashing) {
817        DPRINTF(IEW, "[tid:%i]: ROB is still squashing.\n", tid);
818
819        dispatchStatus[tid] = Squashing;
820        emptyRenameInsts(tid);
821        wroteToTimeBuffer = true;
822        return;
823    }
824
825    if (checkStall(tid)) {
826        block(tid);
827        dispatchStatus[tid] = Blocked;
828        return;
829    }
830
831    if (dispatchStatus[tid] == Blocked) {
832        // Status from previous cycle was blocked, but there are no more stall
833        // conditions.  Switch over to unblocking.
834        DPRINTF(IEW, "[tid:%i]: Done blocking, switching to unblocking.\n",
835                tid);
836
837        dispatchStatus[tid] = Unblocking;
838
839        unblock(tid);
840
841        return;
842    }
843
844    if (dispatchStatus[tid] == Squashing) {
845        // Switch status to running if rename isn't being told to block or
846        // squash this cycle.
847        DPRINTF(IEW, "[tid:%i]: Done squashing, switching to running.\n",
848                tid);
849
850        dispatchStatus[tid] = Running;
851
852        return;
853    }
854}
855
856template <class Impl>
857void
858DefaultIEW<Impl>::sortInsts()
859{
860    int insts_from_rename = fromRename->size;
861#ifdef DEBUG
862    for (ThreadID tid = 0; tid < numThreads; tid++)
863        assert(insts[tid].empty());
864#endif
865    for (int i = 0; i < insts_from_rename; ++i) {
866        insts[fromRename->insts[i]->threadNumber].push(fromRename->insts[i]);
867    }
868}
869
870template <class Impl>
871void
872DefaultIEW<Impl>::emptyRenameInsts(ThreadID tid)
873{
874    DPRINTF(IEW, "[tid:%i]: Removing incoming rename instructions\n", tid);
875
876    while (!insts[tid].empty()) {
877
878        if (insts[tid].front()->isLoad() ||
879            insts[tid].front()->isStore() ) {
880            toRename->iewInfo[tid].dispatchedToLSQ++;
881        }
882
883        toRename->iewInfo[tid].dispatched++;
884
885        insts[tid].pop();
886    }
887}
888
889template <class Impl>
890void
891DefaultIEW<Impl>::wakeCPU()
892{
893    cpu->wakeCPU();
894}
895
896template <class Impl>
897void
898DefaultIEW<Impl>::activityThisCycle()
899{
900    DPRINTF(Activity, "Activity this cycle.\n");
901    cpu->activityThisCycle();
902}
903
904template <class Impl>
905inline void
906DefaultIEW<Impl>::activateStage()
907{
908    DPRINTF(Activity, "Activating stage.\n");
909    cpu->activateStage(O3CPU::IEWIdx);
910}
911
912template <class Impl>
913inline void
914DefaultIEW<Impl>::deactivateStage()
915{
916    DPRINTF(Activity, "Deactivating stage.\n");
917    cpu->deactivateStage(O3CPU::IEWIdx);
918}
919
920template<class Impl>
921void
922DefaultIEW<Impl>::dispatch(ThreadID tid)
923{
924    // If status is Running or idle,
925    //     call dispatchInsts()
926    // If status is Unblocking,
927    //     buffer any instructions coming from rename
928    //     continue trying to empty skid buffer
929    //     check if stall conditions have passed
930
931    if (dispatchStatus[tid] == Blocked) {
932        ++iewBlockCycles;
933
934    } else if (dispatchStatus[tid] == Squashing) {
935        ++iewSquashCycles;
936    }
937
938    // Dispatch should try to dispatch as many instructions as its bandwidth
939    // will allow, as long as it is not currently blocked.
940    if (dispatchStatus[tid] == Running ||
941        dispatchStatus[tid] == Idle) {
942        DPRINTF(IEW, "[tid:%i] Not blocked, so attempting to run "
943                "dispatch.\n", tid);
944
945        dispatchInsts(tid);
946    } else if (dispatchStatus[tid] == Unblocking) {
947        // Make sure that the skid buffer has something in it if the
948        // status is unblocking.
949        assert(!skidsEmpty());
950
951        // If the status was unblocking, then instructions from the skid
952        // buffer were used.  Remove those instructions and handle
953        // the rest of unblocking.
954        dispatchInsts(tid);
955
956        ++iewUnblockCycles;
957
958        if (validInstsFromRename()) {
959            // Add the current inputs to the skid buffer so they can be
960            // reprocessed when this stage unblocks.
961            skidInsert(tid);
962        }
963
964        unblock(tid);
965    }
966}
967
968template <class Impl>
969void
970DefaultIEW<Impl>::dispatchInsts(ThreadID tid)
971{
972    // Obtain instructions from skid buffer if unblocking, or queue from rename
973    // otherwise.
974    std::queue<DynInstPtr> &insts_to_dispatch =
975        dispatchStatus[tid] == Unblocking ?
976        skidBuffer[tid] : insts[tid];
977
978    int insts_to_add = insts_to_dispatch.size();
979
980    DynInstPtr inst;
981    bool add_to_iq = false;
982    int dis_num_inst = 0;
983
984    // Loop through the instructions, putting them in the instruction
985    // queue.
986    for ( ; dis_num_inst < insts_to_add &&
987              dis_num_inst < dispatchWidth;
988          ++dis_num_inst)
989    {
990        inst = insts_to_dispatch.front();
991
992        if (dispatchStatus[tid] == Unblocking) {
993            DPRINTF(IEW, "[tid:%i]: Issue: Examining instruction from skid "
994                    "buffer\n", tid);
995        }
996
997        // Make sure there's a valid instruction there.
998        assert(inst);
999
1000        DPRINTF(IEW, "[tid:%i]: Issue: Adding PC %s [sn:%lli] [tid:%i] to "
1001                "IQ.\n",
1002                tid, inst->pcState(), inst->seqNum, inst->threadNumber);
1003
1004        // Be sure to mark these instructions as ready so that the
1005        // commit stage can go ahead and execute them, and mark
1006        // them as issued so the IQ doesn't reprocess them.
1007
1008        // Check for squashed instructions.
1009        if (inst->isSquashed()) {
1010            DPRINTF(IEW, "[tid:%i]: Issue: Squashed instruction encountered, "
1011                    "not adding to IQ.\n", tid);
1012
1013            ++iewDispSquashedInsts;
1014
1015            insts_to_dispatch.pop();
1016
1017            //Tell Rename That An Instruction has been processed
1018            if (inst->isLoad() || inst->isStore()) {
1019                toRename->iewInfo[tid].dispatchedToLSQ++;
1020            }
1021            toRename->iewInfo[tid].dispatched++;
1022
1023            continue;
1024        }
1025
1026        // Check for full conditions.
1027        if (instQueue.isFull(tid)) {
1028            DPRINTF(IEW, "[tid:%i]: Issue: IQ has become full.\n", tid);
1029
1030            // Call function to start blocking.
1031            block(tid);
1032
1033            // Set unblock to false. Special case where we are using
1034            // skidbuffer (unblocking) instructions but then we still
1035            // get full in the IQ.
1036            toRename->iewUnblock[tid] = false;
1037
1038            ++iewIQFullEvents;
1039            break;
1040        } else if (ldstQueue.isFull(tid)) {
1041            DPRINTF(IEW, "[tid:%i]: Issue: LSQ has become full.\n",tid);
1042
1043            // Call function to start blocking.
1044            block(tid);
1045
1046            // Set unblock to false. Special case where we are using
1047            // skidbuffer (unblocking) instructions but then we still
1048            // get full in the IQ.
1049            toRename->iewUnblock[tid] = false;
1050
1051            ++iewLSQFullEvents;
1052            break;
1053        }
1054
1055        // Otherwise issue the instruction just fine.
1056        if (inst->isLoad()) {
1057            DPRINTF(IEW, "[tid:%i]: Issue: Memory instruction "
1058                    "encountered, adding to LSQ.\n", tid);
1059
1060            // Reserve a spot in the load store queue for this
1061            // memory access.
1062            ldstQueue.insertLoad(inst);
1063
1064            ++iewDispLoadInsts;
1065
1066            add_to_iq = true;
1067
1068            toRename->iewInfo[tid].dispatchedToLSQ++;
1069        } else if (inst->isStore()) {
1070            DPRINTF(IEW, "[tid:%i]: Issue: Memory instruction "
1071                    "encountered, adding to LSQ.\n", tid);
1072
1073            ldstQueue.insertStore(inst);
1074
1075            ++iewDispStoreInsts;
1076
1077            if (inst->isStoreConditional()) {
1078                // Store conditionals need to be set as "canCommit()"
1079                // so that commit can process them when they reach the
1080                // head of commit.
1081                // @todo: This is somewhat specific to Alpha.
1082                inst->setCanCommit();
1083                instQueue.insertNonSpec(inst);
1084                add_to_iq = false;
1085
1086                ++iewDispNonSpecInsts;
1087            } else {
1088                add_to_iq = true;
1089            }
1090
1091            toRename->iewInfo[tid].dispatchedToLSQ++;
1092        } else if (inst->isMemBarrier() || inst->isWriteBarrier()) {
1093            // Same as non-speculative stores.
1094            inst->setCanCommit();
1095            instQueue.insertBarrier(inst);
1096            add_to_iq = false;
1097        } else if (inst->isNop()) {
1098            DPRINTF(IEW, "[tid:%i]: Issue: Nop instruction encountered, "
1099                    "skipping.\n", tid);
1100
1101            inst->setIssued();
1102            inst->setExecuted();
1103            inst->setCanCommit();
1104
1105            instQueue.recordProducer(inst);
1106
1107            iewExecutedNop[tid]++;
1108
1109            add_to_iq = false;
1110        } else if (inst->isExecuted()) {
1111            assert(0 && "Instruction shouldn't be executed.\n");
1112            DPRINTF(IEW, "Issue: Executed branch encountered, "
1113                    "skipping.\n");
1114
1115            inst->setIssued();
1116            inst->setCanCommit();
1117
1118            instQueue.recordProducer(inst);
1119
1120            add_to_iq = false;
1121        } else {
1122            add_to_iq = true;
1123        }
1124        if (inst->isNonSpeculative()) {
1125            DPRINTF(IEW, "[tid:%i]: Issue: Nonspeculative instruction "
1126                    "encountered, skipping.\n", tid);
1127
1128            // Same as non-speculative stores.
1129            inst->setCanCommit();
1130
1131            // Specifically insert it as nonspeculative.
1132            instQueue.insertNonSpec(inst);
1133
1134            ++iewDispNonSpecInsts;
1135
1136            add_to_iq = false;
1137        }
1138
1139        // If the instruction queue is not full, then add the
1140        // instruction.
1141        if (add_to_iq) {
1142            instQueue.insert(inst);
1143        }
1144
1145        insts_to_dispatch.pop();
1146
1147        toRename->iewInfo[tid].dispatched++;
1148
1149        ++iewDispatchedInsts;
1150
1151#if TRACING_ON
1152        inst->dispatchTick = curTick();
1153#endif
1154    }
1155
1156    if (!insts_to_dispatch.empty()) {
1157        DPRINTF(IEW,"[tid:%i]: Issue: Bandwidth Full. Blocking.\n", tid);
1158        block(tid);
1159        toRename->iewUnblock[tid] = false;
1160    }
1161
1162    if (dispatchStatus[tid] == Idle && dis_num_inst) {
1163        dispatchStatus[tid] = Running;
1164
1165        updatedQueues = true;
1166    }
1167
1168    dis_num_inst = 0;
1169}
1170
1171template <class Impl>
1172void
1173DefaultIEW<Impl>::printAvailableInsts()
1174{
1175    int inst = 0;
1176
1177    std::cout << "Available Instructions: ";
1178
1179    while (fromIssue->insts[inst]) {
1180
1181        if (inst%3==0) std::cout << "\n\t";
1182
1183        std::cout << "PC: " << fromIssue->insts[inst]->pcState()
1184             << " TN: " << fromIssue->insts[inst]->threadNumber
1185             << " SN: " << fromIssue->insts[inst]->seqNum << " | ";
1186
1187        inst++;
1188
1189    }
1190
1191    std::cout << "\n";
1192}
1193
1194template <class Impl>
1195void
1196DefaultIEW<Impl>::executeInsts()
1197{
1198    wbNumInst = 0;
1199    wbCycle = 0;
1200
1201    list<ThreadID>::iterator threads = activeThreads->begin();
1202    list<ThreadID>::iterator end = activeThreads->end();
1203
1204    while (threads != end) {
1205        ThreadID tid = *threads++;
1206        fetchRedirect[tid] = false;
1207    }
1208
1209    // Uncomment this if you want to see all available instructions.
1210    // @todo This doesn't actually work anymore, we should fix it.
1211//    printAvailableInsts();
1212
1213    // Execute/writeback any instructions that are available.
1214    int insts_to_execute = fromIssue->size;
1215    int inst_num = 0;
1216    for (; inst_num < insts_to_execute;
1217          ++inst_num) {
1218
1219        DPRINTF(IEW, "Execute: Executing instructions from IQ.\n");
1220
1221        DynInstPtr inst = instQueue.getInstToExecute();
1222
1223        DPRINTF(IEW, "Execute: Processing PC %s, [tid:%i] [sn:%i].\n",
1224                inst->pcState(), inst->threadNumber,inst->seqNum);
1225
1226        // Check if the instruction is squashed; if so then skip it
1227        if (inst->isSquashed()) {
1228            DPRINTF(IEW, "Execute: Instruction was squashed. PC: %s, [tid:%i]"
1229                         " [sn:%i]\n", inst->pcState(), inst->threadNumber,
1230                         inst->seqNum);
1231
1232            // Consider this instruction executed so that commit can go
1233            // ahead and retire the instruction.
1234            inst->setExecuted();
1235
1236            // Not sure if I should set this here or just let commit try to
1237            // commit any squashed instructions.  I like the latter a bit more.
1238            inst->setCanCommit();
1239
1240            ++iewExecSquashedInsts;
1241
1242            decrWb(inst->seqNum);
1243            continue;
1244        }
1245
1246        Fault fault = NoFault;
1247
1248        // Execute instruction.
1249        // Note that if the instruction faults, it will be handled
1250        // at the commit stage.
1251        if (inst->isMemRef()) {
1252            DPRINTF(IEW, "Execute: Calculating address for memory "
1253                    "reference.\n");
1254
1255            // Tell the LDSTQ to execute this instruction (if it is a load).
1256            if (inst->isLoad()) {
1257                // Loads will mark themselves as executed, and their writeback
1258                // event adds the instruction to the queue to commit
1259                fault = ldstQueue.executeLoad(inst);
1260
1261                if (inst->isTranslationDelayed() &&
1262                    fault == NoFault) {
1263                    // A hw page table walk is currently going on; the
1264                    // instruction must be deferred.
1265                    DPRINTF(IEW, "Execute: Delayed translation, deferring "
1266                            "load.\n");
1267                    instQueue.deferMemInst(inst);
1268                    continue;
1269                }
1270
1271                if (inst->isDataPrefetch() || inst->isInstPrefetch()) {
1272                    inst->fault = NoFault;
1273                }
1274            } else if (inst->isStore()) {
1275                fault = ldstQueue.executeStore(inst);
1276
1277                if (inst->isTranslationDelayed() &&
1278                    fault == NoFault) {
1279                    // A hw page table walk is currently going on; the
1280                    // instruction must be deferred.
1281                    DPRINTF(IEW, "Execute: Delayed translation, deferring "
1282                            "store.\n");
1283                    instQueue.deferMemInst(inst);
1284                    continue;
1285                }
1286
1287                // If the store had a fault then it may not have a mem req
1288                if (fault != NoFault || inst->readPredicate() == false ||
1289                        !inst->isStoreConditional()) {
1290                    // If the instruction faulted, then we need to send it along
1291                    // to commit without the instruction completing.
1292                    // Send this instruction to commit, also make sure iew stage
1293                    // realizes there is activity.
1294                    inst->setExecuted();
1295                    instToCommit(inst);
1296                    activityThisCycle();
1297                }
1298
1299                // Store conditionals will mark themselves as
1300                // executed, and their writeback event will add the
1301                // instruction to the queue to commit.
1302            } else {
1303                panic("Unexpected memory type!\n");
1304            }
1305
1306        } else {
1307            // If the instruction has already faulted, then skip executing it.
1308            // Such case can happen when it faulted during ITLB translation.
1309            // If we execute the instruction (even if it's a nop) the fault
1310            // will be replaced and we will lose it.
1311            if (inst->getFault() == NoFault) {
1312                inst->execute();
1313                if (inst->readPredicate() == false)
1314                    inst->forwardOldRegs();
1315            }
1316
1317            inst->setExecuted();
1318
1319            instToCommit(inst);
1320        }
1321
1322        updateExeInstStats(inst);
1323
1324        // Check if branch prediction was correct, if not then we need
1325        // to tell commit to squash in flight instructions.  Only
1326        // handle this if there hasn't already been something that
1327        // redirects fetch in this group of instructions.
1328
1329        // This probably needs to prioritize the redirects if a different
1330        // scheduler is used.  Currently the scheduler schedules the oldest
1331        // instruction first, so the branch resolution order will be correct.
1332        ThreadID tid = inst->threadNumber;
1333
1334        if (!fetchRedirect[tid] ||
1335            !toCommit->squash[tid] ||
1336            toCommit->squashedSeqNum[tid] > inst->seqNum) {
1337
1338            // Prevent testing for misprediction on load instructions,
1339            // that have not been executed.
1340            bool loadNotExecuted = !inst->isExecuted() && inst->isLoad();
1341
1342            if (inst->mispredicted() && !loadNotExecuted) {
1343                fetchRedirect[tid] = true;
1344
1345                DPRINTF(IEW, "Execute: Branch mispredict detected.\n");
1346                DPRINTF(IEW, "Predicted target was PC:%#x, NPC:%#x.\n",
1347                        inst->predInstAddr(), inst->predNextInstAddr());
1348                DPRINTF(IEW, "Execute: Redirecting fetch to PC: %s.\n",
1349                        inst->pcState(), inst->nextInstAddr());
1350                // If incorrect, then signal the ROB that it must be squashed.
1351                squashDueToBranch(inst, tid);
1352
1353                if (inst->readPredTaken()) {
1354                    predictedTakenIncorrect++;
1355                } else {
1356                    predictedNotTakenIncorrect++;
1357                }
1358            } else if (ldstQueue.violation(tid)) {
1359                assert(inst->isMemRef());
1360                // If there was an ordering violation, then get the
1361                // DynInst that caused the violation.  Note that this
1362                // clears the violation signal.
1363                DynInstPtr violator;
1364                violator = ldstQueue.getMemDepViolator(tid);
1365
1366                DPRINTF(IEW, "LDSTQ detected a violation. Violator PC: %s "
1367                        "[sn:%lli], inst PC: %s [sn:%lli]. Addr is: %#x.\n",
1368                        violator->pcState(), violator->seqNum,
1369                        inst->pcState(), inst->seqNum, inst->physEffAddr);
1370
1371                fetchRedirect[tid] = true;
1372
1373                // Tell the instruction queue that a violation has occured.
1374                instQueue.violation(inst, violator);
1375
1376                // Squash.
1377                squashDueToMemOrder(inst,tid);
1378
1379                ++memOrderViolationEvents;
1380            } else if (ldstQueue.loadBlocked(tid) &&
1381                       !ldstQueue.isLoadBlockedHandled(tid)) {
1382                fetchRedirect[tid] = true;
1383
1384                DPRINTF(IEW, "Load operation couldn't execute because the "
1385                        "memory system is blocked.  PC: %s [sn:%lli]\n",
1386                        inst->pcState(), inst->seqNum);
1387
1388                squashDueToMemBlocked(inst, tid);
1389            }
1390        } else {
1391            // Reset any state associated with redirects that will not
1392            // be used.
1393            if (ldstQueue.violation(tid)) {
1394                assert(inst->isMemRef());
1395
1396                DynInstPtr violator = ldstQueue.getMemDepViolator(tid);
1397
1398                DPRINTF(IEW, "LDSTQ detected a violation.  Violator PC: "
1399                        "%s, inst PC: %s.  Addr is: %#x.\n",
1400                        violator->pcState(), inst->pcState(),
1401                        inst->physEffAddr);
1402                DPRINTF(IEW, "Violation will not be handled because "
1403                        "already squashing\n");
1404
1405                ++memOrderViolationEvents;
1406            }
1407            if (ldstQueue.loadBlocked(tid) &&
1408                !ldstQueue.isLoadBlockedHandled(tid)) {
1409                DPRINTF(IEW, "Load operation couldn't execute because the "
1410                        "memory system is blocked.  PC: %s [sn:%lli]\n",
1411                        inst->pcState(), inst->seqNum);
1412                DPRINTF(IEW, "Blocked load will not be handled because "
1413                        "already squashing\n");
1414
1415                ldstQueue.setLoadBlockedHandled(tid);
1416            }
1417
1418        }
1419    }
1420
1421    // Update and record activity if we processed any instructions.
1422    if (inst_num) {
1423        if (exeStatus == Idle) {
1424            exeStatus = Running;
1425        }
1426
1427        updatedQueues = true;
1428
1429        cpu->activityThisCycle();
1430    }
1431
1432    // Need to reset this in case a writeback event needs to write into the
1433    // iew queue.  That way the writeback event will write into the correct
1434    // spot in the queue.
1435    wbNumInst = 0;
1436
1437}
1438
1439template <class Impl>
1440void
1441DefaultIEW<Impl>::writebackInsts()
1442{
1443    // Loop through the head of the time buffer and wake any
1444    // dependents.  These instructions are about to write back.  Also
1445    // mark scoreboard that this instruction is finally complete.
1446    // Either have IEW have direct access to scoreboard, or have this
1447    // as part of backwards communication.
1448    for (int inst_num = 0; inst_num < wbWidth &&
1449             toCommit->insts[inst_num]; inst_num++) {
1450        DynInstPtr inst = toCommit->insts[inst_num];
1451        ThreadID tid = inst->threadNumber;
1452
1453        DPRINTF(IEW, "Sending instructions to commit, [sn:%lli] PC %s.\n",
1454                inst->seqNum, inst->pcState());
1455
1456        iewInstsToCommit[tid]++;
1457
1458        // Some instructions will be sent to commit without having
1459        // executed because they need commit to handle them.
1460        // E.g. Uncached loads have not actually executed when they
1461        // are first sent to commit.  Instead commit must tell the LSQ
1462        // when it's ready to execute the uncached load.
1463        if (!inst->isSquashed() && inst->isExecuted() && inst->getFault() == NoFault) {
1464            int dependents = instQueue.wakeDependents(inst);
1465
1466            for (int i = 0; i < inst->numDestRegs(); i++) {
1467                //mark as Ready
1468                DPRINTF(IEW,"Setting Destination Register %i\n",
1469                        inst->renamedDestRegIdx(i));
1470                scoreboard->setReg(inst->renamedDestRegIdx(i));
1471            }
1472
1473            if (dependents) {
1474                producerInst[tid]++;
1475                consumerInst[tid]+= dependents;
1476            }
1477            writebackCount[tid]++;
1478        }
1479
1480        decrWb(inst->seqNum);
1481    }
1482}
1483
1484template<class Impl>
1485void
1486DefaultIEW<Impl>::tick()
1487{
1488    wbNumInst = 0;
1489    wbCycle = 0;
1490
1491    wroteToTimeBuffer = false;
1492    updatedQueues = false;
1493
1494    sortInsts();
1495
1496    // Free function units marked as being freed this cycle.
1497    fuPool->processFreeUnits();
1498
1499    list<ThreadID>::iterator threads = activeThreads->begin();
1500    list<ThreadID>::iterator end = activeThreads->end();
1501
1502    // Check stall and squash signals, dispatch any instructions.
1503    while (threads != end) {
1504        ThreadID tid = *threads++;
1505
1506        DPRINTF(IEW,"Issue: Processing [tid:%i]\n",tid);
1507
1508        checkSignalsAndUpdate(tid);
1509        dispatch(tid);
1510    }
1511
1512    if (exeStatus != Squashing) {
1513        executeInsts();
1514
1515        writebackInsts();
1516
1517        // Have the instruction queue try to schedule any ready instructions.
1518        // (In actuality, this scheduling is for instructions that will
1519        // be executed next cycle.)
1520        instQueue.scheduleReadyInsts();
1521
1522        // Also should advance its own time buffers if the stage ran.
1523        // Not the best place for it, but this works (hopefully).
1524        issueToExecQueue.advance();
1525    }
1526
1527    bool broadcast_free_entries = false;
1528
1529    if (updatedQueues || exeStatus == Running || updateLSQNextCycle) {
1530        exeStatus = Idle;
1531        updateLSQNextCycle = false;
1532
1533        broadcast_free_entries = true;
1534    }
1535
1536    // Writeback any stores using any leftover bandwidth.
1537    ldstQueue.writebackStores();
1538
1539    // Check the committed load/store signals to see if there's a load
1540    // or store to commit.  Also check if it's being told to execute a
1541    // nonspeculative instruction.
1542    // This is pretty inefficient...
1543
1544    threads = activeThreads->begin();
1545    while (threads != end) {
1546        ThreadID tid = (*threads++);
1547
1548        DPRINTF(IEW,"Processing [tid:%i]\n",tid);
1549
1550        // Update structures based on instructions committed.
1551        if (fromCommit->commitInfo[tid].doneSeqNum != 0 &&
1552            !fromCommit->commitInfo[tid].squash &&
1553            !fromCommit->commitInfo[tid].robSquashing) {
1554
1555            ldstQueue.commitStores(fromCommit->commitInfo[tid].doneSeqNum,tid);
1556
1557            ldstQueue.commitLoads(fromCommit->commitInfo[tid].doneSeqNum,tid);
1558
1559            updateLSQNextCycle = true;
1560            instQueue.commit(fromCommit->commitInfo[tid].doneSeqNum,tid);
1561        }
1562
1563        if (fromCommit->commitInfo[tid].nonSpecSeqNum != 0) {
1564
1565            //DPRINTF(IEW,"NonspecInst from thread %i",tid);
1566            if (fromCommit->commitInfo[tid].uncached) {
1567                instQueue.replayMemInst(fromCommit->commitInfo[tid].uncachedLoad);
1568                fromCommit->commitInfo[tid].uncachedLoad->setAtCommit();
1569            } else {
1570                instQueue.scheduleNonSpec(
1571                    fromCommit->commitInfo[tid].nonSpecSeqNum);
1572            }
1573        }
1574
1575        if (broadcast_free_entries) {
1576            toFetch->iewInfo[tid].iqCount =
1577                instQueue.getCount(tid);
1578            toFetch->iewInfo[tid].ldstqCount =
1579                ldstQueue.getCount(tid);
1580
1581            toRename->iewInfo[tid].usedIQ = true;
1582            toRename->iewInfo[tid].freeIQEntries =
1583                instQueue.numFreeEntries();
1584            toRename->iewInfo[tid].usedLSQ = true;
1585            toRename->iewInfo[tid].freeLSQEntries =
1586                ldstQueue.numFreeEntries(tid);
1587
1588            wroteToTimeBuffer = true;
1589        }
1590
1591        DPRINTF(IEW, "[tid:%i], Dispatch dispatched %i instructions.\n",
1592                tid, toRename->iewInfo[tid].dispatched);
1593    }
1594
1595    DPRINTF(IEW, "IQ has %i free entries (Can schedule: %i).  "
1596            "LSQ has %i free entries.\n",
1597            instQueue.numFreeEntries(), instQueue.hasReadyInsts(),
1598            ldstQueue.numFreeEntries());
1599
1600    updateStatus();
1601
1602    if (wroteToTimeBuffer) {
1603        DPRINTF(Activity, "Activity this cycle.\n");
1604        cpu->activityThisCycle();
1605    }
1606}
1607
1608template <class Impl>
1609void
1610DefaultIEW<Impl>::updateExeInstStats(DynInstPtr &inst)
1611{
1612    ThreadID tid = inst->threadNumber;
1613
1614    //
1615    //  Pick off the software prefetches
1616    //
1617#ifdef TARGET_ALPHA
1618    if (inst->isDataPrefetch())
1619        iewExecutedSwp[tid]++;
1620    else
1621        iewIewExecutedcutedInsts++;
1622#else
1623    iewExecutedInsts++;
1624#endif
1625
1626#if TRACING_ON
1627    inst->completeTick = curTick();
1628#endif
1629
1630    //
1631    //  Control operations
1632    //
1633    if (inst->isControl())
1634        iewExecutedBranches[tid]++;
1635
1636    //
1637    //  Memory operations
1638    //
1639    if (inst->isMemRef()) {
1640        iewExecutedRefs[tid]++;
1641
1642        if (inst->isLoad()) {
1643            iewExecLoadInsts[tid]++;
1644        }
1645    }
1646}
1647
1648template <class Impl>
1649void
1650DefaultIEW<Impl>::checkMisprediction(DynInstPtr &inst)
1651{
1652    ThreadID tid = inst->threadNumber;
1653
1654    if (!fetchRedirect[tid] ||
1655        !toCommit->squash[tid] ||
1656        toCommit->squashedSeqNum[tid] > inst->seqNum) {
1657
1658        if (inst->mispredicted()) {
1659            fetchRedirect[tid] = true;
1660
1661            DPRINTF(IEW, "Execute: Branch mispredict detected.\n");
1662            DPRINTF(IEW, "Predicted target was PC:%#x, NPC:%#x.\n",
1663                    inst->predInstAddr(), inst->predNextInstAddr());
1664            DPRINTF(IEW, "Execute: Redirecting fetch to PC: %#x,"
1665                    " NPC: %#x.\n", inst->nextInstAddr(),
1666                    inst->nextInstAddr());
1667            // If incorrect, then signal the ROB that it must be squashed.
1668            squashDueToBranch(inst, tid);
1669
1670            if (inst->readPredTaken()) {
1671                predictedTakenIncorrect++;
1672            } else {
1673                predictedNotTakenIncorrect++;
1674            }
1675        }
1676    }
1677}
1678