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