Implement ComputeLatency for MachineInstr ScheduleDAGs. Factor
authorDan Gohman <gohman@apple.com>
Fri, 21 Nov 2008 00:12:10 +0000 (00:12 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 21 Nov 2008 00:12:10 +0000 (00:12 +0000)
some of the latency computation logic out of the SDNode
ScheduleDAG code into a TargetInstrItineraries helper method
to help with this.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59761 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/ScheduleDAGInstrs.h
include/llvm/Target/TargetInstrItineraries.h
lib/CodeGen/ScheduleDAGInstrs.cpp
lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp

index 703c5b0876ef8bb5c81e164137e448c479ce968f..cc2cb0f39229fe3791aac77ecf6d841d4646be41 100644 (file)
@@ -53,6 +53,10 @@ namespace llvm {
     /// input.
     virtual void BuildSchedUnits();
 
+    /// ComputeLatency - Compute node latency.
+    ///
+    virtual void ComputeLatency(SUnit *SU);
+
     virtual MachineBasicBlock *EmitSchedule();
 
     /// Schedule - Order nodes according to selected style, filling
index 1a5f46b1500ff8de43fe89a3bbda00dce216770b..18931ea7fb486c8c0861fa31893f99c33a6152c0 100644 (file)
@@ -73,6 +73,24 @@ struct InstrItineraryData {
     unsigned StageIdx = Itineratries[ItinClassIndx].Last;
     return Stages + StageIdx;
   }
+
+  /// getLatency - Return the scheduling latency of the given class.  A
+  /// simple latency value for an instruction is an over-simplification
+  /// for some architectures, but it's a reasonable first approximation.
+  ///
+  unsigned getLatency(unsigned ItinClassIndx) const {
+    // If the target doesn't provide latency information, use a simple
+    // non-zero default value for all instructions.
+    if (isEmpty())
+      return 1;
+
+    // Just sum the cycle count for each stage.
+    unsigned Latency = 0;
+    for (const InstrStage *IS = begin(ItinClassIndx), *E = end(ItinClassIndx);
+         IS != E; ++IS)
+      Latency += IS->Cycles;
+    return Latency;
+  }
 };
 
 
index b6bc44e849e9f2d59ff5a0191f3d3f774bc54ec4..06d8ed9b25d0a89330dffd1c3b7f9cc7e0ce055b 100644 (file)
@@ -50,7 +50,7 @@ void ScheduleDAGInstrs::BuildSchedUnits() {
       assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
       std::vector<SUnit *> &UseList = Uses[Reg];
       SUnit *&Def = Defs[Reg];
-      // Optionally add output and anti dependences
+      // Optionally add output and anti dependences.
       if (Def && Def != SU)
         Def->addPred(SU, /*isCtrl=*/true, /*isSpecial=*/false,
                      /*PhyReg=*/Reg, Cost);
@@ -102,6 +102,15 @@ void ScheduleDAGInstrs::BuildSchedUnits() {
   }
 }
 
+void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
+  const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
+
+  // Compute the latency for the node.  We use the sum of the latencies for
+  // all nodes flagged together into this SUnit.
+  SU->Latency =
+    InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
+}
+
 void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
   SU->getInstr()->dump();
 }
index 9d32d9afac5aee5d48ebbb68b459c6c51353afb2..91a8294e1d45a383f035123df372e42b05c20c29 100644 (file)
@@ -193,15 +193,17 @@ void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
   }
 
   SU->Latency = 0;
-  for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
+  bool SawMachineOpcode = false;
+  for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
     if (N->isMachineOpcode()) {
-      unsigned SchedClass = TII->get(N->getMachineOpcode()).getSchedClass();
-      const InstrStage *S = InstrItins.begin(SchedClass);
-      const InstrStage *E = InstrItins.end(SchedClass);
-      for (; S != E; ++S)
-        SU->Latency += S->Cycles;
+      SawMachineOpcode = true;
+      SU->Latency +=
+        InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass());
     }
-  }
+
+  // Ensure that CopyToReg and similar nodes have a non-zero latency.
+  if (!SawMachineOpcode)
+    SU->Latency = 1;
 }
 
 /// CountResults - The results of target nodes have register or immediate