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 "cpu/o3/decode.hh"
33 #include "params/DerivO3CPU.hh"
36 DefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params)
38 renameToDecodeDelay(params->renameToDecodeDelay),
39 iewToDecodeDelay(params->iewToDecodeDelay),
40 commitToDecodeDelay(params->commitToDecodeDelay),
41 fetchToDecodeDelay(params->fetchToDecodeDelay),
42 decodeWidth(params->decodeWidth),
43 numThreads(params->numThreads)
47 // Setup status, make sure stall signals are clear.
48 for (int i = 0; i < numThreads; ++i) {
49 decodeStatus[i] = Idle;
51 stalls[i].rename = false;
52 stalls[i].iew = false;
53 stalls[i].commit = false;
56 // @todo: Make into a parameter
57 skidBufferMax = (fetchToDecodeDelay * params->fetchWidth) + decodeWidth;
62 DefaultDecode<Impl>::name() const
64 return cpu->name() + ".decode";
69 DefaultDecode<Impl>::regStats()
72 .name(name() + ".DECODE:IdleCycles")
73 .desc("Number of cycles decode is idle")
74 .prereq(decodeIdleCycles);
76 .name(name() + ".DECODE:BlockedCycles")
77 .desc("Number of cycles decode is blocked")
78 .prereq(decodeBlockedCycles);
80 .name(name() + ".DECODE:RunCycles")
81 .desc("Number of cycles decode is running")
82 .prereq(decodeRunCycles);
84 .name(name() + ".DECODE:UnblockCycles")
85 .desc("Number of cycles decode is unblocking")
86 .prereq(decodeUnblockCycles);
88 .name(name() + ".DECODE:SquashCycles")
89 .desc("Number of cycles decode is squashing")
90 .prereq(decodeSquashCycles);
92 .name(name() + ".DECODE:BranchResolved")
93 .desc("Number of times decode resolved a branch")
94 .prereq(decodeBranchResolved);
96 .name(name() + ".DECODE:BranchMispred")
97 .desc("Number of times decode detected a branch misprediction")
98 .prereq(decodeBranchMispred);
100 .name(name() + ".DECODE:ControlMispred")
101 .desc("Number of times decode detected an instruction incorrectly"
102 " predicted as a control")
103 .prereq(decodeControlMispred);
105 .name(name() + ".DECODE:DecodedInsts")
106 .desc("Number of instructions handled by decode")
107 .prereq(decodeDecodedInsts);
109 .name(name() + ".DECODE:SquashedInsts")
110 .desc("Number of squashed instructions handled by decode")
111 .prereq(decodeSquashedInsts);
116 DefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
120 // Setup wire to write information back to fetch.
121 toFetch = timeBuffer->getWire(0);
123 // Create wires to get information from proper places in time buffer.
124 fromRename = timeBuffer->getWire(-renameToDecodeDelay);
125 fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
126 fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
131 DefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
133 decodeQueue = dq_ptr;
135 // Setup wire to write information to proper place in decode queue.
136 toRename = decodeQueue->getWire(0);
141 DefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
145 // Setup wire to read information from fetch queue.
146 fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
151 DefaultDecode<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
153 activeThreads = at_ptr;
156 template <class Impl>
158 DefaultDecode<Impl>::drain()
160 // Decode is done draining at any time.
161 cpu->signalDrained();
165 template <class Impl>
167 DefaultDecode<Impl>::takeOverFrom()
171 // Be sure to reset state and clear out any old instructions.
172 for (int i = 0; i < numThreads; ++i) {
173 decodeStatus[i] = Idle;
175 stalls[i].rename = false;
176 stalls[i].iew = false;
177 stalls[i].commit = false;
178 while (!insts[i].empty())
180 while (!skidBuffer[i].empty())
184 wroteToTimeBuffer = false;
189 DefaultDecode<Impl>::checkStall(unsigned tid) const
191 bool ret_val = false;
193 if (stalls[tid].rename) {
194 DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
196 } else if (stalls[tid].iew) {
197 DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
199 } else if (stalls[tid].commit) {
200 DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
209 DefaultDecode<Impl>::fetchInstsValid()
211 return fromFetch->size > 0;
216 DefaultDecode<Impl>::block(unsigned tid)
218 DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
220 // Add the current inputs to the skid buffer so they can be
221 // reprocessed when this stage unblocks.
224 // If the decode status is blocked or unblocking then decode has not yet
225 // signalled fetch to unblock. In that case, there is no need to tell
227 if (decodeStatus[tid] != Blocked) {
228 // Set the status to Blocked.
229 decodeStatus[tid] = Blocked;
231 if (decodeStatus[tid] != Unblocking) {
232 toFetch->decodeBlock[tid] = true;
233 wroteToTimeBuffer = true;
244 DefaultDecode<Impl>::unblock(unsigned tid)
246 // Decode is done unblocking only if the skid buffer is empty.
247 if (skidBuffer[tid].empty()) {
248 DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
249 toFetch->decodeUnblock[tid] = true;
250 wroteToTimeBuffer = true;
252 decodeStatus[tid] = Running;
256 DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
263 DefaultDecode<Impl>::squash(DynInstPtr &inst, unsigned tid)
265 DPRINTF(Decode, "[tid:%i]: [sn:%i] Squashing due to incorrect branch prediction "
266 "detected at decode.\n", tid, inst->seqNum);
268 // Send back mispredict information.
269 toFetch->decodeInfo[tid].branchMispredict = true;
270 toFetch->decodeInfo[tid].predIncorrect = true;
271 toFetch->decodeInfo[tid].squash = true;
272 toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
273 toFetch->decodeInfo[tid].nextMicroPC = inst->readMicroPC();
275 #if ISA_HAS_DELAY_SLOT
276 toFetch->decodeInfo[tid].nextPC = inst->readPC() + sizeof(TheISA::MachInst);
277 toFetch->decodeInfo[tid].nextNPC = inst->branchTarget();
278 toFetch->decodeInfo[tid].branchTaken = inst->readNextNPC() !=
279 (inst->readNextPC() + sizeof(TheISA::MachInst));
281 toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
282 toFetch->decodeInfo[tid].nextNPC =
283 inst->branchTarget() + sizeof(TheISA::MachInst);
284 toFetch->decodeInfo[tid].branchTaken =
285 inst->readNextPC() != (inst->readPC() + sizeof(TheISA::MachInst));
289 InstSeqNum squash_seq_num = inst->seqNum;
291 // Might have to tell fetch to unblock.
292 if (decodeStatus[tid] == Blocked ||
293 decodeStatus[tid] == Unblocking) {
294 toFetch->decodeUnblock[tid] = 1;
297 // Set status to squashing.
298 decodeStatus[tid] = Squashing;
300 for (int i=0; i<fromFetch->size; i++) {
301 if (fromFetch->insts[i]->threadNumber == tid &&
302 fromFetch->insts[i]->seqNum > squash_seq_num) {
303 fromFetch->insts[i]->setSquashed();
307 // Clear the instruction list and skid buffer in case they have any
309 while (!insts[tid].empty()) {
313 while (!skidBuffer[tid].empty()) {
314 skidBuffer[tid].pop();
317 // Squash instructions up until this one
318 cpu->removeInstsUntil(squash_seq_num, tid);
323 DefaultDecode<Impl>::squash(unsigned tid)
325 DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
327 if (decodeStatus[tid] == Blocked ||
328 decodeStatus[tid] == Unblocking) {
330 // In syscall emulation, we can have both a block and a squash due
331 // to a syscall in the same cycle. This would cause both signals to
332 // be high. This shouldn't happen in full system.
333 // @todo: Determine if this still happens.
334 if (toFetch->decodeBlock[tid]) {
335 toFetch->decodeBlock[tid] = 0;
337 toFetch->decodeUnblock[tid] = 1;
340 toFetch->decodeUnblock[tid] = 1;
344 // Set status to squashing.
345 decodeStatus[tid] = Squashing;
347 // Go through incoming instructions from fetch and squash them.
348 unsigned squash_count = 0;
350 for (int i=0; i<fromFetch->size; i++) {
351 if (fromFetch->insts[i]->threadNumber == tid) {
352 fromFetch->insts[i]->setSquashed();
357 // Clear the instruction list and skid buffer in case they have any
359 while (!insts[tid].empty()) {
363 while (!skidBuffer[tid].empty()) {
364 skidBuffer[tid].pop();
372 DefaultDecode<Impl>::skidInsert(unsigned tid)
374 DynInstPtr inst = NULL;
376 while (!insts[tid].empty()) {
377 inst = insts[tid].front();
381 assert(tid == inst->threadNumber);
383 DPRINTF(Decode,"Inserting [sn:%lli] PC:%#x into decode skidBuffer %i\n",
384 inst->seqNum, inst->readPC(), inst->threadNumber);
386 skidBuffer[tid].push(inst);
389 // @todo: Eventually need to enforce this by not letting a thread
390 // fetch past its skidbuffer
391 assert(skidBuffer[tid].size() <= skidBufferMax);
396 DefaultDecode<Impl>::skidsEmpty()
398 std::list<unsigned>::iterator threads = activeThreads->begin();
399 std::list<unsigned>::iterator end = activeThreads->end();
401 while (threads != end) {
402 unsigned tid = *threads++;
403 if (!skidBuffer[tid].empty())
412 DefaultDecode<Impl>::updateStatus()
414 bool any_unblocking = false;
416 std::list<unsigned>::iterator threads = activeThreads->begin();
417 std::list<unsigned>::iterator end = activeThreads->end();
419 while (threads != end) {
420 unsigned tid = *threads++;
422 if (decodeStatus[tid] == Unblocking) {
423 any_unblocking = true;
428 // Decode will have activity if it's unblocking.
429 if (any_unblocking) {
430 if (_status == Inactive) {
433 DPRINTF(Activity, "Activating stage.\n");
435 cpu->activateStage(O3CPU::DecodeIdx);
438 // If it's not unblocking, then decode will not have any internal
439 // activity. Switch it to inactive.
440 if (_status == Active) {
442 DPRINTF(Activity, "Deactivating stage.\n");
444 cpu->deactivateStage(O3CPU::DecodeIdx);
449 template <class Impl>
451 DefaultDecode<Impl>::sortInsts()
453 int insts_from_fetch = fromFetch->size;
455 for (int i=0; i < numThreads; i++)
456 assert(insts[i].empty());
458 for (int i = 0; i < insts_from_fetch; ++i) {
459 insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
465 DefaultDecode<Impl>::readStallSignals(unsigned tid)
467 if (fromRename->renameBlock[tid]) {
468 stalls[tid].rename = true;
471 if (fromRename->renameUnblock[tid]) {
472 assert(stalls[tid].rename);
473 stalls[tid].rename = false;
476 if (fromIEW->iewBlock[tid]) {
477 stalls[tid].iew = true;
480 if (fromIEW->iewUnblock[tid]) {
481 assert(stalls[tid].iew);
482 stalls[tid].iew = false;
485 if (fromCommit->commitBlock[tid]) {
486 stalls[tid].commit = true;
489 if (fromCommit->commitUnblock[tid]) {
490 assert(stalls[tid].commit);
491 stalls[tid].commit = false;
495 template <class Impl>
497 DefaultDecode<Impl>::checkSignalsAndUpdate(unsigned tid)
499 // Check if there's a squash signal, squash if there is.
500 // Check stall signals, block if necessary.
501 // If status was blocked
502 // Check if stall conditions have passed
503 // if so then go to unblocking
504 // If status was Squashing
505 // check if squashing is not high. Switch to running this cycle.
507 // Update the per thread stall statuses.
508 readStallSignals(tid);
510 // Check squash signals from commit.
511 if (fromCommit->commitInfo[tid].squash) {
513 DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
514 "from commit.\n", tid);
521 // Check ROB squash signals from commit.
522 if (fromCommit->commitInfo[tid].robSquashing) {
523 DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
525 // Continue to squash.
526 decodeStatus[tid] = Squashing;
531 if (checkStall(tid)) {
535 if (decodeStatus[tid] == Blocked) {
536 DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
539 decodeStatus[tid] = Unblocking;
546 if (decodeStatus[tid] == Squashing) {
547 // Switch status to running if decode isn't being told to block or
548 // squash this cycle.
549 DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
552 decodeStatus[tid] = Running;
557 // If we've reached this point, we have not gotten any signals that
558 // cause decode to change its status. Decode remains the same as before.
564 DefaultDecode<Impl>::tick()
566 wroteToTimeBuffer = false;
568 bool status_change = false;
572 std::list<unsigned>::iterator threads = activeThreads->begin();
573 std::list<unsigned>::iterator end = activeThreads->end();
577 //Check stall and squash signals.
578 while (threads != end) {
579 unsigned tid = *threads++;
581 DPRINTF(Decode,"Processing [tid:%i]\n",tid);
582 status_change = checkSignalsAndUpdate(tid) || status_change;
584 decode(status_change, tid);
591 if (wroteToTimeBuffer) {
592 DPRINTF(Activity, "Activity this cycle.\n");
594 cpu->activityThisCycle();
600 DefaultDecode<Impl>::decode(bool &status_change, unsigned tid)
602 // If status is Running or idle,
603 // call decodeInsts()
604 // If status is Unblocking,
605 // buffer any instructions coming from fetch
606 // continue trying to empty skid buffer
607 // check if stall conditions have passed
609 if (decodeStatus[tid] == Blocked) {
610 ++decodeBlockedCycles;
611 } else if (decodeStatus[tid] == Squashing) {
612 ++decodeSquashCycles;
615 // Decode should try to decode as many instructions as its bandwidth
616 // will allow, as long as it is not currently blocked.
617 if (decodeStatus[tid] == Running ||
618 decodeStatus[tid] == Idle) {
619 DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
623 } else if (decodeStatus[tid] == Unblocking) {
624 // Make sure that the skid buffer has something in it if the
625 // status is unblocking.
626 assert(!skidsEmpty());
628 // If the status was unblocking, then instructions from the skid
629 // buffer were used. Remove those instructions and handle
630 // the rest of unblocking.
633 if (fetchInstsValid()) {
634 // Add the current inputs to the skid buffer so they can be
635 // reprocessed when this stage unblocks.
639 status_change = unblock(tid) || status_change;
643 template <class Impl>
645 DefaultDecode<Impl>::decodeInsts(unsigned tid)
647 // Instructions can come either from the skid buffer or the list of
648 // instructions coming from fetch, depending on decode's status.
649 int insts_available = decodeStatus[tid] == Unblocking ?
650 skidBuffer[tid].size() : insts[tid].size();
652 if (insts_available == 0) {
653 DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
655 // Should I change the status to idle?
658 } else if (decodeStatus[tid] == Unblocking) {
659 DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
661 ++decodeUnblockCycles;
662 } else if (decodeStatus[tid] == Running) {
668 std::queue<DynInstPtr>
669 &insts_to_decode = decodeStatus[tid] == Unblocking ?
670 skidBuffer[tid] : insts[tid];
672 DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
674 while (insts_available > 0 && toRenameIndex < decodeWidth) {
675 assert(!insts_to_decode.empty());
677 inst = insts_to_decode.front();
679 insts_to_decode.pop();
681 DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
683 tid, inst->seqNum, inst->readPC());
685 if (inst->isSquashed()) {
686 DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %#x is "
687 "squashed, skipping.\n",
688 tid, inst->seqNum, inst->readPC());
690 ++decodeSquashedInsts;
697 // Also check if instructions have no source registers. Mark
698 // them as ready to issue at any time. Not sure if this check
699 // should exist here or at a later stage; however it doesn't matter
700 // too much for function correctness.
701 if (inst->numSrcRegs() == 0) {
705 // This current instruction is valid, so add it into the decode
706 // queue. The next instruction may not be valid, so check to
707 // see if branches were predicted correctly.
708 toRename->insts[toRenameIndex] = inst;
712 ++decodeDecodedInsts;
715 // Ensure that if it was predicted as a branch, it really is a
717 if (inst->readPredTaken() && !inst->isControl()) {
718 DPRINTF(Decode, "PredPC : %#x != NextPC: %#x\n",
719 inst->readPredPC(), inst->readNextPC() + 4);
721 panic("Instruction predicted as a branch!");
723 ++decodeControlMispred;
725 // Might want to set some sort of boolean and just do
726 // a check at the end
727 squash(inst, inst->threadNumber);
732 // Go ahead and compute any PC-relative branches.
733 if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
734 ++decodeBranchResolved;
736 if (inst->branchTarget() != inst->readPredPC()) {
737 ++decodeBranchMispred;
739 // Might want to set some sort of boolean and just do
740 // a check at the end
741 squash(inst, inst->threadNumber);
742 Addr target = inst->branchTarget();
744 #if ISA_HAS_DELAY_SLOT
745 DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %#x PredNextPC: %#x\n",
746 inst->seqNum, inst->readPC() + sizeof(TheISA::MachInst), target);
748 //The micro pc after an instruction level branch should be 0
749 inst->setPredTarg(inst->readPC() + sizeof(TheISA::MachInst), target, 0);
751 DPRINTF(Decode, "[sn:%i]: Updating predictions: PredPC: %#x PredNextPC: %#x\n",
752 inst->seqNum, target, target + sizeof(TheISA::MachInst));
753 //The micro pc after an instruction level branch should be 0
754 inst->setPredTarg(target, target + sizeof(TheISA::MachInst), 0);
761 // If we didn't process all instructions, then we will need to block
762 // and put all those instructions into the skid buffer.
763 if (!insts_to_decode.empty()) {
767 // Record that decode has written to the time buffer for activity
770 wroteToTimeBuffer = true;