Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 1 | //===-- 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 Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetRegisterInfo.h" |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame^] | 24 | static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true), |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 25 | cl::desc("Use TargetSchedModel for latency lookup")); |
| 26 | |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 27 | static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true), |
| 28 | cl::desc("Use InstrItineraryData for latency lookup")); |
| 29 | |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 30 | void TargetSchedModel::init(const MCSchedModel &sm, |
| 31 | const TargetSubtargetInfo *sti, |
| 32 | const TargetInstrInfo *tii) { |
| 33 | SchedModel = sm; |
| 34 | STI = sti; |
| 35 | TII = tii; |
| 36 | STI->initInstrItins(InstrItins); |
| 37 | } |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 38 | |
| 39 | /// If we can determine the operand latency from the def only, without machine |
| 40 | /// model or itinerary lookup, do so. Otherwise return -1. |
| 41 | int TargetSchedModel::getDefLatency(const MachineInstr *DefMI, |
| 42 | bool FindMin) const { |
| 43 | |
| 44 | // Return a latency based on the itinerary properties and defining instruction |
| 45 | // if possible. Some common subtargets don't require per-operand latency, |
| 46 | // especially for minimum latencies. |
| 47 | if (FindMin) { |
| 48 | // If MinLatency is invalid, then use the itinerary for MinLatency. If no |
| 49 | // itinerary exists either, then use single cycle latency. |
| 50 | if (SchedModel.MinLatency < 0 |
| 51 | && !(EnableSchedItins && hasInstrItineraries())) { |
| 52 | return 1; |
| 53 | } |
| 54 | return SchedModel.MinLatency; |
| 55 | } |
| 56 | else if (!(EnableSchedModel && hasInstrSchedModel()) |
| 57 | && !(EnableSchedItins && hasInstrItineraries())) { |
| 58 | return TII->defaultDefLatency(&SchedModel, DefMI); |
| 59 | } |
| 60 | // ...operand lookup required |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | /// Return the MCSchedClassDesc for this instruction. Some SchedClasses require |
| 65 | /// evaluation of predicates that depend on instruction operands or flags. |
| 66 | const MCSchedClassDesc *TargetSchedModel:: |
| 67 | resolveSchedClass(const MachineInstr *MI) const { |
| 68 | |
| 69 | // Get the definition's scheduling class descriptor from this machine model. |
| 70 | unsigned SchedClass = MI->getDesc().getSchedClass(); |
| 71 | const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
| 72 | |
| 73 | #ifndef NDEBUG |
| 74 | unsigned NIter = 0; |
| 75 | #endif |
| 76 | while (SCDesc->isVariant()) { |
| 77 | assert(++NIter < 6 && "Variants are nested deeper than the magic number"); |
| 78 | |
| 79 | SchedClass = STI->resolveSchedClass(SchedClass, MI, this); |
| 80 | SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
| 81 | } |
| 82 | return SCDesc; |
| 83 | } |
| 84 | |
| 85 | /// Find the def index of this operand. This index maps to the machine model and |
| 86 | /// is independent of use operands. Def operands may be reordered with uses or |
| 87 | /// merged with uses without affecting the def index (e.g. before/after |
| 88 | /// regalloc). However, an instruction's def operands must never be reordered |
| 89 | /// with respect to each other. |
| 90 | static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) { |
| 91 | unsigned DefIdx = 0; |
| 92 | for (unsigned i = 0; i != DefOperIdx; ++i) { |
| 93 | const MachineOperand &MO = MI->getOperand(i); |
| 94 | if (MO.isReg() && MO.isDef()) |
| 95 | ++DefIdx; |
| 96 | } |
| 97 | return DefIdx; |
| 98 | } |
| 99 | |
| 100 | /// Find the use index of this operand. This is independent of the instruction's |
| 101 | /// def operands. |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 102 | /// |
| 103 | /// Note that uses are not determined by the operand's isUse property, which |
| 104 | /// is simply the inverse of isDef. Here we consider any readsReg operand to be |
| 105 | /// a "use". The machine model allows an operand to be both a Def and Use. |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 106 | static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) { |
| 107 | unsigned UseIdx = 0; |
| 108 | for (unsigned i = 0; i != UseOperIdx; ++i) { |
| 109 | const MachineOperand &MO = MI->getOperand(i); |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 110 | if (MO.isReg() && MO.readsReg()) |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 111 | ++UseIdx; |
| 112 | } |
| 113 | return UseIdx; |
| 114 | } |
| 115 | |
| 116 | // Top-level API for clients that know the operand indices. |
| 117 | unsigned TargetSchedModel::computeOperandLatency( |
| 118 | const MachineInstr *DefMI, unsigned DefOperIdx, |
| 119 | const MachineInstr *UseMI, unsigned UseOperIdx, |
| 120 | bool FindMin) const { |
| 121 | |
| 122 | int DefLatency = getDefLatency(DefMI, FindMin); |
| 123 | if (DefLatency >= 0) |
| 124 | return DefLatency; |
| 125 | |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame^] | 126 | if (EnableSchedItins && hasInstrItineraries()) { |
| 127 | int OperLatency = 0; |
| 128 | if (UseMI) { |
| 129 | OperLatency = |
| 130 | TII->getOperandLatency(&InstrItins, DefMI, DefOperIdx, UseMI, UseOperIdx); |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 131 | } |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame^] | 132 | else { |
| 133 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 134 | OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx); |
| 135 | } |
| 136 | if (OperLatency >= 0) |
| 137 | return OperLatency; |
| 138 | |
| 139 | // No operand latency was found. |
| 140 | unsigned InstrLatency = TII->getInstrLatency(&InstrItins, DefMI); |
| 141 | |
| 142 | // Expected latency is the max of the stage latency and itinerary props. |
| 143 | if (!FindMin) |
| 144 | InstrLatency = std::max(InstrLatency, |
| 145 | TII->defaultDefLatency(&SchedModel, DefMI)); |
| 146 | return InstrLatency; |
| 147 | } |
| 148 | assert(!FindMin && EnableSchedModel && hasInstrSchedModel() && |
| 149 | "Expected a SchedModel for this cpu"); |
| 150 | const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); |
| 151 | unsigned DefIdx = findDefIdx(DefMI, DefOperIdx); |
| 152 | if (DefIdx < SCDesc->NumWriteLatencyEntries) { |
| 153 | // Lookup the definition's write latency in SubtargetInfo. |
| 154 | const MCWriteLatencyEntry *WLEntry = |
| 155 | STI->getWriteLatencyEntry(SCDesc, DefIdx); |
| 156 | unsigned WriteID = WLEntry->WriteResourceID; |
| 157 | unsigned Latency = WLEntry->Cycles; |
| 158 | if (!UseMI) |
| 159 | return Latency; |
| 160 | |
| 161 | // Lookup the use's latency adjustment in SubtargetInfo. |
| 162 | const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI); |
| 163 | if (UseDesc->NumReadAdvanceEntries == 0) |
| 164 | return Latency; |
| 165 | unsigned UseIdx = findUseIdx(UseMI, UseOperIdx); |
| 166 | return Latency - STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID); |
| 167 | } |
| 168 | // If DefIdx does not exist in the model (e.g. implicit defs), then return |
| 169 | // unit latency (defaultDefLatency may be too conservative). |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 170 | #ifndef NDEBUG |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame^] | 171 | if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit() |
| 172 | && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef()) { |
| 173 | std::string Err; |
| 174 | raw_string_ostream ss(Err); |
| 175 | ss << "DefIdx " << DefIdx << " exceeds machine model writes for " |
| 176 | << *DefMI; |
| 177 | report_fatal_error(ss.str()); |
| 178 | } |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 179 | #endif |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame^] | 180 | return 1; |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 181 | } |