xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 //===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===//
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 the ScheduleDAG class, which is a base class used by
10 // scheduling implementation classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ScheduleDAGSDNodes.h"
15 #include "InstrEmitter.h"
16 #include "SDNodeDbgValue.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/TargetInstrInfo.h"
26 #include "llvm/CodeGen/TargetLowering.h"
27 #include "llvm/CodeGen/TargetRegisterInfo.h"
28 #include "llvm/CodeGen/TargetSubtargetInfo.h"
29 #include "llvm/Config/llvm-config.h"
30 #include "llvm/MC/MCInstrItineraries.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetMachine.h"
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "pre-RA-sched"
38 
39 STATISTIC(LoadsClustered, "Number of loads clustered together");
40 
41 // This allows the latency-based scheduler to notice high latency instructions
42 // without a target itinerary. The choice of number here has more to do with
43 // balancing scheduler heuristics than with the actual machine latency.
44 static cl::opt<int> HighLatencyCycles(
45   "sched-high-latency-cycles", cl::Hidden, cl::init(10),
46   cl::desc("Roughly estimate the number of cycles that 'long latency'"
47            "instructions take for targets with no itinerary"));
48 
49 ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
50     : ScheduleDAG(mf), InstrItins(mf.getSubtarget().getInstrItineraryData()) {}
51 
52 /// Run - perform scheduling.
53 ///
54 void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb) {
55   BB = bb;
56   DAG = dag;
57 
58   // Clear the scheduler's SUnit DAG.
59   ScheduleDAG::clearDAG();
60   Sequence.clear();
61 
62   // Invoke the target's selection of scheduler.
63   Schedule();
64 }
65 
66 /// NewSUnit - Creates a new SUnit and return a ptr to it.
67 ///
68 SUnit *ScheduleDAGSDNodes::newSUnit(SDNode *N) {
69 #ifndef NDEBUG
70   const SUnit *Addr = nullptr;
71   if (!SUnits.empty())
72     Addr = &SUnits[0];
73 #endif
74   SUnits.emplace_back(N, (unsigned)SUnits.size());
75   assert((Addr == nullptr || Addr == &SUnits[0]) &&
76          "SUnits std::vector reallocated on the fly!");
77   SUnits.back().OrigNode = &SUnits.back();
78   SUnit *SU = &SUnits.back();
79   const TargetLowering &TLI = DAG->getTargetLoweringInfo();
80   if (!N ||
81       (N->isMachineOpcode() &&
82        N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF))
83     SU->SchedulingPref = Sched::None;
84   else
85     SU->SchedulingPref = TLI.getSchedulingPreference(N);
86   return SU;
87 }
88 
89 SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
90   SUnit *SU = newSUnit(Old->getNode());
91   SU->OrigNode = Old->OrigNode;
92   SU->Latency = Old->Latency;
93   SU->isVRegCycle = Old->isVRegCycle;
94   SU->isCall = Old->isCall;
95   SU->isCallOp = Old->isCallOp;
96   SU->isTwoAddress = Old->isTwoAddress;
97   SU->isCommutable = Old->isCommutable;
98   SU->hasPhysRegDefs = Old->hasPhysRegDefs;
99   SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
100   SU->isScheduleHigh = Old->isScheduleHigh;
101   SU->isScheduleLow = Old->isScheduleLow;
102   SU->SchedulingPref = Old->SchedulingPref;
103   Old->isCloned = true;
104   return SU;
105 }
106 
107 /// CheckForPhysRegDependency - Check if the dependency between def and use of
108 /// a specified operand is a physical register dependency. If so, returns the
109 /// register and the cost of copying the register.
110 static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
111                                       const TargetRegisterInfo *TRI,
112                                       const TargetInstrInfo *TII,
113                                       unsigned &PhysReg, int &Cost) {
114   if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
115     return;
116 
117   unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
118   if (Register::isVirtualRegister(Reg))
119     return;
120 
121   unsigned ResNo = User->getOperand(2).getResNo();
122   if (Def->getOpcode() == ISD::CopyFromReg &&
123       cast<RegisterSDNode>(Def->getOperand(1))->getReg() == Reg) {
124     PhysReg = Reg;
125   } else if (Def->isMachineOpcode()) {
126     const MCInstrDesc &II = TII->get(Def->getMachineOpcode());
127     if (ResNo >= II.getNumDefs() && II.hasImplicitDefOfPhysReg(Reg))
128       PhysReg = Reg;
129   }
130 
131   if (PhysReg != 0) {
132     const TargetRegisterClass *RC =
133         TRI->getMinimalPhysRegClass(Reg, Def->getSimpleValueType(ResNo));
134     Cost = RC->getCopyCost();
135   }
136 }
137 
138 // Helper for AddGlue to clone node operands.
139 static void CloneNodeWithValues(SDNode *N, SelectionDAG *DAG, ArrayRef<EVT> VTs,
140                                 SDValue ExtraOper = SDValue()) {
141   SmallVector<SDValue, 8> Ops(N->op_begin(), N->op_end());
142   if (ExtraOper.getNode())
143     Ops.push_back(ExtraOper);
144 
145   SDVTList VTList = DAG->getVTList(VTs);
146   MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
147 
148   // Store memory references.
149   SmallVector<MachineMemOperand *, 2> MMOs;
150   if (MN)
151     MMOs.assign(MN->memoperands_begin(), MN->memoperands_end());
152 
153   DAG->MorphNodeTo(N, N->getOpcode(), VTList, Ops);
154 
155   // Reset the memory references
156   if (MN)
157     DAG->setNodeMemRefs(MN, MMOs);
158 }
159 
160 static bool AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
161   SDNode *GlueDestNode = Glue.getNode();
162 
163   // Don't add glue from a node to itself.
164   if (GlueDestNode == N) return false;
165 
166   // Don't add a glue operand to something that already uses glue.
167   if (GlueDestNode &&
168       N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
169     return false;
170   }
171   // Don't add glue to something that already has a glue value.
172   if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return false;
173 
174   SmallVector<EVT, 4> VTs(N->values());
175   if (AddGlue)
176     VTs.push_back(MVT::Glue);
177 
178   CloneNodeWithValues(N, DAG, VTs, Glue);
179 
180   return true;
181 }
182 
183 // Cleanup after unsuccessful AddGlue. Use the standard method of morphing the
184 // node even though simply shrinking the value list is sufficient.
185 static void RemoveUnusedGlue(SDNode *N, SelectionDAG *DAG) {
186   assert((N->getValueType(N->getNumValues() - 1) == MVT::Glue &&
187           !N->hasAnyUseOfValue(N->getNumValues() - 1)) &&
188          "expected an unused glue value");
189 
190   CloneNodeWithValues(N, DAG,
191                       makeArrayRef(N->value_begin(), N->getNumValues() - 1));
192 }
193 
194 /// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
195 /// This function finds loads of the same base and different offsets. If the
196 /// offsets are not far apart (target specific), it add MVT::Glue inputs and
197 /// outputs to ensure they are scheduled together and in order. This
198 /// optimization may benefit some targets by improving cache locality.
199 void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
200   SDValue Chain;
201   unsigned NumOps = Node->getNumOperands();
202   if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
203     Chain = Node->getOperand(NumOps-1);
204   if (!Chain)
205     return;
206 
207   // Skip any load instruction that has a tied input. There may be an additional
208   // dependency requiring a different order than by increasing offsets, and the
209   // added glue may introduce a cycle.
210   auto hasTiedInput = [this](const SDNode *N) {
211     const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
212     for (unsigned I = 0; I != MCID.getNumOperands(); ++I) {
213       if (MCID.getOperandConstraint(I, MCOI::TIED_TO) != -1)
214         return true;
215     }
216 
217     return false;
218   };
219 
220   // Look for other loads of the same chain. Find loads that are loading from
221   // the same base pointer and different offsets.
222   SmallPtrSet<SDNode*, 16> Visited;
223   SmallVector<int64_t, 4> Offsets;
224   DenseMap<long long, SDNode*> O2SMap;  // Map from offset to SDNode.
225   bool Cluster = false;
226   SDNode *Base = Node;
227 
228   if (hasTiedInput(Base))
229     return;
230 
231   // This algorithm requires a reasonably low use count before finding a match
232   // to avoid uselessly blowing up compile time in large blocks.
233   unsigned UseCount = 0;
234   for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
235        I != E && UseCount < 100; ++I, ++UseCount) {
236     if (I.getUse().getResNo() != Chain.getResNo())
237       continue;
238 
239     SDNode *User = *I;
240     if (User == Node || !Visited.insert(User).second)
241       continue;
242     int64_t Offset1, Offset2;
243     if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
244         Offset1 == Offset2 ||
245         hasTiedInput(User)) {
246       // FIXME: Should be ok if they addresses are identical. But earlier
247       // optimizations really should have eliminated one of the loads.
248       continue;
249     }
250     if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
251       Offsets.push_back(Offset1);
252     O2SMap.insert(std::make_pair(Offset2, User));
253     Offsets.push_back(Offset2);
254     if (Offset2 < Offset1)
255       Base = User;
256     Cluster = true;
257     // Reset UseCount to allow more matches.
258     UseCount = 0;
259   }
260 
261   if (!Cluster)
262     return;
263 
264   // Sort them in increasing order.
265   llvm::sort(Offsets);
266 
267   // Check if the loads are close enough.
268   SmallVector<SDNode*, 4> Loads;
269   unsigned NumLoads = 0;
270   int64_t BaseOff = Offsets[0];
271   SDNode *BaseLoad = O2SMap[BaseOff];
272   Loads.push_back(BaseLoad);
273   for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
274     int64_t Offset = Offsets[i];
275     SDNode *Load = O2SMap[Offset];
276     if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
277       break; // Stop right here. Ignore loads that are further away.
278     Loads.push_back(Load);
279     ++NumLoads;
280   }
281 
282   if (NumLoads == 0)
283     return;
284 
285   // Cluster loads by adding MVT::Glue outputs and inputs. This also
286   // ensure they are scheduled in order of increasing addresses.
287   SDNode *Lead = Loads[0];
288   SDValue InGlue;
289   if (AddGlue(Lead, InGlue, true, DAG))
290     InGlue = SDValue(Lead, Lead->getNumValues() - 1);
291   for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
292     bool OutGlue = I < E - 1;
293     SDNode *Load = Loads[I];
294 
295     // If AddGlue fails, we could leave an unsused glue value. This should not
296     // cause any
297     if (AddGlue(Load, InGlue, OutGlue, DAG)) {
298       if (OutGlue)
299         InGlue = SDValue(Load, Load->getNumValues() - 1);
300 
301       ++LoadsClustered;
302     }
303     else if (!OutGlue && InGlue.getNode())
304       RemoveUnusedGlue(InGlue.getNode(), DAG);
305   }
306 }
307 
308 /// ClusterNodes - Cluster certain nodes which should be scheduled together.
309 ///
310 void ScheduleDAGSDNodes::ClusterNodes() {
311   for (SDNode &NI : DAG->allnodes()) {
312     SDNode *Node = &NI;
313     if (!Node || !Node->isMachineOpcode())
314       continue;
315 
316     unsigned Opc = Node->getMachineOpcode();
317     const MCInstrDesc &MCID = TII->get(Opc);
318     if (MCID.mayLoad())
319       // Cluster loads from "near" addresses into combined SUnits.
320       ClusterNeighboringLoads(Node);
321   }
322 }
323 
324 void ScheduleDAGSDNodes::BuildSchedUnits() {
325   // During scheduling, the NodeId field of SDNode is used to map SDNodes
326   // to their associated SUnits by holding SUnits table indices. A value
327   // of -1 means the SDNode does not yet have an associated SUnit.
328   unsigned NumNodes = 0;
329   for (SDNode &NI : DAG->allnodes()) {
330     NI.setNodeId(-1);
331     ++NumNodes;
332   }
333 
334   // Reserve entries in the vector for each of the SUnits we are creating.  This
335   // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
336   // invalidated.
337   // FIXME: Multiply by 2 because we may clone nodes during scheduling.
338   // This is a temporary workaround.
339   SUnits.reserve(NumNodes * 2);
340 
341   // Add all nodes in depth first order.
342   SmallVector<SDNode*, 64> Worklist;
343   SmallPtrSet<SDNode*, 32> Visited;
344   Worklist.push_back(DAG->getRoot().getNode());
345   Visited.insert(DAG->getRoot().getNode());
346 
347   SmallVector<SUnit*, 8> CallSUnits;
348   while (!Worklist.empty()) {
349     SDNode *NI = Worklist.pop_back_val();
350 
351     // Add all operands to the worklist unless they've already been added.
352     for (const SDValue &Op : NI->op_values())
353       if (Visited.insert(Op.getNode()).second)
354         Worklist.push_back(Op.getNode());
355 
356     if (isPassiveNode(NI))  // Leaf node, e.g. a TargetImmediate.
357       continue;
358 
359     // If this node has already been processed, stop now.
360     if (NI->getNodeId() != -1) continue;
361 
362     SUnit *NodeSUnit = newSUnit(NI);
363 
364     // See if anything is glued to this node, if so, add them to glued
365     // nodes.  Nodes can have at most one glue input and one glue output.  Glue
366     // is required to be the last operand and result of a node.
367 
368     // Scan up to find glued preds.
369     SDNode *N = NI;
370     while (N->getNumOperands() &&
371            N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
372       N = N->getOperand(N->getNumOperands()-1).getNode();
373       assert(N->getNodeId() == -1 && "Node already inserted!");
374       N->setNodeId(NodeSUnit->NodeNum);
375       if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
376         NodeSUnit->isCall = true;
377     }
378 
379     // Scan down to find any glued succs.
380     N = NI;
381     while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
382       SDValue GlueVal(N, N->getNumValues()-1);
383 
384       // There are either zero or one users of the Glue result.
385       bool HasGlueUse = false;
386       for (SDNode *U : N->uses())
387         if (GlueVal.isOperandOf(U)) {
388           HasGlueUse = true;
389           assert(N->getNodeId() == -1 && "Node already inserted!");
390           N->setNodeId(NodeSUnit->NodeNum);
391           N = U;
392           if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
393             NodeSUnit->isCall = true;
394           break;
395         }
396       if (!HasGlueUse) break;
397     }
398 
399     if (NodeSUnit->isCall)
400       CallSUnits.push_back(NodeSUnit);
401 
402     // Schedule zero-latency TokenFactor below any nodes that may increase the
403     // schedule height. Otherwise, ancestors of the TokenFactor may appear to
404     // have false stalls.
405     if (NI->getOpcode() == ISD::TokenFactor)
406       NodeSUnit->isScheduleLow = true;
407 
408     // If there are glue operands involved, N is now the bottom-most node
409     // of the sequence of nodes that are glued together.
410     // Update the SUnit.
411     NodeSUnit->setNode(N);
412     assert(N->getNodeId() == -1 && "Node already inserted!");
413     N->setNodeId(NodeSUnit->NodeNum);
414 
415     // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
416     InitNumRegDefsLeft(NodeSUnit);
417 
418     // Assign the Latency field of NodeSUnit using target-provided information.
419     computeLatency(NodeSUnit);
420   }
421 
422   // Find all call operands.
423   while (!CallSUnits.empty()) {
424     SUnit *SU = CallSUnits.pop_back_val();
425     for (const SDNode *SUNode = SU->getNode(); SUNode;
426          SUNode = SUNode->getGluedNode()) {
427       if (SUNode->getOpcode() != ISD::CopyToReg)
428         continue;
429       SDNode *SrcN = SUNode->getOperand(2).getNode();
430       if (isPassiveNode(SrcN)) continue;   // Not scheduled.
431       SUnit *SrcSU = &SUnits[SrcN->getNodeId()];
432       SrcSU->isCallOp = true;
433     }
434   }
435 }
436 
437 void ScheduleDAGSDNodes::AddSchedEdges() {
438   const TargetSubtargetInfo &ST = MF.getSubtarget();
439 
440   // Check to see if the scheduler cares about latencies.
441   bool UnitLatencies = forceUnitLatencies();
442 
443   // Pass 2: add the preds, succs, etc.
444   for (SUnit &SU : SUnits) {
445     SDNode *MainNode = SU.getNode();
446 
447     if (MainNode->isMachineOpcode()) {
448       unsigned Opc = MainNode->getMachineOpcode();
449       const MCInstrDesc &MCID = TII->get(Opc);
450       for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
451         if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
452           SU.isTwoAddress = true;
453           break;
454         }
455       }
456       if (MCID.isCommutable())
457         SU.isCommutable = true;
458     }
459 
460     // Find all predecessors and successors of the group.
461     for (SDNode *N = SU.getNode(); N; N = N->getGluedNode()) {
462       if (N->isMachineOpcode() &&
463           TII->get(N->getMachineOpcode()).getImplicitDefs()) {
464         SU.hasPhysRegClobbers = true;
465         unsigned NumUsed = InstrEmitter::CountResults(N);
466         while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
467           --NumUsed;    // Skip over unused values at the end.
468         if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
469           SU.hasPhysRegDefs = true;
470       }
471 
472       for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
473         SDNode *OpN = N->getOperand(i).getNode();
474         unsigned DefIdx = N->getOperand(i).getResNo();
475         if (isPassiveNode(OpN)) continue;   // Not scheduled.
476         SUnit *OpSU = &SUnits[OpN->getNodeId()];
477         assert(OpSU && "Node has no SUnit!");
478         if (OpSU == &SU)
479           continue; // In the same group.
480 
481         EVT OpVT = N->getOperand(i).getValueType();
482         assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
483         bool isChain = OpVT == MVT::Other;
484 
485         unsigned PhysReg = 0;
486         int Cost = 1;
487         // Determine if this is a physical register dependency.
488         CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
489         assert((PhysReg == 0 || !isChain) &&
490                "Chain dependence via physreg data?");
491         // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
492         // emits a copy from the physical register to a virtual register unless
493         // it requires a cross class copy (cost < 0). That means we are only
494         // treating "expensive to copy" register dependency as physical register
495         // dependency. This may change in the future though.
496         if (Cost >= 0 && !StressSched)
497           PhysReg = 0;
498 
499         // If this is a ctrl dep, latency is 1.
500         unsigned OpLatency = isChain ? 1 : OpSU->Latency;
501         // Special-case TokenFactor chains as zero-latency.
502         if(isChain && OpN->getOpcode() == ISD::TokenFactor)
503           OpLatency = 0;
504 
505         SDep Dep = isChain ? SDep(OpSU, SDep::Barrier)
506           : SDep(OpSU, SDep::Data, PhysReg);
507         Dep.setLatency(OpLatency);
508         if (!isChain && !UnitLatencies) {
509           computeOperandLatency(OpN, N, i, Dep);
510           ST.adjustSchedDependency(OpSU, DefIdx, &SU, i, Dep);
511         }
512 
513         if (!SU.addPred(Dep) && !Dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
514           // Multiple register uses are combined in the same SUnit. For example,
515           // we could have a set of glued nodes with all their defs consumed by
516           // another set of glued nodes. Register pressure tracking sees this as
517           // a single use, so to keep pressure balanced we reduce the defs.
518           //
519           // We can't tell (without more book-keeping) if this results from
520           // glued nodes or duplicate operands. As long as we don't reduce
521           // NumRegDefsLeft to zero, we handle the common cases well.
522           --OpSU->NumRegDefsLeft;
523         }
524       }
525     }
526   }
527 }
528 
529 /// BuildSchedGraph - Build the SUnit graph from the selection dag that we
530 /// are input.  This SUnit graph is similar to the SelectionDAG, but
531 /// excludes nodes that aren't interesting to scheduling, and represents
532 /// glued together nodes with a single SUnit.
533 void ScheduleDAGSDNodes::BuildSchedGraph(AAResults *AA) {
534   // Cluster certain nodes which should be scheduled together.
535   ClusterNodes();
536   // Populate the SUnits array.
537   BuildSchedUnits();
538   // Compute all the scheduling dependencies between nodes.
539   AddSchedEdges();
540 }
541 
542 // Initialize NumNodeDefs for the current Node's opcode.
543 void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
544   // Check for phys reg copy.
545   if (!Node)
546     return;
547 
548   if (!Node->isMachineOpcode()) {
549     if (Node->getOpcode() == ISD::CopyFromReg)
550       NodeNumDefs = 1;
551     else
552       NodeNumDefs = 0;
553     return;
554   }
555   unsigned POpc = Node->getMachineOpcode();
556   if (POpc == TargetOpcode::IMPLICIT_DEF) {
557     // No register need be allocated for this.
558     NodeNumDefs = 0;
559     return;
560   }
561   if (POpc == TargetOpcode::PATCHPOINT &&
562       Node->getValueType(0) == MVT::Other) {
563     // PATCHPOINT is defined to have one result, but it might really have none
564     // if we're not using CallingConv::AnyReg. Don't mistake the chain for a
565     // real definition.
566     NodeNumDefs = 0;
567     return;
568   }
569   unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
570   // Some instructions define regs that are not represented in the selection DAG
571   // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
572   NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
573   DefIdx = 0;
574 }
575 
576 // Construct a RegDefIter for this SUnit and find the first valid value.
577 ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
578                                            const ScheduleDAGSDNodes *SD)
579     : SchedDAG(SD), Node(SU->getNode()) {
580   InitNodeNumDefs();
581   Advance();
582 }
583 
584 // Advance to the next valid value defined by the SUnit.
585 void ScheduleDAGSDNodes::RegDefIter::Advance() {
586   for (;Node;) { // Visit all glued nodes.
587     for (;DefIdx < NodeNumDefs; ++DefIdx) {
588       if (!Node->hasAnyUseOfValue(DefIdx))
589         continue;
590       ValueType = Node->getSimpleValueType(DefIdx);
591       ++DefIdx;
592       return; // Found a normal regdef.
593     }
594     Node = Node->getGluedNode();
595     if (!Node) {
596       return; // No values left to visit.
597     }
598     InitNodeNumDefs();
599   }
600 }
601 
602 void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
603   assert(SU->NumRegDefsLeft == 0 && "expect a new node");
604   for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
605     assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
606     ++SU->NumRegDefsLeft;
607   }
608 }
609 
610 void ScheduleDAGSDNodes::computeLatency(SUnit *SU) {
611   SDNode *N = SU->getNode();
612 
613   // TokenFactor operands are considered zero latency, and some schedulers
614   // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
615   // whenever node latency is nonzero.
616   if (N && N->getOpcode() == ISD::TokenFactor) {
617     SU->Latency = 0;
618     return;
619   }
620 
621   // Check to see if the scheduler cares about latencies.
622   if (forceUnitLatencies()) {
623     SU->Latency = 1;
624     return;
625   }
626 
627   if (!InstrItins || InstrItins->isEmpty()) {
628     if (N && N->isMachineOpcode() &&
629         TII->isHighLatencyDef(N->getMachineOpcode()))
630       SU->Latency = HighLatencyCycles;
631     else
632       SU->Latency = 1;
633     return;
634   }
635 
636   // Compute the latency for the node.  We use the sum of the latencies for
637   // all nodes glued together into this SUnit.
638   SU->Latency = 0;
639   for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
640     if (N->isMachineOpcode())
641       SU->Latency += TII->getInstrLatency(InstrItins, N);
642 }
643 
644 void ScheduleDAGSDNodes::computeOperandLatency(SDNode *Def, SDNode *Use,
645                                                unsigned OpIdx, SDep& dep) const{
646   // Check to see if the scheduler cares about latencies.
647   if (forceUnitLatencies())
648     return;
649 
650   if (dep.getKind() != SDep::Data)
651     return;
652 
653   unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
654   if (Use->isMachineOpcode())
655     // Adjust the use operand index by num of defs.
656     OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
657   int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
658   if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
659       !BB->succ_empty()) {
660     unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
661     if (Register::isVirtualRegister(Reg))
662       // This copy is a liveout value. It is likely coalesced, so reduce the
663       // latency so not to penalize the def.
664       // FIXME: need target specific adjustment here?
665       Latency = (Latency > 1) ? Latency - 1 : 1;
666   }
667   if (Latency >= 0)
668     dep.setLatency(Latency);
669 }
670 
671 void ScheduleDAGSDNodes::dumpNode(const SUnit &SU) const {
672 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
673   dumpNodeName(SU);
674   dbgs() << ": ";
675 
676   if (!SU.getNode()) {
677     dbgs() << "PHYS REG COPY\n";
678     return;
679   }
680 
681   SU.getNode()->dump(DAG);
682   dbgs() << "\n";
683   SmallVector<SDNode *, 4> GluedNodes;
684   for (SDNode *N = SU.getNode()->getGluedNode(); N; N = N->getGluedNode())
685     GluedNodes.push_back(N);
686   while (!GluedNodes.empty()) {
687     dbgs() << "    ";
688     GluedNodes.back()->dump(DAG);
689     dbgs() << "\n";
690     GluedNodes.pop_back();
691   }
692 #endif
693 }
694 
695 void ScheduleDAGSDNodes::dump() const {
696 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
697   if (EntrySU.getNode() != nullptr)
698     dumpNodeAll(EntrySU);
699   for (const SUnit &SU : SUnits)
700     dumpNodeAll(SU);
701   if (ExitSU.getNode() != nullptr)
702     dumpNodeAll(ExitSU);
703 #endif
704 }
705 
706 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
707 void ScheduleDAGSDNodes::dumpSchedule() const {
708   for (const SUnit *SU : Sequence) {
709     if (SU)
710       dumpNode(*SU);
711     else
712       dbgs() << "**** NOOP ****\n";
713   }
714 }
715 #endif
716 
717 #ifndef NDEBUG
718 /// VerifyScheduledSequence - Verify that all SUnits were scheduled and that
719 /// their state is consistent with the nodes listed in Sequence.
720 ///
721 void ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) {
722   unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp);
723   unsigned Noops = llvm::count(Sequence, nullptr);
724   assert(Sequence.size() - Noops == ScheduledNodes &&
725          "The number of nodes scheduled doesn't match the expected number!");
726 }
727 #endif // NDEBUG
728 
729 /// ProcessSDDbgValues - Process SDDbgValues associated with this node.
730 static void
731 ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter,
732                    SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders,
733                    DenseMap<SDValue, Register> &VRBaseMap, unsigned Order) {
734   if (!N->getHasDebugValue())
735     return;
736 
737   /// Returns true if \p DV has any VReg operand locations which don't exist in
738   /// VRBaseMap.
739   auto HasUnknownVReg = [&VRBaseMap](SDDbgValue *DV) {
740     for (const SDDbgOperand &L : DV->getLocationOps()) {
741       if (L.getKind() == SDDbgOperand::SDNODE &&
742           VRBaseMap.count({L.getSDNode(), L.getResNo()}) == 0)
743         return true;
744     }
745     return false;
746   };
747 
748   // Opportunistically insert immediate dbg_value uses, i.e. those with the same
749   // source order number as N.
750   MachineBasicBlock *BB = Emitter.getBlock();
751   MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
752   for (auto DV : DAG->GetDbgValues(N)) {
753     if (DV->isEmitted())
754       continue;
755     unsigned DVOrder = DV->getOrder();
756     if (Order != 0 && DVOrder != Order)
757       continue;
758     // If DV has any VReg location operands which haven't been mapped then
759     // either that node is no longer available or we just haven't visited the
760     // node yet. In the former case we should emit an undef dbg_value, but we
761     // can do it later. And for the latter we'll want to wait until all
762     // dependent nodes have been visited.
763     if (!DV->isInvalidated() && HasUnknownVReg(DV))
764       continue;
765     MachineInstr *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap);
766     if (!DbgMI)
767       continue;
768     Orders.push_back({DVOrder, DbgMI});
769     BB->insert(InsertPos, DbgMI);
770   }
771 }
772 
773 // ProcessSourceNode - Process nodes with source order numbers. These are added
774 // to a vector which EmitSchedule uses to determine how to insert dbg_value
775 // instructions in the right order.
776 static void
777 ProcessSourceNode(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter,
778                   DenseMap<SDValue, Register> &VRBaseMap,
779                   SmallVectorImpl<std::pair<unsigned, MachineInstr *>> &Orders,
780                   SmallSet<Register, 8> &Seen, MachineInstr *NewInsn) {
781   unsigned Order = N->getIROrder();
782   if (!Order || Seen.count(Order)) {
783     // Process any valid SDDbgValues even if node does not have any order
784     // assigned.
785     ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
786     return;
787   }
788 
789   // If a new instruction was generated for this Order number, record it.
790   // Otherwise, leave this order number unseen: we will either find later
791   // instructions for it, or leave it unseen if there were no instructions at
792   // all.
793   if (NewInsn) {
794     Seen.insert(Order);
795     Orders.push_back({Order, NewInsn});
796   }
797 
798   // Even if no instruction was generated, a Value may have become defined via
799   // earlier nodes. Try to process them now.
800   ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
801 }
802 
803 void ScheduleDAGSDNodes::
804 EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, Register> &VRBaseMap,
805                 MachineBasicBlock::iterator InsertPos) {
806   for (const SDep &Pred : SU->Preds) {
807     if (Pred.isCtrl())
808       continue; // ignore chain preds
809     if (Pred.getSUnit()->CopyDstRC) {
810       // Copy to physical register.
811       DenseMap<SUnit *, Register>::iterator VRI =
812           VRBaseMap.find(Pred.getSUnit());
813       assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
814       // Find the destination physical register.
815       Register Reg;
816       for (const SDep &Succ : SU->Succs) {
817         if (Succ.isCtrl())
818           continue; // ignore chain preds
819         if (Succ.getReg()) {
820           Reg = Succ.getReg();
821           break;
822         }
823       }
824       BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), Reg)
825         .addReg(VRI->second);
826     } else {
827       // Copy from physical register.
828       assert(Pred.getReg() && "Unknown physical register!");
829       Register VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
830       bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second;
831       (void)isNew; // Silence compiler warning.
832       assert(isNew && "Node emitted out of order - early");
833       BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), VRBase)
834           .addReg(Pred.getReg());
835     }
836     break;
837   }
838 }
839 
840 /// EmitSchedule - Emit the machine code in scheduled order. Return the new
841 /// InsertPos and MachineBasicBlock that contains this insertion
842 /// point. ScheduleDAGSDNodes holds a BB pointer for convenience, but this does
843 /// not necessarily refer to returned BB. The emitter may split blocks.
844 MachineBasicBlock *ScheduleDAGSDNodes::
845 EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
846   InstrEmitter Emitter(DAG->getTarget(), BB, InsertPos,
847                        DAG->getUseInstrRefDebugInfo());
848   DenseMap<SDValue, Register> VRBaseMap;
849   DenseMap<SUnit*, Register> CopyVRBaseMap;
850   SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
851   SmallSet<Register, 8> Seen;
852   bool HasDbg = DAG->hasDebugValues();
853 
854   // Emit a node, and determine where its first instruction is for debuginfo.
855   // Zero, one, or multiple instructions can be created when emitting a node.
856   auto EmitNode =
857       [&](SDNode *Node, bool IsClone, bool IsCloned,
858           DenseMap<SDValue, Register> &VRBaseMap) -> MachineInstr * {
859     // Fetch instruction prior to this, or end() if nonexistant.
860     auto GetPrevInsn = [&](MachineBasicBlock::iterator I) {
861       if (I == BB->begin())
862         return BB->end();
863       else
864         return std::prev(Emitter.getInsertPos());
865     };
866 
867     MachineBasicBlock::iterator Before = GetPrevInsn(Emitter.getInsertPos());
868     Emitter.EmitNode(Node, IsClone, IsCloned, VRBaseMap);
869     MachineBasicBlock::iterator After = GetPrevInsn(Emitter.getInsertPos());
870 
871     // If the iterator did not change, no instructions were inserted.
872     if (Before == After)
873       return nullptr;
874 
875     MachineInstr *MI;
876     if (Before == BB->end()) {
877       // There were no prior instructions; the new ones must start at the
878       // beginning of the block.
879       MI = &Emitter.getBlock()->instr_front();
880     } else {
881       // Return first instruction after the pre-existing instructions.
882       MI = &*std::next(Before);
883     }
884 
885     if (MI->isCandidateForCallSiteEntry() &&
886         DAG->getTarget().Options.EmitCallSiteInfo)
887       MF.addCallArgsForwardingRegs(MI, DAG->getSDCallSiteInfo(Node));
888 
889     if (DAG->getNoMergeSiteInfo(Node)) {
890       MI->setFlag(MachineInstr::MIFlag::NoMerge);
891     }
892 
893     return MI;
894   };
895 
896   // If this is the first BB, emit byval parameter dbg_value's.
897   if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
898     SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
899     SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
900     for (; PDI != PDE; ++PDI) {
901       MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
902       if (DbgMI) {
903         BB->insert(InsertPos, DbgMI);
904         // We re-emit the dbg_value closer to its use, too, after instructions
905         // are emitted to the BB.
906         (*PDI)->clearIsEmitted();
907       }
908     }
909   }
910 
911   for (SUnit *SU : Sequence) {
912     if (!SU) {
913       // Null SUnit* is a noop.
914       TII->insertNoop(*Emitter.getBlock(), InsertPos);
915       continue;
916     }
917 
918     // For pre-regalloc scheduling, create instructions corresponding to the
919     // SDNode and any glued SDNodes and append them to the block.
920     if (!SU->getNode()) {
921       // Emit a copy.
922       EmitPhysRegCopy(SU, CopyVRBaseMap, InsertPos);
923       continue;
924     }
925 
926     SmallVector<SDNode *, 4> GluedNodes;
927     for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
928       GluedNodes.push_back(N);
929     while (!GluedNodes.empty()) {
930       SDNode *N = GluedNodes.back();
931       auto NewInsn = EmitNode(N, SU->OrigNode != SU, SU->isCloned, VRBaseMap);
932       // Remember the source order of the inserted instruction.
933       if (HasDbg)
934         ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen, NewInsn);
935 
936       if (MDNode *MD = DAG->getHeapAllocSite(N))
937         if (NewInsn && NewInsn->isCall())
938           NewInsn->setHeapAllocMarker(MF, MD);
939 
940       GluedNodes.pop_back();
941     }
942     auto NewInsn =
943         EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
944     // Remember the source order of the inserted instruction.
945     if (HasDbg)
946       ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders, Seen,
947                         NewInsn);
948 
949     if (MDNode *MD = DAG->getHeapAllocSite(SU->getNode())) {
950       if (NewInsn && NewInsn->isCall())
951         NewInsn->setHeapAllocMarker(MF, MD);
952     }
953   }
954 
955   // Insert all the dbg_values which have not already been inserted in source
956   // order sequence.
957   if (HasDbg) {
958     MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
959 
960     // Sort the source order instructions and use the order to insert debug
961     // values. Use stable_sort so that DBG_VALUEs are inserted in the same order
962     // regardless of the host's implementation fo std::sort.
963     llvm::stable_sort(Orders, less_first());
964     std::stable_sort(DAG->DbgBegin(), DAG->DbgEnd(),
965                      [](const SDDbgValue *LHS, const SDDbgValue *RHS) {
966                        return LHS->getOrder() < RHS->getOrder();
967                      });
968 
969     SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
970     SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
971     // Now emit the rest according to source order.
972     unsigned LastOrder = 0;
973     for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
974       unsigned Order = Orders[i].first;
975       MachineInstr *MI = Orders[i].second;
976       // Insert all SDDbgValue's whose order(s) are before "Order".
977       assert(MI);
978       for (; DI != DE; ++DI) {
979         if ((*DI)->getOrder() < LastOrder || (*DI)->getOrder() >= Order)
980           break;
981         if ((*DI)->isEmitted())
982           continue;
983 
984         MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
985         if (DbgMI) {
986           if (!LastOrder)
987             // Insert to start of the BB (after PHIs).
988             BB->insert(BBBegin, DbgMI);
989           else {
990             // Insert at the instruction, which may be in a different
991             // block, if the block was split by a custom inserter.
992             MachineBasicBlock::iterator Pos = MI;
993             MI->getParent()->insert(Pos, DbgMI);
994           }
995         }
996       }
997       LastOrder = Order;
998     }
999     // Add trailing DbgValue's before the terminator. FIXME: May want to add
1000     // some of them before one or more conditional branches?
1001     SmallVector<MachineInstr*, 8> DbgMIs;
1002     for (; DI != DE; ++DI) {
1003       if ((*DI)->isEmitted())
1004         continue;
1005       assert((*DI)->getOrder() >= LastOrder &&
1006              "emitting DBG_VALUE out of order");
1007       if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap))
1008         DbgMIs.push_back(DbgMI);
1009     }
1010 
1011     MachineBasicBlock *InsertBB = Emitter.getBlock();
1012     MachineBasicBlock::iterator Pos = InsertBB->getFirstTerminator();
1013     InsertBB->insert(Pos, DbgMIs.begin(), DbgMIs.end());
1014 
1015     SDDbgInfo::DbgLabelIterator DLI = DAG->DbgLabelBegin();
1016     SDDbgInfo::DbgLabelIterator DLE = DAG->DbgLabelEnd();
1017     // Now emit the rest according to source order.
1018     LastOrder = 0;
1019     for (const auto &InstrOrder : Orders) {
1020       unsigned Order = InstrOrder.first;
1021       MachineInstr *MI = InstrOrder.second;
1022       if (!MI)
1023         continue;
1024 
1025       // Insert all SDDbgLabel's whose order(s) are before "Order".
1026       for (; DLI != DLE &&
1027              (*DLI)->getOrder() >= LastOrder && (*DLI)->getOrder() < Order;
1028              ++DLI) {
1029         MachineInstr *DbgMI = Emitter.EmitDbgLabel(*DLI);
1030         if (DbgMI) {
1031           if (!LastOrder)
1032             // Insert to start of the BB (after PHIs).
1033             BB->insert(BBBegin, DbgMI);
1034           else {
1035             // Insert at the instruction, which may be in a different
1036             // block, if the block was split by a custom inserter.
1037             MachineBasicBlock::iterator Pos = MI;
1038             MI->getParent()->insert(Pos, DbgMI);
1039           }
1040         }
1041       }
1042       if (DLI == DLE)
1043         break;
1044 
1045       LastOrder = Order;
1046     }
1047   }
1048 
1049   InsertPos = Emitter.getInsertPos();
1050   // In some cases, DBG_VALUEs might be inserted after the first terminator,
1051   // which results in an invalid MBB. If that happens, move the DBG_VALUEs
1052   // before the first terminator.
1053   MachineBasicBlock *InsertBB = Emitter.getBlock();
1054   auto FirstTerm = InsertBB->getFirstTerminator();
1055   if (FirstTerm != InsertBB->end()) {
1056     assert(!FirstTerm->isDebugValue() &&
1057            "first terminator cannot be a debug value");
1058     for (MachineInstr &MI : make_early_inc_range(
1059              make_range(std::next(FirstTerm), InsertBB->end()))) {
1060       // Only scan up to insertion point.
1061       if (&MI == InsertPos)
1062         break;
1063 
1064       if (!MI.isDebugValue())
1065         continue;
1066 
1067       // The DBG_VALUE was referencing a value produced by a terminator. By
1068       // moving the DBG_VALUE, the referenced value also needs invalidating.
1069       MI.getOperand(0).ChangeToRegister(0, false);
1070       MI.moveBefore(&*FirstTerm);
1071     }
1072   }
1073   return InsertBB;
1074 }
1075 
1076 /// Return the basic block label.
1077 std::string ScheduleDAGSDNodes::getDAGName() const {
1078   return "sunit-dag." + BB->getFullName();
1079 }
1080