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