ScheduleDAGFast.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. //===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This implements a fast scheduler.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "InstrEmitter.h"
  13. #include "ScheduleDAGSDNodes.h"
  14. #include "SDNodeDbgValue.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallSet.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/CodeGen/SchedulerRegistry.h"
  19. #include "llvm/CodeGen/SelectionDAGISel.h"
  20. #include "llvm/CodeGen/TargetInstrInfo.h"
  21. #include "llvm/CodeGen/TargetRegisterInfo.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/InlineAsm.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "pre-RA-sched"
  29. STATISTIC(NumUnfolds, "Number of nodes unfolded");
  30. STATISTIC(NumDups, "Number of duplicated nodes");
  31. STATISTIC(NumPRCopies, "Number of physical copies");
  32. static RegisterScheduler
  33. fastDAGScheduler("fast", "Fast suboptimal list scheduling",
  34. createFastDAGScheduler);
  35. static RegisterScheduler
  36. linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling",
  37. createDAGLinearizer);
  38. namespace {
  39. /// FastPriorityQueue - A degenerate priority queue that considers
  40. /// all nodes to have the same priority.
  41. ///
  42. struct FastPriorityQueue {
  43. SmallVector<SUnit *, 16> Queue;
  44. bool empty() const { return Queue.empty(); }
  45. void push(SUnit *U) {
  46. Queue.push_back(U);
  47. }
  48. SUnit *pop() {
  49. if (empty()) return nullptr;
  50. return Queue.pop_back_val();
  51. }
  52. };
  53. //===----------------------------------------------------------------------===//
  54. /// ScheduleDAGFast - The actual "fast" list scheduler implementation.
  55. ///
  56. class ScheduleDAGFast : public ScheduleDAGSDNodes {
  57. private:
  58. /// AvailableQueue - The priority queue to use for the available SUnits.
  59. FastPriorityQueue AvailableQueue;
  60. /// LiveRegDefs - A set of physical registers and their definition
  61. /// that are "live". These nodes must be scheduled before any other nodes that
  62. /// modifies the registers can be scheduled.
  63. unsigned NumLiveRegs;
  64. std::vector<SUnit*> LiveRegDefs;
  65. std::vector<unsigned> LiveRegCycles;
  66. public:
  67. ScheduleDAGFast(MachineFunction &mf)
  68. : ScheduleDAGSDNodes(mf) {}
  69. void Schedule() override;
  70. /// AddPred - adds a predecessor edge to SUnit SU.
  71. /// This returns true if this is a new predecessor.
  72. void AddPred(SUnit *SU, const SDep &D) {
  73. SU->addPred(D);
  74. }
  75. /// RemovePred - removes a predecessor edge from SUnit SU.
  76. /// This returns true if an edge was removed.
  77. void RemovePred(SUnit *SU, const SDep &D) {
  78. SU->removePred(D);
  79. }
  80. private:
  81. void ReleasePred(SUnit *SU, SDep *PredEdge);
  82. void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
  83. void ScheduleNodeBottomUp(SUnit*, unsigned);
  84. SUnit *CopyAndMoveSuccessors(SUnit*);
  85. void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
  86. const TargetRegisterClass*,
  87. const TargetRegisterClass*,
  88. SmallVectorImpl<SUnit*>&);
  89. bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
  90. void ListScheduleBottomUp();
  91. /// forceUnitLatencies - The fast scheduler doesn't care about real latencies.
  92. bool forceUnitLatencies() const override { return true; }
  93. };
  94. } // end anonymous namespace
  95. /// Schedule - Schedule the DAG using list scheduling.
  96. void ScheduleDAGFast::Schedule() {
  97. LLVM_DEBUG(dbgs() << "********** List Scheduling **********\n");
  98. NumLiveRegs = 0;
  99. LiveRegDefs.resize(TRI->getNumRegs(), nullptr);
  100. LiveRegCycles.resize(TRI->getNumRegs(), 0);
  101. // Build the scheduling graph.
  102. BuildSchedGraph(nullptr);
  103. LLVM_DEBUG(dump());
  104. // Execute the actual scheduling loop.
  105. ListScheduleBottomUp();
  106. }
  107. //===----------------------------------------------------------------------===//
  108. // Bottom-Up Scheduling
  109. //===----------------------------------------------------------------------===//
  110. /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
  111. /// the AvailableQueue if the count reaches zero. Also update its cycle bound.
  112. void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
  113. SUnit *PredSU = PredEdge->getSUnit();
  114. #ifndef NDEBUG
  115. if (PredSU->NumSuccsLeft == 0) {
  116. dbgs() << "*** Scheduling failed! ***\n";
  117. dumpNode(*PredSU);
  118. dbgs() << " has been released too many times!\n";
  119. llvm_unreachable(nullptr);
  120. }
  121. #endif
  122. --PredSU->NumSuccsLeft;
  123. // If all the node's successors are scheduled, this node is ready
  124. // to be scheduled. Ignore the special EntrySU node.
  125. if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
  126. PredSU->isAvailable = true;
  127. AvailableQueue.push(PredSU);
  128. }
  129. }
  130. void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
  131. // Bottom up: release predecessors
  132. for (SDep &Pred : SU->Preds) {
  133. ReleasePred(SU, &Pred);
  134. if (Pred.isAssignedRegDep()) {
  135. // This is a physical register dependency and it's impossible or
  136. // expensive to copy the register. Make sure nothing that can
  137. // clobber the register is scheduled between the predecessor and
  138. // this node.
  139. if (!LiveRegDefs[Pred.getReg()]) {
  140. ++NumLiveRegs;
  141. LiveRegDefs[Pred.getReg()] = Pred.getSUnit();
  142. LiveRegCycles[Pred.getReg()] = CurCycle;
  143. }
  144. }
  145. }
  146. }
  147. /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
  148. /// count of its predecessors. If a predecessor pending count is zero, add it to
  149. /// the Available queue.
  150. void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
  151. LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
  152. LLVM_DEBUG(dumpNode(*SU));
  153. assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
  154. SU->setHeightToAtLeast(CurCycle);
  155. Sequence.push_back(SU);
  156. ReleasePredecessors(SU, CurCycle);
  157. // Release all the implicit physical register defs that are live.
  158. for (SDep &Succ : SU->Succs) {
  159. if (Succ.isAssignedRegDep()) {
  160. if (LiveRegCycles[Succ.getReg()] == Succ.getSUnit()->getHeight()) {
  161. assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
  162. assert(LiveRegDefs[Succ.getReg()] == SU &&
  163. "Physical register dependency violated?");
  164. --NumLiveRegs;
  165. LiveRegDefs[Succ.getReg()] = nullptr;
  166. LiveRegCycles[Succ.getReg()] = 0;
  167. }
  168. }
  169. }
  170. SU->isScheduled = true;
  171. }
  172. /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
  173. /// successors to the newly created node.
  174. SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
  175. if (SU->getNode()->getGluedNode())
  176. return nullptr;
  177. SDNode *N = SU->getNode();
  178. if (!N)
  179. return nullptr;
  180. SUnit *NewSU;
  181. bool TryUnfold = false;
  182. for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
  183. MVT VT = N->getSimpleValueType(i);
  184. if (VT == MVT::Glue)
  185. return nullptr;
  186. else if (VT == MVT::Other)
  187. TryUnfold = true;
  188. }
  189. for (const SDValue &Op : N->op_values()) {
  190. MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
  191. if (VT == MVT::Glue)
  192. return nullptr;
  193. }
  194. if (TryUnfold) {
  195. SmallVector<SDNode*, 2> NewNodes;
  196. if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
  197. return nullptr;
  198. LLVM_DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
  199. assert(NewNodes.size() == 2 && "Expected a load folding node!");
  200. N = NewNodes[1];
  201. SDNode *LoadNode = NewNodes[0];
  202. unsigned NumVals = N->getNumValues();
  203. unsigned OldNumVals = SU->getNode()->getNumValues();
  204. for (unsigned i = 0; i != NumVals; ++i)
  205. DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
  206. DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
  207. SDValue(LoadNode, 1));
  208. SUnit *NewSU = newSUnit(N);
  209. assert(N->getNodeId() == -1 && "Node already inserted!");
  210. N->setNodeId(NewSU->NodeNum);
  211. const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
  212. for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
  213. if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
  214. NewSU->isTwoAddress = true;
  215. break;
  216. }
  217. }
  218. if (MCID.isCommutable())
  219. NewSU->isCommutable = true;
  220. // LoadNode may already exist. This can happen when there is another
  221. // load from the same location and producing the same type of value
  222. // but it has different alignment or volatileness.
  223. bool isNewLoad = true;
  224. SUnit *LoadSU;
  225. if (LoadNode->getNodeId() != -1) {
  226. LoadSU = &SUnits[LoadNode->getNodeId()];
  227. isNewLoad = false;
  228. } else {
  229. LoadSU = newSUnit(LoadNode);
  230. LoadNode->setNodeId(LoadSU->NodeNum);
  231. }
  232. SDep ChainPred;
  233. SmallVector<SDep, 4> ChainSuccs;
  234. SmallVector<SDep, 4> LoadPreds;
  235. SmallVector<SDep, 4> NodePreds;
  236. SmallVector<SDep, 4> NodeSuccs;
  237. for (SDep &Pred : SU->Preds) {
  238. if (Pred.isCtrl())
  239. ChainPred = Pred;
  240. else if (Pred.getSUnit()->getNode() &&
  241. Pred.getSUnit()->getNode()->isOperandOf(LoadNode))
  242. LoadPreds.push_back(Pred);
  243. else
  244. NodePreds.push_back(Pred);
  245. }
  246. for (SDep &Succ : SU->Succs) {
  247. if (Succ.isCtrl())
  248. ChainSuccs.push_back(Succ);
  249. else
  250. NodeSuccs.push_back(Succ);
  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. SDep D(LoadSU, SDep::Barrier);
  289. D.setLatency(LoadSU->Latency);
  290. AddPred(NewSU, D);
  291. }
  292. ++NumUnfolds;
  293. if (NewSU->NumSuccsLeft == 0) {
  294. NewSU->isAvailable = true;
  295. return NewSU;
  296. }
  297. SU = NewSU;
  298. }
  299. LLVM_DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
  300. NewSU = Clone(SU);
  301. // New SUnit has the exact same predecessors.
  302. for (SDep &Pred : SU->Preds)
  303. if (!Pred.isArtificial())
  304. AddPred(NewSU, Pred);
  305. // Only copy scheduled successors. Cut them from old node's successor
  306. // list and move them over.
  307. SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
  308. for (SDep &Succ : SU->Succs) {
  309. if (Succ.isArtificial())
  310. continue;
  311. SUnit *SuccSU = Succ.getSUnit();
  312. if (SuccSU->isScheduled) {
  313. SDep D = Succ;
  314. D.setSUnit(NewSU);
  315. AddPred(SuccSU, D);
  316. D.setSUnit(SU);
  317. DelDeps.push_back(std::make_pair(SuccSU, D));
  318. }
  319. }
  320. for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
  321. RemovePred(DelDeps[i].first, DelDeps[i].second);
  322. ++NumDups;
  323. return NewSU;
  324. }
  325. /// InsertCopiesAndMoveSuccs - Insert register copies and move all
  326. /// scheduled successors of the given SUnit to the last copy.
  327. void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
  328. const TargetRegisterClass *DestRC,
  329. const TargetRegisterClass *SrcRC,
  330. SmallVectorImpl<SUnit*> &Copies) {
  331. SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr));
  332. CopyFromSU->CopySrcRC = SrcRC;
  333. CopyFromSU->CopyDstRC = DestRC;
  334. SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr));
  335. CopyToSU->CopySrcRC = DestRC;
  336. CopyToSU->CopyDstRC = SrcRC;
  337. // Only copy scheduled successors. Cut them from old node's successor
  338. // list and move them over.
  339. SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
  340. for (SDep &Succ : SU->Succs) {
  341. if (Succ.isArtificial())
  342. continue;
  343. SUnit *SuccSU = Succ.getSUnit();
  344. if (SuccSU->isScheduled) {
  345. SDep D = Succ;
  346. D.setSUnit(CopyToSU);
  347. AddPred(SuccSU, D);
  348. DelDeps.push_back(std::make_pair(SuccSU, Succ));
  349. }
  350. }
  351. for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
  352. RemovePred(DelDeps[i].first, DelDeps[i].second);
  353. }
  354. SDep FromDep(SU, SDep::Data, Reg);
  355. FromDep.setLatency(SU->Latency);
  356. AddPred(CopyFromSU, FromDep);
  357. SDep ToDep(CopyFromSU, SDep::Data, 0);
  358. ToDep.setLatency(CopyFromSU->Latency);
  359. AddPred(CopyToSU, ToDep);
  360. Copies.push_back(CopyFromSU);
  361. Copies.push_back(CopyToSU);
  362. ++NumPRCopies;
  363. }
  364. /// getPhysicalRegisterVT - Returns the ValueType of the physical register
  365. /// definition of the specified node.
  366. /// FIXME: Move to SelectionDAG?
  367. static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
  368. const TargetInstrInfo *TII) {
  369. unsigned NumRes;
  370. if (N->getOpcode() == ISD::CopyFromReg) {
  371. // CopyFromReg has: "chain, Val, glue" so operand 1 gives the type.
  372. NumRes = 1;
  373. } else {
  374. const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
  375. assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
  376. NumRes = MCID.getNumDefs();
  377. for (const MCPhysReg *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
  378. if (Reg == *ImpDef)
  379. break;
  380. ++NumRes;
  381. }
  382. }
  383. return N->getSimpleValueType(NumRes);
  384. }
  385. /// CheckForLiveRegDef - Return true and update live register vector if the
  386. /// specified register def of the specified SUnit clobbers any "live" registers.
  387. static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
  388. std::vector<SUnit*> &LiveRegDefs,
  389. SmallSet<unsigned, 4> &RegAdded,
  390. SmallVectorImpl<unsigned> &LRegs,
  391. const TargetRegisterInfo *TRI) {
  392. bool Added = false;
  393. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  394. if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) {
  395. if (RegAdded.insert(*AI).second) {
  396. LRegs.push_back(*AI);
  397. Added = true;
  398. }
  399. }
  400. }
  401. return Added;
  402. }
  403. /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
  404. /// scheduling of the given node to satisfy live physical register dependencies.
  405. /// If the specific node is the last one that's available to schedule, do
  406. /// whatever is necessary (i.e. backtracking or cloning) to make it possible.
  407. bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
  408. SmallVectorImpl<unsigned> &LRegs){
  409. if (NumLiveRegs == 0)
  410. return false;
  411. SmallSet<unsigned, 4> RegAdded;
  412. // If this node would clobber any "live" register, then it's not ready.
  413. for (SDep &Pred : SU->Preds) {
  414. if (Pred.isAssignedRegDep()) {
  415. CheckForLiveRegDef(Pred.getSUnit(), Pred.getReg(), LiveRegDefs,
  416. RegAdded, LRegs, TRI);
  417. }
  418. }
  419. for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
  420. if (Node->getOpcode() == ISD::INLINEASM ||
  421. Node->getOpcode() == ISD::INLINEASM_BR) {
  422. // Inline asm can clobber physical defs.
  423. unsigned NumOps = Node->getNumOperands();
  424. if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
  425. --NumOps; // Ignore the glue operand.
  426. for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
  427. unsigned Flags =
  428. cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
  429. unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
  430. ++i; // Skip the ID value.
  431. if (InlineAsm::isRegDefKind(Flags) ||
  432. InlineAsm::isRegDefEarlyClobberKind(Flags) ||
  433. InlineAsm::isClobberKind(Flags)) {
  434. // Check for def of register or earlyclobber register.
  435. for (; NumVals; --NumVals, ++i) {
  436. unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
  437. if (Register::isPhysicalRegister(Reg))
  438. CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
  439. }
  440. } else
  441. i += NumVals;
  442. }
  443. continue;
  444. }
  445. if (!Node->isMachineOpcode())
  446. continue;
  447. const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
  448. if (!MCID.ImplicitDefs)
  449. continue;
  450. for (const MCPhysReg *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) {
  451. CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
  452. }
  453. }
  454. return !LRegs.empty();
  455. }
  456. /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
  457. /// schedulers.
  458. void ScheduleDAGFast::ListScheduleBottomUp() {
  459. unsigned CurCycle = 0;
  460. // Release any predecessors of the special Exit node.
  461. ReleasePredecessors(&ExitSU, CurCycle);
  462. // Add root to Available queue.
  463. if (!SUnits.empty()) {
  464. SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
  465. assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
  466. RootSU->isAvailable = true;
  467. AvailableQueue.push(RootSU);
  468. }
  469. // While Available queue is not empty, grab the node with the highest
  470. // priority. If it is not ready put it back. Schedule the node.
  471. SmallVector<SUnit*, 4> NotReady;
  472. DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
  473. Sequence.reserve(SUnits.size());
  474. while (!AvailableQueue.empty()) {
  475. bool Delayed = false;
  476. LRegsMap.clear();
  477. SUnit *CurSU = AvailableQueue.pop();
  478. while (CurSU) {
  479. SmallVector<unsigned, 4> LRegs;
  480. if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
  481. break;
  482. Delayed = true;
  483. LRegsMap.insert(std::make_pair(CurSU, LRegs));
  484. CurSU->isPending = true; // This SU is not in AvailableQueue right now.
  485. NotReady.push_back(CurSU);
  486. CurSU = AvailableQueue.pop();
  487. }
  488. // All candidates are delayed due to live physical reg dependencies.
  489. // Try code duplication or inserting cross class copies
  490. // to resolve it.
  491. if (Delayed && !CurSU) {
  492. if (!CurSU) {
  493. // Try duplicating the nodes that produces these
  494. // "expensive to copy" values to break the dependency. In case even
  495. // that doesn't work, insert cross class copies.
  496. SUnit *TrySU = NotReady[0];
  497. SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
  498. assert(LRegs.size() == 1 && "Can't handle this yet!");
  499. unsigned Reg = LRegs[0];
  500. SUnit *LRDef = LiveRegDefs[Reg];
  501. MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
  502. const TargetRegisterClass *RC =
  503. TRI->getMinimalPhysRegClass(Reg, VT);
  504. const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
  505. // If cross copy register class is the same as RC, then it must be
  506. // possible copy the value directly. Do not try duplicate the def.
  507. // If cross copy register class is not the same as RC, then it's
  508. // possible to copy the value but it require cross register class copies
  509. // and it is expensive.
  510. // If cross copy register class is null, then it's not possible to copy
  511. // the value at all.
  512. SUnit *NewDef = nullptr;
  513. if (DestRC != RC) {
  514. NewDef = CopyAndMoveSuccessors(LRDef);
  515. if (!DestRC && !NewDef)
  516. report_fatal_error("Can't handle live physical "
  517. "register dependency!");
  518. }
  519. if (!NewDef) {
  520. // Issue copies, these can be expensive cross register class copies.
  521. SmallVector<SUnit*, 2> Copies;
  522. InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
  523. LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum
  524. << " to SU #" << Copies.front()->NodeNum << "\n");
  525. AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
  526. NewDef = Copies.back();
  527. }
  528. LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum
  529. << " to SU #" << TrySU->NodeNum << "\n");
  530. LiveRegDefs[Reg] = NewDef;
  531. AddPred(NewDef, SDep(TrySU, SDep::Artificial));
  532. TrySU->isAvailable = false;
  533. CurSU = NewDef;
  534. }
  535. if (!CurSU) {
  536. llvm_unreachable("Unable to resolve live physical register dependencies!");
  537. }
  538. }
  539. // Add the nodes that aren't ready back onto the available list.
  540. for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
  541. NotReady[i]->isPending = false;
  542. // May no longer be available due to backtracking.
  543. if (NotReady[i]->isAvailable)
  544. AvailableQueue.push(NotReady[i]);
  545. }
  546. NotReady.clear();
  547. if (CurSU)
  548. ScheduleNodeBottomUp(CurSU, CurCycle);
  549. ++CurCycle;
  550. }
  551. // Reverse the order since it is bottom up.
  552. std::reverse(Sequence.begin(), Sequence.end());
  553. #ifndef NDEBUG
  554. VerifyScheduledSequence(/*isBottomUp=*/true);
  555. #endif
  556. }
  557. namespace {
  558. //===----------------------------------------------------------------------===//
  559. // ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the
  560. // DAG in topological order.
  561. // IMPORTANT: this may not work for targets with phyreg dependency.
  562. //
  563. class ScheduleDAGLinearize : public ScheduleDAGSDNodes {
  564. public:
  565. ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {}
  566. void Schedule() override;
  567. MachineBasicBlock *
  568. EmitSchedule(MachineBasicBlock::iterator &InsertPos) override;
  569. private:
  570. std::vector<SDNode*> Sequence;
  571. DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user
  572. void ScheduleNode(SDNode *N);
  573. };
  574. } // end anonymous namespace
  575. void ScheduleDAGLinearize::ScheduleNode(SDNode *N) {
  576. if (N->getNodeId() != 0)
  577. llvm_unreachable(nullptr);
  578. if (!N->isMachineOpcode() &&
  579. (N->getOpcode() == ISD::EntryToken || isPassiveNode(N)))
  580. // These nodes do not need to be translated into MIs.
  581. return;
  582. LLVM_DEBUG(dbgs() << "\n*** Scheduling: ");
  583. LLVM_DEBUG(N->dump(DAG));
  584. Sequence.push_back(N);
  585. unsigned NumOps = N->getNumOperands();
  586. if (unsigned NumLeft = NumOps) {
  587. SDNode *GluedOpN = nullptr;
  588. do {
  589. const SDValue &Op = N->getOperand(NumLeft-1);
  590. SDNode *OpN = Op.getNode();
  591. if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) {
  592. // Schedule glue operand right above N.
  593. GluedOpN = OpN;
  594. assert(OpN->getNodeId() != 0 && "Glue operand not ready?");
  595. OpN->setNodeId(0);
  596. ScheduleNode(OpN);
  597. continue;
  598. }
  599. if (OpN == GluedOpN)
  600. // Glue operand is already scheduled.
  601. continue;
  602. DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN);
  603. if (DI != GluedMap.end() && DI->second != N)
  604. // Users of glues are counted against the glued users.
  605. OpN = DI->second;
  606. unsigned Degree = OpN->getNodeId();
  607. assert(Degree > 0 && "Predecessor over-released!");
  608. OpN->setNodeId(--Degree);
  609. if (Degree == 0)
  610. ScheduleNode(OpN);
  611. } while (--NumLeft);
  612. }
  613. }
  614. /// findGluedUser - Find the representative use of a glue value by walking
  615. /// the use chain.
  616. static SDNode *findGluedUser(SDNode *N) {
  617. while (SDNode *Glued = N->getGluedUser())
  618. N = Glued;
  619. return N;
  620. }
  621. void ScheduleDAGLinearize::Schedule() {
  622. LLVM_DEBUG(dbgs() << "********** DAG Linearization **********\n");
  623. SmallVector<SDNode*, 8> Glues;
  624. unsigned DAGSize = 0;
  625. for (SDNode &Node : DAG->allnodes()) {
  626. SDNode *N = &Node;
  627. // Use node id to record degree.
  628. unsigned Degree = N->use_size();
  629. N->setNodeId(Degree);
  630. unsigned NumVals = N->getNumValues();
  631. if (NumVals && N->getValueType(NumVals-1) == MVT::Glue &&
  632. N->hasAnyUseOfValue(NumVals-1)) {
  633. SDNode *User = findGluedUser(N);
  634. if (User) {
  635. Glues.push_back(N);
  636. GluedMap.insert(std::make_pair(N, User));
  637. }
  638. }
  639. if (N->isMachineOpcode() ||
  640. (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N)))
  641. ++DAGSize;
  642. }
  643. for (unsigned i = 0, e = Glues.size(); i != e; ++i) {
  644. SDNode *Glue = Glues[i];
  645. SDNode *GUser = GluedMap[Glue];
  646. unsigned Degree = Glue->getNodeId();
  647. unsigned UDegree = GUser->getNodeId();
  648. // Glue user must be scheduled together with the glue operand. So other
  649. // users of the glue operand must be treated as its users.
  650. SDNode *ImmGUser = Glue->getGluedUser();
  651. for (const SDNode *U : Glue->uses())
  652. if (U == ImmGUser)
  653. --Degree;
  654. GUser->setNodeId(UDegree + Degree);
  655. Glue->setNodeId(1);
  656. }
  657. Sequence.reserve(DAGSize);
  658. ScheduleNode(DAG->getRoot().getNode());
  659. }
  660. MachineBasicBlock*
  661. ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
  662. InstrEmitter Emitter(DAG->getTarget(), BB, InsertPos,
  663. DAG->getUseInstrRefDebugInfo());
  664. DenseMap<SDValue, Register> VRBaseMap;
  665. LLVM_DEBUG({ dbgs() << "\n*** Final schedule ***\n"; });
  666. unsigned NumNodes = Sequence.size();
  667. MachineBasicBlock *BB = Emitter.getBlock();
  668. for (unsigned i = 0; i != NumNodes; ++i) {
  669. SDNode *N = Sequence[NumNodes-i-1];
  670. LLVM_DEBUG(N->dump(DAG));
  671. Emitter.EmitNode(N, false, false, VRBaseMap);
  672. // Emit any debug values associated with the node.
  673. if (N->getHasDebugValue()) {
  674. MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
  675. for (auto DV : DAG->GetDbgValues(N)) {
  676. if (!DV->isEmitted())
  677. if (auto *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap))
  678. BB->insert(InsertPos, DbgMI);
  679. }
  680. }
  681. }
  682. LLVM_DEBUG(dbgs() << '\n');
  683. InsertPos = Emitter.getInsertPos();
  684. return Emitter.getBlock();
  685. }
  686. //===----------------------------------------------------------------------===//
  687. // Public Constructor Functions
  688. //===----------------------------------------------------------------------===//
  689. llvm::ScheduleDAGSDNodes *
  690. llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
  691. return new ScheduleDAGFast(*IS->MF);
  692. }
  693. llvm::ScheduleDAGSDNodes *
  694. llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) {
  695. return new ScheduleDAGLinearize(*IS->MF);
  696. }