decode.hh revision 2674:6d4afef73a20
12623SN/A/*
22623SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
32623SN/A * All rights reserved.
42623SN/A *
52623SN/A * Redistribution and use in source and binary forms, with or without
62623SN/A * modification, are permitted provided that the following conditions are
72623SN/A * met: redistributions of source code must retain the above copyright
82623SN/A * notice, this list of conditions and the following disclaimer;
92623SN/A * redistributions in binary form must reproduce the above copyright
102623SN/A * notice, this list of conditions and the following disclaimer in the
112623SN/A * documentation and/or other materials provided with the distribution;
122623SN/A * neither the name of the copyright holders nor the names of its
132623SN/A * contributors may be used to endorse or promote products derived from
142623SN/A * this software without specific prior written permission.
152623SN/A *
162623SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
292623SN/A */
302623SN/A
312623SN/A#ifndef __CPU_O3_DECODE_HH__
322623SN/A#define __CPU_O3_DECODE_HH__
332623SN/A
342623SN/A#include <queue>
352623SN/A
362623SN/A#include "base/statistics.hh"
372623SN/A#include "base/timebuf.hh"
382623SN/A
392623SN/A/**
402623SN/A * DefaultDecode class handles both single threaded and SMT
412623SN/A * decode. Its width is specified by the parameters; each cycles it
425487Snate@binkert.org * tries to decode that many instructions. Because instructions are
435487Snate@binkert.org * actually decoded when the StaticInst is created, this stage does
442623SN/A * not do much other than check any PC-relative branches.
452623SN/A */
462623SN/Atemplate<class Impl>
472623SN/Aclass DefaultDecode
482623SN/A{
492623SN/A  private:
502623SN/A    // Typedefs from the Impl.
512623SN/A    typedef typename Impl::FullCPU FullCPU;
522623SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
532623SN/A    typedef typename Impl::Params Params;
542623SN/A    typedef typename Impl::CPUPol CPUPol;
552623SN/A
562623SN/A    // Typedefs from the CPU policy.
572623SN/A    typedef typename CPUPol::FetchStruct FetchStruct;
582623SN/A    typedef typename CPUPol::DecodeStruct DecodeStruct;
592623SN/A    typedef typename CPUPol::TimeStruct TimeStruct;
602623SN/A
612623SN/A  public:
622623SN/A    /** Overall decode stage status. Used to determine if the CPU can
632623SN/A     * deschedule itself due to a lack of activity.
642623SN/A     */
652623SN/A    enum DecodeStatus {
662623SN/A        Active,
672623SN/A        Inactive
682623SN/A    };
692623SN/A
702623SN/A    /** Individual thread status. */
712623SN/A    enum ThreadStatus {
725336Shines@cs.fsu.edu        Running,
732623SN/A        Idle,
742623SN/A        StartSquash,
752623SN/A        Squashing,
762623SN/A        Blocked,
772623SN/A        Unblocking
785487Snate@binkert.org    };
795487Snate@binkert.org
802623SN/A  private:
812623SN/A    /** Decode status. */
822623SN/A    DecodeStatus _status;
832623SN/A
842623SN/A    /** Per-thread status. */
852623SN/A    ThreadStatus decodeStatus[Impl::MaxThreads];
862623SN/A
872623SN/A  public:
882640Sstever@eecs.umich.edu    /** DefaultDecode constructor. */
893401Sktlim@umich.edu    DefaultDecode(Params *params);
902623SN/A
912623SN/A    /** Returns the name of decode. */
923647Srdreslin@umich.edu    std::string name() const;
933647Srdreslin@umich.edu
942623SN/A    /** Registers statistics. */
952623SN/A    void regStats();
964192Sktlim@umich.edu
974192Sktlim@umich.edu    /** Sets CPU pointer. */
983349Sbinkertn@umich.edu    void setCPU(FullCPU *cpu_ptr);
992623SN/A
1003349Sbinkertn@umich.edu    /** Sets the main backwards communication time buffer pointer. */
1012623SN/A    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
1023349Sbinkertn@umich.edu
1032623SN/A    /** Sets pointer to time buffer used to communicate to the next stage. */
1042623SN/A    void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
1052623SN/A
1062657Ssaidi@eecs.umich.edu    /** Sets pointer to time buffer coming from fetch. */
1072623SN/A    void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
1082623SN/A
1094475Sstever@eecs.umich.edu    /** Sets pointer to list of active threads. */
1104475Sstever@eecs.umich.edu    void setActiveThreads(std::list<unsigned> *at_ptr);
1113192Srdreslin@umich.edu
1122623SN/A    /** Switches out the decode stage. */
1132623SN/A    void switchOut();
1144192Sktlim@umich.edu
1154192Sktlim@umich.edu    /** Takes over from another CPU's thread. */
1164192Sktlim@umich.edu    void takeOverFrom();
1174192Sktlim@umich.edu
1184192Sktlim@umich.edu    /** Ticks decode, processing all input signals and decoding as many
1194192Sktlim@umich.edu     * instructions as possible.
1204192Sktlim@umich.edu     */
1214192Sktlim@umich.edu    void tick();
1224192Sktlim@umich.edu
1234192Sktlim@umich.edu    /** Determines what to do based on decode's current status.
1244192Sktlim@umich.edu     * @param status_change decode() sets this variable if there was a status
1252623SN/A     * change (ie switching from from blocking to unblocking).
1264968Sacolyte@umich.edu     * @param tid Thread id to decode instructions from.
1274968Sacolyte@umich.edu     */
1284870Sstever@eecs.umich.edu    void decode(bool &status_change, unsigned tid);
1294870Sstever@eecs.umich.edu
1304870Sstever@eecs.umich.edu    /** Processes instructions from fetch and passes them on to rename.
1312623SN/A     * Decoding of instructions actually happens when they are created in
1322623SN/A     * fetch, so this function mostly checks if PC-relative branches are
1332662Sstever@eecs.umich.edu     * correct.
1342623SN/A     */
1354968Sacolyte@umich.edu    void decodeInsts(unsigned tid);
1364968Sacolyte@umich.edu
1372623SN/A  private:
1382623SN/A    /** Inserts a thread's instructions into the skid buffer, to be decoded
1392856Srdreslin@umich.edu     * once decode unblocks.
1402856Srdreslin@umich.edu     */
1412623SN/A    void skidInsert(unsigned tid);
1422623SN/A
1432915Sktlim@umich.edu    /** Returns if all of the skid buffers are empty. */
1442623SN/A    bool skidsEmpty();
1452798Sktlim@umich.edu
1462623SN/A    /** Updates overall decode status based on all of the threads' statuses. */
1472623SN/A    void updateStatus();
1482623SN/A
1492623SN/A    /** Separates instructions from fetch into individual lists of instructions
1502623SN/A     * sorted by thread.
1512623SN/A     */
1522623SN/A    void sortInsts();
1532623SN/A
1542623SN/A    /** Reads all stall signals from the backwards communication timebuffer. */
1552623SN/A    void readStallSignals(unsigned tid);
1565177Sgblack@eecs.umich.edu
1575177Sgblack@eecs.umich.edu    /** Checks all input signals and updates decode's status appropriately. */
1585177Sgblack@eecs.umich.edu    bool checkSignalsAndUpdate(unsigned tid);
1595177Sgblack@eecs.umich.edu
1605177Sgblack@eecs.umich.edu    /** Checks all stall signals, and returns if any are true. */
1615315Sstever@gmail.com    bool checkStall(unsigned tid) const;
1625315Sstever@gmail.com
1635315Sstever@gmail.com    /** Returns if there any instructions from fetch on this cycle. */
1645315Sstever@gmail.com    inline bool fetchInstsValid();
1655315Sstever@gmail.com
1665315Sstever@gmail.com    /** Switches decode to blocking, and signals back that decode has
1672623SN/A     * become blocked.
1682623SN/A     * @return Returns true if there is a status change.
1692623SN/A     */
170    bool block(unsigned tid);
171
172    /** Switches decode to unblocking if the skid buffer is empty, and
173     * signals back that decode has unblocked.
174     * @return Returns true if there is a status change.
175     */
176    bool unblock(unsigned tid);
177
178    /** Squashes if there is a PC-relative branch that was predicted
179     * incorrectly. Sends squash information back to fetch.
180     */
181    void squash(DynInstPtr &inst, unsigned tid);
182
183  public:
184    /** Squashes due to commit signalling a squash. Changes status to
185     * squashing and clears block/unblock signals as needed.
186     */
187    unsigned squash(unsigned tid);
188
189  private:
190    // Interfaces to objects outside of decode.
191    /** CPU interface. */
192    FullCPU *cpu;
193
194    /** Time buffer interface. */
195    TimeBuffer<TimeStruct> *timeBuffer;
196
197    /** Wire to get rename's output from backwards time buffer. */
198    typename TimeBuffer<TimeStruct>::wire fromRename;
199
200    /** Wire to get iew's information from backwards time buffer. */
201    typename TimeBuffer<TimeStruct>::wire fromIEW;
202
203    /** Wire to get commit's information from backwards time buffer. */
204    typename TimeBuffer<TimeStruct>::wire fromCommit;
205
206    /** Wire to write information heading to previous stages. */
207    // Might not be the best name as not only fetch will read it.
208    typename TimeBuffer<TimeStruct>::wire toFetch;
209
210    /** Decode instruction queue. */
211    TimeBuffer<DecodeStruct> *decodeQueue;
212
213    /** Wire used to write any information heading to rename. */
214    typename TimeBuffer<DecodeStruct>::wire toRename;
215
216    /** Fetch instruction queue interface. */
217    TimeBuffer<FetchStruct> *fetchQueue;
218
219    /** Wire to get fetch's output from fetch queue. */
220    typename TimeBuffer<FetchStruct>::wire fromFetch;
221
222    /** Queue of all instructions coming from fetch this cycle. */
223    std::queue<DynInstPtr> insts[Impl::MaxThreads];
224
225    /** Skid buffer between fetch and decode. */
226    std::queue<DynInstPtr> skidBuffer[Impl::MaxThreads];
227
228    /** Variable that tracks if decode has written to the time buffer this
229     * cycle. Used to tell CPU if there is activity this cycle.
230     */
231    bool wroteToTimeBuffer;
232
233    /** Source of possible stalls. */
234    struct Stalls {
235        bool rename;
236        bool iew;
237        bool commit;
238    };
239
240    /** Tracks which stages are telling decode to stall. */
241    Stalls stalls[Impl::MaxThreads];
242
243    /** Rename to decode delay, in ticks. */
244    unsigned renameToDecodeDelay;
245
246    /** IEW to decode delay, in ticks. */
247    unsigned iewToDecodeDelay;
248
249    /** Commit to decode delay, in ticks. */
250    unsigned commitToDecodeDelay;
251
252    /** Fetch to decode delay, in ticks. */
253    unsigned fetchToDecodeDelay;
254
255    /** The width of decode, in instructions. */
256    unsigned decodeWidth;
257
258    /** Index of instructions being sent to rename. */
259    unsigned toRenameIndex;
260
261    /** number of Active Threads*/
262    unsigned numThreads;
263
264    /** List of active thread ids */
265    std::list<unsigned> *activeThreads;
266
267    /** Number of branches in flight. */
268    unsigned branchCount[Impl::MaxThreads];
269
270    /** Maximum size of the skid buffer. */
271    unsigned skidBufferMax;
272
273    /** Stat for total number of idle cycles. */
274    Stats::Scalar<> decodeIdleCycles;
275    /** Stat for total number of blocked cycles. */
276    Stats::Scalar<> decodeBlockedCycles;
277    /** Stat for total number of normal running cycles. */
278    Stats::Scalar<> decodeRunCycles;
279    /** Stat for total number of unblocking cycles. */
280    Stats::Scalar<> decodeUnblockCycles;
281    /** Stat for total number of squashing cycles. */
282    Stats::Scalar<> decodeSquashCycles;
283    /** Stat for number of times a branch is resolved at decode. */
284    Stats::Scalar<> decodeBranchResolved;
285    /** Stat for number of times a branch mispredict is detected. */
286    Stats::Scalar<> decodeBranchMispred;
287    /** Stat for number of times decode detected a non-control instruction
288     * incorrectly predicted as a branch.
289     */
290    Stats::Scalar<> decodeControlMispred;
291    /** Stat for total number of decoded instructions. */
292    Stats::Scalar<> decodeDecodedInsts;
293    /** Stat for total number of squashed instructions. */
294    Stats::Scalar<> decodeSquashedInsts;
295};
296
297#endif // __CPU_O3_DECODE_HH__
298