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" 15*81ad6265SDimitry 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. 720b57cec5SDimitry Andric unsigned NumLiveRegs; 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 } 2990b57cec5SDimitry Andric for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { 3000b57cec5SDimitry Andric const SDep &Pred = LoadPreds[i]; 3010b57cec5SDimitry Andric RemovePred(SU, Pred); 3020b57cec5SDimitry Andric if (isNewLoad) { 3030b57cec5SDimitry Andric AddPred(LoadSU, Pred); 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { 3070b57cec5SDimitry Andric const SDep &Pred = NodePreds[i]; 3080b57cec5SDimitry Andric RemovePred(SU, Pred); 3090b57cec5SDimitry Andric AddPred(NewSU, Pred); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { 3120b57cec5SDimitry Andric SDep D = NodeSuccs[i]; 3130b57cec5SDimitry Andric SUnit *SuccDep = D.getSUnit(); 3140b57cec5SDimitry Andric D.setSUnit(SU); 3150b57cec5SDimitry Andric RemovePred(SuccDep, D); 3160b57cec5SDimitry Andric D.setSUnit(NewSU); 3170b57cec5SDimitry Andric AddPred(SuccDep, D); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { 3200b57cec5SDimitry Andric SDep D = ChainSuccs[i]; 3210b57cec5SDimitry Andric SUnit *SuccDep = D.getSUnit(); 3220b57cec5SDimitry Andric D.setSUnit(SU); 3230b57cec5SDimitry Andric RemovePred(SuccDep, D); 3240b57cec5SDimitry Andric if (isNewLoad) { 3250b57cec5SDimitry Andric D.setSUnit(LoadSU); 3260b57cec5SDimitry Andric AddPred(SuccDep, D); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric if (isNewLoad) { 3300b57cec5SDimitry Andric SDep D(LoadSU, SDep::Barrier); 3310b57cec5SDimitry Andric D.setLatency(LoadSU->Latency); 3320b57cec5SDimitry Andric AddPred(NewSU, D); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric ++NumUnfolds; 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric if (NewSU->NumSuccsLeft == 0) { 3380b57cec5SDimitry Andric NewSU->isAvailable = true; 3390b57cec5SDimitry Andric return NewSU; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric SU = NewSU; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n"); 3450b57cec5SDimitry Andric NewSU = Clone(SU); 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric // New SUnit has the exact same predecessors. 3480b57cec5SDimitry Andric for (SDep &Pred : SU->Preds) 3490b57cec5SDimitry Andric if (!Pred.isArtificial()) 3500b57cec5SDimitry Andric AddPred(NewSU, Pred); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // Only copy scheduled successors. Cut them from old node's successor 3530b57cec5SDimitry Andric // list and move them over. 3540b57cec5SDimitry Andric SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; 3550b57cec5SDimitry Andric for (SDep &Succ : SU->Succs) { 3560b57cec5SDimitry Andric if (Succ.isArtificial()) 3570b57cec5SDimitry Andric continue; 3580b57cec5SDimitry Andric SUnit *SuccSU = Succ.getSUnit(); 3590b57cec5SDimitry Andric if (SuccSU->isScheduled) { 3600b57cec5SDimitry Andric SDep D = Succ; 3610b57cec5SDimitry Andric D.setSUnit(NewSU); 3620b57cec5SDimitry Andric AddPred(SuccSU, D); 3630b57cec5SDimitry Andric D.setSUnit(SU); 3640b57cec5SDimitry Andric DelDeps.push_back(std::make_pair(SuccSU, D)); 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) 3680b57cec5SDimitry Andric RemovePred(DelDeps[i].first, DelDeps[i].second); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric ++NumDups; 3710b57cec5SDimitry Andric return NewSU; 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric /// InsertCopiesAndMoveSuccs - Insert register copies and move all 3750b57cec5SDimitry Andric /// scheduled successors of the given SUnit to the last copy. 3760b57cec5SDimitry Andric void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, 3770b57cec5SDimitry Andric const TargetRegisterClass *DestRC, 3780b57cec5SDimitry Andric const TargetRegisterClass *SrcRC, 3790b57cec5SDimitry Andric SmallVectorImpl<SUnit*> &Copies) { 3800b57cec5SDimitry Andric SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr)); 3810b57cec5SDimitry Andric CopyFromSU->CopySrcRC = SrcRC; 3820b57cec5SDimitry Andric CopyFromSU->CopyDstRC = DestRC; 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr)); 3850b57cec5SDimitry Andric CopyToSU->CopySrcRC = DestRC; 3860b57cec5SDimitry Andric CopyToSU->CopyDstRC = SrcRC; 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric // Only copy scheduled successors. Cut them from old node's successor 3890b57cec5SDimitry Andric // list and move them over. 3900b57cec5SDimitry Andric SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; 3910b57cec5SDimitry Andric for (SDep &Succ : SU->Succs) { 3920b57cec5SDimitry Andric if (Succ.isArtificial()) 3930b57cec5SDimitry Andric continue; 3940b57cec5SDimitry Andric SUnit *SuccSU = Succ.getSUnit(); 3950b57cec5SDimitry Andric if (SuccSU->isScheduled) { 3960b57cec5SDimitry Andric SDep D = Succ; 3970b57cec5SDimitry Andric D.setSUnit(CopyToSU); 3980b57cec5SDimitry Andric AddPred(SuccSU, D); 3990b57cec5SDimitry Andric DelDeps.push_back(std::make_pair(SuccSU, Succ)); 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { 4030b57cec5SDimitry Andric RemovePred(DelDeps[i].first, DelDeps[i].second); 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric SDep FromDep(SU, SDep::Data, Reg); 4060b57cec5SDimitry Andric FromDep.setLatency(SU->Latency); 4070b57cec5SDimitry Andric AddPred(CopyFromSU, FromDep); 4080b57cec5SDimitry Andric SDep ToDep(CopyFromSU, SDep::Data, 0); 4090b57cec5SDimitry Andric ToDep.setLatency(CopyFromSU->Latency); 4100b57cec5SDimitry Andric AddPred(CopyToSU, ToDep); 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric Copies.push_back(CopyFromSU); 4130b57cec5SDimitry Andric Copies.push_back(CopyToSU); 4140b57cec5SDimitry Andric 4150b57cec5SDimitry Andric ++NumPRCopies; 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric /// getPhysicalRegisterVT - Returns the ValueType of the physical register 4190b57cec5SDimitry Andric /// definition of the specified node. 4200b57cec5SDimitry Andric /// FIXME: Move to SelectionDAG? 4210b57cec5SDimitry Andric static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg, 4220b57cec5SDimitry Andric const TargetInstrInfo *TII) { 4230b57cec5SDimitry Andric unsigned NumRes; 4240b57cec5SDimitry Andric if (N->getOpcode() == ISD::CopyFromReg) { 4250b57cec5SDimitry Andric // CopyFromReg has: "chain, Val, glue" so operand 1 gives the type. 4260b57cec5SDimitry Andric NumRes = 1; 4270b57cec5SDimitry Andric } else { 4280b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 4290b57cec5SDimitry Andric assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!"); 4300b57cec5SDimitry Andric NumRes = MCID.getNumDefs(); 4310b57cec5SDimitry Andric for (const MCPhysReg *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) { 4320b57cec5SDimitry Andric if (Reg == *ImpDef) 4330b57cec5SDimitry Andric break; 4340b57cec5SDimitry Andric ++NumRes; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric return N->getSimpleValueType(NumRes); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric /// CheckForLiveRegDef - Return true and update live register vector if the 4410b57cec5SDimitry Andric /// specified register def of the specified SUnit clobbers any "live" registers. 4420b57cec5SDimitry Andric static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg, 4430b57cec5SDimitry Andric std::vector<SUnit *> &LiveRegDefs, 4440b57cec5SDimitry Andric SmallSet<unsigned, 4> &RegAdded, 4450b57cec5SDimitry Andric SmallVectorImpl<unsigned> &LRegs, 446*81ad6265SDimitry Andric const TargetRegisterInfo *TRI, 447*81ad6265SDimitry Andric const SDNode *Node = nullptr) { 4480b57cec5SDimitry Andric bool Added = false; 4490b57cec5SDimitry Andric for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { 450*81ad6265SDimitry Andric // Check if Ref is live. 451*81ad6265SDimitry Andric if (!LiveRegDefs[*AI]) 452*81ad6265SDimitry Andric continue; 453*81ad6265SDimitry Andric 454*81ad6265SDimitry Andric // Allow multiple uses of the same def. 455*81ad6265SDimitry Andric if (LiveRegDefs[*AI] == SU) 456*81ad6265SDimitry Andric continue; 457*81ad6265SDimitry Andric 458*81ad6265SDimitry Andric // Allow multiple uses of same def 459*81ad6265SDimitry Andric if (Node && LiveRegDefs[*AI]->getNode() == Node) 460*81ad6265SDimitry Andric continue; 461*81ad6265SDimitry Andric 462*81ad6265SDimitry Andric // Add Reg to the set of interfering live regs. 4630b57cec5SDimitry Andric if (RegAdded.insert(*AI).second) { 4640b57cec5SDimitry Andric LRegs.push_back(*AI); 4650b57cec5SDimitry Andric Added = true; 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric return Added; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay 4720b57cec5SDimitry Andric /// scheduling of the given node to satisfy live physical register dependencies. 4730b57cec5SDimitry Andric /// If the specific node is the last one that's available to schedule, do 4740b57cec5SDimitry Andric /// whatever is necessary (i.e. backtracking or cloning) to make it possible. 4750b57cec5SDimitry Andric bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU, 4760b57cec5SDimitry Andric SmallVectorImpl<unsigned> &LRegs){ 4770b57cec5SDimitry Andric if (NumLiveRegs == 0) 4780b57cec5SDimitry Andric return false; 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric SmallSet<unsigned, 4> RegAdded; 4810b57cec5SDimitry Andric // If this node would clobber any "live" register, then it's not ready. 4820b57cec5SDimitry Andric for (SDep &Pred : SU->Preds) { 4830b57cec5SDimitry Andric if (Pred.isAssignedRegDep()) { 4840b57cec5SDimitry Andric CheckForLiveRegDef(Pred.getSUnit(), Pred.getReg(), LiveRegDefs, 4850b57cec5SDimitry Andric RegAdded, LRegs, TRI); 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) { 4900b57cec5SDimitry Andric if (Node->getOpcode() == ISD::INLINEASM || 4910b57cec5SDimitry Andric Node->getOpcode() == ISD::INLINEASM_BR) { 4920b57cec5SDimitry Andric // Inline asm can clobber physical defs. 4930b57cec5SDimitry Andric unsigned NumOps = Node->getNumOperands(); 4940b57cec5SDimitry Andric if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue) 4950b57cec5SDimitry Andric --NumOps; // Ignore the glue operand. 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) { 4980b57cec5SDimitry Andric unsigned Flags = 4990b57cec5SDimitry Andric cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); 5000b57cec5SDimitry Andric unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric ++i; // Skip the ID value. 5030b57cec5SDimitry Andric if (InlineAsm::isRegDefKind(Flags) || 5040b57cec5SDimitry Andric InlineAsm::isRegDefEarlyClobberKind(Flags) || 5050b57cec5SDimitry Andric InlineAsm::isClobberKind(Flags)) { 5060b57cec5SDimitry Andric // Check for def of register or earlyclobber register. 5070b57cec5SDimitry Andric for (; NumVals; --NumVals, ++i) { 5080b57cec5SDimitry Andric unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 5098bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) 5100b57cec5SDimitry Andric CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI); 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric } else 5130b57cec5SDimitry Andric i += NumVals; 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric continue; 5160b57cec5SDimitry Andric } 517*81ad6265SDimitry Andric 518*81ad6265SDimitry Andric if (Node->getOpcode() == ISD::CopyToReg) { 519*81ad6265SDimitry Andric Register Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 520*81ad6265SDimitry Andric if (Reg.isPhysical()) { 521*81ad6265SDimitry Andric SDNode *SrcNode = Node->getOperand(2).getNode(); 522*81ad6265SDimitry Andric CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI, SrcNode); 523*81ad6265SDimitry Andric } 524*81ad6265SDimitry Andric } 525*81ad6265SDimitry Andric 5260b57cec5SDimitry Andric if (!Node->isMachineOpcode()) 5270b57cec5SDimitry Andric continue; 5280b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode()); 5290b57cec5SDimitry Andric if (!MCID.ImplicitDefs) 5300b57cec5SDimitry Andric continue; 5310b57cec5SDimitry Andric for (const MCPhysReg *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) { 5320b57cec5SDimitry Andric CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI); 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric return !LRegs.empty(); 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up 5400b57cec5SDimitry Andric /// schedulers. 5410b57cec5SDimitry Andric void ScheduleDAGFast::ListScheduleBottomUp() { 5420b57cec5SDimitry Andric unsigned CurCycle = 0; 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric // Release any predecessors of the special Exit node. 5450b57cec5SDimitry Andric ReleasePredecessors(&ExitSU, CurCycle); 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric // Add root to Available queue. 5480b57cec5SDimitry Andric if (!SUnits.empty()) { 5490b57cec5SDimitry Andric SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()]; 5500b57cec5SDimitry Andric assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!"); 5510b57cec5SDimitry Andric RootSU->isAvailable = true; 5520b57cec5SDimitry Andric AvailableQueue.push(RootSU); 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // While Available queue is not empty, grab the node with the highest 5560b57cec5SDimitry Andric // priority. If it is not ready put it back. Schedule the node. 5570b57cec5SDimitry Andric SmallVector<SUnit*, 4> NotReady; 5580b57cec5SDimitry Andric DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; 5590b57cec5SDimitry Andric Sequence.reserve(SUnits.size()); 5600b57cec5SDimitry Andric while (!AvailableQueue.empty()) { 5610b57cec5SDimitry Andric bool Delayed = false; 5620b57cec5SDimitry Andric LRegsMap.clear(); 5630b57cec5SDimitry Andric SUnit *CurSU = AvailableQueue.pop(); 5640b57cec5SDimitry Andric while (CurSU) { 5650b57cec5SDimitry Andric SmallVector<unsigned, 4> LRegs; 5660b57cec5SDimitry Andric if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) 5670b57cec5SDimitry Andric break; 5680b57cec5SDimitry Andric Delayed = true; 5690b57cec5SDimitry Andric LRegsMap.insert(std::make_pair(CurSU, LRegs)); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric CurSU->isPending = true; // This SU is not in AvailableQueue right now. 5720b57cec5SDimitry Andric NotReady.push_back(CurSU); 5730b57cec5SDimitry Andric CurSU = AvailableQueue.pop(); 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric // All candidates are delayed due to live physical reg dependencies. 5770b57cec5SDimitry Andric // Try code duplication or inserting cross class copies 5780b57cec5SDimitry Andric // to resolve it. 5790b57cec5SDimitry Andric if (Delayed && !CurSU) { 5800b57cec5SDimitry Andric if (!CurSU) { 5810b57cec5SDimitry Andric // Try duplicating the nodes that produces these 5820b57cec5SDimitry Andric // "expensive to copy" values to break the dependency. In case even 5830b57cec5SDimitry Andric // that doesn't work, insert cross class copies. 5840b57cec5SDimitry Andric SUnit *TrySU = NotReady[0]; 5850b57cec5SDimitry Andric SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU]; 5860b57cec5SDimitry Andric assert(LRegs.size() == 1 && "Can't handle this yet!"); 5870b57cec5SDimitry Andric unsigned Reg = LRegs[0]; 5880b57cec5SDimitry Andric SUnit *LRDef = LiveRegDefs[Reg]; 5890b57cec5SDimitry Andric MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII); 5900b57cec5SDimitry Andric const TargetRegisterClass *RC = 5910b57cec5SDimitry Andric TRI->getMinimalPhysRegClass(Reg, VT); 5920b57cec5SDimitry Andric const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC); 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric // If cross copy register class is the same as RC, then it must be 5950b57cec5SDimitry Andric // possible copy the value directly. Do not try duplicate the def. 5960b57cec5SDimitry Andric // If cross copy register class is not the same as RC, then it's 5970b57cec5SDimitry Andric // possible to copy the value but it require cross register class copies 5980b57cec5SDimitry Andric // and it is expensive. 5990b57cec5SDimitry Andric // If cross copy register class is null, then it's not possible to copy 6000b57cec5SDimitry Andric // the value at all. 6010b57cec5SDimitry Andric SUnit *NewDef = nullptr; 6020b57cec5SDimitry Andric if (DestRC != RC) { 6030b57cec5SDimitry Andric NewDef = CopyAndMoveSuccessors(LRDef); 6040b57cec5SDimitry Andric if (!DestRC && !NewDef) 6050b57cec5SDimitry Andric report_fatal_error("Can't handle live physical " 6060b57cec5SDimitry Andric "register dependency!"); 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric if (!NewDef) { 6090b57cec5SDimitry Andric // Issue copies, these can be expensive cross register class copies. 6100b57cec5SDimitry Andric SmallVector<SUnit*, 2> Copies; 6110b57cec5SDimitry Andric InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); 6120b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum 6130b57cec5SDimitry Andric << " to SU #" << Copies.front()->NodeNum << "\n"); 6140b57cec5SDimitry Andric AddPred(TrySU, SDep(Copies.front(), SDep::Artificial)); 6150b57cec5SDimitry Andric NewDef = Copies.back(); 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum 6190b57cec5SDimitry Andric << " to SU #" << TrySU->NodeNum << "\n"); 6200b57cec5SDimitry Andric LiveRegDefs[Reg] = NewDef; 6210b57cec5SDimitry Andric AddPred(NewDef, SDep(TrySU, SDep::Artificial)); 6220b57cec5SDimitry Andric TrySU->isAvailable = false; 6230b57cec5SDimitry Andric CurSU = NewDef; 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric if (!CurSU) { 6270b57cec5SDimitry Andric llvm_unreachable("Unable to resolve live physical register dependencies!"); 6280b57cec5SDimitry Andric } 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric // Add the nodes that aren't ready back onto the available list. 6320b57cec5SDimitry Andric for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { 6330b57cec5SDimitry Andric NotReady[i]->isPending = false; 6340b57cec5SDimitry Andric // May no longer be available due to backtracking. 6350b57cec5SDimitry Andric if (NotReady[i]->isAvailable) 6360b57cec5SDimitry Andric AvailableQueue.push(NotReady[i]); 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric NotReady.clear(); 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric if (CurSU) 6410b57cec5SDimitry Andric ScheduleNodeBottomUp(CurSU, CurCycle); 6420b57cec5SDimitry Andric ++CurCycle; 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric // Reverse the order since it is bottom up. 6460b57cec5SDimitry Andric std::reverse(Sequence.begin(), Sequence.end()); 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric #ifndef NDEBUG 6490b57cec5SDimitry Andric VerifyScheduledSequence(/*isBottomUp=*/true); 6500b57cec5SDimitry Andric #endif 6510b57cec5SDimitry Andric } 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric namespace { 6550b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6560b57cec5SDimitry Andric // ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the 6570b57cec5SDimitry Andric // DAG in topological order. 6580b57cec5SDimitry Andric // IMPORTANT: this may not work for targets with phyreg dependency. 6590b57cec5SDimitry Andric // 6600b57cec5SDimitry Andric class ScheduleDAGLinearize : public ScheduleDAGSDNodes { 6610b57cec5SDimitry Andric public: 6620b57cec5SDimitry Andric ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {} 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric void Schedule() override; 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric MachineBasicBlock * 6670b57cec5SDimitry Andric EmitSchedule(MachineBasicBlock::iterator &InsertPos) override; 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric private: 6700b57cec5SDimitry Andric std::vector<SDNode*> Sequence; 6710b57cec5SDimitry Andric DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric void ScheduleNode(SDNode *N); 6740b57cec5SDimitry Andric }; 6750b57cec5SDimitry Andric } // end anonymous namespace 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric void ScheduleDAGLinearize::ScheduleNode(SDNode *N) { 6780b57cec5SDimitry Andric if (N->getNodeId() != 0) 6790b57cec5SDimitry Andric llvm_unreachable(nullptr); 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric if (!N->isMachineOpcode() && 6820b57cec5SDimitry Andric (N->getOpcode() == ISD::EntryToken || isPassiveNode(N))) 6830b57cec5SDimitry Andric // These nodes do not need to be translated into MIs. 6840b57cec5SDimitry Andric return; 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n*** Scheduling: "); 6870b57cec5SDimitry Andric LLVM_DEBUG(N->dump(DAG)); 6880b57cec5SDimitry Andric Sequence.push_back(N); 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric unsigned NumOps = N->getNumOperands(); 6910b57cec5SDimitry Andric if (unsigned NumLeft = NumOps) { 6920b57cec5SDimitry Andric SDNode *GluedOpN = nullptr; 6930b57cec5SDimitry Andric do { 6940b57cec5SDimitry Andric const SDValue &Op = N->getOperand(NumLeft-1); 6950b57cec5SDimitry Andric SDNode *OpN = Op.getNode(); 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) { 6980b57cec5SDimitry Andric // Schedule glue operand right above N. 6990b57cec5SDimitry Andric GluedOpN = OpN; 7000b57cec5SDimitry Andric assert(OpN->getNodeId() != 0 && "Glue operand not ready?"); 7010b57cec5SDimitry Andric OpN->setNodeId(0); 7020b57cec5SDimitry Andric ScheduleNode(OpN); 7030b57cec5SDimitry Andric continue; 7040b57cec5SDimitry Andric } 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric if (OpN == GluedOpN) 7070b57cec5SDimitry Andric // Glue operand is already scheduled. 7080b57cec5SDimitry Andric continue; 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN); 7110b57cec5SDimitry Andric if (DI != GluedMap.end() && DI->second != N) 7120b57cec5SDimitry Andric // Users of glues are counted against the glued users. 7130b57cec5SDimitry Andric OpN = DI->second; 7140b57cec5SDimitry Andric 7150b57cec5SDimitry Andric unsigned Degree = OpN->getNodeId(); 7160b57cec5SDimitry Andric assert(Degree > 0 && "Predecessor over-released!"); 7170b57cec5SDimitry Andric OpN->setNodeId(--Degree); 7180b57cec5SDimitry Andric if (Degree == 0) 7190b57cec5SDimitry Andric ScheduleNode(OpN); 7200b57cec5SDimitry Andric } while (--NumLeft); 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric /// findGluedUser - Find the representative use of a glue value by walking 7250b57cec5SDimitry Andric /// the use chain. 7260b57cec5SDimitry Andric static SDNode *findGluedUser(SDNode *N) { 7270b57cec5SDimitry Andric while (SDNode *Glued = N->getGluedUser()) 7280b57cec5SDimitry Andric N = Glued; 7290b57cec5SDimitry Andric return N; 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric void ScheduleDAGLinearize::Schedule() { 7330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** DAG Linearization **********\n"); 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric SmallVector<SDNode*, 8> Glues; 7360b57cec5SDimitry Andric unsigned DAGSize = 0; 7370b57cec5SDimitry Andric for (SDNode &Node : DAG->allnodes()) { 7380b57cec5SDimitry Andric SDNode *N = &Node; 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric // Use node id to record degree. 7410b57cec5SDimitry Andric unsigned Degree = N->use_size(); 7420b57cec5SDimitry Andric N->setNodeId(Degree); 7430b57cec5SDimitry Andric unsigned NumVals = N->getNumValues(); 7440b57cec5SDimitry Andric if (NumVals && N->getValueType(NumVals-1) == MVT::Glue && 7450b57cec5SDimitry Andric N->hasAnyUseOfValue(NumVals-1)) { 7460b57cec5SDimitry Andric SDNode *User = findGluedUser(N); 7470b57cec5SDimitry Andric if (User) { 7480b57cec5SDimitry Andric Glues.push_back(N); 7490b57cec5SDimitry Andric GluedMap.insert(std::make_pair(N, User)); 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric } 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric if (N->isMachineOpcode() || 7540b57cec5SDimitry Andric (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N))) 7550b57cec5SDimitry Andric ++DAGSize; 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric for (unsigned i = 0, e = Glues.size(); i != e; ++i) { 7590b57cec5SDimitry Andric SDNode *Glue = Glues[i]; 7600b57cec5SDimitry Andric SDNode *GUser = GluedMap[Glue]; 7610b57cec5SDimitry Andric unsigned Degree = Glue->getNodeId(); 7620b57cec5SDimitry Andric unsigned UDegree = GUser->getNodeId(); 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric // Glue user must be scheduled together with the glue operand. So other 7650b57cec5SDimitry Andric // users of the glue operand must be treated as its users. 7660b57cec5SDimitry Andric SDNode *ImmGUser = Glue->getGluedUser(); 7670b57cec5SDimitry Andric for (const SDNode *U : Glue->uses()) 7680b57cec5SDimitry Andric if (U == ImmGUser) 7690b57cec5SDimitry Andric --Degree; 7700b57cec5SDimitry Andric GUser->setNodeId(UDegree + Degree); 7710b57cec5SDimitry Andric Glue->setNodeId(1); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric Sequence.reserve(DAGSize); 7750b57cec5SDimitry Andric ScheduleNode(DAG->getRoot().getNode()); 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric MachineBasicBlock* 7790b57cec5SDimitry Andric ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) { 7803a9a9c0cSDimitry Andric InstrEmitter Emitter(DAG->getTarget(), BB, InsertPos, 7813a9a9c0cSDimitry Andric DAG->getUseInstrRefDebugInfo()); 7825ffd83dbSDimitry Andric DenseMap<SDValue, Register> VRBaseMap; 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric LLVM_DEBUG({ dbgs() << "\n*** Final schedule ***\n"; }); 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric unsigned NumNodes = Sequence.size(); 7870b57cec5SDimitry Andric MachineBasicBlock *BB = Emitter.getBlock(); 7880b57cec5SDimitry Andric for (unsigned i = 0; i != NumNodes; ++i) { 7890b57cec5SDimitry Andric SDNode *N = Sequence[NumNodes-i-1]; 7900b57cec5SDimitry Andric LLVM_DEBUG(N->dump(DAG)); 7910b57cec5SDimitry Andric Emitter.EmitNode(N, false, false, VRBaseMap); 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric // Emit any debug values associated with the node. 7940b57cec5SDimitry Andric if (N->getHasDebugValue()) { 7950b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos(); 7960b57cec5SDimitry Andric for (auto DV : DAG->GetDbgValues(N)) { 7970b57cec5SDimitry Andric if (!DV->isEmitted()) 7980b57cec5SDimitry Andric if (auto *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap)) 7990b57cec5SDimitry Andric BB->insert(InsertPos, DbgMI); 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric } 8020b57cec5SDimitry Andric } 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << '\n'); 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric InsertPos = Emitter.getInsertPos(); 8070b57cec5SDimitry Andric return Emitter.getBlock(); 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8110b57cec5SDimitry Andric // Public Constructor Functions 8120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric llvm::ScheduleDAGSDNodes * 8150b57cec5SDimitry Andric llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) { 8160b57cec5SDimitry Andric return new ScheduleDAGFast(*IS->MF); 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric llvm::ScheduleDAGSDNodes * 8200b57cec5SDimitry Andric llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) { 8210b57cec5SDimitry Andric return new ScheduleDAGLinearize(*IS->MF); 8220b57cec5SDimitry Andric } 823