2 * Copyright (c) 2010-2013, 2018 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Copyright (c) 2004-2006 The Regents of The University of Michigan
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #ifndef __CPU_O3_IEW_IMPL_IMPL_HH__
43 #define __CPU_O3_IEW_IMPL_IMPL_HH__
45 // @todo: Fix the instantaneous communication among all the stages within
46 // iew. There's a clear delay between issue and execute, yet backwards
47 // communication happens simultaneously.
51 #include "arch/utility.hh"
52 #include "config/the_isa.hh"
53 #include "cpu/checker/cpu.hh"
54 #include "cpu/o3/fu_pool.hh"
55 #include "cpu/o3/iew.hh"
56 #include "cpu/timebuf.hh"
57 #include "debug/Activity.hh"
58 #include "debug/Drain.hh"
59 #include "debug/IEW.hh"
60 #include "debug/O3PipeView.hh"
61 #include "params/DerivO3CPU.hh"
66 DefaultIEW<Impl>::DefaultIEW(O3CPU *_cpu, DerivO3CPUParams *params)
67 : issueToExecQueue(params->backComSize, params->forwardComSize),
69 instQueue(_cpu, this, params),
70 ldstQueue(_cpu, this, params),
71 fuPool(params->fuPool),
72 commitToIEWDelay(params->commitToIEWDelay),
73 renameToIEWDelay(params->renameToIEWDelay),
74 issueToExecuteDelay(params->issueToExecuteDelay),
75 dispatchWidth(params->dispatchWidth),
76 issueWidth(params->issueWidth),
79 wbWidth(params->wbWidth),
80 numThreads(params->numThreads)
82 if (dispatchWidth > Impl::MaxWidth)
83 fatal("dispatchWidth (%d) is larger than compiled limit (%d),\n"
84 "\tincrease MaxWidth in src/cpu/o3/impl.hh\n",
85 dispatchWidth, static_cast<int>(Impl::MaxWidth));
86 if (issueWidth > Impl::MaxWidth)
87 fatal("issueWidth (%d) is larger than compiled limit (%d),\n"
88 "\tincrease MaxWidth in src/cpu/o3/impl.hh\n",
89 issueWidth, static_cast<int>(Impl::MaxWidth));
90 if (wbWidth > Impl::MaxWidth)
91 fatal("wbWidth (%d) is larger than compiled limit (%d),\n"
92 "\tincrease MaxWidth in src/cpu/o3/impl.hh\n",
93 wbWidth, static_cast<int>(Impl::MaxWidth));
99 // Setup wire to read instructions coming from issue.
100 fromIssue = issueToExecQueue.getWire(-issueToExecuteDelay);
102 // Instruction queue needs the queue between issue and execute.
103 instQueue.setIssueToExecuteQueue(&issueToExecQueue);
105 for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
106 dispatchStatus[tid] = Running;
107 fetchRedirect[tid] = false;
110 updateLSQNextCycle = false;
112 skidBufferMax = (renameToIEWDelay + 1) * params->renameWidth;
115 template <class Impl>
117 DefaultIEW<Impl>::name() const
119 return cpu->name() + ".iew";
122 template <class Impl>
124 DefaultIEW<Impl>::regProbePoints()
126 ppDispatch = new ProbePointArg<DynInstPtr>(cpu->getProbeManager(), "Dispatch");
127 ppMispredict = new ProbePointArg<DynInstPtr>(cpu->getProbeManager(), "Mispredict");
129 * Probe point with dynamic instruction as the argument used to probe when
130 * an instruction starts to execute.
132 ppExecute = new ProbePointArg<DynInstPtr>(cpu->getProbeManager(),
135 * Probe point with dynamic instruction as the argument used to probe when
136 * an instruction execution completes and it is marked ready to commit.
138 ppToCommit = new ProbePointArg<DynInstPtr>(cpu->getProbeManager(),
142 template <class Impl>
144 DefaultIEW<Impl>::regStats()
146 using namespace Stats;
148 instQueue.regStats();
149 ldstQueue.regStats();
152 .name(name() + ".iewIdleCycles")
153 .desc("Number of cycles IEW is idle");
156 .name(name() + ".iewSquashCycles")
157 .desc("Number of cycles IEW is squashing");
160 .name(name() + ".iewBlockCycles")
161 .desc("Number of cycles IEW is blocking");
164 .name(name() + ".iewUnblockCycles")
165 .desc("Number of cycles IEW is unblocking");
168 .name(name() + ".iewDispatchedInsts")
169 .desc("Number of instructions dispatched to IQ");
172 .name(name() + ".iewDispSquashedInsts")
173 .desc("Number of squashed instructions skipped by dispatch");
176 .name(name() + ".iewDispLoadInsts")
177 .desc("Number of dispatched load instructions");
180 .name(name() + ".iewDispStoreInsts")
181 .desc("Number of dispatched store instructions");
184 .name(name() + ".iewDispNonSpecInsts")
185 .desc("Number of dispatched non-speculative instructions");
188 .name(name() + ".iewIQFullEvents")
189 .desc("Number of times the IQ has become full, causing a stall");
192 .name(name() + ".iewLSQFullEvents")
193 .desc("Number of times the LSQ has become full, causing a stall");
195 memOrderViolationEvents
196 .name(name() + ".memOrderViolationEvents")
197 .desc("Number of memory order violations");
199 predictedTakenIncorrect
200 .name(name() + ".predictedTakenIncorrect")
201 .desc("Number of branches that were predicted taken incorrectly");
203 predictedNotTakenIncorrect
204 .name(name() + ".predictedNotTakenIncorrect")
205 .desc("Number of branches that were predicted not taken incorrectly");
208 .name(name() + ".branchMispredicts")
209 .desc("Number of branch mispredicts detected at execute");
211 branchMispredicts = predictedTakenIncorrect + predictedNotTakenIncorrect;
214 .name(name() + ".iewExecutedInsts")
215 .desc("Number of executed instructions");
218 .init(cpu->numThreads)
219 .name(name() + ".iewExecLoadInsts")
220 .desc("Number of load instructions executed")
224 .name(name() + ".iewExecSquashedInsts")
225 .desc("Number of squashed instructions skipped in execute");
228 .init(cpu->numThreads)
229 .name(name() + ".exec_swp")
230 .desc("number of swp insts executed")
234 .init(cpu->numThreads)
235 .name(name() + ".exec_nop")
236 .desc("number of nop insts executed")
240 .init(cpu->numThreads)
241 .name(name() + ".exec_refs")
242 .desc("number of memory reference insts executed")
246 .init(cpu->numThreads)
247 .name(name() + ".exec_branches")
248 .desc("Number of branches executed")
252 .name(name() + ".exec_stores")
253 .desc("Number of stores executed")
255 iewExecStoreInsts = iewExecutedRefs - iewExecLoadInsts;
258 .name(name() + ".exec_rate")
259 .desc("Inst execution rate")
262 iewExecRate = iewExecutedInsts / cpu->numCycles;
265 .init(cpu->numThreads)
266 .name(name() + ".wb_sent")
267 .desc("cumulative count of insts sent to commit")
271 .init(cpu->numThreads)
272 .name(name() + ".wb_count")
273 .desc("cumulative count of insts written-back")
277 .init(cpu->numThreads)
278 .name(name() + ".wb_producers")
279 .desc("num instructions producing a value")
283 .init(cpu->numThreads)
284 .name(name() + ".wb_consumers")
285 .desc("num instructions consuming a value")
289 .name(name() + ".wb_fanout")
290 .desc("average fanout of values written-back")
293 wbFanout = producerInst / consumerInst;
296 .name(name() + ".wb_rate")
297 .desc("insts written-back per cycle")
299 wbRate = writebackCount / cpu->numCycles;
304 DefaultIEW<Impl>::startupStage()
306 for (ThreadID tid = 0; tid < numThreads; tid++) {
307 toRename->iewInfo[tid].usedIQ = true;
308 toRename->iewInfo[tid].freeIQEntries =
309 instQueue.numFreeEntries(tid);
311 toRename->iewInfo[tid].usedLSQ = true;
312 toRename->iewInfo[tid].freeLQEntries = ldstQueue.numFreeLoadEntries(tid);
313 toRename->iewInfo[tid].freeSQEntries = ldstQueue.numFreeStoreEntries(tid);
316 // Initialize the checker's dcache port here
318 cpu->checker->setDcachePort(&ldstQueue.getDataPort());
321 cpu->activateStage(O3CPU::IEWIdx);
326 DefaultIEW<Impl>::clearStates(ThreadID tid)
328 toRename->iewInfo[tid].usedIQ = true;
329 toRename->iewInfo[tid].freeIQEntries =
330 instQueue.numFreeEntries(tid);
332 toRename->iewInfo[tid].usedLSQ = true;
333 toRename->iewInfo[tid].freeLQEntries = ldstQueue.numFreeLoadEntries(tid);
334 toRename->iewInfo[tid].freeSQEntries = ldstQueue.numFreeStoreEntries(tid);
339 DefaultIEW<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
343 // Setup wire to read information from time buffer, from commit.
344 fromCommit = timeBuffer->getWire(-commitToIEWDelay);
346 // Setup wire to write information back to previous stages.
347 toRename = timeBuffer->getWire(0);
349 toFetch = timeBuffer->getWire(0);
351 // Instruction queue also needs main time buffer.
352 instQueue.setTimeBuffer(tb_ptr);
357 DefaultIEW<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
359 renameQueue = rq_ptr;
361 // Setup wire to read information from rename queue.
362 fromRename = renameQueue->getWire(-renameToIEWDelay);
367 DefaultIEW<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
371 // Setup wire to write instructions to commit.
372 toCommit = iewQueue->getWire(0);
377 DefaultIEW<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
379 activeThreads = at_ptr;
381 ldstQueue.setActiveThreads(at_ptr);
382 instQueue.setActiveThreads(at_ptr);
387 DefaultIEW<Impl>::setScoreboard(Scoreboard *sb_ptr)
392 template <class Impl>
394 DefaultIEW<Impl>::isDrained() const
396 bool drained = ldstQueue.isDrained() && instQueue.isDrained();
398 for (ThreadID tid = 0; tid < numThreads; tid++) {
399 if (!insts[tid].empty()) {
400 DPRINTF(Drain, "%i: Insts not empty.\n", tid);
403 if (!skidBuffer[tid].empty()) {
404 DPRINTF(Drain, "%i: Skid buffer not empty.\n", tid);
407 drained = drained && dispatchStatus[tid] == Running;
410 // Also check the FU pool as instructions are "stored" in FU
411 // completion events until they are done and not accounted for
413 if (drained && !fuPool->isDrained()) {
414 DPRINTF(Drain, "FU pool still busy.\n");
421 template <class Impl>
423 DefaultIEW<Impl>::drainSanityCheck() const
427 instQueue.drainSanityCheck();
428 ldstQueue.drainSanityCheck();
431 template <class Impl>
433 DefaultIEW<Impl>::takeOverFrom()
440 instQueue.takeOverFrom();
441 ldstQueue.takeOverFrom();
442 fuPool->takeOverFrom();
445 cpu->activityThisCycle();
447 for (ThreadID tid = 0; tid < numThreads; tid++) {
448 dispatchStatus[tid] = Running;
449 fetchRedirect[tid] = false;
452 updateLSQNextCycle = false;
454 for (int i = 0; i < issueToExecQueue.getSize(); ++i) {
455 issueToExecQueue.advance();
461 DefaultIEW<Impl>::squash(ThreadID tid)
463 DPRINTF(IEW, "[tid:%i] Squashing all instructions.\n", tid);
465 // Tell the IQ to start squashing.
466 instQueue.squash(tid);
468 // Tell the LDSTQ to start squashing.
469 ldstQueue.squash(fromCommit->commitInfo[tid].doneSeqNum, tid);
470 updatedQueues = true;
472 // Clear the skid buffer in case it has any data in it.
474 "Removing skidbuffer instructions until "
475 "[sn:%llu] [tid:%i]\n",
476 fromCommit->commitInfo[tid].doneSeqNum, tid);
478 while (!skidBuffer[tid].empty()) {
479 if (skidBuffer[tid].front()->isLoad()) {
480 toRename->iewInfo[tid].dispatchedToLQ++;
482 if (skidBuffer[tid].front()->isStore() ||
483 skidBuffer[tid].front()->isAtomic()) {
484 toRename->iewInfo[tid].dispatchedToSQ++;
487 toRename->iewInfo[tid].dispatched++;
489 skidBuffer[tid].pop();
492 emptyRenameInsts(tid);
497 DefaultIEW<Impl>::squashDueToBranch(const DynInstPtr& inst, ThreadID tid)
499 DPRINTF(IEW, "[tid:%i] [sn:%llu] Squashing from a specific instruction,"
501 "\n", tid, inst->seqNum, inst->pcState() );
503 if (!toCommit->squash[tid] ||
504 inst->seqNum < toCommit->squashedSeqNum[tid]) {
505 toCommit->squash[tid] = true;
506 toCommit->squashedSeqNum[tid] = inst->seqNum;
507 toCommit->branchTaken[tid] = inst->pcState().branching();
509 TheISA::PCState pc = inst->pcState();
510 TheISA::advancePC(pc, inst->staticInst);
512 toCommit->pc[tid] = pc;
513 toCommit->mispredictInst[tid] = inst;
514 toCommit->includeSquashInst[tid] = false;
516 wroteToTimeBuffer = true;
523 DefaultIEW<Impl>::squashDueToMemOrder(const DynInstPtr& inst, ThreadID tid)
525 DPRINTF(IEW, "[tid:%i] Memory violation, squashing violator and younger "
526 "insts, PC: %s [sn:%llu].\n", tid, inst->pcState(), inst->seqNum);
527 // Need to include inst->seqNum in the following comparison to cover the
528 // corner case when a branch misprediction and a memory violation for the
529 // same instruction (e.g. load PC) are detected in the same cycle. In this
530 // case the memory violator should take precedence over the branch
531 // misprediction because it requires the violator itself to be included in
533 if (!toCommit->squash[tid] ||
534 inst->seqNum <= toCommit->squashedSeqNum[tid]) {
535 toCommit->squash[tid] = true;
537 toCommit->squashedSeqNum[tid] = inst->seqNum;
538 toCommit->pc[tid] = inst->pcState();
539 toCommit->mispredictInst[tid] = NULL;
541 // Must include the memory violator in the squash.
542 toCommit->includeSquashInst[tid] = true;
544 wroteToTimeBuffer = true;
550 DefaultIEW<Impl>::block(ThreadID tid)
552 DPRINTF(IEW, "[tid:%i] Blocking.\n", tid);
554 if (dispatchStatus[tid] != Blocked &&
555 dispatchStatus[tid] != Unblocking) {
556 toRename->iewBlock[tid] = true;
557 wroteToTimeBuffer = true;
560 // Add the current inputs to the skid buffer so they can be
561 // reprocessed when this stage unblocks.
564 dispatchStatus[tid] = Blocked;
569 DefaultIEW<Impl>::unblock(ThreadID tid)
571 DPRINTF(IEW, "[tid:%i] Reading instructions out of the skid "
572 "buffer %u.\n",tid, tid);
574 // If the skid bufffer is empty, signal back to previous stages to unblock.
575 // Also switch status to running.
576 if (skidBuffer[tid].empty()) {
577 toRename->iewUnblock[tid] = true;
578 wroteToTimeBuffer = true;
579 DPRINTF(IEW, "[tid:%i] Done unblocking.\n",tid);
580 dispatchStatus[tid] = Running;
586 DefaultIEW<Impl>::wakeDependents(const DynInstPtr& inst)
588 instQueue.wakeDependents(inst);
593 DefaultIEW<Impl>::rescheduleMemInst(const DynInstPtr& inst)
595 instQueue.rescheduleMemInst(inst);
600 DefaultIEW<Impl>::replayMemInst(const DynInstPtr& inst)
602 instQueue.replayMemInst(inst);
607 DefaultIEW<Impl>::blockMemInst(const DynInstPtr& inst)
609 instQueue.blockMemInst(inst);
614 DefaultIEW<Impl>::cacheUnblocked()
616 instQueue.cacheUnblocked();
621 DefaultIEW<Impl>::instToCommit(const DynInstPtr& inst)
623 // This function should not be called after writebackInsts in a
624 // single cycle. That will cause problems with an instruction
625 // being added to the queue to commit without being processed by
626 // writebackInsts prior to being sent to commit.
628 // First check the time slot that this instruction will write
629 // to. If there are free write ports at the time, then go ahead
630 // and write the instruction to that time. If there are not,
631 // keep looking back to see where's the first time there's a
633 while ((*iewQueue)[wbCycle].insts[wbNumInst]) {
635 if (wbNumInst == wbWidth) {
641 DPRINTF(IEW, "Current wb cycle: %i, width: %i, numInst: %i\nwbActual:%i\n",
642 wbCycle, wbWidth, wbNumInst, wbCycle * wbWidth + wbNumInst);
643 // Add finished instruction to queue to commit.
644 (*iewQueue)[wbCycle].insts[wbNumInst] = inst;
645 (*iewQueue)[wbCycle].size++;
648 template <class Impl>
650 DefaultIEW<Impl>::validInstsFromRename()
652 unsigned inst_count = 0;
654 for (int i=0; i<fromRename->size; i++) {
655 if (!fromRename->insts[i]->isSquashed())
664 DefaultIEW<Impl>::skidInsert(ThreadID tid)
666 DynInstPtr inst = NULL;
668 while (!insts[tid].empty()) {
669 inst = insts[tid].front();
673 DPRINTF(IEW,"[tid:%i] Inserting [sn:%lli] PC:%s into "
674 "dispatch skidBuffer %i\n",tid, inst->seqNum,
675 inst->pcState(),tid);
677 skidBuffer[tid].push(inst);
680 assert(skidBuffer[tid].size() <= skidBufferMax &&
681 "Skidbuffer Exceeded Max Size");
686 DefaultIEW<Impl>::skidCount()
690 list<ThreadID>::iterator threads = activeThreads->begin();
691 list<ThreadID>::iterator end = activeThreads->end();
693 while (threads != end) {
694 ThreadID tid = *threads++;
695 unsigned thread_count = skidBuffer[tid].size();
696 if (max < thread_count)
705 DefaultIEW<Impl>::skidsEmpty()
707 list<ThreadID>::iterator threads = activeThreads->begin();
708 list<ThreadID>::iterator end = activeThreads->end();
710 while (threads != end) {
711 ThreadID tid = *threads++;
713 if (!skidBuffer[tid].empty())
720 template <class Impl>
722 DefaultIEW<Impl>::updateStatus()
724 bool any_unblocking = false;
726 list<ThreadID>::iterator threads = activeThreads->begin();
727 list<ThreadID>::iterator end = activeThreads->end();
729 while (threads != end) {
730 ThreadID tid = *threads++;
732 if (dispatchStatus[tid] == Unblocking) {
733 any_unblocking = true;
738 // If there are no ready instructions waiting to be scheduled by the IQ,
739 // and there's no stores waiting to write back, and dispatch is not
740 // unblocking, then there is no internal activity for the IEW stage.
741 instQueue.intInstQueueReads++;
742 if (_status == Active && !instQueue.hasReadyInsts() &&
743 !ldstQueue.willWB() && !any_unblocking) {
744 DPRINTF(IEW, "IEW switching to idle\n");
749 } else if (_status == Inactive && (instQueue.hasReadyInsts() ||
750 ldstQueue.willWB() ||
752 // Otherwise there is internal activity. Set to active.
753 DPRINTF(IEW, "IEW switching to active\n");
761 template <class Impl>
763 DefaultIEW<Impl>::checkStall(ThreadID tid)
767 if (fromCommit->commitInfo[tid].robSquashing) {
768 DPRINTF(IEW,"[tid:%i] Stall from Commit stage detected.\n",tid);
770 } else if (instQueue.isFull(tid)) {
771 DPRINTF(IEW,"[tid:%i] Stall: IQ is full.\n",tid);
778 template <class Impl>
780 DefaultIEW<Impl>::checkSignalsAndUpdate(ThreadID tid)
782 // Check if there's a squash signal, squash if there is
783 // Check stall signals, block if there is.
784 // If status was Blocked
785 // if so then go to unblocking
786 // If status was Squashing
787 // check if squashing is not high. Switch to running this cycle.
789 if (fromCommit->commitInfo[tid].squash) {
792 if (dispatchStatus[tid] == Blocked ||
793 dispatchStatus[tid] == Unblocking) {
794 toRename->iewUnblock[tid] = true;
795 wroteToTimeBuffer = true;
798 dispatchStatus[tid] = Squashing;
799 fetchRedirect[tid] = false;
803 if (fromCommit->commitInfo[tid].robSquashing) {
804 DPRINTF(IEW, "[tid:%i] ROB is still squashing.\n", tid);
806 dispatchStatus[tid] = Squashing;
807 emptyRenameInsts(tid);
808 wroteToTimeBuffer = true;
811 if (checkStall(tid)) {
813 dispatchStatus[tid] = Blocked;
817 if (dispatchStatus[tid] == Blocked) {
818 // Status from previous cycle was blocked, but there are no more stall
819 // conditions. Switch over to unblocking.
820 DPRINTF(IEW, "[tid:%i] Done blocking, switching to unblocking.\n",
823 dispatchStatus[tid] = Unblocking;
830 if (dispatchStatus[tid] == Squashing) {
831 // Switch status to running if rename isn't being told to block or
832 // squash this cycle.
833 DPRINTF(IEW, "[tid:%i] Done squashing, switching to running.\n",
836 dispatchStatus[tid] = Running;
842 template <class Impl>
844 DefaultIEW<Impl>::sortInsts()
846 int insts_from_rename = fromRename->size;
848 for (ThreadID tid = 0; tid < numThreads; tid++)
849 assert(insts[tid].empty());
851 for (int i = 0; i < insts_from_rename; ++i) {
852 insts[fromRename->insts[i]->threadNumber].push(fromRename->insts[i]);
856 template <class Impl>
858 DefaultIEW<Impl>::emptyRenameInsts(ThreadID tid)
860 DPRINTF(IEW, "[tid:%i] Removing incoming rename instructions\n", tid);
862 while (!insts[tid].empty()) {
864 if (insts[tid].front()->isLoad()) {
865 toRename->iewInfo[tid].dispatchedToLQ++;
867 if (insts[tid].front()->isStore() ||
868 insts[tid].front()->isAtomic()) {
869 toRename->iewInfo[tid].dispatchedToSQ++;
872 toRename->iewInfo[tid].dispatched++;
878 template <class Impl>
880 DefaultIEW<Impl>::wakeCPU()
885 template <class Impl>
887 DefaultIEW<Impl>::activityThisCycle()
889 DPRINTF(Activity, "Activity this cycle.\n");
890 cpu->activityThisCycle();
893 template <class Impl>
895 DefaultIEW<Impl>::activateStage()
897 DPRINTF(Activity, "Activating stage.\n");
898 cpu->activateStage(O3CPU::IEWIdx);
901 template <class Impl>
903 DefaultIEW<Impl>::deactivateStage()
905 DPRINTF(Activity, "Deactivating stage.\n");
906 cpu->deactivateStage(O3CPU::IEWIdx);
911 DefaultIEW<Impl>::dispatch(ThreadID tid)
913 // If status is Running or idle,
914 // call dispatchInsts()
915 // If status is Unblocking,
916 // buffer any instructions coming from rename
917 // continue trying to empty skid buffer
918 // check if stall conditions have passed
920 if (dispatchStatus[tid] == Blocked) {
923 } else if (dispatchStatus[tid] == Squashing) {
927 // Dispatch should try to dispatch as many instructions as its bandwidth
928 // will allow, as long as it is not currently blocked.
929 if (dispatchStatus[tid] == Running ||
930 dispatchStatus[tid] == Idle) {
931 DPRINTF(IEW, "[tid:%i] Not blocked, so attempting to run "
935 } else if (dispatchStatus[tid] == Unblocking) {
936 // Make sure that the skid buffer has something in it if the
937 // status is unblocking.
938 assert(!skidsEmpty());
940 // If the status was unblocking, then instructions from the skid
941 // buffer were used. Remove those instructions and handle
942 // the rest of unblocking.
947 if (validInstsFromRename()) {
948 // Add the current inputs to the skid buffer so they can be
949 // reprocessed when this stage unblocks.
957 template <class Impl>
959 DefaultIEW<Impl>::dispatchInsts(ThreadID tid)
961 // Obtain instructions from skid buffer if unblocking, or queue from rename
963 std::queue<DynInstPtr> &insts_to_dispatch =
964 dispatchStatus[tid] == Unblocking ?
965 skidBuffer[tid] : insts[tid];
967 int insts_to_add = insts_to_dispatch.size();
970 bool add_to_iq = false;
971 int dis_num_inst = 0;
973 // Loop through the instructions, putting them in the instruction
975 for ( ; dis_num_inst < insts_to_add &&
976 dis_num_inst < dispatchWidth;
979 inst = insts_to_dispatch.front();
981 if (dispatchStatus[tid] == Unblocking) {
982 DPRINTF(IEW, "[tid:%i] Issue: Examining instruction from skid "
986 // Make sure there's a valid instruction there.
989 DPRINTF(IEW, "[tid:%i] Issue: Adding PC %s [sn:%lli] [tid:%i] to "
991 tid, inst->pcState(), inst->seqNum, inst->threadNumber);
993 // Be sure to mark these instructions as ready so that the
994 // commit stage can go ahead and execute them, and mark
995 // them as issued so the IQ doesn't reprocess them.
997 // Check for squashed instructions.
998 if (inst->isSquashed()) {
999 DPRINTF(IEW, "[tid:%i] Issue: Squashed instruction encountered, "
1000 "not adding to IQ.\n", tid);
1002 ++iewDispSquashedInsts;
1004 insts_to_dispatch.pop();
1006 //Tell Rename That An Instruction has been processed
1007 if (inst->isLoad()) {
1008 toRename->iewInfo[tid].dispatchedToLQ++;
1010 if (inst->isStore() || inst->isAtomic()) {
1011 toRename->iewInfo[tid].dispatchedToSQ++;
1014 toRename->iewInfo[tid].dispatched++;
1019 // Check for full conditions.
1020 if (instQueue.isFull(tid)) {
1021 DPRINTF(IEW, "[tid:%i] Issue: IQ has become full.\n", tid);
1023 // Call function to start blocking.
1026 // Set unblock to false. Special case where we are using
1027 // skidbuffer (unblocking) instructions but then we still
1028 // get full in the IQ.
1029 toRename->iewUnblock[tid] = false;
1035 // Check LSQ if inst is LD/ST
1036 if ((inst->isAtomic() && ldstQueue.sqFull(tid)) ||
1037 (inst->isLoad() && ldstQueue.lqFull(tid)) ||
1038 (inst->isStore() && ldstQueue.sqFull(tid))) {
1039 DPRINTF(IEW, "[tid:%i] Issue: %s has become full.\n",tid,
1040 inst->isLoad() ? "LQ" : "SQ");
1042 // Call function to start blocking.
1045 // Set unblock to false. Special case where we are using
1046 // skidbuffer (unblocking) instructions but then we still
1047 // get full in the IQ.
1048 toRename->iewUnblock[tid] = false;
1054 // Otherwise issue the instruction just fine.
1055 if (inst->isAtomic()) {
1056 DPRINTF(IEW, "[tid:%i] Issue: Memory instruction "
1057 "encountered, adding to LSQ.\n", tid);
1059 ldstQueue.insertStore(inst);
1061 ++iewDispStoreInsts;
1063 // AMOs need to be set as "canCommit()"
1064 // so that commit can process them when they reach the
1066 inst->setCanCommit();
1067 instQueue.insertNonSpec(inst);
1070 ++iewDispNonSpecInsts;
1072 toRename->iewInfo[tid].dispatchedToSQ++;
1073 } else if (inst->isLoad()) {
1074 DPRINTF(IEW, "[tid:%i] Issue: Memory instruction "
1075 "encountered, adding to LSQ.\n", tid);
1077 // Reserve a spot in the load store queue for this
1079 ldstQueue.insertLoad(inst);
1085 toRename->iewInfo[tid].dispatchedToLQ++;
1086 } else if (inst->isStore()) {
1087 DPRINTF(IEW, "[tid:%i] Issue: Memory instruction "
1088 "encountered, adding to LSQ.\n", tid);
1090 ldstQueue.insertStore(inst);
1092 ++iewDispStoreInsts;
1094 if (inst->isStoreConditional()) {
1095 // Store conditionals need to be set as "canCommit()"
1096 // so that commit can process them when they reach the
1098 // @todo: This is somewhat specific to Alpha.
1099 inst->setCanCommit();
1100 instQueue.insertNonSpec(inst);
1103 ++iewDispNonSpecInsts;
1108 toRename->iewInfo[tid].dispatchedToSQ++;
1109 } else if (inst->isMemBarrier() || inst->isWriteBarrier()) {
1110 // Same as non-speculative stores.
1111 inst->setCanCommit();
1112 instQueue.insertBarrier(inst);
1114 } else if (inst->isNop()) {
1115 DPRINTF(IEW, "[tid:%i] Issue: Nop instruction encountered, "
1116 "skipping.\n", tid);
1119 inst->setExecuted();
1120 inst->setCanCommit();
1122 instQueue.recordProducer(inst);
1124 iewExecutedNop[tid]++;
1128 assert(!inst->isExecuted());
1132 if (add_to_iq && inst->isNonSpeculative()) {
1133 DPRINTF(IEW, "[tid:%i] Issue: Nonspeculative instruction "
1134 "encountered, skipping.\n", tid);
1136 // Same as non-speculative stores.
1137 inst->setCanCommit();
1139 // Specifically insert it as nonspeculative.
1140 instQueue.insertNonSpec(inst);
1142 ++iewDispNonSpecInsts;
1147 // If the instruction queue is not full, then add the
1150 instQueue.insert(inst);
1153 insts_to_dispatch.pop();
1155 toRename->iewInfo[tid].dispatched++;
1157 ++iewDispatchedInsts;
1160 inst->dispatchTick = curTick() - inst->fetchTick;
1162 ppDispatch->notify(inst);
1165 if (!insts_to_dispatch.empty()) {
1166 DPRINTF(IEW,"[tid:%i] Issue: Bandwidth Full. Blocking.\n", tid);
1168 toRename->iewUnblock[tid] = false;
1171 if (dispatchStatus[tid] == Idle && dis_num_inst) {
1172 dispatchStatus[tid] = Running;
1174 updatedQueues = true;
1180 template <class Impl>
1182 DefaultIEW<Impl>::printAvailableInsts()
1186 std::cout << "Available Instructions: ";
1188 while (fromIssue->insts[inst]) {
1190 if (inst%3==0) std::cout << "\n\t";
1192 std::cout << "PC: " << fromIssue->insts[inst]->pcState()
1193 << " TN: " << fromIssue->insts[inst]->threadNumber
1194 << " SN: " << fromIssue->insts[inst]->seqNum << " | ";
1203 template <class Impl>
1205 DefaultIEW<Impl>::executeInsts()
1210 list<ThreadID>::iterator threads = activeThreads->begin();
1211 list<ThreadID>::iterator end = activeThreads->end();
1213 while (threads != end) {
1214 ThreadID tid = *threads++;
1215 fetchRedirect[tid] = false;
1218 // Uncomment this if you want to see all available instructions.
1219 // @todo This doesn't actually work anymore, we should fix it.
1220 // printAvailableInsts();
1222 // Execute/writeback any instructions that are available.
1223 int insts_to_execute = fromIssue->size;
1225 for (; inst_num < insts_to_execute;
1228 DPRINTF(IEW, "Execute: Executing instructions from IQ.\n");
1230 DynInstPtr inst = instQueue.getInstToExecute();
1232 DPRINTF(IEW, "Execute: Processing PC %s, [tid:%i] [sn:%llu].\n",
1233 inst->pcState(), inst->threadNumber,inst->seqNum);
1235 // Notify potential listeners that this instruction has started
1237 ppExecute->notify(inst);
1239 // Check if the instruction is squashed; if so then skip it
1240 if (inst->isSquashed()) {
1241 DPRINTF(IEW, "Execute: Instruction was squashed. PC: %s, [tid:%i]"
1242 " [sn:%llu]\n", inst->pcState(), inst->threadNumber,
1245 // Consider this instruction executed so that commit can go
1246 // ahead and retire the instruction.
1247 inst->setExecuted();
1249 // Not sure if I should set this here or just let commit try to
1250 // commit any squashed instructions. I like the latter a bit more.
1251 inst->setCanCommit();
1253 ++iewExecSquashedInsts;
1258 Fault fault = NoFault;
1260 // Execute instruction.
1261 // Note that if the instruction faults, it will be handled
1262 // at the commit stage.
1263 if (inst->isMemRef()) {
1264 DPRINTF(IEW, "Execute: Calculating address for memory "
1267 // Tell the LDSTQ to execute this instruction (if it is a load).
1268 if (inst->isAtomic()) {
1269 // AMOs are treated like store requests
1270 fault = ldstQueue.executeStore(inst);
1272 if (inst->isTranslationDelayed() &&
1274 // A hw page table walk is currently going on; the
1275 // instruction must be deferred.
1276 DPRINTF(IEW, "Execute: Delayed translation, deferring "
1278 instQueue.deferMemInst(inst);
1281 } else if (inst->isLoad()) {
1282 // Loads will mark themselves as executed, and their writeback
1283 // event adds the instruction to the queue to commit
1284 fault = ldstQueue.executeLoad(inst);
1286 if (inst->isTranslationDelayed() &&
1288 // A hw page table walk is currently going on; the
1289 // instruction must be deferred.
1290 DPRINTF(IEW, "Execute: Delayed translation, deferring "
1292 instQueue.deferMemInst(inst);
1296 if (inst->isDataPrefetch() || inst->isInstPrefetch()) {
1297 inst->fault = NoFault;
1299 } else if (inst->isStore()) {
1300 fault = ldstQueue.executeStore(inst);
1302 if (inst->isTranslationDelayed() &&
1304 // A hw page table walk is currently going on; the
1305 // instruction must be deferred.
1306 DPRINTF(IEW, "Execute: Delayed translation, deferring "
1308 instQueue.deferMemInst(inst);
1312 // If the store had a fault then it may not have a mem req
1313 if (fault != NoFault || !inst->readPredicate() ||
1314 !inst->isStoreConditional()) {
1315 // If the instruction faulted, then we need to send it along
1316 // to commit without the instruction completing.
1317 // Send this instruction to commit, also make sure iew stage
1318 // realizes there is activity.
1319 inst->setExecuted();
1321 activityThisCycle();
1324 // Store conditionals will mark themselves as
1325 // executed, and their writeback event will add the
1326 // instruction to the queue to commit.
1328 panic("Unexpected memory type!\n");
1332 // If the instruction has already faulted, then skip executing it.
1333 // Such case can happen when it faulted during ITLB translation.
1334 // If we execute the instruction (even if it's a nop) the fault
1335 // will be replaced and we will lose it.
1336 if (inst->getFault() == NoFault) {
1338 if (!inst->readPredicate())
1339 inst->forwardOldRegs();
1342 inst->setExecuted();
1347 updateExeInstStats(inst);
1349 // Check if branch prediction was correct, if not then we need
1350 // to tell commit to squash in flight instructions. Only
1351 // handle this if there hasn't already been something that
1352 // redirects fetch in this group of instructions.
1354 // This probably needs to prioritize the redirects if a different
1355 // scheduler is used. Currently the scheduler schedules the oldest
1356 // instruction first, so the branch resolution order will be correct.
1357 ThreadID tid = inst->threadNumber;
1359 if (!fetchRedirect[tid] ||
1360 !toCommit->squash[tid] ||
1361 toCommit->squashedSeqNum[tid] > inst->seqNum) {
1363 // Prevent testing for misprediction on load instructions,
1364 // that have not been executed.
1365 bool loadNotExecuted = !inst->isExecuted() && inst->isLoad();
1367 if (inst->mispredicted() && !loadNotExecuted) {
1368 fetchRedirect[tid] = true;
1370 DPRINTF(IEW, "[tid:%i] [sn:%llu] Execute: "
1371 "Branch mispredict detected.\n",
1373 DPRINTF(IEW, "[tid:%i] [sn:%llu] "
1374 "Predicted target was PC: %s\n",
1375 tid,inst->seqNum,inst->readPredTarg());
1376 DPRINTF(IEW, "[tid:%i] [sn:%llu] Execute: "
1377 "Redirecting fetch to PC: %s\n",
1378 tid,inst->seqNum,inst->pcState());
1379 // If incorrect, then signal the ROB that it must be squashed.
1380 squashDueToBranch(inst, tid);
1382 ppMispredict->notify(inst);
1384 if (inst->readPredTaken()) {
1385 predictedTakenIncorrect++;
1387 predictedNotTakenIncorrect++;
1389 } else if (ldstQueue.violation(tid)) {
1390 assert(inst->isMemRef());
1391 // If there was an ordering violation, then get the
1392 // DynInst that caused the violation. Note that this
1393 // clears the violation signal.
1394 DynInstPtr violator;
1395 violator = ldstQueue.getMemDepViolator(tid);
1397 DPRINTF(IEW, "LDSTQ detected a violation. Violator PC: %s "
1398 "[sn:%lli], inst PC: %s [sn:%lli]. Addr is: %#x.\n",
1399 violator->pcState(), violator->seqNum,
1400 inst->pcState(), inst->seqNum, inst->physEffAddr);
1402 fetchRedirect[tid] = true;
1404 // Tell the instruction queue that a violation has occured.
1405 instQueue.violation(inst, violator);
1408 squashDueToMemOrder(violator, tid);
1410 ++memOrderViolationEvents;
1413 // Reset any state associated with redirects that will not
1415 if (ldstQueue.violation(tid)) {
1416 assert(inst->isMemRef());
1418 DynInstPtr violator = ldstQueue.getMemDepViolator(tid);
1420 DPRINTF(IEW, "LDSTQ detected a violation. Violator PC: "
1421 "%s, inst PC: %s. Addr is: %#x.\n",
1422 violator->pcState(), inst->pcState(),
1424 DPRINTF(IEW, "Violation will not be handled because "
1425 "already squashing\n");
1427 ++memOrderViolationEvents;
1432 // Update and record activity if we processed any instructions.
1434 if (exeStatus == Idle) {
1435 exeStatus = Running;
1438 updatedQueues = true;
1440 cpu->activityThisCycle();
1443 // Need to reset this in case a writeback event needs to write into the
1444 // iew queue. That way the writeback event will write into the correct
1445 // spot in the queue.
1450 template <class Impl>
1452 DefaultIEW<Impl>::writebackInsts()
1454 // Loop through the head of the time buffer and wake any
1455 // dependents. These instructions are about to write back. Also
1456 // mark scoreboard that this instruction is finally complete.
1457 // Either have IEW have direct access to scoreboard, or have this
1458 // as part of backwards communication.
1459 for (int inst_num = 0; inst_num < wbWidth &&
1460 toCommit->insts[inst_num]; inst_num++) {
1461 DynInstPtr inst = toCommit->insts[inst_num];
1462 ThreadID tid = inst->threadNumber;
1464 DPRINTF(IEW, "Sending instructions to commit, [sn:%lli] PC %s.\n",
1465 inst->seqNum, inst->pcState());
1467 iewInstsToCommit[tid]++;
1468 // Notify potential listeners that execution is complete for this
1470 ppToCommit->notify(inst);
1472 // Some instructions will be sent to commit without having
1473 // executed because they need commit to handle them.
1474 // E.g. Strictly ordered loads have not actually executed when they
1475 // are first sent to commit. Instead commit must tell the LSQ
1476 // when it's ready to execute the strictly ordered load.
1477 if (!inst->isSquashed() && inst->isExecuted() && inst->getFault() == NoFault) {
1478 int dependents = instQueue.wakeDependents(inst);
1480 for (int i = 0; i < inst->numDestRegs(); i++) {
1481 // Mark register as ready if not pinned
1482 if (inst->renamedDestRegIdx(i)->
1483 getNumPinnedWritesToComplete() == 0) {
1484 DPRINTF(IEW,"Setting Destination Register %i (%s)\n",
1485 inst->renamedDestRegIdx(i)->index(),
1486 inst->renamedDestRegIdx(i)->className());
1487 scoreboard->setReg(inst->renamedDestRegIdx(i));
1492 producerInst[tid]++;
1493 consumerInst[tid]+= dependents;
1495 writebackCount[tid]++;
1500 template<class Impl>
1502 DefaultIEW<Impl>::tick()
1507 wroteToTimeBuffer = false;
1508 updatedQueues = false;
1514 // Free function units marked as being freed this cycle.
1515 fuPool->processFreeUnits();
1517 list<ThreadID>::iterator threads = activeThreads->begin();
1518 list<ThreadID>::iterator end = activeThreads->end();
1520 // Check stall and squash signals, dispatch any instructions.
1521 while (threads != end) {
1522 ThreadID tid = *threads++;
1524 DPRINTF(IEW,"Issue: Processing [tid:%i]\n",tid);
1526 checkSignalsAndUpdate(tid);
1530 if (exeStatus != Squashing) {
1535 // Have the instruction queue try to schedule any ready instructions.
1536 // (In actuality, this scheduling is for instructions that will
1537 // be executed next cycle.)
1538 instQueue.scheduleReadyInsts();
1540 // Also should advance its own time buffers if the stage ran.
1541 // Not the best place for it, but this works (hopefully).
1542 issueToExecQueue.advance();
1545 bool broadcast_free_entries = false;
1547 if (updatedQueues || exeStatus == Running || updateLSQNextCycle) {
1549 updateLSQNextCycle = false;
1551 broadcast_free_entries = true;
1554 // Writeback any stores using any leftover bandwidth.
1555 ldstQueue.writebackStores();
1557 // Check the committed load/store signals to see if there's a load
1558 // or store to commit. Also check if it's being told to execute a
1559 // nonspeculative instruction.
1560 // This is pretty inefficient...
1562 threads = activeThreads->begin();
1563 while (threads != end) {
1564 ThreadID tid = (*threads++);
1566 DPRINTF(IEW,"Processing [tid:%i]\n",tid);
1568 // Update structures based on instructions committed.
1569 if (fromCommit->commitInfo[tid].doneSeqNum != 0 &&
1570 !fromCommit->commitInfo[tid].squash &&
1571 !fromCommit->commitInfo[tid].robSquashing) {
1573 ldstQueue.commitStores(fromCommit->commitInfo[tid].doneSeqNum,tid);
1575 ldstQueue.commitLoads(fromCommit->commitInfo[tid].doneSeqNum,tid);
1577 updateLSQNextCycle = true;
1578 instQueue.commit(fromCommit->commitInfo[tid].doneSeqNum,tid);
1581 if (fromCommit->commitInfo[tid].nonSpecSeqNum != 0) {
1583 //DPRINTF(IEW,"NonspecInst from thread %i",tid);
1584 if (fromCommit->commitInfo[tid].strictlyOrdered) {
1585 instQueue.replayMemInst(
1586 fromCommit->commitInfo[tid].strictlyOrderedLoad);
1587 fromCommit->commitInfo[tid].strictlyOrderedLoad->setAtCommit();
1589 instQueue.scheduleNonSpec(
1590 fromCommit->commitInfo[tid].nonSpecSeqNum);
1594 if (broadcast_free_entries) {
1595 toFetch->iewInfo[tid].iqCount =
1596 instQueue.getCount(tid);
1597 toFetch->iewInfo[tid].ldstqCount =
1598 ldstQueue.getCount(tid);
1600 toRename->iewInfo[tid].usedIQ = true;
1601 toRename->iewInfo[tid].freeIQEntries =
1602 instQueue.numFreeEntries(tid);
1603 toRename->iewInfo[tid].usedLSQ = true;
1605 toRename->iewInfo[tid].freeLQEntries =
1606 ldstQueue.numFreeLoadEntries(tid);
1607 toRename->iewInfo[tid].freeSQEntries =
1608 ldstQueue.numFreeStoreEntries(tid);
1610 wroteToTimeBuffer = true;
1613 DPRINTF(IEW, "[tid:%i], Dispatch dispatched %i instructions.\n",
1614 tid, toRename->iewInfo[tid].dispatched);
1617 DPRINTF(IEW, "IQ has %i free entries (Can schedule: %i). "
1618 "LQ has %i free entries. SQ has %i free entries.\n",
1619 instQueue.numFreeEntries(), instQueue.hasReadyInsts(),
1620 ldstQueue.numFreeLoadEntries(), ldstQueue.numFreeStoreEntries());
1624 if (wroteToTimeBuffer) {
1625 DPRINTF(Activity, "Activity this cycle.\n");
1626 cpu->activityThisCycle();
1630 template <class Impl>
1632 DefaultIEW<Impl>::updateExeInstStats(const DynInstPtr& inst)
1634 ThreadID tid = inst->threadNumber;
1639 if (DTRACE(O3PipeView)) {
1640 inst->completeTick = curTick() - inst->fetchTick;
1645 // Control operations
1647 if (inst->isControl())
1648 iewExecutedBranches[tid]++;
1651 // Memory operations
1653 if (inst->isMemRef()) {
1654 iewExecutedRefs[tid]++;
1656 if (inst->isLoad()) {
1657 iewExecLoadInsts[tid]++;
1662 template <class Impl>
1664 DefaultIEW<Impl>::checkMisprediction(const DynInstPtr& inst)
1666 ThreadID tid = inst->threadNumber;
1668 if (!fetchRedirect[tid] ||
1669 !toCommit->squash[tid] ||
1670 toCommit->squashedSeqNum[tid] > inst->seqNum) {
1672 if (inst->mispredicted()) {
1673 fetchRedirect[tid] = true;
1675 DPRINTF(IEW, "[tid:%i] [sn:%llu] Execute: "
1676 "Branch mispredict detected.\n",
1678 DPRINTF(IEW, "[tid:%i] [sn:%llu] Predicted target "
1679 "was PC:%#x, NPC:%#x\n",
1681 inst->predInstAddr(), inst->predNextInstAddr());
1682 DPRINTF(IEW, "[tid:%i] [sn:%llu] Execute: "
1683 "Redirecting fetch to PC: %#x, "
1686 inst->nextInstAddr(),
1687 inst->nextInstAddr());
1688 // If incorrect, then signal the ROB that it must be squashed.
1689 squashDueToBranch(inst, tid);
1691 if (inst->readPredTaken()) {
1692 predictedTakenIncorrect++;
1694 predictedNotTakenIncorrect++;
1700 #endif//__CPU_O3_IEW_IMPL_IMPL_HH__