blob: 83e52d3353548ca9eaeb7f7225b8bdaf0fc9cf02 [file] [log] [blame]
Andrew Trickd2a19da2012-09-14 20:26:46 +00001//===-- llvm/Target/TargetSchedule.cpp - Sched Machine Model ----*- 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//
10// This file implements a wrapper around MCSchedModel that allows the interface
11// to benefit from information currently only available in TargetInstrInfo.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/TargetSchedule.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/raw_ostream.h"
Andrew Trickd2a19da2012-09-14 20:26:46 +000018#include "llvm/Target/TargetInstrInfo.h"
Andrew Trick6e6d5972012-09-18 04:03:34 +000019#include "llvm/Target/TargetRegisterInfo.h"
Andrew Trickd2a19da2012-09-14 20:26:46 +000020#include "llvm/Target/TargetSubtargetInfo.h"
Andrew Trickd2a19da2012-09-14 20:26:46 +000021
22using namespace llvm;
23
Andrew Trick8abcf4d2012-10-04 00:24:34 +000024static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true),
Andrew Trickd2a19da2012-09-14 20:26:46 +000025 cl::desc("Use TargetSchedModel for latency lookup"));
26
Andrew Trick6e6d5972012-09-18 04:03:34 +000027static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true),
28 cl::desc("Use InstrItineraryData for latency lookup"));
29
Andrew Trickcfcf5202012-10-09 23:44:26 +000030bool TargetSchedModel::hasInstrSchedModel() const {
31 return EnableSchedModel && SchedModel.hasInstrSchedModel();
32}
33
34bool TargetSchedModel::hasInstrItineraries() const {
35 return EnableSchedItins && !InstrItins.isEmpty();
36}
37
Andrew Tricke96390e2012-11-06 07:10:38 +000038static unsigned gcd(unsigned Dividend, unsigned Divisor) {
39 // Dividend and Divisor will be naturally swapped as needed.
40 while(Divisor) {
41 unsigned Rem = Dividend % Divisor;
42 Dividend = Divisor;
43 Divisor = Rem;
44 };
45 return Dividend;
46}
47static unsigned lcm(unsigned A, unsigned B) {
48 unsigned LCM = (uint64_t(A) * B) / gcd(A, B);
49 assert((LCM >= A && LCM >= B) && "LCM overflow");
50 return LCM;
51}
52
Andrew Trickd2a19da2012-09-14 20:26:46 +000053void TargetSchedModel::init(const MCSchedModel &sm,
54 const TargetSubtargetInfo *sti,
55 const TargetInstrInfo *tii) {
56 SchedModel = sm;
57 STI = sti;
58 TII = tii;
59 STI->initInstrItins(InstrItins);
Andrew Tricke96390e2012-11-06 07:10:38 +000060
61 unsigned NumRes = SchedModel.getNumProcResourceKinds();
62 ResourceFactors.resize(NumRes);
63 ResourceLCM = SchedModel.IssueWidth;
64 for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
65 unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
66 if (NumUnits > 0)
67 ResourceLCM = lcm(ResourceLCM, NumUnits);
68 }
69 MicroOpFactor = ResourceLCM / SchedModel.IssueWidth;
70 for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
71 unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
72 ResourceFactors[Idx] = NumUnits ? (ResourceLCM / NumUnits) : 0;
73 }
Andrew Trickd2a19da2012-09-14 20:26:46 +000074}
Andrew Trick6e6d5972012-09-18 04:03:34 +000075
Andrew Tricke96390e2012-11-06 07:10:38 +000076unsigned TargetSchedModel::getNumMicroOps(const MachineInstr *MI,
77 const MCSchedClassDesc *SC) const {
Andrew Trickdd79f0f2012-10-10 05:43:09 +000078 if (hasInstrItineraries()) {
79 int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000080 return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, *MI);
Andrew Trickdd79f0f2012-10-10 05:43:09 +000081 }
Andrew Trick5f35afb2012-10-11 05:37:06 +000082 if (hasInstrSchedModel()) {
Andrew Tricke96390e2012-11-06 07:10:38 +000083 if (!SC)
84 SC = resolveSchedClass(MI);
85 if (SC->isValid())
86 return SC->NumMicroOps;
Andrew Trick5f35afb2012-10-11 05:37:06 +000087 }
88 return MI->isTransient() ? 0 : 1;
Andrew Trickdd79f0f2012-10-10 05:43:09 +000089}
90
Andrew Trick0b1d8d02012-10-17 17:27:10 +000091// The machine model may explicitly specify an invalid latency, which
92// effectively means infinite latency. Since users of the TargetSchedule API
93// don't know how to handle this, we convert it to a very large latency that is
94// easy to distinguish when debugging the DAG but won't induce overflow.
Andrew Trickde2109e2013-06-15 04:49:57 +000095static unsigned capLatency(int Cycles) {
Andrew Trick0b1d8d02012-10-17 17:27:10 +000096 return Cycles >= 0 ? Cycles : 1000;
97}
98
Andrew Trick6e6d5972012-09-18 04:03:34 +000099/// Return the MCSchedClassDesc for this instruction. Some SchedClasses require
100/// evaluation of predicates that depend on instruction operands or flags.
101const MCSchedClassDesc *TargetSchedModel::
102resolveSchedClass(const MachineInstr *MI) const {
103
104 // Get the definition's scheduling class descriptor from this machine model.
105 unsigned SchedClass = MI->getDesc().getSchedClass();
106 const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass);
Andrew Trickbe2bccb2013-04-13 06:07:45 +0000107 if (!SCDesc->isValid())
108 return SCDesc;
Andrew Trick6e6d5972012-09-18 04:03:34 +0000109
110#ifndef NDEBUG
111 unsigned NIter = 0;
112#endif
113 while (SCDesc->isVariant()) {
114 assert(++NIter < 6 && "Variants are nested deeper than the magic number");
115
116 SchedClass = STI->resolveSchedClass(SchedClass, MI, this);
117 SCDesc = SchedModel.getSchedClassDesc(SchedClass);
118 }
119 return SCDesc;
120}
121
122/// Find the def index of this operand. This index maps to the machine model and
123/// is independent of use operands. Def operands may be reordered with uses or
124/// merged with uses without affecting the def index (e.g. before/after
125/// regalloc). However, an instruction's def operands must never be reordered
126/// with respect to each other.
127static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) {
128 unsigned DefIdx = 0;
129 for (unsigned i = 0; i != DefOperIdx; ++i) {
130 const MachineOperand &MO = MI->getOperand(i);
131 if (MO.isReg() && MO.isDef())
132 ++DefIdx;
133 }
134 return DefIdx;
135}
136
137/// Find the use index of this operand. This is independent of the instruction's
138/// def operands.
Andrew Trickf2b70d92012-09-18 18:20:02 +0000139///
140/// Note that uses are not determined by the operand's isUse property, which
141/// is simply the inverse of isDef. Here we consider any readsReg operand to be
142/// a "use". The machine model allows an operand to be both a Def and Use.
Andrew Trick6e6d5972012-09-18 04:03:34 +0000143static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) {
144 unsigned UseIdx = 0;
145 for (unsigned i = 0; i != UseOperIdx; ++i) {
146 const MachineOperand &MO = MI->getOperand(i);
Matthias Braun3a133152016-08-24 02:32:29 +0000147 if (MO.isReg() && MO.readsReg() && !MO.isDef())
Andrew Trick6e6d5972012-09-18 04:03:34 +0000148 ++UseIdx;
149 }
150 return UseIdx;
151}
152
153// Top-level API for clients that know the operand indices.
154unsigned TargetSchedModel::computeOperandLatency(
155 const MachineInstr *DefMI, unsigned DefOperIdx,
Andrew Trickde2109e2013-06-15 04:49:57 +0000156 const MachineInstr *UseMI, unsigned UseOperIdx) const {
Andrew Trick6e6d5972012-09-18 04:03:34 +0000157
Andrew Trickde2109e2013-06-15 04:49:57 +0000158 if (!hasInstrSchedModel() && !hasInstrItineraries())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000159 return TII->defaultDefLatency(SchedModel, *DefMI);
Andrew Trick6e6d5972012-09-18 04:03:34 +0000160
Andrew Trickcfcf5202012-10-09 23:44:26 +0000161 if (hasInstrItineraries()) {
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000162 int OperLatency = 0;
163 if (UseMI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000164 OperLatency = TII->getOperandLatency(&InstrItins, *DefMI, DefOperIdx,
165 *UseMI, UseOperIdx);
Andrew Trick6e6d5972012-09-18 04:03:34 +0000166 }
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000167 else {
168 unsigned DefClass = DefMI->getDesc().getSchedClass();
169 OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx);
170 }
171 if (OperLatency >= 0)
172 return OperLatency;
173
174 // No operand latency was found.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000175 unsigned InstrLatency = TII->getInstrLatency(&InstrItins, *DefMI);
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000176
177 // Expected latency is the max of the stage latency and itinerary props.
Andrew Trick780fae82012-10-09 23:44:32 +0000178 // Rather than directly querying InstrItins stage latency, we call a TII
179 // hook to allow subtargets to specialize latency. This hook is only
180 // applicable to the InstrItins model. InstrSchedModel should model all
181 // special cases without TII hooks.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000182 InstrLatency =
183 std::max(InstrLatency, TII->defaultDefLatency(SchedModel, *DefMI));
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000184 return InstrLatency;
185 }
Andrew Trickde2109e2013-06-15 04:49:57 +0000186 // hasInstrSchedModel()
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000187 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
188 unsigned DefIdx = findDefIdx(DefMI, DefOperIdx);
189 if (DefIdx < SCDesc->NumWriteLatencyEntries) {
190 // Lookup the definition's write latency in SubtargetInfo.
191 const MCWriteLatencyEntry *WLEntry =
192 STI->getWriteLatencyEntry(SCDesc, DefIdx);
193 unsigned WriteID = WLEntry->WriteResourceID;
Andrew Trickde2109e2013-06-15 04:49:57 +0000194 unsigned Latency = capLatency(WLEntry->Cycles);
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000195 if (!UseMI)
196 return Latency;
197
198 // Lookup the use's latency adjustment in SubtargetInfo.
199 const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI);
200 if (UseDesc->NumReadAdvanceEntries == 0)
201 return Latency;
202 unsigned UseIdx = findUseIdx(UseMI, UseOperIdx);
Andrew Trick5d4861862013-06-17 21:45:18 +0000203 int Advance = STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID);
204 if (Advance > 0 && (unsigned)Advance > Latency) // unsigned wrap
205 return 0;
206 return Latency - Advance;
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000207 }
208 // If DefIdx does not exist in the model (e.g. implicit defs), then return
209 // unit latency (defaultDefLatency may be too conservative).
Andrew Trickf2b70d92012-09-18 18:20:02 +0000210#ifndef NDEBUG
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000211 if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit()
Andrew Trickb6854d82013-09-25 18:14:12 +0000212 && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef()
213 && SchedModel.isComplete()) {
Matthias Braun244a6772015-07-17 17:50:11 +0000214 errs() << "DefIdx " << DefIdx << " exceeds machine model writes for "
MinSeong Kim4a9a4e12016-01-05 14:50:15 +0000215 << *DefMI << " (Try with MCSchedModel.CompleteModel set to false)";
Matthias Braun244a6772015-07-17 17:50:11 +0000216 llvm_unreachable("incomplete machine model");
Andrew Trick8abcf4d2012-10-04 00:24:34 +0000217 }
Andrew Trickf2b70d92012-09-18 18:20:02 +0000218#endif
Andrew Trick60570172013-03-16 18:58:57 +0000219 // FIXME: Automatically giving all implicit defs defaultDefLatency is
220 // undesirable. We should only do it for defs that are known to the MC
221 // desc like flags. Truly implicit defs should get 1 cycle latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000222 return DefMI->isTransient() ? 0 : TII->defaultDefLatency(SchedModel, *DefMI);
Andrew Trick6e6d5972012-09-18 04:03:34 +0000223}
Andrew Trick780fae82012-10-09 23:44:32 +0000224
Matthias Braun42e1e662015-05-14 18:01:13 +0000225unsigned
226TargetSchedModel::computeInstrLatency(const MCSchedClassDesc &SCDesc) const {
227 unsigned Latency = 0;
228 for (unsigned DefIdx = 0, DefEnd = SCDesc.NumWriteLatencyEntries;
229 DefIdx != DefEnd; ++DefIdx) {
230 // Lookup the definition's write latency in SubtargetInfo.
231 const MCWriteLatencyEntry *WLEntry =
232 STI->getWriteLatencyEntry(&SCDesc, DefIdx);
233 Latency = std::max(Latency, capLatency(WLEntry->Cycles));
234 }
235 return Latency;
236}
237
Gerolf Hoflehner5e1207e2014-08-03 21:35:39 +0000238unsigned TargetSchedModel::computeInstrLatency(unsigned Opcode) const {
239 assert(hasInstrSchedModel() && "Only call this function with a SchedModel");
240
241 unsigned SCIdx = TII->get(Opcode).getSchedClass();
242 const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SCIdx);
Gerolf Hoflehner5e1207e2014-08-03 21:35:39 +0000243
Matthias Braun42e1e662015-05-14 18:01:13 +0000244 if (SCDesc->isValid() && !SCDesc->isVariant())
245 return computeInstrLatency(*SCDesc);
Gerolf Hoflehner5e1207e2014-08-03 21:35:39 +0000246
Matthias Braun7a247f72015-05-14 18:33:29 +0000247 llvm_unreachable("No MI sched latency");
Gerolf Hoflehner5e1207e2014-08-03 21:35:39 +0000248}
249
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000250unsigned
251TargetSchedModel::computeInstrLatency(const MachineInstr *MI,
252 bool UseDefaultDefLatency) const {
Andrew Trickc334bd42012-10-10 05:43:18 +0000253 // For the itinerary model, fall back to the old subtarget hook.
254 // Allow subtargets to compute Bundle latencies outside the machine model.
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000255 if (hasInstrItineraries() || MI->isBundle() ||
256 (!hasInstrSchedModel() && !UseDefaultDefLatency))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000257 return TII->getInstrLatency(&InstrItins, *MI);
Andrew Trickc334bd42012-10-10 05:43:18 +0000258
Andrew Trick780fae82012-10-09 23:44:32 +0000259 if (hasInstrSchedModel()) {
Andrew Trick780fae82012-10-09 23:44:32 +0000260 const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
Matthias Braun42e1e662015-05-14 18:01:13 +0000261 if (SCDesc->isValid())
262 return computeInstrLatency(*SCDesc);
Andrew Trick780fae82012-10-09 23:44:32 +0000263 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000264 return TII->defaultDefLatency(SchedModel, *MI);
Andrew Trick780fae82012-10-09 23:44:32 +0000265}
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000266
267unsigned TargetSchedModel::
268computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx,
269 const MachineInstr *DepMI) const {
Junmo Park11811922016-06-21 08:09:58 +0000270 if (!SchedModel.isOutOfOrder())
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000271 return 1;
272
Junmo Park11811922016-06-21 08:09:58 +0000273 // Out-of-order processor can dispatch WAW dependencies in the same cycle.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000274
275 // Treat predication as a data dependency for out-of-order cpus. In-order
276 // cpus do not need to treat predicated writes specially.
277 //
278 // TODO: The following hack exists because predication passes do not
279 // correctly append imp-use operands, and readsReg() strangely returns false
280 // for predicated defs.
281 unsigned Reg = DefMI->getOperand(DefOperIdx).getReg();
282 const MachineFunction &MF = *DefMI->getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000283 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000284 if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(*DepMI))
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000285 return computeInstrLatency(DefMI);
286
287 // If we have a per operand scheduling model, check if this def is writing
288 // an unbuffered resource. If so, it treated like an in-order cpu.
289 if (hasInstrSchedModel()) {
290 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
Andrew Trick5f35afb2012-10-11 05:37:06 +0000291 if (SCDesc->isValid()) {
292 for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc),
293 *PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) {
Andrew Trickde2109e2013-06-15 04:49:57 +0000294 if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->BufferSize)
Andrew Trick5f35afb2012-10-11 05:37:06 +0000295 return 1;
296 }
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000297 }
298 }
299 return 0;
300}