blob: a45fd342aef6c03bc509f941bbde7c192580b3ae [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- Backend.cpp --------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
11/// Implementation of class Backend which emulates an hardware OoO backend.
12///
13//===----------------------------------------------------------------------===//
14
15#include "Backend.h"
16#include "HWEventListener.h"
17#include "llvm/CodeGen/TargetSchedule.h"
18#include "llvm/Support/Debug.h"
19
20namespace mca {
21
22#define DEBUG_TYPE "llvm-mca"
23
24using namespace llvm;
25
26void Backend::addEventListener(HWEventListener *Listener) {
27 if (Listener)
28 Listeners.insert(Listener);
29}
30
31void Backend::runCycle(unsigned Cycle) {
32 notifyCycleBegin(Cycle);
33
Andrea Di Biagio4732d43ca2018-03-14 14:57:23 +000034 while (SM.hasNext()) {
35 InstRef IR = SM.peekNext();
36 std::unique_ptr<Instruction> NewIS(
37 IB->createInstruction(STI, IR.first, *IR.second));
38 const InstrDesc &Desc = NewIS->getDesc();
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000039 if (!DU->isAvailable(Desc.NumMicroOps) ||
40 !DU->canDispatch(IR.first, *NewIS))
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000041 break;
42
Andrea Di Biagio4732d43ca2018-03-14 14:57:23 +000043 Instruction *IS = NewIS.get();
44 Instructions[IR.first] = std::move(NewIS);
45 IS->setRCUTokenID(DU->dispatch(IR.first, IS, STI));
46 SM.updateNext();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000047 }
48
49 notifyCycleEnd(Cycle);
50}
51
52void Backend::notifyCycleBegin(unsigned Cycle) {
53 DEBUG(dbgs() << "[E] Cycle begin: " << Cycle << '\n');
54 for (HWEventListener *Listener : Listeners)
55 Listener->onCycleBegin(Cycle);
56
57 DU->cycleEvent(Cycle);
58 HWS->cycleEvent(Cycle);
59}
60
Clement Courbet844f22d2018-03-13 13:11:01 +000061void Backend::notifyInstructionEvent(const HWInstructionEvent &Event) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000062 for (HWEventListener *Listener : Listeners)
Clement Courbet844f22d2018-03-13 13:11:01 +000063 Listener->onInstructionEvent(Event);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000064}
65
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000066void Backend::notifyStallEvent(const HWStallEvent &Event) {
67 for (HWEventListener *Listener : Listeners)
68 Listener->onStallEvent(Event);
69}
70
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000071void Backend::notifyResourceAvailable(const ResourceRef &RR) {
72 DEBUG(dbgs() << "[E] Resource Available: [" << RR.first << '.' << RR.second
73 << "]\n");
74 for (HWEventListener *Listener : Listeners)
75 Listener->onResourceAvailable(RR);
76}
77
78void Backend::notifyCycleEnd(unsigned Cycle) {
79 DEBUG(dbgs() << "[E] Cycle end: " << Cycle << "\n\n");
80 for (HWEventListener *Listener : Listeners)
81 Listener->onCycleEnd(Cycle);
82}
83
84} // namespace mca.