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.
32 #include "config/full_system.hh"
33 #include "config/use_checker.hh"
38 #include "arch/utility.hh"
39 #include "base/loader/symtab.hh"
40 #include "base/timebuf.hh"
41 #include "cpu/exetrace.hh"
42 #include "cpu/o3/commit.hh"
43 #include "cpu/o3/thread_state.hh"
46 #include "cpu/checker/cpu.hh"
50 DefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
52 : Event(&mainEventQueue, CPU_Tick_Pri), commit(_commit), tid(_tid)
54 this->setFlags(Event::AutoDelete);
59 DefaultCommit<Impl>::TrapEvent::process()
61 // This will get reset by commit if it was switched out at the
62 // time of this event processing.
63 commit->trapSquash[tid] = true;
68 DefaultCommit<Impl>::TrapEvent::description()
74 DefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, Params *params)
77 iewToCommitDelay(params->iewToCommitDelay),
78 commitToIEWDelay(params->commitToIEWDelay),
79 renameToROBDelay(params->renameToROBDelay),
80 fetchToCommitDelay(params->commitToFetchDelay),
81 renameWidth(params->renameWidth),
82 commitWidth(params->commitWidth),
83 numThreads(params->numberOfThreads),
86 trapLatency(params->trapLatency)
89 _nextStatus = Inactive;
90 std::string policy = params->smtCommitPolicy;
92 //Convert string to lowercase
93 std::transform(policy.begin(), policy.end(), policy.begin(),
94 (int(*)(int)) tolower);
96 //Assign commit policy
97 if (policy == "aggressive"){
98 commitPolicy = Aggressive;
100 DPRINTF(Commit,"Commit Policy set to Aggressive.");
101 } else if (policy == "roundrobin"){
102 commitPolicy = RoundRobin;
104 //Set-Up Priority List
105 for (int tid=0; tid < numThreads; tid++) {
106 priority_list.push_back(tid);
109 DPRINTF(Commit,"Commit Policy set to Round Robin.");
110 } else if (policy == "oldestready"){
111 commitPolicy = OldestReady;
113 DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
115 assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
116 "RoundRobin,OldestReady}");
119 for (int i=0; i < numThreads; i++) {
120 commitStatus[i] = Idle;
121 changedROBNumEntries[i] = false;
122 checkEmptyROB[i] = false;
123 trapInFlight[i] = false;
124 committedStores[i] = false;
125 trapSquash[i] = false;
127 PC[i] = nextPC[i] = nextNPC[i] = 0;
134 template <class Impl>
136 DefaultCommit<Impl>::name() const
138 return cpu->name() + ".commit";
141 template <class Impl>
143 DefaultCommit<Impl>::regStats()
145 using namespace Stats;
147 .name(name() + ".commitCommittedInsts")
148 .desc("The number of committed instructions")
149 .prereq(commitCommittedInsts);
151 .name(name() + ".commitSquashedInsts")
152 .desc("The number of squashed insts skipped by commit")
153 .prereq(commitSquashedInsts);
155 .name(name() + ".commitSquashEvents")
156 .desc("The number of times commit is told to squash")
157 .prereq(commitSquashEvents);
159 .name(name() + ".commitNonSpecStalls")
160 .desc("The number of times commit has been forced to stall to "
161 "communicate backwards")
162 .prereq(commitNonSpecStalls);
164 .name(name() + ".branchMispredicts")
165 .desc("The number of times a branch was mispredicted")
166 .prereq(branchMispredicts);
168 .init(0,commitWidth,1)
169 .name(name() + ".COM:committed_per_cycle")
170 .desc("Number of insts commited each cycle")
175 .init(cpu->number_of_threads)
176 .name(name() + ".COM:count")
177 .desc("Number of instructions committed")
182 .init(cpu->number_of_threads)
183 .name(name() + ".COM:swp_count")
184 .desc("Number of s/w prefetches committed")
189 .init(cpu->number_of_threads)
190 .name(name() + ".COM:refs")
191 .desc("Number of memory references committed")
196 .init(cpu->number_of_threads)
197 .name(name() + ".COM:loads")
198 .desc("Number of loads committed")
203 .init(cpu->number_of_threads)
204 .name(name() + ".COM:membars")
205 .desc("Number of memory barriers committed")
210 .init(cpu->number_of_threads)
211 .name(name() + ".COM:branches")
212 .desc("Number of branches committed")
217 .init(cpu->number_of_threads)
218 .name(name() + ".COM:bw_limited")
219 .desc("number of insts not committed due to BW limits")
223 commitEligibleSamples
224 .name(name() + ".COM:bw_lim_events")
225 .desc("number cycles where commit BW limit reached")
229 template <class Impl>
231 DefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
236 template <class Impl>
238 DefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
242 // Setup wire to send information back to IEW.
243 toIEW = timeBuffer->getWire(0);
245 // Setup wire to read data from IEW (for the ROB).
246 robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
249 template <class Impl>
251 DefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
255 // Setup wire to get instructions from rename (for the ROB).
256 fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
259 template <class Impl>
261 DefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
263 renameQueue = rq_ptr;
265 // Setup wire to get instructions from rename (for the ROB).
266 fromRename = renameQueue->getWire(-renameToROBDelay);
269 template <class Impl>
271 DefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
275 // Setup wire to get instructions from IEW.
276 fromIEW = iewQueue->getWire(-iewToCommitDelay);
279 template <class Impl>
281 DefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
283 iewStage = iew_stage;
288 DefaultCommit<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
290 activeThreads = at_ptr;
293 template <class Impl>
295 DefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
297 for (int i=0; i < numThreads; i++) {
298 renameMap[i] = &rm_ptr[i];
302 template <class Impl>
304 DefaultCommit<Impl>::setROB(ROB *rob_ptr)
309 template <class Impl>
311 DefaultCommit<Impl>::initStage()
313 rob->setActiveThreads(activeThreads);
316 // Broadcast the number of free entries.
317 for (int i=0; i < numThreads; i++) {
318 toIEW->commitInfo[i].usedROB = true;
319 toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
320 toIEW->commitInfo[i].emptyROB = true;
323 // Commit must broadcast the number of free entries it has at the
324 // start of the simulation, so it starts as active.
325 cpu->activateStage(O3CPU::CommitIdx);
327 cpu->activityThisCycle();
328 trapLatency = cpu->cycles(trapLatency);
331 template <class Impl>
333 DefaultCommit<Impl>::drain()
340 template <class Impl>
342 DefaultCommit<Impl>::switchOut()
345 drainPending = false;
349 template <class Impl>
351 DefaultCommit<Impl>::resume()
353 drainPending = false;
356 template <class Impl>
358 DefaultCommit<Impl>::takeOverFrom()
362 _nextStatus = Inactive;
363 for (int i=0; i < numThreads; i++) {
364 commitStatus[i] = Idle;
365 changedROBNumEntries[i] = false;
366 trapSquash[i] = false;
373 template <class Impl>
375 DefaultCommit<Impl>::updateStatus()
377 // reset ROB changed variable
378 std::list<unsigned>::iterator threads = activeThreads->begin();
379 std::list<unsigned>::iterator end = activeThreads->end();
381 while (threads != end) {
382 unsigned tid = *threads++;
384 changedROBNumEntries[tid] = false;
386 // Also check if any of the threads has a trap pending
387 if (commitStatus[tid] == TrapPending ||
388 commitStatus[tid] == FetchTrapPending) {
389 _nextStatus = Active;
393 if (_nextStatus == Inactive && _status == Active) {
394 DPRINTF(Activity, "Deactivating stage.\n");
395 cpu->deactivateStage(O3CPU::CommitIdx);
396 } else if (_nextStatus == Active && _status == Inactive) {
397 DPRINTF(Activity, "Activating stage.\n");
398 cpu->activateStage(O3CPU::CommitIdx);
401 _status = _nextStatus;
404 template <class Impl>
406 DefaultCommit<Impl>::setNextStatus()
410 std::list<unsigned>::iterator threads = activeThreads->begin();
411 std::list<unsigned>::iterator end = activeThreads->end();
413 while (threads != end) {
414 unsigned tid = *threads++;
416 if (commitStatus[tid] == ROBSquashing) {
421 squashCounter = squashes;
423 // If commit is currently squashing, then it will have activity for the
424 // next cycle. Set its next status as active.
426 _nextStatus = Active;
430 template <class Impl>
432 DefaultCommit<Impl>::changedROBEntries()
434 std::list<unsigned>::iterator threads = activeThreads->begin();
435 std::list<unsigned>::iterator end = activeThreads->end();
437 while (threads != end) {
438 unsigned tid = *threads++;
440 if (changedROBNumEntries[tid]) {
448 template <class Impl>
450 DefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
452 return rob->numFreeEntries(tid);
455 template <class Impl>
457 DefaultCommit<Impl>::generateTrapEvent(unsigned tid)
459 DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
461 TrapEvent *trap = new TrapEvent(this, tid);
463 trap->schedule(curTick + trapLatency);
464 trapInFlight[tid] = true;
467 template <class Impl>
469 DefaultCommit<Impl>::generateTCEvent(unsigned tid)
471 assert(!trapInFlight[tid]);
472 DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
474 tcSquash[tid] = true;
477 template <class Impl>
479 DefaultCommit<Impl>::squashAll(unsigned tid)
481 // If we want to include the squashing instruction in the squash,
482 // then use one older sequence number.
483 // Hopefully this doesn't mess things up. Basically I want to squash
484 // all instructions of this thread.
485 InstSeqNum squashed_inst = rob->isEmpty() ?
486 0 : rob->readHeadInst(tid)->seqNum - 1;
488 // All younger instructions will be squashed. Set the sequence
489 // number as the youngest instruction in the ROB (0 in this case.
490 // Hopefully nothing breaks.)
491 youngestSeqNum[tid] = 0;
493 rob->squash(squashed_inst, tid);
494 changedROBNumEntries[tid] = true;
496 // Send back the sequence number of the squashed instruction.
497 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
499 // Send back the squash signal to tell stages that they should
501 toIEW->commitInfo[tid].squash = true;
503 // Send back the rob squashing signal so other stages know that
504 // the ROB is in the process of squashing.
505 toIEW->commitInfo[tid].robSquashing = true;
507 toIEW->commitInfo[tid].branchMispredict = false;
509 toIEW->commitInfo[tid].nextPC = PC[tid];
510 toIEW->commitInfo[tid].nextNPC = nextPC[tid];
513 template <class Impl>
515 DefaultCommit<Impl>::squashFromTrap(unsigned tid)
519 DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
521 thread[tid]->trapPending = false;
522 thread[tid]->inSyscall = false;
523 trapInFlight[tid] = false;
525 trapSquash[tid] = false;
527 commitStatus[tid] = ROBSquashing;
528 cpu->activityThisCycle();
531 template <class Impl>
533 DefaultCommit<Impl>::squashFromTC(unsigned tid)
537 DPRINTF(Commit, "Squashing from TC, restarting at PC %#x\n", PC[tid]);
539 thread[tid]->inSyscall = false;
540 assert(!thread[tid]->trapPending);
542 commitStatus[tid] = ROBSquashing;
543 cpu->activityThisCycle();
545 tcSquash[tid] = false;
548 template <class Impl>
550 DefaultCommit<Impl>::tick()
552 wroteToTimeBuffer = false;
553 _nextStatus = Inactive;
555 if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
556 cpu->signalDrained();
557 drainPending = false;
561 if (activeThreads->empty())
564 std::list<unsigned>::iterator threads = activeThreads->begin();
565 std::list<unsigned>::iterator end = activeThreads->end();
567 // Check if any of the threads are done squashing. Change the
568 // status if they are done.
569 while (threads != end) {
570 unsigned tid = *threads++;
572 // Clear the bit saying if the thread has committed stores
574 committedStores[tid] = false;
576 if (commitStatus[tid] == ROBSquashing) {
578 if (rob->isDoneSquashing(tid)) {
579 commitStatus[tid] = Running;
581 DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
582 " insts this cycle.\n", tid);
584 toIEW->commitInfo[tid].robSquashing = true;
585 wroteToTimeBuffer = true;
592 markCompletedInsts();
594 threads = activeThreads->begin();
596 while (threads != end) {
597 unsigned tid = *threads++;
599 if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
600 // The ROB has more instructions it can commit. Its next status
602 _nextStatus = Active;
604 DynInstPtr inst = rob->readHeadInst(tid);
606 DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
607 " ROB and ready to commit\n",
608 tid, inst->seqNum, inst->readPC());
610 } else if (!rob->isEmpty(tid)) {
611 DynInstPtr inst = rob->readHeadInst(tid);
613 DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
614 "%#x is head of ROB and not ready\n",
615 tid, inst->seqNum, inst->readPC());
618 DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
619 tid, rob->countInsts(tid), rob->numFreeEntries(tid));
623 if (wroteToTimeBuffer) {
624 DPRINTF(Activity, "Activity This Cycle.\n");
625 cpu->activityThisCycle();
632 template <class Impl>
634 DefaultCommit<Impl>::handleInterrupt()
636 if (interrupt != NoFault) {
637 // Wait until the ROB is empty and all stores have drained in
638 // order to enter the interrupt.
639 if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
640 // Squash or record that I need to squash this cycle if
641 // an interrupt needed to be handled.
642 DPRINTF(Commit, "Interrupt detected.\n");
644 Fault new_interrupt = cpu->getInterrupts();
645 assert(new_interrupt != NoFault);
647 // Clear the interrupt now that it's going to be handled
648 toIEW->commitInfo[0].clearInterrupt = true;
650 assert(!thread[0]->inSyscall);
651 thread[0]->inSyscall = true;
653 // CPU will handle interrupt.
654 cpu->processInterrupts(interrupt);
656 thread[0]->inSyscall = false;
658 commitStatus[0] = TrapPending;
660 // Generate trap squash event.
661 generateTrapEvent(0);
665 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
667 } else if (commitStatus[0] != TrapPending &&
668 cpu->check_interrupts(cpu->tcBase(0)) &&
671 // Process interrupts if interrupts are enabled, not in PAL
672 // mode, and no other traps or external squashes are currently
674 // @todo: Allow other threads to handle interrupts.
676 // Get any interrupt that happened
677 interrupt = cpu->getInterrupts();
679 if (interrupt != NoFault) {
680 // Tell fetch that there is an interrupt pending. This
681 // will make fetch wait until it sees a non PAL-mode PC,
682 // at which point it stops fetching instructions.
683 toIEW->commitInfo[0].interruptPending = true;
687 #endif // FULL_SYSTEM
689 template <class Impl>
691 DefaultCommit<Impl>::commit()
695 // Check for any interrupt, and start processing it. Or if we
696 // have an outstanding interrupt and are at a point when it is
697 // valid to take an interrupt, process it.
698 if (cpu->check_interrupts(cpu->tcBase(0))) {
701 #endif // FULL_SYSTEM
703 ////////////////////////////////////
704 // Check for any possible squashes, handle them first
705 ////////////////////////////////////
706 std::list<unsigned>::iterator threads = activeThreads->begin();
707 std::list<unsigned>::iterator end = activeThreads->end();
709 while (threads != end) {
710 unsigned tid = *threads++;
712 // Not sure which one takes priority. I think if we have
713 // both, that's a bad sign.
714 if (trapSquash[tid] == true) {
715 assert(!tcSquash[tid]);
717 } else if (tcSquash[tid] == true) {
718 assert(commitStatus[tid] != TrapPending);
722 // Squashed sequence number must be older than youngest valid
723 // instruction in the ROB. This prevents squashes from younger
724 // instructions overriding squashes from older instructions.
725 if (fromIEW->squash[tid] &&
726 commitStatus[tid] != TrapPending &&
727 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
729 DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
731 fromIEW->mispredPC[tid],
732 fromIEW->squashedSeqNum[tid]);
734 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
736 fromIEW->nextPC[tid]);
738 commitStatus[tid] = ROBSquashing;
740 // If we want to include the squashing instruction in the squash,
741 // then use one older sequence number.
742 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
744 #if ISA_HAS_DELAY_SLOT
745 InstSeqNum bdelay_done_seq_num = squashed_inst;
746 bool squash_bdelay_slot = fromIEW->squashDelaySlot[tid];
747 bool branchMispredict = fromIEW->branchMispredict[tid];
749 // Squashing/not squashing the branch delay slot only makes
750 // sense when you're squashing from a branch, ie from a branch
752 if (branchMispredict && !squash_bdelay_slot) {
753 bdelay_done_seq_num++;
757 if (fromIEW->includeSquashInst[tid] == true) {
759 #if ISA_HAS_DELAY_SLOT
760 bdelay_done_seq_num--;
764 // All younger instructions will be squashed. Set the sequence
765 // number as the youngest instruction in the ROB.
766 youngestSeqNum[tid] = squashed_inst;
768 #if ISA_HAS_DELAY_SLOT
769 rob->squash(bdelay_done_seq_num, tid);
770 toIEW->commitInfo[tid].squashDelaySlot = squash_bdelay_slot;
771 toIEW->commitInfo[tid].bdelayDoneSeqNum = bdelay_done_seq_num;
773 rob->squash(squashed_inst, tid);
774 toIEW->commitInfo[tid].squashDelaySlot = true;
776 changedROBNumEntries[tid] = true;
778 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
780 toIEW->commitInfo[tid].squash = true;
782 // Send back the rob squashing signal so other stages know that
783 // the ROB is in the process of squashing.
784 toIEW->commitInfo[tid].robSquashing = true;
786 toIEW->commitInfo[tid].branchMispredict =
787 fromIEW->branchMispredict[tid];
789 toIEW->commitInfo[tid].branchTaken =
790 fromIEW->branchTaken[tid];
792 toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
793 toIEW->commitInfo[tid].nextNPC = fromIEW->nextNPC[tid];
795 toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
797 if (toIEW->commitInfo[tid].branchMispredict) {
806 if (squashCounter != numThreads) {
807 // If we're not currently squashing, then get instructions.
810 // Try to commit any instructions.
813 #if ISA_HAS_DELAY_SLOT
818 //Check for any activity
819 threads = activeThreads->begin();
821 while (threads != end) {
822 unsigned tid = *threads++;
824 if (changedROBNumEntries[tid]) {
825 toIEW->commitInfo[tid].usedROB = true;
826 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
828 wroteToTimeBuffer = true;
829 changedROBNumEntries[tid] = false;
830 if (rob->isEmpty(tid))
831 checkEmptyROB[tid] = true;
834 // ROB is only considered "empty" for previous stages if: a)
835 // ROB is empty, b) there are no outstanding stores, c) IEW
836 // stage has received any information regarding stores that
838 // c) is checked by making sure to not consider the ROB empty
839 // on the same cycle as when stores have been committed.
840 // @todo: Make this handle multi-cycle communication between
842 if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
843 !iewStage->hasStoresToWB() && !committedStores[tid]) {
844 checkEmptyROB[tid] = false;
845 toIEW->commitInfo[tid].usedROB = true;
846 toIEW->commitInfo[tid].emptyROB = true;
847 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
848 wroteToTimeBuffer = true;
854 template <class Impl>
856 DefaultCommit<Impl>::commitInsts()
858 ////////////////////////////////////
860 // Note that commit will be handled prior to putting new
861 // instructions in the ROB so that the ROB only tries to commit
862 // instructions it has in this current cycle, and not instructions
863 // it is writing in during this cycle. Can't commit and squash
864 // things at the same time...
865 ////////////////////////////////////
867 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
869 unsigned num_committed = 0;
871 DynInstPtr head_inst;
873 // Commit as many instructions as possible until the commit bandwidth
874 // limit is reached, or it becomes impossible to commit any more.
875 while (num_committed < commitWidth) {
876 int commit_thread = getCommittingThread();
878 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
881 head_inst = rob->readHeadInst(commit_thread);
883 int tid = head_inst->threadNumber;
885 assert(tid == commit_thread);
887 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
888 head_inst->seqNum, tid);
890 // If the head instruction is squashed, it is ready to retire
891 // (be removed from the ROB) at any time.
892 if (head_inst->isSquashed()) {
894 DPRINTF(Commit, "Retiring squashed instruction from "
897 rob->retireHead(commit_thread);
899 ++commitSquashedInsts;
901 // Record that the number of ROB entries has changed.
902 changedROBNumEntries[tid] = true;
904 PC[tid] = head_inst->readPC();
905 nextPC[tid] = head_inst->readNextPC();
906 nextNPC[tid] = head_inst->readNextNPC();
908 // Increment the total number of non-speculative instructions
910 // Hack for now: it really shouldn't happen until after the
911 // commit is deemed to be successful, but this count is needed
913 thread[tid]->funcExeInst++;
915 // Try to commit the head instruction.
916 bool commit_success = commitHead(head_inst, num_committed);
918 if (commit_success) {
921 changedROBNumEntries[tid] = true;
923 // Set the doneSeqNum to the youngest committed instruction.
924 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
926 ++commitCommittedInsts;
928 // To match the old model, don't count nops and instruction
929 // prefetches towards the total commit count.
930 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
934 PC[tid] = nextPC[tid];
935 #if ISA_HAS_DELAY_SLOT
936 nextPC[tid] = nextNPC[tid];
937 nextNPC[tid] = nextNPC[tid] + sizeof(TheISA::MachInst);
939 nextPC[tid] = nextPC[tid] + sizeof(TheISA::MachInst);
946 // Debug statement. Checks to make sure we're not
947 // currently updating state while handling PC events.
949 assert(!thread[tid]->inSyscall &&
950 !thread[tid]->trapPending);
952 cpu->system->pcEventQueue.service(
953 thread[tid]->getTC());
955 } while (oldpc != PC[tid]);
957 DPRINTF(Commit, "PC skip function event, stopping commit\n");
962 DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
963 "[tid:%i] [sn:%i].\n",
964 head_inst->readPC(), tid ,head_inst->seqNum);
970 DPRINTF(CommitRate, "%i\n", num_committed);
971 numCommittedDist.sample(num_committed);
973 if (num_committed == commitWidth) {
974 commitEligibleSamples++;
978 template <class Impl>
980 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
984 int tid = head_inst->threadNumber;
986 // If the instruction is not executed yet, then it will need extra
987 // handling. Signal backwards that it should be executed.
988 if (!head_inst->isExecuted()) {
989 // Keep this number correct. We have not yet actually executed
990 // and committed this instruction.
991 thread[tid]->funcExeInst--;
993 if (head_inst->isNonSpeculative() ||
994 head_inst->isStoreConditional() ||
995 head_inst->isMemBarrier() ||
996 head_inst->isWriteBarrier()) {
998 DPRINTF(Commit, "Encountered a barrier or non-speculative "
999 "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
1000 head_inst->seqNum, head_inst->readPC());
1002 if (inst_num > 0 || iewStage->hasStoresToWB()) {
1003 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1007 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1009 // Change the instruction so it won't try to commit again until
1011 head_inst->clearCanCommit();
1013 ++commitNonSpecStalls;
1016 } else if (head_inst->isLoad()) {
1017 if (inst_num > 0 || iewStage->hasStoresToWB()) {
1018 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1022 assert(head_inst->uncacheable());
1023 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
1024 head_inst->seqNum, head_inst->readPC());
1026 // Send back the non-speculative instruction's sequence
1027 // number. Tell the lsq to re-execute the load.
1028 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1029 toIEW->commitInfo[tid].uncached = true;
1030 toIEW->commitInfo[tid].uncachedLoad = head_inst;
1032 head_inst->clearCanCommit();
1036 panic("Trying to commit un-executed instruction "
1037 "of unknown type!\n");
1041 if (head_inst->isThreadSync()) {
1042 // Not handled for now.
1043 panic("Thread sync instructions are not handled yet.\n");
1046 // Check if the instruction caused a fault. If so, trap.
1047 Fault inst_fault = head_inst->getFault();
1049 // Stores mark themselves as completed.
1050 if (!head_inst->isStore() && inst_fault == NoFault) {
1051 head_inst->setCompleted();
1055 // Use checker prior to updating anything due to traps or PC
1058 cpu->checker->verify(head_inst);
1062 // DTB will sometimes need the machine instruction for when
1063 // faults happen. So we will set it here, prior to the DTB
1064 // possibly needing it for its fault.
1065 thread[tid]->setInst(
1066 static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
1068 if (inst_fault != NoFault) {
1069 DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
1070 head_inst->seqNum, head_inst->readPC());
1072 if (iewStage->hasStoresToWB() || inst_num > 0) {
1073 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1077 head_inst->setCompleted();
1080 if (cpu->checker && head_inst->isStore()) {
1081 cpu->checker->verify(head_inst);
1085 assert(!thread[tid]->inSyscall);
1087 // Mark that we're in state update mode so that the trap's
1088 // execution doesn't generate extra squashes.
1089 thread[tid]->inSyscall = true;
1091 // Execute the trap. Although it's slightly unrealistic in
1092 // terms of timing (as it doesn't wait for the full timing of
1093 // the trap event to complete before updating state), it's
1094 // needed to update the state as soon as possible. This
1095 // prevents external agents from changing any specific state
1096 // that the trap need.
1097 cpu->trap(inst_fault, tid);
1099 // Exit state update mode to avoid accidental updating.
1100 thread[tid]->inSyscall = false;
1102 commitStatus[tid] = TrapPending;
1104 if (head_inst->traceData) {
1105 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1106 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1107 head_inst->traceData->dump();
1108 delete head_inst->traceData;
1109 head_inst->traceData = NULL;
1112 // Generate trap squash event.
1113 generateTrapEvent(tid);
1114 // warn("%lli fault (%d) handled @ PC %08p", curTick, inst_fault->name(), head_inst->readPC());
1118 updateComInstStats(head_inst);
1121 if (thread[tid]->profile) {
1122 // bool usermode = TheISA::inUserMode(thread[tid]->getTC());
1123 // thread[tid]->profilePC = usermode ? 1 : head_inst->readPC();
1124 thread[tid]->profilePC = head_inst->readPC();
1125 ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
1126 head_inst->staticInst);
1129 thread[tid]->profileNode = node;
1133 if (head_inst->traceData) {
1134 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1135 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1136 head_inst->traceData->dump();
1137 delete head_inst->traceData;
1138 head_inst->traceData = NULL;
1141 // Update the commit rename map
1142 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1143 renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
1144 head_inst->renamedDestRegIdx(i));
1147 if (head_inst->isCopy())
1148 panic("Should not commit any copy instructions!");
1150 // Finally clear the head ROB entry.
1151 rob->retireHead(tid);
1153 // If this was a store, record it for this cycle.
1154 if (head_inst->isStore())
1155 committedStores[tid] = true;
1157 // Return true to indicate that we have committed an instruction.
1161 template <class Impl>
1163 DefaultCommit<Impl>::getInsts()
1165 DPRINTF(Commit, "Getting instructions from Rename stage.\n");
1167 #if ISA_HAS_DELAY_SLOT
1168 // Read any renamed instructions and place them into the ROB.
1169 int insts_to_process = std::min((int)renameWidth,
1170 (int)(fromRename->size + skidBuffer.size()));
1173 DPRINTF(Commit, "%i insts available to process. Rename Insts:%i "
1174 "SkidBuffer Insts:%i\n", insts_to_process, fromRename->size,
1177 // Read any renamed instructions and place them into the ROB.
1178 int insts_to_process = std::min((int)renameWidth, fromRename->size);
1182 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
1185 #if ISA_HAS_DELAY_SLOT
1186 // Get insts from skidBuffer or from Rename
1187 if (skidBuffer.size() > 0) {
1188 DPRINTF(Commit, "Grabbing skidbuffer inst.\n");
1189 inst = skidBuffer.front();
1192 DPRINTF(Commit, "Grabbing rename inst.\n");
1193 inst = fromRename->insts[rename_idx++];
1196 inst = fromRename->insts[inst_num];
1198 int tid = inst->threadNumber;
1200 if (!inst->isSquashed() &&
1201 commitStatus[tid] != ROBSquashing &&
1202 commitStatus[tid] != TrapPending) {
1203 changedROBNumEntries[tid] = true;
1205 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
1206 inst->readPC(), inst->seqNum, tid);
1208 rob->insertInst(inst);
1210 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1212 youngestSeqNum[tid] = inst->seqNum;
1214 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1215 "squashed, skipping.\n",
1216 inst->readPC(), inst->seqNum, tid);
1220 #if ISA_HAS_DELAY_SLOT
1221 if (rename_idx < fromRename->size) {
1222 DPRINTF(Commit,"Placing Rename Insts into skidBuffer.\n");
1225 rename_idx < fromRename->size;
1227 DynInstPtr inst = fromRename->insts[rename_idx];
1229 if (!inst->isSquashed()) {
1230 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ",
1231 "skidBuffer.\n", inst->readPC(), inst->seqNum,
1232 inst->threadNumber);
1233 skidBuffer.push(inst);
1235 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1236 "squashed, skipping.\n",
1237 inst->readPC(), inst->seqNum, inst->threadNumber);
1245 template <class Impl>
1247 DefaultCommit<Impl>::skidInsert()
1249 DPRINTF(Commit, "Attempting to any instructions from rename into "
1252 for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
1253 DynInstPtr inst = fromRename->insts[inst_num];
1255 if (!inst->isSquashed()) {
1256 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ",
1257 "skidBuffer.\n", inst->readPC(), inst->seqNum,
1258 inst->threadNumber);
1259 skidBuffer.push(inst);
1261 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1262 "squashed, skipping.\n",
1263 inst->readPC(), inst->seqNum, inst->threadNumber);
1268 template <class Impl>
1270 DefaultCommit<Impl>::markCompletedInsts()
1272 // Grab completed insts out of the IEW instruction queue, and mark
1273 // instructions completed within the ROB.
1274 for (int inst_num = 0;
1275 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1278 if (!fromIEW->insts[inst_num]->isSquashed()) {
1279 DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
1281 fromIEW->insts[inst_num]->threadNumber,
1282 fromIEW->insts[inst_num]->readPC(),
1283 fromIEW->insts[inst_num]->seqNum);
1285 // Mark the instruction as ready to commit.
1286 fromIEW->insts[inst_num]->setCanCommit();
1291 template <class Impl>
1293 DefaultCommit<Impl>::robDoneSquashing()
1295 std::list<unsigned>::iterator threads = activeThreads->begin();
1296 std::list<unsigned>::iterator end = activeThreads->end();
1298 while (threads != end) {
1299 unsigned tid = *threads++;
1301 if (!rob->isDoneSquashing(tid))
1308 template <class Impl>
1310 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1312 unsigned thread = inst->threadNumber;
1315 // Pick off the software prefetches
1318 if (inst->isDataPrefetch()) {
1319 statComSwp[thread]++;
1321 statComInst[thread]++;
1324 statComInst[thread]++;
1328 // Control Instructions
1330 if (inst->isControl())
1331 statComBranches[thread]++;
1334 // Memory references
1336 if (inst->isMemRef()) {
1337 statComRefs[thread]++;
1339 if (inst->isLoad()) {
1340 statComLoads[thread]++;
1344 if (inst->isMemBarrier()) {
1345 statComMembars[thread]++;
1349 ////////////////////////////////////////
1351 // SMT COMMIT POLICY MAINTAINED HERE //
1353 ////////////////////////////////////////
1354 template <class Impl>
1356 DefaultCommit<Impl>::getCommittingThread()
1358 if (numThreads > 1) {
1359 switch (commitPolicy) {
1362 //If Policy is Aggressive, commit will call
1363 //this function multiple times per
1365 return oldestReady();
1368 return roundRobin();
1371 return oldestReady();
1377 assert(!activeThreads->empty());
1378 int tid = activeThreads->front();
1380 if (commitStatus[tid] == Running ||
1381 commitStatus[tid] == Idle ||
1382 commitStatus[tid] == FetchTrapPending) {
1390 template<class Impl>
1392 DefaultCommit<Impl>::roundRobin()
1394 std::list<unsigned>::iterator pri_iter = priority_list.begin();
1395 std::list<unsigned>::iterator end = priority_list.end();
1397 while (pri_iter != end) {
1398 unsigned tid = *pri_iter;
1400 if (commitStatus[tid] == Running ||
1401 commitStatus[tid] == Idle ||
1402 commitStatus[tid] == FetchTrapPending) {
1404 if (rob->isHeadReady(tid)) {
1405 priority_list.erase(pri_iter);
1406 priority_list.push_back(tid);
1418 template<class Impl>
1420 DefaultCommit<Impl>::oldestReady()
1422 unsigned oldest = 0;
1425 std::list<unsigned>::iterator threads = activeThreads->begin();
1426 std::list<unsigned>::iterator end = activeThreads->end();
1428 while (threads != end) {
1429 unsigned tid = *threads++;
1431 if (!rob->isEmpty(tid) &&
1432 (commitStatus[tid] == Running ||
1433 commitStatus[tid] == Idle ||
1434 commitStatus[tid] == FetchTrapPending)) {
1436 if (rob->isHeadReady(tid)) {
1438 DynInstPtr head_inst = rob->readHeadInst(tid);
1443 } else if (head_inst->seqNum < oldest) {