2 * Copyright (c) 2010 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include "arch/utility.hh"
48 #include "base/loader/symtab.hh"
49 #include "base/cp_annotate.hh"
50 #include "config/the_isa.hh"
51 #include "config/use_checker.hh"
52 #include "cpu/o3/commit.hh"
53 #include "cpu/o3/thread_state.hh"
54 #include "cpu/exetrace.hh"
55 #include "cpu/timebuf.hh"
56 #include "debug/Activity.hh"
57 #include "debug/Commit.hh"
58 #include "debug/CommitRate.hh"
59 #include "debug/ExecFaulting.hh"
60 #include "debug/O3PipeView.hh"
61 #include "params/DerivO3CPU.hh"
62 #include "sim/faults.hh"
63 #include "sim/full_system.hh"
66 #include "cpu/checker/cpu.hh"
72 DefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
74 : Event(CPU_Tick_Pri, AutoDelete), commit(_commit), tid(_tid)
80 DefaultCommit<Impl>::TrapEvent::process()
82 // This will get reset by commit if it was switched out at the
83 // time of this event processing.
84 commit->trapSquash[tid] = true;
89 DefaultCommit<Impl>::TrapEvent::description() const
95 DefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params)
98 iewToCommitDelay(params->iewToCommitDelay),
99 commitToIEWDelay(params->commitToIEWDelay),
100 renameToROBDelay(params->renameToROBDelay),
101 fetchToCommitDelay(params->commitToFetchDelay),
102 renameWidth(params->renameWidth),
103 commitWidth(params->commitWidth),
104 numThreads(params->numThreads),
107 trapLatency(params->trapLatency)
110 _nextStatus = Inactive;
111 std::string policy = params->smtCommitPolicy;
113 //Convert string to lowercase
114 std::transform(policy.begin(), policy.end(), policy.begin(),
115 (int(*)(int)) tolower);
117 //Assign commit policy
118 if (policy == "aggressive"){
119 commitPolicy = Aggressive;
121 DPRINTF(Commit,"Commit Policy set to Aggressive.\n");
122 } else if (policy == "roundrobin"){
123 commitPolicy = RoundRobin;
125 //Set-Up Priority List
126 for (ThreadID tid = 0; tid < numThreads; tid++) {
127 priority_list.push_back(tid);
130 DPRINTF(Commit,"Commit Policy set to Round Robin.\n");
131 } else if (policy == "oldestready"){
132 commitPolicy = OldestReady;
134 DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
136 assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
137 "RoundRobin,OldestReady}");
140 for (ThreadID tid = 0; tid < numThreads; tid++) {
141 commitStatus[tid] = Idle;
142 changedROBNumEntries[tid] = false;
143 checkEmptyROB[tid] = false;
144 trapInFlight[tid] = false;
145 committedStores[tid] = false;
146 trapSquash[tid] = false;
147 tcSquash[tid] = false;
149 lastCommitedSeqNum[tid] = 0;
154 template <class Impl>
156 DefaultCommit<Impl>::name() const
158 return cpu->name() + ".commit";
161 template <class Impl>
163 DefaultCommit<Impl>::regStats()
165 using namespace Stats;
167 .name(name() + ".commitCommittedInsts")
168 .desc("The number of committed instructions")
169 .prereq(commitCommittedInsts);
171 .name(name() + ".commitSquashedInsts")
172 .desc("The number of squashed insts skipped by commit")
173 .prereq(commitSquashedInsts);
175 .name(name() + ".commitSquashEvents")
176 .desc("The number of times commit is told to squash")
177 .prereq(commitSquashEvents);
179 .name(name() + ".commitNonSpecStalls")
180 .desc("The number of times commit has been forced to stall to "
181 "communicate backwards")
182 .prereq(commitNonSpecStalls);
184 .name(name() + ".branchMispredicts")
185 .desc("The number of times a branch was mispredicted")
186 .prereq(branchMispredicts);
188 .init(0,commitWidth,1)
189 .name(name() + ".committed_per_cycle")
190 .desc("Number of insts commited each cycle")
195 .init(cpu->numThreads)
196 .name(name() + ".count")
197 .desc("Number of instructions committed")
202 .init(cpu->numThreads)
203 .name(name() + ".swp_count")
204 .desc("Number of s/w prefetches committed")
209 .init(cpu->numThreads)
210 .name(name() + ".refs")
211 .desc("Number of memory references committed")
216 .init(cpu->numThreads)
217 .name(name() + ".loads")
218 .desc("Number of loads committed")
223 .init(cpu->numThreads)
224 .name(name() + ".membars")
225 .desc("Number of memory barriers committed")
230 .init(cpu->numThreads)
231 .name(name() + ".branches")
232 .desc("Number of branches committed")
237 .init(cpu->numThreads)
238 .name(name() + ".fp_insts")
239 .desc("Number of committed floating point instructions.")
244 .init(cpu->numThreads)
245 .name(name()+".int_insts")
246 .desc("Number of committed integer instructions.")
251 .init(cpu->numThreads)
252 .name(name()+".function_calls")
253 .desc("Number of function calls committed.")
258 .init(cpu->numThreads)
259 .name(name() + ".bw_limited")
260 .desc("number of insts not committed due to BW limits")
264 commitEligibleSamples
265 .name(name() + ".bw_lim_events")
266 .desc("number cycles where commit BW limit reached")
270 template <class Impl>
272 DefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
277 template <class Impl>
279 DefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
283 // Setup wire to send information back to IEW.
284 toIEW = timeBuffer->getWire(0);
286 // Setup wire to read data from IEW (for the ROB).
287 robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
290 template <class Impl>
292 DefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
296 // Setup wire to get instructions from rename (for the ROB).
297 fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
300 template <class Impl>
302 DefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
304 renameQueue = rq_ptr;
306 // Setup wire to get instructions from rename (for the ROB).
307 fromRename = renameQueue->getWire(-renameToROBDelay);
310 template <class Impl>
312 DefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
316 // Setup wire to get instructions from IEW.
317 fromIEW = iewQueue->getWire(-iewToCommitDelay);
320 template <class Impl>
322 DefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
324 iewStage = iew_stage;
329 DefaultCommit<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
331 activeThreads = at_ptr;
334 template <class Impl>
336 DefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
338 for (ThreadID tid = 0; tid < numThreads; tid++)
339 renameMap[tid] = &rm_ptr[tid];
342 template <class Impl>
344 DefaultCommit<Impl>::setROB(ROB *rob_ptr)
349 template <class Impl>
351 DefaultCommit<Impl>::initStage()
353 rob->setActiveThreads(activeThreads);
356 // Broadcast the number of free entries.
357 for (ThreadID tid = 0; tid < numThreads; tid++) {
358 toIEW->commitInfo[tid].usedROB = true;
359 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
360 toIEW->commitInfo[tid].emptyROB = true;
363 // Commit must broadcast the number of free entries it has at the
364 // start of the simulation, so it starts as active.
365 cpu->activateStage(O3CPU::CommitIdx);
367 cpu->activityThisCycle();
368 trapLatency = cpu->ticks(trapLatency);
371 template <class Impl>
373 DefaultCommit<Impl>::drain()
380 template <class Impl>
382 DefaultCommit<Impl>::switchOut()
385 drainPending = false;
389 template <class Impl>
391 DefaultCommit<Impl>::resume()
393 drainPending = false;
396 template <class Impl>
398 DefaultCommit<Impl>::takeOverFrom()
402 _nextStatus = Inactive;
403 for (ThreadID tid = 0; tid < numThreads; tid++) {
404 commitStatus[tid] = Idle;
405 changedROBNumEntries[tid] = false;
406 trapSquash[tid] = false;
407 tcSquash[tid] = false;
413 template <class Impl>
415 DefaultCommit<Impl>::updateStatus()
417 // reset ROB changed variable
418 list<ThreadID>::iterator threads = activeThreads->begin();
419 list<ThreadID>::iterator end = activeThreads->end();
421 while (threads != end) {
422 ThreadID tid = *threads++;
424 changedROBNumEntries[tid] = false;
426 // Also check if any of the threads has a trap pending
427 if (commitStatus[tid] == TrapPending ||
428 commitStatus[tid] == FetchTrapPending) {
429 _nextStatus = Active;
433 if (_nextStatus == Inactive && _status == Active) {
434 DPRINTF(Activity, "Deactivating stage.\n");
435 cpu->deactivateStage(O3CPU::CommitIdx);
436 } else if (_nextStatus == Active && _status == Inactive) {
437 DPRINTF(Activity, "Activating stage.\n");
438 cpu->activateStage(O3CPU::CommitIdx);
441 _status = _nextStatus;
444 template <class Impl>
446 DefaultCommit<Impl>::setNextStatus()
450 list<ThreadID>::iterator threads = activeThreads->begin();
451 list<ThreadID>::iterator end = activeThreads->end();
453 while (threads != end) {
454 ThreadID tid = *threads++;
456 if (commitStatus[tid] == ROBSquashing) {
461 squashCounter = squashes;
463 // If commit is currently squashing, then it will have activity for the
464 // next cycle. Set its next status as active.
466 _nextStatus = Active;
470 template <class Impl>
472 DefaultCommit<Impl>::changedROBEntries()
474 list<ThreadID>::iterator threads = activeThreads->begin();
475 list<ThreadID>::iterator end = activeThreads->end();
477 while (threads != end) {
478 ThreadID tid = *threads++;
480 if (changedROBNumEntries[tid]) {
488 template <class Impl>
490 DefaultCommit<Impl>::numROBFreeEntries(ThreadID tid)
492 return rob->numFreeEntries(tid);
495 template <class Impl>
497 DefaultCommit<Impl>::generateTrapEvent(ThreadID tid)
499 DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
501 TrapEvent *trap = new TrapEvent(this, tid);
503 cpu->schedule(trap, curTick() + trapLatency);
504 trapInFlight[tid] = true;
505 thread[tid]->trapPending = true;
508 template <class Impl>
510 DefaultCommit<Impl>::generateTCEvent(ThreadID tid)
512 assert(!trapInFlight[tid]);
513 DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
515 tcSquash[tid] = true;
518 template <class Impl>
520 DefaultCommit<Impl>::squashAll(ThreadID tid)
522 // If we want to include the squashing instruction in the squash,
523 // then use one older sequence number.
524 // Hopefully this doesn't mess things up. Basically I want to squash
525 // all instructions of this thread.
526 InstSeqNum squashed_inst = rob->isEmpty() ?
527 lastCommitedSeqNum[tid] : rob->readHeadInst(tid)->seqNum - 1;
529 // All younger instructions will be squashed. Set the sequence
530 // number as the youngest instruction in the ROB (0 in this case.
531 // Hopefully nothing breaks.)
532 youngestSeqNum[tid] = lastCommitedSeqNum[tid];
534 rob->squash(squashed_inst, tid);
535 changedROBNumEntries[tid] = true;
537 // Send back the sequence number of the squashed instruction.
538 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
540 // Send back the squash signal to tell stages that they should
542 toIEW->commitInfo[tid].squash = true;
544 // Send back the rob squashing signal so other stages know that
545 // the ROB is in the process of squashing.
546 toIEW->commitInfo[tid].robSquashing = true;
548 toIEW->commitInfo[tid].mispredictInst = NULL;
549 toIEW->commitInfo[tid].squashInst = NULL;
551 toIEW->commitInfo[tid].pc = pc[tid];
554 template <class Impl>
556 DefaultCommit<Impl>::squashFromTrap(ThreadID tid)
560 DPRINTF(Commit, "Squashing from trap, restarting at PC %s\n", pc[tid]);
562 thread[tid]->trapPending = false;
563 thread[tid]->inSyscall = false;
564 trapInFlight[tid] = false;
566 trapSquash[tid] = false;
568 commitStatus[tid] = ROBSquashing;
569 cpu->activityThisCycle();
572 template <class Impl>
574 DefaultCommit<Impl>::squashFromTC(ThreadID tid)
578 DPRINTF(Commit, "Squashing from TC, restarting at PC %s\n", pc[tid]);
580 thread[tid]->inSyscall = false;
581 assert(!thread[tid]->trapPending);
583 commitStatus[tid] = ROBSquashing;
584 cpu->activityThisCycle();
586 tcSquash[tid] = false;
589 template <class Impl>
591 DefaultCommit<Impl>::squashAfter(ThreadID tid, DynInstPtr &head_inst,
592 uint64_t squash_after_seq_num)
594 youngestSeqNum[tid] = squash_after_seq_num;
596 rob->squash(squash_after_seq_num, tid);
597 changedROBNumEntries[tid] = true;
599 // Send back the sequence number of the squashed instruction.
600 toIEW->commitInfo[tid].doneSeqNum = squash_after_seq_num;
602 toIEW->commitInfo[tid].squashInst = head_inst;
603 // Send back the squash signal to tell stages that they should squash.
604 toIEW->commitInfo[tid].squash = true;
606 // Send back the rob squashing signal so other stages know that
607 // the ROB is in the process of squashing.
608 toIEW->commitInfo[tid].robSquashing = true;
610 toIEW->commitInfo[tid].mispredictInst = NULL;
612 toIEW->commitInfo[tid].pc = pc[tid];
613 DPRINTF(Commit, "Executing squash after for [tid:%i] inst [sn:%lli]\n",
614 tid, squash_after_seq_num);
615 commitStatus[tid] = ROBSquashing;
618 template <class Impl>
620 DefaultCommit<Impl>::tick()
622 wroteToTimeBuffer = false;
623 _nextStatus = Inactive;
625 if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
626 cpu->signalDrained();
627 drainPending = false;
631 if (activeThreads->empty())
634 list<ThreadID>::iterator threads = activeThreads->begin();
635 list<ThreadID>::iterator end = activeThreads->end();
637 // Check if any of the threads are done squashing. Change the
638 // status if they are done.
639 while (threads != end) {
640 ThreadID tid = *threads++;
642 // Clear the bit saying if the thread has committed stores
644 committedStores[tid] = false;
646 if (commitStatus[tid] == ROBSquashing) {
648 if (rob->isDoneSquashing(tid)) {
649 commitStatus[tid] = Running;
651 DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
652 " insts this cycle.\n", tid);
654 toIEW->commitInfo[tid].robSquashing = true;
655 wroteToTimeBuffer = true;
662 markCompletedInsts();
664 threads = activeThreads->begin();
666 while (threads != end) {
667 ThreadID tid = *threads++;
669 if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
670 // The ROB has more instructions it can commit. Its next status
672 _nextStatus = Active;
674 DynInstPtr inst = rob->readHeadInst(tid);
676 DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %s is head of"
677 " ROB and ready to commit\n",
678 tid, inst->seqNum, inst->pcState());
680 } else if (!rob->isEmpty(tid)) {
681 DynInstPtr inst = rob->readHeadInst(tid);
683 DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
684 "%s is head of ROB and not ready\n",
685 tid, inst->seqNum, inst->pcState());
688 DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
689 tid, rob->countInsts(tid), rob->numFreeEntries(tid));
693 if (wroteToTimeBuffer) {
694 DPRINTF(Activity, "Activity This Cycle.\n");
695 cpu->activityThisCycle();
701 template <class Impl>
703 DefaultCommit<Impl>::handleInterrupt()
705 // Verify that we still have an interrupt to handle
706 if (!cpu->checkInterrupts(cpu->tcBase(0))) {
707 DPRINTF(Commit, "Pending interrupt is cleared by master before "
708 "it got handled. Restart fetching from the orig path.\n");
709 toIEW->commitInfo[0].clearInterrupt = true;
714 // Wait until all in flight instructions are finished before enterring
716 if (cpu->instList.empty()) {
717 // Squash or record that I need to squash this cycle if
718 // an interrupt needed to be handled.
719 DPRINTF(Commit, "Interrupt detected.\n");
721 // Clear the interrupt now that it's going to be handled
722 toIEW->commitInfo[0].clearInterrupt = true;
724 assert(!thread[0]->inSyscall);
725 thread[0]->inSyscall = true;
727 // CPU will handle interrupt.
728 cpu->processInterrupts(interrupt);
730 thread[0]->inSyscall = false;
732 commitStatus[0] = TrapPending;
734 // Generate trap squash event.
735 generateTrapEvent(0);
739 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
743 template <class Impl>
745 DefaultCommit<Impl>::propagateInterrupt()
747 if (commitStatus[0] == TrapPending || interrupt || trapSquash[0] ||
751 // Process interrupts if interrupts are enabled, not in PAL
752 // mode, and no other traps or external squashes are currently
754 // @todo: Allow other threads to handle interrupts.
756 // Get any interrupt that happened
757 interrupt = cpu->getInterrupts();
759 // Tell fetch that there is an interrupt pending. This
760 // will make fetch wait until it sees a non PAL-mode PC,
761 // at which point it stops fetching instructions.
762 if (interrupt != NoFault)
763 toIEW->commitInfo[0].interruptPending = true;
766 template <class Impl>
768 DefaultCommit<Impl>::commit()
771 // Check for any interrupt that we've already squashed for and start
773 if (interrupt != NoFault)
776 // Check if we have a interrupt and get read to handle it
777 if (cpu->checkInterrupts(cpu->tcBase(0)))
778 propagateInterrupt();
781 ////////////////////////////////////
782 // Check for any possible squashes, handle them first
783 ////////////////////////////////////
784 list<ThreadID>::iterator threads = activeThreads->begin();
785 list<ThreadID>::iterator end = activeThreads->end();
787 while (threads != end) {
788 ThreadID tid = *threads++;
790 // Not sure which one takes priority. I think if we have
791 // both, that's a bad sign.
792 if (trapSquash[tid] == true) {
793 assert(!tcSquash[tid]);
795 } else if (tcSquash[tid] == true) {
796 assert(commitStatus[tid] != TrapPending);
800 // Squashed sequence number must be older than youngest valid
801 // instruction in the ROB. This prevents squashes from younger
802 // instructions overriding squashes from older instructions.
803 if (fromIEW->squash[tid] &&
804 commitStatus[tid] != TrapPending &&
805 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
807 if (fromIEW->mispredictInst[tid]) {
809 "[tid:%i]: Squashing due to branch mispred PC:%#x [sn:%i]\n",
811 fromIEW->mispredictInst[tid]->instAddr(),
812 fromIEW->squashedSeqNum[tid]);
815 "[tid:%i]: Squashing due to order violation [sn:%i]\n",
816 tid, fromIEW->squashedSeqNum[tid]);
819 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
821 fromIEW->pc[tid].nextInstAddr());
823 commitStatus[tid] = ROBSquashing;
825 // If we want to include the squashing instruction in the squash,
826 // then use one older sequence number.
827 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
829 if (fromIEW->includeSquashInst[tid] == true) {
833 // All younger instructions will be squashed. Set the sequence
834 // number as the youngest instruction in the ROB.
835 youngestSeqNum[tid] = squashed_inst;
837 rob->squash(squashed_inst, tid);
838 changedROBNumEntries[tid] = true;
840 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
842 toIEW->commitInfo[tid].squash = true;
844 // Send back the rob squashing signal so other stages know that
845 // the ROB is in the process of squashing.
846 toIEW->commitInfo[tid].robSquashing = true;
848 toIEW->commitInfo[tid].mispredictInst =
849 fromIEW->mispredictInst[tid];
850 toIEW->commitInfo[tid].branchTaken =
851 fromIEW->branchTaken[tid];
852 toIEW->commitInfo[tid].squashInst = NULL;
854 toIEW->commitInfo[tid].pc = fromIEW->pc[tid];
856 if (toIEW->commitInfo[tid].mispredictInst) {
865 if (squashCounter != numThreads) {
866 // If we're not currently squashing, then get instructions.
869 // Try to commit any instructions.
873 //Check for any activity
874 threads = activeThreads->begin();
876 while (threads != end) {
877 ThreadID tid = *threads++;
879 if (changedROBNumEntries[tid]) {
880 toIEW->commitInfo[tid].usedROB = true;
881 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
883 wroteToTimeBuffer = true;
884 changedROBNumEntries[tid] = false;
885 if (rob->isEmpty(tid))
886 checkEmptyROB[tid] = true;
889 // ROB is only considered "empty" for previous stages if: a)
890 // ROB is empty, b) there are no outstanding stores, c) IEW
891 // stage has received any information regarding stores that
893 // c) is checked by making sure to not consider the ROB empty
894 // on the same cycle as when stores have been committed.
895 // @todo: Make this handle multi-cycle communication between
897 if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
898 !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
899 checkEmptyROB[tid] = false;
900 toIEW->commitInfo[tid].usedROB = true;
901 toIEW->commitInfo[tid].emptyROB = true;
902 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
903 wroteToTimeBuffer = true;
909 template <class Impl>
911 DefaultCommit<Impl>::commitInsts()
913 ////////////////////////////////////
915 // Note that commit will be handled prior to putting new
916 // instructions in the ROB so that the ROB only tries to commit
917 // instructions it has in this current cycle, and not instructions
918 // it is writing in during this cycle. Can't commit and squash
919 // things at the same time...
920 ////////////////////////////////////
922 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
924 unsigned num_committed = 0;
926 DynInstPtr head_inst;
928 // Commit as many instructions as possible until the commit bandwidth
929 // limit is reached, or it becomes impossible to commit any more.
930 while (num_committed < commitWidth) {
931 int commit_thread = getCommittingThread();
933 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
936 head_inst = rob->readHeadInst(commit_thread);
938 ThreadID tid = head_inst->threadNumber;
940 assert(tid == commit_thread);
942 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
943 head_inst->seqNum, tid);
945 // If the head instruction is squashed, it is ready to retire
946 // (be removed from the ROB) at any time.
947 if (head_inst->isSquashed()) {
949 DPRINTF(Commit, "Retiring squashed instruction from "
952 rob->retireHead(commit_thread);
954 ++commitSquashedInsts;
956 // Record that the number of ROB entries has changed.
957 changedROBNumEntries[tid] = true;
959 pc[tid] = head_inst->pcState();
961 // Increment the total number of non-speculative instructions
963 // Hack for now: it really shouldn't happen until after the
964 // commit is deemed to be successful, but this count is needed
966 thread[tid]->funcExeInst++;
968 // Try to commit the head instruction.
969 bool commit_success = commitHead(head_inst, num_committed);
971 if (commit_success) {
974 changedROBNumEntries[tid] = true;
976 // Set the doneSeqNum to the youngest committed instruction.
977 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
979 ++commitCommittedInsts;
981 // To match the old model, don't count nops and instruction
982 // prefetches towards the total commit count.
983 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
987 // Updates misc. registers.
988 head_inst->updateMiscRegs();
990 TheISA::advancePC(pc[tid], head_inst->staticInst);
992 // Keep track of the last sequence number commited
993 lastCommitedSeqNum[tid] = head_inst->seqNum;
995 // If this is an instruction that doesn't play nicely with
996 // others squash everything and restart fetch
997 if (head_inst->isSquashAfter())
998 squashAfter(tid, head_inst, head_inst->seqNum);
1002 // Debug statement. Checks to make sure we're not
1003 // currently updating state while handling PC events.
1004 assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
1006 oldpc = pc[tid].instAddr();
1007 cpu->system->pcEventQueue.service(thread[tid]->getTC());
1009 } while (oldpc != pc[tid].instAddr());
1012 "PC skip function event, stopping commit\n");
1016 DPRINTF(Commit, "Unable to commit head instruction PC:%s "
1017 "[tid:%i] [sn:%i].\n",
1018 head_inst->pcState(), tid ,head_inst->seqNum);
1024 DPRINTF(CommitRate, "%i\n", num_committed);
1025 numCommittedDist.sample(num_committed);
1027 if (num_committed == commitWidth) {
1028 commitEligibleSamples++;
1032 template <class Impl>
1034 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
1038 ThreadID tid = head_inst->threadNumber;
1040 // If the instruction is not executed yet, then it will need extra
1041 // handling. Signal backwards that it should be executed.
1042 if (!head_inst->isExecuted()) {
1043 // Keep this number correct. We have not yet actually executed
1044 // and committed this instruction.
1045 thread[tid]->funcExeInst--;
1047 if (head_inst->isNonSpeculative() ||
1048 head_inst->isStoreConditional() ||
1049 head_inst->isMemBarrier() ||
1050 head_inst->isWriteBarrier()) {
1052 DPRINTF(Commit, "Encountered a barrier or non-speculative "
1053 "instruction [sn:%lli] at the head of the ROB, PC %s.\n",
1054 head_inst->seqNum, head_inst->pcState());
1056 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
1057 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1061 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1063 // Change the instruction so it won't try to commit again until
1065 head_inst->clearCanCommit();
1067 ++commitNonSpecStalls;
1070 } else if (head_inst->isLoad()) {
1071 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
1072 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1076 assert(head_inst->uncacheable());
1077 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %s.\n",
1078 head_inst->seqNum, head_inst->pcState());
1080 // Send back the non-speculative instruction's sequence
1081 // number. Tell the lsq to re-execute the load.
1082 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1083 toIEW->commitInfo[tid].uncached = true;
1084 toIEW->commitInfo[tid].uncachedLoad = head_inst;
1086 head_inst->clearCanCommit();
1090 panic("Trying to commit un-executed instruction "
1091 "of unknown type!\n");
1095 if (head_inst->isThreadSync()) {
1096 // Not handled for now.
1097 panic("Thread sync instructions are not handled yet.\n");
1100 // Check if the instruction caused a fault. If so, trap.
1101 Fault inst_fault = head_inst->getFault();
1103 // Stores mark themselves as completed.
1104 if (!head_inst->isStore() && inst_fault == NoFault) {
1105 head_inst->setCompleted();
1109 // Use checker prior to updating anything due to traps or PC
1112 cpu->checker->verify(head_inst);
1116 if (inst_fault != NoFault) {
1117 DPRINTF(Commit, "Inst [sn:%lli] PC %s has a fault\n",
1118 head_inst->seqNum, head_inst->pcState());
1120 if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
1121 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1125 head_inst->setCompleted();
1128 if (cpu->checker && head_inst->isStore()) {
1129 cpu->checker->verify(head_inst);
1133 assert(!thread[tid]->inSyscall);
1135 // Mark that we're in state update mode so that the trap's
1136 // execution doesn't generate extra squashes.
1137 thread[tid]->inSyscall = true;
1139 // Execute the trap. Although it's slightly unrealistic in
1140 // terms of timing (as it doesn't wait for the full timing of
1141 // the trap event to complete before updating state), it's
1142 // needed to update the state as soon as possible. This
1143 // prevents external agents from changing any specific state
1144 // that the trap need.
1145 cpu->trap(inst_fault, tid, head_inst->staticInst);
1147 // Exit state update mode to avoid accidental updating.
1148 thread[tid]->inSyscall = false;
1150 commitStatus[tid] = TrapPending;
1152 DPRINTF(Commit, "Committing instruction with fault [sn:%lli]\n",
1154 if (head_inst->traceData) {
1155 if (DTRACE(ExecFaulting)) {
1156 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1157 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1158 head_inst->traceData->dump();
1160 delete head_inst->traceData;
1161 head_inst->traceData = NULL;
1164 // Generate trap squash event.
1165 generateTrapEvent(tid);
1169 updateComInstStats(head_inst);
1172 if (thread[tid]->profile) {
1173 thread[tid]->profilePC = head_inst->instAddr();
1174 ProfileNode *node = thread[tid]->profile->consume(
1175 thread[tid]->getTC(), head_inst->staticInst);
1178 thread[tid]->profileNode = node;
1180 if (CPA::available()) {
1181 if (head_inst->isControl()) {
1182 ThreadContext *tc = thread[tid]->getTC();
1183 CPA::cpa()->swAutoBegin(tc, head_inst->nextInstAddr());
1187 DPRINTF(Commit, "Committing instruction with [sn:%lli] PC %s\n",
1188 head_inst->seqNum, head_inst->pcState());
1189 if (head_inst->traceData) {
1190 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1191 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1192 head_inst->traceData->dump();
1193 delete head_inst->traceData;
1194 head_inst->traceData = NULL;
1197 // Update the commit rename map
1198 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1199 renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
1200 head_inst->renamedDestRegIdx(i));
1203 // Finally clear the head ROB entry.
1204 rob->retireHead(tid);
1207 // Print info needed by the pipeline activity viewer.
1208 DPRINTFR(O3PipeView, "O3PipeView:fetch:%llu:0x%08llx:%d:%llu:%s\n",
1209 head_inst->fetchTick,
1210 head_inst->instAddr(),
1211 head_inst->microPC(),
1213 head_inst->staticInst->disassemble(head_inst->instAddr()));
1214 DPRINTFR(O3PipeView, "O3PipeView:decode:%llu\n", head_inst->decodeTick);
1215 DPRINTFR(O3PipeView, "O3PipeView:rename:%llu\n", head_inst->renameTick);
1216 DPRINTFR(O3PipeView, "O3PipeView:dispatch:%llu\n", head_inst->dispatchTick);
1217 DPRINTFR(O3PipeView, "O3PipeView:issue:%llu\n", head_inst->issueTick);
1218 DPRINTFR(O3PipeView, "O3PipeView:complete:%llu\n", head_inst->completeTick);
1219 DPRINTFR(O3PipeView, "O3PipeView:retire:%llu\n", curTick());
1222 // If this was a store, record it for this cycle.
1223 if (head_inst->isStore())
1224 committedStores[tid] = true;
1226 // Return true to indicate that we have committed an instruction.
1230 template <class Impl>
1232 DefaultCommit<Impl>::getInsts()
1234 DPRINTF(Commit, "Getting instructions from Rename stage.\n");
1236 // Read any renamed instructions and place them into the ROB.
1237 int insts_to_process = std::min((int)renameWidth, fromRename->size);
1239 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
1242 inst = fromRename->insts[inst_num];
1243 ThreadID tid = inst->threadNumber;
1245 if (!inst->isSquashed() &&
1246 commitStatus[tid] != ROBSquashing &&
1247 commitStatus[tid] != TrapPending) {
1248 changedROBNumEntries[tid] = true;
1250 DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ROB.\n",
1251 inst->pcState(), inst->seqNum, tid);
1253 rob->insertInst(inst);
1255 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1257 youngestSeqNum[tid] = inst->seqNum;
1259 DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
1260 "squashed, skipping.\n",
1261 inst->pcState(), inst->seqNum, tid);
1266 template <class Impl>
1268 DefaultCommit<Impl>::skidInsert()
1270 DPRINTF(Commit, "Attempting to any instructions from rename into "
1273 for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
1274 DynInstPtr inst = fromRename->insts[inst_num];
1276 if (!inst->isSquashed()) {
1277 DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ",
1278 "skidBuffer.\n", inst->pcState(), inst->seqNum,
1279 inst->threadNumber);
1280 skidBuffer.push(inst);
1282 DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
1283 "squashed, skipping.\n",
1284 inst->pcState(), inst->seqNum, inst->threadNumber);
1289 template <class Impl>
1291 DefaultCommit<Impl>::markCompletedInsts()
1293 // Grab completed insts out of the IEW instruction queue, and mark
1294 // instructions completed within the ROB.
1295 for (int inst_num = 0;
1296 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1299 if (!fromIEW->insts[inst_num]->isSquashed()) {
1300 DPRINTF(Commit, "[tid:%i]: Marking PC %s, [sn:%lli] ready "
1302 fromIEW->insts[inst_num]->threadNumber,
1303 fromIEW->insts[inst_num]->pcState(),
1304 fromIEW->insts[inst_num]->seqNum);
1306 // Mark the instruction as ready to commit.
1307 fromIEW->insts[inst_num]->setCanCommit();
1312 template <class Impl>
1314 DefaultCommit<Impl>::robDoneSquashing()
1316 list<ThreadID>::iterator threads = activeThreads->begin();
1317 list<ThreadID>::iterator end = activeThreads->end();
1319 while (threads != end) {
1320 ThreadID tid = *threads++;
1322 if (!rob->isDoneSquashing(tid))
1329 template <class Impl>
1331 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1333 ThreadID tid = inst->threadNumber;
1336 // Pick off the software prefetches
1339 if (inst->isDataPrefetch()) {
1349 // Control Instructions
1351 if (inst->isControl())
1352 statComBranches[tid]++;
1355 // Memory references
1357 if (inst->isMemRef()) {
1360 if (inst->isLoad()) {
1361 statComLoads[tid]++;
1365 if (inst->isMemBarrier()) {
1366 statComMembars[tid]++;
1369 // Integer Instruction
1370 if (inst->isInteger())
1371 statComInteger[tid]++;
1373 // Floating Point Instruction
1374 if (inst->isFloating())
1375 statComFloating[tid]++;
1379 statComFunctionCalls[tid]++;
1383 ////////////////////////////////////////
1385 // SMT COMMIT POLICY MAINTAINED HERE //
1387 ////////////////////////////////////////
1388 template <class Impl>
1390 DefaultCommit<Impl>::getCommittingThread()
1392 if (numThreads > 1) {
1393 switch (commitPolicy) {
1396 //If Policy is Aggressive, commit will call
1397 //this function multiple times per
1399 return oldestReady();
1402 return roundRobin();
1405 return oldestReady();
1408 return InvalidThreadID;
1411 assert(!activeThreads->empty());
1412 ThreadID tid = activeThreads->front();
1414 if (commitStatus[tid] == Running ||
1415 commitStatus[tid] == Idle ||
1416 commitStatus[tid] == FetchTrapPending) {
1419 return InvalidThreadID;
1424 template<class Impl>
1426 DefaultCommit<Impl>::roundRobin()
1428 list<ThreadID>::iterator pri_iter = priority_list.begin();
1429 list<ThreadID>::iterator end = priority_list.end();
1431 while (pri_iter != end) {
1432 ThreadID tid = *pri_iter;
1434 if (commitStatus[tid] == Running ||
1435 commitStatus[tid] == Idle ||
1436 commitStatus[tid] == FetchTrapPending) {
1438 if (rob->isHeadReady(tid)) {
1439 priority_list.erase(pri_iter);
1440 priority_list.push_back(tid);
1449 return InvalidThreadID;
1452 template<class Impl>
1454 DefaultCommit<Impl>::oldestReady()
1456 unsigned oldest = 0;
1459 list<ThreadID>::iterator threads = activeThreads->begin();
1460 list<ThreadID>::iterator end = activeThreads->end();
1462 while (threads != end) {
1463 ThreadID tid = *threads++;
1465 if (!rob->isEmpty(tid) &&
1466 (commitStatus[tid] == Running ||
1467 commitStatus[tid] == Idle ||
1468 commitStatus[tid] == FetchTrapPending)) {
1470 if (rob->isHeadReady(tid)) {
1472 DynInstPtr head_inst = rob->readHeadInst(tid);
1477 } else if (head_inst->seqNum < oldest) {
1487 return InvalidThreadID;