2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // @todo: Fix the instantaneous communication among all the stages within
30 // iew. There's a clear delay between issue and execute, yet backwards
31 // communication happens simultaneously.
35 #include "base/timebuf.hh"
36 #include "cpu/o3/fu_pool.hh"
37 #include "cpu/o3/iew.hh"
42 DefaultIEW<Impl>::LdWritebackEvent::LdWritebackEvent(DynInstPtr &_inst,
43 DefaultIEW<Impl> *_iew)
44 : Event(&mainEventQueue), inst(_inst), iewStage(_iew)
46 this->setFlags(Event::AutoDelete);
51 DefaultIEW<Impl>::LdWritebackEvent::process()
53 DPRINTF(IEW, "Load writeback event [sn:%lli]\n", inst->seqNum);
54 DPRINTF(Activity, "Activity: Ld Writeback event [sn:%lli]\n", inst->seqNum);
56 //iewStage->ldstQueue.removeMSHR(inst->threadNumber,inst->seqNum);
58 if (iewStage->isSwitchedOut()) {
61 } else if (inst->isSquashed()) {
69 if (!inst->isExecuted()) {
72 // Complete access to copy data to proper place.
73 if (inst->isStore()) {
78 // Need to insert instruction into queue to commit
79 iewStage->instToCommit(inst);
81 iewStage->activityThisCycle();
88 DefaultIEW<Impl>::LdWritebackEvent::description()
90 return "Load writeback event";
94 DefaultIEW<Impl>::DefaultIEW(Params *params)
95 : // @todo: Make this into a parameter.
96 issueToExecQueue(5, 5),
99 fuPool(params->fuPool),
100 commitToIEWDelay(params->commitToIEWDelay),
101 renameToIEWDelay(params->renameToIEWDelay),
102 issueToExecuteDelay(params->issueToExecuteDelay),
103 issueReadWidth(params->issueWidth),
104 issueWidth(params->issueWidth),
105 executeWidth(params->executeWidth),
106 numThreads(params->numberOfThreads),
113 // Setup wire to read instructions coming from issue.
114 fromIssue = issueToExecQueue.getWire(-issueToExecuteDelay);
116 // Instruction queue needs the queue between issue and execute.
117 instQueue.setIssueToExecuteQueue(&issueToExecQueue);
119 instQueue.setIEW(this);
120 ldstQueue.setIEW(this);
122 for (int i=0; i < numThreads; i++) {
123 dispatchStatus[i] = Running;
124 stalls[i].commit = false;
125 fetchRedirect[i] = false;
128 updateLSQNextCycle = false;
130 skidBufferMax = (3 * (renameToIEWDelay * params->renameWidth)) + issueWidth;
133 template <class Impl>
135 DefaultIEW<Impl>::name() const
137 return cpu->name() + ".iew";
140 template <class Impl>
142 DefaultIEW<Impl>::regStats()
144 using namespace Stats;
146 instQueue.regStats();
149 .name(name() + ".iewIdleCycles")
150 .desc("Number of cycles IEW is idle");
153 .name(name() + ".iewSquashCycles")
154 .desc("Number of cycles IEW is squashing");
157 .name(name() + ".iewBlockCycles")
158 .desc("Number of cycles IEW is blocking");
161 .name(name() + ".iewUnblockCycles")
162 .desc("Number of cycles IEW is unblocking");
165 .name(name() + ".iewDispatchedInsts")
166 .desc("Number of instructions dispatched to IQ");
169 .name(name() + ".iewDispSquashedInsts")
170 .desc("Number of squashed instructions skipped by dispatch");
173 .name(name() + ".iewDispLoadInsts")
174 .desc("Number of dispatched load instructions");
177 .name(name() + ".iewDispStoreInsts")
178 .desc("Number of dispatched store instructions");
181 .name(name() + ".iewDispNonSpecInsts")
182 .desc("Number of dispatched non-speculative instructions");
185 .name(name() + ".iewIQFullEvents")
186 .desc("Number of times the IQ has become full, causing a stall");
189 .name(name() + ".iewLSQFullEvents")
190 .desc("Number of times the LSQ has become full, causing a stall");
193 .name(name() + ".iewExecutedInsts")
194 .desc("Number of executed instructions");
197 .init(cpu->number_of_threads)
198 .name(name() + ".iewExecLoadInsts")
199 .desc("Number of load instructions executed")
203 .name(name() + ".iewExecSquashedInsts")
204 .desc("Number of squashed instructions skipped in execute");
206 memOrderViolationEvents
207 .name(name() + ".memOrderViolationEvents")
208 .desc("Number of memory order violations");
210 predictedTakenIncorrect
211 .name(name() + ".predictedTakenIncorrect")
212 .desc("Number of branches that were predicted taken incorrectly");
214 predictedNotTakenIncorrect
215 .name(name() + ".predictedNotTakenIncorrect")
216 .desc("Number of branches that were predicted not taken incorrectly");
219 .name(name() + ".branchMispredicts")
220 .desc("Number of branch mispredicts detected at execute");
222 branchMispredicts = predictedTakenIncorrect + predictedNotTakenIncorrect;
225 .init(cpu->number_of_threads)
226 .name(name() + ".EXEC:swp")
227 .desc("number of swp insts executed")
232 .init(cpu->number_of_threads)
233 .name(name() + ".EXEC:nop")
234 .desc("number of nop insts executed")
239 .init(cpu->number_of_threads)
240 .name(name() + ".EXEC:refs")
241 .desc("number of memory reference insts executed")
246 .init(cpu->number_of_threads)
247 .name(name() + ".EXEC:branches")
248 .desc("Number of branches executed")
253 .name(name() + ".EXEC:rate")
254 .desc("Inst execution rate")
257 issueRate = iewExecutedInsts / cpu->numCycles;
260 .name(name() + ".EXEC:stores")
261 .desc("Number of stores executed")
264 iewExecStoreInsts = exeRefs - iewExecLoadInsts;
266 for (int i=0; i<Num_OpClasses; ++i) {
267 stringstream subname;
268 subname << opClassStrings[i] << "_delay";
269 issue_delay_dist.subname(i, subname.str());
277 .init(cpu->number_of_threads)
278 .name(name() + ".WB:sent")
279 .desc("cumulative count of insts sent to commit")
284 .init(cpu->number_of_threads)
285 .name(name() + ".WB:count")
286 .desc("cumulative count of insts written-back")
291 .init(cpu->number_of_threads)
292 .name(name() + ".WB:producers")
293 .desc("num instructions producing a value")
298 .init(cpu->number_of_threads)
299 .name(name() + ".WB:consumers")
300 .desc("num instructions consuming a value")
305 .init(cpu->number_of_threads)
306 .name(name() + ".WB:penalized")
307 .desc("number of instrctions required to write to 'other' IQ")
312 .name(name() + ".WB:penalized_rate")
313 .desc ("fraction of instructions written-back that wrote to 'other' IQ")
317 wbPenalizedRate = wbPenalized / writebackCount;
320 .name(name() + ".WB:fanout")
321 .desc("average fanout of values written-back")
325 wbFanout = producerInst / consumerInst;
328 .name(name() + ".WB:rate")
329 .desc("insts written-back per cycle")
332 wbRate = writebackCount / cpu->numCycles;
337 DefaultIEW<Impl>::initStage()
339 for (int tid=0; tid < numThreads; tid++) {
340 toRename->iewInfo[tid].usedIQ = true;
341 toRename->iewInfo[tid].freeIQEntries =
342 instQueue.numFreeEntries(tid);
344 toRename->iewInfo[tid].usedLSQ = true;
345 toRename->iewInfo[tid].freeLSQEntries =
346 ldstQueue.numFreeEntries(tid);
352 DefaultIEW<Impl>::setCPU(FullCPU *cpu_ptr)
354 DPRINTF(IEW, "Setting CPU pointer.\n");
357 instQueue.setCPU(cpu_ptr);
358 ldstQueue.setCPU(cpu_ptr);
360 cpu->activateStage(FullCPU::IEWIdx);
365 DefaultIEW<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
367 DPRINTF(IEW, "Setting time buffer pointer.\n");
370 // Setup wire to read information from time buffer, from commit.
371 fromCommit = timeBuffer->getWire(-commitToIEWDelay);
373 // Setup wire to write information back to previous stages.
374 toRename = timeBuffer->getWire(0);
376 toFetch = timeBuffer->getWire(0);
378 // Instruction queue also needs main time buffer.
379 instQueue.setTimeBuffer(tb_ptr);
384 DefaultIEW<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
386 DPRINTF(IEW, "Setting rename queue pointer.\n");
387 renameQueue = rq_ptr;
389 // Setup wire to read information from rename queue.
390 fromRename = renameQueue->getWire(-renameToIEWDelay);
395 DefaultIEW<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
397 DPRINTF(IEW, "Setting IEW queue pointer.\n");
400 // Setup wire to write instructions to commit.
401 toCommit = iewQueue->getWire(0);
406 DefaultIEW<Impl>::setActiveThreads(list<unsigned> *at_ptr)
408 DPRINTF(IEW, "Setting active threads list pointer.\n");
409 activeThreads = at_ptr;
411 ldstQueue.setActiveThreads(at_ptr);
412 instQueue.setActiveThreads(at_ptr);
417 DefaultIEW<Impl>::setScoreboard(Scoreboard *sb_ptr)
419 DPRINTF(IEW, "Setting scoreboard pointer.\n");
426 DefaultIEW<Impl>::setPageTable(PageTable *pt_ptr)
428 ldstQueue.setPageTable(pt_ptr);
432 template <class Impl>
434 DefaultIEW<Impl>::switchOut()
436 cpu->signalSwitched();
439 template <class Impl>
441 DefaultIEW<Impl>::doSwitchOut()
445 instQueue.switchOut();
446 ldstQueue.switchOut();
449 for (int i = 0; i < numThreads; i++) {
450 while (!insts[i].empty())
452 while (!skidBuffer[i].empty())
457 template <class Impl>
459 DefaultIEW<Impl>::takeOverFrom()
466 instQueue.takeOverFrom();
467 ldstQueue.takeOverFrom();
468 fuPool->takeOverFrom();
471 cpu->activityThisCycle();
473 for (int i=0; i < numThreads; i++) {
474 dispatchStatus[i] = Running;
475 stalls[i].commit = false;
476 fetchRedirect[i] = false;
479 updateLSQNextCycle = false;
481 // @todo: Fix hardcoded number
482 for (int i = 0; i < 6; ++i) {
483 issueToExecQueue.advance();
489 DefaultIEW<Impl>::squash(unsigned tid)
491 DPRINTF(IEW, "[tid:%i]: Squashing all instructions.\n",
494 // Tell the IQ to start squashing.
495 instQueue.squash(tid);
497 // Tell the LDSTQ to start squashing.
498 ldstQueue.squash(fromCommit->commitInfo[tid].doneSeqNum, tid);
500 updatedQueues = true;
502 // Clear the skid buffer in case it has any data in it.
503 while (!skidBuffer[tid].empty()) {
505 if (skidBuffer[tid].front()->isLoad() ||
506 skidBuffer[tid].front()->isStore() ) {
507 toRename->iewInfo[tid].dispatchedToLSQ++;
510 toRename->iewInfo[tid].dispatched++;
512 skidBuffer[tid].pop();
515 while (!insts[tid].empty()) {
516 if (insts[tid].front()->isLoad() ||
517 insts[tid].front()->isStore() ) {
518 toRename->iewInfo[tid].dispatchedToLSQ++;
521 toRename->iewInfo[tid].dispatched++;
529 DefaultIEW<Impl>::squashDueToBranch(DynInstPtr &inst, unsigned tid)
531 DPRINTF(IEW, "[tid:%i]: Squashing from a specific instruction, PC: %#x "
532 "[sn:%i].\n", tid, inst->readPC(), inst->seqNum);
534 toCommit->squash[tid] = true;
535 toCommit->squashedSeqNum[tid] = inst->seqNum;
536 toCommit->mispredPC[tid] = inst->readPC();
537 toCommit->nextPC[tid] = inst->readNextPC();
538 toCommit->branchMispredict[tid] = true;
539 toCommit->branchTaken[tid] = inst->readNextPC() !=
540 (inst->readPC() + sizeof(TheISA::MachInst));
542 toCommit->includeSquashInst[tid] = false;
544 wroteToTimeBuffer = true;
549 DefaultIEW<Impl>::squashDueToMemOrder(DynInstPtr &inst, unsigned tid)
551 DPRINTF(IEW, "[tid:%i]: Squashing from a specific instruction, "
552 "PC: %#x [sn:%i].\n", tid, inst->readPC(), inst->seqNum);
554 toCommit->squash[tid] = true;
555 toCommit->squashedSeqNum[tid] = inst->seqNum;
556 toCommit->nextPC[tid] = inst->readNextPC();
558 toCommit->includeSquashInst[tid] = false;
560 wroteToTimeBuffer = true;
565 DefaultIEW<Impl>::squashDueToMemBlocked(DynInstPtr &inst, unsigned tid)
567 DPRINTF(IEW, "[tid:%i]: Memory blocked, squashing load and younger insts, "
568 "PC: %#x [sn:%i].\n", tid, inst->readPC(), inst->seqNum);
570 toCommit->squash[tid] = true;
571 toCommit->squashedSeqNum[tid] = inst->seqNum;
572 toCommit->nextPC[tid] = inst->readPC();
574 toCommit->includeSquashInst[tid] = true;
576 ldstQueue.setLoadBlockedHandled(tid);
578 wroteToTimeBuffer = true;
583 DefaultIEW<Impl>::block(unsigned tid)
585 DPRINTF(IEW, "[tid:%u]: Blocking.\n", tid);
587 if (dispatchStatus[tid] != Blocked &&
588 dispatchStatus[tid] != Unblocking) {
589 toRename->iewBlock[tid] = true;
590 wroteToTimeBuffer = true;
593 // Add the current inputs to the skid buffer so they can be
594 // reprocessed when this stage unblocks.
597 dispatchStatus[tid] = Blocked;
602 DefaultIEW<Impl>::unblock(unsigned tid)
604 DPRINTF(IEW, "[tid:%i]: Reading instructions out of the skid "
605 "buffer %u.\n",tid, tid);
607 // If the skid bufffer is empty, signal back to previous stages to unblock.
608 // Also switch status to running.
609 if (skidBuffer[tid].empty()) {
610 toRename->iewUnblock[tid] = true;
611 wroteToTimeBuffer = true;
612 DPRINTF(IEW, "[tid:%i]: Done unblocking.\n",tid);
613 dispatchStatus[tid] = Running;
619 DefaultIEW<Impl>::wakeDependents(DynInstPtr &inst)
621 instQueue.wakeDependents(inst);
626 DefaultIEW<Impl>::rescheduleMemInst(DynInstPtr &inst)
628 instQueue.rescheduleMemInst(inst);
633 DefaultIEW<Impl>::replayMemInst(DynInstPtr &inst)
635 instQueue.replayMemInst(inst);
640 DefaultIEW<Impl>::instToCommit(DynInstPtr &inst)
642 // First check the time slot that this instruction will write
643 // to. If there are free write ports at the time, then go ahead
644 // and write the instruction to that time. If there are not,
645 // keep looking back to see where's the first time there's a
647 while ((*iewQueue)[wbCycle].insts[wbNumInst]) {
649 if (wbNumInst == issueWidth) {
657 // Add finished instruction to queue to commit.
658 (*iewQueue)[wbCycle].insts[wbNumInst] = inst;
659 (*iewQueue)[wbCycle].size++;
662 template <class Impl>
664 DefaultIEW<Impl>::validInstsFromRename()
666 unsigned inst_count = 0;
668 for (int i=0; i<fromRename->size; i++) {
669 if (!fromRename->insts[i]->squashed)
678 DefaultIEW<Impl>::skidInsert(unsigned tid)
680 DynInstPtr inst = NULL;
682 while (!insts[tid].empty()) {
683 inst = insts[tid].front();
687 DPRINTF(Decode,"[tid:%i]: Inserting [sn:%lli] PC:%#x into "
688 "dispatch skidBuffer %i\n",tid, inst->seqNum,
691 skidBuffer[tid].push(inst);
694 assert(skidBuffer[tid].size() <= skidBufferMax &&
695 "Skidbuffer Exceeded Max Size");
700 DefaultIEW<Impl>::skidCount()
704 list<unsigned>::iterator threads = (*activeThreads).begin();
706 while (threads != (*activeThreads).end()) {
707 unsigned thread_count = skidBuffer[*threads++].size();
708 if (max < thread_count)
717 DefaultIEW<Impl>::skidsEmpty()
719 list<unsigned>::iterator threads = (*activeThreads).begin();
721 while (threads != (*activeThreads).end()) {
722 if (!skidBuffer[*threads++].empty())
729 template <class Impl>
731 DefaultIEW<Impl>::updateStatus()
733 bool any_unblocking = false;
735 list<unsigned>::iterator threads = (*activeThreads).begin();
737 threads = (*activeThreads).begin();
739 while (threads != (*activeThreads).end()) {
740 unsigned tid = *threads++;
742 if (dispatchStatus[tid] == Unblocking) {
743 any_unblocking = true;
748 // If there are no ready instructions waiting to be scheduled by the IQ,
749 // and there's no stores waiting to write back, and dispatch is not
750 // unblocking, then there is no internal activity for the IEW stage.
751 if (_status == Active && !instQueue.hasReadyInsts() &&
752 !ldstQueue.willWB() && !any_unblocking) {
753 DPRINTF(IEW, "IEW switching to idle\n");
758 } else if (_status == Inactive && (instQueue.hasReadyInsts() ||
759 ldstQueue.willWB() ||
761 // Otherwise there is internal activity. Set to active.
762 DPRINTF(IEW, "IEW switching to active\n");
770 template <class Impl>
772 DefaultIEW<Impl>::resetEntries()
774 instQueue.resetEntries();
775 ldstQueue.resetEntries();
778 template <class Impl>
780 DefaultIEW<Impl>::readStallSignals(unsigned tid)
782 if (fromCommit->commitBlock[tid]) {
783 stalls[tid].commit = true;
786 if (fromCommit->commitUnblock[tid]) {
787 assert(stalls[tid].commit);
788 stalls[tid].commit = false;
792 template <class Impl>
794 DefaultIEW<Impl>::checkStall(unsigned tid)
798 if (stalls[tid].commit) {
799 DPRINTF(IEW,"[tid:%i]: Stall from Commit stage detected.\n",tid);
801 } else if (instQueue.isFull(tid)) {
802 DPRINTF(IEW,"[tid:%i]: Stall: IQ is full.\n",tid);
804 } else if (ldstQueue.isFull(tid)) {
805 DPRINTF(IEW,"[tid:%i]: Stall: LSQ is full\n",tid);
807 if (ldstQueue.numLoads(tid) > 0 ) {
809 DPRINTF(IEW,"[tid:%i]: LSQ oldest load: [sn:%i] \n",
810 tid,ldstQueue.getLoadHeadSeqNum(tid));
813 if (ldstQueue.numStores(tid) > 0) {
815 DPRINTF(IEW,"[tid:%i]: LSQ oldest store: [sn:%i] \n",
816 tid,ldstQueue.getStoreHeadSeqNum(tid));
820 } else if (ldstQueue.isStalled(tid)) {
821 DPRINTF(IEW,"[tid:%i]: Stall: LSQ stall detected.\n",tid);
828 template <class Impl>
830 DefaultIEW<Impl>::checkSignalsAndUpdate(unsigned tid)
832 // Check if there's a squash signal, squash if there is
833 // Check stall signals, block if there is.
834 // If status was Blocked
835 // if so then go to unblocking
836 // If status was Squashing
837 // check if squashing is not high. Switch to running this cycle.
839 readStallSignals(tid);
841 if (fromCommit->commitInfo[tid].squash) {
844 if (dispatchStatus[tid] == Blocked ||
845 dispatchStatus[tid] == Unblocking) {
846 toRename->iewUnblock[tid] = true;
847 wroteToTimeBuffer = true;
850 dispatchStatus[tid] = Squashing;
852 fetchRedirect[tid] = false;
856 if (fromCommit->commitInfo[tid].robSquashing) {
857 DPRINTF(IEW, "[tid:%i]: ROB is still squashing.\n");
859 dispatchStatus[tid] = Squashing;
864 if (checkStall(tid)) {
866 dispatchStatus[tid] = Blocked;
870 if (dispatchStatus[tid] == Blocked) {
871 // Status from previous cycle was blocked, but there are no more stall
872 // conditions. Switch over to unblocking.
873 DPRINTF(IEW, "[tid:%i]: Done blocking, switching to unblocking.\n",
876 dispatchStatus[tid] = Unblocking;
883 if (dispatchStatus[tid] == Squashing) {
884 // Switch status to running if rename isn't being told to block or
885 // squash this cycle.
886 DPRINTF(IEW, "[tid:%i]: Done squashing, switching to running.\n",
889 dispatchStatus[tid] = Running;
895 template <class Impl>
897 DefaultIEW<Impl>::sortInsts()
899 int insts_from_rename = fromRename->size;
901 for (int i = 0; i < numThreads; i++)
902 assert(insts[i].empty());
904 for (int i = 0; i < insts_from_rename; ++i) {
905 insts[fromRename->insts[i]->threadNumber].push(fromRename->insts[i]);
909 template <class Impl>
911 DefaultIEW<Impl>::wakeCPU()
916 template <class Impl>
918 DefaultIEW<Impl>::activityThisCycle()
920 DPRINTF(Activity, "Activity this cycle.\n");
921 cpu->activityThisCycle();
924 template <class Impl>
926 DefaultIEW<Impl>::activateStage()
928 DPRINTF(Activity, "Activating stage.\n");
929 cpu->activateStage(FullCPU::IEWIdx);
932 template <class Impl>
934 DefaultIEW<Impl>::deactivateStage()
936 DPRINTF(Activity, "Deactivating stage.\n");
937 cpu->deactivateStage(FullCPU::IEWIdx);
942 DefaultIEW<Impl>::dispatch(unsigned tid)
944 // If status is Running or idle,
945 // call dispatchInsts()
946 // If status is Unblocking,
947 // buffer any instructions coming from rename
948 // continue trying to empty skid buffer
949 // check if stall conditions have passed
951 if (dispatchStatus[tid] == Blocked) {
954 } else if (dispatchStatus[tid] == Squashing) {
958 // Dispatch should try to dispatch as many instructions as its bandwidth
959 // will allow, as long as it is not currently blocked.
960 if (dispatchStatus[tid] == Running ||
961 dispatchStatus[tid] == Idle) {
962 DPRINTF(IEW, "[tid:%i] Not blocked, so attempting to run "
966 } else if (dispatchStatus[tid] == Unblocking) {
967 // Make sure that the skid buffer has something in it if the
968 // status is unblocking.
969 assert(!skidsEmpty());
971 // If the status was unblocking, then instructions from the skid
972 // buffer were used. Remove those instructions and handle
973 // the rest of unblocking.
978 if (validInstsFromRename() && dispatchedAllInsts) {
979 // Add the current inputs to the skid buffer so they can be
980 // reprocessed when this stage unblocks.
988 template <class Impl>
990 DefaultIEW<Impl>::dispatchInsts(unsigned tid)
992 dispatchedAllInsts = true;
994 // Obtain instructions from skid buffer if unblocking, or queue from rename
996 std::queue<DynInstPtr> &insts_to_dispatch =
997 dispatchStatus[tid] == Unblocking ?
998 skidBuffer[tid] : insts[tid];
1000 int insts_to_add = insts_to_dispatch.size();
1003 bool add_to_iq = false;
1004 int dis_num_inst = 0;
1006 // Loop through the instructions, putting them in the instruction
1008 for ( ; dis_num_inst < insts_to_add &&
1009 dis_num_inst < issueReadWidth;
1012 inst = insts_to_dispatch.front();
1014 if (dispatchStatus[tid] == Unblocking) {
1015 DPRINTF(IEW, "[tid:%i]: Issue: Examining instruction from skid "
1019 // Make sure there's a valid instruction there.
1022 DPRINTF(IEW, "[tid:%i]: Issue: Adding PC %#x [sn:%lli] [tid:%i] to "
1024 tid, inst->readPC(), inst->seqNum, inst->threadNumber);
1026 // Be sure to mark these instructions as ready so that the
1027 // commit stage can go ahead and execute them, and mark
1028 // them as issued so the IQ doesn't reprocess them.
1030 // Check for squashed instructions.
1031 if (inst->isSquashed()) {
1032 DPRINTF(IEW, "[tid:%i]: Issue: Squashed instruction encountered, "
1033 "not adding to IQ.\n", tid);
1035 ++iewDispSquashedInsts;
1037 insts_to_dispatch.pop();
1039 //Tell Rename That An Instruction has been processed
1040 if (inst->isLoad() || inst->isStore()) {
1041 toRename->iewInfo[tid].dispatchedToLSQ++;
1043 toRename->iewInfo[tid].dispatched++;
1048 // Check for full conditions.
1049 if (instQueue.isFull(tid)) {
1050 DPRINTF(IEW, "[tid:%i]: Issue: IQ has become full.\n", tid);
1052 // Call function to start blocking.
1055 // Set unblock to false. Special case where we are using
1056 // skidbuffer (unblocking) instructions but then we still
1057 // get full in the IQ.
1058 toRename->iewUnblock[tid] = false;
1060 dispatchedAllInsts = false;
1064 } else if (ldstQueue.isFull(tid)) {
1065 DPRINTF(IEW, "[tid:%i]: Issue: LSQ has become full.\n",tid);
1067 // Call function to start blocking.
1070 // Set unblock to false. Special case where we are using
1071 // skidbuffer (unblocking) instructions but then we still
1072 // get full in the IQ.
1073 toRename->iewUnblock[tid] = false;
1075 dispatchedAllInsts = false;
1081 // Otherwise issue the instruction just fine.
1082 if (inst->isLoad()) {
1083 DPRINTF(IEW, "[tid:%i]: Issue: Memory instruction "
1084 "encountered, adding to LSQ.\n", tid);
1086 // Reserve a spot in the load store queue for this
1088 ldstQueue.insertLoad(inst);
1094 toRename->iewInfo[tid].dispatchedToLSQ++;
1095 } else if (inst->isStore()) {
1096 DPRINTF(IEW, "[tid:%i]: Issue: Memory instruction "
1097 "encountered, adding to LSQ.\n", tid);
1099 ldstQueue.insertStore(inst);
1101 ++iewDispStoreInsts;
1103 if (inst->isNonSpeculative()) {
1104 // Non-speculative stores (namely store conditionals)
1105 // need to be set as "canCommit()" so that commit can
1106 // process them when they reach the head of commit.
1107 inst->setCanCommit();
1108 instQueue.insertNonSpec(inst);
1111 ++iewDispNonSpecInsts;
1116 toRename->iewInfo[tid].dispatchedToLSQ++;
1118 } else if (inst->isMemBarrier() || inst->isWriteBarrier()) {
1119 // Same as non-speculative stores.
1120 inst->setCanCommit();
1121 instQueue.insertBarrier(inst);
1124 } else if (inst->isNonSpeculative()) {
1125 DPRINTF(IEW, "[tid:%i]: Issue: Nonspeculative instruction "
1126 "encountered, skipping.\n", tid);
1128 // Same as non-speculative stores.
1129 inst->setCanCommit();
1131 // Specifically insert it as nonspeculative.
1132 instQueue.insertNonSpec(inst);
1134 ++iewDispNonSpecInsts;
1137 } else if (inst->isNop()) {
1138 DPRINTF(IEW, "[tid:%i]: Issue: Nop instruction encountered, "
1139 "skipping.\n", tid);
1142 inst->setExecuted();
1143 inst->setCanCommit();
1145 instQueue.recordProducer(inst);
1150 } else if (inst->isExecuted()) {
1151 assert(0 && "Instruction shouldn't be executed.\n");
1152 DPRINTF(IEW, "Issue: Executed branch encountered, "
1156 inst->setCanCommit();
1158 instQueue.recordProducer(inst);
1165 // If the instruction queue is not full, then add the
1168 instQueue.insert(inst);
1171 insts_to_dispatch.pop();
1173 toRename->iewInfo[tid].dispatched++;
1175 ++iewDispatchedInsts;
1178 if (!insts_to_dispatch.empty()) {
1179 DPRINTF(IEW,"[tid:%i]: Issue: Bandwidth Full. Blocking.\n");
1181 toRename->iewUnblock[tid] = false;
1184 if (dispatchStatus[tid] == Idle && dis_num_inst) {
1185 dispatchStatus[tid] = Running;
1187 updatedQueues = true;
1193 template <class Impl>
1195 DefaultIEW<Impl>::printAvailableInsts()
1199 cout << "Available Instructions: ";
1201 while (fromIssue->insts[inst]) {
1203 if (inst%3==0) cout << "\n\t";
1205 cout << "PC: " << fromIssue->insts[inst]->readPC()
1206 << " TN: " << fromIssue->insts[inst]->threadNumber
1207 << " SN: " << fromIssue->insts[inst]->seqNum << " | ";
1216 template <class Impl>
1218 DefaultIEW<Impl>::executeInsts()
1223 list<unsigned>::iterator threads = (*activeThreads).begin();
1225 while (threads != (*activeThreads).end()) {
1226 unsigned tid = *threads++;
1227 fetchRedirect[tid] = false;
1231 printAvailableInsts();
1234 // Execute/writeback any instructions that are available.
1236 for ( ; inst_num < issueWidth && fromIssue->insts[inst_num];
1239 DPRINTF(IEW, "Execute: Executing instructions from IQ.\n");
1241 DynInstPtr inst = fromIssue->insts[inst_num];
1243 DPRINTF(IEW, "Execute: Processing PC %#x, [tid:%i] [sn:%i].\n",
1244 inst->readPC(), inst->threadNumber,inst->seqNum);
1246 // Check if the instruction is squashed; if so then skip it
1247 if (inst->isSquashed()) {
1248 DPRINTF(IEW, "Execute: Instruction was squashed.\n");
1250 // Consider this instruction executed so that commit can go
1251 // ahead and retire the instruction.
1252 inst->setExecuted();
1254 // Not sure if I should set this here or just let commit try to
1255 // commit any squashed instructions. I like the latter a bit more.
1256 inst->setCanCommit();
1258 ++iewExecSquashedInsts;
1263 Fault fault = NoFault;
1265 // Execute instruction.
1266 // Note that if the instruction faults, it will be handled
1267 // at the commit stage.
1268 if (inst->isMemRef() &&
1269 (!inst->isDataPrefetch() && !inst->isInstPrefetch())) {
1270 DPRINTF(IEW, "Execute: Calculating address for memory "
1273 // Tell the LDSTQ to execute this instruction (if it is a load).
1274 if (inst->isLoad()) {
1275 // Loads will mark themselves as executed, and their writeback
1276 // event adds the instruction to the queue to commit
1277 fault = ldstQueue.executeLoad(inst);
1278 } else if (inst->isStore()) {
1279 ldstQueue.executeStore(inst);
1281 // If the store had a fault then it may not have a mem req
1282 if (inst->req && !(inst->req->flags & LOCKED)) {
1283 inst->setExecuted();
1288 // Store conditionals will mark themselves as
1289 // executed, and their writeback event will add the
1290 // instruction to the queue to commit.
1292 panic("Unexpected memory type!\n");
1298 inst->setExecuted();
1303 updateExeInstStats(inst);
1305 // Check if branch prediction was correct, if not then we need
1306 // to tell commit to squash in flight instructions. Only
1307 // handle this if there hasn't already been something that
1308 // redirects fetch in this group of instructions.
1310 // This probably needs to prioritize the redirects if a different
1311 // scheduler is used. Currently the scheduler schedules the oldest
1312 // instruction first, so the branch resolution order will be correct.
1313 unsigned tid = inst->threadNumber;
1315 if (!fetchRedirect[tid]) {
1317 if (inst->mispredicted()) {
1318 fetchRedirect[tid] = true;
1320 DPRINTF(IEW, "Execute: Branch mispredict detected.\n");
1321 DPRINTF(IEW, "Execute: Redirecting fetch to PC: %#x.\n",
1324 // If incorrect, then signal the ROB that it must be squashed.
1325 squashDueToBranch(inst, tid);
1327 if (inst->predTaken()) {
1328 predictedTakenIncorrect++;
1330 predictedNotTakenIncorrect++;
1332 } else if (ldstQueue.violation(tid)) {
1333 fetchRedirect[tid] = true;
1335 // If there was an ordering violation, then get the
1336 // DynInst that caused the violation. Note that this
1337 // clears the violation signal.
1338 DynInstPtr violator;
1339 violator = ldstQueue.getMemDepViolator(tid);
1341 DPRINTF(IEW, "LDSTQ detected a violation. Violator PC: "
1342 "%#x, inst PC: %#x. Addr is: %#x.\n",
1343 violator->readPC(), inst->readPC(), inst->physEffAddr);
1345 // Tell the instruction queue that a violation has occured.
1346 instQueue.violation(inst, violator);
1349 squashDueToMemOrder(inst,tid);
1351 ++memOrderViolationEvents;
1352 } else if (ldstQueue.loadBlocked(tid) &&
1353 !ldstQueue.isLoadBlockedHandled(tid)) {
1354 fetchRedirect[tid] = true;
1356 DPRINTF(IEW, "Load operation couldn't execute because the "
1357 "memory system is blocked. PC: %#x [sn:%lli]\n",
1358 inst->readPC(), inst->seqNum);
1360 squashDueToMemBlocked(inst, tid);
1366 if (exeStatus == Idle) {
1367 exeStatus = Running;
1370 updatedQueues = true;
1372 cpu->activityThisCycle();
1375 // Need to reset this in case a writeback event needs to write into the
1376 // iew queue. That way the writeback event will write into the correct
1377 // spot in the queue.
1381 template <class Impl>
1383 DefaultIEW<Impl>::writebackInsts()
1385 // Loop through the head of the time buffer and wake any
1386 // dependents. These instructions are about to write back. Also
1387 // mark scoreboard that this instruction is finally complete.
1388 // Either have IEW have direct access to scoreboard, or have this
1389 // as part of backwards communication.
1390 for (int inst_num = 0; inst_num < issueWidth &&
1391 toCommit->insts[inst_num]; inst_num++) {
1392 DynInstPtr inst = toCommit->insts[inst_num];
1393 int tid = inst->threadNumber;
1395 DPRINTF(IEW, "Sending instructions to commit, PC %#x.\n",
1398 iewInstsToCommit[tid]++;
1400 // Some instructions will be sent to commit without having
1401 // executed because they need commit to handle them.
1402 // E.g. Uncached loads have not actually executed when they
1403 // are first sent to commit. Instead commit must tell the LSQ
1404 // when it's ready to execute the uncached load.
1405 if (!inst->isSquashed() && inst->isExecuted()) {
1406 int dependents = instQueue.wakeDependents(inst);
1408 for (int i = 0; i < inst->numDestRegs(); i++) {
1410 DPRINTF(IEW,"Setting Destination Register %i\n",
1411 inst->renamedDestRegIdx(i));
1412 scoreboard->setReg(inst->renamedDestRegIdx(i));
1415 producerInst[tid]++;
1416 consumerInst[tid]+= dependents;
1417 writebackCount[tid]++;
1422 template<class Impl>
1424 DefaultIEW<Impl>::tick()
1429 wroteToTimeBuffer = false;
1430 updatedQueues = false;
1434 // Free function units marked as being freed this cycle.
1435 fuPool->processFreeUnits();
1437 list<unsigned>::iterator threads = (*activeThreads).begin();
1439 // Check stall and squash signals, dispatch any instructions.
1440 while (threads != (*activeThreads).end()) {
1441 unsigned tid = *threads++;
1443 DPRINTF(IEW,"Issue: Processing [tid:%i]\n",tid);
1445 checkSignalsAndUpdate(tid);
1449 if (exeStatus != Squashing) {
1454 // Have the instruction queue try to schedule any ready instructions.
1455 // (In actuality, this scheduling is for instructions that will
1456 // be executed next cycle.)
1457 instQueue.scheduleReadyInsts();
1459 // Also should advance its own time buffers if the stage ran.
1460 // Not the best place for it, but this works (hopefully).
1461 issueToExecQueue.advance();
1464 bool broadcast_free_entries = false;
1466 if (updatedQueues || exeStatus == Running || updateLSQNextCycle) {
1468 updateLSQNextCycle = false;
1470 broadcast_free_entries = true;
1473 // Writeback any stores using any leftover bandwidth.
1474 ldstQueue.writebackStores();
1476 // Check the committed load/store signals to see if there's a load
1477 // or store to commit. Also check if it's being told to execute a
1478 // nonspeculative instruction.
1479 // This is pretty inefficient...
1481 threads = (*activeThreads).begin();
1482 while (threads != (*activeThreads).end()) {
1483 unsigned tid = (*threads++);
1485 DPRINTF(IEW,"Processing [tid:%i]\n",tid);
1487 if (fromCommit->commitInfo[tid].doneSeqNum != 0 &&
1488 !fromCommit->commitInfo[tid].squash &&
1489 !fromCommit->commitInfo[tid].robSquashing) {
1491 ldstQueue.commitStores(fromCommit->commitInfo[tid].doneSeqNum,tid);
1493 ldstQueue.commitLoads(fromCommit->commitInfo[tid].doneSeqNum,tid);
1495 updateLSQNextCycle = true;
1496 instQueue.commit(fromCommit->commitInfo[tid].doneSeqNum,tid);
1499 if (fromCommit->commitInfo[tid].nonSpecSeqNum != 0) {
1501 //DPRINTF(IEW,"NonspecInst from thread %i",tid);
1502 if (fromCommit->commitInfo[tid].uncached) {
1503 instQueue.replayMemInst(fromCommit->commitInfo[tid].uncachedLoad);
1505 instQueue.scheduleNonSpec(
1506 fromCommit->commitInfo[tid].nonSpecSeqNum);
1510 if (broadcast_free_entries) {
1511 toFetch->iewInfo[tid].iqCount =
1512 instQueue.getCount(tid);
1513 toFetch->iewInfo[tid].ldstqCount =
1514 ldstQueue.getCount(tid);
1516 toRename->iewInfo[tid].usedIQ = true;
1517 toRename->iewInfo[tid].freeIQEntries =
1518 instQueue.numFreeEntries();
1519 toRename->iewInfo[tid].usedLSQ = true;
1520 toRename->iewInfo[tid].freeLSQEntries =
1521 ldstQueue.numFreeEntries(tid);
1523 wroteToTimeBuffer = true;
1526 DPRINTF(IEW, "[tid:%i], Dispatch dispatched %i instructions.\n",
1527 tid, toRename->iewInfo[tid].dispatched);
1530 DPRINTF(IEW, "IQ has %i free entries (Can schedule: %i). "
1531 "LSQ has %i free entries.\n",
1532 instQueue.numFreeEntries(), instQueue.hasReadyInsts(),
1533 ldstQueue.numFreeEntries());
1537 if (wroteToTimeBuffer) {
1538 DPRINTF(Activity, "Activity this cycle.\n");
1539 cpu->activityThisCycle();
1543 template <class Impl>
1545 DefaultIEW<Impl>::updateExeInstStats(DynInstPtr &inst)
1547 int thread_number = inst->threadNumber;
1550 // Pick off the software prefetches
1553 if (inst->isDataPrefetch())
1554 exeSwp[thread_number]++;
1558 iewExecutedInsts[thread_number]++;
1562 // Control operations
1564 if (inst->isControl())
1565 exeBranches[thread_number]++;
1568 // Memory operations
1570 if (inst->isMemRef()) {
1571 exeRefs[thread_number]++;
1573 if (inst->isLoad()) {
1574 iewExecLoadInsts[thread_number]++;