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"
34 DefaultDecode<Impl>::DefaultDecode(O3CPU *_cpu, Params *params)
36 renameToDecodeDelay(params->renameToDecodeDelay),
37 iewToDecodeDelay(params->iewToDecodeDelay),
38 commitToDecodeDelay(params->commitToDecodeDelay),
39 fetchToDecodeDelay(params->fetchToDecodeDelay),
40 decodeWidth(params->decodeWidth),
41 numThreads(params->numberOfThreads)
45 // Setup status, make sure stall signals are clear.
46 for (int i = 0; i < numThreads; ++i) {
47 decodeStatus[i] = Idle;
49 stalls[i].rename = false;
50 stalls[i].iew = false;
51 stalls[i].commit = false;
54 // @todo: Make into a parameter
55 skidBufferMax = (fetchToDecodeDelay * params->fetchWidth) + decodeWidth;
60 DefaultDecode<Impl>::name() const
62 return cpu->name() + ".decode";
67 DefaultDecode<Impl>::regStats()
70 .name(name() + ".DECODE:IdleCycles")
71 .desc("Number of cycles decode is idle")
72 .prereq(decodeIdleCycles);
74 .name(name() + ".DECODE:BlockedCycles")
75 .desc("Number of cycles decode is blocked")
76 .prereq(decodeBlockedCycles);
78 .name(name() + ".DECODE:RunCycles")
79 .desc("Number of cycles decode is running")
80 .prereq(decodeRunCycles);
82 .name(name() + ".DECODE:UnblockCycles")
83 .desc("Number of cycles decode is unblocking")
84 .prereq(decodeUnblockCycles);
86 .name(name() + ".DECODE:SquashCycles")
87 .desc("Number of cycles decode is squashing")
88 .prereq(decodeSquashCycles);
90 .name(name() + ".DECODE:BranchResolved")
91 .desc("Number of times decode resolved a branch")
92 .prereq(decodeBranchResolved);
94 .name(name() + ".DECODE:BranchMispred")
95 .desc("Number of times decode detected a branch misprediction")
96 .prereq(decodeBranchMispred);
98 .name(name() + ".DECODE:ControlMispred")
99 .desc("Number of times decode detected an instruction incorrectly"
100 " predicted as a control")
101 .prereq(decodeControlMispred);
103 .name(name() + ".DECODE:DecodedInsts")
104 .desc("Number of instructions handled by decode")
105 .prereq(decodeDecodedInsts);
107 .name(name() + ".DECODE:SquashedInsts")
108 .desc("Number of squashed instructions handled by decode")
109 .prereq(decodeSquashedInsts);
114 DefaultDecode<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
118 // Setup wire to write information back to fetch.
119 toFetch = timeBuffer->getWire(0);
121 // Create wires to get information from proper places in time buffer.
122 fromRename = timeBuffer->getWire(-renameToDecodeDelay);
123 fromIEW = timeBuffer->getWire(-iewToDecodeDelay);
124 fromCommit = timeBuffer->getWire(-commitToDecodeDelay);
129 DefaultDecode<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
131 decodeQueue = dq_ptr;
133 // Setup wire to write information to proper place in decode queue.
134 toRename = decodeQueue->getWire(0);
139 DefaultDecode<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
143 // Setup wire to read information from fetch queue.
144 fromFetch = fetchQueue->getWire(-fetchToDecodeDelay);
149 DefaultDecode<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
151 activeThreads = at_ptr;
154 template <class Impl>
156 DefaultDecode<Impl>::drain()
158 // Decode is done draining at any time.
159 cpu->signalDrained();
163 template <class Impl>
165 DefaultDecode<Impl>::takeOverFrom()
169 // Be sure to reset state and clear out any old instructions.
170 for (int i = 0; i < numThreads; ++i) {
171 decodeStatus[i] = Idle;
173 stalls[i].rename = false;
174 stalls[i].iew = false;
175 stalls[i].commit = false;
176 while (!insts[i].empty())
178 while (!skidBuffer[i].empty())
182 wroteToTimeBuffer = false;
187 DefaultDecode<Impl>::checkStall(unsigned tid) const
189 bool ret_val = false;
191 if (stalls[tid].rename) {
192 DPRINTF(Decode,"[tid:%i]: Stall fom Rename stage detected.\n", tid);
194 } else if (stalls[tid].iew) {
195 DPRINTF(Decode,"[tid:%i]: Stall fom IEW stage detected.\n", tid);
197 } else if (stalls[tid].commit) {
198 DPRINTF(Decode,"[tid:%i]: Stall fom Commit stage detected.\n", tid);
207 DefaultDecode<Impl>::fetchInstsValid()
209 return fromFetch->size > 0;
214 DefaultDecode<Impl>::block(unsigned tid)
216 DPRINTF(Decode, "[tid:%u]: Blocking.\n", tid);
218 // Add the current inputs to the skid buffer so they can be
219 // reprocessed when this stage unblocks.
222 // If the decode status is blocked or unblocking then decode has not yet
223 // signalled fetch to unblock. In that case, there is no need to tell
225 if (decodeStatus[tid] != Blocked) {
226 // Set the status to Blocked.
227 decodeStatus[tid] = Blocked;
229 if (decodeStatus[tid] != Unblocking) {
230 toFetch->decodeBlock[tid] = true;
231 wroteToTimeBuffer = true;
242 DefaultDecode<Impl>::unblock(unsigned tid)
244 // Decode is done unblocking only if the skid buffer is empty.
245 if (skidBuffer[tid].empty()) {
246 DPRINTF(Decode, "[tid:%u]: Done unblocking.\n", tid);
247 toFetch->decodeUnblock[tid] = true;
248 wroteToTimeBuffer = true;
250 decodeStatus[tid] = Running;
254 DPRINTF(Decode, "[tid:%u]: Currently unblocking.\n", tid);
261 DefaultDecode<Impl>::squash(DynInstPtr &inst, unsigned tid)
263 DPRINTF(Decode, "[tid:%i]: Squashing due to incorrect branch prediction "
264 "detected at decode.\n", tid);
266 // Send back mispredict information.
267 toFetch->decodeInfo[tid].branchMispredict = true;
268 toFetch->decodeInfo[tid].predIncorrect = true;
269 toFetch->decodeInfo[tid].doneSeqNum = inst->seqNum;
270 toFetch->decodeInfo[tid].squash = true;
271 toFetch->decodeInfo[tid].nextPC = inst->branchTarget();
272 ///FIXME There needs to be a way to set the nextPC and nextNPC
273 ///explicitly for ISAs with delay slots.
274 toFetch->decodeInfo[tid].nextNPC =
275 inst->branchTarget() + sizeof(TheISA::MachInst);
276 toFetch->decodeInfo[tid].nextMicroPC = inst->readMicroPC();
277 #if ISA_HAS_DELAY_SLOT
278 toFetch->decodeInfo[tid].branchTaken = inst->readNextNPC() !=
279 (inst->readNextPC() + sizeof(TheISA::MachInst));
281 toFetch->decodeInfo[tid].branchTaken =
282 inst->readNextPC() != (inst->readPC() + sizeof(TheISA::MachInst));
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(unsigned tid)
321 DPRINTF(Decode, "[tid:%i]: Squashing.\n",tid);
323 if (decodeStatus[tid] == Blocked ||
324 decodeStatus[tid] == Unblocking) {
326 // In syscall emulation, we can have both a block and a squash due
327 // to a syscall in the same cycle. This would cause both signals to
328 // be high. This shouldn't happen in full system.
329 // @todo: Determine if this still happens.
330 if (toFetch->decodeBlock[tid]) {
331 toFetch->decodeBlock[tid] = 0;
333 toFetch->decodeUnblock[tid] = 1;
336 toFetch->decodeUnblock[tid] = 1;
340 // Set status to squashing.
341 decodeStatus[tid] = Squashing;
343 // Go through incoming instructions from fetch and squash them.
344 unsigned squash_count = 0;
346 for (int i=0; i<fromFetch->size; i++) {
347 if (fromFetch->insts[i]->threadNumber == tid) {
348 fromFetch->insts[i]->setSquashed();
353 // Clear the instruction list and skid buffer in case they have any
355 while (!insts[tid].empty()) {
359 while (!skidBuffer[tid].empty()) {
360 skidBuffer[tid].pop();
368 DefaultDecode<Impl>::skidInsert(unsigned tid)
370 DynInstPtr inst = NULL;
372 while (!insts[tid].empty()) {
373 inst = insts[tid].front();
377 assert(tid == inst->threadNumber);
379 DPRINTF(Decode,"Inserting [sn:%lli] PC:%#x into decode skidBuffer %i\n",
380 inst->seqNum, inst->readPC(), inst->threadNumber);
382 skidBuffer[tid].push(inst);
385 // @todo: Eventually need to enforce this by not letting a thread
386 // fetch past its skidbuffer
387 assert(skidBuffer[tid].size() <= skidBufferMax);
392 DefaultDecode<Impl>::skidsEmpty()
394 std::list<unsigned>::iterator threads = activeThreads->begin();
395 std::list<unsigned>::iterator end = activeThreads->end();
397 while (threads != end) {
398 unsigned tid = *threads++;
399 if (!skidBuffer[tid].empty())
408 DefaultDecode<Impl>::updateStatus()
410 bool any_unblocking = false;
412 std::list<unsigned>::iterator threads = activeThreads->begin();
413 std::list<unsigned>::iterator end = activeThreads->end();
415 while (threads != end) {
416 unsigned tid = *threads++;
418 if (decodeStatus[tid] == Unblocking) {
419 any_unblocking = true;
424 // Decode will have activity if it's unblocking.
425 if (any_unblocking) {
426 if (_status == Inactive) {
429 DPRINTF(Activity, "Activating stage.\n");
431 cpu->activateStage(O3CPU::DecodeIdx);
434 // If it's not unblocking, then decode will not have any internal
435 // activity. Switch it to inactive.
436 if (_status == Active) {
438 DPRINTF(Activity, "Deactivating stage.\n");
440 cpu->deactivateStage(O3CPU::DecodeIdx);
445 template <class Impl>
447 DefaultDecode<Impl>::sortInsts()
449 int insts_from_fetch = fromFetch->size;
451 for (int i=0; i < numThreads; i++)
452 assert(insts[i].empty());
454 for (int i = 0; i < insts_from_fetch; ++i) {
455 insts[fromFetch->insts[i]->threadNumber].push(fromFetch->insts[i]);
461 DefaultDecode<Impl>::readStallSignals(unsigned tid)
463 if (fromRename->renameBlock[tid]) {
464 stalls[tid].rename = true;
467 if (fromRename->renameUnblock[tid]) {
468 assert(stalls[tid].rename);
469 stalls[tid].rename = false;
472 if (fromIEW->iewBlock[tid]) {
473 stalls[tid].iew = true;
476 if (fromIEW->iewUnblock[tid]) {
477 assert(stalls[tid].iew);
478 stalls[tid].iew = false;
481 if (fromCommit->commitBlock[tid]) {
482 stalls[tid].commit = true;
485 if (fromCommit->commitUnblock[tid]) {
486 assert(stalls[tid].commit);
487 stalls[tid].commit = false;
491 template <class Impl>
493 DefaultDecode<Impl>::checkSignalsAndUpdate(unsigned tid)
495 // Check if there's a squash signal, squash if there is.
496 // Check stall signals, block if necessary.
497 // If status was blocked
498 // Check if stall conditions have passed
499 // if so then go to unblocking
500 // If status was Squashing
501 // check if squashing is not high. Switch to running this cycle.
503 // Update the per thread stall statuses.
504 readStallSignals(tid);
506 // Check squash signals from commit.
507 if (fromCommit->commitInfo[tid].squash) {
509 DPRINTF(Decode, "[tid:%u]: Squashing instructions due to squash "
510 "from commit.\n", tid);
517 // Check ROB squash signals from commit.
518 if (fromCommit->commitInfo[tid].robSquashing) {
519 DPRINTF(Decode, "[tid:%u]: ROB is still squashing.\n", tid);
521 // Continue to squash.
522 decodeStatus[tid] = Squashing;
527 if (checkStall(tid)) {
531 if (decodeStatus[tid] == Blocked) {
532 DPRINTF(Decode, "[tid:%u]: Done blocking, switching to unblocking.\n",
535 decodeStatus[tid] = Unblocking;
542 if (decodeStatus[tid] == Squashing) {
543 // Switch status to running if decode isn't being told to block or
544 // squash this cycle.
545 DPRINTF(Decode, "[tid:%u]: Done squashing, switching to running.\n",
548 decodeStatus[tid] = Running;
553 // If we've reached this point, we have not gotten any signals that
554 // cause decode to change its status. Decode remains the same as before.
560 DefaultDecode<Impl>::tick()
562 wroteToTimeBuffer = false;
564 bool status_change = false;
568 std::list<unsigned>::iterator threads = activeThreads->begin();
569 std::list<unsigned>::iterator end = activeThreads->end();
573 //Check stall and squash signals.
574 while (threads != end) {
575 unsigned tid = *threads++;
577 DPRINTF(Decode,"Processing [tid:%i]\n",tid);
578 status_change = checkSignalsAndUpdate(tid) || status_change;
580 decode(status_change, tid);
587 if (wroteToTimeBuffer) {
588 DPRINTF(Activity, "Activity this cycle.\n");
590 cpu->activityThisCycle();
596 DefaultDecode<Impl>::decode(bool &status_change, unsigned tid)
598 // If status is Running or idle,
599 // call decodeInsts()
600 // If status is Unblocking,
601 // buffer any instructions coming from fetch
602 // continue trying to empty skid buffer
603 // check if stall conditions have passed
605 if (decodeStatus[tid] == Blocked) {
606 ++decodeBlockedCycles;
607 } else if (decodeStatus[tid] == Squashing) {
608 ++decodeSquashCycles;
611 // Decode should try to decode as many instructions as its bandwidth
612 // will allow, as long as it is not currently blocked.
613 if (decodeStatus[tid] == Running ||
614 decodeStatus[tid] == Idle) {
615 DPRINTF(Decode, "[tid:%u]: Not blocked, so attempting to run "
619 } else if (decodeStatus[tid] == Unblocking) {
620 // Make sure that the skid buffer has something in it if the
621 // status is unblocking.
622 assert(!skidsEmpty());
624 // If the status was unblocking, then instructions from the skid
625 // buffer were used. Remove those instructions and handle
626 // the rest of unblocking.
629 if (fetchInstsValid()) {
630 // Add the current inputs to the skid buffer so they can be
631 // reprocessed when this stage unblocks.
635 status_change = unblock(tid) || status_change;
639 template <class Impl>
641 DefaultDecode<Impl>::decodeInsts(unsigned tid)
643 // Instructions can come either from the skid buffer or the list of
644 // instructions coming from fetch, depending on decode's status.
645 int insts_available = decodeStatus[tid] == Unblocking ?
646 skidBuffer[tid].size() : insts[tid].size();
648 if (insts_available == 0) {
649 DPRINTF(Decode, "[tid:%u] Nothing to do, breaking out"
651 // Should I change the status to idle?
654 } else if (decodeStatus[tid] == Unblocking) {
655 DPRINTF(Decode, "[tid:%u] Unblocking, removing insts from skid "
657 ++decodeUnblockCycles;
658 } else if (decodeStatus[tid] == Running) {
664 std::queue<DynInstPtr>
665 &insts_to_decode = decodeStatus[tid] == Unblocking ?
666 skidBuffer[tid] : insts[tid];
668 DPRINTF(Decode, "[tid:%u]: Sending instruction to rename.\n",tid);
670 while (insts_available > 0 && toRenameIndex < decodeWidth) {
671 assert(!insts_to_decode.empty());
673 inst = insts_to_decode.front();
675 insts_to_decode.pop();
677 DPRINTF(Decode, "[tid:%u]: Processing instruction [sn:%lli] with "
679 tid, inst->seqNum, inst->readPC());
681 if (inst->isSquashed()) {
682 DPRINTF(Decode, "[tid:%u]: Instruction %i with PC %#x is "
683 "squashed, skipping.\n",
684 tid, inst->seqNum, inst->readPC());
686 ++decodeSquashedInsts;
693 // Also check if instructions have no source registers. Mark
694 // them as ready to issue at any time. Not sure if this check
695 // should exist here or at a later stage; however it doesn't matter
696 // too much for function correctness.
697 if (inst->numSrcRegs() == 0) {
701 // This current instruction is valid, so add it into the decode
702 // queue. The next instruction may not be valid, so check to
703 // see if branches were predicted correctly.
704 toRename->insts[toRenameIndex] = inst;
708 ++decodeDecodedInsts;
711 // Ensure that if it was predicted as a branch, it really is a
713 if (inst->readPredTaken() && !inst->isControl()) {
714 DPRINTF(Decode, "PredPC : %#x != NextPC: %#x\n",
715 inst->readPredPC(), inst->readNextPC() + 4);
717 panic("Instruction predicted as a branch!");
719 ++decodeControlMispred;
721 // Might want to set some sort of boolean and just do
722 // a check at the end
723 squash(inst, inst->threadNumber);
728 // Go ahead and compute any PC-relative branches.
729 if (inst->isDirectCtrl() && inst->isUncondCtrl()) {
730 ++decodeBranchResolved;
732 if (inst->branchTarget() != inst->readPredPC()) {
733 ++decodeBranchMispred;
735 // Might want to set some sort of boolean and just do
736 // a check at the end
737 squash(inst, inst->threadNumber);
738 Addr target = inst->branchTarget();
739 //The micro pc after an instruction level branch should be 0
740 inst->setPredTarg(target, target + sizeof(TheISA::MachInst), 0);
746 // If we didn't process all instructions, then we will need to block
747 // and put all those instructions into the skid buffer.
748 if (!insts_to_decode.empty()) {
752 // Record that decode has written to the time buffer for activity
755 wroteToTimeBuffer = true;