2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "cpu/o3/inst_queue.hh"
34 #include "cpu/o3/mem_dep_unit.hh"
36 template <class MemDepPred, class Impl>
37 MemDepUnit<MemDepPred, Impl>::MemDepUnit(Params *params)
38 : depPred(params->SSITSize, params->LFSTSize), loadBarrier(false),
39 loadBarrierSN(0), storeBarrier(false), storeBarrierSN(0), iqPtr(NULL)
41 DPRINTF(MemDepUnit, "Creating MemDepUnit object.\n");
44 template <class MemDepPred, class Impl>
45 MemDepUnit<MemDepPred, Impl>::~MemDepUnit()
47 for (int tid=0; tid < Impl::MaxThreads; tid++) {
49 ListIt inst_list_it = instList[tid].begin();
53 while (!instList[tid].empty()) {
54 hash_it = memDepHash.find((*inst_list_it)->seqNum);
56 assert(hash_it != memDepHash.end());
58 memDepHash.erase(hash_it);
60 instList[tid].erase(inst_list_it++);
65 assert(MemDepEntry::memdep_count == 0);
69 template <class MemDepPred, class Impl>
71 MemDepUnit<MemDepPred, Impl>::name() const
76 template <class MemDepPred, class Impl>
78 MemDepUnit<MemDepPred, Impl>::init(Params *params, int tid)
80 DPRINTF(MemDepUnit, "Creating MemDepUnit %i object.\n",tid);
84 depPred.init(params->SSITSize, params->LFSTSize);
87 template <class MemDepPred, class Impl>
89 MemDepUnit<MemDepPred, Impl>::regStats()
92 .name(name() + ".memDep.insertedLoads")
93 .desc("Number of loads inserted to the mem dependence unit.");
96 .name(name() + ".memDep.insertedStores")
97 .desc("Number of stores inserted to the mem dependence unit.");
100 .name(name() + ".memDep.conflictingLoads")
101 .desc("Number of conflicting loads.");
104 .name(name() + ".memDep.conflictingStores")
105 .desc("Number of conflicting stores.");
108 template <class MemDepPred, class Impl>
110 MemDepUnit<MemDepPred, Impl>::switchOut()
112 assert(instList[0].empty());
113 assert(instsToReplay.empty());
114 assert(memDepHash.empty());
116 for (int i = 0; i < Impl::MaxThreads; ++i) {
119 instsToReplay.clear();
123 template <class MemDepPred, class Impl>
125 MemDepUnit<MemDepPred, Impl>::takeOverFrom()
127 // Be sure to reset all state.
128 loadBarrier = storeBarrier = false;
129 loadBarrierSN = storeBarrierSN = 0;
133 template <class MemDepPred, class Impl>
135 MemDepUnit<MemDepPred, Impl>::setIQ(InstructionQueue<Impl> *iq_ptr)
140 template <class MemDepPred, class Impl>
142 MemDepUnit<MemDepPred, Impl>::insert(DynInstPtr &inst)
144 unsigned tid = inst->threadNumber;
146 MemDepEntryPtr inst_entry = new MemDepEntry(inst);
148 // Add the MemDepEntry to the hash.
150 std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
152 MemDepEntry::memdep_insert++;
155 instList[tid].push_back(inst);
157 inst_entry->listIt = --(instList[tid].end());
159 // Check any barriers and the dependence predictor for any
160 // producing memrefs/stores.
161 InstSeqNum producing_store;
162 if (inst->isLoad() && loadBarrier) {
163 producing_store = loadBarrierSN;
164 } else if (inst->isStore() && storeBarrier) {
165 producing_store = storeBarrierSN;
167 producing_store = depPred.checkInst(inst->readPC());
170 MemDepEntryPtr store_entry = NULL;
172 // If there is a producing store, try to find the entry.
173 if (producing_store != 0) {
174 MemDepHashIt hash_it = memDepHash.find(producing_store);
176 if (hash_it != memDepHash.end()) {
177 store_entry = (*hash_it).second;
181 // If no store entry, then instruction can issue as soon as the registers
184 DPRINTF(MemDepUnit, "No dependency for inst PC "
185 "%#x [sn:%lli].\n", inst->readPC(), inst->seqNum);
187 inst_entry->memDepReady = true;
189 if (inst->readyToIssue()) {
190 inst_entry->regsReady = true;
192 moveToReady(inst_entry);
195 // Otherwise make the instruction dependent on the store/barrier.
196 DPRINTF(MemDepUnit, "Adding to dependency list; "
197 "inst PC %#x is dependent on [sn:%lli].\n",
198 inst->readPC(), producing_store);
200 if (inst->readyToIssue()) {
201 inst_entry->regsReady = true;
204 // Add this instruction to the list of dependents.
205 store_entry->dependInsts.push_back(inst_entry);
207 if (inst->isLoad()) {
214 if (inst->isStore()) {
215 DPRINTF(MemDepUnit, "Inserting store PC %#x [sn:%lli].\n",
216 inst->readPC(), inst->seqNum);
218 depPred.insertStore(inst->readPC(), inst->seqNum, inst->threadNumber);
221 } else if (inst->isLoad()) {
224 panic("Unknown type! (most likely a barrier).");
228 template <class MemDepPred, class Impl>
230 MemDepUnit<MemDepPred, Impl>::insertNonSpec(DynInstPtr &inst)
232 unsigned tid = inst->threadNumber;
234 MemDepEntryPtr inst_entry = new MemDepEntry(inst);
236 // Insert the MemDepEntry into the hash.
238 std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
240 MemDepEntry::memdep_insert++;
243 // Add the instruction to the list.
244 instList[tid].push_back(inst);
246 inst_entry->listIt = --(instList[tid].end());
248 // Might want to turn this part into an inline function or something.
249 // It's shared between both insert functions.
250 if (inst->isStore()) {
251 DPRINTF(MemDepUnit, "Inserting store PC %#x [sn:%lli].\n",
252 inst->readPC(), inst->seqNum);
254 depPred.insertStore(inst->readPC(), inst->seqNum, inst->threadNumber);
257 } else if (inst->isLoad()) {
260 panic("Unknown type! (most likely a barrier).");
264 template <class MemDepPred, class Impl>
266 MemDepUnit<MemDepPred, Impl>::insertBarrier(DynInstPtr &barr_inst)
268 InstSeqNum barr_sn = barr_inst->seqNum;
269 // Memory barriers block loads and stores, write barriers only stores.
270 if (barr_inst->isMemBarrier()) {
272 loadBarrierSN = barr_sn;
274 storeBarrierSN = barr_sn;
275 DPRINTF(MemDepUnit, "Inserted a memory barrier\n");
276 } else if (barr_inst->isWriteBarrier()) {
278 storeBarrierSN = barr_sn;
279 DPRINTF(MemDepUnit, "Inserted a write barrier\n");
282 unsigned tid = barr_inst->threadNumber;
284 MemDepEntryPtr inst_entry = new MemDepEntry(barr_inst);
286 // Add the MemDepEntry to the hash.
288 std::pair<InstSeqNum, MemDepEntryPtr>(barr_sn, inst_entry));
290 MemDepEntry::memdep_insert++;
293 // Add the instruction to the instruction list.
294 instList[tid].push_back(barr_inst);
296 inst_entry->listIt = --(instList[tid].end());
299 template <class MemDepPred, class Impl>
301 MemDepUnit<MemDepPred, Impl>::regsReady(DynInstPtr &inst)
303 DPRINTF(MemDepUnit, "Marking registers as ready for "
304 "instruction PC %#x [sn:%lli].\n",
305 inst->readPC(), inst->seqNum);
307 MemDepEntryPtr inst_entry = findInHash(inst);
309 inst_entry->regsReady = true;
311 if (inst_entry->memDepReady) {
312 DPRINTF(MemDepUnit, "Instruction has its memory "
313 "dependencies resolved, adding it to the ready list.\n");
315 moveToReady(inst_entry);
317 DPRINTF(MemDepUnit, "Instruction still waiting on "
318 "memory dependency.\n");
322 template <class MemDepPred, class Impl>
324 MemDepUnit<MemDepPred, Impl>::nonSpecInstReady(DynInstPtr &inst)
326 DPRINTF(MemDepUnit, "Marking non speculative "
327 "instruction PC %#x as ready [sn:%lli].\n",
328 inst->readPC(), inst->seqNum);
330 MemDepEntryPtr inst_entry = findInHash(inst);
332 moveToReady(inst_entry);
335 template <class MemDepPred, class Impl>
337 MemDepUnit<MemDepPred, Impl>::reschedule(DynInstPtr &inst)
339 instsToReplay.push_back(inst);
342 template <class MemDepPred, class Impl>
344 MemDepUnit<MemDepPred, Impl>::replay(DynInstPtr &inst)
346 DynInstPtr temp_inst;
347 bool found_inst = false;
349 // For now this replay function replays all waiting memory ops.
350 while (!instsToReplay.empty()) {
351 temp_inst = instsToReplay.front();
353 MemDepEntryPtr inst_entry = findInHash(temp_inst);
355 DPRINTF(MemDepUnit, "Replaying mem instruction PC %#x "
357 temp_inst->readPC(), temp_inst->seqNum);
359 moveToReady(inst_entry);
361 if (temp_inst == inst) {
365 instsToReplay.pop_front();
371 template <class MemDepPred, class Impl>
373 MemDepUnit<MemDepPred, Impl>::completed(DynInstPtr &inst)
375 DPRINTF(MemDepUnit, "Completed mem instruction PC %#x "
377 inst->readPC(), inst->seqNum);
379 unsigned tid = inst->threadNumber;
381 // Remove the instruction from the hash and the list.
382 MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
384 assert(hash_it != memDepHash.end());
386 instList[tid].erase((*hash_it).second->listIt);
388 (*hash_it).second = NULL;
390 memDepHash.erase(hash_it);
392 MemDepEntry::memdep_erase++;
396 template <class MemDepPred, class Impl>
398 MemDepUnit<MemDepPred, Impl>::completeBarrier(DynInstPtr &inst)
400 wakeDependents(inst);
403 InstSeqNum barr_sn = inst->seqNum;
405 if (inst->isMemBarrier()) {
406 assert(loadBarrier && storeBarrier);
407 if (loadBarrierSN == barr_sn)
409 if (storeBarrierSN == barr_sn)
410 storeBarrier = false;
411 } else if (inst->isWriteBarrier()) {
412 assert(storeBarrier);
413 if (storeBarrierSN == barr_sn)
414 storeBarrier = false;
418 template <class MemDepPred, class Impl>
420 MemDepUnit<MemDepPred, Impl>::wakeDependents(DynInstPtr &inst)
422 // Only stores and barriers have dependents.
423 if (!inst->isStore() && !inst->isMemBarrier() && !inst->isWriteBarrier()) {
427 MemDepEntryPtr inst_entry = findInHash(inst);
429 for (int i = 0; i < inst_entry->dependInsts.size(); ++i ) {
430 MemDepEntryPtr woken_inst = inst_entry->dependInsts[i];
432 if (!woken_inst->inst) {
433 // Potentially removed mem dep entries could be on this list
437 DPRINTF(MemDepUnit, "Waking up a dependent inst, "
439 woken_inst->inst->seqNum);
441 if (woken_inst->regsReady && !woken_inst->squashed) {
442 moveToReady(woken_inst);
444 woken_inst->memDepReady = true;
448 inst_entry->dependInsts.clear();
451 template <class MemDepPred, class Impl>
453 MemDepUnit<MemDepPred, Impl>::squash(const InstSeqNum &squashed_num,
456 if (!instsToReplay.empty()) {
457 ListIt replay_it = instsToReplay.begin();
458 while (replay_it != instsToReplay.end()) {
459 if ((*replay_it)->threadNumber == tid &&
460 (*replay_it)->seqNum > squashed_num) {
461 instsToReplay.erase(replay_it++);
468 ListIt squash_it = instList[tid].end();
471 MemDepHashIt hash_it;
473 while (!instList[tid].empty() &&
474 (*squash_it)->seqNum > squashed_num) {
476 DPRINTF(MemDepUnit, "Squashing inst [sn:%lli]\n",
477 (*squash_it)->seqNum);
479 hash_it = memDepHash.find((*squash_it)->seqNum);
481 assert(hash_it != memDepHash.end());
483 (*hash_it).second->squashed = true;
485 (*hash_it).second = NULL;
487 memDepHash.erase(hash_it);
489 MemDepEntry::memdep_erase++;
492 instList[tid].erase(squash_it--);
495 // Tell the dependency predictor to squash as well.
496 depPred.squash(squashed_num, tid);
499 template <class MemDepPred, class Impl>
501 MemDepUnit<MemDepPred, Impl>::violation(DynInstPtr &store_inst,
502 DynInstPtr &violating_load)
504 DPRINTF(MemDepUnit, "Passing violating PCs to store sets,"
505 " load: %#x, store: %#x\n", violating_load->readPC(),
506 store_inst->readPC());
507 // Tell the memory dependence unit of the violation.
508 depPred.violation(violating_load->readPC(), store_inst->readPC());
511 template <class MemDepPred, class Impl>
513 MemDepUnit<MemDepPred, Impl>::issue(DynInstPtr &inst)
515 DPRINTF(MemDepUnit, "Issuing instruction PC %#x [sn:%lli].\n",
516 inst->readPC(), inst->seqNum);
518 depPred.issued(inst->readPC(), inst->seqNum, inst->isStore());
521 template <class MemDepPred, class Impl>
522 inline typename MemDepUnit<MemDepPred,Impl>::MemDepEntryPtr &
523 MemDepUnit<MemDepPred, Impl>::findInHash(const DynInstPtr &inst)
525 MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
527 assert(hash_it != memDepHash.end());
529 return (*hash_it).second;
532 template <class MemDepPred, class Impl>
534 MemDepUnit<MemDepPred, Impl>::moveToReady(MemDepEntryPtr &woken_inst_entry)
536 DPRINTF(MemDepUnit, "Adding instruction [sn:%lli] "
537 "to the ready list.\n", woken_inst_entry->inst->seqNum);
539 assert(!woken_inst_entry->squashed);
541 iqPtr->addReadyMemInst(woken_inst_entry->inst);
545 template <class MemDepPred, class Impl>
547 MemDepUnit<MemDepPred, Impl>::dumpLists()
549 for (unsigned tid=0; tid < Impl::MaxThreads; tid++) {
550 cprintf("Instruction list %i size: %i\n",
551 tid, instList[tid].size());
553 ListIt inst_list_it = instList[tid].begin();
556 while (inst_list_it != instList[tid].end()) {
557 cprintf("Instruction:%i\nPC:%#x\n[sn:%i]\n[tid:%i]\nIssued:%i\n"
559 num, (*inst_list_it)->readPC(),
560 (*inst_list_it)->seqNum,
561 (*inst_list_it)->threadNumber,
562 (*inst_list_it)->isIssued(),
563 (*inst_list_it)->isSquashed());
569 cprintf("Memory dependence hash size: %i\n", memDepHash.size());
572 cprintf("Memory dependence entries: %i\n", MemDepEntry::memdep_count);