inst_queue_impl.hh revision 1062
12124SN/A#ifndef __INST_QUEUE_IMPL_HH__
22124SN/A#define __INST_QUEUE_IMPL_HH__
32124SN/A
42124SN/A// Todo:
52022SN/A// Current ordering allows for 0 cycle added-to-scheduled.  Could maybe fake
62124SN/A// it; either do in reverse order, or have added instructions put into a
72124SN/A// different ready queue that, in scheduleRreadyInsts(), gets put onto the
82124SN/A// normal ready queue.  This would however give only a one cycle delay,
92124SN/A// but probably is more flexible to actually add in a delay parameter than
102124SN/A// just running it backwards.
112124SN/A
122124SN/A#include <vector>
132124SN/A
142124SN/A#include "sim/universe.hh"
152124SN/A#include "cpu/beta_cpu/inst_queue.hh"
162022SN/A
172124SN/A// Either compile error or max int due to sign extension.
182124SN/A// Blatant hack to avoid compile warnings.
192124SN/Aconst InstSeqNum MaxInstSeqNum = 0 - 1;
202124SN/A
212124SN/Atemplate <class Impl>
222124SN/AInstructionQueue<Impl>::InstructionQueue(Params &params)
232124SN/A    : memDepUnit(params),
242124SN/A      numEntries(params.numIQEntries),
252124SN/A      intWidth(params.executeIntWidth),
262124SN/A      floatWidth(params.executeFloatWidth),
272124SN/A      branchWidth(params.executeBranchWidth),
282665Ssaidi@eecs.umich.edu      memoryWidth(params.executeMemoryWidth),
292665Ssaidi@eecs.umich.edu      totalWidth(params.issueWidth),
302665Ssaidi@eecs.umich.edu      numPhysIntRegs(params.numPhysIntRegs),
312022SN/A      numPhysFloatRegs(params.numPhysFloatRegs),
322649Ssaidi@eecs.umich.edu      commitToIEWDelay(params.commitToIEWDelay)
332649Ssaidi@eecs.umich.edu{
342649Ssaidi@eecs.umich.edu    DPRINTF(IQ, "IQ: Int width is %i.\n", params.executeIntWidth);
352649Ssaidi@eecs.umich.edu
362649Ssaidi@eecs.umich.edu    // Initialize the number of free IQ entries.
372022SN/A    freeEntries = numEntries;
382124SN/A
392124SN/A    // Set the number of physical registers as the number of int + float
402124SN/A    numPhysRegs = numPhysIntRegs + numPhysFloatRegs;
412124SN/A
422124SN/A    DPRINTF(IQ, "IQ: There are %i physical registers.\n", numPhysRegs);
432124SN/A
442124SN/A    //Create an entry for each physical register within the
452124SN/A    //dependency graph.
462124SN/A    dependGraph = new DependencyEntry[numPhysRegs];
472124SN/A
482124SN/A    // Resize the register scoreboard.
492124SN/A    regScoreboard.resize(numPhysRegs);
502124SN/A
512239SN/A    // Initialize all the head pointers to point to NULL, and all the
522124SN/A    // entries as unready.
532124SN/A    // Note that in actuality, the registers corresponding to the logical
542124SN/A    // registers start off as ready.  However this doesn't matter for the
552124SN/A    // IQ as the instruction should have been correctly told if those
562124SN/A    // registers are ready in rename.  Thus it can all be initialized as
572124SN/A    // unready.
582124SN/A    for (int i = 0; i < numPhysRegs; ++i)
592124SN/A    {
602124SN/A        dependGraph[i].next = NULL;
612124SN/A        dependGraph[i].inst = NULL;
622022SN/A        regScoreboard[i] = false;
632239SN/A    }
642239SN/A
652239SN/A}
662239SN/A
672239SN/Atemplate <class Impl>
682239SN/Avoid
692124SN/AInstructionQueue<Impl>::regStats()
702022SN/A{
712124SN/A    iqInstsAdded
722124SN/A        .name(name() + ".iqInstsAdded")
732022SN/A        .desc("Number of instructions added to the IQ (excludes non-spec)")
742124SN/A        .prereq(iqInstsAdded);
752124SN/A
762124SN/A    iqNonSpecInstsAdded
772124SN/A        .name(name() + ".iqNonSpecInstsAdded")
782124SN/A        .desc("Number of non-speculative instructions added to the IQ")
792124SN/A        .prereq(iqNonSpecInstsAdded);
802022SN/A
812022SN/A//    iqIntInstsAdded;
822124SN/A
832022SN/A    iqIntInstsIssued
842124SN/A        .name(name() + ".iqIntInstsIssued")
852124SN/A        .desc("Number of integer instructions issued")
862124SN/A        .prereq(iqIntInstsIssued);
872124SN/A
882239SN/A//    iqFloatInstsAdded;
892124SN/A
902124SN/A    iqFloatInstsIssued
912022SN/A        .name(name() + ".iqFloatInstsIssued")
922022SN/A        .desc("Number of float instructions issued")
932124SN/A        .prereq(iqFloatInstsIssued);
942124SN/A
952124SN/A//    iqBranchInstsAdded;
962124SN/A
972124SN/A    iqBranchInstsIssued
982124SN/A        .name(name() + ".iqBranchInstsIssued")
992022SN/A        .desc("Number of branch instructions issued")
1002022SN/A        .prereq(iqBranchInstsIssued);
1012124SN/A
1022124SN/A//    iqMemInstsAdded;
1032124SN/A
1042124SN/A    iqMemInstsIssued
1052124SN/A        .name(name() + ".iqMemInstsIssued")
1062124SN/A        .desc("Number of memory instructions issued")
1072124SN/A        .prereq(iqMemInstsIssued);
1082124SN/A
1092124SN/A//    iqMiscInstsAdded;
1102124SN/A
1112124SN/A    iqMiscInstsIssued
1122124SN/A        .name(name() + ".iqMiscInstsIssued")
1132124SN/A        .desc("Number of miscellaneous instructions issued")
1142124SN/A        .prereq(iqMiscInstsIssued);
1152124SN/A
1162124SN/A    iqSquashedInstsIssued
1172124SN/A        .name(name() + ".iqSquashedInstsIssued")
1182124SN/A        .desc("Number of squashed instructions issued")
1192124SN/A        .prereq(iqSquashedInstsIssued);
1202124SN/A
1212124SN/A    iqLoopSquashStalls
1222124SN/A        .name(name() + ".iqLoopSquashStalls")
1232124SN/A        .desc("Number of times issue loop had to restart due to squashed "
1242124SN/A              "inst; mainly for profiling")
1252124SN/A        .prereq(iqLoopSquashStalls);
1262124SN/A
1272124SN/A    iqSquashedInstsExamined
1282124SN/A        .name(name() + ".iqSquashedInstsExamined")
1292124SN/A        .desc("Number of squashed instructions iterated over during squash;"
1302124SN/A              " mainly for profiling")
1312124SN/A        .prereq(iqSquashedInstsExamined);
1322124SN/A
1332124SN/A    iqSquashedOperandsExamined
1342124SN/A        .name(name() + ".iqSquashedOperandsExamined")
1352124SN/A        .desc("Number of squashed operands that are examined and possibly "
1362124SN/A              "removed from graph")
1372124SN/A        .prereq(iqSquashedOperandsExamined);
1382124SN/A
1392124SN/A    iqSquashedNonSpecRemoved
1402124SN/A        .name(name() + ".iqSquashedNonSpecRemoved")
1412124SN/A        .desc("Number of squashed non-spec instructions that were removed")
1422124SN/A        .prereq(iqSquashedNonSpecRemoved);
1432124SN/A
1442124SN/A    // Tell mem dependence unit to reg stats as well.
1452022SN/A    memDepUnit.regStats();
1462022SN/A}
1472124SN/A
1482124SN/Atemplate <class Impl>
1492132SN/Avoid
1502022SN/AInstructionQueue<Impl>::setCPU(FullCPU *cpu_ptr)
1512124SN/A{
1522124SN/A    cpu = cpu_ptr;
1532124SN/A
1542132SN/A    tail = cpu->instList.begin();
1552124SN/A}
1562124SN/A
1572124SN/Atemplate <class Impl>
1582124SN/Avoid
1592124SN/AInstructionQueue<Impl>::setIssueToExecuteQueue(
1602124SN/A                        TimeBuffer<IssueStruct> *i2e_ptr)
1612124SN/A{
1622124SN/A    DPRINTF(IQ, "IQ: Set the issue to execute queue.\n");
1632124SN/A    issueToExecuteQueue = i2e_ptr;
1642124SN/A}
1652124SN/A
1662124SN/Atemplate <class Impl>
1672124SN/Avoid
1682124SN/AInstructionQueue<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
1692124SN/A{
1702124SN/A    DPRINTF(IQ, "IQ: Set the time buffer.\n");
1712124SN/A    timeBuffer = tb_ptr;
1722124SN/A
1732124SN/A    fromCommit = timeBuffer->getWire(-commitToIEWDelay);
1742124SN/A}
1752124SN/A
1762124SN/A// Might want to do something more complex if it knows how many instructions
1772124SN/A// will be issued this cycle.
1782124SN/Atemplate <class Impl>
1792124SN/Abool
1802124SN/AInstructionQueue<Impl>::isFull()
1812124SN/A{
1822124SN/A    if (freeEntries == 0) {
1832124SN/A        return(true);
1842132SN/A    } else {
1852124SN/A        return(false);
1862124SN/A    }
1872124SN/A}
1882124SN/A
1892132SN/Atemplate <class Impl>
1902124SN/Aunsigned
1912124SN/AInstructionQueue<Impl>::numFreeEntries()
1922124SN/A{
1932124SN/A    return freeEntries;
1942124SN/A}
1952124SN/A
1962124SN/Atemplate <class Impl>
1972124SN/Avoid
1982124SN/AInstructionQueue<Impl>::insert(DynInstPtr &new_inst)
1992124SN/A{
2002124SN/A    // Make sure the instruction is valid
2012124SN/A    assert(new_inst);
2022124SN/A
2032124SN/A    DPRINTF(IQ, "IQ: Adding instruction PC %#x to the IQ.\n",
2042124SN/A            new_inst->readPC());
2052124SN/A
2062132SN/A    // Check if there are any free entries.  Panic if there are none.
2072124SN/A    // Might want to have this return a fault in the future instead of
2082124SN/A    // panicing.
2092124SN/A    assert(freeEntries != 0);
2102239SN/A
2112132SN/A    // If the IQ currently has nothing in it, then there's a possibility
2122239SN/A    // that the tail iterator is invalid (might have been pointing at an
2132239SN/A    // instruction that was retired).  Reset the tail iterator.
2142239SN/A    if (freeEntries == numEntries) {
2152239SN/A        tail = cpu->instList.begin();
2162239SN/A    }
2172239SN/A
2182239SN/A    // Move the tail iterator.  Instructions may not have been issued
2192239SN/A    // to the IQ, so we may have to increment the iterator more than once.
2202239SN/A    while ((*tail) != new_inst) {
2212239SN/A        tail++;
2222239SN/A
2232239SN/A        // Make sure the tail iterator points at something legal.
2242239SN/A        assert(tail != cpu->instList.end());
2252239SN/A    }
2262239SN/A
2272124SN/A
2282124SN/A    // Decrease the number of free entries.
2292124SN/A    --freeEntries;
2302124SN/A
2312124SN/A    // Look through its source registers (physical regs), and mark any
2322124SN/A    // dependencies.
2332132SN/A    addToDependents(new_inst);
2342124SN/A
2352124SN/A    // Have this instruction set itself as the producer of its destination
2362124SN/A    // register(s).
2372132SN/A    createDependency(new_inst);
2382124SN/A
2392124SN/A    // If it's a memory instruction, add it to the memory dependency
2402124SN/A    // unit.
2412124SN/A    if (new_inst->isMemRef()) {
2422124SN/A        memDepUnit.insert(new_inst);
2432124SN/A        // Uh..forgot to look it up and put it on the proper dependency list
2442124SN/A        // if the instruction should not go yet.
2452124SN/A    } else {
2462124SN/A        // If the instruction is ready then add it to the ready list.
2472124SN/A        addIfReady(new_inst);
2482124SN/A    }
2492124SN/A
2502124SN/A    ++iqInstsAdded;
2512124SN/A
2522124SN/A    assert(freeEntries == (numEntries - countInsts()));
2532124SN/A}
2542124SN/A
2552124SN/Atemplate <class Impl>
2562124SN/Avoid
2572124SN/AInstructionQueue<Impl>::insertNonSpec(DynInstPtr &inst)
2582124SN/A{
2592132SN/A    nonSpecInsts[inst->seqNum] = inst;
2602124SN/A
2612124SN/A    // @todo: Clean up this code; can do it by setting inst as unable
2622239SN/A    // to issue, then calling normal insert on the inst.
2632132SN/A
2642239SN/A    // Make sure the instruction is valid
2652239SN/A    assert(inst);
2662239SN/A
2672239SN/A    DPRINTF(IQ, "IQ: Adding instruction PC %#x to the IQ.\n",
2682239SN/A            inst->readPC());
2692239SN/A
2702239SN/A    // Check if there are any free entries.  Panic if there are none.
2712239SN/A    // Might want to have this return a fault in the future instead of
2722239SN/A    // panicing.
2732239SN/A    assert(freeEntries != 0);
2742124SN/A
2752124SN/A    // If the IQ currently has nothing in it, then there's a possibility
2762124SN/A    // that the tail iterator is invalid (might have been pointing at an
2772124SN/A    // instruction that was retired).  Reset the tail iterator.
2782124SN/A    if (freeEntries == numEntries) {
2792124SN/A        tail = cpu->instList.begin();
2802132SN/A    }
2812124SN/A
2822124SN/A    // Move the tail iterator.  Instructions may not have been issued
2832124SN/A    // to the IQ, so we may have to increment the iterator more than once.
2842132SN/A    while ((*tail) != inst) {
2852239SN/A        tail++;
2862239SN/A
2872506SN/A        // Make sure the tail iterator points at something legal.
2882239SN/A        assert(tail != cpu->instList.end());
2892239SN/A    }
2902239SN/A
2912239SN/A    // Decrease the number of free entries.
2922239SN/A    --freeEntries;
2932239SN/A
2942239SN/A    // Look through its source registers (physical regs), and mark any
2952239SN/A    // dependencies.
2962239SN/A//    addToDependents(inst);
2972239SN/A
2982239SN/A    // Have this instruction set itself as the producer of its destination
2992124SN/A    // register(s).
3002124SN/A    createDependency(inst);
3012124SN/A
3022124SN/A    // If it's a memory instruction, add it to the memory dependency
3032124SN/A    // unit.
3042124SN/A    if (inst->isMemRef()) {
3052132SN/A        memDepUnit.insertNonSpec(inst);
3062124SN/A    }
3072124SN/A
3082124SN/A    ++iqNonSpecInstsAdded;
3092239SN/A}
3102132SN/A
3112239SN/A// Slightly hack function to advance the tail iterator in the case that
3122239SN/A// the IEW stage issues an instruction that is not added to the IQ.  This
3132239SN/A// is needed in case a long chain of such instructions occurs.
3142239SN/A// I don't think this is used anymore.
3152239SN/Atemplate <class Impl>
3162239SN/Avoid
3172239SN/AInstructionQueue<Impl>::advanceTail(DynInstPtr &inst)
3182239SN/A{
3192239SN/A    // Make sure the instruction is valid
3202239SN/A    assert(inst);
3212239SN/A
3222239SN/A    DPRINTF(IQ, "IQ: Adding instruction PC %#x to the IQ.\n",
3232239SN/A            inst->readPC());
3242239SN/A
3252239SN/A    // Check if there are any free entries.  Panic if there are none.
3262239SN/A    // Might want to have this return a fault in the future instead of
3272239SN/A    // panicing.
3282239SN/A    assert(freeEntries != 0);
3292239SN/A
3302239SN/A    // If the IQ currently has nothing in it, then there's a possibility
3312239SN/A    // that the tail iterator is invalid (might have been pointing at an
3322239SN/A    // instruction that was retired).  Reset the tail iterator.
3332239SN/A    if (freeEntries == numEntries) {
3342239SN/A        tail = cpu->instList.begin();
3352239SN/A    }
3362124SN/A
3372124SN/A    // Move the tail iterator.  Instructions may not have been issued
3382124SN/A    // to the IQ, so we may have to increment the iterator more than once.
3392124SN/A    while ((*tail) != inst) {
3402124SN/A        tail++;
3412124SN/A
3422132SN/A        // Make sure the tail iterator points at something legal.
3432124SN/A        assert(tail != cpu->instList.end());
3442124SN/A    }
3452124SN/A
3462132SN/A    assert(freeEntries <= numEntries);
3472124SN/A
3482124SN/A    // Have this instruction set itself as the producer of its destination
3492124SN/A    // register(s).
3502124SN/A    createDependency(inst);
3512124SN/A}
3522124SN/A
3532124SN/A// Need to make sure the number of float and integer instructions
3542124SN/A// issued does not exceed the total issue bandwidth.
3552124SN/A// @todo: Figure out a better way to remove the squashed items from the
3562124SN/A// lists.  Checking the top item of each list to see if it's squashed
3572124SN/A// wastes time and forces jumps.
3582124SN/Atemplate <class Impl>
3592124SN/Avoid
3602124SN/AInstructionQueue<Impl>::scheduleReadyInsts()
3612124SN/A{
3622124SN/A    DPRINTF(IQ, "IQ: Attempting to schedule ready instructions from "
3632124SN/A                "the IQ.\n");
3642124SN/A
3652124SN/A    int int_issued = 0;
3662124SN/A    int float_issued = 0;
3672124SN/A    int branch_issued = 0;
3682124SN/A    int memory_issued = 0;
3692124SN/A    int squashed_issued = 0;
3702124SN/A    int total_issued = 0;
3712124SN/A
3722124SN/A    IssueStruct *i2e_info = issueToExecuteQueue->access(0);
3732124SN/A
3742124SN/A    bool insts_available = !readyBranchInsts.empty() ||
3752124SN/A        !readyIntInsts.empty() ||
3762124SN/A        !readyFloatInsts.empty() ||
3772132SN/A        !memDepUnit.empty() ||
3782124SN/A        !readyMiscInsts.empty() ||
3792124SN/A        !squashedInsts.empty();
3802239SN/A
3812132SN/A    // Note: Requires a globally defined constant.
3822239SN/A    InstSeqNum oldest_inst = MaxInstSeqNum;
3832239SN/A    InstList list_with_oldest = None;
3842239SN/A
3852506SN/A    // Temporary values.
3862239SN/A    DynInstPtr int_head_inst;
3872239SN/A    DynInstPtr float_head_inst;
3882239SN/A    DynInstPtr branch_head_inst;
3892239SN/A    DynInstPtr mem_head_inst;
3902239SN/A    DynInstPtr misc_head_inst;
3912239SN/A    DynInstPtr squashed_head_inst;
3922239SN/A
3932239SN/A    // Somewhat nasty code to look at all of the lists where issuable
3942239SN/A    // instructions are located, and choose the oldest instruction among
3952239SN/A    // those lists.  Consider a rewrite in the future.
3962239SN/A    while (insts_available && total_issued < totalWidth)
3972239SN/A    {
3982239SN/A        // Set this to false.  Each if-block is required to set it to true
3992124SN/A        // if there were instructions available this check.  This will cause
4002124SN/A        // this loop to run once more than necessary, but avoids extra calls.
4012124SN/A        insts_available = false;
4022124SN/A
4032124SN/A        oldest_inst = MaxInstSeqNum;
4042124SN/A
4052132SN/A        list_with_oldest = None;
4062124SN/A
4072124SN/A        if (!readyIntInsts.empty() &&
4082124SN/A            int_issued < intWidth) {
4092132SN/A
4102239SN/A            insts_available = true;
4112239SN/A
4122239SN/A            int_head_inst = readyIntInsts.top();
4132239SN/A
4142239SN/A            if (int_head_inst->isSquashed()) {
4152239SN/A                readyIntInsts.pop();
4162239SN/A
4172239SN/A                ++iqLoopSquashStalls;
4182239SN/A
4192239SN/A                continue;
4202239SN/A            }
4212239SN/A
4222239SN/A            oldest_inst = int_head_inst->seqNum;
4232239SN/A
4242239SN/A            list_with_oldest = Int;
4252124SN/A        }
4262124SN/A
4272124SN/A        if (!readyFloatInsts.empty() &&
4282124SN/A            float_issued < floatWidth) {
4292124SN/A
4302124SN/A            insts_available = true;
4312124SN/A
4322124SN/A            float_head_inst = readyFloatInsts.top();
4332124SN/A
4342124SN/A            if (float_head_inst->isSquashed()) {
4352124SN/A                readyFloatInsts.pop();
4362124SN/A
4372124SN/A                ++iqLoopSquashStalls;
4382124SN/A
4392124SN/A                continue;
4402124SN/A            } else if (float_head_inst->seqNum < oldest_inst) {
4412124SN/A                oldest_inst = float_head_inst->seqNum;
4422124SN/A
4432124SN/A                list_with_oldest = Float;
4442124SN/A            }
4452124SN/A        }
4462124SN/A
4472124SN/A        if (!readyBranchInsts.empty() &&
4482124SN/A            branch_issued < branchWidth) {
4492124SN/A
4502124SN/A            insts_available = true;
4512124SN/A
4522124SN/A            branch_head_inst = readyBranchInsts.top();
4532124SN/A
4542124SN/A            if (branch_head_inst->isSquashed()) {
4552124SN/A                readyBranchInsts.pop();
4562124SN/A
4572573SN/A                ++iqLoopSquashStalls;
4582573SN/A
4592573SN/A                continue;
4602573SN/A            } else if (branch_head_inst->seqNum < oldest_inst) {
4612573SN/A                oldest_inst = branch_head_inst->seqNum;
4622573SN/A
4632573SN/A                list_with_oldest = Branch;
4642573SN/A            }
4652573SN/A
4662573SN/A        }
4672573SN/A
4682573SN/A        if (!memDepUnit.empty() &&
4692573SN/A            memory_issued < memoryWidth) {
4702573SN/A
4712573SN/A            insts_available = true;
4722573SN/A
4732573SN/A            mem_head_inst = memDepUnit.top();
4742573SN/A
4752495SN/A            if (mem_head_inst->isSquashed()) {
4762495SN/A                memDepUnit.pop();
4772495SN/A
4782495SN/A                ++iqLoopSquashStalls;
4792495SN/A
4802495SN/A                continue;
4812495SN/A            } 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