decode.hh revision 13429
11689SN/A/*
29444SAndreas.Sandberg@ARM.com * Copyright (c) 2012 ARM Limited
39444SAndreas.Sandberg@ARM.com * All rights reserved
49444SAndreas.Sandberg@ARM.com *
59444SAndreas.Sandberg@ARM.com * The license below extends only to copyright in the software and shall
69444SAndreas.Sandberg@ARM.com * not be construed as granting a license to any other intellectual
79444SAndreas.Sandberg@ARM.com * property including but not limited to intellectual property relating
89444SAndreas.Sandberg@ARM.com * to a hardware implementation of the functionality of the software
99444SAndreas.Sandberg@ARM.com * licensed hereunder.  You may use the software subject to the license
109444SAndreas.Sandberg@ARM.com * terms below provided that you ensure that this notice is replicated
119444SAndreas.Sandberg@ARM.com * unmodified and in its entirety in all distributions of the software,
129444SAndreas.Sandberg@ARM.com * modified or unmodified, in source code or in binary form.
139444SAndreas.Sandberg@ARM.com *
142329SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
151689SN/A * All rights reserved.
161689SN/A *
171689SN/A * Redistribution and use in source and binary forms, with or without
181689SN/A * modification, are permitted provided that the following conditions are
191689SN/A * met: redistributions of source code must retain the above copyright
201689SN/A * notice, this list of conditions and the following disclaimer;
211689SN/A * redistributions in binary form must reproduce the above copyright
221689SN/A * notice, this list of conditions and the following disclaimer in the
231689SN/A * documentation and/or other materials provided with the distribution;
241689SN/A * neither the name of the copyright holders nor the names of its
251689SN/A * contributors may be used to endorse or promote products derived from
261689SN/A * this software without specific prior written permission.
271689SN/A *
281689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
371689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
381689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
411689SN/A */
421060SN/A
432292SN/A#ifndef __CPU_O3_DECODE_HH__
442292SN/A#define __CPU_O3_DECODE_HH__
451060SN/A
461060SN/A#include <queue>
471060SN/A
481461SN/A#include "base/statistics.hh"
497813Ssteve.reinhardt@amd.com#include "cpu/timebuf.hh"
501060SN/A
518737Skoansin.tan@gmail.comstruct DerivO3CPUParams;
525529Snate@binkert.org
532292SN/A/**
542329SN/A * DefaultDecode class handles both single threaded and SMT
552329SN/A * decode. Its width is specified by the parameters; each cycles it
562329SN/A * tries to decode that many instructions. Because instructions are
572329SN/A * actually decoded when the StaticInst is created, this stage does
582329SN/A * not do much other than check any PC-relative branches.
592292SN/A */
601060SN/Atemplate<class Impl>
612292SN/Aclass DefaultDecode
621060SN/A{
631060SN/A  private:
641060SN/A    // Typedefs from the Impl.
652733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
661061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
671061SN/A    typedef typename Impl::CPUPol CPUPol;
681060SN/A
691061SN/A    // Typedefs from the CPU policy.
701061SN/A    typedef typename CPUPol::FetchStruct FetchStruct;
711061SN/A    typedef typename CPUPol::DecodeStruct DecodeStruct;
721061SN/A    typedef typename CPUPol::TimeStruct TimeStruct;
731060SN/A
741060SN/A  public:
752292SN/A    /** Overall decode stage status. Used to determine if the CPU can
762292SN/A     * deschedule itself due to a lack of activity.
772292SN/A     */
782292SN/A    enum DecodeStatus {
792292SN/A        Active,
802292SN/A        Inactive
812292SN/A    };
822292SN/A
832292SN/A    /** Individual thread status. */
842292SN/A    enum ThreadStatus {
851060SN/A        Running,
861060SN/A        Idle,
872292SN/A        StartSquash,
881060SN/A        Squashing,
891060SN/A        Blocked,
901060SN/A        Unblocking
911060SN/A    };
921060SN/A
931060SN/A  private:
942292SN/A    /** Decode status. */
952292SN/A    DecodeStatus _status;
962292SN/A
972292SN/A    /** Per-thread status. */
982292SN/A    ThreadStatus decodeStatus[Impl::MaxThreads];
991060SN/A
1001060SN/A  public:
1012292SN/A    /** DefaultDecode constructor. */
1025529Snate@binkert.org    DefaultDecode(O3CPU *_cpu, DerivO3CPUParams *params);
1031060SN/A
1049444SAndreas.Sandberg@ARM.com    void startupStage();
1059444SAndreas.Sandberg@ARM.com    void resetStage();
1069444SAndreas.Sandberg@ARM.com
1072292SN/A    /** Returns the name of decode. */
1082292SN/A    std::string name() const;
1092292SN/A
1102292SN/A    /** Registers statistics. */
1111062SN/A    void regStats();
1121062SN/A
1132292SN/A    /** Sets the main backwards communication time buffer pointer. */
1141060SN/A    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
1151060SN/A
1162292SN/A    /** Sets pointer to time buffer used to communicate to the next stage. */
1171060SN/A    void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
1181060SN/A
1192292SN/A    /** Sets pointer to time buffer coming from fetch. */
1201060SN/A    void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
1211060SN/A
1222292SN/A    /** Sets pointer to list of active threads. */
1236221Snate@binkert.org    void setActiveThreads(std::list<ThreadID> *at_ptr);
1242292SN/A
1259444SAndreas.Sandberg@ARM.com    /** Perform sanity checks after a drain. */
1269444SAndreas.Sandberg@ARM.com    void drainSanityCheck() const;
1272843Sktlim@umich.edu
1289444SAndreas.Sandberg@ARM.com    /** Has the stage drained? */
12910328Smitch.hayenga@arm.com    bool isDrained() const;
1302307SN/A
1312348SN/A    /** Takes over from another CPU's thread. */
1329444SAndreas.Sandberg@ARM.com    void takeOverFrom() { resetStage(); }
1332348SN/A
1342292SN/A    /** Ticks decode, processing all input signals and decoding as many
1352292SN/A     * instructions as possible.
1362292SN/A     */
1371060SN/A    void tick();
1381060SN/A
1392292SN/A    /** Determines what to do based on decode's current status.
1402292SN/A     * @param status_change decode() sets this variable if there was a status
1412292SN/A     * change (ie switching from from blocking to unblocking).
1422292SN/A     * @param tid Thread id to decode instructions from.
1432292SN/A     */
1446221Snate@binkert.org    void decode(bool &status_change, ThreadID tid);
1452292SN/A
1462292SN/A    /** Processes instructions from fetch and passes them on to rename.
1472292SN/A     * Decoding of instructions actually happens when they are created in
1482292SN/A     * fetch, so this function mostly checks if PC-relative branches are
1492292SN/A     * correct.
1502292SN/A     */
1516221Snate@binkert.org    void decodeInsts(ThreadID tid);
1521060SN/A
1531060SN/A  private:
1542292SN/A    /** Inserts a thread's instructions into the skid buffer, to be decoded
1552292SN/A     * once decode unblocks.
1562292SN/A     */
1576221Snate@binkert.org    void skidInsert(ThreadID tid);
1582292SN/A
1592292SN/A    /** Returns if all of the skid buffers are empty. */
1602292SN/A    bool skidsEmpty();
1612292SN/A
1622292SN/A    /** Updates overall decode status based on all of the threads' statuses. */
1632292SN/A    void updateStatus();
1642292SN/A
1652292SN/A    /** Separates instructions from fetch into individual lists of instructions
1662292SN/A     * sorted by thread.
1672292SN/A     */
1682292SN/A    void sortInsts();
1692292SN/A
1702292SN/A    /** Reads all stall signals from the backwards communication timebuffer. */
1716221Snate@binkert.org    void readStallSignals(ThreadID tid);
1722292SN/A
1732292SN/A    /** Checks all input signals and updates decode's status appropriately. */
1746221Snate@binkert.org    bool checkSignalsAndUpdate(ThreadID tid);
1752292SN/A
1762292SN/A    /** Checks all stall signals, and returns if any are true. */
1776221Snate@binkert.org    bool checkStall(ThreadID tid) const;
1782292SN/A
1792292SN/A    /** Returns if there any instructions from fetch on this cycle. */
1801681SN/A    inline bool fetchInstsValid();
1811681SN/A
1822292SN/A    /** Switches decode to blocking, and signals back that decode has
1832292SN/A     * become blocked.
1842292SN/A     * @return Returns true if there is a status change.
1852292SN/A     */
1866221Snate@binkert.org    bool block(ThreadID tid);
1871060SN/A
1882292SN/A    /** Switches decode to unblocking if the skid buffer is empty, and
1892292SN/A     * signals back that decode has unblocked.
1902292SN/A     * @return Returns true if there is a status change.
1912292SN/A     */
1926221Snate@binkert.org    bool unblock(ThreadID tid);
1931060SN/A
1942292SN/A    /** Squashes if there is a PC-relative branch that was predicted
1952292SN/A     * incorrectly. Sends squash information back to fetch.
1962292SN/A     */
19713429Srekai.gonzalezalberquilla@arm.com    void squash(const DynInstPtr &inst, ThreadID tid);
1981060SN/A
1991684SN/A  public:
2002292SN/A    /** Squashes due to commit signalling a squash. Changes status to
2012292SN/A     * squashing and clears block/unblock signals as needed.
2022292SN/A     */
2036221Snate@binkert.org    unsigned squash(ThreadID tid);
2041681SN/A
2051684SN/A  private:
2061060SN/A    // Interfaces to objects outside of decode.
2071060SN/A    /** CPU interface. */
2082733Sktlim@umich.edu    O3CPU *cpu;
2091060SN/A
2101060SN/A    /** Time buffer interface. */
2111060SN/A    TimeBuffer<TimeStruct> *timeBuffer;
2121060SN/A
2131060SN/A    /** Wire to get rename's output from backwards time buffer. */
2141060SN/A    typename TimeBuffer<TimeStruct>::wire fromRename;
2151060SN/A
2161060SN/A    /** Wire to get iew's information from backwards time buffer. */
2171060SN/A    typename TimeBuffer<TimeStruct>::wire fromIEW;
2181060SN/A
2191060SN/A    /** Wire to get commit's information from backwards time buffer. */
2201060SN/A    typename TimeBuffer<TimeStruct>::wire fromCommit;
2211060SN/A
2221060SN/A    /** Wire to write information heading to previous stages. */
2231060SN/A    // Might not be the best name as not only fetch will read it.
2241060SN/A    typename TimeBuffer<TimeStruct>::wire toFetch;
2251060SN/A
2261060SN/A    /** Decode instruction queue. */
2271060SN/A    TimeBuffer<DecodeStruct> *decodeQueue;
2281060SN/A
2291060SN/A    /** Wire used to write any information heading to rename. */
2301060SN/A    typename TimeBuffer<DecodeStruct>::wire toRename;
2311060SN/A
2321060SN/A    /** Fetch instruction queue interface. */
2331060SN/A    TimeBuffer<FetchStruct> *fetchQueue;
2341060SN/A
2351060SN/A    /** Wire to get fetch's output from fetch queue. */
2361060SN/A    typename TimeBuffer<FetchStruct>::wire fromFetch;
2371060SN/A
2382292SN/A    /** Queue of all instructions coming from fetch this cycle. */
2392292SN/A    std::queue<DynInstPtr> insts[Impl::MaxThreads];
2402292SN/A
2411060SN/A    /** Skid buffer between fetch and decode. */
2422292SN/A    std::queue<DynInstPtr> skidBuffer[Impl::MaxThreads];
2431060SN/A
2442292SN/A    /** Variable that tracks if decode has written to the time buffer this
2452292SN/A     * cycle. Used to tell CPU if there is activity this cycle.
2462292SN/A     */
2472292SN/A    bool wroteToTimeBuffer;
2482292SN/A
2492292SN/A    /** Source of possible stalls. */
2502292SN/A    struct Stalls {
2512292SN/A        bool rename;
2522292SN/A    };
2532292SN/A
2542292SN/A    /** Tracks which stages are telling decode to stall. */
2552292SN/A    Stalls stalls[Impl::MaxThreads];
2562292SN/A
2579184Sandreas.hansson@arm.com    /** Rename to decode delay. */
2589184Sandreas.hansson@arm.com    Cycles renameToDecodeDelay;
2591060SN/A
2609184Sandreas.hansson@arm.com    /** IEW to decode delay. */
2619184Sandreas.hansson@arm.com    Cycles iewToDecodeDelay;
2621060SN/A
2639184Sandreas.hansson@arm.com    /** Commit to decode delay. */
2649184Sandreas.hansson@arm.com    Cycles commitToDecodeDelay;
2651060SN/A
2669184Sandreas.hansson@arm.com    /** Fetch to decode delay. */
2679184Sandreas.hansson@arm.com    Cycles fetchToDecodeDelay;
2681060SN/A
2691060SN/A    /** The width of decode, in instructions. */
2701060SN/A    unsigned decodeWidth;
2711061SN/A
2722292SN/A    /** Index of instructions being sent to rename. */
2732292SN/A    unsigned toRenameIndex;
2742292SN/A
2752292SN/A    /** number of Active Threads*/
2766221Snate@binkert.org    ThreadID numThreads;
2772292SN/A
2782292SN/A    /** List of active thread ids */
2796221Snate@binkert.org    std::list<ThreadID> *activeThreads;
2802292SN/A
2812292SN/A    /** Maximum size of the skid buffer. */
2822292SN/A    unsigned skidBufferMax;
2832292SN/A
2842935Sksewell@umich.edu    /** SeqNum of Squashing Branch Delay Instruction (used for MIPS)*/
2852935Sksewell@umich.edu    Addr bdelayDoneSeqNum[Impl::MaxThreads];
2862935Sksewell@umich.edu
2872935Sksewell@umich.edu    /** Instruction used for squashing branch (used for MIPS)*/
2882935Sksewell@umich.edu    DynInstPtr squashInst[Impl::MaxThreads];
2892935Sksewell@umich.edu
2902935Sksewell@umich.edu    /** Tells when their is a pending delay slot inst. to send
2912935Sksewell@umich.edu     *  to rename. If there is, then wait squash after the next
2922935Sksewell@umich.edu     *  instruction (used for MIPS).
2932935Sksewell@umich.edu     */
2942935Sksewell@umich.edu    bool squashAfterDelaySlot[Impl::MaxThreads];
2952935Sksewell@umich.edu
2962935Sksewell@umich.edu
2972292SN/A    /** Stat for total number of idle cycles. */
2985999Snate@binkert.org    Stats::Scalar decodeIdleCycles;
2992292SN/A    /** Stat for total number of blocked cycles. */
3005999Snate@binkert.org    Stats::Scalar decodeBlockedCycles;
3012292SN/A    /** Stat for total number of normal running cycles. */
3025999Snate@binkert.org    Stats::Scalar decodeRunCycles;
3032292SN/A    /** Stat for total number of unblocking cycles. */
3045999Snate@binkert.org    Stats::Scalar decodeUnblockCycles;
3052292SN/A    /** Stat for total number of squashing cycles. */
3065999Snate@binkert.org    Stats::Scalar decodeSquashCycles;
3072307SN/A    /** Stat for number of times a branch is resolved at decode. */
3085999Snate@binkert.org    Stats::Scalar decodeBranchResolved;
3092292SN/A    /** Stat for number of times a branch mispredict is detected. */
3105999Snate@binkert.org    Stats::Scalar decodeBranchMispred;
3112292SN/A    /** Stat for number of times decode detected a non-control instruction
3122292SN/A     * incorrectly predicted as a branch.
3131061SN/A     */
3145999Snate@binkert.org    Stats::Scalar decodeControlMispred;
3152292SN/A    /** Stat for total number of decoded instructions. */
3165999Snate@binkert.org    Stats::Scalar decodeDecodedInsts;
3172292SN/A    /** Stat for total number of squashed instructions. */
3185999Snate@binkert.org    Stats::Scalar decodeSquashedInsts;
3191060SN/A};
3201060SN/A
3212292SN/A#endif // __CPU_O3_DECODE_HH__
322