xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGFast.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements a fast scheduler.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "InstrEmitter.h"
140b57cec5SDimitry Andric #include "SDNodeDbgValue.h"
1581ad6265SDimitry Andric #include "ScheduleDAGSDNodes.h"
160b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
170b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/SchedulerRegistry.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGISel.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
220b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
230b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
250b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #define DEBUG_TYPE "pre-RA-sched"
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric STATISTIC(NumUnfolds,    "Number of nodes unfolded");
310b57cec5SDimitry Andric STATISTIC(NumDups,       "Number of duplicated nodes");
320b57cec5SDimitry Andric STATISTIC(NumPRCopies,   "Number of physical copies");
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric static RegisterScheduler
350b57cec5SDimitry Andric   fastDAGScheduler("fast", "Fast suboptimal list scheduling",
360b57cec5SDimitry Andric                    createFastDAGScheduler);
370b57cec5SDimitry Andric static RegisterScheduler
380b57cec5SDimitry Andric   linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling",
390b57cec5SDimitry Andric                         createDAGLinearizer);
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric namespace {
430b57cec5SDimitry Andric   /// FastPriorityQueue - A degenerate priority queue that considers
440b57cec5SDimitry Andric   /// all nodes to have the same priority.
450b57cec5SDimitry Andric   ///
460b57cec5SDimitry Andric   struct FastPriorityQueue {
470b57cec5SDimitry Andric     SmallVector<SUnit *, 16> Queue;
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric     bool empty() const { return Queue.empty(); }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric     void push(SUnit *U) {
520b57cec5SDimitry Andric       Queue.push_back(U);
530b57cec5SDimitry Andric     }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric     SUnit *pop() {
560b57cec5SDimitry Andric       if (empty()) return nullptr;
57349cc55cSDimitry Andric       return Queue.pop_back_val();
580b57cec5SDimitry Andric     }
590b57cec5SDimitry Andric   };
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
620b57cec5SDimitry Andric /// ScheduleDAGFast - The actual "fast" list scheduler implementation.
630b57cec5SDimitry Andric ///
640b57cec5SDimitry Andric class ScheduleDAGFast : public ScheduleDAGSDNodes {
650b57cec5SDimitry Andric private:
660b57cec5SDimitry Andric   /// AvailableQueue - The priority queue to use for the available SUnits.
670b57cec5SDimitry Andric   FastPriorityQueue AvailableQueue;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   /// LiveRegDefs - A set of physical registers and their definition
700b57cec5SDimitry Andric   /// that are "live". These nodes must be scheduled before any other nodes that
710b57cec5SDimitry Andric   /// modifies the registers can be scheduled.
7206c3fb27SDimitry Andric   unsigned NumLiveRegs = 0u;
730b57cec5SDimitry Andric   std::vector<SUnit*> LiveRegDefs;
740b57cec5SDimitry Andric   std::vector<unsigned> LiveRegCycles;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric public:
770b57cec5SDimitry Andric   ScheduleDAGFast(MachineFunction &mf)
780b57cec5SDimitry Andric     : ScheduleDAGSDNodes(mf) {}
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   void Schedule() override;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   /// AddPred - adds a predecessor edge to SUnit SU.
830b57cec5SDimitry Andric   /// This returns true if this is a new predecessor.
840b57cec5SDimitry Andric   void AddPred(SUnit *SU, const SDep &D) {
850b57cec5SDimitry Andric     SU->addPred(D);
860b57cec5SDimitry Andric   }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   /// RemovePred - removes a predecessor edge from SUnit SU.
890b57cec5SDimitry Andric   /// This returns true if an edge was removed.
900b57cec5SDimitry Andric   void RemovePred(SUnit *SU, const SDep &D) {
910b57cec5SDimitry Andric     SU->removePred(D);
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric private:
950b57cec5SDimitry Andric   void ReleasePred(SUnit *SU, SDep *PredEdge);
960b57cec5SDimitry Andric   void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
970b57cec5SDimitry Andric   void ScheduleNodeBottomUp(SUnit*, unsigned);
980b57cec5SDimitry Andric   SUnit *CopyAndMoveSuccessors(SUnit*);
990b57cec5SDimitry Andric   void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
1000b57cec5SDimitry Andric                                 const TargetRegisterClass*,
1010b57cec5SDimitry Andric                                 const TargetRegisterClass*,
1020b57cec5SDimitry Andric                                 SmallVectorImpl<SUnit*>&);
1030b57cec5SDimitry Andric   bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
1040b57cec5SDimitry Andric   void ListScheduleBottomUp();
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   /// forceUnitLatencies - The fast scheduler doesn't care about real latencies.
1070b57cec5SDimitry Andric   bool forceUnitLatencies() const override { return true; }
1080b57cec5SDimitry Andric };
1090b57cec5SDimitry Andric }  // end anonymous namespace
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric /// Schedule - Schedule the DAG using list scheduling.
1130b57cec5SDimitry Andric void ScheduleDAGFast::Schedule() {
1140b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** List Scheduling **********\n");
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   NumLiveRegs = 0;
1170b57cec5SDimitry Andric   LiveRegDefs.resize(TRI->getNumRegs(), nullptr);
1180b57cec5SDimitry Andric   LiveRegCycles.resize(TRI->getNumRegs(), 0);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // Build the scheduling graph.
1210b57cec5SDimitry Andric   BuildSchedGraph(nullptr);
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   LLVM_DEBUG(dump());
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   // Execute the actual scheduling loop.
1260b57cec5SDimitry Andric   ListScheduleBottomUp();
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1300b57cec5SDimitry Andric //  Bottom-Up Scheduling
1310b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
1340b57cec5SDimitry Andric /// the AvailableQueue if the count reaches zero. Also update its cycle bound.
1350b57cec5SDimitry Andric void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
1360b57cec5SDimitry Andric   SUnit *PredSU = PredEdge->getSUnit();
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric #ifndef NDEBUG
1390b57cec5SDimitry Andric   if (PredSU->NumSuccsLeft == 0) {
1400b57cec5SDimitry Andric     dbgs() << "*** Scheduling failed! ***\n";
1410b57cec5SDimitry Andric     dumpNode(*PredSU);
1420b57cec5SDimitry Andric     dbgs() << " has been released too many times!\n";
1430b57cec5SDimitry Andric     llvm_unreachable(nullptr);
1440b57cec5SDimitry Andric   }
1450b57cec5SDimitry Andric #endif
1460b57cec5SDimitry Andric   --PredSU->NumSuccsLeft;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   // If all the node's successors are scheduled, this node is ready
1490b57cec5SDimitry Andric   // to be scheduled. Ignore the special EntrySU node.
1500b57cec5SDimitry Andric   if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
1510b57cec5SDimitry Andric     PredSU->isAvailable = true;
1520b57cec5SDimitry Andric     AvailableQueue.push(PredSU);
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
1570b57cec5SDimitry Andric   // Bottom up: release predecessors
1580b57cec5SDimitry Andric   for (SDep &Pred : SU->Preds) {
1590b57cec5SDimitry Andric     ReleasePred(SU, &Pred);
1600b57cec5SDimitry Andric     if (Pred.isAssignedRegDep()) {
1610b57cec5SDimitry Andric       // This is a physical register dependency and it's impossible or
1620b57cec5SDimitry Andric       // expensive to copy the register. Make sure nothing that can
1630b57cec5SDimitry Andric       // clobber the register is scheduled between the predecessor and
1640b57cec5SDimitry Andric       // this node.
1650b57cec5SDimitry Andric       if (!LiveRegDefs[Pred.getReg()]) {
1660b57cec5SDimitry Andric         ++NumLiveRegs;
1670b57cec5SDimitry Andric         LiveRegDefs[Pred.getReg()] = Pred.getSUnit();
1680b57cec5SDimitry Andric         LiveRegCycles[Pred.getReg()] = CurCycle;
1690b57cec5SDimitry Andric       }
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
1750b57cec5SDimitry Andric /// count of its predecessors. If a predecessor pending count is zero, add it to
1760b57cec5SDimitry Andric /// the Available queue.
1770b57cec5SDimitry Andric void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
1780b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
1790b57cec5SDimitry Andric   LLVM_DEBUG(dumpNode(*SU));
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
1820b57cec5SDimitry Andric   SU->setHeightToAtLeast(CurCycle);
1830b57cec5SDimitry Andric   Sequence.push_back(SU);
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   ReleasePredecessors(SU, CurCycle);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   // Release all the implicit physical register defs that are live.
1880b57cec5SDimitry Andric   for (SDep &Succ : SU->Succs) {
1890b57cec5SDimitry Andric     if (Succ.isAssignedRegDep()) {
1900b57cec5SDimitry Andric       if (LiveRegCycles[Succ.getReg()] == Succ.getSUnit()->getHeight()) {
1910b57cec5SDimitry Andric         assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
1920b57cec5SDimitry Andric         assert(LiveRegDefs[Succ.getReg()] == SU &&
1930b57cec5SDimitry Andric                "Physical register dependency violated?");
1940b57cec5SDimitry Andric         --NumLiveRegs;
1950b57cec5SDimitry Andric         LiveRegDefs[Succ.getReg()] = nullptr;
1960b57cec5SDimitry Andric         LiveRegCycles[Succ.getReg()] = 0;
1970b57cec5SDimitry Andric       }
1980b57cec5SDimitry Andric     }
1990b57cec5SDimitry Andric   }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   SU->isScheduled = true;
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
2050b57cec5SDimitry Andric /// successors to the newly created node.
2060b57cec5SDimitry Andric SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
2070b57cec5SDimitry Andric   if (SU->getNode()->getGluedNode())
2080b57cec5SDimitry Andric     return nullptr;
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   SDNode *N = SU->getNode();
2110b57cec5SDimitry Andric   if (!N)
2120b57cec5SDimitry Andric     return nullptr;
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   SUnit *NewSU;
2150b57cec5SDimitry Andric   bool TryUnfold = false;
2160b57cec5SDimitry Andric   for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
2170b57cec5SDimitry Andric     MVT VT = N->getSimpleValueType(i);
2180b57cec5SDimitry Andric     if (VT == MVT::Glue)
2190b57cec5SDimitry Andric       return nullptr;
2200b57cec5SDimitry Andric     else if (VT == MVT::Other)
2210b57cec5SDimitry Andric       TryUnfold = true;
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric   for (const SDValue &Op : N->op_values()) {
2240b57cec5SDimitry Andric     MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
2250b57cec5SDimitry Andric     if (VT == MVT::Glue)
2260b57cec5SDimitry Andric       return nullptr;
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   if (TryUnfold) {
2300b57cec5SDimitry Andric     SmallVector<SDNode*, 2> NewNodes;
2310b57cec5SDimitry Andric     if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
2320b57cec5SDimitry Andric       return nullptr;
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
2350b57cec5SDimitry Andric     assert(NewNodes.size() == 2 && "Expected a load folding node!");
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric     N = NewNodes[1];
2380b57cec5SDimitry Andric     SDNode *LoadNode = NewNodes[0];
2390b57cec5SDimitry Andric     unsigned NumVals = N->getNumValues();
2400b57cec5SDimitry Andric     unsigned OldNumVals = SU->getNode()->getNumValues();
2410b57cec5SDimitry Andric     for (unsigned i = 0; i != NumVals; ++i)
2420b57cec5SDimitry Andric       DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
2430b57cec5SDimitry Andric     DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
2440b57cec5SDimitry Andric                                    SDValue(LoadNode, 1));
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric     SUnit *NewSU = newSUnit(N);
2470b57cec5SDimitry Andric     assert(N->getNodeId() == -1 && "Node already inserted!");
2480b57cec5SDimitry Andric     N->setNodeId(NewSU->NodeNum);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric     const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
2510b57cec5SDimitry Andric     for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
2520b57cec5SDimitry Andric       if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
2530b57cec5SDimitry Andric         NewSU->isTwoAddress = true;
2540b57cec5SDimitry Andric         break;
2550b57cec5SDimitry Andric       }
2560b57cec5SDimitry Andric     }
2570b57cec5SDimitry Andric     if (MCID.isCommutable())
2580b57cec5SDimitry Andric       NewSU->isCommutable = true;
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric     // LoadNode may already exist. This can happen when there is another
2610b57cec5SDimitry Andric     // load from the same location and producing the same type of value
2620b57cec5SDimitry Andric     // but it has different alignment or volatileness.
2630b57cec5SDimitry Andric     bool isNewLoad = true;
2640b57cec5SDimitry Andric     SUnit *LoadSU;
2650b57cec5SDimitry Andric     if (LoadNode->getNodeId() != -1) {
2660b57cec5SDimitry Andric       LoadSU = &SUnits[LoadNode->getNodeId()];
2670b57cec5SDimitry Andric       isNewLoad = false;
2680b57cec5SDimitry Andric     } else {
2690b57cec5SDimitry Andric       LoadSU = newSUnit(LoadNode);
2700b57cec5SDimitry Andric       LoadNode->setNodeId(LoadSU->NodeNum);
2710b57cec5SDimitry Andric     }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric     SDep ChainPred;
2740b57cec5SDimitry Andric     SmallVector<SDep, 4> ChainSuccs;
2750b57cec5SDimitry Andric     SmallVector<SDep, 4> LoadPreds;
2760b57cec5SDimitry Andric     SmallVector<SDep, 4> NodePreds;
2770b57cec5SDimitry Andric     SmallVector<SDep, 4> NodeSuccs;
2780b57cec5SDimitry Andric     for (SDep &Pred : SU->Preds) {
2790b57cec5SDimitry Andric       if (Pred.isCtrl())
2800b57cec5SDimitry Andric         ChainPred = Pred;
2810b57cec5SDimitry Andric       else if (Pred.getSUnit()->getNode() &&
2820b57cec5SDimitry Andric                Pred.getSUnit()->getNode()->isOperandOf(LoadNode))
2830b57cec5SDimitry Andric         LoadPreds.push_back(Pred);
2840b57cec5SDimitry Andric       else
2850b57cec5SDimitry Andric         NodePreds.push_back(Pred);
2860b57cec5SDimitry Andric     }
2870b57cec5SDimitry Andric     for (SDep &Succ : SU->Succs) {
2880b57cec5SDimitry Andric       if (Succ.isCtrl())
2890b57cec5SDimitry Andric         ChainSuccs.push_back(Succ);
2900b57cec5SDimitry Andric       else
2910b57cec5SDimitry Andric         NodeSuccs.push_back(Succ);
2920b57cec5SDimitry Andric     }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric     if (ChainPred.getSUnit()) {
2950b57cec5SDimitry Andric       RemovePred(SU, ChainPred);
2960b57cec5SDimitry Andric       if (isNewLoad)
2970b57cec5SDimitry Andric         AddPred(LoadSU, ChainPred);
2980b57cec5SDimitry Andric     }
299cb14a3feSDimitry Andric     for (const SDep &Pred : LoadPreds) {
3000b57cec5SDimitry Andric       RemovePred(SU, Pred);
3010b57cec5SDimitry Andric       if (isNewLoad) {
3020b57cec5SDimitry Andric         AddPred(LoadSU, Pred);
3030b57cec5SDimitry Andric       }
3040b57cec5SDimitry Andric     }
305cb14a3feSDimitry Andric     for (const SDep &Pred : NodePreds) {
3060b57cec5SDimitry Andric       RemovePred(SU, Pred);
3070b57cec5SDimitry Andric       AddPred(NewSU, Pred);
3080b57cec5SDimitry Andric     }
309cb14a3feSDimitry Andric     for (SDep D : NodeSuccs) {
3100b57cec5SDimitry Andric       SUnit *SuccDep = D.getSUnit();
3110b57cec5SDimitry Andric       D.setSUnit(SU);
3120b57cec5SDimitry Andric       RemovePred(SuccDep, D);
3130b57cec5SDimitry Andric       D.setSUnit(NewSU);
3140b57cec5SDimitry Andric       AddPred(SuccDep, D);
3150b57cec5SDimitry Andric     }
316cb14a3feSDimitry Andric     for (SDep D : ChainSuccs) {
3170b57cec5SDimitry Andric       SUnit *SuccDep = D.getSUnit();
3180b57cec5SDimitry Andric       D.setSUnit(SU);
3190b57cec5SDimitry Andric       RemovePred(SuccDep, D);
3200b57cec5SDimitry Andric       if (isNewLoad) {
3210b57cec5SDimitry Andric         D.setSUnit(LoadSU);
3220b57cec5SDimitry Andric         AddPred(SuccDep, D);
3230b57cec5SDimitry Andric       }
3240b57cec5SDimitry Andric     }
3250b57cec5SDimitry Andric     if (isNewLoad) {
3260b57cec5SDimitry Andric       SDep D(LoadSU, SDep::Barrier);
3270b57cec5SDimitry Andric       D.setLatency(LoadSU->Latency);
3280b57cec5SDimitry Andric       AddPred(NewSU, D);
3290b57cec5SDimitry Andric     }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric     ++NumUnfolds;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric     if (NewSU->NumSuccsLeft == 0) {
3340b57cec5SDimitry Andric       NewSU->isAvailable = true;
3350b57cec5SDimitry Andric       return NewSU;
3360b57cec5SDimitry Andric     }
3370b57cec5SDimitry Andric     SU = NewSU;
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
3410b57cec5SDimitry Andric   NewSU = Clone(SU);
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   // New SUnit has the exact same predecessors.
3440b57cec5SDimitry Andric   for (SDep &Pred : SU->Preds)
3450b57cec5SDimitry Andric     if (!Pred.isArtificial())
3460b57cec5SDimitry Andric       AddPred(NewSU, Pred);
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // Only copy scheduled successors. Cut them from old node's successor
3490b57cec5SDimitry Andric   // list and move them over.
3500b57cec5SDimitry Andric   SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
3510b57cec5SDimitry Andric   for (SDep &Succ : SU->Succs) {
3520b57cec5SDimitry Andric     if (Succ.isArtificial())
3530b57cec5SDimitry Andric       continue;
3540b57cec5SDimitry Andric     SUnit *SuccSU = Succ.getSUnit();
3550b57cec5SDimitry Andric     if (SuccSU->isScheduled) {
3560b57cec5SDimitry Andric       SDep D = Succ;
3570b57cec5SDimitry Andric       D.setSUnit(NewSU);
3580b57cec5SDimitry Andric       AddPred(SuccSU, D);
3590b57cec5SDimitry Andric       D.setSUnit(SU);
3600b57cec5SDimitry Andric       DelDeps.push_back(std::make_pair(SuccSU, D));
3610b57cec5SDimitry Andric     }
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric   for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
3640b57cec5SDimitry Andric     RemovePred(DelDeps[i].first, DelDeps[i].second);
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   ++NumDups;
3670b57cec5SDimitry Andric   return NewSU;
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric /// InsertCopiesAndMoveSuccs - Insert register copies and move all
3710b57cec5SDimitry Andric /// scheduled successors of the given SUnit to the last copy.
3720b57cec5SDimitry Andric void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
3730b57cec5SDimitry Andric                                               const TargetRegisterClass *DestRC,
3740b57cec5SDimitry Andric                                               const TargetRegisterClass *SrcRC,
3750b57cec5SDimitry Andric                                               SmallVectorImpl<SUnit*> &Copies) {
3760b57cec5SDimitry Andric   SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr));
3770b57cec5SDimitry Andric   CopyFromSU->CopySrcRC = SrcRC;
3780b57cec5SDimitry Andric   CopyFromSU->CopyDstRC = DestRC;
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr));
3810b57cec5SDimitry Andric   CopyToSU->CopySrcRC = DestRC;
3820b57cec5SDimitry Andric   CopyToSU->CopyDstRC = SrcRC;
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   // Only copy scheduled successors. Cut them from old node's successor
3850b57cec5SDimitry Andric   // list and move them over.
3860b57cec5SDimitry Andric   SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
3870b57cec5SDimitry Andric   for (SDep &Succ : SU->Succs) {
3880b57cec5SDimitry Andric     if (Succ.isArtificial())
3890b57cec5SDimitry Andric       continue;
3900b57cec5SDimitry Andric     SUnit *SuccSU = Succ.getSUnit();
3910b57cec5SDimitry Andric     if (SuccSU->isScheduled) {
3920b57cec5SDimitry Andric       SDep D = Succ;
3930b57cec5SDimitry Andric       D.setSUnit(CopyToSU);
3940b57cec5SDimitry Andric       AddPred(SuccSU, D);
3950b57cec5SDimitry Andric       DelDeps.push_back(std::make_pair(SuccSU, Succ));
3960b57cec5SDimitry Andric     }
3970b57cec5SDimitry Andric   }
3980b57cec5SDimitry Andric   for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
3990b57cec5SDimitry Andric     RemovePred(DelDeps[i].first, DelDeps[i].second);
4000b57cec5SDimitry Andric   }
4010b57cec5SDimitry Andric   SDep FromDep(SU, SDep::Data, Reg);
4020b57cec5SDimitry Andric   FromDep.setLatency(SU->Latency);
4030b57cec5SDimitry Andric   AddPred(CopyFromSU, FromDep);
4040b57cec5SDimitry Andric   SDep ToDep(CopyFromSU, SDep::Data, 0);
4050b57cec5SDimitry Andric   ToDep.setLatency(CopyFromSU->Latency);
4060b57cec5SDimitry Andric   AddPred(CopyToSU, ToDep);
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   Copies.push_back(CopyFromSU);
4090b57cec5SDimitry Andric   Copies.push_back(CopyToSU);
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   ++NumPRCopies;
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric /// getPhysicalRegisterVT - Returns the ValueType of the physical register
4150b57cec5SDimitry Andric /// definition of the specified node.
4160b57cec5SDimitry Andric /// FIXME: Move to SelectionDAG?
4170b57cec5SDimitry Andric static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
4180b57cec5SDimitry Andric                                  const TargetInstrInfo *TII) {
4190b57cec5SDimitry Andric   unsigned NumRes;
4200b57cec5SDimitry Andric   if (N->getOpcode() == ISD::CopyFromReg) {
4210b57cec5SDimitry Andric     // CopyFromReg has: "chain, Val, glue" so operand 1 gives the type.
4220b57cec5SDimitry Andric     NumRes = 1;
4230b57cec5SDimitry Andric   } else {
4240b57cec5SDimitry Andric     const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
425bdd1243dSDimitry Andric     assert(!MCID.implicit_defs().empty() &&
426bdd1243dSDimitry Andric            "Physical reg def must be in implicit def list!");
4270b57cec5SDimitry Andric     NumRes = MCID.getNumDefs();
428bdd1243dSDimitry Andric     for (MCPhysReg ImpDef : MCID.implicit_defs()) {
429bdd1243dSDimitry Andric       if (Reg == ImpDef)
4300b57cec5SDimitry Andric         break;
4310b57cec5SDimitry Andric       ++NumRes;
4320b57cec5SDimitry Andric     }
4330b57cec5SDimitry Andric   }
4340b57cec5SDimitry Andric   return N->getSimpleValueType(NumRes);
4350b57cec5SDimitry Andric }
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric /// CheckForLiveRegDef - Return true and update live register vector if the
4380b57cec5SDimitry Andric /// specified register def of the specified SUnit clobbers any "live" registers.
4390b57cec5SDimitry Andric static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
4400b57cec5SDimitry Andric                                std::vector<SUnit *> &LiveRegDefs,
4410b57cec5SDimitry Andric                                SmallSet<unsigned, 4> &RegAdded,
4420b57cec5SDimitry Andric                                SmallVectorImpl<unsigned> &LRegs,
44381ad6265SDimitry Andric                                const TargetRegisterInfo *TRI,
44481ad6265SDimitry Andric                                const SDNode *Node = nullptr) {
4450b57cec5SDimitry Andric   bool Added = false;
4460b57cec5SDimitry Andric   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
44781ad6265SDimitry Andric     // Check if Ref is live.
44881ad6265SDimitry Andric     if (!LiveRegDefs[*AI])
44981ad6265SDimitry Andric       continue;
45081ad6265SDimitry Andric 
45181ad6265SDimitry Andric     // Allow multiple uses of the same def.
45281ad6265SDimitry Andric     if (LiveRegDefs[*AI] == SU)
45381ad6265SDimitry Andric       continue;
45481ad6265SDimitry Andric 
45581ad6265SDimitry Andric     // Allow multiple uses of same def
45681ad6265SDimitry Andric     if (Node && LiveRegDefs[*AI]->getNode() == Node)
45781ad6265SDimitry Andric       continue;
45881ad6265SDimitry Andric 
45981ad6265SDimitry Andric     // Add Reg to the set of interfering live regs.
4600b57cec5SDimitry Andric     if (RegAdded.insert(*AI).second) {
4610b57cec5SDimitry Andric       LRegs.push_back(*AI);
4620b57cec5SDimitry Andric       Added = true;
4630b57cec5SDimitry Andric     }
4640b57cec5SDimitry Andric   }
4650b57cec5SDimitry Andric   return Added;
4660b57cec5SDimitry Andric }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
4690b57cec5SDimitry Andric /// scheduling of the given node to satisfy live physical register dependencies.
4700b57cec5SDimitry Andric /// If the specific node is the last one that's available to schedule, do
4710b57cec5SDimitry Andric /// whatever is necessary (i.e. backtracking or cloning) to make it possible.
4720b57cec5SDimitry Andric bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
4730b57cec5SDimitry Andric                                               SmallVectorImpl<unsigned> &LRegs){
4740b57cec5SDimitry Andric   if (NumLiveRegs == 0)
4750b57cec5SDimitry Andric     return false;
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   SmallSet<unsigned, 4> RegAdded;
4780b57cec5SDimitry Andric   // If this node would clobber any "live" register, then it's not ready.
4790b57cec5SDimitry Andric   for (SDep &Pred : SU->Preds) {
4800b57cec5SDimitry Andric     if (Pred.isAssignedRegDep()) {
4810b57cec5SDimitry Andric       CheckForLiveRegDef(Pred.getSUnit(), Pred.getReg(), LiveRegDefs,
4820b57cec5SDimitry Andric                          RegAdded, LRegs, TRI);
4830b57cec5SDimitry Andric     }
4840b57cec5SDimitry Andric   }
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
4870b57cec5SDimitry Andric     if (Node->getOpcode() == ISD::INLINEASM ||
4880b57cec5SDimitry Andric         Node->getOpcode() == ISD::INLINEASM_BR) {
4890b57cec5SDimitry Andric       // Inline asm can clobber physical defs.
4900b57cec5SDimitry Andric       unsigned NumOps = Node->getNumOperands();
4910b57cec5SDimitry Andric       if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
4920b57cec5SDimitry Andric         --NumOps;  // Ignore the glue operand.
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric       for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
495647cbc5dSDimitry Andric         unsigned Flags = Node->getConstantOperandVal(i);
4965f757f3fSDimitry Andric         const InlineAsm::Flag F(Flags);
4975f757f3fSDimitry Andric         unsigned NumVals = F.getNumOperandRegisters();
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric         ++i; // Skip the ID value.
5005f757f3fSDimitry Andric         if (F.isRegDefKind() || F.isRegDefEarlyClobberKind() ||
5015f757f3fSDimitry Andric             F.isClobberKind()) {
5020b57cec5SDimitry Andric           // Check for def of register or earlyclobber register.
5030b57cec5SDimitry Andric           for (; NumVals; --NumVals, ++i) {
5040b57cec5SDimitry Andric             unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
5058bcb0991SDimitry Andric             if (Register::isPhysicalRegister(Reg))
5060b57cec5SDimitry Andric               CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
5070b57cec5SDimitry Andric           }
5080b57cec5SDimitry Andric         } else
5090b57cec5SDimitry Andric           i += NumVals;
5100b57cec5SDimitry Andric       }
5110b57cec5SDimitry Andric       continue;
5120b57cec5SDimitry Andric     }
51381ad6265SDimitry Andric 
51481ad6265SDimitry Andric     if (Node->getOpcode() == ISD::CopyToReg) {
51581ad6265SDimitry Andric       Register Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
51681ad6265SDimitry Andric       if (Reg.isPhysical()) {
51781ad6265SDimitry Andric         SDNode *SrcNode = Node->getOperand(2).getNode();
51881ad6265SDimitry Andric         CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI, SrcNode);
51981ad6265SDimitry Andric       }
52081ad6265SDimitry Andric     }
52181ad6265SDimitry Andric 
5220b57cec5SDimitry Andric     if (!Node->isMachineOpcode())
5230b57cec5SDimitry Andric       continue;
5240b57cec5SDimitry Andric     const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
525bdd1243dSDimitry Andric     for (MCPhysReg Reg : MCID.implicit_defs())
526bdd1243dSDimitry Andric       CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
5270b57cec5SDimitry Andric   }
5280b57cec5SDimitry Andric   return !LRegs.empty();
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
5330b57cec5SDimitry Andric /// schedulers.
5340b57cec5SDimitry Andric void ScheduleDAGFast::ListScheduleBottomUp() {
5350b57cec5SDimitry Andric   unsigned CurCycle = 0;
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric   // Release any predecessors of the special Exit node.
5380b57cec5SDimitry Andric   ReleasePredecessors(&ExitSU, CurCycle);
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   // Add root to Available queue.
5410b57cec5SDimitry Andric   if (!SUnits.empty()) {
5420b57cec5SDimitry Andric     SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
5430b57cec5SDimitry Andric     assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
5440b57cec5SDimitry Andric     RootSU->isAvailable = true;
5450b57cec5SDimitry Andric     AvailableQueue.push(RootSU);
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   // While Available queue is not empty, grab the node with the highest
5490b57cec5SDimitry Andric   // priority. If it is not ready put it back.  Schedule the node.
5500b57cec5SDimitry Andric   SmallVector<SUnit*, 4> NotReady;
5510b57cec5SDimitry Andric   DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
5520b57cec5SDimitry Andric   Sequence.reserve(SUnits.size());
5530b57cec5SDimitry Andric   while (!AvailableQueue.empty()) {
5540b57cec5SDimitry Andric     bool Delayed = false;
5550b57cec5SDimitry Andric     LRegsMap.clear();
5560b57cec5SDimitry Andric     SUnit *CurSU = AvailableQueue.pop();
5570b57cec5SDimitry Andric     while (CurSU) {
5580b57cec5SDimitry Andric       SmallVector<unsigned, 4> LRegs;
5590b57cec5SDimitry Andric       if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
5600b57cec5SDimitry Andric         break;
5610b57cec5SDimitry Andric       Delayed = true;
5620b57cec5SDimitry Andric       LRegsMap.insert(std::make_pair(CurSU, LRegs));
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric       CurSU->isPending = true;  // This SU is not in AvailableQueue right now.
5650b57cec5SDimitry Andric       NotReady.push_back(CurSU);
5660b57cec5SDimitry Andric       CurSU = AvailableQueue.pop();
5670b57cec5SDimitry Andric     }
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric     // All candidates are delayed due to live physical reg dependencies.
5700b57cec5SDimitry Andric     // Try code duplication or inserting cross class copies
5710b57cec5SDimitry Andric     // to resolve it.
5720b57cec5SDimitry Andric     if (Delayed && !CurSU) {
5730b57cec5SDimitry Andric       if (!CurSU) {
5740b57cec5SDimitry Andric         // Try duplicating the nodes that produces these
5750b57cec5SDimitry Andric         // "expensive to copy" values to break the dependency. In case even
5760b57cec5SDimitry Andric         // that doesn't work, insert cross class copies.
5770b57cec5SDimitry Andric         SUnit *TrySU = NotReady[0];
5780b57cec5SDimitry Andric         SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
5790b57cec5SDimitry Andric         assert(LRegs.size() == 1 && "Can't handle this yet!");
5800b57cec5SDimitry Andric         unsigned Reg = LRegs[0];
5810b57cec5SDimitry Andric         SUnit *LRDef = LiveRegDefs[Reg];
5820b57cec5SDimitry Andric         MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
5830b57cec5SDimitry Andric         const TargetRegisterClass *RC =
5840b57cec5SDimitry Andric           TRI->getMinimalPhysRegClass(Reg, VT);
5850b57cec5SDimitry Andric         const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric         // If cross copy register class is the same as RC, then it must be
5880b57cec5SDimitry Andric         // possible copy the value directly. Do not try duplicate the def.
5890b57cec5SDimitry Andric         // If cross copy register class is not the same as RC, then it's
5900b57cec5SDimitry Andric         // possible to copy the value but it require cross register class copies
5910b57cec5SDimitry Andric         // and it is expensive.
5920b57cec5SDimitry Andric         // If cross copy register class is null, then it's not possible to copy
5930b57cec5SDimitry Andric         // the value at all.
5940b57cec5SDimitry Andric         SUnit *NewDef = nullptr;
5950b57cec5SDimitry Andric         if (DestRC != RC) {
5960b57cec5SDimitry Andric           NewDef = CopyAndMoveSuccessors(LRDef);
5970b57cec5SDimitry Andric           if (!DestRC && !NewDef)
5980b57cec5SDimitry Andric             report_fatal_error("Can't handle live physical "
5990b57cec5SDimitry Andric                                "register dependency!");
6000b57cec5SDimitry Andric         }
6010b57cec5SDimitry Andric         if (!NewDef) {
6020b57cec5SDimitry Andric           // Issue copies, these can be expensive cross register class copies.
6030b57cec5SDimitry Andric           SmallVector<SUnit*, 2> Copies;
6040b57cec5SDimitry Andric           InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
6050b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum
6060b57cec5SDimitry Andric                             << " to SU #" << Copies.front()->NodeNum << "\n");
6070b57cec5SDimitry Andric           AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
6080b57cec5SDimitry Andric           NewDef = Copies.back();
6090b57cec5SDimitry Andric         }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum
6120b57cec5SDimitry Andric                           << " to SU #" << TrySU->NodeNum << "\n");
6130b57cec5SDimitry Andric         LiveRegDefs[Reg] = NewDef;
6140b57cec5SDimitry Andric         AddPred(NewDef, SDep(TrySU, SDep::Artificial));
6150b57cec5SDimitry Andric         TrySU->isAvailable = false;
6160b57cec5SDimitry Andric         CurSU = NewDef;
6170b57cec5SDimitry Andric       }
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric       if (!CurSU) {
6200b57cec5SDimitry Andric         llvm_unreachable("Unable to resolve live physical register dependencies!");
6210b57cec5SDimitry Andric       }
6220b57cec5SDimitry Andric     }
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric     // Add the nodes that aren't ready back onto the available list.
625*0fca6ea1SDimitry Andric     for (SUnit *SU : NotReady) {
626*0fca6ea1SDimitry Andric       SU->isPending = false;
6270b57cec5SDimitry Andric       // May no longer be available due to backtracking.
628*0fca6ea1SDimitry Andric       if (SU->isAvailable)
629*0fca6ea1SDimitry Andric         AvailableQueue.push(SU);
6300b57cec5SDimitry Andric     }
6310b57cec5SDimitry Andric     NotReady.clear();
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric     if (CurSU)
6340b57cec5SDimitry Andric       ScheduleNodeBottomUp(CurSU, CurCycle);
6350b57cec5SDimitry Andric     ++CurCycle;
6360b57cec5SDimitry Andric   }
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric   // Reverse the order since it is bottom up.
6390b57cec5SDimitry Andric   std::reverse(Sequence.begin(), Sequence.end());
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric #ifndef NDEBUG
6420b57cec5SDimitry Andric   VerifyScheduledSequence(/*isBottomUp=*/true);
6430b57cec5SDimitry Andric #endif
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric namespace {
6480b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6490b57cec5SDimitry Andric // ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the
6500b57cec5SDimitry Andric // DAG in topological order.
6510b57cec5SDimitry Andric // IMPORTANT: this may not work for targets with phyreg dependency.
6520b57cec5SDimitry Andric //
6530b57cec5SDimitry Andric class ScheduleDAGLinearize : public ScheduleDAGSDNodes {
6540b57cec5SDimitry Andric public:
6550b57cec5SDimitry Andric   ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {}
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   void Schedule() override;
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric   MachineBasicBlock *
6600b57cec5SDimitry Andric     EmitSchedule(MachineBasicBlock::iterator &InsertPos) override;
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric private:
6630b57cec5SDimitry Andric   std::vector<SDNode*> Sequence;
6640b57cec5SDimitry Andric   DenseMap<SDNode*, SDNode*> GluedMap;  // Cache glue to its user
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   void ScheduleNode(SDNode *N);
6670b57cec5SDimitry Andric };
6680b57cec5SDimitry Andric } // end anonymous namespace
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric void ScheduleDAGLinearize::ScheduleNode(SDNode *N) {
6710b57cec5SDimitry Andric   if (N->getNodeId() != 0)
6720b57cec5SDimitry Andric     llvm_unreachable(nullptr);
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric   if (!N->isMachineOpcode() &&
6750b57cec5SDimitry Andric       (N->getOpcode() == ISD::EntryToken || isPassiveNode(N)))
6760b57cec5SDimitry Andric     // These nodes do not need to be translated into MIs.
6770b57cec5SDimitry Andric     return;
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\n*** Scheduling: ");
6800b57cec5SDimitry Andric   LLVM_DEBUG(N->dump(DAG));
6810b57cec5SDimitry Andric   Sequence.push_back(N);
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   unsigned NumOps = N->getNumOperands();
6840b57cec5SDimitry Andric   if (unsigned NumLeft = NumOps) {
6850b57cec5SDimitry Andric     SDNode *GluedOpN = nullptr;
6860b57cec5SDimitry Andric     do {
6870b57cec5SDimitry Andric       const SDValue &Op = N->getOperand(NumLeft-1);
6880b57cec5SDimitry Andric       SDNode *OpN = Op.getNode();
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric       if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) {
6910b57cec5SDimitry Andric         // Schedule glue operand right above N.
6920b57cec5SDimitry Andric         GluedOpN = OpN;
6930b57cec5SDimitry Andric         assert(OpN->getNodeId() != 0 && "Glue operand not ready?");
6940b57cec5SDimitry Andric         OpN->setNodeId(0);
6950b57cec5SDimitry Andric         ScheduleNode(OpN);
6960b57cec5SDimitry Andric         continue;
6970b57cec5SDimitry Andric       }
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric       if (OpN == GluedOpN)
7000b57cec5SDimitry Andric         // Glue operand is already scheduled.
7010b57cec5SDimitry Andric         continue;
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric       DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN);
7040b57cec5SDimitry Andric       if (DI != GluedMap.end() && DI->second != N)
7050b57cec5SDimitry Andric         // Users of glues are counted against the glued users.
7060b57cec5SDimitry Andric         OpN = DI->second;
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric       unsigned Degree = OpN->getNodeId();
7090b57cec5SDimitry Andric       assert(Degree > 0 && "Predecessor over-released!");
7100b57cec5SDimitry Andric       OpN->setNodeId(--Degree);
7110b57cec5SDimitry Andric       if (Degree == 0)
7120b57cec5SDimitry Andric         ScheduleNode(OpN);
7130b57cec5SDimitry Andric     } while (--NumLeft);
7140b57cec5SDimitry Andric   }
7150b57cec5SDimitry Andric }
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric /// findGluedUser - Find the representative use of a glue value by walking
7180b57cec5SDimitry Andric /// the use chain.
7190b57cec5SDimitry Andric static SDNode *findGluedUser(SDNode *N) {
7200b57cec5SDimitry Andric   while (SDNode *Glued = N->getGluedUser())
7210b57cec5SDimitry Andric     N = Glued;
7220b57cec5SDimitry Andric   return N;
7230b57cec5SDimitry Andric }
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric void ScheduleDAGLinearize::Schedule() {
7260b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** DAG Linearization **********\n");
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric   SmallVector<SDNode*, 8> Glues;
7290b57cec5SDimitry Andric   unsigned DAGSize = 0;
7300b57cec5SDimitry Andric   for (SDNode &Node : DAG->allnodes()) {
7310b57cec5SDimitry Andric     SDNode *N = &Node;
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric     // Use node id to record degree.
7340b57cec5SDimitry Andric     unsigned Degree = N->use_size();
7350b57cec5SDimitry Andric     N->setNodeId(Degree);
7360b57cec5SDimitry Andric     unsigned NumVals = N->getNumValues();
7370b57cec5SDimitry Andric     if (NumVals && N->getValueType(NumVals-1) == MVT::Glue &&
7380b57cec5SDimitry Andric         N->hasAnyUseOfValue(NumVals-1)) {
7390b57cec5SDimitry Andric       SDNode *User = findGluedUser(N);
7400b57cec5SDimitry Andric       if (User) {
7410b57cec5SDimitry Andric         Glues.push_back(N);
7420b57cec5SDimitry Andric         GluedMap.insert(std::make_pair(N, User));
7430b57cec5SDimitry Andric       }
7440b57cec5SDimitry Andric     }
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric     if (N->isMachineOpcode() ||
7470b57cec5SDimitry Andric         (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N)))
7480b57cec5SDimitry Andric       ++DAGSize;
7490b57cec5SDimitry Andric   }
7500b57cec5SDimitry Andric 
751*0fca6ea1SDimitry Andric   for (SDNode *Glue : Glues) {
7520b57cec5SDimitry Andric     SDNode *GUser = GluedMap[Glue];
7530b57cec5SDimitry Andric     unsigned Degree = Glue->getNodeId();
7540b57cec5SDimitry Andric     unsigned UDegree = GUser->getNodeId();
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric     // Glue user must be scheduled together with the glue operand. So other
7570b57cec5SDimitry Andric     // users of the glue operand must be treated as its users.
7580b57cec5SDimitry Andric     SDNode *ImmGUser = Glue->getGluedUser();
7590b57cec5SDimitry Andric     for (const SDNode *U : Glue->uses())
7600b57cec5SDimitry Andric       if (U == ImmGUser)
7610b57cec5SDimitry Andric         --Degree;
7620b57cec5SDimitry Andric     GUser->setNodeId(UDegree + Degree);
7630b57cec5SDimitry Andric     Glue->setNodeId(1);
7640b57cec5SDimitry Andric   }
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric   Sequence.reserve(DAGSize);
7670b57cec5SDimitry Andric   ScheduleNode(DAG->getRoot().getNode());
7680b57cec5SDimitry Andric }
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric MachineBasicBlock*
7710b57cec5SDimitry Andric ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
772bdd1243dSDimitry Andric   InstrEmitter Emitter(DAG->getTarget(), BB, InsertPos);
7735ffd83dbSDimitry Andric   DenseMap<SDValue, Register> VRBaseMap;
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   LLVM_DEBUG({ dbgs() << "\n*** Final schedule ***\n"; });
7760b57cec5SDimitry Andric 
7770b57cec5SDimitry Andric   unsigned NumNodes = Sequence.size();
7780b57cec5SDimitry Andric   MachineBasicBlock *BB = Emitter.getBlock();
7790b57cec5SDimitry Andric   for (unsigned i = 0; i != NumNodes; ++i) {
7800b57cec5SDimitry Andric     SDNode *N = Sequence[NumNodes-i-1];
7810b57cec5SDimitry Andric     LLVM_DEBUG(N->dump(DAG));
7820b57cec5SDimitry Andric     Emitter.EmitNode(N, false, false, VRBaseMap);
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric     // Emit any debug values associated with the node.
7850b57cec5SDimitry Andric     if (N->getHasDebugValue()) {
7860b57cec5SDimitry Andric       MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
787fcaf7f86SDimitry Andric       for (auto *DV : DAG->GetDbgValues(N)) {
7880b57cec5SDimitry Andric         if (!DV->isEmitted())
7890b57cec5SDimitry Andric           if (auto *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap))
7900b57cec5SDimitry Andric             BB->insert(InsertPos, DbgMI);
7910b57cec5SDimitry Andric       }
7920b57cec5SDimitry Andric     }
7930b57cec5SDimitry Andric   }
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << '\n');
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric   InsertPos = Emitter.getInsertPos();
7980b57cec5SDimitry Andric   return Emitter.getBlock();
7990b57cec5SDimitry Andric }
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8020b57cec5SDimitry Andric //                         Public Constructor Functions
8030b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8040b57cec5SDimitry Andric 
8055f757f3fSDimitry Andric llvm::ScheduleDAGSDNodes *llvm::createFastDAGScheduler(SelectionDAGISel *IS,
8065f757f3fSDimitry Andric                                                        CodeGenOptLevel) {
8070b57cec5SDimitry Andric   return new ScheduleDAGFast(*IS->MF);
8080b57cec5SDimitry Andric }
8090b57cec5SDimitry Andric 
8105f757f3fSDimitry Andric llvm::ScheduleDAGSDNodes *llvm::createDAGLinearizer(SelectionDAGISel *IS,
8115f757f3fSDimitry Andric                                                     CodeGenOptLevel) {
8120b57cec5SDimitry Andric   return new ScheduleDAGLinearize(*IS->MF);
8130b57cec5SDimitry Andric }
814