ScheduleDAGFast.cpp 26 KB

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