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