inst_queue_impl.hh revision 2731
11689SN/A/*
22326SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
291689SN/A */
301689SN/A
312064SN/A#include <limits>
321060SN/A#include <vector>
331060SN/A
341696SN/A#include "sim/root.hh"
351689SN/A
362292SN/A#include "cpu/o3/fu_pool.hh"
371717SN/A#include "cpu/o3/inst_queue.hh"
381060SN/A
392292SN/Ausing namespace std;
401060SN/A
411061SN/Atemplate <class Impl>
422292SN/AInstructionQueue<Impl>::FUCompletion::FUCompletion(DynInstPtr &_inst,
432292SN/A                                                   int fu_idx,
442292SN/A                                                   InstructionQueue<Impl> *iq_ptr)
452292SN/A    : Event(&mainEventQueue, Stat_Event_Pri),
462326SN/A      inst(_inst), fuIdx(fu_idx), iqPtr(iq_ptr), freeFU(false)
471060SN/A{
482292SN/A    this->setFlags(Event::AutoDelete);
492292SN/A}
502292SN/A
512292SN/Atemplate <class Impl>
522292SN/Avoid
532292SN/AInstructionQueue<Impl>::FUCompletion::process()
542292SN/A{
552326SN/A    iqPtr->processFUCompletion(inst, freeFU ? fuIdx : -1);
562292SN/A    inst = NULL;
572292SN/A}
582292SN/A
592292SN/A
602292SN/Atemplate <class Impl>
612292SN/Aconst char *
622292SN/AInstructionQueue<Impl>::FUCompletion::description()
632292SN/A{
642292SN/A    return "Functional unit completion event";
652292SN/A}
662292SN/A
672292SN/Atemplate <class Impl>
682292SN/AInstructionQueue<Impl>::InstructionQueue(Params *params)
692669Sktlim@umich.edu    : fuPool(params->fuPool),
702292SN/A      numEntries(params->numIQEntries),
712292SN/A      totalWidth(params->issueWidth),
722292SN/A      numPhysIntRegs(params->numPhysIntRegs),
732292SN/A      numPhysFloatRegs(params->numPhysFloatRegs),
742292SN/A      commitToIEWDelay(params->commitToIEWDelay)
752292SN/A{
762292SN/A    assert(fuPool);
772292SN/A
782307SN/A    switchedOut = false;
792307SN/A
802292SN/A    numThreads = params->numberOfThreads;
811060SN/A
821060SN/A    // Set the number of physical registers as the number of int + float
831060SN/A    numPhysRegs = numPhysIntRegs + numPhysFloatRegs;
841060SN/A
852292SN/A    DPRINTF(IQ, "There are %i physical registers.\n", numPhysRegs);
861060SN/A
871060SN/A    //Create an entry for each physical register within the
881060SN/A    //dependency graph.
892326SN/A    dependGraph.resize(numPhysRegs);
901060SN/A
911060SN/A    // Resize the register scoreboard.
921060SN/A    regScoreboard.resize(numPhysRegs);
931060SN/A
942292SN/A    //Initialize Mem Dependence Units
952292SN/A    for (int i = 0; i < numThreads; i++) {
962292SN/A        memDepUnit[i].init(params,i);
972292SN/A        memDepUnit[i].setIQ(this);
981060SN/A    }
991060SN/A
1002307SN/A    resetState();
1012292SN/A
1022292SN/A    string policy = params->smtIQPolicy;
1032292SN/A
1042292SN/A    //Convert string to lowercase
1052292SN/A    std::transform(policy.begin(), policy.end(), policy.begin(),
1062292SN/A                   (int(*)(int)) tolower);
1072292SN/A
1082292SN/A    //Figure out resource sharing policy
1092292SN/A    if (policy == "dynamic") {
1102292SN/A        iqPolicy = Dynamic;
1112292SN/A
1122292SN/A        //Set Max Entries to Total ROB Capacity
1132292SN/A        for (int i = 0; i < numThreads; i++) {
1142292SN/A            maxEntries[i] = numEntries;
1152292SN/A        }
1162292SN/A
1172292SN/A    } else if (policy == "partitioned") {
1182292SN/A        iqPolicy = Partitioned;
1192292SN/A
1202292SN/A        //@todo:make work if part_amt doesnt divide evenly.
1212292SN/A        int part_amt = numEntries / numThreads;
1222292SN/A
1232292SN/A        //Divide ROB up evenly
1242292SN/A        for (int i = 0; i < numThreads; i++) {
1252292SN/A            maxEntries[i] = part_amt;
1262292SN/A        }
1272292SN/A
1282292SN/A        DPRINTF(Fetch, "IQ sharing policy set to Partitioned:"
1292292SN/A                "%i entries per thread.\n",part_amt);
1302292SN/A
1312292SN/A    } else if (policy == "threshold") {
1322292SN/A        iqPolicy = Threshold;
1332292SN/A
1342292SN/A        double threshold =  (double)params->smtIQThreshold / 100;
1352292SN/A
1362292SN/A        int thresholdIQ = (int)((double)threshold * numEntries);
1372292SN/A
1382292SN/A        //Divide up by threshold amount
1392292SN/A        for (int i = 0; i < numThreads; i++) {
1402292SN/A            maxEntries[i] = thresholdIQ;
1412292SN/A        }
1422292SN/A
1432292SN/A        DPRINTF(Fetch, "IQ sharing policy set to Threshold:"
1442292SN/A                "%i entries per thread.\n",thresholdIQ);
1452292SN/A   } else {
1462292SN/A       assert(0 && "Invalid IQ Sharing Policy.Options Are:{Dynamic,"
1472292SN/A              "Partitioned, Threshold}");
1482292SN/A   }
1492292SN/A}
1502292SN/A
1512292SN/Atemplate <class Impl>
1522292SN/AInstructionQueue<Impl>::~InstructionQueue()
1532292SN/A{
1542326SN/A    dependGraph.reset();
1552348SN/A#ifdef DEBUG
1562326SN/A    cprintf("Nodes traversed: %i, removed: %i\n",
1572326SN/A            dependGraph.nodesTraversed, dependGraph.nodesRemoved);
1582348SN/A#endif
1592292SN/A}
1602292SN/A
1612292SN/Atemplate <class Impl>
1622292SN/Astd::string
1632292SN/AInstructionQueue<Impl>::name() const
1642292SN/A{
1652292SN/A    return cpu->name() + ".iq";
1661060SN/A}
1671060SN/A
1681061SN/Atemplate <class Impl>
1691060SN/Avoid
1701062SN/AInstructionQueue<Impl>::regStats()
1711062SN/A{
1722301SN/A    using namespace Stats;
1731062SN/A    iqInstsAdded
1741062SN/A        .name(name() + ".iqInstsAdded")
1751062SN/A        .desc("Number of instructions added to the IQ (excludes non-spec)")
1761062SN/A        .prereq(iqInstsAdded);
1771062SN/A
1781062SN/A    iqNonSpecInstsAdded
1791062SN/A        .name(name() + ".iqNonSpecInstsAdded")
1801062SN/A        .desc("Number of non-speculative instructions added to the IQ")
1811062SN/A        .prereq(iqNonSpecInstsAdded);
1821062SN/A
1832301SN/A    iqInstsIssued
1842301SN/A        .name(name() + ".iqInstsIssued")
1852301SN/A        .desc("Number of instructions issued")
1862301SN/A        .prereq(iqInstsIssued);
1871062SN/A
1881062SN/A    iqIntInstsIssued
1891062SN/A        .name(name() + ".iqIntInstsIssued")
1901062SN/A        .desc("Number of integer instructions issued")
1911062SN/A        .prereq(iqIntInstsIssued);
1921062SN/A
1931062SN/A    iqFloatInstsIssued
1941062SN/A        .name(name() + ".iqFloatInstsIssued")
1951062SN/A        .desc("Number of float instructions issued")
1961062SN/A        .prereq(iqFloatInstsIssued);
1971062SN/A
1981062SN/A    iqBranchInstsIssued
1991062SN/A        .name(name() + ".iqBranchInstsIssued")
2001062SN/A        .desc("Number of branch instructions issued")
2011062SN/A        .prereq(iqBranchInstsIssued);
2021062SN/A
2031062SN/A    iqMemInstsIssued
2041062SN/A        .name(name() + ".iqMemInstsIssued")
2051062SN/A        .desc("Number of memory instructions issued")
2061062SN/A        .prereq(iqMemInstsIssued);
2071062SN/A
2081062SN/A    iqMiscInstsIssued
2091062SN/A        .name(name() + ".iqMiscInstsIssued")
2101062SN/A        .desc("Number of miscellaneous instructions issued")
2111062SN/A        .prereq(iqMiscInstsIssued);
2121062SN/A
2131062SN/A    iqSquashedInstsIssued
2141062SN/A        .name(name() + ".iqSquashedInstsIssued")
2151062SN/A        .desc("Number of squashed instructions issued")
2161062SN/A        .prereq(iqSquashedInstsIssued);
2171062SN/A
2181062SN/A    iqSquashedInstsExamined
2191062SN/A        .name(name() + ".iqSquashedInstsExamined")
2201062SN/A        .desc("Number of squashed instructions iterated over during squash;"
2211062SN/A              " mainly for profiling")
2221062SN/A        .prereq(iqSquashedInstsExamined);
2231062SN/A
2241062SN/A    iqSquashedOperandsExamined
2251062SN/A        .name(name() + ".iqSquashedOperandsExamined")
2261062SN/A        .desc("Number of squashed operands that are examined and possibly "
2271062SN/A              "removed from graph")
2281062SN/A        .prereq(iqSquashedOperandsExamined);
2291062SN/A
2301062SN/A    iqSquashedNonSpecRemoved
2311062SN/A        .name(name() + ".iqSquashedNonSpecRemoved")
2321062SN/A        .desc("Number of squashed non-spec instructions that were removed")
2331062SN/A        .prereq(iqSquashedNonSpecRemoved);
2341062SN/A
2352326SN/A    queueResDist
2362301SN/A        .init(Num_OpClasses, 0, 99, 2)
2372301SN/A        .name(name() + ".IQ:residence:")
2382301SN/A        .desc("cycles from dispatch to issue")
2392301SN/A        .flags(total | pdf | cdf )
2402301SN/A        ;
2412301SN/A    for (int i = 0; i < Num_OpClasses; ++i) {
2422326SN/A        queueResDist.subname(i, opClassStrings[i]);
2432301SN/A    }
2442326SN/A    numIssuedDist
2452307SN/A        .init(0,totalWidth,1)
2462301SN/A        .name(name() + ".ISSUE:issued_per_cycle")
2472301SN/A        .desc("Number of insts issued each cycle")
2482307SN/A        .flags(pdf)
2492301SN/A        ;
2502301SN/A/*
2512301SN/A    dist_unissued
2522301SN/A        .init(Num_OpClasses+2)
2532301SN/A        .name(name() + ".ISSUE:unissued_cause")
2542301SN/A        .desc("Reason ready instruction not issued")
2552301SN/A        .flags(pdf | dist)
2562301SN/A        ;
2572301SN/A    for (int i=0; i < (Num_OpClasses + 2); ++i) {
2582301SN/A        dist_unissued.subname(i, unissued_names[i]);
2592301SN/A    }
2602301SN/A*/
2612326SN/A    statIssuedInstType
2622301SN/A        .init(numThreads,Num_OpClasses)
2632301SN/A        .name(name() + ".ISSUE:FU_type")
2642301SN/A        .desc("Type of FU issued")
2652301SN/A        .flags(total | pdf | dist)
2662301SN/A        ;
2672326SN/A    statIssuedInstType.ysubnames(opClassStrings);
2682301SN/A
2692301SN/A    //
2702301SN/A    //  How long did instructions for a particular FU type wait prior to issue
2712301SN/A    //
2722301SN/A
2732326SN/A    issueDelayDist
2742301SN/A        .init(Num_OpClasses,0,99,2)
2752301SN/A        .name(name() + ".ISSUE:")
2762301SN/A        .desc("cycles from operands ready to issue")
2772301SN/A        .flags(pdf | cdf)
2782301SN/A        ;
2792301SN/A
2802301SN/A    for (int i=0; i<Num_OpClasses; ++i) {
2812301SN/A        stringstream subname;
2822301SN/A        subname << opClassStrings[i] << "_delay";
2832326SN/A        issueDelayDist.subname(i, subname.str());
2842301SN/A    }
2852301SN/A
2862326SN/A    issueRate
2872301SN/A        .name(name() + ".ISSUE:rate")
2882301SN/A        .desc("Inst issue rate")
2892301SN/A        .flags(total)
2902301SN/A        ;
2912326SN/A    issueRate = iqInstsIssued / cpu->numCycles;
2922727Sktlim@umich.edu
2932326SN/A    statFuBusy
2942301SN/A        .init(Num_OpClasses)
2952301SN/A        .name(name() + ".ISSUE:fu_full")
2962301SN/A        .desc("attempts to use FU when none available")
2972301SN/A        .flags(pdf | dist)
2982301SN/A        ;
2992301SN/A    for (int i=0; i < Num_OpClasses; ++i) {
3002326SN/A        statFuBusy.subname(i, opClassStrings[i]);
3012301SN/A    }
3022301SN/A
3032326SN/A    fuBusy
3042301SN/A        .init(numThreads)
3052301SN/A        .name(name() + ".ISSUE:fu_busy_cnt")
3062301SN/A        .desc("FU busy when requested")
3072301SN/A        .flags(total)
3082301SN/A        ;
3092301SN/A
3102326SN/A    fuBusyRate
3112301SN/A        .name(name() + ".ISSUE:fu_busy_rate")
3122301SN/A        .desc("FU busy rate (busy events/executed inst)")
3132301SN/A        .flags(total)
3142301SN/A        ;
3152326SN/A    fuBusyRate = fuBusy / iqInstsIssued;
3162301SN/A
3172292SN/A    for ( int i=0; i < numThreads; i++) {
3182292SN/A        // Tell mem dependence unit to reg stats as well.
3192292SN/A        memDepUnit[i].regStats();
3202292SN/A    }
3211062SN/A}
3221062SN/A
3231062SN/Atemplate <class Impl>
3241062SN/Avoid
3252307SN/AInstructionQueue<Impl>::resetState()
3261060SN/A{
3272307SN/A    //Initialize thread IQ counts
3282307SN/A    for (int i = 0; i <numThreads; i++) {
3292307SN/A        count[i] = 0;
3302307SN/A        instList[i].clear();
3312307SN/A    }
3321060SN/A
3332307SN/A    // Initialize the number of free IQ entries.
3342307SN/A    freeEntries = numEntries;
3352307SN/A
3362307SN/A    // Note that in actuality, the registers corresponding to the logical
3372307SN/A    // registers start off as ready.  However this doesn't matter for the
3382307SN/A    // IQ as the instruction should have been correctly told if those
3392307SN/A    // registers are ready in rename.  Thus it can all be initialized as
3402307SN/A    // unready.
3412307SN/A    for (int i = 0; i < numPhysRegs; ++i) {
3422307SN/A        regScoreboard[i] = false;
3432307SN/A    }
3442307SN/A
3452307SN/A    for (int i = 0; i < numThreads; ++i) {
3462307SN/A        squashedSeqNum[i] = 0;
3472307SN/A    }
3482307SN/A
3492307SN/A    for (int i = 0; i < Num_OpClasses; ++i) {
3502307SN/A        while (!readyInsts[i].empty())
3512307SN/A            readyInsts[i].pop();
3522307SN/A        queueOnList[i] = false;
3532307SN/A        readyIt[i] = listOrder.end();
3542307SN/A    }
3552307SN/A    nonSpecInsts.clear();
3562307SN/A    listOrder.clear();
3571060SN/A}
3581060SN/A
3591061SN/Atemplate <class Impl>
3601060SN/Avoid
3612292SN/AInstructionQueue<Impl>::setActiveThreads(list<unsigned> *at_ptr)
3621060SN/A{
3632292SN/A    DPRINTF(IQ, "Setting active threads list pointer.\n");
3642292SN/A    activeThreads = at_ptr;
3652064SN/A}
3662064SN/A
3672064SN/Atemplate <class Impl>
3682064SN/Avoid
3692292SN/AInstructionQueue<Impl>::setIssueToExecuteQueue(TimeBuffer<IssueStruct> *i2e_ptr)
3702064SN/A{
3712292SN/A    DPRINTF(IQ, "Set the issue to execute queue.\n");
3721060SN/A    issueToExecuteQueue = i2e_ptr;
3731060SN/A}
3741060SN/A
3751061SN/Atemplate <class Impl>
3761060SN/Avoid
3771060SN/AInstructionQueue<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
3781060SN/A{
3792292SN/A    DPRINTF(IQ, "Set the time buffer.\n");
3801060SN/A    timeBuffer = tb_ptr;
3811060SN/A
3821060SN/A    fromCommit = timeBuffer->getWire(-commitToIEWDelay);
3831060SN/A}
3841060SN/A
3851684SN/Atemplate <class Impl>
3862307SN/Avoid
3872307SN/AInstructionQueue<Impl>::switchOut()
3882307SN/A{
3892307SN/A    resetState();
3902326SN/A    dependGraph.reset();
3912307SN/A    switchedOut = true;
3922307SN/A    for (int i = 0; i < numThreads; ++i) {
3932307SN/A        memDepUnit[i].switchOut();
3942307SN/A    }
3952307SN/A}
3962307SN/A
3972307SN/Atemplate <class Impl>
3982307SN/Avoid
3992307SN/AInstructionQueue<Impl>::takeOverFrom()
4002307SN/A{
4012307SN/A    switchedOut = false;
4022307SN/A}
4032307SN/A
4042307SN/Atemplate <class Impl>
4052292SN/Aint
4062292SN/AInstructionQueue<Impl>::entryAmount(int num_threads)
4072292SN/A{
4082292SN/A    if (iqPolicy == Partitioned) {
4092292SN/A        return numEntries / num_threads;
4102292SN/A    } else {
4112292SN/A        return 0;
4122292SN/A    }
4132292SN/A}
4142292SN/A
4152292SN/A
4162292SN/Atemplate <class Impl>
4172292SN/Avoid
4182292SN/AInstructionQueue<Impl>::resetEntries()
4192292SN/A{
4202292SN/A    if (iqPolicy != Dynamic || numThreads > 1) {
4212292SN/A        int active_threads = (*activeThreads).size();
4222292SN/A
4232292SN/A        list<unsigned>::iterator threads  = (*activeThreads).begin();
4242292SN/A        list<unsigned>::iterator list_end = (*activeThreads).end();
4252292SN/A
4262292SN/A        while (threads != list_end) {
4272292SN/A            if (iqPolicy == Partitioned) {
4282292SN/A                maxEntries[*threads++] = numEntries / active_threads;
4292292SN/A            } else if(iqPolicy == Threshold && active_threads == 1) {
4302292SN/A                maxEntries[*threads++] = numEntries;
4312292SN/A            }
4322292SN/A        }
4332292SN/A    }
4342292SN/A}
4352292SN/A
4362292SN/Atemplate <class Impl>
4371684SN/Aunsigned
4381684SN/AInstructionQueue<Impl>::numFreeEntries()
4391684SN/A{
4401684SN/A    return freeEntries;
4411684SN/A}
4421684SN/A
4432292SN/Atemplate <class Impl>
4442292SN/Aunsigned
4452292SN/AInstructionQueue<Impl>::numFreeEntries(unsigned tid)
4462292SN/A{
4472292SN/A    return maxEntries[tid] - count[tid];
4482292SN/A}
4492292SN/A
4501060SN/A// Might want to do something more complex if it knows how many instructions
4511060SN/A// will be issued this cycle.
4521061SN/Atemplate <class Impl>
4531060SN/Abool
4541060SN/AInstructionQueue<Impl>::isFull()
4551060SN/A{
4561060SN/A    if (freeEntries == 0) {
4571060SN/A        return(true);
4581060SN/A    } else {
4591060SN/A        return(false);
4601060SN/A    }
4611060SN/A}
4621060SN/A
4631061SN/Atemplate <class Impl>
4642292SN/Abool
4652292SN/AInstructionQueue<Impl>::isFull(unsigned tid)
4662292SN/A{
4672292SN/A    if (numFreeEntries(tid) == 0) {
4682292SN/A        return(true);
4692292SN/A    } else {
4702292SN/A        return(false);
4712292SN/A    }
4722292SN/A}
4732292SN/A
4742292SN/Atemplate <class Impl>
4752292SN/Abool
4762292SN/AInstructionQueue<Impl>::hasReadyInsts()
4772292SN/A{
4782292SN/A    if (!listOrder.empty()) {
4792292SN/A        return true;
4802292SN/A    }
4812292SN/A
4822292SN/A    for (int i = 0; i < Num_OpClasses; ++i) {
4832292SN/A        if (!readyInsts[i].empty()) {
4842292SN/A            return true;
4852292SN/A        }
4862292SN/A    }
4872292SN/A
4882292SN/A    return false;
4892292SN/A}
4902292SN/A
4912292SN/Atemplate <class Impl>
4921060SN/Avoid
4931061SN/AInstructionQueue<Impl>::insert(DynInstPtr &new_inst)
4941060SN/A{
4951060SN/A    // Make sure the instruction is valid
4961060SN/A    assert(new_inst);
4971060SN/A
4982326SN/A    DPRINTF(IQ, "Adding instruction [sn:%lli] PC %#x to the IQ.\n",
4992326SN/A            new_inst->seqNum, new_inst->readPC());
5001060SN/A
5011060SN/A    assert(freeEntries != 0);
5021060SN/A
5032292SN/A    instList[new_inst->threadNumber].push_back(new_inst);
5041060SN/A
5052064SN/A    --freeEntries;
5061060SN/A
5072292SN/A    new_inst->setInIQ();
5081060SN/A
5091060SN/A    // Look through its source registers (physical regs), and mark any
5101060SN/A    // dependencies.
5111060SN/A    addToDependents(new_inst);
5121060SN/A
5131060SN/A    // Have this instruction set itself as the producer of its destination
5141060SN/A    // register(s).
5152326SN/A    addToProducers(new_inst);
5161060SN/A
5171061SN/A    if (new_inst->isMemRef()) {
5182292SN/A        memDepUnit[new_inst->threadNumber].insert(new_inst);
5191062SN/A    } else {
5201062SN/A        addIfReady(new_inst);
5211061SN/A    }
5221061SN/A
5231062SN/A    ++iqInstsAdded;
5241060SN/A
5252292SN/A    count[new_inst->threadNumber]++;
5262292SN/A
5271060SN/A    assert(freeEntries == (numEntries - countInsts()));
5281060SN/A}
5291060SN/A
5301061SN/Atemplate <class Impl>
5311061SN/Avoid
5322292SN/AInstructionQueue<Impl>::insertNonSpec(DynInstPtr &new_inst)
5331061SN/A{
5341061SN/A    // @todo: Clean up this code; can do it by setting inst as unable
5351061SN/A    // to issue, then calling normal insert on the inst.
5361061SN/A
5372292SN/A    assert(new_inst);
5381061SN/A
5392292SN/A    nonSpecInsts[new_inst->seqNum] = new_inst;
5401061SN/A
5412326SN/A    DPRINTF(IQ, "Adding non-speculative instruction [sn:%lli] PC %#x "
5422326SN/A            "to the IQ.\n",
5432326SN/A            new_inst->seqNum, new_inst->readPC());
5442064SN/A
5451061SN/A    assert(freeEntries != 0);
5461061SN/A
5472292SN/A    instList[new_inst->threadNumber].push_back(new_inst);
5481061SN/A
5492064SN/A    --freeEntries;
5501061SN/A
5512292SN/A    new_inst->setInIQ();
5521061SN/A
5531061SN/A    // Have this instruction set itself as the producer of its destination
5541061SN/A    // register(s).
5552326SN/A    addToProducers(new_inst);
5561061SN/A
5571061SN/A    // If it's a memory instruction, add it to the memory dependency
5581061SN/A    // unit.
5592292SN/A    if (new_inst->isMemRef()) {
5602292SN/A        memDepUnit[new_inst->threadNumber].insertNonSpec(new_inst);
5611061SN/A    }
5621062SN/A
5631062SN/A    ++iqNonSpecInstsAdded;
5642292SN/A
5652292SN/A    count[new_inst->threadNumber]++;
5662292SN/A
5672292SN/A    assert(freeEntries == (numEntries - countInsts()));
5681061SN/A}
5691061SN/A
5701061SN/Atemplate <class Impl>
5711060SN/Avoid
5722292SN/AInstructionQueue<Impl>::insertBarrier(DynInstPtr &barr_inst)
5731060SN/A{
5742292SN/A    memDepUnit[barr_inst->threadNumber].insertBarrier(barr_inst);
5751060SN/A
5762292SN/A    insertNonSpec(barr_inst);
5772292SN/A}
5781060SN/A
5792064SN/Atemplate <class Impl>
5802333SN/Atypename Impl::DynInstPtr
5812333SN/AInstructionQueue<Impl>::getInstToExecute()
5822333SN/A{
5832333SN/A    assert(!instsToExecute.empty());
5842333SN/A    DynInstPtr inst = instsToExecute.front();
5852333SN/A    instsToExecute.pop_front();
5862333SN/A    return inst;
5872333SN/A}
5881060SN/A
5892333SN/Atemplate <class Impl>
5902064SN/Avoid
5912292SN/AInstructionQueue<Impl>::addToOrderList(OpClass op_class)
5922292SN/A{
5932292SN/A    assert(!readyInsts[op_class].empty());
5942292SN/A
5952292SN/A    ListOrderEntry queue_entry;
5962292SN/A
5972292SN/A    queue_entry.queueType = op_class;
5982292SN/A
5992292SN/A    queue_entry.oldestInst = readyInsts[op_class].top()->seqNum;
6002292SN/A
6012292SN/A    ListOrderIt list_it = listOrder.begin();
6022292SN/A    ListOrderIt list_end_it = listOrder.end();
6032292SN/A
6042292SN/A    while (list_it != list_end_it) {
6052292SN/A        if ((*list_it).oldestInst > queue_entry.oldestInst) {
6062292SN/A            break;
6072292SN/A        }
6082292SN/A
6092292SN/A        list_it++;
6101060SN/A    }
6111060SN/A
6122292SN/A    readyIt[op_class] = listOrder.insert(list_it, queue_entry);
6132292SN/A    queueOnList[op_class] = true;
6142292SN/A}
6151060SN/A
6162292SN/Atemplate <class Impl>
6172292SN/Avoid
6182292SN/AInstructionQueue<Impl>::moveToYoungerInst(ListOrderIt list_order_it)
6192292SN/A{
6202292SN/A    // Get iterator of next item on the list
6212292SN/A    // Delete the original iterator
6222292SN/A    // Determine if the next item is either the end of the list or younger
6232292SN/A    // than the new instruction.  If so, then add in a new iterator right here.
6242292SN/A    // If not, then move along.
6252292SN/A    ListOrderEntry queue_entry;
6262292SN/A    OpClass op_class = (*list_order_it).queueType;
6272292SN/A    ListOrderIt next_it = list_order_it;
6282292SN/A
6292292SN/A    ++next_it;
6302292SN/A
6312292SN/A    queue_entry.queueType = op_class;
6322292SN/A    queue_entry.oldestInst = readyInsts[op_class].top()->seqNum;
6332292SN/A
6342292SN/A    while (next_it != listOrder.end() &&
6352292SN/A           (*next_it).oldestInst < queue_entry.oldestInst) {
6362292SN/A        ++next_it;
6371060SN/A    }
6381060SN/A
6392292SN/A    readyIt[op_class] = listOrder.insert(next_it, queue_entry);
6401060SN/A}
6411060SN/A
6422292SN/Atemplate <class Impl>
6432292SN/Avoid
6442292SN/AInstructionQueue<Impl>::processFUCompletion(DynInstPtr &inst, int fu_idx)
6452292SN/A{
6462292SN/A    // The CPU could have been sleeping until this op completed (*extremely*
6472292SN/A    // long latency op).  Wake it if it was.  This may be overkill.
6482307SN/A    if (isSwitchedOut()) {
6492307SN/A        return;
6502307SN/A    }
6512307SN/A
6522292SN/A    iewStage->wakeCPU();
6532292SN/A
6542326SN/A    if (fu_idx > -1)
6552326SN/A        fuPool->freeUnitNextCycle(fu_idx);
6562292SN/A
6572326SN/A    // @todo: Ensure that these FU Completions happen at the beginning
6582326SN/A    // of a cycle, otherwise they could add too many instructions to
6592326SN/A    // the queue.
6602333SN/A    issueToExecuteQueue->access(0)->size++;
6612333SN/A    instsToExecute.push_back(inst);
6622292SN/A}
6632292SN/A
6641061SN/A// @todo: Figure out a better way to remove the squashed items from the
6651061SN/A// lists.  Checking the top item of each list to see if it's squashed
6661061SN/A// wastes time and forces jumps.
6671061SN/Atemplate <class Impl>
6681060SN/Avoid
6691060SN/AInstructionQueue<Impl>::scheduleReadyInsts()
6701060SN/A{
6712292SN/A    DPRINTF(IQ, "Attempting to schedule ready instructions from "
6722292SN/A            "the IQ.\n");
6731060SN/A
6741060SN/A    IssueStruct *i2e_info = issueToExecuteQueue->access(0);
6751060SN/A
6762292SN/A    // Have iterator to head of the list
6772292SN/A    // While I haven't exceeded bandwidth or reached the end of the list,
6782292SN/A    // Try to get a FU that can do what this op needs.
6792292SN/A    // If successful, change the oldestInst to the new top of the list, put
6802292SN/A    // the queue in the proper place in the list.
6812292SN/A    // Increment the iterator.
6822292SN/A    // This will avoid trying to schedule a certain op class if there are no
6832292SN/A    // FUs that handle it.
6842292SN/A    ListOrderIt order_it = listOrder.begin();
6852292SN/A    ListOrderIt order_end_it = listOrder.end();
6862292SN/A    int total_issued = 0;
6871060SN/A
6882333SN/A    while (total_issued < totalWidth &&
6892326SN/A           order_it != order_end_it) {
6902292SN/A        OpClass op_class = (*order_it).queueType;
6911060SN/A
6922292SN/A        assert(!readyInsts[op_class].empty());
6931060SN/A
6942292SN/A        DynInstPtr issuing_inst = readyInsts[op_class].top();
6951060SN/A
6962292SN/A        assert(issuing_inst->seqNum == (*order_it).oldestInst);
6971060SN/A
6982292SN/A        if (issuing_inst->isSquashed()) {
6992292SN/A            readyInsts[op_class].pop();
7001060SN/A
7012292SN/A            if (!readyInsts[op_class].empty()) {
7022292SN/A                moveToYoungerInst(order_it);
7032292SN/A            } else {
7042292SN/A                readyIt[op_class] = listOrder.end();
7052292SN/A                queueOnList[op_class] = false;
7061060SN/A            }
7071060SN/A
7082292SN/A            listOrder.erase(order_it++);
7091060SN/A
7102292SN/A            ++iqSquashedInstsIssued;
7112292SN/A
7122292SN/A            continue;
7131060SN/A        }
7141060SN/A
7152326SN/A        int idx = -2;
7162326SN/A        int op_latency = 1;
7172301SN/A        int tid = issuing_inst->threadNumber;
7181060SN/A
7192326SN/A        if (op_class != No_OpClass) {
7202326SN/A            idx = fuPool->getUnit(op_class);
7211060SN/A
7222326SN/A            if (idx > -1) {
7232326SN/A                op_latency = fuPool->getOpLatency(op_class);
7241060SN/A            }
7251060SN/A        }
7261060SN/A
7272348SN/A        // If we have an instruction that doesn't require a FU, or a
7282348SN/A        // valid FU, then schedule for execution.
7292326SN/A        if (idx == -2 || idx != -1) {
7302292SN/A            if (op_latency == 1) {
7312292SN/A                i2e_info->size++;
7322333SN/A                instsToExecute.push_back(issuing_inst);
7331060SN/A
7342326SN/A                // Add the FU onto the list of FU's to be freed next
7352326SN/A                // cycle if we used one.
7362326SN/A                if (idx >= 0)
7372326SN/A                    fuPool->freeUnitNextCycle(idx);
7382292SN/A            } else {
7392292SN/A                int issue_latency = fuPool->getIssueLatency(op_class);
7402326SN/A                // Generate completion event for the FU
7412326SN/A                FUCompletion *execution = new FUCompletion(issuing_inst,
7422326SN/A                                                           idx, this);
7431060SN/A
7442326SN/A                execution->schedule(curTick + cpu->cycles(issue_latency - 1));
7451060SN/A
7462326SN/A                // @todo: Enforce that issue_latency == 1 or op_latency
7472292SN/A                if (issue_latency > 1) {
7482348SN/A                    // If FU isn't pipelined, then it must be freed
7492348SN/A                    // upon the execution completing.
7502326SN/A                    execution->setFreeFU();
7512292SN/A                } else {
7522292SN/A                    // Add the FU onto the list of FU's to be freed next cycle.
7532326SN/A                    fuPool->freeUnitNextCycle(idx);
7542292SN/A                }
7551060SN/A            }
7561060SN/A
7572292SN/A            DPRINTF(IQ, "Thread %i: Issuing instruction PC %#x "
7582292SN/A                    "[sn:%lli]\n",
7592301SN/A                    tid, issuing_inst->readPC(),
7602292SN/A                    issuing_inst->seqNum);
7611060SN/A
7622292SN/A            readyInsts[op_class].pop();
7631061SN/A
7642292SN/A            if (!readyInsts[op_class].empty()) {
7652292SN/A                moveToYoungerInst(order_it);
7662292SN/A            } else {
7672292SN/A                readyIt[op_class] = listOrder.end();
7682292SN/A                queueOnList[op_class] = false;
7691060SN/A            }
7701060SN/A
7712064SN/A            issuing_inst->setIssued();
7722292SN/A            ++total_issued;
7732064SN/A
7742292SN/A            if (!issuing_inst->isMemRef()) {
7752292SN/A                // Memory instructions can not be freed from the IQ until they
7762292SN/A                // complete.
7772292SN/A                ++freeEntries;
7782301SN/A                count[tid]--;
7792731Sktlim@umich.edu                issuing_inst->clearInIQ();
7802292SN/A            } else {
7812301SN/A                memDepUnit[tid].issue(issuing_inst);
7822292SN/A            }
7832292SN/A
7842292SN/A            listOrder.erase(order_it++);
7852326SN/A            statIssuedInstType[tid][op_class]++;
7862292SN/A        } else {
7872326SN/A            statFuBusy[op_class]++;
7882326SN/A            fuBusy[tid]++;
7892292SN/A            ++order_it;
7901060SN/A        }
7911060SN/A    }
7921062SN/A
7932326SN/A    numIssuedDist.sample(total_issued);
7942326SN/A    iqInstsIssued+= total_issued;
7952307SN/A
7962348SN/A    // If we issued any instructions, tell the CPU we had activity.
7972292SN/A    if (total_issued) {
7982292SN/A        cpu->activityThisCycle();
7992292SN/A    } else {
8002292SN/A        DPRINTF(IQ, "Not able to schedule any instructions.\n");
8012292SN/A    }
8021060SN/A}
8031060SN/A
8041061SN/Atemplate <class Impl>
8051060SN/Avoid
8061061SN/AInstructionQueue<Impl>::scheduleNonSpec(const InstSeqNum &inst)
8071060SN/A{
8082292SN/A    DPRINTF(IQ, "Marking nonspeculative instruction [sn:%lli] as ready "
8092292SN/A            "to execute.\n", inst);
8101062SN/A
8112292SN/A    NonSpecMapIt inst_it = nonSpecInsts.find(inst);
8121060SN/A
8131061SN/A    assert(inst_it != nonSpecInsts.end());
8141060SN/A
8152292SN/A    unsigned tid = (*inst_it).second->threadNumber;
8162292SN/A
8171061SN/A    (*inst_it).second->setCanIssue();
8181060SN/A
8191062SN/A    if (!(*inst_it).second->isMemRef()) {
8201062SN/A        addIfReady((*inst_it).second);
8211062SN/A    } else {
8222292SN/A        memDepUnit[tid].nonSpecInstReady((*inst_it).second);
8231062SN/A    }
8241060SN/A
8252292SN/A    (*inst_it).second = NULL;
8262292SN/A
8271061SN/A    nonSpecInsts.erase(inst_it);
8281060SN/A}
8291060SN/A
8301061SN/Atemplate <class Impl>
8311061SN/Avoid
8322292SN/AInstructionQueue<Impl>::commit(const InstSeqNum &inst, unsigned tid)
8332292SN/A{
8342292SN/A    DPRINTF(IQ, "[tid:%i]: Committing instructions older than [sn:%i]\n",
8352292SN/A            tid,inst);
8362292SN/A
8372292SN/A    ListIt iq_it = instList[tid].begin();
8382292SN/A
8392292SN/A    while (iq_it != instList[tid].end() &&
8402292SN/A           (*iq_it)->seqNum <= inst) {
8412292SN/A        ++iq_it;
8422292SN/A        instList[tid].pop_front();
8432292SN/A    }
8442292SN/A
8452292SN/A    assert(freeEntries == (numEntries - countInsts()));
8462292SN/A}
8472292SN/A
8482292SN/Atemplate <class Impl>
8492301SN/Aint
8501684SN/AInstructionQueue<Impl>::wakeDependents(DynInstPtr &completed_inst)
8511684SN/A{
8522301SN/A    int dependents = 0;
8532301SN/A
8542292SN/A    DPRINTF(IQ, "Waking dependents of completed instruction.\n");
8552292SN/A
8562292SN/A    assert(!completed_inst->isSquashed());
8571684SN/A
8581684SN/A    // Tell the memory dependence unit to wake any dependents on this
8592292SN/A    // instruction if it is a memory instruction.  Also complete the memory
8602326SN/A    // instruction at this point since we know it executed without issues.
8612326SN/A    // @todo: Might want to rename "completeMemInst" to something that
8622326SN/A    // indicates that it won't need to be replayed, and call this
8632326SN/A    // earlier.  Might not be a big deal.
8641684SN/A    if (completed_inst->isMemRef()) {
8652292SN/A        memDepUnit[completed_inst->threadNumber].wakeDependents(completed_inst);
8662292SN/A        completeMemInst(completed_inst);
8672292SN/A    } else if (completed_inst->isMemBarrier() ||
8682292SN/A               completed_inst->isWriteBarrier()) {
8692292SN/A        memDepUnit[completed_inst->threadNumber].completeBarrier(completed_inst);
8701684SN/A    }
8711684SN/A
8721684SN/A    for (int dest_reg_idx = 0;
8731684SN/A         dest_reg_idx < completed_inst->numDestRegs();
8741684SN/A         dest_reg_idx++)
8751684SN/A    {
8761684SN/A        PhysRegIndex dest_reg =
8771684SN/A            completed_inst->renamedDestRegIdx(dest_reg_idx);
8781684SN/A
8791684SN/A        // Special case of uniq or control registers.  They are not
8801684SN/A        // handled by the IQ and thus have no dependency graph entry.
8811684SN/A        // @todo Figure out a cleaner way to handle this.
8821684SN/A        if (dest_reg >= numPhysRegs) {
8831684SN/A            continue;
8841684SN/A        }
8851684SN/A
8862292SN/A        DPRINTF(IQ, "Waking any dependents on register %i.\n",
8871684SN/A                (int) dest_reg);
8881684SN/A
8892326SN/A        //Go through the dependency chain, marking the registers as
8902326SN/A        //ready within the waiting instructions.
8912326SN/A        DynInstPtr dep_inst = dependGraph.pop(dest_reg);
8921684SN/A
8932326SN/A        while (dep_inst) {
8942292SN/A            DPRINTF(IQ, "Waking up a dependent instruction, PC%#x.\n",
8952326SN/A                    dep_inst->readPC());
8961684SN/A
8971684SN/A            // Might want to give more information to the instruction
8982326SN/A            // so that it knows which of its source registers is
8992326SN/A            // ready.  However that would mean that the dependency
9002326SN/A            // graph entries would need to hold the src_reg_idx.
9012326SN/A            dep_inst->markSrcRegReady();
9021684SN/A
9032326SN/A            addIfReady(dep_inst);
9041684SN/A
9052326SN/A            dep_inst = dependGraph.pop(dest_reg);
9061684SN/A
9072301SN/A            ++dependents;
9081684SN/A        }
9091684SN/A
9102326SN/A        // Reset the head node now that all of its dependents have
9112326SN/A        // been woken up.
9122326SN/A        assert(dependGraph.empty(dest_reg));
9132326SN/A        dependGraph.clearInst(dest_reg);
9141684SN/A
9151684SN/A        // Mark the scoreboard as having that register ready.
9161684SN/A        regScoreboard[dest_reg] = true;
9171684SN/A    }
9182301SN/A    return dependents;
9192064SN/A}
9202064SN/A
9212064SN/Atemplate <class Impl>
9222064SN/Avoid
9232292SN/AInstructionQueue<Impl>::addReadyMemInst(DynInstPtr &ready_inst)
9242064SN/A{
9252292SN/A    OpClass op_class = ready_inst->opClass();
9262292SN/A
9272292SN/A    readyInsts[op_class].push(ready_inst);
9282292SN/A
9292326SN/A    // Will need to reorder the list if either a queue is not on the list,
9302326SN/A    // or it has an older instruction than last time.
9312326SN/A    if (!queueOnList[op_class]) {
9322326SN/A        addToOrderList(op_class);
9332326SN/A    } else if (readyInsts[op_class].top()->seqNum  <
9342326SN/A               (*readyIt[op_class]).oldestInst) {
9352326SN/A        listOrder.erase(readyIt[op_class]);
9362326SN/A        addToOrderList(op_class);
9372326SN/A    }
9382326SN/A
9392292SN/A    DPRINTF(IQ, "Instruction is ready to issue, putting it onto "
9402292SN/A            "the ready list, PC %#x opclass:%i [sn:%lli].\n",
9412292SN/A            ready_inst->readPC(), op_class, ready_inst->seqNum);
9422064SN/A}
9432064SN/A
9442064SN/Atemplate <class Impl>
9452064SN/Avoid
9462292SN/AInstructionQueue<Impl>::rescheduleMemInst(DynInstPtr &resched_inst)
9472064SN/A{
9482292SN/A    memDepUnit[resched_inst->threadNumber].reschedule(resched_inst);
9492064SN/A}
9502064SN/A
9512064SN/Atemplate <class Impl>
9522064SN/Avoid
9532292SN/AInstructionQueue<Impl>::replayMemInst(DynInstPtr &replay_inst)
9542064SN/A{
9552292SN/A    memDepUnit[replay_inst->threadNumber].replay(replay_inst);
9562292SN/A}
9572292SN/A
9582292SN/Atemplate <class Impl>
9592292SN/Avoid
9602292SN/AInstructionQueue<Impl>::completeMemInst(DynInstPtr &completed_inst)
9612292SN/A{
9622292SN/A    int tid = completed_inst->threadNumber;
9632292SN/A
9642292SN/A    DPRINTF(IQ, "Completing mem instruction PC:%#x [sn:%lli]\n",
9652292SN/A            completed_inst->readPC(), completed_inst->seqNum);
9662292SN/A
9672292SN/A    ++freeEntries;
9682292SN/A
9692292SN/A    completed_inst->memOpDone = true;
9702292SN/A
9712292SN/A    memDepUnit[tid].completed(completed_inst);
9722292SN/A
9732292SN/A    count[tid]--;
9741684SN/A}
9751684SN/A
9761684SN/Atemplate <class Impl>
9771684SN/Avoid
9781061SN/AInstructionQueue<Impl>::violation(DynInstPtr &store,
9791061SN/A                                  DynInstPtr &faulting_load)
9801061SN/A{
9812292SN/A    memDepUnit[store->threadNumber].violation(store, faulting_load);
9821061SN/A}
9831061SN/A
9841061SN/Atemplate <class Impl>
9851060SN/Avoid
9862292SN/AInstructionQueue<Impl>::squash(unsigned tid)
9871060SN/A{
9882292SN/A    DPRINTF(IQ, "[tid:%i]: Starting to squash instructions in "
9892292SN/A            "the IQ.\n", tid);
9901060SN/A
9911060SN/A    // Read instruction sequence number of last instruction out of the
9921060SN/A    // time buffer.
9932292SN/A    squashedSeqNum[tid] = fromCommit->commitInfo[tid].doneSeqNum;
9941060SN/A
9951681SN/A    // Call doSquash if there are insts in the IQ
9962292SN/A    if (count[tid] > 0) {
9972292SN/A        doSquash(tid);
9981681SN/A    }
9991061SN/A
10001061SN/A    // Also tell the memory dependence unit to squash.
10012292SN/A    memDepUnit[tid].squash(squashedSeqNum[tid], tid);
10021060SN/A}
10031060SN/A
10041061SN/Atemplate <class Impl>
10051061SN/Avoid
10062292SN/AInstructionQueue<Impl>::doSquash(unsigned tid)
10071061SN/A{
10082326SN/A    // Start at the tail.
10092326SN/A    ListIt squash_it = instList[tid].end();
10102326SN/A    --squash_it;
10111061SN/A
10122292SN/A    DPRINTF(IQ, "[tid:%i]: Squashing until sequence number %i!\n",
10132292SN/A            tid, squashedSeqNum[tid]);
10141061SN/A
10151061SN/A    // Squash any instructions younger than the squashed sequence number
10161061SN/A    // given.
10172326SN/A    while (squash_it != instList[tid].end() &&
10182326SN/A           (*squash_it)->seqNum > squashedSeqNum[tid]) {
10192292SN/A
10202326SN/A        DynInstPtr squashed_inst = (*squash_it);
10211061SN/A
10221061SN/A        // Only handle the instruction if it actually is in the IQ and
10231061SN/A        // hasn't already been squashed in the IQ.
10242292SN/A        if (squashed_inst->threadNumber != tid ||
10252292SN/A            squashed_inst->isSquashedInIQ()) {
10262326SN/A            --squash_it;
10272292SN/A            continue;
10282292SN/A        }
10292292SN/A
10302292SN/A        if (!squashed_inst->isIssued() ||
10312292SN/A            (squashed_inst->isMemRef() &&
10322292SN/A             !squashed_inst->memOpDone)) {
10331062SN/A
10341061SN/A            // Remove the instruction from the dependency list.
10352292SN/A            if (!squashed_inst->isNonSpeculative() &&
10362336SN/A                !squashed_inst->isStoreConditional() &&
10372292SN/A                !squashed_inst->isMemBarrier() &&
10382292SN/A                !squashed_inst->isWriteBarrier()) {
10391061SN/A
10401061SN/A                for (int src_reg_idx = 0;
10411681SN/A                     src_reg_idx < squashed_inst->numSrcRegs();
10421061SN/A                     src_reg_idx++)
10431061SN/A                {
10441061SN/A                    PhysRegIndex src_reg =
10451061SN/A                        squashed_inst->renamedSrcRegIdx(src_reg_idx);
10461061SN/A
10472326SN/A                    // Only remove it from the dependency graph if it
10482326SN/A                    // was placed there in the first place.
10492326SN/A
10502326SN/A                    // Instead of doing a linked list traversal, we
10512326SN/A                    // can just remove these squashed instructions
10522326SN/A                    // either at issue time, or when the register is
10532326SN/A                    // overwritten.  The only downside to this is it
10542326SN/A                    // leaves more room for error.
10552292SN/A
10561061SN/A                    if (!squashed_inst->isReadySrcRegIdx(src_reg_idx) &&
10571061SN/A                        src_reg < numPhysRegs) {
10582326SN/A                        dependGraph.remove(src_reg, squashed_inst);
10591061SN/A                    }
10601062SN/A
10612292SN/A
10621062SN/A                    ++iqSquashedOperandsExamined;
10631061SN/A                }
10642064SN/A            } else {
10652292SN/A                NonSpecMapIt ns_inst_it =
10662292SN/A                    nonSpecInsts.find(squashed_inst->seqNum);
10672292SN/A                assert(ns_inst_it != nonSpecInsts.end());
10681062SN/A
10692292SN/A                (*ns_inst_it).second = NULL;
10701681SN/A
10712292SN/A                nonSpecInsts.erase(ns_inst_it);
10721062SN/A
10731062SN/A                ++iqSquashedNonSpecRemoved;
10741061SN/A            }
10751061SN/A
10761061SN/A            // Might want to also clear out the head of the dependency graph.
10771061SN/A
10781061SN/A            // Mark it as squashed within the IQ.
10791061SN/A            squashed_inst->setSquashedInIQ();
10801061SN/A
10812292SN/A            // @todo: Remove this hack where several statuses are set so the
10822292SN/A            // inst will flow through the rest of the pipeline.
10831681SN/A            squashed_inst->setIssued();
10841681SN/A            squashed_inst->setCanCommit();
10852731Sktlim@umich.edu            squashed_inst->clearInIQ();
10862292SN/A
10872292SN/A            //Update Thread IQ Count
10882292SN/A            count[squashed_inst->threadNumber]--;
10891681SN/A
10901681SN/A            ++freeEntries;
10911061SN/A
10922326SN/A            DPRINTF(IQ, "[tid:%i]: Instruction [sn:%lli] PC %#x "
10932326SN/A                    "squashed.\n",
10942326SN/A                    tid, squashed_inst->seqNum, squashed_inst->readPC());
10951061SN/A        }
10961061SN/A
10972326SN/A        instList[tid].erase(squash_it--);
10981062SN/A        ++iqSquashedInstsExamined;
10991061SN/A    }
11001060SN/A}
11011060SN/A
11021061SN/Atemplate <class Impl>
11031060SN/Abool
11041061SN/AInstructionQueue<Impl>::addToDependents(DynInstPtr &new_inst)
11051060SN/A{
11061060SN/A    // Loop through the instruction's source registers, adding
11071060SN/A    // them to the dependency list if they are not ready.
11081060SN/A    int8_t total_src_regs = new_inst->numSrcRegs();
11091060SN/A    bool return_val = false;
11101060SN/A
11111060SN/A    for (int src_reg_idx = 0;
11121060SN/A         src_reg_idx < total_src_regs;
11131060SN/A         src_reg_idx++)
11141060SN/A    {
11151060SN/A        // Only add it to the dependency graph if it's not ready.
11161060SN/A        if (!new_inst->isReadySrcRegIdx(src_reg_idx)) {
11171060SN/A            PhysRegIndex src_reg = new_inst->renamedSrcRegIdx(src_reg_idx);
11181060SN/A
11191060SN/A            // Check the IQ's scoreboard to make sure the register
11201060SN/A            // hasn't become ready while the instruction was in flight
11211060SN/A            // between stages.  Only if it really isn't ready should
11221060SN/A            // it be added to the dependency graph.
11231061SN/A            if (src_reg >= numPhysRegs) {
11241061SN/A                continue;
11251061SN/A            } else if (regScoreboard[src_reg] == false) {
11262292SN/A                DPRINTF(IQ, "Instruction PC %#x has src reg %i that "
11271060SN/A                        "is being added to the dependency chain.\n",
11281060SN/A                        new_inst->readPC(), src_reg);
11291060SN/A
11302326SN/A                dependGraph.insert(src_reg, new_inst);
11311060SN/A
11321060SN/A                // Change the return value to indicate that something
11331060SN/A                // was added to the dependency graph.
11341060SN/A                return_val = true;
11351060SN/A            } else {
11362292SN/A                DPRINTF(IQ, "Instruction PC %#x has src reg %i that "
11371060SN/A                        "became ready before it reached the IQ.\n",
11381060SN/A                        new_inst->readPC(), src_reg);
11391060SN/A                // Mark a register ready within the instruction.
11402326SN/A                new_inst->markSrcRegReady(src_reg_idx);
11411060SN/A            }
11421060SN/A        }
11431060SN/A    }
11441060SN/A
11451060SN/A    return return_val;
11461060SN/A}
11471060SN/A
11481061SN/Atemplate <class Impl>
11491060SN/Avoid
11502326SN/AInstructionQueue<Impl>::addToProducers(DynInstPtr &new_inst)
11511060SN/A{
11522326SN/A    // Nothing really needs to be marked when an instruction becomes
11532326SN/A    // the producer of a register's value, but for convenience a ptr
11542326SN/A    // to the producing instruction will be placed in the head node of
11552326SN/A    // the dependency links.
11561060SN/A    int8_t total_dest_regs = new_inst->numDestRegs();
11571060SN/A
11581060SN/A    for (int dest_reg_idx = 0;
11591060SN/A         dest_reg_idx < total_dest_regs;
11601060SN/A         dest_reg_idx++)
11611060SN/A    {
11621061SN/A        PhysRegIndex dest_reg = new_inst->renamedDestRegIdx(dest_reg_idx);
11631061SN/A
11641061SN/A        // Instructions that use the misc regs will have a reg number
11651061SN/A        // higher than the normal physical registers.  In this case these
11661061SN/A        // registers are not renamed, and there is no need to track
11671061SN/A        // dependencies as these instructions must be executed at commit.
11681061SN/A        if (dest_reg >= numPhysRegs) {
11691061SN/A            continue;
11701060SN/A        }
11711060SN/A
11722326SN/A        if (!dependGraph.empty(dest_reg)) {
11732326SN/A            dependGraph.dump();
11742292SN/A            panic("Dependency graph %i not empty!", dest_reg);
11752064SN/A        }
11761062SN/A
11772326SN/A        dependGraph.setInst(dest_reg, new_inst);
11781062SN/A
11791060SN/A        // Mark the scoreboard to say it's not yet ready.
11801060SN/A        regScoreboard[dest_reg] = false;
11811060SN/A    }
11821060SN/A}
11831060SN/A
11841061SN/Atemplate <class Impl>
11851060SN/Avoid
11861061SN/AInstructionQueue<Impl>::addIfReady(DynInstPtr &inst)
11871060SN/A{
11882326SN/A    // If the instruction now has all of its source registers
11891060SN/A    // available, then add it to the list of ready instructions.
11901060SN/A    if (inst->readyToIssue()) {
11911061SN/A
11921060SN/A        //Add the instruction to the proper ready list.
11932292SN/A        if (inst->isMemRef()) {
11941061SN/A
11952292SN/A            DPRINTF(IQ, "Checking if memory instruction can issue.\n");
11961061SN/A
11971062SN/A            // Message to the mem dependence unit that this instruction has
11981062SN/A            // its registers ready.
11992292SN/A            memDepUnit[inst->threadNumber].regsReady(inst);
12001062SN/A
12012292SN/A            return;
12022292SN/A        }
12031062SN/A
12042292SN/A        OpClass op_class = inst->opClass();
12051061SN/A
12062292SN/A        DPRINTF(IQ, "Instruction is ready to issue, putting it onto "
12072292SN/A                "the ready list, PC %#x opclass:%i [sn:%lli].\n",
12082292SN/A                inst->readPC(), op_class, inst->seqNum);
12091061SN/A
12102292SN/A        readyInsts[op_class].push(inst);
12111061SN/A
12122326SN/A        // Will need to reorder the list if either a queue is not on the list,
12132326SN/A        // or it has an older instruction than last time.
12142326SN/A        if (!queueOnList[op_class]) {
12152326SN/A            addToOrderList(op_class);
12162326SN/A        } else if (readyInsts[op_class].top()->seqNum  <
12172326SN/A                   (*readyIt[op_class]).oldestInst) {
12182326SN/A            listOrder.erase(readyIt[op_class]);
12192326SN/A            addToOrderList(op_class);
12201060SN/A        }
12211060SN/A    }
12221060SN/A}
12231060SN/A
12241061SN/Atemplate <class Impl>
12251061SN/Aint
12261061SN/AInstructionQueue<Impl>::countInsts()
12271061SN/A{
12282698Sktlim@umich.edu#if 0
12292292SN/A    //ksewell:This works but definitely could use a cleaner write
12302292SN/A    //with a more intuitive way of counting. Right now it's
12312292SN/A    //just brute force ....
12322698Sktlim@umich.edu    // Change the #if if you want to use this method.
12331061SN/A    int total_insts = 0;
12341061SN/A
12352292SN/A    for (int i = 0; i < numThreads; ++i) {
12362292SN/A        ListIt count_it = instList[i].begin();
12371681SN/A
12382292SN/A        while (count_it != instList[i].end()) {
12392292SN/A            if (!(*count_it)->isSquashed() && !(*count_it)->isSquashedInIQ()) {
12402292SN/A                if (!(*count_it)->isIssued()) {
12412292SN/A                    ++total_insts;
12422292SN/A                } else if ((*count_it)->isMemRef() &&
12432292SN/A                           !(*count_it)->memOpDone) {
12442292SN/A                    // Loads that have not been marked as executed still count
12452292SN/A                    // towards the total instructions.
12462292SN/A                    ++total_insts;
12472292SN/A                }
12482292SN/A            }
12492292SN/A
12502292SN/A            ++count_it;
12511061SN/A        }
12521061SN/A    }
12531061SN/A
12541061SN/A    return total_insts;
12552292SN/A#else
12562292SN/A    return numEntries - freeEntries;
12572292SN/A#endif
12581681SN/A}
12591681SN/A
12601681SN/Atemplate <class Impl>
12611681SN/Avoid
12621061SN/AInstructionQueue<Impl>::dumpLists()
12631061SN/A{
12642292SN/A    for (int i = 0; i < Num_OpClasses; ++i) {
12652292SN/A        cprintf("Ready list %i size: %i\n", i, readyInsts[i].size());
12661061SN/A
12672292SN/A        cprintf("\n");
12682292SN/A    }
12691061SN/A
12701061SN/A    cprintf("Non speculative list size: %i\n", nonSpecInsts.size());
12711061SN/A
12722292SN/A    NonSpecMapIt non_spec_it = nonSpecInsts.begin();
12732292SN/A    NonSpecMapIt non_spec_end_it = nonSpecInsts.end();
12741061SN/A
12751061SN/A    cprintf("Non speculative list: ");
12761061SN/A
12772292SN/A    while (non_spec_it != non_spec_end_it) {
12782292SN/A        cprintf("%#x [sn:%lli]", (*non_spec_it).second->readPC(),
12792292SN/A                (*non_spec_it).second->seqNum);
12801061SN/A        ++non_spec_it;
12811061SN/A    }
12821061SN/A
12831061SN/A    cprintf("\n");
12841061SN/A
12852292SN/A    ListOrderIt list_order_it = listOrder.begin();
12862292SN/A    ListOrderIt list_order_end_it = listOrder.end();
12872292SN/A    int i = 1;
12882292SN/A
12892292SN/A    cprintf("List order: ");
12902292SN/A
12912292SN/A    while (list_order_it != list_order_end_it) {
12922292SN/A        cprintf("%i OpClass:%i [sn:%lli] ", i, (*list_order_it).queueType,
12932292SN/A                (*list_order_it).oldestInst);
12942292SN/A
12952292SN/A        ++list_order_it;
12962292SN/A        ++i;
12972292SN/A    }
12982292SN/A
12992292SN/A    cprintf("\n");
13001061SN/A}
13012292SN/A
13022292SN/A
13032292SN/Atemplate <class Impl>
13042292SN/Avoid
13052292SN/AInstructionQueue<Impl>::dumpInsts()
13062292SN/A{
13072292SN/A    for (int i = 0; i < numThreads; ++i) {
13082292SN/A        int num = 0;
13092292SN/A        int valid_num = 0;
13102292SN/A        ListIt inst_list_it = instList[i].begin();
13112292SN/A
13122292SN/A        while (inst_list_it != instList[i].end())
13132292SN/A        {
13142292SN/A            cprintf("Instruction:%i\n",
13152292SN/A                    num);
13162292SN/A            if (!(*inst_list_it)->isSquashed()) {
13172292SN/A                if (!(*inst_list_it)->isIssued()) {
13182292SN/A                    ++valid_num;
13192292SN/A                    cprintf("Count:%i\n", valid_num);
13202292SN/A                } else if ((*inst_list_it)->isMemRef() &&
13212292SN/A                           !(*inst_list_it)->memOpDone) {
13222326SN/A                    // Loads that have not been marked as executed
13232326SN/A                    // still count towards the total instructions.
13242292SN/A                    ++valid_num;
13252292SN/A                    cprintf("Count:%i\n", valid_num);
13262292SN/A                }
13272292SN/A            }
13282292SN/A
13292292SN/A            cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
13302292SN/A                    "Issued:%i\nSquashed:%i\n",
13312292SN/A                    (*inst_list_it)->readPC(),
13322292SN/A                    (*inst_list_it)->seqNum,
13332292SN/A                    (*inst_list_it)->threadNumber,
13342292SN/A                    (*inst_list_it)->isIssued(),
13352292SN/A                    (*inst_list_it)->isSquashed());
13362292SN/A
13372292SN/A            if ((*inst_list_it)->isMemRef()) {
13382292SN/A                cprintf("MemOpDone:%i\n", (*inst_list_it)->memOpDone);
13392292SN/A            }
13402292SN/A
13412292SN/A            cprintf("\n");
13422292SN/A
13432292SN/A            inst_list_it++;
13442292SN/A            ++num;
13452292SN/A        }
13462292SN/A    }
13472348SN/A
13482348SN/A    cprintf("Insts to Execute list:\n");
13492348SN/A
13502348SN/A    int num = 0;
13512348SN/A    int valid_num = 0;
13522348SN/A    ListIt inst_list_it = instsToExecute.begin();
13532348SN/A
13542348SN/A    while (inst_list_it != instsToExecute.end())
13552348SN/A    {
13562348SN/A        cprintf("Instruction:%i\n",
13572348SN/A                num);
13582348SN/A        if (!(*inst_list_it)->isSquashed()) {
13592348SN/A            if (!(*inst_list_it)->isIssued()) {
13602348SN/A                ++valid_num;
13612348SN/A                cprintf("Count:%i\n", valid_num);
13622348SN/A            } else if ((*inst_list_it)->isMemRef() &&
13632348SN/A                       !(*inst_list_it)->memOpDone) {
13642348SN/A                // Loads that have not been marked as executed
13652348SN/A                // still count towards the total instructions.
13662348SN/A                ++valid_num;
13672348SN/A                cprintf("Count:%i\n", valid_num);
13682348SN/A            }
13692348SN/A        }
13702348SN/A
13712348SN/A        cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
13722348SN/A                "Issued:%i\nSquashed:%i\n",
13732348SN/A                (*inst_list_it)->readPC(),
13742348SN/A                (*inst_list_it)->seqNum,
13752348SN/A                (*inst_list_it)->threadNumber,
13762348SN/A                (*inst_list_it)->isIssued(),
13772348SN/A                (*inst_list_it)->isSquashed());
13782348SN/A
13792348SN/A        if ((*inst_list_it)->isMemRef()) {
13802348SN/A            cprintf("MemOpDone:%i\n", (*inst_list_it)->memOpDone);
13812348SN/A        }
13822348SN/A
13832348SN/A        cprintf("\n");
13842348SN/A
13852348SN/A        inst_list_it++;
13862348SN/A        ++num;
13872348SN/A    }
13882292SN/A}
1389