mem_dep_unit.hh revision 8516
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 */
301061SN/A
312292SN/A#ifndef __CPU_O3_MEM_DEP_UNIT_HH__
322292SN/A#define __CPU_O3_MEM_DEP_UNIT_HH__
331061SN/A
342292SN/A#include <list>
351061SN/A#include <set>
361061SN/A
372292SN/A#include "base/hashmap.hh"
382292SN/A#include "base/refcnt.hh"
391684SN/A#include "base/statistics.hh"
401061SN/A#include "cpu/inst_seq.hh"
418232Snate@binkert.org#include "debug/MemDepUnit.hh"
421061SN/A
432292SN/Astruct SNHash {
442292SN/A    size_t operator() (const InstSeqNum &seq_num) const {
452292SN/A        unsigned a = (unsigned)seq_num;
462292SN/A        unsigned hash = (((a >> 14) ^ ((a >> 2) & 0xffff))) & 0x7FFFFFFF;
472292SN/A
482292SN/A        return hash;
492292SN/A    }
502292SN/A};
512292SN/A
525529Snate@binkert.orgclass DerivO3CPUParams;
535529Snate@binkert.org
542292SN/Atemplate <class Impl>
552292SN/Aclass InstructionQueue;
562292SN/A
571061SN/A/**
581061SN/A * Memory dependency unit class.  This holds the memory dependence predictor.
591061SN/A * As memory operations are issued to the IQ, they are also issued to this
601061SN/A * unit, which then looks up the prediction as to what they are dependent
611061SN/A * upon.  This unit must be checked prior to a memory operation being able
621061SN/A * to issue.  Although this is templated, it's somewhat hard to make a generic
631061SN/A * memory dependence unit.  This one is mostly for store sets; it will be
641061SN/A * quite limited in what other memory dependence predictions it can also
651061SN/A * utilize.  Thus this class should be most likely be rewritten for other
661061SN/A * dependence prediction schemes.
671061SN/A */
681061SN/Atemplate <class MemDepPred, class Impl>
696005Snate@binkert.orgclass MemDepUnit
706005Snate@binkert.org{
716005Snate@binkert.org  protected:
726005Snate@binkert.org    std::string _name;
736005Snate@binkert.org
741061SN/A  public:
751061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
761061SN/A
772292SN/A    /** Empty constructor. Must call init() prior to using in this case. */
783500Sktlim@umich.edu    MemDepUnit();
791061SN/A
802292SN/A    /** Constructs a MemDepUnit with given parameters. */
815529Snate@binkert.org    MemDepUnit(DerivO3CPUParams *params);
822292SN/A
832292SN/A    /** Frees up any memory allocated. */
842292SN/A    ~MemDepUnit();
852292SN/A
862292SN/A    /** Returns the name of the memory dependence unit. */
876005Snate@binkert.org    std::string name() const { return _name; }
882292SN/A
892292SN/A    /** Initializes the unit with parameters and a thread id. */
906221Snate@binkert.org    void init(DerivO3CPUParams *params, ThreadID tid);
912292SN/A
922292SN/A    /** Registers statistics. */
931062SN/A    void regStats();
941061SN/A
952348SN/A    /** Switches out the memory dependence predictor. */
962307SN/A    void switchOut();
972307SN/A
982348SN/A    /** Takes over from another CPU's thread. */
992307SN/A    void takeOverFrom();
1002307SN/A
1012292SN/A    /** Sets the pointer to the IQ. */
1022292SN/A    void setIQ(InstructionQueue<Impl> *iq_ptr);
1032292SN/A
1042292SN/A    /** Inserts a memory instruction. */
1051061SN/A    void insert(DynInstPtr &inst);
1061061SN/A
1072292SN/A    /** Inserts a non-speculative memory instruction. */
1081062SN/A    void insertNonSpec(DynInstPtr &inst);
1091062SN/A
1102292SN/A    /** Inserts a barrier instruction. */
1112292SN/A    void insertBarrier(DynInstPtr &barr_inst);
1121684SN/A
1132292SN/A    /** Indicate that an instruction has its registers ready. */
1141062SN/A    void regsReady(DynInstPtr &inst);
1151062SN/A
1162292SN/A    /** Indicate that a non-speculative instruction is ready. */
1171062SN/A    void nonSpecInstReady(DynInstPtr &inst);
1181061SN/A
1192292SN/A    /** Reschedules an instruction to be re-executed. */
1202292SN/A    void reschedule(DynInstPtr &inst);
1212292SN/A
1222292SN/A    /** Replays all instructions that have been rescheduled by moving them to
1232292SN/A     *  the ready list.
1242292SN/A     */
1252292SN/A    void replay(DynInstPtr &inst);
1262292SN/A
1272292SN/A    /** Completes a memory instruction. */
1282292SN/A    void completed(DynInstPtr &inst);
1292292SN/A
1302292SN/A    /** Completes a barrier instruction. */
1312292SN/A    void completeBarrier(DynInstPtr &inst);
1322292SN/A
1332292SN/A    /** Wakes any dependents of a memory instruction. */
1342292SN/A    void wakeDependents(DynInstPtr &inst);
1352292SN/A
1362292SN/A    /** Squashes all instructions up until a given sequence number for a
1372292SN/A     *  specific thread.
1382292SN/A     */
1396221Snate@binkert.org    void squash(const InstSeqNum &squashed_num, ThreadID tid);
1402292SN/A
1412292SN/A    /** Indicates an ordering violation between a store and a younger load. */
1422292SN/A    void violation(DynInstPtr &store_inst, DynInstPtr &violating_load);
1432292SN/A
1442292SN/A    /** Issues the given instruction */
1451061SN/A    void issue(DynInstPtr &inst);
1461061SN/A
1472292SN/A    /** Debugging function to dump the lists of instructions. */
1482292SN/A    void dumpLists();
1491062SN/A
1501062SN/A  private:
1512292SN/A    typedef typename std::list<DynInstPtr>::iterator ListIt;
1521062SN/A
1532292SN/A    class MemDepEntry;
1541062SN/A
1552292SN/A    typedef RefCountingPtr<MemDepEntry> MemDepEntryPtr;
1561062SN/A
1572292SN/A    /** Memory dependence entries that track memory operations, marking
1582292SN/A     *  when the instruction is ready to execute and what instructions depend
1592292SN/A     *  upon it.
1602292SN/A     */
1612292SN/A    class MemDepEntry : public RefCounted {
1622292SN/A      public:
1632292SN/A        /** Constructs a memory dependence entry. */
1642292SN/A        MemDepEntry(DynInstPtr &new_inst)
1652292SN/A            : inst(new_inst), regsReady(false), memDepReady(false),
1662292SN/A              completed(false), squashed(false)
1672292SN/A        {
1682348SN/A#ifdef DEBUG
1692292SN/A            ++memdep_count;
1701062SN/A
1712292SN/A            DPRINTF(MemDepUnit, "Memory dependency entry created.  "
1728516SMrinmoy.Ghosh@arm.com                    "memdep_count=%i %s\n", memdep_count, inst->pcState());
1732348SN/A#endif
1742292SN/A        }
1751062SN/A
1762292SN/A        /** Frees any pointers. */
1772292SN/A        ~MemDepEntry()
1782292SN/A        {
1792292SN/A            for (int i = 0; i < dependInsts.size(); ++i) {
1802292SN/A                dependInsts[i] = NULL;
1812292SN/A            }
1822348SN/A#ifdef DEBUG
1832292SN/A            --memdep_count;
1842292SN/A
1852292SN/A            DPRINTF(MemDepUnit, "Memory dependency entry deleted.  "
1868516SMrinmoy.Ghosh@arm.com                    "memdep_count=%i %s\n", memdep_count, inst->pcState());
1872348SN/A#endif
1882292SN/A        }
1892292SN/A
1902292SN/A        /** Returns the name of the memory dependence entry. */
1912292SN/A        std::string name() const { return "memdepentry"; }
1922292SN/A
1932292SN/A        /** The instruction being tracked. */
1942292SN/A        DynInstPtr inst;
1952292SN/A
1962292SN/A        /** The iterator to the instruction's location inside the list. */
1972292SN/A        ListIt listIt;
1982292SN/A
1992292SN/A        /** A vector of any dependent instructions. */
2002292SN/A        std::vector<MemDepEntryPtr> dependInsts;
2012292SN/A
2022292SN/A        /** If the registers are ready or not. */
2032292SN/A        bool regsReady;
2042292SN/A        /** If all memory dependencies have been satisfied. */
2052292SN/A        bool memDepReady;
2062292SN/A        /** If the instruction is completed. */
2072292SN/A        bool completed;
2082292SN/A        /** If the instruction is squashed. */
2092292SN/A        bool squashed;
2102292SN/A
2112292SN/A        /** For debugging. */
2122348SN/A#ifdef DEBUG
2132292SN/A        static int memdep_count;
2142292SN/A        static int memdep_insert;
2152292SN/A        static int memdep_erase;
2162348SN/A#endif
2171062SN/A    };
2181062SN/A
2192292SN/A    /** Finds the memory dependence entry in the hash map. */
2202292SN/A    inline MemDepEntryPtr &findInHash(const DynInstPtr &inst);
2211062SN/A
2222292SN/A    /** Moves an entry to the ready list. */
2232292SN/A    inline void moveToReady(MemDepEntryPtr &ready_inst_entry);
2241062SN/A
2252292SN/A    typedef m5::hash_map<InstSeqNum, MemDepEntryPtr, SNHash> MemDepHash;
2261061SN/A
2272292SN/A    typedef typename MemDepHash::iterator MemDepHashIt;
2281061SN/A
2292292SN/A    /** A hash map of all memory dependence entries. */
2302292SN/A    MemDepHash memDepHash;
2311062SN/A
2322292SN/A    /** A list of all instructions in the memory dependence unit. */
2332292SN/A    std::list<DynInstPtr> instList[Impl::MaxThreads];
2341062SN/A
2352292SN/A    /** A list of all instructions that are going to be replayed. */
2362292SN/A    std::list<DynInstPtr> instsToReplay;
2371061SN/A
2381061SN/A    /** The memory dependence predictor.  It is accessed upon new
2391061SN/A     *  instructions being added to the IQ, and responds by telling
2401061SN/A     *  this unit what instruction the newly added instruction is dependent
2411061SN/A     *  upon.
2421061SN/A     */
2431061SN/A    MemDepPred depPred;
2441061SN/A
2452348SN/A    /** Is there an outstanding load barrier that loads must wait on. */
2462292SN/A    bool loadBarrier;
2472348SN/A    /** The sequence number of the load barrier. */
2482292SN/A    InstSeqNum loadBarrierSN;
2492348SN/A    /** Is there an outstanding store barrier that loads must wait on. */
2502292SN/A    bool storeBarrier;
2512348SN/A    /** The sequence number of the store barrier. */
2522292SN/A    InstSeqNum storeBarrierSN;
2532292SN/A
2542292SN/A    /** Pointer to the IQ. */
2552292SN/A    InstructionQueue<Impl> *iqPtr;
2562292SN/A
2572292SN/A    /** The thread id of this memory dependence unit. */
2582292SN/A    int id;
2592292SN/A
2602292SN/A    /** Stat for number of inserted loads. */
2615999Snate@binkert.org    Stats::Scalar insertedLoads;
2622292SN/A    /** Stat for number of inserted stores. */
2635999Snate@binkert.org    Stats::Scalar insertedStores;
2642292SN/A    /** Stat for number of conflicting loads that had to wait for a store. */
2655999Snate@binkert.org    Stats::Scalar conflictingLoads;
2662292SN/A    /** Stat for number of conflicting stores that had to wait for a store. */
2675999Snate@binkert.org    Stats::Scalar conflictingStores;
2681061SN/A};
2691061SN/A
2702292SN/A#endif // __CPU_O3_MEM_DEP_UNIT_HH__
271