11689SN/A/*
210333Smitch.hayenga@arm.com * Copyright (c) 2012, 2014 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 */
421061SN/A
439944Smatt.horsnell@ARM.com#ifndef __CPU_O3_MEM_DEP_UNIT_IMPL_HH__
449944Smatt.horsnell@ARM.com#define __CPU_O3_MEM_DEP_UNIT_IMPL_HH__
459944Smatt.horsnell@ARM.com
461061SN/A#include <map>
471061SN/A
482292SN/A#include "cpu/o3/inst_queue.hh"
491717SN/A#include "cpu/o3/mem_dep_unit.hh"
508232Snate@binkert.org#include "debug/MemDepUnit.hh"
515529Snate@binkert.org#include "params/DerivO3CPU.hh"
525529Snate@binkert.org
531061SN/Atemplate <class MemDepPred, class Impl>
543500Sktlim@umich.eduMemDepUnit<MemDepPred, Impl>::MemDepUnit()
553500Sktlim@umich.edu    : loadBarrier(false), loadBarrierSN(0), storeBarrier(false),
563500Sktlim@umich.edu      storeBarrierSN(0), iqPtr(NULL)
573500Sktlim@umich.edu{
583500Sktlim@umich.edu}
593500Sktlim@umich.edu
603500Sktlim@umich.edutemplate <class MemDepPred, class Impl>
615529Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::MemDepUnit(DerivO3CPUParams *params)
626005Snate@binkert.org    : _name(params->name + ".memdepunit"),
638519SAli.Saidi@ARM.com      depPred(params->store_set_clear_period, params->SSITSize,
648519SAli.Saidi@ARM.com              params->LFSTSize),
658519SAli.Saidi@ARM.com      loadBarrier(false), loadBarrierSN(0), storeBarrier(false),
668519SAli.Saidi@ARM.com      storeBarrierSN(0), iqPtr(NULL)
671061SN/A{
682292SN/A    DPRINTF(MemDepUnit, "Creating MemDepUnit object.\n");
692292SN/A}
702292SN/A
712292SN/Atemplate <class MemDepPred, class Impl>
722292SN/AMemDepUnit<MemDepPred, Impl>::~MemDepUnit()
732292SN/A{
746221Snate@binkert.org    for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
752292SN/A
762292SN/A        ListIt inst_list_it = instList[tid].begin();
772292SN/A
782292SN/A        MemDepHashIt hash_it;
792292SN/A
802292SN/A        while (!instList[tid].empty()) {
812292SN/A            hash_it = memDepHash.find((*inst_list_it)->seqNum);
822292SN/A
832292SN/A            assert(hash_it != memDepHash.end());
842292SN/A
852292SN/A            memDepHash.erase(hash_it);
862292SN/A
872292SN/A            instList[tid].erase(inst_list_it++);
882292SN/A        }
892292SN/A    }
902292SN/A
912678Sktlim@umich.edu#ifdef DEBUG
922292SN/A    assert(MemDepEntry::memdep_count == 0);
932678Sktlim@umich.edu#endif
942292SN/A}
952292SN/A
962292SN/Atemplate <class MemDepPred, class Impl>
972292SN/Avoid
986221Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::init(DerivO3CPUParams *params, ThreadID tid)
992292SN/A{
1002292SN/A    DPRINTF(MemDepUnit, "Creating MemDepUnit %i object.\n",tid);
1012292SN/A
1026005Snate@binkert.org    _name = csprintf("%s.memDep%d", params->name, tid);
1032292SN/A    id = tid;
1042292SN/A
1058519SAli.Saidi@ARM.com    depPred.init(params->store_set_clear_period, params->SSITSize,
1068519SAli.Saidi@ARM.com            params->LFSTSize);
1071061SN/A}
1081061SN/A
1091061SN/Atemplate <class MemDepPred, class Impl>
1101061SN/Avoid
1111062SN/AMemDepUnit<MemDepPred, Impl>::regStats()
1121062SN/A{
1131062SN/A    insertedLoads
1146005Snate@binkert.org        .name(name() + ".insertedLoads")
1151062SN/A        .desc("Number of loads inserted to the mem dependence unit.");
1161062SN/A
1171062SN/A    insertedStores
1186005Snate@binkert.org        .name(name() + ".insertedStores")
1191062SN/A        .desc("Number of stores inserted to the mem dependence unit.");
1201062SN/A
1211062SN/A    conflictingLoads
1226005Snate@binkert.org        .name(name() + ".conflictingLoads")
1231062SN/A        .desc("Number of conflicting loads.");
1241062SN/A
1251062SN/A    conflictingStores
1266005Snate@binkert.org        .name(name() + ".conflictingStores")
1271062SN/A        .desc("Number of conflicting stores.");
1281062SN/A}
1291062SN/A
1301062SN/Atemplate <class MemDepPred, class Impl>
13110510Smitch.hayenga@arm.combool
13210510Smitch.hayenga@arm.comMemDepUnit<MemDepPred, Impl>::isDrained() const
13310510Smitch.hayenga@arm.com{
13410510Smitch.hayenga@arm.com    bool drained = instsToReplay.empty()
13510510Smitch.hayenga@arm.com                 && memDepHash.empty()
13610510Smitch.hayenga@arm.com                 && instsToReplay.empty();
13710510Smitch.hayenga@arm.com    for (int i = 0; i < Impl::MaxThreads; ++i)
13810510Smitch.hayenga@arm.com        drained = drained && instList[i].empty();
13910510Smitch.hayenga@arm.com
14010510Smitch.hayenga@arm.com    return drained;
14110510Smitch.hayenga@arm.com}
14210510Smitch.hayenga@arm.com
14310510Smitch.hayenga@arm.comtemplate <class MemDepPred, class Impl>
1441062SN/Avoid
1459444SAndreas.Sandberg@ARM.comMemDepUnit<MemDepPred, Impl>::drainSanityCheck() const
1462307SN/A{
1472367SN/A    assert(instsToReplay.empty());
1482367SN/A    assert(memDepHash.empty());
1499444SAndreas.Sandberg@ARM.com    for (int i = 0; i < Impl::MaxThreads; ++i)
1509444SAndreas.Sandberg@ARM.com        assert(instList[i].empty());
1519444SAndreas.Sandberg@ARM.com    assert(instsToReplay.empty());
1529444SAndreas.Sandberg@ARM.com    assert(memDepHash.empty());
1532307SN/A}
1542307SN/A
1552307SN/Atemplate <class MemDepPred, class Impl>
1562307SN/Avoid
1572307SN/AMemDepUnit<MemDepPred, Impl>::takeOverFrom()
1582307SN/A{
1592348SN/A    // Be sure to reset all state.
1602307SN/A    loadBarrier = storeBarrier = false;
1612307SN/A    loadBarrierSN = storeBarrierSN = 0;
1622307SN/A    depPred.clear();
1632307SN/A}
1642307SN/A
1652307SN/Atemplate <class MemDepPred, class Impl>
1662307SN/Avoid
1672292SN/AMemDepUnit<MemDepPred, Impl>::setIQ(InstructionQueue<Impl> *iq_ptr)
1682292SN/A{
1692292SN/A    iqPtr = iq_ptr;
1702292SN/A}
1712292SN/A
1722292SN/Atemplate <class MemDepPred, class Impl>
1732292SN/Avoid
17413429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::insert(const DynInstPtr &inst)
1751061SN/A{
1766221Snate@binkert.org    ThreadID tid = inst->threadNumber;
1771061SN/A
17810473Sandreas.hansson@arm.com    MemDepEntryPtr inst_entry = std::make_shared<MemDepEntry>(inst);
1791061SN/A
1802292SN/A    // Add the MemDepEntry to the hash.
1812292SN/A    memDepHash.insert(
1822292SN/A        std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
1832678Sktlim@umich.edu#ifdef DEBUG
1842292SN/A    MemDepEntry::memdep_insert++;
1852678Sktlim@umich.edu#endif
1861061SN/A
1872292SN/A    instList[tid].push_back(inst);
1881062SN/A
1892292SN/A    inst_entry->listIt = --(instList[tid].end());
1901062SN/A
1912329SN/A    // Check any barriers and the dependence predictor for any
1922348SN/A    // producing memrefs/stores.
1932292SN/A    InstSeqNum producing_store;
19413652Sqtt2@cornell.edu    if ((inst->isLoad() || inst->isAtomic()) && loadBarrier) {
1953500Sktlim@umich.edu        DPRINTF(MemDepUnit, "Load barrier [sn:%lli] in flight\n",
1963500Sktlim@umich.edu                loadBarrierSN);
1972292SN/A        producing_store = loadBarrierSN;
19813652Sqtt2@cornell.edu    } else if ((inst->isStore() || inst->isAtomic()) && storeBarrier) {
1993500Sktlim@umich.edu        DPRINTF(MemDepUnit, "Store barrier [sn:%lli] in flight\n",
2003500Sktlim@umich.edu                storeBarrierSN);
2012292SN/A        producing_store = storeBarrierSN;
2022292SN/A    } else {
2037720Sgblack@eecs.umich.edu        producing_store = depPred.checkInst(inst->instAddr());
2042292SN/A    }
2052292SN/A
2062292SN/A    MemDepEntryPtr store_entry = NULL;
2072292SN/A
2082292SN/A    // If there is a producing store, try to find the entry.
2092292SN/A    if (producing_store != 0) {
2103500Sktlim@umich.edu        DPRINTF(MemDepUnit, "Searching for producer\n");
2112292SN/A        MemDepHashIt hash_it = memDepHash.find(producing_store);
2122292SN/A
2132292SN/A        if (hash_it != memDepHash.end()) {
2142292SN/A            store_entry = (*hash_it).second;
2153500Sktlim@umich.edu            DPRINTF(MemDepUnit, "Proucer found\n");
2162292SN/A        }
2172292SN/A    }
2182292SN/A
2192292SN/A    // If no store entry, then instruction can issue as soon as the registers
2202292SN/A    // are ready.
2212292SN/A    if (!store_entry) {
2222292SN/A        DPRINTF(MemDepUnit, "No dependency for inst PC "
2237720Sgblack@eecs.umich.edu                "%s [sn:%lli].\n", inst->pcState(), inst->seqNum);
2242292SN/A
2252292SN/A        inst_entry->memDepReady = true;
2261062SN/A
2271062SN/A        if (inst->readyToIssue()) {
2282292SN/A            inst_entry->regsReady = true;
2291062SN/A
2302292SN/A            moveToReady(inst_entry);
2311062SN/A        }
2321061SN/A    } else {
2332329SN/A        // Otherwise make the instruction dependent on the store/barrier.
2342292SN/A        DPRINTF(MemDepUnit, "Adding to dependency list; "
2357720Sgblack@eecs.umich.edu                "inst PC %s is dependent on [sn:%lli].\n",
2367720Sgblack@eecs.umich.edu                inst->pcState(), producing_store);
2371062SN/A
2381062SN/A        if (inst->readyToIssue()) {
2392292SN/A            inst_entry->regsReady = true;
2401062SN/A        }
2411062SN/A
2424033Sktlim@umich.edu        // Clear the bit saying this instruction can issue.
2434033Sktlim@umich.edu        inst->clearCanIssue();
2444033Sktlim@umich.edu
2451062SN/A        // Add this instruction to the list of dependents.
2462292SN/A        store_entry->dependInsts.push_back(inst_entry);
2471062SN/A
2481062SN/A        if (inst->isLoad()) {
2491062SN/A            ++conflictingLoads;
2501062SN/A        } else {
2511062SN/A            ++conflictingStores;
2521062SN/A        }
2531061SN/A    }
2541061SN/A
25513652Sqtt2@cornell.edu    if (inst->isStore() || inst->isAtomic()) {
25613652Sqtt2@cornell.edu        DPRINTF(MemDepUnit, "Inserting store/atomic PC %s [sn:%lli].\n",
2577720Sgblack@eecs.umich.edu                inst->pcState(), inst->seqNum);
2581062SN/A
2597720Sgblack@eecs.umich.edu        depPred.insertStore(inst->instAddr(), inst->seqNum, inst->threadNumber);
2601062SN/A
2611062SN/A        ++insertedStores;
2621062SN/A    } else if (inst->isLoad()) {
2631062SN/A        ++insertedLoads;
2641062SN/A    } else {
2652292SN/A        panic("Unknown type! (most likely a barrier).");
2661061SN/A    }
2671062SN/A}
2681062SN/A
2691062SN/Atemplate <class MemDepPred, class Impl>
2701062SN/Avoid
27113429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::insertNonSpec(const DynInstPtr &inst)
2721062SN/A{
2736221Snate@binkert.org    ThreadID tid = inst->threadNumber;
2741062SN/A
27510473Sandreas.hansson@arm.com    MemDepEntryPtr inst_entry = std::make_shared<MemDepEntry>(inst);
2761062SN/A
2772292SN/A    // Insert the MemDepEntry into the hash.
2782292SN/A    memDepHash.insert(
2792292SN/A        std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
2802678Sktlim@umich.edu#ifdef DEBUG
2812292SN/A    MemDepEntry::memdep_insert++;
2822678Sktlim@umich.edu#endif
2831062SN/A
2842292SN/A    // Add the instruction to the list.
2852292SN/A    instList[tid].push_back(inst);
2862292SN/A
2872292SN/A    inst_entry->listIt = --(instList[tid].end());
2881062SN/A
2891062SN/A    // Might want to turn this part into an inline function or something.
2901062SN/A    // It's shared between both insert functions.
29113652Sqtt2@cornell.edu    if (inst->isStore() || inst->isAtomic()) {
29213652Sqtt2@cornell.edu        DPRINTF(MemDepUnit, "Inserting store/atomic PC %s [sn:%lli].\n",
2937720Sgblack@eecs.umich.edu                inst->pcState(), inst->seqNum);
2941062SN/A
2957720Sgblack@eecs.umich.edu        depPred.insertStore(inst->instAddr(), inst->seqNum, inst->threadNumber);
2961062SN/A
2971062SN/A        ++insertedStores;
2981062SN/A    } else if (inst->isLoad()) {
2991062SN/A        ++insertedLoads;
3001062SN/A    } else {
3012292SN/A        panic("Unknown type! (most likely a barrier).");
3021062SN/A    }
3031062SN/A}
3041062SN/A
3051062SN/Atemplate <class MemDepPred, class Impl>
3061062SN/Avoid
30713429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::insertBarrier(const DynInstPtr &barr_inst)
3081062SN/A{
3092292SN/A    InstSeqNum barr_sn = barr_inst->seqNum;
3102348SN/A    // Memory barriers block loads and stores, write barriers only stores.
3112292SN/A    if (barr_inst->isMemBarrier()) {
3122292SN/A        loadBarrier = true;
3132292SN/A        loadBarrierSN = barr_sn;
3142292SN/A        storeBarrier = true;
3152292SN/A        storeBarrierSN = barr_sn;
3168516SMrinmoy.Ghosh@arm.com        DPRINTF(MemDepUnit, "Inserted a memory barrier %s SN:%lli\n",
3178516SMrinmoy.Ghosh@arm.com                barr_inst->pcState(),barr_sn);
3182292SN/A    } else if (barr_inst->isWriteBarrier()) {
3192292SN/A        storeBarrier = true;
3202292SN/A        storeBarrierSN = barr_sn;
3212292SN/A        DPRINTF(MemDepUnit, "Inserted a write barrier\n");
3222292SN/A    }
3231062SN/A
3246221Snate@binkert.org    ThreadID tid = barr_inst->threadNumber;
3251062SN/A
32610473Sandreas.hansson@arm.com    MemDepEntryPtr inst_entry = std::make_shared<MemDepEntry>(barr_inst);
3271062SN/A
3282292SN/A    // Add the MemDepEntry to the hash.
3292292SN/A    memDepHash.insert(
3302292SN/A        std::pair<InstSeqNum, MemDepEntryPtr>(barr_sn, inst_entry));
3312678Sktlim@umich.edu#ifdef DEBUG
3322292SN/A    MemDepEntry::memdep_insert++;
3332678Sktlim@umich.edu#endif
3341062SN/A
3352292SN/A    // Add the instruction to the instruction list.
3362292SN/A    instList[tid].push_back(barr_inst);
3372292SN/A
3382292SN/A    inst_entry->listIt = --(instList[tid].end());
3391062SN/A}
3401062SN/A
3411062SN/Atemplate <class MemDepPred, class Impl>
3421062SN/Avoid
34313429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::regsReady(const DynInstPtr &inst)
3441062SN/A{
3452292SN/A    DPRINTF(MemDepUnit, "Marking registers as ready for "
3467720Sgblack@eecs.umich.edu            "instruction PC %s [sn:%lli].\n",
3477720Sgblack@eecs.umich.edu            inst->pcState(), inst->seqNum);
3481062SN/A
3492292SN/A    MemDepEntryPtr inst_entry = findInHash(inst);
3501062SN/A
3512292SN/A    inst_entry->regsReady = true;
3521062SN/A
3532292SN/A    if (inst_entry->memDepReady) {
3542292SN/A        DPRINTF(MemDepUnit, "Instruction has its memory "
3551062SN/A                "dependencies resolved, adding it to the ready list.\n");
3561062SN/A
3572292SN/A        moveToReady(inst_entry);
3581062SN/A    } else {
3592292SN/A        DPRINTF(MemDepUnit, "Instruction still waiting on "
3601062SN/A                "memory dependency.\n");
3611062SN/A    }
3621061SN/A}
3631061SN/A
3641061SN/Atemplate <class MemDepPred, class Impl>
3651062SN/Avoid
36613429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::nonSpecInstReady(const DynInstPtr &inst)
3671061SN/A{
3682292SN/A    DPRINTF(MemDepUnit, "Marking non speculative "
3697720Sgblack@eecs.umich.edu            "instruction PC %s as ready [sn:%lli].\n",
3707720Sgblack@eecs.umich.edu            inst->pcState(), inst->seqNum);
3711062SN/A
3722292SN/A    MemDepEntryPtr inst_entry = findInHash(inst);
3731061SN/A
3742292SN/A    moveToReady(inst_entry);
3751061SN/A}
3761061SN/A
3771061SN/Atemplate <class MemDepPred, class Impl>
3781061SN/Avoid
37913429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::reschedule(const DynInstPtr &inst)
3801061SN/A{
3812292SN/A    instsToReplay.push_back(inst);
3822292SN/A}
3831061SN/A
3842292SN/Atemplate <class MemDepPred, class Impl>
3852292SN/Avoid
38610333Smitch.hayenga@arm.comMemDepUnit<MemDepPred, Impl>::replay()
3872292SN/A{
3882292SN/A    DynInstPtr temp_inst;
3891062SN/A
3902348SN/A    // For now this replay function replays all waiting memory ops.
3912292SN/A    while (!instsToReplay.empty()) {
3922292SN/A        temp_inst = instsToReplay.front();
3931062SN/A
3942292SN/A        MemDepEntryPtr inst_entry = findInHash(temp_inst);
3952292SN/A
3967720Sgblack@eecs.umich.edu        DPRINTF(MemDepUnit, "Replaying mem instruction PC %s [sn:%lli].\n",
3977720Sgblack@eecs.umich.edu                temp_inst->pcState(), temp_inst->seqNum);
3982292SN/A
3992292SN/A        moveToReady(inst_entry);
4002292SN/A
4012292SN/A        instsToReplay.pop_front();
4022292SN/A    }
4032292SN/A}
4042292SN/A
4052292SN/Atemplate <class MemDepPred, class Impl>
4062292SN/Avoid
40713429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::completed(const DynInstPtr &inst)
4082292SN/A{
4097720Sgblack@eecs.umich.edu    DPRINTF(MemDepUnit, "Completed mem instruction PC %s [sn:%lli].\n",
4107720Sgblack@eecs.umich.edu            inst->pcState(), inst->seqNum);
4112292SN/A
4126221Snate@binkert.org    ThreadID tid = inst->threadNumber;
4132292SN/A
4142292SN/A    // Remove the instruction from the hash and the list.
4152292SN/A    MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
4162292SN/A
4172292SN/A    assert(hash_it != memDepHash.end());
4182292SN/A
4192292SN/A    instList[tid].erase((*hash_it).second->listIt);
4202292SN/A
4212292SN/A    (*hash_it).second = NULL;
4222292SN/A
4232292SN/A    memDepHash.erase(hash_it);
4242678Sktlim@umich.edu#ifdef DEBUG
4252292SN/A    MemDepEntry::memdep_erase++;
4262678Sktlim@umich.edu#endif
4272292SN/A}
4282292SN/A
4292292SN/Atemplate <class MemDepPred, class Impl>
4302292SN/Avoid
43113429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::completeBarrier(const DynInstPtr &inst)
4322292SN/A{
4332292SN/A    wakeDependents(inst);
4342292SN/A    completed(inst);
4352292SN/A
4362292SN/A    InstSeqNum barr_sn = inst->seqNum;
4378515SMrinmoy.Ghosh@arm.com    DPRINTF(MemDepUnit, "barrier completed: %s SN:%lli\n", inst->pcState(),
4388515SMrinmoy.Ghosh@arm.com            inst->seqNum);
4392292SN/A    if (inst->isMemBarrier()) {
4402292SN/A        if (loadBarrierSN == barr_sn)
4412292SN/A            loadBarrier = false;
4422292SN/A        if (storeBarrierSN == barr_sn)
4432292SN/A            storeBarrier = false;
4442292SN/A    } else if (inst->isWriteBarrier()) {
4452292SN/A        if (storeBarrierSN == barr_sn)
4462292SN/A            storeBarrier = false;
4472292SN/A    }
4481061SN/A}
4491061SN/A
4501061SN/Atemplate <class MemDepPred, class Impl>
4511061SN/Avoid
45213429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::wakeDependents(const DynInstPtr &inst)
4531061SN/A{
45413652Sqtt2@cornell.edu    // Only stores, atomics and barriers have dependents.
45513652Sqtt2@cornell.edu    if (!inst->isStore() && !inst->isAtomic() && !inst->isMemBarrier() &&
45613652Sqtt2@cornell.edu        !inst->isWriteBarrier()) {
4571062SN/A        return;
4581062SN/A    }
4591062SN/A
4602292SN/A    MemDepEntryPtr inst_entry = findInHash(inst);
4611061SN/A
4622292SN/A    for (int i = 0; i < inst_entry->dependInsts.size(); ++i ) {
4632292SN/A        MemDepEntryPtr woken_inst = inst_entry->dependInsts[i];
4641062SN/A
4652292SN/A        if (!woken_inst->inst) {
4662292SN/A            // Potentially removed mem dep entries could be on this list
4672292SN/A            continue;
4682292SN/A        }
4691061SN/A
4702292SN/A        DPRINTF(MemDepUnit, "Waking up a dependent inst, "
4712292SN/A                "[sn:%lli].\n",
4722292SN/A                woken_inst->inst->seqNum);
4731061SN/A
4742292SN/A        if (woken_inst->regsReady && !woken_inst->squashed) {
4751062SN/A            moveToReady(woken_inst);
4761062SN/A        } else {
4772292SN/A            woken_inst->memDepReady = true;
4781062SN/A        }
4791061SN/A    }
4801061SN/A
4812292SN/A    inst_entry->dependInsts.clear();
4821061SN/A}
4831061SN/A
4841061SN/Atemplate <class MemDepPred, class Impl>
4851061SN/Avoid
4862292SN/AMemDepUnit<MemDepPred, Impl>::squash(const InstSeqNum &squashed_num,
4876221Snate@binkert.org                                     ThreadID tid)
4881061SN/A{
4892292SN/A    if (!instsToReplay.empty()) {
4902292SN/A        ListIt replay_it = instsToReplay.begin();
4912292SN/A        while (replay_it != instsToReplay.end()) {
4922292SN/A            if ((*replay_it)->threadNumber == tid &&
4932292SN/A                (*replay_it)->seqNum > squashed_num) {
4942292SN/A                instsToReplay.erase(replay_it++);
4952292SN/A            } else {
4962292SN/A                ++replay_it;
4971062SN/A            }
4981061SN/A        }
4991061SN/A    }
5001061SN/A
5012292SN/A    ListIt squash_it = instList[tid].end();
5022292SN/A    --squash_it;
5031061SN/A
5042292SN/A    MemDepHashIt hash_it;
5051061SN/A
5062292SN/A    while (!instList[tid].empty() &&
5072292SN/A           (*squash_it)->seqNum > squashed_num) {
5081061SN/A
5092292SN/A        DPRINTF(MemDepUnit, "Squashing inst [sn:%lli]\n",
5102292SN/A                (*squash_it)->seqNum);
5111061SN/A
5128515SMrinmoy.Ghosh@arm.com        if ((*squash_it)->seqNum == loadBarrierSN)
5138515SMrinmoy.Ghosh@arm.com              loadBarrier = false;
5148515SMrinmoy.Ghosh@arm.com
5158515SMrinmoy.Ghosh@arm.com        if ((*squash_it)->seqNum == storeBarrierSN)
5168515SMrinmoy.Ghosh@arm.com              storeBarrier = false;
5178515SMrinmoy.Ghosh@arm.com
5182292SN/A        hash_it = memDepHash.find((*squash_it)->seqNum);
5191061SN/A
5202292SN/A        assert(hash_it != memDepHash.end());
5211062SN/A
5222292SN/A        (*hash_it).second->squashed = true;
5231717SN/A
5242292SN/A        (*hash_it).second = NULL;
5251717SN/A
5262292SN/A        memDepHash.erase(hash_it);
5272678Sktlim@umich.edu#ifdef DEBUG
5282292SN/A        MemDepEntry::memdep_erase++;
5292678Sktlim@umich.edu#endif
5301717SN/A
5312292SN/A        instList[tid].erase(squash_it--);
5321061SN/A    }
5331061SN/A
5341061SN/A    // Tell the dependency predictor to squash as well.
5352292SN/A    depPred.squash(squashed_num, tid);
5361061SN/A}
5371061SN/A
5381061SN/Atemplate <class MemDepPred, class Impl>
5391061SN/Avoid
54013429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::violation(const DynInstPtr &store_inst,
54113429Srekai.gonzalezalberquilla@arm.com                                        const DynInstPtr &violating_load)
5421061SN/A{
5432292SN/A    DPRINTF(MemDepUnit, "Passing violating PCs to store sets,"
5447720Sgblack@eecs.umich.edu            " load: %#x, store: %#x\n", violating_load->instAddr(),
5457720Sgblack@eecs.umich.edu            store_inst->instAddr());
5461061SN/A    // Tell the memory dependence unit of the violation.
5478515SMrinmoy.Ghosh@arm.com    depPred.violation(store_inst->instAddr(), violating_load->instAddr());
5481061SN/A}
5491062SN/A
5501062SN/Atemplate <class MemDepPred, class Impl>
5512292SN/Avoid
55213429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::issue(const DynInstPtr &inst)
5532292SN/A{
5542292SN/A    DPRINTF(MemDepUnit, "Issuing instruction PC %#x [sn:%lli].\n",
5557720Sgblack@eecs.umich.edu            inst->instAddr(), inst->seqNum);
5562292SN/A
5577720Sgblack@eecs.umich.edu    depPred.issued(inst->instAddr(), inst->seqNum, inst->isStore());
5582292SN/A}
5592292SN/A
5602292SN/Atemplate <class MemDepPred, class Impl>
5612292SN/Ainline typename MemDepUnit<MemDepPred,Impl>::MemDepEntryPtr &
56213429Srekai.gonzalezalberquilla@arm.comMemDepUnit<MemDepPred, Impl>::findInHash(const DynInstConstPtr &inst)
5632292SN/A{
5642292SN/A    MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
5652292SN/A
5662292SN/A    assert(hash_it != memDepHash.end());
5672292SN/A
5682292SN/A    return (*hash_it).second;
5692292SN/A}
5702292SN/A
5712292SN/Atemplate <class MemDepPred, class Impl>
5721062SN/Ainline void
5732292SN/AMemDepUnit<MemDepPred, Impl>::moveToReady(MemDepEntryPtr &woken_inst_entry)
5741062SN/A{
5752292SN/A    DPRINTF(MemDepUnit, "Adding instruction [sn:%lli] "
5762292SN/A            "to the ready list.\n", woken_inst_entry->inst->seqNum);
5771062SN/A
5782292SN/A    assert(!woken_inst_entry->squashed);
5791062SN/A
5802292SN/A    iqPtr->addReadyMemInst(woken_inst_entry->inst);
5811062SN/A}
5822292SN/A
5832292SN/A
5842292SN/Atemplate <class MemDepPred, class Impl>
5852292SN/Avoid
5862292SN/AMemDepUnit<MemDepPred, Impl>::dumpLists()
5872292SN/A{
5886221Snate@binkert.org    for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
5892292SN/A        cprintf("Instruction list %i size: %i\n",
5902292SN/A                tid, instList[tid].size());
5912292SN/A
5922292SN/A        ListIt inst_list_it = instList[tid].begin();
5932292SN/A        int num = 0;
5942292SN/A
5952292SN/A        while (inst_list_it != instList[tid].end()) {
59613831SAndrea.Mondelli@ucf.edu            cprintf("Instruction:%i\nPC: %s\n[sn:%llu]\n[tid:%i]\nIssued:%i\n"
5972292SN/A                    "Squashed:%i\n\n",
5987720Sgblack@eecs.umich.edu                    num, (*inst_list_it)->pcState(),
5992292SN/A                    (*inst_list_it)->seqNum,
6002292SN/A                    (*inst_list_it)->threadNumber,
6012292SN/A                    (*inst_list_it)->isIssued(),
6022292SN/A                    (*inst_list_it)->isSquashed());
6032292SN/A            inst_list_it++;
6042292SN/A            ++num;
6052292SN/A        }
6062292SN/A    }
6072292SN/A
6082292SN/A    cprintf("Memory dependence hash size: %i\n", memDepHash.size());
6092292SN/A
6102678Sktlim@umich.edu#ifdef DEBUG
6112292SN/A    cprintf("Memory dependence entries: %i\n", MemDepEntry::memdep_count);
6122678Sktlim@umich.edu#endif
6132292SN/A}
6149944Smatt.horsnell@ARM.com
6159944Smatt.horsnell@ARM.com#endif//__CPU_O3_MEM_DEP_UNIT_IMPL_HH__
616