Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 1 | //===- MCSchedule.cpp - Scheduling ------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the default scheduling model. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/MC/MCSchedule.h" |
Andrea Di Biagio | be8616f | 2018-05-31 13:30:42 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCInst.h" |
Andrea Di Biagio | b9acf13 | 2018-04-15 17:32:17 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCInstrDesc.h" |
| 16 | #include "llvm/MC/MCInstrInfo.h" |
Andrea Di Biagio | 30c1ba4 | 2018-03-13 15:22:13 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSubtargetInfo.h" |
Duncan P. N. Exon Smith | f787ed0 | 2015-07-10 22:17:49 +0000 | [diff] [blame] | 18 | #include <type_traits> |
Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | static_assert(std::is_pod<MCSchedModel>::value, |
| 23 | "We shouldn't have a static constructor here"); |
| 24 | const 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 Biagio | 823e5f9 | 2018-04-03 13:52:26 +0000 | [diff] [blame] | 37 | nullptr, |
Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 38 | nullptr}; |
Andrea Di Biagio | 30c1ba4 | 2018-03-13 15:22:13 +0000 | [diff] [blame] | 39 | |
| 40 | int 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 Biagio | 7faea7c | 2018-03-13 16:28:55 +0000 | [diff] [blame] | 55 | |
Andrea Di Biagio | b9acf13 | 2018-04-15 17:32:17 +0000 | [diff] [blame] | 56 | int 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 Biagio | 7faea7c | 2018-03-13 16:28:55 +0000 | [diff] [blame] | 66 | |
Andrea Di Biagio | be8616f | 2018-05-31 13:30:42 +0000 | [diff] [blame] | 67 | int 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 Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 87 | double |
Andrea Di Biagio | 7faea7c | 2018-03-13 16:28:55 +0000 | [diff] [blame] | 88 | MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI, |
| 89 | const MCSchedClassDesc &SCDesc) { |
| 90 | Optional<double> Throughput; |
Andrea Di Biagio | b9acf13 | 2018-04-15 17:32:17 +0000 | [diff] [blame] | 91 | 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 Biagio | 7faea7c | 2018-03-13 16:28:55 +0000 | [diff] [blame] | 100 | } |
Sanjay Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 101 | 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 Biagio | b9acf13 | 2018-04-15 17:32:17 +0000 | [diff] [blame] | 107 | } |
Andrea Di Biagio | 7faea7c | 2018-03-13 16:28:55 +0000 | [diff] [blame] | 108 | |
Sanjay Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 109 | double |
Andrea Di Biagio | be8616f | 2018-05-31 13:30:42 +0000 | [diff] [blame] | 110 | MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI, |
| 111 | const MCInstrInfo &MCII, |
| 112 | const MCInst &Inst) const { |
Andrea Di Biagio | be8616f | 2018-05-31 13:30:42 +0000 | [diff] [blame] | 113 | unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass(); |
| 114 | const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass); |
Sanjay Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 115 | |
| 116 | // If there's no valid class, assume that the instruction executes/completes |
| 117 | // at the maximum issue width. |
Andrea Di Biagio | be8616f | 2018-05-31 13:30:42 +0000 | [diff] [blame] | 118 | if (!SCDesc->isValid()) |
Sanjay Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 119 | return 1.0 / IssueWidth; |
Andrea Di Biagio | be8616f | 2018-05-31 13:30:42 +0000 | [diff] [blame] | 120 | |
| 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 Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 133 | double |
Andrea Di Biagio | b9acf13 | 2018-04-15 17:32:17 +0000 | [diff] [blame] | 134 | MCSchedModel::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 Patel | 59313be | 2018-06-05 23:34:45 +0000 | [diff] [blame] | 145 | 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 Biagio | 7faea7c | 2018-03-13 16:28:55 +0000 | [diff] [blame] | 151 | } |
Andrea Di Biagio | d768d35 | 2019-01-23 16:35:07 +0000 | [diff] [blame] | 152 | |
| 153 | unsigned |
| 154 | MCSchedModel::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 | } |