decode.hh revision 2733
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright (c) 2004-2006 The Regents of The University of Michigan
312837Sgabeblack@google.com * All rights reserved.
412837Sgabeblack@google.com *
512837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412837Sgabeblack@google.com * this software without specific prior written permission.
1512837Sgabeblack@google.com *
1612837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712837Sgabeblack@google.com *
2812837Sgabeblack@google.com * Authors: Kevin Lim
2912837Sgabeblack@google.com */
3012837Sgabeblack@google.com
3113081Sgabeblack@google.com#ifndef __CPU_O3_DECODE_HH__
3212837Sgabeblack@google.com#define __CPU_O3_DECODE_HH__
3312862Sgabeblack@google.com
3412837Sgabeblack@google.com#include <queue>
3512862Sgabeblack@google.com
3612956Sgabeblack@google.com#include "base/statistics.hh"
3712862Sgabeblack@google.com#include "base/timebuf.hh"
3812837Sgabeblack@google.com
3912982Sgabeblack@google.com/**
4013038Sgabeblack@google.com * DefaultDecode class handles both single threaded and SMT
4112956Sgabeblack@google.com * decode. Its width is specified by the parameters; each cycles it
4212837Sgabeblack@google.com * tries to decode that many instructions. Because instructions are
4312861Sgabeblack@google.com * actually decoded when the StaticInst is created, this stage does
4412837Sgabeblack@google.com * not do much other than check any PC-relative branches.
4512949Sgabeblack@google.com */
4612949Sgabeblack@google.comtemplate<class Impl>
4712837Sgabeblack@google.comclass DefaultDecode
4812837Sgabeblack@google.com{
4912837Sgabeblack@google.com  private:
5012837Sgabeblack@google.com    // Typedefs from the Impl.
5112837Sgabeblack@google.com    typedef typename Impl::O3CPU O3CPU;
5212837Sgabeblack@google.com    typedef typename Impl::DynInstPtr DynInstPtr;
5312837Sgabeblack@google.com    typedef typename Impl::Params Params;
5412837Sgabeblack@google.com    typedef typename Impl::CPUPol CPUPol;
5512837Sgabeblack@google.com
5612837Sgabeblack@google.com    // Typedefs from the CPU policy.
5712837Sgabeblack@google.com    typedef typename CPUPol::FetchStruct FetchStruct;
5812837Sgabeblack@google.com    typedef typename CPUPol::DecodeStruct DecodeStruct;
5912862Sgabeblack@google.com    typedef typename CPUPol::TimeStruct TimeStruct;
6012862Sgabeblack@google.com
6113081Sgabeblack@google.com  public:
6213081Sgabeblack@google.com    /** Overall decode stage status. Used to determine if the CPU can
6313081Sgabeblack@google.com     * deschedule itself due to a lack of activity.
6413081Sgabeblack@google.com     */
6513081Sgabeblack@google.com    enum DecodeStatus {
6613081Sgabeblack@google.com        Active,
6712862Sgabeblack@google.com        Inactive
6812862Sgabeblack@google.com    };
6912862Sgabeblack@google.com
7012949Sgabeblack@google.com    /** Individual thread status. */
7113081Sgabeblack@google.com    enum ThreadStatus {
7213081Sgabeblack@google.com        Running,
7313081Sgabeblack@google.com        Idle,
7413081Sgabeblack@google.com        StartSquash,
7513081Sgabeblack@google.com        Squashing,
7613081Sgabeblack@google.com        Blocked,
7713081Sgabeblack@google.com        Unblocking
7813081Sgabeblack@google.com    };
7913081Sgabeblack@google.com
8013081Sgabeblack@google.com  private:
8113081Sgabeblack@google.com    /** Decode status. */
8213182Sgabeblack@google.com    DecodeStatus _status;
8313182Sgabeblack@google.com
8413182Sgabeblack@google.com    /** Per-thread status. */
8513182Sgabeblack@google.com    ThreadStatus decodeStatus[Impl::MaxThreads];
8613081Sgabeblack@google.com
8713077Sgabeblack@google.com  public:
8813077Sgabeblack@google.com    /** DefaultDecode constructor. */
8912949Sgabeblack@google.com    DefaultDecode(Params *params);
9012949Sgabeblack@google.com
9112949Sgabeblack@google.com    /** Returns the name of decode. */
9212949Sgabeblack@google.com    std::string name() const;
9312862Sgabeblack@google.com
9412862Sgabeblack@google.com    /** Registers statistics. */
9512862Sgabeblack@google.com    void regStats();
9612862Sgabeblack@google.com
9712862Sgabeblack@google.com    /** Sets CPU pointer. */
9812837Sgabeblack@google.com    void setCPU(O3CPU *cpu_ptr);
9912837Sgabeblack@google.com
10012837Sgabeblack@google.com    /** Sets the main backwards communication time buffer pointer. */
10112837Sgabeblack@google.com    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
10212837Sgabeblack@google.com
10312837Sgabeblack@google.com    /** Sets pointer to time buffer used to communicate to the next stage. */
10412837Sgabeblack@google.com    void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
10512837Sgabeblack@google.com
10612837Sgabeblack@google.com    /** Sets pointer to time buffer coming from fetch. */
10712837Sgabeblack@google.com    void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
10812837Sgabeblack@google.com
10912837Sgabeblack@google.com    /** Sets pointer to list of active threads. */
11012837Sgabeblack@google.com    void setActiveThreads(std::list<unsigned> *at_ptr);
11112837Sgabeblack@google.com
11212837Sgabeblack@google.com    /** Switches out the decode stage. */
11312837Sgabeblack@google.com    void switchOut();
11412837Sgabeblack@google.com
11512837Sgabeblack@google.com    /** Takes over from another CPU's thread. */
11612837Sgabeblack@google.com    void takeOverFrom();
11712837Sgabeblack@google.com
11812837Sgabeblack@google.com    /** Ticks decode, processing all input signals and decoding as many
11912837Sgabeblack@google.com     * instructions as possible.
12012837Sgabeblack@google.com     */
12112837Sgabeblack@google.com    void tick();
12212837Sgabeblack@google.com
12312837Sgabeblack@google.com    /** Determines what to do based on decode's current status.
12412837Sgabeblack@google.com     * @param status_change decode() sets this variable if there was a status
12512837Sgabeblack@google.com     * change (ie switching from from blocking to unblocking).
12612837Sgabeblack@google.com     * @param tid Thread id to decode instructions from.
12712837Sgabeblack@google.com     */
12812837Sgabeblack@google.com    void decode(bool &status_change, unsigned tid);
12912837Sgabeblack@google.com
13012837Sgabeblack@google.com    /** Processes instructions from fetch and passes them on to rename.
13112837Sgabeblack@google.com     * Decoding of instructions actually happens when they are created in
13212837Sgabeblack@google.com     * fetch, so this function mostly checks if PC-relative branches are
13312837Sgabeblack@google.com     * correct.
13412837Sgabeblack@google.com     */
13512862Sgabeblack@google.com    void decodeInsts(unsigned tid);
13612837Sgabeblack@google.com
13712837Sgabeblack@google.com  private:
13813081Sgabeblack@google.com    /** Inserts a thread's instructions into the skid buffer, to be decoded
13913081Sgabeblack@google.com     * once decode unblocks.
14013081Sgabeblack@google.com     */
14113081Sgabeblack@google.com    void skidInsert(unsigned tid);
14213081Sgabeblack@google.com
14313081Sgabeblack@google.com    /** Returns if all of the skid buffers are empty. */
14413081Sgabeblack@google.com    bool skidsEmpty();
14513081Sgabeblack@google.com
14613081Sgabeblack@google.com    /** Updates overall decode status based on all of the threads' statuses. */
14713081Sgabeblack@google.com    void updateStatus();
14813081Sgabeblack@google.com
14913081Sgabeblack@google.com    /** Separates instructions from fetch into individual lists of instructions
15012837Sgabeblack@google.com     * sorted by thread.
15112837Sgabeblack@google.com     */
15213038Sgabeblack@google.com    void sortInsts();
15313038Sgabeblack@google.com
15412837Sgabeblack@google.com    /** Reads all stall signals from the backwards communication timebuffer. */
15513038Sgabeblack@google.com    void readStallSignals(unsigned tid);
15613038Sgabeblack@google.com
15713038Sgabeblack@google.com    /** Checks all input signals and updates decode's status appropriately. */
15813038Sgabeblack@google.com    bool checkSignalsAndUpdate(unsigned tid);
15913081Sgabeblack@google.com
16013081Sgabeblack@google.com    /** Checks all stall signals, and returns if any are true. */
16113038Sgabeblack@google.com    bool checkStall(unsigned tid) const;
16213038Sgabeblack@google.com
16312837Sgabeblack@google.com    /** Returns if there any instructions from fetch on this cycle. */
16412861Sgabeblack@google.com    inline bool fetchInstsValid();
16512861Sgabeblack@google.com
16612837Sgabeblack@google.com    /** Switches decode to blocking, and signals back that decode has
16712837Sgabeblack@google.com     * become blocked.
16812837Sgabeblack@google.com     * @return Returns true if there is a status change.
16912837Sgabeblack@google.com     */
17012837Sgabeblack@google.com    bool block(unsigned tid);
17112837Sgabeblack@google.com
17212837Sgabeblack@google.com    /** Switches decode to unblocking if the skid buffer is empty, and
17312837Sgabeblack@google.com     * signals back that decode has unblocked.
17412837Sgabeblack@google.com     * @return Returns true if there is a status change.
17512837Sgabeblack@google.com     */
17612837Sgabeblack@google.com    bool unblock(unsigned tid);
17712837Sgabeblack@google.com
17812837Sgabeblack@google.com    /** Squashes if there is a PC-relative branch that was predicted
17912837Sgabeblack@google.com     * incorrectly. Sends squash information back to fetch.
18012860Sgabeblack@google.com     */
18112860Sgabeblack@google.com    void squash(DynInstPtr &inst, unsigned tid);
18212860Sgabeblack@google.com
18312962Sgabeblack@google.com  public:
18412961Sgabeblack@google.com    /** Squashes due to commit signalling a squash. Changes status to
18512860Sgabeblack@google.com     * squashing and clears block/unblock signals as needed.
18612860Sgabeblack@google.com     */
18712860Sgabeblack@google.com    unsigned squash(unsigned tid);
18812860Sgabeblack@google.com
18912860Sgabeblack@google.com  private:
19012990Sgabeblack@google.com    // Interfaces to objects outside of decode.
19112961Sgabeblack@google.com    /** CPU interface. */
19212860Sgabeblack@google.com    O3CPU *cpu;
19312860Sgabeblack@google.com
19412860Sgabeblack@google.com    /** Time buffer interface. */
19512860Sgabeblack@google.com    TimeBuffer<TimeStruct> *timeBuffer;
19612860Sgabeblack@google.com
19713061Sgabeblack@google.com    /** Wire to get rename's output from backwards time buffer. */
19813061Sgabeblack@google.com    typename TimeBuffer<TimeStruct>::wire fromRename;
19913061Sgabeblack@google.com
20013061Sgabeblack@google.com    /** Wire to get iew's information from backwards time buffer. */
20113084Sgabeblack@google.com    typename TimeBuffer<TimeStruct>::wire fromIEW;
20213084Sgabeblack@google.com
20313084Sgabeblack@google.com    /** Wire to get commit's information from backwards time buffer. */
20413084Sgabeblack@google.com    typename TimeBuffer<TimeStruct>::wire fromCommit;
20513061Sgabeblack@google.com
20613061Sgabeblack@google.com    /** Wire to write information heading to previous stages. */
20712860Sgabeblack@google.com    // Might not be the best name as not only fetch will read it.
20812860Sgabeblack@google.com    typename TimeBuffer<TimeStruct>::wire toFetch;
20912860Sgabeblack@google.com
21012860Sgabeblack@google.com    /** Decode instruction queue. */
21112860Sgabeblack@google.com    TimeBuffer<DecodeStruct> *decodeQueue;
21212861Sgabeblack@google.com
21312861Sgabeblack@google.com    /** Wire used to write any information heading to rename. */
21412861Sgabeblack@google.com    typename TimeBuffer<DecodeStruct>::wire toRename;
21512861Sgabeblack@google.com
21612861Sgabeblack@google.com    /** Fetch instruction queue interface. */
21712861Sgabeblack@google.com    TimeBuffer<FetchStruct> *fetchQueue;
21812860Sgabeblack@google.com
21912860Sgabeblack@google.com    /** Wire to get fetch's output from fetch queue. */
22012860Sgabeblack@google.com    typename TimeBuffer<FetchStruct>::wire fromFetch;
22112860Sgabeblack@google.com
22212860Sgabeblack@google.com    /** Queue of all instructions coming from fetch this cycle. */
22312861Sgabeblack@google.com    std::queue<DynInstPtr> insts[Impl::MaxThreads];
22412860Sgabeblack@google.com
22512860Sgabeblack@google.com    /** Skid buffer between fetch and decode. */
22612860Sgabeblack@google.com    std::queue<DynInstPtr> skidBuffer[Impl::MaxThreads];
22712860Sgabeblack@google.com
22812860Sgabeblack@google.com    /** Variable that tracks if decode has written to the time buffer this
22913212Sgabeblack@google.com     * cycle. Used to tell CPU if there is activity this cycle.
23013212Sgabeblack@google.com     */
23113212Sgabeblack@google.com    bool wroteToTimeBuffer;
23213212Sgabeblack@google.com
23313212Sgabeblack@google.com    /** Source of possible stalls. */
23413212Sgabeblack@google.com    struct Stalls {
23513212Sgabeblack@google.com        bool rename;
23613212Sgabeblack@google.com        bool iew;
23713212Sgabeblack@google.com        bool commit;
23813212Sgabeblack@google.com    };
23912990Sgabeblack@google.com
24012961Sgabeblack@google.com    /** Tracks which stages are telling decode to stall. */
24112961Sgabeblack@google.com    Stalls stalls[Impl::MaxThreads];
24213097Sgabeblack@google.com
24312961Sgabeblack@google.com    /** Rename to decode delay, in ticks. */
24412961Sgabeblack@google.com    unsigned renameToDecodeDelay;
24512961Sgabeblack@google.com
24612990Sgabeblack@google.com    /** IEW to decode delay, in ticks. */
24712961Sgabeblack@google.com    unsigned iewToDecodeDelay;
24812860Sgabeblack@google.com
24912860Sgabeblack@google.com    /** Commit to decode delay, in ticks. */
25012860Sgabeblack@google.com    unsigned commitToDecodeDelay;
25112860Sgabeblack@google.com
25212860Sgabeblack@google.com    /** Fetch to decode delay, in ticks. */
25313255Sgabeblack@google.com    unsigned fetchToDecodeDelay;
25413255Sgabeblack@google.com
25512956Sgabeblack@google.com    /** The width of decode, in instructions. */
25612860Sgabeblack@google.com    unsigned decodeWidth;
25712860Sgabeblack@google.com
25812860Sgabeblack@google.com    /** Index of instructions being sent to rename. */
25912860Sgabeblack@google.com    unsigned toRenameIndex;
26012860Sgabeblack@google.com
26112956Sgabeblack@google.com    /** number of Active Threads*/
26212860Sgabeblack@google.com    unsigned numThreads;
26312860Sgabeblack@google.com
26412860Sgabeblack@google.com    /** List of active thread ids */
26512860Sgabeblack@google.com    std::list<unsigned> *activeThreads;
26612860Sgabeblack@google.com
26712982Sgabeblack@google.com    /** Number of branches in flight. */
26812860Sgabeblack@google.com    unsigned branchCount[Impl::MaxThreads];
26912860Sgabeblack@google.com
27012860Sgabeblack@google.com    /** Maximum size of the skid buffer. */
27112860Sgabeblack@google.com    unsigned skidBufferMax;
27212860Sgabeblack@google.com
27312962Sgabeblack@google.com    /** Stat for total number of idle cycles. */
27412860Sgabeblack@google.com    Stats::Scalar<> decodeIdleCycles;
27512860Sgabeblack@google.com    /** Stat for total number of blocked cycles. */
27612860Sgabeblack@google.com    Stats::Scalar<> decodeBlockedCycles;
27712860Sgabeblack@google.com    /** Stat for total number of normal running cycles. */
27812860Sgabeblack@google.com    Stats::Scalar<> decodeRunCycles;
27912962Sgabeblack@google.com    /** Stat for total number of unblocking cycles. */
28012860Sgabeblack@google.com    Stats::Scalar<> decodeUnblockCycles;
28112860Sgabeblack@google.com    /** Stat for total number of squashing cycles. */
28212860Sgabeblack@google.com    Stats::Scalar<> decodeSquashCycles;
28312860Sgabeblack@google.com    /** Stat for number of times a branch is resolved at decode. */
28412860Sgabeblack@google.com    Stats::Scalar<> decodeBranchResolved;
28512861Sgabeblack@google.com    /** Stat for number of times a branch mispredict is detected. */
28612861Sgabeblack@google.com    Stats::Scalar<> decodeBranchMispred;
28712860Sgabeblack@google.com    /** Stat for number of times decode detected a non-control instruction
28812860Sgabeblack@google.com     * incorrectly predicted as a branch.
28912860Sgabeblack@google.com     */
29012860Sgabeblack@google.com    Stats::Scalar<> decodeControlMispred;
29112860Sgabeblack@google.com    /** Stat for total number of decoded instructions. */
29212962Sgabeblack@google.com    Stats::Scalar<> decodeDecodedInsts;
29312860Sgabeblack@google.com    /** Stat for total number of squashed instructions. */
29412860Sgabeblack@google.com    Stats::Scalar<> decodeSquashedInsts;
29512860Sgabeblack@google.com};
29612860Sgabeblack@google.com
29712860Sgabeblack@google.com#endif // __CPU_O3_DECODE_HH__
29812982Sgabeblack@google.com