ScheduleDAGFast.cpp 27 KB

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