1 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the ScheduleDAG class, which is a base class used by
11 // scheduling implementation classes.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "pre-RA-sched"
16 #include "llvm/CodeGen/ScheduleDAG.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/Support/Debug.h"
24 ScheduleDAG::ScheduleDAG(SelectionDAG *dag, MachineBasicBlock *bb,
25 const TargetMachine &tm)
26 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) {
27 TII = TM.getInstrInfo();
29 TRI = TM.getRegisterInfo();
30 TLI = TM.getTargetLowering();
31 ConstPool = MF->getConstantPool();
34 ScheduleDAG::~ScheduleDAG() {}
36 /// CalculateDepths - compute depths using algorithms for the longest
38 void ScheduleDAG::CalculateDepths() {
39 unsigned DAGSize = SUnits.size();
40 std::vector<SUnit*> WorkList;
41 WorkList.reserve(DAGSize);
43 // Initialize the data structures
44 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
45 SUnit *SU = &SUnits[i];
46 unsigned Degree = SU->Preds.size();
47 // Temporarily use the Depth field as scratch space for the degree count.
50 // Is it a node without dependencies?
52 assert(SU->Preds.empty() && "SUnit should have no predecessors");
54 WorkList.push_back(SU);
58 // Process nodes in the topological order
59 while (!WorkList.empty()) {
60 SUnit *SU = WorkList.back();
64 // Use dynamic programming:
65 // When current node is being processed, all of its dependencies
66 // are already processed.
67 // So, just iterate over all predecessors and take the longest path
68 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
70 unsigned PredDepth = I->Dep->Depth;
71 if (PredDepth+1 > SUDepth) {
72 SUDepth = PredDepth + 1;
78 // Update degrees of all nodes depending on current SUnit
79 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
83 // If all dependencies of the node are processed already,
84 // then the longest path for the node can be computed now
85 WorkList.push_back(SU);
90 /// CalculateHeights - compute heights using algorithms for the longest
92 void ScheduleDAG::CalculateHeights() {
93 unsigned DAGSize = SUnits.size();
94 std::vector<SUnit*> WorkList;
95 WorkList.reserve(DAGSize);
97 // Initialize the data structures
98 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
99 SUnit *SU = &SUnits[i];
100 unsigned Degree = SU->Succs.size();
101 // Temporarily use the Height field as scratch space for the degree count.
104 // Is it a node without dependencies?
106 assert(SU->Succs.empty() && "Something wrong");
107 assert(WorkList.empty() && "Should be empty");
108 // Collect leaf nodes
109 WorkList.push_back(SU);
113 // Process nodes in the topological order
114 while (!WorkList.empty()) {
115 SUnit *SU = WorkList.back();
117 unsigned SUHeight = 0;
119 // Use dynamic programming:
120 // When current node is being processed, all of its dependencies
121 // are already processed.
122 // So, just iterate over all successors and take the longest path
123 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
125 unsigned SuccHeight = I->Dep->Height;
126 if (SuccHeight+1 > SUHeight) {
127 SUHeight = SuccHeight + 1;
131 SU->Height = SUHeight;
133 // Update degrees of all nodes depending on current SUnit
134 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
138 // If all dependencies of the node are processed already,
139 // then the longest path for the node can be computed now
140 WorkList.push_back(SU);
145 /// dump - dump the schedule.
146 void ScheduleDAG::dumpSchedule() const {
147 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
148 if (SUnit *SU = Sequence[i])
151 cerr << "**** NOOP ****\n";
156 /// Run - perform scheduling.
158 void ScheduleDAG::Run() {
161 DOUT << "*** Final schedule ***\n";
162 DEBUG(dumpSchedule());
166 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
167 /// a group of nodes flagged together.
168 void SUnit::dump(const ScheduleDAG *G) const {
169 cerr << "SU(" << NodeNum << "): ";
173 void SUnit::dumpAll(const ScheduleDAG *G) const {
176 cerr << " # preds left : " << NumPredsLeft << "\n";
177 cerr << " # succs left : " << NumSuccsLeft << "\n";
178 cerr << " Latency : " << Latency << "\n";
179 cerr << " Depth : " << Depth << "\n";
180 cerr << " Height : " << Height << "\n";
182 if (Preds.size() != 0) {
183 cerr << " Predecessors:\n";
184 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
190 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
196 if (Succs.size() != 0) {
197 cerr << " Successors:\n";
198 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
204 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
214 /// VerifySchedule - Verify that all SUnits were scheduled and that
215 /// their state is consistent.
217 void ScheduleDAG::VerifySchedule(bool isBottomUp) {
218 bool AnyNotSched = false;
219 unsigned DeadNodes = 0;
221 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
222 if (!SUnits[i].isScheduled) {
223 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
228 cerr << "*** Scheduling failed! ***\n";
229 SUnits[i].dump(this);
230 cerr << "has not been scheduled!\n";
233 if (SUnits[i].isScheduled && SUnits[i].Cycle > (unsigned)INT_MAX) {
235 cerr << "*** Scheduling failed! ***\n";
236 SUnits[i].dump(this);
237 cerr << "has an unexpected Cycle value!\n";
241 if (SUnits[i].NumSuccsLeft != 0) {
243 cerr << "*** Scheduling failed! ***\n";
244 SUnits[i].dump(this);
245 cerr << "has successors left!\n";
249 if (SUnits[i].NumPredsLeft != 0) {
251 cerr << "*** Scheduling failed! ***\n";
252 SUnits[i].dump(this);
253 cerr << "has predecessors left!\n";
258 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
261 assert(!AnyNotSched);
262 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
263 "The number of nodes scheduled doesn't match the expected number!");