blob: cf9d059492b8b3f1e0fe5b7e4784c716de223659 [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 Trick34301ce2012-09-18 04:03:34 +000017#include "llvm/Target/TargetRegisterInfo.h"
Andrew Trick99ab6c62012-09-14 20:26:46 +000018#include "llvm/Target/TargetSubtargetInfo.h"
19#include "llvm/Support/CommandLine.h"
Andrew Trick3918cad2012-09-18 18:20:02 +000020#include "llvm/Support/raw_ostream.h"
Andrew Trick99ab6c62012-09-14 20:26:46 +000021
22using namespace llvm;
23
Andrew Trick72fd0a92012-10-04 00:24:34 +000024static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true),
Andrew Trick99ab6c62012-09-14 20:26:46 +000025 cl::desc("Use TargetSchedModel for latency lookup"));
26
Andrew Trick34301ce2012-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 Trick42bb1062012-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 Trick99ab6c62012-09-14 20:26:46 +000038void TargetSchedModel::init(const MCSchedModel &sm,
39 const TargetSubtargetInfo *sti,
40 const TargetInstrInfo *tii) {
41 SchedModel = sm;
42 STI = sti;
43 TII = tii;
44 STI->initInstrItins(InstrItins);
45}
Andrew Trick34301ce2012-09-18 04:03:34 +000046
47/// If we can determine the operand latency from the def only, without machine
48/// model or itinerary lookup, do so. Otherwise return -1.
49int TargetSchedModel::getDefLatency(const MachineInstr *DefMI,
50 bool FindMin) const {
51
52 // Return a latency based on the itinerary properties and defining instruction
53 // if possible. Some common subtargets don't require per-operand latency,
54 // especially for minimum latencies.
55 if (FindMin) {
56 // If MinLatency is invalid, then use the itinerary for MinLatency. If no
57 // itinerary exists either, then use single cycle latency.
Andrew Trick42bb1062012-10-09 23:44:26 +000058 if (SchedModel.MinLatency < 0 && !hasInstrItineraries()) {
Andrew Trick34301ce2012-09-18 04:03:34 +000059 return 1;
60 }
61 return SchedModel.MinLatency;
62 }
Andrew Trick42bb1062012-10-09 23:44:26 +000063 else if (!hasInstrSchedModel() && !hasInstrItineraries()) {
Andrew Trick34301ce2012-09-18 04:03:34 +000064 return TII->defaultDefLatency(&SchedModel, DefMI);
65 }
66 // ...operand lookup required
67 return -1;
68}
69
70/// Return the MCSchedClassDesc for this instruction. Some SchedClasses require
71/// evaluation of predicates that depend on instruction operands or flags.
72const MCSchedClassDesc *TargetSchedModel::
73resolveSchedClass(const MachineInstr *MI) const {
74
75 // Get the definition's scheduling class descriptor from this machine model.
76 unsigned SchedClass = MI->getDesc().getSchedClass();
77 const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass);
78
79#ifndef NDEBUG
80 unsigned NIter = 0;
81#endif
82 while (SCDesc->isVariant()) {
83 assert(++NIter < 6 && "Variants are nested deeper than the magic number");
84
85 SchedClass = STI->resolveSchedClass(SchedClass, MI, this);
86 SCDesc = SchedModel.getSchedClassDesc(SchedClass);
87 }
88 return SCDesc;
89}
90
91/// Find the def index of this operand. This index maps to the machine model and
92/// is independent of use operands. Def operands may be reordered with uses or
93/// merged with uses without affecting the def index (e.g. before/after
94/// regalloc). However, an instruction's def operands must never be reordered
95/// with respect to each other.
96static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) {
97 unsigned DefIdx = 0;
98 for (unsigned i = 0; i != DefOperIdx; ++i) {
99 const MachineOperand &MO = MI->getOperand(i);
100 if (MO.isReg() && MO.isDef())
101 ++DefIdx;
102 }
103 return DefIdx;
104}
105
106/// Find the use index of this operand. This is independent of the instruction's
107/// def operands.
Andrew Trick3918cad2012-09-18 18:20:02 +0000108///
109/// Note that uses are not determined by the operand's isUse property, which
110/// is simply the inverse of isDef. Here we consider any readsReg operand to be
111/// a "use". The machine model allows an operand to be both a Def and Use.
Andrew Trick34301ce2012-09-18 04:03:34 +0000112static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) {
113 unsigned UseIdx = 0;
114 for (unsigned i = 0; i != UseOperIdx; ++i) {
115 const MachineOperand &MO = MI->getOperand(i);
Andrew Trick3918cad2012-09-18 18:20:02 +0000116 if (MO.isReg() && MO.readsReg())
Andrew Trick34301ce2012-09-18 04:03:34 +0000117 ++UseIdx;
118 }
119 return UseIdx;
120}
121
122// Top-level API for clients that know the operand indices.
123unsigned TargetSchedModel::computeOperandLatency(
124 const MachineInstr *DefMI, unsigned DefOperIdx,
125 const MachineInstr *UseMI, unsigned UseOperIdx,
126 bool FindMin) const {
127
128 int DefLatency = getDefLatency(DefMI, FindMin);
129 if (DefLatency >= 0)
130 return DefLatency;
131
Andrew Trick42bb1062012-10-09 23:44:26 +0000132 if (hasInstrItineraries()) {
Andrew Trick72fd0a92012-10-04 00:24:34 +0000133 int OperLatency = 0;
134 if (UseMI) {
135 OperLatency =
136 TII->getOperandLatency(&InstrItins, DefMI, DefOperIdx, UseMI, UseOperIdx);
Andrew Trick34301ce2012-09-18 04:03:34 +0000137 }
Andrew Trick72fd0a92012-10-04 00:24:34 +0000138 else {
139 unsigned DefClass = DefMI->getDesc().getSchedClass();
140 OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx);
141 }
142 if (OperLatency >= 0)
143 return OperLatency;
144
145 // No operand latency was found.
146 unsigned InstrLatency = TII->getInstrLatency(&InstrItins, DefMI);
147
148 // Expected latency is the max of the stage latency and itinerary props.
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000149 // Rather than directly querying InstrItins stage latency, we call a TII
150 // hook to allow subtargets to specialize latency. This hook is only
151 // applicable to the InstrItins model. InstrSchedModel should model all
152 // special cases without TII hooks.
Andrew Trick72fd0a92012-10-04 00:24:34 +0000153 if (!FindMin)
154 InstrLatency = std::max(InstrLatency,
155 TII->defaultDefLatency(&SchedModel, DefMI));
156 return InstrLatency;
157 }
Andrew Trick42bb1062012-10-09 23:44:26 +0000158 assert(!FindMin && hasInstrSchedModel() &&
Andrew Trick72fd0a92012-10-04 00:24:34 +0000159 "Expected a SchedModel for this cpu");
160 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
161 unsigned DefIdx = findDefIdx(DefMI, DefOperIdx);
162 if (DefIdx < SCDesc->NumWriteLatencyEntries) {
163 // Lookup the definition's write latency in SubtargetInfo.
164 const MCWriteLatencyEntry *WLEntry =
165 STI->getWriteLatencyEntry(SCDesc, DefIdx);
166 unsigned WriteID = WLEntry->WriteResourceID;
167 unsigned Latency = WLEntry->Cycles;
168 if (!UseMI)
169 return Latency;
170
171 // Lookup the use's latency adjustment in SubtargetInfo.
172 const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI);
173 if (UseDesc->NumReadAdvanceEntries == 0)
174 return Latency;
175 unsigned UseIdx = findUseIdx(UseMI, UseOperIdx);
176 return Latency - STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID);
177 }
178 // If DefIdx does not exist in the model (e.g. implicit defs), then return
179 // unit latency (defaultDefLatency may be too conservative).
Andrew Trick3918cad2012-09-18 18:20:02 +0000180#ifndef NDEBUG
Andrew Trick72fd0a92012-10-04 00:24:34 +0000181 if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit()
182 && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef()) {
183 std::string Err;
184 raw_string_ostream ss(Err);
185 ss << "DefIdx " << DefIdx << " exceeds machine model writes for "
186 << *DefMI;
187 report_fatal_error(ss.str());
188 }
Andrew Trick3918cad2012-09-18 18:20:02 +0000189#endif
Andrew Trick72fd0a92012-10-04 00:24:34 +0000190 return 1;
Andrew Trick34301ce2012-09-18 04:03:34 +0000191}
Andrew Trickc0dfffa2012-10-09 23:44:32 +0000192
193unsigned TargetSchedModel::computeInstrLatency(const MachineInstr *MI) const {
194 if (hasInstrItineraries()) {
195 // For the itinerary model, fall back to the old subtarget hook.
196 return TII->getInstrLatency(&InstrItins, MI);
197 }
198 if (hasInstrSchedModel()) {
199 unsigned Latency = 0;
200 const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
201 for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
202 DefIdx != DefEnd; ++DefIdx) {
203 // Lookup the definition's write latency in SubtargetInfo.
204 const MCWriteLatencyEntry *WLEntry =
205 STI->getWriteLatencyEntry(SCDesc, DefIdx);
206 Latency = std::max(Latency, WLEntry->Cycles);
207 }
208 return Latency;
209 }
210 return TII->defaultDefLatency(&SchedModel, MI);
211}