2 * Copyright (c) 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.
31 #include "cpu/checker/cpu.hh"
32 #include "cpu/ozone/lw_back_end.hh"
33 #include "encumbered/cpu/full/op_class.hh"
37 LWBackEnd<Impl>::generateTrapEvent(Tick latency)
39 DPRINTF(BE, "Generating trap event\n");
41 TrapEvent *trap = new TrapEvent(this);
43 trap->schedule(curTick + cpu->cycles(latency));
45 thread->trapPending = true;
50 LWBackEnd<Impl>::wakeDependents(DynInstPtr &inst, bool memory_deps)
52 assert(!inst->isSquashed());
53 std::vector<DynInstPtr> &dependents = memory_deps ? inst->getMemDeps() :
54 inst->getDependents();
55 int num_outputs = dependents.size();
57 DPRINTF(BE, "Waking instruction [sn:%lli] dependents in IQ\n", inst->seqNum);
59 for (int i = 0; i < num_outputs; i++) {
60 DynInstPtr dep_inst = dependents[i];
62 dep_inst->markSrcRegReady();
64 if (!dep_inst->isSquashed())
65 dep_inst->markMemInstReady(inst.get());
68 DPRINTF(BE, "Marking source reg ready [sn:%lli] in IQ\n", dep_inst->seqNum);
70 if (dep_inst->readyToIssue() && dep_inst->isInROB() &&
71 !dep_inst->isNonSpeculative() && !dep_inst->isStoreConditional() &&
72 dep_inst->memDepReady() && !dep_inst->isMemBarrier() &&
73 !dep_inst->isWriteBarrier()) {
74 DPRINTF(BE, "Adding instruction to exeList [sn:%lli]\n",
76 exeList.push(dep_inst);
77 if (dep_inst->iqItValid) {
78 DPRINTF(BE, "Removing instruction from waiting list\n");
79 waitingList.erase(dep_inst->iqIt);
81 dep_inst->iqItValid = false;
82 assert(waitingInsts >= 0);
84 if (dep_inst->isMemRef()) {
85 removeWaitingMemOp(dep_inst);
86 DPRINTF(BE, "Issued a waiting mem op [sn:%lli]\n",
96 LWBackEnd<Impl>::rescheduleMemInst(DynInstPtr &inst)
98 replayList.push_front(inst);
101 template <class Impl>
102 LWBackEnd<Impl>::TrapEvent::TrapEvent(LWBackEnd<Impl> *_be)
103 : Event(&mainEventQueue, CPU_Tick_Pri), be(_be)
105 this->setFlags(Event::AutoDelete);
108 template <class Impl>
110 LWBackEnd<Impl>::TrapEvent::process()
112 be->trapSquash = true;
115 template <class Impl>
117 LWBackEnd<Impl>::TrapEvent::description()
122 template <class Impl>
124 LWBackEnd<Impl>::replayMemInst(DynInstPtr &inst)
126 bool found_inst = false;
127 while (!replayList.empty()) {
128 exeList.push(replayList.front());
129 if (replayList.front() == inst) {
132 replayList.pop_front();
138 LWBackEnd<Impl>::LdWritebackEvent::LdWritebackEvent(DynInstPtr &_inst,
139 LWBackEnd<Impl> *_be)
140 : Event(&mainEventQueue), inst(_inst), be(_be), dcacheMiss(false)
142 this->setFlags(Event::AutoDelete);
147 LWBackEnd<Impl>::LdWritebackEvent::process()
149 DPRINTF(BE, "Load writeback event [sn:%lli]\n", inst->seqNum);
150 // DPRINTF(Activity, "Activity: Ld Writeback event [sn:%lli]\n", inst->seqNum);
152 //iewStage->ldstQueue.removeMSHR(inst->threadNumber,inst->seqNum);
154 // iewStage->wakeCPU();
156 if (be->isSwitchedOut())
160 be->removeDcacheMiss(inst);
163 if (inst->isSquashed()) {
168 if (!inst->isExecuted()) {
171 // Execute again to copy data to proper place.
175 // Need to insert instruction into queue to commit
176 be->instToCommit(inst);
178 //wroteToTimeBuffer = true;
179 // iewStage->activityThisCycle();
186 LWBackEnd<Impl>::LdWritebackEvent::description()
188 return "Load writeback event";
192 template <class Impl>
193 LWBackEnd<Impl>::DCacheCompletionEvent::DCacheCompletionEvent(LWBackEnd *_be)
194 : Event(&mainEventQueue, CPU_Tick_Pri), be(_be)
198 template <class Impl>
200 LWBackEnd<Impl>::DCacheCompletionEvent::process()
204 template <class Impl>
206 LWBackEnd<Impl>::DCacheCompletionEvent::description()
208 return "Cache completion event";
211 template <class Impl>
212 LWBackEnd<Impl>::LWBackEnd(Params *params)
213 : d2i(5, 5), i2e(5, 5), e2c(5, 5), numInstsToWB(5, 5),
214 trapSquash(false), tcSquash(false), cacheCompletionEvent(this),
215 dcacheInterface(params->dcacheInterface), width(params->backEndWidth),
218 numROBEntries = params->numROBEntries;
220 numDispatchEntries = 32;
221 maxOutstandingMemOps = params->maxOutstandingMemOps;
222 numWaitingMemOps = 0;
225 switchPending = false;
229 // Setup IQ and LSQ with their parameters here.
230 instsToDispatch = d2i.getWire(-1);
232 instsToExecute = i2e.getWire(-1);
234 dispatchWidth = params->dispatchWidth ? params->dispatchWidth : width;
235 issueWidth = params->issueWidth ? params->issueWidth : width;
236 wbWidth = params->wbWidth ? params->wbWidth : width;
237 commitWidth = params->commitWidth ? params->commitWidth : width;
239 LSQ.init(params, params->LQEntries, params->SQEntries, 0);
241 dispatchStatus = Running;
244 template <class Impl>
246 LWBackEnd<Impl>::name() const
248 return cpu->name() + ".backend";
251 template <class Impl>
253 LWBackEnd<Impl>::regStats()
255 using namespace Stats;
257 .init(cpu->number_of_threads)
258 .name(name() + ".ROB:cap_events")
259 .desc("number of cycles where ROB cap was active")
264 .init(cpu->number_of_threads)
265 .name(name() + ".ROB:cap_inst")
266 .desc("number of instructions held up by ROB cap")
271 .init(cpu->number_of_threads)
272 .name(name() +".IQ:cap_events" )
273 .desc("number of cycles where IQ cap was active")
278 .init(cpu->number_of_threads)
279 .name(name() + ".IQ:cap_inst")
280 .desc("number of instructions held up by IQ cap")
286 .init(cpu->number_of_threads)
287 .name(name() + ".ISSUE:count")
288 .desc("number of insts issued")
293 .init(cpu->number_of_threads)
294 .name(name() + ".ISSUE:swp")
295 .desc("number of swp insts issued")
300 .init(cpu->number_of_threads)
301 .name(name() + ".ISSUE:nop")
302 .desc("number of nop insts issued")
307 .init(cpu->number_of_threads)
308 .name(name() + ".ISSUE:refs")
309 .desc("number of memory reference insts issued")
314 .init(cpu->number_of_threads)
315 .name(name() + ".ISSUE:loads")
316 .desc("number of load insts issued")
321 .init(cpu->number_of_threads)
322 .name(name() + ".ISSUE:branches")
323 .desc("Number of branches issued")
328 .init(cpu->number_of_threads)
329 .name(name() + ".ISSUE:op_count")
330 .desc("number of insts issued")
335 for (int i=0; i<Num_OpClasses; ++i) {
336 stringstream subname;
337 subname << opClassStrings[i] << "_delay";
338 issue_delay_dist.subname(i, subname.str());
345 .init(cpu->number_of_threads)
346 .name(name() + ".LSQ:forw_loads")
347 .desc("number of loads forwarded via LSQ")
352 .init(cpu->number_of_threads)
353 .name(name() + ".ISSUE:addr_loads")
354 .desc("number of invalid-address loads")
359 .init(cpu->number_of_threads)
360 .name(name() + ".ISSUE:addr_swpfs")
361 .desc("number of invalid-address SW prefetches")
366 .init(cpu->number_of_threads)
367 .name(name() + ".LSQ:blocked_loads")
368 .desc("number of ready loads not issued due to memory disambiguation")
373 .name(name() + ".ISSUE:lsq_invert")
374 .desc("Number of times LSQ instruction issued early")
378 .init(issueWidth + 1)
379 .name(name() + ".ISSUE:issued_per_cycle")
380 .desc("Number of insts issued each cycle")
381 .flags(total | pdf | dist)
384 .init(Num_OpClasses,0,99,2)
385 .name(name() + ".ISSUE:")
386 .desc("cycles from operands ready to issue")
391 .init(Num_OpClasses, 0, 99, 2)
392 .name(name() + ".IQ:residence:")
393 .desc("cycles from dispatch to issue")
394 .flags(total | pdf | cdf )
396 for (int i = 0; i < Num_OpClasses; ++i) {
397 queue_res_dist.subname(i, opClassStrings[i]);
401 .init(cpu->number_of_threads)
402 .name(name() + ".WB:count")
403 .desc("cumulative count of insts written-back")
408 .init(cpu->number_of_threads)
409 .name(name() + ".WB:producers")
410 .desc("num instructions producing a value")
415 .init(cpu->number_of_threads)
416 .name(name() + ".WB:consumers")
417 .desc("num instructions consuming a value")
422 .init(cpu->number_of_threads)
423 .name(name() + ".WB:penalized")
424 .desc("number of instrctions required to write to 'other' IQ")
430 .name(name() + ".WB:penalized_rate")
431 .desc ("fraction of instructions written-back that wrote to 'other' IQ")
435 wb_penalized_rate = wb_penalized / writeback_count;
438 .name(name() + ".WB:fanout")
439 .desc("average fanout of values written-back")
443 wb_fanout = producer_inst / consumer_inst;
446 .name(name() + ".WB:rate")
447 .desc("insts written-back per cycle")
450 wb_rate = writeback_count / cpu->numCycles;
453 .init(cpu->number_of_threads)
454 .name(name() + ".COM:count")
455 .desc("Number of instructions committed")
460 .init(cpu->number_of_threads)
461 .name(name() + ".COM:swp_count")
462 .desc("Number of s/w prefetches committed")
467 .init(cpu->number_of_threads)
468 .name(name() + ".COM:refs")
469 .desc("Number of memory references committed")
474 .init(cpu->number_of_threads)
475 .name(name() + ".COM:loads")
476 .desc("Number of loads committed")
481 .init(cpu->number_of_threads)
482 .name(name() + ".COM:membars")
483 .desc("Number of memory barriers committed")
488 .init(cpu->number_of_threads)
489 .name(name() + ".COM:branches")
490 .desc("Number of branches committed")
494 .init(0,commitWidth,1)
495 .name(name() + ".COM:committed_per_cycle")
496 .desc("Number of insts commited each cycle")
501 // Commit-Eligible instructions...
503 // -> The number of instructions eligible to commit in those
504 // cycles where we reached our commit BW limit (less the number
505 // actually committed)
507 // -> The average value is computed over ALL CYCLES... not just
508 // the BW limited cycles
510 // -> The standard deviation is computed only over cycles where
511 // we reached the BW limit
514 .init(cpu->number_of_threads)
515 .name(name() + ".COM:bw_limited")
516 .desc("number of insts not committed due to BW limits")
520 commit_eligible_samples
521 .name(name() + ".COM:bw_lim_events")
522 .desc("number cycles where commit BW limit reached")
526 .init(cpu->number_of_threads)
527 .name(name() + ".COM:squashed_insts")
528 .desc("Number of instructions removed from inst list")
532 .init(cpu->number_of_threads)
533 .name(name() + ".COM:rob_squashed_insts")
534 .desc("Number of instructions removed from inst list when they reached the head of the ROB")
538 .name(name() + ".ROB:full_count")
539 .desc("number of cycles where ROB was full")
543 .init(cpu->number_of_threads)
544 .name(name() + ".ROB:occupancy")
545 .desc(name() + ".ROB occupancy (cumulative)")
550 .name(name() + ".ROB:full_rate")
551 .desc("ROB full per cycle")
553 ROB_full_rate = ROB_fcount / cpu->numCycles;
556 .name(name() + ".ROB:occ_rate")
557 .desc("ROB occupancy rate")
560 ROB_occ_rate = ROB_count / cpu->numCycles;
563 .init(cpu->number_of_threads,0,numROBEntries,2)
564 .name(name() + ".ROB:occ_dist")
565 .desc("ROB Occupancy per cycle")
570 template <class Impl>
572 LWBackEnd<Impl>::setCPU(FullCPU *cpu_ptr)
576 checker = cpu->checker;
579 template <class Impl>
581 LWBackEnd<Impl>::setCommBuffer(TimeBuffer<CommStruct> *_comm)
584 toIEW = comm->getWire(0);
585 fromCommit = comm->getWire(-1);
589 template <class Impl>
591 LWBackEnd<Impl>::checkInterrupts()
593 if (cpu->checkInterrupts &&
594 cpu->check_interrupts() &&
595 !cpu->inPalMode(thread->readPC()) &&
598 frontEnd->interruptPending = true;
599 if (robEmpty() && !LSQ.hasStoresToWB()) {
600 // Will need to squash all instructions currently in flight and have
601 // the interrupt handler restart at the last non-committed inst.
602 // Most of that can be handled through the trap() function. The
603 // processInterrupts() function really just checks for interrupts
604 // and then calls trap() if there is an interrupt present.
606 // Not sure which thread should be the one to interrupt. For now
607 // always do thread 0.
608 assert(!thread->inSyscall);
609 thread->inSyscall = true;
611 // CPU will handle implementation of the interrupt.
612 cpu->processInterrupts();
614 // Now squash or record that I need to squash this cycle.
615 commitStatus = TrapPending;
617 // Exit state update mode to avoid accidental updating.
618 thread->inSyscall = false;
620 // Generate trap squash event.
623 DPRINTF(BE, "Interrupt detected.\n");
625 DPRINTF(BE, "Interrupt must wait for ROB to drain.\n");
630 template <class Impl>
632 LWBackEnd<Impl>::handleFault(Fault &fault, Tick latency)
634 DPRINTF(BE, "Handling fault!\n");
636 assert(!thread->inSyscall);
638 thread->inSyscall = true;
640 // Consider holding onto the trap and waiting until the trap event
641 // happens for this to be executed.
642 fault->invoke(thread->getTCProxy());
644 // Exit state update mode to avoid accidental updating.
645 thread->inSyscall = false;
647 commitStatus = TrapPending;
649 // Generate trap squash event.
650 generateTrapEvent(latency);
654 template <class Impl>
656 LWBackEnd<Impl>::tick()
658 DPRINTF(BE, "Ticking back end\n");
660 if (switchPending && robEmpty() && !LSQ.hasStoresToWB()) {
661 cpu->signalSwitched();
665 ROB_count[0]+= numInsts;
669 // Read in any done instruction information and update the IQ or LSQ.
678 } else if (tcSquash) {
683 if (dispatchStatus != Blocked) {
686 checkDispatchStatus();
689 if (commitStatus != TrapPending) {
695 LSQ.writebackStores();
697 DPRINTF(BE, "Waiting insts: %i, mem ops: %i, ROB entries in use: %i, "
698 "LSQ loads: %i, LSQ stores: %i\n",
699 waitingInsts, numWaitingMemOps, numInsts,
700 LSQ.numLoads(), LSQ.numStores());
703 assert(numInsts == instList.size());
704 assert(waitingInsts == waitingList.size());
705 assert(numWaitingMemOps == waitingMemOps.size());
706 assert(!switchedOut);
710 template <class Impl>
712 LWBackEnd<Impl>::updateStructures()
714 if (fromCommit->doneSeqNum) {
715 LSQ.commitLoads(fromCommit->doneSeqNum);
716 LSQ.commitStores(fromCommit->doneSeqNum);
719 if (fromCommit->nonSpecSeqNum) {
720 if (fromCommit->uncached) {
721 // LSQ.executeLoad(fromCommit->lqIdx);
723 // IQ.scheduleNonSpec(
724 // fromCommit->nonSpecSeqNum);
729 template <class Impl>
731 LWBackEnd<Impl>::addToLSQ(DynInstPtr &inst)
733 // Do anything LSQ specific here?
737 template <class Impl>
739 LWBackEnd<Impl>::dispatchInsts()
741 DPRINTF(BE, "Trying to dispatch instructions.\n");
743 while (numInsts < numROBEntries &&
744 numWaitingMemOps < maxOutstandingMemOps) {
745 // Get instruction from front of time buffer
746 DynInstPtr inst = frontEnd->getInst();
749 } else if (inst->isSquashed()) {
754 instList.push_front(inst);
758 DPRINTF(BE, "Dispatching instruction [sn:%lli] PC:%#x\n",
759 inst->seqNum, inst->readPC());
761 for (int i = 0; i < inst->numDestRegs(); ++i)
762 renameTable[inst->destRegIdx(i)] = inst;
764 if (inst->isMemBarrier() || inst->isWriteBarrier()) {
766 DPRINTF(BE, "Instruction [sn:%lli] is waiting on "
767 "barrier [sn:%lli].\n",
768 inst->seqNum, memBarrier->seqNum);
769 memBarrier->addMemDependent(inst);
770 inst->addSrcMemInst(memBarrier);
773 inst->setCanCommit();
774 } else if (inst->readyToIssue() &&
775 !inst->isNonSpeculative() &&
776 !inst->isStoreConditional()) {
777 if (inst->isMemRef()) {
781 DPRINTF(BE, "Instruction [sn:%lli] is waiting on "
782 "barrier [sn:%lli].\n",
783 inst->seqNum, memBarrier->seqNum);
784 memBarrier->addMemDependent(inst);
785 inst->addSrcMemInst(memBarrier);
786 addWaitingMemOp(inst);
788 waitingList.push_front(inst);
789 inst->iqIt = waitingList.begin();
790 inst->iqItValid = true;
793 DPRINTF(BE, "Instruction [sn:%lli] ready, addding to "
798 } else if (inst->isNop()) {
799 DPRINTF(BE, "Nop encountered [sn:%lli], skipping exeList.\n",
803 inst->setCanCommit();
805 DPRINTF(BE, "Instruction [sn:%lli] ready, addding to "
811 if (inst->isNonSpeculative() || inst->isStoreConditional()) {
812 inst->setCanCommit();
813 DPRINTF(BE, "Adding non speculative instruction\n");
816 if (inst->isMemRef()) {
817 addWaitingMemOp(inst);
820 memBarrier->addMemDependent(inst);
821 inst->addSrcMemInst(memBarrier);
823 DPRINTF(BE, "Instruction [sn:%lli] is waiting on "
824 "barrier [sn:%lli].\n",
825 inst->seqNum, memBarrier->seqNum);
829 DPRINTF(BE, "Instruction [sn:%lli] not ready, addding to "
832 waitingList.push_front(inst);
833 inst->iqIt = waitingList.begin();
834 inst->iqItValid = true;
839 // Check if IQ or LSQ is full. If so we'll need to break and stop
840 // removing instructions. Also update the number of insts to remove
841 // from the queue. Check here if we don't care about exact stall
846 DPRINTF(BE, "IQ is full!\n");
848 } else if (LSQ.isFull()) {
849 DPRINTF(BE, "LSQ is full!\n");
851 } else if (isFull()) {
852 DPRINTF(BE, "ROB is full!\n");
864 template <class Impl>
866 LWBackEnd<Impl>::dispatchStall()
868 dispatchStatus = Blocked;
869 if (!cpu->decoupledFrontEnd) {
870 // Tell front end to stall here through a timebuffer, or just tell
875 template <class Impl>
877 LWBackEnd<Impl>::checkDispatchStatus()
879 DPRINTF(BE, "Checking dispatch status\n");
880 assert(dispatchStatus == Blocked);
881 if (!LSQ.isFull() && !isFull()) {
882 DPRINTF(BE, "Dispatch no longer blocked\n");
883 dispatchStatus = Running;
888 template <class Impl>
890 LWBackEnd<Impl>::executeInsts()
892 DPRINTF(BE, "Trying to execute instructions\n");
894 int num_executed = 0;
895 while (!exeList.empty() && num_executed < issueWidth) {
896 DynInstPtr inst = exeList.top();
898 DPRINTF(BE, "Executing inst [sn:%lli] PC: %#x\n",
899 inst->seqNum, inst->readPC());
901 // Check if the instruction is squashed; if so then skip it
902 // and don't count it towards the FU usage.
903 if (inst->isSquashed()) {
904 DPRINTF(BE, "Execute: Instruction was squashed.\n");
906 // Not sure how to handle this plus the method of sending # of
907 // instructions to use. Probably will just have to count it
908 // towards the bandwidth usage, but not the FU usage.
911 // Consider this instruction executed so that commit can go
912 // ahead and retire the instruction.
915 // Not sure if I should set this here or just let commit try to
916 // commit any squashed instructions. I like the latter a bit more.
917 inst->setCanCommit();
919 // ++iewExecSquashedInsts;
925 Fault fault = NoFault;
927 // Execute instruction.
928 // Note that if the instruction faults, it will be handled
929 // at the commit stage.
930 if (inst->isMemRef() &&
931 (!inst->isDataPrefetch() && !inst->isInstPrefetch())) {
932 if (dcacheInterface->isBlocked()) {
933 // Should I move the instruction aside?
934 DPRINTF(BE, "Execute: dcache is blocked\n");
937 DPRINTF(BE, "Execute: Initiating access for memory "
940 if (inst->isLoad()) {
941 LSQ.executeLoad(inst);
942 } else if (inst->isStore()) {
943 LSQ.executeStore(inst);
944 if (inst->req && !(inst->req->flags & LOCKED)) {
950 panic("Unknown mem type!");
960 updateExeInstStats(inst);
967 if (inst->mispredicted()) {
968 squashDueToBranch(inst);
970 } else if (LSQ.violation()) {
971 // Get the DynInst that caused the violation. Note that this
972 // clears the violation signal.
974 violator = LSQ.getMemDepViolator();
976 DPRINTF(BE, "LDSTQ detected a violation. Violator PC: "
977 "%#x, inst PC: %#x. Addr is: %#x.\n",
978 violator->readPC(), inst->readPC(), inst->physEffAddr);
981 squashDueToMemViolation(inst);
985 issued_ops[0]+= num_executed;
986 n_issued_dist[num_executed]++;
991 LWBackEnd<Impl>::instToCommit(DynInstPtr &inst)
994 DPRINTF(BE, "Sending instructions to commit [sn:%lli] PC %#x.\n",
995 inst->seqNum, inst->readPC());
997 if (!inst->isSquashed()) {
998 DPRINTF(BE, "Writing back instruction [sn:%lli] PC %#x.\n",
999 inst->seqNum, inst->readPC());
1001 inst->setCanCommit();
1003 if (inst->isExecuted()) {
1004 inst->setResultReady();
1005 int dependents = wakeDependents(inst);
1008 consumer_inst[0]+= dependents;
1013 writeback_count[0]++;
1016 template <class Impl>
1018 LWBackEnd<Impl>::writebackInsts()
1020 int wb_width = wbWidth;
1021 // Using this method I'm not quite sure how to prevent an
1022 // instruction from waking its own dependents multiple times,
1023 // without the guarantee that commit always has enough bandwidth
1024 // to accept all instructions being written back. This guarantee
1025 // might not be too unrealistic.
1026 InstListIt wb_inst_it = writeback.begin();
1027 InstListIt wb_end_it = writeback.end();
1029 int consumer_insts = 0;
1031 for (; inst_num < wb_width &&
1032 wb_inst_it != wb_end_it; inst_num++) {
1033 DynInstPtr inst = (*wb_inst_it);
1035 // Some instructions will be sent to commit without having
1036 // executed because they need commit to handle them.
1037 // E.g. Uncached loads have not actually executed when they
1038 // are first sent to commit. Instead commit must tell the LSQ
1039 // when it's ready to execute the uncached load.
1040 if (!inst->isSquashed()) {
1041 DPRINTF(BE, "Writing back instruction [sn:%lli] PC %#x.\n",
1042 inst->seqNum, inst->readPC());
1044 inst->setCanCommit();
1045 inst->setResultReady();
1047 if (inst->isExecuted()) {
1048 int dependents = wakeDependents(inst);
1051 consumer_insts+= dependents;
1056 writeback.erase(wb_inst_it++);
1058 LSQ.writebackStores();
1059 consumer_inst[0]+= consumer_insts;
1060 writeback_count[0]+= inst_num;
1063 template <class Impl>
1065 LWBackEnd<Impl>::commitInst(int inst_num)
1067 // Read instruction from the head of the ROB
1068 DynInstPtr inst = instList.back();
1070 // Make sure instruction is valid
1073 if (!inst->readyToCommit())
1076 DPRINTF(BE, "Trying to commit instruction [sn:%lli] PC:%#x\n",
1077 inst->seqNum, inst->readPC());
1079 thread->setPC(inst->readPC());
1080 thread->setNextPC(inst->readNextPC());
1081 inst->reachedCommit = true;
1083 // If the instruction is not executed yet, then it is a non-speculative
1084 // or store inst. Signal backwards that it should be executed.
1085 if (!inst->isExecuted()) {
1086 if (inst->isNonSpeculative() ||
1087 inst->isStoreConditional() ||
1088 inst->isMemBarrier() ||
1089 inst->isWriteBarrier()) {
1091 // Hack to make sure syscalls aren't executed until all stores
1092 // write back their data. This direct communication shouldn't
1093 // be used for anything other than this.
1094 if (inst_num > 0 || LSQ.hasStoresToWB())
1096 if ((inst->isMemBarrier() || inst->isWriteBarrier() ||
1097 inst->isQuiesce()) &&
1098 LSQ.hasStoresToWB())
1101 DPRINTF(BE, "Waiting for all stores to writeback.\n");
1105 DPRINTF(BE, "Encountered a store or non-speculative "
1106 "instruction at the head of the ROB, PC %#x.\n",
1109 if (inst->isMemBarrier() || inst->isWriteBarrier()) {
1110 DPRINTF(BE, "Waking dependents on barrier [sn:%lli]\n",
1113 wakeDependents(inst, true);
1114 if (memBarrier == inst)
1116 inst->clearMemDependents();
1119 // Send back the non-speculative instruction's sequence number.
1120 if (inst->iqItValid) {
1121 DPRINTF(BE, "Removing instruction from waiting list\n");
1122 waitingList.erase(inst->iqIt);
1123 inst->iqItValid = false;
1125 assert(waitingInsts >= 0);
1126 if (inst->isStore())
1127 removeWaitingMemOp(inst);
1132 // Change the instruction so it won't try to commit again until
1134 inst->clearCanCommit();
1136 // ++commitNonSpecStalls;
1139 } else if (inst->isLoad()) {
1140 DPRINTF(BE, "[sn:%lli]: Uncached load, PC %#x.\n",
1141 inst->seqNum, inst->readPC());
1143 // Send back the non-speculative instruction's sequence
1144 // number. Maybe just tell the lsq to re-execute the load.
1146 // Send back the non-speculative instruction's sequence number.
1147 if (inst->iqItValid) {
1148 DPRINTF(BE, "Removing instruction from waiting list\n");
1149 waitingList.erase(inst->iqIt);
1150 inst->iqItValid = false;
1152 assert(waitingInsts >= 0);
1153 removeWaitingMemOp(inst);
1155 replayMemInst(inst);
1157 inst->clearCanCommit();
1161 panic("Trying to commit un-executed instruction "
1162 "of unknown type!\n");
1166 // Not handled for now.
1167 assert(!inst->isThreadSync());
1168 assert(inst->memDepReady());
1169 // Stores will mark themselves as totally completed as they need
1170 // to wait to writeback to memory. @todo: Hack...attempt to fix
1171 // having the checker be forced to wait until a store completes in
1172 // order to check all of the instructions. If the store at the
1173 // head of the check list misses, but a later store hits, then
1174 // loads in the checker may see the younger store values instead
1175 // of the store they should see. Either the checker needs its own
1176 // memory (annoying to update), its own store buffer (how to tell
1177 // which value is correct?), or something else...
1178 if (!inst->isStore()) {
1179 inst->setCompleted();
1181 // Check if the instruction caused a fault. If so, trap.
1182 Fault inst_fault = inst->getFault();
1184 // Use checker prior to updating anything due to traps or PC
1187 checker->tick(inst);
1190 if (inst_fault != NoFault) {
1191 DPRINTF(BE, "Inst [sn:%lli] PC %#x has a fault\n",
1192 inst->seqNum, inst->readPC());
1194 // Instruction is completed as it has a fault.
1195 inst->setCompleted();
1197 if (LSQ.hasStoresToWB()) {
1198 DPRINTF(BE, "Stores still in flight, will wait until drained.\n");
1200 } else if (inst_num != 0) {
1201 DPRINTF(BE, "Will wait until instruction is head of commit group.\n");
1203 } else if (checker && inst->isStore()) {
1204 checker->tick(inst);
1208 static_cast<TheISA::MachInst>(inst->staticInst->machInst));
1210 handleFault(inst_fault);
1212 #else // !FULL_SYSTEM
1213 panic("fault (%d) detected @ PC %08p", inst_fault,
1215 #endif // FULL_SYSTEM
1220 for (int i = 0; i < inst->numDestRegs(); ++i) {
1221 DPRINTF(BE, "Commit rename map setting reg %i to [sn:%lli]\n",
1222 (int)inst->destRegIdx(i), inst->seqNum);
1223 thread->renameTable[inst->destRegIdx(i)] = inst;
1227 if (inst->traceData) {
1228 inst->traceData->setFetchSeq(inst->seqNum);
1229 inst->traceData->setCPSeq(thread->numInst);
1230 inst->traceData->finalize();
1231 inst->traceData = NULL;
1234 inst->clearDependents();
1236 frontEnd->addFreeRegs(freed_regs);
1238 instList.pop_back();
1241 ++thread->funcExeInst;
1242 // Maybe move this to where the fault is handled; if the fault is
1243 // handled, don't try to set this myself as the fault will set it.
1244 // If not, then I set thread->PC = thread->nextPC and
1245 // thread->nextPC = thread->nextPC + 4.
1246 thread->setPC(thread->readNextPC());
1247 thread->setNextPC(thread->readNextPC() + sizeof(TheISA::MachInst));
1248 updateComInstStats(inst);
1250 // Write the done sequence number here.
1251 toIEW->doneSeqNum = inst->seqNum;
1252 lastCommitCycle = curTick;
1259 assert(!thread->inSyscall && !thread->trapPending);
1260 oldpc = thread->readPC();
1261 cpu->system->pcEventQueue.service(
1262 thread->getTCProxy());
1264 } while (oldpc != thread->readPC());
1266 DPRINTF(BE, "PC skip function event, stopping commit\n");
1274 template <class Impl>
1276 LWBackEnd<Impl>::commitInsts()
1278 // Not sure this should be a loop or not.
1280 while (!instList.empty() && inst_num < commitWidth) {
1281 if (instList.back()->isSquashed()) {
1282 instList.back()->clearDependents();
1283 instList.pop_back();
1285 ROBSquashedInsts[instList.back()->threadNumber]++;
1289 if (!commitInst(inst_num++)) {
1290 DPRINTF(BE, "Can't commit, Instruction [sn:%lli] PC "
1291 "%#x is head of ROB and not ready\n",
1292 instList.back()->seqNum, instList.back()->readPC());
1297 n_committed_dist.sample(inst_num);
1300 template <class Impl>
1302 LWBackEnd<Impl>::squash(const InstSeqNum &sn)
1307 InstListIt waiting_list_end = waitingList.end();
1308 InstListIt insts_it = waitingList.begin();
1310 while (insts_it != waiting_list_end && (*insts_it)->seqNum > sn)
1312 if ((*insts_it)->isSquashed()) {
1316 DPRINTF(BE, "Squashing instruction on waitingList PC %#x, [sn:%lli].\n",
1317 (*insts_it)->readPC(),
1318 (*insts_it)->seqNum);
1320 if ((*insts_it)->isMemRef()) {
1321 DPRINTF(BE, "Squashing a waiting mem op [sn:%lli]\n",
1322 (*insts_it)->seqNum);
1323 removeWaitingMemOp((*insts_it));
1326 waitingList.erase(insts_it++);
1329 assert(waitingInsts >= 0);
1331 insts_it = instList.begin();
1333 while (!instList.empty() && (*insts_it)->seqNum > sn)
1335 if ((*insts_it)->isSquashed()) {
1339 DPRINTF(BE, "Squashing instruction on inst list PC %#x, [sn:%lli].\n",
1340 (*insts_it)->readPC(),
1341 (*insts_it)->seqNum);
1343 // Mark the instruction as squashed, and ready to commit so that
1344 // it can drain out of the pipeline.
1345 (*insts_it)->setSquashed();
1347 (*insts_it)->setCanCommit();
1349 (*insts_it)->removeInROB();
1351 for (int i = 0; i < (*insts_it)->numDestRegs(); ++i) {
1352 DynInstPtr prev_dest = (*insts_it)->getPrevDestInst(i);
1353 DPRINTF(BE, "Commit rename map setting reg %i to [sn:%lli]\n",
1354 (int)(*insts_it)->destRegIdx(i), prev_dest->seqNum);
1355 renameTable[(*insts_it)->destRegIdx(i)] = prev_dest;
1359 (*insts_it)->clearDependents();
1361 squashedInsts[(*insts_it)->threadNumber]++;
1363 instList.erase(insts_it++);
1367 insts_it = waitingList.begin();
1368 while (!waitingList.empty() && insts_it != waitingList.end()) {
1369 if ((*insts_it)->seqNum < sn) {
1373 assert((*insts_it)->isSquashed());
1375 waitingList.erase(insts_it++);
1379 while (memBarrier && memBarrier->seqNum > sn) {
1380 DPRINTF(BE, "[sn:%lli] Memory barrier squashed (or previously "
1381 "squashed)\n", memBarrier->seqNum);
1382 memBarrier->clearMemDependents();
1383 if (memBarrier->memDepReady()) {
1384 DPRINTF(BE, "No previous barrier\n");
1387 std::list<DynInstPtr> &srcs = memBarrier->getMemSrcs();
1388 memBarrier = srcs.front();
1390 assert(srcs.empty());
1391 DPRINTF(BE, "Previous barrier: [sn:%lli]\n",
1392 memBarrier->seqNum);
1396 frontEnd->addFreeRegs(freed_regs);
1399 template <class Impl>
1401 LWBackEnd<Impl>::squashFromTC()
1403 InstSeqNum squashed_inst = robEmpty() ? 0 : instList.back()->seqNum - 1;
1404 squash(squashed_inst);
1405 frontEnd->squash(squashed_inst, thread->readPC(),
1407 frontEnd->interruptPending = false;
1409 thread->trapPending = false;
1410 thread->inSyscall = false;
1412 commitStatus = Running;
1415 template <class Impl>
1417 LWBackEnd<Impl>::squashFromTrap()
1419 InstSeqNum squashed_inst = robEmpty() ? 0 : instList.back()->seqNum - 1;
1420 squash(squashed_inst);
1421 frontEnd->squash(squashed_inst, thread->readPC(),
1423 frontEnd->interruptPending = false;
1425 thread->trapPending = false;
1426 thread->inSyscall = false;
1428 commitStatus = Running;
1431 template <class Impl>
1433 LWBackEnd<Impl>::squashDueToBranch(DynInstPtr &inst)
1435 // Update the branch predictor state I guess
1436 DPRINTF(BE, "Squashing due to branch [sn:%lli], will restart at PC %#x\n",
1437 inst->seqNum, inst->readNextPC());
1438 squash(inst->seqNum);
1439 frontEnd->squash(inst->seqNum, inst->readNextPC(),
1440 true, inst->mispredicted());
1443 template <class Impl>
1445 LWBackEnd<Impl>::squashDueToMemViolation(DynInstPtr &inst)
1447 // Update the branch predictor state I guess
1448 DPRINTF(BE, "Squashing due to violation [sn:%lli], will restart at PC %#x\n",
1449 inst->seqNum, inst->readNextPC());
1450 squash(inst->seqNum);
1451 frontEnd->squash(inst->seqNum, inst->readNextPC(),
1452 false, inst->mispredicted());
1455 template <class Impl>
1457 LWBackEnd<Impl>::squashDueToMemBlocked(DynInstPtr &inst)
1459 DPRINTF(IEW, "Memory blocked, squashing load and younger insts, "
1460 "PC: %#x [sn:%i].\n", inst->readPC(), inst->seqNum);
1462 squash(inst->seqNum - 1);
1463 frontEnd->squash(inst->seqNum - 1, inst->readPC());
1466 template <class Impl>
1468 LWBackEnd<Impl>::fetchFault(Fault &fault)
1470 faultFromFetch = fault;
1471 fetchHasFault = true;
1474 template <class Impl>
1476 LWBackEnd<Impl>::switchOut()
1478 switchPending = true;
1481 template <class Impl>
1483 LWBackEnd<Impl>::doSwitchOut()
1486 switchPending = false;
1487 // Need to get rid of all committed, non-speculative state and write it
1488 // to memory/TC. In this case this is stores that have committed and not
1489 // yet written back.
1491 assert(!LSQ.hasStoresToWB());
1498 template <class Impl>
1500 LWBackEnd<Impl>::takeOverFrom(ThreadContext *old_xc)
1502 switchedOut = false;
1507 numWaitingMemOps = 0;
1508 waitingMemOps.clear();
1510 switchedOut = false;
1511 dispatchStatus = Running;
1512 commitStatus = Running;
1513 LSQ.takeOverFrom(old_xc);
1516 template <class Impl>
1518 LWBackEnd<Impl>::updateExeInstStats(DynInstPtr &inst)
1520 int thread_number = inst->threadNumber;
1523 // Pick off the software prefetches
1526 if (inst->isDataPrefetch())
1527 exe_swp[thread_number]++;
1529 exe_inst[thread_number]++;
1531 exe_inst[thread_number]++;
1535 // Control operations
1537 if (inst->isControl())
1538 exe_branches[thread_number]++;
1541 // Memory operations
1543 if (inst->isMemRef()) {
1544 exe_refs[thread_number]++;
1547 exe_loads[thread_number]++;
1551 template <class Impl>
1553 LWBackEnd<Impl>::updateComInstStats(DynInstPtr &inst)
1555 unsigned tid = inst->threadNumber;
1557 // keep an instruction count
1563 // Pick off the software prefetches
1566 if (inst->isDataPrefetch()) {
1567 stat_com_swp[tid]++;
1569 stat_com_inst[tid]++;
1572 stat_com_inst[tid]++;
1576 // Control Instructions
1578 if (inst->isControl())
1579 stat_com_branches[tid]++;
1582 // Memory references
1584 if (inst->isMemRef()) {
1585 stat_com_refs[tid]++;
1587 if (inst->isLoad()) {
1588 stat_com_loads[tid]++;
1592 if (inst->isMemBarrier()) {
1593 stat_com_membars[tid]++;
1597 template <class Impl>
1599 LWBackEnd<Impl>::dumpInsts()
1604 InstListIt inst_list_it = --(instList.end());
1606 cprintf("ExeList size: %i\n", exeList.size());
1608 cprintf("Inst list size: %i\n", instList.size());
1610 while (inst_list_it != instList.end())
1612 cprintf("Instruction:%i\n",
1614 if (!(*inst_list_it)->isSquashed()) {
1615 if (!(*inst_list_it)->isIssued()) {
1617 cprintf("Count:%i\n", valid_num);
1618 } else if ((*inst_list_it)->isMemRef() &&
1619 !(*inst_list_it)->memOpDone) {
1620 // Loads that have not been marked as executed still count
1621 // towards the total instructions.
1623 cprintf("Count:%i\n", valid_num);
1627 cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
1628 "Issued:%i\nSquashed:%i\n",
1629 (*inst_list_it)->readPC(),
1630 (*inst_list_it)->seqNum,
1631 (*inst_list_it)->threadNumber,
1632 (*inst_list_it)->isIssued(),
1633 (*inst_list_it)->isSquashed());
1635 if ((*inst_list_it)->isMemRef()) {
1636 cprintf("MemOpDone:%i\n", (*inst_list_it)->memOpDone);
1645 cprintf("Waiting list size: %i\n", waitingList.size());
1647 inst_list_it = --(waitingList.end());
1649 while (inst_list_it != waitingList.end())
1651 cprintf("Instruction:%i\n",
1653 if (!(*inst_list_it)->isSquashed()) {
1654 if (!(*inst_list_it)->isIssued()) {
1656 cprintf("Count:%i\n", valid_num);
1657 } else if ((*inst_list_it)->isMemRef() &&
1658 !(*inst_list_it)->memOpDone) {
1659 // Loads that have not been marked as executed still count
1660 // towards the total instructions.
1662 cprintf("Count:%i\n", valid_num);
1666 cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
1667 "Issued:%i\nSquashed:%i\n",
1668 (*inst_list_it)->readPC(),
1669 (*inst_list_it)->seqNum,
1670 (*inst_list_it)->threadNumber,
1671 (*inst_list_it)->isIssued(),
1672 (*inst_list_it)->isSquashed());
1674 if ((*inst_list_it)->isMemRef()) {
1675 cprintf("MemOpDone:%i\n", (*inst_list_it)->memOpDone);
1684 cprintf("waitingMemOps list size: %i\n", waitingMemOps.size());
1686 MemIt waiting_it = waitingMemOps.begin();
1688 while (waiting_it != waitingMemOps.end())
1690 cprintf("[sn:%lli] ", (*waiting_it));