ScheduleDAGFast.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. //===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This implements a fast scheduler.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "pre-RA-sched"
  14. #include "llvm/CodeGen/ScheduleDAGSDNodes.h"
  15. #include "llvm/CodeGen/SchedulerRegistry.h"
  16. #include "llvm/Target/TargetRegisterInfo.h"
  17. #include "llvm/Target/TargetData.h"
  18. #include "llvm/Target/TargetMachine.h"
  19. #include "llvm/Target/TargetInstrInfo.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/Compiler.h"
  22. #include "llvm/ADT/SmallSet.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/ADT/STLExtras.h"
  25. #include "llvm/Support/CommandLine.h"
  26. using namespace llvm;
  27. STATISTIC(NumUnfolds, "Number of nodes unfolded");
  28. STATISTIC(NumDups, "Number of duplicated nodes");
  29. STATISTIC(NumCCCopies, "Number of cross class copies");
  30. static RegisterScheduler
  31. fastDAGScheduler("fast", "Fast suboptimal list scheduling",
  32. createFastDAGScheduler);
  33. namespace {
  34. /// FastPriorityQueue - A degenerate priority queue that considers
  35. /// all nodes to have the same priority.
  36. ///
  37. struct VISIBILITY_HIDDEN FastPriorityQueue {
  38. SmallVector<SUnit *, 16> Queue;
  39. bool empty() const { return Queue.empty(); }
  40. void push(SUnit *U) {
  41. Queue.push_back(U);
  42. }
  43. SUnit *pop() {
  44. if (empty()) return NULL;
  45. SUnit *V = Queue.back();
  46. Queue.pop_back();
  47. return V;
  48. }
  49. };
  50. //===----------------------------------------------------------------------===//
  51. /// ScheduleDAGFast - The actual "fast" list scheduler implementation.
  52. ///
  53. class VISIBILITY_HIDDEN ScheduleDAGFast : public ScheduleDAGSDNodes {
  54. private:
  55. /// AvailableQueue - The priority queue to use for the available SUnits.
  56. FastPriorityQueue AvailableQueue;
  57. /// LiveRegDefs - A set of physical registers and their definition
  58. /// that are "live". These nodes must be scheduled before any other nodes that
  59. /// modifies the registers can be scheduled.
  60. unsigned NumLiveRegs;
  61. std::vector<SUnit*> LiveRegDefs;
  62. std::vector<unsigned> LiveRegCycles;
  63. public:
  64. ScheduleDAGFast(SelectionDAG *dag, MachineBasicBlock *bb,
  65. const TargetMachine &tm)
  66. : ScheduleDAGSDNodes(dag, bb, tm) {}
  67. void Schedule();
  68. /// AddPred - adds a predecessor edge to SUnit SU.
  69. /// This returns true if this is a new predecessor.
  70. bool AddPred(SUnit *SU, const SDep &D) {
  71. return SU->addPred(D);
  72. }
  73. /// RemovePred - removes a predecessor edge from SUnit SU.
  74. /// This returns true if an edge was removed.
  75. bool RemovePred(SUnit *SU, const SDep &D) {
  76. return SU->removePred(D);
  77. }
  78. private:
  79. void ReleasePred(SUnit *SU, SDep *PredEdge);
  80. void ScheduleNodeBottomUp(SUnit*, unsigned);
  81. SUnit *CopyAndMoveSuccessors(SUnit*);
  82. void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
  83. const TargetRegisterClass*,
  84. const TargetRegisterClass*,
  85. SmallVector<SUnit*, 2>&);
  86. bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
  87. void ListScheduleBottomUp();
  88. };
  89. } // end anonymous namespace
  90. /// Schedule - Schedule the DAG using list scheduling.
  91. void ScheduleDAGFast::Schedule() {
  92. DOUT << "********** List Scheduling **********\n";
  93. NumLiveRegs = 0;
  94. LiveRegDefs.resize(TRI->getNumRegs(), NULL);
  95. LiveRegCycles.resize(TRI->getNumRegs(), 0);
  96. // Build scheduling units.
  97. BuildSchedUnits();
  98. DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
  99. SUnits[su].dumpAll(this));
  100. // Execute the actual scheduling loop.
  101. ListScheduleBottomUp();
  102. }
  103. //===----------------------------------------------------------------------===//
  104. // Bottom-Up Scheduling
  105. //===----------------------------------------------------------------------===//
  106. /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
  107. /// the AvailableQueue if the count reaches zero. Also update its cycle bound.
  108. void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
  109. SUnit *PredSU = PredEdge->getSUnit();
  110. --PredSU->NumSuccsLeft;
  111. #ifndef NDEBUG
  112. if (PredSU->NumSuccsLeft < 0) {
  113. cerr << "*** Scheduling failed! ***\n";
  114. PredSU->dump(this);
  115. cerr << " has been released too many times!\n";
  116. assert(0);
  117. }
  118. #endif
  119. if (PredSU->NumSuccsLeft == 0) {
  120. PredSU->isAvailable = true;
  121. AvailableQueue.push(PredSU);
  122. }
  123. }
  124. /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
  125. /// count of its predecessors. If a predecessor pending count is zero, add it to
  126. /// the Available queue.
  127. void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
  128. DOUT << "*** Scheduling [" << CurCycle << "]: ";
  129. DEBUG(SU->dump(this));
  130. SU->Cycle = CurCycle;
  131. Sequence.push_back(SU);
  132. // Bottom up: release predecessors
  133. for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  134. I != E; ++I) {
  135. ReleasePred(SU, &*I);
  136. if (I->isAssignedRegDep()) {
  137. // This is a physical register dependency and it's impossible or
  138. // expensive to copy the register. Make sure nothing that can
  139. // clobber the register is scheduled between the predecessor and
  140. // this node.
  141. if (!LiveRegDefs[I->getReg()]) {
  142. ++NumLiveRegs;
  143. LiveRegDefs[I->getReg()] = I->getSUnit();
  144. LiveRegCycles[I->getReg()] = CurCycle;
  145. }
  146. }
  147. }
  148. // Release all the implicit physical register defs that are live.
  149. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  150. I != E; ++I) {
  151. if (I->isAssignedRegDep()) {
  152. if (LiveRegCycles[I->getReg()] == I->getSUnit()->Cycle) {
  153. assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
  154. assert(LiveRegDefs[I->getReg()] == SU &&
  155. "Physical register dependency violated?");
  156. --NumLiveRegs;
  157. LiveRegDefs[I->getReg()] = NULL;
  158. LiveRegCycles[I->getReg()] = 0;
  159. }
  160. }
  161. }
  162. SU->isScheduled = true;
  163. }
  164. /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
  165. /// successors to the newly created node.
  166. SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
  167. if (SU->getNode()->getFlaggedNode())
  168. return NULL;
  169. SDNode *N = SU->getNode();
  170. if (!N)
  171. return NULL;
  172. SUnit *NewSU;
  173. bool TryUnfold = false;
  174. for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
  175. MVT VT = N->getValueType(i);
  176. if (VT == MVT::Flag)
  177. return NULL;
  178. else if (VT == MVT::Other)
  179. TryUnfold = true;
  180. }
  181. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  182. const SDValue &Op = N->getOperand(i);
  183. MVT VT = Op.getNode()->getValueType(Op.getResNo());
  184. if (VT == MVT::Flag)
  185. return NULL;
  186. }
  187. if (TryUnfold) {
  188. SmallVector<SDNode*, 2> NewNodes;
  189. if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
  190. return NULL;
  191. DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
  192. assert(NewNodes.size() == 2 && "Expected a load folding node!");
  193. N = NewNodes[1];
  194. SDNode *LoadNode = NewNodes[0];
  195. unsigned NumVals = N->getNumValues();
  196. unsigned OldNumVals = SU->getNode()->getNumValues();
  197. for (unsigned i = 0; i != NumVals; ++i)
  198. DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
  199. DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
  200. SDValue(LoadNode, 1));
  201. SUnit *NewSU = NewSUnit(N);
  202. assert(N->getNodeId() == -1 && "Node already inserted!");
  203. N->setNodeId(NewSU->NodeNum);
  204. const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
  205. for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
  206. if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
  207. NewSU->isTwoAddress = true;
  208. break;
  209. }
  210. }
  211. if (TID.isCommutable())
  212. NewSU->isCommutable = true;
  213. // FIXME: Calculate height / depth and propagate the changes?
  214. NewSU->Depth = SU->Depth;
  215. NewSU->Height = SU->Height;
  216. // LoadNode may already exist. This can happen when there is another
  217. // load from the same location and producing the same type of value
  218. // but it has different alignment or volatileness.
  219. bool isNewLoad = true;
  220. SUnit *LoadSU;
  221. if (LoadNode->getNodeId() != -1) {
  222. LoadSU = &SUnits[LoadNode->getNodeId()];
  223. isNewLoad = false;
  224. } else {
  225. LoadSU = NewSUnit(LoadNode);
  226. LoadNode->setNodeId(LoadSU->NodeNum);
  227. LoadSU->Depth = SU->Depth;
  228. LoadSU->Height = SU->Height;
  229. }
  230. SDep ChainPred;
  231. SmallVector<SDep, 4> ChainSuccs;
  232. SmallVector<SDep, 4> LoadPreds;
  233. SmallVector<SDep, 4> NodePreds;
  234. SmallVector<SDep, 4> NodeSuccs;
  235. for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  236. I != E; ++I) {
  237. if (I->isCtrl())
  238. ChainPred = *I;
  239. else if (I->getSUnit()->getNode() &&
  240. I->getSUnit()->getNode()->isOperandOf(LoadNode))
  241. LoadPreds.push_back(*I);
  242. else
  243. NodePreds.push_back(*I);
  244. }
  245. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  246. I != E; ++I) {
  247. if (I->isCtrl())
  248. ChainSuccs.push_back(*I);
  249. else
  250. NodeSuccs.push_back(*I);
  251. }
  252. if (ChainPred.getSUnit()) {
  253. RemovePred(SU, ChainPred);
  254. if (isNewLoad)
  255. AddPred(LoadSU, ChainPred);
  256. }
  257. for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
  258. const SDep &Pred = LoadPreds[i];
  259. RemovePred(SU, Pred);
  260. if (isNewLoad) {
  261. AddPred(LoadSU, Pred);
  262. }
  263. }
  264. for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
  265. const SDep &Pred = NodePreds[i];
  266. RemovePred(SU, Pred);
  267. AddPred(NewSU, Pred);
  268. }
  269. for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
  270. SDep D = NodeSuccs[i];
  271. SUnit *SuccDep = D.getSUnit();
  272. D.setSUnit(SU);
  273. RemovePred(SuccDep, D);
  274. D.setSUnit(NewSU);
  275. AddPred(SuccDep, D);
  276. }
  277. for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
  278. SDep D = ChainSuccs[i];
  279. SUnit *SuccDep = D.getSUnit();
  280. D.setSUnit(SU);
  281. RemovePred(SuccDep, D);
  282. if (isNewLoad) {
  283. D.setSUnit(LoadSU);
  284. AddPred(SuccDep, D);
  285. }
  286. }
  287. if (isNewLoad) {
  288. AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
  289. }
  290. ++NumUnfolds;
  291. if (NewSU->NumSuccsLeft == 0) {
  292. NewSU->isAvailable = true;
  293. return NewSU;
  294. }
  295. SU = NewSU;
  296. }
  297. DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
  298. NewSU = Clone(SU);
  299. // New SUnit has the exact same predecessors.
  300. for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  301. I != E; ++I)
  302. if (!I->isArtificial()) {
  303. AddPred(NewSU, *I);
  304. NewSU->Depth = std::max(NewSU->Depth, I->getSUnit()->Depth+1);
  305. }
  306. // Only copy scheduled successors. Cut them from old node's successor
  307. // list and move them over.
  308. SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
  309. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  310. I != E; ++I) {
  311. if (I->isArtificial())
  312. continue;
  313. SUnit *SuccSU = I->getSUnit();
  314. if (SuccSU->isScheduled) {
  315. NewSU->Height = std::max(NewSU->Height, SuccSU->Height+1);
  316. SDep D = *I;
  317. D.setSUnit(NewSU);
  318. AddPred(SuccSU, D);
  319. D.setSUnit(SU);
  320. DelDeps.push_back(std::make_pair(SuccSU, D));
  321. }
  322. }
  323. for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
  324. RemovePred(DelDeps[i].first, DelDeps[i].second);
  325. }
  326. ++NumDups;
  327. return NewSU;
  328. }
  329. /// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
  330. /// and move all scheduled successors of the given SUnit to the last copy.
  331. void ScheduleDAGFast::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
  332. const TargetRegisterClass *DestRC,
  333. const TargetRegisterClass *SrcRC,
  334. SmallVector<SUnit*, 2> &Copies) {
  335. SUnit *CopyFromSU = NewSUnit(static_cast<SDNode *>(NULL));
  336. CopyFromSU->CopySrcRC = SrcRC;
  337. CopyFromSU->CopyDstRC = DestRC;
  338. SUnit *CopyToSU = NewSUnit(static_cast<SDNode *>(NULL));
  339. CopyToSU->CopySrcRC = DestRC;
  340. CopyToSU->CopyDstRC = SrcRC;
  341. // Only copy scheduled successors. Cut them from old node's successor
  342. // list and move them over.
  343. SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
  344. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  345. I != E; ++I) {
  346. if (I->isArtificial())
  347. continue;
  348. SUnit *SuccSU = I->getSUnit();
  349. if (SuccSU->isScheduled) {
  350. SDep D = *I;
  351. D.setSUnit(CopyToSU);
  352. AddPred(SuccSU, D);
  353. DelDeps.push_back(std::make_pair(SuccSU, *I));
  354. }
  355. }
  356. for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
  357. RemovePred(DelDeps[i].first, DelDeps[i].second);
  358. }
  359. AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
  360. AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
  361. Copies.push_back(CopyFromSU);
  362. Copies.push_back(CopyToSU);
  363. ++NumCCCopies;
  364. }
  365. /// getPhysicalRegisterVT - Returns the ValueType of the physical register
  366. /// definition of the specified node.
  367. /// FIXME: Move to SelectionDAG?
  368. static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
  369. const TargetInstrInfo *TII) {
  370. const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
  371. assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
  372. unsigned NumRes = TID.getNumDefs();
  373. for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
  374. if (Reg == *ImpDef)
  375. break;
  376. ++NumRes;
  377. }
  378. return N->getValueType(NumRes);
  379. }
  380. /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
  381. /// scheduling of the given node to satisfy live physical register dependencies.
  382. /// If the specific node is the last one that's available to schedule, do
  383. /// whatever is necessary (i.e. backtracking or cloning) to make it possible.
  384. bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
  385. SmallVector<unsigned, 4> &LRegs){
  386. if (NumLiveRegs == 0)
  387. return false;
  388. SmallSet<unsigned, 4> RegAdded;
  389. // If this node would clobber any "live" register, then it's not ready.
  390. for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  391. I != E; ++I) {
  392. if (I->isAssignedRegDep()) {
  393. unsigned Reg = I->getReg();
  394. if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
  395. if (RegAdded.insert(Reg))
  396. LRegs.push_back(Reg);
  397. }
  398. for (const unsigned *Alias = TRI->getAliasSet(Reg);
  399. *Alias; ++Alias)
  400. if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
  401. if (RegAdded.insert(*Alias))
  402. LRegs.push_back(*Alias);
  403. }
  404. }
  405. }
  406. for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
  407. if (!Node->isMachineOpcode())
  408. continue;
  409. const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
  410. if (!TID.ImplicitDefs)
  411. continue;
  412. for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
  413. if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
  414. if (RegAdded.insert(*Reg))
  415. LRegs.push_back(*Reg);
  416. }
  417. for (const unsigned *Alias = TRI->getAliasSet(*Reg);
  418. *Alias; ++Alias)
  419. if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
  420. if (RegAdded.insert(*Alias))
  421. LRegs.push_back(*Alias);
  422. }
  423. }
  424. }
  425. return !LRegs.empty();
  426. }
  427. /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
  428. /// schedulers.
  429. void ScheduleDAGFast::ListScheduleBottomUp() {
  430. unsigned CurCycle = 0;
  431. // Add root to Available queue.
  432. if (!SUnits.empty()) {
  433. SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
  434. assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
  435. RootSU->isAvailable = true;
  436. AvailableQueue.push(RootSU);
  437. }
  438. // While Available queue is not empty, grab the node with the highest
  439. // priority. If it is not ready put it back. Schedule the node.
  440. SmallVector<SUnit*, 4> NotReady;
  441. DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
  442. Sequence.reserve(SUnits.size());
  443. while (!AvailableQueue.empty()) {
  444. bool Delayed = false;
  445. LRegsMap.clear();
  446. SUnit *CurSU = AvailableQueue.pop();
  447. while (CurSU) {
  448. SmallVector<unsigned, 4> LRegs;
  449. if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
  450. break;
  451. Delayed = true;
  452. LRegsMap.insert(std::make_pair(CurSU, LRegs));
  453. CurSU->isPending = true; // This SU is not in AvailableQueue right now.
  454. NotReady.push_back(CurSU);
  455. CurSU = AvailableQueue.pop();
  456. }
  457. // All candidates are delayed due to live physical reg dependencies.
  458. // Try code duplication or inserting cross class copies
  459. // to resolve it.
  460. if (Delayed && !CurSU) {
  461. if (!CurSU) {
  462. // Try duplicating the nodes that produces these
  463. // "expensive to copy" values to break the dependency. In case even
  464. // that doesn't work, insert cross class copies.
  465. SUnit *TrySU = NotReady[0];
  466. SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
  467. assert(LRegs.size() == 1 && "Can't handle this yet!");
  468. unsigned Reg = LRegs[0];
  469. SUnit *LRDef = LiveRegDefs[Reg];
  470. SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
  471. if (!NewDef) {
  472. // Issue expensive cross register class copies.
  473. MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
  474. const TargetRegisterClass *RC =
  475. TRI->getPhysicalRegisterRegClass(Reg, VT);
  476. const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
  477. if (!DestRC) {
  478. assert(false && "Don't know how to copy this physical register!");
  479. abort();
  480. }
  481. SmallVector<SUnit*, 2> Copies;
  482. InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
  483. DOUT << "Adding an edge from SU # " << TrySU->NodeNum
  484. << " to SU #" << Copies.front()->NodeNum << "\n";
  485. AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
  486. /*Reg=*/0, /*isNormalMemory=*/false,
  487. /*isMustAlias=*/false, /*isArtificial=*/true));
  488. NewDef = Copies.back();
  489. }
  490. DOUT << "Adding an edge from SU # " << NewDef->NodeNum
  491. << " to SU #" << TrySU->NodeNum << "\n";
  492. LiveRegDefs[Reg] = NewDef;
  493. AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
  494. /*Reg=*/0, /*isNormalMemory=*/false,
  495. /*isMustAlias=*/false, /*isArtificial=*/true));
  496. TrySU->isAvailable = false;
  497. CurSU = NewDef;
  498. }
  499. if (!CurSU) {
  500. assert(false && "Unable to resolve live physical register dependencies!");
  501. abort();
  502. }
  503. }
  504. // Add the nodes that aren't ready back onto the available list.
  505. for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
  506. NotReady[i]->isPending = false;
  507. // May no longer be available due to backtracking.
  508. if (NotReady[i]->isAvailable)
  509. AvailableQueue.push(NotReady[i]);
  510. }
  511. NotReady.clear();
  512. if (CurSU)
  513. ScheduleNodeBottomUp(CurSU, CurCycle);
  514. ++CurCycle;
  515. }
  516. // Reverse the order if it is bottom up.
  517. std::reverse(Sequence.begin(), Sequence.end());
  518. #ifndef NDEBUG
  519. // Verify that all SUnits were scheduled.
  520. bool AnyNotSched = false;
  521. unsigned DeadNodes = 0;
  522. unsigned Noops = 0;
  523. for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
  524. if (!SUnits[i].isScheduled) {
  525. if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
  526. ++DeadNodes;
  527. continue;
  528. }
  529. if (!AnyNotSched)
  530. cerr << "*** List scheduling failed! ***\n";
  531. SUnits[i].dump(this);
  532. cerr << "has not been scheduled!\n";
  533. AnyNotSched = true;
  534. }
  535. if (SUnits[i].NumSuccsLeft != 0) {
  536. if (!AnyNotSched)
  537. cerr << "*** List scheduling failed! ***\n";
  538. SUnits[i].dump(this);
  539. cerr << "has successors left!\n";
  540. AnyNotSched = true;
  541. }
  542. }
  543. for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
  544. if (!Sequence[i])
  545. ++Noops;
  546. assert(!AnyNotSched);
  547. assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
  548. "The number of nodes scheduled doesn't match the expected number!");
  549. #endif
  550. }
  551. //===----------------------------------------------------------------------===//
  552. // Public Constructor Functions
  553. //===----------------------------------------------------------------------===//
  554. llvm::ScheduleDAG* llvm::createFastDAGScheduler(SelectionDAGISel *IS,
  555. SelectionDAG *DAG,
  556. const TargetMachine *TM,
  557. MachineBasicBlock *BB, bool) {
  558. return new ScheduleDAGFast(DAG, BB, *TM);
  559. }