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