xref: /freebsd-src/contrib/llvm-project/llvm/lib/MC/MCSchedule.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- MCSchedule.cpp - Scheduling ------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the default scheduling model.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/MC/MCSchedule.h"
140b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
18bdd1243dSDimitry Andric #include <optional>
190b57cec5SDimitry Andric #include <type_traits>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
23*5f757f3fSDimitry Andric static_assert(std::is_trivial_v<MCSchedModel>,
24*5f757f3fSDimitry Andric               "MCSchedModel is required to be a trivial type");
250b57cec5SDimitry Andric const MCSchedModel MCSchedModel::Default = {DefaultIssueWidth,
260b57cec5SDimitry Andric                                             DefaultMicroOpBufferSize,
270b57cec5SDimitry Andric                                             DefaultLoopMicroOpBufferSize,
280b57cec5SDimitry Andric                                             DefaultLoadLatency,
290b57cec5SDimitry Andric                                             DefaultHighLatency,
300b57cec5SDimitry Andric                                             DefaultMispredictPenalty,
310b57cec5SDimitry Andric                                             false,
320b57cec5SDimitry Andric                                             true,
33*5f757f3fSDimitry Andric                                             /*EnableIntervals=*/false,
340b57cec5SDimitry Andric                                             0,
350b57cec5SDimitry Andric                                             nullptr,
360b57cec5SDimitry Andric                                             nullptr,
370b57cec5SDimitry Andric                                             0,
380b57cec5SDimitry Andric                                             0,
390b57cec5SDimitry Andric                                             nullptr,
400b57cec5SDimitry Andric                                             nullptr};
410b57cec5SDimitry Andric 
computeInstrLatency(const MCSubtargetInfo & STI,const MCSchedClassDesc & SCDesc)420b57cec5SDimitry Andric int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
430b57cec5SDimitry Andric                                       const MCSchedClassDesc &SCDesc) {
440b57cec5SDimitry Andric   int Latency = 0;
450b57cec5SDimitry Andric   for (unsigned DefIdx = 0, DefEnd = SCDesc.NumWriteLatencyEntries;
460b57cec5SDimitry Andric        DefIdx != DefEnd; ++DefIdx) {
470b57cec5SDimitry Andric     // Lookup the definition's write latency in SubtargetInfo.
480b57cec5SDimitry Andric     const MCWriteLatencyEntry *WLEntry =
490b57cec5SDimitry Andric         STI.getWriteLatencyEntry(&SCDesc, DefIdx);
500b57cec5SDimitry Andric     // Early exit if we found an invalid latency.
510b57cec5SDimitry Andric     if (WLEntry->Cycles < 0)
520b57cec5SDimitry Andric       return WLEntry->Cycles;
530b57cec5SDimitry Andric     Latency = std::max(Latency, static_cast<int>(WLEntry->Cycles));
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric   return Latency;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
computeInstrLatency(const MCSubtargetInfo & STI,unsigned SchedClass) const580b57cec5SDimitry Andric int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
590b57cec5SDimitry Andric                                       unsigned SchedClass) const {
600b57cec5SDimitry Andric   const MCSchedClassDesc &SCDesc = *getSchedClassDesc(SchedClass);
610b57cec5SDimitry Andric   if (!SCDesc.isValid())
620b57cec5SDimitry Andric     return 0;
630b57cec5SDimitry Andric   if (!SCDesc.isVariant())
640b57cec5SDimitry Andric     return MCSchedModel::computeInstrLatency(STI, SCDesc);
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   llvm_unreachable("unsupported variant scheduling class");
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
computeInstrLatency(const MCSubtargetInfo & STI,const MCInstrInfo & MCII,const MCInst & Inst) const690b57cec5SDimitry Andric int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
700b57cec5SDimitry Andric                                       const MCInstrInfo &MCII,
710b57cec5SDimitry Andric                                       const MCInst &Inst) const {
720b57cec5SDimitry Andric   unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
730b57cec5SDimitry Andric   const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
740b57cec5SDimitry Andric   if (!SCDesc->isValid())
750b57cec5SDimitry Andric     return 0;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   unsigned CPUID = getProcessorID();
780b57cec5SDimitry Andric   while (SCDesc->isVariant()) {
79e8d8bef9SDimitry Andric     SchedClass = STI.resolveVariantSchedClass(SchedClass, &Inst, &MCII, CPUID);
800b57cec5SDimitry Andric     SCDesc = getSchedClassDesc(SchedClass);
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   if (SchedClass)
840b57cec5SDimitry Andric     return MCSchedModel::computeInstrLatency(STI, *SCDesc);
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   llvm_unreachable("unsupported variant scheduling class");
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric double
getReciprocalThroughput(const MCSubtargetInfo & STI,const MCSchedClassDesc & SCDesc)900b57cec5SDimitry Andric MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI,
910b57cec5SDimitry Andric                                       const MCSchedClassDesc &SCDesc) {
92bdd1243dSDimitry Andric   std::optional<double> Throughput;
930b57cec5SDimitry Andric   const MCSchedModel &SM = STI.getSchedModel();
940b57cec5SDimitry Andric   const MCWriteProcResEntry *I = STI.getWriteProcResBegin(&SCDesc);
950b57cec5SDimitry Andric   const MCWriteProcResEntry *E = STI.getWriteProcResEnd(&SCDesc);
960b57cec5SDimitry Andric   for (; I != E; ++I) {
97*5f757f3fSDimitry Andric     if (!I->ReleaseAtCycle)
980b57cec5SDimitry Andric       continue;
990b57cec5SDimitry Andric     unsigned NumUnits = SM.getProcResource(I->ProcResourceIdx)->NumUnits;
100*5f757f3fSDimitry Andric     double Temp = NumUnits * 1.0 / I->ReleaseAtCycle;
101bdd1243dSDimitry Andric     Throughput = Throughput ? std::min(*Throughput, Temp) : Temp;
1020b57cec5SDimitry Andric   }
10381ad6265SDimitry Andric   if (Throughput)
104bdd1243dSDimitry Andric     return 1.0 / *Throughput;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   // If no throughput value was calculated, assume that we can execute at the
1070b57cec5SDimitry Andric   // maximum issue width scaled by number of micro-ops for the schedule class.
1080b57cec5SDimitry Andric   return ((double)SCDesc.NumMicroOps) / SM.IssueWidth;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric double
getReciprocalThroughput(const MCSubtargetInfo & STI,const MCInstrInfo & MCII,const MCInst & Inst) const1120b57cec5SDimitry Andric MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI,
1130b57cec5SDimitry Andric                                       const MCInstrInfo &MCII,
1140b57cec5SDimitry Andric                                       const MCInst &Inst) const {
1150b57cec5SDimitry Andric   unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
1160b57cec5SDimitry Andric   const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   // If there's no valid class, assume that the instruction executes/completes
1190b57cec5SDimitry Andric   // at the maximum issue width.
1200b57cec5SDimitry Andric   if (!SCDesc->isValid())
1210b57cec5SDimitry Andric     return 1.0 / IssueWidth;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   unsigned CPUID = getProcessorID();
1240b57cec5SDimitry Andric   while (SCDesc->isVariant()) {
125e8d8bef9SDimitry Andric     SchedClass = STI.resolveVariantSchedClass(SchedClass, &Inst, &MCII, CPUID);
1260b57cec5SDimitry Andric     SCDesc = getSchedClassDesc(SchedClass);
1270b57cec5SDimitry Andric   }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   if (SchedClass)
1300b57cec5SDimitry Andric     return MCSchedModel::getReciprocalThroughput(STI, *SCDesc);
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   llvm_unreachable("unsupported variant scheduling class");
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric double
getReciprocalThroughput(unsigned SchedClass,const InstrItineraryData & IID)1360b57cec5SDimitry Andric MCSchedModel::getReciprocalThroughput(unsigned SchedClass,
1370b57cec5SDimitry Andric                                       const InstrItineraryData &IID) {
138bdd1243dSDimitry Andric   std::optional<double> Throughput;
1390b57cec5SDimitry Andric   const InstrStage *I = IID.beginStage(SchedClass);
1400b57cec5SDimitry Andric   const InstrStage *E = IID.endStage(SchedClass);
1410b57cec5SDimitry Andric   for (; I != E; ++I) {
1420b57cec5SDimitry Andric     if (!I->getCycles())
1430b57cec5SDimitry Andric       continue;
144bdd1243dSDimitry Andric     double Temp = llvm::popcount(I->getUnits()) * 1.0 / I->getCycles();
145bdd1243dSDimitry Andric     Throughput = Throughput ? std::min(*Throughput, Temp) : Temp;
1460b57cec5SDimitry Andric   }
14781ad6265SDimitry Andric   if (Throughput)
148bdd1243dSDimitry Andric     return 1.0 / *Throughput;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   // If there are no execution resources specified for this class, then assume
1510b57cec5SDimitry Andric   // that it can execute at the maximum default issue width.
1520b57cec5SDimitry Andric   return 1.0 / DefaultIssueWidth;
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric unsigned
getForwardingDelayCycles(ArrayRef<MCReadAdvanceEntry> Entries,unsigned WriteResourceID)1560b57cec5SDimitry Andric MCSchedModel::getForwardingDelayCycles(ArrayRef<MCReadAdvanceEntry> Entries,
1570b57cec5SDimitry Andric                                        unsigned WriteResourceID) {
1580b57cec5SDimitry Andric   if (Entries.empty())
1590b57cec5SDimitry Andric     return 0;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   int DelayCycles = 0;
1620b57cec5SDimitry Andric   for (const MCReadAdvanceEntry &E : Entries) {
1630b57cec5SDimitry Andric     if (E.WriteResourceID != WriteResourceID)
1640b57cec5SDimitry Andric       continue;
1650b57cec5SDimitry Andric     DelayCycles = std::min(DelayCycles, E.Cycles);
1660b57cec5SDimitry Andric   }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   return std::abs(DelayCycles);
1690b57cec5SDimitry Andric }
170