decode.hh revision 6221
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 */
301060SN/A
312292SN/A#ifndef __CPU_O3_DECODE_HH__
322292SN/A#define __CPU_O3_DECODE_HH__
331060SN/A
341060SN/A#include <queue>
351060SN/A
361461SN/A#include "base/statistics.hh"
371060SN/A#include "base/timebuf.hh"
381060SN/A
395529Snate@binkert.orgclass DerivO3CPUParams;
405529Snate@binkert.org
412292SN/A/**
422329SN/A * DefaultDecode class handles both single threaded and SMT
432329SN/A * decode. Its width is specified by the parameters; each cycles it
442329SN/A * tries to decode that many instructions. Because instructions are
452329SN/A * actually decoded when the StaticInst is created, this stage does
462329SN/A * not do much other than check any PC-relative branches.
472292SN/A */
481060SN/Atemplate<class Impl>
492292SN/Aclass DefaultDecode
501060SN/A{
511060SN/A  private:
521060SN/A    // Typedefs from the Impl.
532733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
541061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
551061SN/A    typedef typename Impl::CPUPol CPUPol;
561060SN/A
571061SN/A    // Typedefs from the CPU policy.
581061SN/A    typedef typename CPUPol::FetchStruct FetchStruct;
591061SN/A    typedef typename CPUPol::DecodeStruct DecodeStruct;
601061SN/A    typedef typename CPUPol::TimeStruct TimeStruct;
611060SN/A
621060SN/A  public:
632292SN/A    /** Overall decode stage status. Used to determine if the CPU can
642292SN/A     * deschedule itself due to a lack of activity.
652292SN/A     */
662292SN/A    enum DecodeStatus {
672292SN/A        Active,
682292SN/A        Inactive
692292SN/A    };
702292SN/A
712292SN/A    /** Individual thread status. */
722292SN/A    enum ThreadStatus {
731060SN/A        Running,
741060SN/A        Idle,
752292SN/A        StartSquash,
761060SN/A        Squashing,
771060SN/A        Blocked,
781060SN/A        Unblocking
791060SN/A    };
801060SN/A
811060SN/A  private:
822292SN/A    /** Decode status. */
832292SN/A    DecodeStatus _status;
842292SN/A
852292SN/A    /** Per-thread status. */
862292SN/A    ThreadStatus decodeStatus[Impl::MaxThreads];
871060SN/A
881060SN/A  public:
892292SN/A    /** DefaultDecode constructor. */
905529Snate@binkert.org    DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params);
911060SN/A
922292SN/A    /** Returns the name of decode. */
932292SN/A    std::string name() const;
942292SN/A
952292SN/A    /** Registers statistics. */
961062SN/A    void regStats();
971062SN/A
982292SN/A    /** Sets the main backwards communication time buffer pointer. */
991060SN/A    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
1001060SN/A
1012292SN/A    /** Sets pointer to time buffer used to communicate to the next stage. */
1021060SN/A    void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
1031060SN/A
1042292SN/A    /** Sets pointer to time buffer coming from fetch. */
1051060SN/A    void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
1061060SN/A
1072292SN/A    /** Sets pointer to list of active threads. */
1086221Snate@binkert.org    void setActiveThreads(std::list<ThreadID> *at_ptr);
1092292SN/A
1102843Sktlim@umich.edu    /** Drains the decode stage. */
1112863Sktlim@umich.edu    bool drain();
1122843Sktlim@umich.edu
1132843Sktlim@umich.edu    /** Resumes execution after a drain. */
1142843Sktlim@umich.edu    void resume() { }
1152843Sktlim@umich.edu
1162348SN/A    /** Switches out the decode stage. */
1172843Sktlim@umich.edu    void switchOut() { }
1182307SN/A
1192348SN/A    /** Takes over from another CPU's thread. */
1202307SN/A    void takeOverFrom();
1212348SN/A
1222292SN/A    /** Ticks decode, processing all input signals and decoding as many
1232292SN/A     * instructions as possible.
1242292SN/A     */
1251060SN/A    void tick();
1261060SN/A
1272292SN/A    /** Determines what to do based on decode's current status.
1282292SN/A     * @param status_change decode() sets this variable if there was a status
1292292SN/A     * change (ie switching from from blocking to unblocking).
1302292SN/A     * @param tid Thread id to decode instructions from.
1312292SN/A     */
1326221Snate@binkert.org    void decode(bool &status_change, ThreadID tid);
1332292SN/A
1342292SN/A    /** Processes instructions from fetch and passes them on to rename.
1352292SN/A     * Decoding of instructions actually happens when they are created in
1362292SN/A     * fetch, so this function mostly checks if PC-relative branches are
1372292SN/A     * correct.
1382292SN/A     */
1396221Snate@binkert.org    void decodeInsts(ThreadID tid);
1401060SN/A
1411060SN/A  private:
1422292SN/A    /** Inserts a thread's instructions into the skid buffer, to be decoded
1432292SN/A     * once decode unblocks.
1442292SN/A     */
1456221Snate@binkert.org    void skidInsert(ThreadID tid);
1462292SN/A
1472292SN/A    /** Returns if all of the skid buffers are empty. */
1482292SN/A    bool skidsEmpty();
1492292SN/A
1502292SN/A    /** Updates overall decode status based on all of the threads' statuses. */
1512292SN/A    void updateStatus();
1522292SN/A
1532292SN/A    /** Separates instructions from fetch into individual lists of instructions
1542292SN/A     * sorted by thread.
1552292SN/A     */
1562292SN/A    void sortInsts();
1572292SN/A
1582292SN/A    /** Reads all stall signals from the backwards communication timebuffer. */
1596221Snate@binkert.org    void readStallSignals(ThreadID tid);
1602292SN/A
1612292SN/A    /** Checks all input signals and updates decode's status appropriately. */
1626221Snate@binkert.org    bool checkSignalsAndUpdate(ThreadID tid);
1632292SN/A
1642292SN/A    /** Checks all stall signals, and returns if any are true. */
1656221Snate@binkert.org    bool checkStall(ThreadID tid) const;
1662292SN/A
1672292SN/A    /** Returns if there any instructions from fetch on this cycle. */
1681681SN/A    inline bool fetchInstsValid();
1691681SN/A
1702292SN/A    /** Switches decode to blocking, and signals back that decode has
1712292SN/A     * become blocked.
1722292SN/A     * @return Returns true if there is a status change.
1732292SN/A     */
1746221Snate@binkert.org    bool block(ThreadID tid);
1751060SN/A
1762292SN/A    /** Switches decode to unblocking if the skid buffer is empty, and
1772292SN/A     * signals back that decode has unblocked.
1782292SN/A     * @return Returns true if there is a status change.
1792292SN/A     */
1806221Snate@binkert.org    bool unblock(ThreadID tid);
1811060SN/A
1822292SN/A    /** Squashes if there is a PC-relative branch that was predicted
1832292SN/A     * incorrectly. Sends squash information back to fetch.
1842292SN/A     */
1856221Snate@binkert.org    void squash(DynInstPtr &inst, ThreadID tid);
1861060SN/A
1871684SN/A  public:
1882292SN/A    /** Squashes due to commit signalling a squash. Changes status to
1892292SN/A     * squashing and clears block/unblock signals as needed.
1902292SN/A     */
1916221Snate@binkert.org    unsigned squash(ThreadID tid);
1921681SN/A
1931684SN/A  private:
1941060SN/A    // Interfaces to objects outside of decode.
1951060SN/A    /** CPU interface. */
1962733Sktlim@umich.edu    O3CPU *cpu;
1971060SN/A
1981060SN/A    /** Time buffer interface. */
1991060SN/A    TimeBuffer<TimeStruct> *timeBuffer;
2001060SN/A
2011060SN/A    /** Wire to get rename's output from backwards time buffer. */
2021060SN/A    typename TimeBuffer<TimeStruct>::wire fromRename;
2031060SN/A
2041060SN/A    /** Wire to get iew's information from backwards time buffer. */
2051060SN/A    typename TimeBuffer<TimeStruct>::wire fromIEW;
2061060SN/A
2071060SN/A    /** Wire to get commit's information from backwards time buffer. */
2081060SN/A    typename TimeBuffer<TimeStruct>::wire fromCommit;
2091060SN/A
2101060SN/A    /** Wire to write information heading to previous stages. */
2111060SN/A    // Might not be the best name as not only fetch will read it.
2121060SN/A    typename TimeBuffer<TimeStruct>::wire toFetch;
2131060SN/A
2141060SN/A    /** Decode instruction queue. */
2151060SN/A    TimeBuffer<DecodeStruct> *decodeQueue;
2161060SN/A
2171060SN/A    /** Wire used to write any information heading to rename. */
2181060SN/A    typename TimeBuffer<DecodeStruct>::wire toRename;
2191060SN/A
2201060SN/A    /** Fetch instruction queue interface. */
2211060SN/A    TimeBuffer<FetchStruct> *fetchQueue;
2221060SN/A
2231060SN/A    /** Wire to get fetch's output from fetch queue. */
2241060SN/A    typename TimeBuffer<FetchStruct>::wire fromFetch;
2251060SN/A
2262292SN/A    /** Queue of all instructions coming from fetch this cycle. */
2272292SN/A    std::queue<DynInstPtr> insts[Impl::MaxThreads];
2282292SN/A
2291060SN/A    /** Skid buffer between fetch and decode. */
2302292SN/A    std::queue<DynInstPtr> skidBuffer[Impl::MaxThreads];
2311060SN/A
2322292SN/A    /** Variable that tracks if decode has written to the time buffer this
2332292SN/A     * cycle. Used to tell CPU if there is activity this cycle.
2342292SN/A     */
2352292SN/A    bool wroteToTimeBuffer;
2362292SN/A
2372292SN/A    /** Source of possible stalls. */
2382292SN/A    struct Stalls {
2392292SN/A        bool rename;
2402292SN/A        bool iew;
2412292SN/A        bool commit;
2422292SN/A    };
2432292SN/A
2442292SN/A    /** Tracks which stages are telling decode to stall. */
2452292SN/A    Stalls stalls[Impl::MaxThreads];
2462292SN/A
2471060SN/A    /** Rename to decode delay, in ticks. */
2481060SN/A    unsigned renameToDecodeDelay;
2491060SN/A
2501060SN/A    /** IEW to decode delay, in ticks. */
2511060SN/A    unsigned iewToDecodeDelay;
2521060SN/A
2531060SN/A    /** Commit to decode delay, in ticks. */
2541060SN/A    unsigned commitToDecodeDelay;
2551060SN/A
2561060SN/A    /** Fetch to decode delay, in ticks. */
2571060SN/A    unsigned fetchToDecodeDelay;
2581060SN/A
2591060SN/A    /** The width of decode, in instructions. */
2601060SN/A    unsigned decodeWidth;
2611061SN/A
2622292SN/A    /** Index of instructions being sent to rename. */
2632292SN/A    unsigned toRenameIndex;
2642292SN/A
2652292SN/A    /** number of Active Threads*/
2666221Snate@binkert.org    ThreadID numThreads;
2672292SN/A
2682292SN/A    /** List of active thread ids */
2696221Snate@binkert.org    std::list<ThreadID> *activeThreads;
2702292SN/A
2712292SN/A    /** Number of branches in flight. */
2722292SN/A    unsigned branchCount[Impl::MaxThreads];
2732292SN/A
2742292SN/A    /** Maximum size of the skid buffer. */
2752292SN/A    unsigned skidBufferMax;
2762292SN/A
2772935Sksewell@umich.edu    /** SeqNum of Squashing Branch Delay Instruction (used for MIPS)*/
2782935Sksewell@umich.edu    Addr bdelayDoneSeqNum[Impl::MaxThreads];
2792935Sksewell@umich.edu
2802935Sksewell@umich.edu    /** Instruction used for squashing branch (used for MIPS)*/
2812935Sksewell@umich.edu    DynInstPtr squashInst[Impl::MaxThreads];
2822935Sksewell@umich.edu
2832935Sksewell@umich.edu    /** Tells when their is a pending delay slot inst. to send
2842935Sksewell@umich.edu     *  to rename. If there is, then wait squash after the next
2852935Sksewell@umich.edu     *  instruction (used for MIPS).
2862935Sksewell@umich.edu     */
2872935Sksewell@umich.edu    bool squashAfterDelaySlot[Impl::MaxThreads];
2882935Sksewell@umich.edu
2892935Sksewell@umich.edu
2902292SN/A    /** Stat for total number of idle cycles. */
2915999Snate@binkert.org    Stats::Scalar decodeIdleCycles;
2922292SN/A    /** Stat for total number of blocked cycles. */
2935999Snate@binkert.org    Stats::Scalar decodeBlockedCycles;
2942292SN/A    /** Stat for total number of normal running cycles. */
2955999Snate@binkert.org    Stats::Scalar decodeRunCycles;
2962292SN/A    /** Stat for total number of unblocking cycles. */
2975999Snate@binkert.org    Stats::Scalar decodeUnblockCycles;
2982292SN/A    /** Stat for total number of squashing cycles. */
2995999Snate@binkert.org    Stats::Scalar decodeSquashCycles;
3002307SN/A    /** Stat for number of times a branch is resolved at decode. */
3015999Snate@binkert.org    Stats::Scalar decodeBranchResolved;
3022292SN/A    /** Stat for number of times a branch mispredict is detected. */
3035999Snate@binkert.org    Stats::Scalar decodeBranchMispred;
3042292SN/A    /** Stat for number of times decode detected a non-control instruction
3052292SN/A     * incorrectly predicted as a branch.
3061061SN/A     */
3075999Snate@binkert.org    Stats::Scalar decodeControlMispred;
3082292SN/A    /** Stat for total number of decoded instructions. */
3095999Snate@binkert.org    Stats::Scalar decodeDecodedInsts;
3102292SN/A    /** Stat for total number of squashed instructions. */
3115999Snate@binkert.org    Stats::Scalar decodeSquashedInsts;
3121060SN/A};
3131060SN/A
3142292SN/A#endif // __CPU_O3_DECODE_HH__
315