rename.hh revision 2733
11689SN/A/*
22329SN/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
312292SN/A#ifndef __CPU_O3_RENAME_HH__
322292SN/A#define __CPU_O3_RENAME_HH__
331060SN/A
341060SN/A#include <list>
351060SN/A
361461SN/A#include "base/statistics.hh"
371060SN/A#include "base/timebuf.hh"
381060SN/A
392292SN/A/**
402329SN/A * DefaultRename handles both single threaded and SMT rename. Its
412329SN/A * width is specified by the parameters; each cycle it tries to rename
422329SN/A * that many instructions. It holds onto the rename history of all
432329SN/A * instructions with destination registers, storing the
442329SN/A * arch. register, the new physical register, and the old physical
452329SN/A * register, to allow for undoing of mappings if squashing happens, or
462329SN/A * freeing up registers upon commit. Rename handles blocking if the
472329SN/A * ROB, IQ, or LSQ is going to be full. Rename also handles barriers,
482329SN/A * and does so by stalling on the instruction until the ROB is empty
492329SN/A * and there are no instructions in flight to the ROB.
502292SN/A */
511060SN/Atemplate<class Impl>
522292SN/Aclass DefaultRename
531060SN/A{
541060SN/A  public:
551060SN/A    // Typedefs from the Impl.
561060SN/A    typedef typename Impl::CPUPol CPUPol;
571061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
582733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
591060SN/A    typedef typename Impl::Params Params;
601060SN/A
612292SN/A    // Typedefs from the CPUPol
621061SN/A    typedef typename CPUPol::DecodeStruct DecodeStruct;
631061SN/A    typedef typename CPUPol::RenameStruct RenameStruct;
641061SN/A    typedef typename CPUPol::TimeStruct TimeStruct;
651060SN/A    typedef typename CPUPol::FreeList FreeList;
661060SN/A    typedef typename CPUPol::RenameMap RenameMap;
672292SN/A    // These are used only for initialization.
682292SN/A    typedef typename CPUPol::IEW IEW;
692292SN/A    typedef typename CPUPol::Commit Commit;
701060SN/A
711060SN/A    // Typedefs from the ISA.
722107SN/A    typedef TheISA::RegIndex RegIndex;
731060SN/A
742329SN/A    // A list is used to queue the instructions.  Barrier insts must
752329SN/A    // be added to the front of the list, which is the only reason for
762329SN/A    // using a list instead of a queue. (Most other stages use a
772329SN/A    // queue)
782292SN/A    typedef std::list<DynInstPtr> InstQueue;
792292SN/A
801060SN/A  public:
812329SN/A    /** Overall rename status. Used to determine if the CPU can
822329SN/A     * deschedule itself due to a lack of activity.
832292SN/A     */
842292SN/A    enum RenameStatus {
852292SN/A        Active,
862292SN/A        Inactive
872292SN/A    };
882292SN/A
892292SN/A    /** Individual thread status. */
902292SN/A    enum ThreadStatus {
911060SN/A        Running,
921060SN/A        Idle,
932292SN/A        StartSquash,
941060SN/A        Squashing,
951060SN/A        Blocked,
961060SN/A        Unblocking,
972301SN/A        SerializeStall
981060SN/A    };
991060SN/A
1001060SN/A  private:
1012292SN/A    /** Rename status. */
1022292SN/A    RenameStatus _status;
1032292SN/A
1042292SN/A    /** Per-thread status. */
1052292SN/A    ThreadStatus renameStatus[Impl::MaxThreads];
1061060SN/A
1071060SN/A  public:
1082292SN/A    /** DefaultRename constructor. */
1092292SN/A    DefaultRename(Params *params);
1101060SN/A
1112292SN/A    /** Returns the name of rename. */
1122292SN/A    std::string name() const;
1132292SN/A
1142292SN/A    /** Registers statistics. */
1151062SN/A    void regStats();
1161062SN/A
1172292SN/A    /** Sets CPU pointer. */
1182733Sktlim@umich.edu    void setCPU(O3CPU *cpu_ptr);
1191060SN/A
1202292SN/A    /** Sets the main backwards communication time buffer pointer. */
1211060SN/A    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
1221060SN/A
1232292SN/A    /** Sets pointer to time buffer used to communicate to the next stage. */
1241060SN/A    void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
1251060SN/A
1262292SN/A    /** Sets pointer to time buffer coming from decode. */
1271060SN/A    void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
1281060SN/A
1292292SN/A    /** Sets pointer to IEW stage. Used only for initialization. */
1302292SN/A    void setIEWStage(IEW *iew_stage)
1312292SN/A    { iew_ptr = iew_stage; }
1321060SN/A
1332292SN/A    /** Sets pointer to commit stage. Used only for initialization. */
1342292SN/A    void setCommitStage(Commit *commit_stage)
1352292SN/A    { commit_ptr = commit_stage; }
1362292SN/A
1372292SN/A  private:
1382292SN/A    /** Pointer to IEW stage. Used only for initialization. */
1392292SN/A    IEW *iew_ptr;
1402292SN/A
1412292SN/A    /** Pointer to commit stage. Used only for initialization. */
1422292SN/A    Commit *commit_ptr;
1432292SN/A
1442292SN/A  public:
1452292SN/A    /** Initializes variables for the stage. */
1462292SN/A    void initStage();
1472292SN/A
1482292SN/A    /** Sets pointer to list of active threads. */
1492292SN/A    void setActiveThreads(std::list<unsigned> *at_ptr);
1502292SN/A
1512292SN/A    /** Sets pointer to rename maps (per-thread structures). */
1522292SN/A    void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]);
1532292SN/A
1542292SN/A    /** Sets pointer to the free list. */
1551060SN/A    void setFreeList(FreeList *fl_ptr);
1561060SN/A
1572292SN/A    /** Sets pointer to the scoreboard. */
1582292SN/A    void setScoreboard(Scoreboard *_scoreboard);
1592292SN/A
1602348SN/A    /** Switches out the rename stage. */
1612307SN/A    void switchOut();
1622307SN/A
1632348SN/A    /** Completes the switch out. */
1642316SN/A    void doSwitchOut();
1652316SN/A
1662348SN/A    /** Takes over from another CPU's thread. */
1672307SN/A    void takeOverFrom();
1682307SN/A
1692292SN/A    /** Squashes all instructions in a thread. */
1702292SN/A    void squash(unsigned tid);
1712292SN/A
1722292SN/A    /** Ticks rename, which processes all input signals and attempts to rename
1732292SN/A     * as many instructions as possible.
1742292SN/A     */
1752292SN/A    void tick();
1762292SN/A
1772292SN/A    /** Debugging function used to dump history buffer of renamings. */
1781060SN/A    void dumpHistory();
1791060SN/A
1802292SN/A  private:
1812292SN/A    /** Determines what to do based on rename's current status.
1822292SN/A     * @param status_change rename() sets this variable if there was a status
1832292SN/A     * change (ie switching from blocking to unblocking).
1842292SN/A     * @param tid Thread id to rename instructions from.
1852292SN/A     */
1862292SN/A    void rename(bool &status_change, unsigned tid);
1871060SN/A
1882292SN/A    /** Renames instructions for the given thread. Also handles serializing
1892292SN/A     * instructions.
1902292SN/A     */
1912292SN/A    void renameInsts(unsigned tid);
1921060SN/A
1932292SN/A    /** Inserts unused instructions from a given thread into the skid buffer,
1942292SN/A     * to be renamed once rename unblocks.
1952292SN/A     */
1962292SN/A    void skidInsert(unsigned tid);
1971060SN/A
1982292SN/A    /** Separates instructions from decode into individual lists of instructions
1992292SN/A     * sorted by thread.
2002292SN/A     */
2012292SN/A    void sortInsts();
2021060SN/A
2032292SN/A    /** Returns if all of the skid buffers are empty. */
2042292SN/A    bool skidsEmpty();
2051060SN/A
2062292SN/A    /** Updates overall rename status based on all of the threads' statuses. */
2072292SN/A    void updateStatus();
2081060SN/A
2092292SN/A    /** Switches rename to blocking, and signals back that rename has become
2102292SN/A     * blocked.
2112292SN/A     * @return Returns true if there is a status change.
2122292SN/A     */
2132292SN/A    bool block(unsigned tid);
2141060SN/A
2152292SN/A    /** Switches rename to unblocking if the skid buffer is empty, and signals
2162292SN/A     * back that rename has unblocked.
2172292SN/A     * @return Returns true if there is a status change.
2182292SN/A     */
2192292SN/A    bool unblock(unsigned tid);
2201061SN/A
2212292SN/A    /** Executes actual squash, removing squashed instructions. */
2222292SN/A    void doSquash(unsigned tid);
2231061SN/A
2242292SN/A    /** Removes a committed instruction's rename history. */
2252292SN/A    void removeFromHistory(InstSeqNum inst_seq_num, unsigned tid);
2261061SN/A
2272292SN/A    /** Renames the source registers of an instruction. */
2282292SN/A    inline void renameSrcRegs(DynInstPtr &inst, unsigned tid);
2291061SN/A
2302292SN/A    /** Renames the destination registers of an instruction. */
2312292SN/A    inline void renameDestRegs(DynInstPtr &inst, unsigned tid);
2322292SN/A
2332292SN/A    /** Calculates the number of free ROB entries for a specific thread. */
2342292SN/A    inline int calcFreeROBEntries(unsigned tid);
2352292SN/A
2362292SN/A    /** Calculates the number of free IQ entries for a specific thread. */
2372292SN/A    inline int calcFreeIQEntries(unsigned tid);
2382292SN/A
2392292SN/A    /** Calculates the number of free LSQ entries for a specific thread. */
2402292SN/A    inline int calcFreeLSQEntries(unsigned tid);
2412292SN/A
2422292SN/A    /** Returns the number of valid instructions coming from decode. */
2432292SN/A    unsigned validInsts();
2442292SN/A
2452292SN/A    /** Reads signals telling rename to block/unblock. */
2462292SN/A    void readStallSignals(unsigned tid);
2472292SN/A
2482292SN/A    /** Checks if any stages are telling rename to block. */
2492292SN/A    bool checkStall(unsigned tid);
2502292SN/A
2512348SN/A    /** Gets the number of free entries for a specific thread. */
2522292SN/A    void readFreeEntries(unsigned tid);
2532292SN/A
2542348SN/A    /** Checks the signals and updates the status. */
2552292SN/A    bool checkSignalsAndUpdate(unsigned tid);
2562292SN/A
2572292SN/A    /** Either serializes on the next instruction available in the InstQueue,
2582292SN/A     * or records that it must serialize on the next instruction to enter
2592292SN/A     * rename.
2602292SN/A     * @param inst_list The list of younger, unprocessed instructions for the
2612292SN/A     * thread that has the serializeAfter instruction.
2622292SN/A     * @param tid The thread id.
2632292SN/A     */
2642292SN/A    void serializeAfter(InstQueue &inst_list, unsigned tid);
2652292SN/A
2662292SN/A    /** Holds the information for each destination register rename. It holds
2672292SN/A     * the instruction's sequence number, the arch register, the old physical
2682292SN/A     * register for that arch. register, and the new physical register.
2691060SN/A     */
2701060SN/A    struct RenameHistory {
2711060SN/A        RenameHistory(InstSeqNum _instSeqNum, RegIndex _archReg,
2721060SN/A                      PhysRegIndex _newPhysReg, PhysRegIndex _prevPhysReg)
2731060SN/A            : instSeqNum(_instSeqNum), archReg(_archReg),
2742292SN/A              newPhysReg(_newPhysReg), prevPhysReg(_prevPhysReg)
2751060SN/A        {
2761060SN/A        }
2771060SN/A
2782292SN/A        /** The sequence number of the instruction that renamed. */
2791060SN/A        InstSeqNum instSeqNum;
2802292SN/A        /** The architectural register index that was renamed. */
2811060SN/A        RegIndex archReg;
2822292SN/A        /** The new physical register that the arch. register is renamed to. */
2831060SN/A        PhysRegIndex newPhysReg;
2842292SN/A        /** The old physical register that the arch. register was renamed to. */
2851060SN/A        PhysRegIndex prevPhysReg;
2861060SN/A    };
2871060SN/A
2882292SN/A    /** A per-thread list of all destination register renames, used to either
2892292SN/A     * undo rename mappings or free old physical registers.
2902292SN/A     */
2912292SN/A    std::list<RenameHistory> historyBuffer[Impl::MaxThreads];
2921060SN/A
2932292SN/A    /** Pointer to CPU. */
2942733Sktlim@umich.edu    O3CPU *cpu;
2951060SN/A
2962292SN/A    /** Pointer to main time buffer used for backwards communication. */
2971060SN/A    TimeBuffer<TimeStruct> *timeBuffer;
2981060SN/A
2991060SN/A    /** Wire to get IEW's output from backwards time buffer. */
3001060SN/A    typename TimeBuffer<TimeStruct>::wire fromIEW;
3011060SN/A
3021060SN/A    /** Wire to get commit's output from backwards time buffer. */
3031060SN/A    typename TimeBuffer<TimeStruct>::wire fromCommit;
3041060SN/A
3051060SN/A    /** Wire to write infromation heading to previous stages. */
3061060SN/A    typename TimeBuffer<TimeStruct>::wire toDecode;
3071060SN/A
3081060SN/A    /** Rename instruction queue. */
3091060SN/A    TimeBuffer<RenameStruct> *renameQueue;
3101060SN/A
3111060SN/A    /** Wire to write any information heading to IEW. */
3121060SN/A    typename TimeBuffer<RenameStruct>::wire toIEW;
3131060SN/A
3141060SN/A    /** Decode instruction queue interface. */
3151060SN/A    TimeBuffer<DecodeStruct> *decodeQueue;
3161060SN/A
3171060SN/A    /** Wire to get decode's output from decode queue. */
3181060SN/A    typename TimeBuffer<DecodeStruct>::wire fromDecode;
3191060SN/A
3202292SN/A    /** Queue of all instructions coming from decode this cycle. */
3212292SN/A    InstQueue insts[Impl::MaxThreads];
3222292SN/A
3231060SN/A    /** Skid buffer between rename and decode. */
3242292SN/A    InstQueue skidBuffer[Impl::MaxThreads];
3251060SN/A
3261060SN/A    /** Rename map interface. */
3272292SN/A    RenameMap *renameMap[Impl::MaxThreads];
3281060SN/A
3291060SN/A    /** Free list interface. */
3301060SN/A    FreeList *freeList;
3311060SN/A
3322292SN/A    /** Pointer to the list of active threads. */
3332292SN/A    std::list<unsigned> *activeThreads;
3342292SN/A
3352292SN/A    /** Pointer to the scoreboard. */
3362292SN/A    Scoreboard *scoreboard;
3372292SN/A
3382292SN/A    /** Count of instructions in progress that have been sent off to the IQ
3392292SN/A     * and ROB, but are not yet included in their occupancy counts.
3402292SN/A     */
3412292SN/A    int instsInProgress[Impl::MaxThreads];
3422292SN/A
3432292SN/A    /** Variable that tracks if decode has written to the time buffer this
3442292SN/A     * cycle. Used to tell CPU if there is activity this cycle.
3452292SN/A     */
3462292SN/A    bool wroteToTimeBuffer;
3472292SN/A
3482292SN/A    /** Structures whose free entries impact the amount of instructions that
3492292SN/A     * can be renamed.
3502292SN/A     */
3512292SN/A    struct FreeEntries {
3522292SN/A        unsigned iqEntries;
3532292SN/A        unsigned lsqEntries;
3542292SN/A        unsigned robEntries;
3552292SN/A    };
3562292SN/A
3572292SN/A    /** Per-thread tracking of the number of free entries of back-end
3582292SN/A     * structures.
3592292SN/A     */
3602292SN/A    FreeEntries freeEntries[Impl::MaxThreads];
3612292SN/A
3622292SN/A    /** Records if the ROB is empty. In SMT mode the ROB may be dynamically
3632292SN/A     * partitioned between threads, so the ROB must tell rename when it is
3642292SN/A     * empty.
3652292SN/A     */
3662292SN/A    bool emptyROB[Impl::MaxThreads];
3672292SN/A
3682292SN/A    /** Source of possible stalls. */
3692292SN/A    struct Stalls {
3702292SN/A        bool iew;
3712292SN/A        bool commit;
3722292SN/A    };
3732292SN/A
3742292SN/A    /** Tracks which stages are telling decode to stall. */
3752292SN/A    Stalls stalls[Impl::MaxThreads];
3762292SN/A
3772301SN/A    /** The serialize instruction that rename has stalled on. */
3782301SN/A    DynInstPtr serializeInst[Impl::MaxThreads];
3792292SN/A
3802292SN/A    /** Records if rename needs to serialize on the next instruction for any
3812292SN/A     * thread.
3822292SN/A     */
3832292SN/A    bool serializeOnNextInst[Impl::MaxThreads];
3842292SN/A
3851060SN/A    /** Delay between iew and rename, in ticks. */
3861060SN/A    int iewToRenameDelay;
3871060SN/A
3881060SN/A    /** Delay between decode and rename, in ticks. */
3891060SN/A    int decodeToRenameDelay;
3901060SN/A
3911060SN/A    /** Delay between commit and rename, in ticks. */
3921060SN/A    unsigned commitToRenameDelay;
3931060SN/A
3941060SN/A    /** Rename width, in instructions. */
3951060SN/A    unsigned renameWidth;
3961060SN/A
3971060SN/A    /** Commit width, in instructions.  Used so rename knows how many
3981060SN/A     *  instructions might have freed registers in the previous cycle.
3991060SN/A     */
4001060SN/A    unsigned commitWidth;
4011061SN/A
4022292SN/A    /** The index of the instruction in the time buffer to IEW that rename is
4032292SN/A     * currently using.
4041061SN/A     */
4052292SN/A    unsigned toIEWIndex;
4061062SN/A
4072292SN/A    /** Whether or not rename needs to block this cycle. */
4082292SN/A    bool blockThisCycle;
4092292SN/A
4102292SN/A    /** The number of threads active in rename. */
4112292SN/A    unsigned numThreads;
4122292SN/A
4132292SN/A    /** The maximum skid buffer size. */
4142292SN/A    unsigned skidBufferMax;
4152292SN/A
4162292SN/A    /** Enum to record the source of a structure full stall.  Can come from
4172292SN/A     * either ROB, IQ, LSQ, and it is priortized in that order.
4182292SN/A     */
4192292SN/A    enum FullSource {
4202292SN/A        ROB,
4212292SN/A        IQ,
4222292SN/A        LSQ,
4232292SN/A        NONE
4242292SN/A    };
4252292SN/A
4262292SN/A    /** Function used to increment the stat that corresponds to the source of
4272292SN/A     * the stall.
4282292SN/A     */
4292292SN/A    inline void incrFullStat(const FullSource &source);
4302292SN/A
4312292SN/A    /** Stat for total number of cycles spent squashing. */
4321062SN/A    Stats::Scalar<> renameSquashCycles;
4332292SN/A    /** Stat for total number of cycles spent idle. */
4341062SN/A    Stats::Scalar<> renameIdleCycles;
4352292SN/A    /** Stat for total number of cycles spent blocking. */
4361062SN/A    Stats::Scalar<> renameBlockCycles;
4372301SN/A    /** Stat for total number of cycles spent stalling for a serializing inst. */
4382301SN/A    Stats::Scalar<> renameSerializeStallCycles;
4392292SN/A    /** Stat for total number of cycles spent running normally. */
4402292SN/A    Stats::Scalar<> renameRunCycles;
4412292SN/A    /** Stat for total number of cycles spent unblocking. */
4421062SN/A    Stats::Scalar<> renameUnblockCycles;
4432292SN/A    /** Stat for total number of renamed instructions. */
4441062SN/A    Stats::Scalar<> renameRenamedInsts;
4452292SN/A    /** Stat for total number of squashed instructions that rename discards. */
4461062SN/A    Stats::Scalar<> renameSquashedInsts;
4472292SN/A    /** Stat for total number of times that the ROB starts a stall in rename. */
4481062SN/A    Stats::Scalar<> renameROBFullEvents;
4492292SN/A    /** Stat for total number of times that the IQ starts a stall in rename. */
4501062SN/A    Stats::Scalar<> renameIQFullEvents;
4512292SN/A    /** Stat for total number of times that the LSQ starts a stall in rename. */
4522292SN/A    Stats::Scalar<> renameLSQFullEvents;
4532292SN/A    /** Stat for total number of times that rename runs out of free registers
4542292SN/A     * to use to rename. */
4551062SN/A    Stats::Scalar<> renameFullRegistersEvents;
4562292SN/A    /** Stat for total number of renamed destination registers. */
4571062SN/A    Stats::Scalar<> renameRenamedOperands;
4582292SN/A    /** Stat for total number of source register rename lookups. */
4591062SN/A    Stats::Scalar<> renameRenameLookups;
4602292SN/A    /** Stat for total number of committed renaming mappings. */
4611062SN/A    Stats::Scalar<> renameCommittedMaps;
4622292SN/A    /** Stat for total number of mappings that were undone due to a squash. */
4631062SN/A    Stats::Scalar<> renameUndoneMaps;
4642348SN/A    /** Number of serialize instructions handled. */
4652301SN/A    Stats::Scalar<> renamedSerializing;
4662348SN/A    /** Number of instructions marked as temporarily serializing. */
4672301SN/A    Stats::Scalar<> renamedTempSerializing;
4682348SN/A    /** Number of instructions inserted into skid buffers. */
4692307SN/A    Stats::Scalar<> renameSkidInsts;
4701060SN/A};
4711060SN/A
4722292SN/A#endif // __CPU_O3_RENAME_HH__
473