inst_queue.hh revision 1710
16691Stjones1@inf.ed.ac.uk/*
26691Stjones1@inf.ed.ac.uk * Copyright (c) 2004-2005 The Regents of The University of Michigan
36691Stjones1@inf.ed.ac.uk * All rights reserved.
46691Stjones1@inf.ed.ac.uk *
56691Stjones1@inf.ed.ac.uk * Redistribution and use in source and binary forms, with or without
66691Stjones1@inf.ed.ac.uk * modification, are permitted provided that the following conditions are
76691Stjones1@inf.ed.ac.uk * met: redistributions of source code must retain the above copyright
86691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer;
96691Stjones1@inf.ed.ac.uk * redistributions in binary form must reproduce the above copyright
106691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer in the
116691Stjones1@inf.ed.ac.uk * documentation and/or other materials provided with the distribution;
126691Stjones1@inf.ed.ac.uk * neither the name of the copyright holders nor the names of its
136691Stjones1@inf.ed.ac.uk * contributors may be used to endorse or promote products derived from
146691Stjones1@inf.ed.ac.uk * this software without specific prior written permission.
156691Stjones1@inf.ed.ac.uk *
166691Stjones1@inf.ed.ac.uk * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176691Stjones1@inf.ed.ac.uk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186691Stjones1@inf.ed.ac.uk * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196691Stjones1@inf.ed.ac.uk * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206691Stjones1@inf.ed.ac.uk * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216691Stjones1@inf.ed.ac.uk * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226691Stjones1@inf.ed.ac.uk * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236691Stjones1@inf.ed.ac.uk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246691Stjones1@inf.ed.ac.uk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256691Stjones1@inf.ed.ac.uk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266691Stjones1@inf.ed.ac.uk * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276691Stjones1@inf.ed.ac.uk */
286691Stjones1@inf.ed.ac.uk
296691Stjones1@inf.ed.ac.uk#ifndef __CPU_BETA_CPU_INST_QUEUE_HH__
306691Stjones1@inf.ed.ac.uk#define __CPU_BETA_CPU_INST_QUEUE_HH__
316691Stjones1@inf.ed.ac.uk
326691Stjones1@inf.ed.ac.uk#include <list>
336691Stjones1@inf.ed.ac.uk#include <map>
346691Stjones1@inf.ed.ac.uk#include <queue>
356691Stjones1@inf.ed.ac.uk#include <vector>
366691Stjones1@inf.ed.ac.uk
376691Stjones1@inf.ed.ac.uk#include "base/statistics.hh"
386691Stjones1@inf.ed.ac.uk#include "base/timebuf.hh"
396691Stjones1@inf.ed.ac.uk#include "cpu/inst_seq.hh"
406691Stjones1@inf.ed.ac.uk#include "sim/host.hh"
416691Stjones1@inf.ed.ac.uk
426691Stjones1@inf.ed.ac.uk/**
436691Stjones1@inf.ed.ac.uk * A standard instruction queue class.  It holds ready instructions, in
446691Stjones1@inf.ed.ac.uk * order, in seperate priority queues to facilitate the scheduling of
456691Stjones1@inf.ed.ac.uk * instructions.  The IQ uses a separate linked list to track dependencies.
466691Stjones1@inf.ed.ac.uk * Similar to the rename map and the free list, it expects that
476691Stjones1@inf.ed.ac.uk * floating point registers have their indices start after the integer
486691Stjones1@inf.ed.ac.uk * registers (ie with 96 int and 96 fp registers, regs 0-95 are integer
496691Stjones1@inf.ed.ac.uk * and 96-191 are fp).  This remains true even for both logical and
506691Stjones1@inf.ed.ac.uk * physical register indices.
516691Stjones1@inf.ed.ac.uk */
526691Stjones1@inf.ed.ac.uktemplate <class Impl>
536691Stjones1@inf.ed.ac.ukclass InstructionQueue
546691Stjones1@inf.ed.ac.uk{
556691Stjones1@inf.ed.ac.uk  public:
566691Stjones1@inf.ed.ac.uk    //Typedefs from the Impl.
576691Stjones1@inf.ed.ac.uk    typedef typename Impl::FullCPU FullCPU;
586691Stjones1@inf.ed.ac.uk    typedef typename Impl::DynInstPtr DynInstPtr;
596691Stjones1@inf.ed.ac.uk    typedef typename Impl::Params Params;
606691Stjones1@inf.ed.ac.uk
616691Stjones1@inf.ed.ac.uk    typedef typename Impl::CPUPol::MemDepUnit MemDepUnit;
626691Stjones1@inf.ed.ac.uk    typedef typename Impl::CPUPol::IssueStruct IssueStruct;
636691Stjones1@inf.ed.ac.uk    typedef typename Impl::CPUPol::TimeStruct TimeStruct;
646691Stjones1@inf.ed.ac.uk
656691Stjones1@inf.ed.ac.uk    // Typedef of iterator through the list of instructions.  Might be
666691Stjones1@inf.ed.ac.uk    // better to untie this from the FullCPU or pass its information to
676691Stjones1@inf.ed.ac.uk    // the stages.
686691Stjones1@inf.ed.ac.uk    typedef typename std::list<DynInstPtr>::iterator ListIt;
696691Stjones1@inf.ed.ac.uk
706691Stjones1@inf.ed.ac.uk    /**
716691Stjones1@inf.ed.ac.uk     * Struct for comparing entries to be added to the priority queue.  This
726691Stjones1@inf.ed.ac.uk     * gives reverse ordering to the instructions in terms of sequence
736691Stjones1@inf.ed.ac.uk     * numbers: the instructions with smaller sequence numbers (and hence
746691Stjones1@inf.ed.ac.uk     * are older) will be at the top of the priority queue.
756691Stjones1@inf.ed.ac.uk     */
766691Stjones1@inf.ed.ac.uk    struct pqCompare
776691Stjones1@inf.ed.ac.uk    {
786691Stjones1@inf.ed.ac.uk        bool operator() (const DynInstPtr &lhs, const DynInstPtr &rhs) const
796691Stjones1@inf.ed.ac.uk        {
806691Stjones1@inf.ed.ac.uk            return lhs->seqNum > rhs->seqNum;
816691Stjones1@inf.ed.ac.uk        }
826691Stjones1@inf.ed.ac.uk    };
836691Stjones1@inf.ed.ac.uk
846691Stjones1@inf.ed.ac.uk    /**
856691Stjones1@inf.ed.ac.uk     * Struct for comparing entries to be added to the set.  This gives
866691Stjones1@inf.ed.ac.uk     * standard ordering in terms of sequence numbers.
878588Sgblack@eecs.umich.edu     */
886691Stjones1@inf.ed.ac.uk    struct setCompare
896691Stjones1@inf.ed.ac.uk    {
906691Stjones1@inf.ed.ac.uk        bool operator() (const DynInstPtr &lhs, const DynInstPtr &rhs) const
916691Stjones1@inf.ed.ac.uk        {
926691Stjones1@inf.ed.ac.uk            return lhs->seqNum < rhs->seqNum;
936691Stjones1@inf.ed.ac.uk        }
946691Stjones1@inf.ed.ac.uk    };
956691Stjones1@inf.ed.ac.uk
966691Stjones1@inf.ed.ac.uk    typedef std::priority_queue<DynInstPtr, vector<DynInstPtr>, pqCompare>
976691Stjones1@inf.ed.ac.uk    ReadyInstQueue;
986691Stjones1@inf.ed.ac.uk
996691Stjones1@inf.ed.ac.uk    InstructionQueue(Params &params);
1006691Stjones1@inf.ed.ac.uk
1016691Stjones1@inf.ed.ac.uk    void regStats();
1026691Stjones1@inf.ed.ac.uk
1036691Stjones1@inf.ed.ac.uk    void setCPU(FullCPU *cpu);
1046691Stjones1@inf.ed.ac.uk
1056691Stjones1@inf.ed.ac.uk    void setIssueToExecuteQueue(TimeBuffer<IssueStruct> *i2eQueue);
1066691Stjones1@inf.ed.ac.uk
1076691Stjones1@inf.ed.ac.uk    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
1086691Stjones1@inf.ed.ac.uk
1096691Stjones1@inf.ed.ac.uk    unsigned numFreeEntries();
1106691Stjones1@inf.ed.ac.uk
1116691Stjones1@inf.ed.ac.uk    bool isFull();
1128588Sgblack@eecs.umich.edu
1136691Stjones1@inf.ed.ac.uk    void insert(DynInstPtr &new_inst);
1146691Stjones1@inf.ed.ac.uk
1156691Stjones1@inf.ed.ac.uk    void insertNonSpec(DynInstPtr &new_inst);
1166691Stjones1@inf.ed.ac.uk
1176691Stjones1@inf.ed.ac.uk    void advanceTail(DynInstPtr &inst);
1186691Stjones1@inf.ed.ac.uk
1196691Stjones1@inf.ed.ac.uk    void scheduleReadyInsts();
1206691Stjones1@inf.ed.ac.uk
1216691Stjones1@inf.ed.ac.uk    void scheduleNonSpec(const InstSeqNum &inst);
1226691Stjones1@inf.ed.ac.uk
1236691Stjones1@inf.ed.ac.uk    void wakeDependents(DynInstPtr &completed_inst);
1246691Stjones1@inf.ed.ac.uk
1256691Stjones1@inf.ed.ac.uk    void violation(DynInstPtr &store, DynInstPtr &faulting_load);
1266691Stjones1@inf.ed.ac.uk
1276691Stjones1@inf.ed.ac.uk    // Change this to take in the sequence number
1286691Stjones1@inf.ed.ac.uk    void squash();
1296691Stjones1@inf.ed.ac.uk
1306691Stjones1@inf.ed.ac.uk    void doSquash();
1316691Stjones1@inf.ed.ac.uk
1326691Stjones1@inf.ed.ac.uk    void stopSquash();
133
134  private:
135    /** Pointer to the CPU. */
136    FullCPU *cpu;
137
138    /** The memory dependence unit, which tracks/predicts memory dependences
139     *  between instructions.
140     */
141    MemDepUnit memDepUnit;
142
143    /** The queue to the execute stage.  Issued instructions will be written
144     *  into it.
145     */
146    TimeBuffer<IssueStruct> *issueToExecuteQueue;
147
148    /** The backwards time buffer. */
149    TimeBuffer<TimeStruct> *timeBuffer;
150
151    /** Wire to read information from timebuffer. */
152    typename TimeBuffer<TimeStruct>::wire fromCommit;
153
154    enum InstList {
155        Int,
156        Float,
157        Branch,
158        Memory,
159        Misc,
160        Squashed,
161        None
162    };
163
164    /** List of ready int instructions.  Used to keep track of the order in
165     *  which instructions should issue.
166     */
167    ReadyInstQueue readyIntInsts;
168
169    /** List of ready floating point instructions. */
170    ReadyInstQueue readyFloatInsts;
171
172    /** List of ready branch instructions. */
173    ReadyInstQueue readyBranchInsts;
174
175    /** List of ready miscellaneous instructions. */
176    ReadyInstQueue readyMiscInsts;
177
178    /** List of squashed instructions (which are still valid and in IQ).
179     *  Implemented using a priority queue; the entries must contain both
180     *  the IQ index and sequence number of each instruction so that
181     *  ordering based on sequence numbers can be used.
182     */
183    ReadyInstQueue squashedInsts;
184
185    /** List of non-speculative instructions that will be scheduled
186     *  once the IQ gets a signal from commit.  While it's redundant to
187     *  have the key be a part of the value (the sequence number is stored
188     *  inside of DynInst), when these instructions are woken up only
189     *  the sequence number will be available.  Thus it is most efficient to be
190     *  able to search by the sequence number alone.
191     */
192    std::map<InstSeqNum, DynInstPtr> nonSpecInsts;
193
194    typedef typename std::map<InstSeqNum, DynInstPtr>::iterator non_spec_it_t;
195
196    /** Number of free IQ entries left. */
197    unsigned freeEntries;
198
199    /** The number of entries in the instruction queue. */
200    unsigned numEntries;
201
202    /** The number of integer instructions that can be issued in one
203     *  cycle.
204     */
205    unsigned intWidth;
206
207    /** The number of floating point instructions that can be issued
208     *  in one cycle.
209     */
210    unsigned floatWidth;
211
212    /** The number of branches that can be issued in one cycle. */
213    unsigned branchWidth;
214
215    /** The number of memory instructions that can be issued in one cycle. */
216    unsigned memoryWidth;
217
218    /** The total number of instructions that can be issued in one cycle. */
219    unsigned totalWidth;
220
221    //The number of physical registers in the CPU.
222    unsigned numPhysRegs;
223
224    /** The number of physical integer registers in the CPU. */
225    unsigned numPhysIntRegs;
226
227    /** The number of floating point registers in the CPU. */
228    unsigned numPhysFloatRegs;
229
230    /** Delay between commit stage and the IQ.
231     *  @todo: Make there be a distinction between the delays within IEW.
232     */
233    unsigned commitToIEWDelay;
234
235    //////////////////////////////////
236    // Variables needed for squashing
237    //////////////////////////////////
238
239    /** The sequence number of the squashed instruction. */
240    InstSeqNum squashedSeqNum;
241
242    /** Iterator that points to the youngest instruction in the IQ. */
243    ListIt tail;
244
245    /** Iterator that points to the last instruction that has been squashed.
246     *  This will not be valid unless the IQ is in the process of squashing.
247     */
248    ListIt squashIt;
249
250    ///////////////////////////////////
251    // Dependency graph stuff
252    ///////////////////////////////////
253
254    class DependencyEntry
255    {
256      public:
257        DynInstPtr inst;
258        //Might want to include data about what arch. register the
259        //dependence is waiting on.
260        DependencyEntry *next;
261
262        //This function, and perhaps this whole class, stand out a little
263        //bit as they don't fit a classification well.  I want access
264        //to the underlying structure of the linked list, yet at
265        //the same time it feels like this should be something abstracted
266        //away.  So for now it will sit here, within the IQ, until
267        //a better implementation is decided upon.
268        // This function probably shouldn't be within the entry...
269        void insert(DynInstPtr &new_inst);
270
271        void remove(DynInstPtr &inst_to_remove);
272
273        // Debug variable, remove when done testing.
274        static unsigned mem_alloc_counter;
275    };
276
277    /** Array of linked lists.  Each linked list is a list of all the
278     *  instructions that depend upon a given register.  The actual
279     *  register's index is used to index into the graph; ie all
280     *  instructions in flight that are dependent upon r34 will be
281     *  in the linked list of dependGraph[34].
282     */
283    DependencyEntry *dependGraph;
284
285    /** A cache of the recently woken registers.  It is 1 if the register
286     *  has been woken up recently, and 0 if the register has been added
287     *  to the dependency graph and has not yet received its value.  It
288     *  is basically a secondary scoreboard, and should pretty much mirror
289     *  the scoreboard that exists in the rename map.
290     */
291    vector<bool> regScoreboard;
292
293    bool addToDependents(DynInstPtr &new_inst);
294    void insertDependency(DynInstPtr &new_inst);
295    void createDependency(DynInstPtr &new_inst);
296
297    void addIfReady(DynInstPtr &inst);
298
299  private:
300    /** Debugging function to count how many entries are in the IQ.  It does
301     *  a linear walk through the instructions, so do not call this function
302     *  during normal execution.
303     */
304    int countInsts();
305
306    /** Debugging function to dump out the dependency graph.
307     */
308    void dumpDependGraph();
309
310    /** Debugging function to dump all the list sizes, as well as print
311     *  out the list of nonspeculative instructions.  Should not be used
312     *  in any other capacity, but it has no harmful sideaffects.
313     */
314    void dumpLists();
315
316    Stats::Scalar<> iqInstsAdded;
317    Stats::Scalar<> iqNonSpecInstsAdded;
318//    Stats::Scalar<> iqIntInstsAdded;
319    Stats::Scalar<> iqIntInstsIssued;
320//    Stats::Scalar<> iqFloatInstsAdded;
321    Stats::Scalar<> iqFloatInstsIssued;
322//    Stats::Scalar<> iqBranchInstsAdded;
323    Stats::Scalar<> iqBranchInstsIssued;
324//    Stats::Scalar<> iqMemInstsAdded;
325    Stats::Scalar<> iqMemInstsIssued;
326//    Stats::Scalar<> iqMiscInstsAdded;
327    Stats::Scalar<> iqMiscInstsIssued;
328    Stats::Scalar<> iqSquashedInstsIssued;
329    Stats::Scalar<> iqLoopSquashStalls;
330    Stats::Scalar<> iqSquashedInstsExamined;
331    Stats::Scalar<> iqSquashedOperandsExamined;
332    Stats::Scalar<> iqSquashedNonSpecRemoved;
333
334};
335
336#endif //__CPU_BETA_CPU_INST_QUEUE_HH__
337