rename_impl.hh (7767:bf5377d8f5c1) rename_impl.hh (7854:3c6783497976)
1/*
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved.
4 *
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.
13 *
2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Kevin Lim
29 * Korey Sewell
30 */
31
32#include <list>
33
34#include "arch/isa_traits.hh"
35#include "arch/registers.hh"
36#include "config/full_system.hh"
37#include "config/the_isa.hh"
38#include "cpu/o3/rename.hh"
39#include "params/DerivO3CPU.hh"
40
41using namespace std;
42
43template <class Impl>
44DefaultRename<Impl>::DefaultRename(O3CPU *_cpu, DerivO3CPUParams *params)
45 : cpu(_cpu),
46 iewToRenameDelay(params->iewToRenameDelay),
47 decodeToRenameDelay(params->decodeToRenameDelay),
48 commitToRenameDelay(params->commitToRenameDelay),
49 renameWidth(params->renameWidth),
50 commitWidth(params->commitWidth),
51 resumeSerialize(false),
52 resumeUnblocking(false),
53 numThreads(params->numThreads),
54 maxPhysicalRegs(params->numPhysIntRegs + params->numPhysFloatRegs)
55{
56 _status = Inactive;
57
58 for (ThreadID tid = 0; tid < numThreads; tid++) {
59 renameStatus[tid] = Idle;
60
61 freeEntries[tid].iqEntries = 0;
62 freeEntries[tid].lsqEntries = 0;
63 freeEntries[tid].robEntries = 0;
64
65 stalls[tid].iew = false;
66 stalls[tid].commit = false;
67 serializeInst[tid] = NULL;
68
69 instsInProgress[tid] = 0;
70
71 emptyROB[tid] = true;
72
73 serializeOnNextInst[tid] = false;
74 }
75
76 // @todo: Make into a parameter.
77 skidBufferMax = (2 * (iewToRenameDelay * params->decodeWidth)) + renameWidth;
78}
79
80template <class Impl>
81std::string
82DefaultRename<Impl>::name() const
83{
84 return cpu->name() + ".rename";
85}
86
87template <class Impl>
88void
89DefaultRename<Impl>::regStats()
90{
91 renameSquashCycles
92 .name(name() + ".RENAME:SquashCycles")
93 .desc("Number of cycles rename is squashing")
94 .prereq(renameSquashCycles);
95 renameIdleCycles
96 .name(name() + ".RENAME:IdleCycles")
97 .desc("Number of cycles rename is idle")
98 .prereq(renameIdleCycles);
99 renameBlockCycles
100 .name(name() + ".RENAME:BlockCycles")
101 .desc("Number of cycles rename is blocking")
102 .prereq(renameBlockCycles);
103 renameSerializeStallCycles
104 .name(name() + ".RENAME:serializeStallCycles")
105 .desc("count of cycles rename stalled for serializing inst")
106 .flags(Stats::total);
107 renameRunCycles
108 .name(name() + ".RENAME:RunCycles")
109 .desc("Number of cycles rename is running")
110 .prereq(renameIdleCycles);
111 renameUnblockCycles
112 .name(name() + ".RENAME:UnblockCycles")
113 .desc("Number of cycles rename is unblocking")
114 .prereq(renameUnblockCycles);
115 renameRenamedInsts
116 .name(name() + ".RENAME:RenamedInsts")
117 .desc("Number of instructions processed by rename")
118 .prereq(renameRenamedInsts);
119 renameSquashedInsts
120 .name(name() + ".RENAME:SquashedInsts")
121 .desc("Number of squashed instructions processed by rename")
122 .prereq(renameSquashedInsts);
123 renameROBFullEvents
124 .name(name() + ".RENAME:ROBFullEvents")
125 .desc("Number of times rename has blocked due to ROB full")
126 .prereq(renameROBFullEvents);
127 renameIQFullEvents
128 .name(name() + ".RENAME:IQFullEvents")
129 .desc("Number of times rename has blocked due to IQ full")
130 .prereq(renameIQFullEvents);
131 renameLSQFullEvents
132 .name(name() + ".RENAME:LSQFullEvents")
133 .desc("Number of times rename has blocked due to LSQ full")
134 .prereq(renameLSQFullEvents);
135 renameFullRegistersEvents
136 .name(name() + ".RENAME:FullRegisterEvents")
137 .desc("Number of times there has been no free registers")
138 .prereq(renameFullRegistersEvents);
139 renameRenamedOperands
140 .name(name() + ".RENAME:RenamedOperands")
141 .desc("Number of destination operands rename has renamed")
142 .prereq(renameRenamedOperands);
143 renameRenameLookups
144 .name(name() + ".RENAME:RenameLookups")
145 .desc("Number of register rename lookups that rename has made")
146 .prereq(renameRenameLookups);
147 renameCommittedMaps
148 .name(name() + ".RENAME:CommittedMaps")
149 .desc("Number of HB maps that are committed")
150 .prereq(renameCommittedMaps);
151 renameUndoneMaps
152 .name(name() + ".RENAME:UndoneMaps")
153 .desc("Number of HB maps that are undone due to squashing")
154 .prereq(renameUndoneMaps);
155 renamedSerializing
156 .name(name() + ".RENAME:serializingInsts")
157 .desc("count of serializing insts renamed")
158 .flags(Stats::total)
159 ;
160 renamedTempSerializing
161 .name(name() + ".RENAME:tempSerializingInsts")
162 .desc("count of temporary serializing insts renamed")
163 .flags(Stats::total)
164 ;
165 renameSkidInsts
166 .name(name() + ".RENAME:skidInsts")
167 .desc("count of insts added to the skid buffer")
168 .flags(Stats::total)
169 ;
170}
171
172template <class Impl>
173void
174DefaultRename<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
175{
176 timeBuffer = tb_ptr;
177
178 // Setup wire to read information from time buffer, from IEW stage.
179 fromIEW = timeBuffer->getWire(-iewToRenameDelay);
180
181 // Setup wire to read infromation from time buffer, from commit stage.
182 fromCommit = timeBuffer->getWire(-commitToRenameDelay);
183
184 // Setup wire to write information to previous stages.
185 toDecode = timeBuffer->getWire(0);
186}
187
188template <class Impl>
189void
190DefaultRename<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
191{
192 renameQueue = rq_ptr;
193
194 // Setup wire to write information to future stages.
195 toIEW = renameQueue->getWire(0);
196}
197
198template <class Impl>
199void
200DefaultRename<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
201{
202 decodeQueue = dq_ptr;
203
204 // Setup wire to get information from decode.
205 fromDecode = decodeQueue->getWire(-decodeToRenameDelay);
206}
207
208template <class Impl>
209void
210DefaultRename<Impl>::initStage()
211{
212 // Grab the number of free entries directly from the stages.
213 for (ThreadID tid = 0; tid < numThreads; tid++) {
214 freeEntries[tid].iqEntries = iew_ptr->instQueue.numFreeEntries(tid);
215 freeEntries[tid].lsqEntries = iew_ptr->ldstQueue.numFreeEntries(tid);
216 freeEntries[tid].robEntries = commit_ptr->numROBFreeEntries(tid);
217 emptyROB[tid] = true;
218 }
219}
220
221template<class Impl>
222void
223DefaultRename<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
224{
225 activeThreads = at_ptr;
226}
227
228
229template <class Impl>
230void
231DefaultRename<Impl>::setRenameMap(RenameMap rm_ptr[])
232{
233 for (ThreadID tid = 0; tid < numThreads; tid++)
234 renameMap[tid] = &rm_ptr[tid];
235}
236
237template <class Impl>
238void
239DefaultRename<Impl>::setFreeList(FreeList *fl_ptr)
240{
241 freeList = fl_ptr;
242}
243
244template<class Impl>
245void
246DefaultRename<Impl>::setScoreboard(Scoreboard *_scoreboard)
247{
248 scoreboard = _scoreboard;
249}
250
251template <class Impl>
252bool
253DefaultRename<Impl>::drain()
254{
255 // Rename is ready to switch out at any time.
256 cpu->signalDrained();
257 return true;
258}
259
260template <class Impl>
261void
262DefaultRename<Impl>::switchOut()
263{
264 // Clear any state, fix up the rename map.
265 for (ThreadID tid = 0; tid < numThreads; tid++) {
266 typename std::list<RenameHistory>::iterator hb_it =
267 historyBuffer[tid].begin();
268
269 while (!historyBuffer[tid].empty()) {
270 assert(hb_it != historyBuffer[tid].end());
271
272 DPRINTF(Rename, "[tid:%u]: Removing history entry with sequence "
273 "number %i.\n", tid, (*hb_it).instSeqNum);
274
275 // Tell the rename map to set the architected register to the
276 // previous physical register that it was renamed to.
277 renameMap[tid]->setEntry(hb_it->archReg, hb_it->prevPhysReg);
278
279 // Put the renamed physical register back on the free list.
280 freeList->addReg(hb_it->newPhysReg);
281
282 // Be sure to mark its register as ready if it's a misc register.
283 if (hb_it->newPhysReg >= maxPhysicalRegs) {
284 scoreboard->setReg(hb_it->newPhysReg);
285 }
286
287 historyBuffer[tid].erase(hb_it++);
288 }
289 insts[tid].clear();
290 skidBuffer[tid].clear();
291 }
292}
293
294template <class Impl>
295void
296DefaultRename<Impl>::takeOverFrom()
297{
298 _status = Inactive;
299 initStage();
300
301 // Reset all state prior to taking over from the other CPU.
302 for (ThreadID tid = 0; tid < numThreads; tid++) {
303 renameStatus[tid] = Idle;
304
305 stalls[tid].iew = false;
306 stalls[tid].commit = false;
307 serializeInst[tid] = NULL;
308
309 instsInProgress[tid] = 0;
310
311 emptyROB[tid] = true;
312
313 serializeOnNextInst[tid] = false;
314 }
315}
316
317template <class Impl>
318void
319DefaultRename<Impl>::squash(const InstSeqNum &squash_seq_num, ThreadID tid)
320{
321 DPRINTF(Rename, "[tid:%u]: Squashing instructions.\n",tid);
322
323 // Clear the stall signal if rename was blocked or unblocking before.
324 // If it still needs to block, the blocking should happen the next
325 // cycle and there should be space to hold everything due to the squash.
326 if (renameStatus[tid] == Blocked ||
327 renameStatus[tid] == Unblocking) {
328 toDecode->renameUnblock[tid] = 1;
329
330 resumeSerialize = false;
331 serializeInst[tid] = NULL;
332 } else if (renameStatus[tid] == SerializeStall) {
333 if (serializeInst[tid]->seqNum <= squash_seq_num) {
334 DPRINTF(Rename, "Rename will resume serializing after squash\n");
335 resumeSerialize = true;
336 assert(serializeInst[tid]);
337 } else {
338 resumeSerialize = false;
339 toDecode->renameUnblock[tid] = 1;
340
341 serializeInst[tid] = NULL;
342 }
343 }
344
345 // Set the status to Squashing.
346 renameStatus[tid] = Squashing;
347
348 // Squash any instructions from decode.
349 unsigned squashCount = 0;
350
351 for (int i=0; i<fromDecode->size; i++) {
352 if (fromDecode->insts[i]->threadNumber == tid &&
353 fromDecode->insts[i]->seqNum > squash_seq_num) {
354 fromDecode->insts[i]->setSquashed();
355 wroteToTimeBuffer = true;
356 squashCount++;
357 }
358
359 }
360
361 // Clear the instruction list and skid buffer in case they have any
362 // insts in them.
363 insts[tid].clear();
364
365 // Clear the skid buffer in case it has any data in it.
366 skidBuffer[tid].clear();
367
368 doSquash(squash_seq_num, tid);
369}
370
371template <class Impl>
372void
373DefaultRename<Impl>::tick()
374{
375 wroteToTimeBuffer = false;
376
377 blockThisCycle = false;
378
379 bool status_change = false;
380
381 toIEWIndex = 0;
382
383 sortInsts();
384
385 list<ThreadID>::iterator threads = activeThreads->begin();
386 list<ThreadID>::iterator end = activeThreads->end();
387
388 // Check stall and squash signals.
389 while (threads != end) {
390 ThreadID tid = *threads++;
391
392 DPRINTF(Rename, "Processing [tid:%i]\n", tid);
393
394 status_change = checkSignalsAndUpdate(tid) || status_change;
395
396 rename(status_change, tid);
397 }
398
399 if (status_change) {
400 updateStatus();
401 }
402
403 if (wroteToTimeBuffer) {
404 DPRINTF(Activity, "Activity this cycle.\n");
405 cpu->activityThisCycle();
406 }
407
408 threads = activeThreads->begin();
409
410 while (threads != end) {
411 ThreadID tid = *threads++;
412
413 // If we committed this cycle then doneSeqNum will be > 0
414 if (fromCommit->commitInfo[tid].doneSeqNum != 0 &&
415 !fromCommit->commitInfo[tid].squash &&
416 renameStatus[tid] != Squashing) {
417
418 removeFromHistory(fromCommit->commitInfo[tid].doneSeqNum,
419 tid);
420 }
421 }
422
423 // @todo: make into updateProgress function
424 for (ThreadID tid = 0; tid < numThreads; tid++) {
425 instsInProgress[tid] -= fromIEW->iewInfo[tid].dispatched;
426
427 assert(instsInProgress[tid] >=0);
428 }
429
430}
431
432template<class Impl>
433void
434DefaultRename<Impl>::rename(bool &status_change, ThreadID tid)
435{
436 // If status is Running or idle,
437 // call renameInsts()
438 // If status is Unblocking,
439 // buffer any instructions coming from decode
440 // continue trying to empty skid buffer
441 // check if stall conditions have passed
442
443 if (renameStatus[tid] == Blocked) {
444 ++renameBlockCycles;
445 } else if (renameStatus[tid] == Squashing) {
446 ++renameSquashCycles;
447 } else if (renameStatus[tid] == SerializeStall) {
448 ++renameSerializeStallCycles;
449 // If we are currently in SerializeStall and resumeSerialize
450 // was set, then that means that we are resuming serializing
451 // this cycle. Tell the previous stages to block.
452 if (resumeSerialize) {
453 resumeSerialize = false;
454 block(tid);
455 toDecode->renameUnblock[tid] = false;
456 }
457 } else if (renameStatus[tid] == Unblocking) {
458 if (resumeUnblocking) {
459 block(tid);
460 resumeUnblocking = false;
461 toDecode->renameUnblock[tid] = false;
462 }
463 }
464
465 if (renameStatus[tid] == Running ||
466 renameStatus[tid] == Idle) {
467 DPRINTF(Rename, "[tid:%u]: Not blocked, so attempting to run "
468 "stage.\n", tid);
469
470 renameInsts(tid);
471 } else if (renameStatus[tid] == Unblocking) {
472 renameInsts(tid);
473
474 if (validInsts()) {
475 // Add the current inputs to the skid buffer so they can be
476 // reprocessed when this stage unblocks.
477 skidInsert(tid);
478 }
479
480 // If we switched over to blocking, then there's a potential for
481 // an overall status change.
482 status_change = unblock(tid) || status_change || blockThisCycle;
483 }
484}
485
486template <class Impl>
487void
488DefaultRename<Impl>::renameInsts(ThreadID tid)
489{
490 // Instructions can be either in the skid buffer or the queue of
491 // instructions coming from decode, depending on the status.
492 int insts_available = renameStatus[tid] == Unblocking ?
493 skidBuffer[tid].size() : insts[tid].size();
494
495 // Check the decode queue to see if instructions are available.
496 // If there are no available instructions to rename, then do nothing.
497 if (insts_available == 0) {
498 DPRINTF(Rename, "[tid:%u]: Nothing to do, breaking out early.\n",
499 tid);
500 // Should I change status to idle?
501 ++renameIdleCycles;
502 return;
503 } else if (renameStatus[tid] == Unblocking) {
504 ++renameUnblockCycles;
505 } else if (renameStatus[tid] == Running) {
506 ++renameRunCycles;
507 }
508
509 DynInstPtr inst;
510
511 // Will have to do a different calculation for the number of free
512 // entries.
513 int free_rob_entries = calcFreeROBEntries(tid);
514 int free_iq_entries = calcFreeIQEntries(tid);
515 int free_lsq_entries = calcFreeLSQEntries(tid);
516 int min_free_entries = free_rob_entries;
517
518 FullSource source = ROB;
519
520 if (free_iq_entries < min_free_entries) {
521 min_free_entries = free_iq_entries;
522 source = IQ;
523 }
524
525 if (free_lsq_entries < min_free_entries) {
526 min_free_entries = free_lsq_entries;
527 source = LSQ;
528 }
529
530 // Check if there's any space left.
531 if (min_free_entries <= 0) {
532 DPRINTF(Rename, "[tid:%u]: Blocking due to no free ROB/IQ/LSQ "
533 "entries.\n"
534 "ROB has %i free entries.\n"
535 "IQ has %i free entries.\n"
536 "LSQ has %i free entries.\n",
537 tid,
538 free_rob_entries,
539 free_iq_entries,
540 free_lsq_entries);
541
542 blockThisCycle = true;
543
544 block(tid);
545
546 incrFullStat(source);
547
548 return;
549 } else if (min_free_entries < insts_available) {
550 DPRINTF(Rename, "[tid:%u]: Will have to block this cycle."
551 "%i insts available, but only %i insts can be "
552 "renamed due to ROB/IQ/LSQ limits.\n",
553 tid, insts_available, min_free_entries);
554
555 insts_available = min_free_entries;
556
557 blockThisCycle = true;
558
559 incrFullStat(source);
560 }
561
562 InstQueue &insts_to_rename = renameStatus[tid] == Unblocking ?
563 skidBuffer[tid] : insts[tid];
564
565 DPRINTF(Rename, "[tid:%u]: %i available instructions to "
566 "send iew.\n", tid, insts_available);
567
568 DPRINTF(Rename, "[tid:%u]: %i insts pipelining from Rename | %i insts "
569 "dispatched to IQ last cycle.\n",
570 tid, instsInProgress[tid], fromIEW->iewInfo[tid].dispatched);
571
572 // Handle serializing the next instruction if necessary.
573 if (serializeOnNextInst[tid]) {
574 if (emptyROB[tid] && instsInProgress[tid] == 0) {
575 // ROB already empty; no need to serialize.
576 serializeOnNextInst[tid] = false;
577 } else if (!insts_to_rename.empty()) {
578 insts_to_rename.front()->setSerializeBefore();
579 }
580 }
581
582 int renamed_insts = 0;
583
584 while (insts_available > 0 && toIEWIndex < renameWidth) {
585 DPRINTF(Rename, "[tid:%u]: Sending instructions to IEW.\n", tid);
586
587 assert(!insts_to_rename.empty());
588
589 inst = insts_to_rename.front();
590
591 insts_to_rename.pop_front();
592
593 if (renameStatus[tid] == Unblocking) {
594 DPRINTF(Rename,"[tid:%u]: Removing [sn:%lli] PC:%s from rename "
595 "skidBuffer\n", tid, inst->seqNum, inst->pcState());
596 }
597
598 if (inst->isSquashed()) {
599 DPRINTF(Rename, "[tid:%u]: instruction %i with PC %s is "
600 "squashed, skipping.\n", tid, inst->seqNum,
601 inst->pcState());
602
603 ++renameSquashedInsts;
604
605 // Decrement how many instructions are available.
606 --insts_available;
607
608 continue;
609 }
610
611 DPRINTF(Rename, "[tid:%u]: Processing instruction [sn:%lli] with "
612 "PC %s.\n", tid, inst->seqNum, inst->pcState());
613
614 // Handle serializeAfter/serializeBefore instructions.
615 // serializeAfter marks the next instruction as serializeBefore.
616 // serializeBefore makes the instruction wait in rename until the ROB
617 // is empty.
618
619 // In this model, IPR accesses are serialize before
620 // instructions, and store conditionals are serialize after
621 // instructions. This is mainly due to lack of support for
622 // out-of-order operations of either of those classes of
623 // instructions.
624 if ((inst->isIprAccess() || inst->isSerializeBefore()) &&
625 !inst->isSerializeHandled()) {
626 DPRINTF(Rename, "Serialize before instruction encountered.\n");
627
628 if (!inst->isTempSerializeBefore()) {
629 renamedSerializing++;
630 inst->setSerializeHandled();
631 } else {
632 renamedTempSerializing++;
633 }
634
635 // Change status over to SerializeStall so that other stages know
636 // what this is blocked on.
637 renameStatus[tid] = SerializeStall;
638
639 serializeInst[tid] = inst;
640
641 blockThisCycle = true;
642
643 break;
644 } else if ((inst->isStoreConditional() || inst->isSerializeAfter()) &&
645 !inst->isSerializeHandled()) {
646 DPRINTF(Rename, "Serialize after instruction encountered.\n");
647
648 renamedSerializing++;
649
650 inst->setSerializeHandled();
651
652 serializeAfter(insts_to_rename, tid);
653 }
654
655 // Check here to make sure there are enough destination registers
656 // to rename to. Otherwise block.
657 if (renameMap[tid]->numFreeEntries() < inst->numDestRegs()) {
658 DPRINTF(Rename, "Blocking due to lack of free "
659 "physical registers to rename to.\n");
660 blockThisCycle = true;
661 insts_to_rename.push_front(inst);
662 ++renameFullRegistersEvents;
663
664 break;
665 }
666
667 renameSrcRegs(inst, inst->threadNumber);
668
669 renameDestRegs(inst, inst->threadNumber);
670
671 ++renamed_insts;
672
673 // Put instruction in rename queue.
674 toIEW->insts[toIEWIndex] = inst;
675 ++(toIEW->size);
676
677 // Increment which instruction we're on.
678 ++toIEWIndex;
679
680 // Decrement how many instructions are available.
681 --insts_available;
682 }
683
684 instsInProgress[tid] += renamed_insts;
685 renameRenamedInsts += renamed_insts;
686
687 // If we wrote to the time buffer, record this.
688 if (toIEWIndex) {
689 wroteToTimeBuffer = true;
690 }
691
692 // Check if there's any instructions left that haven't yet been renamed.
693 // If so then block.
694 if (insts_available) {
695 blockThisCycle = true;
696 }
697
698 if (blockThisCycle) {
699 block(tid);
700 toDecode->renameUnblock[tid] = false;
701 }
702}
703
704template<class Impl>
705void
706DefaultRename<Impl>::skidInsert(ThreadID tid)
707{
708 DynInstPtr inst = NULL;
709
710 while (!insts[tid].empty()) {
711 inst = insts[tid].front();
712
713 insts[tid].pop_front();
714
715 assert(tid == inst->threadNumber);
716
717 DPRINTF(Rename, "[tid:%u]: Inserting [sn:%lli] PC: %s into Rename "
718 "skidBuffer\n", tid, inst->seqNum, inst->pcState());
719
720 ++renameSkidInsts;
721
722 skidBuffer[tid].push_back(inst);
723 }
724
725 if (skidBuffer[tid].size() > skidBufferMax)
726 {
727 typename InstQueue::iterator it;
728 warn("Skidbuffer contents:\n");
729 for(it = skidBuffer[tid].begin(); it != skidBuffer[tid].end(); it++)
730 {
731 warn("[tid:%u]: %s [sn:%i].\n", tid,
732 (*it)->staticInst->disassemble(inst->instAddr()),
733 (*it)->seqNum);
734 }
735 panic("Skidbuffer Exceeded Max Size");
736 }
737}
738
739template <class Impl>
740void
741DefaultRename<Impl>::sortInsts()
742{
743 int insts_from_decode = fromDecode->size;
744#ifdef DEBUG
745 for (ThreadID tid = 0; tid < numThreads; tid++)
746 assert(insts[tid].empty());
747#endif
748 for (int i = 0; i < insts_from_decode; ++i) {
749 DynInstPtr inst = fromDecode->insts[i];
750 insts[inst->threadNumber].push_back(inst);
751 }
752}
753
754template<class Impl>
755bool
756DefaultRename<Impl>::skidsEmpty()
757{
758 list<ThreadID>::iterator threads = activeThreads->begin();
759 list<ThreadID>::iterator end = activeThreads->end();
760
761 while (threads != end) {
762 ThreadID tid = *threads++;
763
764 if (!skidBuffer[tid].empty())
765 return false;
766 }
767
768 return true;
769}
770
771template<class Impl>
772void
773DefaultRename<Impl>::updateStatus()
774{
775 bool any_unblocking = false;
776
777 list<ThreadID>::iterator threads = activeThreads->begin();
778 list<ThreadID>::iterator end = activeThreads->end();
779
780 while (threads != end) {
781 ThreadID tid = *threads++;
782
783 if (renameStatus[tid] == Unblocking) {
784 any_unblocking = true;
785 break;
786 }
787 }
788
789 // Rename will have activity if it's unblocking.
790 if (any_unblocking) {
791 if (_status == Inactive) {
792 _status = Active;
793
794 DPRINTF(Activity, "Activating stage.\n");
795
796 cpu->activateStage(O3CPU::RenameIdx);
797 }
798 } else {
799 // If it's not unblocking, then rename will not have any internal
800 // activity. Switch it to inactive.
801 if (_status == Active) {
802 _status = Inactive;
803 DPRINTF(Activity, "Deactivating stage.\n");
804
805 cpu->deactivateStage(O3CPU::RenameIdx);
806 }
807 }
808}
809
810template <class Impl>
811bool
812DefaultRename<Impl>::block(ThreadID tid)
813{
814 DPRINTF(Rename, "[tid:%u]: Blocking.\n", tid);
815
816 // Add the current inputs onto the skid buffer, so they can be
817 // reprocessed when this stage unblocks.
818 skidInsert(tid);
819
820 // Only signal backwards to block if the previous stages do not think
821 // rename is already blocked.
822 if (renameStatus[tid] != Blocked) {
823 // If resumeUnblocking is set, we unblocked during the squash,
824 // but now we're have unblocking status. We need to tell earlier
825 // stages to block.
826 if (resumeUnblocking || renameStatus[tid] != Unblocking) {
827 toDecode->renameBlock[tid] = true;
828 toDecode->renameUnblock[tid] = false;
829 wroteToTimeBuffer = true;
830 }
831
832 // Rename can not go from SerializeStall to Blocked, otherwise
833 // it would not know to complete the serialize stall.
834 if (renameStatus[tid] != SerializeStall) {
835 // Set status to Blocked.
836 renameStatus[tid] = Blocked;
837 return true;
838 }
839 }
840
841 return false;
842}
843
844template <class Impl>
845bool
846DefaultRename<Impl>::unblock(ThreadID tid)
847{
848 DPRINTF(Rename, "[tid:%u]: Trying to unblock.\n", tid);
849
850 // Rename is done unblocking if the skid buffer is empty.
851 if (skidBuffer[tid].empty() && renameStatus[tid] != SerializeStall) {
852
853 DPRINTF(Rename, "[tid:%u]: Done unblocking.\n", tid);
854
855 toDecode->renameUnblock[tid] = true;
856 wroteToTimeBuffer = true;
857
858 renameStatus[tid] = Running;
859 return true;
860 }
861
862 return false;
863}
864
865template <class Impl>
866void
867DefaultRename<Impl>::doSquash(const InstSeqNum &squashed_seq_num, ThreadID tid)
868{
869 typename std::list<RenameHistory>::iterator hb_it =
870 historyBuffer[tid].begin();
871
872 // After a syscall squashes everything, the history buffer may be empty
873 // but the ROB may still be squashing instructions.
874 if (historyBuffer[tid].empty()) {
875 return;
876 }
877
878 // Go through the most recent instructions, undoing the mappings
879 // they did and freeing up the registers.
880 while (!historyBuffer[tid].empty() &&
881 (*hb_it).instSeqNum > squashed_seq_num) {
882 assert(hb_it != historyBuffer[tid].end());
883
884 DPRINTF(Rename, "[tid:%u]: Removing history entry with sequence "
885 "number %i.\n", tid, (*hb_it).instSeqNum);
886
887 // Tell the rename map to set the architected register to the
888 // previous physical register that it was renamed to.
889 renameMap[tid]->setEntry(hb_it->archReg, hb_it->prevPhysReg);
890
891 // Put the renamed physical register back on the free list.
892 freeList->addReg(hb_it->newPhysReg);
893
894 // Be sure to mark its register as ready if it's a misc register.
895 if (hb_it->newPhysReg >= maxPhysicalRegs) {
896 scoreboard->setReg(hb_it->newPhysReg);
897 }
898
899 historyBuffer[tid].erase(hb_it++);
900
901 ++renameUndoneMaps;
902 }
903}
904
905template<class Impl>
906void
907DefaultRename<Impl>::removeFromHistory(InstSeqNum inst_seq_num, ThreadID tid)
908{
909 DPRINTF(Rename, "[tid:%u]: Removing a committed instruction from the "
910 "history buffer %u (size=%i), until [sn:%lli].\n",
911 tid, tid, historyBuffer[tid].size(), inst_seq_num);
912
913 typename std::list<RenameHistory>::iterator hb_it =
914 historyBuffer[tid].end();
915
916 --hb_it;
917
918 if (historyBuffer[tid].empty()) {
919 DPRINTF(Rename, "[tid:%u]: History buffer is empty.\n", tid);
920 return;
921 } else if (hb_it->instSeqNum > inst_seq_num) {
922 DPRINTF(Rename, "[tid:%u]: Old sequence number encountered. Ensure "
923 "that a syscall happened recently.\n", tid);
924 return;
925 }
926
927 // Commit all the renames up until (and including) the committed sequence
928 // number. Some or even all of the committed instructions may not have
929 // rename histories if they did not have destination registers that were
930 // renamed.
931 while (!historyBuffer[tid].empty() &&
932 hb_it != historyBuffer[tid].end() &&
933 (*hb_it).instSeqNum <= inst_seq_num) {
934
935 DPRINTF(Rename, "[tid:%u]: Freeing up older rename of reg %i, "
936 "[sn:%lli].\n",
937 tid, (*hb_it).prevPhysReg, (*hb_it).instSeqNum);
938
939 freeList->addReg((*hb_it).prevPhysReg);
940 ++renameCommittedMaps;
941
942 historyBuffer[tid].erase(hb_it--);
943 }
944}
945
946template <class Impl>
947inline void
948DefaultRename<Impl>::renameSrcRegs(DynInstPtr &inst, ThreadID tid)
949{
950 assert(renameMap[tid] != 0);
951
952 unsigned num_src_regs = inst->numSrcRegs();
953
954 // Get the architectual register numbers from the source and
955 // destination operands, and redirect them to the right register.
956 // Will need to mark dependencies though.
957 for (int src_idx = 0; src_idx < num_src_regs; src_idx++) {
958 RegIndex src_reg = inst->srcRegIdx(src_idx);
959 RegIndex flat_src_reg = src_reg;
960 if (src_reg < TheISA::FP_Base_DepTag) {
961 flat_src_reg = inst->tcBase()->flattenIntIndex(src_reg);
962 DPRINTF(Rename, "Flattening index %d to %d.\n",
963 (int)src_reg, (int)flat_src_reg);
964 } else if (src_reg < TheISA::Ctrl_Base_DepTag) {
965 src_reg = src_reg - TheISA::FP_Base_DepTag;
966 flat_src_reg = inst->tcBase()->flattenFloatIndex(src_reg);
967 DPRINTF(Rename, "Flattening index %d to %d.\n",
968 (int)src_reg, (int)flat_src_reg);
969 flat_src_reg += TheISA::NumIntRegs;
970 } else if (src_reg < TheISA::Max_DepTag) {
971 flat_src_reg = src_reg - TheISA::Ctrl_Base_DepTag +
972 TheISA::NumFloatRegs + TheISA::NumIntRegs;
973 DPRINTF(Rename, "Adjusting reg index from %d to %d.\n",
974 src_reg, flat_src_reg);
975 } else {
976 panic("Reg index is out of bound: %d.", src_reg);
977 }
978
979 inst->flattenSrcReg(src_idx, flat_src_reg);
980
981 // Look up the source registers to get the phys. register they've
982 // been renamed to, and set the sources to those registers.
983 PhysRegIndex renamed_reg = renameMap[tid]->lookup(flat_src_reg);
984
985 DPRINTF(Rename, "[tid:%u]: Looking up arch reg %i, got "
986 "physical reg %i.\n", tid, (int)flat_src_reg,
987 (int)renamed_reg);
988
989 inst->renameSrcReg(src_idx, renamed_reg);
990
991 // See if the register is ready or not.
992 if (scoreboard->getReg(renamed_reg) == true) {
993 DPRINTF(Rename, "[tid:%u]: Register %d is ready.\n",
994 tid, renamed_reg);
995
996 inst->markSrcRegReady(src_idx);
997 } else {
998 DPRINTF(Rename, "[tid:%u]: Register %d is not ready.\n",
999 tid, renamed_reg);
1000 }
1001
1002 ++renameRenameLookups;
1003 }
1004}
1005
1006template <class Impl>
1007inline void
1008DefaultRename<Impl>::renameDestRegs(DynInstPtr &inst, ThreadID tid)
1009{
1010 typename RenameMap::RenameInfo rename_result;
1011
1012 unsigned num_dest_regs = inst->numDestRegs();
1013
1014 // Rename the destination registers.
1015 for (int dest_idx = 0; dest_idx < num_dest_regs; dest_idx++) {
1016 RegIndex dest_reg = inst->destRegIdx(dest_idx);
1017 RegIndex flat_dest_reg = dest_reg;
1018 if (dest_reg < TheISA::FP_Base_DepTag) {
1019 // Integer registers are flattened.
1020 flat_dest_reg = inst->tcBase()->flattenIntIndex(dest_reg);
1021 DPRINTF(Rename, "Flattening index %d to %d.\n",
1022 (int)dest_reg, (int)flat_dest_reg);
1023 } else if (dest_reg < TheISA::Ctrl_Base_DepTag) {
1024 dest_reg = dest_reg - TheISA::FP_Base_DepTag;
1025 flat_dest_reg = inst->tcBase()->flattenFloatIndex(dest_reg);
1026 DPRINTF(Rename, "Flattening index %d to %d.\n",
1027 (int)dest_reg, (int)flat_dest_reg);
1028 flat_dest_reg += TheISA::NumIntRegs;
1029 } else if (dest_reg < TheISA::Max_DepTag) {
1030 // Floating point and Miscellaneous registers need their indexes
1031 // adjusted to account for the expanded number of flattened int regs.
1032 flat_dest_reg = dest_reg - TheISA::Ctrl_Base_DepTag +
1033 TheISA::NumIntRegs + TheISA::NumFloatRegs;
1034 DPRINTF(Rename, "Adjusting reg index from %d to %d.\n",
1035 dest_reg, flat_dest_reg);
1036 } else {
1037 panic("Reg index is out of bound: %d.", dest_reg);
1038 }
1039
1040 inst->flattenDestReg(dest_idx, flat_dest_reg);
1041
1042 // Get the physical register that the destination will be
1043 // renamed to.
1044 rename_result = renameMap[tid]->rename(flat_dest_reg);
1045
1046 //Mark Scoreboard entry as not ready
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Kevin Lim
41 * Korey Sewell
42 */
43
44#include <list>
45
46#include "arch/isa_traits.hh"
47#include "arch/registers.hh"
48#include "config/full_system.hh"
49#include "config/the_isa.hh"
50#include "cpu/o3/rename.hh"
51#include "params/DerivO3CPU.hh"
52
53using namespace std;
54
55template <class Impl>
56DefaultRename<Impl>::DefaultRename(O3CPU *_cpu, DerivO3CPUParams *params)
57 : cpu(_cpu),
58 iewToRenameDelay(params->iewToRenameDelay),
59 decodeToRenameDelay(params->decodeToRenameDelay),
60 commitToRenameDelay(params->commitToRenameDelay),
61 renameWidth(params->renameWidth),
62 commitWidth(params->commitWidth),
63 resumeSerialize(false),
64 resumeUnblocking(false),
65 numThreads(params->numThreads),
66 maxPhysicalRegs(params->numPhysIntRegs + params->numPhysFloatRegs)
67{
68 _status = Inactive;
69
70 for (ThreadID tid = 0; tid < numThreads; tid++) {
71 renameStatus[tid] = Idle;
72
73 freeEntries[tid].iqEntries = 0;
74 freeEntries[tid].lsqEntries = 0;
75 freeEntries[tid].robEntries = 0;
76
77 stalls[tid].iew = false;
78 stalls[tid].commit = false;
79 serializeInst[tid] = NULL;
80
81 instsInProgress[tid] = 0;
82
83 emptyROB[tid] = true;
84
85 serializeOnNextInst[tid] = false;
86 }
87
88 // @todo: Make into a parameter.
89 skidBufferMax = (2 * (iewToRenameDelay * params->decodeWidth)) + renameWidth;
90}
91
92template <class Impl>
93std::string
94DefaultRename<Impl>::name() const
95{
96 return cpu->name() + ".rename";
97}
98
99template <class Impl>
100void
101DefaultRename<Impl>::regStats()
102{
103 renameSquashCycles
104 .name(name() + ".RENAME:SquashCycles")
105 .desc("Number of cycles rename is squashing")
106 .prereq(renameSquashCycles);
107 renameIdleCycles
108 .name(name() + ".RENAME:IdleCycles")
109 .desc("Number of cycles rename is idle")
110 .prereq(renameIdleCycles);
111 renameBlockCycles
112 .name(name() + ".RENAME:BlockCycles")
113 .desc("Number of cycles rename is blocking")
114 .prereq(renameBlockCycles);
115 renameSerializeStallCycles
116 .name(name() + ".RENAME:serializeStallCycles")
117 .desc("count of cycles rename stalled for serializing inst")
118 .flags(Stats::total);
119 renameRunCycles
120 .name(name() + ".RENAME:RunCycles")
121 .desc("Number of cycles rename is running")
122 .prereq(renameIdleCycles);
123 renameUnblockCycles
124 .name(name() + ".RENAME:UnblockCycles")
125 .desc("Number of cycles rename is unblocking")
126 .prereq(renameUnblockCycles);
127 renameRenamedInsts
128 .name(name() + ".RENAME:RenamedInsts")
129 .desc("Number of instructions processed by rename")
130 .prereq(renameRenamedInsts);
131 renameSquashedInsts
132 .name(name() + ".RENAME:SquashedInsts")
133 .desc("Number of squashed instructions processed by rename")
134 .prereq(renameSquashedInsts);
135 renameROBFullEvents
136 .name(name() + ".RENAME:ROBFullEvents")
137 .desc("Number of times rename has blocked due to ROB full")
138 .prereq(renameROBFullEvents);
139 renameIQFullEvents
140 .name(name() + ".RENAME:IQFullEvents")
141 .desc("Number of times rename has blocked due to IQ full")
142 .prereq(renameIQFullEvents);
143 renameLSQFullEvents
144 .name(name() + ".RENAME:LSQFullEvents")
145 .desc("Number of times rename has blocked due to LSQ full")
146 .prereq(renameLSQFullEvents);
147 renameFullRegistersEvents
148 .name(name() + ".RENAME:FullRegisterEvents")
149 .desc("Number of times there has been no free registers")
150 .prereq(renameFullRegistersEvents);
151 renameRenamedOperands
152 .name(name() + ".RENAME:RenamedOperands")
153 .desc("Number of destination operands rename has renamed")
154 .prereq(renameRenamedOperands);
155 renameRenameLookups
156 .name(name() + ".RENAME:RenameLookups")
157 .desc("Number of register rename lookups that rename has made")
158 .prereq(renameRenameLookups);
159 renameCommittedMaps
160 .name(name() + ".RENAME:CommittedMaps")
161 .desc("Number of HB maps that are committed")
162 .prereq(renameCommittedMaps);
163 renameUndoneMaps
164 .name(name() + ".RENAME:UndoneMaps")
165 .desc("Number of HB maps that are undone due to squashing")
166 .prereq(renameUndoneMaps);
167 renamedSerializing
168 .name(name() + ".RENAME:serializingInsts")
169 .desc("count of serializing insts renamed")
170 .flags(Stats::total)
171 ;
172 renamedTempSerializing
173 .name(name() + ".RENAME:tempSerializingInsts")
174 .desc("count of temporary serializing insts renamed")
175 .flags(Stats::total)
176 ;
177 renameSkidInsts
178 .name(name() + ".RENAME:skidInsts")
179 .desc("count of insts added to the skid buffer")
180 .flags(Stats::total)
181 ;
182}
183
184template <class Impl>
185void
186DefaultRename<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
187{
188 timeBuffer = tb_ptr;
189
190 // Setup wire to read information from time buffer, from IEW stage.
191 fromIEW = timeBuffer->getWire(-iewToRenameDelay);
192
193 // Setup wire to read infromation from time buffer, from commit stage.
194 fromCommit = timeBuffer->getWire(-commitToRenameDelay);
195
196 // Setup wire to write information to previous stages.
197 toDecode = timeBuffer->getWire(0);
198}
199
200template <class Impl>
201void
202DefaultRename<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
203{
204 renameQueue = rq_ptr;
205
206 // Setup wire to write information to future stages.
207 toIEW = renameQueue->getWire(0);
208}
209
210template <class Impl>
211void
212DefaultRename<Impl>::setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr)
213{
214 decodeQueue = dq_ptr;
215
216 // Setup wire to get information from decode.
217 fromDecode = decodeQueue->getWire(-decodeToRenameDelay);
218}
219
220template <class Impl>
221void
222DefaultRename<Impl>::initStage()
223{
224 // Grab the number of free entries directly from the stages.
225 for (ThreadID tid = 0; tid < numThreads; tid++) {
226 freeEntries[tid].iqEntries = iew_ptr->instQueue.numFreeEntries(tid);
227 freeEntries[tid].lsqEntries = iew_ptr->ldstQueue.numFreeEntries(tid);
228 freeEntries[tid].robEntries = commit_ptr->numROBFreeEntries(tid);
229 emptyROB[tid] = true;
230 }
231}
232
233template<class Impl>
234void
235DefaultRename<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
236{
237 activeThreads = at_ptr;
238}
239
240
241template <class Impl>
242void
243DefaultRename<Impl>::setRenameMap(RenameMap rm_ptr[])
244{
245 for (ThreadID tid = 0; tid < numThreads; tid++)
246 renameMap[tid] = &rm_ptr[tid];
247}
248
249template <class Impl>
250void
251DefaultRename<Impl>::setFreeList(FreeList *fl_ptr)
252{
253 freeList = fl_ptr;
254}
255
256template<class Impl>
257void
258DefaultRename<Impl>::setScoreboard(Scoreboard *_scoreboard)
259{
260 scoreboard = _scoreboard;
261}
262
263template <class Impl>
264bool
265DefaultRename<Impl>::drain()
266{
267 // Rename is ready to switch out at any time.
268 cpu->signalDrained();
269 return true;
270}
271
272template <class Impl>
273void
274DefaultRename<Impl>::switchOut()
275{
276 // Clear any state, fix up the rename map.
277 for (ThreadID tid = 0; tid < numThreads; tid++) {
278 typename std::list<RenameHistory>::iterator hb_it =
279 historyBuffer[tid].begin();
280
281 while (!historyBuffer[tid].empty()) {
282 assert(hb_it != historyBuffer[tid].end());
283
284 DPRINTF(Rename, "[tid:%u]: Removing history entry with sequence "
285 "number %i.\n", tid, (*hb_it).instSeqNum);
286
287 // Tell the rename map to set the architected register to the
288 // previous physical register that it was renamed to.
289 renameMap[tid]->setEntry(hb_it->archReg, hb_it->prevPhysReg);
290
291 // Put the renamed physical register back on the free list.
292 freeList->addReg(hb_it->newPhysReg);
293
294 // Be sure to mark its register as ready if it's a misc register.
295 if (hb_it->newPhysReg >= maxPhysicalRegs) {
296 scoreboard->setReg(hb_it->newPhysReg);
297 }
298
299 historyBuffer[tid].erase(hb_it++);
300 }
301 insts[tid].clear();
302 skidBuffer[tid].clear();
303 }
304}
305
306template <class Impl>
307void
308DefaultRename<Impl>::takeOverFrom()
309{
310 _status = Inactive;
311 initStage();
312
313 // Reset all state prior to taking over from the other CPU.
314 for (ThreadID tid = 0; tid < numThreads; tid++) {
315 renameStatus[tid] = Idle;
316
317 stalls[tid].iew = false;
318 stalls[tid].commit = false;
319 serializeInst[tid] = NULL;
320
321 instsInProgress[tid] = 0;
322
323 emptyROB[tid] = true;
324
325 serializeOnNextInst[tid] = false;
326 }
327}
328
329template <class Impl>
330void
331DefaultRename<Impl>::squash(const InstSeqNum &squash_seq_num, ThreadID tid)
332{
333 DPRINTF(Rename, "[tid:%u]: Squashing instructions.\n",tid);
334
335 // Clear the stall signal if rename was blocked or unblocking before.
336 // If it still needs to block, the blocking should happen the next
337 // cycle and there should be space to hold everything due to the squash.
338 if (renameStatus[tid] == Blocked ||
339 renameStatus[tid] == Unblocking) {
340 toDecode->renameUnblock[tid] = 1;
341
342 resumeSerialize = false;
343 serializeInst[tid] = NULL;
344 } else if (renameStatus[tid] == SerializeStall) {
345 if (serializeInst[tid]->seqNum <= squash_seq_num) {
346 DPRINTF(Rename, "Rename will resume serializing after squash\n");
347 resumeSerialize = true;
348 assert(serializeInst[tid]);
349 } else {
350 resumeSerialize = false;
351 toDecode->renameUnblock[tid] = 1;
352
353 serializeInst[tid] = NULL;
354 }
355 }
356
357 // Set the status to Squashing.
358 renameStatus[tid] = Squashing;
359
360 // Squash any instructions from decode.
361 unsigned squashCount = 0;
362
363 for (int i=0; i<fromDecode->size; i++) {
364 if (fromDecode->insts[i]->threadNumber == tid &&
365 fromDecode->insts[i]->seqNum > squash_seq_num) {
366 fromDecode->insts[i]->setSquashed();
367 wroteToTimeBuffer = true;
368 squashCount++;
369 }
370
371 }
372
373 // Clear the instruction list and skid buffer in case they have any
374 // insts in them.
375 insts[tid].clear();
376
377 // Clear the skid buffer in case it has any data in it.
378 skidBuffer[tid].clear();
379
380 doSquash(squash_seq_num, tid);
381}
382
383template <class Impl>
384void
385DefaultRename<Impl>::tick()
386{
387 wroteToTimeBuffer = false;
388
389 blockThisCycle = false;
390
391 bool status_change = false;
392
393 toIEWIndex = 0;
394
395 sortInsts();
396
397 list<ThreadID>::iterator threads = activeThreads->begin();
398 list<ThreadID>::iterator end = activeThreads->end();
399
400 // Check stall and squash signals.
401 while (threads != end) {
402 ThreadID tid = *threads++;
403
404 DPRINTF(Rename, "Processing [tid:%i]\n", tid);
405
406 status_change = checkSignalsAndUpdate(tid) || status_change;
407
408 rename(status_change, tid);
409 }
410
411 if (status_change) {
412 updateStatus();
413 }
414
415 if (wroteToTimeBuffer) {
416 DPRINTF(Activity, "Activity this cycle.\n");
417 cpu->activityThisCycle();
418 }
419
420 threads = activeThreads->begin();
421
422 while (threads != end) {
423 ThreadID tid = *threads++;
424
425 // If we committed this cycle then doneSeqNum will be > 0
426 if (fromCommit->commitInfo[tid].doneSeqNum != 0 &&
427 !fromCommit->commitInfo[tid].squash &&
428 renameStatus[tid] != Squashing) {
429
430 removeFromHistory(fromCommit->commitInfo[tid].doneSeqNum,
431 tid);
432 }
433 }
434
435 // @todo: make into updateProgress function
436 for (ThreadID tid = 0; tid < numThreads; tid++) {
437 instsInProgress[tid] -= fromIEW->iewInfo[tid].dispatched;
438
439 assert(instsInProgress[tid] >=0);
440 }
441
442}
443
444template<class Impl>
445void
446DefaultRename<Impl>::rename(bool &status_change, ThreadID tid)
447{
448 // If status is Running or idle,
449 // call renameInsts()
450 // If status is Unblocking,
451 // buffer any instructions coming from decode
452 // continue trying to empty skid buffer
453 // check if stall conditions have passed
454
455 if (renameStatus[tid] == Blocked) {
456 ++renameBlockCycles;
457 } else if (renameStatus[tid] == Squashing) {
458 ++renameSquashCycles;
459 } else if (renameStatus[tid] == SerializeStall) {
460 ++renameSerializeStallCycles;
461 // If we are currently in SerializeStall and resumeSerialize
462 // was set, then that means that we are resuming serializing
463 // this cycle. Tell the previous stages to block.
464 if (resumeSerialize) {
465 resumeSerialize = false;
466 block(tid);
467 toDecode->renameUnblock[tid] = false;
468 }
469 } else if (renameStatus[tid] == Unblocking) {
470 if (resumeUnblocking) {
471 block(tid);
472 resumeUnblocking = false;
473 toDecode->renameUnblock[tid] = false;
474 }
475 }
476
477 if (renameStatus[tid] == Running ||
478 renameStatus[tid] == Idle) {
479 DPRINTF(Rename, "[tid:%u]: Not blocked, so attempting to run "
480 "stage.\n", tid);
481
482 renameInsts(tid);
483 } else if (renameStatus[tid] == Unblocking) {
484 renameInsts(tid);
485
486 if (validInsts()) {
487 // Add the current inputs to the skid buffer so they can be
488 // reprocessed when this stage unblocks.
489 skidInsert(tid);
490 }
491
492 // If we switched over to blocking, then there's a potential for
493 // an overall status change.
494 status_change = unblock(tid) || status_change || blockThisCycle;
495 }
496}
497
498template <class Impl>
499void
500DefaultRename<Impl>::renameInsts(ThreadID tid)
501{
502 // Instructions can be either in the skid buffer or the queue of
503 // instructions coming from decode, depending on the status.
504 int insts_available = renameStatus[tid] == Unblocking ?
505 skidBuffer[tid].size() : insts[tid].size();
506
507 // Check the decode queue to see if instructions are available.
508 // If there are no available instructions to rename, then do nothing.
509 if (insts_available == 0) {
510 DPRINTF(Rename, "[tid:%u]: Nothing to do, breaking out early.\n",
511 tid);
512 // Should I change status to idle?
513 ++renameIdleCycles;
514 return;
515 } else if (renameStatus[tid] == Unblocking) {
516 ++renameUnblockCycles;
517 } else if (renameStatus[tid] == Running) {
518 ++renameRunCycles;
519 }
520
521 DynInstPtr inst;
522
523 // Will have to do a different calculation for the number of free
524 // entries.
525 int free_rob_entries = calcFreeROBEntries(tid);
526 int free_iq_entries = calcFreeIQEntries(tid);
527 int free_lsq_entries = calcFreeLSQEntries(tid);
528 int min_free_entries = free_rob_entries;
529
530 FullSource source = ROB;
531
532 if (free_iq_entries < min_free_entries) {
533 min_free_entries = free_iq_entries;
534 source = IQ;
535 }
536
537 if (free_lsq_entries < min_free_entries) {
538 min_free_entries = free_lsq_entries;
539 source = LSQ;
540 }
541
542 // Check if there's any space left.
543 if (min_free_entries <= 0) {
544 DPRINTF(Rename, "[tid:%u]: Blocking due to no free ROB/IQ/LSQ "
545 "entries.\n"
546 "ROB has %i free entries.\n"
547 "IQ has %i free entries.\n"
548 "LSQ has %i free entries.\n",
549 tid,
550 free_rob_entries,
551 free_iq_entries,
552 free_lsq_entries);
553
554 blockThisCycle = true;
555
556 block(tid);
557
558 incrFullStat(source);
559
560 return;
561 } else if (min_free_entries < insts_available) {
562 DPRINTF(Rename, "[tid:%u]: Will have to block this cycle."
563 "%i insts available, but only %i insts can be "
564 "renamed due to ROB/IQ/LSQ limits.\n",
565 tid, insts_available, min_free_entries);
566
567 insts_available = min_free_entries;
568
569 blockThisCycle = true;
570
571 incrFullStat(source);
572 }
573
574 InstQueue &insts_to_rename = renameStatus[tid] == Unblocking ?
575 skidBuffer[tid] : insts[tid];
576
577 DPRINTF(Rename, "[tid:%u]: %i available instructions to "
578 "send iew.\n", tid, insts_available);
579
580 DPRINTF(Rename, "[tid:%u]: %i insts pipelining from Rename | %i insts "
581 "dispatched to IQ last cycle.\n",
582 tid, instsInProgress[tid], fromIEW->iewInfo[tid].dispatched);
583
584 // Handle serializing the next instruction if necessary.
585 if (serializeOnNextInst[tid]) {
586 if (emptyROB[tid] && instsInProgress[tid] == 0) {
587 // ROB already empty; no need to serialize.
588 serializeOnNextInst[tid] = false;
589 } else if (!insts_to_rename.empty()) {
590 insts_to_rename.front()->setSerializeBefore();
591 }
592 }
593
594 int renamed_insts = 0;
595
596 while (insts_available > 0 && toIEWIndex < renameWidth) {
597 DPRINTF(Rename, "[tid:%u]: Sending instructions to IEW.\n", tid);
598
599 assert(!insts_to_rename.empty());
600
601 inst = insts_to_rename.front();
602
603 insts_to_rename.pop_front();
604
605 if (renameStatus[tid] == Unblocking) {
606 DPRINTF(Rename,"[tid:%u]: Removing [sn:%lli] PC:%s from rename "
607 "skidBuffer\n", tid, inst->seqNum, inst->pcState());
608 }
609
610 if (inst->isSquashed()) {
611 DPRINTF(Rename, "[tid:%u]: instruction %i with PC %s is "
612 "squashed, skipping.\n", tid, inst->seqNum,
613 inst->pcState());
614
615 ++renameSquashedInsts;
616
617 // Decrement how many instructions are available.
618 --insts_available;
619
620 continue;
621 }
622
623 DPRINTF(Rename, "[tid:%u]: Processing instruction [sn:%lli] with "
624 "PC %s.\n", tid, inst->seqNum, inst->pcState());
625
626 // Handle serializeAfter/serializeBefore instructions.
627 // serializeAfter marks the next instruction as serializeBefore.
628 // serializeBefore makes the instruction wait in rename until the ROB
629 // is empty.
630
631 // In this model, IPR accesses are serialize before
632 // instructions, and store conditionals are serialize after
633 // instructions. This is mainly due to lack of support for
634 // out-of-order operations of either of those classes of
635 // instructions.
636 if ((inst->isIprAccess() || inst->isSerializeBefore()) &&
637 !inst->isSerializeHandled()) {
638 DPRINTF(Rename, "Serialize before instruction encountered.\n");
639
640 if (!inst->isTempSerializeBefore()) {
641 renamedSerializing++;
642 inst->setSerializeHandled();
643 } else {
644 renamedTempSerializing++;
645 }
646
647 // Change status over to SerializeStall so that other stages know
648 // what this is blocked on.
649 renameStatus[tid] = SerializeStall;
650
651 serializeInst[tid] = inst;
652
653 blockThisCycle = true;
654
655 break;
656 } else if ((inst->isStoreConditional() || inst->isSerializeAfter()) &&
657 !inst->isSerializeHandled()) {
658 DPRINTF(Rename, "Serialize after instruction encountered.\n");
659
660 renamedSerializing++;
661
662 inst->setSerializeHandled();
663
664 serializeAfter(insts_to_rename, tid);
665 }
666
667 // Check here to make sure there are enough destination registers
668 // to rename to. Otherwise block.
669 if (renameMap[tid]->numFreeEntries() < inst->numDestRegs()) {
670 DPRINTF(Rename, "Blocking due to lack of free "
671 "physical registers to rename to.\n");
672 blockThisCycle = true;
673 insts_to_rename.push_front(inst);
674 ++renameFullRegistersEvents;
675
676 break;
677 }
678
679 renameSrcRegs(inst, inst->threadNumber);
680
681 renameDestRegs(inst, inst->threadNumber);
682
683 ++renamed_insts;
684
685 // Put instruction in rename queue.
686 toIEW->insts[toIEWIndex] = inst;
687 ++(toIEW->size);
688
689 // Increment which instruction we're on.
690 ++toIEWIndex;
691
692 // Decrement how many instructions are available.
693 --insts_available;
694 }
695
696 instsInProgress[tid] += renamed_insts;
697 renameRenamedInsts += renamed_insts;
698
699 // If we wrote to the time buffer, record this.
700 if (toIEWIndex) {
701 wroteToTimeBuffer = true;
702 }
703
704 // Check if there's any instructions left that haven't yet been renamed.
705 // If so then block.
706 if (insts_available) {
707 blockThisCycle = true;
708 }
709
710 if (blockThisCycle) {
711 block(tid);
712 toDecode->renameUnblock[tid] = false;
713 }
714}
715
716template<class Impl>
717void
718DefaultRename<Impl>::skidInsert(ThreadID tid)
719{
720 DynInstPtr inst = NULL;
721
722 while (!insts[tid].empty()) {
723 inst = insts[tid].front();
724
725 insts[tid].pop_front();
726
727 assert(tid == inst->threadNumber);
728
729 DPRINTF(Rename, "[tid:%u]: Inserting [sn:%lli] PC: %s into Rename "
730 "skidBuffer\n", tid, inst->seqNum, inst->pcState());
731
732 ++renameSkidInsts;
733
734 skidBuffer[tid].push_back(inst);
735 }
736
737 if (skidBuffer[tid].size() > skidBufferMax)
738 {
739 typename InstQueue::iterator it;
740 warn("Skidbuffer contents:\n");
741 for(it = skidBuffer[tid].begin(); it != skidBuffer[tid].end(); it++)
742 {
743 warn("[tid:%u]: %s [sn:%i].\n", tid,
744 (*it)->staticInst->disassemble(inst->instAddr()),
745 (*it)->seqNum);
746 }
747 panic("Skidbuffer Exceeded Max Size");
748 }
749}
750
751template <class Impl>
752void
753DefaultRename<Impl>::sortInsts()
754{
755 int insts_from_decode = fromDecode->size;
756#ifdef DEBUG
757 for (ThreadID tid = 0; tid < numThreads; tid++)
758 assert(insts[tid].empty());
759#endif
760 for (int i = 0; i < insts_from_decode; ++i) {
761 DynInstPtr inst = fromDecode->insts[i];
762 insts[inst->threadNumber].push_back(inst);
763 }
764}
765
766template<class Impl>
767bool
768DefaultRename<Impl>::skidsEmpty()
769{
770 list<ThreadID>::iterator threads = activeThreads->begin();
771 list<ThreadID>::iterator end = activeThreads->end();
772
773 while (threads != end) {
774 ThreadID tid = *threads++;
775
776 if (!skidBuffer[tid].empty())
777 return false;
778 }
779
780 return true;
781}
782
783template<class Impl>
784void
785DefaultRename<Impl>::updateStatus()
786{
787 bool any_unblocking = false;
788
789 list<ThreadID>::iterator threads = activeThreads->begin();
790 list<ThreadID>::iterator end = activeThreads->end();
791
792 while (threads != end) {
793 ThreadID tid = *threads++;
794
795 if (renameStatus[tid] == Unblocking) {
796 any_unblocking = true;
797 break;
798 }
799 }
800
801 // Rename will have activity if it's unblocking.
802 if (any_unblocking) {
803 if (_status == Inactive) {
804 _status = Active;
805
806 DPRINTF(Activity, "Activating stage.\n");
807
808 cpu->activateStage(O3CPU::RenameIdx);
809 }
810 } else {
811 // If it's not unblocking, then rename will not have any internal
812 // activity. Switch it to inactive.
813 if (_status == Active) {
814 _status = Inactive;
815 DPRINTF(Activity, "Deactivating stage.\n");
816
817 cpu->deactivateStage(O3CPU::RenameIdx);
818 }
819 }
820}
821
822template <class Impl>
823bool
824DefaultRename<Impl>::block(ThreadID tid)
825{
826 DPRINTF(Rename, "[tid:%u]: Blocking.\n", tid);
827
828 // Add the current inputs onto the skid buffer, so they can be
829 // reprocessed when this stage unblocks.
830 skidInsert(tid);
831
832 // Only signal backwards to block if the previous stages do not think
833 // rename is already blocked.
834 if (renameStatus[tid] != Blocked) {
835 // If resumeUnblocking is set, we unblocked during the squash,
836 // but now we're have unblocking status. We need to tell earlier
837 // stages to block.
838 if (resumeUnblocking || renameStatus[tid] != Unblocking) {
839 toDecode->renameBlock[tid] = true;
840 toDecode->renameUnblock[tid] = false;
841 wroteToTimeBuffer = true;
842 }
843
844 // Rename can not go from SerializeStall to Blocked, otherwise
845 // it would not know to complete the serialize stall.
846 if (renameStatus[tid] != SerializeStall) {
847 // Set status to Blocked.
848 renameStatus[tid] = Blocked;
849 return true;
850 }
851 }
852
853 return false;
854}
855
856template <class Impl>
857bool
858DefaultRename<Impl>::unblock(ThreadID tid)
859{
860 DPRINTF(Rename, "[tid:%u]: Trying to unblock.\n", tid);
861
862 // Rename is done unblocking if the skid buffer is empty.
863 if (skidBuffer[tid].empty() && renameStatus[tid] != SerializeStall) {
864
865 DPRINTF(Rename, "[tid:%u]: Done unblocking.\n", tid);
866
867 toDecode->renameUnblock[tid] = true;
868 wroteToTimeBuffer = true;
869
870 renameStatus[tid] = Running;
871 return true;
872 }
873
874 return false;
875}
876
877template <class Impl>
878void
879DefaultRename<Impl>::doSquash(const InstSeqNum &squashed_seq_num, ThreadID tid)
880{
881 typename std::list<RenameHistory>::iterator hb_it =
882 historyBuffer[tid].begin();
883
884 // After a syscall squashes everything, the history buffer may be empty
885 // but the ROB may still be squashing instructions.
886 if (historyBuffer[tid].empty()) {
887 return;
888 }
889
890 // Go through the most recent instructions, undoing the mappings
891 // they did and freeing up the registers.
892 while (!historyBuffer[tid].empty() &&
893 (*hb_it).instSeqNum > squashed_seq_num) {
894 assert(hb_it != historyBuffer[tid].end());
895
896 DPRINTF(Rename, "[tid:%u]: Removing history entry with sequence "
897 "number %i.\n", tid, (*hb_it).instSeqNum);
898
899 // Tell the rename map to set the architected register to the
900 // previous physical register that it was renamed to.
901 renameMap[tid]->setEntry(hb_it->archReg, hb_it->prevPhysReg);
902
903 // Put the renamed physical register back on the free list.
904 freeList->addReg(hb_it->newPhysReg);
905
906 // Be sure to mark its register as ready if it's a misc register.
907 if (hb_it->newPhysReg >= maxPhysicalRegs) {
908 scoreboard->setReg(hb_it->newPhysReg);
909 }
910
911 historyBuffer[tid].erase(hb_it++);
912
913 ++renameUndoneMaps;
914 }
915}
916
917template<class Impl>
918void
919DefaultRename<Impl>::removeFromHistory(InstSeqNum inst_seq_num, ThreadID tid)
920{
921 DPRINTF(Rename, "[tid:%u]: Removing a committed instruction from the "
922 "history buffer %u (size=%i), until [sn:%lli].\n",
923 tid, tid, historyBuffer[tid].size(), inst_seq_num);
924
925 typename std::list<RenameHistory>::iterator hb_it =
926 historyBuffer[tid].end();
927
928 --hb_it;
929
930 if (historyBuffer[tid].empty()) {
931 DPRINTF(Rename, "[tid:%u]: History buffer is empty.\n", tid);
932 return;
933 } else if (hb_it->instSeqNum > inst_seq_num) {
934 DPRINTF(Rename, "[tid:%u]: Old sequence number encountered. Ensure "
935 "that a syscall happened recently.\n", tid);
936 return;
937 }
938
939 // Commit all the renames up until (and including) the committed sequence
940 // number. Some or even all of the committed instructions may not have
941 // rename histories if they did not have destination registers that were
942 // renamed.
943 while (!historyBuffer[tid].empty() &&
944 hb_it != historyBuffer[tid].end() &&
945 (*hb_it).instSeqNum <= inst_seq_num) {
946
947 DPRINTF(Rename, "[tid:%u]: Freeing up older rename of reg %i, "
948 "[sn:%lli].\n",
949 tid, (*hb_it).prevPhysReg, (*hb_it).instSeqNum);
950
951 freeList->addReg((*hb_it).prevPhysReg);
952 ++renameCommittedMaps;
953
954 historyBuffer[tid].erase(hb_it--);
955 }
956}
957
958template <class Impl>
959inline void
960DefaultRename<Impl>::renameSrcRegs(DynInstPtr &inst, ThreadID tid)
961{
962 assert(renameMap[tid] != 0);
963
964 unsigned num_src_regs = inst->numSrcRegs();
965
966 // Get the architectual register numbers from the source and
967 // destination operands, and redirect them to the right register.
968 // Will need to mark dependencies though.
969 for (int src_idx = 0; src_idx < num_src_regs; src_idx++) {
970 RegIndex src_reg = inst->srcRegIdx(src_idx);
971 RegIndex flat_src_reg = src_reg;
972 if (src_reg < TheISA::FP_Base_DepTag) {
973 flat_src_reg = inst->tcBase()->flattenIntIndex(src_reg);
974 DPRINTF(Rename, "Flattening index %d to %d.\n",
975 (int)src_reg, (int)flat_src_reg);
976 } else if (src_reg < TheISA::Ctrl_Base_DepTag) {
977 src_reg = src_reg - TheISA::FP_Base_DepTag;
978 flat_src_reg = inst->tcBase()->flattenFloatIndex(src_reg);
979 DPRINTF(Rename, "Flattening index %d to %d.\n",
980 (int)src_reg, (int)flat_src_reg);
981 flat_src_reg += TheISA::NumIntRegs;
982 } else if (src_reg < TheISA::Max_DepTag) {
983 flat_src_reg = src_reg - TheISA::Ctrl_Base_DepTag +
984 TheISA::NumFloatRegs + TheISA::NumIntRegs;
985 DPRINTF(Rename, "Adjusting reg index from %d to %d.\n",
986 src_reg, flat_src_reg);
987 } else {
988 panic("Reg index is out of bound: %d.", src_reg);
989 }
990
991 inst->flattenSrcReg(src_idx, flat_src_reg);
992
993 // Look up the source registers to get the phys. register they've
994 // been renamed to, and set the sources to those registers.
995 PhysRegIndex renamed_reg = renameMap[tid]->lookup(flat_src_reg);
996
997 DPRINTF(Rename, "[tid:%u]: Looking up arch reg %i, got "
998 "physical reg %i.\n", tid, (int)flat_src_reg,
999 (int)renamed_reg);
1000
1001 inst->renameSrcReg(src_idx, renamed_reg);
1002
1003 // See if the register is ready or not.
1004 if (scoreboard->getReg(renamed_reg) == true) {
1005 DPRINTF(Rename, "[tid:%u]: Register %d is ready.\n",
1006 tid, renamed_reg);
1007
1008 inst->markSrcRegReady(src_idx);
1009 } else {
1010 DPRINTF(Rename, "[tid:%u]: Register %d is not ready.\n",
1011 tid, renamed_reg);
1012 }
1013
1014 ++renameRenameLookups;
1015 }
1016}
1017
1018template <class Impl>
1019inline void
1020DefaultRename<Impl>::renameDestRegs(DynInstPtr &inst, ThreadID tid)
1021{
1022 typename RenameMap::RenameInfo rename_result;
1023
1024 unsigned num_dest_regs = inst->numDestRegs();
1025
1026 // Rename the destination registers.
1027 for (int dest_idx = 0; dest_idx < num_dest_regs; dest_idx++) {
1028 RegIndex dest_reg = inst->destRegIdx(dest_idx);
1029 RegIndex flat_dest_reg = dest_reg;
1030 if (dest_reg < TheISA::FP_Base_DepTag) {
1031 // Integer registers are flattened.
1032 flat_dest_reg = inst->tcBase()->flattenIntIndex(dest_reg);
1033 DPRINTF(Rename, "Flattening index %d to %d.\n",
1034 (int)dest_reg, (int)flat_dest_reg);
1035 } else if (dest_reg < TheISA::Ctrl_Base_DepTag) {
1036 dest_reg = dest_reg - TheISA::FP_Base_DepTag;
1037 flat_dest_reg = inst->tcBase()->flattenFloatIndex(dest_reg);
1038 DPRINTF(Rename, "Flattening index %d to %d.\n",
1039 (int)dest_reg, (int)flat_dest_reg);
1040 flat_dest_reg += TheISA::NumIntRegs;
1041 } else if (dest_reg < TheISA::Max_DepTag) {
1042 // Floating point and Miscellaneous registers need their indexes
1043 // adjusted to account for the expanded number of flattened int regs.
1044 flat_dest_reg = dest_reg - TheISA::Ctrl_Base_DepTag +
1045 TheISA::NumIntRegs + TheISA::NumFloatRegs;
1046 DPRINTF(Rename, "Adjusting reg index from %d to %d.\n",
1047 dest_reg, flat_dest_reg);
1048 } else {
1049 panic("Reg index is out of bound: %d.", dest_reg);
1050 }
1051
1052 inst->flattenDestReg(dest_idx, flat_dest_reg);
1053
1054 // Get the physical register that the destination will be
1055 // renamed to.
1056 rename_result = renameMap[tid]->rename(flat_dest_reg);
1057
1058 //Mark Scoreboard entry as not ready
1047 scoreboard->unsetReg(rename_result.first);
1059 if (dest_reg < TheISA::Ctrl_Base_DepTag)
1060 scoreboard->unsetReg(rename_result.first);
1048
1049 DPRINTF(Rename, "[tid:%u]: Renaming arch reg %i to physical "
1050 "reg %i.\n", tid, (int)flat_dest_reg,
1051 (int)rename_result.first);
1052
1053 // Record the rename information so that a history can be kept.
1054 RenameHistory hb_entry(inst->seqNum, flat_dest_reg,
1055 rename_result.first,
1056 rename_result.second);
1057
1058 historyBuffer[tid].push_front(hb_entry);
1059
1060 DPRINTF(Rename, "[tid:%u]: Adding instruction to history buffer "
1061 "(size=%i), [sn:%lli].\n",tid,
1062 historyBuffer[tid].size(),
1063 (*historyBuffer[tid].begin()).instSeqNum);
1064
1065 // Tell the instruction to rename the appropriate destination
1066 // register (dest_idx) to the new physical register
1067 // (rename_result.first), and record the previous physical
1068 // register that the same logical register was renamed to
1069 // (rename_result.second).
1070 inst->renameDestReg(dest_idx,
1071 rename_result.first,
1072 rename_result.second);
1073
1074 ++renameRenamedOperands;
1075 }
1076}
1077
1078template <class Impl>
1079inline int
1080DefaultRename<Impl>::calcFreeROBEntries(ThreadID tid)
1081{
1082 int num_free = freeEntries[tid].robEntries -
1083 (instsInProgress[tid] - fromIEW->iewInfo[tid].dispatched);
1084
1085 //DPRINTF(Rename,"[tid:%i]: %i rob free\n",tid,num_free);
1086
1087 return num_free;
1088}
1089
1090template <class Impl>
1091inline int
1092DefaultRename<Impl>::calcFreeIQEntries(ThreadID tid)
1093{
1094 int num_free = freeEntries[tid].iqEntries -
1095 (instsInProgress[tid] - fromIEW->iewInfo[tid].dispatched);
1096
1097 //DPRINTF(Rename,"[tid:%i]: %i iq free\n",tid,num_free);
1098
1099 return num_free;
1100}
1101
1102template <class Impl>
1103inline int
1104DefaultRename<Impl>::calcFreeLSQEntries(ThreadID tid)
1105{
1106 int num_free = freeEntries[tid].lsqEntries -
1107 (instsInProgress[tid] - fromIEW->iewInfo[tid].dispatchedToLSQ);
1108
1109 //DPRINTF(Rename,"[tid:%i]: %i lsq free\n",tid,num_free);
1110
1111 return num_free;
1112}
1113
1114template <class Impl>
1115unsigned
1116DefaultRename<Impl>::validInsts()
1117{
1118 unsigned inst_count = 0;
1119
1120 for (int i=0; i<fromDecode->size; i++) {
1121 if (!fromDecode->insts[i]->isSquashed())
1122 inst_count++;
1123 }
1124
1125 return inst_count;
1126}
1127
1128template <class Impl>
1129void
1130DefaultRename<Impl>::readStallSignals(ThreadID tid)
1131{
1132 if (fromIEW->iewBlock[tid]) {
1133 stalls[tid].iew = true;
1134 }
1135
1136 if (fromIEW->iewUnblock[tid]) {
1137 assert(stalls[tid].iew);
1138 stalls[tid].iew = false;
1139 }
1140
1141 if (fromCommit->commitBlock[tid]) {
1142 stalls[tid].commit = true;
1143 }
1144
1145 if (fromCommit->commitUnblock[tid]) {
1146 assert(stalls[tid].commit);
1147 stalls[tid].commit = false;
1148 }
1149}
1150
1151template <class Impl>
1152bool
1153DefaultRename<Impl>::checkStall(ThreadID tid)
1154{
1155 bool ret_val = false;
1156
1157 if (stalls[tid].iew) {
1158 DPRINTF(Rename,"[tid:%i]: Stall from IEW stage detected.\n", tid);
1159 ret_val = true;
1160 } else if (stalls[tid].commit) {
1161 DPRINTF(Rename,"[tid:%i]: Stall from Commit stage detected.\n", tid);
1162 ret_val = true;
1163 } else if (calcFreeROBEntries(tid) <= 0) {
1164 DPRINTF(Rename,"[tid:%i]: Stall: ROB has 0 free entries.\n", tid);
1165 ret_val = true;
1166 } else if (calcFreeIQEntries(tid) <= 0) {
1167 DPRINTF(Rename,"[tid:%i]: Stall: IQ has 0 free entries.\n", tid);
1168 ret_val = true;
1169 } else if (calcFreeLSQEntries(tid) <= 0) {
1170 DPRINTF(Rename,"[tid:%i]: Stall: LSQ has 0 free entries.\n", tid);
1171 ret_val = true;
1172 } else if (renameMap[tid]->numFreeEntries() <= 0) {
1173 DPRINTF(Rename,"[tid:%i]: Stall: RenameMap has 0 free entries.\n", tid);
1174 ret_val = true;
1175 } else if (renameStatus[tid] == SerializeStall &&
1176 (!emptyROB[tid] || instsInProgress[tid])) {
1177 DPRINTF(Rename,"[tid:%i]: Stall: Serialize stall and ROB is not "
1178 "empty.\n",
1179 tid);
1180 ret_val = true;
1181 }
1182
1183 return ret_val;
1184}
1185
1186template <class Impl>
1187void
1188DefaultRename<Impl>::readFreeEntries(ThreadID tid)
1189{
1190 bool updated = false;
1191 if (fromIEW->iewInfo[tid].usedIQ) {
1192 freeEntries[tid].iqEntries =
1193 fromIEW->iewInfo[tid].freeIQEntries;
1194 updated = true;
1195 }
1196
1197 if (fromIEW->iewInfo[tid].usedLSQ) {
1198 freeEntries[tid].lsqEntries =
1199 fromIEW->iewInfo[tid].freeLSQEntries;
1200 updated = true;
1201 }
1202
1203 if (fromCommit->commitInfo[tid].usedROB) {
1204 freeEntries[tid].robEntries =
1205 fromCommit->commitInfo[tid].freeROBEntries;
1206 emptyROB[tid] = fromCommit->commitInfo[tid].emptyROB;
1207 updated = true;
1208 }
1209
1210 DPRINTF(Rename, "[tid:%i]: Free IQ: %i, Free ROB: %i, Free LSQ: %i\n",
1211 tid,
1212 freeEntries[tid].iqEntries,
1213 freeEntries[tid].robEntries,
1214 freeEntries[tid].lsqEntries);
1215
1216 DPRINTF(Rename, "[tid:%i]: %i instructions not yet in ROB\n",
1217 tid, instsInProgress[tid]);
1218}
1219
1220template <class Impl>
1221bool
1222DefaultRename<Impl>::checkSignalsAndUpdate(ThreadID tid)
1223{
1224 // Check if there's a squash signal, squash if there is
1225 // Check stall signals, block if necessary.
1226 // If status was blocked
1227 // check if stall conditions have passed
1228 // if so then go to unblocking
1229 // If status was Squashing
1230 // check if squashing is not high. Switch to running this cycle.
1231 // If status was serialize stall
1232 // check if ROB is empty and no insts are in flight to the ROB
1233
1234 readFreeEntries(tid);
1235 readStallSignals(tid);
1236
1237 if (fromCommit->commitInfo[tid].squash) {
1238 DPRINTF(Rename, "[tid:%u]: Squashing instructions due to squash from "
1239 "commit.\n", tid);
1240
1241 squash(fromCommit->commitInfo[tid].doneSeqNum, tid);
1242
1243 return true;
1244 }
1245
1246 if (fromCommit->commitInfo[tid].robSquashing) {
1247 DPRINTF(Rename, "[tid:%u]: ROB is still squashing.\n", tid);
1248
1249 renameStatus[tid] = Squashing;
1250
1251 return true;
1252 }
1253
1254 if (checkStall(tid)) {
1255 return block(tid);
1256 }
1257
1258 if (renameStatus[tid] == Blocked) {
1259 DPRINTF(Rename, "[tid:%u]: Done blocking, switching to unblocking.\n",
1260 tid);
1261
1262 renameStatus[tid] = Unblocking;
1263
1264 unblock(tid);
1265
1266 return true;
1267 }
1268
1269 if (renameStatus[tid] == Squashing) {
1270 // Switch status to running if rename isn't being told to block or
1271 // squash this cycle.
1272 if (resumeSerialize) {
1273 DPRINTF(Rename, "[tid:%u]: Done squashing, switching to serialize.\n",
1274 tid);
1275
1276 renameStatus[tid] = SerializeStall;
1277 return true;
1278 } else if (resumeUnblocking) {
1279 DPRINTF(Rename, "[tid:%u]: Done squashing, switching to unblocking.\n",
1280 tid);
1281 renameStatus[tid] = Unblocking;
1282 return true;
1283 } else {
1284 DPRINTF(Rename, "[tid:%u]: Done squashing, switching to running.\n",
1285 tid);
1286
1287 renameStatus[tid] = Running;
1288 return false;
1289 }
1290 }
1291
1292 if (renameStatus[tid] == SerializeStall) {
1293 // Stall ends once the ROB is free.
1294 DPRINTF(Rename, "[tid:%u]: Done with serialize stall, switching to "
1295 "unblocking.\n", tid);
1296
1297 DynInstPtr serial_inst = serializeInst[tid];
1298
1299 renameStatus[tid] = Unblocking;
1300
1301 unblock(tid);
1302
1303 DPRINTF(Rename, "[tid:%u]: Processing instruction [%lli] with "
1304 "PC %s.\n", tid, serial_inst->seqNum, serial_inst->pcState());
1305
1306 // Put instruction into queue here.
1307 serial_inst->clearSerializeBefore();
1308
1309 if (!skidBuffer[tid].empty()) {
1310 skidBuffer[tid].push_front(serial_inst);
1311 } else {
1312 insts[tid].push_front(serial_inst);
1313 }
1314
1315 DPRINTF(Rename, "[tid:%u]: Instruction must be processed by rename."
1316 " Adding to front of list.\n", tid);
1317
1318 serializeInst[tid] = NULL;
1319
1320 return true;
1321 }
1322
1323 // If we've reached this point, we have not gotten any signals that
1324 // cause rename to change its status. Rename remains the same as before.
1325 return false;
1326}
1327
1328template<class Impl>
1329void
1330DefaultRename<Impl>::serializeAfter(InstQueue &inst_list, ThreadID tid)
1331{
1332 if (inst_list.empty()) {
1333 // Mark a bit to say that I must serialize on the next instruction.
1334 serializeOnNextInst[tid] = true;
1335 return;
1336 }
1337
1338 // Set the next instruction as serializing.
1339 inst_list.front()->setSerializeBefore();
1340}
1341
1342template <class Impl>
1343inline void
1344DefaultRename<Impl>::incrFullStat(const FullSource &source)
1345{
1346 switch (source) {
1347 case ROB:
1348 ++renameROBFullEvents;
1349 break;
1350 case IQ:
1351 ++renameIQFullEvents;
1352 break;
1353 case LSQ:
1354 ++renameLSQFullEvents;
1355 break;
1356 default:
1357 panic("Rename full stall stat should be incremented for a reason!");
1358 break;
1359 }
1360}
1361
1362template <class Impl>
1363void
1364DefaultRename<Impl>::dumpHistory()
1365{
1366 typename std::list<RenameHistory>::iterator buf_it;
1367
1368 for (ThreadID tid = 0; tid < numThreads; tid++) {
1369
1370 buf_it = historyBuffer[tid].begin();
1371
1372 while (buf_it != historyBuffer[tid].end()) {
1373 cprintf("Seq num: %i\nArch reg: %i New phys reg: %i Old phys "
1374 "reg: %i\n", (*buf_it).instSeqNum, (int)(*buf_it).archReg,
1375 (int)(*buf_it).newPhysReg, (int)(*buf_it).prevPhysReg);
1376
1377 buf_it++;
1378 }
1379 }
1380}
1061
1062 DPRINTF(Rename, "[tid:%u]: Renaming arch reg %i to physical "
1063 "reg %i.\n", tid, (int)flat_dest_reg,
1064 (int)rename_result.first);
1065
1066 // Record the rename information so that a history can be kept.
1067 RenameHistory hb_entry(inst->seqNum, flat_dest_reg,
1068 rename_result.first,
1069 rename_result.second);
1070
1071 historyBuffer[tid].push_front(hb_entry);
1072
1073 DPRINTF(Rename, "[tid:%u]: Adding instruction to history buffer "
1074 "(size=%i), [sn:%lli].\n",tid,
1075 historyBuffer[tid].size(),
1076 (*historyBuffer[tid].begin()).instSeqNum);
1077
1078 // Tell the instruction to rename the appropriate destination
1079 // register (dest_idx) to the new physical register
1080 // (rename_result.first), and record the previous physical
1081 // register that the same logical register was renamed to
1082 // (rename_result.second).
1083 inst->renameDestReg(dest_idx,
1084 rename_result.first,
1085 rename_result.second);
1086
1087 ++renameRenamedOperands;
1088 }
1089}
1090
1091template <class Impl>
1092inline int
1093DefaultRename<Impl>::calcFreeROBEntries(ThreadID tid)
1094{
1095 int num_free = freeEntries[tid].robEntries -
1096 (instsInProgress[tid] - fromIEW->iewInfo[tid].dispatched);
1097
1098 //DPRINTF(Rename,"[tid:%i]: %i rob free\n",tid,num_free);
1099
1100 return num_free;
1101}
1102
1103template <class Impl>
1104inline int
1105DefaultRename<Impl>::calcFreeIQEntries(ThreadID tid)
1106{
1107 int num_free = freeEntries[tid].iqEntries -
1108 (instsInProgress[tid] - fromIEW->iewInfo[tid].dispatched);
1109
1110 //DPRINTF(Rename,"[tid:%i]: %i iq free\n",tid,num_free);
1111
1112 return num_free;
1113}
1114
1115template <class Impl>
1116inline int
1117DefaultRename<Impl>::calcFreeLSQEntries(ThreadID tid)
1118{
1119 int num_free = freeEntries[tid].lsqEntries -
1120 (instsInProgress[tid] - fromIEW->iewInfo[tid].dispatchedToLSQ);
1121
1122 //DPRINTF(Rename,"[tid:%i]: %i lsq free\n",tid,num_free);
1123
1124 return num_free;
1125}
1126
1127template <class Impl>
1128unsigned
1129DefaultRename<Impl>::validInsts()
1130{
1131 unsigned inst_count = 0;
1132
1133 for (int i=0; i<fromDecode->size; i++) {
1134 if (!fromDecode->insts[i]->isSquashed())
1135 inst_count++;
1136 }
1137
1138 return inst_count;
1139}
1140
1141template <class Impl>
1142void
1143DefaultRename<Impl>::readStallSignals(ThreadID tid)
1144{
1145 if (fromIEW->iewBlock[tid]) {
1146 stalls[tid].iew = true;
1147 }
1148
1149 if (fromIEW->iewUnblock[tid]) {
1150 assert(stalls[tid].iew);
1151 stalls[tid].iew = false;
1152 }
1153
1154 if (fromCommit->commitBlock[tid]) {
1155 stalls[tid].commit = true;
1156 }
1157
1158 if (fromCommit->commitUnblock[tid]) {
1159 assert(stalls[tid].commit);
1160 stalls[tid].commit = false;
1161 }
1162}
1163
1164template <class Impl>
1165bool
1166DefaultRename<Impl>::checkStall(ThreadID tid)
1167{
1168 bool ret_val = false;
1169
1170 if (stalls[tid].iew) {
1171 DPRINTF(Rename,"[tid:%i]: Stall from IEW stage detected.\n", tid);
1172 ret_val = true;
1173 } else if (stalls[tid].commit) {
1174 DPRINTF(Rename,"[tid:%i]: Stall from Commit stage detected.\n", tid);
1175 ret_val = true;
1176 } else if (calcFreeROBEntries(tid) <= 0) {
1177 DPRINTF(Rename,"[tid:%i]: Stall: ROB has 0 free entries.\n", tid);
1178 ret_val = true;
1179 } else if (calcFreeIQEntries(tid) <= 0) {
1180 DPRINTF(Rename,"[tid:%i]: Stall: IQ has 0 free entries.\n", tid);
1181 ret_val = true;
1182 } else if (calcFreeLSQEntries(tid) <= 0) {
1183 DPRINTF(Rename,"[tid:%i]: Stall: LSQ has 0 free entries.\n", tid);
1184 ret_val = true;
1185 } else if (renameMap[tid]->numFreeEntries() <= 0) {
1186 DPRINTF(Rename,"[tid:%i]: Stall: RenameMap has 0 free entries.\n", tid);
1187 ret_val = true;
1188 } else if (renameStatus[tid] == SerializeStall &&
1189 (!emptyROB[tid] || instsInProgress[tid])) {
1190 DPRINTF(Rename,"[tid:%i]: Stall: Serialize stall and ROB is not "
1191 "empty.\n",
1192 tid);
1193 ret_val = true;
1194 }
1195
1196 return ret_val;
1197}
1198
1199template <class Impl>
1200void
1201DefaultRename<Impl>::readFreeEntries(ThreadID tid)
1202{
1203 bool updated = false;
1204 if (fromIEW->iewInfo[tid].usedIQ) {
1205 freeEntries[tid].iqEntries =
1206 fromIEW->iewInfo[tid].freeIQEntries;
1207 updated = true;
1208 }
1209
1210 if (fromIEW->iewInfo[tid].usedLSQ) {
1211 freeEntries[tid].lsqEntries =
1212 fromIEW->iewInfo[tid].freeLSQEntries;
1213 updated = true;
1214 }
1215
1216 if (fromCommit->commitInfo[tid].usedROB) {
1217 freeEntries[tid].robEntries =
1218 fromCommit->commitInfo[tid].freeROBEntries;
1219 emptyROB[tid] = fromCommit->commitInfo[tid].emptyROB;
1220 updated = true;
1221 }
1222
1223 DPRINTF(Rename, "[tid:%i]: Free IQ: %i, Free ROB: %i, Free LSQ: %i\n",
1224 tid,
1225 freeEntries[tid].iqEntries,
1226 freeEntries[tid].robEntries,
1227 freeEntries[tid].lsqEntries);
1228
1229 DPRINTF(Rename, "[tid:%i]: %i instructions not yet in ROB\n",
1230 tid, instsInProgress[tid]);
1231}
1232
1233template <class Impl>
1234bool
1235DefaultRename<Impl>::checkSignalsAndUpdate(ThreadID tid)
1236{
1237 // Check if there's a squash signal, squash if there is
1238 // Check stall signals, block if necessary.
1239 // If status was blocked
1240 // check if stall conditions have passed
1241 // if so then go to unblocking
1242 // If status was Squashing
1243 // check if squashing is not high. Switch to running this cycle.
1244 // If status was serialize stall
1245 // check if ROB is empty and no insts are in flight to the ROB
1246
1247 readFreeEntries(tid);
1248 readStallSignals(tid);
1249
1250 if (fromCommit->commitInfo[tid].squash) {
1251 DPRINTF(Rename, "[tid:%u]: Squashing instructions due to squash from "
1252 "commit.\n", tid);
1253
1254 squash(fromCommit->commitInfo[tid].doneSeqNum, tid);
1255
1256 return true;
1257 }
1258
1259 if (fromCommit->commitInfo[tid].robSquashing) {
1260 DPRINTF(Rename, "[tid:%u]: ROB is still squashing.\n", tid);
1261
1262 renameStatus[tid] = Squashing;
1263
1264 return true;
1265 }
1266
1267 if (checkStall(tid)) {
1268 return block(tid);
1269 }
1270
1271 if (renameStatus[tid] == Blocked) {
1272 DPRINTF(Rename, "[tid:%u]: Done blocking, switching to unblocking.\n",
1273 tid);
1274
1275 renameStatus[tid] = Unblocking;
1276
1277 unblock(tid);
1278
1279 return true;
1280 }
1281
1282 if (renameStatus[tid] == Squashing) {
1283 // Switch status to running if rename isn't being told to block or
1284 // squash this cycle.
1285 if (resumeSerialize) {
1286 DPRINTF(Rename, "[tid:%u]: Done squashing, switching to serialize.\n",
1287 tid);
1288
1289 renameStatus[tid] = SerializeStall;
1290 return true;
1291 } else if (resumeUnblocking) {
1292 DPRINTF(Rename, "[tid:%u]: Done squashing, switching to unblocking.\n",
1293 tid);
1294 renameStatus[tid] = Unblocking;
1295 return true;
1296 } else {
1297 DPRINTF(Rename, "[tid:%u]: Done squashing, switching to running.\n",
1298 tid);
1299
1300 renameStatus[tid] = Running;
1301 return false;
1302 }
1303 }
1304
1305 if (renameStatus[tid] == SerializeStall) {
1306 // Stall ends once the ROB is free.
1307 DPRINTF(Rename, "[tid:%u]: Done with serialize stall, switching to "
1308 "unblocking.\n", tid);
1309
1310 DynInstPtr serial_inst = serializeInst[tid];
1311
1312 renameStatus[tid] = Unblocking;
1313
1314 unblock(tid);
1315
1316 DPRINTF(Rename, "[tid:%u]: Processing instruction [%lli] with "
1317 "PC %s.\n", tid, serial_inst->seqNum, serial_inst->pcState());
1318
1319 // Put instruction into queue here.
1320 serial_inst->clearSerializeBefore();
1321
1322 if (!skidBuffer[tid].empty()) {
1323 skidBuffer[tid].push_front(serial_inst);
1324 } else {
1325 insts[tid].push_front(serial_inst);
1326 }
1327
1328 DPRINTF(Rename, "[tid:%u]: Instruction must be processed by rename."
1329 " Adding to front of list.\n", tid);
1330
1331 serializeInst[tid] = NULL;
1332
1333 return true;
1334 }
1335
1336 // If we've reached this point, we have not gotten any signals that
1337 // cause rename to change its status. Rename remains the same as before.
1338 return false;
1339}
1340
1341template<class Impl>
1342void
1343DefaultRename<Impl>::serializeAfter(InstQueue &inst_list, ThreadID tid)
1344{
1345 if (inst_list.empty()) {
1346 // Mark a bit to say that I must serialize on the next instruction.
1347 serializeOnNextInst[tid] = true;
1348 return;
1349 }
1350
1351 // Set the next instruction as serializing.
1352 inst_list.front()->setSerializeBefore();
1353}
1354
1355template <class Impl>
1356inline void
1357DefaultRename<Impl>::incrFullStat(const FullSource &source)
1358{
1359 switch (source) {
1360 case ROB:
1361 ++renameROBFullEvents;
1362 break;
1363 case IQ:
1364 ++renameIQFullEvents;
1365 break;
1366 case LSQ:
1367 ++renameLSQFullEvents;
1368 break;
1369 default:
1370 panic("Rename full stall stat should be incremented for a reason!");
1371 break;
1372 }
1373}
1374
1375template <class Impl>
1376void
1377DefaultRename<Impl>::dumpHistory()
1378{
1379 typename std::list<RenameHistory>::iterator buf_it;
1380
1381 for (ThreadID tid = 0; tid < numThreads; tid++) {
1382
1383 buf_it = historyBuffer[tid].begin();
1384
1385 while (buf_it != historyBuffer[tid].end()) {
1386 cprintf("Seq num: %i\nArch reg: %i New phys reg: %i Old phys "
1387 "reg: %i\n", (*buf_it).instSeqNum, (int)(*buf_it).archReg,
1388 (int)(*buf_it).newPhysReg, (int)(*buf_it).prevPhysReg);
1389
1390 buf_it++;
1391 }
1392 }
1393}