blob: 6a096a16c4e0751fe6a39a5bf7bc1932b99a343e [file] [log] [blame]
Andrew Trick99ab6c62012-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"
16#include "llvm/Target/TargetInstrInfo.h"
Andrew Trick412cd2f2012-10-10 05:43:09 +000017#include "llvm/Target/TargetMachine.h"
Andrew Trick34301ce2012-09-18 04:03:34 +000018#include "llvm/Target/TargetRegisterInfo.h"
Andrew Trick99ab6c62012-09-14 20:26:46 +000019#include "llvm/Target/TargetSubtargetInfo.h"
20#include "llvm/Support/CommandLine.h"
Andrew Trick3918cad2012-09-18 18:20:02 +000021#include "llvm/Support/raw_ostream.h"
Andrew Trick99ab6c62012-09-14 20:26:46 +000022
23using namespace llvm;
24
Andrew Trick72fd0a92012-10-04 00:24:34 +000025static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true),
Andrew Trick99ab6c62012-09-14 20:26:46 +000026 cl::desc("Use TargetSchedModel for latency lookup"));
27
Andrew Trick34301ce2012-09-18 04:03:34 +000028static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true),
29 cl::desc("Use InstrItineraryData for latency lookup"));
30
Andrew Trick42bb1062012-10-09 23:44:26 +000031bool TargetSchedModel::hasInstrSchedModel() const {
32 return EnableSchedModel && SchedModel.hasInstrSchedModel();
33}
34
35bool TargetSchedModel::hasInstrItineraries() const {
36 return EnableSchedItins && !InstrItins.isEmpty();
37}
38
Andrew Trick99ab6c62012-09-14 20:26:46 +000039void TargetSchedModel::init(const MCSchedModel &sm,
40 const TargetSubtargetInfo *sti,
41 const TargetInstrInfo *tii) {
42 SchedModel = sm;
43 STI = sti;
44 TII = tii;
45 STI->initInstrItins(InstrItins);
46}
Andrew Trick34301ce2012-09-18 04:03:34 +000047
Andrew Trick412cd2f2012-10-10 05:43:09 +000048unsigned TargetSchedModel::getNumMicroOps(MachineInstr *MI) const {
49 if (hasInstrItineraries()) {
50 int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass());
51 return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, MI);
52 }
Andrew Trick4903c152012-10-11 05:37:06 +000053 if (hasInstrSchedModel()) {
54 const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
55 if (SCDesc->isValid())
56 return SCDesc->NumMicroOps;
57 }
58 return MI->isTransient() ? 0 : 1;
Andrew Trick412cd2f2012-10-10 05:43:09 +000059}
60
Andrew Trickfdd6fa82012-10-17 17:27:10 +000061// The machine model may explicitly specify an invalid latency, which
62// effectively means infinite latency. Since users of the TargetSchedule API
63// don't know how to handle this, we convert it to a very large latency that is
64// easy to distinguish when debugging the DAG but won't induce overflow.
65static unsigned convertLatency(int Cycles) {
66 return Cycles >= 0 ? Cycles : 1000;
67}
68
Andrew Trick34301ce2012-09-18 04:03:34 +000069/// If we can determine the operand latency from the def only, without machine
70/// model or itinerary lookup, do so. Otherwise return -1.
71int TargetSchedModel::getDefLatency(const MachineInstr *DefMI,
72 bool FindMin) const {
73
74 // Return a latency based on the itinerary properties and defining instruction
75 // if possible. Some common subtargets don't require per-operand latency,
76 // especially for minimum latencies.
77 if (FindMin) {
78 // If MinLatency is invalid, then use the itinerary for MinLatency. If no
79 // itinerary exists either, then use single cycle latency.
Andrew Trick42bb1062012-10-09 23:44:26 +000080 if (SchedModel.MinLatency < 0 && !hasInstrItineraries()) {
Andrew Trick34301ce2012-09-18 04:03:34 +000081 return 1;
82 }
83 return SchedModel.MinLatency;
84 }
Andrew Trick42bb1062012-10-09 23:44:26 +000085 else if (!hasInstrSchedModel() && !hasInstrItineraries()) {
Andrew Trick34301ce2012-09-18 04:03:34 +000086 return TII->defaultDefLatency(&SchedModel, DefMI);
87 }
88 // ...operand lookup required
89 return -1;
90}
91
92/// Return the MCSchedClassDesc for this instruction. Some SchedClasses require
93/// evaluation of predicates that depend on instruction operands or flags.
94const MCSchedClassDesc *TargetSchedModel::
95resolveSchedClass(const MachineInstr *MI) const {
96
97 // Get the definition's scheduling class descriptor from this machine model.
98 unsigned SchedClass = MI->getDesc().getSchedClass();
99 const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass);
100
101#ifndef NDEBUG
102 unsigned NIter = 0;
103#endif
104 while (SCDesc->isVariant()) {
105 assert(++NIter < 6 && "Variants are nested deeper than the magic number");
106
107 SchedClass = STI->resolveSchedClass(SchedClass, MI, this);
108 SCDesc = SchedModel.getSchedClassDesc(SchedClass);
109 }
110 return SCDesc;
111}
112
113/// Find the def index of this operand. This index maps to the machine model and
114/// is independent of use operands. Def operands may be reordered with uses or
115/// merged with uses without affecting the def index (e.g. before/after
116/// regalloc). However, an instruction's def operands must never be reordered
117/// with respect to each other.
118static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) {
119 unsigned DefIdx = 0;
120 for (unsigned i = 0; i != DefOperIdx; ++i) {
121 const MachineOperand &MO = MI->getOperand(i);
122 if (MO.isReg() && MO.isDef())
123 ++DefIdx;
124 }
125 return DefIdx;
126}
127
128/// Find the use index of this operand. This is independent of the instruction's
129/// def operands.
Andrew Trick3918cad2012-09-18 18:20:02 +0000130///
131/// Note that uses are not determined by the operand's isUse property, which
132/// is simply the inverse of isDef. Here we consider any readsReg operand to be
133/// a "use". The machine model allows an operand to be both a Def and Use.
Andrew Trick34301ce2012-09-18 04:03:34 +0000134static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) {
135 unsigned UseIdx = 0;
136 for (unsigned i = 0; i != UseOperIdx; ++i) {
137 const MachineOperand &MO = MI->getOperand(i);
Andrew Trick3918cad2012-09-18 18:20:02 +0000138 if (MO.isReg() && MO.readsReg())
Andrew Trick34301ce2012-09-18 04:03:34 +0000139 ++UseIdx;
140 }
141 return UseIdx;
142}
143
144// Top-level API for clients that know the operand indices.
145unsigned TargetSchedModel::computeOperandLatency(
146 const MachineInstr *DefMI, unsigned DefOperIdx,
147 const MachineInstr *UseMI, unsigned UseOperIdx,
148 bool FindMin) const {
149
150 int DefLatency = getDefLatency(DefMI, FindMin);
151 if (DefLatency >= 0)
152 return DefLatency;
153
Andrew Trick42bb1062012-10-09 23:44:26 +0000154 if (hasInstrItineraries()) {
Andrew Trick72fd0a92012-10-04 00:24:34 +0000155 int OperLatency = 0;
156 if (UseMI) {
157 OperLatency =
158 TII->getOperandLatency(&InstrItins, DefMI, DefOperIdx, UseMI, UseOperIdx);
Andrew Trick34301ce2012-09-18 04:03:34 +0000159 }
Andrew Trick72fd0a92012-10-04 00:24:34 +0000160 else {
161 unsigned DefClass = DefMI->getDesc().getSchedClass();
162 OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx);
163 }
164 if (OperLatency >= 0)
165 return OperLatency;
166
167 // No operand latency was found.
168 unsigned InstrLatency = TII->getInstrLatency(&InstrItins, DefMI);
169
170 // Expected latency is the max of the stage latency and itinerary props.
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000171 // Rather than directly querying InstrItins stage latency, we call a TII
172 // hook to allow subtargets to specialize latency. This hook is only
173 // applicable to the InstrItins model. InstrSchedModel should model all
174 // special cases without TII hooks.
Andrew Trick72fd0a92012-10-04 00:24:34 +0000175 if (!FindMin)
176 InstrLatency = std::max(InstrLatency,
177 TII->defaultDefLatency(&SchedModel, DefMI));
178 return InstrLatency;
179 }
Andrew Trick42bb1062012-10-09 23:44:26 +0000180 assert(!FindMin && hasInstrSchedModel() &&
Andrew Trick72fd0a92012-10-04 00:24:34 +0000181 "Expected a SchedModel for this cpu");
182 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
183 unsigned DefIdx = findDefIdx(DefMI, DefOperIdx);
184 if (DefIdx < SCDesc->NumWriteLatencyEntries) {
185 // Lookup the definition's write latency in SubtargetInfo.
186 const MCWriteLatencyEntry *WLEntry =
187 STI->getWriteLatencyEntry(SCDesc, DefIdx);
188 unsigned WriteID = WLEntry->WriteResourceID;
Andrew Trickfdd6fa82012-10-17 17:27:10 +0000189 unsigned Latency = convertLatency(WLEntry->Cycles);
Andrew Trick72fd0a92012-10-04 00:24:34 +0000190 if (!UseMI)
191 return Latency;
192
193 // Lookup the use's latency adjustment in SubtargetInfo.
194 const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI);
195 if (UseDesc->NumReadAdvanceEntries == 0)
196 return Latency;
197 unsigned UseIdx = findUseIdx(UseMI, UseOperIdx);
198 return Latency - STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID);
199 }
200 // If DefIdx does not exist in the model (e.g. implicit defs), then return
201 // unit latency (defaultDefLatency may be too conservative).
Andrew Trick3918cad2012-09-18 18:20:02 +0000202#ifndef NDEBUG
Andrew Trick72fd0a92012-10-04 00:24:34 +0000203 if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit()
204 && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef()) {
205 std::string Err;
206 raw_string_ostream ss(Err);
207 ss << "DefIdx " << DefIdx << " exceeds machine model writes for "
208 << *DefMI;
209 report_fatal_error(ss.str());
210 }
Andrew Trick3918cad2012-09-18 18:20:02 +0000211#endif
Andrew Trick4903c152012-10-11 05:37:06 +0000212 return DefMI->isTransient() ? 0 : 1;
Andrew Trick34301ce2012-09-18 04:03:34 +0000213}
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000214
215unsigned TargetSchedModel::computeInstrLatency(const MachineInstr *MI) const {
Andrew Trick82d46ae2012-10-10 05:43:18 +0000216 // For the itinerary model, fall back to the old subtarget hook.
217 // Allow subtargets to compute Bundle latencies outside the machine model.
218 if (hasInstrItineraries() || MI->isBundle())
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000219 return TII->getInstrLatency(&InstrItins, MI);
Andrew Trick82d46ae2012-10-10 05:43:18 +0000220
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000221 if (hasInstrSchedModel()) {
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000222 const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
Andrew Trick4903c152012-10-11 05:37:06 +0000223 if (SCDesc->isValid()) {
224 unsigned Latency = 0;
225 for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
226 DefIdx != DefEnd; ++DefIdx) {
227 // Lookup the definition's write latency in SubtargetInfo.
228 const MCWriteLatencyEntry *WLEntry =
229 STI->getWriteLatencyEntry(SCDesc, DefIdx);
Andrew Trickfdd6fa82012-10-17 17:27:10 +0000230 Latency = std::max(Latency, convertLatency(WLEntry->Cycles));
Andrew Trick4903c152012-10-11 05:37:06 +0000231 }
232 return Latency;
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000233 }
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000234 }
235 return TII->defaultDefLatency(&SchedModel, MI);
236}
Andrew Trick412cd2f2012-10-10 05:43:09 +0000237
238unsigned TargetSchedModel::
239computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx,
240 const MachineInstr *DepMI) const {
241 // MinLatency == -1 is for in-order processors that always have unit
242 // MinLatency. MinLatency > 0 is for in-order processors with varying min
243 // latencies, but since this is not a RAW dep, we always use unit latency.
244 if (SchedModel.MinLatency != 0)
245 return 1;
246
247 // MinLatency == 0 indicates an out-of-order processor that can dispatch
248 // WAW dependencies in the same cycle.
249
250 // Treat predication as a data dependency for out-of-order cpus. In-order
251 // cpus do not need to treat predicated writes specially.
252 //
253 // TODO: The following hack exists because predication passes do not
254 // correctly append imp-use operands, and readsReg() strangely returns false
255 // for predicated defs.
256 unsigned Reg = DefMI->getOperand(DefOperIdx).getReg();
257 const MachineFunction &MF = *DefMI->getParent()->getParent();
258 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
259 if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(DepMI))
260 return computeInstrLatency(DefMI);
261
262 // If we have a per operand scheduling model, check if this def is writing
263 // an unbuffered resource. If so, it treated like an in-order cpu.
264 if (hasInstrSchedModel()) {
265 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
Andrew Trick4903c152012-10-11 05:37:06 +0000266 if (SCDesc->isValid()) {
267 for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc),
268 *PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) {
269 if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->IsBuffered)
270 return 1;
271 }
Andrew Trick412cd2f2012-10-10 05:43:09 +0000272 }
273 }
274 return 0;
275}