LLVM API Documentation

TargetSchedule.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/TargetSchedule.h - Sched Machine Model -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a wrapper around MCSchedModel that allows the interface to
00011 // benefit from information currently only available in TargetInstrInfo.
00012 // Ideally, the scheduling interface would be fully defined in the MC layer.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_TARGETSCHEDULE_H
00017 #define LLVM_CODEGEN_TARGETSCHEDULE_H
00018 
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/MC/MCInstrItineraries.h"
00021 #include "llvm/MC/MCSchedule.h"
00022 #include "llvm/Target/TargetSubtargetInfo.h"
00023 
00024 namespace llvm {
00025 
00026 class TargetRegisterInfo;
00027 class TargetSubtargetInfo;
00028 class TargetInstrInfo;
00029 class MachineInstr;
00030 
00031 /// Provide an instruction scheduling machine model to CodeGen passes.
00032 class TargetSchedModel {
00033   // For efficiency, hold a copy of the statically defined MCSchedModel for this
00034   // processor.
00035   MCSchedModel SchedModel;
00036   InstrItineraryData InstrItins;
00037   const TargetSubtargetInfo *STI;
00038   const TargetInstrInfo *TII;
00039 
00040   SmallVector<unsigned, 16> ResourceFactors;
00041   unsigned MicroOpFactor; // Multiply to normalize microops to resource units.
00042   unsigned ResourceLCM;   // Resource units per cycle. Latency normalization factor.
00043 public:
00044   TargetSchedModel(): SchedModel(MCSchedModel::GetDefaultSchedModel()), STI(nullptr), TII(nullptr) {}
00045 
00046   /// \brief Initialize the machine model for instruction scheduling.
00047   ///
00048   /// The machine model API keeps a copy of the top-level MCSchedModel table
00049   /// indices and may query TargetSubtargetInfo and TargetInstrInfo to resolve
00050   /// dynamic properties.
00051   void init(const MCSchedModel &sm, const TargetSubtargetInfo *sti,
00052             const TargetInstrInfo *tii);
00053 
00054   /// Return the MCSchedClassDesc for this instruction.
00055   const MCSchedClassDesc *resolveSchedClass(const MachineInstr *MI) const;
00056 
00057   /// \brief TargetInstrInfo getter.
00058   const TargetInstrInfo *getInstrInfo() const { return TII; }
00059 
00060   /// \brief Return true if this machine model includes an instruction-level
00061   /// scheduling model.
00062   ///
00063   /// This is more detailed than the course grain IssueWidth and default
00064   /// latency properties, but separate from the per-cycle itinerary data.
00065   bool hasInstrSchedModel() const;
00066 
00067   const MCSchedModel *getMCSchedModel() const { return &SchedModel; }
00068 
00069   /// \brief Return true if this machine model includes cycle-to-cycle itinerary
00070   /// data.
00071   ///
00072   /// This models scheduling at each stage in the processor pipeline.
00073   bool hasInstrItineraries() const;
00074 
00075   const InstrItineraryData *getInstrItineraries() const {
00076     if (hasInstrItineraries())
00077       return &InstrItins;
00078     return nullptr;
00079   }
00080 
00081   /// \brief Identify the processor corresponding to the current subtarget.
00082   unsigned getProcessorID() const { return SchedModel.getProcessorID(); }
00083 
00084   /// \brief Maximum number of micro-ops that may be scheduled per cycle.
00085   unsigned getIssueWidth() const { return SchedModel.IssueWidth; }
00086 
00087   /// \brief Return the number of issue slots required for this MI.
00088   unsigned getNumMicroOps(const MachineInstr *MI,
00089                           const MCSchedClassDesc *SC = nullptr) const;
00090 
00091   /// \brief Get the number of kinds of resources for this target.
00092   unsigned getNumProcResourceKinds() const {
00093     return SchedModel.getNumProcResourceKinds();
00094   }
00095 
00096   /// \brief Get a processor resource by ID for convenience.
00097   const MCProcResourceDesc *getProcResource(unsigned PIdx) const {
00098     return SchedModel.getProcResource(PIdx);
00099   }
00100 
00101 #ifndef NDEBUG
00102   const char *getResourceName(unsigned PIdx) const {
00103     if (!PIdx)
00104       return "MOps";
00105     return SchedModel.getProcResource(PIdx)->Name;
00106   }
00107 #endif
00108 
00109   typedef const MCWriteProcResEntry *ProcResIter;
00110 
00111   // \brief Get an iterator into the processor resources consumed by this
00112   // scheduling class.
00113   ProcResIter getWriteProcResBegin(const MCSchedClassDesc *SC) const {
00114     // The subtarget holds a single resource table for all processors.
00115     return STI->getWriteProcResBegin(SC);
00116   }
00117   ProcResIter getWriteProcResEnd(const MCSchedClassDesc *SC) const {
00118     return STI->getWriteProcResEnd(SC);
00119   }
00120 
00121   /// \brief Multiply the number of units consumed for a resource by this factor
00122   /// to normalize it relative to other resources.
00123   unsigned getResourceFactor(unsigned ResIdx) const {
00124     return ResourceFactors[ResIdx];
00125   }
00126 
00127   /// \brief Multiply number of micro-ops by this factor to normalize it
00128   /// relative to other resources.
00129   unsigned getMicroOpFactor() const {
00130     return MicroOpFactor;
00131   }
00132 
00133   /// \brief Multiply cycle count by this factor to normalize it relative to
00134   /// other resources. This is the number of resource units per cycle.
00135   unsigned getLatencyFactor() const {
00136     return ResourceLCM;
00137   }
00138 
00139   /// \brief Number of micro-ops that may be buffered for OOO execution.
00140   unsigned getMicroOpBufferSize() const { return SchedModel.MicroOpBufferSize; }
00141 
00142   /// \brief Number of resource units that may be buffered for OOO execution.
00143   /// \return The buffer size in resource units or -1 for unlimited.
00144   int getResourceBufferSize(unsigned PIdx) const {
00145     return SchedModel.getProcResource(PIdx)->BufferSize;
00146   }
00147 
00148   /// \brief Compute operand latency based on the available machine model.
00149   ///
00150   /// Compute and return the latency of the given data dependent def and use
00151   /// when the operand indices are already known. UseMI may be NULL for an
00152   /// unknown user.
00153   unsigned computeOperandLatency(const MachineInstr *DefMI, unsigned DefOperIdx,
00154                                  const MachineInstr *UseMI, unsigned UseOperIdx)
00155     const;
00156 
00157   /// \brief Compute the instruction latency based on the available machine
00158   /// model.
00159   ///
00160   /// Compute and return the expected latency of this instruction independent of
00161   /// a particular use. computeOperandLatency is the preferred API, but this is
00162   /// occasionally useful to help estimate instruction cost.
00163   ///
00164   /// If UseDefaultDefLatency is false and no new machine sched model is
00165   /// present this method falls back to TII->getInstrLatency with an empty
00166   /// instruction itinerary (this is so we preserve the previous behavior of the
00167   /// if converter after moving it to TargetSchedModel).
00168   unsigned computeInstrLatency(const MachineInstr *MI,
00169                                bool UseDefaultDefLatency = true) const;
00170   unsigned computeInstrLatency(unsigned Opcode) const;
00171 
00172   /// \brief Output dependency latency of a pair of defs of the same register.
00173   ///
00174   /// This is typically one cycle.
00175   unsigned computeOutputLatency(const MachineInstr *DefMI, unsigned DefIdx,
00176                                 const MachineInstr *DepMI) const;
00177 };
00178 
00179 } // namespace llvm
00180 
00181 #endif