mem_dep_unit_impl.hh revision 2329
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 2004-2006 The Regents of The University of Michigan
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
297054Snate@binkert.org#include <map>
307054Snate@binkert.org
316145Snate@binkert.org#include "cpu/o3/inst_queue.hh"
327055Snate@binkert.org#include "cpu/o3/mem_dep_unit.hh"
337454Snate@binkert.org
347055Snate@binkert.orgtemplate <class MemDepPred, class Impl>
356154Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::MemDepUnit(Params *params)
366154Snate@binkert.org    : depPred(params->SSITSize, params->LFSTSize), loadBarrier(false),
376154Snate@binkert.org      loadBarrierSN(0), storeBarrier(false), storeBarrierSN(0), iqPtr(NULL)
387054Snate@binkert.org{
396876Ssteve.reinhardt@amd.com    DPRINTF(MemDepUnit, "Creating MemDepUnit object.\n");
406145Snate@binkert.org}
416145Snate@binkert.org
426145Snate@binkert.orgtemplate <class MemDepPred, class Impl>
436145Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::~MemDepUnit()
446145Snate@binkert.org{
456145Snate@binkert.org    for (int tid=0; tid < Impl::MaxThreads; tid++) {
466145Snate@binkert.org
477054Snate@binkert.org        ListIt inst_list_it = instList[tid].begin();
487054Snate@binkert.org
497054Snate@binkert.org        MemDepHashIt hash_it;
506876Ssteve.reinhardt@amd.com
516876Ssteve.reinhardt@amd.com        while (!instList[tid].empty()) {
527054Snate@binkert.org            hash_it = memDepHash.find((*inst_list_it)->seqNum);
536145Snate@binkert.org
547054Snate@binkert.org            assert(hash_it != memDepHash.end());
556145Snate@binkert.org
568259SBrad.Beckmann@amd.com            memDepHash.erase(hash_it);
578259SBrad.Beckmann@amd.com
588259SBrad.Beckmann@amd.com            instList[tid].erase(inst_list_it++);
598259SBrad.Beckmann@amd.com        }
607055Snate@binkert.org    }
617054Snate@binkert.org
627055Snate@binkert.org    assert(MemDepEntry::memdep_count == 0);
636285Snate@binkert.org}
647054Snate@binkert.org
656145Snate@binkert.orgtemplate <class MemDepPred, class Impl>
667054Snate@binkert.orgstd::string
677054Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::name() const
687054Snate@binkert.org{
697454Snate@binkert.org    return "memdepunit";
706145Snate@binkert.org}
717054Snate@binkert.org
727054Snate@binkert.orgtemplate <class MemDepPred, class Impl>
736145Snate@binkert.orgvoid
747054Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::init(Params *params, int tid)
756145Snate@binkert.org{
767054Snate@binkert.org    DPRINTF(MemDepUnit, "Creating MemDepUnit %i object.\n",tid);
778257SBrad.Beckmann@amd.com
788257SBrad.Beckmann@amd.com    id = tid;
798257SBrad.Beckmann@amd.com
808257SBrad.Beckmann@amd.com    depPred.init(params->SSITSize, params->LFSTSize);
818257SBrad.Beckmann@amd.com}
828257SBrad.Beckmann@amd.com
838257SBrad.Beckmann@amd.comtemplate <class MemDepPred, class Impl>
848257SBrad.Beckmann@amd.comvoid
858257SBrad.Beckmann@amd.comMemDepUnit<MemDepPred, Impl>::regStats()
868257SBrad.Beckmann@amd.com{
878257SBrad.Beckmann@amd.com    insertedLoads
888257SBrad.Beckmann@amd.com        .name(name() + ".memDep.insertedLoads")
896145Snate@binkert.org        .desc("Number of loads inserted to the mem dependence unit.");
907055Snate@binkert.org
916145Snate@binkert.org    insertedStores
927054Snate@binkert.org        .name(name() + ".memDep.insertedStores")
937054Snate@binkert.org        .desc("Number of stores inserted to the mem dependence unit.");
947054Snate@binkert.org
957054Snate@binkert.org    conflictingLoads
967054Snate@binkert.org        .name(name() + ".memDep.conflictingLoads")
977054Snate@binkert.org        .desc("Number of conflicting loads.");
987054Snate@binkert.org
997054Snate@binkert.org    conflictingStores
1006145Snate@binkert.org        .name(name() + ".memDep.conflictingStores")
1017054Snate@binkert.org        .desc("Number of conflicting stores.");
1027054Snate@binkert.org}
1037054Snate@binkert.org
1046145Snate@binkert.orgtemplate <class MemDepPred, class Impl>
1057054Snate@binkert.orgvoid
1067454Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::switchOut()
1077454Snate@binkert.org{
1086145Snate@binkert.org    for (int i = 0; i < Impl::MaxThreads; ++i) {
1097454Snate@binkert.org        instList[i].clear();
1107454Snate@binkert.org    }
1117454Snate@binkert.org    instsToReplay.clear();
1127454Snate@binkert.org    memDepHash.clear();
1137454Snate@binkert.org}
1148259SBrad.Beckmann@amd.com
1158259SBrad.Beckmann@amd.comtemplate <class MemDepPred, class Impl>
1168259SBrad.Beckmann@amd.comvoid
1178259SBrad.Beckmann@amd.comMemDepUnit<MemDepPred, Impl>::takeOverFrom()
1186145Snate@binkert.org{
1196145Snate@binkert.org    loadBarrier = storeBarrier = false;
1207055Snate@binkert.org    loadBarrierSN = storeBarrierSN = 0;
1217055Snate@binkert.org    depPred.clear();
1226145Snate@binkert.org}
1237054Snate@binkert.org
1247055Snate@binkert.orgtemplate <class MemDepPred, class Impl>
1257054Snate@binkert.orgvoid
1266145Snate@binkert.orgMemDepUnit<MemDepPred, Impl>::setIQ(InstructionQueue<Impl> *iq_ptr)
1276145Snate@binkert.org{
1287054Snate@binkert.org    iqPtr = iq_ptr;
129}
130
131template <class MemDepPred, class Impl>
132void
133MemDepUnit<MemDepPred, Impl>::insert(DynInstPtr &inst)
134{
135    unsigned tid = inst->threadNumber;
136
137    MemDepEntryPtr inst_entry = new MemDepEntry(inst);
138
139    // Add the MemDepEntry to the hash.
140    memDepHash.insert(
141        std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
142    MemDepEntry::memdep_insert++;
143
144    instList[tid].push_back(inst);
145
146    inst_entry->listIt = --(instList[tid].end());
147
148    // Check any barriers and the dependence predictor for any
149    // producing stores.
150    InstSeqNum producing_store;
151    if (inst->isLoad() && loadBarrier) {
152        producing_store = loadBarrierSN;
153    } else if (inst->isStore() && storeBarrier) {
154        producing_store = storeBarrierSN;
155    } else {
156        producing_store = depPred.checkInst(inst->readPC());
157    }
158
159    MemDepEntryPtr store_entry = NULL;
160
161    // If there is a producing store, try to find the entry.
162    if (producing_store != 0) {
163        MemDepHashIt hash_it = memDepHash.find(producing_store);
164
165        if (hash_it != memDepHash.end()) {
166            store_entry = (*hash_it).second;
167        }
168    }
169
170    // If no store entry, then instruction can issue as soon as the registers
171    // are ready.
172    if (!store_entry) {
173        DPRINTF(MemDepUnit, "No dependency for inst PC "
174                "%#x [sn:%lli].\n", inst->readPC(), inst->seqNum);
175
176        inst_entry->memDepReady = true;
177
178        if (inst->readyToIssue()) {
179            inst_entry->regsReady = true;
180
181            moveToReady(inst_entry);
182        }
183    } else {
184        // Otherwise make the instruction dependent on the store/barrier.
185        DPRINTF(MemDepUnit, "Adding to dependency list; "
186                "inst PC %#x is dependent on [sn:%lli].\n",
187                inst->readPC(), producing_store);
188
189        if (inst->readyToIssue()) {
190            inst_entry->regsReady = true;
191        }
192
193        // Add this instruction to the list of dependents.
194        store_entry->dependInsts.push_back(inst_entry);
195
196        if (inst->isLoad()) {
197            ++conflictingLoads;
198        } else {
199            ++conflictingStores;
200        }
201    }
202
203    if (inst->isStore()) {
204        DPRINTF(MemDepUnit, "Inserting store PC %#x [sn:%lli].\n",
205                inst->readPC(), inst->seqNum);
206
207        depPred.insertStore(inst->readPC(), inst->seqNum, inst->threadNumber);
208
209        ++insertedStores;
210    } else if (inst->isLoad()) {
211        ++insertedLoads;
212    } else {
213        panic("Unknown type! (most likely a barrier).");
214    }
215}
216
217template <class MemDepPred, class Impl>
218void
219MemDepUnit<MemDepPred, Impl>::insertNonSpec(DynInstPtr &inst)
220{
221    unsigned tid = inst->threadNumber;
222
223    MemDepEntryPtr inst_entry = new MemDepEntry(inst);
224
225    // Insert the MemDepEntry into the hash.
226    memDepHash.insert(
227        std::pair<InstSeqNum, MemDepEntryPtr>(inst->seqNum, inst_entry));
228    MemDepEntry::memdep_insert++;
229
230    // Add the instruction to the list.
231    instList[tid].push_back(inst);
232
233    inst_entry->listIt = --(instList[tid].end());
234
235    // Might want to turn this part into an inline function or something.
236    // It's shared between both insert functions.
237    if (inst->isStore()) {
238        DPRINTF(MemDepUnit, "Inserting store PC %#x [sn:%lli].\n",
239                inst->readPC(), inst->seqNum);
240
241        depPred.insertStore(inst->readPC(), inst->seqNum, inst->threadNumber);
242
243        ++insertedStores;
244    } else if (inst->isLoad()) {
245        ++insertedLoads;
246    } else {
247        panic("Unknown type! (most likely a barrier).");
248    }
249}
250
251template <class MemDepPred, class Impl>
252void
253MemDepUnit<MemDepPred, Impl>::insertBarrier(DynInstPtr &barr_inst)
254{
255    InstSeqNum barr_sn = barr_inst->seqNum;
256    if (barr_inst->isMemBarrier()) {
257        loadBarrier = true;
258        loadBarrierSN = barr_sn;
259        storeBarrier = true;
260        storeBarrierSN = barr_sn;
261        DPRINTF(MemDepUnit, "Inserted a memory barrier\n");
262    } else if (barr_inst->isWriteBarrier()) {
263        storeBarrier = true;
264        storeBarrierSN = barr_sn;
265        DPRINTF(MemDepUnit, "Inserted a write barrier\n");
266    }
267
268    unsigned tid = barr_inst->threadNumber;
269
270    MemDepEntryPtr inst_entry = new MemDepEntry(barr_inst);
271
272    // Add the MemDepEntry to the hash.
273    memDepHash.insert(
274        std::pair<InstSeqNum, MemDepEntryPtr>(barr_sn, inst_entry));
275    MemDepEntry::memdep_insert++;
276
277    // Add the instruction to the instruction list.
278    instList[tid].push_back(barr_inst);
279
280    inst_entry->listIt = --(instList[tid].end());
281}
282
283template <class MemDepPred, class Impl>
284void
285MemDepUnit<MemDepPred, Impl>::regsReady(DynInstPtr &inst)
286{
287    DPRINTF(MemDepUnit, "Marking registers as ready for "
288            "instruction PC %#x [sn:%lli].\n",
289            inst->readPC(), inst->seqNum);
290
291    MemDepEntryPtr inst_entry = findInHash(inst);
292
293    inst_entry->regsReady = true;
294
295    if (inst_entry->memDepReady) {
296        DPRINTF(MemDepUnit, "Instruction has its memory "
297                "dependencies resolved, adding it to the ready list.\n");
298
299        moveToReady(inst_entry);
300    } else {
301        DPRINTF(MemDepUnit, "Instruction still waiting on "
302                "memory dependency.\n");
303    }
304}
305
306template <class MemDepPred, class Impl>
307void
308MemDepUnit<MemDepPred, Impl>::nonSpecInstReady(DynInstPtr &inst)
309{
310    DPRINTF(MemDepUnit, "Marking non speculative "
311            "instruction PC %#x as ready [sn:%lli].\n",
312            inst->readPC(), inst->seqNum);
313
314    MemDepEntryPtr inst_entry = findInHash(inst);
315
316    moveToReady(inst_entry);
317}
318
319template <class MemDepPred, class Impl>
320void
321MemDepUnit<MemDepPred, Impl>::reschedule(DynInstPtr &inst)
322{
323    instsToReplay.push_back(inst);
324}
325
326template <class MemDepPred, class Impl>
327void
328MemDepUnit<MemDepPred, Impl>::replay(DynInstPtr &inst)
329{
330    DynInstPtr temp_inst;
331    bool found_inst = false;
332
333    while (!instsToReplay.empty()) {
334        temp_inst = instsToReplay.front();
335
336        MemDepEntryPtr inst_entry = findInHash(temp_inst);
337
338        DPRINTF(MemDepUnit, "Replaying mem instruction PC %#x "
339                "[sn:%lli].\n",
340                temp_inst->readPC(), temp_inst->seqNum);
341
342        moveToReady(inst_entry);
343
344        if (temp_inst == inst) {
345            found_inst = true;
346        }
347
348        instsToReplay.pop_front();
349    }
350
351    assert(found_inst);
352}
353
354template <class MemDepPred, class Impl>
355void
356MemDepUnit<MemDepPred, Impl>::completed(DynInstPtr &inst)
357{
358    DPRINTF(MemDepUnit, "Completed mem instruction PC %#x "
359            "[sn:%lli].\n",
360            inst->readPC(), inst->seqNum);
361
362    unsigned tid = inst->threadNumber;
363
364    // Remove the instruction from the hash and the list.
365    MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
366
367    assert(hash_it != memDepHash.end());
368
369    instList[tid].erase((*hash_it).second->listIt);
370
371    (*hash_it).second = NULL;
372
373    memDepHash.erase(hash_it);
374    MemDepEntry::memdep_erase++;
375}
376
377template <class MemDepPred, class Impl>
378void
379MemDepUnit<MemDepPred, Impl>::completeBarrier(DynInstPtr &inst)
380{
381    wakeDependents(inst);
382    completed(inst);
383
384    InstSeqNum barr_sn = inst->seqNum;
385
386    if (inst->isMemBarrier()) {
387        assert(loadBarrier && storeBarrier);
388        if (loadBarrierSN == barr_sn)
389            loadBarrier = false;
390        if (storeBarrierSN == barr_sn)
391            storeBarrier = false;
392    } else if (inst->isWriteBarrier()) {
393        assert(storeBarrier);
394        if (storeBarrierSN == barr_sn)
395            storeBarrier = false;
396    }
397}
398
399template <class MemDepPred, class Impl>
400void
401MemDepUnit<MemDepPred, Impl>::wakeDependents(DynInstPtr &inst)
402{
403    // Only stores and barriers have dependents.
404    if (!inst->isStore() && !inst->isMemBarrier() && !inst->isWriteBarrier()) {
405        return;
406    }
407
408    MemDepEntryPtr inst_entry = findInHash(inst);
409
410    for (int i = 0; i < inst_entry->dependInsts.size(); ++i ) {
411        MemDepEntryPtr woken_inst = inst_entry->dependInsts[i];
412
413        if (!woken_inst->inst) {
414            // Potentially removed mem dep entries could be on this list
415            continue;
416        }
417
418        DPRINTF(MemDepUnit, "Waking up a dependent inst, "
419                "[sn:%lli].\n",
420                woken_inst->inst->seqNum);
421
422        if (woken_inst->regsReady && !woken_inst->squashed) {
423            moveToReady(woken_inst);
424        } else {
425            woken_inst->memDepReady = true;
426        }
427    }
428
429    inst_entry->dependInsts.clear();
430}
431
432template <class MemDepPred, class Impl>
433void
434MemDepUnit<MemDepPred, Impl>::squash(const InstSeqNum &squashed_num,
435                                     unsigned tid)
436{
437    if (!instsToReplay.empty()) {
438        ListIt replay_it = instsToReplay.begin();
439        while (replay_it != instsToReplay.end()) {
440            if ((*replay_it)->threadNumber == tid &&
441                (*replay_it)->seqNum > squashed_num) {
442                instsToReplay.erase(replay_it++);
443            } else {
444                ++replay_it;
445            }
446        }
447    }
448
449    ListIt squash_it = instList[tid].end();
450    --squash_it;
451
452    MemDepHashIt hash_it;
453
454    while (!instList[tid].empty() &&
455           (*squash_it)->seqNum > squashed_num) {
456
457        DPRINTF(MemDepUnit, "Squashing inst [sn:%lli]\n",
458                (*squash_it)->seqNum);
459
460        hash_it = memDepHash.find((*squash_it)->seqNum);
461
462        assert(hash_it != memDepHash.end());
463
464        (*hash_it).second->squashed = true;
465
466        (*hash_it).second = NULL;
467
468        memDepHash.erase(hash_it);
469        MemDepEntry::memdep_erase++;
470
471        instList[tid].erase(squash_it--);
472    }
473
474    // Tell the dependency predictor to squash as well.
475    depPred.squash(squashed_num, tid);
476}
477
478template <class MemDepPred, class Impl>
479void
480MemDepUnit<MemDepPred, Impl>::violation(DynInstPtr &store_inst,
481                                        DynInstPtr &violating_load)
482{
483    DPRINTF(MemDepUnit, "Passing violating PCs to store sets,"
484            " load: %#x, store: %#x\n", violating_load->readPC(),
485            store_inst->readPC());
486    // Tell the memory dependence unit of the violation.
487    depPred.violation(violating_load->readPC(), store_inst->readPC());
488}
489
490template <class MemDepPred, class Impl>
491void
492MemDepUnit<MemDepPred, Impl>::issue(DynInstPtr &inst)
493{
494    DPRINTF(MemDepUnit, "Issuing instruction PC %#x [sn:%lli].\n",
495            inst->readPC(), inst->seqNum);
496
497    depPred.issued(inst->readPC(), inst->seqNum, inst->isStore());
498}
499
500template <class MemDepPred, class Impl>
501inline typename MemDepUnit<MemDepPred,Impl>::MemDepEntryPtr &
502MemDepUnit<MemDepPred, Impl>::findInHash(const DynInstPtr &inst)
503{
504    MemDepHashIt hash_it = memDepHash.find(inst->seqNum);
505
506    assert(hash_it != memDepHash.end());
507
508    return (*hash_it).second;
509}
510
511template <class MemDepPred, class Impl>
512inline void
513MemDepUnit<MemDepPred, Impl>::moveToReady(MemDepEntryPtr &woken_inst_entry)
514{
515    DPRINTF(MemDepUnit, "Adding instruction [sn:%lli] "
516            "to the ready list.\n", woken_inst_entry->inst->seqNum);
517
518    assert(!woken_inst_entry->squashed);
519
520    iqPtr->addReadyMemInst(woken_inst_entry->inst);
521}
522
523
524template <class MemDepPred, class Impl>
525void
526MemDepUnit<MemDepPred, Impl>::dumpLists()
527{
528    for (unsigned tid=0; tid < Impl::MaxThreads; tid++) {
529        cprintf("Instruction list %i size: %i\n",
530                tid, instList[tid].size());
531
532        ListIt inst_list_it = instList[tid].begin();
533        int num = 0;
534
535        while (inst_list_it != instList[tid].end()) {
536            cprintf("Instruction:%i\nPC:%#x\n[sn:%i]\n[tid:%i]\nIssued:%i\n"
537                    "Squashed:%i\n\n",
538                    num, (*inst_list_it)->readPC(),
539                    (*inst_list_it)->seqNum,
540                    (*inst_list_it)->threadNumber,
541                    (*inst_list_it)->isIssued(),
542                    (*inst_list_it)->isSquashed());
543            inst_list_it++;
544            ++num;
545        }
546    }
547
548    cprintf("Memory dependence hash size: %i\n", memDepHash.size());
549
550    cprintf("Memory dependence entries: %i\n", MemDepEntry::memdep_count);
551}
552