LLVM API Documentation
00001 //===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This implements a fast scheduler. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/SchedulerRegistry.h" 00015 #include "InstrEmitter.h" 00016 #include "ScheduleDAGSDNodes.h" 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/ADT/SmallSet.h" 00019 #include "llvm/ADT/Statistic.h" 00020 #include "llvm/CodeGen/SelectionDAGISel.h" 00021 #include "llvm/IR/DataLayout.h" 00022 #include "llvm/IR/InlineAsm.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 #include "llvm/Target/TargetInstrInfo.h" 00027 #include "llvm/Target/TargetRegisterInfo.h" 00028 using namespace llvm; 00029 00030 #define DEBUG_TYPE "pre-RA-sched" 00031 00032 STATISTIC(NumUnfolds, "Number of nodes unfolded"); 00033 STATISTIC(NumDups, "Number of duplicated nodes"); 00034 STATISTIC(NumPRCopies, "Number of physical copies"); 00035 00036 static RegisterScheduler 00037 fastDAGScheduler("fast", "Fast suboptimal list scheduling", 00038 createFastDAGScheduler); 00039 static RegisterScheduler 00040 linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling", 00041 createDAGLinearizer); 00042 00043 00044 namespace { 00045 /// FastPriorityQueue - A degenerate priority queue that considers 00046 /// all nodes to have the same priority. 00047 /// 00048 struct FastPriorityQueue { 00049 SmallVector<SUnit *, 16> Queue; 00050 00051 bool empty() const { return Queue.empty(); } 00052 00053 void push(SUnit *U) { 00054 Queue.push_back(U); 00055 } 00056 00057 SUnit *pop() { 00058 if (empty()) return nullptr; 00059 SUnit *V = Queue.back(); 00060 Queue.pop_back(); 00061 return V; 00062 } 00063 }; 00064 00065 //===----------------------------------------------------------------------===// 00066 /// ScheduleDAGFast - The actual "fast" list scheduler implementation. 00067 /// 00068 class ScheduleDAGFast : public ScheduleDAGSDNodes { 00069 private: 00070 /// AvailableQueue - The priority queue to use for the available SUnits. 00071 FastPriorityQueue AvailableQueue; 00072 00073 /// LiveRegDefs - A set of physical registers and their definition 00074 /// that are "live". These nodes must be scheduled before any other nodes that 00075 /// modifies the registers can be scheduled. 00076 unsigned NumLiveRegs; 00077 std::vector<SUnit*> LiveRegDefs; 00078 std::vector<unsigned> LiveRegCycles; 00079 00080 public: 00081 ScheduleDAGFast(MachineFunction &mf) 00082 : ScheduleDAGSDNodes(mf) {} 00083 00084 void Schedule() override; 00085 00086 /// AddPred - adds a predecessor edge to SUnit SU. 00087 /// This returns true if this is a new predecessor. 00088 void AddPred(SUnit *SU, const SDep &D) { 00089 SU->addPred(D); 00090 } 00091 00092 /// RemovePred - removes a predecessor edge from SUnit SU. 00093 /// This returns true if an edge was removed. 00094 void RemovePred(SUnit *SU, const SDep &D) { 00095 SU->removePred(D); 00096 } 00097 00098 private: 00099 void ReleasePred(SUnit *SU, SDep *PredEdge); 00100 void ReleasePredecessors(SUnit *SU, unsigned CurCycle); 00101 void ScheduleNodeBottomUp(SUnit*, unsigned); 00102 SUnit *CopyAndMoveSuccessors(SUnit*); 00103 void InsertCopiesAndMoveSuccs(SUnit*, unsigned, 00104 const TargetRegisterClass*, 00105 const TargetRegisterClass*, 00106 SmallVectorImpl<SUnit*>&); 00107 bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&); 00108 void ListScheduleBottomUp(); 00109 00110 /// forceUnitLatencies - The fast scheduler doesn't care about real latencies. 00111 bool forceUnitLatencies() const override { return true; } 00112 }; 00113 } // end anonymous namespace 00114 00115 00116 /// Schedule - Schedule the DAG using list scheduling. 00117 void ScheduleDAGFast::Schedule() { 00118 DEBUG(dbgs() << "********** List Scheduling **********\n"); 00119 00120 NumLiveRegs = 0; 00121 LiveRegDefs.resize(TRI->getNumRegs(), nullptr); 00122 LiveRegCycles.resize(TRI->getNumRegs(), 0); 00123 00124 // Build the scheduling graph. 00125 BuildSchedGraph(nullptr); 00126 00127 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) 00128 SUnits[su].dumpAll(this)); 00129 00130 // Execute the actual scheduling loop. 00131 ListScheduleBottomUp(); 00132 } 00133 00134 //===----------------------------------------------------------------------===// 00135 // Bottom-Up Scheduling 00136 //===----------------------------------------------------------------------===// 00137 00138 /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to 00139 /// the AvailableQueue if the count reaches zero. Also update its cycle bound. 00140 void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) { 00141 SUnit *PredSU = PredEdge->getSUnit(); 00142 00143 #ifndef NDEBUG 00144 if (PredSU->NumSuccsLeft == 0) { 00145 dbgs() << "*** Scheduling failed! ***\n"; 00146 PredSU->dump(this); 00147 dbgs() << " has been released too many times!\n"; 00148 llvm_unreachable(nullptr); 00149 } 00150 #endif 00151 --PredSU->NumSuccsLeft; 00152 00153 // If all the node's successors are scheduled, this node is ready 00154 // to be scheduled. Ignore the special EntrySU node. 00155 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) { 00156 PredSU->isAvailable = true; 00157 AvailableQueue.push(PredSU); 00158 } 00159 } 00160 00161 void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) { 00162 // Bottom up: release predecessors 00163 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00164 I != E; ++I) { 00165 ReleasePred(SU, &*I); 00166 if (I->isAssignedRegDep()) { 00167 // This is a physical register dependency and it's impossible or 00168 // expensive to copy the register. Make sure nothing that can 00169 // clobber the register is scheduled between the predecessor and 00170 // this node. 00171 if (!LiveRegDefs[I->getReg()]) { 00172 ++NumLiveRegs; 00173 LiveRegDefs[I->getReg()] = I->getSUnit(); 00174 LiveRegCycles[I->getReg()] = CurCycle; 00175 } 00176 } 00177 } 00178 } 00179 00180 /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending 00181 /// count of its predecessors. If a predecessor pending count is zero, add it to 00182 /// the Available queue. 00183 void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { 00184 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); 00185 DEBUG(SU->dump(this)); 00186 00187 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!"); 00188 SU->setHeightToAtLeast(CurCycle); 00189 Sequence.push_back(SU); 00190 00191 ReleasePredecessors(SU, CurCycle); 00192 00193 // Release all the implicit physical register defs that are live. 00194 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00195 I != E; ++I) { 00196 if (I->isAssignedRegDep()) { 00197 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) { 00198 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); 00199 assert(LiveRegDefs[I->getReg()] == SU && 00200 "Physical register dependency violated?"); 00201 --NumLiveRegs; 00202 LiveRegDefs[I->getReg()] = nullptr; 00203 LiveRegCycles[I->getReg()] = 0; 00204 } 00205 } 00206 } 00207 00208 SU->isScheduled = true; 00209 } 00210 00211 /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled 00212 /// successors to the newly created node. 00213 SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) { 00214 if (SU->getNode()->getGluedNode()) 00215 return nullptr; 00216 00217 SDNode *N = SU->getNode(); 00218 if (!N) 00219 return nullptr; 00220 00221 SUnit *NewSU; 00222 bool TryUnfold = false; 00223 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { 00224 EVT VT = N->getValueType(i); 00225 if (VT == MVT::Glue) 00226 return nullptr; 00227 else if (VT == MVT::Other) 00228 TryUnfold = true; 00229 } 00230 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 00231 const SDValue &Op = N->getOperand(i); 00232 EVT VT = Op.getNode()->getValueType(Op.getResNo()); 00233 if (VT == MVT::Glue) 00234 return nullptr; 00235 } 00236 00237 if (TryUnfold) { 00238 SmallVector<SDNode*, 2> NewNodes; 00239 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes)) 00240 return nullptr; 00241 00242 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n"); 00243 assert(NewNodes.size() == 2 && "Expected a load folding node!"); 00244 00245 N = NewNodes[1]; 00246 SDNode *LoadNode = NewNodes[0]; 00247 unsigned NumVals = N->getNumValues(); 00248 unsigned OldNumVals = SU->getNode()->getNumValues(); 00249 for (unsigned i = 0; i != NumVals; ++i) 00250 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i)); 00251 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1), 00252 SDValue(LoadNode, 1)); 00253 00254 SUnit *NewSU = newSUnit(N); 00255 assert(N->getNodeId() == -1 && "Node already inserted!"); 00256 N->setNodeId(NewSU->NodeNum); 00257 00258 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 00259 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) { 00260 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) { 00261 NewSU->isTwoAddress = true; 00262 break; 00263 } 00264 } 00265 if (MCID.isCommutable()) 00266 NewSU->isCommutable = true; 00267 00268 // LoadNode may already exist. This can happen when there is another 00269 // load from the same location and producing the same type of value 00270 // but it has different alignment or volatileness. 00271 bool isNewLoad = true; 00272 SUnit *LoadSU; 00273 if (LoadNode->getNodeId() != -1) { 00274 LoadSU = &SUnits[LoadNode->getNodeId()]; 00275 isNewLoad = false; 00276 } else { 00277 LoadSU = newSUnit(LoadNode); 00278 LoadNode->setNodeId(LoadSU->NodeNum); 00279 } 00280 00281 SDep ChainPred; 00282 SmallVector<SDep, 4> ChainSuccs; 00283 SmallVector<SDep, 4> LoadPreds; 00284 SmallVector<SDep, 4> NodePreds; 00285 SmallVector<SDep, 4> NodeSuccs; 00286 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00287 I != E; ++I) { 00288 if (I->isCtrl()) 00289 ChainPred = *I; 00290 else if (I->getSUnit()->getNode() && 00291 I->getSUnit()->getNode()->isOperandOf(LoadNode)) 00292 LoadPreds.push_back(*I); 00293 else 00294 NodePreds.push_back(*I); 00295 } 00296 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00297 I != E; ++I) { 00298 if (I->isCtrl()) 00299 ChainSuccs.push_back(*I); 00300 else 00301 NodeSuccs.push_back(*I); 00302 } 00303 00304 if (ChainPred.getSUnit()) { 00305 RemovePred(SU, ChainPred); 00306 if (isNewLoad) 00307 AddPred(LoadSU, ChainPred); 00308 } 00309 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { 00310 const SDep &Pred = LoadPreds[i]; 00311 RemovePred(SU, Pred); 00312 if (isNewLoad) { 00313 AddPred(LoadSU, Pred); 00314 } 00315 } 00316 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { 00317 const SDep &Pred = NodePreds[i]; 00318 RemovePred(SU, Pred); 00319 AddPred(NewSU, Pred); 00320 } 00321 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { 00322 SDep D = NodeSuccs[i]; 00323 SUnit *SuccDep = D.getSUnit(); 00324 D.setSUnit(SU); 00325 RemovePred(SuccDep, D); 00326 D.setSUnit(NewSU); 00327 AddPred(SuccDep, D); 00328 } 00329 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { 00330 SDep D = ChainSuccs[i]; 00331 SUnit *SuccDep = D.getSUnit(); 00332 D.setSUnit(SU); 00333 RemovePred(SuccDep, D); 00334 if (isNewLoad) { 00335 D.setSUnit(LoadSU); 00336 AddPred(SuccDep, D); 00337 } 00338 } 00339 if (isNewLoad) { 00340 SDep D(LoadSU, SDep::Barrier); 00341 D.setLatency(LoadSU->Latency); 00342 AddPred(NewSU, D); 00343 } 00344 00345 ++NumUnfolds; 00346 00347 if (NewSU->NumSuccsLeft == 0) { 00348 NewSU->isAvailable = true; 00349 return NewSU; 00350 } 00351 SU = NewSU; 00352 } 00353 00354 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n"); 00355 NewSU = Clone(SU); 00356 00357 // New SUnit has the exact same predecessors. 00358 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00359 I != E; ++I) 00360 if (!I->isArtificial()) 00361 AddPred(NewSU, *I); 00362 00363 // Only copy scheduled successors. Cut them from old node's successor 00364 // list and move them over. 00365 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; 00366 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00367 I != E; ++I) { 00368 if (I->isArtificial()) 00369 continue; 00370 SUnit *SuccSU = I->getSUnit(); 00371 if (SuccSU->isScheduled) { 00372 SDep D = *I; 00373 D.setSUnit(NewSU); 00374 AddPred(SuccSU, D); 00375 D.setSUnit(SU); 00376 DelDeps.push_back(std::make_pair(SuccSU, D)); 00377 } 00378 } 00379 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) 00380 RemovePred(DelDeps[i].first, DelDeps[i].second); 00381 00382 ++NumDups; 00383 return NewSU; 00384 } 00385 00386 /// InsertCopiesAndMoveSuccs - Insert register copies and move all 00387 /// scheduled successors of the given SUnit to the last copy. 00388 void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, 00389 const TargetRegisterClass *DestRC, 00390 const TargetRegisterClass *SrcRC, 00391 SmallVectorImpl<SUnit*> &Copies) { 00392 SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr)); 00393 CopyFromSU->CopySrcRC = SrcRC; 00394 CopyFromSU->CopyDstRC = DestRC; 00395 00396 SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr)); 00397 CopyToSU->CopySrcRC = DestRC; 00398 CopyToSU->CopyDstRC = SrcRC; 00399 00400 // Only copy scheduled successors. Cut them from old node's successor 00401 // list and move them over. 00402 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; 00403 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00404 I != E; ++I) { 00405 if (I->isArtificial()) 00406 continue; 00407 SUnit *SuccSU = I->getSUnit(); 00408 if (SuccSU->isScheduled) { 00409 SDep D = *I; 00410 D.setSUnit(CopyToSU); 00411 AddPred(SuccSU, D); 00412 DelDeps.push_back(std::make_pair(SuccSU, *I)); 00413 } 00414 } 00415 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { 00416 RemovePred(DelDeps[i].first, DelDeps[i].second); 00417 } 00418 SDep FromDep(SU, SDep::Data, Reg); 00419 FromDep.setLatency(SU->Latency); 00420 AddPred(CopyFromSU, FromDep); 00421 SDep ToDep(CopyFromSU, SDep::Data, 0); 00422 ToDep.setLatency(CopyFromSU->Latency); 00423 AddPred(CopyToSU, ToDep); 00424 00425 Copies.push_back(CopyFromSU); 00426 Copies.push_back(CopyToSU); 00427 00428 ++NumPRCopies; 00429 } 00430 00431 /// getPhysicalRegisterVT - Returns the ValueType of the physical register 00432 /// definition of the specified node. 00433 /// FIXME: Move to SelectionDAG? 00434 static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg, 00435 const TargetInstrInfo *TII) { 00436 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 00437 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!"); 00438 unsigned NumRes = MCID.getNumDefs(); 00439 for (const uint16_t *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) { 00440 if (Reg == *ImpDef) 00441 break; 00442 ++NumRes; 00443 } 00444 return N->getValueType(NumRes); 00445 } 00446 00447 /// CheckForLiveRegDef - Return true and update live register vector if the 00448 /// specified register def of the specified SUnit clobbers any "live" registers. 00449 static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg, 00450 std::vector<SUnit*> &LiveRegDefs, 00451 SmallSet<unsigned, 4> &RegAdded, 00452 SmallVectorImpl<unsigned> &LRegs, 00453 const TargetRegisterInfo *TRI) { 00454 bool Added = false; 00455 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { 00456 if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) { 00457 if (RegAdded.insert(*AI)) { 00458 LRegs.push_back(*AI); 00459 Added = true; 00460 } 00461 } 00462 } 00463 return Added; 00464 } 00465 00466 /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay 00467 /// scheduling of the given node to satisfy live physical register dependencies. 00468 /// If the specific node is the last one that's available to schedule, do 00469 /// whatever is necessary (i.e. backtracking or cloning) to make it possible. 00470 bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU, 00471 SmallVectorImpl<unsigned> &LRegs){ 00472 if (NumLiveRegs == 0) 00473 return false; 00474 00475 SmallSet<unsigned, 4> RegAdded; 00476 // If this node would clobber any "live" register, then it's not ready. 00477 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00478 I != E; ++I) { 00479 if (I->isAssignedRegDep()) { 00480 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs, 00481 RegAdded, LRegs, TRI); 00482 } 00483 } 00484 00485 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) { 00486 if (Node->getOpcode() == ISD::INLINEASM) { 00487 // Inline asm can clobber physical defs. 00488 unsigned NumOps = Node->getNumOperands(); 00489 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue) 00490 --NumOps; // Ignore the glue operand. 00491 00492 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) { 00493 unsigned Flags = 00494 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); 00495 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 00496 00497 ++i; // Skip the ID value. 00498 if (InlineAsm::isRegDefKind(Flags) || 00499 InlineAsm::isRegDefEarlyClobberKind(Flags) || 00500 InlineAsm::isClobberKind(Flags)) { 00501 // Check for def of register or earlyclobber register. 00502 for (; NumVals; --NumVals, ++i) { 00503 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 00504 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 00505 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI); 00506 } 00507 } else 00508 i += NumVals; 00509 } 00510 continue; 00511 } 00512 if (!Node->isMachineOpcode()) 00513 continue; 00514 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode()); 00515 if (!MCID.ImplicitDefs) 00516 continue; 00517 for (const uint16_t *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) { 00518 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI); 00519 } 00520 } 00521 return !LRegs.empty(); 00522 } 00523 00524 00525 /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up 00526 /// schedulers. 00527 void ScheduleDAGFast::ListScheduleBottomUp() { 00528 unsigned CurCycle = 0; 00529 00530 // Release any predecessors of the special Exit node. 00531 ReleasePredecessors(&ExitSU, CurCycle); 00532 00533 // Add root to Available queue. 00534 if (!SUnits.empty()) { 00535 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()]; 00536 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!"); 00537 RootSU->isAvailable = true; 00538 AvailableQueue.push(RootSU); 00539 } 00540 00541 // While Available queue is not empty, grab the node with the highest 00542 // priority. If it is not ready put it back. Schedule the node. 00543 SmallVector<SUnit*, 4> NotReady; 00544 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; 00545 Sequence.reserve(SUnits.size()); 00546 while (!AvailableQueue.empty()) { 00547 bool Delayed = false; 00548 LRegsMap.clear(); 00549 SUnit *CurSU = AvailableQueue.pop(); 00550 while (CurSU) { 00551 SmallVector<unsigned, 4> LRegs; 00552 if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) 00553 break; 00554 Delayed = true; 00555 LRegsMap.insert(std::make_pair(CurSU, LRegs)); 00556 00557 CurSU->isPending = true; // This SU is not in AvailableQueue right now. 00558 NotReady.push_back(CurSU); 00559 CurSU = AvailableQueue.pop(); 00560 } 00561 00562 // All candidates are delayed due to live physical reg dependencies. 00563 // Try code duplication or inserting cross class copies 00564 // to resolve it. 00565 if (Delayed && !CurSU) { 00566 if (!CurSU) { 00567 // Try duplicating the nodes that produces these 00568 // "expensive to copy" values to break the dependency. In case even 00569 // that doesn't work, insert cross class copies. 00570 SUnit *TrySU = NotReady[0]; 00571 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU]; 00572 assert(LRegs.size() == 1 && "Can't handle this yet!"); 00573 unsigned Reg = LRegs[0]; 00574 SUnit *LRDef = LiveRegDefs[Reg]; 00575 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII); 00576 const TargetRegisterClass *RC = 00577 TRI->getMinimalPhysRegClass(Reg, VT); 00578 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC); 00579 00580 // If cross copy register class is the same as RC, then it must be 00581 // possible copy the value directly. Do not try duplicate the def. 00582 // If cross copy register class is not the same as RC, then it's 00583 // possible to copy the value but it require cross register class copies 00584 // and it is expensive. 00585 // If cross copy register class is null, then it's not possible to copy 00586 // the value at all. 00587 SUnit *NewDef = nullptr; 00588 if (DestRC != RC) { 00589 NewDef = CopyAndMoveSuccessors(LRDef); 00590 if (!DestRC && !NewDef) 00591 report_fatal_error("Can't handle live physical " 00592 "register dependency!"); 00593 } 00594 if (!NewDef) { 00595 // Issue copies, these can be expensive cross register class copies. 00596 SmallVector<SUnit*, 2> Copies; 00597 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); 00598 DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum 00599 << " to SU #" << Copies.front()->NodeNum << "\n"); 00600 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial)); 00601 NewDef = Copies.back(); 00602 } 00603 00604 DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum 00605 << " to SU #" << TrySU->NodeNum << "\n"); 00606 LiveRegDefs[Reg] = NewDef; 00607 AddPred(NewDef, SDep(TrySU, SDep::Artificial)); 00608 TrySU->isAvailable = false; 00609 CurSU = NewDef; 00610 } 00611 00612 if (!CurSU) { 00613 llvm_unreachable("Unable to resolve live physical register dependencies!"); 00614 } 00615 } 00616 00617 // Add the nodes that aren't ready back onto the available list. 00618 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { 00619 NotReady[i]->isPending = false; 00620 // May no longer be available due to backtracking. 00621 if (NotReady[i]->isAvailable) 00622 AvailableQueue.push(NotReady[i]); 00623 } 00624 NotReady.clear(); 00625 00626 if (CurSU) 00627 ScheduleNodeBottomUp(CurSU, CurCycle); 00628 ++CurCycle; 00629 } 00630 00631 // Reverse the order since it is bottom up. 00632 std::reverse(Sequence.begin(), Sequence.end()); 00633 00634 #ifndef NDEBUG 00635 VerifyScheduledSequence(/*isBottomUp=*/true); 00636 #endif 00637 } 00638 00639 00640 namespace { 00641 //===----------------------------------------------------------------------===// 00642 // ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the 00643 // DAG in topological order. 00644 // IMPORTANT: this may not work for targets with phyreg dependency. 00645 // 00646 class ScheduleDAGLinearize : public ScheduleDAGSDNodes { 00647 public: 00648 ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {} 00649 00650 void Schedule() override; 00651 00652 MachineBasicBlock * 00653 EmitSchedule(MachineBasicBlock::iterator &InsertPos) override; 00654 00655 private: 00656 std::vector<SDNode*> Sequence; 00657 DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user 00658 00659 void ScheduleNode(SDNode *N); 00660 }; 00661 } // end anonymous namespace 00662 00663 void ScheduleDAGLinearize::ScheduleNode(SDNode *N) { 00664 if (N->getNodeId() != 0) 00665 llvm_unreachable(nullptr); 00666 00667 if (!N->isMachineOpcode() && 00668 (N->getOpcode() == ISD::EntryToken || isPassiveNode(N))) 00669 // These nodes do not need to be translated into MIs. 00670 return; 00671 00672 DEBUG(dbgs() << "\n*** Scheduling: "); 00673 DEBUG(N->dump(DAG)); 00674 Sequence.push_back(N); 00675 00676 unsigned NumOps = N->getNumOperands(); 00677 if (unsigned NumLeft = NumOps) { 00678 SDNode *GluedOpN = nullptr; 00679 do { 00680 const SDValue &Op = N->getOperand(NumLeft-1); 00681 SDNode *OpN = Op.getNode(); 00682 00683 if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) { 00684 // Schedule glue operand right above N. 00685 GluedOpN = OpN; 00686 assert(OpN->getNodeId() != 0 && "Glue operand not ready?"); 00687 OpN->setNodeId(0); 00688 ScheduleNode(OpN); 00689 continue; 00690 } 00691 00692 if (OpN == GluedOpN) 00693 // Glue operand is already scheduled. 00694 continue; 00695 00696 DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN); 00697 if (DI != GluedMap.end() && DI->second != N) 00698 // Users of glues are counted against the glued users. 00699 OpN = DI->second; 00700 00701 unsigned Degree = OpN->getNodeId(); 00702 assert(Degree > 0 && "Predecessor over-released!"); 00703 OpN->setNodeId(--Degree); 00704 if (Degree == 0) 00705 ScheduleNode(OpN); 00706 } while (--NumLeft); 00707 } 00708 } 00709 00710 /// findGluedUser - Find the representative use of a glue value by walking 00711 /// the use chain. 00712 static SDNode *findGluedUser(SDNode *N) { 00713 while (SDNode *Glued = N->getGluedUser()) 00714 N = Glued; 00715 return N; 00716 } 00717 00718 void ScheduleDAGLinearize::Schedule() { 00719 DEBUG(dbgs() << "********** DAG Linearization **********\n"); 00720 00721 SmallVector<SDNode*, 8> Glues; 00722 unsigned DAGSize = 0; 00723 for (SelectionDAG::allnodes_iterator I = DAG->allnodes_begin(), 00724 E = DAG->allnodes_end(); I != E; ++I) { 00725 SDNode *N = I; 00726 00727 // Use node id to record degree. 00728 unsigned Degree = N->use_size(); 00729 N->setNodeId(Degree); 00730 unsigned NumVals = N->getNumValues(); 00731 if (NumVals && N->getValueType(NumVals-1) == MVT::Glue && 00732 N->hasAnyUseOfValue(NumVals-1)) { 00733 SDNode *User = findGluedUser(N); 00734 if (User) { 00735 Glues.push_back(N); 00736 GluedMap.insert(std::make_pair(N, User)); 00737 } 00738 } 00739 00740 if (N->isMachineOpcode() || 00741 (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N))) 00742 ++DAGSize; 00743 } 00744 00745 for (unsigned i = 0, e = Glues.size(); i != e; ++i) { 00746 SDNode *Glue = Glues[i]; 00747 SDNode *GUser = GluedMap[Glue]; 00748 unsigned Degree = Glue->getNodeId(); 00749 unsigned UDegree = GUser->getNodeId(); 00750 00751 // Glue user must be scheduled together with the glue operand. So other 00752 // users of the glue operand must be treated as its users. 00753 SDNode *ImmGUser = Glue->getGluedUser(); 00754 for (SDNode::use_iterator ui = Glue->use_begin(), ue = Glue->use_end(); 00755 ui != ue; ++ui) 00756 if (*ui == ImmGUser) 00757 --Degree; 00758 GUser->setNodeId(UDegree + Degree); 00759 Glue->setNodeId(1); 00760 } 00761 00762 Sequence.reserve(DAGSize); 00763 ScheduleNode(DAG->getRoot().getNode()); 00764 } 00765 00766 MachineBasicBlock* 00767 ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) { 00768 InstrEmitter Emitter(BB, InsertPos); 00769 DenseMap<SDValue, unsigned> VRBaseMap; 00770 00771 DEBUG({ 00772 dbgs() << "\n*** Final schedule ***\n"; 00773 }); 00774 00775 // FIXME: Handle dbg_values. 00776 unsigned NumNodes = Sequence.size(); 00777 for (unsigned i = 0; i != NumNodes; ++i) { 00778 SDNode *N = Sequence[NumNodes-i-1]; 00779 DEBUG(N->dump(DAG)); 00780 Emitter.EmitNode(N, false, false, VRBaseMap); 00781 } 00782 00783 DEBUG(dbgs() << '\n'); 00784 00785 InsertPos = Emitter.getInsertPos(); 00786 return Emitter.getBlock(); 00787 } 00788 00789 //===----------------------------------------------------------------------===// 00790 // Public Constructor Functions 00791 //===----------------------------------------------------------------------===// 00792 00793 llvm::ScheduleDAGSDNodes * 00794 llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) { 00795 return new ScheduleDAGFast(*IS->MF); 00796 } 00797 00798 llvm::ScheduleDAGSDNodes * 00799 llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) { 00800 return new ScheduleDAGLinearize(*IS->MF); 00801 }