blob: 3e7a3650e9d0944eabc4557ea75133f4af1e5cce [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"
Matt Davis5d1cda12018-05-15 20:21:04 +000016#include "FetchStage.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000017#include "HWEventListener.h"
18#include "llvm/CodeGen/TargetSchedule.h"
19#include "llvm/Support/Debug.h"
20
21namespace mca {
22
23#define DEBUG_TYPE "llvm-mca"
24
25using namespace llvm;
26
27void Backend::addEventListener(HWEventListener *Listener) {
28 if (Listener)
29 Listeners.insert(Listener);
30}
31
Matt Davis5d1cda12018-05-15 20:21:04 +000032void Backend::run() {
Matt Davis679083e2018-05-17 19:22:29 +000033 while (Fetch->isReady() || !Dispatch->isReady())
Matt Davis5d1cda12018-05-15 20:21:04 +000034 runCycle(Cycles++);
35}
36
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000037void Backend::runCycle(unsigned Cycle) {
38 notifyCycleBegin(Cycle);
39
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000040 // Update the stages before we do any processing for this cycle.
Matt Davis5d1cda12018-05-15 20:21:04 +000041 InstRef IR;
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000042 Retire->preExecute(IR);
Matt Davisbd125322018-05-22 20:51:58 +000043 Dispatch->preExecute(IR);
Matt Davis488ac4c2018-06-14 01:20:18 +000044 Execute->preExecute(IR);
Matt Davisbd125322018-05-22 20:51:58 +000045
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000046 // Fetch instructions and dispatch them to the hardware.
Matt Davis5d1cda12018-05-15 20:21:04 +000047 while (Fetch->execute(IR)) {
Matt Davis679083e2018-05-17 19:22:29 +000048 if (!Dispatch->execute(IR))
Matt Davis21a8d322018-05-07 18:29:15 +000049 break;
Matt Davis488ac4c2018-06-14 01:20:18 +000050 Execute->execute(IR);
Matt Davis5d1cda12018-05-15 20:21:04 +000051 Fetch->postExecute(IR);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000052 }
53
54 notifyCycleEnd(Cycle);
55}
56
57void Backend::notifyCycleBegin(unsigned Cycle) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000058 LLVM_DEBUG(dbgs() << "[E] Cycle begin: " << Cycle << '\n');
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000059 for (HWEventListener *Listener : Listeners)
Andrea Di Biagio3e646442018-04-12 10:49:40 +000060 Listener->onCycleBegin();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000061}
62
Clement Courbet844f22d2018-03-13 13:11:01 +000063void Backend::notifyInstructionEvent(const HWInstructionEvent &Event) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000064 for (HWEventListener *Listener : Listeners)
Clement Courbet844f22d2018-03-13 13:11:01 +000065 Listener->onInstructionEvent(Event);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000066}
67
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000068void Backend::notifyStallEvent(const HWStallEvent &Event) {
69 for (HWEventListener *Listener : Listeners)
70 Listener->onStallEvent(Event);
71}
72
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000073void Backend::notifyResourceAvailable(const ResourceRef &RR) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000074 LLVM_DEBUG(dbgs() << "[E] Resource Available: [" << RR.first << '.'
75 << RR.second << "]\n");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000076 for (HWEventListener *Listener : Listeners)
77 Listener->onResourceAvailable(RR);
78}
79
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +000080void Backend::notifyReservedBuffers(ArrayRef<unsigned> Buffers) {
81 for (HWEventListener *Listener : Listeners)
82 Listener->onReservedBuffers(Buffers);
83}
84
85void Backend::notifyReleasedBuffers(ArrayRef<unsigned> Buffers) {
86 for (HWEventListener *Listener : Listeners)
87 Listener->onReleasedBuffers(Buffers);
88}
89
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000090void Backend::notifyCycleEnd(unsigned Cycle) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000091 LLVM_DEBUG(dbgs() << "[E] Cycle end: " << Cycle << "\n\n");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000092 for (HWEventListener *Listener : Listeners)
Andrea Di Biagio3e646442018-04-12 10:49:40 +000093 Listener->onCycleEnd();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000094}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000095} // namespace mca.