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