2 * Copyright (c) 2012, 2014 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __CPU_O3_MEM_DEP_UNIT_IMPL_HH__
42 #define __CPU_O3_MEM_DEP_UNIT_IMPL_HH__
46 #include "cpu/o3/inst_queue.hh"
47 #include "cpu/o3/mem_dep_unit.hh"
48 #include "debug/MemDepUnit.hh"
49 #include "params/DerivO3CPU.hh"
51 template <class MemDepPred, class Impl>
52 MemDepUnit<MemDepPred, Impl>::MemDepUnit()
53 : loadBarrier(false), loadBarrierSN(0), storeBarrier(false),
54 storeBarrierSN(0), iqPtr(NULL)
58 template <class MemDepPred, class Impl>
59 MemDepUnit<MemDepPred, Impl>::MemDepUnit(DerivO3CPUParams *params)
60 : _name(params->name + ".memdepunit"),
61 depPred(params->store_set_clear_period, params->SSITSize,
63 loadBarrier(false), loadBarrierSN(0), storeBarrier(false),
64 storeBarrierSN(0), iqPtr(NULL)
66 DPRINTF(MemDepUnit, "Creating MemDepUnit object.\n");
69 template <class MemDepPred, class Impl>
70 MemDepUnit<MemDepPred, Impl>::~MemDepUnit()
72 for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
74 ListIt inst_list_it = instList[tid].begin();
78 while (!instList[tid].empty()) {
79 hash_it = memDepHash.find((*inst_list_it)->seqNum);
81 assert(hash_it != memDepHash.end());
83 memDepHash.erase(hash_it);
85 instList[tid].erase(inst_list_it++);
90 assert(MemDepEntry::memdep_count == 0);
94 template <class MemDepPred, class Impl>
96 MemDepUnit<MemDepPred, Impl>::init(DerivO3CPUParams *params, ThreadID tid)
98 DPRINTF(MemDepUnit, "Creating MemDepUnit %i object.\n",tid);
100 _name = csprintf("%s.memDep%d", params->name, tid);
103 depPred.init(params->store_set_clear_period, params->SSITSize,
107 template <class MemDepPred, class Impl>
109 MemDepUnit<MemDepPred, Impl>::regStats()
112 .name(name() + ".insertedLoads")
113 .desc("Number of loads inserted to the mem dependence unit.");
116 .name(name() + ".insertedStores")
117 .desc("Number of stores inserted to the mem dependence unit.");
120 .name(name() + ".conflictingLoads")
121 .desc("Number of conflicting loads.");
124 .name(name() + ".conflictingStores")
125 .desc("Number of conflicting stores.");
128 template <class MemDepPred, class Impl>
130 MemDepUnit<MemDepPred, Impl>::isDrained() const
132 bool drained = instsToReplay.empty()
133 && memDepHash.empty()
134 && instsToReplay.empty();
135 for (int i = 0; i < Impl::MaxThreads; ++i)
136 drained = drained && instList[i].empty();
141 template <class MemDepPred, class Impl>
143 MemDepUnit<MemDepPred, Impl>::drainSanityCheck() const
145 assert(instsToReplay.empty());
146 assert(memDepHash.empty());
147 for (int i = 0; i < Impl::MaxThreads; ++i)
148 assert(instList[i].empty());
149 assert(instsToReplay.empty());
150 assert(memDepHash.empty());
153 template <class MemDepPred, class Impl>
155 MemDepUnit<MemDepPred, Impl>::takeOverFrom()
157 // Be sure to reset all state.
158 loadBarrier = storeBarrier = false;
159 loadBarrierSN = storeBarrierSN = 0;
163 template <class MemDepPred, class Impl>
165 MemDepUnit<MemDepPred, Impl>::setIQ(InstructionQueue<Impl> *iq_ptr)
170 template <class MemDepPred, class Impl>
172 MemDepUnit<MemDepPred, Impl>::insert(const DynInstPtr &inst)
174 ThreadID tid = inst->threadNumber;
176 MemDepEntryPtr inst_entry = std::make_shared<MemDepEntry>(inst);
178 // Add the MemDepEntry to the hash.
180 std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
182 MemDepEntry::memdep_insert++;
185 instList[tid].push_back(inst);
187 inst_entry->listIt = --(instList[tid].end());
189 // Check any barriers and the dependence predictor for any
190 // producing memrefs/stores.
191 InstSeqNum producing_store;
192 if ((inst->isLoad() || inst->isAtomic()) && loadBarrier) {
193 DPRINTF(MemDepUnit, "Load barrier [sn:%lli] in flight\n",
195 producing_store = loadBarrierSN;
196 } else if ((inst->isStore() || inst->isAtomic()) && storeBarrier) {
197 DPRINTF(MemDepUnit, "Store barrier [sn:%lli] in flight\n",
199 producing_store = storeBarrierSN;
201 producing_store = depPred.checkInst(inst->instAddr());
204 MemDepEntryPtr store_entry = NULL;
206 // If there is a producing store, try to find the entry.
207 if (producing_store != 0) {
208 DPRINTF(MemDepUnit, "Searching for producer\n");
209 MemDepHashIt hash_it = memDepHash.find(producing_store);
211 if (hash_it != memDepHash.end()) {
212 store_entry = (*hash_it).second;
213 DPRINTF(MemDepUnit, "Proucer found\n");
217 // If no store entry, then instruction can issue as soon as the registers
220 DPRINTF(MemDepUnit, "No dependency for inst PC "
221 "%s [sn:%lli].\n", inst->pcState(), inst->seqNum);
223 inst_entry->memDepReady = true;
225 if (inst->readyToIssue()) {
226 inst_entry->regsReady = true;
228 moveToReady(inst_entry);
231 // Otherwise make the instruction dependent on the store/barrier.
232 DPRINTF(MemDepUnit, "Adding to dependency list; "
233 "inst PC %s is dependent on [sn:%lli].\n",
234 inst->pcState(), producing_store);
236 if (inst->readyToIssue()) {
237 inst_entry->regsReady = true;
240 // Clear the bit saying this instruction can issue.
241 inst->clearCanIssue();
243 // Add this instruction to the list of dependents.
244 store_entry->dependInsts.push_back(inst_entry);
246 if (inst->isLoad()) {
253 if (inst->isStore() || inst->isAtomic()) {
254 DPRINTF(MemDepUnit, "Inserting store/atomic PC %s [sn:%lli].\n",
255 inst->pcState(), inst->seqNum);
257 depPred.insertStore(inst->instAddr(), inst->seqNum, inst->threadNumber);
260 } else if (inst->isLoad()) {
263 panic("Unknown type! (most likely a barrier).");
267 template <class MemDepPred, class Impl>
269 MemDepUnit<MemDepPred, Impl>::insertNonSpec(const DynInstPtr &inst)
271 ThreadID tid = inst->threadNumber;
273 MemDepEntryPtr inst_entry = std::make_shared<MemDepEntry>(inst);
275 // Insert the MemDepEntry into the hash.
277 std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
279 MemDepEntry::memdep_insert++;
282 // Add the instruction to the list.
283 instList[tid].push_back(inst);
285 inst_entry->listIt = --(instList[tid].end());
287 // Might want to turn this part into an inline function or something.
288 // It's shared between both insert functions.
289 if (inst->isStore() || inst->isAtomic()) {
290 DPRINTF(MemDepUnit, "Inserting store/atomic PC %s [sn:%lli].\n",
291 inst->pcState(), inst->seqNum);
293 depPred.insertStore(inst->instAddr(), inst->seqNum, inst->threadNumber);
296 } else if (inst->isLoad()) {
299 panic("Unknown type! (most likely a barrier).");
303 template <class MemDepPred, class Impl>
305 MemDepUnit<MemDepPred, Impl>::insertBarrier(const DynInstPtr &barr_inst)
307 InstSeqNum barr_sn = barr_inst->seqNum;
308 // Memory barriers block loads and stores, write barriers only stores.
309 if (barr_inst->isMemBarrier()) {
311 loadBarrierSN = barr_sn;
313 storeBarrierSN = barr_sn;
314 DPRINTF(MemDepUnit, "Inserted a memory barrier %s SN:%lli\n",
315 barr_inst->pcState(),barr_sn);
316 } else if (barr_inst->isWriteBarrier()) {
318 storeBarrierSN = barr_sn;
319 DPRINTF(MemDepUnit, "Inserted a write barrier\n");
322 ThreadID tid = barr_inst->threadNumber;
324 MemDepEntryPtr inst_entry = std::make_shared<MemDepEntry>(barr_inst);
326 // Add the MemDepEntry to the hash.
328 std::pair<InstSeqNum, MemDepEntryPtr>(barr_sn, inst_entry));
330 MemDepEntry::memdep_insert++;
333 // Add the instruction to the instruction list.
334 instList[tid].push_back(barr_inst);
336 inst_entry->listIt = --(instList[tid].end());
339 template <class MemDepPred, class Impl>
341 MemDepUnit<MemDepPred, Impl>::regsReady(const DynInstPtr &inst)
343 DPRINTF(MemDepUnit, "Marking registers as ready for "
344 "instruction PC %s [sn:%lli].\n",
345 inst->pcState(), inst->seqNum);
347 MemDepEntryPtr inst_entry = findInHash(inst);
349 inst_entry->regsReady = true;
351 if (inst_entry->memDepReady) {
352 DPRINTF(MemDepUnit, "Instruction has its memory "
353 "dependencies resolved, adding it to the ready list.\n");
355 moveToReady(inst_entry);
357 DPRINTF(MemDepUnit, "Instruction still waiting on "
358 "memory dependency.\n");
362 template <class MemDepPred, class Impl>
364 MemDepUnit<MemDepPred, Impl>::nonSpecInstReady(const DynInstPtr &inst)
366 DPRINTF(MemDepUnit, "Marking non speculative "
367 "instruction PC %s as ready [sn:%lli].\n",
368 inst->pcState(), inst->seqNum);
370 MemDepEntryPtr inst_entry = findInHash(inst);
372 moveToReady(inst_entry);
375 template <class MemDepPred, class Impl>
377 MemDepUnit<MemDepPred, Impl>::reschedule(const DynInstPtr &inst)
379 instsToReplay.push_back(inst);
382 template <class MemDepPred, class Impl>
384 MemDepUnit<MemDepPred, Impl>::replay()
386 DynInstPtr temp_inst;
388 // For now this replay function replays all waiting memory ops.
389 while (!instsToReplay.empty()) {
390 temp_inst = instsToReplay.front();
392 MemDepEntryPtr inst_entry = findInHash(temp_inst);
394 DPRINTF(MemDepUnit, "Replaying mem instruction PC %s [sn:%lli].\n",
395 temp_inst->pcState(), temp_inst->seqNum);
397 moveToReady(inst_entry);
399 instsToReplay.pop_front();
403 template <class MemDepPred, class Impl>
405 MemDepUnit<MemDepPred, Impl>::completed(const DynInstPtr &inst)
407 DPRINTF(MemDepUnit, "Completed mem instruction PC %s [sn:%lli].\n",
408 inst->pcState(), inst->seqNum);
410 ThreadID tid = inst->threadNumber;
412 // Remove the instruction from the hash and the list.
413 MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
415 assert(hash_it != memDepHash.end());
417 instList[tid].erase((*hash_it).second->listIt);
419 (*hash_it).second = NULL;
421 memDepHash.erase(hash_it);
423 MemDepEntry::memdep_erase++;
427 template <class MemDepPred, class Impl>
429 MemDepUnit<MemDepPred, Impl>::completeBarrier(const DynInstPtr &inst)
431 wakeDependents(inst);
434 InstSeqNum barr_sn = inst->seqNum;
435 DPRINTF(MemDepUnit, "barrier completed: %s SN:%lli\n", inst->pcState(),
437 if (inst->isMemBarrier()) {
438 if (loadBarrierSN == barr_sn)
440 if (storeBarrierSN == barr_sn)
441 storeBarrier = false;
442 } else if (inst->isWriteBarrier()) {
443 if (storeBarrierSN == barr_sn)
444 storeBarrier = false;
448 template <class MemDepPred, class Impl>
450 MemDepUnit<MemDepPred, Impl>::wakeDependents(const DynInstPtr &inst)
452 // Only stores, atomics and barriers have dependents.
453 if (!inst->isStore() && !inst->isAtomic() && !inst->isMemBarrier() &&
454 !inst->isWriteBarrier()) {
458 MemDepEntryPtr inst_entry = findInHash(inst);
460 for (int i = 0; i < inst_entry->dependInsts.size(); ++i ) {
461 MemDepEntryPtr woken_inst = inst_entry->dependInsts[i];
463 if (!woken_inst->inst) {
464 // Potentially removed mem dep entries could be on this list
468 DPRINTF(MemDepUnit, "Waking up a dependent inst, "
470 woken_inst->inst->seqNum);
472 if (woken_inst->regsReady && !woken_inst->squashed) {
473 moveToReady(woken_inst);
475 woken_inst->memDepReady = true;
479 inst_entry->dependInsts.clear();
482 template <class MemDepPred, class Impl>
484 MemDepUnit<MemDepPred, Impl>::squash(const InstSeqNum &squashed_num,
487 if (!instsToReplay.empty()) {
488 ListIt replay_it = instsToReplay.begin();
489 while (replay_it != instsToReplay.end()) {
490 if ((*replay_it)->threadNumber == tid &&
491 (*replay_it)->seqNum > squashed_num) {
492 instsToReplay.erase(replay_it++);
499 ListIt squash_it = instList[tid].end();
502 MemDepHashIt hash_it;
504 while (!instList[tid].empty() &&
505 (*squash_it)->seqNum > squashed_num) {
507 DPRINTF(MemDepUnit, "Squashing inst [sn:%lli]\n",
508 (*squash_it)->seqNum);
510 if ((*squash_it)->seqNum == loadBarrierSN)
513 if ((*squash_it)->seqNum == storeBarrierSN)
514 storeBarrier = false;
516 hash_it = memDepHash.find((*squash_it)->seqNum);
518 assert(hash_it != memDepHash.end());
520 (*hash_it).second->squashed = true;
522 (*hash_it).second = NULL;
524 memDepHash.erase(hash_it);
526 MemDepEntry::memdep_erase++;
529 instList[tid].erase(squash_it--);
532 // Tell the dependency predictor to squash as well.
533 depPred.squash(squashed_num, tid);
536 template <class MemDepPred, class Impl>
538 MemDepUnit<MemDepPred, Impl>::violation(const DynInstPtr &store_inst,
539 const DynInstPtr &violating_load)
541 DPRINTF(MemDepUnit, "Passing violating PCs to store sets,"
542 " load: %#x, store: %#x\n", violating_load->instAddr(),
543 store_inst->instAddr());
544 // Tell the memory dependence unit of the violation.
545 depPred.violation(store_inst->instAddr(), violating_load->instAddr());
548 template <class MemDepPred, class Impl>
550 MemDepUnit<MemDepPred, Impl>::issue(const DynInstPtr &inst)
552 DPRINTF(MemDepUnit, "Issuing instruction PC %#x [sn:%lli].\n",
553 inst->instAddr(), inst->seqNum);
555 depPred.issued(inst->instAddr(), inst->seqNum, inst->isStore());
558 template <class MemDepPred, class Impl>
559 inline typename MemDepUnit<MemDepPred,Impl>::MemDepEntryPtr &
560 MemDepUnit<MemDepPred, Impl>::findInHash(const DynInstConstPtr &inst)
562 MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
564 assert(hash_it != memDepHash.end());
566 return (*hash_it).second;
569 template <class MemDepPred, class Impl>
571 MemDepUnit<MemDepPred, Impl>::moveToReady(MemDepEntryPtr &woken_inst_entry)
573 DPRINTF(MemDepUnit, "Adding instruction [sn:%lli] "
574 "to the ready list.\n", woken_inst_entry->inst->seqNum);
576 assert(!woken_inst_entry->squashed);
578 iqPtr->addReadyMemInst(woken_inst_entry->inst);
582 template <class MemDepPred, class Impl>
584 MemDepUnit<MemDepPred, Impl>::dumpLists()
586 for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
587 cprintf("Instruction list %i size: %i\n",
588 tid, instList[tid].size());
590 ListIt inst_list_it = instList[tid].begin();
593 while (inst_list_it != instList[tid].end()) {
594 cprintf("Instruction:%i\nPC: %s\n[sn:%llu]\n[tid:%i]\nIssued:%i\n"
596 num, (*inst_list_it)->pcState(),
597 (*inst_list_it)->seqNum,
598 (*inst_list_it)->threadNumber,
599 (*inst_list_it)->isIssued(),
600 (*inst_list_it)->isSquashed());
606 cprintf("Memory dependence hash size: %i\n", memDepHash.size());
609 cprintf("Memory dependence entries: %i\n", MemDepEntry::memdep_count);
613 #endif//__CPU_O3_MEM_DEP_UNIT_IMPL_HH__