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.
31 #include "arch/types.hh"
32 #include "base/trace.hh"
33 #include "config/the_isa.hh"
34 #include "cpu/o3/decode.hh"
35 #include "cpu/inst_seq.hh"
36 #include "debug/Activity.hh"
37 #include "debug/Decode.hh"
38 #include "params/DerivO3CPU.hh"
39 #include "sim/full_system.hh"
44 DefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params)
46 renameToDecodeDelay(params->renameToDecodeDelay),
47 iewToDecodeDelay(params->iewToDecodeDelay),
48 commitToDecodeDelay(params->commitToDecodeDelay),
49 fetchToDecodeDelay(params->fetchToDecodeDelay),
50 decodeWidth(params->decodeWidth),
51 numThreads(params->numThreads)
55 // Setup status, make sure stall signals are clear.
56 for (ThreadID tid = 0; tid < numThreads; ++tid) {
57 decodeStatus[tid] = Idle;
59 stalls[tid].rename = false;
60 stalls[tid].iew = false;
61 stalls[tid].commit = false;
64 // @todo: Make into a parameter
65 skidBufferMax = (fetchToDecodeDelay * params->fetchWidth) + decodeWidth;
70 DefaultDecode<Impl>::name() const
72 return cpu->name() + ".decode";
77 DefaultDecode<Impl>::regStats()
80 .name(name() + ".IdleCycles")
81 .desc("Number of cycles decode is idle")
82 .prereq(decodeIdleCycles);
84 .name(name() + ".BlockedCycles")
85 .desc("Number of cycles decode is blocked")
86 .prereq(decodeBlockedCycles);
88 .name(name() + ".RunCycles")
89 .desc("Number of cycles decode is running")
90 .prereq(decodeRunCycles);
92 .name(name() + ".UnblockCycles")
93 .desc("Number of cycles decode is unblocking")
94 .prereq(decodeUnblockCycles);
96 .name(name() + ".SquashCycles")
97 .desc("Number of cycles decode is squashing")
98 .prereq(decodeSquashCycles);
100 .name(name() + ".BranchResolved")
101 .desc("Number of times decode resolved a branch")
102 .prereq(decodeBranchResolved);
104 .name(name() + ".BranchMispred")
105 .desc("Number of times decode detected a branch misprediction")
106 .prereq(decodeBranchMispred);
108 .name(name() + ".ControlMispred")
109 .desc("Number of times decode detected an instruction incorrectly"
110 " predicted as a control")
111 .prereq(decodeControlMispred);
113 .name(name() + ".DecodedInsts")
114 .desc("Number of instructions handled by decode")
115 .prereq(decodeDecodedInsts);
117 .name(name() + ".SquashedInsts")
118 .desc("Number of squashed instructions handled by decode")
119 .prereq(decodeSquashedInsts);
124 DefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
128 // Setup wire to write information back to fetch.
129 toFetch = timeBuffer->getWire(0);
131 // Create wires to get information from proper places in time buffer.
132 fromRename = timeBuffer->getWire(-renameToDecodeDelay);
133 fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
134 fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
139 DefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
141 decodeQueue = dq_ptr;
143 // Setup wire to write information to proper place in decode queue.
144 toRename = decodeQueue->getWire(0);
149 DefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
153 // Setup wire to read information from fetch queue.
154 fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
159 DefaultDecode<Impl>::setActiveThreads(std::list<ThreadID> *at_ptr)
161 activeThreads = at_ptr;
164 template <class Impl>
166 DefaultDecode<Impl>::drain()
168 // Decode is done draining at any time.
169 cpu->signalDrained();
173 template <class Impl>
175 DefaultDecode<Impl>::takeOverFrom()
179 // Be sure to reset state and clear out any old instructions.
180 for (ThreadID tid = 0; tid < numThreads; ++tid) {
181 decodeStatus[tid] = Idle;
183 stalls[tid].rename = false;
184 stalls[tid].iew = false;
185 stalls[tid].commit = false;
186 while (!insts[tid].empty())
188 while (!skidBuffer[tid].empty())
189 skidBuffer[tid].pop();
190 branchCount[tid] = 0;
192 wroteToTimeBuffer = false;
197 DefaultDecode<Impl>::checkStall(ThreadID tid) const
199 bool ret_val = false;
201 if (stalls[tid].rename) {
202 DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
204 } else if (stalls[tid].iew) {
205 DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
207 } else if (stalls[tid].commit) {
208 DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
217 DefaultDecode<Impl>::fetchInstsValid()
219 return fromFetch->size > 0;
224 DefaultDecode<Impl>::block(ThreadID tid)
226 DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
228 // Add the current inputs to the skid buffer so they can be
229 // reprocessed when this stage unblocks.
232 // If the decode status is blocked or unblocking then decode has not yet
233 // signalled fetch to unblock. In that case, there is no need to tell
235 if (decodeStatus[tid] != Blocked) {
236 // Set the status to Blocked.
237 decodeStatus[tid] = Blocked;
239 if (decodeStatus[tid] != Unblocking) {
240 toFetch->decodeBlock[tid] = true;
241 wroteToTimeBuffer = true;
252 DefaultDecode<Impl>::unblock(ThreadID tid)
254 // Decode is done unblocking only if the skid buffer is empty.
255 if (skidBuffer[tid].empty()) {
256 DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
257 toFetch->decodeUnblock[tid] = true;
258 wroteToTimeBuffer = true;
260 decodeStatus[tid] = Running;
264 DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
271 DefaultDecode<Impl>::squash(DynInstPtr &inst, ThreadID tid)
273 DPRINTF(Decode, "[tid:%i]: [sn:%i] Squashing due to incorrect branch "
274 "prediction detected at decode.\n", tid, inst->seqNum);
276 // Send back mispredict information.
277 toFetch->decodeInfo[tid].branchMispredict = true;
278 toFetch->decodeInfo[tid].predIncorrect = true;
279 toFetch->decodeInfo[tid].squash = true;
280 toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
281 toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
282 toFetch->decodeInfo[tid].branchTaken = inst->pcState().branching();
283 toFetch->decodeInfo[tid].squashInst = inst;
285 InstSeqNum squash_seq_num = inst->seqNum;
287 // Might have to tell fetch to unblock.
288 if (decodeStatus[tid] == Blocked ||
289 decodeStatus[tid] == Unblocking) {
290 toFetch->decodeUnblock[tid] = 1;
293 // Set status to squashing.
294 decodeStatus[tid] = Squashing;
296 for (int i=0; i<fromFetch->size; i++) {
297 if (fromFetch->insts[i]->threadNumber == tid &&
298 fromFetch->insts[i]->seqNum > squash_seq_num) {
299 fromFetch->insts[i]->setSquashed();
303 // Clear the instruction list and skid buffer in case they have any
305 while (!insts[tid].empty()) {
309 while (!skidBuffer[tid].empty()) {
310 skidBuffer[tid].pop();
313 // Squash instructions up until this one
314 cpu->removeInstsUntil(squash_seq_num, tid);
319 DefaultDecode<Impl>::squash(ThreadID tid)
321 DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
323 if (decodeStatus[tid] == Blocked ||
324 decodeStatus[tid] == Unblocking) {
326 toFetch->decodeUnblock[tid] = 1;
328 // In syscall emulation, we can have both a block and a squash due
329 // to a syscall in the same cycle. This would cause both signals
330 // to be high. This shouldn't happen in full system.
331 // @todo: Determine if this still happens.
332 if (toFetch->decodeBlock[tid])
333 toFetch->decodeBlock[tid] = 0;
335 toFetch->decodeUnblock[tid] = 1;
339 // Set status to squashing.
340 decodeStatus[tid] = Squashing;
342 // Go through incoming instructions from fetch and squash them.
343 unsigned squash_count = 0;
345 for (int i=0; i<fromFetch->size; i++) {
346 if (fromFetch->insts[i]->threadNumber == tid) {
347 fromFetch->insts[i]->setSquashed();
352 // Clear the instruction list and skid buffer in case they have any
354 while (!insts[tid].empty()) {
358 while (!skidBuffer[tid].empty()) {
359 skidBuffer[tid].pop();
367 DefaultDecode<Impl>::skidInsert(ThreadID tid)
369 DynInstPtr inst = NULL;
371 while (!insts[tid].empty()) {
372 inst = insts[tid].front();
376 assert(tid == inst->threadNumber);
378 DPRINTF(Decode,"Inserting [sn:%lli] PC: %s into decode skidBuffer %i\n",
379 inst->seqNum, inst->pcState(), inst->threadNumber);
381 skidBuffer[tid].push(inst);
384 // @todo: Eventually need to enforce this by not letting a thread
385 // fetch past its skidbuffer
386 assert(skidBuffer[tid].size() <= skidBufferMax);
391 DefaultDecode<Impl>::skidsEmpty()
393 list<ThreadID>::iterator threads = activeThreads->begin();
394 list<ThreadID>::iterator end = activeThreads->end();
396 while (threads != end) {
397 ThreadID tid = *threads++;
398 if (!skidBuffer[tid].empty())
407 DefaultDecode<Impl>::updateStatus()
409 bool any_unblocking = false;
411 list<ThreadID>::iterator threads = activeThreads->begin();
412 list<ThreadID>::iterator end = activeThreads->end();
414 while (threads != end) {
415 ThreadID tid = *threads++;
417 if (decodeStatus[tid] == Unblocking) {
418 any_unblocking = true;
423 // Decode will have activity if it's unblocking.
424 if (any_unblocking) {
425 if (_status == Inactive) {
428 DPRINTF(Activity, "Activating stage.\n");
430 cpu->activateStage(O3CPU::DecodeIdx);
433 // If it's not unblocking, then decode will not have any internal
434 // activity. Switch it to inactive.
435 if (_status == Active) {
437 DPRINTF(Activity, "Deactivating stage.\n");
439 cpu->deactivateStage(O3CPU::DecodeIdx);
444 template <class Impl>
446 DefaultDecode<Impl>::sortInsts()
448 int insts_from_fetch = fromFetch->size;
449 for (int i = 0; i < insts_from_fetch; ++i) {
450 insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
456 DefaultDecode<Impl>::readStallSignals(ThreadID tid)
458 if (fromRename->renameBlock[tid]) {
459 stalls[tid].rename = true;
462 if (fromRename->renameUnblock[tid]) {
463 assert(stalls[tid].rename);
464 stalls[tid].rename = false;
467 if (fromIEW->iewBlock[tid]) {
468 stalls[tid].iew = true;
471 if (fromIEW->iewUnblock[tid]) {
472 assert(stalls[tid].iew);
473 stalls[tid].iew = false;
476 if (fromCommit->commitBlock[tid]) {
477 stalls[tid].commit = true;
480 if (fromCommit->commitUnblock[tid]) {
481 assert(stalls[tid].commit);
482 stalls[tid].commit = false;
486 template <class Impl>
488 DefaultDecode<Impl>::checkSignalsAndUpdate(ThreadID tid)
490 // Check if there's a squash signal, squash if there is.
491 // Check stall signals, block if necessary.
492 // If status was blocked
493 // Check if stall conditions have passed
494 // if so then go to unblocking
495 // If status was Squashing
496 // check if squashing is not high. Switch to running this cycle.
498 // Update the per thread stall statuses.
499 readStallSignals(tid);
501 // Check squash signals from commit.
502 if (fromCommit->commitInfo[tid].squash) {
504 DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
505 "from commit.\n", tid);
512 // Check ROB squash signals from commit.
513 if (fromCommit->commitInfo[tid].robSquashing) {
514 DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
516 // Continue to squash.
517 decodeStatus[tid] = Squashing;
522 if (checkStall(tid)) {
526 if (decodeStatus[tid] == Blocked) {
527 DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
530 decodeStatus[tid] = Unblocking;
537 if (decodeStatus[tid] == Squashing) {
538 // Switch status to running if decode isn't being told to block or
539 // squash this cycle.
540 DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
543 decodeStatus[tid] = Running;
548 // If we've reached this point, we have not gotten any signals that
549 // cause decode to change its status. Decode remains the same as before.
555 DefaultDecode<Impl>::tick()
557 wroteToTimeBuffer = false;
559 bool status_change = false;
563 list<ThreadID>::iterator threads = activeThreads->begin();
564 list<ThreadID>::iterator end = activeThreads->end();
568 //Check stall and squash signals.
569 while (threads != end) {
570 ThreadID tid = *threads++;
572 DPRINTF(Decode,"Processing [tid:%i]\n",tid);
573 status_change = checkSignalsAndUpdate(tid) || status_change;
575 decode(status_change, tid);
582 if (wroteToTimeBuffer) {
583 DPRINTF(Activity, "Activity this cycle.\n");
585 cpu->activityThisCycle();
591 DefaultDecode<Impl>::decode(bool &status_change, ThreadID tid)
593 // If status is Running or idle,
594 // call decodeInsts()
595 // If status is Unblocking,
596 // buffer any instructions coming from fetch
597 // continue trying to empty skid buffer
598 // check if stall conditions have passed
600 if (decodeStatus[tid] == Blocked) {
601 ++decodeBlockedCycles;
602 } else if (decodeStatus[tid] == Squashing) {
603 ++decodeSquashCycles;
606 // Decode should try to decode as many instructions as its bandwidth
607 // will allow, as long as it is not currently blocked.
608 if (decodeStatus[tid] == Running ||
609 decodeStatus[tid] == Idle) {
610 DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
614 } else if (decodeStatus[tid] == Unblocking) {
615 // Make sure that the skid buffer has something in it if the
616 // status is unblocking.
617 assert(!skidsEmpty());
619 // If the status was unblocking, then instructions from the skid
620 // buffer were used. Remove those instructions and handle
621 // the rest of unblocking.
624 if (fetchInstsValid()) {
625 // Add the current inputs to the skid buffer so they can be
626 // reprocessed when this stage unblocks.
630 status_change = unblock(tid) || status_change;
634 template <class Impl>
636 DefaultDecode<Impl>::decodeInsts(ThreadID tid)
638 // Instructions can come either from the skid buffer or the list of
639 // instructions coming from fetch, depending on decode's status.
640 int insts_available = decodeStatus[tid] == Unblocking ?
641 skidBuffer[tid].size() : insts[tid].size();
643 if (insts_available == 0) {
644 DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
646 // Should I change the status to idle?
649 } else if (decodeStatus[tid] == Unblocking) {
650 DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
652 ++decodeUnblockCycles;
653 } else if (decodeStatus[tid] == Running) {
659 std::queue<DynInstPtr>
660 &insts_to_decode = decodeStatus[tid] == Unblocking ?
661 skidBuffer[tid] : insts[tid];
663 DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
665 while (insts_available > 0 && toRenameIndex < decodeWidth) {
666 assert(!insts_to_decode.empty());
668 inst = insts_to_decode.front();
670 insts_to_decode.pop();
672 DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
673 "PC %s\n", tid, inst->seqNum, inst->pcState());
675 if (inst->isSquashed()) {
676 DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %s is "
677 "squashed, skipping.\n",
678 tid, inst->seqNum, inst->pcState());
680 ++decodeSquashedInsts;
687 // Also check if instructions have no source registers. Mark
688 // them as ready to issue at any time. Not sure if this check
689 // should exist here or at a later stage; however it doesn't matter
690 // too much for function correctness.
691 if (inst->numSrcRegs() == 0) {
695 // This current instruction is valid, so add it into the decode
696 // queue. The next instruction may not be valid, so check to
697 // see if branches were predicted correctly.
698 toRename->insts[toRenameIndex] = inst;
702 ++decodeDecodedInsts;
706 inst->decodeTick = curTick();
709 // Ensure that if it was predicted as a branch, it really is a
711 if (inst->readPredTaken() && !inst->isControl()) {
712 panic("Instruction predicted as a branch!");
714 ++decodeControlMispred;
716 // Might want to set some sort of boolean and just do
717 // a check at the end
718 squash(inst, inst->threadNumber);
723 // Go ahead and compute any PC-relative branches.
724 if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
725 ++decodeBranchResolved;
727 if (!(inst->branchTarget() == inst->readPredTarg())) {
728 ++decodeBranchMispred;
730 // Might want to set some sort of boolean and just do
731 // a check at the end
732 squash(inst, inst->threadNumber);
733 TheISA::PCState target = inst->branchTarget();
735 DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %s\n",
736 inst->seqNum, target);
737 //The micro pc after an instruction level branch should be 0
738 inst->setPredTarg(target);
744 // If we didn't process all instructions, then we will need to block
745 // and put all those instructions into the skid buffer.
746 if (!insts_to_decode.empty()) {
750 // Record that decode has written to the time buffer for activity
753 wroteToTimeBuffer = true;