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