rename.hh revision 2361
1/*
2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __CPU_O3_RENAME_HH__
30#define __CPU_O3_RENAME_HH__
31
32#include <list>
33
34#include "base/statistics.hh"
35#include "base/timebuf.hh"
36
37/**
38 * DefaultRename handles both single threaded and SMT rename. Its
39 * width is specified by the parameters; each cycle it tries to rename
40 * that many instructions. It holds onto the rename history of all
41 * instructions with destination registers, storing the
42 * arch. register, the new physical register, and the old physical
43 * register, to allow for undoing of mappings if squashing happens, or
44 * freeing up registers upon commit. Rename handles blocking if the
45 * ROB, IQ, or LSQ is going to be full. Rename also handles barriers,
46 * and does so by stalling on the instruction until the ROB is empty
47 * and there are no instructions in flight to the ROB.
48 */
49template<class Impl>
50class DefaultRename
51{
52  public:
53    // Typedefs from the Impl.
54    typedef typename Impl::CPUPol CPUPol;
55    typedef typename Impl::DynInstPtr DynInstPtr;
56    typedef typename Impl::FullCPU FullCPU;
57    typedef typename Impl::Params Params;
58
59    // Typedefs from the CPUPol
60    typedef typename CPUPol::DecodeStruct DecodeStruct;
61    typedef typename CPUPol::RenameStruct RenameStruct;
62    typedef typename CPUPol::TimeStruct TimeStruct;
63    typedef typename CPUPol::FreeList FreeList;
64    typedef typename CPUPol::RenameMap RenameMap;
65    // These are used only for initialization.
66    typedef typename CPUPol::IEW IEW;
67    typedef typename CPUPol::Commit Commit;
68
69    // Typedefs from the ISA.
70    typedef TheISA::RegIndex RegIndex;
71
72    // A list is used to queue the instructions.  Barrier insts must
73    // be added to the front of the list, which is the only reason for
74    // using a list instead of a queue. (Most other stages use a
75    // queue)
76    typedef std::list<DynInstPtr> InstQueue;
77
78  public:
79    /** Overall rename status. Used to determine if the CPU can
80     * deschedule itself due to a lack of activity.
81     */
82    enum RenameStatus {
83        Active,
84        Inactive
85    };
86
87    /** Individual thread status. */
88    enum ThreadStatus {
89        Running,
90        Idle,
91        StartSquash,
92        Squashing,
93        Blocked,
94        Unblocking,
95        SerializeStall
96    };
97
98  private:
99    /** Rename status. */
100    RenameStatus _status;
101
102    /** Per-thread status. */
103    ThreadStatus renameStatus[Impl::MaxThreads];
104
105  public:
106    /** DefaultRename constructor. */
107    DefaultRename(Params *params);
108
109    /** Returns the name of rename. */
110    std::string name() const;
111
112    /** Registers statistics. */
113    void regStats();
114
115    /** Sets CPU pointer. */
116    void setCPU(FullCPU *cpu_ptr);
117
118    /** Sets the main backwards communication time buffer pointer. */
119    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
120
121    /** Sets pointer to time buffer used to communicate to the next stage. */
122    void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
123
124    /** Sets pointer to time buffer coming from decode. */
125    void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
126
127    /** Sets pointer to IEW stage. Used only for initialization. */
128    void setIEWStage(IEW *iew_stage)
129    { iew_ptr = iew_stage; }
130
131    /** Sets pointer to commit stage. Used only for initialization. */
132    void setCommitStage(Commit *commit_stage)
133    { commit_ptr = commit_stage; }
134
135  private:
136    /** Pointer to IEW stage. Used only for initialization. */
137    IEW *iew_ptr;
138
139    /** Pointer to commit stage. Used only for initialization. */
140    Commit *commit_ptr;
141
142  public:
143    /** Initializes variables for the stage. */
144    void initStage();
145
146    /** Sets pointer to list of active threads. */
147    void setActiveThreads(std::list<unsigned> *at_ptr);
148
149    /** Sets pointer to rename maps (per-thread structures). */
150    void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]);
151
152    /** Sets pointer to the free list. */
153    void setFreeList(FreeList *fl_ptr);
154
155    /** Sets pointer to the scoreboard. */
156    void setScoreboard(Scoreboard *_scoreboard);
157
158    /** Switches out the rename stage. */
159    void switchOut();
160
161    /** Completes the switch out. */
162    void doSwitchOut();
163
164    /** Takes over from another CPU's thread. */
165    void takeOverFrom();
166
167    /** Squashes all instructions in a thread. */
168    void squash(unsigned tid);
169
170    /** Ticks rename, which processes all input signals and attempts to rename
171     * as many instructions as possible.
172     */
173    void tick();
174
175    /** Debugging function used to dump history buffer of renamings. */
176    void dumpHistory();
177
178  private:
179    /** Determines what to do based on rename's current status.
180     * @param status_change rename() sets this variable if there was a status
181     * change (ie switching from blocking to unblocking).
182     * @param tid Thread id to rename instructions from.
183     */
184    void rename(bool &status_change, unsigned tid);
185
186    /** Renames instructions for the given thread. Also handles serializing
187     * instructions.
188     */
189    void renameInsts(unsigned tid);
190
191    /** Inserts unused instructions from a given thread into the skid buffer,
192     * to be renamed once rename unblocks.
193     */
194    void skidInsert(unsigned tid);
195
196    /** Separates instructions from decode into individual lists of instructions
197     * sorted by thread.
198     */
199    void sortInsts();
200
201    /** Returns if all of the skid buffers are empty. */
202    bool skidsEmpty();
203
204    /** Updates overall rename status based on all of the threads' statuses. */
205    void updateStatus();
206
207    /** Switches rename to blocking, and signals back that rename has become
208     * blocked.
209     * @return Returns true if there is a status change.
210     */
211    bool block(unsigned tid);
212
213    /** Switches rename to unblocking if the skid buffer is empty, and signals
214     * back that rename has unblocked.
215     * @return Returns true if there is a status change.
216     */
217    bool unblock(unsigned tid);
218
219    /** Executes actual squash, removing squashed instructions. */
220    void doSquash(unsigned tid);
221
222    /** Removes a committed instruction's rename history. */
223    void removeFromHistory(InstSeqNum inst_seq_num, unsigned tid);
224
225    /** Renames the source registers of an instruction. */
226    inline void renameSrcRegs(DynInstPtr &inst, unsigned tid);
227
228    /** Renames the destination registers of an instruction. */
229    inline void renameDestRegs(DynInstPtr &inst, unsigned tid);
230
231    /** Calculates the number of free ROB entries for a specific thread. */
232    inline int calcFreeROBEntries(unsigned tid);
233
234    /** Calculates the number of free IQ entries for a specific thread. */
235    inline int calcFreeIQEntries(unsigned tid);
236
237    /** Calculates the number of free LSQ entries for a specific thread. */
238    inline int calcFreeLSQEntries(unsigned tid);
239
240    /** Returns the number of valid instructions coming from decode. */
241    unsigned validInsts();
242
243    /** Reads signals telling rename to block/unblock. */
244    void readStallSignals(unsigned tid);
245
246    /** Checks if any stages are telling rename to block. */
247    bool checkStall(unsigned tid);
248
249    /** Gets the number of free entries for a specific thread. */
250    void readFreeEntries(unsigned tid);
251
252    /** Checks the signals and updates the status. */
253    bool checkSignalsAndUpdate(unsigned tid);
254
255    /** Either serializes on the next instruction available in the InstQueue,
256     * or records that it must serialize on the next instruction to enter
257     * rename.
258     * @param inst_list The list of younger, unprocessed instructions for the
259     * thread that has the serializeAfter instruction.
260     * @param tid The thread id.
261     */
262    void serializeAfter(InstQueue &inst_list, unsigned tid);
263
264    /** Holds the information for each destination register rename. It holds
265     * the instruction's sequence number, the arch register, the old physical
266     * register for that arch. register, and the new physical register.
267     */
268    struct RenameHistory {
269        RenameHistory(InstSeqNum _instSeqNum, RegIndex _archReg,
270                      PhysRegIndex _newPhysReg, PhysRegIndex _prevPhysReg)
271            : instSeqNum(_instSeqNum), archReg(_archReg),
272              newPhysReg(_newPhysReg), prevPhysReg(_prevPhysReg)
273        {
274        }
275
276        /** The sequence number of the instruction that renamed. */
277        InstSeqNum instSeqNum;
278        /** The architectural register index that was renamed. */
279        RegIndex archReg;
280        /** The new physical register that the arch. register is renamed to. */
281        PhysRegIndex newPhysReg;
282        /** The old physical register that the arch. register was renamed to. */
283        PhysRegIndex prevPhysReg;
284    };
285
286    /** A per-thread list of all destination register renames, used to either
287     * undo rename mappings or free old physical registers.
288     */
289    std::list<RenameHistory> historyBuffer[Impl::MaxThreads];
290
291    /** Pointer to CPU. */
292    FullCPU *cpu;
293
294    /** Pointer to main time buffer used for backwards communication. */
295    TimeBuffer<TimeStruct> *timeBuffer;
296
297    /** Wire to get IEW's output from backwards time buffer. */
298    typename TimeBuffer<TimeStruct>::wire fromIEW;
299
300    /** Wire to get commit's output from backwards time buffer. */
301    typename TimeBuffer<TimeStruct>::wire fromCommit;
302
303    /** Wire to write infromation heading to previous stages. */
304    typename TimeBuffer<TimeStruct>::wire toDecode;
305
306    /** Rename instruction queue. */
307    TimeBuffer<RenameStruct> *renameQueue;
308
309    /** Wire to write any information heading to IEW. */
310    typename TimeBuffer<RenameStruct>::wire toIEW;
311
312    /** Decode instruction queue interface. */
313    TimeBuffer<DecodeStruct> *decodeQueue;
314
315    /** Wire to get decode's output from decode queue. */
316    typename TimeBuffer<DecodeStruct>::wire fromDecode;
317
318    /** Queue of all instructions coming from decode this cycle. */
319    InstQueue insts[Impl::MaxThreads];
320
321    /** Skid buffer between rename and decode. */
322    InstQueue skidBuffer[Impl::MaxThreads];
323
324    /** Rename map interface. */
325    RenameMap *renameMap[Impl::MaxThreads];
326
327    /** Free list interface. */
328    FreeList *freeList;
329
330    /** Pointer to the list of active threads. */
331    std::list<unsigned> *activeThreads;
332
333    /** Pointer to the scoreboard. */
334    Scoreboard *scoreboard;
335
336    /** Count of instructions in progress that have been sent off to the IQ
337     * and ROB, but are not yet included in their occupancy counts.
338     */
339    int instsInProgress[Impl::MaxThreads];
340
341    /** Variable that tracks if decode has written to the time buffer this
342     * cycle. Used to tell CPU if there is activity this cycle.
343     */
344    bool wroteToTimeBuffer;
345
346    /** Structures whose free entries impact the amount of instructions that
347     * can be renamed.
348     */
349    struct FreeEntries {
350        unsigned iqEntries;
351        unsigned lsqEntries;
352        unsigned robEntries;
353    };
354
355    /** Per-thread tracking of the number of free entries of back-end
356     * structures.
357     */
358    FreeEntries freeEntries[Impl::MaxThreads];
359
360    /** Records if the ROB is empty. In SMT mode the ROB may be dynamically
361     * partitioned between threads, so the ROB must tell rename when it is
362     * empty.
363     */
364    bool emptyROB[Impl::MaxThreads];
365
366    /** Source of possible stalls. */
367    struct Stalls {
368        bool iew;
369        bool commit;
370    };
371
372    /** Tracks which stages are telling decode to stall. */
373    Stalls stalls[Impl::MaxThreads];
374
375    /** The serialize instruction that rename has stalled on. */
376    DynInstPtr serializeInst[Impl::MaxThreads];
377
378    /** Records if rename needs to serialize on the next instruction for any
379     * thread.
380     */
381    bool serializeOnNextInst[Impl::MaxThreads];
382
383    /** Delay between iew and rename, in ticks. */
384    int iewToRenameDelay;
385
386    /** Delay between decode and rename, in ticks. */
387    int decodeToRenameDelay;
388
389    /** Delay between commit and rename, in ticks. */
390    unsigned commitToRenameDelay;
391
392    /** Rename width, in instructions. */
393    unsigned renameWidth;
394
395    /** Commit width, in instructions.  Used so rename knows how many
396     *  instructions might have freed registers in the previous cycle.
397     */
398    unsigned commitWidth;
399
400    /** The index of the instruction in the time buffer to IEW that rename is
401     * currently using.
402     */
403    unsigned toIEWIndex;
404
405    /** Whether or not rename needs to block this cycle. */
406    bool blockThisCycle;
407
408    /** The number of threads active in rename. */
409    unsigned numThreads;
410
411    /** The maximum skid buffer size. */
412    unsigned skidBufferMax;
413
414    PhysRegIndex maxPhysicalRegs;
415
416    /** Enum to record the source of a structure full stall.  Can come from
417     * either ROB, IQ, LSQ, and it is priortized in that order.
418     */
419    enum FullSource {
420        ROB,
421        IQ,
422        LSQ,
423        NONE
424    };
425
426    /** Function used to increment the stat that corresponds to the source of
427     * the stall.
428     */
429    inline void incrFullStat(const FullSource &source);
430
431    /** Stat for total number of cycles spent squashing. */
432    Stats::Scalar<> renameSquashCycles;
433    /** Stat for total number of cycles spent idle. */
434    Stats::Scalar<> renameIdleCycles;
435    /** Stat for total number of cycles spent blocking. */
436    Stats::Scalar<> renameBlockCycles;
437    /** Stat for total number of cycles spent stalling for a serializing inst. */
438    Stats::Scalar<> renameSerializeStallCycles;
439    /** Stat for total number of cycles spent running normally. */
440    Stats::Scalar<> renameRunCycles;
441    /** Stat for total number of cycles spent unblocking. */
442    Stats::Scalar<> renameUnblockCycles;
443    /** Stat for total number of renamed instructions. */
444    Stats::Scalar<> renameRenamedInsts;
445    /** Stat for total number of squashed instructions that rename discards. */
446    Stats::Scalar<> renameSquashedInsts;
447    /** Stat for total number of times that the ROB starts a stall in rename. */
448    Stats::Scalar<> renameROBFullEvents;
449    /** Stat for total number of times that the IQ starts a stall in rename. */
450    Stats::Scalar<> renameIQFullEvents;
451    /** Stat for total number of times that the LSQ starts a stall in rename. */
452    Stats::Scalar<> renameLSQFullEvents;
453    /** Stat for total number of times that rename runs out of free registers
454     * to use to rename. */
455    Stats::Scalar<> renameFullRegistersEvents;
456    /** Stat for total number of renamed destination registers. */
457    Stats::Scalar<> renameRenamedOperands;
458    /** Stat for total number of source register rename lookups. */
459    Stats::Scalar<> renameRenameLookups;
460    /** Stat for total number of committed renaming mappings. */
461    Stats::Scalar<> renameCommittedMaps;
462    /** Stat for total number of mappings that were undone due to a squash. */
463    Stats::Scalar<> renameUndoneMaps;
464    /** Number of serialize instructions handled. */
465    Stats::Scalar<> renamedSerializing;
466    /** Number of instructions marked as temporarily serializing. */
467    Stats::Scalar<> renamedTempSerializing;
468    /** Number of instructions inserted into skid buffers. */
469    Stats::Scalar<> renameSkidInsts;
470};
471
472#endif // __CPU_O3_RENAME_HH__
473