rename.hh revision 2863
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
1602843Sktlim@umich.edu    /** Drains the rename stage. */
1612863Sktlim@umich.edu    bool drain();
1622843Sktlim@umich.edu
1632843Sktlim@umich.edu    /** Resumes execution after a drain. */
1642843Sktlim@umich.edu    void resume() { }
1652843Sktlim@umich.edu
1662348SN/A    /** Switches out the rename stage. */
1672307SN/A    void switchOut();
1682307SN/A
1692348SN/A    /** Takes over from another CPU's thread. */
1702307SN/A    void takeOverFrom();
1712307SN/A
1722292SN/A    /** Squashes all instructions in a thread. */
1732292SN/A    void squash(unsigned tid);
1742292SN/A
1752292SN/A    /** Ticks rename, which processes all input signals and attempts to rename
1762292SN/A     * as many instructions as possible.
1772292SN/A     */
1782292SN/A    void tick();
1792292SN/A
1802292SN/A    /** Debugging function used to dump history buffer of renamings. */
1811060SN/A    void dumpHistory();
1821060SN/A
1832292SN/A  private:
1842292SN/A    /** Determines what to do based on rename's current status.
1852292SN/A     * @param status_change rename() sets this variable if there was a status
1862292SN/A     * change (ie switching from blocking to unblocking).
1872292SN/A     * @param tid Thread id to rename instructions from.
1882292SN/A     */
1892292SN/A    void rename(bool &status_change, unsigned tid);
1901060SN/A
1912292SN/A    /** Renames instructions for the given thread. Also handles serializing
1922292SN/A     * instructions.
1932292SN/A     */
1942292SN/A    void renameInsts(unsigned tid);
1951060SN/A
1962292SN/A    /** Inserts unused instructions from a given thread into the skid buffer,
1972292SN/A     * to be renamed once rename unblocks.
1982292SN/A     */
1992292SN/A    void skidInsert(unsigned tid);
2001060SN/A
2012292SN/A    /** Separates instructions from decode into individual lists of instructions
2022292SN/A     * sorted by thread.
2032292SN/A     */
2042292SN/A    void sortInsts();
2051060SN/A
2062292SN/A    /** Returns if all of the skid buffers are empty. */
2072292SN/A    bool skidsEmpty();
2081060SN/A
2092292SN/A    /** Updates overall rename status based on all of the threads' statuses. */
2102292SN/A    void updateStatus();
2111060SN/A
2122292SN/A    /** Switches rename to blocking, and signals back that rename has become
2132292SN/A     * blocked.
2142292SN/A     * @return Returns true if there is a status change.
2152292SN/A     */
2162292SN/A    bool block(unsigned tid);
2171060SN/A
2182292SN/A    /** Switches rename to unblocking if the skid buffer is empty, and signals
2192292SN/A     * back that rename has unblocked.
2202292SN/A     * @return Returns true if there is a status change.
2212292SN/A     */
2222292SN/A    bool unblock(unsigned tid);
2231061SN/A
2242292SN/A    /** Executes actual squash, removing squashed instructions. */
2252292SN/A    void doSquash(unsigned tid);
2261061SN/A
2272292SN/A    /** Removes a committed instruction's rename history. */
2282292SN/A    void removeFromHistory(InstSeqNum inst_seq_num, unsigned tid);
2291061SN/A
2302292SN/A    /** Renames the source registers of an instruction. */
2312292SN/A    inline void renameSrcRegs(DynInstPtr &inst, unsigned tid);
2321061SN/A
2332292SN/A    /** Renames the destination registers of an instruction. */
2342292SN/A    inline void renameDestRegs(DynInstPtr &inst, unsigned tid);
2352292SN/A
2362292SN/A    /** Calculates the number of free ROB entries for a specific thread. */
2372292SN/A    inline int calcFreeROBEntries(unsigned tid);
2382292SN/A
2392292SN/A    /** Calculates the number of free IQ entries for a specific thread. */
2402292SN/A    inline int calcFreeIQEntries(unsigned tid);
2412292SN/A
2422292SN/A    /** Calculates the number of free LSQ entries for a specific thread. */
2432292SN/A    inline int calcFreeLSQEntries(unsigned tid);
2442292SN/A
2452292SN/A    /** Returns the number of valid instructions coming from decode. */
2462292SN/A    unsigned validInsts();
2472292SN/A
2482292SN/A    /** Reads signals telling rename to block/unblock. */
2492292SN/A    void readStallSignals(unsigned tid);
2502292SN/A
2512292SN/A    /** Checks if any stages are telling rename to block. */
2522292SN/A    bool checkStall(unsigned tid);
2532292SN/A
2542348SN/A    /** Gets the number of free entries for a specific thread. */
2552292SN/A    void readFreeEntries(unsigned tid);
2562292SN/A
2572348SN/A    /** Checks the signals and updates the status. */
2582292SN/A    bool checkSignalsAndUpdate(unsigned tid);
2592292SN/A
2602292SN/A    /** Either serializes on the next instruction available in the InstQueue,
2612292SN/A     * or records that it must serialize on the next instruction to enter
2622292SN/A     * rename.
2632292SN/A     * @param inst_list The list of younger, unprocessed instructions for the
2642292SN/A     * thread that has the serializeAfter instruction.
2652292SN/A     * @param tid The thread id.
2662292SN/A     */
2672292SN/A    void serializeAfter(InstQueue &inst_list, unsigned tid);
2682292SN/A
2692292SN/A    /** Holds the information for each destination register rename. It holds
2702292SN/A     * the instruction's sequence number, the arch register, the old physical
2712292SN/A     * register for that arch. register, and the new physical register.
2721060SN/A     */
2731060SN/A    struct RenameHistory {
2741060SN/A        RenameHistory(InstSeqNum _instSeqNum, RegIndex _archReg,
2751060SN/A                      PhysRegIndex _newPhysReg, PhysRegIndex _prevPhysReg)
2761060SN/A            : instSeqNum(_instSeqNum), archReg(_archReg),
2772292SN/A              newPhysReg(_newPhysReg), prevPhysReg(_prevPhysReg)
2781060SN/A        {
2791060SN/A        }
2801060SN/A
2812292SN/A        /** The sequence number of the instruction that renamed. */
2821060SN/A        InstSeqNum instSeqNum;
2832292SN/A        /** The architectural register index that was renamed. */
2841060SN/A        RegIndex archReg;
2852292SN/A        /** The new physical register that the arch. register is renamed to. */
2861060SN/A        PhysRegIndex newPhysReg;
2872292SN/A        /** The old physical register that the arch. register was renamed to. */
2881060SN/A        PhysRegIndex prevPhysReg;
2891060SN/A    };
2901060SN/A
2912292SN/A    /** A per-thread list of all destination register renames, used to either
2922292SN/A     * undo rename mappings or free old physical registers.
2932292SN/A     */
2942292SN/A    std::list<RenameHistory> historyBuffer[Impl::MaxThreads];
2951060SN/A
2962292SN/A    /** Pointer to CPU. */
2972733Sktlim@umich.edu    O3CPU *cpu;
2981060SN/A
2992292SN/A    /** Pointer to main time buffer used for backwards communication. */
3001060SN/A    TimeBuffer<TimeStruct> *timeBuffer;
3011060SN/A
3021060SN/A    /** Wire to get IEW's output from backwards time buffer. */
3031060SN/A    typename TimeBuffer<TimeStruct>::wire fromIEW;
3041060SN/A
3051060SN/A    /** Wire to get commit's output from backwards time buffer. */
3061060SN/A    typename TimeBuffer<TimeStruct>::wire fromCommit;
3071060SN/A
3081060SN/A    /** Wire to write infromation heading to previous stages. */
3091060SN/A    typename TimeBuffer<TimeStruct>::wire toDecode;
3101060SN/A
3111060SN/A    /** Rename instruction queue. */
3121060SN/A    TimeBuffer<RenameStruct> *renameQueue;
3131060SN/A
3141060SN/A    /** Wire to write any information heading to IEW. */
3151060SN/A    typename TimeBuffer<RenameStruct>::wire toIEW;
3161060SN/A
3171060SN/A    /** Decode instruction queue interface. */
3181060SN/A    TimeBuffer<DecodeStruct> *decodeQueue;
3191060SN/A
3201060SN/A    /** Wire to get decode's output from decode queue. */
3211060SN/A    typename TimeBuffer<DecodeStruct>::wire fromDecode;
3221060SN/A
3232292SN/A    /** Queue of all instructions coming from decode this cycle. */
3242292SN/A    InstQueue insts[Impl::MaxThreads];
3252292SN/A
3261060SN/A    /** Skid buffer between rename and decode. */
3272292SN/A    InstQueue skidBuffer[Impl::MaxThreads];
3281060SN/A
3291060SN/A    /** Rename map interface. */
3302292SN/A    RenameMap *renameMap[Impl::MaxThreads];
3311060SN/A
3321060SN/A    /** Free list interface. */
3331060SN/A    FreeList *freeList;
3341060SN/A
3352292SN/A    /** Pointer to the list of active threads. */
3362292SN/A    std::list<unsigned> *activeThreads;
3372292SN/A
3382292SN/A    /** Pointer to the scoreboard. */
3392292SN/A    Scoreboard *scoreboard;
3402292SN/A
3412292SN/A    /** Count of instructions in progress that have been sent off to the IQ
3422292SN/A     * and ROB, but are not yet included in their occupancy counts.
3432292SN/A     */
3442292SN/A    int instsInProgress[Impl::MaxThreads];
3452292SN/A
3462292SN/A    /** Variable that tracks if decode has written to the time buffer this
3472292SN/A     * cycle. Used to tell CPU if there is activity this cycle.
3482292SN/A     */
3492292SN/A    bool wroteToTimeBuffer;
3502292SN/A
3512292SN/A    /** Structures whose free entries impact the amount of instructions that
3522292SN/A     * can be renamed.
3532292SN/A     */
3542292SN/A    struct FreeEntries {
3552292SN/A        unsigned iqEntries;
3562292SN/A        unsigned lsqEntries;
3572292SN/A        unsigned robEntries;
3582292SN/A    };
3592292SN/A
3602292SN/A    /** Per-thread tracking of the number of free entries of back-end
3612292SN/A     * structures.
3622292SN/A     */
3632292SN/A    FreeEntries freeEntries[Impl::MaxThreads];
3642292SN/A
3652292SN/A    /** Records if the ROB is empty. In SMT mode the ROB may be dynamically
3662292SN/A     * partitioned between threads, so the ROB must tell rename when it is
3672292SN/A     * empty.
3682292SN/A     */
3692292SN/A    bool emptyROB[Impl::MaxThreads];
3702292SN/A
3712292SN/A    /** Source of possible stalls. */
3722292SN/A    struct Stalls {
3732292SN/A        bool iew;
3742292SN/A        bool commit;
3752292SN/A    };
3762292SN/A
3772292SN/A    /** Tracks which stages are telling decode to stall. */
3782292SN/A    Stalls stalls[Impl::MaxThreads];
3792292SN/A
3802301SN/A    /** The serialize instruction that rename has stalled on. */
3812301SN/A    DynInstPtr serializeInst[Impl::MaxThreads];
3822292SN/A
3832292SN/A    /** Records if rename needs to serialize on the next instruction for any
3842292SN/A     * thread.
3852292SN/A     */
3862292SN/A    bool serializeOnNextInst[Impl::MaxThreads];
3872292SN/A
3881060SN/A    /** Delay between iew and rename, in ticks. */
3891060SN/A    int iewToRenameDelay;
3901060SN/A
3911060SN/A    /** Delay between decode and rename, in ticks. */
3921060SN/A    int decodeToRenameDelay;
3931060SN/A
3941060SN/A    /** Delay between commit and rename, in ticks. */
3951060SN/A    unsigned commitToRenameDelay;
3961060SN/A
3971060SN/A    /** Rename width, in instructions. */
3981060SN/A    unsigned renameWidth;
3991060SN/A
4001060SN/A    /** Commit width, in instructions.  Used so rename knows how many
4011060SN/A     *  instructions might have freed registers in the previous cycle.
4021060SN/A     */
4031060SN/A    unsigned commitWidth;
4041061SN/A
4052292SN/A    /** The index of the instruction in the time buffer to IEW that rename is
4062292SN/A     * currently using.
4071061SN/A     */
4082292SN/A    unsigned toIEWIndex;
4091062SN/A
4102292SN/A    /** Whether or not rename needs to block this cycle. */
4112292SN/A    bool blockThisCycle;
4122292SN/A
4132292SN/A    /** The number of threads active in rename. */
4142292SN/A    unsigned numThreads;
4152292SN/A
4162292SN/A    /** The maximum skid buffer size. */
4172292SN/A    unsigned skidBufferMax;
4182292SN/A
4192292SN/A    /** Enum to record the source of a structure full stall.  Can come from
4202292SN/A     * either ROB, IQ, LSQ, and it is priortized in that order.
4212292SN/A     */
4222292SN/A    enum FullSource {
4232292SN/A        ROB,
4242292SN/A        IQ,
4252292SN/A        LSQ,
4262292SN/A        NONE
4272292SN/A    };
4282292SN/A
4292292SN/A    /** Function used to increment the stat that corresponds to the source of
4302292SN/A     * the stall.
4312292SN/A     */
4322292SN/A    inline void incrFullStat(const FullSource &source);
4332292SN/A
4342292SN/A    /** Stat for total number of cycles spent squashing. */
4351062SN/A    Stats::Scalar<> renameSquashCycles;
4362292SN/A    /** Stat for total number of cycles spent idle. */
4371062SN/A    Stats::Scalar<> renameIdleCycles;
4382292SN/A    /** Stat for total number of cycles spent blocking. */
4391062SN/A    Stats::Scalar<> renameBlockCycles;
4402301SN/A    /** Stat for total number of cycles spent stalling for a serializing inst. */
4412301SN/A    Stats::Scalar<> renameSerializeStallCycles;
4422292SN/A    /** Stat for total number of cycles spent running normally. */
4432292SN/A    Stats::Scalar<> renameRunCycles;
4442292SN/A    /** Stat for total number of cycles spent unblocking. */
4451062SN/A    Stats::Scalar<> renameUnblockCycles;
4462292SN/A    /** Stat for total number of renamed instructions. */
4471062SN/A    Stats::Scalar<> renameRenamedInsts;
4482292SN/A    /** Stat for total number of squashed instructions that rename discards. */
4491062SN/A    Stats::Scalar<> renameSquashedInsts;
4502292SN/A    /** Stat for total number of times that the ROB starts a stall in rename. */
4511062SN/A    Stats::Scalar<> renameROBFullEvents;
4522292SN/A    /** Stat for total number of times that the IQ starts a stall in rename. */
4531062SN/A    Stats::Scalar<> renameIQFullEvents;
4542292SN/A    /** Stat for total number of times that the LSQ starts a stall in rename. */
4552292SN/A    Stats::Scalar<> renameLSQFullEvents;
4562292SN/A    /** Stat for total number of times that rename runs out of free registers
4572292SN/A     * to use to rename. */
4581062SN/A    Stats::Scalar<> renameFullRegistersEvents;
4592292SN/A    /** Stat for total number of renamed destination registers. */
4601062SN/A    Stats::Scalar<> renameRenamedOperands;
4612292SN/A    /** Stat for total number of source register rename lookups. */
4621062SN/A    Stats::Scalar<> renameRenameLookups;
4632292SN/A    /** Stat for total number of committed renaming mappings. */
4641062SN/A    Stats::Scalar<> renameCommittedMaps;
4652292SN/A    /** Stat for total number of mappings that were undone due to a squash. */
4661062SN/A    Stats::Scalar<> renameUndoneMaps;
4672348SN/A    /** Number of serialize instructions handled. */
4682301SN/A    Stats::Scalar<> renamedSerializing;
4692348SN/A    /** Number of instructions marked as temporarily serializing. */
4702301SN/A    Stats::Scalar<> renamedTempSerializing;
4712348SN/A    /** Number of instructions inserted into skid buffers. */
4722307SN/A    Stats::Scalar<> renameSkidInsts;
4731060SN/A};
4741060SN/A
4752292SN/A#endif // __CPU_O3_RENAME_HH__
476