1f4a2713aSLionel Sambuc //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
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 the ScheduleDAG class, which is a base class used by
11f4a2713aSLionel Sambuc // scheduling implementation classes.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "llvm/CodeGen/ScheduleDAG.h"
16f4a2713aSLionel Sambuc #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
17f4a2713aSLionel Sambuc #include "llvm/CodeGen/SelectionDAGNodes.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
21f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
22f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
23f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
25f4a2713aSLionel Sambuc #include <climits>
26f4a2713aSLionel Sambuc using namespace llvm;
27f4a2713aSLionel Sambuc
28*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "pre-RA-sched"
29*0a6a1f1dSLionel Sambuc
30f4a2713aSLionel Sambuc #ifndef NDEBUG
31f4a2713aSLionel Sambuc static cl::opt<bool> StressSchedOpt(
32f4a2713aSLionel Sambuc "stress-sched", cl::Hidden, cl::init(false),
33f4a2713aSLionel Sambuc cl::desc("Stress test instruction scheduling"));
34f4a2713aSLionel Sambuc #endif
35f4a2713aSLionel Sambuc
anchor()36f4a2713aSLionel Sambuc void SchedulingPriorityQueue::anchor() { }
37f4a2713aSLionel Sambuc
ScheduleDAG(MachineFunction & mf)38f4a2713aSLionel Sambuc ScheduleDAG::ScheduleDAG(MachineFunction &mf)
39*0a6a1f1dSLionel Sambuc : TM(mf.getTarget()), TII(TM.getSubtargetImpl()->getInstrInfo()),
40*0a6a1f1dSLionel Sambuc TRI(TM.getSubtargetImpl()->getRegisterInfo()), MF(mf),
41*0a6a1f1dSLionel Sambuc MRI(mf.getRegInfo()), EntrySU(), ExitSU() {
42f4a2713aSLionel Sambuc #ifndef NDEBUG
43f4a2713aSLionel Sambuc StressSched = StressSchedOpt;
44f4a2713aSLionel Sambuc #endif
45f4a2713aSLionel Sambuc }
46f4a2713aSLionel Sambuc
~ScheduleDAG()47f4a2713aSLionel Sambuc ScheduleDAG::~ScheduleDAG() {}
48f4a2713aSLionel Sambuc
49f4a2713aSLionel Sambuc /// Clear the DAG state (e.g. between scheduling regions).
clearDAG()50f4a2713aSLionel Sambuc void ScheduleDAG::clearDAG() {
51f4a2713aSLionel Sambuc SUnits.clear();
52f4a2713aSLionel Sambuc EntrySU = SUnit();
53f4a2713aSLionel Sambuc ExitSU = SUnit();
54f4a2713aSLionel Sambuc }
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc /// getInstrDesc helper to handle SDNodes.
getNodeDesc(const SDNode * Node) const57f4a2713aSLionel Sambuc const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
58*0a6a1f1dSLionel Sambuc if (!Node || !Node->isMachineOpcode()) return nullptr;
59f4a2713aSLionel Sambuc return &TII->get(Node->getMachineOpcode());
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc /// addPred - This adds the specified edge as a pred of the current node if
63f4a2713aSLionel Sambuc /// not already. It also adds the current node as a successor of the
64f4a2713aSLionel Sambuc /// specified node.
addPred(const SDep & D,bool Required)65f4a2713aSLionel Sambuc bool SUnit::addPred(const SDep &D, bool Required) {
66*0a6a1f1dSLionel Sambuc // If this node already has this dependence, don't add a redundant one.
67f4a2713aSLionel Sambuc for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
68f4a2713aSLionel Sambuc I != E; ++I) {
69f4a2713aSLionel Sambuc // Zero-latency weak edges may be added purely for heuristic ordering. Don't
70f4a2713aSLionel Sambuc // add them if another kind of edge already exists.
71f4a2713aSLionel Sambuc if (!Required && I->getSUnit() == D.getSUnit())
72f4a2713aSLionel Sambuc return false;
73f4a2713aSLionel Sambuc if (I->overlaps(D)) {
74f4a2713aSLionel Sambuc // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
75f4a2713aSLionel Sambuc if (I->getLatency() < D.getLatency()) {
76f4a2713aSLionel Sambuc SUnit *PredSU = I->getSUnit();
77f4a2713aSLionel Sambuc // Find the corresponding successor in N.
78f4a2713aSLionel Sambuc SDep ForwardD = *I;
79f4a2713aSLionel Sambuc ForwardD.setSUnit(this);
80f4a2713aSLionel Sambuc for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
81f4a2713aSLionel Sambuc EE = PredSU->Succs.end(); II != EE; ++II) {
82f4a2713aSLionel Sambuc if (*II == ForwardD) {
83f4a2713aSLionel Sambuc II->setLatency(D.getLatency());
84f4a2713aSLionel Sambuc break;
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc I->setLatency(D.getLatency());
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc return false;
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc // Now add a corresponding succ to N.
93f4a2713aSLionel Sambuc SDep P = D;
94f4a2713aSLionel Sambuc P.setSUnit(this);
95f4a2713aSLionel Sambuc SUnit *N = D.getSUnit();
96f4a2713aSLionel Sambuc // Update the bookkeeping.
97f4a2713aSLionel Sambuc if (D.getKind() == SDep::Data) {
98f4a2713aSLionel Sambuc assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
99f4a2713aSLionel Sambuc assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
100f4a2713aSLionel Sambuc ++NumPreds;
101f4a2713aSLionel Sambuc ++N->NumSuccs;
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc if (!N->isScheduled) {
104f4a2713aSLionel Sambuc if (D.isWeak()) {
105f4a2713aSLionel Sambuc ++WeakPredsLeft;
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc else {
108f4a2713aSLionel Sambuc assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
109f4a2713aSLionel Sambuc ++NumPredsLeft;
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc }
112f4a2713aSLionel Sambuc if (!isScheduled) {
113f4a2713aSLionel Sambuc if (D.isWeak()) {
114f4a2713aSLionel Sambuc ++N->WeakSuccsLeft;
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc else {
117f4a2713aSLionel Sambuc assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
118f4a2713aSLionel Sambuc ++N->NumSuccsLeft;
119f4a2713aSLionel Sambuc }
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc Preds.push_back(D);
122f4a2713aSLionel Sambuc N->Succs.push_back(P);
123f4a2713aSLionel Sambuc if (P.getLatency() != 0) {
124f4a2713aSLionel Sambuc this->setDepthDirty();
125f4a2713aSLionel Sambuc N->setHeightDirty();
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc return true;
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc
130f4a2713aSLionel Sambuc /// removePred - This removes the specified edge as a pred of the current
131f4a2713aSLionel Sambuc /// node if it exists. It also removes the current node as a successor of
132f4a2713aSLionel Sambuc /// the specified node.
removePred(const SDep & D)133f4a2713aSLionel Sambuc void SUnit::removePred(const SDep &D) {
134f4a2713aSLionel Sambuc // Find the matching predecessor.
135f4a2713aSLionel Sambuc for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
136f4a2713aSLionel Sambuc I != E; ++I)
137f4a2713aSLionel Sambuc if (*I == D) {
138f4a2713aSLionel Sambuc // Find the corresponding successor in N.
139f4a2713aSLionel Sambuc SDep P = D;
140f4a2713aSLionel Sambuc P.setSUnit(this);
141f4a2713aSLionel Sambuc SUnit *N = D.getSUnit();
142f4a2713aSLionel Sambuc SmallVectorImpl<SDep>::iterator Succ = std::find(N->Succs.begin(),
143f4a2713aSLionel Sambuc N->Succs.end(), P);
144f4a2713aSLionel Sambuc assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
145f4a2713aSLionel Sambuc N->Succs.erase(Succ);
146f4a2713aSLionel Sambuc Preds.erase(I);
147f4a2713aSLionel Sambuc // Update the bookkeeping.
148f4a2713aSLionel Sambuc if (P.getKind() == SDep::Data) {
149f4a2713aSLionel Sambuc assert(NumPreds > 0 && "NumPreds will underflow!");
150f4a2713aSLionel Sambuc assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
151f4a2713aSLionel Sambuc --NumPreds;
152f4a2713aSLionel Sambuc --N->NumSuccs;
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc if (!N->isScheduled) {
155f4a2713aSLionel Sambuc if (D.isWeak())
156f4a2713aSLionel Sambuc --WeakPredsLeft;
157f4a2713aSLionel Sambuc else {
158f4a2713aSLionel Sambuc assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
159f4a2713aSLionel Sambuc --NumPredsLeft;
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc if (!isScheduled) {
163f4a2713aSLionel Sambuc if (D.isWeak())
164f4a2713aSLionel Sambuc --N->WeakSuccsLeft;
165f4a2713aSLionel Sambuc else {
166f4a2713aSLionel Sambuc assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
167f4a2713aSLionel Sambuc --N->NumSuccsLeft;
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc }
170f4a2713aSLionel Sambuc if (P.getLatency() != 0) {
171f4a2713aSLionel Sambuc this->setDepthDirty();
172f4a2713aSLionel Sambuc N->setHeightDirty();
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc return;
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
setDepthDirty()178f4a2713aSLionel Sambuc void SUnit::setDepthDirty() {
179f4a2713aSLionel Sambuc if (!isDepthCurrent) return;
180f4a2713aSLionel Sambuc SmallVector<SUnit*, 8> WorkList;
181f4a2713aSLionel Sambuc WorkList.push_back(this);
182f4a2713aSLionel Sambuc do {
183f4a2713aSLionel Sambuc SUnit *SU = WorkList.pop_back_val();
184f4a2713aSLionel Sambuc SU->isDepthCurrent = false;
185f4a2713aSLionel Sambuc for (SUnit::const_succ_iterator I = SU->Succs.begin(),
186f4a2713aSLionel Sambuc E = SU->Succs.end(); I != E; ++I) {
187f4a2713aSLionel Sambuc SUnit *SuccSU = I->getSUnit();
188f4a2713aSLionel Sambuc if (SuccSU->isDepthCurrent)
189f4a2713aSLionel Sambuc WorkList.push_back(SuccSU);
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc } while (!WorkList.empty());
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc
setHeightDirty()194f4a2713aSLionel Sambuc void SUnit::setHeightDirty() {
195f4a2713aSLionel Sambuc if (!isHeightCurrent) return;
196f4a2713aSLionel Sambuc SmallVector<SUnit*, 8> WorkList;
197f4a2713aSLionel Sambuc WorkList.push_back(this);
198f4a2713aSLionel Sambuc do {
199f4a2713aSLionel Sambuc SUnit *SU = WorkList.pop_back_val();
200f4a2713aSLionel Sambuc SU->isHeightCurrent = false;
201f4a2713aSLionel Sambuc for (SUnit::const_pred_iterator I = SU->Preds.begin(),
202f4a2713aSLionel Sambuc E = SU->Preds.end(); I != E; ++I) {
203f4a2713aSLionel Sambuc SUnit *PredSU = I->getSUnit();
204f4a2713aSLionel Sambuc if (PredSU->isHeightCurrent)
205f4a2713aSLionel Sambuc WorkList.push_back(PredSU);
206f4a2713aSLionel Sambuc }
207f4a2713aSLionel Sambuc } while (!WorkList.empty());
208f4a2713aSLionel Sambuc }
209f4a2713aSLionel Sambuc
210f4a2713aSLionel Sambuc /// setDepthToAtLeast - Update this node's successors to reflect the
211f4a2713aSLionel Sambuc /// fact that this node's depth just increased.
212f4a2713aSLionel Sambuc ///
setDepthToAtLeast(unsigned NewDepth)213f4a2713aSLionel Sambuc void SUnit::setDepthToAtLeast(unsigned NewDepth) {
214f4a2713aSLionel Sambuc if (NewDepth <= getDepth())
215f4a2713aSLionel Sambuc return;
216f4a2713aSLionel Sambuc setDepthDirty();
217f4a2713aSLionel Sambuc Depth = NewDepth;
218f4a2713aSLionel Sambuc isDepthCurrent = true;
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc
221f4a2713aSLionel Sambuc /// setHeightToAtLeast - Update this node's predecessors to reflect the
222f4a2713aSLionel Sambuc /// fact that this node's height just increased.
223f4a2713aSLionel Sambuc ///
setHeightToAtLeast(unsigned NewHeight)224f4a2713aSLionel Sambuc void SUnit::setHeightToAtLeast(unsigned NewHeight) {
225f4a2713aSLionel Sambuc if (NewHeight <= getHeight())
226f4a2713aSLionel Sambuc return;
227f4a2713aSLionel Sambuc setHeightDirty();
228f4a2713aSLionel Sambuc Height = NewHeight;
229f4a2713aSLionel Sambuc isHeightCurrent = true;
230f4a2713aSLionel Sambuc }
231f4a2713aSLionel Sambuc
232f4a2713aSLionel Sambuc /// ComputeDepth - Calculate the maximal path from the node to the exit.
233f4a2713aSLionel Sambuc ///
ComputeDepth()234f4a2713aSLionel Sambuc void SUnit::ComputeDepth() {
235f4a2713aSLionel Sambuc SmallVector<SUnit*, 8> WorkList;
236f4a2713aSLionel Sambuc WorkList.push_back(this);
237f4a2713aSLionel Sambuc do {
238f4a2713aSLionel Sambuc SUnit *Cur = WorkList.back();
239f4a2713aSLionel Sambuc
240f4a2713aSLionel Sambuc bool Done = true;
241f4a2713aSLionel Sambuc unsigned MaxPredDepth = 0;
242f4a2713aSLionel Sambuc for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
243f4a2713aSLionel Sambuc E = Cur->Preds.end(); I != E; ++I) {
244f4a2713aSLionel Sambuc SUnit *PredSU = I->getSUnit();
245f4a2713aSLionel Sambuc if (PredSU->isDepthCurrent)
246f4a2713aSLionel Sambuc MaxPredDepth = std::max(MaxPredDepth,
247f4a2713aSLionel Sambuc PredSU->Depth + I->getLatency());
248f4a2713aSLionel Sambuc else {
249f4a2713aSLionel Sambuc Done = false;
250f4a2713aSLionel Sambuc WorkList.push_back(PredSU);
251f4a2713aSLionel Sambuc }
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc if (Done) {
255f4a2713aSLionel Sambuc WorkList.pop_back();
256f4a2713aSLionel Sambuc if (MaxPredDepth != Cur->Depth) {
257f4a2713aSLionel Sambuc Cur->setDepthDirty();
258f4a2713aSLionel Sambuc Cur->Depth = MaxPredDepth;
259f4a2713aSLionel Sambuc }
260f4a2713aSLionel Sambuc Cur->isDepthCurrent = true;
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc } while (!WorkList.empty());
263f4a2713aSLionel Sambuc }
264f4a2713aSLionel Sambuc
265f4a2713aSLionel Sambuc /// ComputeHeight - Calculate the maximal path from the node to the entry.
266f4a2713aSLionel Sambuc ///
ComputeHeight()267f4a2713aSLionel Sambuc void SUnit::ComputeHeight() {
268f4a2713aSLionel Sambuc SmallVector<SUnit*, 8> WorkList;
269f4a2713aSLionel Sambuc WorkList.push_back(this);
270f4a2713aSLionel Sambuc do {
271f4a2713aSLionel Sambuc SUnit *Cur = WorkList.back();
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc bool Done = true;
274f4a2713aSLionel Sambuc unsigned MaxSuccHeight = 0;
275f4a2713aSLionel Sambuc for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
276f4a2713aSLionel Sambuc E = Cur->Succs.end(); I != E; ++I) {
277f4a2713aSLionel Sambuc SUnit *SuccSU = I->getSUnit();
278f4a2713aSLionel Sambuc if (SuccSU->isHeightCurrent)
279f4a2713aSLionel Sambuc MaxSuccHeight = std::max(MaxSuccHeight,
280f4a2713aSLionel Sambuc SuccSU->Height + I->getLatency());
281f4a2713aSLionel Sambuc else {
282f4a2713aSLionel Sambuc Done = false;
283f4a2713aSLionel Sambuc WorkList.push_back(SuccSU);
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc }
286f4a2713aSLionel Sambuc
287f4a2713aSLionel Sambuc if (Done) {
288f4a2713aSLionel Sambuc WorkList.pop_back();
289f4a2713aSLionel Sambuc if (MaxSuccHeight != Cur->Height) {
290f4a2713aSLionel Sambuc Cur->setHeightDirty();
291f4a2713aSLionel Sambuc Cur->Height = MaxSuccHeight;
292f4a2713aSLionel Sambuc }
293f4a2713aSLionel Sambuc Cur->isHeightCurrent = true;
294f4a2713aSLionel Sambuc }
295f4a2713aSLionel Sambuc } while (!WorkList.empty());
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc
biasCriticalPath()298f4a2713aSLionel Sambuc void SUnit::biasCriticalPath() {
299f4a2713aSLionel Sambuc if (NumPreds < 2)
300f4a2713aSLionel Sambuc return;
301f4a2713aSLionel Sambuc
302f4a2713aSLionel Sambuc SUnit::pred_iterator BestI = Preds.begin();
303f4a2713aSLionel Sambuc unsigned MaxDepth = BestI->getSUnit()->getDepth();
304*0a6a1f1dSLionel Sambuc for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
305*0a6a1f1dSLionel Sambuc ++I) {
306f4a2713aSLionel Sambuc if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
307f4a2713aSLionel Sambuc BestI = I;
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc if (BestI != Preds.begin())
310f4a2713aSLionel Sambuc std::swap(*Preds.begin(), *BestI);
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
314f4a2713aSLionel Sambuc /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
315f4a2713aSLionel Sambuc /// a group of nodes flagged together.
dump(const ScheduleDAG * G) const316f4a2713aSLionel Sambuc void SUnit::dump(const ScheduleDAG *G) const {
317f4a2713aSLionel Sambuc dbgs() << "SU(" << NodeNum << "): ";
318f4a2713aSLionel Sambuc G->dumpNode(this);
319f4a2713aSLionel Sambuc }
320f4a2713aSLionel Sambuc
dumpAll(const ScheduleDAG * G) const321f4a2713aSLionel Sambuc void SUnit::dumpAll(const ScheduleDAG *G) const {
322f4a2713aSLionel Sambuc dump(G);
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc dbgs() << " # preds left : " << NumPredsLeft << "\n";
325f4a2713aSLionel Sambuc dbgs() << " # succs left : " << NumSuccsLeft << "\n";
326f4a2713aSLionel Sambuc if (WeakPredsLeft)
327f4a2713aSLionel Sambuc dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
328f4a2713aSLionel Sambuc if (WeakSuccsLeft)
329f4a2713aSLionel Sambuc dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
330f4a2713aSLionel Sambuc dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
331f4a2713aSLionel Sambuc dbgs() << " Latency : " << Latency << "\n";
332f4a2713aSLionel Sambuc dbgs() << " Depth : " << getDepth() << "\n";
333f4a2713aSLionel Sambuc dbgs() << " Height : " << getHeight() << "\n";
334f4a2713aSLionel Sambuc
335f4a2713aSLionel Sambuc if (Preds.size() != 0) {
336f4a2713aSLionel Sambuc dbgs() << " Predecessors:\n";
337f4a2713aSLionel Sambuc for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
338f4a2713aSLionel Sambuc I != E; ++I) {
339f4a2713aSLionel Sambuc dbgs() << " ";
340f4a2713aSLionel Sambuc switch (I->getKind()) {
341f4a2713aSLionel Sambuc case SDep::Data: dbgs() << "val "; break;
342f4a2713aSLionel Sambuc case SDep::Anti: dbgs() << "anti"; break;
343f4a2713aSLionel Sambuc case SDep::Output: dbgs() << "out "; break;
344f4a2713aSLionel Sambuc case SDep::Order: dbgs() << "ch "; break;
345f4a2713aSLionel Sambuc }
346f4a2713aSLionel Sambuc dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
347f4a2713aSLionel Sambuc if (I->isArtificial())
348f4a2713aSLionel Sambuc dbgs() << " *";
349f4a2713aSLionel Sambuc dbgs() << ": Latency=" << I->getLatency();
350f4a2713aSLionel Sambuc if (I->isAssignedRegDep())
351f4a2713aSLionel Sambuc dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
352f4a2713aSLionel Sambuc dbgs() << "\n";
353f4a2713aSLionel Sambuc }
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc if (Succs.size() != 0) {
356f4a2713aSLionel Sambuc dbgs() << " Successors:\n";
357f4a2713aSLionel Sambuc for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
358f4a2713aSLionel Sambuc I != E; ++I) {
359f4a2713aSLionel Sambuc dbgs() << " ";
360f4a2713aSLionel Sambuc switch (I->getKind()) {
361f4a2713aSLionel Sambuc case SDep::Data: dbgs() << "val "; break;
362f4a2713aSLionel Sambuc case SDep::Anti: dbgs() << "anti"; break;
363f4a2713aSLionel Sambuc case SDep::Output: dbgs() << "out "; break;
364f4a2713aSLionel Sambuc case SDep::Order: dbgs() << "ch "; break;
365f4a2713aSLionel Sambuc }
366f4a2713aSLionel Sambuc dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
367f4a2713aSLionel Sambuc if (I->isArtificial())
368f4a2713aSLionel Sambuc dbgs() << " *";
369f4a2713aSLionel Sambuc dbgs() << ": Latency=" << I->getLatency();
370f4a2713aSLionel Sambuc if (I->isAssignedRegDep())
371f4a2713aSLionel Sambuc dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
372f4a2713aSLionel Sambuc dbgs() << "\n";
373f4a2713aSLionel Sambuc }
374f4a2713aSLionel Sambuc }
375f4a2713aSLionel Sambuc dbgs() << "\n";
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc #endif
378f4a2713aSLionel Sambuc
379f4a2713aSLionel Sambuc #ifndef NDEBUG
380f4a2713aSLionel Sambuc /// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
381f4a2713aSLionel Sambuc /// their state is consistent. Return the number of scheduled nodes.
382f4a2713aSLionel Sambuc ///
VerifyScheduledDAG(bool isBottomUp)383f4a2713aSLionel Sambuc unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
384f4a2713aSLionel Sambuc bool AnyNotSched = false;
385f4a2713aSLionel Sambuc unsigned DeadNodes = 0;
386f4a2713aSLionel Sambuc for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
387f4a2713aSLionel Sambuc if (!SUnits[i].isScheduled) {
388f4a2713aSLionel Sambuc if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
389f4a2713aSLionel Sambuc ++DeadNodes;
390f4a2713aSLionel Sambuc continue;
391f4a2713aSLionel Sambuc }
392f4a2713aSLionel Sambuc if (!AnyNotSched)
393f4a2713aSLionel Sambuc dbgs() << "*** Scheduling failed! ***\n";
394f4a2713aSLionel Sambuc SUnits[i].dump(this);
395f4a2713aSLionel Sambuc dbgs() << "has not been scheduled!\n";
396f4a2713aSLionel Sambuc AnyNotSched = true;
397f4a2713aSLionel Sambuc }
398f4a2713aSLionel Sambuc if (SUnits[i].isScheduled &&
399f4a2713aSLionel Sambuc (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
400f4a2713aSLionel Sambuc unsigned(INT_MAX)) {
401f4a2713aSLionel Sambuc if (!AnyNotSched)
402f4a2713aSLionel Sambuc dbgs() << "*** Scheduling failed! ***\n";
403f4a2713aSLionel Sambuc SUnits[i].dump(this);
404f4a2713aSLionel Sambuc dbgs() << "has an unexpected "
405f4a2713aSLionel Sambuc << (isBottomUp ? "Height" : "Depth") << " value!\n";
406f4a2713aSLionel Sambuc AnyNotSched = true;
407f4a2713aSLionel Sambuc }
408f4a2713aSLionel Sambuc if (isBottomUp) {
409f4a2713aSLionel Sambuc if (SUnits[i].NumSuccsLeft != 0) {
410f4a2713aSLionel Sambuc if (!AnyNotSched)
411f4a2713aSLionel Sambuc dbgs() << "*** Scheduling failed! ***\n";
412f4a2713aSLionel Sambuc SUnits[i].dump(this);
413f4a2713aSLionel Sambuc dbgs() << "has successors left!\n";
414f4a2713aSLionel Sambuc AnyNotSched = true;
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc } else {
417f4a2713aSLionel Sambuc if (SUnits[i].NumPredsLeft != 0) {
418f4a2713aSLionel Sambuc if (!AnyNotSched)
419f4a2713aSLionel Sambuc dbgs() << "*** Scheduling failed! ***\n";
420f4a2713aSLionel Sambuc SUnits[i].dump(this);
421f4a2713aSLionel Sambuc dbgs() << "has predecessors left!\n";
422f4a2713aSLionel Sambuc AnyNotSched = true;
423f4a2713aSLionel Sambuc }
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc }
426f4a2713aSLionel Sambuc assert(!AnyNotSched);
427f4a2713aSLionel Sambuc return SUnits.size() - DeadNodes;
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc #endif
430f4a2713aSLionel Sambuc
431f4a2713aSLionel Sambuc /// InitDAGTopologicalSorting - create the initial topological
432f4a2713aSLionel Sambuc /// ordering from the DAG to be scheduled.
433f4a2713aSLionel Sambuc ///
434f4a2713aSLionel Sambuc /// The idea of the algorithm is taken from
435f4a2713aSLionel Sambuc /// "Online algorithms for managing the topological order of
436f4a2713aSLionel Sambuc /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
437f4a2713aSLionel Sambuc /// This is the MNR algorithm, which was first introduced by
438f4a2713aSLionel Sambuc /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
439f4a2713aSLionel Sambuc /// "Maintaining a topological order under edge insertions".
440f4a2713aSLionel Sambuc ///
441f4a2713aSLionel Sambuc /// Short description of the algorithm:
442f4a2713aSLionel Sambuc ///
443f4a2713aSLionel Sambuc /// Topological ordering, ord, of a DAG maps each node to a topological
444f4a2713aSLionel Sambuc /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
445f4a2713aSLionel Sambuc ///
446f4a2713aSLionel Sambuc /// This means that if there is a path from the node X to the node Z,
447f4a2713aSLionel Sambuc /// then ord(X) < ord(Z).
448f4a2713aSLionel Sambuc ///
449f4a2713aSLionel Sambuc /// This property can be used to check for reachability of nodes:
450f4a2713aSLionel Sambuc /// if Z is reachable from X, then an insertion of the edge Z->X would
451f4a2713aSLionel Sambuc /// create a cycle.
452f4a2713aSLionel Sambuc ///
453f4a2713aSLionel Sambuc /// The algorithm first computes a topological ordering for the DAG by
454f4a2713aSLionel Sambuc /// initializing the Index2Node and Node2Index arrays and then tries to keep
455f4a2713aSLionel Sambuc /// the ordering up-to-date after edge insertions by reordering the DAG.
456f4a2713aSLionel Sambuc ///
457f4a2713aSLionel Sambuc /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
458f4a2713aSLionel Sambuc /// the nodes reachable from Y, and then shifts them using Shift to lie
459f4a2713aSLionel Sambuc /// immediately after X in Index2Node.
InitDAGTopologicalSorting()460f4a2713aSLionel Sambuc void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
461f4a2713aSLionel Sambuc unsigned DAGSize = SUnits.size();
462f4a2713aSLionel Sambuc std::vector<SUnit*> WorkList;
463f4a2713aSLionel Sambuc WorkList.reserve(DAGSize);
464f4a2713aSLionel Sambuc
465f4a2713aSLionel Sambuc Index2Node.resize(DAGSize);
466f4a2713aSLionel Sambuc Node2Index.resize(DAGSize);
467f4a2713aSLionel Sambuc
468f4a2713aSLionel Sambuc // Initialize the data structures.
469f4a2713aSLionel Sambuc if (ExitSU)
470f4a2713aSLionel Sambuc WorkList.push_back(ExitSU);
471f4a2713aSLionel Sambuc for (unsigned i = 0, e = DAGSize; i != e; ++i) {
472f4a2713aSLionel Sambuc SUnit *SU = &SUnits[i];
473f4a2713aSLionel Sambuc int NodeNum = SU->NodeNum;
474f4a2713aSLionel Sambuc unsigned Degree = SU->Succs.size();
475f4a2713aSLionel Sambuc // Temporarily use the Node2Index array as scratch space for degree counts.
476f4a2713aSLionel Sambuc Node2Index[NodeNum] = Degree;
477f4a2713aSLionel Sambuc
478f4a2713aSLionel Sambuc // Is it a node without dependencies?
479f4a2713aSLionel Sambuc if (Degree == 0) {
480f4a2713aSLionel Sambuc assert(SU->Succs.empty() && "SUnit should have no successors");
481f4a2713aSLionel Sambuc // Collect leaf nodes.
482f4a2713aSLionel Sambuc WorkList.push_back(SU);
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc }
485f4a2713aSLionel Sambuc
486f4a2713aSLionel Sambuc int Id = DAGSize;
487f4a2713aSLionel Sambuc while (!WorkList.empty()) {
488f4a2713aSLionel Sambuc SUnit *SU = WorkList.back();
489f4a2713aSLionel Sambuc WorkList.pop_back();
490f4a2713aSLionel Sambuc if (SU->NodeNum < DAGSize)
491f4a2713aSLionel Sambuc Allocate(SU->NodeNum, --Id);
492f4a2713aSLionel Sambuc for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
493f4a2713aSLionel Sambuc I != E; ++I) {
494f4a2713aSLionel Sambuc SUnit *SU = I->getSUnit();
495f4a2713aSLionel Sambuc if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
496f4a2713aSLionel Sambuc // If all dependencies of the node are processed already,
497f4a2713aSLionel Sambuc // then the node can be computed now.
498f4a2713aSLionel Sambuc WorkList.push_back(SU);
499f4a2713aSLionel Sambuc }
500f4a2713aSLionel Sambuc }
501f4a2713aSLionel Sambuc
502f4a2713aSLionel Sambuc Visited.resize(DAGSize);
503f4a2713aSLionel Sambuc
504f4a2713aSLionel Sambuc #ifndef NDEBUG
505f4a2713aSLionel Sambuc // Check correctness of the ordering
506f4a2713aSLionel Sambuc for (unsigned i = 0, e = DAGSize; i != e; ++i) {
507f4a2713aSLionel Sambuc SUnit *SU = &SUnits[i];
508f4a2713aSLionel Sambuc for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
509f4a2713aSLionel Sambuc I != E; ++I) {
510f4a2713aSLionel Sambuc assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
511f4a2713aSLionel Sambuc "Wrong topological sorting");
512f4a2713aSLionel Sambuc }
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc #endif
515f4a2713aSLionel Sambuc }
516f4a2713aSLionel Sambuc
517f4a2713aSLionel Sambuc /// AddPred - Updates the topological ordering to accommodate an edge
518f4a2713aSLionel Sambuc /// to be added from SUnit X to SUnit Y.
AddPred(SUnit * Y,SUnit * X)519f4a2713aSLionel Sambuc void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
520f4a2713aSLionel Sambuc int UpperBound, LowerBound;
521f4a2713aSLionel Sambuc LowerBound = Node2Index[Y->NodeNum];
522f4a2713aSLionel Sambuc UpperBound = Node2Index[X->NodeNum];
523f4a2713aSLionel Sambuc bool HasLoop = false;
524f4a2713aSLionel Sambuc // Is Ord(X) < Ord(Y) ?
525f4a2713aSLionel Sambuc if (LowerBound < UpperBound) {
526f4a2713aSLionel Sambuc // Update the topological order.
527f4a2713aSLionel Sambuc Visited.reset();
528f4a2713aSLionel Sambuc DFS(Y, UpperBound, HasLoop);
529f4a2713aSLionel Sambuc assert(!HasLoop && "Inserted edge creates a loop!");
530f4a2713aSLionel Sambuc // Recompute topological indexes.
531f4a2713aSLionel Sambuc Shift(Visited, LowerBound, UpperBound);
532f4a2713aSLionel Sambuc }
533f4a2713aSLionel Sambuc }
534f4a2713aSLionel Sambuc
535f4a2713aSLionel Sambuc /// RemovePred - Updates the topological ordering to accommodate an
536f4a2713aSLionel Sambuc /// an edge to be removed from the specified node N from the predecessors
537f4a2713aSLionel Sambuc /// of the current node M.
RemovePred(SUnit * M,SUnit * N)538f4a2713aSLionel Sambuc void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
539f4a2713aSLionel Sambuc // InitDAGTopologicalSorting();
540f4a2713aSLionel Sambuc }
541f4a2713aSLionel Sambuc
542f4a2713aSLionel Sambuc /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
543f4a2713aSLionel Sambuc /// all nodes affected by the edge insertion. These nodes will later get new
544f4a2713aSLionel Sambuc /// topological indexes by means of the Shift method.
DFS(const SUnit * SU,int UpperBound,bool & HasLoop)545f4a2713aSLionel Sambuc void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
546f4a2713aSLionel Sambuc bool &HasLoop) {
547f4a2713aSLionel Sambuc std::vector<const SUnit*> WorkList;
548f4a2713aSLionel Sambuc WorkList.reserve(SUnits.size());
549f4a2713aSLionel Sambuc
550f4a2713aSLionel Sambuc WorkList.push_back(SU);
551f4a2713aSLionel Sambuc do {
552f4a2713aSLionel Sambuc SU = WorkList.back();
553f4a2713aSLionel Sambuc WorkList.pop_back();
554f4a2713aSLionel Sambuc Visited.set(SU->NodeNum);
555f4a2713aSLionel Sambuc for (int I = SU->Succs.size()-1; I >= 0; --I) {
556f4a2713aSLionel Sambuc unsigned s = SU->Succs[I].getSUnit()->NodeNum;
557f4a2713aSLionel Sambuc // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
558f4a2713aSLionel Sambuc if (s >= Node2Index.size())
559f4a2713aSLionel Sambuc continue;
560f4a2713aSLionel Sambuc if (Node2Index[s] == UpperBound) {
561f4a2713aSLionel Sambuc HasLoop = true;
562f4a2713aSLionel Sambuc return;
563f4a2713aSLionel Sambuc }
564f4a2713aSLionel Sambuc // Visit successors if not already and in affected region.
565f4a2713aSLionel Sambuc if (!Visited.test(s) && Node2Index[s] < UpperBound) {
566f4a2713aSLionel Sambuc WorkList.push_back(SU->Succs[I].getSUnit());
567f4a2713aSLionel Sambuc }
568f4a2713aSLionel Sambuc }
569f4a2713aSLionel Sambuc } while (!WorkList.empty());
570f4a2713aSLionel Sambuc }
571f4a2713aSLionel Sambuc
572f4a2713aSLionel Sambuc /// Shift - Renumber the nodes so that the topological ordering is
573f4a2713aSLionel Sambuc /// preserved.
Shift(BitVector & Visited,int LowerBound,int UpperBound)574f4a2713aSLionel Sambuc void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
575f4a2713aSLionel Sambuc int UpperBound) {
576f4a2713aSLionel Sambuc std::vector<int> L;
577f4a2713aSLionel Sambuc int shift = 0;
578f4a2713aSLionel Sambuc int i;
579f4a2713aSLionel Sambuc
580f4a2713aSLionel Sambuc for (i = LowerBound; i <= UpperBound; ++i) {
581f4a2713aSLionel Sambuc // w is node at topological index i.
582f4a2713aSLionel Sambuc int w = Index2Node[i];
583f4a2713aSLionel Sambuc if (Visited.test(w)) {
584f4a2713aSLionel Sambuc // Unmark.
585f4a2713aSLionel Sambuc Visited.reset(w);
586f4a2713aSLionel Sambuc L.push_back(w);
587f4a2713aSLionel Sambuc shift = shift + 1;
588f4a2713aSLionel Sambuc } else {
589f4a2713aSLionel Sambuc Allocate(w, i - shift);
590f4a2713aSLionel Sambuc }
591f4a2713aSLionel Sambuc }
592f4a2713aSLionel Sambuc
593f4a2713aSLionel Sambuc for (unsigned j = 0; j < L.size(); ++j) {
594f4a2713aSLionel Sambuc Allocate(L[j], i - shift);
595f4a2713aSLionel Sambuc i = i + 1;
596f4a2713aSLionel Sambuc }
597f4a2713aSLionel Sambuc }
598f4a2713aSLionel Sambuc
599f4a2713aSLionel Sambuc
600f4a2713aSLionel Sambuc /// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
601f4a2713aSLionel Sambuc /// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
WillCreateCycle(SUnit * TargetSU,SUnit * SU)602f4a2713aSLionel Sambuc bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
603f4a2713aSLionel Sambuc // Is SU reachable from TargetSU via successor edges?
604f4a2713aSLionel Sambuc if (IsReachable(SU, TargetSU))
605f4a2713aSLionel Sambuc return true;
606f4a2713aSLionel Sambuc for (SUnit::pred_iterator
607f4a2713aSLionel Sambuc I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
608f4a2713aSLionel Sambuc if (I->isAssignedRegDep() &&
609f4a2713aSLionel Sambuc IsReachable(SU, I->getSUnit()))
610f4a2713aSLionel Sambuc return true;
611f4a2713aSLionel Sambuc return false;
612f4a2713aSLionel Sambuc }
613f4a2713aSLionel Sambuc
614f4a2713aSLionel Sambuc /// IsReachable - Checks if SU is reachable from TargetSU.
IsReachable(const SUnit * SU,const SUnit * TargetSU)615f4a2713aSLionel Sambuc bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
616f4a2713aSLionel Sambuc const SUnit *TargetSU) {
617f4a2713aSLionel Sambuc // If insertion of the edge SU->TargetSU would create a cycle
618f4a2713aSLionel Sambuc // then there is a path from TargetSU to SU.
619f4a2713aSLionel Sambuc int UpperBound, LowerBound;
620f4a2713aSLionel Sambuc LowerBound = Node2Index[TargetSU->NodeNum];
621f4a2713aSLionel Sambuc UpperBound = Node2Index[SU->NodeNum];
622f4a2713aSLionel Sambuc bool HasLoop = false;
623f4a2713aSLionel Sambuc // Is Ord(TargetSU) < Ord(SU) ?
624f4a2713aSLionel Sambuc if (LowerBound < UpperBound) {
625f4a2713aSLionel Sambuc Visited.reset();
626f4a2713aSLionel Sambuc // There may be a path from TargetSU to SU. Check for it.
627f4a2713aSLionel Sambuc DFS(TargetSU, UpperBound, HasLoop);
628f4a2713aSLionel Sambuc }
629f4a2713aSLionel Sambuc return HasLoop;
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc
632f4a2713aSLionel Sambuc /// Allocate - assign the topological index to the node n.
Allocate(int n,int index)633f4a2713aSLionel Sambuc void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
634f4a2713aSLionel Sambuc Node2Index[n] = index;
635f4a2713aSLionel Sambuc Index2Node[index] = n;
636f4a2713aSLionel Sambuc }
637f4a2713aSLionel Sambuc
638f4a2713aSLionel Sambuc ScheduleDAGTopologicalSort::
ScheduleDAGTopologicalSort(std::vector<SUnit> & sunits,SUnit * exitsu)639f4a2713aSLionel Sambuc ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
640f4a2713aSLionel Sambuc : SUnits(sunits), ExitSU(exitsu) {}
641f4a2713aSLionel Sambuc
~ScheduleHazardRecognizer()642f4a2713aSLionel Sambuc ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}
643