inst_queue_impl.hh revision 1062
13536SN/A#ifndef __INST_QUEUE_IMPL_HH__
211274Sshingarov@labware.com#define __INST_QUEUE_IMPL_HH__
310595Sgabeblack@google.com
410037SARM gem5 Developers// Todo:
57752SWilliam.Wang@arm.com// Current ordering allows for 0 cycle added-to-scheduled.  Could maybe fake
67752SWilliam.Wang@arm.com// it; either do in reverse order, or have added instructions put into a
77752SWilliam.Wang@arm.com// different ready queue that, in scheduleRreadyInsts(), gets put onto the
87752SWilliam.Wang@arm.com// normal ready queue.  This would however give only a one cycle delay,
97752SWilliam.Wang@arm.com// but probably is more flexible to actually add in a delay parameter than
107752SWilliam.Wang@arm.com// just running it backwards.
117752SWilliam.Wang@arm.com
127752SWilliam.Wang@arm.com#include <vector>
137752SWilliam.Wang@arm.com
147752SWilliam.Wang@arm.com#include "sim/universe.hh"
157752SWilliam.Wang@arm.com#include "cpu/beta_cpu/inst_queue.hh"
163536SN/A
173536SN/A// Either compile error or max int due to sign extension.
183536SN/A// Blatant hack to avoid compile warnings.
193536SN/Aconst InstSeqNum MaxInstSeqNum = 0 - 1;
203536SN/A
213536SN/Atemplate <class Impl>
223536SN/AInstructionQueue<Impl>::InstructionQueue(Params &params)
233536SN/A    : memDepUnit(params),
243536SN/A      numEntries(params.numIQEntries),
253536SN/A      intWidth(params.executeIntWidth),
263536SN/A      floatWidth(params.executeFloatWidth),
273536SN/A      branchWidth(params.executeBranchWidth),
283536SN/A      memoryWidth(params.executeMemoryWidth),
293536SN/A      totalWidth(params.issueWidth),
303536SN/A      numPhysIntRegs(params.numPhysIntRegs),
313536SN/A      numPhysFloatRegs(params.numPhysFloatRegs),
323536SN/A      commitToIEWDelay(params.commitToIEWDelay)
333536SN/A{
343536SN/A    DPRINTF(IQ, "IQ: Int width is %i.\n", params.executeIntWidth);
353536SN/A
363536SN/A    // Initialize the number of free IQ entries.
373536SN/A    freeEntries = numEntries;
383536SN/A
393536SN/A    // Set the number of physical registers as the number of int + float
403536SN/A    numPhysRegs = numPhysIntRegs + numPhysFloatRegs;
413536SN/A
423536SN/A    DPRINTF(IQ, "IQ: There are %i physical registers.\n", numPhysRegs);
437752SWilliam.Wang@arm.com
4411274Sshingarov@labware.com    //Create an entry for each physical register within the
453536SN/A    //dependency graph.
463536SN/A    dependGraph = new DependencyEntry[numPhysRegs];
473536SN/A
488332Snate@binkert.org    // Resize the register scoreboard.
498332Snate@binkert.org    regScoreboard.resize(numPhysRegs);
503536SN/A
513536SN/A    // Initialize all the head pointers to point to NULL, and all the
523536SN/A    // entries as unready.
533536SN/A    // Note that in actuality, the registers corresponding to the logical
543536SN/A    // registers start off as ready.  However this doesn't matter for the
553536SN/A    // IQ as the instruction should have been correctly told if those
563536SN/A    // registers are ready in rename.  Thus it can all be initialized as
575543SN/A    // unready.
585543SN/A    for (int i = 0; i < numPhysRegs; ++i)
593536SN/A    {
603536SN/A        dependGraph[i].next = NULL;
613536SN/A        dependGraph[i].inst = NULL;
623536SN/A        regScoreboard[i] = false;
633536SN/A    }
643536SN/A
653536SN/A}
663536SN/A
673536SN/Atemplate <class Impl>
683536SN/Avoid
693536SN/AInstructionQueue<Impl>::regStats()
705543SN/A{
715543SN/A    iqInstsAdded
723536SN/A        .name(name() + ".iqInstsAdded")
733536SN/A        .desc("Number of instructions added to the IQ (excludes non-spec)")
743536SN/A        .prereq(iqInstsAdded);
753536SN/A
763536SN/A    iqNonSpecInstsAdded
773536SN/A        .name(name() + ".iqNonSpecInstsAdded")
783536SN/A        .desc("Number of non-speculative instructions added to the IQ")
793536SN/A        .prereq(iqNonSpecInstsAdded);
803536SN/A
813536SN/A//    iqIntInstsAdded;
823536SN/A
833536SN/A    iqIntInstsIssued
843536SN/A        .name(name() + ".iqIntInstsIssued")
853536SN/A        .desc("Number of integer instructions issued")
863536SN/A        .prereq(iqIntInstsIssued);
873536SN/A
885543SN/A//    iqFloatInstsAdded;
893536SN/A
903536SN/A    iqFloatInstsIssued
913536SN/A        .name(name() + ".iqFloatInstsIssued")
923536SN/A        .desc("Number of float instructions issued")
933536SN/A        .prereq(iqFloatInstsIssued);
943536SN/A
953536SN/A//    iqBranchInstsAdded;
963536SN/A
973536SN/A    iqBranchInstsIssued
983536SN/A        .name(name() + ".iqBranchInstsIssued")
993536SN/A        .desc("Number of branch instructions issued")
1003536SN/A        .prereq(iqBranchInstsIssued);
1013536SN/A
1023536SN/A//    iqMemInstsAdded;
1033536SN/A
1043536SN/A    iqMemInstsIssued
1053536SN/A        .name(name() + ".iqMemInstsIssued")
1063536SN/A        .desc("Number of memory instructions issued")
1073536SN/A        .prereq(iqMemInstsIssued);
1085543SN/A
1095543SN/A//    iqMiscInstsAdded;
1103536SN/A
1113536SN/A    iqMiscInstsIssued
1123536SN/A        .name(name() + ".iqMiscInstsIssued")
1133536SN/A        .desc("Number of miscellaneous instructions issued")
1143536SN/A        .prereq(iqMiscInstsIssued);
1153536SN/A
1163536SN/A    iqSquashedInstsIssued
1173536SN/A        .name(name() + ".iqSquashedInstsIssued")
1183536SN/A        .desc("Number of squashed instructions issued")
1193536SN/A        .prereq(iqSquashedInstsIssued);
1203536SN/A
1213536SN/A    iqLoopSquashStalls
1223536SN/A        .name(name() + ".iqLoopSquashStalls")
1233536SN/A        .desc("Number of times issue loop had to restart due to squashed "
1243536SN/A              "inst; mainly for profiling")
1253536SN/A        .prereq(iqLoopSquashStalls);
1263536SN/A
1273536SN/A    iqSquashedInstsExamined
1283536SN/A        .name(name() + ".iqSquashedInstsExamined")
1293536SN/A        .desc("Number of squashed instructions iterated over during squash;"
1303536SN/A              " mainly for profiling")
1313536SN/A        .prereq(iqSquashedInstsExamined);
1323536SN/A
1333536SN/A    iqSquashedOperandsExamined
1343536SN/A        .name(name() + ".iqSquashedOperandsExamined")
13511793Sbrandon.potter@amd.com        .desc("Number of squashed operands that are examined and possibly "
13611793Sbrandon.potter@amd.com              "removed from graph")
1373536SN/A        .prereq(iqSquashedOperandsExamined);
1385569SN/A
1393536SN/A    iqSquashedNonSpecRemoved
1403536SN/A        .name(name() + ".iqSquashedNonSpecRemoved")
1413536SN/A        .desc("Number of squashed non-spec instructions that were removed")
1429020Sgblack@eecs.umich.edu        .prereq(iqSquashedNonSpecRemoved);
1438229Snate@binkert.org
1448229Snate@binkert.org    // Tell mem dependence unit to reg stats as well.
14510037SARM gem5 Developers    memDepUnit.regStats();
1467752SWilliam.Wang@arm.com}
1477752SWilliam.Wang@arm.com
14810707SAndreas.Sandberg@ARM.comtemplate <class Impl>
1493536SN/Avoid
1503536SN/AInstructionQueue<Impl>::setCPU(FullCPU *cpu_ptr)
1513536SN/A{
1523536SN/A    cpu = cpu_ptr;
1538229Snate@binkert.org
1543536SN/A    tail = cpu->instList.begin();
1557752SWilliam.Wang@arm.com}
1568232Snate@binkert.org
1578232Snate@binkert.orgtemplate <class Impl>
1588229Snate@binkert.orgvoid
1593536SN/AInstructionQueue<Impl>::setIssueToExecuteQueue(
1603536SN/A                        TimeBuffer<IssueStruct> *i2e_ptr)
1618782Sgblack@eecs.umich.edu{
1623536SN/A    DPRINTF(IQ, "IQ: Set the issue to execute queue.\n");
1633536SN/A    issueToExecuteQueue = i2e_ptr;
1643536SN/A}
1657752SWilliam.Wang@arm.com
1663536SN/Atemplate <class Impl>
1675569SN/Avoid
16812031Sgabeblack@google.comInstructionQueue<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
1693536SN/A{
1703536SN/A    DPRINTF(IQ, "IQ: Set the time buffer.\n");
1713536SN/A    timeBuffer = tb_ptr;
1725569SN/A
1735569SN/A    fromCommit = timeBuffer->getWire(-commitToIEWDelay);
1745569SN/A}
1753536SN/A
1763536SN/A// Might want to do something more complex if it knows how many instructions
1773536SN/A// will be issued this cycle.
1788782Sgblack@eecs.umich.edutemplate <class Impl>
17910707SAndreas.Sandberg@ARM.combool
18010707SAndreas.Sandberg@ARM.comInstructionQueue<Impl>::isFull()
18110707SAndreas.Sandberg@ARM.com{
18210707SAndreas.Sandberg@ARM.com    if (freeEntries == 0) {
1838782Sgblack@eecs.umich.edu        return(true);
18410707SAndreas.Sandberg@ARM.com    } else {
1858782Sgblack@eecs.umich.edu        return(false);
1868782Sgblack@eecs.umich.edu    }
1878782Sgblack@eecs.umich.edu}
1888782Sgblack@eecs.umich.edu
1898782Sgblack@eecs.umich.edutemplate <class Impl>
1908782Sgblack@eecs.umich.eduunsigned
1918782Sgblack@eecs.umich.eduInstructionQueue<Impl>::numFreeEntries()
1928782Sgblack@eecs.umich.edu{
1933536SN/A    return freeEntries;
1948782Sgblack@eecs.umich.edu}
1958782Sgblack@eecs.umich.edu
1963536SN/Atemplate <class Impl>
1973536SN/Avoid
1983536SN/AInstructionQueue<Impl>::insert(DynInstPtr &new_inst)
19911274Sshingarov@labware.com{
2003536SN/A    // Make sure the instruction is valid
20111274Sshingarov@labware.com    assert(new_inst);
2027752SWilliam.Wang@arm.com
20311274Sshingarov@labware.com    DPRINTF(IQ, "IQ: Adding instruction PC %#x to the IQ.\n",
20411274Sshingarov@labware.com            new_inst->readPC());
20511274Sshingarov@labware.com
20611274Sshingarov@labware.com    // Check if there are any free entries.  Panic if there are none.
20711274Sshingarov@labware.com    // Might want to have this return a fault in the future instead of
2083536SN/A    // panicing.
20911274Sshingarov@labware.com    assert(freeEntries != 0);
21011274Sshingarov@labware.com
21111274Sshingarov@labware.com    // If the IQ currently has nothing in it, then there's a possibility
21211274Sshingarov@labware.com    // that the tail iterator is invalid (might have been pointing at an
21311274Sshingarov@labware.com    // instruction that was retired).  Reset the tail iterator.
2143536SN/A    if (freeEntries == numEntries) {
2153536SN/A        tail = cpu->instList.begin();
2163536SN/A    }
2173536SN/A
21811274Sshingarov@labware.com    // Move the tail iterator.  Instructions may not have been issued
2193536SN/A    // to the IQ, so we may have to increment the iterator more than once.
22011274Sshingarov@labware.com    while ((*tail) != new_inst) {
2217752SWilliam.Wang@arm.com        tail++;
22211274Sshingarov@labware.com
22311274Sshingarov@labware.com        // Make sure the tail iterator points at something legal.
22411274Sshingarov@labware.com        assert(tail != cpu->instList.end());
22511274Sshingarov@labware.com    }
22611274Sshingarov@labware.com
22711274Sshingarov@labware.com
22811274Sshingarov@labware.com    // Decrease the number of free entries.
22911274Sshingarov@labware.com    --freeEntries;
2307752SWilliam.Wang@arm.com
23111274Sshingarov@labware.com    // Look through its source registers (physical regs), and mark any
23211274Sshingarov@labware.com    // dependencies.
23311274Sshingarov@labware.com    addToDependents(new_inst);
23411274Sshingarov@labware.com
23511274Sshingarov@labware.com    // Have this instruction set itself as the producer of its destination
2363536SN/A    // register(s).
2373536SN/A    createDependency(new_inst);
2383536SN/A
23911274Sshingarov@labware.com    // If it's a memory instruction, add it to the memory dependency
24011274Sshingarov@labware.com    // unit.
2413536SN/A    if (new_inst->isMemRef()) {
24211274Sshingarov@labware.com        memDepUnit.insert(new_inst);
24311274Sshingarov@labware.com        // Uh..forgot to look it up and put it on the proper dependency list
24411274Sshingarov@labware.com        // if the instruction should not go yet.
24511274Sshingarov@labware.com    } else {
24611274Sshingarov@labware.com        // If the instruction is ready then add it to the ready list.
24711274Sshingarov@labware.com        addIfReady(new_inst);
24811274Sshingarov@labware.com    }
24911274Sshingarov@labware.com
25011274Sshingarov@labware.com    ++iqInstsAdded;
25111274Sshingarov@labware.com
25211274Sshingarov@labware.com    assert(freeEntries == (numEntries - countInsts()));
25311274Sshingarov@labware.com}
25411274Sshingarov@labware.com
25511274Sshingarov@labware.comtemplate <class Impl>
25611274Sshingarov@labware.comvoid
25711274Sshingarov@labware.comInstructionQueue<Impl>::insertNonSpec(DynInstPtr &inst)
25811274Sshingarov@labware.com{
25911274Sshingarov@labware.com    nonSpecInsts[inst->seqNum] = inst;
26011274Sshingarov@labware.com
26111274Sshingarov@labware.com    // @todo: Clean up this code; can do it by setting inst as unable
26211274Sshingarov@labware.com    // to issue, then calling normal insert on the inst.
26311274Sshingarov@labware.com
26411274Sshingarov@labware.com    // Make sure the instruction is valid
26511274Sshingarov@labware.com    assert(inst);
2663536SN/A
2673536SN/A    DPRINTF(IQ, "IQ: Adding instruction PC %#x to the IQ.\n",
26811274Sshingarov@labware.com            inst->readPC());
26911274Sshingarov@labware.com
27011274Sshingarov@labware.com    // Check if there are any free entries.  Panic if there are none.
27111274Sshingarov@labware.com    // Might want to have this return a fault in the future instead of
27211274Sshingarov@labware.com    // panicing.
27311274Sshingarov@labware.com    assert(freeEntries != 0);
27411274Sshingarov@labware.com
27511274Sshingarov@labware.com    // If the IQ currently has nothing in it, then there's a possibility
27611274Sshingarov@labware.com    // that the tail iterator is invalid (might have been pointing at an
27711274Sshingarov@labware.com    // instruction that was retired).  Reset the tail iterator.
27811274Sshingarov@labware.com    if (freeEntries == numEntries) {
27911274Sshingarov@labware.com        tail = cpu->instList.begin();
28011274Sshingarov@labware.com    }
28111274Sshingarov@labware.com
28211274Sshingarov@labware.com    // Move the tail iterator.  Instructions may not have been issued
28311274Sshingarov@labware.com    // to the IQ, so we may have to increment the iterator more than once.
28411274Sshingarov@labware.com    while ((*tail) != inst) {
28511274Sshingarov@labware.com        tail++;
28611274Sshingarov@labware.com
28711274Sshingarov@labware.com        // Make sure the tail iterator points at something legal.
28811274Sshingarov@labware.com        assert(tail != cpu->instList.end());
28911274Sshingarov@labware.com    }
29011274Sshingarov@labware.com
29111274Sshingarov@labware.com    // Decrease the number of free entries.
29211274Sshingarov@labware.com    --freeEntries;
29311274Sshingarov@labware.com
29411274Sshingarov@labware.com    // Look through its source registers (physical regs), and mark any
29511274Sshingarov@labware.com    // dependencies.
29611274Sshingarov@labware.com//    addToDependents(inst);
29711274Sshingarov@labware.com
29811274Sshingarov@labware.com    // Have this instruction set itself as the producer of its destination
29911274Sshingarov@labware.com    // register(s).
30012031Sgabeblack@google.com    createDependency(inst);
30111274Sshingarov@labware.com
30212031Sgabeblack@google.com    // If it's a memory instruction, add it to the memory dependency
30311274Sshingarov@labware.com    // unit.
304    if (inst->isMemRef()) {
305        memDepUnit.insertNonSpec(inst);
306    }
307
308    ++iqNonSpecInstsAdded;
309}
310
311// Slightly hack function to advance the tail iterator in the case that
312// the IEW stage issues an instruction that is not added to the IQ.  This
313// is needed in case a long chain of such instructions occurs.
314// I don't think this is used anymore.
315template <class Impl>
316void
317InstructionQueue<Impl>::advanceTail(DynInstPtr &inst)
318{
319    // Make sure the instruction is valid
320    assert(inst);
321
322    DPRINTF(IQ, "IQ: Adding instruction PC %#x to the IQ.\n",
323            inst->readPC());
324
325    // Check if there are any free entries.  Panic if there are none.
326    // Might want to have this return a fault in the future instead of
327    // panicing.
328    assert(freeEntries != 0);
329
330    // If the IQ currently has nothing in it, then there's a possibility
331    // that the tail iterator is invalid (might have been pointing at an
332    // instruction that was retired).  Reset the tail iterator.
333    if (freeEntries == numEntries) {
334        tail = cpu->instList.begin();
335    }
336
337    // Move the tail iterator.  Instructions may not have been issued
338    // to the IQ, so we may have to increment the iterator more than once.
339    while ((*tail) != inst) {
340        tail++;
341
342        // Make sure the tail iterator points at something legal.
343        assert(tail != cpu->instList.end());
344    }
345
346    assert(freeEntries <= numEntries);
347
348    // Have this instruction set itself as the producer of its destination
349    // register(s).
350    createDependency(inst);
351}
352
353// Need to make sure the number of float and integer instructions
354// issued does not exceed the total issue bandwidth.
355// @todo: Figure out a better way to remove the squashed items from the
356// lists.  Checking the top item of each list to see if it's squashed
357// wastes time and forces jumps.
358template <class Impl>
359void
360InstructionQueue<Impl>::scheduleReadyInsts()
361{
362    DPRINTF(IQ, "IQ: Attempting to schedule ready instructions from "
363                "the IQ.\n");
364
365    int int_issued = 0;
366    int float_issued = 0;
367    int branch_issued = 0;
368    int memory_issued = 0;
369    int squashed_issued = 0;
370    int total_issued = 0;
371
372    IssueStruct *i2e_info = issueToExecuteQueue->access(0);
373
374    bool insts_available = !readyBranchInsts.empty() ||
375        !readyIntInsts.empty() ||
376        !readyFloatInsts.empty() ||
377        !memDepUnit.empty() ||
378        !readyMiscInsts.empty() ||
379        !squashedInsts.empty();
380
381    // Note: Requires a globally defined constant.
382    InstSeqNum oldest_inst = MaxInstSeqNum;
383    InstList list_with_oldest = None;
384
385    // Temporary values.
386    DynInstPtr int_head_inst;
387    DynInstPtr float_head_inst;
388    DynInstPtr branch_head_inst;
389    DynInstPtr mem_head_inst;
390    DynInstPtr misc_head_inst;
391    DynInstPtr squashed_head_inst;
392
393    // Somewhat nasty code to look at all of the lists where issuable
394    // instructions are located, and choose the oldest instruction among
395    // those lists.  Consider a rewrite in the future.
396    while (insts_available && total_issued < totalWidth)
397    {
398        // Set this to false.  Each if-block is required to set it to true
399        // if there were instructions available this check.  This will cause
400        // this loop to run once more than necessary, but avoids extra calls.
401        insts_available = false;
402
403        oldest_inst = MaxInstSeqNum;
404
405        list_with_oldest = None;
406
407        if (!readyIntInsts.empty() &&
408            int_issued < intWidth) {
409
410            insts_available = true;
411
412            int_head_inst = readyIntInsts.top();
413
414            if (int_head_inst->isSquashed()) {
415                readyIntInsts.pop();
416
417                ++iqLoopSquashStalls;
418
419                continue;
420            }
421
422            oldest_inst = int_head_inst->seqNum;
423
424            list_with_oldest = Int;
425        }
426
427        if (!readyFloatInsts.empty() &&
428            float_issued < floatWidth) {
429
430            insts_available = true;
431
432            float_head_inst = readyFloatInsts.top();
433
434            if (float_head_inst->isSquashed()) {
435                readyFloatInsts.pop();
436
437                ++iqLoopSquashStalls;
438
439                continue;
440            } else if (float_head_inst->seqNum < oldest_inst) {
441                oldest_inst = float_head_inst->seqNum;
442
443                list_with_oldest = Float;
444            }
445        }
446
447        if (!readyBranchInsts.empty() &&
448            branch_issued < branchWidth) {
449
450            insts_available = true;
451
452            branch_head_inst = readyBranchInsts.top();
453
454            if (branch_head_inst->isSquashed()) {
455                readyBranchInsts.pop();
456
457                ++iqLoopSquashStalls;
458
459                continue;
460            } else if (branch_head_inst->seqNum < oldest_inst) {
461                oldest_inst = branch_head_inst->seqNum;
462
463                list_with_oldest = Branch;
464            }
465
466        }
467
468        if (!memDepUnit.empty() &&
469            memory_issued < memoryWidth) {
470
471            insts_available = true;
472
473            mem_head_inst = memDepUnit.top();
474
475            if (mem_head_inst->isSquashed()) {
476                memDepUnit.pop();
477
478                ++iqLoopSquashStalls;
479
480                continue;
481            } else if (mem_head_inst->seqNum < oldest_inst) {
482                oldest_inst = mem_head_inst->seqNum;
483
484                list_with_oldest = Memory;
485            }
486        }
487
488        if (!readyMiscInsts.empty()) {
489
490            insts_available = true;
491
492            misc_head_inst = readyMiscInsts.top();
493
494            if (misc_head_inst->isSquashed()) {
495                readyMiscInsts.pop();
496
497                ++iqLoopSquashStalls;
498
499                continue;
500            } else if (misc_head_inst->seqNum < oldest_inst) {
501                oldest_inst = misc_head_inst->seqNum;
502
503                list_with_oldest = Misc;
504            }
505        }
506
507        if (!squashedInsts.empty()) {
508
509            insts_available = true;
510
511            squashed_head_inst = squashedInsts.top();
512
513            if (squashed_head_inst->seqNum < oldest_inst) {
514                list_with_oldest = Squashed;
515            }
516
517        }
518
519        DynInstPtr issuing_inst = NULL;
520
521        switch (list_with_oldest) {
522          case None:
523            DPRINTF(IQ, "IQ: Not able to schedule any instructions. Issuing "
524                    "inst is %#x.\n", issuing_inst);
525            break;
526
527          case Int:
528            issuing_inst = int_head_inst;
529            readyIntInsts.pop();
530            ++int_issued;
531            DPRINTF(IQ, "IQ: Issuing integer instruction PC %#x.\n",
532                    issuing_inst->readPC());
533            break;
534
535          case Float:
536            issuing_inst = float_head_inst;
537            readyFloatInsts.pop();
538            ++float_issued;
539            DPRINTF(IQ, "IQ: Issuing float instruction PC %#x.\n",
540                    issuing_inst->readPC());
541            break;
542
543          case Branch:
544            issuing_inst = branch_head_inst;
545            readyBranchInsts.pop();
546            ++branch_issued;
547            DPRINTF(IQ, "IQ: Issuing branch instruction PC %#x.\n",
548                    issuing_inst->readPC());
549            break;
550
551          case Memory:
552            issuing_inst = mem_head_inst;
553
554            memDepUnit.pop();
555            ++memory_issued;
556            DPRINTF(IQ, "IQ: Issuing memory instruction PC %#x.\n",
557                    issuing_inst->readPC());
558            break;
559
560          case Misc:
561            issuing_inst = misc_head_inst;
562            readyMiscInsts.pop();
563
564            ++iqMiscInstsIssued;
565
566            DPRINTF(IQ, "IQ: Issuing a miscellaneous instruction PC %#x.\n",
567                    issuing_inst->readPC());
568            break;
569
570          case Squashed:
571            issuing_inst = squashed_head_inst;
572            squashedInsts.pop();
573            ++squashed_issued;
574            DPRINTF(IQ, "IQ: Issuing squashed instruction PC %#x.\n",
575                    issuing_inst->readPC());
576            break;
577        }
578
579        if (list_with_oldest != None) {
580            i2e_info->insts[total_issued] = issuing_inst;
581            i2e_info->size++;
582
583            issuing_inst->setIssued();
584
585            ++freeEntries;
586            ++total_issued;
587        }
588
589        assert(freeEntries == (numEntries - countInsts()));
590    }
591
592    iqIntInstsIssued += int_issued;
593    iqFloatInstsIssued += float_issued;
594    iqBranchInstsIssued += branch_issued;
595    iqMemInstsIssued += memory_issued;
596    iqSquashedInstsIssued += squashed_issued;
597}
598
599template <class Impl>
600void
601InstructionQueue<Impl>::scheduleNonSpec(const InstSeqNum &inst)
602{
603    DPRINTF(IQ, "IQ: Marking nonspeculative instruction with sequence "
604            "number %i as ready to execute.\n", inst);
605
606    non_spec_it_t inst_it = nonSpecInsts.find(inst);
607
608    assert(inst_it != nonSpecInsts.end());
609
610    // Mark this instruction as ready to issue.
611    (*inst_it).second->setCanIssue();
612
613    // Now schedule the instruction.
614    if (!(*inst_it).second->isMemRef()) {
615        addIfReady((*inst_it).second);
616    } else {
617        memDepUnit.nonSpecInstReady((*inst_it).second);
618    }
619
620    nonSpecInsts.erase(inst_it);
621}
622
623template <class Impl>
624void
625InstructionQueue<Impl>::violation(DynInstPtr &store,
626                                  DynInstPtr &faulting_load)
627{
628    memDepUnit.violation(store, faulting_load);
629}
630
631template <class Impl>
632void
633InstructionQueue<Impl>::squash()
634{
635    DPRINTF(IQ, "IQ: Starting to squash instructions in the IQ.\n");
636
637    // Read instruction sequence number of last instruction out of the
638    // time buffer.
639    squashedSeqNum = fromCommit->commitInfo.doneSeqNum;
640
641    // Setup the squash iterator to point to the tail.
642    squashIt = tail;
643
644    // Call doSquash.
645    doSquash();
646
647    // Also tell the memory dependence unit to squash.
648    memDepUnit.squash(squashedSeqNum);
649}
650
651template <class Impl>
652void
653InstructionQueue<Impl>::doSquash()
654{
655    // Make sure the squash iterator isn't pointing to nothing.
656    assert(squashIt != cpu->instList.end());
657    // Make sure the squashed sequence number is valid.
658    assert(squashedSeqNum != 0);
659
660    DPRINTF(IQ, "IQ: Squashing instructions in the IQ.\n");
661
662    // Squash any instructions younger than the squashed sequence number
663    // given.
664    while ((*squashIt)->seqNum > squashedSeqNum) {
665        DynInstPtr squashed_inst = (*squashIt);
666
667        // Only handle the instruction if it actually is in the IQ and
668        // hasn't already been squashed in the IQ.
669        if (!squashed_inst->isIssued() &&
670            !squashed_inst->isSquashedInIQ()) {
671
672            // Remove the instruction from the dependency list.
673            // Hack for now: These below don't add themselves to the
674            // dependency list, so don't try to remove them.
675            if (!squashed_inst->isNonSpeculative() &&
676                !squashed_inst->isStore()) {
677                int8_t total_src_regs = squashed_inst->numSrcRegs();
678
679                for (int src_reg_idx = 0;
680                     src_reg_idx < total_src_regs;
681                     src_reg_idx++)
682                {
683                    PhysRegIndex src_reg =
684                        squashed_inst->renamedSrcRegIdx(src_reg_idx);
685
686                    // Only remove it from the dependency graph if it was
687                    // placed there in the first place.
688                    // HACK: This assumes that instructions woken up from the
689                    // dependency chain aren't informed that a specific src
690                    // register has become ready.  This may not always be true
691                    // in the future.
692                    if (!squashed_inst->isReadySrcRegIdx(src_reg_idx) &&
693                        src_reg < numPhysRegs) {
694                        dependGraph[src_reg].remove(squashed_inst);
695                    }
696
697                    ++iqSquashedOperandsExamined;
698                }
699
700                // Might want to remove producers as well.
701            } else {
702                nonSpecInsts.erase(squashed_inst->seqNum);
703
704                ++iqSquashedNonSpecRemoved;
705            }
706
707            // Might want to also clear out the head of the dependency graph.
708
709            // Mark it as squashed within the IQ.
710            squashed_inst->setSquashedInIQ();
711
712            squashedInsts.push(squashed_inst);
713
714            DPRINTF(IQ, "IQ: Instruction PC %#x squashed.\n",
715                    squashed_inst->readPC());
716        }
717
718        --squashIt;
719        ++iqSquashedInstsExamined;
720    }
721}
722
723template <class Impl>
724void
725InstructionQueue<Impl>::stopSquash()
726{
727    // Clear up the squash variables to ensure that squashing doesn't
728    // get called improperly.
729    squashedSeqNum = 0;
730
731    squashIt = cpu->instList.end();
732}
733
734template <class Impl>
735void
736InstructionQueue<Impl>::wakeDependents(DynInstPtr &completed_inst)
737{
738    DPRINTF(IQ, "IQ: Waking dependents of completed instruction.\n");
739    //Look at the physical destination register of the DynInst
740    //and look it up on the dependency graph.  Then mark as ready
741    //any instructions within the instruction queue.
742    int8_t total_dest_regs = completed_inst->numDestRegs();
743
744    DependencyEntry *curr;
745
746    // Tell the memory dependence unit to wake any dependents on this
747    // instruction if it is a memory instruction.
748
749    if (completed_inst->isMemRef()) {
750        memDepUnit.wakeDependents(completed_inst);
751    }
752
753    for (int dest_reg_idx = 0;
754         dest_reg_idx < total_dest_regs;
755         dest_reg_idx++)
756    {
757        PhysRegIndex dest_reg =
758            completed_inst->renamedDestRegIdx(dest_reg_idx);
759
760        // Special case of uniq or control registers.  They are not
761        // handled by the IQ and thus have no dependency graph entry.
762        // @todo Figure out a cleaner way to handle thie.
763        if (dest_reg >= numPhysRegs) {
764            continue;
765        }
766
767        DPRINTF(IQ, "IQ: Waking any dependents on register %i.\n",
768                (int) dest_reg);
769
770        //Maybe abstract this part into a function.
771        //Go through the dependency chain, marking the registers as ready
772        //within the waiting instructions.
773        while (dependGraph[dest_reg].next) {
774
775            curr = dependGraph[dest_reg].next;
776
777            DPRINTF(IQ, "IQ: Waking up a dependent instruction, PC%#x.\n",
778                    curr->inst->readPC());
779
780            // Might want to give more information to the instruction
781            // so that it knows which of its source registers is ready.
782            // However that would mean that the dependency graph entries
783            // would need to hold the src_reg_idx.
784            curr->inst->markSrcRegReady();
785
786            addIfReady(curr->inst);
787
788            dependGraph[dest_reg].next = curr->next;
789
790            DependencyEntry::mem_alloc_counter--;
791
792            delete curr;
793        }
794
795        // Reset the head node now that all of its dependents have been woken
796        // up.
797        dependGraph[dest_reg].next = NULL;
798        dependGraph[dest_reg].inst = NULL;
799
800        // Mark the scoreboard as having that register ready.
801        regScoreboard[dest_reg] = true;
802    }
803}
804
805template <class Impl>
806bool
807InstructionQueue<Impl>::addToDependents(DynInstPtr &new_inst)
808{
809    // Loop through the instruction's source registers, adding
810    // them to the dependency list if they are not ready.
811    int8_t total_src_regs = new_inst->numSrcRegs();
812    bool return_val = false;
813
814    for (int src_reg_idx = 0;
815         src_reg_idx < total_src_regs;
816         src_reg_idx++)
817    {
818        // Only add it to the dependency graph if it's not ready.
819        if (!new_inst->isReadySrcRegIdx(src_reg_idx)) {
820            PhysRegIndex src_reg = new_inst->renamedSrcRegIdx(src_reg_idx);
821
822            // Check the IQ's scoreboard to make sure the register
823            // hasn't become ready while the instruction was in flight
824            // between stages.  Only if it really isn't ready should
825            // it be added to the dependency graph.
826            if (src_reg >= numPhysRegs) {
827                continue;
828            } else if (regScoreboard[src_reg] == false) {
829                DPRINTF(IQ, "IQ: Instruction PC %#x has src reg %i that "
830                        "is being added to the dependency chain.\n",
831                        new_inst->readPC(), src_reg);
832
833                dependGraph[src_reg].insert(new_inst);
834
835                // Change the return value to indicate that something
836                // was added to the dependency graph.
837                return_val = true;
838            } else {
839                DPRINTF(IQ, "IQ: Instruction PC %#x has src reg %i that "
840                        "became ready before it reached the IQ.\n",
841                        new_inst->readPC(), src_reg);
842                // Mark a register ready within the instruction.
843                new_inst->markSrcRegReady();
844            }
845        }
846    }
847
848    return return_val;
849}
850
851template <class Impl>
852void
853InstructionQueue<Impl>::createDependency(DynInstPtr &new_inst)
854{
855    //Actually nothing really needs to be marked when an
856    //instruction becomes the producer of a register's value,
857    //but for convenience a ptr to the producing instruction will
858    //be placed in the head node of the dependency links.
859    int8_t total_dest_regs = new_inst->numDestRegs();
860
861    for (int dest_reg_idx = 0;
862         dest_reg_idx < total_dest_regs;
863         dest_reg_idx++)
864    {
865        PhysRegIndex dest_reg = new_inst->renamedDestRegIdx(dest_reg_idx);
866
867        // Instructions that use the misc regs will have a reg number
868        // higher than the normal physical registers.  In this case these
869        // registers are not renamed, and there is no need to track
870        // dependencies as these instructions must be executed at commit.
871        if (dest_reg >= numPhysRegs) {
872            continue;
873        }
874
875        dependGraph[dest_reg].inst = new_inst;
876
877        assert(!dependGraph[dest_reg].next);
878
879        // Mark the scoreboard to say it's not yet ready.
880        regScoreboard[dest_reg] = false;
881    }
882}
883
884template <class Impl>
885void
886InstructionQueue<Impl>::DependencyEntry::insert(DynInstPtr &new_inst)
887{
888    //Add this new, dependent instruction at the head of the dependency
889    //chain.
890
891    // First create the entry that will be added to the head of the
892    // dependency chain.
893    DependencyEntry *new_entry = new DependencyEntry;
894    new_entry->next = this->next;
895    new_entry->inst = new_inst;
896
897    // Then actually add it to the chain.
898    this->next = new_entry;
899
900    ++mem_alloc_counter;
901}
902
903template <class Impl>
904void
905InstructionQueue<Impl>::DependencyEntry::remove(DynInstPtr &inst_to_remove)
906{
907    DependencyEntry *prev = this;
908    DependencyEntry *curr = this->next;
909
910    // Make sure curr isn't NULL.  Because this instruction is being
911    // removed from a dependency list, it must have been placed there at
912    // an earlier time.  The dependency chain should not be empty,
913    // unless the instruction dependent upon it is already ready.
914    if (curr == NULL) {
915        return;
916    }
917
918    // Find the instruction to remove within the dependency linked list.
919    while(curr->inst != inst_to_remove)
920    {
921        prev = curr;
922        curr = curr->next;
923
924        assert(curr != NULL);
925    }
926
927    // Now remove this instruction from the list.
928    prev->next = curr->next;
929
930    --mem_alloc_counter;
931
932    delete curr;
933}
934
935template <class Impl>
936void
937InstructionQueue<Impl>::dumpDependGraph()
938{
939    DependencyEntry *curr;
940
941    for (int i = 0; i < numPhysRegs; ++i)
942    {
943        curr = &dependGraph[i];
944
945        if (curr->inst) {
946            cprintf("dependGraph[%i]: producer: %#x consumer: ", i,
947                    curr->inst->readPC());
948        } else {
949            cprintf("dependGraph[%i]: No producer. consumer: ", i);
950        }
951
952        while (curr->next != NULL) {
953            curr = curr->next;
954
955            cprintf("%#x ", curr->inst->readPC());
956        }
957
958        cprintf("\n");
959    }
960}
961
962template <class Impl>
963void
964InstructionQueue<Impl>::addIfReady(DynInstPtr &inst)
965{
966    //If the instruction now has all of its source registers
967    // available, then add it to the list of ready instructions.
968    if (inst->readyToIssue()) {
969
970        //Add the instruction to the proper ready list.
971        if (inst->isControl()) {
972
973            DPRINTF(IQ, "IQ: Branch instruction is ready to issue, "
974                    "putting it onto the ready list, PC %#x.\n",
975                    inst->readPC());
976            readyBranchInsts.push(inst);
977
978        } else if (inst->isMemRef()) {
979
980            DPRINTF(IQ, "IQ: Checking if memory instruction can issue.\n");
981
982            // Message to the mem dependence unit that this instruction has
983            // its registers ready.
984
985            memDepUnit.regsReady(inst);
986
987#if 0
988            if (memDepUnit.readyToIssue(inst)) {
989                DPRINTF(IQ, "IQ: Memory instruction is ready to issue, "
990                        "putting it onto the ready list, PC %#x.\n",
991                        inst->readPC());
992                readyMemInsts.push(inst);
993            } else {
994                // Make dependent on the store.
995                // Will need some way to get the store instruction it should
996                // be dependent upon; then when the store issues it can
997                // put the instruction on the ready list.
998                // Yet another tree?
999                assert(0 && "Instruction has no way to actually issue");
1000            }
1001#endif
1002
1003        } else if (inst->isInteger()) {
1004
1005            DPRINTF(IQ, "IQ: Integer instruction is ready to issue, "
1006                    "putting it onto the ready list, PC %#x.\n",
1007                    inst->readPC());
1008            readyIntInsts.push(inst);
1009
1010        } else if (inst->isFloating()) {
1011
1012            DPRINTF(IQ, "IQ: Floating instruction is ready to issue, "
1013                    "putting it onto the ready list, PC %#x.\n",
1014                    inst->readPC());
1015            readyFloatInsts.push(inst);
1016
1017        } else {
1018            DPRINTF(IQ, "IQ: Miscellaneous instruction is ready to issue, "
1019                    "putting it onto the ready list, PC %#x..\n",
1020                    inst->readPC());
1021
1022            readyMiscInsts.push(inst);
1023        }
1024    }
1025}
1026
1027template <class Impl>
1028int
1029InstructionQueue<Impl>::countInsts()
1030{
1031    ListIt count_it = cpu->instList.begin();
1032    int total_insts = 0;
1033
1034    while (count_it != tail) {
1035        if (!(*count_it)->isIssued()) {
1036            ++total_insts;
1037        }
1038
1039        ++count_it;
1040
1041        assert(count_it != cpu->instList.end());
1042    }
1043
1044    // Need to count the tail iterator as well.
1045    if (count_it != cpu->instList.end() &&
1046        (*count_it) &&
1047        !(*count_it)->isIssued()) {
1048        ++total_insts;
1049    }
1050
1051    return total_insts;
1052}
1053
1054template <class Impl>
1055void
1056InstructionQueue<Impl>::dumpLists()
1057{
1058    cprintf("Ready integer list size: %i\n", readyIntInsts.size());
1059
1060    cprintf("Ready float list size: %i\n", readyFloatInsts.size());
1061
1062    cprintf("Ready branch list size: %i\n", readyBranchInsts.size());
1063
1064//    cprintf("Ready memory list size: %i\n", readyMemInsts.size());
1065
1066    cprintf("Ready misc list size: %i\n", readyMiscInsts.size());
1067
1068    cprintf("Squashed list size: %i\n", squashedInsts.size());
1069
1070    cprintf("Non speculative list size: %i\n", nonSpecInsts.size());
1071
1072    non_spec_it_t non_spec_it = nonSpecInsts.begin();
1073
1074    cprintf("Non speculative list: ");
1075
1076    while (non_spec_it != nonSpecInsts.end()) {
1077        cprintf("%#x ", (*non_spec_it).second->readPC());
1078        ++non_spec_it;
1079    }
1080
1081    cprintf("\n");
1082
1083}
1084
1085#endif // __INST_QUEUE_IMPL_HH__
1086