LLVM API Documentation
00001 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===// 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 implements the ScheduleDAG class, which is a base class used by 00011 // scheduling implementation classes. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "pre-RA-sched" 00016 #include "llvm/CodeGen/ScheduleDAG.h" 00017 #include "llvm/Target/TargetMachine.h" 00018 #include "llvm/Target/TargetInstrInfo.h" 00019 #include "llvm/Target/TargetRegisterInfo.h" 00020 #include "llvm/Support/Debug.h" 00021 #include <climits> 00022 using namespace llvm; 00023 00024 ScheduleDAG::ScheduleDAG(SelectionDAG *dag, MachineBasicBlock *bb, 00025 const TargetMachine &tm) 00026 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) { 00027 TII = TM.getInstrInfo(); 00028 MF = BB->getParent(); 00029 TRI = TM.getRegisterInfo(); 00030 TLI = TM.getTargetLowering(); 00031 ConstPool = MF->getConstantPool(); 00032 } 00033 00034 ScheduleDAG::~ScheduleDAG() {} 00035 00036 /// dump - dump the schedule. 00037 void ScheduleDAG::dumpSchedule() const { 00038 for (unsigned i = 0, e = Sequence.size(); i != e; i++) { 00039 if (SUnit *SU = Sequence[i]) 00040 SU->dump(this); 00041 else 00042 cerr << "**** NOOP ****\n"; 00043 } 00044 } 00045 00046 00047 /// Run - perform scheduling. 00048 /// 00049 void ScheduleDAG::Run() { 00050 Schedule(); 00051 00052 DOUT << "*** Final schedule ***\n"; 00053 DEBUG(dumpSchedule()); 00054 DOUT << "\n"; 00055 } 00056 00057 /// addPred - This adds the specified edge as a pred of the current node if 00058 /// not already. It also adds the current node as a successor of the 00059 /// specified node. 00060 void SUnit::addPred(const SDep &D) { 00061 // If this node already has this depenence, don't add a redundant one. 00062 for (unsigned i = 0, e = (unsigned)Preds.size(); i != e; ++i) 00063 if (Preds[i] == D) 00064 return; 00065 // Now add a corresponding succ to N. 00066 SDep P = D; 00067 P.setSUnit(this); 00068 SUnit *N = D.getSUnit(); 00069 // Update the bookkeeping. 00070 if (D.getKind() == SDep::Data) { 00071 ++NumPreds; 00072 ++N->NumSuccs; 00073 } 00074 if (!N->isScheduled) 00075 ++NumPredsLeft; 00076 if (!isScheduled) 00077 ++N->NumSuccsLeft; 00078 N->Succs.push_back(P); 00079 Preds.push_back(D); 00080 if (P.getLatency() != 0) { 00081 this->setDepthDirty(); 00082 N->setHeightDirty(); 00083 } 00084 } 00085 00086 /// removePred - This removes the specified edge as a pred of the current 00087 /// node if it exists. It also removes the current node as a successor of 00088 /// the specified node. 00089 void SUnit::removePred(const SDep &D) { 00090 // Find the matching predecessor. 00091 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end(); 00092 I != E; ++I) 00093 if (*I == D) { 00094 bool FoundSucc = false; 00095 // Find the corresponding successor in N. 00096 SDep P = D; 00097 P.setSUnit(this); 00098 SUnit *N = D.getSUnit(); 00099 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(), 00100 EE = N->Succs.end(); II != EE; ++II) 00101 if (*II == P) { 00102 FoundSucc = true; 00103 N->Succs.erase(II); 00104 break; 00105 } 00106 assert(FoundSucc && "Mismatching preds / succs lists!"); 00107 Preds.erase(I); 00108 // Update the bookkeeping; 00109 if (D.getKind() == SDep::Data) { 00110 --NumPreds; 00111 --N->NumSuccs; 00112 } 00113 if (!N->isScheduled) 00114 --NumPredsLeft; 00115 if (!isScheduled) 00116 --N->NumSuccsLeft; 00117 if (P.getLatency() != 0) { 00118 this->setDepthDirty(); 00119 N->setHeightDirty(); 00120 } 00121 return; 00122 } 00123 } 00124 00125 void SUnit::setDepthDirty() { 00126 if (!isDepthCurrent) return; 00127 SmallVector<SUnit*, 8> WorkList; 00128 WorkList.push_back(this); 00129 do { 00130 SUnit *SU = WorkList.pop_back_val(); 00131 SU->isDepthCurrent = false; 00132 for (SUnit::const_succ_iterator I = SU->Succs.begin(), 00133 E = SU->Succs.end(); I != E; ++I) { 00134 SUnit *SuccSU = I->getSUnit(); 00135 if (SuccSU->isDepthCurrent) 00136 WorkList.push_back(SuccSU); 00137 } 00138 } while (!WorkList.empty()); 00139 } 00140 00141 void SUnit::setHeightDirty() { 00142 if (!isHeightCurrent) return; 00143 SmallVector<SUnit*, 8> WorkList; 00144 WorkList.push_back(this); 00145 do { 00146 SUnit *SU = WorkList.pop_back_val(); 00147 SU->isHeightCurrent = false; 00148 for (SUnit::const_pred_iterator I = SU->Preds.begin(), 00149 E = SU->Preds.end(); I != E; ++I) { 00150 SUnit *PredSU = I->getSUnit(); 00151 if (PredSU->isHeightCurrent) 00152 WorkList.push_back(PredSU); 00153 } 00154 } while (!WorkList.empty()); 00155 } 00156 00157 /// setDepthToAtLeast - Update this node's successors to reflect the 00158 /// fact that this node's depth just increased. 00159 /// 00160 void SUnit::setDepthToAtLeast(unsigned NewDepth) { 00161 if (NewDepth <= getDepth()) 00162 return; 00163 setDepthDirty(); 00164 Depth = NewDepth; 00165 isDepthCurrent = true; 00166 } 00167 00168 /// setHeightToAtLeast - Update this node's predecessors to reflect the 00169 /// fact that this node's height just increased. 00170 /// 00171 void SUnit::setHeightToAtLeast(unsigned NewHeight) { 00172 if (NewHeight <= getHeight()) 00173 return; 00174 setHeightDirty(); 00175 Height = NewHeight; 00176 isHeightCurrent = true; 00177 } 00178 00179 /// ComputeDepth - Calculate the maximal path from the node to the exit. 00180 /// 00181 void SUnit::ComputeDepth() { 00182 SmallVector<SUnit*, 8> WorkList; 00183 WorkList.push_back(this); 00184 do { 00185 SUnit *Cur = WorkList.back(); 00186 00187 bool Done = true; 00188 unsigned MaxPredDepth = 0; 00189 for (SUnit::const_pred_iterator I = Cur->Preds.begin(), 00190 E = Cur->Preds.end(); I != E; ++I) { 00191 SUnit *PredSU = I->getSUnit(); 00192 if (PredSU->isDepthCurrent) 00193 MaxPredDepth = std::max(MaxPredDepth, 00194 PredSU->Depth + I->getLatency()); 00195 else { 00196 Done = false; 00197 WorkList.push_back(PredSU); 00198 } 00199 } 00200 00201 if (Done) { 00202 WorkList.pop_back(); 00203 if (MaxPredDepth != Cur->Depth) { 00204 Cur->setDepthDirty(); 00205 Cur->Depth = MaxPredDepth; 00206 } 00207 Cur->isDepthCurrent = true; 00208 } 00209 } while (!WorkList.empty()); 00210 } 00211 00212 /// ComputeHeight - Calculate the maximal path from the node to the entry. 00213 /// 00214 void SUnit::ComputeHeight() { 00215 SmallVector<SUnit*, 8> WorkList; 00216 WorkList.push_back(this); 00217 do { 00218 SUnit *Cur = WorkList.back(); 00219 00220 bool Done = true; 00221 unsigned MaxSuccHeight = 0; 00222 for (SUnit::const_succ_iterator I = Cur->Succs.begin(), 00223 E = Cur->Succs.end(); I != E; ++I) { 00224 SUnit *SuccSU = I->getSUnit(); 00225 if (SuccSU->isHeightCurrent) 00226 MaxSuccHeight = std::max(MaxSuccHeight, 00227 SuccSU->Height + I->getLatency()); 00228 else { 00229 Done = false; 00230 WorkList.push_back(SuccSU); 00231 } 00232 } 00233 00234 if (Done) { 00235 WorkList.pop_back(); 00236 if (MaxSuccHeight != Cur->Height) { 00237 Cur->setHeightDirty(); 00238 Cur->Height = MaxSuccHeight; 00239 } 00240 Cur->isHeightCurrent = true; 00241 } 00242 } while (!WorkList.empty()); 00243 } 00244 00245 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or 00246 /// a group of nodes flagged together. 00247 void SUnit::dump(const ScheduleDAG *G) const { 00248 cerr << "SU(" << NodeNum << "): "; 00249 G->dumpNode(this); 00250 } 00251 00252 void SUnit::dumpAll(const ScheduleDAG *G) const { 00253 dump(G); 00254 00255 cerr << " # preds left : " << NumPredsLeft << "\n"; 00256 cerr << " # succs left : " << NumSuccsLeft << "\n"; 00257 cerr << " Latency : " << Latency << "\n"; 00258 cerr << " Depth : " << Depth << "\n"; 00259 cerr << " Height : " << Height << "\n"; 00260 00261 if (Preds.size() != 0) { 00262 cerr << " Predecessors:\n"; 00263 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end(); 00264 I != E; ++I) { 00265 cerr << " "; 00266 switch (I->getKind()) { 00267 case SDep::Data: cerr << "val "; break; 00268 case SDep::Anti: cerr << "anti"; break; 00269 case SDep::Output: cerr << "out "; break; 00270 case SDep::Order: cerr << "ch "; break; 00271 } 00272 cerr << "#"; 00273 cerr << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")"; 00274 if (I->isArtificial()) 00275 cerr << " *"; 00276 cerr << "\n"; 00277 } 00278 } 00279 if (Succs.size() != 0) { 00280 cerr << " Successors:\n"; 00281 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end(); 00282 I != E; ++I) { 00283 cerr << " "; 00284 switch (I->getKind()) { 00285 case SDep::Data: cerr << "val "; break; 00286 case SDep::Anti: cerr << "anti"; break; 00287 case SDep::Output: cerr << "out "; break; 00288 case SDep::Order: cerr << "ch "; break; 00289 } 00290 cerr << "#"; 00291 cerr << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")"; 00292 if (I->isArtificial()) 00293 cerr << " *"; 00294 cerr << "\n"; 00295 } 00296 } 00297 cerr << "\n"; 00298 } 00299 00300 #ifndef NDEBUG 00301 /// VerifySchedule - Verify that all SUnits were scheduled and that 00302 /// their state is consistent. 00303 /// 00304 void ScheduleDAG::VerifySchedule(bool isBottomUp) { 00305 bool AnyNotSched = false; 00306 unsigned DeadNodes = 0; 00307 unsigned Noops = 0; 00308 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { 00309 if (!SUnits[i].isScheduled) { 00310 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) { 00311 ++DeadNodes; 00312 continue; 00313 } 00314 if (!AnyNotSched) 00315 cerr << "*** Scheduling failed! ***\n"; 00316 SUnits[i].dump(this); 00317 cerr << "has not been scheduled!\n"; 00318 AnyNotSched = true; 00319 } 00320 if (SUnits[i].isScheduled && 00321 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getHeight()) > 00322 unsigned(INT_MAX)) { 00323 if (!AnyNotSched) 00324 cerr << "*** Scheduling failed! ***\n"; 00325 SUnits[i].dump(this); 00326 cerr << "has an unexpected " 00327 << (isBottomUp ? "Height" : "Depth") << " value!\n"; 00328 AnyNotSched = true; 00329 } 00330 if (isBottomUp) { 00331 if (SUnits[i].NumSuccsLeft != 0) { 00332 if (!AnyNotSched) 00333 cerr << "*** Scheduling failed! ***\n"; 00334 SUnits[i].dump(this); 00335 cerr << "has successors left!\n"; 00336 AnyNotSched = true; 00337 } 00338 } else { 00339 if (SUnits[i].NumPredsLeft != 0) { 00340 if (!AnyNotSched) 00341 cerr << "*** Scheduling failed! ***\n"; 00342 SUnits[i].dump(this); 00343 cerr << "has predecessors left!\n"; 00344 AnyNotSched = true; 00345 } 00346 } 00347 } 00348 for (unsigned i = 0, e = Sequence.size(); i != e; ++i) 00349 if (!Sequence[i]) 00350 ++Noops; 00351 assert(!AnyNotSched); 00352 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() && 00353 "The number of nodes scheduled doesn't match the expected number!"); 00354 } 00355 #endif 00356 00357 /// InitDAGTopologicalSorting - create the initial topological 00358 /// ordering from the DAG to be scheduled. 00359 /// 00360 /// The idea of the algorithm is taken from 00361 /// "Online algorithms for managing the topological order of 00362 /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly 00363 /// This is the MNR algorithm, which was first introduced by 00364 /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in 00365 /// "Maintaining a topological order under edge insertions". 00366 /// 00367 /// Short description of the algorithm: 00368 /// 00369 /// Topological ordering, ord, of a DAG maps each node to a topological 00370 /// index so that for all edges X->Y it is the case that ord(X) < ord(Y). 00371 /// 00372 /// This means that if there is a path from the node X to the node Z, 00373 /// then ord(X) < ord(Z). 00374 /// 00375 /// This property can be used to check for reachability of nodes: 00376 /// if Z is reachable from X, then an insertion of the edge Z->X would 00377 /// create a cycle. 00378 /// 00379 /// The algorithm first computes a topological ordering for the DAG by 00380 /// initializing the Index2Node and Node2Index arrays and then tries to keep 00381 /// the ordering up-to-date after edge insertions by reordering the DAG. 00382 /// 00383 /// On insertion of the edge X->Y, the algorithm first marks by calling DFS 00384 /// the nodes reachable from Y, and then shifts them using Shift to lie 00385 /// immediately after X in Index2Node. 00386 void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() { 00387 unsigned DAGSize = SUnits.size(); 00388 std::vector<SUnit*> WorkList; 00389 WorkList.reserve(DAGSize); 00390 00391 Index2Node.resize(DAGSize); 00392 Node2Index.resize(DAGSize); 00393 00394 // Initialize the data structures. 00395 for (unsigned i = 0, e = DAGSize; i != e; ++i) { 00396 SUnit *SU = &SUnits[i]; 00397 int NodeNum = SU->NodeNum; 00398 unsigned Degree = SU->Succs.size(); 00399 // Temporarily use the Node2Index array as scratch space for degree counts. 00400 Node2Index[NodeNum] = Degree; 00401 00402 // Is it a node without dependencies? 00403 if (Degree == 0) { 00404 assert(SU->Succs.empty() && "SUnit should have no successors"); 00405 // Collect leaf nodes. 00406 WorkList.push_back(SU); 00407 } 00408 } 00409 00410 int Id = DAGSize; 00411 while (!WorkList.empty()) { 00412 SUnit *SU = WorkList.back(); 00413 WorkList.pop_back(); 00414 Allocate(SU->NodeNum, --Id); 00415 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00416 I != E; ++I) { 00417 SUnit *SU = I->getSUnit(); 00418 if (!--Node2Index[SU->NodeNum]) 00419 // If all dependencies of the node are processed already, 00420 // then the node can be computed now. 00421 WorkList.push_back(SU); 00422 } 00423 } 00424 00425 Visited.resize(DAGSize); 00426 00427 #ifndef NDEBUG 00428 // Check correctness of the ordering 00429 for (unsigned i = 0, e = DAGSize; i != e; ++i) { 00430 SUnit *SU = &SUnits[i]; 00431 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00432 I != E; ++I) { 00433 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] && 00434 "Wrong topological sorting"); 00435 } 00436 } 00437 #endif 00438 } 00439 00440 /// AddPred - Updates the topological ordering to accomodate an edge 00441 /// to be added from SUnit X to SUnit Y. 00442 void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) { 00443 int UpperBound, LowerBound; 00444 LowerBound = Node2Index[Y->NodeNum]; 00445 UpperBound = Node2Index[X->NodeNum]; 00446 bool HasLoop = false; 00447 // Is Ord(X) < Ord(Y) ? 00448 if (LowerBound < UpperBound) { 00449 // Update the topological order. 00450 Visited.reset(); 00451 DFS(Y, UpperBound, HasLoop); 00452 assert(!HasLoop && "Inserted edge creates a loop!"); 00453 // Recompute topological indexes. 00454 Shift(Visited, LowerBound, UpperBound); 00455 } 00456 } 00457 00458 /// RemovePred - Updates the topological ordering to accomodate an 00459 /// an edge to be removed from the specified node N from the predecessors 00460 /// of the current node M. 00461 void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) { 00462 // InitDAGTopologicalSorting(); 00463 } 00464 00465 /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark 00466 /// all nodes affected by the edge insertion. These nodes will later get new 00467 /// topological indexes by means of the Shift method. 00468 void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound, 00469 bool& HasLoop) { 00470 std::vector<const SUnit*> WorkList; 00471 WorkList.reserve(SUnits.size()); 00472 00473 WorkList.push_back(SU); 00474 do { 00475 SU = WorkList.back(); 00476 WorkList.pop_back(); 00477 Visited.set(SU->NodeNum); 00478 for (int I = SU->Succs.size()-1; I >= 0; --I) { 00479 int s = SU->Succs[I].getSUnit()->NodeNum; 00480 if (Node2Index[s] == UpperBound) { 00481 HasLoop = true; 00482 return; 00483 } 00484 // Visit successors if not already and in affected region. 00485 if (!Visited.test(s) && Node2Index[s] < UpperBound) { 00486 WorkList.push_back(SU->Succs[I].getSUnit()); 00487 } 00488 } 00489 } while (!WorkList.empty()); 00490 } 00491 00492 /// Shift - Renumber the nodes so that the topological ordering is 00493 /// preserved. 00494 void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound, 00495 int UpperBound) { 00496 std::vector<int> L; 00497 int shift = 0; 00498 int i; 00499 00500 for (i = LowerBound; i <= UpperBound; ++i) { 00501 // w is node at topological index i. 00502 int w = Index2Node[i]; 00503 if (Visited.test(w)) { 00504 // Unmark. 00505 Visited.reset(w); 00506 L.push_back(w); 00507 shift = shift + 1; 00508 } else { 00509 Allocate(w, i - shift); 00510 } 00511 } 00512 00513 for (unsigned j = 0; j < L.size(); ++j) { 00514 Allocate(L[j], i - shift); 00515 i = i + 1; 00516 } 00517 } 00518 00519 00520 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will 00521 /// create a cycle. 00522 bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) { 00523 if (IsReachable(TargetSU, SU)) 00524 return true; 00525 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00526 I != E; ++I) 00527 if (I->isAssignedRegDep() && 00528 IsReachable(TargetSU, I->getSUnit())) 00529 return true; 00530 return false; 00531 } 00532 00533 /// IsReachable - Checks if SU is reachable from TargetSU. 00534 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU, 00535 const SUnit *TargetSU) { 00536 // If insertion of the edge SU->TargetSU would create a cycle 00537 // then there is a path from TargetSU to SU. 00538 int UpperBound, LowerBound; 00539 LowerBound = Node2Index[TargetSU->NodeNum]; 00540 UpperBound = Node2Index[SU->NodeNum]; 00541 bool HasLoop = false; 00542 // Is Ord(TargetSU) < Ord(SU) ? 00543 if (LowerBound < UpperBound) { 00544 Visited.reset(); 00545 // There may be a path from TargetSU to SU. Check for it. 00546 DFS(TargetSU, UpperBound, HasLoop); 00547 } 00548 return HasLoop; 00549 } 00550 00551 /// Allocate - assign the topological index to the node n. 00552 void ScheduleDAGTopologicalSort::Allocate(int n, int index) { 00553 Node2Index[n] = index; 00554 Index2Node[index] = n; 00555 } 00556 00557 ScheduleDAGTopologicalSort::ScheduleDAGTopologicalSort( 00558 std::vector<SUnit> &sunits) 00559 : SUnits(sunits) {}
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.