blob: 1fc5ec5e975f8424f384deb9dc8658230ab2ea85 [file] [log] [blame]
Duncan P. N. Exon Smithf862f872015-07-10 22:13:43 +00001//===- MCSchedule.cpp - Scheduling ------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Duncan P. N. Exon Smithf862f872015-07-10 22:13:43 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the default scheduling model.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/MC/MCSchedule.h"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +000014#include "llvm/MC/MCInst.h"
Andrea Di Biagiob9acf132018-04-15 17:32:17 +000015#include "llvm/MC/MCInstrDesc.h"
16#include "llvm/MC/MCInstrInfo.h"
Andrea Di Biagio30c1ba42018-03-13 15:22:13 +000017#include "llvm/MC/MCSubtargetInfo.h"
Duncan P. N. Exon Smithf787ed02015-07-10 22:17:49 +000018#include <type_traits>
Duncan P. N. Exon Smithf862f872015-07-10 22:13:43 +000019
20using namespace llvm;
21
22static_assert(std::is_pod<MCSchedModel>::value,
23 "We shouldn't have a static constructor here");
24const MCSchedModel MCSchedModel::Default = {DefaultIssueWidth,
25 DefaultMicroOpBufferSize,
26 DefaultLoopMicroOpBufferSize,
27 DefaultLoadLatency,
28 DefaultHighLatency,
29 DefaultMispredictPenalty,
30 false,
31 true,
32 0,
33 nullptr,
34 nullptr,
35 0,
36 0,
Andrea Di Biagio823e5f92018-04-03 13:52:26 +000037 nullptr,
Duncan P. N. Exon Smithf862f872015-07-10 22:13:43 +000038 nullptr};
Andrea Di Biagio30c1ba42018-03-13 15:22:13 +000039
40int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
41 const MCSchedClassDesc &SCDesc) {
42 int Latency = 0;
43 for (unsigned DefIdx = 0, DefEnd = SCDesc.NumWriteLatencyEntries;
44 DefIdx != DefEnd; ++DefIdx) {
45 // Lookup the definition's write latency in SubtargetInfo.
46 const MCWriteLatencyEntry *WLEntry =
47 STI.getWriteLatencyEntry(&SCDesc, DefIdx);
48 // Early exit if we found an invalid latency.
49 if (WLEntry->Cycles < 0)
50 return WLEntry->Cycles;
51 Latency = std::max(Latency, static_cast<int>(WLEntry->Cycles));
52 }
53 return Latency;
54}
Andrea Di Biagio7faea7c2018-03-13 16:28:55 +000055
Andrea Di Biagiob9acf132018-04-15 17:32:17 +000056int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
57 unsigned SchedClass) const {
58 const MCSchedClassDesc &SCDesc = *getSchedClassDesc(SchedClass);
59 if (!SCDesc.isValid())
60 return 0;
61 if (!SCDesc.isVariant())
62 return MCSchedModel::computeInstrLatency(STI, SCDesc);
63
64 llvm_unreachable("unsupported variant scheduling class");
65}
Andrea Di Biagio7faea7c2018-03-13 16:28:55 +000066
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +000067int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
68 const MCInstrInfo &MCII,
69 const MCInst &Inst) const {
70 unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
71 const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
72 if (!SCDesc->isValid())
73 return 0;
74
75 unsigned CPUID = getProcessorID();
76 while (SCDesc->isVariant()) {
77 SchedClass = STI.resolveVariantSchedClass(SchedClass, &Inst, CPUID);
78 SCDesc = getSchedClassDesc(SchedClass);
79 }
80
81 if (SchedClass)
82 return MCSchedModel::computeInstrLatency(STI, *SCDesc);
83
84 llvm_unreachable("unsupported variant scheduling class");
85}
86
Sanjay Patel59313be2018-06-05 23:34:45 +000087double
Andrea Di Biagio7faea7c2018-03-13 16:28:55 +000088MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI,
89 const MCSchedClassDesc &SCDesc) {
90 Optional<double> Throughput;
Andrea Di Biagiob9acf132018-04-15 17:32:17 +000091 const MCSchedModel &SM = STI.getSchedModel();
92 const MCWriteProcResEntry *I = STI.getWriteProcResBegin(&SCDesc);
93 const MCWriteProcResEntry *E = STI.getWriteProcResEnd(&SCDesc);
94 for (; I != E; ++I) {
95 if (!I->Cycles)
96 continue;
97 unsigned NumUnits = SM.getProcResource(I->ProcResourceIdx)->NumUnits;
98 double Temp = NumUnits * 1.0 / I->Cycles;
99 Throughput = Throughput ? std::min(Throughput.getValue(), Temp) : Temp;
Andrea Di Biagio7faea7c2018-03-13 16:28:55 +0000100 }
Sanjay Patel59313be2018-06-05 23:34:45 +0000101 if (Throughput.hasValue())
102 return 1.0 / Throughput.getValue();
103
104 // If no throughput value was calculated, assume that we can execute at the
105 // maximum issue width scaled by number of micro-ops for the schedule class.
106 return ((double)SCDesc.NumMicroOps) / SM.IssueWidth;
Andrea Di Biagiob9acf132018-04-15 17:32:17 +0000107}
Andrea Di Biagio7faea7c2018-03-13 16:28:55 +0000108
Sanjay Patel59313be2018-06-05 23:34:45 +0000109double
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +0000110MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI,
111 const MCInstrInfo &MCII,
112 const MCInst &Inst) const {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +0000113 unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
114 const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
Sanjay Patel59313be2018-06-05 23:34:45 +0000115
116 // If there's no valid class, assume that the instruction executes/completes
117 // at the maximum issue width.
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +0000118 if (!SCDesc->isValid())
Sanjay Patel59313be2018-06-05 23:34:45 +0000119 return 1.0 / IssueWidth;
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +0000120
121 unsigned CPUID = getProcessorID();
122 while (SCDesc->isVariant()) {
123 SchedClass = STI.resolveVariantSchedClass(SchedClass, &Inst, CPUID);
124 SCDesc = getSchedClassDesc(SchedClass);
125 }
126
127 if (SchedClass)
128 return MCSchedModel::getReciprocalThroughput(STI, *SCDesc);
129
130 llvm_unreachable("unsupported variant scheduling class");
131}
132
Sanjay Patel59313be2018-06-05 23:34:45 +0000133double
Andrea Di Biagiob9acf132018-04-15 17:32:17 +0000134MCSchedModel::getReciprocalThroughput(unsigned SchedClass,
135 const InstrItineraryData &IID) {
136 Optional<double> Throughput;
137 const InstrStage *I = IID.beginStage(SchedClass);
138 const InstrStage *E = IID.endStage(SchedClass);
139 for (; I != E; ++I) {
140 if (!I->getCycles())
141 continue;
142 double Temp = countPopulation(I->getUnits()) * 1.0 / I->getCycles();
143 Throughput = Throughput ? std::min(Throughput.getValue(), Temp) : Temp;
144 }
Sanjay Patel59313be2018-06-05 23:34:45 +0000145 if (Throughput.hasValue())
146 return 1.0 / Throughput.getValue();
147
148 // If there are no execution resources specified for this class, then assume
149 // that it can execute at the maximum default issue width.
150 return 1.0 / DefaultIssueWidth;
Andrea Di Biagio7faea7c2018-03-13 16:28:55 +0000151}
Andrea Di Biagiod768d352019-01-23 16:35:07 +0000152
153unsigned
154MCSchedModel::getForwardingDelayCycles(ArrayRef<MCReadAdvanceEntry> Entries,
155 unsigned WriteResourceID) {
156 if (Entries.empty())
157 return 0;
158
159 int DelayCycles = 0;
160 for (const MCReadAdvanceEntry &E : Entries) {
161 if (E.WriteResourceID != WriteResourceID)
162 continue;
163 DelayCycles = std::min(DelayCycles, E.Cycles);
164 }
165
166 return std::abs(DelayCycles);
167}